main.c 28 KB

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