packages.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * dpkg - main program for package management
  3. * packages.c - common to actions that process packages
  4. *
  5. * Copyright (C) 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 <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <dirent.h>
  30. #include <ctype.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <myopt.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. struct pkginqueue {
  40. struct pkginqueue *next;
  41. struct pkginfo *pkg;
  42. };
  43. static struct pkginqueue *queuehead= 0, **queuetail= &queuehead;
  44. int queuelen=0, sincenothing=0, dependtry=0;
  45. void add_to_queue(struct pkginfo *pkg) {
  46. struct pkginqueue *newent;
  47. newent= m_malloc(sizeof(struct pkginqueue));
  48. newent->pkg= pkg;
  49. newent->next= 0;
  50. *queuetail= newent;
  51. queuetail= &newent->next;
  52. queuelen++;
  53. }
  54. void packages(const char *const *argv) {
  55. struct pkgiterator *it;
  56. struct pkginfo *pkg;
  57. const char *thisarg;
  58. size_t l;
  59. modstatdb_init(admindir,
  60. f_noact ? msdbrw_readonly
  61. : fc_nonroot ? msdbrw_write
  62. : msdbrw_needsuperuser);
  63. checkpath();
  64. if (f_pending) {
  65. if (*argv)
  66. badusage(_("--%s --pending does not take any non-option arguments"),cipaction->olong);
  67. it= iterpkgstart();
  68. while ((pkg= iterpkgnext(it)) != 0) {
  69. switch (cipaction->arg) {
  70. case act_configure:
  71. if (pkg->status != stat_unpacked && pkg->status != stat_halfconfigured)
  72. continue;
  73. if (pkg->want != want_install)
  74. continue;
  75. break;
  76. case act_remove:
  77. case act_purge:
  78. if (pkg->want != want_purge) {
  79. if (pkg->want != want_deinstall) continue;
  80. if (pkg->status == stat_configfiles) continue;
  81. }
  82. if (pkg->status == stat_notinstalled)
  83. continue;
  84. break;
  85. default:
  86. internerr("unknown action for pending");
  87. }
  88. add_to_queue(pkg);
  89. }
  90. iterpkgend(it);
  91. } else {
  92. if (!*argv)
  93. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  94. while ((thisarg= *argv++) != 0) {
  95. pkg= findpackage(thisarg);
  96. if (pkg->status == stat_notinstalled) {
  97. l= strlen(pkg->name);
  98. if (l >= sizeof(DEBEXT) && !strcmp(pkg->name+l-sizeof(DEBEXT)+1,DEBEXT))
  99. badusage(_("you must specify packages by their own names,"
  100. " not by quoting the names of the files they come in"));
  101. }
  102. add_to_queue(pkg);
  103. }
  104. }
  105. ensure_diversions();
  106. process_queue();
  107. modstatdb_shutdown();
  108. }
  109. void process_queue(void) {
  110. struct pkginqueue *removeent, *rundown;
  111. struct pkginfo *volatile pkg;
  112. jmp_buf ejbuf;
  113. enum istobes istobe= itb_normal;
  114. clear_istobes();
  115. switch (cipaction->arg) {
  116. case act_configure: case act_install: istobe= itb_installnew; break;
  117. case act_remove: case act_purge: istobe= itb_remove; break;
  118. default: internerr("unknown action for queue start");
  119. }
  120. for (rundown= queuehead; rundown; rundown= rundown->next) {
  121. ensure_package_clientdata(rundown->pkg);
  122. if (rundown->pkg->clientdata->istobe == istobe) {
  123. /* Remove it from the queue - this is a second copy ! */
  124. switch (cipaction->arg) {
  125. case act_configure: case act_remove: case act_purge:
  126. printf(_("Package %s listed more than once, only processing once.\n"),
  127. rundown->pkg->name);
  128. break;
  129. case act_install:
  130. printf(_("More than one copy of package %s has been unpacked\n"
  131. " in this run ! Only configuring it once.\n"),
  132. rundown->pkg->name);
  133. break;
  134. default:
  135. internerr("unknown action in duplicate");
  136. }
  137. rundown->pkg= 0;
  138. } else {
  139. rundown->pkg->clientdata->istobe= istobe;
  140. }
  141. }
  142. while (queuelen) {
  143. removeent= queuehead;
  144. assert(removeent);
  145. queuehead= queuehead->next;
  146. queuelen--;
  147. if (queuetail == &removeent->next) queuetail= &queuehead;
  148. pkg= removeent->pkg;
  149. free(removeent);
  150. if (!pkg) continue; /* duplicate, which we removed earlier */
  151. assert(pkg->status <= stat_configfiles);
  152. if (setjmp(ejbuf)) {
  153. /* give up on it from the point of view of other packages, ie reset istobe */
  154. pkg->clientdata->istobe= itb_normal;
  155. error_unwind(ehflag_bombout);
  156. if (onerr_abort > 0) break;
  157. continue;
  158. }
  159. push_error_handler(&ejbuf,print_error_perpackage,pkg->name);
  160. if (sincenothing++ > queuelen*2+2) {
  161. dependtry++; sincenothing= 0;
  162. assert(dependtry <= 4);
  163. }
  164. switch (cipaction->arg) {
  165. case act_install:
  166. /* Don't try to configure pkgs that we've just disappeared. */
  167. if (pkg->status == stat_notinstalled)
  168. break;
  169. case act_configure:
  170. deferred_configure(pkg);
  171. break;
  172. case act_remove: case act_purge:
  173. deferred_remove(pkg);
  174. break;
  175. default:
  176. internerr("unknown action in queue");
  177. }
  178. if (ferror(stdout)) werr("stdout");
  179. if (ferror(stderr)) werr("stderr");
  180. set_error_display(0,0);
  181. error_unwind(ehflag_normaltidy);
  182. }
  183. }
  184. /*** dependency processing - common to --configure and --remove ***/
  185. /*
  186. * The algorithm for deciding what to configure or remove first is as
  187. * follows:
  188. *
  189. * Loop through all packages doing a `try 1' until we've been round and
  190. * nothing has been done, then do `try 2' and `try 3' likewise.
  191. *
  192. * When configuring, in each try we check to see whether all
  193. * dependencies of this package are done. If so we do it. If some of
  194. * the dependencies aren't done yet but will be later we defer the
  195. * package, otherwise it is an error.
  196. *
  197. * When removing, in each try we check to see whether there are any
  198. * packages that would have dependencies missing if we removed this
  199. * one. If not we remove it now. If some of these packages are
  200. * themselves scheduled for removal we defer the package until they
  201. * have been done.
  202. *
  203. * The criteria for satisfying a dependency vary with the various
  204. * tries. In try 1 we treat the dependencies as absolute. In try 2 we
  205. * check break any cycles in the dependency graph involving the package
  206. * we are trying to process before trying to process the package
  207. * normally. In try 3 (which should only be reached if
  208. * --force-depends-version is set) we ignore version number clauses in
  209. * Depends lines. In try 4 (only reached if --force-depends is set) we
  210. * say "ok" regardless.
  211. *
  212. * If we are configuring and one of the packages we depend on is
  213. * awaiting configuration but wasn't specified in the argument list we
  214. * will add it to the argument list if --configure-any is specified.
  215. * In this case we note this as having "done something" so that we
  216. * don't needlessly escalate to higher levels of dependency checking
  217. * and breaking.
  218. */
  219. static int deppossi_ok_found(struct pkginfo *possdependee,
  220. struct pkginfo *requiredby,
  221. struct pkginfo *removing,
  222. struct pkginfo *providing,
  223. int *matched,
  224. struct deppossi *checkversion,
  225. int *interestingwarnings,
  226. struct varbuf *oemsgs) {
  227. int thisf;
  228. if (ignore_depends(possdependee)) {
  229. debug(dbg_depcondetail," ignoring depended package so ok and found");
  230. return 3;
  231. }
  232. thisf= 0;
  233. if (possdependee == removing) {
  234. varbufaddstr(oemsgs,_(" Package "));
  235. varbufaddstr(oemsgs,possdependee->name);
  236. if (providing) {
  237. varbufaddstr(oemsgs,_(" which provides "));
  238. varbufaddstr(oemsgs,providing->name);
  239. }
  240. varbufaddstr(oemsgs,_(" is to be removed.\n"));
  241. *matched= 1;
  242. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  243. debug(dbg_depcondetail," removing possdependee, returning %d",thisf);
  244. return thisf;
  245. }
  246. switch (possdependee->status) {
  247. case stat_installed:
  248. case stat_unpacked:
  249. case stat_halfconfigured:
  250. assert(possdependee->installed.valid);
  251. if (checkversion && !versionsatisfied(&possdependee->installed,checkversion)) {
  252. varbufaddstr(oemsgs,_(" Version of "));
  253. varbufaddstr(oemsgs,possdependee->name);
  254. varbufaddstr(oemsgs,_(" on system is "));
  255. varbufaddstr(oemsgs,versiondescribe(&possdependee->installed.version,
  256. vdew_nonambig));
  257. varbufaddstr(oemsgs,".\n");
  258. assert(checkversion->verrel != dvr_none);
  259. if (fc_depends) thisf= (dependtry >= 3) ? 2 : 1;
  260. debug(dbg_depcondetail," bad version, returning %d",thisf);
  261. (*interestingwarnings)++;
  262. return thisf;
  263. }
  264. if (possdependee->status == stat_installed) {
  265. debug(dbg_depcondetail," is installed, ok and found");
  266. return 3;
  267. }
  268. if (possdependee->clientdata &&
  269. possdependee->clientdata->istobe == itb_installnew) {
  270. debug(dbg_depcondetail," unpacked/halfconfigured, defer");
  271. return 1;
  272. } else if (!removing && fc_configureany && !skip_due_to_hold(possdependee)) {
  273. fprintf(stderr,
  274. _("dpkg: also configuring `%s' (required by `%s')\n"),
  275. possdependee->name, requiredby->name);
  276. add_to_queue(possdependee); sincenothing=0; return 1;
  277. } else {
  278. varbufaddstr(oemsgs,_(" Package "));
  279. varbufaddstr(oemsgs,possdependee->name);
  280. if (providing) {
  281. varbufaddstr(oemsgs,_(" which provides "));
  282. varbufaddstr(oemsgs,providing->name);
  283. }
  284. varbufaddstr(oemsgs,_(" is not configured yet.\n"));
  285. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  286. debug(dbg_depcondetail," not configured/able - returning %d",thisf);
  287. (*interestingwarnings)++;
  288. return thisf;
  289. }
  290. default:
  291. varbufaddstr(oemsgs,_(" Package "));
  292. varbufaddstr(oemsgs,possdependee->name);
  293. if (providing) {
  294. varbufaddstr(oemsgs,_(" which provides "));
  295. varbufaddstr(oemsgs,providing->name);
  296. }
  297. varbufaddstr(oemsgs,_(" is not installed.\n"));
  298. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  299. debug(dbg_depcondetail," not installed - returning %d",thisf);
  300. (*interestingwarnings)++;
  301. return thisf;
  302. }
  303. }
  304. int dependencies_ok(struct pkginfo *pkg, struct pkginfo *removing,
  305. struct varbuf *aemsgs) {
  306. int ok, matched, found, thisf, interestingwarnings;
  307. struct varbuf oemsgs;
  308. struct dependency *dep;
  309. struct deppossi *possi, *provider;
  310. varbufinit(&oemsgs);
  311. interestingwarnings= 0;
  312. ok= 2; /* 2=ok, 1=defer, 0=halt */
  313. debug(dbg_depcon,"checking dependencies of %s (- %s)",
  314. pkg->name, removing ? removing->name : "<none>");
  315. assert(pkg->installed.valid);
  316. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  317. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  318. debug(dbg_depcondetail," checking group ...");
  319. matched= 0; varbufreset(&oemsgs);
  320. found= 0; /* 0=none, 1=defer, 2=withwarning, 3=ok */
  321. for (possi= dep->list; found != 3 && possi; possi= possi->next) {
  322. debug(dbg_depcondetail," checking possibility -> %s",possi->ed->name);
  323. if (possi->cyclebreak) {
  324. debug(dbg_depcondetail," break cycle so ok and found");
  325. found= 3; break;
  326. }
  327. thisf= deppossi_ok_found(possi->ed,pkg,removing,0,
  328. &matched,possi,&interestingwarnings,&oemsgs);
  329. if (thisf > found) found= thisf;
  330. if (found != 3 && possi->verrel == dvr_none) {
  331. if (possi->ed->installed.valid) {
  332. for (provider= possi->ed->installed.depended;
  333. found != 3 && provider;
  334. provider= provider->nextrev) {
  335. if (provider->up->type != dep_provides) continue;
  336. debug(dbg_depcondetail," checking provider %s",provider->up->up->name);
  337. thisf= deppossi_ok_found(provider->up->up,pkg,removing,possi->ed,
  338. &matched,0,&interestingwarnings,&oemsgs);
  339. if (thisf > found) found= thisf;
  340. }
  341. }
  342. }
  343. debug(dbg_depcondetail," found %d",found);
  344. if (thisf > found) found= thisf;
  345. }
  346. debug(dbg_depcondetail," found %d matched %d",found,matched);
  347. if (removing && !matched) continue;
  348. switch (found) {
  349. case 0:
  350. ok= 0;
  351. case 2:
  352. varbufaddstr(aemsgs, " ");
  353. varbufaddstr(aemsgs, pkg->name);
  354. varbufaddstr(aemsgs, _(" depends on "));
  355. varbufdependency(aemsgs, dep);
  356. if (interestingwarnings) {
  357. /* Don't print the line about the package to be removed if
  358. * that's the only line.
  359. */
  360. varbufaddstr(aemsgs, _("; however:\n"));
  361. varbufaddc(&oemsgs, 0);
  362. varbufaddstr(aemsgs, oemsgs.buf);
  363. } else {
  364. varbufaddstr(aemsgs, ".\n");
  365. }
  366. break;
  367. case 1:
  368. if (ok>1) ok= 1;
  369. break;
  370. case 3:
  371. break;
  372. default:
  373. internerr("unknown value for found");
  374. }
  375. }
  376. if (ok == 0 && (pkg->clientdata && pkg->clientdata->istobe == itb_remove))
  377. ok= 1;
  378. varbuffree(&oemsgs);
  379. debug(dbg_depcon,"ok %d msgs >>%.*s<<", ok, (int)aemsgs->used, aemsgs->buf);
  380. return ok;
  381. }