trigcmd.c 5.8 KB

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