trigcmd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * dpkg-trigger - trigger management utility
  3. *
  4. * Copyright © 2007 Canonical Ltd.
  5. * Written by Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
  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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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 License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #if HAVE_LOCALE_H
  26. #include <locale.h>
  27. #endif
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/options.h>
  36. #include <dpkg/trigdeferred.h>
  37. #include <dpkg/triglib.h>
  38. #include <dpkg/pkg-spec.h>
  39. static const char printforhelp[] = N_(
  40. "Type dpkg-trigger --help for help about this utility.");
  41. static void DPKG_ATTR_NORET
  42. printversion(const struct cmdinfo *ci, const char *value)
  43. {
  44. printf(_("Debian %s package trigger utility version %s.\n"),
  45. dpkg_get_progname(), PACKAGE_RELEASE);
  46. printf(_(
  47. "This is free software; see the GNU General Public License version 2 or\n"
  48. "later for copying conditions. There is NO warranty.\n"));
  49. m_output(stdout, _("<standard output>"));
  50. exit(0);
  51. }
  52. static void DPKG_ATTR_NORET
  53. usage(const struct cmdinfo *ci, const char *value)
  54. {
  55. printf(_(
  56. "Usage: %s [<options> ...] <trigger-name>\n"
  57. " %s [<options> ...] <command>\n"
  58. "\n"), dpkg_get_progname(), dpkg_get_progname());
  59. printf(_(
  60. "Commands:\n"
  61. " --check-supported Check if the running dpkg supports triggers.\n"
  62. "\n"));
  63. printf(_(
  64. " -?, --help Show this help message.\n"
  65. " --version Show the version.\n"
  66. "\n"));
  67. printf(_(
  68. "Options:\n"
  69. " --admindir=<directory> Use <directory> instead of %s.\n"
  70. " --by-package=<package> Override trigger awaiter (normally set\n"
  71. " by dpkg).\n"
  72. " --await Package needs to await the processing.\n"
  73. " --no-await No package needs to await the processing.\n"
  74. " --no-act Just test - don't actually change anything.\n"
  75. "\n"), ADMINDIR);
  76. m_output(stdout, _("<standard output>"));
  77. exit(0);
  78. }
  79. static const char *admindir;
  80. static int f_noact, f_check;
  81. static int f_await = 1;
  82. static const char *bypackage, *activate;
  83. static bool done_trig, ctrig;
  84. static void
  85. yespackage(const char *awname)
  86. {
  87. trigdef_update_printf(" %s", awname);
  88. }
  89. static const char *
  90. parse_awaiter_package(void)
  91. {
  92. struct dpkg_error err = DPKG_ERROR_INIT;
  93. struct pkginfo *pkg;
  94. if (!f_await)
  95. bypackage = "-";
  96. if (bypackage == NULL) {
  97. const char *pkgname, *archname;
  98. pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE");
  99. archname = getenv("DPKG_MAINTSCRIPT_ARCH");
  100. if (pkgname == NULL || archname == NULL)
  101. badusage(_("must be called from a maintainer script"
  102. " (or with a --by-package option)"));
  103. pkg = pkg_spec_find_pkg(pkgname, archname, &err);
  104. } else if (strcmp(bypackage, "-") == 0) {
  105. pkg = NULL;
  106. } else {
  107. pkg = pkg_spec_parse_pkg(bypackage, &err);
  108. }
  109. /* Normalize the bypackage name if there was no error. */
  110. if (pkg)
  111. bypackage = pkg_name(pkg, pnaw_nonambig);
  112. return err.str;
  113. }
  114. static void
  115. tdm_add_trig_begin(const char *trig)
  116. {
  117. ctrig = strcmp(trig, activate) == 0;
  118. trigdef_update_printf("%s", trig);
  119. if (!ctrig || done_trig)
  120. return;
  121. yespackage(bypackage);
  122. done_trig = true;
  123. }
  124. static void
  125. tdm_add_package(const char *awname)
  126. {
  127. if (ctrig && strcmp(awname, bypackage) == 0)
  128. return;
  129. yespackage(awname);
  130. }
  131. static void
  132. tdm_add_trig_end(void)
  133. {
  134. trigdef_update_printf("\n");
  135. }
  136. static const struct trigdefmeths tdm_add = {
  137. .trig_begin = tdm_add_trig_begin,
  138. .package = tdm_add_package,
  139. .trig_end = tdm_add_trig_end,
  140. };
  141. static int
  142. do_check(void)
  143. {
  144. enum trigdef_update_status uf;
  145. uf = trigdef_update_start(TDUF_NO_LOCK_OK);
  146. switch (uf) {
  147. case TDUS_ERROR_NO_DIR:
  148. notice(_("triggers data directory not yet created"));
  149. return 1;
  150. case TDUS_ERROR_NO_DEFERRED:
  151. notice(_("trigger records not yet in existence"));
  152. return 1;
  153. case TDUS_OK:
  154. case TDUS_ERROR_EMPTY_DEFERRED:
  155. return 0;
  156. default:
  157. internerr("unknown trigdef_update_start return value '%d'", uf);
  158. }
  159. }
  160. static const struct cmdinfo cmdinfos[] = {
  161. { "admindir", 0, 1, NULL, &admindir },
  162. { "by-package", 'f', 1, NULL, &bypackage },
  163. { "await", 0, 0, &f_await, NULL, NULL, 1 },
  164. { "no-await", 0, 0, &f_await, NULL, NULL, 0 },
  165. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  166. { "check-supported", 0, 0, &f_check, NULL, NULL, 1 },
  167. { "help", '?', 0, NULL, NULL, usage },
  168. { "version", 0, 0, NULL, NULL, printversion },
  169. { NULL }
  170. };
  171. int
  172. main(int argc, const char *const *argv)
  173. {
  174. const char *badname;
  175. enum trigdef_update_flags tduf;
  176. enum trigdef_update_status tdus;
  177. dpkg_locales_init(PACKAGE);
  178. dpkg_program_init("dpkg-trigger");
  179. dpkg_options_parse(&argv, cmdinfos, printforhelp);
  180. admindir = dpkg_db_set_dir(admindir);
  181. if (f_check) {
  182. if (*argv)
  183. badusage(_("--%s takes no arguments"),
  184. "check-supported");
  185. return do_check();
  186. }
  187. if (!*argv || argv[1])
  188. badusage(_("takes one argument, the trigger name"));
  189. badname = parse_awaiter_package();
  190. if (badname)
  191. badusage(_("illegal awaited package name '%.250s': %.250s"),
  192. bypackage, badname);
  193. activate = argv[0];
  194. badname = trig_name_is_illegal(activate);
  195. if (badname)
  196. badusage(_("invalid trigger name '%.250s': %.250s"),
  197. activate, badname);
  198. trigdef_set_methods(&tdm_add);
  199. tduf = TDUF_NO_LOCK_OK;
  200. if (!f_noact)
  201. tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY;
  202. tdus = trigdef_update_start(tduf);
  203. if (tdus >= 0) {
  204. trigdef_parse();
  205. if (!done_trig)
  206. trigdef_update_printf("%s %s\n", activate, bypackage);
  207. trigdef_process_done();
  208. }
  209. dpkg_program_done();
  210. return 0;
  211. }