packages.c 13 KB

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