dbmodify.c 11 KB

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