triglib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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, 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, 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, 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) || fclose(nf))
  335. ohshite(_("unable to write new trigger interest file `%.250s'"),
  336. newfn.buf);
  337. pop_cleanup(ehflag_normaltidy);
  338. if (rename(newfn.buf, trk_explicit_fn.buf))
  339. ohshite(_("unable to install new trigger interest file `%.250s'"),
  340. trk_explicit_fn.buf);
  341. }
  342. TKI_DEFINE(explicit);
  343. /*---------- file triggers ----------*/
  344. static struct {
  345. struct trigfileint *head, *tail;
  346. } filetriggers;
  347. /*
  348. * Values:
  349. * -1: not read,
  350. * 0: not edited
  351. * 1: edited
  352. */
  353. static int filetriggers_edited = -1;
  354. /* Called by various people with signum -1 and +1 to mean remove and add
  355. * and also by trig_file_interests_ensure with signum +2 meaning add
  356. * but die if already present.
  357. */
  358. static void
  359. trk_file_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  360. {
  361. struct filenamenode *fnn;
  362. struct trigfileint **search, *tfi;
  363. fnn = trigh.namenode_find(trig, signum <= 0);
  364. if (!fnn) {
  365. assert(signum < 0);
  366. return;
  367. }
  368. for (search = trigh.namenode_interested(fnn);
  369. (tfi = *search);
  370. search = &tfi->samefile_next)
  371. if (tfi->pkg == pkg)
  372. goto found;
  373. /* not found */
  374. if (signum < 0)
  375. return;
  376. tfi = nfmalloc(sizeof(*tfi));
  377. tfi->pkg = pkg;
  378. tfi->fnn = fnn;
  379. tfi->samefile_next = *trigh.namenode_interested(fnn);
  380. *trigh.namenode_interested(fnn) = tfi;
  381. LIST_LINK_TAIL_PART(filetriggers, tfi, inoverall.);
  382. goto edited;
  383. found:
  384. if (signum > 1)
  385. ohshit(_("duplicate file trigger interest for filename `%.250s' "
  386. "and package `%.250s'"), trig, pkg->name);
  387. if (signum > 0)
  388. return;
  389. /* remove it: */
  390. *search = tfi->samefile_next;
  391. LIST_UNLINK_PART(filetriggers, tfi, inoverall.);
  392. edited:
  393. filetriggers_edited = 1;
  394. }
  395. void
  396. trig_file_interests_save(void)
  397. {
  398. struct trigfileint *tfi;
  399. FILE *nf;
  400. if (filetriggers_edited <= 0)
  401. return;
  402. nf = fopen(triggersnewfilefile, "w");
  403. if (!nf)
  404. ohshite(_("unable to create new file triggers file `%.250s'"),
  405. triggersnewfilefile);
  406. push_cleanup(cu_closefile, ~ehflag_normaltidy, NULL, 0, 1, nf);
  407. for (tfi = filetriggers.head; tfi; tfi = tfi->inoverall.next)
  408. fprintf(nf, "%s %s\n", trigh.namenode_name(tfi->fnn),
  409. tfi->pkg->name);
  410. if (ferror(nf) || fclose(nf))
  411. ohshite(_("unable to write new file triggers file `%.250s'"),
  412. triggersnewfilefile);
  413. pop_cleanup(ehflag_normaltidy);
  414. if (rename(triggersnewfilefile, triggersfilefile))
  415. ohshite(_("unable to install new file triggers file as `%.250s'"),
  416. triggersfilefile);
  417. filetriggers_edited = 0;
  418. }
  419. void
  420. trig_file_interests_ensure(void)
  421. {
  422. FILE *f;
  423. char linebuf[1024], *space;
  424. struct pkginfo *pkg;
  425. const char *emsg;
  426. if (filetriggers_edited >= 0)
  427. return;
  428. f = fopen(triggersfilefile, "r");
  429. if (!f) {
  430. if (errno == ENOENT)
  431. goto ok;
  432. ohshite(_("unable to read file triggers file `%.250s'"),
  433. triggersfilefile);
  434. }
  435. push_cleanup(cu_closefile, ~0, NULL, 0, 1, f);
  436. while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) {
  437. space = strchr(linebuf, ' ');
  438. if (!space || linebuf[0] != '/')
  439. ohshit(_("syntax error in file triggers file `%.250s'"),
  440. triggersfilefile);
  441. *space++ = 0;
  442. if ((emsg = illegal_packagename(space, NULL)))
  443. ohshit(_("file triggers record mentions illegal "
  444. "package name `%.250s' (for interest in file "
  445. "`%.250s'): %.250s"), space, linebuf, emsg);
  446. pkg = findpackage(space);
  447. trk_file_interest_change(linebuf, pkg, +2);
  448. }
  449. pop_cleanup(ehflag_normaltidy);
  450. ok:
  451. filetriggers_edited = 0;
  452. }
  453. static struct filenamenode *filetriggers_activating;
  454. void
  455. trig_file_activate_byname(const char *trig, struct pkginfo *aw)
  456. {
  457. struct filenamenode *fnn = trigh.namenode_find(trig, 1);
  458. if (fnn)
  459. trig_file_activate(fnn, aw);
  460. }
  461. void
  462. trig_file_activate(struct filenamenode *trig, struct pkginfo *aw)
  463. {
  464. struct trigfileint *tfi;
  465. for (tfi = *trigh.namenode_interested(trig); tfi;
  466. tfi = tfi->samefile_next)
  467. trig_record_activation(tfi->pkg, aw, (char*)trigh.namenode_name(trig));
  468. }
  469. static void
  470. trk_file_activate_start(void)
  471. {
  472. filetriggers_activating = trigh.namenode_find(trig_activating_name, 1);
  473. }
  474. static void
  475. trk_file_activate_awaiter(struct pkginfo *aw)
  476. {
  477. if (!filetriggers_activating)
  478. return;
  479. trig_file_activate(filetriggers_activating, aw);
  480. }
  481. static void
  482. trk_file_activate_done(void)
  483. {
  484. }
  485. TKI_DEFINE(file);
  486. /*---------- trigger control info file ----------*/
  487. static void
  488. trig_cicb_interest_change(const char *trig, struct pkginfo *pkg, int signum)
  489. {
  490. const struct trigkindinfo *tki = trig_classify_byname(trig);
  491. assert(filetriggers_edited >= 0);
  492. tki->interest_change(trig, pkg, signum);
  493. }
  494. void
  495. trig_cicb_interest_delete(const char *trig, void *user)
  496. {
  497. trig_cicb_interest_change(trig, user, -1);
  498. }
  499. void
  500. trig_cicb_interest_add(const char *trig, void *user)
  501. {
  502. trig_cicb_interest_change(trig, user, +1);
  503. }
  504. void
  505. trig_cicb_statuschange_activate(const char *trig, void *user)
  506. {
  507. struct pkginfo *aw = user;
  508. trig_activate_start(trig);
  509. dtki->activate_awaiter(aw);
  510. dtki->activate_done();
  511. }
  512. static void
  513. parse_ci_call(const char *file, const char *cmd, trig_parse_cicb *cb,
  514. const char *trig, void *user)
  515. {
  516. const char *emsg;
  517. emsg = illegal_triggername(trig);
  518. if (emsg)
  519. ohshit(_("triggers ci file `%.250s' contains illegal trigger "
  520. "syntax in trigger name `%.250s': %.250s"),
  521. file, trig, emsg);
  522. if (cb)
  523. cb(trig, user);
  524. }
  525. void
  526. trig_parse_ci(const char *file, trig_parse_cicb *interest,
  527. trig_parse_cicb *activate, void *user)
  528. {
  529. FILE *f;
  530. char linebuf[MAXTRIGDIRECTIVE], *cmd, *spc, *eol;
  531. int l;
  532. f = fopen(file, "r");
  533. if (!f) {
  534. if (errno == ENOENT)
  535. /* No file is just like an empty one. */
  536. return;
  537. ohshite(_("unable to open triggers ci file `%.250s'"), file);
  538. }
  539. push_cleanup(cu_closefile, ~0, NULL, 0, 1, f);
  540. while ((l = fgets_checked(linebuf, sizeof(linebuf), f, file)) >= 0) {
  541. for (cmd = linebuf; cisspace(*cmd); cmd++);
  542. if (*cmd == '#')
  543. continue;
  544. for (eol = linebuf + l; eol > cmd && cisspace(eol[-1]); eol--);
  545. if (eol == cmd)
  546. continue;
  547. *eol = 0;
  548. for (spc = cmd; *spc && !cisspace(*spc); spc++);
  549. if (!*spc)
  550. ohshit(_("triggers ci file contains unknown directive syntax"));
  551. *spc++ = 0;
  552. while (cisspace(*spc))
  553. spc++;
  554. if (!strcmp(cmd, "interest")) {
  555. parse_ci_call(file, cmd, interest, spc, user);
  556. } else if (!strcmp(cmd, "activate")) {
  557. parse_ci_call(file, cmd, activate, spc, user);
  558. } else {
  559. ohshit(_("triggers ci file contains unknown directive `%.250s'"),
  560. cmd);
  561. }
  562. }
  563. pop_cleanup(ehflag_normaltidy); /* fclose */
  564. }
  565. /*---------- Unincorp file incorporation ----------*/
  566. static void
  567. tdm_incorp_trig_begin(const char *trig)
  568. {
  569. trig_activate_start(trig);
  570. }
  571. static void
  572. tdm_incorp_package(const char *awname)
  573. {
  574. struct pkginfo *aw = strcmp(awname, "-") ? findpackage(awname) : NULL;
  575. dtki->activate_awaiter(aw);
  576. }
  577. static void
  578. tdm_incorp_trig_end(void)
  579. {
  580. dtki->activate_done();
  581. }
  582. static const struct trigdefmeths tdm_incorp = {
  583. tdm_incorp_trig_begin,
  584. tdm_incorp_package,
  585. tdm_incorp_trig_end
  586. };
  587. void
  588. trig_incorporate(enum modstatdb_rw cstatus, const char *admindir)
  589. {
  590. int ur;
  591. enum trigdef_updateflags tduf;
  592. trigdef = &tdm_incorp;
  593. trig_file_interests_ensure();
  594. tduf = tduf_nolockok;
  595. if (cstatus >= msdbrw_write) {
  596. tduf |= tduf_write;
  597. if (trigh.transitional_activate)
  598. tduf |= tduf_writeifenoent;
  599. }
  600. ur = trigdef_update_start(tduf, admindir);
  601. if (ur == -1 && cstatus >= msdbrw_write) {
  602. if (mkdir(triggersdir, 0755)) {
  603. if (errno != EEXIST)
  604. ohshite(_("unable to create triggers state"
  605. " directory `%.250s'"), triggersdir);
  606. } else if (chown(triggersdir, 0, 0)) {
  607. ohshite(_("unable to set ownership of triggers state"
  608. " directory `%.250s'"), triggersdir);
  609. }
  610. ur = trigdef_update_start(tduf, admindir);
  611. }
  612. switch (ur) {
  613. case -2:
  614. return;
  615. case -1:
  616. case -3:
  617. if (!trigh.transitional_activate)
  618. return;
  619. /* Fall through. */
  620. case 1:
  621. trigh.transitional_activate(cstatus);
  622. break;
  623. case 2:
  624. /* Read and incorporate triggers. */
  625. trigdef_yylex();
  626. break;
  627. default:
  628. abort();
  629. }
  630. /* Right, that's it. New (empty) Unincorp can be installed. */
  631. trigdef_process_done();
  632. }
  633. /*---------- default hooks ----------*/
  634. struct filenamenode {
  635. struct filenamenode *next;
  636. const char *name;
  637. struct trigfileint *trig_interested;
  638. };
  639. static struct filenamenode *trigger_files;
  640. static struct filenamenode *
  641. th_simple_nn_find(const char *name, int nonew)
  642. {
  643. struct filenamenode *search;
  644. for (search = trigger_files; search; search = search->next)
  645. if (!strcmp(search->name, name))
  646. return search;
  647. /* Not found. */
  648. if (nonew)
  649. return NULL;
  650. search = nfmalloc(sizeof(*search));
  651. search->name = nfstrsave(name);
  652. search->trig_interested = NULL;
  653. search->next = trigger_files;
  654. trigger_files = search;
  655. return search;
  656. }
  657. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  658. struct trig_hooks trigh = {
  659. NULL,
  660. NULL,
  661. th_simple_nn_find,
  662. th_nn_interested,
  663. th_nn_name
  664. };