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