triglib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * triglib.c - trigger handling
  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 <unistd.h>
  27. #include <errno.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <dpkg.h>
  31. #include <dpkg-db.h>
  32. #include <dlist.h>
  33. const char *
  34. illegal_triggername(const char *p)
  35. {
  36. int c;
  37. if (!*p)
  38. return _("empty trigger names are not permitted");
  39. while ((c = *p++)) {
  40. if (c <= ' ' || c >= 0177)
  41. return _("trigger name contains invalid character");
  42. }
  43. return NULL;
  44. }
  45. /*========== recording triggers ==========*/
  46. /*---------- noting trigger activation in memory ----------*/
  47. /* Called via trig_*activate* et al from:
  48. * - trig_incorporate: reading of Unincorp (explicit trigger activations)
  49. * - various places: processing start (`activate' in triggers ci file)
  50. * - namenodetouse: file triggers during unpack / remove
  51. * - deferred_configure: file triggers during config file processing
  52. *
  53. * Not called from trig_transitional_activation; that runs
  54. * trig_note_pend directly which means that (a) awaiters are not
  55. * recorded (how would we know?) and (b) we don't enqueue them for
  56. * deferred processing in this run.
  57. *
  58. * We add the trigger to Triggers-Pending first. This makes it
  59. * harder to get into the state where Triggers-Awaited for aw lists
  60. * pend but Triggers-Pending for pend is empty. (See also the
  61. * comment in deppossi_ok_found regarding this situation.)
  62. */
  63. /* aw might be NULL, and trig is not copied! */
  64. static void
  65. trig_record_activation(struct pkginfo *pend, struct pkginfo *aw, const char *trig)
  66. {
  67. if (pend->status < stat_triggersawaited)
  68. /* Not interested then. */
  69. return;
  70. if (trig_note_pend(pend, trig))
  71. modstatdb_note_ifwrite(pend);
  72. if (trigh.enqueue_deferred)
  73. trigh.enqueue_deferred(pend);
  74. if (aw && pend->status > stat_configfiles)
  75. if (trig_note_aw(pend, aw)) {
  76. if (aw->status > stat_triggersawaited)
  77. aw->status = stat_triggersawaited;
  78. modstatdb_note_ifwrite(aw);
  79. }
  80. }
  81. /* NB that this is also called from fields.c where *pend is a temporary! */
  82. int
  83. trig_note_pend_core(struct pkginfo *pend, const char *trig /*not copied!*/)
  84. {
  85. struct trigpend *tp;
  86. for (tp = pend->trigpend_head; tp; tp = tp->next)
  87. if (!strcmp(tp->name, trig))
  88. return 0;
  89. tp = nfmalloc(sizeof(*tp));
  90. tp->name = trig;
  91. tp->next = pend->trigpend_head;
  92. pend->trigpend_head = tp;
  93. return 1;
  94. }
  95. /* Returns: 1 for done, 0 for already noted. */
  96. int
  97. trig_note_pend(struct pkginfo *pend, const char *trig /*not copied!*/)
  98. {
  99. if (!trig_note_pend_core(pend, trig))
  100. return 0;
  101. pend->status = pend->trigaw.head ? stat_triggersawaited :
  102. stat_triggerspending;
  103. return 1;
  104. }
  105. /* Returns: 1 for done, 0 for already noted. */
  106. /* NB that this is called also from fields.c where *aw is a temporary
  107. * but pend is from findpackage()! */
  108. int
  109. trig_note_aw(struct pkginfo *pend, struct pkginfo *aw)
  110. {
  111. struct trigaw *ta;
  112. /* We search through aw's list because that's probably shorter. */
  113. for (ta = aw->trigaw.head; ta; ta = ta->sameaw.next)
  114. if (ta->pend == pend)
  115. return 0;
  116. ta = nfmalloc(sizeof(*ta));
  117. ta->aw = aw;
  118. ta->pend = pend;
  119. ta->nextsamepend = pend->othertrigaw_head;
  120. pend->othertrigaw_head = ta;
  121. LIST_LINK_TAIL_PART(aw->trigaw, ta, sameaw.);
  122. return 1;
  123. }
  124. void
  125. trig_clear_awaiters(struct pkginfo *notpend)
  126. {
  127. struct trigaw *ta;
  128. struct pkginfo *aw;
  129. assert(!notpend->trigpend_head);
  130. ta = notpend->othertrigaw_head;
  131. notpend->othertrigaw_head = NULL;
  132. for (; ta; ta = ta->nextsamepend) {
  133. aw = ta->aw;
  134. if (!aw)
  135. continue;
  136. LIST_UNLINK_PART(aw->trigaw, ta, sameaw.);
  137. if (!aw->trigaw.head && aw->status == stat_triggersawaited) {
  138. aw->status = aw->trigpend_head ? stat_triggerspending :
  139. stat_installed;
  140. modstatdb_note(aw);
  141. }
  142. }
  143. }
  144. /* FIXME: switch to use the generic pkg list for lenny+1 */
  145. struct pkg_list {
  146. struct pkg_list *next;
  147. struct pkginfo *pkg;
  148. };
  149. static struct pkg_list *trig_awaited_pend_head;
  150. void
  151. trig_enqueue_awaited_pend(struct pkginfo *pend)
  152. {
  153. struct pkg_list *tp;
  154. for (tp = trig_awaited_pend_head; tp; tp = tp->next)
  155. if (tp->pkg == pend)
  156. return;
  157. tp = nfmalloc(sizeof(*tp));
  158. tp->pkg = pend;
  159. tp->next = trig_awaited_pend_head;
  160. trig_awaited_pend_head = tp;
  161. }
  162. /*
  163. * Fix up packages in state triggers-awaited w/o the corresponding package
  164. * with pending triggers. This can happen when dpkg was interrupted
  165. * while in modstatdb_note, and the package in triggers-pending had its
  166. * state modified but dpkg could not clearing the awaiters.
  167. *
  168. * XXX: possibly get rid of some of the checks done somewhere else for
  169. * this condition at run-time.
  170. */
  171. void
  172. trig_fixup_awaiters(enum modstatdb_rw cstatus)
  173. {
  174. struct pkg_list *tp;
  175. if (cstatus < msdbrw_write)
  176. return;
  177. for (tp = trig_awaited_pend_head; tp; tp = tp->next)
  178. if (!tp->pkg->trigpend_head)
  179. trig_clear_awaiters(tp->pkg);
  180. trig_awaited_pend_head = NULL;
  181. }
  182. /*---------- generalised handling of trigger kinds ----------*/
  183. static const struct trigkindinfo tki_explicit, tki_file, tki_unknown;
  184. struct trigkindinfo {
  185. /* Only for trig_activate_start. */
  186. void (*activate_start)(void);
  187. /* Rest are for everyone: */
  188. void (*activate_awaiter)(struct pkginfo *pkg /* may be NULL */);
  189. void (*activate_done)(void);
  190. void (*interest_change)(const char *name, struct pkginfo *pkg, int signum);
  191. };
  192. #define TKI_DEFINE(kindname) \
  193. static const struct trigkindinfo tki_##kindname= { \
  194. trk_##kindname##_activate_start, \
  195. trk_##kindname##_activate_awaiter, \
  196. trk_##kindname##_activate_done, \
  197. trk_##kindname##_interest_change \
  198. };
  199. static const struct trigkindinfo *dtki;
  200. /* As passed into activate_start. */
  201. static const char *trig_activating_name;
  202. static const struct trigkindinfo *
  203. trig_classify_byname(const char *name)
  204. {
  205. if (name[0] == '/') {
  206. const char *slash;
  207. slash = name;
  208. while (slash) {
  209. if (slash[1] == '\0' || slash[1] == '/')
  210. goto invalid;
  211. slash = strchr(slash + 2, '/');
  212. }
  213. return &tki_file;
  214. }
  215. if (!illegal_packagename(name, NULL) && !strchr(name, '_'))
  216. return &tki_explicit;
  217. invalid:
  218. return &tki_unknown;
  219. }
  220. /* Calling sequence is:
  221. * trig_activate_start(triggername)
  222. * dtki->activate_awaiter(awaiting_package) } zero or more times
  223. * dtki->activate_awaiter(0) } in any order
  224. * dtki->activate_done(0)
  225. */
  226. static void
  227. trig_activate_start(const char *name)
  228. {
  229. dtki = trig_classify_byname(name);
  230. trig_activating_name = name;
  231. dtki->activate_start();
  232. }
  233. /*---------- unknown trigger kinds ----------*/
  234. static void
  235. trk_unknown_activate_start(void)
  236. {
  237. }
  238. static void
  239. trk_unknown_activate_awaiter(struct pkginfo *aw)
  240. {
  241. }
  242. static void
  243. trk_unknown_activate_done(void)
  244. {
  245. }
  246. static void
  247. trk_unknown_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  248. {
  249. ohshit(_("invalid or unknown syntax in trigger name `%.250s'"
  250. " (in trigger interests for package `%.250s')"),
  251. trig, pkg->name);
  252. }
  253. TKI_DEFINE(unknown);
  254. /*---------- explicit triggers ----------*/
  255. static FILE *trk_explicit_f;
  256. static struct varbuf trk_explicit_fn;
  257. static char *trk_explicit_trig;
  258. static void
  259. trk_explicit_activate_done(void)
  260. {
  261. if (trk_explicit_f) {
  262. fclose(trk_explicit_f);
  263. trk_explicit_f = NULL;
  264. }
  265. }
  266. static void
  267. trk_explicit_start(const char *trig)
  268. {
  269. trk_explicit_activate_done();
  270. varbufreset(&trk_explicit_fn);
  271. varbufaddstr(&trk_explicit_fn, triggersdir);
  272. varbufaddstr(&trk_explicit_fn, trig);
  273. varbufaddc(&trk_explicit_fn, 0);
  274. trk_explicit_f = fopen(trk_explicit_fn.buf, "r");
  275. if (!trk_explicit_f) {
  276. if (errno != ENOENT)
  277. ohshite(_("failed to open trigger interest list file `%.250s'"),
  278. trk_explicit_fn.buf);
  279. }
  280. }
  281. static int
  282. trk_explicit_fgets(char *buf, size_t sz)
  283. {
  284. return fgets_checked(buf, sz, trk_explicit_f, trk_explicit_fn.buf);
  285. }
  286. static void
  287. trk_explicit_activate_start(void)
  288. {
  289. trk_explicit_start(trig_activating_name);
  290. trk_explicit_trig = nfstrsave(trig_activating_name);
  291. }
  292. static void
  293. trk_explicit_activate_awaiter(struct pkginfo *aw)
  294. {
  295. char buf[1024];
  296. const char *emsg;
  297. struct pkginfo *pend;
  298. if (!trk_explicit_f)
  299. return;
  300. if (fseek(trk_explicit_f, 0, SEEK_SET))
  301. ohshite(_("failed to rewind trigger interest file `%.250s'"),
  302. trk_explicit_fn.buf);
  303. while (trk_explicit_fgets(buf, sizeof(buf)) >= 0) {
  304. if ((emsg = illegal_packagename(buf, NULL)))
  305. ohshit(_("trigger interest file `%.250s' syntax error; "
  306. "illegal package name `%.250s': %.250s"),
  307. trk_explicit_fn.buf, buf, emsg);
  308. pend = findpackage(buf);
  309. trig_record_activation(pend, aw, trk_explicit_trig);
  310. }
  311. }
  312. static void
  313. trk_explicit_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  314. {
  315. static struct varbuf newfn;
  316. char buf[1024];
  317. FILE *nf;
  318. trk_explicit_start(trig);
  319. varbufreset(&newfn);
  320. varbufprintf(&newfn, "%s/%s.new", triggersdir, trig);
  321. varbufaddc(&newfn, 0);
  322. nf = fopen(newfn.buf, "w");
  323. if (!nf)
  324. ohshite(_("unable to create new trigger interest file `%.250s'"),
  325. newfn.buf);
  326. push_cleanup(cu_closefile, ~ehflag_normaltidy, NULL, 0, 1, nf);
  327. while (trk_explicit_f && trk_explicit_fgets(buf, sizeof(buf)) >= 0) {
  328. if (!strcmp(buf, pkg->name))
  329. continue;
  330. fprintf(nf, "%s\n", buf);
  331. }
  332. if (signum > 0)
  333. fprintf(nf, "%s\n", pkg->name);
  334. if (ferror(nf))
  335. ohshite(_("unable to write new trigger interest file `%.250s'"),
  336. newfn.buf);
  337. pop_cleanup(ehflag_normaltidy);
  338. if (fclose(nf))
  339. ohshite(_("unable to close new trigger interest file `%.250s'"),
  340. newfn.buf);
  341. if (rename(newfn.buf, trk_explicit_fn.buf))
  342. ohshite(_("unable to install new trigger interest file `%.250s'"),
  343. trk_explicit_fn.buf);
  344. }
  345. TKI_DEFINE(explicit);
  346. /*---------- file triggers ----------*/
  347. static struct {
  348. struct trigfileint *head, *tail;
  349. } filetriggers;
  350. /*
  351. * Values:
  352. * -1: not read,
  353. * 0: not edited
  354. * 1: edited
  355. */
  356. static int filetriggers_edited = -1;
  357. /* Called by various people with signum -1 and +1 to mean remove and add
  358. * and also by trig_file_interests_ensure with signum +2 meaning add
  359. * but die if already present.
  360. */
  361. static void
  362. trk_file_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  363. {
  364. struct filenamenode *fnn;
  365. struct trigfileint **search, *tfi;
  366. fnn = trigh.namenode_find(trig, signum <= 0);
  367. if (!fnn) {
  368. assert(signum < 0);
  369. return;
  370. }
  371. for (search = trigh.namenode_interested(fnn);
  372. (tfi = *search);
  373. search = &tfi->samefile_next)
  374. if (tfi->pkg == pkg)
  375. goto found;
  376. /* not found */
  377. if (signum < 0)
  378. return;
  379. tfi = nfmalloc(sizeof(*tfi));
  380. tfi->pkg = pkg;
  381. tfi->fnn = fnn;
  382. tfi->samefile_next = *trigh.namenode_interested(fnn);
  383. *trigh.namenode_interested(fnn) = tfi;
  384. LIST_LINK_TAIL_PART(filetriggers, tfi, inoverall.);
  385. goto edited;
  386. found:
  387. if (signum > 1)
  388. ohshit(_("duplicate file trigger interest for filename `%.250s' "
  389. "and package `%.250s'"), trig, pkg->name);
  390. if (signum > 0)
  391. return;
  392. /* remove it: */
  393. *search = tfi->samefile_next;
  394. LIST_UNLINK_PART(filetriggers, tfi, inoverall.);
  395. edited:
  396. filetriggers_edited = 1;
  397. }
  398. void
  399. trig_file_interests_save(void)
  400. {
  401. struct trigfileint *tfi;
  402. FILE *nf;
  403. if (filetriggers_edited <= 0)
  404. return;
  405. nf = fopen(triggersnewfilefile, "w");
  406. if (!nf)
  407. ohshite(_("unable to create new file triggers file `%.250s'"),
  408. triggersnewfilefile);
  409. push_cleanup(cu_closefile, ~ehflag_normaltidy, NULL, 0, 1, nf);
  410. for (tfi = filetriggers.head; tfi; tfi = tfi->inoverall.next)
  411. fprintf(nf, "%s %s\n", trigh.namenode_name(tfi->fnn),
  412. tfi->pkg->name);
  413. if (ferror(nf))
  414. ohshite(_("unable to write new file triggers file `%.250s'"),
  415. triggersnewfilefile);
  416. pop_cleanup(ehflag_normaltidy);
  417. if (fclose(nf))
  418. ohshite(_("unable to close new file triggers file `%.250s'"),
  419. triggersnewfilefile);
  420. if (rename(triggersnewfilefile, triggersfilefile))
  421. ohshite(_("unable to install new file triggers file as `%.250s'"),
  422. triggersfilefile);
  423. filetriggers_edited = 0;
  424. }
  425. void
  426. trig_file_interests_ensure(void)
  427. {
  428. FILE *f;
  429. char linebuf[1024], *space;
  430. struct pkginfo *pkg;
  431. const char *emsg;
  432. if (filetriggers_edited >= 0)
  433. return;
  434. f = fopen(triggersfilefile, "r");
  435. if (!f) {
  436. if (errno == ENOENT)
  437. goto ok;
  438. ohshite(_("unable to read file triggers file `%.250s'"),
  439. triggersfilefile);
  440. }
  441. push_cleanup(cu_closefile, ~0, NULL, 0, 1, f);
  442. while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) {
  443. space = strchr(linebuf, ' ');
  444. if (!space || linebuf[0] != '/')
  445. ohshit(_("syntax error in file triggers file `%.250s'"),
  446. triggersfilefile);
  447. *space++ = '\0';
  448. if ((emsg = illegal_packagename(space, NULL)))
  449. ohshit(_("file triggers record mentions illegal "
  450. "package name `%.250s' (for interest in file "
  451. "`%.250s'): %.250s"), space, linebuf, emsg);
  452. pkg = findpackage(space);
  453. trk_file_interest_change(linebuf, pkg, +2);
  454. }
  455. pop_cleanup(ehflag_normaltidy);
  456. ok:
  457. filetriggers_edited = 0;
  458. }
  459. static struct filenamenode *filetriggers_activating;
  460. void
  461. trig_file_activate_byname(const char *trig, struct pkginfo *aw)
  462. {
  463. struct filenamenode *fnn = trigh.namenode_find(trig, 1);
  464. if (fnn)
  465. trig_file_activate(fnn, aw);
  466. }
  467. void
  468. trig_file_activate(struct filenamenode *trig, struct pkginfo *aw)
  469. {
  470. struct trigfileint *tfi;
  471. for (tfi = *trigh.namenode_interested(trig); tfi;
  472. tfi = tfi->samefile_next)
  473. trig_record_activation(tfi->pkg, aw, trigh.namenode_name(trig));
  474. }
  475. static void
  476. trk_file_activate_start(void)
  477. {
  478. filetriggers_activating = trigh.namenode_find(trig_activating_name, 1);
  479. }
  480. static void
  481. trk_file_activate_awaiter(struct pkginfo *aw)
  482. {
  483. if (!filetriggers_activating)
  484. return;
  485. trig_file_activate(filetriggers_activating, aw);
  486. }
  487. static void
  488. trk_file_activate_done(void)
  489. {
  490. }
  491. TKI_DEFINE(file);
  492. /*---------- trigger control info file ----------*/
  493. static void
  494. trig_cicb_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  495. {
  496. const struct trigkindinfo *tki = trig_classify_byname(trig);
  497. assert(filetriggers_edited >= 0);
  498. tki->interest_change(trig, pkg, signum);
  499. }
  500. void
  501. trig_cicb_interest_delete(const char *trig, void *user)
  502. {
  503. trig_cicb_interest_change(trig, user, -1);
  504. }
  505. void
  506. trig_cicb_interest_add(const char *trig, void *user)
  507. {
  508. trig_cicb_interest_change(trig, user, +1);
  509. }
  510. void
  511. trig_cicb_statuschange_activate(const char *trig, void *user)
  512. {
  513. struct pkginfo *aw = user;
  514. trig_activate_start(trig);
  515. dtki->activate_awaiter(aw);
  516. dtki->activate_done();
  517. }
  518. static void
  519. parse_ci_call(const char *file, const char *cmd, trig_parse_cicb *cb,
  520. const char *trig, void *user)
  521. {
  522. const char *emsg;
  523. emsg = illegal_triggername(trig);
  524. if (emsg)
  525. ohshit(_("triggers ci file `%.250s' contains illegal trigger "
  526. "syntax in trigger name `%.250s': %.250s"),
  527. file, trig, emsg);
  528. if (cb)
  529. cb(trig, user);
  530. }
  531. void
  532. trig_parse_ci(const char *file, trig_parse_cicb *interest,
  533. trig_parse_cicb *activate, void *user)
  534. {
  535. FILE *f;
  536. char linebuf[MAXTRIGDIRECTIVE], *cmd, *spc, *eol;
  537. int l;
  538. f = fopen(file, "r");
  539. if (!f) {
  540. if (errno == ENOENT)
  541. /* No file is just like an empty one. */
  542. return;
  543. ohshite(_("unable to open triggers ci file `%.250s'"), file);
  544. }
  545. push_cleanup(cu_closefile, ~0, NULL, 0, 1, f);
  546. while ((l = fgets_checked(linebuf, sizeof(linebuf), f, file)) >= 0) {
  547. for (cmd = linebuf; cisspace(*cmd); cmd++);
  548. if (*cmd == '#')
  549. continue;
  550. for (eol = linebuf + l; eol > cmd && cisspace(eol[-1]); eol--);
  551. if (eol == cmd)
  552. continue;
  553. *eol = '\0';
  554. for (spc = cmd; *spc && !cisspace(*spc); spc++);
  555. if (!*spc)
  556. ohshit(_("triggers ci file contains unknown directive syntax"));
  557. *spc++ = '\0';
  558. while (cisspace(*spc))
  559. spc++;
  560. if (!strcmp(cmd, "interest")) {
  561. parse_ci_call(file, cmd, interest, spc, user);
  562. } else if (!strcmp(cmd, "activate")) {
  563. parse_ci_call(file, cmd, activate, spc, user);
  564. } else {
  565. ohshit(_("triggers ci file contains unknown directive `%.250s'"),
  566. cmd);
  567. }
  568. }
  569. pop_cleanup(ehflag_normaltidy); /* fclose */
  570. }
  571. /*---------- Unincorp file incorporation ----------*/
  572. static void
  573. tdm_incorp_trig_begin(const char *trig)
  574. {
  575. trig_activate_start(trig);
  576. }
  577. static void
  578. tdm_incorp_package(const char *awname)
  579. {
  580. struct pkginfo *aw = strcmp(awname, "-") ? findpackage(awname) : NULL;
  581. dtki->activate_awaiter(aw);
  582. }
  583. static void
  584. tdm_incorp_trig_end(void)
  585. {
  586. dtki->activate_done();
  587. }
  588. static const struct trigdefmeths tdm_incorp = {
  589. tdm_incorp_trig_begin,
  590. tdm_incorp_package,
  591. tdm_incorp_trig_end
  592. };
  593. void
  594. trig_incorporate(enum modstatdb_rw cstatus, const char *admindir)
  595. {
  596. int ur;
  597. enum trigdef_updateflags tduf;
  598. trigdef = &tdm_incorp;
  599. trig_file_interests_ensure();
  600. tduf = tduf_nolockok;
  601. if (cstatus >= msdbrw_write) {
  602. tduf |= tduf_write;
  603. if (trigh.transitional_activate)
  604. tduf |= tduf_writeifenoent;
  605. }
  606. ur = trigdef_update_start(tduf, admindir);
  607. if (ur == -1 && cstatus >= msdbrw_write) {
  608. if (mkdir(triggersdir, 0755)) {
  609. if (errno != EEXIST)
  610. ohshite(_("unable to create triggers state"
  611. " directory `%.250s'"), triggersdir);
  612. } else if (chown(triggersdir, 0, 0)) {
  613. ohshite(_("unable to set ownership of triggers state"
  614. " directory `%.250s'"), triggersdir);
  615. }
  616. ur = trigdef_update_start(tduf, admindir);
  617. }
  618. switch (ur) {
  619. case -2:
  620. return;
  621. case -1:
  622. case -3:
  623. if (!trigh.transitional_activate)
  624. return;
  625. /* Fall through. */
  626. case 1:
  627. trigh.transitional_activate(cstatus);
  628. break;
  629. case 2:
  630. /* Read and incorporate triggers. */
  631. trigdef_yylex();
  632. break;
  633. default:
  634. internerr("unknown trigdef_update_start return value '%d'", ur);
  635. }
  636. /* Right, that's it. New (empty) Unincorp can be installed. */
  637. trigdef_process_done();
  638. }
  639. /*---------- default hooks ----------*/
  640. struct filenamenode {
  641. struct filenamenode *next;
  642. const char *name;
  643. struct trigfileint *trig_interested;
  644. };
  645. static struct filenamenode *trigger_files;
  646. static struct filenamenode *
  647. th_simple_nn_find(const char *name, int nonew)
  648. {
  649. struct filenamenode *search;
  650. for (search = trigger_files; search; search = search->next)
  651. if (!strcmp(search->name, name))
  652. return search;
  653. /* Not found. */
  654. if (nonew)
  655. return NULL;
  656. search = nfmalloc(sizeof(*search));
  657. search->name = nfstrsave(name);
  658. search->trig_interested = NULL;
  659. search->next = trigger_files;
  660. trigger_files = search;
  661. return search;
  662. }
  663. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  664. struct trig_hooks trigh = {
  665. NULL,
  666. NULL,
  667. th_simple_nn_find,
  668. th_nn_interested,
  669. th_nn_name
  670. };