dbmodify.c 11 KB

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