trigproc.c 11 KB

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