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