main.c 26 KB

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