packages.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * dpkg - main program for package management
  3. * packages.c - common to actions that process packages
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.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 <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <dirent.h>
  29. #include <ctype.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #include <config.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <myopt.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. struct pkginqueue {
  40. struct pkginqueue *next;
  41. struct pkginfo *pkg;
  42. };
  43. static struct pkginqueue *queuehead= 0, **queuetail= &queuehead;
  44. int queuelen=0, sincenothing=0, dependtry=0;
  45. void add_to_queue(struct pkginfo *pkg) {
  46. struct pkginqueue *newent;
  47. newent= m_malloc(sizeof(struct pkginqueue));
  48. newent->pkg= pkg;
  49. newent->next= 0;
  50. *queuetail= newent;
  51. queuetail= &newent->next;
  52. queuelen++;
  53. }
  54. void packages(const char *const *argv) {
  55. struct pkgiterator *it;
  56. struct pkginfo *pkg;
  57. const char *thisarg;
  58. int l;
  59. modstatdb_init(admindir,
  60. f_noact ? msdbrw_readonly
  61. : fc_nonroot ? msdbrw_write
  62. : msdbrw_needsuperuser);
  63. checkpath();
  64. if (f_pending) {
  65. if (*argv)
  66. badusage(_("--%s --pending does not take any non-option arguments"),cipaction->olong);
  67. it= iterpkgstart();
  68. while ((pkg= iterpkgnext(it)) != 0) {
  69. switch (cipaction->arg) {
  70. case act_configure:
  71. if (pkg->status != stat_unpacked && pkg->status != stat_halfconfigured)
  72. continue;
  73. if (pkg->want != want_install)
  74. continue;
  75. break;
  76. case act_remove:
  77. case act_purge:
  78. if (pkg->want != want_purge) {
  79. if (pkg->want != want_deinstall) continue;
  80. if (pkg->status == stat_configfiles) continue;
  81. }
  82. if (pkg->status == stat_notinstalled)
  83. continue;
  84. break;
  85. default:
  86. internerr("unknown action for pending");
  87. }
  88. add_to_queue(pkg);
  89. }
  90. iterpkgend(it);
  91. } else {
  92. if (!*argv)
  93. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  94. while ((thisarg= *argv++) != 0) {
  95. pkg= findpackage(thisarg);
  96. if (pkg->status == stat_notinstalled) {
  97. l= strlen(pkg->name);
  98. if (l >= sizeof(DEBEXT) && !strcmp(pkg->name+l-sizeof(DEBEXT)+1,DEBEXT))
  99. badusage(_("you must specify packages by their own names,"
  100. " not by quoting the names of the files they come in"));
  101. }
  102. add_to_queue(pkg);
  103. }
  104. }
  105. ensure_diversions();
  106. process_queue();
  107. modstatdb_shutdown();
  108. }
  109. void process_queue(void) {
  110. struct pkginqueue *removeent, *rundown;
  111. struct pkginfo *volatile pkg;
  112. jmp_buf ejbuf;
  113. enum istobes istobe;
  114. clear_istobes();
  115. switch (cipaction->arg) {
  116. case act_configure: case act_install: istobe= itb_installnew; break;
  117. case act_remove: case act_purge: istobe= itb_remove; break;
  118. default: internerr("unknown action for queue start");
  119. }
  120. for (rundown= queuehead; rundown; rundown= rundown->next) {
  121. ensure_package_clientdata(rundown->pkg);
  122. if (rundown->pkg->clientdata->istobe == istobe) {
  123. /* Remove it from the queue - this is a second copy ! */
  124. switch (cipaction->arg) {
  125. case act_configure: case act_remove: case act_purge:
  126. printf(_("Package %s listed more than once, only processing once.\n"),
  127. rundown->pkg->name);
  128. break;
  129. case act_install:
  130. printf(_("More than one copy of package %s has been unpacked\n"
  131. " in this run ! Only configuring it once.\n"),
  132. rundown->pkg->name);
  133. break;
  134. default:
  135. internerr("unknown action in duplicate");
  136. }
  137. rundown->pkg= 0;
  138. } else {
  139. rundown->pkg->clientdata->istobe= istobe;
  140. }
  141. }
  142. while (queuelen) {
  143. removeent= queuehead;
  144. assert(removeent);
  145. queuehead= queuehead->next;
  146. queuelen--;
  147. if (queuetail == &removeent->next) queuetail= &queuehead;
  148. pkg= removeent->pkg;
  149. free(removeent);
  150. if (!pkg) continue; /* duplicate, which we removed earlier */
  151. assert(pkg->status <= stat_configfiles);
  152. if (setjmp(ejbuf)) {
  153. /* give up on it from the point of view of other packages, ie reset istobe */
  154. pkg->clientdata->istobe= itb_normal;
  155. error_unwind(ehflag_bombout);
  156. if (onerr_abort > 0) break;
  157. continue;
  158. }
  159. push_error_handler(&ejbuf,print_error_perpackage,pkg->name);
  160. if (sincenothing++ > queuelen*2+2) {
  161. dependtry++; sincenothing= 0;
  162. assert(dependtry <= 4);
  163. }
  164. switch (cipaction->arg) {
  165. case act_configure:
  166. case act_install:
  167. deferred_configure(pkg);
  168. break;
  169. case act_remove: case act_purge:
  170. deferred_remove(pkg);
  171. break;
  172. default:
  173. internerr("unknown action in queue");
  174. }
  175. if (ferror(stdout)) werr("stdout");
  176. if (ferror(stderr)) werr("stderr");
  177. set_error_display(0,0);
  178. error_unwind(ehflag_normaltidy);
  179. }
  180. }
  181. /*** dependency processing - common to --configure and --remove ***/
  182. /*
  183. * The algorithm for deciding what to configure or remove first is as
  184. * follows:
  185. *
  186. * Loop through all packages doing a `try 1' until we've been round and
  187. * nothing has been done, then do `try 2' and `try 3' likewise.
  188. *
  189. * When configuring, in each try we check to see whether all
  190. * dependencies of this package are done. If so we do it. If some of
  191. * the dependencies aren't done yet but will be later we defer the
  192. * package, otherwise it is an error.
  193. *
  194. * When removing, in each try we check to see whether there are any
  195. * packages that would have dependencies missing if we removed this
  196. * one. If not we remove it now. If some of these packages are
  197. * themselves scheduled for removal we defer the package until they
  198. * have been done.
  199. *
  200. * The criteria for satisfying a dependency vary with the various
  201. * tries. In try 1 we treat the dependencies as absolute. In try 2 we
  202. * check break any cycles in the dependency graph involving the package
  203. * we are trying to process before trying to process the package
  204. * normally. In try 3 (which should only be reached if
  205. * --force-depends-version is set) we ignore version number clauses in
  206. * Depends lines. In try 4 (only reached if --force-depends is set) we
  207. * say "ok" regardless.
  208. *
  209. * If we are configuring and one of the packages we depend on is
  210. * awaiting configuration but wasn't specified in the argument list we
  211. * will add it to the argument list if --configure-any is specified.
  212. * In this case we note this as having "done something" so that we
  213. * don't needlessly escalate to higher levels of dependency checking
  214. * and breaking.
  215. */
  216. static int deppossi_ok_found(struct pkginfo *possdependee,
  217. struct pkginfo *requiredby,
  218. struct pkginfo *removing,
  219. struct pkginfo *providing,
  220. int *matched,
  221. struct deppossi *checkversion,
  222. int *interestingwarnings,
  223. struct varbuf *oemsgs,
  224. struct deppossi *provider) {
  225. int thisf;
  226. if (ignore_depends(possdependee)) {
  227. debug(dbg_depcondetail," ignoring depended package so ok and found");
  228. return 3;
  229. }
  230. thisf= 0;
  231. if (possdependee == removing) {
  232. varbufaddstr(oemsgs,_(" Package "));
  233. varbufaddstr(oemsgs,possdependee->name);
  234. if (providing) {
  235. varbufaddstr(oemsgs,_(" which provides "));
  236. varbufaddstr(oemsgs,providing->name);
  237. }
  238. varbufaddstr(oemsgs,_(" is to be removed.\n"));
  239. *matched= 1;
  240. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  241. debug(dbg_depcondetail," removing possdependee, returning %d",thisf);
  242. return thisf;
  243. }
  244. switch (possdependee->status) {
  245. case stat_installed:
  246. case stat_unpacked:
  247. case stat_halfconfigured:
  248. assert(possdependee->installed.valid);
  249. if (checkversion) {
  250. if (!provider) {
  251. debug(dbg_depcondetail," checking non-provided pkg %s",possdependee->name);
  252. if (!versionsatisfied(&possdependee->installed,checkversion)) {
  253. varbufaddstr(oemsgs,_(" Version of "));
  254. varbufaddstr(oemsgs,possdependee->name);
  255. varbufaddstr(oemsgs,_(" on system is "));
  256. varbufaddstr(oemsgs,versiondescribe(&possdependee->installed.version,
  257. vdew_nonambig));
  258. varbufaddstr(oemsgs,".\n");
  259. assert(checkversion->verrel != dvr_none);
  260. if (fc_depends) thisf= (dependtry >= 3) ? 2 : 1;
  261. debug(dbg_depcondetail," bad version, returning %d",thisf);
  262. (*interestingwarnings)++;
  263. return thisf;
  264. }
  265. } else {
  266. debug(dbg_depcondetail," checking package %s provided by pkg %s",
  267. checkversion->ed->name,possdependee->name);
  268. if (!versionsatisfied3(&checkversion->version,&provider->version,
  269. checkversion->verrel)) {
  270. varbufaddstr(oemsgs,_(" Version of "));
  271. varbufaddstr(oemsgs,checkversion->ed->name);
  272. varbufaddstr(oemsgs,_(" on system, provided by "));
  273. varbufaddstr(oemsgs,possdependee->name);
  274. varbufaddstr(oemsgs,_(", is "));
  275. varbufaddstr(oemsgs,versiondescribe(&provider->version,
  276. vdew_nonambig));
  277. varbufaddstr(oemsgs,".\n");
  278. assert(checkversion->verrel != dvr_none);
  279. if (fc_depends) thisf= (dependtry >= 3) ? 2 : 1;
  280. debug(dbg_depcondetail," bad version, returning %d",thisf);
  281. (*interestingwarnings)++;
  282. return thisf;
  283. }
  284. }
  285. }
  286. if (possdependee->status == stat_installed) {
  287. debug(dbg_depcondetail," is installed, ok and found");
  288. return 3;
  289. }
  290. if (possdependee->clientdata &&
  291. possdependee->clientdata->istobe == itb_installnew) {
  292. debug(dbg_depcondetail," unpacked/halfconfigured, defer");
  293. return 1;
  294. } else if (!removing && fc_configureany && !skip_due_to_hold(possdependee)) {
  295. fprintf(stderr,
  296. _("dpkg: also configuring `%s' (required by `%s')\n"),
  297. possdependee->name, requiredby->name);
  298. add_to_queue(possdependee); sincenothing=0; return 1;
  299. } else {
  300. varbufaddstr(oemsgs,_(" Package "));
  301. varbufaddstr(oemsgs,possdependee->name);
  302. if (providing) {
  303. varbufaddstr(oemsgs,_(" which provides "));
  304. varbufaddstr(oemsgs,providing->name);
  305. }
  306. varbufaddstr(oemsgs,_(" is not configured yet.\n"));
  307. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  308. debug(dbg_depcondetail," not configured/able - returning %d",thisf);
  309. (*interestingwarnings)++;
  310. return thisf;
  311. }
  312. default:
  313. varbufaddstr(oemsgs,_(" Package "));
  314. varbufaddstr(oemsgs,possdependee->name);
  315. if (providing) {
  316. varbufaddstr(oemsgs,_(" which provides "));
  317. varbufaddstr(oemsgs,providing->name);
  318. }
  319. varbufaddstr(oemsgs,_(" is not installed.\n"));
  320. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  321. debug(dbg_depcondetail," not installed - returning %d",thisf);
  322. (*interestingwarnings)++;
  323. return thisf;
  324. }
  325. }
  326. int dependencies_ok(struct pkginfo *pkg, struct pkginfo *removing,
  327. struct varbuf *aemsgs) {
  328. int ok, matched, found, thisf, interestingwarnings;
  329. struct varbuf oemsgs;
  330. struct dependency *dep;
  331. struct deppossi *possi, *provider;
  332. varbufinit(&oemsgs);
  333. interestingwarnings= 0;
  334. ok= 2; /* 2=ok, 1=defer, 0=halt */
  335. debug(dbg_depcon,"checking dependencies of %s (- %s)",
  336. pkg->name, removing ? removing->name : "<none>");
  337. assert(pkg->installed.valid);
  338. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  339. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  340. debug(dbg_depcondetail," checking group ...");
  341. matched= 0; varbufreset(&oemsgs);
  342. found= 0; /* 0=none, 1=defer, 2=withwarning, 3=ok */
  343. for (possi= dep->list; found != 3 && possi; possi= possi->next) {
  344. debug(dbg_depcondetail," checking possibility -> %s",possi->ed->name);
  345. if (possi->cyclebreak) {
  346. debug(dbg_depcondetail," break cycle, so ok and found");
  347. found= 3; break;
  348. }
  349. thisf= deppossi_ok_found(possi->ed,pkg,removing,0,
  350. &matched,possi,&interestingwarnings,&oemsgs,NULL);
  351. if (thisf > found) found= thisf;
  352. if (found != 3) {
  353. if (possi->ed->installed.valid) {
  354. for (provider= possi->ed->installed.depended;
  355. found != 3 && provider;
  356. provider= provider->nextrev) {
  357. if (provider->up->type != dep_provides) continue;
  358. debug(dbg_depcondetail," checking provider %s",provider->up->up->name);
  359. if (possi->verrel == dvr_none) {
  360. /* A simple package dep, no version */
  361. thisf= deppossi_ok_found(provider->up->up,pkg,removing,possi->ed,
  362. &matched,NULL,&interestingwarnings,&oemsgs,NULL);
  363. } else if (provider->verrel == dvr_exact) {
  364. /* For versioned depends, we only check providers with
  365. * dvr_exact. It doesn't make sense to check ones without a
  366. * version since we have nothing to verify it. Also, it is
  367. * way too complex to allow anything but exact in a provided
  368. * version. A few examples below to deter you from
  369. * trying:
  370. *
  371. * - foo depends on pkg1 (>= 0.6), bar provides pkg1 (<= 1.0).
  372. * Should pass (easy enough)
  373. *
  374. * - foo depends on pkg1 (>= 0.7) and (<= 1.1), bar
  375. * provides pkg1 (>= 1.2). Should fail (little harder)
  376. *
  377. * - foo depends on pkg1 (>= 0.4), bar provides pkg1 (<= 1.0)
  378. * and (>= 0.5), IOW, inclusive of only those versions.
  379. * This would require backchecking the other provided
  380. * versions in the possi, which would make things sickly
  381. * complex and overly time consuming. Should fail (very
  382. * hard to implement).
  383. *
  384. * There's probably some miracle formula to do this, but I
  385. * don't see it, and there's little to gain, IMNHO. Also,
  386. * packages can get around most of these by providing
  387. * multiple dvr_exact versions. -- Ben
  388. */
  389. thisf= deppossi_ok_found(provider->up->up,pkg,removing,possi->ed,
  390. &matched,possi,&interestingwarnings,&oemsgs,provider);
  391. }
  392. if (thisf > found) found= thisf;
  393. }
  394. }
  395. }
  396. debug(dbg_depcondetail," found %d",found);
  397. if (thisf > found) found= thisf;
  398. }
  399. debug(dbg_depcondetail," found %d matched %d",found,matched);
  400. if (removing && !matched) continue;
  401. switch (found) {
  402. case 0:
  403. ok= 0;
  404. case 2:
  405. varbufaddstr(aemsgs, " ");
  406. varbufaddstr(aemsgs, pkg->name);
  407. varbufaddstr(aemsgs, _(" depends on "));
  408. varbufdependency(aemsgs, dep);
  409. if (interestingwarnings) {
  410. /* Don't print the line about the package to be removed if
  411. * that's the only line.
  412. */
  413. varbufaddstr(aemsgs, _("; however:\n"));
  414. varbufaddc(&oemsgs, 0);
  415. varbufaddstr(aemsgs, oemsgs.buf);
  416. } else {
  417. varbufaddstr(aemsgs, ".\n");
  418. }
  419. break;
  420. case 1:
  421. if (ok>1) ok= 1;
  422. break;
  423. case 3:
  424. break;
  425. default:
  426. internerr("unknown value for found");
  427. }
  428. }
  429. if (ok == 0 && (pkg->clientdata && pkg->clientdata->istobe == itb_remove))
  430. ok= 1;
  431. varbuffree(&oemsgs);
  432. debug(dbg_depcon,"ok %d msgs >>%.*s<<", ok, aemsgs->used, aemsgs->buf);
  433. return ok;
  434. }