packages.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 <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 <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <dirent.h>
  30. #include <ctype.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <assert.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. size_t 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= itb_normal;
  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_install:
  166. /* Don't try to configure pkgs that we've just disappeared. */
  167. if (pkg->status == stat_notinstalled)
  168. break;
  169. case act_configure:
  170. deferred_configure(pkg);
  171. break;
  172. case act_remove: case act_purge:
  173. deferred_remove(pkg);
  174. break;
  175. default:
  176. internerr("unknown action in queue");
  177. }
  178. if (ferror(stdout)) werr("stdout");
  179. if (ferror(stderr)) werr("stderr");
  180. set_error_display(0,0);
  181. error_unwind(ehflag_normaltidy);
  182. }
  183. }
  184. /*** dependency processing - common to --configure and --remove ***/
  185. /*
  186. * The algorithm for deciding what to configure or remove first is as
  187. * follows:
  188. *
  189. * Loop through all packages doing a `try 1' until we've been round and
  190. * nothing has been done, then do `try 2' and `try 3' likewise.
  191. *
  192. * When configuring, in each try we check to see whether all
  193. * dependencies of this package are done. If so we do it. If some of
  194. * the dependencies aren't done yet but will be later we defer the
  195. * package, otherwise it is an error.
  196. *
  197. * When removing, in each try we check to see whether there are any
  198. * packages that would have dependencies missing if we removed this
  199. * one. If not we remove it now. If some of these packages are
  200. * themselves scheduled for removal we defer the package until they
  201. * have been done.
  202. *
  203. * The criteria for satisfying a dependency vary with the various
  204. * tries. In try 1 we treat the dependencies as absolute. In try 2 we
  205. * check break any cycles in the dependency graph involving the package
  206. * we are trying to process before trying to process the package
  207. * normally. In try 3 (which should only be reached if
  208. * --force-depends-version is set) we ignore version number clauses in
  209. * Depends lines. In try 4 (only reached if --force-depends is set) we
  210. * say "ok" regardless.
  211. *
  212. * If we are configuring and one of the packages we depend on is
  213. * awaiting configuration but wasn't specified in the argument list we
  214. * will add it to the argument list if --configure-any is specified.
  215. * In this case we note this as having "done something" so that we
  216. * don't needlessly escalate to higher levels of dependency checking
  217. * and breaking.
  218. */
  219. static int deppossi_ok_found(struct pkginfo *possdependee,
  220. struct pkginfo *requiredby,
  221. struct pkginfo *removing,
  222. struct pkginfo *providing,
  223. int *matched,
  224. struct deppossi *checkversion,
  225. int *interestingwarnings,
  226. struct varbuf *oemsgs) {
  227. int thisf;
  228. if (ignore_depends(possdependee)) {
  229. debug(dbg_depcondetail," ignoring depended package so ok and found");
  230. return 3;
  231. }
  232. thisf= 0;
  233. if (possdependee == removing) {
  234. if (providing) {
  235. varbufprintf(oemsgs,
  236. _(" Package %s which provides %s is to be removed.\n"),
  237. possdependee->name, providing->name);
  238. } else {
  239. varbufprintf(oemsgs, _(" Package %s is to be removed.\n"),
  240. possdependee->name);
  241. }
  242. *matched= 1;
  243. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  244. debug(dbg_depcondetail," removing possdependee, returning %d",thisf);
  245. return thisf;
  246. }
  247. switch (possdependee->status) {
  248. case stat_installed:
  249. case stat_unpacked:
  250. case stat_halfconfigured:
  251. assert(possdependee->installed.valid);
  252. if (checkversion && !versionsatisfied(&possdependee->installed,checkversion)) {
  253. varbufprintf(oemsgs, _(" Version of %s on system is %s.\n"),
  254. possdependee->name,
  255. versiondescribe(&possdependee->installed.version,
  256. vdew_nonambig));
  257. assert(checkversion->verrel != dvr_none);
  258. if (fc_depends || fc_dependsversion) thisf= (dependtry >= 3) ? 2 : 1;
  259. debug(dbg_depcondetail," bad version, returning %d",thisf);
  260. (*interestingwarnings)++;
  261. return thisf;
  262. }
  263. if (possdependee->status == stat_installed) {
  264. debug(dbg_depcondetail," is installed, ok and found");
  265. return 3;
  266. }
  267. if (possdependee->clientdata &&
  268. possdependee->clientdata->istobe == itb_installnew) {
  269. debug(dbg_depcondetail," unpacked/halfconfigured, defer");
  270. return 1;
  271. } else if (!removing && fc_configureany && !skip_due_to_hold(possdependee)) {
  272. fprintf(stderr,
  273. _("dpkg: also configuring `%s' (required by `%s')\n"),
  274. possdependee->name, requiredby->name);
  275. add_to_queue(possdependee); sincenothing=0; return 1;
  276. } else {
  277. if (providing) {
  278. varbufprintf(oemsgs,
  279. _(" Package %s which provides %s is not configured yet.\n"),
  280. possdependee->name, providing->name);
  281. } else {
  282. varbufprintf(oemsgs, _(" Package %s is not configured yet.\n"),
  283. possdependee->name);
  284. }
  285. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  286. debug(dbg_depcondetail," not configured/able - returning %d",thisf);
  287. (*interestingwarnings)++;
  288. return thisf;
  289. }
  290. default:
  291. if (providing) {
  292. varbufprintf(oemsgs,
  293. _(" Package %s which provides %s is not installed.\n"),
  294. possdependee->name, providing->name);
  295. } else {
  296. varbufprintf(oemsgs, _(" Package %s is not installed.\n"),
  297. possdependee->name);
  298. }
  299. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  300. debug(dbg_depcondetail," not installed - returning %d",thisf);
  301. (*interestingwarnings)++;
  302. return thisf;
  303. }
  304. }
  305. static void breaks_check_one(struct varbuf *aemsgs, int *ok,
  306. struct deppossi *breaks,
  307. struct pkginfo *broken,
  308. struct pkginfo *breaker,
  309. struct pkginfo *virtbroken) {
  310. struct varbuf depmsg;
  311. debug(dbg_depcondetail, " checking breaker %s virtbroken %s",
  312. breaker->name, virtbroken ? virtbroken->name : "<none>");
  313. if (breaker->status == stat_notinstalled ||
  314. breaker->status == stat_configfiles) return;
  315. if (broken == breaker) return;
  316. if (!versionsatisfied(&broken->installed, breaks)) return;
  317. if (ignore_depends(breaker)) return;
  318. if (virtbroken && ignore_depends(virtbroken)) return;
  319. varbufinit(&depmsg);
  320. varbufdependency(&depmsg, breaks->up);
  321. varbufaddc(&depmsg, 0);
  322. varbufprintf(aemsgs, _(" %s (%s) breaks %s and is %s.\n"),
  323. breaker->name,
  324. versiondescribe(&breaker->installed.version, vdew_nonambig),
  325. depmsg.buf,
  326. gettext(statusstrings[breaker->status]));
  327. varbuffree(&depmsg);
  328. if (virtbroken) {
  329. varbufprintf(aemsgs, _(" %s (%s) provides %s.\n"),
  330. broken->name,
  331. versiondescribe(&broken->installed.version, vdew_nonambig),
  332. virtbroken->name);
  333. } else if (breaks->verrel != dvr_none) {
  334. varbufprintf(aemsgs, _(" Version of %s to be configured is %s.\n"),
  335. broken->name,
  336. versiondescribe(&broken->installed.version, vdew_nonambig));
  337. if (fc_dependsversion) return;
  338. }
  339. if (force_breaks(breaks)) return;
  340. *ok= 0;
  341. }
  342. void breaks_check_target(struct varbuf *aemsgs, int *ok,
  343. struct pkginfo *broken,
  344. struct pkginfo *target,
  345. struct pkginfo *virtbroken) {
  346. struct deppossi *possi;
  347. for (possi= target->installed.depended; possi; possi= possi->nextrev) {
  348. if (possi->up->type != dep_breaks) continue;
  349. if (virtbroken && possi->verrel != dvr_none) continue;
  350. breaks_check_one(aemsgs, ok, possi, broken, possi->up->up, virtbroken);
  351. }
  352. }
  353. int breakses_ok(struct pkginfo *pkg, struct varbuf *aemsgs) {
  354. struct dependency *dep;
  355. struct pkginfo *virtbroken;
  356. int ok= 2;
  357. debug(dbg_depcon, " checking Breaks");
  358. breaks_check_target(aemsgs, &ok, pkg, pkg, 0);
  359. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  360. if (dep->type != dep_provides) continue;
  361. virtbroken= dep->list->ed;
  362. debug(dbg_depcondetail, " checking virtbroken %s", virtbroken->name);
  363. breaks_check_target(aemsgs, &ok, pkg, virtbroken, virtbroken);
  364. }
  365. return ok;
  366. }
  367. int dependencies_ok(struct pkginfo *pkg, struct pkginfo *removing,
  368. struct varbuf *aemsgs) {
  369. int ok, matched, found, thisf, interestingwarnings;
  370. struct varbuf oemsgs;
  371. struct dependency *dep;
  372. struct deppossi *possi, *provider;
  373. varbufinit(&oemsgs);
  374. interestingwarnings= 0;
  375. ok= 2; /* 2=ok, 1=defer, 0=halt */
  376. debug(dbg_depcon,"checking dependencies of %s (- %s)",
  377. pkg->name, removing ? removing->name : "<none>");
  378. assert(pkg->installed.valid);
  379. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  380. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  381. debug(dbg_depcondetail," checking group ...");
  382. matched= 0; varbufreset(&oemsgs);
  383. found= 0; /* 0=none, 1=defer, 2=withwarning, 3=ok */
  384. for (possi= dep->list; found != 3 && possi; possi= possi->next) {
  385. debug(dbg_depcondetail," checking possibility -> %s",possi->ed->name);
  386. if (possi->cyclebreak) {
  387. debug(dbg_depcondetail," break cycle so ok and found");
  388. found= 3; break;
  389. }
  390. thisf= deppossi_ok_found(possi->ed,pkg,removing,0,
  391. &matched,possi,&interestingwarnings,&oemsgs);
  392. if (thisf > found) found= thisf;
  393. if (found != 3 && possi->verrel == dvr_none) {
  394. if (possi->ed->installed.valid) {
  395. for (provider= possi->ed->installed.depended;
  396. found != 3 && provider;
  397. provider= provider->nextrev) {
  398. if (provider->up->type != dep_provides) continue;
  399. debug(dbg_depcondetail," checking provider %s",provider->up->up->name);
  400. thisf= deppossi_ok_found(provider->up->up,pkg,removing,possi->ed,
  401. &matched,0,&interestingwarnings,&oemsgs);
  402. if (thisf > found) found= thisf;
  403. }
  404. }
  405. }
  406. debug(dbg_depcondetail," found %d",found);
  407. if (thisf > found) found= thisf;
  408. }
  409. debug(dbg_depcondetail," found %d matched %d",found,matched);
  410. if (removing && !matched) continue;
  411. switch (found) {
  412. case 0:
  413. ok= 0;
  414. case 2:
  415. varbufaddstr(aemsgs, " ");
  416. varbufaddstr(aemsgs, pkg->name);
  417. varbufaddstr(aemsgs, _(" depends on "));
  418. varbufdependency(aemsgs, dep);
  419. if (interestingwarnings) {
  420. /* Don't print the line about the package to be removed if
  421. * that's the only line.
  422. */
  423. varbufaddstr(aemsgs, _("; however:\n"));
  424. varbufaddc(&oemsgs, 0);
  425. varbufaddstr(aemsgs, oemsgs.buf);
  426. } else {
  427. varbufaddstr(aemsgs, ".\n");
  428. }
  429. break;
  430. case 1:
  431. if (ok>1) ok= 1;
  432. break;
  433. case 3:
  434. break;
  435. default:
  436. internerr("unknown value for found");
  437. }
  438. }
  439. if (ok == 0 && (pkg->clientdata && pkg->clientdata->istobe == itb_remove))
  440. ok= 1;
  441. varbuffree(&oemsgs);
  442. debug(dbg_depcon,"ok %d msgs >>%.*s<<", ok, (int)aemsgs->used, aemsgs->buf);
  443. return ok;
  444. }