dbmodify.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * dpkg - main program for package management
  3. * dbmodify.c - routines for managing dpkg database updates
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 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. varbufinit(&uvb, 10240);
  168. }
  169. trig_fixup_awaiters(cstatus);
  170. trig_incorporate(cstatus, admindir);
  171. return cstatus;
  172. }
  173. void modstatdb_checkpoint(void) {
  174. int i;
  175. assert(cstatus >= msdbrw_write);
  176. writedb(statusfile,0,1);
  177. for (i=0; i<nextupdate; i++) {
  178. sprintf(updatefnrest, IMPORTANTFMT, i);
  179. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  180. if (unlink(updatefnbuf))
  181. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  182. }
  183. nextupdate= 0;
  184. }
  185. void modstatdb_shutdown(void) {
  186. const struct fni *fnip;
  187. switch (cstatus) {
  188. case msdbrw_write:
  189. modstatdb_checkpoint();
  190. writedb(availablefile,1,0);
  191. /* tidy up a bit, but don't worry too much about failure */
  192. fclose(importanttmp);
  193. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  194. varbuffree(&uvb);
  195. /* fall through */
  196. case msdbrw_needsuperuserlockonly:
  197. unlockdatabase();
  198. default:
  199. break;
  200. }
  201. for (fnip=fnis; fnip->suffix; fnip++) {
  202. free(*fnip->store);
  203. *fnip->store= NULL;
  204. }
  205. free(updatefnbuf);
  206. }
  207. static void
  208. modstatdb_note_core(struct pkginfo *pkg)
  209. {
  210. assert(cstatus >= msdbrw_write);
  211. varbufreset(&uvb);
  212. varbufrecord(&uvb, pkg, &pkg->installed);
  213. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  214. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  215. if (fflush(importanttmp))
  216. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  217. if (ftruncate(fileno(importanttmp), uvb.used))
  218. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  219. if (fsync(fileno(importanttmp)))
  220. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  221. if (fclose(importanttmp))
  222. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  223. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  224. if (rename(importanttmpfile, updatefnbuf))
  225. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  226. /* Have we made a real mess? */
  227. assert(strlen(updatefnrest) <= IMPORTANTMAXLEN);
  228. nextupdate++;
  229. if (nextupdate > MAXUPDATES) {
  230. modstatdb_checkpoint();
  231. nextupdate = 0;
  232. }
  233. createimptmp();
  234. }
  235. /* Note: If anyone wants to set some triggers-pending, they must also
  236. * set status appropriately, or we will undo it. That is, it is legal
  237. * to call this when pkg->status and pkg->trigpend_head disagree and
  238. * in that case pkg->status takes precedence and pkg->trigpend_head
  239. * will be adjusted.
  240. */
  241. void modstatdb_note(struct pkginfo *pkg) {
  242. struct trigaw *ta;
  243. onerr_abort++;
  244. /* Clear pending triggers here so that only code that sets the status
  245. * to interesting (for triggers) values has to care about triggers.
  246. */
  247. if (pkg->status != stat_triggerspending &&
  248. pkg->status != stat_triggersawaited)
  249. pkg->trigpend_head = NULL;
  250. if (pkg->status <= stat_configfiles) {
  251. for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next)
  252. ta->aw = NULL;
  253. pkg->trigaw.head = pkg->trigaw.tail = NULL;
  254. }
  255. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
  256. versiondescribe(&pkg->installed.version, vdew_nonambig));
  257. statusfd_send("status: %s: %s", pkg->name, statusinfos[pkg->status].name);
  258. if (cstatus >= msdbrw_write)
  259. modstatdb_note_core(pkg);
  260. if (!pkg->trigpend_head && pkg->othertrigaw_head) {
  261. /* Automatically remove us from other packages' Triggers-Awaited.
  262. * We do this last because we want to maximise our chances of
  263. * successfully recording the status of the package we were
  264. * pointed at by our caller, although there is some risk of
  265. * leaving us in a slightly odd situation which is cleared up
  266. * by the trigger handling logic in deppossi_ok_found.
  267. */
  268. trig_clear_awaiters(pkg);
  269. }
  270. onerr_abort--;
  271. }
  272. void
  273. modstatdb_note_ifwrite(struct pkginfo *pkg)
  274. {
  275. if (cstatus >= msdbrw_write)
  276. modstatdb_note(pkg);
  277. }
  278. const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
  279. static struct varbuf vb;
  280. varbufreset(&vb);
  281. varbufaddstr(&vb,admindir);
  282. varbufaddstr(&vb,"/" INFODIR);
  283. varbufaddstr(&vb,pkg->name);
  284. varbufaddc(&vb,'.');
  285. varbufaddstr(&vb,whichfile);
  286. varbufaddc(&vb,0);
  287. return vb.buf;
  288. }