main.c 27 KB

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