packages.c 20 KB

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