triglib.c 19 KB

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