trigproc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/fcntl.h>
  24. #include <sys/stat.h>
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include <dpkg/pkg-queue.h>
  31. #include <dpkg/triglib.h>
  32. #include "main.h"
  33. #include "filesdb.h"
  34. /*
  35. * Trigger processing algorithms:
  36. *
  37. *
  38. * There is a separate queue (‘deferred trigproc list’) for triggers
  39. * ‘relevant’ to what we just did; when we find something triggered ‘now’
  40. * we add it to that queue (unless --no-triggers).
  41. *
  42. *
  43. * We want to prefer configuring packages where possible to doing
  44. * trigger processing, but we want to prefer trigger processing to
  45. * cycle-breaking and dependency forcing. This is achieved as
  46. * follows:
  47. *
  48. * Each time during configure processing where a package D is blocked by
  49. * only (ie Depends unsatisfied but would be satisfied by) a t-awaiter W
  50. * we make a note of (one of) W's t-pending, T. (Only the last such T.)
  51. * (If --no-triggers and nonempty argument list and package isn't in
  52. * argument list then we don't do this.)
  53. *
  54. * Each time in packages.c where we increment dependtry, we instead see
  55. * if we have encountered such a t-pending T. If we do, we trigproc T
  56. * instead of incrementing dependtry and this counts as having done
  57. * something so we reset sincenothing.
  58. *
  59. *
  60. * For --triggers-only and --configure, we go through each thing in the
  61. * argument queue (the add_to_queue queue) and check what its state is
  62. * and if appropriate we trigproc it. If we didn't have a queue (or had
  63. * just --pending) we search all triggers-pending packages and add them
  64. * to the deferred trigproc list.
  65. *
  66. *
  67. * Before quitting from most operations, we trigproc each package in the
  68. * deferred trigproc list. This may (if not --no-triggers) of course add
  69. * new things to the deferred trigproc list.
  70. *
  71. *
  72. * Note that ‘we trigproc T’ must involve trigger cycle detection and
  73. * also automatic setting of t-awaiters to t-pending or installed. In
  74. * particular, we do cycle detection even for trigger processing in the
  75. * configure dependtry loop (and it is OK to do it for explicitly
  76. * specified packages from the command line arguments; duplicates are
  77. * removed by packages.c:process_queue).
  78. */
  79. /*========== Deferred trigger queue. ==========*/
  80. static struct pkg_queue deferred = PKG_QUEUE_INIT;
  81. static void
  82. trigproc_enqueue_deferred(struct pkginfo *pend)
  83. {
  84. if (f_triggers < 0)
  85. return;
  86. ensure_package_clientdata(pend);
  87. if (pend->clientdata->trigprocdeferred)
  88. return;
  89. pend->clientdata->trigprocdeferred = pkg_queue_push(&deferred, pend);
  90. debug(dbg_triggers, "trigproc_enqueue_deferred pend=%s", pend->name);
  91. }
  92. void
  93. trigproc_run_deferred(void)
  94. {
  95. jmp_buf ejbuf;
  96. debug(dbg_triggers, "trigproc_run_deferred");
  97. while (!pkg_queue_is_empty(&deferred)) {
  98. struct pkginfo *pkg;
  99. pkg = pkg_queue_pop(&deferred);
  100. if (!pkg)
  101. continue;
  102. if (setjmp(ejbuf)) {
  103. pop_error_context(ehflag_bombout);
  104. continue;
  105. }
  106. push_error_context_jump(&ejbuf, print_error_perpackage,
  107. pkg->name);
  108. pkg->clientdata->trigprocdeferred = NULL;
  109. trigproc(pkg);
  110. pop_error_context(ehflag_normaltidy);
  111. }
  112. }
  113. /*
  114. * Called by modstatdb_note.
  115. */
  116. void
  117. trig_activate_packageprocessing(struct pkginfo *pkg)
  118. {
  119. debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
  120. pkg->name);
  121. trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE), NULL,
  122. trig_cicb_statuschange_activate, pkg);
  123. }
  124. /*========== Actual trigger processing. ==========*/
  125. struct trigcyclenode {
  126. struct trigcyclenode *next;
  127. struct trigcycleperpkg *pkgs;
  128. struct pkginfo *then_processed;
  129. };
  130. struct trigcycleperpkg {
  131. struct trigcycleperpkg *next;
  132. struct pkginfo *pkg;
  133. struct trigpend *then_trigs;
  134. };
  135. static bool tortoise_advance;
  136. static struct trigcyclenode *tortoise, *hare;
  137. void
  138. trigproc_reset_cycle(void)
  139. {
  140. tortoise_advance = false;
  141. tortoise = hare = NULL;
  142. }
  143. /*
  144. * Returns package we're to give up on.
  145. */
  146. static struct pkginfo *
  147. check_trigger_cycle(struct pkginfo *processing_now)
  148. {
  149. struct trigcyclenode *tcn;
  150. struct trigcycleperpkg *tcpp, *tortoise_pkg;
  151. struct trigpend *hare_trig, *tortoise_trig;
  152. struct pkgiterator *it;
  153. struct pkginfo *pkg, *giveup;
  154. const char *sep;
  155. debug(dbg_triggers, "check_triggers_cycle pnow=%s", processing_now->name);
  156. tcn = nfmalloc(sizeof(*tcn));
  157. tcn->pkgs = NULL;
  158. tcn->then_processed = processing_now;
  159. it = pkg_db_iter_new();
  160. while ((pkg = pkg_db_iter_next(it))) {
  161. if (!pkg->trigpend_head)
  162. continue;
  163. tcpp = nfmalloc(sizeof(*tcpp));
  164. tcpp->pkg = pkg;
  165. tcpp->then_trigs = pkg->trigpend_head;
  166. tcpp->next = tcn->pkgs;
  167. tcn->pkgs = tcpp;
  168. }
  169. pkg_db_iter_free(it);
  170. if (!hare) {
  171. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
  172. processing_now->name);
  173. tcn->next = NULL;
  174. hare = tortoise = tcn;
  175. return NULL;
  176. }
  177. tcn->next = NULL;
  178. hare->next = tcn;
  179. hare = tcn;
  180. if (tortoise_advance)
  181. tortoise = tortoise->next;
  182. tortoise_advance = !tortoise_advance;
  183. /* Now we compare hare to tortoise.
  184. * We want to find a trigger pending in tortoise which is not in hare
  185. * if we find such a thing we have proved that hare isn't a superset
  186. * of tortoise and so that we haven't found a loop (yet). */
  187. for (tortoise_pkg = tortoise->pkgs;
  188. tortoise_pkg;
  189. tortoise_pkg = tortoise_pkg->next) {
  190. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s tortoise=%s",
  191. processing_now->name, tortoise_pkg->pkg->name);
  192. for (tortoise_trig = tortoise_pkg->then_trigs;
  193. tortoise_trig;
  194. tortoise_trig = tortoise_trig->next) {
  195. debug(dbg_triggersdetail,
  196. "check_triggers_cycle pnow=%s tortoise=%s"
  197. " tortoisetrig=%s", processing_now->name,
  198. tortoise_pkg->pkg->name, tortoise_trig->name);
  199. /* hare is now so we can just look up in the actual
  200. * data. */
  201. for (hare_trig = tortoise_pkg->pkg->trigpend_head;
  202. hare_trig;
  203. hare_trig = hare_trig->next) {
  204. debug(dbg_triggersstupid,
  205. "check_triggers_cycle pnow=%s tortoise=%s"
  206. " tortoisetrig=%s haretrig=%s",
  207. processing_now->name, tortoise_pkg->pkg->name,
  208. tortoise_trig->name, hare_trig->name);
  209. if (!strcmp(hare_trig->name, tortoise_trig->name))
  210. goto found_in_hare;
  211. }
  212. /* Not found in hare, yay! */
  213. debug(dbg_triggersdetail,
  214. "check_triggers_cycle pnow=%s tortoise=%s OK",
  215. processing_now->name, tortoise_pkg->pkg->name);
  216. return NULL;
  217. found_in_hare:;
  218. }
  219. }
  220. /* Oh dear. hare is a superset of tortoise. We are making no
  221. * progress. */
  222. fprintf(stderr, _("%s: cycle found while processing triggers:\n chain of"
  223. " packages whose triggers are or may be responsible:\n"),
  224. thisname);
  225. sep = " ";
  226. for (tcn = tortoise; tcn; tcn = tcn->next) {
  227. fprintf(stderr, "%s%s", sep, tcn->then_processed->name);
  228. sep = " -> ";
  229. }
  230. fprintf(stderr, _("\n" " packages' pending triggers which are"
  231. " or may be unresolvable:\n"));
  232. for (tortoise_pkg = tortoise->pkgs;
  233. tortoise_pkg;
  234. tortoise_pkg = tortoise_pkg->next) {
  235. fprintf(stderr, " %s", tortoise_pkg->pkg->name);
  236. sep = ": ";
  237. for (tortoise_trig = tortoise_pkg->then_trigs;
  238. tortoise_trig;
  239. tortoise_trig = tortoise_trig->next) {
  240. fprintf(stderr, "%s%s", sep, tortoise_trig->name);
  241. }
  242. fprintf(stderr, "\n");
  243. }
  244. /* We give up on the _earliest_ package involved. */
  245. giveup = tortoise->pkgs->pkg;
  246. debug(dbg_triggers, "check_triggers_cycle pnow=%s giveup=%p",
  247. processing_now->name, giveup->name);
  248. assert(giveup->status == stat_triggersawaited ||
  249. giveup->status == stat_triggerspending);
  250. giveup->status = stat_halfconfigured;
  251. modstatdb_note(giveup);
  252. print_error_perpackage(_("triggers looping, abandoned"), giveup->name);
  253. return giveup;
  254. }
  255. /*
  256. * Does cycle checking. Doesn't mind if pkg has no triggers pending - in
  257. * that case does nothing but fix up any stale awaiters.
  258. */
  259. void
  260. trigproc(struct pkginfo *pkg)
  261. {
  262. static struct varbuf namesarg;
  263. struct trigpend *tp;
  264. struct pkginfo *gaveup;
  265. debug(dbg_triggers, "trigproc %s", pkg->name);
  266. if (pkg->clientdata->trigprocdeferred)
  267. pkg->clientdata->trigprocdeferred->pkg = NULL;
  268. pkg->clientdata->trigprocdeferred = NULL;
  269. if (pkg->trigpend_head) {
  270. assert(pkg->status == stat_triggerspending ||
  271. pkg->status == stat_triggersawaited);
  272. gaveup = check_trigger_cycle(pkg);
  273. if (gaveup == pkg)
  274. return;
  275. printf(_("Processing triggers for %s ...\n"), pkg->name);
  276. log_action("trigproc", pkg);
  277. varbuf_reset(&namesarg);
  278. for (tp = pkg->trigpend_head; tp; tp = tp->next) {
  279. varbuf_add_char(&namesarg, ' ');
  280. varbuf_add_str(&namesarg, tp->name);
  281. }
  282. varbuf_end_str(&namesarg);
  283. /* Setting the status to half-configured
  284. * causes modstatdb_note to clear pending triggers. */
  285. pkg->status = stat_halfconfigured;
  286. modstatdb_note(pkg);
  287. if (!f_noact) {
  288. sincenothing = 0;
  289. maintainer_script_postinst(pkg, "triggered",
  290. namesarg.buf + 1, NULL);
  291. }
  292. /* This is to cope if the package triggers itself: */
  293. pkg->status = pkg->trigaw.head ? stat_triggersawaited :
  294. pkg->trigpend_head ? stat_triggerspending :
  295. stat_installed;
  296. post_postinst_tasks_core(pkg);
  297. } else {
  298. /* In other branch is done by modstatdb_note. */
  299. trig_clear_awaiters(pkg);
  300. }
  301. }
  302. /*========== Transitional global activation. ==========*/
  303. static void
  304. transitional_interest_callback_ro(const char *trig, void *user)
  305. {
  306. struct pkginfo *pend = user;
  307. debug(dbg_triggersdetail,
  308. "trig_transitional_interest_callback trig=%s pend=%s",
  309. trig, pend->name);
  310. if (pend->status >= stat_triggersawaited)
  311. trig_note_pend(pend, nfstrsave(trig));
  312. }
  313. static void
  314. transitional_interest_callback(const char *trig, void *user)
  315. {
  316. struct pkginfo *pend = user;
  317. trig_cicb_interest_add(trig, pend);
  318. transitional_interest_callback_ro(trig, user);
  319. }
  320. /*
  321. * cstatus might be msdbrw_readonly if we're in --no-act mode, in which
  322. * case we don't write out all of the interest files etc. but we do
  323. * invent all of the activations for our own benefit.
  324. */
  325. static void
  326. trig_transitional_activate(enum modstatdb_rw cstatus)
  327. {
  328. struct pkgiterator *it;
  329. struct pkginfo *pkg;
  330. it = pkg_db_iter_new();
  331. while ((pkg = pkg_db_iter_next(it))) {
  332. if (pkg->status <= stat_halfinstalled)
  333. continue;
  334. debug(dbg_triggersdetail, "trig_transitional_activate %s %s",
  335. pkg->name, statusinfos[pkg->status].name);
  336. pkg->trigpend_head = NULL;
  337. trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE),
  338. cstatus >= msdbrw_write ?
  339. transitional_interest_callback :
  340. transitional_interest_callback_ro, NULL, pkg);
  341. }
  342. pkg_db_iter_free(it);
  343. if (cstatus >= msdbrw_write) {
  344. modstatdb_checkpoint();
  345. trig_file_interests_save();
  346. }
  347. }
  348. /*========== Hook setup. ==========*/
  349. static struct filenamenode *
  350. th_proper_nn_find(const char *name, bool nonew)
  351. {
  352. return findnamenode(name, nonew ? fnn_nonew : 0);
  353. }
  354. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  355. static const struct trig_hooks trig_our_hooks = {
  356. .enqueue_deferred = trigproc_enqueue_deferred,
  357. .transitional_activate = trig_transitional_activate,
  358. .namenode_find = th_proper_nn_find,
  359. .namenode_interested = th_nn_interested,
  360. .namenode_name = th_nn_name,
  361. };
  362. void
  363. trigproc_install_hooks(void)
  364. {
  365. trig_override_hooks(&trig_our_hooks);
  366. }