dbmodify.c 11 KB

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