packages.c 20 KB

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