trigproc.c 11 KB

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