main.c 26 KB

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