trigcmd.c 5.6 KB

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