Imported Upstream version 1.16.10
[services/dpkg.git] / src / enquiry.c
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
24 /* FIXME: per-package audit. */
25
26 #include <config.h>
27 #include <compat.h>
28
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/termios.h>
33
34 #include <assert.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41
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>
49
50 #include "filesdb.h"
51 #include "infodb.h"
52 #include "main.h"
53
54 struct badstatinfo {
55   bool (*yesno)(struct pkginfo *, const struct badstatinfo *bsi);
56   union {
57     int number;
58     const char *string;
59   } value;
60   const char *explanation;
61 };
62
63 static bool
64 bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi)
65 {
66   return pkg->eflag & eflag_reinstreq;
67 }
68
69 static bool
70 bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi)
71 {
72   if (pkg->eflag & eflag_reinstreq)
73     return false;
74   return (int)pkg->status == bsi->value.number;
75 }
76
77 static bool
78 bsyn_infofile(struct pkginfo *pkg, const struct badstatinfo *bsi)
79 {
80   if (pkg->status < stat_halfinstalled)
81     return false;
82   return !pkg_infodb_has_file(pkg, &pkg->installed, bsi->value.string);
83 }
84
85 static bool
86 bsyn_arch(struct pkginfo *pkg, const struct badstatinfo *bsi)
87 {
88   if (pkg->status < stat_halfinstalled)
89     return false;
90   return pkg->installed.arch->type == (enum dpkg_arch_type)bsi->value.number;
91 }
92
93 static const struct badstatinfo badstatinfos[]= {
94   {
95     .yesno = bsyn_reinstreq,
96     .value.number = 0,
97     .explanation = N_(
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")
101   }, {
102     .yesno = bsyn_status,
103     .value.number = stat_unpacked,
104     .explanation = N_(
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")
108   }, {
109     .yesno = bsyn_status,
110     .value.number = stat_halfconfigured,
111     .explanation = N_(
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")
115   }, {
116     .yesno = bsyn_status,
117     .value.number = stat_halfinstalled,
118     .explanation = N_(
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")
122   }, {
123     .yesno = bsyn_status,
124     .value.number = stat_triggersawaited,
125     .explanation = N_(
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")
129   }, {
130     .yesno = bsyn_status,
131     .value.number = stat_triggerspending,
132     .explanation = N_(
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")
136   }, {
137     .yesno = bsyn_infofile,
138     .value.string = LISTFILE,
139     .explanation = N_(
140     "The following packages are missing the list control file in the\n"
141     "database, they need to be reinstalled:\n")
142   }, {
143     .yesno = bsyn_infofile,
144     .value.string = HASHFILE,
145     .explanation = N_(
146     "The following packages are missing the md5sums control file in the\n"
147     "database, they need to be reinstalled:\n")
148   }, {
149     .yesno = bsyn_arch,
150     .value.number = arch_none,
151     .explanation = N_("The following packages do not have an architecture:\n")
152   }, {
153     .yesno = bsyn_arch,
154     .value.number = arch_illegal,
155     .explanation = N_("The following packages have an illegal architecture:\n")
156   }, {
157     .yesno = bsyn_arch,
158     .value.number = arch_unknown,
159     .explanation = N_(
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")
163   }, {
164     .yesno = NULL
165   }
166 };
167
168 static void describebriefly(struct pkginfo *pkg) {
169   int maxl, l;
170   const char *pdesc;
171
172   maxl= 57;
173   l= strlen(pkg->set->name);
174   if (l>20) maxl -= (l-20);
175
176   pdesc = pkg_summary(pkg, &pkg->installed, &l);
177   l = min(l, maxl);
178
179   printf(" %-20s %.*s\n", pkg_name(pkg, pnaw_nonambig), l, pdesc);
180 }
181
182 int
183 audit(const char *const *argv)
184 {
185   const struct badstatinfo *bsi;
186   bool head_running = false;
187
188   if (*argv)
189     badusage(_("--%s takes no arguments"), cipaction->olong);
190
191   modstatdb_open(msdbrw_readonly);
192
193   for (bsi= badstatinfos; bsi->yesno; bsi++) {
194     struct pkgiterator *it;
195     struct pkginfo *pkg;
196     bool head = false;
197
198     it = pkg_db_iter_new();
199     while ((pkg = pkg_db_iter_next_pkg(it))) {
200       if (!bsi->yesno(pkg,bsi)) continue;
201       if (!head_running) {
202         if (modstatdb_is_locked())
203           puts(_(
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"));
206         head_running = true;
207       }
208       if (!head) {
209         fputs(gettext(bsi->explanation),stdout);
210         head = true;
211       }
212       describebriefly(pkg);
213     }
214     pkg_db_iter_free(it);
215     if (head) putchar('\n');
216   }
217
218   m_output(stdout, _("<standard output>"));
219
220   return 0;
221 }
222
223 struct sectionentry {
224   struct sectionentry *next;
225   const char *name;
226   int count;
227 };
228
229 static bool
230 yettobeunpacked(struct pkginfo *pkg, const char **thissect)
231 {
232   if (pkg->want != want_install)
233     return false;
234
235   switch (pkg->status) {
236   case stat_unpacked: case stat_installed: case stat_halfconfigured:
237   case stat_triggerspending:
238   case stat_triggersawaited:
239     return false;
240   case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
241     if (thissect)
242       *thissect = str_is_set(pkg->section) ? pkg->section :
243                                              C_("section", "<unknown>");
244     return true;
245   default:
246     internerr("unknown package status '%d'", pkg->status);
247   }
248   return false;
249 }
250
251 int
252 unpackchk(const char *const *argv)
253 {
254   int totalcount, sects;
255   struct sectionentry *sectionentries, *se, **sep;
256   struct pkgiterator *it;
257   struct pkginfo *pkg;
258   const char *thissect;
259   char buf[20];
260   int width;
261
262   if (*argv)
263     badusage(_("--%s takes no arguments"), cipaction->olong);
264
265   modstatdb_open(msdbrw_readonly);
266
267   totalcount= 0;
268   sectionentries = NULL;
269   sects= 0;
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);
274     if (!se) {
275       se= nfmalloc(sizeof(struct sectionentry));
276       for (sep= &sectionentries;
277            *sep && strcasecmp(thissect,(*sep)->name) > 0;
278            sep= &(*sep)->next);
279       se->name= thissect;
280       se->count= 0;
281       se->next= *sep;
282       *sep= se;
283       sects++;
284     }
285     se->count++; totalcount++;
286   }
287   pkg_db_iter_free(it);
288
289   if (totalcount == 0)
290     return 0;
291
292   if (totalcount <= 12) {
293     it = pkg_db_iter_new();
294     while ((pkg = pkg_db_iter_next_pkg(it))) {
295       if (!yettobeunpacked(pkg, NULL))
296         continue;
297       describebriefly(pkg);
298     }
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))) {
308         const char *pkgname;
309
310         if (!yettobeunpacked(pkg,&thissect)) continue;
311         if (strcasecmp(thissect,se->name)) continue;
312         pkgname = pkg_name(pkg, pnaw_nonambig);
313         width -= strlen(pkgname);
314         width--;
315         if (width < 4) { printf(" ..."); break; }
316         printf(" %s", pkgname);
317       }
318       pkg_db_iter_free(it);
319       putchar('\n');
320     }
321   } else {
322     printf(P_(" %d package, from the following section:",
323               " %d packages, from the following sections:", totalcount),
324            totalcount);
325     width= 0;
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);
331     }
332     putchar('\n');
333   }
334
335   m_output(stdout, _("<standard output>"));
336
337   return 0;
338 }
339
340 static int
341 assert_version_support(const char *const *argv,
342                        struct dpkg_version *version,
343                        const char *feature_name)
344 {
345   struct pkginfo *pkg;
346
347   if (*argv)
348     badusage(_("--%s takes no arguments"), cipaction->olong);
349
350   modstatdb_open(msdbrw_readonly);
351
352   pkg = pkg_db_find_singleton("dpkg");
353   switch (pkg->status) {
354   case stat_installed:
355   case stat_triggerspending:
356     return 0;
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))
360       return 0;
361     printf(_("Version of dpkg with working %s support not yet configured.\n"
362              " Please use 'dpkg --configure dpkg', and then try again.\n"),
363            feature_name);
364     return 1;
365   default:
366     printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
367            feature_name);
368     return 1;
369   }
370 }
371
372 int
373 assertpredep(const char *const *argv)
374 {
375   struct dpkg_version version = { 0, "1.1.0", NULL };
376
377   return assert_version_support(argv, &version, _("Pre-Depends field"));
378 }
379
380 int
381 assertepoch(const char *const *argv)
382 {
383   struct dpkg_version version = { 0, "1.4.0.7", NULL };
384
385   return assert_version_support(argv, &version, _("epoch"));
386 }
387
388 int
389 assertlongfilenames(const char *const *argv)
390 {
391   struct dpkg_version version = { 0, "1.4.1.17", NULL };
392
393   return assert_version_support(argv, &version, _("long filenames"));
394 }
395
396 int
397 assertmulticonrep(const char *const *argv)
398 {
399   struct dpkg_version version = { 0, "1.4.1.19", NULL };
400
401   return assert_version_support(argv, &version,
402                                 _("multiple Conflicts and Replaces"));
403 }
404
405 int
406 assertmultiarch(const char *const *argv)
407 {
408   struct dpkg_version version = { 0, "1.16.2", NULL };
409
410   return assert_version_support(argv, &version, _("multi-arch"));
411 }
412
413 /**
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.
417  *
418  * If such a package is present output is the Packages file entry,
419  * which can be massaged as appropriate.
420  *
421  * Exit status:
422  *  0 = a package printed, OK
423  *  1 = no suitable package available
424  *  2 = error
425  */
426 int
427 predeppackage(const char *const *argv)
428 {
429   static struct varbuf vb;
430
431   struct pkgiterator *it;
432   struct pkginfo *pkg = NULL, *startpkg, *trypkg;
433   struct dependency *dep;
434   struct deppossi *possi, *provider;
435
436   if (*argv)
437     badusage(_("--%s takes no arguments"), cipaction->olong);
438
439   modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
440   /* We use clientdata->istobe to detect loops. */
441   clear_istobes();
442
443   dep = NULL;
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)
448       continue;
449     /* Ignore packages not available. */
450     if (!pkg->files)
451       continue;
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))
456         continue;
457       /* This will leave dep non-NULL, and so exit the loop. */
458       break;
459     }
460     pkg->clientdata->istobe= itb_normal;
461     /* If dep is NULL we go and get the next package. */
462   }
463   pkg_db_iter_free(it);
464
465   if (!dep)
466     return 1; /* Not found. */
467   assert(pkg);
468   startpkg= pkg;
469   pkg->clientdata->istobe= itb_preinstall;
470
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. */
474   do {
475     /* We search for a package which would satisfy dep, and put it in pkg. */
476     for (possi = dep->list, pkg = NULL;
477          !pkg && possi;
478          possi=possi->next) {
479       struct deppossi_pkg_iterator *possi_iter;
480
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)) {
485           pkg = trypkg;
486           break;
487         }
488         if (possi->verrel != dpkg_relation_none)
489           continue;
490         for (provider = possi->ed->depended.available;
491              !pkg && provider;
492              provider = provider->next) {
493           if (provider->up->type != dep_provides)
494             continue;
495           trypkg = provider->up->up;
496           if (!trypkg->files)
497             continue;
498           if (trypkg->clientdata->istobe == itb_normal) {
499             pkg = trypkg;
500             break;
501           }
502         }
503       }
504       deppossi_pkg_iter_free(possi_iter);
505     }
506     if (!pkg) {
507       varbuf_reset(&vb);
508       describedepcon(&vb,dep);
509       varbuf_end_str(&vb);
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));
514     }
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))
519         continue;
520       /* This will leave dep non-NULL, and so exit the loop. */
521       break;
522     }
523   } while (dep);
524
525   /* OK, we've found it - pkg has no unsatisfied pre-dependencies! */
526   writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
527
528   m_output(stdout, _("<standard output>"));
529
530   return 0;
531 }
532
533 int
534 printarch(const char *const *argv)
535 {
536   if (*argv)
537     badusage(_("--%s takes no arguments"), cipaction->olong);
538
539   printf("%s\n", dpkg_arch_get(arch_native)->name);
540
541   m_output(stdout, _("<standard output>"));
542
543   return 0;
544 }
545
546 int
547 printinstarch(const char *const *argv)
548 {
549   warning(_("obsolete option '--%s'; please use '--%s' instead"),
550           "print-installation-architecture", "print-architecture");
551   return printarch(argv);
552 }
553
554 int
555 print_foreign_arches(const char *const *argv)
556 {
557   struct dpkg_arch *arch;
558
559   if (*argv)
560     badusage(_("--%s takes no arguments"), cipaction->olong);
561
562   dpkg_arch_load_list();
563
564   for (arch = dpkg_arch_get_list(); arch; arch = arch->next) {
565     if (arch->type != arch_foreign)
566       continue;
567
568     printf("%s\n", arch->name);
569   }
570
571   m_output(stdout, _("<standard output>"));
572
573   return 0;
574 }
575
576 int
577 cmpversions(const char *const *argv)
578 {
579   struct relationinfo {
580     const char *string;
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;
584   };
585
586   static const struct relationinfo relationinfos[]= {
587     /*             < = > !a!2!b  */
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  },
594
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  },
600
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  },
609     { NULL                       }
610   };
611
612   const struct relationinfo *rip;
613   struct dpkg_version a, b;
614   struct dpkg_error err;
615   int r;
616
617   if (!argv[0] || !argv[1] || !argv[2] || argv[3])
618     badusage(_("--compare-versions takes three arguments:"
619              " <version> <relation> <version>"));
620
621   for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
622
623   if (!rip->string) badusage(_("--compare-versions bad relation"));
624
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);
629       else
630         ohshit(_("version '%s' has bad syntax: %s"), argv[0], err.str);
631       dpkg_error_destroy(&err);
632     }
633   } else {
634     dpkg_version_blank(&a);
635   }
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);
640       else
641         ohshit(_("version '%s' has bad syntax: %s"), argv[2], err.str);
642       dpkg_error_destroy(&err);
643     }
644   } else {
645     dpkg_version_blank(&b);
646   }
647   if (!dpkg_version_is_informative(&a)) {
648     if (dpkg_version_is_informative(&b))
649       return rip->if_none_a;
650     else
651       return rip->if_none_both;
652   } else if (!dpkg_version_is_informative(&b)) {
653     return rip->if_none_b;
654   }
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),
659         r);
660   if (r > 0)
661     return rip->if_greater;
662   else if (r < 0)
663     return rip->if_lesser;
664   else
665     return rip->if_equal;
666 }