dbmodify.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. const struct fni { const char *suffix; char **store; } fnis[]= {
  107. { STATUSFILE, &statusfile },
  108. { AVAILFILE, &availablefile },
  109. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  110. { NULL, NULL }
  111. };
  112. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  113. const struct fni *fnip;
  114. admindir= adir;
  115. for (fnip=fnis; fnip->suffix; fnip++) {
  116. free(*fnip->store);
  117. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  118. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  119. }
  120. cflags= readwritereq & msdbrw_flagsmask;
  121. readwritereq &= ~msdbrw_flagsmask;
  122. switch (readwritereq) {
  123. case msdbrw_needsuperuser:
  124. case msdbrw_needsuperuserlockonly:
  125. if (getuid() || geteuid())
  126. ohshit(_("requested operation requires superuser privilege"));
  127. /* fall through */
  128. case msdbrw_write: case msdbrw_writeifposs:
  129. if (access(adir,W_OK)) {
  130. if (errno != EACCES)
  131. ohshite(_("unable to access dpkg status area"));
  132. else if (readwritereq == msdbrw_write)
  133. ohshit(_("operation requires read/write access to dpkg status area"));
  134. cstatus= msdbrw_readonly;
  135. } else {
  136. lockdatabase(adir);
  137. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  138. msdbrw_needsuperuserlockonly :
  139. msdbrw_write);
  140. }
  141. break;
  142. case msdbrw_readonly:
  143. cstatus= msdbrw_readonly; break;
  144. default:
  145. internerr("unknown readwritereq");
  146. }
  147. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  148. strcpy(updatefnbuf,adir);
  149. strcat(updatefnbuf,"/" UPDATESDIR);
  150. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  151. if (cstatus != msdbrw_needsuperuserlockonly) {
  152. cleanupdates();
  153. if(!(cflags & msdbrw_noavail))
  154. parsedb(availablefile,
  155. pdb_recordavailable|pdb_rejectstatus,
  156. NULL,NULL,NULL);
  157. }
  158. if (cstatus >= msdbrw_write) {
  159. createimptmp();
  160. uvb.used= 0;
  161. uvb.size= 10240;
  162. uvb.buf= m_malloc(uvb.size);
  163. }
  164. return cstatus;
  165. }
  166. static void checkpoint(void) {
  167. int i;
  168. assert(cstatus >= msdbrw_write);
  169. writedb(statusfile,0,1);
  170. for (i=0; i<nextupdate; i++) {
  171. sprintf(updatefnrest, IMPORTANTFMT, i);
  172. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  173. if (unlink(updatefnbuf))
  174. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  175. }
  176. nextupdate= 0;
  177. }
  178. void modstatdb_shutdown(void) {
  179. const struct fni *fnip;
  180. switch (cstatus) {
  181. case msdbrw_write:
  182. checkpoint();
  183. writedb(availablefile,1,0);
  184. /* tidy up a bit, but don't worry too much about failure */
  185. fclose(importanttmp);
  186. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  187. varbuffree(&uvb);
  188. /* fall through */
  189. case msdbrw_needsuperuserlockonly:
  190. unlockdatabase(admindir);
  191. default:
  192. break;
  193. }
  194. for (fnip=fnis; fnip->suffix; fnip++) {
  195. free(*fnip->store);
  196. *fnip->store= NULL;
  197. }
  198. free(updatefnbuf);
  199. }
  200. struct pipef *status_pipes= NULL;
  201. void modstatdb_note(struct pkginfo *pkg) {
  202. assert(cstatus >= msdbrw_write);
  203. onerr_abort++;
  204. if (status_pipes) {
  205. static struct varbuf *status= NULL;
  206. struct pipef *pipef= status_pipes;
  207. int r;
  208. if (status == NULL) {
  209. status = nfmalloc(sizeof(struct varbuf));
  210. varbufinit(status);
  211. } else
  212. varbufreset(status);
  213. r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name);
  214. while (pipef) {
  215. write(pipef->fd, status->buf, r);
  216. pipef= pipef->next;
  217. }
  218. }
  219. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
  220. versiondescribe(&pkg->installed.version, vdew_nonambig));
  221. varbufreset(&uvb);
  222. varbufrecord(&uvb, pkg, &pkg->installed);
  223. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  224. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  225. if (fflush(importanttmp))
  226. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  227. if (ftruncate(fileno(importanttmp), uvb.used))
  228. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  229. if (fsync(fileno(importanttmp)))
  230. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  231. if (fclose(importanttmp))
  232. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  233. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  234. if (rename(importanttmpfile, updatefnbuf))
  235. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  236. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  237. nextupdate++;
  238. if (nextupdate > MAXUPDATES) {
  239. checkpoint();
  240. nextupdate= 0;
  241. }
  242. createimptmp();
  243. onerr_abort--;
  244. }
  245. const char *log_file= NULL;
  246. void log_message(const char *fmt, ...) {
  247. static struct varbuf *log= NULL;
  248. static FILE *logfd= NULL;
  249. char time_str[20];
  250. time_t now;
  251. va_list al;
  252. if (!log_file)
  253. return;
  254. if (!logfd) {
  255. logfd= fopen(log_file, "a");
  256. if (!logfd) {
  257. fprintf(stderr, _("couldn't open log `%s': %s\n"), log_file,
  258. strerror(errno));
  259. log_file= NULL;
  260. return;
  261. }
  262. setlinebuf(logfd);
  263. setcloexec(fileno(logfd), log_file);
  264. }
  265. if (!log) {
  266. log= nfmalloc(sizeof(struct varbuf));
  267. varbufinit(log);
  268. } else
  269. varbufreset(log);
  270. va_start(al,fmt);
  271. varbufvprintf(log, fmt, al);
  272. varbufaddc(log, 0);
  273. va_end(al);
  274. time(&now);
  275. strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));
  276. fprintf(logfd, "%s %s\n", time_str, log->buf);
  277. }