2 * dpkg - main program for package management
3 * enquiry.c - status enquiry and listing options
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>
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.
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.
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/>.
24 /* FIXME: per-package audit. */
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
32 #include <sys/termios.h>
42 #include <dpkg/i18n.h>
43 #include <dpkg/dpkg.h>
44 #include <dpkg/dpkg-db.h>
45 #include <dpkg/arch.h>
46 #include <dpkg/pkg-show.h>
47 #include <dpkg/string.h>
48 #include <dpkg/options.h>
55 bool (*yesno)(struct pkginfo *, const struct badstatinfo *bsi);
60 const char *explanation;
64 bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi)
66 return pkg->eflag & eflag_reinstreq;
70 bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi)
72 if (pkg->eflag & eflag_reinstreq)
74 return (int)pkg->status == bsi->value.number;
78 bsyn_infofile(struct pkginfo *pkg, const struct badstatinfo *bsi)
80 if (pkg->status < stat_halfinstalled)
82 return !pkg_infodb_has_file(pkg, &pkg->installed, bsi->value.string);
86 bsyn_arch(struct pkginfo *pkg, const struct badstatinfo *bsi)
88 if (pkg->status < stat_halfinstalled)
90 return pkg->installed.arch->type == (enum dpkg_arch_type)bsi->value.number;
93 static const struct badstatinfo badstatinfos[]= {
95 .yesno = bsyn_reinstreq,
98 "The following packages are in a mess due to serious problems during\n"
99 "installation. They must be reinstalled for them (and any packages\n"
100 "that depend on them) to function properly:\n")
102 .yesno = bsyn_status,
103 .value.number = stat_unpacked,
105 "The following packages have been unpacked but not yet configured.\n"
106 "They must be configured using dpkg --configure or the configure\n"
107 "menu option in dselect for them to work:\n")
109 .yesno = bsyn_status,
110 .value.number = stat_halfconfigured,
112 "The following packages are only half configured, probably due to problems\n"
113 "configuring them the first time. The configuration should be retried using\n"
114 "dpkg --configure <package> or the configure menu option in dselect:\n")
116 .yesno = bsyn_status,
117 .value.number = stat_halfinstalled,
119 "The following packages are only half installed, due to problems during\n"
120 "installation. The installation can probably be completed by retrying it;\n"
121 "the packages can be removed using dselect or dpkg --remove:\n")
123 .yesno = bsyn_status,
124 .value.number = stat_triggersawaited,
126 "The following packages are awaiting processing of triggers that they\n"
127 "have activated in other packages. This processing can be requested using\n"
128 "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
130 .yesno = bsyn_status,
131 .value.number = stat_triggerspending,
133 "The following packages have been triggered, but the trigger processing\n"
134 "has not yet been done. Trigger processing can be requested using\n"
135 "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
137 .yesno = bsyn_infofile,
138 .value.string = LISTFILE,
140 "The following packages are missing the list control file in the\n"
141 "database, they need to be reinstalled:\n")
143 .yesno = bsyn_infofile,
144 .value.string = HASHFILE,
146 "The following packages are missing the md5sums control file in the\n"
147 "database, they need to be reinstalled:\n")
150 .value.number = arch_none,
151 .explanation = N_("The following packages do not have an architecture:\n")
154 .value.number = arch_illegal,
155 .explanation = N_("The following packages have an illegal architecture:\n")
158 .value.number = arch_unknown,
160 "The following packages have an unknown foreign architecture, which will\n"
161 "cause dependency issues on front-ends. This can be fixed by registering\n"
162 "the foreign architecture with dpkg --add-architecture:\n")
168 static void describebriefly(struct pkginfo *pkg) {
173 l= strlen(pkg->set->name);
174 if (l>20) maxl -= (l-20);
176 pdesc = pkg_summary(pkg, &pkg->installed, &l);
179 printf(" %-20s %.*s\n", pkg_name(pkg, pnaw_nonambig), l, pdesc);
183 audit(const char *const *argv)
185 const struct badstatinfo *bsi;
186 bool head_running = false;
189 badusage(_("--%s takes no arguments"), cipaction->olong);
191 modstatdb_open(msdbrw_readonly);
193 for (bsi= badstatinfos; bsi->yesno; bsi++) {
194 struct pkgiterator *it;
198 it = pkg_db_iter_new();
199 while ((pkg = pkg_db_iter_next_pkg(it))) {
200 if (!bsi->yesno(pkg,bsi)) continue;
202 if (modstatdb_is_locked())
204 "Another process has locked the database for writing, and might currently be\n"
205 "modifying it, some of the following problems might just be due to that.\n"));
209 fputs(gettext(bsi->explanation),stdout);
212 describebriefly(pkg);
214 pkg_db_iter_free(it);
215 if (head) putchar('\n');
218 m_output(stdout, _("<standard output>"));
223 struct sectionentry {
224 struct sectionentry *next;
230 yettobeunpacked(struct pkginfo *pkg, const char **thissect)
232 if (pkg->want != want_install)
235 switch (pkg->status) {
236 case stat_unpacked: case stat_installed: case stat_halfconfigured:
237 case stat_triggerspending:
238 case stat_triggersawaited:
240 case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
242 *thissect = str_is_set(pkg->section) ? pkg->section :
243 C_("section", "<unknown>");
246 internerr("unknown package status '%d'", pkg->status);
252 unpackchk(const char *const *argv)
254 int totalcount, sects;
255 struct sectionentry *sectionentries, *se, **sep;
256 struct pkgiterator *it;
258 const char *thissect;
263 badusage(_("--%s takes no arguments"), cipaction->olong);
265 modstatdb_open(msdbrw_readonly);
268 sectionentries = NULL;
270 it = pkg_db_iter_new();
271 while ((pkg = pkg_db_iter_next_pkg(it))) {
272 if (!yettobeunpacked(pkg, &thissect)) continue;
273 for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
275 se= nfmalloc(sizeof(struct sectionentry));
276 for (sep= §ionentries;
277 *sep && strcasecmp(thissect,(*sep)->name) > 0;
285 se->count++; totalcount++;
287 pkg_db_iter_free(it);
292 if (totalcount <= 12) {
293 it = pkg_db_iter_new();
294 while ((pkg = pkg_db_iter_next_pkg(it))) {
295 if (!yettobeunpacked(pkg, NULL))
297 describebriefly(pkg);
299 pkg_db_iter_free(it);
300 } else if (sects <= 12) {
301 for (se= sectionentries; se; se= se->next) {
302 sprintf(buf,"%d",se->count);
303 printf(_(" %d in %s: "),se->count,se->name);
304 width= 70-strlen(se->name)-strlen(buf);
305 while (width > 59) { putchar(' '); width--; }
306 it = pkg_db_iter_new();
307 while ((pkg = pkg_db_iter_next_pkg(it))) {
310 if (!yettobeunpacked(pkg,&thissect)) continue;
311 if (strcasecmp(thissect,se->name)) continue;
312 pkgname = pkg_name(pkg, pnaw_nonambig);
313 width -= strlen(pkgname);
315 if (width < 4) { printf(" ..."); break; }
316 printf(" %s", pkgname);
318 pkg_db_iter_free(it);
322 printf(P_(" %d package, from the following section:",
323 " %d packages, from the following sections:", totalcount),
326 for (se= sectionentries; se; se= se->next) {
327 sprintf(buf,"%d",se->count);
328 width -= (6 + strlen(se->name) + strlen(buf));
329 if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
330 printf(" %s (%d)",se->name,se->count);
335 m_output(stdout, _("<standard output>"));
341 assert_version_support(const char *const *argv,
342 struct dpkg_version *version,
343 const char *feature_name)
348 badusage(_("--%s takes no arguments"), cipaction->olong);
350 modstatdb_open(msdbrw_readonly);
352 pkg = pkg_db_find_singleton("dpkg");
353 switch (pkg->status) {
355 case stat_triggerspending:
357 case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
358 case stat_triggersawaited:
359 if (dpkg_version_relate(&pkg->configversion, dpkg_relation_ge, version))
361 printf(_("Version of dpkg with working %s support not yet configured.\n"
362 " Please use 'dpkg --configure dpkg', and then try again.\n"),
366 printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
373 assertpredep(const char *const *argv)
375 struct dpkg_version version = { 0, "1.1.0", NULL };
377 return assert_version_support(argv, &version, _("Pre-Depends field"));
381 assertepoch(const char *const *argv)
383 struct dpkg_version version = { 0, "1.4.0.7", NULL };
385 return assert_version_support(argv, &version, _("epoch"));
389 assertlongfilenames(const char *const *argv)
391 struct dpkg_version version = { 0, "1.4.1.17", NULL };
393 return assert_version_support(argv, &version, _("long filenames"));
397 assertmulticonrep(const char *const *argv)
399 struct dpkg_version version = { 0, "1.4.1.19", NULL };
401 return assert_version_support(argv, &version,
402 _("multiple Conflicts and Replaces"));
406 assertmultiarch(const char *const *argv)
408 struct dpkg_version version = { 0, "1.16.2", NULL };
410 return assert_version_support(argv, &version, _("multi-arch"));
414 * Print a single package which:
415 * (a) is the target of one or more relevant predependencies.
416 * (b) has itself no unsatisfied pre-dependencies.
418 * If such a package is present output is the Packages file entry,
419 * which can be massaged as appropriate.
422 * 0 = a package printed, OK
423 * 1 = no suitable package available
427 predeppackage(const char *const *argv)
429 static struct varbuf vb;
431 struct pkgiterator *it;
432 struct pkginfo *pkg = NULL, *startpkg, *trypkg;
433 struct dependency *dep;
434 struct deppossi *possi, *provider;
437 badusage(_("--%s takes no arguments"), cipaction->olong);
439 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
440 /* We use clientdata->istobe to detect loops. */
444 it = pkg_db_iter_new();
445 while (!dep && (pkg = pkg_db_iter_next_pkg(it))) {
446 /* Ignore packages user doesn't want. */
447 if (pkg->want != want_install)
449 /* Ignore packages not available. */
452 pkg->clientdata->istobe= itb_preinstall;
453 for (dep= pkg->available.depends; dep; dep= dep->next) {
454 if (dep->type != dep_predepends) continue;
455 if (depisok(dep, &vb, NULL, NULL, true))
457 /* This will leave dep non-NULL, and so exit the loop. */
460 pkg->clientdata->istobe= itb_normal;
461 /* If dep is NULL we go and get the next package. */
463 pkg_db_iter_free(it);
466 return 1; /* Not found. */
469 pkg->clientdata->istobe= itb_preinstall;
471 /* OK, we have found an unsatisfied predependency.
472 * Now go and find the first thing we need to install, as a first step
473 * towards satisfying it. */
475 /* We search for a package which would satisfy dep, and put it in pkg. */
476 for (possi = dep->list, pkg = NULL;
479 struct deppossi_pkg_iterator *possi_iter;
481 possi_iter = deppossi_pkg_iter_new(possi, wpb_available);
482 while (!pkg && (trypkg = deppossi_pkg_iter_next(possi_iter))) {
483 if (trypkg->files && trypkg->clientdata->istobe == itb_normal &&
484 versionsatisfied(&trypkg->available, possi)) {
488 if (possi->verrel != dpkg_relation_none)
490 for (provider = possi->ed->depended.available;
492 provider = provider->next) {
493 if (provider->up->type != dep_provides)
495 trypkg = provider->up->up;
498 if (trypkg->clientdata->istobe == itb_normal) {
504 deppossi_pkg_iter_free(possi_iter);
508 describedepcon(&vb,dep);
510 notice(_("cannot see how to satisfy pre-dependency:\n %s"), vb.buf);
511 ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
512 pkgbin_name(dep->up, &dep->up->available, pnaw_nonambig),
513 pkgbin_name(startpkg, &startpkg->available, pnaw_nonambig));
515 pkg->clientdata->istobe= itb_preinstall;
516 for (dep= pkg->available.depends; dep; dep= dep->next) {
517 if (dep->type != dep_predepends) continue;
518 if (depisok(dep, &vb, NULL, NULL, true))
520 /* This will leave dep non-NULL, and so exit the loop. */
525 /* OK, we've found it - pkg has no unsatisfied pre-dependencies! */
526 writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
528 m_output(stdout, _("<standard output>"));
534 printarch(const char *const *argv)
537 badusage(_("--%s takes no arguments"), cipaction->olong);
539 printf("%s\n", dpkg_arch_get(arch_native)->name);
541 m_output(stdout, _("<standard output>"));
547 printinstarch(const char *const *argv)
549 warning(_("obsolete option '--%s'; please use '--%s' instead"),
550 "print-installation-architecture", "print-architecture");
551 return printarch(argv);
555 print_foreign_arches(const char *const *argv)
557 struct dpkg_arch *arch;
560 badusage(_("--%s takes no arguments"), cipaction->olong);
562 dpkg_arch_load_list();
564 for (arch = dpkg_arch_get_list(); arch; arch = arch->next) {
565 if (arch->type != arch_foreign)
568 printf("%s\n", arch->name);
571 m_output(stdout, _("<standard output>"));
577 cmpversions(const char *const *argv)
579 struct relationinfo {
581 /* These values are exit status codes, so 0 = true, 1 = false. */
582 int if_lesser, if_equal, if_greater;
583 int if_none_a, if_none_both, if_none_b;
586 static const struct relationinfo relationinfos[]= {
588 { "le", 0,0,1, 0,0,1 },
589 { "lt", 0,1,1, 0,1,1 },
590 { "eq", 1,0,1, 1,0,1 },
591 { "ne", 0,1,0, 0,1,0 },
592 { "ge", 1,0,0, 1,0,0 },
593 { "gt", 1,1,0, 1,1,0 },
595 /* These treat an empty version as later than any version. */
596 { "le-nl", 0,0,1, 1,0,0 },
597 { "lt-nl", 0,1,1, 1,1,0 },
598 { "ge-nl", 1,0,0, 0,0,1 },
599 { "gt-nl", 1,1,0, 0,1,1 },
601 /* For compatibility with dpkg control file syntax. */
602 { "<", 0,0,1, 0,0,1 },
603 { "<=", 0,0,1, 0,0,1 },
604 { "<<", 0,1,1, 0,1,1 },
605 { "=", 1,0,1, 1,0,1 },
606 { ">", 1,0,0, 1,0,0 },
607 { ">=", 1,0,0, 1,0,0 },
608 { ">>", 1,1,0, 1,1,0 },
612 const struct relationinfo *rip;
613 struct dpkg_version a, b;
614 struct dpkg_error err;
617 if (!argv[0] || !argv[1] || !argv[2] || argv[3])
618 badusage(_("--compare-versions takes three arguments:"
619 " <version> <relation> <version>"));
621 for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
623 if (!rip->string) badusage(_("--compare-versions bad relation"));
625 if (*argv[0] && strcmp(argv[0],"<unknown>")) {
626 if (parseversion(&a, argv[0], &err) < 0) {
627 if (err.type == DPKG_MSG_WARN)
628 warning(_("version '%s' has bad syntax: %s"), argv[0], err.str);
630 ohshit(_("version '%s' has bad syntax: %s"), argv[0], err.str);
631 dpkg_error_destroy(&err);
634 dpkg_version_blank(&a);
636 if (*argv[2] && strcmp(argv[2],"<unknown>")) {
637 if (parseversion(&b, argv[2], &err) < 0) {
638 if (err.type == DPKG_MSG_WARN)
639 warning(_("version '%s' has bad syntax: %s"), argv[2], err.str);
641 ohshit(_("version '%s' has bad syntax: %s"), argv[2], err.str);
642 dpkg_error_destroy(&err);
645 dpkg_version_blank(&b);
647 if (!dpkg_version_is_informative(&a)) {
648 if (dpkg_version_is_informative(&b))
649 return rip->if_none_a;
651 return rip->if_none_both;
652 } else if (!dpkg_version_is_informative(&b)) {
653 return rip->if_none_b;
655 r = dpkg_version_compare(&a, &b);
656 debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
657 versiondescribe(&a,vdew_always),
658 versiondescribe(&b,vdew_always),
661 return rip->if_greater;
663 return rip->if_lesser;
665 return rip->if_equal;