packages.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <dirent.h>
  29. #include <ctype.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #include <config.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_configure:
  166. case act_install:
  167. deferred_configure(pkg);
  168. break;
  169. case act_remove: case act_purge:
  170. deferred_remove(pkg);
  171. break;
  172. default:
  173. internerr("unknown action in queue");
  174. }
  175. if (ferror(stdout)) werr("stdout");
  176. if (ferror(stderr)) werr("stderr");
  177. set_error_display(0,0);
  178. error_unwind(ehflag_normaltidy);
  179. }
  180. }
  181. /*** dependency processing - common to --configure and --remove ***/
  182. /*
  183. * The algorithm for deciding what to configure or remove first is as
  184. * follows:
  185. *
  186. * Loop through all packages doing a `try 1' until we've been round and
  187. * nothing has been done, then do `try 2' and `try 3' likewise.
  188. *
  189. * When configuring, in each try we check to see whether all
  190. * dependencies of this package are done. If so we do it. If some of
  191. * the dependencies aren't done yet but will be later we defer the
  192. * package, otherwise it is an error.
  193. *
  194. * When removing, in each try we check to see whether there are any
  195. * packages that would have dependencies missing if we removed this
  196. * one. If not we remove it now. If some of these packages are
  197. * themselves scheduled for removal we defer the package until they
  198. * have been done.
  199. *
  200. * The criteria for satisfying a dependency vary with the various
  201. * tries. In try 1 we treat the dependencies as absolute. In try 2 we
  202. * check break any cycles in the dependency graph involving the package
  203. * we are trying to process before trying to process the package
  204. * normally. In try 3 (which should only be reached if
  205. * --force-depends-version is set) we ignore version number clauses in
  206. * Depends lines. In try 4 (only reached if --force-depends is set) we
  207. * say "ok" regardless.
  208. *
  209. * If we are configuring and one of the packages we depend on is
  210. * awaiting configuration but wasn't specified in the argument list we
  211. * will add it to the argument list if --configure-any is specified.
  212. * In this case we note this as having "done something" so that we
  213. * don't needlessly escalate to higher levels of dependency checking
  214. * and breaking.
  215. */
  216. static int deppossi_ok_found(struct pkginfo *possdependee,
  217. struct pkginfo *requiredby,
  218. struct pkginfo *removing,
  219. struct pkginfo *providing,
  220. int *matched,
  221. struct deppossi *checkversion,
  222. int *interestingwarnings,
  223. struct varbuf *oemsgs) {
  224. int thisf;
  225. if (ignore_depends(possdependee)) {
  226. debug(dbg_depcondetail," ignoring depended package so ok and found");
  227. return 3;
  228. }
  229. thisf= 0;
  230. if (possdependee == removing) {
  231. varbufaddstr(oemsgs,_(" Package "));
  232. varbufaddstr(oemsgs,possdependee->name);
  233. if (providing) {
  234. varbufaddstr(oemsgs,_(" which provides "));
  235. varbufaddstr(oemsgs,providing->name);
  236. }
  237. varbufaddstr(oemsgs,_(" is to be removed.\n"));
  238. *matched= 1;
  239. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  240. debug(dbg_depcondetail," removing possdependee, returning %d",thisf);
  241. return thisf;
  242. }
  243. switch (possdependee->status) {
  244. case stat_installed:
  245. case stat_unpacked:
  246. case stat_halfconfigured:
  247. assert(possdependee->installed.valid);
  248. if (checkversion && !versionsatisfied(&possdependee->installed,checkversion)) {
  249. varbufaddstr(oemsgs,_(" Version of "));
  250. varbufaddstr(oemsgs,possdependee->name);
  251. varbufaddstr(oemsgs,_(" on system is "));
  252. varbufaddstr(oemsgs,versiondescribe(&possdependee->installed.version,
  253. vdew_nonambig));
  254. varbufaddstr(oemsgs,".\n");
  255. assert(checkversion->verrel != dvr_none);
  256. if (fc_depends) thisf= (dependtry >= 3) ? 2 : 1;
  257. debug(dbg_depcondetail," bad version, returning %d",thisf);
  258. (*interestingwarnings)++;
  259. return thisf;
  260. }
  261. if (possdependee->status == stat_installed) {
  262. debug(dbg_depcondetail," is installed, ok and found");
  263. return 3;
  264. }
  265. if (possdependee->clientdata &&
  266. possdependee->clientdata->istobe == itb_installnew) {
  267. debug(dbg_depcondetail," unpacked/halfconfigured, defer");
  268. return 1;
  269. } else if (!removing && fc_configureany && !skip_due_to_hold(possdependee)) {
  270. fprintf(stderr,
  271. _("dpkg: also configuring `%s' (required by `%s')\n"),
  272. possdependee->name, requiredby->name);
  273. add_to_queue(possdependee); sincenothing=0; return 1;
  274. } else {
  275. varbufaddstr(oemsgs,_(" Package "));
  276. varbufaddstr(oemsgs,possdependee->name);
  277. if (providing) {
  278. varbufaddstr(oemsgs,_(" which provides "));
  279. varbufaddstr(oemsgs,providing->name);
  280. }
  281. varbufaddstr(oemsgs,_(" is not configured yet.\n"));
  282. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  283. debug(dbg_depcondetail," not configured/able - returning %d",thisf);
  284. (*interestingwarnings)++;
  285. return thisf;
  286. }
  287. default:
  288. varbufaddstr(oemsgs,_(" Package "));
  289. varbufaddstr(oemsgs,possdependee->name);
  290. if (providing) {
  291. varbufaddstr(oemsgs,_(" which provides "));
  292. varbufaddstr(oemsgs,providing->name);
  293. }
  294. varbufaddstr(oemsgs,_(" is not installed.\n"));
  295. if (fc_depends) thisf= (dependtry >= 4) ? 2 : 1;
  296. debug(dbg_depcondetail," not installed - returning %d",thisf);
  297. (*interestingwarnings)++;
  298. return thisf;
  299. }
  300. }
  301. int dependencies_ok(struct pkginfo *pkg, struct pkginfo *removing,
  302. struct varbuf *aemsgs) {
  303. int ok, matched, found, thisf, interestingwarnings;
  304. struct varbuf oemsgs;
  305. struct dependency *dep;
  306. struct deppossi *possi, *provider;
  307. varbufinit(&oemsgs);
  308. interestingwarnings= 0;
  309. ok= 2; /* 2=ok, 1=defer, 0=halt */
  310. debug(dbg_depcon,"checking dependencies of %s (- %s)",
  311. pkg->name, removing ? removing->name : "<none>");
  312. assert(pkg->installed.valid);
  313. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  314. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  315. debug(dbg_depcondetail," checking group ...");
  316. matched= 0; varbufreset(&oemsgs);
  317. found= 0; /* 0=none, 1=defer, 2=withwarning, 3=ok */
  318. for (possi= dep->list; found != 3 && possi; possi= possi->next) {
  319. debug(dbg_depcondetail," checking possibility -> %s",possi->ed->name);
  320. if (possi->cyclebreak) {
  321. debug(dbg_depcondetail," break cycle so ok and found");
  322. found= 3; break;
  323. }
  324. thisf= deppossi_ok_found(possi->ed,pkg,removing,0,
  325. &matched,possi,&interestingwarnings,&oemsgs);
  326. if (thisf > found) found= thisf;
  327. if (found != 3 && possi->verrel == dvr_none) {
  328. if (possi->ed->installed.valid) {
  329. for (provider= possi->ed->installed.depended;
  330. found != 3 && provider;
  331. provider= provider->nextrev) {
  332. if (provider->up->type != dep_provides) continue;
  333. debug(dbg_depcondetail," checking provider %s",provider->up->up->name);
  334. thisf= deppossi_ok_found(provider->up->up,pkg,removing,possi->ed,
  335. &matched,0,&interestingwarnings,&oemsgs);
  336. if (thisf > found) found= thisf;
  337. }
  338. }
  339. }
  340. debug(dbg_depcondetail," found %d",found);
  341. if (thisf > found) found= thisf;
  342. }
  343. debug(dbg_depcondetail," found %d matched %d",found,matched);
  344. if (removing && !matched) continue;
  345. switch (found) {
  346. case 0:
  347. ok= 0;
  348. case 2:
  349. varbufaddstr(aemsgs, " ");
  350. varbufaddstr(aemsgs, pkg->name);
  351. varbufaddstr(aemsgs, _(" depends on "));
  352. varbufdependency(aemsgs, dep);
  353. if (interestingwarnings) {
  354. /* Don't print the line about the package to be removed if
  355. * that's the only line.
  356. */
  357. varbufaddstr(aemsgs, _("; however:\n"));
  358. varbufaddc(&oemsgs, 0);
  359. varbufaddstr(aemsgs, oemsgs.buf);
  360. } else {
  361. varbufaddstr(aemsgs, ".\n");
  362. }
  363. break;
  364. case 1:
  365. if (ok>1) ok= 1;
  366. break;
  367. case 3:
  368. break;
  369. default:
  370. internerr("unknown value for found");
  371. }
  372. }
  373. if (ok == 0 && (pkg->clientdata && pkg->clientdata->istobe == itb_remove))
  374. ok= 1;
  375. varbuffree(&oemsgs);
  376. debug(dbg_depcon,"ok %d msgs >>%.*s<<", ok, (int)aemsgs->used, aemsgs->buf);
  377. return ok;
  378. }