| 1 |
/* |
| 2 |
* dpkg-trigger - trigger management utility |
| 3 |
* |
| 4 |
* Copyright © 2007 Canonical Ltd. |
| 5 |
* Written by Ian Jackson <[email protected]> |
| 6 |
* Copyright © 2008-2014 Guillem Jover <[email protected]> |
| 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 |
printversion(const char *const *argv) |
| 50 |
{ |
| 51 |
printf(_("Debian %s package trigger utility version %s.\n"), |
| 52 |
dpkg_get_progname(), PACKAGE_RELEASE); |
| 53 |
|
| 54 |
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 |
m_output(stdout, _("<standard output>")); |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static int |
| 64 |
usage(const char *const *argv) |
| 65 |
{ |
| 66 |
printf(_( |
| 67 |
"Usage: %s [<option>...] <trigger-name>\n" |
| 68 |
" %s [<option>...] <command>\n" |
| 69 |
"\n"), dpkg_get_progname(), dpkg_get_progname()); |
| 70 |
|
| 71 |
printf(_( |
| 72 |
"Commands:\n" |
| 73 |
" --check-supported Check if the running dpkg supports triggers.\n" |
| 74 |
"\n")); |
| 75 |
|
| 76 |
printf(_( |
| 77 |
" -?, --help Show this help message.\n" |
| 78 |
" --version Show the version.\n" |
| 79 |
"\n")); |
| 80 |
|
| 81 |
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 - do not actually change anything.\n" |
| 90 |
"\n"), ADMINDIR, "/"); |
| 91 |
|
| 92 |
m_output(stdout, _("<standard output>")); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static int opt_act = 1; |
| 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 |
yespackage(const char *awname) |
| 106 |
{ |
| 107 |
trigdef_update_printf(" %s", awname); |
| 108 |
} |
| 109 |
|
| 110 |
static const char * |
| 111 |
parse_awaiter_package(void) |
| 112 |
{ |
| 113 |
struct dpkg_error err = DPKG_ERROR_INIT; |
| 114 |
struct pkginfo *pkg; |
| 115 |
|
| 116 |
if (!opt_await) |
| 117 |
opt_bypackage = "-"; |
| 118 |
|
| 119 |
if (opt_bypackage == NULL) { |
| 120 |
const char *pkgname, *archname; |
| 121 |
|
| 122 |
pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE"); |
| 123 |
archname = getenv("DPKG_MAINTSCRIPT_ARCH"); |
| 124 |
if (pkgname == NULL || archname == NULL) |
| 125 |
badusage(_("must be called from a maintainer script" |
| 126 |
" (or with a --by-package option)")); |
| 127 |
|
| 128 |
pkg = pkg_spec_find_pkg(pkgname, archname, &err); |
| 129 |
} else if (strcmp(opt_bypackage, "-") == 0) { |
| 130 |
pkg = NULL; |
| 131 |
} else { |
| 132 |
pkg = pkg_spec_parse_pkg(opt_bypackage, &err); |
| 133 |
} |
| 134 |
|
| 135 |
/* Normalize the bypackage name if there was no error. */ |
| 136 |
if (pkg) |
| 137 |
opt_bypackage = pkg_name(pkg, pnaw_same); |
| 138 |
|
| 139 |
return err.str; |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
tdm_add_trig_begin(const char *trig) |
| 144 |
{ |
| 145 |
ctrig = strcmp(trig, activate) == 0; |
| 146 |
trigdef_update_printf("%s", trig); |
| 147 |
if (!ctrig || done_trig) |
| 148 |
return; |
| 149 |
|
| 150 |
yespackage(opt_bypackage); |
| 151 |
done_trig = true; |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
tdm_add_package(const char *awname) |
| 156 |
{ |
| 157 |
if (ctrig && strcmp(awname, opt_bypackage) == 0) |
| 158 |
return; |
| 159 |
|
| 160 |
yespackage(awname); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
tdm_add_trig_end(void) |
| 165 |
{ |
| 166 |
trigdef_update_printf("\n"); |
| 167 |
} |
| 168 |
|
| 169 |
static const struct trigdefmeths tdm_add = { |
| 170 |
.trig_begin = tdm_add_trig_begin, |
| 171 |
.package = tdm_add_package, |
| 172 |
.trig_end = tdm_add_trig_end, |
| 173 |
}; |
| 174 |
|
| 175 |
static int |
| 176 |
do_trigger(const char *const *argv) |
| 177 |
{ |
| 178 |
const char *badname; |
| 179 |
enum trigdef_update_flags tduf; |
| 180 |
enum trigdef_update_status tdus; |
| 181 |
|
| 182 |
if (!*argv || argv[1]) |
| 183 |
badusage(_("takes one argument, the trigger name")); |
| 184 |
|
| 185 |
badname = parse_awaiter_package(); |
| 186 |
if (badname) |
| 187 |
badusage(_("invalid awaited package name '%s': %s"), |
| 188 |
opt_bypackage, badname); |
| 189 |
|
| 190 |
activate = argv[0]; |
| 191 |
badname = trig_name_is_invalid(activate); |
| 192 |
if (badname) |
| 193 |
badusage(_("invalid trigger name '%s': %s"), |
| 194 |
activate, badname); |
| 195 |
|
| 196 |
trigdef_set_methods(&tdm_add); |
| 197 |
|
| 198 |
tduf = TDUF_NO_LOCK_OK; |
| 199 |
if (opt_act) |
| 200 |
tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY; |
| 201 |
tdus = trigdef_update_start(tduf); |
| 202 |
if (tdus >= 0) { |
| 203 |
trigdef_parse(); |
| 204 |
if (!done_trig) |
| 205 |
trigdef_update_printf("%s %s\n", activate, opt_bypackage); |
| 206 |
trigdef_process_done(); |
| 207 |
} |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static int |
| 213 |
do_check(const char *const *argv) |
| 214 |
{ |
| 215 |
enum trigdef_update_status uf; |
| 216 |
|
| 217 |
if (*argv) |
| 218 |
badusage(_("--%s takes no arguments"), "check-supported"); |
| 219 |
|
| 220 |
uf = trigdef_update_start(TDUF_NO_LOCK_OK); |
| 221 |
switch (uf) { |
| 222 |
case TDUS_ERROR_NO_DIR: |
| 223 |
notice(_("triggers data directory not yet created")); |
| 224 |
return 1; |
| 225 |
case TDUS_ERROR_NO_DEFERRED: |
| 226 |
notice(_("trigger records not yet in existence")); |
| 227 |
return 1; |
| 228 |
case TDUS_OK: |
| 229 |
case TDUS_ERROR_EMPTY_DEFERRED: |
| 230 |
return 0; |
| 231 |
default: |
| 232 |
internerr("unknown trigdef_update_start return value '%d'", uf); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
static const struct cmdinfo cmdinfo_trigger = |
| 237 |
ACTION("trigger", 0, 0, do_trigger); |
| 238 |
|
| 239 |
static const struct cmdinfo cmdinfos[] = { |
| 240 |
ACTION("check-supported", 0, 0, do_check), |
| 241 |
ACTION("help", '?', 0, usage), |
| 242 |
ACTION("version", 0, 0, printversion), |
| 243 |
|
| 244 |
{ "admindir", 0, 1, NULL, NULL, set_admindir, 0 }, |
| 245 |
{ "root", 0, 1, NULL, NULL, set_root, 0 }, |
| 246 |
{ "by-package", 'f', 1, NULL, &opt_bypackage }, |
| 247 |
{ "await", 0, 0, &opt_await, NULL, NULL, 1 }, |
| 248 |
{ "no-await", 0, 0, &opt_await, NULL, NULL, 0 }, |
| 249 |
{ "no-act", 0, 0, &opt_act, NULL, NULL, 0 }, |
| 250 |
{ NULL } |
| 251 |
}; |
| 252 |
|
| 253 |
int |
| 254 |
main(int argc, const char *const *argv) |
| 255 |
{ |
| 256 |
int ret; |
| 257 |
|
| 258 |
dpkg_locales_init(PACKAGE); |
| 259 |
dpkg_program_init("dpkg-trigger"); |
| 260 |
dpkg_options_parse(&argv, cmdinfos, printforhelp); |
| 261 |
|
| 262 |
debug(dbg_general, "root=%s admindir=%s", |
| 263 |
dpkg_fsys_get_dir(), dpkg_db_get_dir()); |
| 264 |
|
| 265 |
if (!cipaction) |
| 266 |
setaction(&cmdinfo_trigger, NULL); |
| 267 |
|
| 268 |
ret = cipaction->action(argv); |
| 269 |
|
| 270 |
dpkg_program_done(); |
| 271 |
dpkg_locales_done(); |
| 272 |
|
| 273 |
return ret; |
| 274 |
} |