chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / src / trigproc.c
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 <assert.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31
32 #include <dpkg/i18n.h>
33 #include <dpkg/dpkg.h>
34 #include <dpkg/dpkg-db.h>
35 #include <dpkg/pkg.h>
36 #include <dpkg/pkg-queue.h>
37 #include <dpkg/triglib.h>
38
39 #include "main.h"
40 #include "filesdb.h"
41 #include "infodb.h"
42
43 /*
44  * Trigger processing algorithms:
45  *
46  *
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).
50  *
51  *
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
55  * 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 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.
67  *
68  *
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.
74  *
75  *
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.
79  *
80  *
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).
87  */
88
89 /*========== Deferred trigger queue. ==========*/
90
91 static struct pkg_queue deferred = PKG_QUEUE_INIT;
92
93 static void
94 trigproc_enqueue_deferred(struct pkginfo *pend)
95 {
96         if (f_triggers < 0)
97                 return;
98         ensure_package_clientdata(pend);
99         if (pend->clientdata->trigprocdeferred)
100                 return;
101         pend->clientdata->trigprocdeferred = pkg_queue_push(&deferred, pend);
102         debug(dbg_triggers, "trigproc_enqueue_deferred pend=%s",
103               pkg_name(pend, pnaw_always));
104 }
105
106 /**
107  * Populate the deferred trigger queue.
108  *
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.
113  *
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.
118  *
119  * XXX: This can be removed once apt is fixed in the next stable release.
120  */
121 void
122 trigproc_populate_deferred(void)
123 {
124         struct pkgiterator *iter;
125         struct pkginfo *pkg;
126
127         iter = pkg_db_iter_new();
128         while ((pkg = pkg_db_iter_next_pkg(iter))) {
129                 if (!pkg->trigpend_head)
130                         continue;
131
132                 if (pkg->status != PKG_STAT_TRIGGERSAWAITED &&
133                     pkg->status != PKG_STAT_TRIGGERSPENDING)
134                         continue;
135
136                 if (pkg->want != PKG_WANT_INSTALL)
137                         continue;
138
139                 trigproc_enqueue_deferred(pkg);
140         }
141         pkg_db_iter_free(iter);
142 }
143
144 void
145 trigproc_run_deferred(void)
146 {
147         jmp_buf ejbuf;
148
149         debug(dbg_triggers, "trigproc_run_deferred");
150         while (!pkg_queue_is_empty(&deferred)) {
151                 struct pkginfo *pkg;
152
153                 pkg  = pkg_queue_pop(&deferred);
154                 if (!pkg)
155                         continue;
156
157                 if (setjmp(ejbuf)) {
158                         pop_error_context(ehflag_bombout);
159                         continue;
160                 }
161                 push_error_context_jump(&ejbuf, print_error_perpackage,
162                                         pkg_name(pkg, pnaw_nonambig));
163
164                 pkg->clientdata->trigprocdeferred = NULL;
165                 trigproc(pkg, TRIGPROC_TRY);
166
167                 pop_error_context(ehflag_normaltidy);
168         }
169 }
170
171 /*
172  * Called by modstatdb_note.
173  */
174 void
175 trig_activate_packageprocessing(struct pkginfo *pkg)
176 {
177         debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
178               pkg_name(pkg, pnaw_always));
179
180         trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
181                       NULL, trig_cicb_statuschange_activate,
182                       pkg, &pkg->installed);
183 }
184
185 /*========== Actual trigger processing. ==========*/
186
187 struct trigcyclenode {
188         struct trigcyclenode *next;
189         struct trigcycleperpkg *pkgs;
190         struct pkginfo *then_processed;
191 };
192
193 struct trigcycleperpkg {
194         struct trigcycleperpkg *next;
195         struct pkginfo *pkg;
196         struct trigpend *then_trigs;
197 };
198
199 static bool tortoise_advance;
200 static struct trigcyclenode *tortoise, *hare;
201
202 void
203 trigproc_reset_cycle(void)
204 {
205         tortoise_advance = false;
206         tortoise = hare = NULL;
207 }
208
209 static bool
210 tortoise_not_in_hare(struct pkginfo *processing_now,
211                      struct trigcycleperpkg *tortoise_pkg)
212 {
213         const char *processing_now_name, *tortoise_name;
214         struct trigpend *hare_trig, *tortoise_trig;
215
216         processing_now_name = pkg_name(processing_now, pnaw_nonambig);
217         tortoise_name = pkg_name(tortoise_pkg->pkg, pnaw_nonambig);
218
219         debug(dbg_triggersdetail, "%s pnow=%s tortoise=%s", __func__,
220               processing_now_name, tortoise_name);
221         for (tortoise_trig = tortoise_pkg->then_trigs;
222              tortoise_trig;
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);
227
228                 /* hare is now so we can just look up in the actual data. */
229                 for (hare_trig = tortoise_pkg->pkg->trigpend_head;
230                      hare_trig;
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)
237                                 break;
238                 }
239
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);
244                         return true;
245                 }
246         }
247
248         return false;
249 }
250
251 /*
252  * Returns package we're to give up on.
253  */
254 static struct pkginfo *
255 check_trigger_cycle(struct pkginfo *processing_now)
256 {
257         struct trigcyclenode *tcn;
258         struct trigcycleperpkg *tcpp, *tortoise_pkg;
259         struct trigpend *tortoise_trig;
260         struct pkgiterator *iter;
261         struct pkginfo *pkg, *giveup;
262         const char *sep;
263
264         debug(dbg_triggers, "check_triggers_cycle pnow=%s",
265               pkg_name(processing_now, pnaw_always));
266
267         tcn = nfmalloc(sizeof(*tcn));
268         tcn->pkgs = NULL;
269         tcn->then_processed = processing_now;
270
271         iter = pkg_db_iter_new();
272         while ((pkg = pkg_db_iter_next_pkg(iter))) {
273                 if (!pkg->trigpend_head)
274                         continue;
275                 tcpp = nfmalloc(sizeof(*tcpp));
276                 tcpp->pkg = pkg;
277                 tcpp->then_trigs = pkg->trigpend_head;
278                 tcpp->next = tcn->pkgs;
279                 tcn->pkgs = tcpp;
280         }
281         pkg_db_iter_free(iter);
282         if (!hare) {
283                 debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
284                       pkg_name(processing_now, pnaw_always));
285                 tcn->next = NULL;
286                 hare = tortoise = tcn;
287                 return NULL;
288         }
289
290         tcn->next = NULL;
291         hare->next = tcn;
292         hare = tcn;
293         if (tortoise_advance)
294                 tortoise = tortoise->next;
295         tortoise_advance = !tortoise_advance;
296
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;
302              tortoise_pkg;
303              tortoise_pkg = tortoise_pkg->next) {
304                 if (tortoise_not_in_hare(processing_now, tortoise_pkg))
305                         return NULL;
306         }
307         /* Oh dear. hare is a superset of tortoise. We are making no
308          * progress. */
309         notice(_("cycle found while processing triggers:\n"
310                  " chain of packages whose triggers are or may be responsible:"));
311         sep = "  ";
312         for (tcn = tortoise; tcn; tcn = tcn->next) {
313                 fprintf(stderr, "%s%s", sep,
314                         pkg_name(tcn->then_processed, pnaw_nonambig));
315                 sep = " -> ";
316         }
317         fprintf(stderr, _("\n" " packages' pending triggers which are"
318                           " or may be unresolvable:\n"));
319         for (tortoise_pkg = tortoise->pkgs;
320              tortoise_pkg;
321              tortoise_pkg = tortoise_pkg->next) {
322                 fprintf(stderr, "  %s",
323                         pkg_name(tortoise_pkg->pkg, pnaw_nonambig));
324                 sep = ": ";
325                 for (tortoise_trig = tortoise_pkg->then_trigs;
326                      tortoise_trig;
327                      tortoise_trig = tortoise_trig->next) {
328                         fprintf(stderr, "%s%s", sep, tortoise_trig->name);
329                 }
330                 fprintf(stderr, "\n");
331         }
332
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));
344
345         return giveup;
346 }
347
348 /*
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.
351  */
352 void
353 trigproc(struct pkginfo *pkg, enum trigproc_type type)
354 {
355         static struct varbuf namesarg;
356
357         struct varbuf depwhynot = VARBUF_INIT;
358         struct trigpend *tp;
359         struct pkginfo *gaveup;
360
361         debug(dbg_triggers, "trigproc %s", pkg_name(pkg, pnaw_always));
362
363         if (pkg->clientdata->trigprocdeferred)
364                 pkg->clientdata->trigprocdeferred->pkg = NULL;
365         pkg->clientdata->trigprocdeferred = NULL;
366
367         if (pkg->trigpend_head) {
368                 enum dep_check ok;
369
370                 assert(pkg->status == PKG_STAT_TRIGGERSPENDING ||
371                        pkg->status == PKG_STAT_TRIGGERSAWAITED);
372
373                 if (dependtry > 1) {
374                         gaveup = check_trigger_cycle(pkg);
375                         if (gaveup == pkg)
376                                 return;
377
378                         if (findbreakcycle(pkg))
379                                 sincenothing = 0;
380                 }
381
382                 ok = dependencies_ok(pkg, NULL, &depwhynot);
383                 if (ok == DEP_CHECK_DEFER) {
384                         varbuf_destroy(&depwhynot);
385                         enqueue_package(pkg);
386                         return;
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();
394
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);
401                                 return;
402                         }
403
404                         sincenothing = 0;
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);
417                 }
418
419                 if (dependtry <= 1) {
420                         gaveup = check_trigger_cycle(pkg);
421                         if (gaveup == pkg)
422                                 return;
423                 }
424
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);
429
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);
434                 }
435                 varbuf_end_str(&namesarg);
436
437                 /* Setting the status to half-configured
438                  * causes modstatdb_note to clear pending triggers. */
439                 pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
440                 modstatdb_note(pkg);
441
442                 if (!f_noact) {
443                         sincenothing = 0;
444                         maintscript_postinst(pkg, "triggered",
445                                              namesarg.buf + 1, NULL);
446                 }
447
448                 post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
449         } else {
450                 /* In other branch is done by modstatdb_note(), from inside
451                  * post_postinst_tasks(). */
452                 trig_clear_awaiters(pkg);
453         }
454 }
455
456 /*========== Transitional global activation. ==========*/
457
458 static void
459 transitional_interest_callback_ro(const char *trig, struct pkginfo *pkg,
460                                   struct pkgbin *pkgbin, enum trig_options opts)
461 {
462         struct pkginfo *pend = pkg;
463         struct pkgbin *pendbin = pkgbin;
464
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));
470 }
471
472 static void
473 transitional_interest_callback(const char *trig, struct pkginfo *pkg,
474                                struct pkgbin *pkgbin, enum trig_options opts)
475 {
476         struct pkginfo *pend = pkg;
477         struct pkgbin *pendbin = pkgbin;
478
479         trig_cicb_interest_add(trig, pend, pendbin, opts);
480         transitional_interest_callback_ro(trig, pend, pendbin, opts);
481 }
482
483 /*
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.
487  */
488 static void
489 trig_transitional_activate(enum modstatdb_rw cstatus)
490 {
491         struct pkgiterator *iter;
492         struct pkginfo *pkg;
493
494         iter = pkg_db_iter_new();
495         while ((pkg = pkg_db_iter_next_pkg(iter))) {
496                 if (pkg->status <= PKG_STAT_HALFINSTALLED)
497                         continue;
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,
503                                                   TRIGGERSCIFILE),
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)
513                         continue;
514
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);
519                 else
520                         pkg_set_status(pkg, PKG_STAT_INSTALLED);
521         }
522         pkg_db_iter_free(iter);
523
524         if (cstatus >= msdbrw_write) {
525                 modstatdb_checkpoint();
526                 trig_file_interests_save();
527         }
528 }
529
530 /*========== Hook setup. ==========*/
531
532 static struct filenamenode *
533 th_proper_nn_find(const char *name, bool nonew)
534 {
535         return findnamenode(name, nonew ? fnn_nonew : 0);
536 }
537
538 TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
539
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,
546 };
547
548 void
549 trigproc_install_hooks(void)
550 {
551         trig_override_hooks(&trig_our_hooks);
552 }