main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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/options.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 native_arch[] = ARCHITECTURE;
  149. static const char printforhelp[] = N_(
  150. "Type dpkg --help for help about installing and deinstalling packages [*];\n"
  151. "Use `dselect' or `aptitude' for user-friendly package management;\n"
  152. "Type dpkg -Dhelp for a list of dpkg debug flag values;\n"
  153. "Type dpkg --force-help for a list of forcing options;\n"
  154. "Type dpkg-deb --help for help about manipulating *.deb files;\n"
  155. "\n"
  156. "Options marked [*] produce a lot of output - pipe it through `less' or `more' !");
  157. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  158. int f_autodeconf=0, f_nodebsig=0;
  159. int f_triggers = 0;
  160. int fc_downgrade=1, fc_configureany=0, fc_hold=0, fc_removereinstreq=0, fc_overwrite=0;
  161. int fc_removeessential=0, fc_conflicts=0, fc_depends=0, fc_dependsversion=0;
  162. int fc_breaks=0, fc_badpath=0, fc_overwritediverted=0, fc_architecture=0;
  163. int fc_nonroot=0, fc_overwritedir=0, fc_conff_new=0, fc_conff_miss=0;
  164. int fc_conff_old=0, fc_conff_def=0;
  165. int fc_conff_ask = 0;
  166. int fc_unsafe_io = 0;
  167. int fc_badverify = 0;
  168. int fc_badversion = 0;
  169. int errabort = 50;
  170. static 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. { "bad-version", &fc_badversion,
  209. ' ', N_("Process even packages with wrong versions") },
  210. { "overwrite", &fc_overwrite,
  211. ' ', N_("Overwrite a file from one package with another") },
  212. { "overwrite-diverted", &fc_overwritediverted,
  213. ' ', N_("Overwrite a diverted file with an undiverted version") },
  214. { "overwrite-dir", &fc_overwritedir,
  215. '!', N_("Overwrite one package's directory with another's file") },
  216. { "unsafe-io", &fc_unsafe_io,
  217. '!', N_("Do not perform safe I/O operations when unpacking") },
  218. { "confnew", &fc_conff_new,
  219. '!', N_("Always use the new config files, don't prompt") },
  220. { "confold", &fc_conff_old,
  221. '!', N_("Always use the old config files, don't prompt") },
  222. /* XXX: Handle automatically line wrappings. */
  223. { "confdef", &fc_conff_def,
  224. '!', N_("Use the default option for new config files if one\n"
  225. " is available, don't prompt. If no default can be found,\n"
  226. " you will be prompted unless one of the confold or\n"
  227. " confnew options is also given") },
  228. { "confmiss", &fc_conff_miss,
  229. '!', N_("Always install missing config files") },
  230. { "confask", &fc_conff_ask,
  231. '!', N_("Offer to replace config files with no new versions") },
  232. { "architecture", &fc_architecture,
  233. '!', N_("Process even packages with wrong or no architecture") },
  234. { "breaks", &fc_breaks,
  235. '!', N_("Install even if it would break another package") },
  236. { "conflicts", &fc_conflicts,
  237. '!', N_("Allow installation of conflicting packages") },
  238. { "depends", &fc_depends,
  239. '!', N_("Turn all dependency problems into warnings") },
  240. { "depends-version", &fc_dependsversion,
  241. '!', N_("Turn dependency version problems into warnings") },
  242. { "remove-reinstreq", &fc_removereinstreq,
  243. '!', N_("Remove packages which require installation") },
  244. { "remove-essential", &fc_removeessential,
  245. '!', N_("Remove an essential package") },
  246. { NULL }
  247. };
  248. #define DBG_DEF(n, d) \
  249. { .flag = dbg_##n, .name = #n, .desc = d }
  250. static const struct debuginfo {
  251. int flag;
  252. const char *name;
  253. const char *desc;
  254. } debuginfos[] = {
  255. DBG_DEF(general, N_("Generally helpful progress information")),
  256. DBG_DEF(scripts, N_("Invocation and status of maintainer scripts")),
  257. DBG_DEF(eachfile, N_("Output for each file processed")),
  258. DBG_DEF(eachfiledetail, N_("Lots of output for each file processed")),
  259. DBG_DEF(conff, N_("Output for each configuration file")),
  260. DBG_DEF(conffdetail, N_("Lots of output for each configuration file")),
  261. DBG_DEF(depcon, N_("Dependencies and conflicts")),
  262. DBG_DEF(depcondetail, N_("Lots of dependencies/conflicts output")),
  263. DBG_DEF(triggers, N_("Trigger activation and processing")),
  264. DBG_DEF(triggersdetail, N_("Lots of output regarding triggers")),
  265. DBG_DEF(triggersstupid, N_("Silly amounts of output regarding triggers")),
  266. DBG_DEF(veryverbose, N_("Lots of drivel about eg the dpkg/info directory")),
  267. DBG_DEF(stupidlyverbose, N_("Insane amounts of drivel")),
  268. { 0, NULL, NULL }
  269. };
  270. static void setdebug(const struct cmdinfo *cpi, const char *value) {
  271. char *endp;
  272. unsigned long mask;
  273. const struct debuginfo *dip;
  274. if (*value == 'h') {
  275. printf(_(
  276. "%s debugging option, --debug=<octal> or -D<octal>:\n"
  277. "\n"
  278. " Number Ref. in source Description\n"), DPKG);
  279. for (dip = debuginfos; dip->name; dip++)
  280. printf(" %6o %-16s %s\n", dip->flag, dip->name, gettext(dip->desc));
  281. printf(_("\n"
  282. "Debugging options can be mixed using bitwise-or.\n"
  283. "Note that the meanings and values are subject to change.\n"));
  284. m_output(stdout, _("<standard output>"));
  285. exit(0);
  286. }
  287. mask = strtoul(value, &endp, 8);
  288. if (value == endp || *endp) badusage(_("--debug requires an octal argument"));
  289. debug_set_mask(mask);
  290. }
  291. static void
  292. setfilter(const struct cmdinfo *cip, const char *value)
  293. {
  294. filter_add(value, cip->arg_int);
  295. }
  296. static void setroot(const struct cmdinfo *cip, const char *value) {
  297. char *p;
  298. instdir= value;
  299. m_asprintf(&p, "%s%s", value, ADMINDIR);
  300. admindir= p;
  301. }
  302. static void ignoredepends(const struct cmdinfo *cip, const char *value) {
  303. char *copy, *p;
  304. const char *pnerr;
  305. copy= m_malloc(strlen(value)+2);
  306. strcpy(copy,value);
  307. copy[strlen(value) + 1] = '\0';
  308. for (p=copy; *p; p++) {
  309. if (*p != ',') continue;
  310. *p++ = '\0';
  311. if (!*p || *p==',' || p==copy+1)
  312. badusage(_("null package name in --ignore-depends comma-separated list `%.250s'"),
  313. value);
  314. }
  315. p= copy;
  316. while (*p) {
  317. pnerr = pkg_name_is_illegal(p, NULL);
  318. if (pnerr) ohshite(_("--ignore-depends requires a legal package name. "
  319. "`%.250s' is not; %s"), p, pnerr);
  320. pkg_list_prepend(&ignoredependss, pkg_db_find(p));
  321. p+= strlen(p)+1;
  322. }
  323. free(copy);
  324. }
  325. static void setinteger(const struct cmdinfo *cip, const char *value) {
  326. unsigned long v;
  327. char *ep;
  328. v= strtoul(value,&ep,0);
  329. if (value == ep || *ep || v > INT_MAX)
  330. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  331. *cip->iassignto= v;
  332. }
  333. static void setpipe(const struct cmdinfo *cip, const char *value) {
  334. unsigned long v;
  335. char *ep;
  336. v= strtoul(value,&ep,0);
  337. if (value == ep || *ep || v > INT_MAX)
  338. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  339. statusfd_add(v);
  340. }
  341. static bool
  342. is_invoke_action(enum action action)
  343. {
  344. switch (action) {
  345. case act_unpack:
  346. case act_configure:
  347. case act_install:
  348. case act_triggers:
  349. case act_remove:
  350. case act_purge:
  351. return true;
  352. default:
  353. return false;
  354. }
  355. }
  356. struct invoke_hook *pre_invoke_hooks = NULL;
  357. struct invoke_hook **pre_invoke_hooks_tail = &pre_invoke_hooks;
  358. struct invoke_hook *post_invoke_hooks = NULL;
  359. struct invoke_hook **post_invoke_hooks_tail = &post_invoke_hooks;
  360. struct invoke_hook *status_loggers = NULL;
  361. struct invoke_hook **status_loggers_tail = &status_loggers;
  362. static void
  363. set_invoke_hook(const struct cmdinfo *cip, const char *value)
  364. {
  365. struct invoke_hook ***hook_tail = cip->arg_ptr;
  366. struct invoke_hook *hook_new;
  367. hook_new = nfmalloc(sizeof(struct invoke_hook));
  368. hook_new->command = nfstrsave(value);
  369. hook_new->next = NULL;
  370. /* Add the new hook at the tail of the list to preserve the order. */
  371. **hook_tail = hook_new;
  372. *hook_tail = &hook_new->next;
  373. }
  374. static void
  375. run_invoke_hooks(const char *action, struct invoke_hook *hook_head)
  376. {
  377. struct invoke_hook *hook;
  378. setenv("DPKG_HOOK_ACTION", action, 1);
  379. for (hook = hook_head; hook; hook = hook->next) {
  380. int status;
  381. /* XXX: As an optimization, use exec instead if no shell metachar are
  382. * used “!$=&|\\`'"^~;<>{}[]()?*#”. */
  383. status = system(hook->command);
  384. if (status != 0)
  385. ohshit(_("error executing hook '%s', exit code %d"), hook->command,
  386. status);
  387. }
  388. unsetenv("DPKG_HOOK_ACTION");
  389. }
  390. static void
  391. run_status_loggers(struct invoke_hook *hook_head)
  392. {
  393. struct invoke_hook *hook;
  394. for (hook = hook_head; hook; hook = hook->next) {
  395. pid_t pid;
  396. int p[2];
  397. m_pipe(p);
  398. pid = subproc_fork();
  399. if (pid == 0) {
  400. /* Setup stdin and stdout. */
  401. m_dup2(p[0], 0);
  402. close(1);
  403. close(p[0]);
  404. close(p[1]);
  405. command_shell(hook->command, _("status logger"));
  406. }
  407. close(p[0]);
  408. statusfd_add(p[1]);
  409. }
  410. }
  411. static void setforce(const struct cmdinfo *cip, const char *value) {
  412. const char *comma;
  413. size_t l;
  414. const struct forceinfo *fip;
  415. if (!strcmp(value,"help")) {
  416. printf(_(
  417. "%s forcing options - control behaviour when problems found:\n"
  418. " warn but continue: --force-<thing>,<thing>,...\n"
  419. " stop with error: --refuse-<thing>,<thing>,... | --no-force-<thing>,...\n"
  420. " Forcing things:\n"), DPKG);
  421. for (fip = forceinfos; fip->name; fip++)
  422. printf(" %s %-18s %s\n", forcetype_str(fip->type), fip->name,
  423. gettext(fip->desc));
  424. printf(_(
  425. "\n"
  426. "WARNING - use of options marked [!] can seriously damage your installation.\n"
  427. "Forcing options marked [*] are enabled by default.\n"));
  428. m_output(stdout, _("<standard output>"));
  429. exit(0);
  430. }
  431. for (;;) {
  432. comma= strchr(value,',');
  433. l = comma ? (size_t)(comma - value) : strlen(value);
  434. for (fip=forceinfos; fip->name; fip++)
  435. if (!strncmp(fip->name,value,l) && strlen(fip->name)==l) break;
  436. if (!fip->name) {
  437. badusage(_("unknown force/refuse option `%.*s'"),
  438. (int)min(l, 250), value);
  439. } else if (strcmp(fip->name, "all") == 0) {
  440. for (fip = forceinfos; fip->name; fip++)
  441. if (fip->opt)
  442. *fip->opt = cip->arg_int;
  443. } else if (fip->opt) {
  444. *fip->opt = cip->arg_int;
  445. } else {
  446. warning(_("obsolete force/refuse option '%s'\n"), fip->name);
  447. }
  448. if (!comma) break;
  449. value= ++comma;
  450. }
  451. }
  452. int execbackend(const char *const *argv) DPKG_ATTR_NORET;
  453. int commandfd(const char *const *argv);
  454. /* This table has both the action entries in it and the normal options.
  455. * The action entries are made with the ACTION macro, as they all
  456. * have a very similar structure. */
  457. static const struct cmdinfo cmdinfos[]= {
  458. #define ACTIONBACKEND(longopt, shortopt, backend) \
  459. { longopt, shortopt, 0, NULL, NULL, setaction, 0, (void *)backend, execbackend }
  460. ACTION( "install", 'i', act_install, archivefiles ),
  461. ACTION( "unpack", 0, act_unpack, archivefiles ),
  462. ACTION( "record-avail", 'A', act_avail, archivefiles ),
  463. ACTION( "configure", 0, act_configure, packages ),
  464. ACTION( "remove", 'r', act_remove, packages ),
  465. ACTION( "purge", 'P', act_purge, packages ),
  466. ACTION( "triggers-only", 0, act_triggers, packages ),
  467. ACTIONBACKEND( "listfiles", 'L', DPKGQUERY),
  468. ACTIONBACKEND( "status", 's', DPKGQUERY),
  469. ACTION( "get-selections", 0, act_getselections, getselections ),
  470. ACTION( "set-selections", 0, act_setselections, setselections ),
  471. ACTION( "clear-selections", 0, act_clearselections, clearselections ),
  472. ACTIONBACKEND( "print-avail", 'p', DPKGQUERY),
  473. ACTION( "update-avail", 0, act_avreplace, updateavailable ),
  474. ACTION( "merge-avail", 0, act_avmerge, updateavailable ),
  475. ACTION( "clear-avail", 0, act_avclear, updateavailable ),
  476. ACTION( "forget-old-unavail", 0, act_forgetold, forgetold ),
  477. ACTION( "audit", 'C', act_audit, audit ),
  478. ACTION( "yet-to-unpack", 0, act_unpackchk, unpackchk ),
  479. ACTIONBACKEND( "list", 'l', DPKGQUERY),
  480. ACTIONBACKEND( "search", 'S', DPKGQUERY),
  481. ACTION( "assert-support-predepends", 0, act_assertpredep, assertpredep ),
  482. ACTION( "assert-working-epoch", 0, act_assertepoch, assertepoch ),
  483. ACTION( "assert-long-filenames", 0, act_assertlongfilenames, assertlongfilenames ),
  484. ACTION( "assert-multi-conrep", 0, act_assertmulticonrep, assertmulticonrep ),
  485. ACTION( "print-architecture", 0, act_printarch, printarch ),
  486. ACTION( "print-installation-architecture", 0, act_printinstarch, printinstarch ),
  487. ACTION( "predep-package", 0, act_predeppackage, predeppackage ),
  488. ACTION( "compare-versions", 0, act_cmpversions, cmpversions ),
  489. /*
  490. ACTION( "command-fd", 'c', act_commandfd, commandfd ),
  491. */
  492. { "pre-invoke", 0, 1, NULL, NULL, set_invoke_hook, 0, &pre_invoke_hooks_tail },
  493. { "post-invoke", 0, 1, NULL, NULL, set_invoke_hook, 0, &post_invoke_hooks_tail },
  494. { "path-exclude", 0, 1, NULL, NULL, setfilter, 0 },
  495. { "path-include", 0, 1, NULL, NULL, setfilter, 1 },
  496. { "status-logger", 0, 1, NULL, NULL, set_invoke_hook, 0, &status_loggers_tail },
  497. { "status-fd", 0, 1, NULL, NULL, setpipe, 0 },
  498. { "log", 0, 1, NULL, &log_file, NULL, 0 },
  499. { "pending", 'a', 0, &f_pending, NULL, NULL, 1 },
  500. { "recursive", 'R', 0, &f_recursive, NULL, NULL, 1 },
  501. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  502. { "dry-run", 0, 0, &f_noact, NULL, NULL, 1 },
  503. { "simulate", 0, 0, &f_noact, NULL, NULL, 1 },
  504. { "no-debsig", 0, 0, &f_nodebsig, NULL, NULL, 1 },
  505. /* Alias ('G') for --refuse. */
  506. { NULL, 'G', 0, &fc_downgrade, NULL, NULL, 0 },
  507. { "selected-only", 'O', 0, &f_alsoselect, NULL, NULL, 0 },
  508. { "triggers", 0, 0, &f_triggers, NULL, NULL, 1 },
  509. { "no-triggers", 0, 0, &f_triggers, NULL, NULL, -1 },
  510. /* FIXME: Remove ('N') sometime. */
  511. { "no-also-select", 'N', 0, &f_alsoselect, NULL, NULL, 0 },
  512. { "skip-same-version", 'E', 0, &f_skipsame, NULL, NULL, 1 },
  513. { "auto-deconfigure", 'B', 0, &f_autodeconf, NULL, NULL, 1 },
  514. { "root", 0, 1, NULL, NULL, setroot, 0 },
  515. { "abort-after", 0, 1, &errabort, NULL, setinteger, 0 },
  516. { "admindir", 0, 1, NULL, &admindir, NULL, 0 },
  517. { "instdir", 0, 1, NULL, &instdir, NULL, 0 },
  518. { "ignore-depends", 0, 1, NULL, NULL, ignoredepends, 0 },
  519. { "force", 0, 2, NULL, NULL, setforce, 1 },
  520. { "refuse", 0, 2, NULL, NULL, setforce, 0 },
  521. { "no-force", 0, 2, NULL, NULL, setforce, 0 },
  522. { "debug", 'D', 1, NULL, NULL, setdebug, 0 },
  523. { "help", 'h', 0, NULL, NULL, usage, 0 },
  524. { "version", 0, 0, NULL, NULL, printversion, 0 },
  525. ACTIONBACKEND( "build", 'b', BACKEND),
  526. ACTIONBACKEND( "contents", 'c', BACKEND),
  527. ACTIONBACKEND( "control", 'e', BACKEND),
  528. ACTIONBACKEND( "info", 'I', BACKEND),
  529. ACTIONBACKEND( "field", 'f', BACKEND),
  530. ACTIONBACKEND( "extract", 'x', BACKEND),
  531. ACTIONBACKEND( "vextract", 'X', BACKEND),
  532. ACTIONBACKEND( "fsys-tarfile", 0, BACKEND),
  533. { NULL, 0, 0, NULL, NULL, NULL, 0 }
  534. };
  535. int
  536. execbackend(const char *const *argv)
  537. {
  538. struct command cmd;
  539. char *arg;
  540. command_init(&cmd, cipaction->arg_ptr, NULL);
  541. command_add_arg(&cmd, cipaction->arg_ptr);
  542. m_asprintf(&arg, "--%s", cipaction->olong);
  543. command_add_arg(&cmd, arg);
  544. /* Exlicitely separate arguments from options as any user-supplied
  545. * separator got stripped by the option parser */
  546. command_add_arg(&cmd, "--");
  547. command_add_argl(&cmd, (const char **)argv);
  548. command_exec(&cmd);
  549. }
  550. int
  551. commandfd(const char *const *argv)
  552. {
  553. struct varbuf linevb = VARBUF_INIT;
  554. const char * pipein;
  555. const char **newargs = NULL;
  556. char *ptr, *endptr;
  557. FILE *in;
  558. unsigned long infd;
  559. int ret = 0;
  560. int c, lno, i;
  561. bool skipchar;
  562. pipein = *argv++;
  563. if (pipein == NULL)
  564. badusage(_("--command-fd takes one argument, not zero"));
  565. if (*argv)
  566. badusage(_("--command-fd only takes one argument"));
  567. errno = 0;
  568. infd = strtoul(pipein, &endptr, 10);
  569. if (pipein == endptr || *endptr || infd > INT_MAX)
  570. ohshite(_("invalid integer for --%s: `%.250s'"), "command-fd", pipein);
  571. if ((in= fdopen(infd, "r")) == NULL)
  572. ohshite(_("couldn't open `%i' for stream"), (int) infd);
  573. for (;;) {
  574. bool mode = false;
  575. int argc= 1;
  576. lno= 0;
  577. push_error_context();
  578. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  579. if (c == EOF) break;
  580. if (c == '#') {
  581. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  582. continue;
  583. }
  584. varbuf_reset(&linevb);
  585. do {
  586. varbuf_add_char(&linevb, c);
  587. c= getc(in);
  588. if (c == '\n') lno++;
  589. /* This isn't fully accurate, but overestimating can't hurt. */
  590. if (isspace(c))
  591. argc++;
  592. } while (c != EOF && c != '\n');
  593. if (c == EOF) ohshit(_("unexpected eof before end of line %d"),lno);
  594. if (!argc) continue;
  595. varbuf_end_str(&linevb);
  596. newargs = m_realloc(newargs, sizeof(const char *) * (argc + 1));
  597. argc= 1;
  598. ptr= linevb.buf;
  599. endptr = ptr + linevb.used + 1;
  600. skipchar = false;
  601. while(ptr < endptr) {
  602. if (skipchar) {
  603. skipchar = false;
  604. } else if (*ptr == '\\') {
  605. memmove(ptr, (ptr+1), (linevb.used-(linevb.buf - ptr)-1));
  606. endptr--;
  607. skipchar = true;
  608. continue;
  609. } else if (isspace(*ptr)) {
  610. if (mode == true) {
  611. *ptr = '\0';
  612. mode = false;
  613. }
  614. } else {
  615. if (mode == false) {
  616. newargs[argc]= ptr;
  617. argc++;
  618. mode = true;
  619. }
  620. }
  621. ptr++;
  622. }
  623. *ptr = '\0';
  624. newargs[argc++] = NULL;
  625. /* We strdup() each argument, but never free it, because the
  626. * error messages contain references back to these strings.
  627. * Freeing them, and reusing the memory, would make those
  628. * error messages confusing, to say the least. */
  629. for(i=1;i<argc;i++)
  630. if (newargs[i])
  631. newargs[i] = m_strdup(newargs[i]);
  632. setaction(NULL, NULL);
  633. myopt((const char *const **)&newargs, cmdinfos, printforhelp);
  634. if (!cipaction) badusage(_("need an action option"));
  635. ret |= cipaction->action(newargs);
  636. pop_error_context(ehflag_normaltidy);
  637. }
  638. return ret;
  639. }
  640. int main(int argc, const char *const *argv) {
  641. int ret;
  642. setlocale(LC_ALL, "");
  643. bindtextdomain(PACKAGE, LOCALEDIR);
  644. textdomain(PACKAGE);
  645. dpkg_set_progname("dpkg");
  646. standard_startup();
  647. loadcfgfile(DPKG, cmdinfos);
  648. myopt(&argv, cmdinfos, printforhelp);
  649. if (!cipaction) badusage(_("need an action option"));
  650. admindir = dpkg_db_set_dir(admindir);
  651. /* Always set environment, to avoid possible security risks. */
  652. if (setenv("DPKG_ADMINDIR", admindir, 1) < 0)
  653. ohshite(_("unable to setenv for subprocesses"));
  654. if (!f_triggers)
  655. f_triggers = (cipaction->arg_int == act_triggers && *argv) ? -1 : 1;
  656. setvbuf(stdout, NULL, _IONBF, 0);
  657. if (is_invoke_action(cipaction->arg_int)) {
  658. run_invoke_hooks(cipaction->olong, pre_invoke_hooks);
  659. run_status_loggers(status_loggers);
  660. }
  661. filesdbinit();
  662. ret = cipaction->action(argv);
  663. if (is_invoke_action(cipaction->arg_int))
  664. run_invoke_hooks(cipaction->olong, post_invoke_hooks);
  665. standard_shutdown();
  666. return reportbroken_retexitstatus(ret);
  667. }