main.c 28 KB

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