dbmodify.c 11 KB

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