trigproc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * dpkg - main program for package management
  3. * trigproc.c - trigger processing
  4. *
  5. * Copyright © 2007 Canonical Ltd
  6. * written by Ian Jackson <ijackson@chiark.greenend.org.uk>
  7. * Copyright © 2008-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 <assert.h>
  26. #include <fcntl.h>
  27. #include <stdlib.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/dpkg-db.h>
  31. #include <dpkg/pkg.h>
  32. #include <dpkg/pkg-queue.h>
  33. #include <dpkg/triglib.h>
  34. #include "main.h"
  35. #include "filesdb.h"
  36. #include "infodb.h"
  37. /*
  38. * Trigger processing algorithms:
  39. *
  40. *
  41. * There is a separate queue (‘deferred trigproc list’) for triggers
  42. * ‘relevant’ to what we just did; when we find something triggered ‘now’
  43. * we add it to that queue (unless --no-triggers).
  44. *
  45. *
  46. * We want to prefer configuring packages where possible to doing
  47. * trigger processing, but we want to prefer trigger processing to
  48. * cycle-breaking and dependency forcing. This is achieved as
  49. * follows:
  50. *
  51. * Each time during configure processing where a package D is blocked by
  52. * only (ie Depends unsatisfied but would be satisfied by) a t-awaiter W
  53. * we make a note of (one of) W's t-pending, T. (Only the last such T.)
  54. * (If --no-triggers and nonempty argument list and package isn't in
  55. * argument list then we don't do this.)
  56. *
  57. * Each time in packages.c where we increment dependtry, we instead see
  58. * if we have encountered such a t-pending T. If we do, we trigproc T
  59. * instead of incrementing dependtry and this counts as having done
  60. * something so we reset sincenothing.
  61. *
  62. *
  63. * For --triggers-only and --configure, we go through each thing in the
  64. * argument queue (the enqueue_package queue) and check what its state is
  65. * and if appropriate we trigproc it. If we didn't have a queue (or had
  66. * just --pending) we search all triggers-pending packages and add them
  67. * to the deferred trigproc list.
  68. *
  69. *
  70. * Before quitting from most operations, we trigproc each package in the
  71. * deferred trigproc list. This may (if not --no-triggers) of course add
  72. * new things to the deferred trigproc list.
  73. *
  74. *
  75. * Note that ‘we trigproc T’ must involve trigger cycle detection and
  76. * also automatic setting of t-awaiters to t-pending or installed. In
  77. * particular, we do cycle detection even for trigger processing in the
  78. * configure dependtry loop (and it is OK to do it for explicitly
  79. * specified packages from the command line arguments; duplicates are
  80. * removed by packages.c:process_queue).
  81. */
  82. /*========== Deferred trigger queue. ==========*/
  83. static struct pkg_queue deferred = PKG_QUEUE_INIT;
  84. static void
  85. trigproc_enqueue_deferred(struct pkginfo *pend)
  86. {
  87. if (f_triggers < 0)
  88. return;
  89. ensure_package_clientdata(pend);
  90. if (pend->clientdata->trigprocdeferred)
  91. return;
  92. pend->clientdata->trigprocdeferred = pkg_queue_push(&deferred, pend);
  93. debug(dbg_triggers, "trigproc_enqueue_deferred pend=%s",
  94. pkg_name(pend, pnaw_always));
  95. }
  96. /**
  97. * Populate the deferred trigger queue.
  98. *
  99. * When dpkg is called with a specific set of packages to act on, we might
  100. * have packages pending trigger processing. But because there are frontends
  101. * that do not perform a final «dpkg --configure --pending» call (i.e. apt),
  102. * the system is left in a state with packages not fully installed.
  103. *
  104. * We have to populate the deferred trigger queue from the entire package
  105. * database, so that we might try to do opportunistic trigger processing
  106. * when going through the deferred trigger queue, because a fixed apt will
  107. * not request the necessary processing anyway.
  108. *
  109. * XXX: This can be removed once apt is fixed in the next stable release.
  110. */
  111. void
  112. trigproc_populate_deferred(void)
  113. {
  114. struct pkgiterator *iter;
  115. struct pkginfo *pkg;
  116. iter = pkg_db_iter_new();
  117. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  118. if (!pkg->trigpend_head)
  119. continue;
  120. if (pkg->status != PKG_STAT_TRIGGERSAWAITED &&
  121. pkg->status != PKG_STAT_TRIGGERSPENDING)
  122. continue;
  123. if (pkg->want != PKG_WANT_INSTALL)
  124. continue;
  125. trigproc_enqueue_deferred(pkg);
  126. }
  127. pkg_db_iter_free(iter);
  128. }
  129. void
  130. trigproc_run_deferred(void)
  131. {
  132. jmp_buf ejbuf;
  133. debug(dbg_triggers, "trigproc_run_deferred");
  134. while (!pkg_queue_is_empty(&deferred)) {
  135. struct pkginfo *pkg;
  136. pkg = pkg_queue_pop(&deferred);
  137. if (!pkg)
  138. continue;
  139. if (setjmp(ejbuf)) {
  140. pop_error_context(ehflag_bombout);
  141. continue;
  142. }
  143. push_error_context_jump(&ejbuf, print_error_perpackage,
  144. pkg_name(pkg, pnaw_nonambig));
  145. pkg->clientdata->trigprocdeferred = NULL;
  146. trigproc(pkg, TRIGPROC_TRY);
  147. pop_error_context(ehflag_normaltidy);
  148. }
  149. }
  150. /*
  151. * Called by modstatdb_note.
  152. */
  153. void
  154. trig_activate_packageprocessing(struct pkginfo *pkg)
  155. {
  156. debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
  157. pkg_name(pkg, pnaw_always));
  158. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  159. NULL, trig_cicb_statuschange_activate,
  160. pkg, &pkg->installed);
  161. }
  162. /*========== Actual trigger processing. ==========*/
  163. struct trigcyclenode {
  164. struct trigcyclenode *next;
  165. struct trigcycleperpkg *pkgs;
  166. struct pkginfo *then_processed;
  167. };
  168. struct trigcycleperpkg {
  169. struct trigcycleperpkg *next;
  170. struct pkginfo *pkg;
  171. struct trigpend *then_trigs;
  172. };
  173. static bool tortoise_advance;
  174. static struct trigcyclenode *tortoise, *hare;
  175. void
  176. trigproc_reset_cycle(void)
  177. {
  178. tortoise_advance = false;
  179. tortoise = hare = NULL;
  180. }
  181. static bool
  182. tortoise_not_in_hare(struct pkginfo *processing_now,
  183. struct trigcycleperpkg *tortoise_pkg)
  184. {
  185. const char *processing_now_name, *tortoise_name;
  186. struct trigpend *hare_trig, *tortoise_trig;
  187. processing_now_name = pkg_name(processing_now, pnaw_nonambig);
  188. tortoise_name = pkg_name(tortoise_pkg->pkg, pnaw_nonambig);
  189. debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s", __func__,
  190. processing_now_name, tortoise_name);
  191. for (tortoise_trig = tortoise_pkg->then_trigs;
  192. tortoise_trig;
  193. tortoise_trig = tortoise_trig->next) {
  194. debug(dbg_triggersdetail,
  195. "%s pnow=%s tortoise=%s tortoisetrig=%s", __func__,
  196. processing_now_name, tortoise_name, tortoise_trig->name);
  197. /* hare is now so we can just look up in the actual data. */
  198. for (hare_trig = tortoise_pkg->pkg->trigpend_head;
  199. hare_trig;
  200. hare_trig = hare_trig->next) {
  201. debug(dbg_triggersstupid, "%s pnow=%s tortoise=%s"
  202. " tortoisetrig=%s haretrig=%s", __func__,
  203. processing_now_name, tortoise_name,
  204. tortoise_trig->name, hare_trig->name);
  205. if (strcmp(hare_trig->name, tortoise_trig->name) == 0)
  206. break;
  207. }
  208. if (hare_trig == NULL) {
  209. /* Not found in hare, yay! */
  210. debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s OK",
  211. __func__, processing_now_name, tortoise_name);
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. /*
  218. * Returns package we're to give up on.
  219. */
  220. static struct pkginfo *
  221. check_trigger_cycle(struct pkginfo *processing_now)
  222. {
  223. struct trigcyclenode *tcn;
  224. struct trigcycleperpkg *tcpp, *tortoise_pkg;
  225. struct trigpend *tortoise_trig;
  226. struct pkgiterator *iter;
  227. struct pkginfo *pkg, *giveup;
  228. const char *sep;
  229. debug(dbg_triggers, "check_triggers_cycle pnow=%s",
  230. pkg_name(processing_now, pnaw_always));
  231. tcn = nfmalloc(sizeof(*tcn));
  232. tcn->pkgs = NULL;
  233. tcn->then_processed = processing_now;
  234. iter = pkg_db_iter_new();
  235. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  236. if (!pkg->trigpend_head)
  237. continue;
  238. tcpp = nfmalloc(sizeof(*tcpp));
  239. tcpp->pkg = pkg;
  240. tcpp->then_trigs = pkg->trigpend_head;
  241. tcpp->next = tcn->pkgs;
  242. tcn->pkgs = tcpp;
  243. }
  244. pkg_db_iter_free(iter);
  245. if (!hare) {
  246. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
  247. pkg_name(processing_now, pnaw_always));
  248. tcn->next = NULL;
  249. hare = tortoise = tcn;
  250. return NULL;
  251. }
  252. tcn->next = NULL;
  253. hare->next = tcn;
  254. hare = tcn;
  255. if (tortoise_advance)
  256. tortoise = tortoise->next;
  257. tortoise_advance = !tortoise_advance;
  258. /* Now we compare hare to tortoise.
  259. * We want to find a trigger pending in tortoise which is not in hare
  260. * if we find such a thing we have proved that hare isn't a superset
  261. * of tortoise and so that we haven't found a loop (yet). */
  262. for (tortoise_pkg = tortoise->pkgs;
  263. tortoise_pkg;
  264. tortoise_pkg = tortoise_pkg->next) {
  265. if (tortoise_not_in_hare(processing_now, tortoise_pkg))
  266. return NULL;
  267. }
  268. /* Oh dear. hare is a superset of tortoise. We are making no
  269. * progress. */
  270. notice(_("cycle found while processing triggers:\n"
  271. " chain of packages whose triggers are or may be responsible:"));
  272. sep = " ";
  273. for (tcn = tortoise; tcn; tcn = tcn->next) {
  274. fprintf(stderr, "%s%s", sep,
  275. pkg_name(tcn->then_processed, pnaw_nonambig));
  276. sep = " -> ";
  277. }
  278. fprintf(stderr, _("\n" " packages' pending triggers which are"
  279. " or may be unresolvable:\n"));
  280. for (tortoise_pkg = tortoise->pkgs;
  281. tortoise_pkg;
  282. tortoise_pkg = tortoise_pkg->next) {
  283. fprintf(stderr, " %s",
  284. pkg_name(tortoise_pkg->pkg, pnaw_nonambig));
  285. sep = ": ";
  286. for (tortoise_trig = tortoise_pkg->then_trigs;
  287. tortoise_trig;
  288. tortoise_trig = tortoise_trig->next) {
  289. fprintf(stderr, "%s%s", sep, tortoise_trig->name);
  290. }
  291. fprintf(stderr, "\n");
  292. }
  293. /* We give up on the _earliest_ package involved. */
  294. giveup = tortoise->pkgs->pkg;
  295. debug(dbg_triggers, "check_triggers_cycle pnow=%s giveup=%s",
  296. pkg_name(processing_now, pnaw_always),
  297. pkg_name(giveup, pnaw_always));
  298. assert(giveup->status == PKG_STAT_TRIGGERSAWAITED ||
  299. giveup->status == PKG_STAT_TRIGGERSPENDING);
  300. pkg_set_status(giveup, PKG_STAT_HALFCONFIGURED);
  301. modstatdb_note(giveup);
  302. print_error_perpackage(_("triggers looping, abandoned"),
  303. pkg_name(giveup, pnaw_nonambig));
  304. return giveup;
  305. }
  306. /*
  307. * Does cycle checking. Doesn't mind if pkg has no triggers pending - in
  308. * that case does nothing but fix up any stale awaiters.
  309. */
  310. void
  311. trigproc(struct pkginfo *pkg, enum trigproc_type type)
  312. {
  313. static struct varbuf namesarg;
  314. struct varbuf depwhynot = VARBUF_INIT;
  315. struct trigpend *tp;
  316. struct pkginfo *gaveup;
  317. debug(dbg_triggers, "trigproc %s", pkg_name(pkg, pnaw_always));
  318. if (pkg->clientdata->trigprocdeferred)
  319. pkg->clientdata->trigprocdeferred->pkg = NULL;
  320. pkg->clientdata->trigprocdeferred = NULL;
  321. if (pkg->trigpend_head) {
  322. enum dep_check ok;
  323. assert(pkg->status == PKG_STAT_TRIGGERSPENDING ||
  324. pkg->status == PKG_STAT_TRIGGERSAWAITED);
  325. if (dependtry > 1) {
  326. gaveup = check_trigger_cycle(pkg);
  327. if (gaveup == pkg)
  328. return;
  329. if (findbreakcycle(pkg))
  330. sincenothing = 0;
  331. }
  332. ok = dependencies_ok(pkg, NULL, &depwhynot);
  333. if (ok == DEP_CHECK_DEFER) {
  334. varbuf_destroy(&depwhynot);
  335. enqueue_package(pkg);
  336. return;
  337. } else if (ok == DEP_CHECK_HALT) {
  338. /* We cannot process this package on this dpkg run,
  339. * and we can get here repeatedly if this package is
  340. * required to make progress for other packages. So
  341. * reset the trigger cycles tracking to avoid bogus
  342. * cycle detections. */
  343. trigproc_reset_cycle();
  344. /* When doing opportunistic trigger processig, nothing
  345. * requires us to be able to make progress; skip the
  346. * package and silently ignore the error due to
  347. * unsatisfiable dependencies. */
  348. if (type == TRIGPROC_TRY) {
  349. varbuf_destroy(&depwhynot);
  350. return;
  351. }
  352. sincenothing = 0;
  353. varbuf_end_str(&depwhynot);
  354. notice(_("dependency problems prevent processing "
  355. "triggers for %s:\n%s"),
  356. pkg_name(pkg, pnaw_nonambig), depwhynot.buf);
  357. varbuf_destroy(&depwhynot);
  358. ohshit(_("dependency problems - leaving triggers unprocessed"));
  359. } else if (depwhynot.used) {
  360. varbuf_end_str(&depwhynot);
  361. notice(_("%s: dependency problems, but processing "
  362. "triggers anyway as you requested:\n%s"),
  363. pkg_name(pkg, pnaw_nonambig), depwhynot.buf);
  364. varbuf_destroy(&depwhynot);
  365. }
  366. if (dependtry <= 1) {
  367. gaveup = check_trigger_cycle(pkg);
  368. if (gaveup == pkg)
  369. return;
  370. }
  371. printf(_("Processing triggers for %s (%s) ...\n"),
  372. pkg_name(pkg, pnaw_nonambig),
  373. versiondescribe(&pkg->installed.version, vdew_nonambig));
  374. log_action("trigproc", pkg, &pkg->installed);
  375. varbuf_reset(&namesarg);
  376. for (tp = pkg->trigpend_head; tp; tp = tp->next) {
  377. varbuf_add_char(&namesarg, ' ');
  378. varbuf_add_str(&namesarg, tp->name);
  379. }
  380. varbuf_end_str(&namesarg);
  381. /* Setting the status to half-configured
  382. * causes modstatdb_note to clear pending triggers. */
  383. pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
  384. modstatdb_note(pkg);
  385. if (!f_noact) {
  386. sincenothing = 0;
  387. maintscript_postinst(pkg, "triggered",
  388. namesarg.buf + 1, NULL);
  389. }
  390. post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
  391. } else {
  392. /* In other branch is done by modstatdb_note(), from inside
  393. * post_postinst_tasks(). */
  394. trig_clear_awaiters(pkg);
  395. }
  396. }
  397. /*========== Transitional global activation. ==========*/
  398. static void
  399. transitional_interest_callback_ro(const char *trig, struct pkginfo *pkg,
  400. struct pkgbin *pkgbin, enum trig_options opts)
  401. {
  402. struct pkginfo *pend = pkg;
  403. struct pkgbin *pendbin = pkgbin;
  404. debug(dbg_triggersdetail,
  405. "trig_transitional_interest_callback trig=%s pend=%s",
  406. trig, pkgbin_name(pend, pendbin, pnaw_always));
  407. if (pend->status >= PKG_STAT_TRIGGERSAWAITED)
  408. trig_note_pend(pend, nfstrsave(trig));
  409. }
  410. static void
  411. transitional_interest_callback(const char *trig, struct pkginfo *pkg,
  412. struct pkgbin *pkgbin, enum trig_options opts)
  413. {
  414. struct pkginfo *pend = pkg;
  415. struct pkgbin *pendbin = pkgbin;
  416. trig_cicb_interest_add(trig, pend, pendbin, opts);
  417. transitional_interest_callback_ro(trig, pend, pendbin, opts);
  418. }
  419. /*
  420. * cstatus might be msdbrw_readonly if we're in --no-act mode, in which
  421. * case we don't write out all of the interest files etc. but we do
  422. * invent all of the activations for our own benefit.
  423. */
  424. static void
  425. trig_transitional_activate(enum modstatdb_rw cstatus)
  426. {
  427. struct pkgiterator *iter;
  428. struct pkginfo *pkg;
  429. iter = pkg_db_iter_new();
  430. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  431. if (pkg->status <= PKG_STAT_HALFINSTALLED)
  432. continue;
  433. debug(dbg_triggersdetail, "trig_transitional_activate %s %s",
  434. pkg_name(pkg, pnaw_always),
  435. pkg_status_name(pkg));
  436. pkg->trigpend_head = NULL;
  437. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed,
  438. TRIGGERSCIFILE),
  439. cstatus >= msdbrw_write ?
  440. transitional_interest_callback :
  441. transitional_interest_callback_ro, NULL,
  442. pkg, &pkg->installed);
  443. /* Ensure we're not creating incoherent data that can't
  444. * be written down. This should never happen in theory but
  445. * can happen if you restore an old status file that is
  446. * not in sync with the infodb files. */
  447. if (pkg->status < PKG_STAT_TRIGGERSAWAITED)
  448. continue;
  449. if (pkg->trigaw.head)
  450. pkg_set_status(pkg, PKG_STAT_TRIGGERSAWAITED);
  451. else if (pkg->trigpend_head)
  452. pkg_set_status(pkg, PKG_STAT_TRIGGERSPENDING);
  453. else
  454. pkg_set_status(pkg, PKG_STAT_INSTALLED);
  455. }
  456. pkg_db_iter_free(iter);
  457. if (cstatus >= msdbrw_write) {
  458. modstatdb_checkpoint();
  459. trig_file_interests_save();
  460. }
  461. }
  462. /*========== Hook setup. ==========*/
  463. static struct filenamenode *
  464. th_proper_nn_find(const char *name, bool nonew)
  465. {
  466. return findnamenode(name, nonew ? fnn_nonew : 0);
  467. }
  468. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  469. static const struct trig_hooks trig_our_hooks = {
  470. .enqueue_deferred = trigproc_enqueue_deferred,
  471. .transitional_activate = trig_transitional_activate,
  472. .namenode_find = th_proper_nn_find,
  473. .namenode_interested = th_nn_interested,
  474. .namenode_name = th_nn_name,
  475. };
  476. void
  477. trigproc_install_hooks(void)
  478. {
  479. trig_override_hooks(&trig_our_hooks);
  480. }