packages.c 16 KB

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