main.c 28 KB

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