triglib.c 21 KB

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