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