dbmodify.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. char *triggersdir, *triggersfilefile, *triggersnewfilefile;
  41. static enum modstatdb_rw cstatus=-1, cflags=0;
  42. static char *importanttmpfile=NULL;
  43. static FILE *importanttmp;
  44. static int nextupdate;
  45. static int updateslength;
  46. static char *updatefnbuf, *updatefnrest;
  47. static const char *admindir;
  48. static struct varbuf uvb;
  49. static int ulist_select(const struct dirent *de) {
  50. const char *p;
  51. int l;
  52. for (p= de->d_name, l=0; *p; p++, l++)
  53. if (!cisdigit(*p)) return 0;
  54. if (l > IMPORTANTMAXLEN)
  55. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  56. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  57. if (updateslength == -1) updateslength= l;
  58. else if (l != updateslength)
  59. ohshit(_("updates directory contains files with different length names "
  60. "(both %d and %d)"), l, updateslength);
  61. return 1;
  62. }
  63. static void cleanupdates(void) {
  64. struct dirent **cdlist;
  65. int cdn, i;
  66. parsedb(statusfile, pdb_weakclassification, NULL,NULL,NULL);
  67. *updatefnrest= 0;
  68. updateslength= -1;
  69. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  70. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  71. if (cdn) {
  72. for (i=0; i<cdn; i++) {
  73. strcpy(updatefnrest, cdlist[i]->d_name);
  74. parsedb(updatefnbuf, pdb_weakclassification, NULL,NULL,NULL);
  75. if (cstatus < msdbrw_write) free(cdlist[i]);
  76. }
  77. if (cstatus >= msdbrw_write) {
  78. writedb(statusfile,0,1);
  79. for (i=0; i<cdn; i++) {
  80. strcpy(updatefnrest, cdlist[i]->d_name);
  81. if (unlink(updatefnbuf))
  82. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  83. free(cdlist[i]);
  84. }
  85. }
  86. }
  87. free(cdlist);
  88. nextupdate= 0;
  89. }
  90. static void createimptmp(void) {
  91. int i;
  92. onerr_abort++;
  93. importanttmp= fopen(importanttmpfile,"w");
  94. if (!importanttmp)
  95. ohshite(_("unable to create `%.255s'"), importanttmpfile);
  96. setcloexec(fileno(importanttmp),importanttmpfile);
  97. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  98. if (ferror(importanttmp))
  99. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  100. if (fflush(importanttmp))
  101. ohshite(_("unable to flush %.250s after padding"), importanttmpfile);
  102. if (fseek(importanttmp,0,SEEK_SET))
  103. ohshite(_("unable to seek to start of %.250s after padding"),
  104. importanttmpfile);
  105. onerr_abort--;
  106. }
  107. static const struct fni {
  108. const char *suffix;
  109. char **store;
  110. } fnis[] = {
  111. { STATUSFILE, &statusfile },
  112. { AVAILFILE, &availablefile },
  113. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  114. { TRIGGERSDIR, &triggersdir },
  115. { TRIGGERSDIR "/File", &triggersfilefile },
  116. { TRIGGERSDIR "/File.new", &triggersnewfilefile},
  117. { NULL, NULL }
  118. };
  119. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  120. const struct fni *fnip;
  121. admindir= adir;
  122. for (fnip=fnis; fnip->suffix; fnip++) {
  123. free(*fnip->store);
  124. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  125. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  126. }
  127. cflags= readwritereq & msdbrw_flagsmask;
  128. readwritereq &= ~msdbrw_flagsmask;
  129. switch (readwritereq) {
  130. case msdbrw_needsuperuser:
  131. case msdbrw_needsuperuserlockonly:
  132. if (getuid() || geteuid())
  133. ohshit(_("requested operation requires superuser privilege"));
  134. /* fall through */
  135. case msdbrw_write: case msdbrw_writeifposs:
  136. if (access(adir,W_OK)) {
  137. if (errno != EACCES)
  138. ohshite(_("unable to access dpkg status area"));
  139. else if (readwritereq == msdbrw_write)
  140. ohshit(_("operation requires read/write access to dpkg status area"));
  141. cstatus= msdbrw_readonly;
  142. } else {
  143. lockdatabase(adir);
  144. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  145. msdbrw_needsuperuserlockonly :
  146. msdbrw_write);
  147. }
  148. break;
  149. case msdbrw_readonly:
  150. cstatus= msdbrw_readonly; break;
  151. default:
  152. internerr("unknown readwritereq");
  153. }
  154. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  155. strcpy(updatefnbuf,adir);
  156. strcat(updatefnbuf,"/" UPDATESDIR);
  157. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  158. if (cstatus != msdbrw_needsuperuserlockonly) {
  159. cleanupdates();
  160. if(!(cflags & msdbrw_noavail))
  161. parsedb(availablefile,
  162. pdb_recordavailable|pdb_rejectstatus,
  163. NULL,NULL,NULL);
  164. }
  165. if (cstatus >= msdbrw_write) {
  166. createimptmp();
  167. uvb.used= 0;
  168. uvb.size= 10240;
  169. uvb.buf= m_malloc(uvb.size);
  170. }
  171. trig_incorporate(cstatus, admindir);
  172. return cstatus;
  173. }
  174. void modstatdb_checkpoint(void) {
  175. int i;
  176. assert(cstatus >= msdbrw_write);
  177. writedb(statusfile,0,1);
  178. for (i=0; i<nextupdate; i++) {
  179. sprintf(updatefnrest, IMPORTANTFMT, i);
  180. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  181. if (unlink(updatefnbuf))
  182. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  183. }
  184. nextupdate= 0;
  185. }
  186. void modstatdb_shutdown(void) {
  187. const struct fni *fnip;
  188. switch (cstatus) {
  189. case msdbrw_write:
  190. modstatdb_checkpoint();
  191. writedb(availablefile,1,0);
  192. /* tidy up a bit, but don't worry too much about failure */
  193. fclose(importanttmp);
  194. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  195. varbuffree(&uvb);
  196. /* fall through */
  197. case msdbrw_needsuperuserlockonly:
  198. unlockdatabase();
  199. default:
  200. break;
  201. }
  202. for (fnip=fnis; fnip->suffix; fnip++) {
  203. free(*fnip->store);
  204. *fnip->store= NULL;
  205. }
  206. free(updatefnbuf);
  207. }
  208. /* Note: If anyone wants to set some triggers-pending, they must also
  209. * set status appropriately, or we will undo it. That is, it is legal
  210. * to call this when pkg->status and pkg->trigpend_head disagree and
  211. * in that case pkg->status takes precedence and pkg->trigpend_head
  212. * will be adjusted.
  213. */
  214. void modstatdb_note(struct pkginfo *pkg) {
  215. struct trigaw *ta;
  216. assert(cstatus >= msdbrw_write);
  217. onerr_abort++;
  218. if (pkg->status != stat_triggerspending &&
  219. pkg->status != stat_triggersawaited)
  220. pkg->trigpend_head = NULL;
  221. if (pkg->status <= stat_configfiles) {
  222. for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next)
  223. ta->aw = NULL;
  224. pkg->trigaw.head = pkg->trigaw.tail = NULL;
  225. }
  226. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
  227. versiondescribe(&pkg->installed.version, vdew_nonambig));
  228. statusfd_send("status: %s: %s", pkg->name, statusinfos[pkg->status].name);
  229. varbufreset(&uvb);
  230. varbufrecord(&uvb, pkg, &pkg->installed);
  231. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  232. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  233. if (fflush(importanttmp))
  234. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  235. if (ftruncate(fileno(importanttmp), uvb.used))
  236. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  237. if (fsync(fileno(importanttmp)))
  238. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  239. if (fclose(importanttmp))
  240. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  241. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  242. if (rename(importanttmpfile, updatefnbuf))
  243. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  244. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  245. nextupdate++;
  246. if (nextupdate > MAXUPDATES) {
  247. modstatdb_checkpoint();
  248. nextupdate= 0;
  249. }
  250. createimptmp();
  251. if (!pkg->trigpend_head && pkg->othertrigaw_head) {
  252. /* Automatically remove us from other packages' Triggers-Awaited.
  253. * We do this last because we want to maximise our chances of
  254. * successfully recording the status of the package we were
  255. * pointed at by our caller, although there is some risk of
  256. * leaving us in a slightly odd situation which is cleared up
  257. * by the trigger handling logic in deppossi_ok_found.
  258. */
  259. trig_clear_awaiters(pkg);
  260. }
  261. onerr_abort--;
  262. }
  263. void
  264. modstatdb_note_ifwrite(struct pkginfo *pkg)
  265. {
  266. if (cstatus >= msdbrw_write)
  267. modstatdb_note(pkg);
  268. }
  269. const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
  270. static struct varbuf vb;
  271. varbufreset(&vb);
  272. varbufaddstr(&vb,admindir);
  273. varbufaddstr(&vb,"/" INFODIR);
  274. varbufaddstr(&vb,pkg->name);
  275. varbufaddc(&vb,'.');
  276. varbufaddstr(&vb,whichfile);
  277. varbufaddc(&vb,0);
  278. return vb.buf;
  279. }