dbmodify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. *
  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 <stdbool.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/dpkg.h>
  40. #include <dpkg/dpkg-db.h>
  41. #include <dpkg/file.h>
  42. #include <dpkg/dir.h>
  43. #include <dpkg/triglib.h>
  44. static bool db_initialized;
  45. static enum modstatdb_rw cstatus=-1, cflags=0;
  46. static char *lockfile;
  47. static char *statusfile, *availablefile;
  48. static char *importanttmpfile=NULL;
  49. static FILE *importanttmp;
  50. static int nextupdate;
  51. static char *updatesdir;
  52. static int updateslength;
  53. static char *updatefnbuf, *updatefnrest;
  54. static struct varbuf uvb;
  55. static int ulist_select(const struct dirent *de) {
  56. const char *p;
  57. int l;
  58. for (p= de->d_name, l=0; *p; p++, l++)
  59. if (!cisdigit(*p)) return 0;
  60. if (l > IMPORTANTMAXLEN)
  61. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  62. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  63. if (updateslength == -1) updateslength= l;
  64. else if (l != updateslength)
  65. ohshit(_("updates directory contains files with different length names "
  66. "(both %d and %d)"), l, updateslength);
  67. return 1;
  68. }
  69. static void cleanupdates(void) {
  70. struct dirent **cdlist;
  71. int cdn, i;
  72. parsedb(statusfile, pdb_lax_parser | pdb_weakclassification, NULL);
  73. *updatefnrest = '\0';
  74. updateslength= -1;
  75. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  76. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  77. if (cdn) {
  78. for (i=0; i<cdn; i++) {
  79. strcpy(updatefnrest, cdlist[i]->d_name);
  80. parsedb(updatefnbuf, pdb_lax_parser | pdb_weakclassification,
  81. NULL);
  82. if (cstatus < msdbrw_write) free(cdlist[i]);
  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. free(cdlist[i]);
  91. }
  92. dir_sync_path(updatesdir);
  93. }
  94. }
  95. free(cdlist);
  96. nextupdate= 0;
  97. }
  98. static void createimptmp(void) {
  99. int i;
  100. onerr_abort++;
  101. importanttmp= fopen(importanttmpfile,"w");
  102. if (!importanttmp)
  103. ohshite(_("unable to create `%.255s'"), importanttmpfile);
  104. setcloexec(fileno(importanttmp),importanttmpfile);
  105. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  106. if (ferror(importanttmp))
  107. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  108. if (fflush(importanttmp))
  109. ohshite(_("unable to flush %.250s after padding"), importanttmpfile);
  110. if (fseek(importanttmp,0,SEEK_SET))
  111. ohshite(_("unable to seek to start of %.250s after padding"),
  112. importanttmpfile);
  113. onerr_abort--;
  114. }
  115. static const struct fni {
  116. const char *suffix;
  117. char **store;
  118. } fnis[] = {
  119. { LOCKFILE, &lockfile },
  120. { STATUSFILE, &statusfile },
  121. { AVAILFILE, &availablefile },
  122. { UPDATESDIR, &updatesdir },
  123. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  124. { NULL, NULL }
  125. };
  126. void
  127. modstatdb_init(void)
  128. {
  129. const struct fni *fnip;
  130. if (db_initialized)
  131. return;
  132. for (fnip = fnis; fnip->suffix; fnip++) {
  133. free(*fnip->store);
  134. *fnip->store = dpkg_db_get_path(fnip->suffix);
  135. }
  136. updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
  137. strcpy(updatefnbuf, updatesdir);
  138. updatefnrest = updatefnbuf + strlen(updatefnbuf);
  139. db_initialized = true;
  140. }
  141. void
  142. modstatdb_done(void)
  143. {
  144. const struct fni *fnip;
  145. if (!db_initialized)
  146. return;
  147. for (fnip = fnis; fnip->suffix; fnip++) {
  148. free(*fnip->store);
  149. *fnip->store = NULL;
  150. }
  151. free(updatefnbuf);
  152. db_initialized = false;
  153. }
  154. static int dblockfd = -1;
  155. bool
  156. modstatdb_is_locked(void)
  157. {
  158. int lockfd;
  159. bool locked;
  160. if (dblockfd == -1) {
  161. lockfd = open(lockfile, O_RDONLY);
  162. if (lockfd == -1)
  163. ohshite(_("unable to open lock file %s for testing"), lockfile);
  164. } else {
  165. lockfd = dblockfd;
  166. }
  167. locked = file_is_locked(lockfd, lockfile);
  168. /* We only close the file if there was no lock open, otherwise we would
  169. * release the existing lock on close. */
  170. if (dblockfd == -1)
  171. close(lockfd);
  172. return locked;
  173. }
  174. bool
  175. modstatdb_can_lock(void)
  176. {
  177. if (dblockfd >= 0)
  178. return true;
  179. dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
  180. if (dblockfd == -1) {
  181. if (errno == EACCES || errno == EPERM)
  182. return false;
  183. else
  184. ohshite(_("unable to open/create status database lockfile"));
  185. }
  186. return true;
  187. }
  188. void
  189. modstatdb_lock(void)
  190. {
  191. if (!modstatdb_can_lock())
  192. ohshit(_("you do not have permission to lock the dpkg status database"));
  193. file_lock(&dblockfd, FILE_LOCK_NOWAIT, lockfile, _("dpkg status database"));
  194. }
  195. void
  196. modstatdb_unlock(void)
  197. {
  198. /* Unlock. */
  199. pop_cleanup(ehflag_normaltidy);
  200. dblockfd = -1;
  201. }
  202. enum modstatdb_rw
  203. modstatdb_open(enum modstatdb_rw readwritereq)
  204. {
  205. modstatdb_init();
  206. cflags = readwritereq & msdbrw_available_mask;
  207. readwritereq &= ~msdbrw_available_mask;
  208. switch (readwritereq) {
  209. case msdbrw_needsuperuser:
  210. case msdbrw_needsuperuserlockonly:
  211. if (getuid() || geteuid())
  212. ohshit(_("requested operation requires superuser privilege"));
  213. /* Fall through. */
  214. case msdbrw_write: case msdbrw_writeifposs:
  215. if (access(dpkg_db_get_dir(), W_OK)) {
  216. if (errno != EACCES)
  217. ohshite(_("unable to access dpkg status area"));
  218. else if (readwritereq == msdbrw_write)
  219. ohshit(_("operation requires read/write access to dpkg status area"));
  220. cstatus= msdbrw_readonly;
  221. } else {
  222. modstatdb_lock();
  223. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  224. msdbrw_needsuperuserlockonly :
  225. msdbrw_write);
  226. }
  227. break;
  228. case msdbrw_readonly:
  229. cstatus= msdbrw_readonly; break;
  230. default:
  231. internerr("unknown modstatdb_rw '%d'", readwritereq);
  232. }
  233. dpkg_arch_load_list();
  234. if (cstatus != msdbrw_needsuperuserlockonly) {
  235. cleanupdates();
  236. if (cflags >= msdbrw_available_readonly)
  237. parsedb(availablefile,
  238. pdb_recordavailable | pdb_rejectstatus | pdb_lax_parser,
  239. 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. unlink(importanttmpfile);
  277. varbuf_destroy(&uvb);
  278. /* Fall through. */
  279. case msdbrw_needsuperuserlockonly:
  280. modstatdb_unlock();
  281. default:
  282. break;
  283. }
  284. modstatdb_done();
  285. }
  286. static void
  287. modstatdb_note_core(struct pkginfo *pkg)
  288. {
  289. assert(cstatus >= msdbrw_write);
  290. varbuf_reset(&uvb);
  291. varbufrecord(&uvb, pkg, &pkg->installed);
  292. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  293. ohshite(_("unable to write updated status of `%.250s'"), pkg->set->name);
  294. if (fflush(importanttmp))
  295. ohshite(_("unable to flush updated status of `%.250s'"), pkg->set->name);
  296. if (ftruncate(fileno(importanttmp), uvb.used))
  297. ohshite(_("unable to truncate for updated status of `%.250s'"),
  298. pkg->set->name);
  299. if (fsync(fileno(importanttmp)))
  300. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->set->name);
  301. if (fclose(importanttmp))
  302. ohshite(_("unable to close updated status of `%.250s'"), pkg->set->name);
  303. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  304. if (rename(importanttmpfile, updatefnbuf))
  305. ohshite(_("unable to install updated status of `%.250s'"), pkg->set->name);
  306. dir_sync_path(updatesdir);
  307. /* Have we made a real mess? */
  308. assert(strlen(updatefnrest) <= IMPORTANTMAXLEN);
  309. nextupdate++;
  310. if (nextupdate > MAXUPDATES) {
  311. modstatdb_checkpoint();
  312. nextupdate = 0;
  313. }
  314. createimptmp();
  315. }
  316. /*
  317. * Note: If anyone wants to set some triggers-pending, they must also
  318. * set status appropriately, or we will undo it. That is, it is legal
  319. * to call this when pkg->status and pkg->trigpend_head disagree and
  320. * in that case pkg->status takes precedence and pkg->trigpend_head
  321. * will be adjusted.
  322. */
  323. void modstatdb_note(struct pkginfo *pkg) {
  324. struct trigaw *ta;
  325. onerr_abort++;
  326. /* Clear pending triggers here so that only code that sets the status
  327. * to interesting (for triggers) values has to care about triggers. */
  328. if (pkg->status != stat_triggerspending &&
  329. pkg->status != stat_triggersawaited)
  330. pkg->trigpend_head = NULL;
  331. if (pkg->status <= stat_configfiles) {
  332. for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next)
  333. ta->aw = NULL;
  334. pkg->trigaw.head = pkg->trigaw.tail = NULL;
  335. }
  336. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->set->name,
  337. versiondescribe(&pkg->installed.version, vdew_nonambig));
  338. statusfd_send("status: %s: %s", pkg->set->name, statusinfos[pkg->status].name);
  339. if (cstatus >= msdbrw_write)
  340. modstatdb_note_core(pkg);
  341. if (!pkg->trigpend_head && pkg->othertrigaw_head) {
  342. /* Automatically remove us from other packages' Triggers-Awaited.
  343. * We do this last because we want to maximize our chances of
  344. * successfully recording the status of the package we were
  345. * pointed at by our caller, although there is some risk of
  346. * leaving us in a slightly odd situation which is cleared up
  347. * by the trigger handling logic in deppossi_ok_found. */
  348. trig_clear_awaiters(pkg);
  349. }
  350. onerr_abort--;
  351. }
  352. void
  353. modstatdb_note_ifwrite(struct pkginfo *pkg)
  354. {
  355. if (cstatus >= msdbrw_write)
  356. modstatdb_note(pkg);
  357. }