dbmodify.c 9.9 KB

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