packages.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * dpkg - main program for package management
  3. * packages.c - common to actions that process packages
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <assert.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <dirent.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/pkg-list.h>
  36. #include <dpkg/pkg-queue.h>
  37. #include <dpkg/myopt.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. static struct pkginfo *progress_bytrigproc;
  41. static struct pkg_queue queue = PKG_QUEUE_INIT;
  42. int sincenothing = 0, dependtry = 0;
  43. void
  44. add_to_queue(struct pkginfo *pkg)
  45. {
  46. pkg_queue_push(&queue, pkg);
  47. }
  48. static void
  49. enqueue_pending(void)
  50. {
  51. struct pkgiterator *it;
  52. struct pkginfo *pkg;
  53. it = iterpkgstart();
  54. while ((pkg = iterpkgnext(it)) != NULL) {
  55. switch (cipaction->arg) {
  56. case act_configure:
  57. if (!(pkg->status == stat_unpacked ||
  58. pkg->status == stat_halfconfigured ||
  59. pkg->trigpend_head))
  60. continue;
  61. if (pkg->want != want_install)
  62. continue;
  63. break;
  64. case act_triggers:
  65. if (!pkg->trigpend_head)
  66. continue;
  67. if (pkg->want != want_install)
  68. continue;
  69. break;
  70. case act_remove:
  71. case act_purge:
  72. if (pkg->want != want_purge) {
  73. if (pkg->want != want_deinstall)
  74. continue;
  75. if (pkg->status == stat_configfiles)
  76. continue;
  77. }
  78. if (pkg->status == stat_notinstalled)
  79. continue;
  80. break;
  81. default:
  82. internerr("unknown action '%d'", cipaction->arg);
  83. }
  84. add_to_queue(pkg);
  85. }
  86. iterpkgend(it);
  87. }
  88. static void
  89. enqueue_specified(const char *const *argv)
  90. {
  91. const char *thisarg;
  92. while ((thisarg = *argv++) != NULL) {
  93. struct pkginfo *pkg;
  94. pkg = findpackage(thisarg);
  95. if (pkg->status == stat_notinstalled) {
  96. size_t l = strlen(pkg->name);
  97. const char *extension = pkg->name + l - sizeof(DEBEXT) + 1;
  98. if (l >= sizeof(DEBEXT) && strcmp(extension, DEBEXT) == 0)
  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. void
  106. packages(const char *const *argv)
  107. {
  108. trigproc_install_hooks();
  109. modstatdb_init(admindir,
  110. f_noact ? msdbrw_readonly
  111. : fc_nonroot ? msdbrw_write
  112. : msdbrw_needsuperuser);
  113. checkpath();
  114. log_message("startup packages %s", cipaction->olong);
  115. if (f_pending) {
  116. if (*argv)
  117. badusage(_("--%s --pending does not take any non-option arguments"),cipaction->olong);
  118. enqueue_pending();
  119. } else {
  120. if (!*argv)
  121. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  122. enqueue_specified(argv);
  123. }
  124. ensure_diversions();
  125. process_queue();
  126. trigproc_run_deferred();
  127. modstatdb_shutdown();
  128. }
  129. void process_queue(void) {
  130. struct pkg_list *rundown;
  131. struct pkginfo *volatile pkg;
  132. volatile enum action action_todo;
  133. jmp_buf ejbuf;
  134. enum istobes istobe= itb_normal;
  135. if (abort_processing)
  136. return;
  137. clear_istobes();
  138. switch (cipaction->arg) {
  139. case act_triggers:
  140. case act_configure: case act_install: istobe= itb_installnew; break;
  141. case act_remove: case act_purge: istobe= itb_remove; break;
  142. default:
  143. internerr("unknown action '%d'", cipaction->arg);
  144. }
  145. for (rundown = queue.head; rundown; rundown = rundown->next) {
  146. ensure_package_clientdata(rundown->pkg);
  147. if (rundown->pkg->clientdata->istobe == istobe) {
  148. /* Erase the queue entry - this is a second copy! */
  149. switch (cipaction->arg) {
  150. case act_triggers:
  151. case act_configure: case act_remove: case act_purge:
  152. printf(_("Package %s listed more than once, only processing once.\n"),
  153. rundown->pkg->name);
  154. break;
  155. case act_install:
  156. printf(_("More than one copy of package %s has been unpacked\n"
  157. " in this run ! Only configuring it once.\n"),
  158. rundown->pkg->name);
  159. break;
  160. default:
  161. internerr("unknown action '%d'", cipaction->arg);
  162. }
  163. rundown->pkg = NULL;
  164. } else {
  165. rundown->pkg->clientdata->istobe= istobe;
  166. }
  167. }
  168. while (!pkg_queue_is_empty(&queue)) {
  169. pkg = pkg_queue_pop(&queue);
  170. if (!pkg) continue; /* duplicate, which we removed earlier */
  171. action_todo = cipaction->arg;
  172. if (sincenothing++ > queue.length * 2 + 2) {
  173. if (progress_bytrigproc && progress_bytrigproc->trigpend_head) {
  174. add_to_queue(pkg);
  175. pkg = progress_bytrigproc;
  176. action_todo = act_configure;
  177. } else {
  178. dependtry++;
  179. sincenothing = 0;
  180. assert(dependtry <= 4);
  181. }
  182. }
  183. assert(pkg->status <= stat_installed);
  184. if (setjmp(ejbuf)) {
  185. /* give up on it from the point of view of other packages, ie reset istobe */
  186. pkg->clientdata->istobe= itb_normal;
  187. error_unwind(ehflag_bombout);
  188. if (abort_processing)
  189. return;
  190. continue;
  191. }
  192. push_error_handler(&ejbuf,print_error_perpackage,pkg->name);
  193. switch (action_todo) {
  194. case act_triggers:
  195. if (!pkg->trigpend_head)
  196. ohshit(_("package %.250s is not ready for trigger processing\n"
  197. " (current status `%.250s' with no pending triggers)"),
  198. pkg->name, statusinfos[pkg->status].name);
  199. /* Fall through. */
  200. case act_install:
  201. /* Don't try to configure pkgs that we've just disappeared. */
  202. if (pkg->status == stat_notinstalled)
  203. break;
  204. /* Fall through. */
  205. case act_configure:
  206. /* Do whatever is most needed. */
  207. if (pkg->trigpend_head)
  208. trigproc(pkg);
  209. else
  210. deferred_configure(pkg);
  211. break;
  212. case act_remove: case act_purge:
  213. deferred_remove(pkg);
  214. break;
  215. default:
  216. internerr("unknown action '%d'", cipaction->arg);
  217. }
  218. m_output(stdout, _("<standard output>"));
  219. m_output(stderr, _("<standard error>"));
  220. set_error_display(NULL, NULL);
  221. error_unwind(ehflag_normaltidy);
  222. }
  223. assert(!queue.length);
  224. }
  225. /*** dependency processing - common to --configure and --remove ***/
  226. /*
  227. * The algorithm for deciding what to configure or remove first is as
  228. * follows:
  229. *
  230. * Loop through all packages doing a `try 1' until we've been round and
  231. * nothing has been done, then do `try 2' and `try 3' likewise.
  232. *
  233. * When configuring, in each try we check to see whether all
  234. * dependencies of this package are done. If so we do it. If some of
  235. * the dependencies aren't done yet but will be later we defer the
  236. * package, otherwise it is an error.
  237. *
  238. * When removing, in each try we check to see whether there are any
  239. * packages that would have dependencies missing if we removed this
  240. * one. If not we remove it now. If some of these packages are
  241. * themselves scheduled for removal we defer the package until they
  242. * have been done.
  243. *
  244. * The criteria for satisfying a dependency vary with the various
  245. * tries. In try 1 we treat the dependencies as absolute. In try 2 we
  246. * check break any cycles in the dependency graph involving the package
  247. * we are trying to process before trying to process the package
  248. * normally. In try 3 (which should only be reached if
  249. * --force-depends-version is set) we ignore version number clauses in
  250. * Depends lines. In try 4 (only reached if --force-depends is set) we
  251. * say "ok" regardless.
  252. *
  253. * If we are configuring and one of the packages we depend on is
  254. * awaiting configuration but wasn't specified in the argument list we
  255. * will add it to the argument list if --configure-any is specified.
  256. * In this case we note this as having "done something" so that we
  257. * don't needlessly escalate to higher levels of dependency checking
  258. * and breaking.
  259. */
  260. /* Return values:
  261. * 0: cannot be satisfied.
  262. * 1: defer: may be satisfied later, when other packages are better or
  263. * at higher dependtry due to --force
  264. * will set *fixbytrig to package whose trigger processing would help
  265. * if applicable (and leave it alone otherwise)
  266. * 2: not satisfied but forcing
  267. * (*interestingwarnings >= 0 on exit? caller is to print oemsgs)
  268. * 3: satisfied now
  269. */
  270. static int deppossi_ok_found(struct pkginfo *possdependee,
  271. struct pkginfo *requiredby,
  272. struct pkginfo *removing,
  273. struct pkginfo *providing,
  274. struct pkginfo **fixbytrig,
  275. bool *matched,
  276. struct deppossi *checkversion,
  277. int *interestingwarnings,
  278. struct varbuf *oemsgs) {
  279. int thisf;
  280. if (ignore_depends(possdependee)) {
  281. debug(dbg_depcondetail," ignoring depended package so ok and found");
  282. return 3;
  283. }
  284. thisf= 0;
  285. if (possdependee == removing) {
  286. if (providing) {
  287. varbufprintf(oemsgs,
  288. _(" Package %s which provides %s is to be removed.\n"),
  289. possdependee->name, providing->name);
  290. } else {
  291. varbufprintf(oemsgs, _(" Package %s is to be removed.\n"),
  292. possdependee->name);
  293. }
  294. *matched = true;
  295. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  296. debug(dbg_depcondetail," removing possdependee, returning %d",thisf);
  297. return thisf;
  298. }
  299. switch (possdependee->status) {
  300. case stat_unpacked:
  301. case stat_halfconfigured:
  302. case stat_triggersawaited:
  303. case stat_triggerspending:
  304. case stat_installed:
  305. if (checkversion && !versionsatisfied(&possdependee->installed,checkversion)) {
  306. varbufprintf(oemsgs, _(" Version of %s on system is %s.\n"),
  307. possdependee->name,
  308. versiondescribe(&possdependee->installed.version,
  309. vdew_nonambig));
  310. assert(checkversion->verrel != dvr_none);
  311. if (fc_depends || fc_dependsversion) thisf= (dependtry >= 3) ? 2 : 1;
  312. debug(dbg_depcondetail," bad version, returning %d",thisf);
  313. (*interestingwarnings)++;
  314. return thisf;
  315. }
  316. if (possdependee->status == stat_installed ||
  317. possdependee->status == stat_triggerspending) {
  318. debug(dbg_depcondetail," is installed, ok and found");
  319. return 3;
  320. }
  321. if (possdependee->status == stat_triggersawaited) {
  322. assert(possdependee->trigaw.head);
  323. if (removing || !(f_triggers ||
  324. possdependee->clientdata->istobe == itb_installnew)) {
  325. if (providing) {
  326. varbufprintf(oemsgs,
  327. _(" Package %s which provides %s awaits trigger processing.\n"),
  328. possdependee->name, providing->name);
  329. } else {
  330. varbufprintf(oemsgs,
  331. _(" Package %s awaits trigger processing.\n"),
  332. possdependee->name);
  333. }
  334. debug(dbg_depcondetail, " triggers-awaited, no fixbytrig");
  335. goto unsuitable;
  336. }
  337. /* We don't check the status of trigaw.head->pend here, just in case
  338. * we get into the pathological situation where Triggers-Awaited but
  339. * the named package doesn't actually have any pending triggers. In
  340. * that case we queue the non-pending package for trigger processing
  341. * anyway, and that trigger processing will be a noop except for
  342. * sorting out all of the packages which name it in T-Awaited.
  343. *
  344. * (This situation can only arise if modstatdb_note success in
  345. * clearing the triggers-pending status of the pending package
  346. * but then fails to go on to update the awaiters.)
  347. */
  348. *fixbytrig = possdependee->trigaw.head->pend;
  349. debug(dbg_depcondetail,
  350. " triggers-awaited, fixbytrig `%s', returning 1",
  351. (*fixbytrig)->name);
  352. return 1;
  353. }
  354. if (possdependee->clientdata &&
  355. possdependee->clientdata->istobe == itb_installnew) {
  356. debug(dbg_depcondetail," unpacked/halfconfigured, defer");
  357. return 1;
  358. } else if (!removing && fc_configureany &&
  359. !skip_due_to_hold(possdependee) &&
  360. !(possdependee->status == stat_halfconfigured)) {
  361. fprintf(stderr,
  362. _("dpkg: also configuring `%s' (required by `%s')\n"),
  363. possdependee->name, requiredby->name);
  364. add_to_queue(possdependee); sincenothing=0; return 1;
  365. } else {
  366. if (providing) {
  367. varbufprintf(oemsgs,
  368. _(" Package %s which provides %s is not configured yet.\n"),
  369. possdependee->name, providing->name);
  370. } else {
  371. varbufprintf(oemsgs, _(" Package %s is not configured yet.\n"),
  372. possdependee->name);
  373. }
  374. debug(dbg_depcondetail, " not configured/able");
  375. goto unsuitable;
  376. }
  377. default:
  378. if (providing) {
  379. varbufprintf(oemsgs,
  380. _(" Package %s which provides %s is not installed.\n"),
  381. possdependee->name, providing->name);
  382. } else {
  383. varbufprintf(oemsgs, _(" Package %s is not installed.\n"),
  384. possdependee->name);
  385. }
  386. debug(dbg_depcondetail, " not installed");
  387. goto unsuitable;
  388. }
  389. unsuitable:
  390. if (fc_depends)
  391. thisf = (dependtry >= 4) ? 2 : 1;
  392. debug(dbg_depcondetail, " returning %d", thisf);
  393. (*interestingwarnings)++;
  394. return thisf;
  395. }
  396. static void breaks_check_one(struct varbuf *aemsgs, int *ok,
  397. struct deppossi *breaks,
  398. struct pkginfo *broken,
  399. struct pkginfo *breaker,
  400. struct pkginfo *virtbroken) {
  401. struct varbuf depmsg = VARBUF_INIT;
  402. debug(dbg_depcondetail, " checking breaker %s virtbroken %s",
  403. breaker->name, virtbroken ? virtbroken->name : "<none>");
  404. if (breaker->status == stat_notinstalled ||
  405. breaker->status == stat_configfiles) return;
  406. if (broken == breaker) return;
  407. if (!versionsatisfied(&broken->installed, breaks)) return;
  408. if (ignore_depends(breaker)) return;
  409. if (virtbroken && ignore_depends(virtbroken)) return;
  410. varbufdependency(&depmsg, breaks->up);
  411. varbufaddc(&depmsg, 0);
  412. varbufprintf(aemsgs, _(" %s (%s) breaks %s and is %s.\n"),
  413. breaker->name,
  414. versiondescribe(&breaker->installed.version, vdew_nonambig),
  415. depmsg.buf,
  416. gettext(statusstrings[breaker->status]));
  417. varbuf_destroy(&depmsg);
  418. if (virtbroken) {
  419. varbufprintf(aemsgs, _(" %s (%s) provides %s.\n"),
  420. broken->name,
  421. versiondescribe(&broken->installed.version, vdew_nonambig),
  422. virtbroken->name);
  423. } else if (breaks->verrel != dvr_none) {
  424. varbufprintf(aemsgs, _(" Version of %s to be configured is %s.\n"),
  425. broken->name,
  426. versiondescribe(&broken->installed.version, vdew_nonambig));
  427. if (fc_dependsversion) return;
  428. }
  429. if (force_breaks(breaks)) return;
  430. *ok= 0;
  431. }
  432. static void breaks_check_target(struct varbuf *aemsgs, int *ok,
  433. struct pkginfo *broken,
  434. struct pkginfo *target,
  435. struct pkginfo *virtbroken) {
  436. struct deppossi *possi;
  437. for (possi = target->installed.depended; possi; possi = possi->rev_next) {
  438. if (possi->up->type != dep_breaks) continue;
  439. if (virtbroken && possi->verrel != dvr_none) continue;
  440. breaks_check_one(aemsgs, ok, possi, broken, possi->up->up, virtbroken);
  441. }
  442. }
  443. int breakses_ok(struct pkginfo *pkg, struct varbuf *aemsgs) {
  444. struct dependency *dep;
  445. struct pkginfo *virtbroken;
  446. int ok= 2;
  447. debug(dbg_depcon, " checking Breaks");
  448. breaks_check_target(aemsgs, &ok, pkg, pkg, NULL);
  449. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  450. if (dep->type != dep_provides) continue;
  451. virtbroken= dep->list->ed;
  452. debug(dbg_depcondetail, " checking virtbroken %s", virtbroken->name);
  453. breaks_check_target(aemsgs, &ok, pkg, virtbroken, virtbroken);
  454. }
  455. return ok;
  456. }
  457. int dependencies_ok(struct pkginfo *pkg, struct pkginfo *removing,
  458. struct varbuf *aemsgs) {
  459. int ok, found, thisf, interestingwarnings;
  460. bool matched, anycannotfixbytrig;
  461. struct varbuf oemsgs = VARBUF_INIT;
  462. struct dependency *dep;
  463. struct deppossi *possi, *provider;
  464. struct pkginfo *possfixbytrig, *canfixbytrig;
  465. interestingwarnings= 0;
  466. ok= 2; /* 2=ok, 1=defer, 0=halt */
  467. debug(dbg_depcon,"checking dependencies of %s (- %s)",
  468. pkg->name, removing ? removing->name : "<none>");
  469. anycannotfixbytrig = false;
  470. canfixbytrig = NULL;
  471. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  472. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  473. debug(dbg_depcondetail," checking group ...");
  474. matched = false;
  475. varbufreset(&oemsgs);
  476. found= 0; /* 0=none, 1=defer, 2=withwarning, 3=ok */
  477. possfixbytrig = NULL;
  478. for (possi= dep->list; found != 3 && possi; possi= possi->next) {
  479. debug(dbg_depcondetail," checking possibility -> %s",possi->ed->name);
  480. if (possi->cyclebreak) {
  481. debug(dbg_depcondetail," break cycle so ok and found");
  482. found= 3; break;
  483. }
  484. thisf = deppossi_ok_found(possi->ed, pkg, removing, NULL,
  485. &possfixbytrig,
  486. &matched,possi,&interestingwarnings,&oemsgs);
  487. if (thisf > found) found= thisf;
  488. if (found != 3 && possi->verrel == dvr_none) {
  489. for (provider = possi->ed->installed.depended;
  490. found != 3 && provider;
  491. provider = provider->rev_next) {
  492. if (provider->up->type != dep_provides)
  493. continue;
  494. debug(dbg_depcondetail, " checking provider %s",
  495. provider->up->up->name);
  496. thisf = deppossi_ok_found(provider->up->up, pkg, removing, possi->ed,
  497. &possfixbytrig, &matched, NULL,
  498. &interestingwarnings, &oemsgs);
  499. if (thisf > found)
  500. found = thisf;
  501. }
  502. }
  503. debug(dbg_depcondetail," found %d",found);
  504. if (thisf > found) found= thisf;
  505. }
  506. debug(dbg_depcondetail, " found %d matched %d possfixbytrig %s",
  507. found, matched, possfixbytrig ? possfixbytrig->name : "-");
  508. if (removing && !matched) continue;
  509. switch (found) {
  510. case 0:
  511. anycannotfixbytrig = true;
  512. ok= 0;
  513. case 2:
  514. varbufaddstr(aemsgs, " ");
  515. varbufaddstr(aemsgs, pkg->name);
  516. varbufaddstr(aemsgs, _(" depends on "));
  517. varbufdependency(aemsgs, dep);
  518. if (interestingwarnings) {
  519. /* Don't print the line about the package to be removed if
  520. * that's the only line.
  521. */
  522. varbufaddstr(aemsgs, _("; however:\n"));
  523. varbufaddc(&oemsgs, 0);
  524. varbufaddstr(aemsgs, oemsgs.buf);
  525. } else {
  526. varbufaddstr(aemsgs, ".\n");
  527. }
  528. break;
  529. case 1:
  530. if (possfixbytrig)
  531. canfixbytrig = possfixbytrig;
  532. else
  533. anycannotfixbytrig = true;
  534. if (ok>1) ok= 1;
  535. break;
  536. case 3:
  537. break;
  538. default:
  539. internerr("unknown value for found '%d'", found);
  540. }
  541. }
  542. if (ok == 0 && (pkg->clientdata && pkg->clientdata->istobe == itb_remove))
  543. ok= 1;
  544. if (!anycannotfixbytrig && canfixbytrig)
  545. progress_bytrigproc = canfixbytrig;
  546. varbuf_destroy(&oemsgs);
  547. debug(dbg_depcon,"ok %d msgs >>%.*s<<", ok, (int)aemsgs->used, aemsgs->buf);
  548. return ok;
  549. }