2 * dpkg - main program for package management
3 * enquiry.c - status enquiry and listing options
5 * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2006, 2008-2016 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 <https://www.gnu.org/licenses/>.
27 #include <sys/types.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-array.h>
42 #include <dpkg/pkg-show.h>
43 #include <dpkg/triglib.h>
44 #include <dpkg/string.h>
45 #include <dpkg/options.h>
51 struct audit_problem {
52 bool (*check)(struct pkginfo *, const struct audit_problem *problem);
57 const char *explanation;
61 audit_reinstreq(struct pkginfo *pkg, const struct audit_problem *problem)
63 return pkg->eflag & PKG_EFLAG_REINSTREQ;
67 audit_status(struct pkginfo *pkg, const struct audit_problem *problem)
69 if (pkg->eflag & PKG_EFLAG_REINSTREQ)
71 return (int)pkg->status == problem->value.number;
75 audit_infofile(struct pkginfo *pkg, const struct audit_problem *problem)
77 if (pkg->status < PKG_STAT_HALFINSTALLED)
79 return !pkg_infodb_has_file(pkg, &pkg->installed, problem->value.string);
83 audit_arch(struct pkginfo *pkg, const struct audit_problem *problem)
85 if (pkg->status < PKG_STAT_HALFINSTALLED)
87 return pkg->installed.arch->type == (enum dpkg_arch_type)problem->value.number;
90 static const struct audit_problem audit_problems[] = {
92 .check = audit_reinstreq,
95 "The following packages are in a mess due to serious problems during\n"
96 "installation. They must be reinstalled for them (and any packages\n"
97 "that depend on them) to function properly:\n")
99 .check = audit_status,
100 .value.number = PKG_STAT_UNPACKED,
102 "The following packages have been unpacked but not yet configured.\n"
103 "They must be configured using dpkg --configure or the configure\n"
104 "menu option in dselect for them to work:\n")
106 .check = audit_status,
107 .value.number = PKG_STAT_HALFCONFIGURED,
109 "The following packages are only half configured, probably due to problems\n"
110 "configuring them the first time. The configuration should be retried using\n"
111 "dpkg --configure <package> or the configure menu option in dselect:\n")
113 .check = audit_status,
114 .value.number = PKG_STAT_HALFINSTALLED,
116 "The following packages are only half installed, due to problems during\n"
117 "installation. The installation can probably be completed by retrying it;\n"
118 "the packages can be removed using dselect or dpkg --remove:\n")
120 .check = audit_status,
121 .value.number = PKG_STAT_TRIGGERSAWAITED,
123 "The following packages are awaiting processing of triggers that they\n"
124 "have activated in other packages. This processing can be requested using\n"
125 "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
127 .check = audit_status,
128 .value.number = PKG_STAT_TRIGGERSPENDING,
130 "The following packages have been triggered, but the trigger processing\n"
131 "has not yet been done. Trigger processing can be requested using\n"
132 "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
134 .check = audit_infofile,
135 .value.string = LISTFILE,
137 "The following packages are missing the list control file in the\n"
138 "database, they need to be reinstalled:\n")
140 .check = audit_infofile,
141 .value.string = HASHFILE,
143 "The following packages are missing the md5sums control file in the\n"
144 "database, they need to be reinstalled:\n")
147 .value.number = DPKG_ARCH_EMPTY,
148 .explanation = N_("The following packages do not have an architecture:\n")
151 .value.number = DPKG_ARCH_ILLEGAL,
152 .explanation = N_("The following packages have an illegal architecture:\n")
155 .value.number = DPKG_ARCH_UNKNOWN,
157 "The following packages have an unknown foreign architecture, which will\n"
158 "cause dependency issues on front-ends. This can be fixed by registering\n"
159 "the foreign architecture with dpkg --add-architecture:\n")
165 static void describebriefly(struct pkginfo *pkg) {
170 l= strlen(pkg->set->name);
171 if (l>20) maxl -= (l-20);
173 pdesc = pkgbin_summary(pkg, &pkg->installed, &l);
176 printf(" %-20s %.*s\n", pkg_name(pkg, pnaw_nonambig), l, pdesc);
179 static struct pkginfo *
180 pkg_array_mapper(const char *name)
184 pkg = dpkg_options_parse_pkgname(cipaction, name);
185 if (pkg->status == PKG_STAT_NOTINSTALLED)
186 notice(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
192 audit(const char *const *argv)
194 const struct audit_problem *problem;
195 struct pkg_array array;
196 bool head_running = false;
199 modstatdb_open(msdbrw_readonly);
202 pkg_array_init_from_db(&array);
204 pkg_array_init_from_names(&array, pkg_array_mapper, (const char **)argv);
206 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
208 for (problem = audit_problems; problem->check; problem++) {
211 for (i = 0; i < array.n_pkgs; i++) {
212 struct pkginfo *pkg = array.pkgs[i];
214 if (!problem->check(pkg, problem))
217 if (modstatdb_is_locked())
219 "Another process has locked the database for writing, and might currently be\n"
220 "modifying it, some of the following problems might just be due to that.\n"));
224 fputs(gettext(problem->explanation), stdout);
227 describebriefly(pkg);
230 if (head) putchar('\n');
233 pkg_array_destroy(&array);
235 m_output(stdout, _("<standard output>"));
240 struct sectionentry {
241 struct sectionentry *next;
247 yettobeunpacked(struct pkginfo *pkg, const char **thissect)
249 if (pkg->want != PKG_WANT_INSTALL)
252 switch (pkg->status) {
253 case PKG_STAT_UNPACKED:
254 case PKG_STAT_INSTALLED:
255 case PKG_STAT_HALFCONFIGURED:
256 case PKG_STAT_TRIGGERSPENDING:
257 case PKG_STAT_TRIGGERSAWAITED:
259 case PKG_STAT_NOTINSTALLED:
260 case PKG_STAT_HALFINSTALLED:
261 case PKG_STAT_CONFIGFILES:
263 *thissect = str_is_set(pkg->section) ? pkg->section :
264 C_("section", "<unknown>");
267 internerr("unknown package status '%d'", pkg->status);
273 unpackchk(const char *const *argv)
275 int totalcount, sects;
276 struct sectionentry *sectionentries, *se, **sep;
277 struct pkgiterator *iter;
279 const char *thissect;
284 badusage(_("--%s takes no arguments"), cipaction->olong);
286 modstatdb_open(msdbrw_readonly);
289 sectionentries = NULL;
291 iter = pkg_db_iter_new();
292 while ((pkg = pkg_db_iter_next_pkg(iter))) {
293 if (!yettobeunpacked(pkg, &thissect)) continue;
294 for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
296 se= nfmalloc(sizeof(struct sectionentry));
297 for (sep= §ionentries;
298 *sep && strcasecmp(thissect,(*sep)->name) > 0;
306 se->count++; totalcount++;
308 pkg_db_iter_free(iter);
313 if (totalcount <= 12) {
314 iter = pkg_db_iter_new();
315 while ((pkg = pkg_db_iter_next_pkg(iter))) {
316 if (!yettobeunpacked(pkg, NULL))
318 describebriefly(pkg);
320 pkg_db_iter_free(iter);
321 } else if (sects <= 12) {
322 for (se= sectionentries; se; se= se->next) {
323 sprintf(buf,"%d",se->count);
324 printf(_(" %d in %s: "),se->count,se->name);
325 width= 70-strlen(se->name)-strlen(buf);
326 while (width > 59) { putchar(' '); width--; }
327 iter = pkg_db_iter_new();
328 while ((pkg = pkg_db_iter_next_pkg(iter))) {
331 if (!yettobeunpacked(pkg,&thissect)) continue;
332 if (strcasecmp(thissect,se->name)) continue;
333 pkgname = pkg_name(pkg, pnaw_nonambig);
334 width -= strlen(pkgname);
336 if (width < 4) { printf(" ..."); break; }
337 printf(" %s", pkgname);
339 pkg_db_iter_free(iter);
343 printf(P_(" %d package, from the following section:",
344 " %d packages, from the following sections:", totalcount),
347 for (se= sectionentries; se; se= se->next) {
348 sprintf(buf,"%d",se->count);
349 width -= (6 + strlen(se->name) + strlen(buf));
350 if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
351 printf(" %s (%d)",se->name,se->count);
356 m_output(stdout, _("<standard output>"));
362 assert_version_support(const char *const *argv,
363 struct dpkg_version *version,
364 const char *feature_name)
369 badusage(_("--%s takes no arguments"), cipaction->olong);
371 modstatdb_open(msdbrw_readonly);
373 pkg = pkg_db_find_singleton("dpkg");
374 switch (pkg->status) {
375 case PKG_STAT_INSTALLED:
376 case PKG_STAT_TRIGGERSPENDING:
378 case PKG_STAT_UNPACKED:
379 case PKG_STAT_HALFCONFIGURED:
380 case PKG_STAT_HALFINSTALLED:
381 case PKG_STAT_TRIGGERSAWAITED:
382 if (dpkg_version_relate(&pkg->configversion, DPKG_RELATION_GE, version))
384 printf(_("Version of dpkg with working %s support not yet configured.\n"
385 " Please use 'dpkg --configure dpkg', and then try again.\n"),
389 printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
396 assertpredep(const char *const *argv)
398 struct dpkg_version version = { 0, "1.1.0", NULL };
400 return assert_version_support(argv, &version, _("Pre-Depends field"));
404 assertepoch(const char *const *argv)
406 struct dpkg_version version = { 0, "1.4.0.7", NULL };
408 return assert_version_support(argv, &version, _("epoch"));
412 assertlongfilenames(const char *const *argv)
414 struct dpkg_version version = { 0, "1.4.1.17", NULL };
416 return assert_version_support(argv, &version, _("long filenames"));
420 assertmulticonrep(const char *const *argv)
422 struct dpkg_version version = { 0, "1.4.1.19", NULL };
424 return assert_version_support(argv, &version,
425 _("multiple Conflicts and Replaces"));
429 assertmultiarch(const char *const *argv)
431 struct dpkg_version version = { 0, "1.16.2", NULL };
433 return assert_version_support(argv, &version, _("multi-arch"));
437 assertverprovides(const char *const *argv)
439 struct dpkg_version version = { 0, "1.17.11", NULL };
441 return assert_version_support(argv, &version, _("versioned Provides"));
445 * Print a single package which:
446 * (a) is the target of one or more relevant predependencies.
447 * (b) has itself no unsatisfied pre-dependencies.
449 * If such a package is present output is the Packages file entry,
450 * which can be massaged as appropriate.
453 * 0 = a package printed, OK
454 * 1 = no suitable package available
458 predeppackage(const char *const *argv)
460 static struct varbuf vb;
462 struct pkgiterator *iter;
463 struct pkginfo *pkg = NULL, *startpkg, *trypkg;
464 struct dependency *dep;
465 struct deppossi *possi, *provider;
468 badusage(_("--%s takes no arguments"), cipaction->olong);
470 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
471 /* We use clientdata->istobe to detect loops. */
475 iter = pkg_db_iter_new();
476 while (!dep && (pkg = pkg_db_iter_next_pkg(iter))) {
477 /* Ignore packages user doesn't want. */
478 if (pkg->want != PKG_WANT_INSTALL)
480 /* Ignore packages not available. */
483 pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
484 for (dep= pkg->available.depends; dep; dep= dep->next) {
485 if (dep->type != dep_predepends) continue;
486 if (depisok(dep, &vb, NULL, NULL, true))
488 /* This will leave dep non-NULL, and so exit the loop. */
491 pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
492 /* If dep is NULL we go and get the next package. */
494 pkg_db_iter_free(iter);
497 return 1; /* Not found. */
500 pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
502 /* OK, we have found an unsatisfied predependency.
503 * Now go and find the first thing we need to install, as a first step
504 * towards satisfying it. */
506 /* We search for a package which would satisfy dep, and put it in pkg. */
507 for (possi = dep->list, pkg = NULL;
510 struct deppossi_pkg_iterator *possi_iter;
512 possi_iter = deppossi_pkg_iter_new(possi, wpb_available);
513 while (!pkg && (trypkg = deppossi_pkg_iter_next(possi_iter))) {
515 trypkg->clientdata->istobe == PKG_ISTOBE_NORMAL &&
516 versionsatisfied(&trypkg->available, possi)) {
520 for (provider = possi->ed->depended.available;
522 provider = provider->next) {
523 if (provider->up->type != dep_provides)
525 if (!pkg_virtual_deppossi_satisfied(possi, provider))
527 trypkg = provider->up->up;
530 if (trypkg->clientdata->istobe == PKG_ISTOBE_NORMAL) {
536 deppossi_pkg_iter_free(possi_iter);
540 describedepcon(&vb,dep);
542 notice(_("cannot see how to satisfy pre-dependency:\n %s"), vb.buf);
543 ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
544 pkgbin_name(dep->up, &dep->up->available, pnaw_nonambig),
545 pkgbin_name(startpkg, &startpkg->available, pnaw_nonambig));
547 pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
548 for (dep= pkg->available.depends; dep; dep= dep->next) {
549 if (dep->type != dep_predepends) continue;
550 if (depisok(dep, &vb, NULL, NULL, true))
552 /* This will leave dep non-NULL, and so exit the loop. */
557 /* OK, we've found it - pkg has no unsatisfied pre-dependencies! */
558 writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
560 m_output(stdout, _("<standard output>"));
566 printarch(const char *const *argv)
569 badusage(_("--%s takes no arguments"), cipaction->olong);
571 printf("%s\n", dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
573 m_output(stdout, _("<standard output>"));
579 print_foreign_arches(const char *const *argv)
581 struct dpkg_arch *arch;
584 badusage(_("--%s takes no arguments"), cipaction->olong);
586 dpkg_arch_load_list();
588 for (arch = dpkg_arch_get_list(); arch; arch = arch->next) {
589 if (arch->type != DPKG_ARCH_FOREIGN)
592 printf("%s\n", arch->name);
595 m_output(stdout, _("<standard output>"));
601 validate_pkgname(const char *const *argv)
605 if (!argv[0] || argv[1])
606 badusage(_("--%s takes one <pkgname> argument"), cipaction->olong);
608 emsg = pkg_name_is_illegal(argv[0]);
610 ohshit(_("package name '%s' is invalid: %s"), argv[0], emsg);
616 validate_trigname(const char *const *argv)
620 if (!argv[0] || argv[1])
621 badusage(_("--%s takes one <trigname> argument"), cipaction->olong);
623 emsg = trig_name_is_illegal(argv[0]);
625 ohshit(_("trigger name '%s' is invalid: %s"), argv[0], emsg);
631 validate_archname(const char *const *argv)
635 if (!argv[0] || argv[1])
636 badusage(_("--%s takes one <archname> argument"), cipaction->olong);
638 emsg = dpkg_arch_name_is_illegal(argv[0]);
640 ohshit(_("architecture name '%s' is invalid: %s"), argv[0], emsg);
646 validate_version(const char *const *argv)
648 struct dpkg_version version;
649 struct dpkg_error err;
651 if (!argv[0] || argv[1])
652 badusage(_("--%s takes one <version> argument"), cipaction->olong);
654 if (parseversion(&version, argv[0], &err) < 0) {
655 dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[0]);
656 dpkg_error_destroy(&err);
665 cmpversions(const char *const *argv)
667 struct relationinfo {
669 /* These values are exit status codes, so 0 = true, 1 = false. */
670 int if_lesser, if_equal, if_greater;
671 int if_none_a, if_none_both, if_none_b;
675 static const struct relationinfo relationinfos[]= {
677 { "le", 0,0,1, 0,0,1 },
678 { "lt", 0,1,1, 0,1,1 },
679 { "eq", 1,0,1, 1,0,1 },
680 { "ne", 0,1,0, 0,1,0 },
681 { "ge", 1,0,0, 1,0,0 },
682 { "gt", 1,1,0, 1,1,0 },
684 /* These treat an empty version as later than any version. */
685 { "le-nl", 0,0,1, 1,0,0 },
686 { "lt-nl", 0,1,1, 1,1,0 },
687 { "ge-nl", 1,0,0, 0,0,1 },
688 { "gt-nl", 1,1,0, 0,1,1 },
690 /* For compatibility with dpkg control file syntax. */
691 { "<", 0,0,1, 0,0,1, .obsolete = true },
692 { "<=", 0,0,1, 0,0,1 },
693 { "<<", 0,1,1, 0,1,1 },
694 { "=", 1,0,1, 1,0,1 },
695 { ">", 1,0,0, 1,0,0, .obsolete = true },
696 { ">=", 1,0,0, 1,0,0 },
697 { ">>", 1,1,0, 1,1,0 },
701 const struct relationinfo *rip;
702 struct dpkg_version a, b;
703 struct dpkg_error err;
706 if (!argv[0] || !argv[1] || !argv[2] || argv[3])
707 badusage(_("--compare-versions takes three arguments:"
708 " <version> <relation> <version>"));
710 for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
712 if (!rip->string) badusage(_("--compare-versions bad relation"));
715 warning(_("--%s used with obsolete relation operator '%s'"),
716 cipaction->olong, rip->string);
718 if (*argv[0] && strcmp(argv[0],"<unknown>")) {
719 if (parseversion(&a, argv[0], &err) < 0) {
720 dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[0]);
721 dpkg_error_destroy(&err);
724 dpkg_version_blank(&a);
726 if (*argv[2] && strcmp(argv[2],"<unknown>")) {
727 if (parseversion(&b, argv[2], &err) < 0) {
728 dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[2]);
729 dpkg_error_destroy(&err);
732 dpkg_version_blank(&b);
734 if (!dpkg_version_is_informative(&a)) {
735 if (dpkg_version_is_informative(&b))
736 return rip->if_none_a;
738 return rip->if_none_both;
739 } else if (!dpkg_version_is_informative(&b)) {
740 return rip->if_none_b;
742 rc = dpkg_version_compare(&a, &b);
743 debug(dbg_general, "cmpversions a='%s' b='%s' r=%d",
744 versiondescribe(&a,vdew_always),
745 versiondescribe(&b,vdew_always),
748 return rip->if_greater;
750 return rip->if_lesser;
752 return rip->if_equal;