dbmodify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  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. * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <limits.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <fcntl.h>
  33. #include <dirent.h>
  34. #include <unistd.h>
  35. #include <stdbool.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/c-ctype.h>
  40. #include <dpkg/dpkg.h>
  41. #include <dpkg/dpkg-db.h>
  42. #include <dpkg/file.h>
  43. #include <dpkg/dir.h>
  44. #include <dpkg/triglib.h>
  45. static bool db_initialized;
  46. static enum modstatdb_rw cstatus=-1, cflags=0;
  47. static char *lockfile;
  48. static char *statusfile, *availablefile;
  49. static char *importanttmpfile=NULL;
  50. static FILE *importanttmp;
  51. static int nextupdate;
  52. static char *updatesdir;
  53. static int updateslength;
  54. static char *updatefnbuf, *updatefnrest;
  55. static struct varbuf uvb;
  56. static int ulist_select(const struct dirent *de) {
  57. const char *p;
  58. int l;
  59. for (p= de->d_name, l=0; *p; p++, l++)
  60. if (!c_isdigit(*p))
  61. return 0;
  62. if (l > IMPORTANTMAXLEN)
  63. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  64. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  65. if (updateslength == -1) updateslength= l;
  66. else if (l != updateslength)
  67. ohshit(_("updates directory contains files with different length names "
  68. "(both %d and %d)"), l, updateslength);
  69. return 1;
  70. }
  71. static void cleanupdates(void) {
  72. struct dirent **cdlist;
  73. int cdn, i;
  74. parsedb(statusfile, pdb_parse_status, NULL);
  75. *updatefnrest = '\0';
  76. updateslength= -1;
  77. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  78. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  79. if (cdn) {
  80. for (i=0; i<cdn; i++) {
  81. strcpy(updatefnrest, cdlist[i]->d_name);
  82. parsedb(updatefnbuf, pdb_parse_update, NULL);
  83. }
  84. if (cstatus >= msdbrw_write) {
  85. writedb(statusfile, wdb_must_sync);
  86. for (i=0; i<cdn; i++) {
  87. strcpy(updatefnrest, cdlist[i]->d_name);
  88. if (unlink(updatefnbuf))
  89. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  90. }
  91. dir_sync_path(updatesdir);
  92. }
  93. for (i = 0; i < cdn; i++)
  94. free(cdlist[i]);
  95. }
  96. free(cdlist);
  97. nextupdate= 0;
  98. }
  99. static void createimptmp(void) {
  100. int i;
  101. onerr_abort++;
  102. importanttmp= fopen(importanttmpfile,"w");
  103. if (!importanttmp)
  104. ohshite(_("unable to create `%.255s'"), importanttmpfile);
  105. setcloexec(fileno(importanttmp),importanttmpfile);
  106. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  107. if (ferror(importanttmp))
  108. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  109. if (fflush(importanttmp))
  110. ohshite(_("unable to flush %.250s after padding"), importanttmpfile);
  111. if (fseek(importanttmp,0,SEEK_SET))
  112. ohshite(_("unable to seek to start of %.250s after padding"),
  113. importanttmpfile);
  114. onerr_abort--;
  115. }
  116. static const struct fni {
  117. const char *suffix;
  118. char **store;
  119. } fnis[] = {
  120. { LOCKFILE, &lockfile },
  121. { STATUSFILE, &statusfile },
  122. { AVAILFILE, &availablefile },
  123. { UPDATESDIR, &updatesdir },
  124. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  125. { NULL, NULL }
  126. };
  127. void
  128. modstatdb_init(void)
  129. {
  130. const struct fni *fnip;
  131. if (db_initialized)
  132. return;
  133. for (fnip = fnis; fnip->suffix; fnip++) {
  134. free(*fnip->store);
  135. *fnip->store = dpkg_db_get_path(fnip->suffix);
  136. }
  137. updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
  138. strcpy(updatefnbuf, updatesdir);
  139. updatefnrest = updatefnbuf + strlen(updatefnbuf);
  140. db_initialized = true;
  141. }
  142. void
  143. modstatdb_done(void)
  144. {
  145. const struct fni *fnip;
  146. if (!db_initialized)
  147. return;
  148. for (fnip = fnis; fnip->suffix; fnip++) {
  149. free(*fnip->store);
  150. *fnip->store = NULL;
  151. }
  152. free(updatefnbuf);
  153. db_initialized = false;
  154. }
  155. static int dblockfd = -1;
  156. bool
  157. modstatdb_is_locked(void)
  158. {
  159. int lockfd;
  160. bool locked;
  161. if (dblockfd == -1) {
  162. lockfd = open(lockfile, O_RDONLY);
  163. if (lockfd == -1)
  164. ohshite(_("unable to open lock file %s for testing"), lockfile);
  165. } else {
  166. lockfd = dblockfd;
  167. }
  168. locked = file_is_locked(lockfd, lockfile);
  169. /* We only close the file if there was no lock open, otherwise we would
  170. * release the existing lock on close. */
  171. if (dblockfd == -1)
  172. close(lockfd);
  173. return locked;
  174. }
  175. bool
  176. modstatdb_can_lock(void)
  177. {
  178. if (dblockfd >= 0)
  179. return true;
  180. dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
  181. if (dblockfd == -1) {
  182. if (errno == EACCES || errno == EPERM)
  183. return false;
  184. else
  185. ohshite(_("unable to open/create status database lockfile"));
  186. }
  187. return true;
  188. }
  189. void
  190. modstatdb_lock(void)
  191. {
  192. if (!modstatdb_can_lock())
  193. ohshit(_("you do not have permission to lock the dpkg status database"));
  194. file_lock(&dblockfd, FILE_LOCK_NOWAIT, lockfile, _("dpkg status database"));
  195. }
  196. void
  197. modstatdb_unlock(void)
  198. {
  199. /* Unlock. */
  200. pop_cleanup(ehflag_normaltidy);
  201. dblockfd = -1;
  202. }
  203. enum modstatdb_rw
  204. modstatdb_open(enum modstatdb_rw readwritereq)
  205. {
  206. modstatdb_init();
  207. cflags = readwritereq & msdbrw_available_mask;
  208. readwritereq &= ~msdbrw_available_mask;
  209. switch (readwritereq) {
  210. case msdbrw_needsuperuser:
  211. case msdbrw_needsuperuserlockonly:
  212. if (getuid() || geteuid())
  213. ohshit(_("requested operation requires superuser privilege"));
  214. /* Fall through. */
  215. case msdbrw_write: case msdbrw_writeifposs:
  216. if (access(dpkg_db_get_dir(), W_OK)) {
  217. if (errno != EACCES)
  218. ohshite(_("unable to access dpkg status area"));
  219. else if (readwritereq == msdbrw_write)
  220. ohshit(_("operation requires read/write access to dpkg status area"));
  221. cstatus= msdbrw_readonly;
  222. } else {
  223. modstatdb_lock();
  224. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  225. msdbrw_needsuperuserlockonly :
  226. msdbrw_write);
  227. }
  228. break;
  229. case msdbrw_readonly:
  230. cstatus= msdbrw_readonly; break;
  231. default:
  232. internerr("unknown modstatdb_rw '%d'", readwritereq);
  233. }
  234. dpkg_arch_load_list();
  235. if (cstatus != msdbrw_needsuperuserlockonly) {
  236. cleanupdates();
  237. if (cflags >= msdbrw_available_readonly)
  238. parsedb(availablefile, pdb_parse_available, NULL);
  239. }
  240. if (cstatus >= msdbrw_write) {
  241. createimptmp();
  242. varbuf_init(&uvb, 10240);
  243. }
  244. trig_fixup_awaiters(cstatus);
  245. trig_incorporate(cstatus);
  246. return cstatus;
  247. }
  248. enum modstatdb_rw
  249. modstatdb_get_status(void)
  250. {
  251. return cstatus;
  252. }
  253. void modstatdb_checkpoint(void) {
  254. int i;
  255. assert(cstatus >= msdbrw_write);
  256. writedb(statusfile, wdb_must_sync);
  257. for (i=0; i<nextupdate; i++) {
  258. sprintf(updatefnrest, IMPORTANTFMT, i);
  259. /* Have we made a real mess? */
  260. assert(strlen(updatefnrest) <= IMPORTANTMAXLEN);
  261. if (unlink(updatefnbuf))
  262. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  263. }
  264. dir_sync_path(updatesdir);
  265. nextupdate= 0;
  266. }
  267. void modstatdb_shutdown(void) {
  268. if (cflags >= msdbrw_available_write)
  269. writedb(availablefile, wdb_dump_available);
  270. switch (cstatus) {
  271. case msdbrw_write:
  272. modstatdb_checkpoint();
  273. /* Tidy up a bit, but don't worry too much about failure. */
  274. fclose(importanttmp);
  275. unlink(importanttmpfile);
  276. varbuf_destroy(&uvb);
  277. /* Fall through. */
  278. case msdbrw_needsuperuserlockonly:
  279. modstatdb_unlock();
  280. default:
  281. break;
  282. }
  283. modstatdb_done();
  284. }
  285. static void
  286. modstatdb_note_core(struct pkginfo *pkg)
  287. {
  288. assert(cstatus >= msdbrw_write);
  289. varbuf_reset(&uvb);
  290. varbufrecord(&uvb, pkg, &pkg->installed);
  291. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  292. ohshite(_("unable to write updated status of `%.250s'"),
  293. pkg_name(pkg, pnaw_nonambig));
  294. if (fflush(importanttmp))
  295. ohshite(_("unable to flush updated status of `%.250s'"),
  296. pkg_name(pkg, pnaw_nonambig));
  297. if (ftruncate(fileno(importanttmp), uvb.used))
  298. ohshite(_("unable to truncate for updated status of `%.250s'"),
  299. pkg_name(pkg, pnaw_nonambig));
  300. if (fsync(fileno(importanttmp)))
  301. ohshite(_("unable to fsync updated status of `%.250s'"),
  302. pkg_name(pkg, pnaw_nonambig));
  303. if (fclose(importanttmp))
  304. ohshite(_("unable to close updated status of `%.250s'"),
  305. pkg_name(pkg, pnaw_nonambig));
  306. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  307. if (rename(importanttmpfile, updatefnbuf))
  308. ohshite(_("unable to install updated status of `%.250s'"),
  309. pkg_name(pkg, pnaw_nonambig));
  310. dir_sync_path(updatesdir);
  311. /* Have we made a real mess? */
  312. assert(strlen(updatefnrest) <= IMPORTANTMAXLEN);
  313. nextupdate++;
  314. if (nextupdate > MAXUPDATES) {
  315. modstatdb_checkpoint();
  316. nextupdate = 0;
  317. }
  318. createimptmp();
  319. }
  320. /*
  321. * Note: If anyone wants to set some triggers-pending, they must also
  322. * set status appropriately, or we will undo it. That is, it is legal
  323. * to call this when pkg->status and pkg->trigpend_head disagree and
  324. * in that case pkg->status takes precedence and pkg->trigpend_head
  325. * will be adjusted.
  326. */
  327. void modstatdb_note(struct pkginfo *pkg) {
  328. struct trigaw *ta;
  329. onerr_abort++;
  330. /* Clear pending triggers here so that only code that sets the status
  331. * to interesting (for triggers) values has to care about triggers. */
  332. if (pkg->status != PKG_STAT_TRIGGERSPENDING &&
  333. pkg->status != PKG_STAT_TRIGGERSAWAITED)
  334. pkg->trigpend_head = NULL;
  335. if (pkg->status <= PKG_STAT_CONFIGFILES) {
  336. for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next)
  337. ta->aw = NULL;
  338. pkg->trigaw.head = pkg->trigaw.tail = NULL;
  339. }
  340. log_message("status %s %s %s", pkg_status_name(pkg),
  341. pkg_name(pkg, pnaw_always),
  342. versiondescribe(&pkg->installed.version, vdew_nonambig));
  343. statusfd_send("status: %s: %s", pkg_name(pkg, pnaw_nonambig),
  344. pkg_status_name(pkg));
  345. if (cstatus >= msdbrw_write)
  346. modstatdb_note_core(pkg);
  347. if (!pkg->trigpend_head && pkg->othertrigaw_head) {
  348. /* Automatically remove us from other packages' Triggers-Awaited.
  349. * We do this last because we want to maximize our chances of
  350. * successfully recording the status of the package we were
  351. * pointed at by our caller, although there is some risk of
  352. * leaving us in a slightly odd situation which is cleared up
  353. * by the trigger handling logic in deppossi_ok_found. */
  354. trig_clear_awaiters(pkg);
  355. }
  356. onerr_abort--;
  357. }
  358. void
  359. modstatdb_note_ifwrite(struct pkginfo *pkg)
  360. {
  361. if (cstatus >= msdbrw_write)
  362. modstatdb_note(pkg);
  363. }