trigcmd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. const char thisname[] = "dpkg-trigger";
  39. 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. thisname, DPKG_VERSION_ARCH);
  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"), thisname, thisname);
  59. printf(_(
  60. "Commands:\n"
  61. " --check-supported Check if the running dpkg supports triggers.\n"
  62. "\n"));
  63. printf(_(
  64. " -h|--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. " --no-await No package needs to await the processing.\n"
  73. " --no-act Just test - don't actually change anything.\n"
  74. "\n"), ADMINDIR);
  75. m_output(stdout, _("<standard output>"));
  76. exit(0);
  77. }
  78. static const char *admindir = ADMINDIR;
  79. static int f_noact, f_check;
  80. static const char *bypackage, *activate;
  81. static int done_trig, ctrig;
  82. static void
  83. noawait(const struct cmdinfo *ci, const char *value)
  84. {
  85. bypackage = "-";
  86. }
  87. static void
  88. yespackage(const char *awname)
  89. {
  90. trigdef_update_printf(" %s", awname);
  91. }
  92. static void
  93. tdm_add_trig_begin(const char *trig)
  94. {
  95. ctrig = !strcmp(trig, activate);
  96. trigdef_update_printf("%s", trig);
  97. if (!ctrig || done_trig)
  98. return;
  99. yespackage(bypackage);
  100. done_trig = 1;
  101. }
  102. static void
  103. tdm_add_package(const char *awname)
  104. {
  105. if (ctrig && !strcmp(awname, bypackage))
  106. return;
  107. yespackage(awname);
  108. }
  109. static void
  110. tdm_add_trig_end(void)
  111. {
  112. trigdef_update_printf("\n");
  113. }
  114. static const struct trigdefmeths tdm_add = {
  115. .trig_begin = tdm_add_trig_begin,
  116. .package = tdm_add_package,
  117. .trig_end = tdm_add_trig_end,
  118. };
  119. static void
  120. do_check(void)
  121. {
  122. int uf;
  123. uf = trigdef_update_start(tduf_nolockok, admindir);
  124. switch (uf) {
  125. case -1:
  126. fprintf(stderr, _("%s: triggers data directory not yet created\n"),
  127. thisname);
  128. exit(1);
  129. case -3:
  130. fprintf(stderr, _("%s: trigger records not yet in existence\n"),
  131. thisname);
  132. exit(1);
  133. case 2:
  134. case -2:
  135. exit(0);
  136. default:
  137. internerr("unknown trigdef_update_start return value '%d'", uf);
  138. }
  139. }
  140. static const struct cmdinfo cmdinfos[] = {
  141. { "admindir", 0, 1, NULL, &admindir },
  142. { "by-package", 'f', 1, NULL, &bypackage },
  143. { "no-await", 0, 0, NULL, &bypackage, noawait },
  144. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  145. { "check-supported", 0, 0, &f_check, NULL, NULL, 1 },
  146. { "help", 'h', 0, NULL, NULL, usage },
  147. { "version", 0, 0, NULL, NULL, printversion },
  148. { NULL }
  149. };
  150. int
  151. main(int argc, const char *const *argv)
  152. {
  153. jmp_buf ejbuf;
  154. int uf;
  155. const char *badname;
  156. enum trigdef_updateflags tduf;
  157. setlocale(LC_ALL, "");
  158. bindtextdomain(PACKAGE, LOCALEDIR);
  159. textdomain(PACKAGE);
  160. standard_startup(&ejbuf);
  161. myopt(&argv, cmdinfos);
  162. setvbuf(stdout, NULL, _IONBF, 0);
  163. if (f_check) {
  164. if (*argv)
  165. badusage(_("--%s takes no arguments"),
  166. "check-supported");
  167. do_check();
  168. }
  169. if (!*argv || argv[1])
  170. badusage(_("takes one argument, the trigger name"));
  171. if (!bypackage) {
  172. bypackage = getenv(MAINTSCRIPTPKGENVVAR);
  173. if (!bypackage)
  174. ohshit(_("dpkg-trigger must be called from a maintainer script"
  175. " (or with a --by-package option)"));
  176. }
  177. if (strcmp(bypackage, "-") &&
  178. (badname = illegal_packagename(bypackage, NULL)))
  179. ohshit(_("dpkg-trigger: illegal awaited package name `%.250s': %.250s"),
  180. bypackage, badname);
  181. activate = argv[0];
  182. if ((badname = illegal_triggername(activate)))
  183. badusage(_("invalid trigger name `%.250s': %.250s"),
  184. activate, badname);
  185. trigdef_set_methods(&tdm_add);
  186. tduf = tduf_nolockok;
  187. if (!f_noact)
  188. tduf |= tduf_write | tduf_writeifempty;
  189. uf = trigdef_update_start(tduf, admindir);
  190. if (uf >= 0) {
  191. trigdef_yylex();
  192. if (!done_trig)
  193. trigdef_update_printf("%s %s\n", activate, bypackage);
  194. trigdef_process_done();
  195. }
  196. standard_shutdown();
  197. return 0;
  198. }