Branch data Line data Source code
1 : : /*
2 : : * dpkg-trigger - trigger management utility
3 : : *
4 : : * Copyright © 2007 Canonical Ltd.
5 : : * Written by Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
7 : : *
8 : : * This is free software; you can redistribute it and/or modify
9 : : * it under the terms of the GNU General Public License as published by
10 : : * the Free Software Foundation; either version 2 of the License, or
11 : : * (at your option) any later version.
12 : : *
13 : : * This is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : * GNU General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU General Public License
19 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <sys/types.h>
26 : :
27 : : #include <fcntl.h>
28 : : #if HAVE_LOCALE_H
29 : : #include <locale.h>
30 : : #endif
31 : : #include <string.h>
32 : : #include <unistd.h>
33 : : #include <stdlib.h>
34 : : #include <stdio.h>
35 : :
36 : : #include <dpkg/i18n.h>
37 : : #include <dpkg/dpkg.h>
38 : : #include <dpkg/dpkg-db.h>
39 : : #include <dpkg/debug.h>
40 : : #include <dpkg/options.h>
41 : : #include <dpkg/trigdeferred.h>
42 : : #include <dpkg/triglib.h>
43 : : #include <dpkg/pkg-spec.h>
44 : :
45 : : static const char printforhelp[] = N_(
46 : : "Type dpkg-trigger --help for help about this utility.");
47 : :
48 : : static int
49 : 19 : printversion(const char *const *argv)
50 : : {
51 : 19 : printf(_("Debian %s package trigger utility version %s.\n"),
52 : : dpkg_get_progname(), PACKAGE_RELEASE);
53 : :
54 : 19 : printf(_(
55 : : "This is free software; see the GNU General Public License version 2 or\n"
56 : : "later for copying conditions. There is NO warranty.\n"));
57 : :
58 : 19 : m_output(stdout, _("<standard output>"));
59 : :
60 : 19 : return 0;
61 : : }
62 : :
63 : : static int
64 : 0 : usage(const char *const *argv)
65 : : {
66 : 0 : printf(_(
67 : : "Usage: %s [<option>...] <trigger-name>\n"
68 : : " %s [<option>...] <command>\n"
69 : : "\n"), dpkg_get_progname(), dpkg_get_progname());
70 : :
71 : 0 : printf(_(
72 : : "Commands:\n"
73 : : " --check-supported Check if the running dpkg supports triggers.\n"
74 : : "\n"));
75 : :
76 : 0 : printf(_(
77 : : " -?, --help Show this help message.\n"
78 : : " --version Show the version.\n"
79 : : "\n"));
80 : :
81 : 0 : printf(_(
82 : : "Options:\n"
83 : : " --admindir=<directory> Use <directory> instead of %s.\n"
84 : : " --root=<directory> Use <directory> instead of %s.\n"
85 : : " --by-package=<package> Override trigger awaiter (normally set\n"
86 : : " by dpkg).\n"
87 : : " --await Package needs to await the processing.\n"
88 : : " --no-await No package needs to await the processing.\n"
89 : : " --no-act Just test - don't actually change anything.\n"
90 : : "\n"), ADMINDIR, "/");
91 : :
92 : 0 : m_output(stdout, _("<standard output>"));
93 : :
94 : 0 : return 0;
95 : : }
96 : :
97 : : static int f_noact;
98 : : static int f_await = 1;
99 : :
100 : : static const char *bypackage, *activate;
101 : : static bool done_trig, ctrig;
102 : :
103 : : static void
104 : 0 : yespackage(const char *awname)
105 : : {
106 : 0 : trigdef_update_printf(" %s", awname);
107 : 0 : }
108 : :
109 : : static const char *
110 : 0 : parse_awaiter_package(void)
111 : : {
112 : 0 : struct dpkg_error err = DPKG_ERROR_INIT;
113 : : struct pkginfo *pkg;
114 : :
115 [ # # ]: 0 : if (!f_await)
116 : 0 : bypackage = "-";
117 : :
118 [ # # ]: 0 : if (bypackage == NULL) {
119 : : const char *pkgname, *archname;
120 : :
121 : 0 : pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE");
122 : 0 : archname = getenv("DPKG_MAINTSCRIPT_ARCH");
123 [ # # # # ]: 0 : if (pkgname == NULL || archname == NULL)
124 : 0 : badusage(_("must be called from a maintainer script"
125 : : " (or with a --by-package option)"));
126 : :
127 : 0 : pkg = pkg_spec_find_pkg(pkgname, archname, &err);
128 [ # # ]: 0 : } else if (strcmp(bypackage, "-") == 0) {
129 : 0 : pkg = NULL;
130 : : } else {
131 : 0 : pkg = pkg_spec_parse_pkg(bypackage, &err);
132 : : }
133 : :
134 : : /* Normalize the bypackage name if there was no error. */
135 [ # # ]: 0 : if (pkg)
136 : 0 : bypackage = pkg_name(pkg, pnaw_same);
137 : :
138 : 0 : return err.str;
139 : : }
140 : :
141 : : static void
142 : 0 : tdm_add_trig_begin(const char *trig)
143 : : {
144 : 0 : ctrig = strcmp(trig, activate) == 0;
145 : 0 : trigdef_update_printf("%s", trig);
146 [ # # # # ]: 0 : if (!ctrig || done_trig)
147 : 0 : return;
148 : 0 : yespackage(bypackage);
149 : 0 : done_trig = true;
150 : : }
151 : :
152 : : static void
153 : 0 : tdm_add_package(const char *awname)
154 : : {
155 [ # # # # ]: 0 : if (ctrig && strcmp(awname, bypackage) == 0)
156 : 0 : return;
157 : 0 : yespackage(awname);
158 : : }
159 : :
160 : : static void
161 : 0 : tdm_add_trig_end(void)
162 : : {
163 : 0 : trigdef_update_printf("\n");
164 : 0 : }
165 : :
166 : : static const struct trigdefmeths tdm_add = {
167 : : .trig_begin = tdm_add_trig_begin,
168 : : .package = tdm_add_package,
169 : : .trig_end = tdm_add_trig_end,
170 : : };
171 : :
172 : : static int
173 : 0 : do_trigger(const char *const *argv)
174 : : {
175 : : const char *badname;
176 : : enum trigdef_update_flags tduf;
177 : : enum trigdef_update_status tdus;
178 : :
179 [ # # # # ]: 0 : if (!*argv || argv[1])
180 : 0 : badusage(_("takes one argument, the trigger name"));
181 : :
182 : 0 : badname = parse_awaiter_package();
183 [ # # ]: 0 : if (badname)
184 : 0 : badusage(_("illegal awaited package name '%.250s': %.250s"),
185 : : bypackage, badname);
186 : :
187 : 0 : activate = argv[0];
188 : 0 : badname = trig_name_is_illegal(activate);
189 [ # # ]: 0 : if (badname)
190 : 0 : badusage(_("invalid trigger name '%.250s': %.250s"),
191 : : activate, badname);
192 : :
193 : 0 : trigdef_set_methods(&tdm_add);
194 : :
195 : 0 : tduf = TDUF_NO_LOCK_OK;
196 [ # # ]: 0 : if (!f_noact)
197 : 0 : tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY;
198 : 0 : tdus = trigdef_update_start(tduf);
199 [ # # ]: 0 : if (tdus >= 0) {
200 : 0 : trigdef_parse();
201 [ # # ]: 0 : if (!done_trig)
202 : 0 : trigdef_update_printf("%s %s\n", activate, bypackage);
203 : 0 : trigdef_process_done();
204 : : }
205 : :
206 : 0 : return 0;
207 : : }
208 : :
209 : : static int
210 : 0 : do_check(const char *const *argv)
211 : : {
212 : : enum trigdef_update_status uf;
213 : :
214 [ # # ]: 0 : if (*argv)
215 : 0 : badusage(_("--%s takes no arguments"), "check-supported");
216 : :
217 : 0 : uf = trigdef_update_start(TDUF_NO_LOCK_OK);
218 [ # # # # ]: 0 : switch (uf) {
219 : 0 : case TDUS_ERROR_NO_DIR:
220 : 0 : notice(_("triggers data directory not yet created"));
221 : 0 : return 1;
222 : 0 : case TDUS_ERROR_NO_DEFERRED:
223 : 0 : notice(_("trigger records not yet in existence"));
224 : 0 : return 1;
225 : 0 : case TDUS_OK:
226 : : case TDUS_ERROR_EMPTY_DEFERRED:
227 : 0 : return 0;
228 : 0 : default:
229 : 0 : internerr("unknown trigdef_update_start return value '%d'", uf);
230 : : }
231 : : }
232 : :
233 : : static const struct cmdinfo cmdinfo_trigger =
234 : : ACTION("trigger", 0, 0, do_trigger);
235 : :
236 : : static const struct cmdinfo cmdinfos[] = {
237 : : ACTION("check-supported", 0, 0, do_check),
238 : : ACTION("help", '?', 0, usage),
239 : : ACTION("version", 0, 0, printversion),
240 : :
241 : : { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
242 : : { "root", 0, 1, NULL, NULL, set_root, 0 },
243 : : { "by-package", 'f', 1, NULL, &bypackage },
244 : : { "await", 0, 0, &f_await, NULL, NULL, 1 },
245 : : { "no-await", 0, 0, &f_await, NULL, NULL, 0 },
246 : : { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
247 : : { NULL }
248 : : };
249 : :
250 : : int
251 : 19 : main(int argc, const char *const *argv)
252 : : {
253 : : int ret;
254 : :
255 : 19 : dpkg_locales_init(PACKAGE);
256 : 19 : dpkg_program_init("dpkg-trigger");
257 : 19 : dpkg_options_parse(&argv, cmdinfos, printforhelp);
258 : :
259 : 19 : debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
260 : :
261 [ - + ]: 19 : if (!cipaction)
262 : 0 : setaction(&cmdinfo_trigger, NULL);
263 : :
264 : 19 : ret = cipaction->action(argv);
265 : :
266 : 19 : dpkg_program_done();
267 : 19 : dpkg_locales_done();
268 : :
269 : 19 : return ret;
270 : : }
|