packages.c 20 KB

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