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 : : #ifdef 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 opt_noact;
98 : : static int opt_await = 1;
99 : : static const char *opt_bypackage;
100 : :
101 : : static const char *activate;
102 : : static bool done_trig, ctrig;
103 : :
104 : : static void
105 : 0 : yespackage(const char *awname)
106 : : {
107 : 0 : trigdef_update_printf(" %s", awname);
108 : 0 : }
109 : :
110 : : static const char *
111 : 0 : parse_awaiter_package(void)
112 : : {
113 : 0 : struct dpkg_error err = DPKG_ERROR_INIT;
114 : : struct pkginfo *pkg;
115 : :
116 [ # # ]: 0 : if (!opt_await)
117 : 0 : opt_bypackage = "-";
118 : :
119 [ # # ]: 0 : if (opt_bypackage == NULL) {
120 : : const char *pkgname, *archname;
121 : :
122 : 0 : pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE");
123 : 0 : archname = getenv("DPKG_MAINTSCRIPT_ARCH");
124 [ # # # # ]: 0 : if (pkgname == NULL || archname == NULL)
125 : 0 : badusage(_("must be called from a maintainer script"
126 : : " (or with a --by-package option)"));
127 : :
128 : 0 : pkg = pkg_spec_find_pkg(pkgname, archname, &err);
129 [ # # ]: 0 : } else if (strcmp(opt_bypackage, "-") == 0) {
130 : 0 : pkg = NULL;
131 : : } else {
132 : 0 : pkg = pkg_spec_parse_pkg(opt_bypackage, &err);
133 : : }
134 : :
135 : : /* Normalize the bypackage name if there was no error. */
136 [ # # ]: 0 : if (pkg)
137 : 0 : opt_bypackage = pkg_name(pkg, pnaw_same);
138 : :
139 : 0 : return err.str;
140 : : }
141 : :
142 : : static void
143 : 0 : tdm_add_trig_begin(const char *trig)
144 : : {
145 : 0 : ctrig = strcmp(trig, activate) == 0;
146 : 0 : trigdef_update_printf("%s", trig);
147 [ # # # # ]: 0 : if (!ctrig || done_trig)
148 : 0 : return;
149 : 0 : yespackage(opt_bypackage);
150 : 0 : done_trig = true;
151 : : }
152 : :
153 : : static void
154 : 0 : tdm_add_package(const char *awname)
155 : : {
156 [ # # # # ]: 0 : if (ctrig && strcmp(awname, opt_bypackage) == 0)
157 : 0 : return;
158 : 0 : yespackage(awname);
159 : : }
160 : :
161 : : static void
162 : 0 : tdm_add_trig_end(void)
163 : : {
164 : 0 : trigdef_update_printf("\n");
165 : 0 : }
166 : :
167 : : static const struct trigdefmeths tdm_add = {
168 : : .trig_begin = tdm_add_trig_begin,
169 : : .package = tdm_add_package,
170 : : .trig_end = tdm_add_trig_end,
171 : : };
172 : :
173 : : static int
174 : 0 : do_trigger(const char *const *argv)
175 : : {
176 : : const char *badname;
177 : : enum trigdef_update_flags tduf;
178 : : enum trigdef_update_status tdus;
179 : :
180 [ # # # # ]: 0 : if (!*argv || argv[1])
181 : 0 : badusage(_("takes one argument, the trigger name"));
182 : :
183 : 0 : badname = parse_awaiter_package();
184 [ # # ]: 0 : if (badname)
185 : 0 : badusage(_("illegal awaited package name '%.250s': %.250s"),
186 : : opt_bypackage, badname);
187 : :
188 : 0 : activate = argv[0];
189 : 0 : badname = trig_name_is_illegal(activate);
190 [ # # ]: 0 : if (badname)
191 : 0 : badusage(_("invalid trigger name '%.250s': %.250s"),
192 : : activate, badname);
193 : :
194 : 0 : trigdef_set_methods(&tdm_add);
195 : :
196 : 0 : tduf = TDUF_NO_LOCK_OK;
197 [ # # ]: 0 : if (!opt_noact)
198 : 0 : tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY;
199 : 0 : tdus = trigdef_update_start(tduf);
200 [ # # ]: 0 : if (tdus >= 0) {
201 : 0 : trigdef_parse();
202 [ # # ]: 0 : if (!done_trig)
203 : 0 : trigdef_update_printf("%s %s\n", activate, opt_bypackage);
204 : 0 : trigdef_process_done();
205 : : }
206 : :
207 : 0 : return 0;
208 : : }
209 : :
210 : : static int
211 : 0 : do_check(const char *const *argv)
212 : : {
213 : : enum trigdef_update_status uf;
214 : :
215 [ # # ]: 0 : if (*argv)
216 : 0 : badusage(_("--%s takes no arguments"), "check-supported");
217 : :
218 : 0 : uf = trigdef_update_start(TDUF_NO_LOCK_OK);
219 [ # # # # ]: 0 : switch (uf) {
220 : 0 : case TDUS_ERROR_NO_DIR:
221 : 0 : notice(_("triggers data directory not yet created"));
222 : 0 : return 1;
223 : 0 : case TDUS_ERROR_NO_DEFERRED:
224 : 0 : notice(_("trigger records not yet in existence"));
225 : 0 : return 1;
226 : 0 : case TDUS_OK:
227 : : case TDUS_ERROR_EMPTY_DEFERRED:
228 : 0 : return 0;
229 : 0 : default:
230 : 0 : internerr("unknown trigdef_update_start return value '%d'", uf);
231 : : }
232 : : }
233 : :
234 : : static const struct cmdinfo cmdinfo_trigger =
235 : : ACTION("trigger", 0, 0, do_trigger);
236 : :
237 : : static const struct cmdinfo cmdinfos[] = {
238 : : ACTION("check-supported", 0, 0, do_check),
239 : : ACTION("help", '?', 0, usage),
240 : : ACTION("version", 0, 0, printversion),
241 : :
242 : : { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
243 : : { "root", 0, 1, NULL, NULL, set_root, 0 },
244 : : { "by-package", 'f', 1, NULL, &opt_bypackage },
245 : : { "await", 0, 0, &opt_await, NULL, NULL, 1 },
246 : : { "no-await", 0, 0, &opt_await, NULL, NULL, 0 },
247 : : { "no-act", 0, 0, &opt_noact, NULL, NULL, 1 },
248 : : { NULL }
249 : : };
250 : :
251 : : int
252 : 19 : main(int argc, const char *const *argv)
253 : : {
254 : : int ret;
255 : :
256 : 19 : dpkg_locales_init(PACKAGE);
257 : 19 : dpkg_program_init("dpkg-trigger");
258 : 19 : dpkg_options_parse(&argv, cmdinfos, printforhelp);
259 : :
260 : 19 : debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
261 : :
262 [ - + ]: 19 : if (!cipaction)
263 : 0 : setaction(&cmdinfo_trigger, NULL);
264 : :
265 : 19 : ret = cipaction->action(argv);
266 : :
267 : 19 : dpkg_program_done();
268 : 19 : dpkg_locales_done();
269 : :
270 : 19 : return ret;
271 : : }
|