enquiry.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * dpkg - main program for package management
  3. * enquiry.c - status enquiry and listing options
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006,2008-2012 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /* FIXME: per-package audit. */
  24. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/stat.h>
  29. #include <sys/termios.h>
  30. #include <assert.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <stdbool.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/arch.h>
  41. #include <dpkg/pkg-show.h>
  42. #include <dpkg/options.h>
  43. #include "filesdb.h"
  44. #include "main.h"
  45. struct badstatinfo {
  46. bool (*yesno)(struct pkginfo *, const struct badstatinfo *bsi);
  47. int value;
  48. const char *explanation;
  49. };
  50. static bool
  51. bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi)
  52. {
  53. return pkg->eflag & eflag_reinstreq;
  54. }
  55. static bool
  56. bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi)
  57. {
  58. if (pkg->eflag & eflag_reinstreq)
  59. return false;
  60. return (int)pkg->status == bsi->value;
  61. }
  62. static bool
  63. bsyn_arch(struct pkginfo *pkg, const struct badstatinfo *bsi)
  64. {
  65. if (pkg->status < stat_halfinstalled)
  66. return false;
  67. return pkg->installed.arch->type == (enum dpkg_arch_type)bsi->value;
  68. }
  69. static const struct badstatinfo badstatinfos[]= {
  70. {
  71. .yesno = bsyn_reinstreq,
  72. .value = 0,
  73. .explanation = N_(
  74. "The following packages are in a mess due to serious problems during\n"
  75. "installation. They must be reinstalled for them (and any packages\n"
  76. "that depend on them) to function properly:\n")
  77. }, {
  78. .yesno = bsyn_status,
  79. .value = stat_unpacked,
  80. .explanation = N_(
  81. "The following packages have been unpacked but not yet configured.\n"
  82. "They must be configured using dpkg --configure or the configure\n"
  83. "menu option in dselect for them to work:\n")
  84. }, {
  85. .yesno = bsyn_status,
  86. .value = stat_halfconfigured,
  87. .explanation = N_(
  88. "The following packages are only half configured, probably due to problems\n"
  89. "configuring them the first time. The configuration should be retried using\n"
  90. "dpkg --configure <package> or the configure menu option in dselect:\n")
  91. }, {
  92. .yesno = bsyn_status,
  93. .value = stat_halfinstalled,
  94. .explanation = N_(
  95. "The following packages are only half installed, due to problems during\n"
  96. "installation. The installation can probably be completed by retrying it;\n"
  97. "the packages can be removed using dselect or dpkg --remove:\n")
  98. }, {
  99. .yesno = bsyn_status,
  100. .value = stat_triggersawaited,
  101. .explanation = N_(
  102. "The following packages are awaiting processing of triggers that they\n"
  103. "have activated in other packages. This processing can be requested using\n"
  104. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  105. }, {
  106. .yesno = bsyn_status,
  107. .value = stat_triggerspending,
  108. .explanation = N_(
  109. "The following packages have been triggered, but the trigger processing\n"
  110. "has not yet been done. Trigger processing can be requested using\n"
  111. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  112. }, {
  113. .yesno = bsyn_arch,
  114. .value = arch_none,
  115. .explanation = N_("The following packages do not have an architecture:\n")
  116. }, {
  117. .yesno = bsyn_arch,
  118. .value = arch_illegal,
  119. .explanation = N_("The following packages have an illegal architecture:\n")
  120. }, {
  121. .yesno = bsyn_arch,
  122. .value = arch_unknown,
  123. .explanation = N_(
  124. "The following packages have an unknown foreign architecture, which will\n"
  125. "cause dependency issues on front-ends. This can be fixed by registering\n"
  126. "the foreign architecture with dpkg --add-architecture:\n")
  127. }, {
  128. .yesno = NULL
  129. }
  130. };
  131. static void describebriefly(struct pkginfo *pkg) {
  132. int maxl, l;
  133. const char *pdesc;
  134. maxl= 57;
  135. l= strlen(pkg->set->name);
  136. if (l>20) maxl -= (l-20);
  137. pdesc = pkg_summary(pkg, &pkg->installed, &l);
  138. l = min(l, maxl);
  139. printf(" %-20s %.*s\n", pkg_name(pkg, pnaw_nonambig), l, pdesc);
  140. }
  141. int
  142. audit(const char *const *argv)
  143. {
  144. const struct badstatinfo *bsi;
  145. bool head_running = false;
  146. if (*argv)
  147. badusage(_("--%s takes no arguments"), cipaction->olong);
  148. modstatdb_open(msdbrw_readonly);
  149. for (bsi= badstatinfos; bsi->yesno; bsi++) {
  150. struct pkgiterator *it;
  151. struct pkginfo *pkg;
  152. bool head = false;
  153. it = pkg_db_iter_new();
  154. while ((pkg = pkg_db_iter_next_pkg(it))) {
  155. if (!bsi->yesno(pkg,bsi)) continue;
  156. if (!head_running) {
  157. if (modstatdb_is_locked())
  158. puts(_(
  159. "Another process has locked the database for writing, and might currently be\n"
  160. "modifying it, some of the following problems might just be due to that.\n"));
  161. head_running = true;
  162. }
  163. if (!head) {
  164. fputs(gettext(bsi->explanation),stdout);
  165. head = true;
  166. }
  167. describebriefly(pkg);
  168. }
  169. pkg_db_iter_free(it);
  170. if (head) putchar('\n');
  171. }
  172. m_output(stdout, _("<standard output>"));
  173. return 0;
  174. }
  175. struct sectionentry {
  176. struct sectionentry *next;
  177. const char *name;
  178. int count;
  179. };
  180. static bool
  181. yettobeunpacked(struct pkginfo *pkg, const char **thissect)
  182. {
  183. if (pkg->want != want_install)
  184. return false;
  185. switch (pkg->status) {
  186. case stat_unpacked: case stat_installed: case stat_halfconfigured:
  187. case stat_triggerspending:
  188. case stat_triggersawaited:
  189. return false;
  190. case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
  191. if (thissect)
  192. *thissect = pkg->section && *pkg->section ? pkg->section :
  193. C_("section", "<unknown>");
  194. return true;
  195. default:
  196. internerr("unknown package status '%d'", pkg->status);
  197. }
  198. return false;
  199. }
  200. int
  201. unpackchk(const char *const *argv)
  202. {
  203. int totalcount, sects;
  204. struct sectionentry *sectionentries, *se, **sep;
  205. struct pkgiterator *it;
  206. struct pkginfo *pkg;
  207. const char *thissect;
  208. char buf[20];
  209. int width;
  210. if (*argv)
  211. badusage(_("--%s takes no arguments"), cipaction->olong);
  212. modstatdb_open(msdbrw_readonly);
  213. totalcount= 0;
  214. sectionentries = NULL;
  215. sects= 0;
  216. it = pkg_db_iter_new();
  217. while ((pkg = pkg_db_iter_next_pkg(it))) {
  218. if (!yettobeunpacked(pkg, &thissect)) continue;
  219. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  220. if (!se) {
  221. se= nfmalloc(sizeof(struct sectionentry));
  222. for (sep= &sectionentries;
  223. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  224. sep= &(*sep)->next);
  225. se->name= thissect;
  226. se->count= 0;
  227. se->next= *sep;
  228. *sep= se;
  229. sects++;
  230. }
  231. se->count++; totalcount++;
  232. }
  233. pkg_db_iter_free(it);
  234. if (totalcount == 0)
  235. return 0;
  236. if (totalcount <= 12) {
  237. it = pkg_db_iter_new();
  238. while ((pkg = pkg_db_iter_next_pkg(it))) {
  239. if (!yettobeunpacked(pkg, NULL))
  240. continue;
  241. describebriefly(pkg);
  242. }
  243. pkg_db_iter_free(it);
  244. } else if (sects <= 12) {
  245. for (se= sectionentries; se; se= se->next) {
  246. sprintf(buf,"%d",se->count);
  247. printf(_(" %d in %s: "),se->count,se->name);
  248. width= 70-strlen(se->name)-strlen(buf);
  249. while (width > 59) { putchar(' '); width--; }
  250. it = pkg_db_iter_new();
  251. while ((pkg = pkg_db_iter_next_pkg(it))) {
  252. const char *pkgname;
  253. if (!yettobeunpacked(pkg,&thissect)) continue;
  254. if (strcasecmp(thissect,se->name)) continue;
  255. pkgname = pkg_name(pkg, pnaw_nonambig);
  256. width -= strlen(pkgname);
  257. width--;
  258. if (width < 4) { printf(" ..."); break; }
  259. printf(" %s", pkgname);
  260. }
  261. pkg_db_iter_free(it);
  262. putchar('\n');
  263. }
  264. } else {
  265. printf(P_(" %d package, from the following section:",
  266. " %d packages, from the following sections:", totalcount),
  267. totalcount);
  268. width= 0;
  269. for (se= sectionentries; se; se= se->next) {
  270. sprintf(buf,"%d",se->count);
  271. width -= (6 + strlen(se->name) + strlen(buf));
  272. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  273. printf(" %s (%d)",se->name,se->count);
  274. }
  275. putchar('\n');
  276. }
  277. m_output(stdout, _("<standard output>"));
  278. return 0;
  279. }
  280. static int
  281. assert_version_support(const char *const *argv,
  282. struct versionrevision *version,
  283. const char *feature_name)
  284. {
  285. struct pkginfo *pkg;
  286. if (*argv)
  287. badusage(_("--%s takes no arguments"), cipaction->olong);
  288. modstatdb_open(msdbrw_readonly);
  289. pkg = pkg_db_find_singleton("dpkg");
  290. switch (pkg->status) {
  291. case stat_installed:
  292. case stat_triggerspending:
  293. return 0;
  294. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  295. case stat_triggersawaited:
  296. if (versionsatisfied3(&pkg->configversion, version, dvr_laterequal))
  297. return 0;
  298. printf(_("Version of dpkg with working %s support not yet configured.\n"
  299. " Please use 'dpkg --configure dpkg', and then try again.\n"),
  300. feature_name);
  301. return 1;
  302. default:
  303. printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
  304. feature_name);
  305. return 1;
  306. }
  307. }
  308. int
  309. assertpredep(const char *const *argv)
  310. {
  311. struct versionrevision version = { 0, "1.1.0", NULL };
  312. return assert_version_support(argv, &version, _("Pre-Depends field"));
  313. }
  314. int
  315. assertepoch(const char *const *argv)
  316. {
  317. struct versionrevision version = { 0, "1.4.0.7", NULL };
  318. return assert_version_support(argv, &version, _("epoch"));
  319. }
  320. int
  321. assertlongfilenames(const char *const *argv)
  322. {
  323. struct versionrevision version = { 0, "1.4.1.17", NULL };
  324. return assert_version_support(argv, &version, _("long filenames"));
  325. }
  326. int
  327. assertmulticonrep(const char *const *argv)
  328. {
  329. struct versionrevision version = { 0, "1.4.1.19", NULL };
  330. return assert_version_support(argv, &version,
  331. _("multiple Conflicts and Replaces"));
  332. }
  333. int
  334. assertmultiarch(const char *const *argv)
  335. {
  336. struct versionrevision version = { 0, "1.16.2", NULL };
  337. return assert_version_support(argv, &version, _("multi-arch"));
  338. }
  339. /**
  340. * Print a single package which:
  341. * (a) is the target of one or more relevant predependencies.
  342. * (b) has itself no unsatisfied pre-dependencies.
  343. *
  344. * If such a package is present output is the Packages file entry,
  345. * which can be massaged as appropriate.
  346. *
  347. * Exit status:
  348. * 0 = a package printed, OK
  349. * 1 = no suitable package available
  350. * 2 = error
  351. */
  352. int
  353. predeppackage(const char *const *argv)
  354. {
  355. static struct varbuf vb;
  356. struct pkgiterator *it;
  357. struct pkginfo *pkg = NULL, *startpkg, *trypkg;
  358. struct dependency *dep;
  359. struct deppossi *possi, *provider;
  360. if (*argv)
  361. badusage(_("--%s takes no arguments"), cipaction->olong);
  362. modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
  363. /* We use clientdata->istobe to detect loops. */
  364. clear_istobes();
  365. dep = NULL;
  366. it = pkg_db_iter_new();
  367. while (!dep && (pkg = pkg_db_iter_next_pkg(it))) {
  368. /* Ignore packages user doesn't want. */
  369. if (pkg->want != want_install)
  370. continue;
  371. /* Ignore packages not available. */
  372. if (!pkg->files)
  373. continue;
  374. pkg->clientdata->istobe= itb_preinstall;
  375. for (dep= pkg->available.depends; dep; dep= dep->next) {
  376. if (dep->type != dep_predepends) continue;
  377. if (depisok(dep, &vb, NULL, NULL, true))
  378. continue;
  379. /* This will leave dep non-NULL, and so exit the loop. */
  380. break;
  381. }
  382. pkg->clientdata->istobe= itb_normal;
  383. /* If dep is NULL we go and get the next package. */
  384. }
  385. pkg_db_iter_free(it);
  386. if (!dep)
  387. return 1; /* Not found. */
  388. assert(pkg);
  389. startpkg= pkg;
  390. pkg->clientdata->istobe= itb_preinstall;
  391. /* OK, we have found an unsatisfied predependency.
  392. * Now go and find the first thing we need to install, as a first step
  393. * towards satisfying it. */
  394. do {
  395. /* We search for a package which would satisfy dep, and put it in pkg. */
  396. for (possi = dep->list, pkg = NULL;
  397. !pkg && possi;
  398. possi=possi->next) {
  399. struct deppossi_pkg_iterator *possi_iter;
  400. possi_iter = deppossi_pkg_iter_new(possi, wpb_available);
  401. while (!pkg && (trypkg = deppossi_pkg_iter_next(possi_iter))) {
  402. if (trypkg->files && trypkg->clientdata->istobe == itb_normal &&
  403. versionsatisfied(&trypkg->available, possi)) {
  404. pkg = trypkg;
  405. break;
  406. }
  407. if (possi->verrel != dvr_none)
  408. continue;
  409. for (provider = possi->ed->depended.available;
  410. !pkg && provider;
  411. provider = provider->next) {
  412. if (provider->up->type != dep_provides)
  413. continue;
  414. trypkg = provider->up->up;
  415. if (!trypkg->files)
  416. continue;
  417. if (trypkg->clientdata->istobe == itb_normal) {
  418. pkg = trypkg;
  419. break;
  420. }
  421. }
  422. }
  423. deppossi_pkg_iter_free(possi_iter);
  424. }
  425. if (!pkg) {
  426. varbuf_reset(&vb);
  427. describedepcon(&vb,dep);
  428. varbuf_end_str(&vb);
  429. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  430. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  431. pkgbin_name(dep->up, &dep->up->available, pnaw_nonambig),
  432. pkgbin_name(startpkg, &startpkg->available, pnaw_nonambig));
  433. }
  434. pkg->clientdata->istobe= itb_preinstall;
  435. for (dep= pkg->available.depends; dep; dep= dep->next) {
  436. if (dep->type != dep_predepends) continue;
  437. if (depisok(dep, &vb, NULL, NULL, true))
  438. continue;
  439. /* This will leave dep non-NULL, and so exit the loop. */
  440. break;
  441. }
  442. } while (dep);
  443. /* OK, we've found it - pkg has no unsatisfied pre-dependencies! */
  444. writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
  445. m_output(stdout, _("<standard output>"));
  446. return 0;
  447. }
  448. int
  449. printarch(const char *const *argv)
  450. {
  451. if (*argv)
  452. badusage(_("--%s takes no arguments"), cipaction->olong);
  453. printf("%s\n", dpkg_arch_get(arch_native)->name);
  454. m_output(stdout, _("<standard output>"));
  455. return 0;
  456. }
  457. int
  458. printinstarch(const char *const *argv)
  459. {
  460. warning(_("obsolete option '--%s', please use '--%s' instead."),
  461. "print-installation-architecture", "print-architecture");
  462. return printarch(argv);
  463. }
  464. int
  465. print_foreign_arches(const char *const *argv)
  466. {
  467. struct dpkg_arch *arch;
  468. if (*argv)
  469. badusage(_("--%s takes no arguments"), cipaction->olong);
  470. dpkg_arch_load_list();
  471. for (arch = dpkg_arch_get_list(); arch; arch = arch->next) {
  472. if (arch->type != arch_foreign)
  473. continue;
  474. printf("%s\n", arch->name);
  475. }
  476. m_output(stdout, _("<standard output>"));
  477. return 0;
  478. }
  479. int
  480. cmpversions(const char *const *argv)
  481. {
  482. struct relationinfo {
  483. const char *string;
  484. /* These values are exit status codes, so 0 = true, 1 = false. */
  485. int if_lesser, if_equal, if_greater;
  486. int if_none_a, if_none_both, if_none_b;
  487. };
  488. static const struct relationinfo relationinfos[]= {
  489. /* < = > !a!2!b */
  490. { "le", 0,0,1, 0,0,1 },
  491. { "lt", 0,1,1, 0,1,1 },
  492. { "eq", 1,0,1, 1,0,1 },
  493. { "ne", 0,1,0, 0,1,0 },
  494. { "ge", 1,0,0, 1,0,0 },
  495. { "gt", 1,1,0, 1,1,0 },
  496. /* These treat an empty version as later than any version. */
  497. { "le-nl", 0,0,1, 1,0,0 },
  498. { "lt-nl", 0,1,1, 1,1,0 },
  499. { "ge-nl", 1,0,0, 0,0,1 },
  500. { "gt-nl", 1,1,0, 0,1,1 },
  501. /* For compatibility with dpkg control file syntax. */
  502. { "<", 0,0,1, 0,0,1 },
  503. { "<=", 0,0,1, 0,0,1 },
  504. { "<<", 0,1,1, 0,1,1 },
  505. { "=", 1,0,1, 1,0,1 },
  506. { ">", 1,0,0, 1,0,0 },
  507. { ">=", 1,0,0, 1,0,0 },
  508. { ">>", 1,1,0, 1,1,0 },
  509. { NULL }
  510. };
  511. const struct relationinfo *rip;
  512. struct versionrevision a, b;
  513. struct dpkg_error err;
  514. int r;
  515. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  516. badusage(_("--compare-versions takes three arguments:"
  517. " <version> <relation> <version>"));
  518. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  519. if (!rip->string) badusage(_("--compare-versions bad relation"));
  520. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  521. if (parseversion(&a, argv[0], &err) < 0) {
  522. if (err.type == DPKG_MSG_WARN)
  523. warning(_("version '%s' has bad syntax: %s"), argv[0], err.str);
  524. else
  525. ohshit(_("version '%s' has bad syntax: %s"), argv[0], err.str);
  526. dpkg_error_destroy(&err);
  527. }
  528. } else {
  529. dpkg_version_blank(&a);
  530. }
  531. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  532. if (parseversion(&b, argv[2], &err) < 0) {
  533. if (err.type == DPKG_MSG_WARN)
  534. warning(_("version '%s' has bad syntax: %s"), argv[2], err.str);
  535. else
  536. ohshit(_("version '%s' has bad syntax: %s"), argv[2], err.str);
  537. dpkg_error_destroy(&err);
  538. }
  539. } else {
  540. dpkg_version_blank(&b);
  541. }
  542. if (!dpkg_version_is_informative(&a)) {
  543. if (dpkg_version_is_informative(&b))
  544. return rip->if_none_a;
  545. else
  546. return rip->if_none_both;
  547. } else if (!dpkg_version_is_informative(&b)) {
  548. return rip->if_none_b;
  549. }
  550. r= versioncompare(&a,&b);
  551. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  552. versiondescribe(&a,vdew_always),
  553. versiondescribe(&b,vdew_always),
  554. r);
  555. if (r > 0)
  556. return rip->if_greater;
  557. else if (r < 0)
  558. return rip->if_lesser;
  559. else
  560. return rip->if_equal;
  561. }