trigcmd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * dpkg-trigger - trigger management utility
  3. *
  4. * Copyright © 2007 Canonical Ltd.
  5. * Written by Ian Jackson <ian@davenant.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include <sys/termios.h>
  26. #include <fcntl.h>
  27. #if HAVE_LOCALE_H
  28. #include <locale.h>
  29. #endif
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <dpkg/i18n.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/myopt.h>
  38. #include <dpkg/trigdeferred.h>
  39. #include <dpkg/triglib.h>
  40. const char thisname[] = "dpkg-trigger";
  41. const char printforhelp[] = N_(
  42. "Type dpkg-trigger --help for help about this utility.");
  43. static void DPKG_ATTR_NORET
  44. printversion(const struct cmdinfo *ci, const char *value)
  45. {
  46. printf(_("Debian %s package trigger utility version %s.\n"),
  47. thisname, DPKG_VERSION_ARCH);
  48. printf(_(
  49. "This is free software; see the GNU General Public License version 2 or\n"
  50. "later for copying conditions. There is NO warranty.\n"));
  51. m_output(stdout, _("<standard output>"));
  52. exit(0);
  53. }
  54. static void DPKG_ATTR_NORET
  55. usage(const struct cmdinfo *ci, const char *value)
  56. {
  57. printf(_(
  58. "Usage: %s [<options> ...] <trigger-name>\n"
  59. " %s [<options> ...] <command>\n"
  60. "\n"), thisname, thisname);
  61. printf(_(
  62. "Commands:\n"
  63. " --check-supported Check if the running dpkg supports triggers.\n"
  64. "\n"));
  65. printf(_(
  66. " -h|--help Show this help message.\n"
  67. " --version Show the version.\n"
  68. "\n"));
  69. printf(_(
  70. "Options:\n"
  71. " --admindir=<directory> Use <directory> instead of %s.\n"
  72. " --by-package=<package> Override trigger awaiter (normally set\n"
  73. " by dpkg).\n"
  74. " --no-await No package needs to await the processing.\n"
  75. " --no-act Just test - don't actually change anything.\n"
  76. "\n"), ADMINDIR);
  77. m_output(stdout, _("<standard output>"));
  78. exit(0);
  79. }
  80. static const char *admindir = ADMINDIR;
  81. static int f_noact, f_check;
  82. static const char *bypackage, *activate;
  83. static int done_trig, ctrig;
  84. static void
  85. noawait(const struct cmdinfo *ci, const char *value)
  86. {
  87. bypackage = "-";
  88. }
  89. static void
  90. yespackage(const char *awname)
  91. {
  92. trigdef_update_printf(" %s", awname);
  93. }
  94. static void
  95. tdm_add_trig_begin(const char *trig)
  96. {
  97. ctrig = !strcmp(trig, activate);
  98. trigdef_update_printf("%s", trig);
  99. if (!ctrig || done_trig)
  100. return;
  101. yespackage(bypackage);
  102. done_trig = 1;
  103. }
  104. static void
  105. tdm_add_package(const char *awname)
  106. {
  107. if (ctrig && !strcmp(awname, bypackage))
  108. return;
  109. yespackage(awname);
  110. }
  111. static void
  112. tdm_add_trig_end(void)
  113. {
  114. trigdef_update_printf("\n");
  115. }
  116. static const struct trigdefmeths tdm_add = {
  117. .trig_begin = tdm_add_trig_begin,
  118. .package = tdm_add_package,
  119. .trig_end = tdm_add_trig_end,
  120. };
  121. static void
  122. do_check(void)
  123. {
  124. int uf;
  125. uf = trigdef_update_start(tduf_nolockok, admindir);
  126. switch (uf) {
  127. case -1:
  128. fprintf(stderr, _("%s: triggers data directory not yet created\n"),
  129. thisname);
  130. exit(1);
  131. case -3:
  132. fprintf(stderr, _("%s: trigger records not yet in existence\n"),
  133. thisname);
  134. exit(1);
  135. case 2:
  136. case -2:
  137. exit(0);
  138. default:
  139. internerr("unknown trigdef_update_start return value '%d'", uf);
  140. }
  141. }
  142. static const struct cmdinfo cmdinfos[] = {
  143. { "admindir", 0, 1, NULL, &admindir },
  144. { "by-package", 'f', 1, NULL, &bypackage },
  145. { "no-await", 0, 0, NULL, &bypackage, noawait },
  146. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  147. { "check-supported", 0, 0, &f_check, NULL, NULL, 1 },
  148. { "help", 'h', 0, NULL, NULL, usage },
  149. { "version", 0, 0, NULL, NULL, printversion },
  150. { NULL }
  151. };
  152. int
  153. main(int argc, const char *const *argv)
  154. {
  155. jmp_buf ejbuf;
  156. int uf;
  157. const char *badname;
  158. enum trigdef_updateflags tduf;
  159. setlocale(LC_ALL, "");
  160. bindtextdomain(PACKAGE, LOCALEDIR);
  161. textdomain(PACKAGE);
  162. standard_startup(&ejbuf);
  163. myopt(&argv, cmdinfos);
  164. setvbuf(stdout, NULL, _IONBF, 0);
  165. if (f_check) {
  166. if (*argv)
  167. badusage(_("--%s takes no arguments"),
  168. "check-supported");
  169. do_check();
  170. }
  171. if (!*argv || argv[1])
  172. badusage(_("takes one argument, the trigger name"));
  173. if (!bypackage) {
  174. bypackage = getenv(MAINTSCRIPTPKGENVVAR);
  175. if (!bypackage)
  176. ohshit(_("dpkg-trigger must be called from a maintainer script"
  177. " (or with a --by-package option)"));
  178. }
  179. if (strcmp(bypackage, "-") &&
  180. (badname = illegal_packagename(bypackage, NULL)))
  181. ohshit(_("dpkg-trigger: illegal awaited package name `%.250s': %.250s"),
  182. bypackage, badname);
  183. activate = argv[0];
  184. if ((badname = illegal_triggername(activate)))
  185. badusage(_("invalid trigger name `%.250s': %.250s"),
  186. activate, badname);
  187. trigdef_set_methods(&tdm_add);
  188. tduf = tduf_nolockok;
  189. if (!f_noact)
  190. tduf |= tduf_write | tduf_writeifempty;
  191. uf = trigdef_update_start(tduf, admindir);
  192. if (uf >= 0) {
  193. trigdef_yylex();
  194. if (!done_trig)
  195. trigdef_update_printf("%s %s\n", activate, bypackage);
  196. trigdef_process_done();
  197. }
  198. standard_shutdown();
  199. return 0;
  200. }