dbmodify.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * dpkg - main program for package management
  3. * dbmodify.c - routines for managing dpkg database updates
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2001 Wichert Akkerman <wichert@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <signal.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <errno.h>
  31. #include <unistd.h>
  32. #include <dirent.h>
  33. #include <limits.h>
  34. #include <ctype.h>
  35. #include <time.h>
  36. #include <assert.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. char *statusfile=NULL, *availablefile=NULL;
  40. static enum modstatdb_rw cstatus=-1, cflags=0;
  41. static char *importanttmpfile=NULL;
  42. static FILE *importanttmp;
  43. static int nextupdate;
  44. static int updateslength;
  45. static char *updatefnbuf, *updatefnrest;
  46. static const char *admindir;
  47. static struct varbuf uvb;
  48. static int ulist_select(const struct dirent *de) {
  49. const char *p;
  50. int l;
  51. for (p= de->d_name, l=0; *p; p++, l++)
  52. if (!cisdigit(*p)) return 0;
  53. if (l > IMPORTANTMAXLEN)
  54. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  55. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  56. if (updateslength == -1) updateslength= l;
  57. else if (l != updateslength)
  58. ohshit(_("updates directory contains files with different length names "
  59. "(both %d and %d)"), l, updateslength);
  60. return 1;
  61. }
  62. static void cleanupdates(void) {
  63. struct dirent **cdlist;
  64. int cdn, i;
  65. parsedb(statusfile, pdb_weakclassification, NULL,NULL,NULL);
  66. *updatefnrest= 0;
  67. updateslength= -1;
  68. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  69. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  70. if (cdn) {
  71. for (i=0; i<cdn; i++) {
  72. strcpy(updatefnrest, cdlist[i]->d_name);
  73. parsedb(updatefnbuf, pdb_weakclassification, NULL,NULL,NULL);
  74. if (cstatus < msdbrw_write) free(cdlist[i]);
  75. }
  76. if (cstatus >= msdbrw_write) {
  77. writedb(statusfile,0,1);
  78. for (i=0; i<cdn; i++) {
  79. strcpy(updatefnrest, cdlist[i]->d_name);
  80. if (unlink(updatefnbuf))
  81. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  82. free(cdlist[i]);
  83. }
  84. }
  85. }
  86. free(cdlist);
  87. nextupdate= 0;
  88. }
  89. static void createimptmp(void) {
  90. int i;
  91. onerr_abort++;
  92. importanttmp= fopen(importanttmpfile,"w");
  93. if (!importanttmp)
  94. ohshite(_("unable to create `%.255s'"), importanttmpfile);
  95. setcloexec(fileno(importanttmp),importanttmpfile);
  96. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  97. if (ferror(importanttmp))
  98. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  99. if (fflush(importanttmp))
  100. ohshite(_("unable to flush %.250s after padding"), importanttmpfile);
  101. if (fseek(importanttmp,0,SEEK_SET))
  102. ohshite(_("unable to seek to start of %.250s after padding"),
  103. importanttmpfile);
  104. onerr_abort--;
  105. }
  106. static const struct fni {
  107. const char *suffix;
  108. char **store;
  109. } fnis[] = {
  110. { STATUSFILE, &statusfile },
  111. { AVAILFILE, &availablefile },
  112. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  113. { NULL, NULL }
  114. };
  115. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  116. const struct fni *fnip;
  117. admindir= adir;
  118. for (fnip=fnis; fnip->suffix; fnip++) {
  119. free(*fnip->store);
  120. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  121. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  122. }
  123. cflags= readwritereq & msdbrw_flagsmask;
  124. readwritereq &= ~msdbrw_flagsmask;
  125. switch (readwritereq) {
  126. case msdbrw_needsuperuser:
  127. case msdbrw_needsuperuserlockonly:
  128. if (getuid() || geteuid())
  129. ohshit(_("requested operation requires superuser privilege"));
  130. /* fall through */
  131. case msdbrw_write: case msdbrw_writeifposs:
  132. if (access(adir,W_OK)) {
  133. if (errno != EACCES)
  134. ohshite(_("unable to access dpkg status area"));
  135. else if (readwritereq == msdbrw_write)
  136. ohshit(_("operation requires read/write access to dpkg status area"));
  137. cstatus= msdbrw_readonly;
  138. } else {
  139. lockdatabase(adir);
  140. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  141. msdbrw_needsuperuserlockonly :
  142. msdbrw_write);
  143. }
  144. break;
  145. case msdbrw_readonly:
  146. cstatus= msdbrw_readonly; break;
  147. default:
  148. internerr("unknown readwritereq");
  149. }
  150. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  151. strcpy(updatefnbuf,adir);
  152. strcat(updatefnbuf,"/" UPDATESDIR);
  153. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  154. if (cstatus != msdbrw_needsuperuserlockonly) {
  155. cleanupdates();
  156. if(!(cflags & msdbrw_noavail))
  157. parsedb(availablefile,
  158. pdb_recordavailable|pdb_rejectstatus,
  159. NULL,NULL,NULL);
  160. }
  161. if (cstatus >= msdbrw_write) {
  162. createimptmp();
  163. uvb.used= 0;
  164. uvb.size= 10240;
  165. uvb.buf= m_malloc(uvb.size);
  166. }
  167. return cstatus;
  168. }
  169. static void checkpoint(void) {
  170. int i;
  171. assert(cstatus >= msdbrw_write);
  172. writedb(statusfile,0,1);
  173. for (i=0; i<nextupdate; i++) {
  174. sprintf(updatefnrest, IMPORTANTFMT, i);
  175. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  176. if (unlink(updatefnbuf))
  177. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  178. }
  179. nextupdate= 0;
  180. }
  181. void modstatdb_shutdown(void) {
  182. const struct fni *fnip;
  183. switch (cstatus) {
  184. case msdbrw_write:
  185. checkpoint();
  186. writedb(availablefile,1,0);
  187. /* tidy up a bit, but don't worry too much about failure */
  188. fclose(importanttmp);
  189. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  190. varbuffree(&uvb);
  191. /* fall through */
  192. case msdbrw_needsuperuserlockonly:
  193. unlockdatabase(admindir);
  194. default:
  195. break;
  196. }
  197. for (fnip=fnis; fnip->suffix; fnip++) {
  198. free(*fnip->store);
  199. *fnip->store= NULL;
  200. }
  201. free(updatefnbuf);
  202. }
  203. struct pipef *status_pipes= NULL;
  204. void modstatdb_note(struct pkginfo *pkg) {
  205. assert(cstatus >= msdbrw_write);
  206. onerr_abort++;
  207. if (status_pipes) {
  208. static struct varbuf *status= NULL;
  209. struct pipef *pipef= status_pipes;
  210. int r;
  211. if (status == NULL) {
  212. status = nfmalloc(sizeof(struct varbuf));
  213. varbufinit(status);
  214. } else
  215. varbufreset(status);
  216. r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name);
  217. while (pipef) {
  218. write(pipef->fd, status->buf, r);
  219. pipef= pipef->next;
  220. }
  221. }
  222. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
  223. versiondescribe(&pkg->installed.version, vdew_nonambig));
  224. varbufreset(&uvb);
  225. varbufrecord(&uvb, pkg, &pkg->installed);
  226. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  227. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  228. if (fflush(importanttmp))
  229. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  230. if (ftruncate(fileno(importanttmp), uvb.used))
  231. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  232. if (fsync(fileno(importanttmp)))
  233. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  234. if (fclose(importanttmp))
  235. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  236. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  237. if (rename(importanttmpfile, updatefnbuf))
  238. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  239. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  240. nextupdate++;
  241. if (nextupdate > MAXUPDATES) {
  242. checkpoint();
  243. nextupdate= 0;
  244. }
  245. createimptmp();
  246. onerr_abort--;
  247. }
  248. const char *log_file= NULL;
  249. void log_message(const char *fmt, ...) {
  250. static struct varbuf *log= NULL;
  251. static FILE *logfd= NULL;
  252. char time_str[20];
  253. time_t now;
  254. va_list al;
  255. if (!log_file)
  256. return;
  257. if (!logfd) {
  258. logfd= fopen(log_file, "a");
  259. if (!logfd) {
  260. fprintf(stderr, _("couldn't open log `%s': %s\n"), log_file,
  261. strerror(errno));
  262. log_file= NULL;
  263. return;
  264. }
  265. setlinebuf(logfd);
  266. setcloexec(fileno(logfd), log_file);
  267. }
  268. if (!log) {
  269. log= nfmalloc(sizeof(struct varbuf));
  270. varbufinit(log);
  271. } else
  272. varbufreset(log);
  273. va_start(al,fmt);
  274. varbufvprintf(log, fmt, al);
  275. varbufaddc(log, 0);
  276. va_end(al);
  277. time(&now);
  278. strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));
  279. fprintf(logfd, "%s %s\n", time_str, log->buf);
  280. }