triglib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/c-ctype.h>
  32. #include <dpkg/dpkg.h>
  33. #include <dpkg/dpkg-db.h>
  34. #include <dpkg/pkg.h>
  35. #include <dpkg/dlist.h>
  36. #include <dpkg/dir.h>
  37. #include <dpkg/pkg-spec.h>
  38. #include <dpkg/trigdeferred.h>
  39. #include <dpkg/triglib.h>
  40. /*========== Recording triggers. ==========*/
  41. static char *triggersdir, *triggersfilefile;
  42. static char *
  43. trig_get_filename(const char *dir, const char *filename)
  44. {
  45. char *path;
  46. m_asprintf(&path, "%s/%s", dir, filename);
  47. return path;
  48. }
  49. static struct trig_hooks trigh;
  50. /*---------- Noting trigger activation in memory. ----------*/
  51. /*
  52. * Called via trig_*activate* et al from:
  53. * - trig_incorporate: reading of Unincorp (explicit trigger activations)
  54. * - various places: processing start (‘activate’ in triggers ci file)
  55. * - namenodetouse: file triggers during unpack / remove
  56. * - deferred_configure: file triggers during config file processing
  57. *
  58. * Not called from trig_transitional_activation; that runs
  59. * trig_note_pend directly which means that (a) awaiters are not
  60. * recorded (how would we know?) and (b) we don't enqueue them for
  61. * deferred processing in this run.
  62. *
  63. * We add the trigger to Triggers-Pending first. This makes it
  64. * harder to get into the state where Triggers-Awaited for aw lists
  65. * pend but Triggers-Pending for pend is empty. (See also the
  66. * comment in deppossi_ok_found regarding this situation.)
  67. */
  68. /*
  69. * aw might be NULL.
  70. * trig is not copied!
  71. */
  72. static void
  73. trig_record_activation(struct pkginfo *pend, struct pkginfo *aw, const char *trig)
  74. {
  75. if (pend->status < PKG_STAT_TRIGGERSAWAITED)
  76. return; /* Not interested then. */
  77. if (trig_note_pend(pend, trig))
  78. modstatdb_note_ifwrite(pend);
  79. if (trigh.enqueue_deferred)
  80. trigh.enqueue_deferred(pend);
  81. if (aw && pend->status > PKG_STAT_CONFIGFILES)
  82. if (trig_note_aw(pend, aw)) {
  83. if (aw->status > PKG_STAT_TRIGGERSAWAITED)
  84. pkg_set_status(aw, PKG_STAT_TRIGGERSAWAITED);
  85. modstatdb_note_ifwrite(aw);
  86. }
  87. }
  88. void
  89. trig_clear_awaiters(struct pkginfo *notpend)
  90. {
  91. struct trigaw *ta;
  92. struct pkginfo *aw;
  93. assert(!notpend->trigpend_head);
  94. ta = notpend->othertrigaw_head;
  95. notpend->othertrigaw_head = NULL;
  96. for (; ta; ta = ta->samepend_next) {
  97. aw = ta->aw;
  98. if (!aw)
  99. continue;
  100. LIST_UNLINK_PART(aw->trigaw, ta, sameaw.);
  101. if (!aw->trigaw.head && aw->status == PKG_STAT_TRIGGERSAWAITED) {
  102. if (aw->trigpend_head)
  103. pkg_set_status(aw, PKG_STAT_TRIGGERSPENDING);
  104. else
  105. pkg_set_status(aw, PKG_STAT_INSTALLED);
  106. modstatdb_note(aw);
  107. }
  108. }
  109. }
  110. /*
  111. * Fix up packages in state triggers-awaited w/o the corresponding package
  112. * with pending triggers. This can happen when dpkg was interrupted
  113. * while in modstatdb_note, and the package in triggers-pending had its
  114. * state modified but dpkg could not finish clearing the awaiters.
  115. *
  116. * XXX: Possibly get rid of some of the checks done somewhere else for
  117. * this condition at run-time.
  118. */
  119. void
  120. trig_fixup_awaiters(enum modstatdb_rw cstatus)
  121. {
  122. if (cstatus < msdbrw_write)
  123. return;
  124. trig_awaited_pend_foreach(trig_clear_awaiters);
  125. trig_awaited_pend_free();
  126. }
  127. /*---------- Generalized handling of trigger kinds. ----------*/
  128. struct trigkindinfo {
  129. /* Only for trig_activate_start. */
  130. void (*activate_start)(void);
  131. /* Rest are for everyone: */
  132. void (*activate_awaiter)(struct pkginfo *pkg /* may be NULL */);
  133. void (*activate_done)(void);
  134. void (*interest_change)(const char *name, struct pkginfo *pkg,
  135. struct pkgbin *pkgbin,
  136. int signum, enum trig_options opts);
  137. };
  138. static const struct trigkindinfo tki_explicit, tki_file, tki_unknown;
  139. static const struct trigkindinfo *dtki;
  140. /* As passed into activate_start. */
  141. static char *trig_activating_name;
  142. static const struct trigkindinfo *
  143. trig_classify_byname(const char *name)
  144. {
  145. if (name[0] == '/') {
  146. const char *slash;
  147. slash = name;
  148. while (slash) {
  149. if (slash[1] == '\0' || slash[1] == '/')
  150. goto invalid;
  151. slash = strchr(slash + 2, '/');
  152. }
  153. return &tki_file;
  154. }
  155. if (!pkg_name_is_illegal(name) && !strchr(name, '_'))
  156. return &tki_explicit;
  157. invalid:
  158. return &tki_unknown;
  159. }
  160. /*
  161. * Calling sequence is:
  162. * trig_activate_start(triggername);
  163. * dtki->activate_awaiter(awaiting_package); } zero or more times
  164. * dtki->activate_awaiter(NULL); } in any order
  165. * dtki->activate_done();
  166. */
  167. static void
  168. trig_activate_start(const char *name)
  169. {
  170. dtki = trig_classify_byname(name);
  171. trig_activating_name = nfstrsave(name);
  172. dtki->activate_start();
  173. }
  174. /*---------- Unknown trigger kinds. ----------*/
  175. static void
  176. trk_unknown_activate_start(void)
  177. {
  178. }
  179. static void
  180. trk_unknown_activate_awaiter(struct pkginfo *aw)
  181. {
  182. }
  183. static void
  184. trk_unknown_activate_done(void)
  185. {
  186. }
  187. static void DPKG_ATTR_NORET
  188. trk_unknown_interest_change(const char *trig, struct pkginfo *pkg,
  189. struct pkgbin *pkgbin, int signum,
  190. enum trig_options opts)
  191. {
  192. ohshit(_("invalid or unknown syntax in trigger name '%.250s'"
  193. " (in trigger interests for package '%.250s')"),
  194. trig, pkgbin_name(pkg, pkgbin, pnaw_nonambig));
  195. }
  196. static const struct trigkindinfo tki_unknown = {
  197. .activate_start = trk_unknown_activate_start,
  198. .activate_awaiter = trk_unknown_activate_awaiter,
  199. .activate_done = trk_unknown_activate_done,
  200. .interest_change = trk_unknown_interest_change,
  201. };
  202. /*---------- Explicit triggers. ----------*/
  203. static FILE *trk_explicit_f;
  204. static struct varbuf trk_explicit_fn;
  205. static char *trk_explicit_trig;
  206. static void
  207. trk_explicit_activate_done(void)
  208. {
  209. if (trk_explicit_f) {
  210. fclose(trk_explicit_f);
  211. trk_explicit_f = NULL;
  212. }
  213. }
  214. static void
  215. trk_explicit_start(const char *trig)
  216. {
  217. trk_explicit_activate_done();
  218. varbuf_reset(&trk_explicit_fn);
  219. varbuf_add_str(&trk_explicit_fn, triggersdir);
  220. varbuf_add_char(&trk_explicit_fn, '/');
  221. varbuf_add_str(&trk_explicit_fn, trig);
  222. varbuf_end_str(&trk_explicit_fn);
  223. trk_explicit_f = fopen(trk_explicit_fn.buf, "r");
  224. if (!trk_explicit_f) {
  225. if (errno != ENOENT)
  226. ohshite(_("failed to open trigger interest list file '%.250s'"),
  227. trk_explicit_fn.buf);
  228. }
  229. }
  230. static int
  231. trk_explicit_fgets(char *buf, size_t sz)
  232. {
  233. return fgets_checked(buf, sz, trk_explicit_f, trk_explicit_fn.buf);
  234. }
  235. static void
  236. trk_explicit_activate_start(void)
  237. {
  238. trk_explicit_start(trig_activating_name);
  239. trk_explicit_trig = trig_activating_name;
  240. }
  241. static void
  242. trk_explicit_activate_awaiter(struct pkginfo *aw)
  243. {
  244. char buf[1024];
  245. struct pkginfo *pend;
  246. if (!trk_explicit_f)
  247. return;
  248. if (fseek(trk_explicit_f, 0, SEEK_SET))
  249. ohshite(_("failed to rewind trigger interest file '%.250s'"),
  250. trk_explicit_fn.buf);
  251. while (trk_explicit_fgets(buf, sizeof(buf)) >= 0) {
  252. struct dpkg_error err;
  253. char *slash;
  254. bool noawait = false;
  255. slash = strchr(buf, '/');
  256. if (slash && strcmp("/noawait", slash) == 0) {
  257. noawait = true;
  258. *slash = '\0';
  259. }
  260. if (slash && strcmp("/await", slash) == 0) {
  261. noawait = false;
  262. *slash = '\0';
  263. }
  264. pend = pkg_spec_parse_pkg(buf, &err);
  265. if (pend == NULL)
  266. ohshit(_("trigger interest file '%.250s' syntax error; "
  267. "illegal package name '%.250s': %.250s"),
  268. trk_explicit_fn.buf, buf, err.str);
  269. trig_record_activation(pend, noawait ? NULL : aw,
  270. trk_explicit_trig);
  271. }
  272. }
  273. static void
  274. trk_explicit_interest_change(const char *trig, struct pkginfo *pkg,
  275. struct pkgbin *pkgbin, int signum,
  276. enum trig_options opts)
  277. {
  278. char buf[1024];
  279. struct atomic_file *file;
  280. bool empty = true;
  281. trk_explicit_start(trig);
  282. file = atomic_file_new(trk_explicit_fn.buf, 0);
  283. atomic_file_open(file);
  284. while (trk_explicit_f && trk_explicit_fgets(buf, sizeof(buf)) >= 0) {
  285. const char *pkgname = pkgbin_name(pkg, pkgbin, pnaw_nonambig);
  286. size_t len = strlen(pkgname);
  287. if (strncmp(buf, pkgname, len) == 0 && len < sizeof(buf) &&
  288. (buf[len] == '\0' || buf[len] == '/'))
  289. continue;
  290. fprintf(file->fp, "%s\n", buf);
  291. empty = false;
  292. }
  293. if (signum > 0) {
  294. fprintf(file->fp, "%s%s\n",
  295. pkgbin_name(pkg, pkgbin, pnaw_nonambig),
  296. (opts == TRIG_NOAWAIT) ? "/noawait" : "");
  297. empty = false;
  298. }
  299. if (!empty)
  300. atomic_file_sync(file);
  301. atomic_file_close(file);
  302. if (empty)
  303. atomic_file_remove(file);
  304. else
  305. atomic_file_commit(file);
  306. atomic_file_free(file);
  307. dir_sync_path(triggersdir);
  308. }
  309. static const struct trigkindinfo tki_explicit = {
  310. .activate_start = trk_explicit_activate_start,
  311. .activate_awaiter = trk_explicit_activate_awaiter,
  312. .activate_done = trk_explicit_activate_done,
  313. .interest_change = trk_explicit_interest_change,
  314. };
  315. /*---------- File triggers. ----------*/
  316. static struct {
  317. struct trigfileint *head, *tail;
  318. } filetriggers;
  319. /*
  320. * Values:
  321. * -1: Not read.
  322. * 0: Not edited.
  323. * 1: Edited
  324. */
  325. static int filetriggers_edited = -1;
  326. /*
  327. * Called by various people with signum -1 and +1 to mean remove and add
  328. * and also by trig_file_interests_ensure() with signum +2 meaning add
  329. * but die if already present.
  330. */
  331. static void
  332. trk_file_interest_change(const char *trig, struct pkginfo *pkg,
  333. struct pkgbin *pkgbin, int signum,
  334. enum trig_options opts)
  335. {
  336. struct filenamenode *fnn;
  337. struct trigfileint **search, *tfi;
  338. fnn = trigh.namenode_find(trig, signum <= 0);
  339. if (!fnn) {
  340. assert(signum < 0);
  341. return;
  342. }
  343. for (search = trigh.namenode_interested(fnn);
  344. (tfi = *search);
  345. search = &tfi->samefile_next)
  346. if (tfi->pkg == pkg)
  347. goto found;
  348. /* Not found. */
  349. if (signum < 0)
  350. return;
  351. tfi = nfmalloc(sizeof(*tfi));
  352. tfi->pkg = pkg;
  353. tfi->pkgbin = pkgbin;
  354. tfi->fnn = fnn;
  355. tfi->options = opts;
  356. tfi->samefile_next = *trigh.namenode_interested(fnn);
  357. *trigh.namenode_interested(fnn) = tfi;
  358. LIST_LINK_TAIL_PART(filetriggers, tfi, inoverall.);
  359. goto edited;
  360. found:
  361. tfi->options = opts;
  362. if (signum > 1)
  363. ohshit(_("duplicate file trigger interest for filename '%.250s' "
  364. "and package '%.250s'"), trig,
  365. pkgbin_name(pkg, pkgbin, pnaw_nonambig));
  366. if (signum > 0)
  367. return;
  368. /* Remove it: */
  369. *search = tfi->samefile_next;
  370. LIST_UNLINK_PART(filetriggers, tfi, inoverall.);
  371. edited:
  372. filetriggers_edited = 1;
  373. }
  374. static void
  375. trig_file_interests_remove(void)
  376. {
  377. if (unlink(triggersfilefile) && errno != ENOENT)
  378. ohshite(_("cannot remove '%.250s'"), triggersfilefile);
  379. }
  380. static void
  381. trig_file_interests_update(void)
  382. {
  383. struct trigfileint *tfi;
  384. struct atomic_file *file;
  385. file = atomic_file_new(triggersfilefile, 0);
  386. atomic_file_open(file);
  387. for (tfi = filetriggers.head; tfi; tfi = tfi->inoverall.next)
  388. fprintf(file->fp, "%s %s%s\n", trigh.namenode_name(tfi->fnn),
  389. pkgbin_name(tfi->pkg, tfi->pkgbin, pnaw_nonambig),
  390. (tfi->options == TRIG_NOAWAIT) ? "/noawait" : "");
  391. atomic_file_sync(file);
  392. atomic_file_close(file);
  393. atomic_file_commit(file);
  394. atomic_file_free(file);
  395. }
  396. void
  397. trig_file_interests_save(void)
  398. {
  399. if (filetriggers_edited <= 0)
  400. return;
  401. if (!filetriggers.head)
  402. trig_file_interests_remove();
  403. else
  404. trig_file_interests_update();
  405. dir_sync_path(triggersdir);
  406. filetriggers_edited = 0;
  407. }
  408. void
  409. trig_file_interests_ensure(void)
  410. {
  411. FILE *f;
  412. char linebuf[1024], *space;
  413. struct pkginfo *pkg;
  414. struct pkgbin *pkgbin;
  415. if (filetriggers_edited >= 0)
  416. return;
  417. f = fopen(triggersfilefile, "r");
  418. if (!f) {
  419. if (errno == ENOENT)
  420. goto ok;
  421. ohshite(_("unable to read file triggers file '%.250s'"),
  422. triggersfilefile);
  423. }
  424. push_cleanup(cu_closestream, ~0, NULL, 0, 1, f);
  425. while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) {
  426. struct dpkg_error err;
  427. char *slash;
  428. enum trig_options trig_opts = TRIG_AWAIT;
  429. space = strchr(linebuf, ' ');
  430. if (!space || linebuf[0] != '/')
  431. ohshit(_("syntax error in file triggers file '%.250s'"),
  432. triggersfilefile);
  433. *space++ = '\0';
  434. slash = strchr(space, '/');
  435. if (slash && strcmp("/noawait", slash) == 0) {
  436. trig_opts = TRIG_NOAWAIT;
  437. *slash = '\0';
  438. }
  439. if (slash && strcmp("/await", slash) == 0) {
  440. trig_opts = TRIG_AWAIT;
  441. *slash = '\0';
  442. }
  443. pkg = pkg_spec_parse_pkg(space, &err);
  444. if (pkg == NULL)
  445. ohshit(_("file triggers record mentions illegal "
  446. "package name '%.250s' (for interest in file "
  447. "'%.250s'): %.250s"), space, linebuf, err.str);
  448. pkgbin = &pkg->installed;
  449. trk_file_interest_change(linebuf, pkg, pkgbin, +2, trig_opts);
  450. }
  451. pop_cleanup(ehflag_normaltidy);
  452. ok:
  453. filetriggers_edited = 0;
  454. }
  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, (tfi->options == TRIG_NOAWAIT) ?
  469. NULL : aw, trigh.namenode_name(trig));
  470. }
  471. static void
  472. trig_file_activate_parents(const char *trig, struct pkginfo *aw)
  473. {
  474. char *path, *slash;
  475. /* Traverse the whole pathname to activate all of its components. */
  476. path = m_strdup(trig);
  477. while ((slash = strrchr(path, '/'))) {
  478. *slash = '\0';
  479. trig_file_activate_byname(path, aw);
  480. }
  481. free(path);
  482. }
  483. void
  484. trig_path_activate(struct filenamenode *trig, struct pkginfo *aw)
  485. {
  486. trig_file_activate(trig, aw);
  487. trig_file_activate_parents(trigh.namenode_name(trig), aw);
  488. }
  489. static void
  490. trig_path_activate_byname(const char *trig, struct pkginfo *aw)
  491. {
  492. struct filenamenode *fnn = trigh.namenode_find(trig, 1);
  493. if (fnn)
  494. trig_file_activate(fnn, aw);
  495. trig_file_activate_parents(trig, aw);
  496. }
  497. static const char *trk_file_trig;
  498. static void
  499. trk_file_activate_start(void)
  500. {
  501. trk_file_trig = trig_activating_name;
  502. }
  503. static void
  504. trk_file_activate_awaiter(struct pkginfo *aw)
  505. {
  506. trig_path_activate_byname(trk_file_trig, aw);
  507. }
  508. static void
  509. trk_file_activate_done(void)
  510. {
  511. }
  512. static const struct trigkindinfo tki_file = {
  513. .activate_start = trk_file_activate_start,
  514. .activate_awaiter = trk_file_activate_awaiter,
  515. .activate_done = trk_file_activate_done,
  516. .interest_change = trk_file_interest_change,
  517. };
  518. /*---------- Trigger control info file. ----------*/
  519. static void
  520. trig_cicb_interest_change(const char *trig, struct pkginfo *pkg,
  521. struct pkgbin *pkgbin, int signum,
  522. enum trig_options opts)
  523. {
  524. const struct trigkindinfo *tki = trig_classify_byname(trig);
  525. assert(filetriggers_edited >= 0);
  526. tki->interest_change(trig, pkg, pkgbin, signum, opts);
  527. }
  528. void
  529. trig_cicb_interest_delete(const char *trig, struct pkginfo *pkg,
  530. struct pkgbin *pkgbin, enum trig_options opts)
  531. {
  532. trig_cicb_interest_change(trig, pkg, pkgbin, -1, opts);
  533. }
  534. void
  535. trig_cicb_interest_add(const char *trig, struct pkginfo *pkg,
  536. struct pkgbin *pkgbin, enum trig_options opts)
  537. {
  538. trig_cicb_interest_change(trig, pkg, pkgbin, +1, opts);
  539. }
  540. void
  541. trig_cicb_statuschange_activate(const char *trig, struct pkginfo *pkg,
  542. struct pkgbin *pkgbin, enum trig_options opts)
  543. {
  544. struct pkginfo *aw = pkg;
  545. trig_activate_start(trig);
  546. dtki->activate_awaiter((opts == TRIG_NOAWAIT) ? NULL : aw);
  547. dtki->activate_done();
  548. }
  549. static void
  550. parse_ci_call(const char *file, const char *cmd, trig_parse_cicb *cb,
  551. const char *trig, struct pkginfo *pkg, struct pkgbin *pkgbin,
  552. enum trig_options opts)
  553. {
  554. const char *emsg;
  555. emsg = trig_name_is_illegal(trig);
  556. if (emsg)
  557. ohshit(_("triggers ci file '%.250s' contains illegal trigger "
  558. "syntax in trigger name '%.250s': %.250s"),
  559. file, trig, emsg);
  560. if (cb)
  561. cb(trig, pkg, pkgbin, opts);
  562. }
  563. void
  564. trig_parse_ci(const char *file, trig_parse_cicb *interest,
  565. trig_parse_cicb *activate, struct pkginfo *pkg,
  566. struct pkgbin *pkgbin)
  567. {
  568. FILE *f;
  569. char linebuf[MAXTRIGDIRECTIVE], *cmd, *spc, *eol;
  570. int l;
  571. f = fopen(file, "r");
  572. if (!f) {
  573. if (errno == ENOENT)
  574. return; /* No file is just like an empty one. */
  575. ohshite(_("unable to open triggers ci file '%.250s'"), file);
  576. }
  577. push_cleanup(cu_closestream, ~0, NULL, 0, 1, f);
  578. while ((l = fgets_checked(linebuf, sizeof(linebuf), f, file)) >= 0) {
  579. for (cmd = linebuf; c_iswhite(*cmd); cmd++) ;
  580. if (*cmd == '#')
  581. continue;
  582. for (eol = linebuf + l; eol > cmd && c_iswhite(eol[-1]); eol--) ;
  583. if (eol == cmd)
  584. continue;
  585. *eol = '\0';
  586. for (spc = cmd; *spc && !c_iswhite(*spc); spc++) ;
  587. if (!*spc)
  588. ohshit(_("triggers ci file contains unknown directive syntax"));
  589. *spc++ = '\0';
  590. while (c_iswhite(*spc))
  591. spc++;
  592. if (strcmp(cmd, "interest") == 0 ||
  593. strcmp(cmd, "interest-await") == 0) {
  594. parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_AWAIT);
  595. } else if (strcmp(cmd, "interest-noawait") == 0) {
  596. parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_NOAWAIT);
  597. } else if (strcmp(cmd, "activate") == 0 ||
  598. strcmp(cmd, "activate-await") == 0) {
  599. parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_AWAIT);
  600. } else if (strcmp(cmd, "activate-noawait") == 0) {
  601. parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_NOAWAIT);
  602. } else {
  603. ohshit(_("triggers ci file contains unknown directive '%.250s'"),
  604. cmd);
  605. }
  606. }
  607. pop_cleanup(ehflag_normaltidy); /* fclose() */
  608. }
  609. /*---------- Unincorp file incorporation. ----------*/
  610. static void
  611. tdm_incorp_trig_begin(const char *trig)
  612. {
  613. trig_activate_start(trig);
  614. }
  615. static void
  616. tdm_incorp_package(const char *awname)
  617. {
  618. struct pkginfo *aw;
  619. if (strcmp(awname, "-") == 0)
  620. aw = NULL;
  621. else
  622. aw = pkg_spec_parse_pkg(awname, NULL);
  623. dtki->activate_awaiter(aw);
  624. }
  625. static void
  626. tdm_incorp_trig_end(void)
  627. {
  628. dtki->activate_done();
  629. }
  630. static const struct trigdefmeths tdm_incorp = {
  631. .trig_begin = tdm_incorp_trig_begin,
  632. .package = tdm_incorp_package,
  633. .trig_end = tdm_incorp_trig_end
  634. };
  635. void
  636. trig_incorporate(enum modstatdb_rw cstatus)
  637. {
  638. enum trigdef_update_status ur;
  639. enum trigdef_update_flags tduf;
  640. free(triggersdir);
  641. triggersdir = dpkg_db_get_path(TRIGGERSDIR);
  642. free(triggersfilefile);
  643. triggersfilefile = trig_get_filename(triggersdir, TRIGGERSFILEFILE);
  644. trigdef_set_methods(&tdm_incorp);
  645. trig_file_interests_ensure();
  646. tduf = TDUF_NO_LOCK_OK;
  647. if (cstatus >= msdbrw_write) {
  648. tduf |= TDUF_WRITE;
  649. if (trigh.transitional_activate)
  650. tduf |= TDUF_WRITE_IF_ENOENT;
  651. }
  652. ur = trigdef_update_start(tduf);
  653. if (ur == TDUS_ERROR_NO_DIR && cstatus >= msdbrw_write) {
  654. if (mkdir(triggersdir, 0755)) {
  655. if (errno != EEXIST)
  656. ohshite(_("unable to create triggers state"
  657. " directory '%.250s'"), triggersdir);
  658. } else if (chown(triggersdir, 0, 0)) {
  659. ohshite(_("unable to set ownership of triggers state"
  660. " directory '%.250s'"), triggersdir);
  661. }
  662. ur = trigdef_update_start(tduf);
  663. }
  664. switch (ur) {
  665. case TDUS_ERROR_EMPTY_DEFERRED:
  666. return;
  667. case TDUS_ERROR_NO_DIR:
  668. case TDUS_ERROR_NO_DEFERRED:
  669. if (!trigh.transitional_activate)
  670. return;
  671. /* Fall through. */
  672. case TDUS_NO_DEFERRED:
  673. trigh.transitional_activate(cstatus);
  674. break;
  675. case TDUS_OK:
  676. /* Read and incorporate triggers. */
  677. trigdef_parse();
  678. break;
  679. default:
  680. internerr("unknown trigdef_update_start return value '%d'", ur);
  681. }
  682. /* Right, that's it. New (empty) Unincorp can be installed. */
  683. trigdef_process_done();
  684. }
  685. /*---------- Default hooks. ----------*/
  686. struct filenamenode {
  687. struct filenamenode *next;
  688. const char *name;
  689. struct trigfileint *trig_interested;
  690. };
  691. static struct filenamenode *trigger_files;
  692. static struct filenamenode *
  693. th_simple_nn_find(const char *name, bool nonew)
  694. {
  695. struct filenamenode *search;
  696. for (search = trigger_files; search; search = search->next)
  697. if (strcmp(search->name, name) == 0)
  698. return search;
  699. /* Not found. */
  700. if (nonew)
  701. return NULL;
  702. search = nfmalloc(sizeof(*search));
  703. search->name = nfstrsave(name);
  704. search->trig_interested = NULL;
  705. search->next = trigger_files;
  706. trigger_files = search;
  707. return search;
  708. }
  709. TRIGHOOKS_DEFINE_NAMENODE_ACCESSORS
  710. static struct trig_hooks trigh = {
  711. .enqueue_deferred = NULL,
  712. .transitional_activate = NULL,
  713. .namenode_find = th_simple_nn_find,
  714. .namenode_interested = th_nn_interested,
  715. .namenode_name = th_nn_name,
  716. };
  717. void
  718. trig_override_hooks(const struct trig_hooks *hooks)
  719. {
  720. trigh = *hooks;
  721. }