trigproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * dpkg - main program for package management
  3. * trigproc.c - trigger processing
  4. *
  5. * Copyright © 2007 Canonical Ltd
  6. * written by Ian Jackson <ian@chiark.greenend.org.uk>
  7. * Copyright © 2008-2013 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/fcntl.h>
  25. #include <sys/stat.h>
  26. #include <assert.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. void
  97. trigproc_run_deferred(void)
  98. {
  99. jmp_buf ejbuf;
  100. debug(dbg_triggers, "trigproc_run_deferred");
  101. while (!pkg_queue_is_empty(&deferred)) {
  102. struct pkginfo *pkg;
  103. pkg = pkg_queue_pop(&deferred);
  104. if (!pkg)
  105. continue;
  106. if (setjmp(ejbuf)) {
  107. pop_error_context(ehflag_bombout);
  108. continue;
  109. }
  110. push_error_context_jump(&ejbuf, print_error_perpackage,
  111. pkg_name(pkg, pnaw_nonambig));
  112. pkg->clientdata->trigprocdeferred = NULL;
  113. trigproc(pkg);
  114. pop_error_context(ehflag_normaltidy);
  115. }
  116. }
  117. /*
  118. * Called by modstatdb_note.
  119. */
  120. void
  121. trig_activate_packageprocessing(struct pkginfo *pkg)
  122. {
  123. debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
  124. pkg_name(pkg, pnaw_always));
  125. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  126. NULL, trig_cicb_statuschange_activate,
  127. pkg, &pkg->installed);
  128. }
  129. /*========== Actual trigger processing. ==========*/
  130. struct trigcyclenode {
  131. struct trigcyclenode *next;
  132. struct trigcycleperpkg *pkgs;
  133. struct pkginfo *then_processed;
  134. };
  135. struct trigcycleperpkg {
  136. struct trigcycleperpkg *next;
  137. struct pkginfo *pkg;
  138. struct trigpend *then_trigs;
  139. };
  140. static bool tortoise_advance;
  141. static struct trigcyclenode *tortoise, *hare;
  142. void
  143. trigproc_reset_cycle(void)
  144. {
  145. tortoise_advance = false;
  146. tortoise = hare = NULL;
  147. }
  148. static bool
  149. tortoise_not_in_hare(struct pkginfo *processing_now,
  150. struct trigcycleperpkg *tortoise_pkg)
  151. {
  152. const char *processing_now_name, *tortoise_name;
  153. struct trigpend *hare_trig, *tortoise_trig;
  154. processing_now_name = pkg_name(processing_now, pnaw_nonambig);
  155. tortoise_name = pkg_name(tortoise_pkg->pkg, pnaw_nonambig);
  156. debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s", __func__,
  157. processing_now_name, tortoise_name);
  158. for (tortoise_trig = tortoise_pkg->then_trigs;
  159. tortoise_trig;
  160. tortoise_trig = tortoise_trig->next) {
  161. debug(dbg_triggersdetail,
  162. "%s pnow=%s tortoise=%s tortoisetrig=%s", __func__,
  163. processing_now_name, tortoise_name, tortoise_trig->name);
  164. /* hare is now so we can just look up in the actual data. */
  165. for (hare_trig = tortoise_pkg->pkg->trigpend_head;
  166. hare_trig;
  167. hare_trig = hare_trig->next) {
  168. debug(dbg_triggersstupid, "%s pnow=%s tortoise=%s"
  169. " tortoisetrig=%s haretrig=%s", __func__,
  170. processing_now_name, tortoise_name,
  171. tortoise_trig->name, hare_trig->name);
  172. if (strcmp(hare_trig->name, tortoise_trig->name) == 0)
  173. break;
  174. }
  175. if (hare_trig == NULL) {
  176. /* Not found in hare, yay! */
  177. debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s OK",
  178. __func__, processing_now_name, tortoise_name);
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /*
  185. * Returns package we're to give up on.
  186. */
  187. static struct pkginfo *
  188. check_trigger_cycle(struct pkginfo *processing_now)
  189. {
  190. struct trigcyclenode *tcn;
  191. struct trigcycleperpkg *tcpp, *tortoise_pkg;
  192. struct trigpend *tortoise_trig;
  193. struct pkgiterator *it;
  194. struct pkginfo *pkg, *giveup;
  195. const char *sep;
  196. debug(dbg_triggers, "check_triggers_cycle pnow=%s",
  197. pkg_name(processing_now, pnaw_always));
  198. tcn = nfmalloc(sizeof(*tcn));
  199. tcn->pkgs = NULL;
  200. tcn->then_processed = processing_now;
  201. it = pkg_db_iter_new();
  202. while ((pkg = pkg_db_iter_next_pkg(it))) {
  203. if (!pkg->trigpend_head)
  204. continue;
  205. tcpp = nfmalloc(sizeof(*tcpp));
  206. tcpp->pkg = pkg;
  207. tcpp->then_trigs = pkg->trigpend_head;
  208. tcpp->next = tcn->pkgs;
  209. tcn->pkgs = tcpp;
  210. }
  211. pkg_db_iter_free(it);
  212. if (!hare) {
  213. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
  214. pkg_name(processing_now, pnaw_always));
  215. tcn->next = NULL;
  216. hare = tortoise = tcn;
  217. return NULL;
  218. }
  219. tcn->next = NULL;
  220. hare->next = tcn;
  221. hare = tcn;
  222. if (tortoise_advance)
  223. tortoise = tortoise->next;
  224. tortoise_advance = !tortoise_advance;
  225. /* Now we compare hare to tortoise.
  226. * We want to find a trigger pending in tortoise which is not in hare
  227. * if we find such a thing we have proved that hare isn't a superset
  228. * of tortoise and so that we haven't found a loop (yet). */
  229. for (tortoise_pkg = tortoise->pkgs;
  230. tortoise_pkg;
  231. tortoise_pkg = tortoise_pkg->next) {
  232. if (tortoise_not_in_hare(processing_now, tortoise_pkg))
  233. return NULL;
  234. }
  235. /* Oh dear. hare is a superset of tortoise. We are making no
  236. * progress. */
  237. notice(_("cycle found while processing triggers:\n"
  238. " chain of packages whose triggers are or may be responsible:"));
  239. sep = " ";
  240. for (tcn = tortoise; tcn; tcn = tcn->next) {
  241. fprintf(stderr, "%s%s", sep,
  242. pkg_name(tcn->then_processed, pnaw_nonambig));
  243. sep = " -> ";
  244. }
  245. fprintf(stderr, _("\n" " packages' pending triggers which are"
  246. " or may be unresolvable:\n"));
  247. for (tortoise_pkg = tortoise->pkgs;
  248. tortoise_pkg;
  249. tortoise_pkg = tortoise_pkg->next) {
  250. fprintf(stderr, " %s",
  251. pkg_name(tortoise_pkg->pkg, pnaw_nonambig));
  252. sep = ": ";
  253. for (tortoise_trig = tortoise_pkg->then_trigs;
  254. tortoise_trig;
  255. tortoise_trig = tortoise_trig->next) {
  256. fprintf(stderr, "%s%s", sep, tortoise_trig->name);
  257. }
  258. fprintf(stderr, "\n");
  259. }
  260. /* We give up on the _earliest_ package involved. */
  261. giveup = tortoise->pkgs->pkg;
  262. debug(dbg_triggers, "check_triggers_cycle pnow=%s giveup=%p",
  263. pkg_name(processing_now, pnaw_always),
  264. pkg_name(giveup, pnaw_always));
  265. assert(giveup->status == stat_triggersawaited ||
  266. giveup->status == stat_triggerspending);
  267. pkg_set_status(giveup, stat_halfconfigured);
  268. modstatdb_note(giveup);
  269. print_error_perpackage(_("triggers looping, abandoned"),
  270. pkg_name(giveup, pnaw_nonambig));
  271. return giveup;
  272. }
  273. /*
  274. * Does cycle checking. Doesn't mind if pkg has no triggers pending - in
  275. * that case does nothing but fix up any stale awaiters.
  276. */
  277. void
  278. trigproc(struct pkginfo *pkg)
  279. {
  280. static struct varbuf namesarg;
  281. struct trigpend *tp;
  282. struct pkginfo *gaveup;
  283. debug(dbg_triggers, "trigproc %s", pkg_name(pkg, pnaw_always));
  284. if (pkg->clientdata->trigprocdeferred)
  285. pkg->clientdata->trigprocdeferred->pkg = NULL;
  286. pkg->clientdata->trigprocdeferred = NULL;
  287. if (pkg->trigpend_head) {
  288. assert(pkg->status == stat_triggerspending ||
  289. pkg->status == stat_triggersawaited);
  290. gaveup = check_trigger_cycle(pkg);
  291. if (gaveup == pkg)
  292. return;
  293. printf(_("Processing triggers for %s (%s) ...\n"),
  294. pkg_name(pkg, pnaw_nonambig),
  295. versiondescribe(&pkg->installed.version, vdew_nonambig));
  296. log_action("trigproc", pkg, &pkg->installed);
  297. varbuf_reset(&namesarg);
  298. for (tp = pkg->trigpend_head; tp; tp = tp->next) {
  299. varbuf_add_char(&namesarg, ' ');
  300. varbuf_add_str(&namesarg, tp->name);
  301. }
  302. varbuf_end_str(&namesarg);
  303. /* Setting the status to half-configured
  304. * causes modstatdb_note to clear pending triggers. */
  305. pkg_set_status(pkg, stat_halfconfigured);
  306. modstatdb_note(pkg);
  307. if (!f_noact) {
  308. sincenothing = 0;
  309. maintscript_postinst(pkg, "triggered",
  310. namesarg.buf + 1, NULL);
  311. }
  312. post_postinst_tasks(pkg, stat_installed);
  313. } else {
  314. /* In other branch is done by modstatdb_note(), from inside
  315. * post_postinst_tasks(). */
  316. trig_clear_awaiters(pkg);
  317. }
  318. }
  319. /*========== Transitional global activation. ==========*/
  320. static void
  321. transitional_interest_callback_ro(const char *trig, struct pkginfo *pkg,
  322. struct pkgbin *pkgbin, enum trig_options opts)
  323. {
  324. struct pkginfo *pend = pkg;
  325. struct pkgbin *pendbin = pkgbin;
  326. debug(dbg_triggersdetail,
  327. "trig_transitional_interest_callback trig=%s pend=%s",
  328. trig, pkgbin_name(pend, pendbin, pnaw_always));
  329. if (pend->status >= stat_triggersawaited)
  330. trig_note_pend(pend, nfstrsave(trig));
  331. }
  332. static void
  333. transitional_interest_callback(const char *trig, struct pkginfo *pkg,
  334. struct pkgbin *pkgbin, enum trig_options opts)
  335. {
  336. struct pkginfo *pend = pkg;
  337. struct pkgbin *pendbin = pkgbin;
  338. trig_cicb_interest_add(trig, pend, pendbin, opts);
  339. transitional_interest_callback_ro(trig, pend, pendbin, opts);
  340. }
  341. /*
  342. * cstatus might be msdbrw_readonly if we're in --no-act mode, in which
  343. * case we don't write out all of the interest files etc. but we do
  344. * invent all of the activations for our own benefit.
  345. */
  346. static void
  347. trig_transitional_activate(enum modstatdb_rw cstatus)
  348. {
  349. struct pkgiterator *it;
  350. struct pkginfo *pkg;
  351. it = pkg_db_iter_new();
  352. while ((pkg = pkg_db_iter_next_pkg(it))) {
  353. if (pkg->status <= stat_halfinstalled)
  354. continue;
  355. debug(dbg_triggersdetail, "trig_transitional_activate %s %s",
  356. pkg_name(pkg, pnaw_always),
  357. statusinfos[pkg->status].name);
  358. pkg->trigpend_head = NULL;
  359. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed,
  360. TRIGGERSCIFILE),
  361. cstatus >= msdbrw_write ?
  362. transitional_interest_callback :
  363. transitional_interest_callback_ro, NULL,
  364. pkg, &pkg->installed);
  365. /* Ensure we're not creating incoherent data that can't
  366. * be written down. This should never happen in theory but
  367. * can happen if you restore an old status file that is
  368. * not in sync with the infodb files. */
  369. if (pkg->status < stat_triggersawaited)
  370. continue;
  371. if (pkg->trigaw.head)
  372. pkg_set_status(pkg, stat_triggersawaited);
  373. else if (pkg->trigpend_head)
  374. pkg_set_status(pkg, stat_triggerspending);
  375. else
  376. pkg_set_status(pkg, stat_installed);
  377. }
  378. pkg_db_iter_free(it);
  379. if (cstatus >= msdbrw_write) {
  380. modstatdb_checkpoint();
  381. trig_file_interests_save();
  382. }
  383. }
  384. /*========== Hook setup. ==========*/
  385. static struct filenamenode *
  386. th_proper_nn_find(const char *name, bool nonew)
  387. {
  388. return findnamenode(name, nonew ? fnn_nonew : 0);
  389. }
  390. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  391. static const struct trig_hooks trig_our_hooks = {
  392. .enqueue_deferred = trigproc_enqueue_deferred,
  393. .transitional_activate = trig_transitional_activate,
  394. .namenode_find = th_proper_nn_find,
  395. .namenode_interested = th_nn_interested,
  396. .namenode_name = th_nn_name,
  397. };
  398. void
  399. trigproc_install_hooks(void)
  400. {
  401. trig_override_hooks(&trig_our_hooks);
  402. }