main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * dpkg - main program for package management
  3. * main.c - main program
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-2010 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2010 Canonical Ltd.
  8. * written by Martin Pitt <martin.pitt@canonical.com>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <limits.h>
  30. #if HAVE_LOCALE_H
  31. #include <locale.h>
  32. #endif
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #include <stdbool.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <dpkg/macros.h>
  42. #include <dpkg/i18n.h>
  43. #include <dpkg/dpkg.h>
  44. #include <dpkg/dpkg-db.h>
  45. #include <dpkg/subproc.h>
  46. #include <dpkg/command.h>
  47. #include <dpkg/myopt.h>
  48. #include "main.h"
  49. #include "filesdb.h"
  50. #include "filters.h"
  51. static void DPKG_ATTR_NORET
  52. printversion(const struct cmdinfo *ci, const char *value)
  53. {
  54. printf(_("Debian `%s' package management program version %s.\n"),
  55. DPKG, DPKG_VERSION_ARCH);
  56. printf(_(
  57. "This is free software; see the GNU General Public License version 2 or\n"
  58. "later for copying conditions. There is NO warranty.\n"));
  59. m_output(stdout, _("<standard output>"));
  60. exit(0);
  61. }
  62. /*
  63. * FIXME: Options that need fixing:
  64. * dpkg --yet-to-unpack
  65. * dpkg --command-fd
  66. */
  67. static void DPKG_ATTR_NORET
  68. usage(const struct cmdinfo *ci, const char *value)
  69. {
  70. printf(_(
  71. "Usage: %s [<option> ...] <command>\n"
  72. "\n"), DPKG);
  73. printf(_(
  74. "Commands:\n"
  75. " -i|--install <.deb file name> ... | -R|--recursive <directory> ...\n"
  76. " --unpack <.deb file name> ... | -R|--recursive <directory> ...\n"
  77. " -A|--record-avail <.deb file name> ... | -R|--recursive <directory> ...\n"
  78. " --configure <package> ... | -a|--pending\n"
  79. " --triggers-only <package> ... | -a|--pending\n"
  80. " -r|--remove <package> ... | -a|--pending\n"
  81. " -P|--purge <package> ... | -a|--pending\n"
  82. " --get-selections [<pattern> ...] Get list of selections to stdout.\n"
  83. " --set-selections Set package selections from stdin.\n"
  84. " --clear-selections Deselect every non-essential package.\n"
  85. " --update-avail <Packages-file> Replace available packages info.\n"
  86. " --merge-avail <Packages-file> Merge with info from file.\n"
  87. " --clear-avail Erase existing available info.\n"
  88. " --forget-old-unavail Forget uninstalled unavailable pkgs.\n"
  89. " -s|--status <package> ... Display package status details.\n"
  90. " -p|--print-avail <package> ... Display available version details.\n"
  91. " -L|--listfiles <package> ... List files `owned' by package(s).\n"
  92. " -l|--list [<pattern> ...] List packages concisely.\n"
  93. " -S|--search <pattern> ... Find package(s) owning file(s).\n"
  94. " -C|--audit Check for broken package(s).\n"
  95. " --print-architecture Print dpkg architecture.\n"
  96. " --compare-versions <a> <op> <b> Compare version numbers - see below.\n"
  97. " --force-help Show help on forcing.\n"
  98. " -Dh|--debug=help Show help on debugging.\n"
  99. "\n"));
  100. printf(_(
  101. " -h|--help Show this help message.\n"
  102. " --version Show the version.\n"
  103. "\n"));
  104. printf(_(
  105. "Use dpkg -b|--build|-c|--contents|-e|--control|-I|--info|-f|--field|\n"
  106. " -x|--extract|-X|--vextract|--fsys-tarfile on archives (type %s --help).\n"
  107. "\n"), BACKEND);
  108. printf(_(
  109. "For internal use: dpkg --assert-support-predepends | --predep-package |\n"
  110. " --assert-working-epoch | --assert-long-filenames | --assert-multi-conrep.\n"
  111. "\n"));
  112. printf(_(
  113. "Options:\n"
  114. " --admindir=<directory> Use <directory> instead of %s.\n"
  115. " --root=<directory> Install on a different root directory.\n"
  116. " --instdir=<directory> Change installation dir without changing admin dir.\n"
  117. " --path-exclude=<pattern> Do not install paths which match a shell pattern.\n"
  118. " --path-include=<pattern> Re-include a pattern after a previous exclusion.\n"
  119. " -O|--selected-only Skip packages not selected for install/upgrade.\n"
  120. " -E|--skip-same-version Skip packages whose same version is installed.\n"
  121. " -G|--refuse-downgrade Skip packages with earlier version than installed.\n"
  122. " -B|--auto-deconfigure Install even if it would break some other package.\n"
  123. " --[no-]triggers Skip or force consequential trigger processing.\n"
  124. " --no-debsig Do not try to verify package signatures.\n"
  125. " --no-act|--dry-run|--simulate\n"
  126. " Just say what we would do - don't do it.\n"
  127. " -D|--debug=<octal> Enable debugging (see -Dhelp or --debug=help).\n"
  128. " --status-fd <n> Send status change updates to file descriptor <n>.\n"
  129. " --log=<filename> Log status changes and actions to <filename>.\n"
  130. " --ignore-depends=<package>,...\n"
  131. " Ignore dependencies involving <package>.\n"
  132. " --force-... Override problems (see --force-help).\n"
  133. " --no-force-...|--refuse-...\n"
  134. " Stop when problems encountered.\n"
  135. " --abort-after <n> Abort after encountering <n> errors.\n"
  136. "\n"), ADMINDIR);
  137. printf(_(
  138. "Comparison operators for --compare-versions are:\n"
  139. " lt le eq ne ge gt (treat empty version as earlier than any version);\n"
  140. " lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);\n"
  141. " < << <= = >= >> > (only for compatibility with control file syntax).\n"
  142. "\n"));
  143. printf(_(
  144. "Use `dselect' or `aptitude' for user-friendly package management.\n"));
  145. m_output(stdout, _("<standard output>"));
  146. exit(0);
  147. }
  148. const char thisname[]= "dpkg";
  149. const char native_arch[] = ARCHITECTURE;
  150. const char printforhelp[]= N_(
  151. "Type dpkg --help for help about installing and deinstalling packages [*];\n"
  152. "Use `dselect' or `aptitude' for user-friendly package management;\n"
  153. "Type dpkg -Dhelp for a list of dpkg debug flag values;\n"
  154. "Type dpkg --force-help for a list of forcing options;\n"
  155. "Type dpkg-deb --help for help about manipulating *.deb files;\n"
  156. "\n"
  157. "Options marked [*] produce a lot of output - pipe it through `less' or `more' !");
  158. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  159. int f_autodeconf=0, f_nodebsig=0;
  160. int f_triggers = 0;
  161. int fc_downgrade=1, fc_configureany=0, fc_hold=0, fc_removereinstreq=0, fc_overwrite=0;
  162. int fc_removeessential=0, fc_conflicts=0, fc_depends=0, fc_dependsversion=0;
  163. int fc_breaks=0, fc_badpath=0, fc_overwritediverted=0, fc_architecture=0;
  164. int fc_nonroot=0, fc_overwritedir=0, fc_conff_new=0, fc_conff_miss=0;
  165. int fc_conff_old=0, fc_conff_def=0;
  166. int fc_conff_ask = 0;
  167. int fc_unsafe_io = 0;
  168. int fc_badverify = 0;
  169. int errabort = 50;
  170. const char *admindir= ADMINDIR;
  171. const char *instdir= "";
  172. struct pkg_list *ignoredependss = NULL;
  173. static const char *
  174. forcetype_str(char type)
  175. {
  176. switch (type) {
  177. case '\0':
  178. case ' ':
  179. return " ";
  180. case '*':
  181. return "[*]";
  182. case '!':
  183. return "[!]";
  184. default:
  185. internerr("unknown force type %c", type);
  186. }
  187. }
  188. static const struct forceinfo {
  189. const char *name;
  190. int *opt;
  191. char type;
  192. const char *desc;
  193. } forceinfos[]= {
  194. { "all", NULL,
  195. '!', N_("Set all force options")},
  196. { "downgrade", &fc_downgrade,
  197. '*', N_("Replace a package with a lower version") },
  198. { "configure-any", &fc_configureany,
  199. ' ', N_("Configure any package which may help this one") },
  200. { "hold", &fc_hold,
  201. ' ', N_("Process incidental packages even when on hold") },
  202. { "not-root", &fc_nonroot,
  203. ' ', N_("Try to (de)install things even when not root") },
  204. { "bad-path", &fc_badpath,
  205. ' ', N_("PATH is missing important programs, problems likely") },
  206. { "bad-verify", &fc_badverify,
  207. ' ', N_("Install a package even if it fails authenticity check") },
  208. { "overwrite", &fc_overwrite,
  209. ' ', N_("Overwrite a file from one package with another") },
  210. { "overwrite-diverted", &fc_overwritediverted,
  211. ' ', N_("Overwrite a diverted file with an undiverted version") },
  212. { "overwrite-dir", &fc_overwritedir,
  213. '!', N_("Overwrite one package's directory with another's file") },
  214. { "unsafe-io", &fc_unsafe_io,
  215. '!', N_("Do not perform safe I/O operations when unpacking") },
  216. { "confnew", &fc_conff_new,
  217. '!', N_("Always use the new config files, don't prompt") },
  218. { "confold", &fc_conff_old,
  219. '!', N_("Always use the old config files, don't prompt") },
  220. /* XXX: Handle automatically line wrappings. */
  221. { "confdef", &fc_conff_def,
  222. '!', N_("Use the default option for new config files if one\n"
  223. " is available, don't prompt. If no default can be found,\n"
  224. " you will be prompted unless one of the confold or\n"
  225. " confnew options is also given") },
  226. { "confmiss", &fc_conff_miss,
  227. '!', N_("Always install missing config files") },
  228. { "confask", &fc_conff_ask,
  229. '!', N_("Offer to replace config files with no new versions") },
  230. { "architecture", &fc_architecture,
  231. '!', N_("Process even packages with wrong or no architecture") },
  232. { "breaks", &fc_breaks,
  233. '!', N_("Install even if it would break another package") },
  234. { "conflicts", &fc_conflicts,
  235. '!', N_("Allow installation of conflicting packages") },
  236. { "depends", &fc_depends,
  237. '!', N_("Turn all dependency problems into warnings") },
  238. { "depends-version", &fc_dependsversion,
  239. '!', N_("Turn dependency version problems into warnings") },
  240. { "remove-reinstreq", &fc_removereinstreq,
  241. '!', N_("Remove packages which require installation") },
  242. { "remove-essential", &fc_removeessential,
  243. '!', N_("Remove an essential package") },
  244. { NULL }
  245. };
  246. #define DBG_DEF(n, d) \
  247. { .flag = dbg_##n, .name = #n, .desc = d }
  248. static const struct debuginfo {
  249. int flag;
  250. const char *name;
  251. const char *desc;
  252. } debuginfos[] = {
  253. DBG_DEF(general, N_("Generally helpful progress information")),
  254. DBG_DEF(scripts, N_("Invocation and status of maintainer scripts")),
  255. DBG_DEF(eachfile, N_("Output for each file processed")),
  256. DBG_DEF(eachfiledetail, N_("Lots of output for each file processed")),
  257. DBG_DEF(conff, N_("Output for each configuration file")),
  258. DBG_DEF(conffdetail, N_("Lots of output for each configuration file")),
  259. DBG_DEF(depcon, N_("Dependencies and conflicts")),
  260. DBG_DEF(depcondetail, N_("Lots of dependencies/conflicts output")),
  261. DBG_DEF(triggers, N_("Trigger activation and processing")),
  262. DBG_DEF(triggersdetail, N_("Lots of output regarding triggers")),
  263. DBG_DEF(triggersstupid, N_("Silly amounts of output regarding triggers")),
  264. DBG_DEF(veryverbose, N_("Lots of drivel about eg the dpkg/info directory")),
  265. DBG_DEF(stupidlyverbose, N_("Insane amounts of drivel")),
  266. { 0, NULL, NULL }
  267. };
  268. static void setdebug(const struct cmdinfo *cpi, const char *value) {
  269. char *endp;
  270. unsigned long mask;
  271. const struct debuginfo *dip;
  272. if (*value == 'h') {
  273. printf(_(
  274. "%s debugging option, --debug=<octal> or -D<octal>:\n"
  275. "\n"
  276. " Number Ref. in source Description\n"), DPKG);
  277. for (dip = debuginfos; dip->name; dip++)
  278. printf(" %6o %-16s %s\n", dip->flag, dip->name, gettext(dip->desc));
  279. printf(_("\n"
  280. "Debugging options can be mixed using bitwise-or.\n"
  281. "Note that the meanings and values are subject to change.\n"));
  282. m_output(stdout, _("<standard output>"));
  283. exit(0);
  284. }
  285. mask = strtoul(value, &endp, 8);
  286. if (value == endp || *endp) badusage(_("--debug requires an octal argument"));
  287. debug_set_mask(mask);
  288. }
  289. static void
  290. setfilter(const struct cmdinfo *cip, const char *value)
  291. {
  292. filter_add(value, cip->arg_int);
  293. }
  294. static void setroot(const struct cmdinfo *cip, const char *value) {
  295. char *p;
  296. instdir= value;
  297. m_asprintf(&p, "%s%s", value, ADMINDIR);
  298. admindir= p;
  299. }
  300. static void ignoredepends(const struct cmdinfo *cip, const char *value) {
  301. char *copy, *p;
  302. const char *pnerr;
  303. copy= m_malloc(strlen(value)+2);
  304. strcpy(copy,value);
  305. copy[strlen(value) + 1] = '\0';
  306. for (p=copy; *p; p++) {
  307. if (*p != ',') continue;
  308. *p++ = '\0';
  309. if (!*p || *p==',' || p==copy+1)
  310. badusage(_("null package name in --ignore-depends comma-separated list `%.250s'"),
  311. value);
  312. }
  313. p= copy;
  314. while (*p) {
  315. pnerr = pkg_name_is_illegal(p, NULL);
  316. if (pnerr) ohshite(_("--ignore-depends requires a legal package name. "
  317. "`%.250s' is not; %s"), p, pnerr);
  318. pkg_list_prepend(&ignoredependss, pkg_db_find(p));
  319. p+= strlen(p)+1;
  320. }
  321. free(copy);
  322. }
  323. static void setinteger(const struct cmdinfo *cip, const char *value) {
  324. unsigned long v;
  325. char *ep;
  326. v= strtoul(value,&ep,0);
  327. if (value == ep || *ep || v > INT_MAX)
  328. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  329. *cip->iassignto= v;
  330. }
  331. static void setpipe(const struct cmdinfo *cip, const char *value) {
  332. unsigned long v;
  333. char *ep;
  334. v= strtoul(value,&ep,0);
  335. if (value == ep || *ep || v > INT_MAX)
  336. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  337. statusfd_add(v);
  338. }
  339. static bool
  340. is_invoke_action(enum action action)
  341. {
  342. switch (action) {
  343. case act_unpack:
  344. case act_configure:
  345. case act_install:
  346. case act_triggers:
  347. case act_remove:
  348. case act_purge:
  349. return true;
  350. default:
  351. return false;
  352. }
  353. }
  354. struct invoke_hook *pre_invoke_hooks = NULL;
  355. struct invoke_hook **pre_invoke_hooks_tail = &pre_invoke_hooks;
  356. struct invoke_hook *post_invoke_hooks = NULL;
  357. struct invoke_hook **post_invoke_hooks_tail = &post_invoke_hooks;
  358. struct invoke_hook *status_loggers = NULL;
  359. struct invoke_hook **status_loggers_tail = &status_loggers;
  360. static void
  361. set_invoke_hook(const struct cmdinfo *cip, const char *value)
  362. {
  363. struct invoke_hook ***hook_tail = cip->arg_ptr;
  364. struct invoke_hook *hook_new;
  365. hook_new = nfmalloc(sizeof(struct invoke_hook));
  366. hook_new->command = nfstrsave(value);
  367. hook_new->next = NULL;
  368. /* Add the new hook at the tail of the list to preserve the order. */
  369. **hook_tail = hook_new;
  370. *hook_tail = &hook_new->next;
  371. }
  372. static void
  373. run_invoke_hooks(const char *action, struct invoke_hook *hook_head)
  374. {
  375. struct invoke_hook *hook;
  376. setenv("DPKG_HOOK_ACTION", action, 1);
  377. for (hook = hook_head; hook; hook = hook->next) {
  378. int status;
  379. /* XXX: As an optimization, use exec instead if no shell metachar are
  380. * used “!$=&|\\`'"^~;<>{}[]()?*#”. */
  381. status = system(hook->command);
  382. if (status != 0)
  383. ohshit(_("error executing hook '%s', exit code %d"), hook->command,
  384. status);
  385. }
  386. unsetenv("DPKG_HOOK_ACTION");
  387. }
  388. static void
  389. run_status_loggers(struct invoke_hook *hook_head)
  390. {
  391. struct invoke_hook *hook;
  392. for (hook = hook_head; hook; hook = hook->next) {
  393. pid_t pid;
  394. int p[2];
  395. m_pipe(p);
  396. pid = subproc_fork();
  397. if (pid == 0) {
  398. /* Setup stdin and stdout. */
  399. m_dup2(p[0], 0);
  400. close(1);
  401. close(p[0]);
  402. close(p[1]);
  403. command_shell(hook->command, _("status logger"));
  404. }
  405. close(p[0]);
  406. statusfd_add(p[1]);
  407. }
  408. }
  409. static void setforce(const struct cmdinfo *cip, const char *value) {
  410. const char *comma;
  411. size_t l;
  412. const struct forceinfo *fip;
  413. if (!strcmp(value,"help")) {
  414. printf(_(
  415. "%s forcing options - control behaviour when problems found:\n"
  416. " warn but continue: --force-<thing>,<thing>,...\n"
  417. " stop with error: --refuse-<thing>,<thing>,... | --no-force-<thing>,...\n"
  418. " Forcing things:\n"), DPKG);
  419. for (fip = forceinfos; fip->name; fip++)
  420. printf(" %s %-18s %s\n", forcetype_str(fip->type), fip->name, fip->desc);
  421. printf(_(
  422. "\n"
  423. "WARNING - use of options marked [!] can seriously damage your installation.\n"
  424. "Forcing options marked [*] are enabled by default.\n"));
  425. m_output(stdout, _("<standard output>"));
  426. exit(0);
  427. }
  428. for (;;) {
  429. comma= strchr(value,',');
  430. l = comma ? (size_t)(comma - value) : strlen(value);
  431. for (fip=forceinfos; fip->name; fip++)
  432. if (!strncmp(fip->name,value,l) && strlen(fip->name)==l) break;
  433. if (!fip->name) {
  434. badusage(_("unknown force/refuse option `%.*s'"),
  435. (int)min(l, 250), value);
  436. } else if (strcmp(fip->name, "all") == 0) {
  437. for (fip = forceinfos; fip->name; fip++)
  438. if (fip->opt)
  439. *fip->opt = cip->arg_int;
  440. } else if (fip->opt) {
  441. *fip->opt = cip->arg_int;
  442. } else {
  443. warning(_("obsolete force/refuse option '%s'\n"), fip->name);
  444. }
  445. if (!comma) break;
  446. value= ++comma;
  447. }
  448. }
  449. void execbackend(const char *const *argv) DPKG_ATTR_NORET;
  450. void commandfd(const char *const *argv);
  451. /* This table has both the action entries in it and the normal options.
  452. * The action entries are made with the ACTION macro, as they all
  453. * have a very similar structure. */
  454. static const struct cmdinfo cmdinfos[]= {
  455. #define ACTIONBACKEND(longopt, shortopt, backend) \
  456. { longopt, shortopt, 0, NULL, NULL, setaction, 0, (void *)backend, (void_func *)execbackend }
  457. ACTION( "install", 'i', act_install, archivefiles ),
  458. ACTION( "unpack", 0, act_unpack, archivefiles ),
  459. ACTION( "record-avail", 'A', act_avail, archivefiles ),
  460. ACTION( "configure", 0, act_configure, packages ),
  461. ACTION( "remove", 'r', act_remove, packages ),
  462. ACTION( "purge", 'P', act_purge, packages ),
  463. ACTION( "triggers-only", 0, act_triggers, packages ),
  464. ACTIONBACKEND( "listfiles", 'L', DPKGQUERY),
  465. ACTIONBACKEND( "status", 's', DPKGQUERY),
  466. ACTION( "get-selections", 0, act_getselections, getselections ),
  467. ACTION( "set-selections", 0, act_setselections, setselections ),
  468. ACTION( "clear-selections", 0, act_clearselections, clearselections ),
  469. ACTIONBACKEND( "print-avail", 'p', DPKGQUERY),
  470. ACTION( "update-avail", 0, act_avreplace, updateavailable ),
  471. ACTION( "merge-avail", 0, act_avmerge, updateavailable ),
  472. ACTION( "clear-avail", 0, act_avclear, updateavailable ),
  473. ACTION( "forget-old-unavail", 0, act_forgetold, forgetold ),
  474. ACTION( "audit", 'C', act_audit, audit ),
  475. ACTION( "yet-to-unpack", 0, act_unpackchk, unpackchk ),
  476. ACTIONBACKEND( "list", 'l', DPKGQUERY),
  477. ACTIONBACKEND( "search", 'S', DPKGQUERY),
  478. ACTION( "assert-support-predepends", 0, act_assertpredep, assertpredep ),
  479. ACTION( "assert-working-epoch", 0, act_assertepoch, assertepoch ),
  480. ACTION( "assert-long-filenames", 0, act_assertlongfilenames, assertlongfilenames ),
  481. ACTION( "assert-multi-conrep", 0, act_assertmulticonrep, assertmulticonrep ),
  482. ACTION( "print-architecture", 0, act_printarch, printarch ),
  483. ACTION( "print-installation-architecture", 0, act_printinstarch, printinstarch ),
  484. ACTION( "predep-package", 0, act_predeppackage, predeppackage ),
  485. ACTION( "compare-versions", 0, act_cmpversions, cmpversions ),
  486. /*
  487. ACTION( "command-fd", 'c', act_commandfd, commandfd ),
  488. */
  489. { "pre-invoke", 0, 1, NULL, NULL, set_invoke_hook, 0, &pre_invoke_hooks_tail },
  490. { "post-invoke", 0, 1, NULL, NULL, set_invoke_hook, 0, &post_invoke_hooks_tail },
  491. { "path-exclude", 0, 1, NULL, NULL, setfilter, 0 },
  492. { "path-include", 0, 1, NULL, NULL, setfilter, 1 },
  493. { "status-logger", 0, 1, NULL, NULL, set_invoke_hook, 0, &status_loggers_tail },
  494. { "status-fd", 0, 1, NULL, NULL, setpipe, 0 },
  495. { "log", 0, 1, NULL, &log_file, NULL, 0 },
  496. { "pending", 'a', 0, &f_pending, NULL, NULL, 1 },
  497. { "recursive", 'R', 0, &f_recursive, NULL, NULL, 1 },
  498. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  499. { "dry-run", 0, 0, &f_noact, NULL, NULL, 1 },
  500. { "simulate", 0, 0, &f_noact, NULL, NULL, 1 },
  501. { "no-debsig", 0, 0, &f_nodebsig, NULL, NULL, 1 },
  502. /* Alias ('G') for --refuse. */
  503. { NULL, 'G', 0, &fc_downgrade, NULL, NULL, 0 },
  504. { "selected-only", 'O', 0, &f_alsoselect, NULL, NULL, 0 },
  505. { "triggers", 0, 0, &f_triggers, NULL, NULL, 1 },
  506. { "no-triggers", 0, 0, &f_triggers, NULL, NULL, -1 },
  507. /* FIXME: Remove ('N') sometime. */
  508. { "no-also-select", 'N', 0, &f_alsoselect, NULL, NULL, 0 },
  509. { "skip-same-version", 'E', 0, &f_skipsame, NULL, NULL, 1 },
  510. { "auto-deconfigure", 'B', 0, &f_autodeconf, NULL, NULL, 1 },
  511. { "root", 0, 1, NULL, NULL, setroot, 0 },
  512. { "abort-after", 0, 1, &errabort, NULL, setinteger, 0 },
  513. { "admindir", 0, 1, NULL, &admindir, NULL, 0 },
  514. { "instdir", 0, 1, NULL, &instdir, NULL, 0 },
  515. { "ignore-depends", 0, 1, NULL, NULL, ignoredepends, 0 },
  516. { "force", 0, 2, NULL, NULL, setforce, 1 },
  517. { "refuse", 0, 2, NULL, NULL, setforce, 0 },
  518. { "no-force", 0, 2, NULL, NULL, setforce, 0 },
  519. { "debug", 'D', 1, NULL, NULL, setdebug, 0 },
  520. { "help", 'h', 0, NULL, NULL, usage, 0 },
  521. { "version", 0, 0, NULL, NULL, printversion, 0 },
  522. ACTIONBACKEND( "build", 'b', BACKEND),
  523. ACTIONBACKEND( "contents", 'c', BACKEND),
  524. ACTIONBACKEND( "control", 'e', BACKEND),
  525. ACTIONBACKEND( "info", 'I', BACKEND),
  526. ACTIONBACKEND( "field", 'f', BACKEND),
  527. ACTIONBACKEND( "extract", 'x', BACKEND),
  528. ACTIONBACKEND( "new", 0, BACKEND),
  529. ACTIONBACKEND( "old", 0, BACKEND),
  530. ACTIONBACKEND( "vextract", 'X', BACKEND),
  531. ACTIONBACKEND( "fsys-tarfile", 0, BACKEND),
  532. { NULL, 0, 0, NULL, NULL, NULL, 0 }
  533. };
  534. void execbackend(const char *const *argv) {
  535. struct command cmd;
  536. char *arg;
  537. command_init(&cmd, cipaction->arg_ptr, NULL);
  538. command_add_arg(&cmd, cipaction->arg_ptr);
  539. m_asprintf(&arg, "--%s", cipaction->olong);
  540. command_add_arg(&cmd, arg);
  541. /* Exlicitely separate arguments from options as any user-supplied
  542. * separator got stripped by the option parser */
  543. command_add_arg(&cmd, "--");
  544. command_add_argl(&cmd, (const char **)argv);
  545. command_exec(&cmd);
  546. }
  547. void commandfd(const char *const *argv) {
  548. struct varbuf linevb = VARBUF_INIT;
  549. const char * pipein;
  550. const char **newargs = NULL;
  551. char *ptr, *endptr;
  552. FILE *in;
  553. unsigned long infd;
  554. int c, lno, i;
  555. bool skipchar;
  556. void (*actionfunction)(const char *const *argv);
  557. pipein = *argv++;
  558. if (pipein == NULL)
  559. badusage(_("--command-fd takes one argument, not zero"));
  560. if (*argv)
  561. badusage(_("--command-fd only takes one argument"));
  562. errno = 0;
  563. infd = strtoul(pipein, &endptr, 10);
  564. if (pipein == endptr || *endptr || infd > INT_MAX)
  565. ohshite(_("invalid integer for --%s: `%.250s'"), "command-fd", pipein);
  566. if ((in= fdopen(infd, "r")) == NULL)
  567. ohshite(_("couldn't open `%i' for stream"), (int) infd);
  568. for (;;) {
  569. bool mode = false;
  570. int argc= 1;
  571. lno= 0;
  572. push_error_context();
  573. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  574. if (c == EOF) break;
  575. if (c == '#') {
  576. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  577. continue;
  578. }
  579. varbuf_reset(&linevb);
  580. do {
  581. varbuf_add_char(&linevb, c);
  582. c= getc(in);
  583. if (c == '\n') lno++;
  584. /* This isn't fully accurate, but overestimating can't hurt. */
  585. if (isspace(c))
  586. argc++;
  587. } while (c != EOF && c != '\n');
  588. if (c == EOF) ohshit(_("unexpected eof before end of line %d"),lno);
  589. if (!argc) continue;
  590. varbuf_end_str(&linevb);
  591. newargs = m_realloc(newargs, sizeof(const char *) * (argc + 1));
  592. argc= 1;
  593. ptr= linevb.buf;
  594. endptr = ptr + linevb.used + 1;
  595. skipchar = false;
  596. while(ptr < endptr) {
  597. if (skipchar) {
  598. skipchar = false;
  599. } else if (*ptr == '\\') {
  600. memmove(ptr, (ptr+1), (linevb.used-(linevb.buf - ptr)-1));
  601. endptr--;
  602. skipchar = true;
  603. continue;
  604. } else if (isspace(*ptr)) {
  605. if (mode == true) {
  606. *ptr = '\0';
  607. mode = false;
  608. }
  609. } else {
  610. if (mode == false) {
  611. newargs[argc]= ptr;
  612. argc++;
  613. mode = true;
  614. }
  615. }
  616. ptr++;
  617. }
  618. *ptr = '\0';
  619. newargs[argc++] = NULL;
  620. /* We strdup() each argument, but never free it, because the
  621. * error messages contain references back to these strings.
  622. * Freeing them, and reusing the memory, would make those
  623. * error messages confusing, to say the least. */
  624. for(i=1;i<argc;i++)
  625. if (newargs[i])
  626. newargs[i] = m_strdup(newargs[i]);
  627. setaction(NULL, NULL);
  628. myopt((const char *const**)&newargs,cmdinfos);
  629. if (!cipaction) badusage(_("need an action option"));
  630. actionfunction = (void (*)(const char *const *))cipaction->arg_func;
  631. actionfunction(newargs);
  632. pop_error_context(ehflag_normaltidy);
  633. }
  634. }
  635. int main(int argc, const char *const *argv) {
  636. void (*actionfunction)(const char *const *argv);
  637. setlocale(LC_ALL, "");
  638. bindtextdomain(PACKAGE, LOCALEDIR);
  639. textdomain(PACKAGE);
  640. standard_startup();
  641. loadcfgfile(DPKG, cmdinfos);
  642. myopt(&argv, cmdinfos);
  643. if (!cipaction) badusage(_("need an action option"));
  644. /* Always set environment, to avoid possible security risks. */
  645. if (setenv("DPKG_ADMINDIR", admindir, 1) < 0)
  646. ohshite(_("unable to setenv for subprocesses"));
  647. if (!f_triggers)
  648. f_triggers = (cipaction->arg_int == act_triggers && *argv) ? -1 : 1;
  649. setvbuf(stdout, NULL, _IONBF, 0);
  650. if (is_invoke_action(cipaction->arg_int)) {
  651. run_invoke_hooks(cipaction->olong, pre_invoke_hooks);
  652. run_status_loggers(status_loggers);
  653. }
  654. filesdbinit();
  655. actionfunction = (void (*)(const char *const *))cipaction->arg_func;
  656. actionfunction(argv);
  657. if (is_invoke_action(cipaction->arg_int))
  658. run_invoke_hooks(cipaction->olong, post_invoke_hooks);
  659. standard_shutdown();
  660. return reportbroken_retexitstatus();
  661. }