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