depcon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * dpkg - main program for package management
  3. * depcon.c - dependency and conflict checking
  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 <errno.h>
  26. #include <unistd.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include "main.h"
  31. struct cyclesofarlink {
  32. struct cyclesofarlink *prev;
  33. struct pkginfo *pkg;
  34. struct deppossi *possi;
  35. };
  36. static bool findbreakcyclerecursive(struct pkginfo *pkg,
  37. struct cyclesofarlink *sofar);
  38. static bool
  39. foundcyclebroken(struct cyclesofarlink *thislink, struct cyclesofarlink *sofar,
  40. struct pkginfo *dependedon, struct deppossi *possi)
  41. {
  42. struct cyclesofarlink *sol;
  43. const char *postinstfilename;
  44. struct stat stab;
  45. if(!possi)
  46. return false;
  47. /* We're investigating the dependency ‘possi’ to see if it
  48. * is part of a loop. To this end we look to see whether the
  49. * depended-on package is already one of the packages whose
  50. * dependencies we're searching. */
  51. for (sol = sofar; sol && sol->pkg != dependedon; sol = sol->prev);
  52. /* If not, we do a recursive search on it to see what we find. */
  53. if (!sol)
  54. return findbreakcyclerecursive(dependedon, thislink);
  55. debug(dbg_depcon,"found cycle");
  56. /* Right, we now break one of the links. We prefer to break
  57. * a dependency of a package without a postinst script, as
  58. * this is a null operation. If this is not possible we break
  59. * the other link in the recursive calling tree which mentions
  60. * this package (this being the first package involved in the
  61. * cycle). It doesn't particularly matter which we pick, but if
  62. * we break the earliest dependency we came across we may be
  63. * able to do something straight away when findbreakcycle returns. */
  64. sofar= thislink;
  65. for (sol = sofar; !(sol != sofar && sol->pkg == dependedon); sol = sol->prev) {
  66. postinstfilename= pkgadminfile(sol->pkg,POSTINSTFILE);
  67. if (lstat(postinstfilename,&stab)) {
  68. if (errno == ENOENT) break;
  69. ohshite(_("unable to check for existence of `%.250s'"),postinstfilename);
  70. }
  71. }
  72. /* Now we have either a package with no postinst, or the other
  73. * occurrence of the current package in the list. */
  74. sol->possi->cyclebreak = true;
  75. debug(dbg_depcon, "cycle broken at %s -> %s",
  76. sol->possi->up->up->name, sol->possi->ed->name);
  77. return true;
  78. }
  79. /**
  80. * Cycle breaking works recursively down the package dependency tree.
  81. *
  82. * ‘sofar’ is the list of packages we've descended down already - if we
  83. * encounter any of its packages again in a dependency we have found a cycle.
  84. */
  85. static bool
  86. findbreakcyclerecursive(struct pkginfo *pkg, struct cyclesofarlink *sofar)
  87. {
  88. struct cyclesofarlink thislink, *sol;
  89. struct dependency *dep;
  90. struct deppossi *possi, *providelink;
  91. struct pkginfo *provider;
  92. if (pkg->clientdata->color == black)
  93. return false;
  94. pkg->clientdata->color = gray;
  95. if (f_debug & dbg_depcondetail) {
  96. struct varbuf str_pkgs = VARBUF_INIT;
  97. for (sol = sofar; sol; sol = sol->prev) {
  98. varbuf_add_str(&str_pkgs, " <- ");
  99. varbuf_add_str(&str_pkgs, sol->pkg->name);
  100. }
  101. varbuf_add_char(&str_pkgs, '\0');
  102. debug(dbg_depcondetail, "findbreakcyclerecursive %s %s", pkg->name,
  103. str_pkgs.buf);
  104. varbuf_destroy(&str_pkgs);
  105. }
  106. thislink.pkg= pkg;
  107. thislink.prev = sofar;
  108. thislink.possi = NULL;
  109. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  110. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  111. for (possi= dep->list; possi; possi= possi->next) {
  112. /* Don't find the same cycles again. */
  113. if (possi->cyclebreak) continue;
  114. thislink.possi= possi;
  115. if (foundcyclebroken(&thislink, sofar, possi->ed,possi))
  116. return true;
  117. /* Right, now we try all the providers ... */
  118. for (providelink= possi->ed->installed.depended;
  119. providelink;
  120. providelink = providelink->rev_next) {
  121. if (providelink->up->type != dep_provides) continue;
  122. provider= providelink->up->up;
  123. if (provider->clientdata->istobe == itb_normal) continue;
  124. /* We don't break things at ‘provides’ links, so ‘possi’ is
  125. * still the one we use. */
  126. if (foundcyclebroken(&thislink, sofar, provider, possi))
  127. return true;
  128. }
  129. }
  130. }
  131. /* Nope, we didn't find a cycle to break. */
  132. pkg->clientdata->color = black;
  133. return false;
  134. }
  135. bool
  136. findbreakcycle(struct pkginfo *pkg)
  137. {
  138. struct pkgiterator *iter;
  139. struct pkginfo *tpkg;
  140. /* Clear the visited flag of all packages before we traverse them. */
  141. iter = pkg_db_iter_new();
  142. while ((tpkg = pkg_db_iter_next(iter))) {
  143. tpkg->clientdata->color = white;
  144. }
  145. pkg_db_iter_free(iter);
  146. return findbreakcyclerecursive(pkg, NULL);
  147. }
  148. void describedepcon(struct varbuf *addto, struct dependency *dep) {
  149. const char *fmt;
  150. struct varbuf depstr = VARBUF_INIT;
  151. switch (dep->type) {
  152. case dep_depends:
  153. fmt = _("%s depends on %s");
  154. break;
  155. case dep_predepends:
  156. fmt = _("%s pre-depends on %s");
  157. break;
  158. case dep_recommends:
  159. fmt = _("%s recommends %s");
  160. break;
  161. case dep_suggests:
  162. fmt = _("%s suggests %s");
  163. break;
  164. case dep_breaks:
  165. fmt = _("%s breaks %s");
  166. break;
  167. case dep_conflicts:
  168. fmt = _("%s conflicts with %s");
  169. break;
  170. case dep_enhances:
  171. fmt = _("%s enhances %s");
  172. break;
  173. default:
  174. internerr("unknown deptype '%d'", dep->type);
  175. }
  176. varbufdependency(&depstr, dep);
  177. varbuf_add_char(&depstr, '\0');
  178. varbuf_printf(addto, fmt, dep->up->name, depstr.buf);
  179. varbuf_destroy(&depstr);
  180. }
  181. /*
  182. * *whynot must already have been initialized; it need not be
  183. * empty though - it will be reset before use.
  184. *
  185. * If depisok returns false for ‘not OK’ it will contain a description,
  186. * newline-terminated BUT NOT NUL-TERMINATED, of the reason.
  187. *
  188. * If depisok returns true it will contain garbage.
  189. * allowunconfigd should be non-zero during the ‘Pre-Depends’ checking
  190. * before a package is unpacked, when it is sufficient for the package
  191. * to be unpacked provided that both the unpacked and previously-configured
  192. * versions are acceptable.
  193. *
  194. * On false return (‘not OK’), *canfixbyremove refers to a package which
  195. * if removed (dep_conflicts) or deconfigured (dep_breaks) will fix
  196. * the problem. Caller may pass NULL for canfixbyremove and need not
  197. * initialize *canfixbyremove.
  198. */
  199. bool
  200. depisok(struct dependency *dep, struct varbuf *whynot,
  201. struct pkginfo **canfixbyremove, bool allowunconfigd)
  202. {
  203. struct deppossi *possi;
  204. struct deppossi *provider;
  205. int nconflicts;
  206. /* Use this buffer so that when internationalisation comes along we
  207. * don't have to rewrite the code completely, only redo the sprintf strings
  208. * (assuming we have the fancy argument-number-specifiers).
  209. * Allow 250x3 for package names, versions, &c, + 250 for ourselves. */
  210. char linebuf[1024];
  211. assert(dep->type == dep_depends || dep->type == dep_predepends ||
  212. dep->type == dep_breaks || dep->type == dep_conflicts ||
  213. dep->type == dep_recommends || dep->type == dep_suggests ||
  214. dep->type == dep_enhances);
  215. if (canfixbyremove)
  216. *canfixbyremove = NULL;
  217. /* The dependency is always OK if we're trying to remove the depend*ing*
  218. * package. */
  219. switch (dep->up->clientdata->istobe) {
  220. case itb_remove: case itb_deconfigure:
  221. return true;
  222. case itb_normal:
  223. /* Only installed packages can be make dependency problems. */
  224. switch (dep->up->status) {
  225. case stat_installed:
  226. case stat_triggerspending:
  227. case stat_triggersawaited:
  228. break;
  229. case stat_notinstalled: case stat_configfiles: case stat_halfinstalled:
  230. case stat_halfconfigured: case stat_unpacked:
  231. return true;
  232. default:
  233. internerr("unknown status depending '%d'", dep->up->status);
  234. }
  235. break;
  236. case itb_installnew: case itb_preinstall:
  237. break;
  238. default:
  239. internerr("unknown istobe depending '%d'", dep->up->clientdata->istobe);
  240. }
  241. /* Describe the dependency, in case we have to moan about it. */
  242. varbuf_reset(whynot);
  243. varbuf_add_char(whynot, ' ');
  244. describedepcon(whynot, dep);
  245. varbuf_add_char(whynot, '\n');
  246. /* TODO: Check dep_enhances as well. */
  247. if (dep->type == dep_depends || dep->type == dep_predepends ||
  248. dep->type == dep_recommends || dep->type == dep_suggests ) {
  249. /* Go through the alternatives. As soon as we find one that
  250. * we like, we return ‘true’ straight away. Otherwise, when we get to
  251. * the end we'll have accumulated all the reasons in whynot and
  252. * can return ‘false’. */
  253. for (possi= dep->list; possi; possi= possi->next) {
  254. switch (possi->ed->clientdata->istobe) {
  255. case itb_remove:
  256. sprintf(linebuf,_(" %.250s is to be removed.\n"),possi->ed->name);
  257. break;
  258. case itb_deconfigure:
  259. sprintf(linebuf,_(" %.250s is to be deconfigured.\n"),possi->ed->name);
  260. break;
  261. case itb_installnew:
  262. if (versionsatisfied(&possi->ed->available, possi))
  263. return true;
  264. sprintf(linebuf,_(" %.250s is to be installed, but is version %.250s.\n"),
  265. possi->ed->name,
  266. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  267. break;
  268. case itb_normal: case itb_preinstall:
  269. switch (possi->ed->status) {
  270. case stat_installed:
  271. case stat_triggerspending:
  272. if (versionsatisfied(&possi->ed->installed, possi))
  273. return true;
  274. sprintf(linebuf,_(" %.250s is installed, but is version %.250s.\n"),
  275. possi->ed->name,
  276. versiondescribe(&possi->ed->installed.version,vdew_nonambig));
  277. break;
  278. case stat_notinstalled:
  279. /* Don't say anything about this yet - it might be a virtual package.
  280. * Later on, if nothing has put anything in linebuf, we know that it
  281. * isn't and issue a diagnostic then. */
  282. *linebuf = '\0';
  283. break;
  284. case stat_unpacked:
  285. case stat_halfconfigured:
  286. case stat_triggersawaited:
  287. if (allowunconfigd) {
  288. if (!informativeversion(&possi->ed->configversion)) {
  289. sprintf(linebuf, _(" %.250s is unpacked, but has never been configured.\n"),
  290. possi->ed->name);
  291. break;
  292. } else if (!versionsatisfied(&possi->ed->installed, possi)) {
  293. sprintf(linebuf, _(" %.250s is unpacked, but is version %.250s.\n"),
  294. possi->ed->name,
  295. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  296. break;
  297. } else if (!versionsatisfied3(&possi->ed->configversion,
  298. &possi->version,possi->verrel)) {
  299. sprintf(linebuf, _(" %.250s latest configured version is %.250s.\n"),
  300. possi->ed->name,
  301. versiondescribe(&possi->ed->configversion,vdew_nonambig));
  302. break;
  303. } else {
  304. return true;
  305. }
  306. }
  307. /* Fall through. */
  308. default:
  309. sprintf(linebuf, _(" %.250s is %s.\n"),
  310. possi->ed->name, gettext(statusstrings[possi->ed->status]));
  311. break;
  312. }
  313. break;
  314. default:
  315. internerr("unknown istobe depended '%d'", possi->ed->clientdata->istobe);
  316. }
  317. varbuf_add_str(whynot, linebuf);
  318. /* If there was no version specified we try looking for Providers. */
  319. if (possi->verrel == dvr_none) {
  320. /* See if the package we're about to install Provides it. */
  321. for (provider= possi->ed->available.depended;
  322. provider;
  323. provider = provider->rev_next) {
  324. if (provider->up->type != dep_provides) continue;
  325. if (provider->up->up->clientdata->istobe == itb_installnew)
  326. return true;
  327. }
  328. /* Now look at the packages already on the system. */
  329. for (provider= possi->ed->installed.depended;
  330. provider;
  331. provider = provider->rev_next) {
  332. if (provider->up->type != dep_provides) continue;
  333. switch (provider->up->up->clientdata->istobe) {
  334. case itb_installnew:
  335. /* Don't pay any attention to the Provides field of the
  336. * currently-installed version of the package we're trying
  337. * to install. We dealt with that by using the available
  338. * information above. */
  339. continue;
  340. case itb_remove:
  341. sprintf(linebuf, _(" %.250s provides %.250s but is to be removed.\n"),
  342. provider->up->up->name, possi->ed->name);
  343. break;
  344. case itb_deconfigure:
  345. sprintf(linebuf, _(" %.250s provides %.250s but is to be deconfigured.\n"),
  346. provider->up->up->name, possi->ed->name);
  347. break;
  348. case itb_normal: case itb_preinstall:
  349. if (provider->up->up->status == stat_installed)
  350. return true;
  351. sprintf(linebuf, _(" %.250s provides %.250s but is %s.\n"),
  352. provider->up->up->name, possi->ed->name,
  353. gettext(statusstrings[provider->up->up->status]));
  354. break;
  355. default:
  356. internerr("unknown istobe provider '%d'",
  357. provider->up->up->clientdata->istobe);
  358. }
  359. varbuf_add_str(whynot, linebuf);
  360. }
  361. if (!*linebuf) {
  362. /* If the package wasn't installed at all, and we haven't said
  363. * yet why this isn't satisfied, we should say so now. */
  364. sprintf(linebuf, _(" %.250s is not installed.\n"), possi->ed->name);
  365. varbuf_add_str(whynot, linebuf);
  366. }
  367. }
  368. }
  369. return false;
  370. } else {
  371. /* It's conflicts or breaks. There's only one main alternative,
  372. * but we also have to consider Providers. We return ‘false’ as soon
  373. * as we find something that matches the conflict, and only describe
  374. * it then. If we get to the end without finding anything we return
  375. * ‘true’. */
  376. possi= dep->list;
  377. nconflicts= 0;
  378. if (possi->ed != possi->up->up) {
  379. /* If the package conflicts with or breaks itself it must mean
  380. * other packages which provide the same virtual name. We
  381. * therefore don't look at the real package and go on to the
  382. * virtual ones. */
  383. switch (possi->ed->clientdata->istobe) {
  384. case itb_remove:
  385. break;
  386. case itb_installnew:
  387. if (!versionsatisfied(&possi->ed->available, possi)) break;
  388. sprintf(linebuf, _(" %.250s (version %.250s) is to be installed.\n"),
  389. possi->ed->name,
  390. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  391. varbuf_add_str(whynot, linebuf);
  392. if (!canfixbyremove)
  393. return false;
  394. nconflicts++;
  395. *canfixbyremove= possi->ed;
  396. break;
  397. case itb_deconfigure:
  398. if (dep->type == dep_breaks)
  399. break; /* Already deconfiguring this. */
  400. /* Fall through. */
  401. case itb_normal: case itb_preinstall:
  402. switch (possi->ed->status) {
  403. case stat_notinstalled: case stat_configfiles:
  404. break;
  405. case stat_halfinstalled: case stat_unpacked:
  406. case stat_halfconfigured:
  407. if (dep->type == dep_breaks)
  408. break; /* No problem. */
  409. case stat_installed:
  410. case stat_triggerspending:
  411. case stat_triggersawaited:
  412. if (!versionsatisfied(&possi->ed->installed, possi)) break;
  413. sprintf(linebuf, _(" %.250s (version %.250s) is present and %s.\n"),
  414. possi->ed->name,
  415. versiondescribe(&possi->ed->installed.version,vdew_nonambig),
  416. gettext(statusstrings[possi->ed->status]));
  417. varbuf_add_str(whynot, linebuf);
  418. if (!canfixbyremove)
  419. return false;
  420. nconflicts++;
  421. *canfixbyremove= possi->ed;
  422. }
  423. break;
  424. default:
  425. internerr("unknown istobe conflict '%d'",
  426. possi->ed->clientdata->istobe);
  427. }
  428. }
  429. /* If there was no version specified we try looking for Providers. */
  430. if (possi->verrel == dvr_none) {
  431. /* See if the package we're about to install Provides it. */
  432. for (provider= possi->ed->available.depended;
  433. provider;
  434. provider = provider->rev_next) {
  435. if (provider->up->type != dep_provides) continue;
  436. if (provider->up->up->clientdata->istobe != itb_installnew) continue;
  437. if (provider->up->up == dep->up)
  438. continue; /* Conflicts and provides the same. */
  439. sprintf(linebuf, _(" %.250s provides %.250s and is to be installed.\n"),
  440. provider->up->up->name, possi->ed->name);
  441. varbuf_add_str(whynot, linebuf);
  442. /* We can't remove the one we're about to install: */
  443. if (canfixbyremove)
  444. *canfixbyremove = NULL;
  445. return false;
  446. }
  447. /* Now look at the packages already on the system. */
  448. for (provider= possi->ed->installed.depended;
  449. provider;
  450. provider = provider->rev_next) {
  451. if (provider->up->type != dep_provides) continue;
  452. if (provider->up->up == dep->up)
  453. continue; /* Conflicts and provides the same. */
  454. switch (provider->up->up->clientdata->istobe) {
  455. case itb_installnew:
  456. /* Don't pay any attention to the Provides field of the
  457. * currently-installed version of the package we're trying
  458. * to install. We dealt with that package by using the
  459. * available information above. */
  460. continue;
  461. case itb_remove:
  462. continue;
  463. case itb_deconfigure:
  464. if (dep->type == dep_breaks)
  465. continue; /* Already deconfiguring. */
  466. case itb_normal: case itb_preinstall:
  467. switch (provider->up->up->status) {
  468. case stat_notinstalled: case stat_configfiles:
  469. continue;
  470. case stat_halfinstalled: case stat_unpacked:
  471. case stat_halfconfigured:
  472. if (dep->type == dep_breaks)
  473. break; /* No problem. */
  474. case stat_installed:
  475. case stat_triggerspending:
  476. case stat_triggersawaited:
  477. sprintf(linebuf,
  478. _(" %.250s provides %.250s and is present and %s.\n"),
  479. provider->up->up->name, possi->ed->name,
  480. gettext(statusstrings[provider->up->up->status]));
  481. varbuf_add_str(whynot, linebuf);
  482. if (!canfixbyremove)
  483. return false;
  484. nconflicts++;
  485. *canfixbyremove= provider->up->up;
  486. break;
  487. }
  488. break;
  489. default:
  490. internerr("unknown istobe conflict provider '%d'",
  491. provider->up->up->clientdata->istobe);
  492. }
  493. }
  494. }
  495. if (!nconflicts)
  496. return true;
  497. if (nconflicts > 1)
  498. *canfixbyremove = NULL;
  499. return false;
  500. } /* if (dependency) {...} else {...} */
  501. }