trigproc.c 11 KB

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