main.c 27 KB

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