2 * dpkg - main program for package management
3 * trigproc.c - trigger processing
5 * Copyright © 2007 Canonical Ltd
6 * written by Ian Jackson <ijackson@chiark.greenend.org.uk>
7 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
32 #include <dpkg/i18n.h>
33 #include <dpkg/dpkg.h>
34 #include <dpkg/dpkg-db.h>
36 #include <dpkg/pkg-queue.h>
37 #include <dpkg/triglib.h>
44 * Trigger processing algorithms:
47 * There is a separate queue (‘deferred trigproc list’) for triggers
48 * ‘relevant’ to what we just did; when we find something triggered ‘now’
49 * we add it to that queue (unless --no-triggers).
52 * We want to prefer configuring packages where possible to doing
53 * trigger processing, but we want to prefer trigger processing to
54 * cycle-breaking and dependency forcing. This is achieved as
57 * Each time during configure processing where a package D is blocked by
58 * only (ie Depends unsatisfied but would be satisfied by) a t-awaiter W
59 * we make a note of (one of) W's t-pending, T. (Only the last such T.)
60 * (If --no-triggers and nonempty argument list and package isn't in
61 * argument list then we don't do this.)
63 * Each time in packages.c where we increment dependtry, we instead see
64 * if we have encountered such a t-pending T. If we do, we trigproc T
65 * instead of incrementing dependtry and this counts as having done
66 * something so we reset sincenothing.
69 * For --triggers-only and --configure, we go through each thing in the
70 * argument queue (the enqueue_package queue) and check what its state is
71 * and if appropriate we trigproc it. If we didn't have a queue (or had
72 * just --pending) we search all triggers-pending packages and add them
73 * to the deferred trigproc list.
76 * Before quitting from most operations, we trigproc each package in the
77 * deferred trigproc list. This may (if not --no-triggers) of course add
78 * new things to the deferred trigproc list.
81 * Note that ‘we trigproc T’ must involve trigger cycle detection and
82 * also automatic setting of t-awaiters to t-pending or installed. In
83 * particular, we do cycle detection even for trigger processing in the
84 * configure dependtry loop (and it is OK to do it for explicitly
85 * specified packages from the command line arguments; duplicates are
86 * removed by packages.c:process_queue).
89 /*========== Deferred trigger queue. ==========*/
91 static struct pkg_queue deferred = PKG_QUEUE_INIT;
94 trigproc_enqueue_deferred(struct pkginfo *pend)
98 ensure_package_clientdata(pend);
99 if (pend->clientdata->trigprocdeferred)
101 pend->clientdata->trigprocdeferred = pkg_queue_push(&deferred, pend);
102 debug(dbg_triggers, "trigproc_enqueue_deferred pend=%s",
103 pkg_name(pend, pnaw_always));
107 * Populate the deferred trigger queue.
109 * When dpkg is called with a specific set of packages to act on, we might
110 * have packages pending trigger processing. But because there are frontends
111 * that do not perform a final «dpkg --configure --pending» call (i.e. apt),
112 * the system is left in a state with packages not fully installed.
114 * We have to populate the deferred trigger queue from the entire package
115 * database, so that we might try to do opportunistic trigger processing
116 * when going through the deferred trigger queue, because a fixed apt will
117 * not request the necessary processing anyway.
119 * XXX: This can be removed once apt is fixed in the next stable release.
122 trigproc_populate_deferred(void)
124 struct pkgiterator *iter;
127 iter = pkg_db_iter_new();
128 while ((pkg = pkg_db_iter_next_pkg(iter))) {
129 if (!pkg->trigpend_head)
132 if (pkg->status != PKG_STAT_TRIGGERSAWAITED &&
133 pkg->status != PKG_STAT_TRIGGERSPENDING)
136 if (pkg->want != PKG_WANT_INSTALL)
139 trigproc_enqueue_deferred(pkg);
141 pkg_db_iter_free(iter);
145 trigproc_run_deferred(void)
149 debug(dbg_triggers, "trigproc_run_deferred");
150 while (!pkg_queue_is_empty(&deferred)) {
153 pkg = pkg_queue_pop(&deferred);
158 pop_error_context(ehflag_bombout);
161 push_error_context_jump(&ejbuf, print_error_perpackage,
162 pkg_name(pkg, pnaw_nonambig));
164 pkg->clientdata->trigprocdeferred = NULL;
165 trigproc(pkg, TRIGPROC_TRY);
167 pop_error_context(ehflag_normaltidy);
172 * Called by modstatdb_note.
175 trig_activate_packageprocessing(struct pkginfo *pkg)
177 debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
178 pkg_name(pkg, pnaw_always));
180 trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
181 NULL, trig_cicb_statuschange_activate,
182 pkg, &pkg->installed);
185 /*========== Actual trigger processing. ==========*/
187 struct trigcyclenode {
188 struct trigcyclenode *next;
189 struct trigcycleperpkg *pkgs;
190 struct pkginfo *then_processed;
193 struct trigcycleperpkg {
194 struct trigcycleperpkg *next;
196 struct trigpend *then_trigs;
199 static bool tortoise_advance;
200 static struct trigcyclenode *tortoise, *hare;
203 trigproc_reset_cycle(void)
205 tortoise_advance = false;
206 tortoise = hare = NULL;
210 tortoise_not_in_hare(struct pkginfo *processing_now,
211 struct trigcycleperpkg *tortoise_pkg)
213 const char *processing_now_name, *tortoise_name;
214 struct trigpend *hare_trig, *tortoise_trig;
216 processing_now_name = pkg_name(processing_now, pnaw_nonambig);
217 tortoise_name = pkg_name(tortoise_pkg->pkg, pnaw_nonambig);
219 debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s", __func__,
220 processing_now_name, tortoise_name);
221 for (tortoise_trig = tortoise_pkg->then_trigs;
223 tortoise_trig = tortoise_trig->next) {
224 debug(dbg_triggersdetail,
225 "%s pnow=%s tortoise=%s tortoisetrig=%s", __func__,
226 processing_now_name, tortoise_name, tortoise_trig->name);
228 /* hare is now so we can just look up in the actual data. */
229 for (hare_trig = tortoise_pkg->pkg->trigpend_head;
231 hare_trig = hare_trig->next) {
232 debug(dbg_triggersstupid, "%s pnow=%s tortoise=%s"
233 " tortoisetrig=%s haretrig=%s", __func__,
234 processing_now_name, tortoise_name,
235 tortoise_trig->name, hare_trig->name);
236 if (strcmp(hare_trig->name, tortoise_trig->name) == 0)
240 if (hare_trig == NULL) {
241 /* Not found in hare, yay! */
242 debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s OK",
243 __func__, processing_now_name, tortoise_name);
252 * Returns package we're to give up on.
254 static struct pkginfo *
255 check_trigger_cycle(struct pkginfo *processing_now)
257 struct trigcyclenode *tcn;
258 struct trigcycleperpkg *tcpp, *tortoise_pkg;
259 struct trigpend *tortoise_trig;
260 struct pkgiterator *iter;
261 struct pkginfo *pkg, *giveup;
264 debug(dbg_triggers, "check_triggers_cycle pnow=%s",
265 pkg_name(processing_now, pnaw_always));
267 tcn = nfmalloc(sizeof(*tcn));
269 tcn->then_processed = processing_now;
271 iter = pkg_db_iter_new();
272 while ((pkg = pkg_db_iter_next_pkg(iter))) {
273 if (!pkg->trigpend_head)
275 tcpp = nfmalloc(sizeof(*tcpp));
277 tcpp->then_trigs = pkg->trigpend_head;
278 tcpp->next = tcn->pkgs;
281 pkg_db_iter_free(iter);
283 debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
284 pkg_name(processing_now, pnaw_always));
286 hare = tortoise = tcn;
293 if (tortoise_advance)
294 tortoise = tortoise->next;
295 tortoise_advance = !tortoise_advance;
297 /* Now we compare hare to tortoise.
298 * We want to find a trigger pending in tortoise which is not in hare
299 * if we find such a thing we have proved that hare isn't a superset
300 * of tortoise and so that we haven't found a loop (yet). */
301 for (tortoise_pkg = tortoise->pkgs;
303 tortoise_pkg = tortoise_pkg->next) {
304 if (tortoise_not_in_hare(processing_now, tortoise_pkg))
307 /* Oh dear. hare is a superset of tortoise. We are making no
309 notice(_("cycle found while processing triggers:\n"
310 " chain of packages whose triggers are or may be responsible:"));
312 for (tcn = tortoise; tcn; tcn = tcn->next) {
313 fprintf(stderr, "%s%s", sep,
314 pkg_name(tcn->then_processed, pnaw_nonambig));
317 fprintf(stderr, _("\n" " packages' pending triggers which are"
318 " or may be unresolvable:\n"));
319 for (tortoise_pkg = tortoise->pkgs;
321 tortoise_pkg = tortoise_pkg->next) {
322 fprintf(stderr, " %s",
323 pkg_name(tortoise_pkg->pkg, pnaw_nonambig));
325 for (tortoise_trig = tortoise_pkg->then_trigs;
327 tortoise_trig = tortoise_trig->next) {
328 fprintf(stderr, "%s%s", sep, tortoise_trig->name);
330 fprintf(stderr, "\n");
333 /* We give up on the _earliest_ package involved. */
334 giveup = tortoise->pkgs->pkg;
335 debug(dbg_triggers, "check_triggers_cycle pnow=%s giveup=%s",
336 pkg_name(processing_now, pnaw_always),
337 pkg_name(giveup, pnaw_always));
338 assert(giveup->status == PKG_STAT_TRIGGERSAWAITED ||
339 giveup->status == PKG_STAT_TRIGGERSPENDING);
340 pkg_set_status(giveup, PKG_STAT_HALFCONFIGURED);
341 modstatdb_note(giveup);
342 print_error_perpackage(_("triggers looping, abandoned"),
343 pkg_name(giveup, pnaw_nonambig));
349 * Does cycle checking. Doesn't mind if pkg has no triggers pending - in
350 * that case does nothing but fix up any stale awaiters.
353 trigproc(struct pkginfo *pkg, enum trigproc_type type)
355 static struct varbuf namesarg;
357 struct varbuf depwhynot = VARBUF_INIT;
359 struct pkginfo *gaveup;
361 debug(dbg_triggers, "trigproc %s", pkg_name(pkg, pnaw_always));
363 if (pkg->clientdata->trigprocdeferred)
364 pkg->clientdata->trigprocdeferred->pkg = NULL;
365 pkg->clientdata->trigprocdeferred = NULL;
367 if (pkg->trigpend_head) {
370 assert(pkg->status == PKG_STAT_TRIGGERSPENDING ||
371 pkg->status == PKG_STAT_TRIGGERSAWAITED);
374 gaveup = check_trigger_cycle(pkg);
378 if (findbreakcycle(pkg))
382 ok = dependencies_ok(pkg, NULL, &depwhynot);
383 if (ok == DEP_CHECK_DEFER) {
384 varbuf_destroy(&depwhynot);
385 enqueue_package(pkg);
387 } else if (ok == DEP_CHECK_HALT) {
388 /* We cannot process this package on this dpkg run,
389 * and we can get here repeatedly if this package is
390 * required to make progress for other packages. So
391 * reset the trigger cycles tracking to avoid bogus
392 * cycle detections. */
393 trigproc_reset_cycle();
395 /* When doing opportunistic trigger processing, nothing
396 * requires us to be able to make progress; skip the
397 * package and silently ignore the error due to
398 * unsatisfiable dependencies. */
399 if (type == TRIGPROC_TRY) {
400 varbuf_destroy(&depwhynot);
405 varbuf_end_str(&depwhynot);
406 notice(_("dependency problems prevent processing "
407 "triggers for %s:\n%s"),
408 pkg_name(pkg, pnaw_nonambig), depwhynot.buf);
409 varbuf_destroy(&depwhynot);
410 ohshit(_("dependency problems - leaving triggers unprocessed"));
411 } else if (depwhynot.used) {
412 varbuf_end_str(&depwhynot);
413 notice(_("%s: dependency problems, but processing "
414 "triggers anyway as you requested:\n%s"),
415 pkg_name(pkg, pnaw_nonambig), depwhynot.buf);
416 varbuf_destroy(&depwhynot);
419 if (dependtry <= 1) {
420 gaveup = check_trigger_cycle(pkg);
425 printf(_("Processing triggers for %s (%s) ...\n"),
426 pkg_name(pkg, pnaw_nonambig),
427 versiondescribe(&pkg->installed.version, vdew_nonambig));
428 log_action("trigproc", pkg, &pkg->installed);
430 varbuf_reset(&namesarg);
431 for (tp = pkg->trigpend_head; tp; tp = tp->next) {
432 varbuf_add_char(&namesarg, ' ');
433 varbuf_add_str(&namesarg, tp->name);
435 varbuf_end_str(&namesarg);
437 /* Setting the status to half-configured
438 * causes modstatdb_note to clear pending triggers. */
439 pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
444 maintscript_postinst(pkg, "triggered",
445 namesarg.buf + 1, NULL);
448 post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
450 /* In other branch is done by modstatdb_note(), from inside
451 * post_postinst_tasks(). */
452 trig_clear_awaiters(pkg);
456 /*========== Transitional global activation. ==========*/
459 transitional_interest_callback_ro(const char *trig, struct pkginfo *pkg,
460 struct pkgbin *pkgbin, enum trig_options opts)
462 struct pkginfo *pend = pkg;
463 struct pkgbin *pendbin = pkgbin;
465 debug(dbg_triggersdetail,
466 "trig_transitional_interest_callback trig=%s pend=%s",
467 trig, pkgbin_name(pend, pendbin, pnaw_always));
468 if (pend->status >= PKG_STAT_TRIGGERSAWAITED)
469 trig_note_pend(pend, nfstrsave(trig));
473 transitional_interest_callback(const char *trig, struct pkginfo *pkg,
474 struct pkgbin *pkgbin, enum trig_options opts)
476 struct pkginfo *pend = pkg;
477 struct pkgbin *pendbin = pkgbin;
479 trig_cicb_interest_add(trig, pend, pendbin, opts);
480 transitional_interest_callback_ro(trig, pend, pendbin, opts);
484 * cstatus might be msdbrw_readonly if we're in --no-act mode, in which
485 * case we don't write out all of the interest files etc. but we do
486 * invent all of the activations for our own benefit.
489 trig_transitional_activate(enum modstatdb_rw cstatus)
491 struct pkgiterator *iter;
494 iter = pkg_db_iter_new();
495 while ((pkg = pkg_db_iter_next_pkg(iter))) {
496 if (pkg->status <= PKG_STAT_HALFINSTALLED)
498 debug(dbg_triggersdetail, "trig_transitional_activate %s %s",
499 pkg_name(pkg, pnaw_always),
500 pkg_status_name(pkg));
501 pkg->trigpend_head = NULL;
502 trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed,
504 cstatus >= msdbrw_write ?
505 transitional_interest_callback :
506 transitional_interest_callback_ro, NULL,
507 pkg, &pkg->installed);
508 /* Ensure we're not creating incoherent data that can't
509 * be written down. This should never happen in theory but
510 * can happen if you restore an old status file that is
511 * not in sync with the infodb files. */
512 if (pkg->status < PKG_STAT_TRIGGERSAWAITED)
515 if (pkg->trigaw.head)
516 pkg_set_status(pkg, PKG_STAT_TRIGGERSAWAITED);
517 else if (pkg->trigpend_head)
518 pkg_set_status(pkg, PKG_STAT_TRIGGERSPENDING);
520 pkg_set_status(pkg, PKG_STAT_INSTALLED);
522 pkg_db_iter_free(iter);
524 if (cstatus >= msdbrw_write) {
525 modstatdb_checkpoint();
526 trig_file_interests_save();
530 /*========== Hook setup. ==========*/
532 static struct filenamenode *
533 th_proper_nn_find(const char *name, bool nonew)
535 return findnamenode(name, nonew ? fnn_nonew : 0);
538 TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
540 static const struct trig_hooks trig_our_hooks = {
541 .enqueue_deferred = trigproc_enqueue_deferred,
542 .transitional_activate = trig_transitional_activate,
543 .namenode_find = th_proper_nn_find,
544 .namenode_interested = th_nn_interested,
545 .namenode_name = th_nn_name,
549 trigproc_install_hooks(void)
551 trig_override_hooks(&trig_our_hooks);