trigcmd.c 6.4 KB

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