trigproc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. */
  80. /*========== deferred trigger queue ==========*/
  81. static struct pkg_queue deferred = PKG_QUEUE_INIT;
  82. static void
  83. trigproc_enqueue_deferred(struct pkginfo *pend)
  84. {
  85. if (f_triggers < 0)
  86. return;
  87. ensure_package_clientdata(pend);
  88. if (pend->clientdata->trigprocdeferred)
  89. return;
  90. pend->clientdata->trigprocdeferred = pkg_queue_push(&deferred, pend);
  91. debug(dbg_triggers, "trigproc_enqueue_deferred pend=%s", pend->name);
  92. }
  93. void
  94. trigproc_run_deferred(void)
  95. {
  96. jmp_buf ejbuf;
  97. debug(dbg_triggers, "trigproc_run_deferred");
  98. while (!pkg_queue_is_empty(&deferred)) {
  99. struct pkginfo *pkg;
  100. pkg = pkg_queue_pop(&deferred);
  101. if (!pkg)
  102. continue;
  103. if (setjmp(ejbuf)) {
  104. error_unwind(ehflag_bombout);
  105. continue;
  106. }
  107. push_error_handler(&ejbuf, print_error_perpackage, pkg->name);
  108. pkg->clientdata->trigprocdeferred = NULL;
  109. trigproc(pkg);
  110. set_error_display(NULL, NULL);
  111. error_unwind(ehflag_normaltidy);
  112. }
  113. }
  114. void
  115. trig_activate_packageprocessing(struct pkginfo *pkg)
  116. {
  117. debug(dbg_triggersdetail, "trigproc_activate_packageprocessing pkg=%s",
  118. pkg->name);
  119. trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE), NULL,
  120. trig_cicb_statuschange_activate, pkg);
  121. }
  122. /*========== actual trigger processing ==========*/
  123. struct trigcyclenode {
  124. struct trigcyclenode *next;
  125. struct trigcycleperpkg *pkgs;
  126. struct pkginfo *then_processed;
  127. };
  128. struct trigcycleperpkg {
  129. struct trigcycleperpkg *next;
  130. struct pkginfo *pkg;
  131. struct trigpend *then_trigs;
  132. };
  133. static int tortoise_advance;
  134. static struct trigcyclenode *tortoise, *hare;
  135. void
  136. trigproc_reset_cycle(void)
  137. {
  138. tortoise_advance = 0;
  139. tortoise = hare = NULL;
  140. }
  141. /* Returns package we're to give up on. */
  142. static struct pkginfo *
  143. check_trigger_cycle(struct pkginfo *processing_now)
  144. {
  145. struct trigcyclenode *tcn;
  146. struct trigcycleperpkg *tcpp, *tortoise_pkg;
  147. struct trigpend *hare_trig, *tortoise_trig;
  148. struct pkgiterator *it;
  149. struct pkginfo *pkg, *giveup;
  150. const char *sep;
  151. debug(dbg_triggers, "check_triggers_cycle pnow=%s", processing_now->name);
  152. tcn = nfmalloc(sizeof(*tcn));
  153. tcn->pkgs = NULL;
  154. tcn->then_processed = processing_now;
  155. it = iterpkgstart();
  156. while ((pkg = iterpkgnext(it))) {
  157. if (!pkg->trigpend_head)
  158. continue;
  159. tcpp = nfmalloc(sizeof(*tcpp));
  160. tcpp->pkg = pkg;
  161. tcpp->then_trigs = pkg->trigpend_head;
  162. tcpp->next = tcn->pkgs;
  163. tcn->pkgs = tcpp;
  164. }
  165. iterpkgend(it);
  166. if (!hare) {
  167. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s first",
  168. processing_now->name);
  169. tcn->next = NULL;
  170. hare = tortoise = tcn;
  171. return NULL;
  172. }
  173. tcn->next = NULL;
  174. hare->next = tcn;
  175. hare = tcn;
  176. if (tortoise_advance)
  177. tortoise = tortoise->next;
  178. tortoise_advance = !tortoise_advance;
  179. /* Now we compare hare to tortoise.
  180. * We want to find a trigger pending in tortoise which is not in hare
  181. * if we find such a thing we have proved that hare isn't a superset
  182. * of tortoise and so that we haven't found a loop (yet).
  183. */
  184. for (tortoise_pkg = tortoise->pkgs;
  185. tortoise_pkg;
  186. tortoise_pkg = tortoise_pkg->next) {
  187. debug(dbg_triggersdetail, "check_triggers_cycle pnow=%s tortoise=%s",
  188. processing_now->name, tortoise_pkg->pkg->name);
  189. for (tortoise_trig = tortoise_pkg->then_trigs;
  190. tortoise_trig;
  191. tortoise_trig = tortoise_trig->next) {
  192. debug(dbg_triggersdetail,
  193. "check_triggers_cycle pnow=%s tortoise=%s"
  194. " tortoisetrig=%s", processing_now->name,
  195. tortoise_pkg->pkg->name, tortoise_trig->name);
  196. /* hare is now so we can just look up in the actual
  197. * data. */
  198. for (hare_trig = tortoise_pkg->pkg->trigpend_head;
  199. hare_trig;
  200. hare_trig = hare_trig->next) {
  201. debug(dbg_triggersstupid,
  202. "check_triggers_cycle pnow=%s tortoise=%s"
  203. " tortoisetrig=%s haretrig=%s",
  204. processing_now->name, tortoise_pkg->pkg->name,
  205. tortoise_trig->name, hare_trig->name);
  206. if (!strcmp(hare_trig->name, tortoise_trig->name))
  207. goto found_in_hare;
  208. }
  209. /* Not found in hare, yay! */
  210. debug(dbg_triggersdetail,
  211. "check_triggers_cycle pnow=%s tortoise=%s OK",
  212. processing_now->name, tortoise_pkg->pkg->name);
  213. return NULL;
  214. found_in_hare:;
  215. }
  216. }
  217. /* Oh dear. hare is a superset of tortoise. We are making no progress. */
  218. fprintf(stderr, _("%s: cycle found while processing triggers:\n chain of"
  219. " packages whose triggers are or may be responsible:\n"),
  220. DPKG);
  221. sep = " ";
  222. for (tcn = tortoise; tcn; tcn = tcn->next) {
  223. fprintf(stderr, "%s%s", sep, tcn->then_processed->name);
  224. sep = " -> ";
  225. }
  226. fprintf(stderr, _("\n" " packages' pending triggers which are"
  227. " or may be unresolvable:\n"));
  228. for (tortoise_pkg = tortoise->pkgs;
  229. tortoise_pkg;
  230. tortoise_pkg = tortoise_pkg->next) {
  231. fprintf(stderr, " %s", tortoise_pkg->pkg->name);
  232. sep = ": ";
  233. for (tortoise_trig = tortoise_pkg->then_trigs;
  234. tortoise_trig;
  235. tortoise_trig = tortoise_trig->next) {
  236. fprintf(stderr, "%s%s", sep, tortoise_trig->name);
  237. }
  238. fprintf(stderr, "\n");
  239. }
  240. /* We give up on the _earliest_ package involved. */
  241. giveup = tortoise->pkgs->pkg;
  242. debug(dbg_triggers, "check_triggers_cycle pnow=%s giveup=%p",
  243. processing_now->name, giveup->name);
  244. assert(giveup->status == stat_triggersawaited ||
  245. giveup->status == stat_triggerspending);
  246. giveup->status = stat_halfconfigured;
  247. modstatdb_note(giveup);
  248. print_error_perpackage(_("triggers looping, abandoned"), giveup->name);
  249. return giveup;
  250. }
  251. void
  252. trigproc(struct pkginfo *pkg)
  253. {
  254. static struct varbuf namesarg;
  255. struct trigpend *tp;
  256. struct pkginfo *gaveup;
  257. debug(dbg_triggers, "trigproc %s", pkg->name);
  258. if (pkg->clientdata->trigprocdeferred)
  259. pkg->clientdata->trigprocdeferred->pkg = NULL;
  260. pkg->clientdata->trigprocdeferred = NULL;
  261. if (pkg->trigpend_head) {
  262. assert(pkg->status == stat_triggerspending ||
  263. pkg->status == stat_triggersawaited);
  264. gaveup = check_trigger_cycle(pkg);
  265. if (gaveup == pkg)
  266. return;
  267. printf(_("Processing triggers for %s ...\n"), pkg->name);
  268. log_action("trigproc", pkg);
  269. varbufreset(&namesarg);
  270. for (tp = pkg->trigpend_head; tp; tp = tp->next) {
  271. varbufaddc(&namesarg, ' ');
  272. varbufaddstr(&namesarg, tp->name);
  273. }
  274. varbufaddc(&namesarg, 0);
  275. /* Setting the status to halfconfigured
  276. * causes modstatdb_note to clear pending triggers.
  277. */
  278. pkg->status = stat_halfconfigured;
  279. modstatdb_note(pkg);
  280. if (!f_noact) {
  281. sincenothing = 0;
  282. maintainer_script_postinst(pkg, "triggered",
  283. namesarg.buf + 1, NULL);
  284. }
  285. /* This is to cope if the package triggers itself: */
  286. pkg->status = pkg->trigaw.head ? stat_triggersawaited :
  287. pkg->trigpend_head ? stat_triggerspending :
  288. stat_installed;
  289. post_postinst_tasks_core(pkg);
  290. } else {
  291. /* In other branch is done by modstatdb_note. */
  292. trig_clear_awaiters(pkg);
  293. }
  294. }
  295. /*========== transitional global activation ==========*/
  296. static void
  297. transitional_interest_callback_ro(const char *trig, void *user)
  298. {
  299. struct pkginfo *pend = user;
  300. debug(dbg_triggersdetail,
  301. "trig_transitional_interest_callback trig=%s pend=%s",
  302. trig, pend->name);
  303. if (pend->status >= stat_triggersawaited)
  304. trig_note_pend(pend, nfstrsave(trig));
  305. }
  306. static void
  307. transitional_interest_callback(const char *trig, void *user)
  308. {
  309. struct pkginfo *pend = user;
  310. trig_cicb_interest_add(trig, pend);
  311. transitional_interest_callback_ro(trig, user);
  312. }
  313. static void
  314. trig_transitional_activate(enum modstatdb_rw cstatus)
  315. {
  316. /* cstatus might be _read if we're in --no-act mode, in which
  317. * case we don't write out all of the interest files etc.
  318. * but we do invent all of the activations for our own benefit.
  319. */
  320. struct pkgiterator *it;
  321. struct pkginfo *pkg;
  322. it = iterpkgstart();
  323. while ((pkg = iterpkgnext(it))) {
  324. if (pkg->status <= stat_halfinstalled)
  325. continue;
  326. debug(dbg_triggersdetail, "trig_transitional_activate %s %s",
  327. pkg->name, statusinfos[pkg->status].name);
  328. pkg->trigpend_head = NULL;
  329. trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE),
  330. cstatus >= msdbrw_write ?
  331. transitional_interest_callback :
  332. transitional_interest_callback_ro, NULL, pkg);
  333. }
  334. iterpkgend(it);
  335. if (cstatus >= msdbrw_write) {
  336. modstatdb_checkpoint();
  337. trig_file_interests_save();
  338. }
  339. }
  340. /*========== hook setup ==========*/
  341. static struct filenamenode *
  342. th_proper_nn_find(const char *name, int nonew)
  343. {
  344. return findnamenode(name, nonew ? fnn_nonew : 0);
  345. }
  346. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  347. static const struct trig_hooks trig_our_hooks = {
  348. .enqueue_deferred = trigproc_enqueue_deferred,
  349. .transitional_activate = trig_transitional_activate,
  350. .namenode_find = th_proper_nn_find,
  351. .namenode_interested = th_nn_interested,
  352. .namenode_name = th_nn_name,
  353. };
  354. void
  355. trigproc_install_hooks(void)
  356. {
  357. trig_override_hooks(&trig_our_hooks);
  358. }