Branch data Line data Source code
1 : : /*
2 : : * dpkg - main program for package management
3 : : * help.c - various helper routines
4 : : *
5 : : * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
7 : : *
8 : : * This is free software; you can redistribute it and/or modify
9 : : * it under the terms of the GNU General Public License as published by
10 : : * the Free Software Foundation; either version 2 of the License, or
11 : : * (at your option) any later version.
12 : : *
13 : : * This is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : * GNU General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU General Public License
19 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <sys/types.h>
26 : : #include <sys/stat.h>
27 : :
28 : : #include <errno.h>
29 : : #include <string.h>
30 : : #include <unistd.h>
31 : : #include <stdlib.h>
32 : :
33 : : #include <dpkg/i18n.h>
34 : : #include <dpkg/dpkg.h>
35 : : #include <dpkg/dpkg-db.h>
36 : : #include <dpkg/path.h>
37 : : #include <dpkg/file.h>
38 : : #include <dpkg/command.h>
39 : : #include <dpkg/db-fsys.h>
40 : :
41 : : #include "main.h"
42 : :
43 : : const char *const statusstrings[]= {
44 : : [PKG_STAT_NOTINSTALLED] = N_("not installed"),
45 : : [PKG_STAT_CONFIGFILES] = N_("not installed but configs remain"),
46 : : [PKG_STAT_HALFINSTALLED] = N_("broken due to failed removal or installation"),
47 : : [PKG_STAT_UNPACKED] = N_("unpacked but not configured"),
48 : : [PKG_STAT_HALFCONFIGURED] = N_("broken due to postinst failure"),
49 : : [PKG_STAT_TRIGGERSAWAITED] = N_("awaiting trigger processing by another package"),
50 : : [PKG_STAT_TRIGGERSPENDING] = N_("triggered"),
51 : : [PKG_STAT_INSTALLED] = N_("installed")
52 : : };
53 : :
54 : : struct fsys_namenode *
55 : 0 : namenodetouse(struct fsys_namenode *namenode, struct pkginfo *pkg,
56 : : struct pkgbin *pkgbin)
57 : : {
58 : : struct fsys_namenode *fnn;
59 : :
60 [ # # ]: 0 : if (!namenode->divert)
61 : 0 : return namenode;
62 : :
63 : 0 : debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
64 : : namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
65 : :
66 [ # # ]: 0 : fnn = (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
67 [ # # ]: 0 : ? namenode->divert->useinstead : namenode;
68 : :
69 : 0 : debug(dbg_eachfile,
70 : : "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
71 [ # # ]: 0 : namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
72 [ # # ]: 0 : namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
73 [ # # ]: 0 : namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
74 : : fnn->name);
75 : :
76 : 0 : return fnn;
77 : : }
78 : :
79 : : /**
80 : : * Verify that some programs can be found in the PATH.
81 : : */
82 : 0 : void checkpath(void) {
83 : : static const char *const prog_list[] = {
84 : : DPKG_DEFAULT_SHELL,
85 : : RM,
86 : : TAR,
87 : : DIFF,
88 : : BACKEND,
89 : : /* macOS uses dyld (Mach-O) instead of ld.so (ELF), and does not have
90 : : * an ldconfig. */
91 : : #if defined(__APPLE__) && defined(__MACH__)
92 : : "update_dyld_shared_cache",
93 : : #elif defined(__GLIBC__) || defined(__UCLIBC__) || \
94 : : defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
95 : : "ldconfig",
96 : : #endif
97 : : #if BUILD_START_STOP_DAEMON
98 : : "start-stop-daemon",
99 : : #endif
100 : : NULL
101 : : };
102 : :
103 : : const char *const *prog;
104 : 0 : int warned= 0;
105 : :
106 [ # # ]: 0 : for (prog = prog_list; *prog; prog++) {
107 [ # # ]: 0 : if (!command_in_path(*prog)) {
108 : 0 : warning(_("'%s' not found in PATH or not executable"), *prog);
109 : 0 : warned++;
110 : : }
111 : : }
112 : :
113 [ # # ]: 0 : if (warned)
114 : 0 : forcibleerr(FORCE_BAD_PATH,
115 : 0 : P_("%d expected program not found in PATH or not executable\n%s",
116 : : "%d expected programs not found in PATH or not executable\n%s",
117 : : warned),
118 : : warned, _("Note: root's PATH should usually contain "
119 : : "/usr/local/sbin, /usr/sbin and /sbin"));
120 : 0 : }
121 : :
122 : : bool
123 : 0 : ignore_depends(const struct pkginfo *pkg)
124 : : {
125 : : struct pkg_list *id;
126 [ # # ]: 0 : for (id= ignoredependss; id; id= id->next)
127 [ # # ]: 0 : if (id->pkg == pkg)
128 : 0 : return true;
129 : 0 : return false;
130 : : }
131 : :
132 : : static bool
133 : 0 : ignore_depends_possi(struct deppossi *possi)
134 : : {
135 : : struct deppossi_pkg_iterator *possi_iter;
136 : : struct pkginfo *pkg;
137 : :
138 : 0 : possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
139 [ # # ]: 0 : while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
140 [ # # ]: 0 : if (ignore_depends(pkg)) {
141 : 0 : deppossi_pkg_iter_free(possi_iter);
142 : 0 : return true;
143 : : }
144 : : }
145 : 0 : deppossi_pkg_iter_free(possi_iter);
146 : :
147 : 0 : return false;
148 : : }
149 : :
150 : : bool
151 : 0 : force_depends(struct deppossi *possi)
152 : : {
153 [ # # ]: 0 : return in_force(FORCE_DEPENDS) ||
154 [ # # # # ]: 0 : ignore_depends_possi(possi) ||
155 : 0 : ignore_depends(possi->up->up);
156 : : }
157 : :
158 : : bool
159 : 0 : force_breaks(struct deppossi *possi)
160 : : {
161 [ # # ]: 0 : return in_force(FORCE_BREAKS) ||
162 [ # # # # ]: 0 : ignore_depends_possi(possi) ||
163 : 0 : ignore_depends(possi->up->up);
164 : : }
165 : :
166 : : bool
167 : 0 : force_conflicts(struct deppossi *possi)
168 : : {
169 : 0 : return in_force(FORCE_CONFLICTS);
170 : : }
171 : :
172 : 0 : void clear_istobes(void) {
173 : : struct pkg_hash_iter *iter;
174 : : struct pkginfo *pkg;
175 : :
176 : 0 : iter = pkg_hash_iter_new();
177 [ # # ]: 0 : while ((pkg = pkg_hash_iter_next_pkg(iter)) != NULL) {
178 : 0 : ensure_package_clientdata(pkg);
179 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
180 : 0 : pkg->clientdata->replacingfilesandsaid= 0;
181 : : }
182 : 0 : pkg_hash_iter_free(iter);
183 : 0 : }
184 : :
185 : : /*
186 : : * Returns true if the directory contains conffiles belonging to pkg,
187 : : * false otherwise.
188 : : */
189 : : bool
190 : 0 : dir_has_conffiles(struct fsys_namenode *file, struct pkginfo *pkg)
191 : : {
192 : : struct conffile *conff;
193 : : size_t namelen;
194 : :
195 : 0 : debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
196 : : pkg_name(pkg, pnaw_always));
197 : 0 : namelen = strlen(file->name);
198 [ # # ]: 0 : for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
199 [ # # ]: 0 : if (conffile_is_disappearing(conff))
200 : 0 : continue;
201 [ # # ]: 0 : if (strncmp(file->name, conff->name, namelen) == 0 &&
202 [ # # # # ]: 0 : strlen(conff->name) > namelen && conff->name[namelen] == '/') {
203 : 0 : debug(dbg_veryverbose, "directory %s has conffile %s from %s",
204 : : file->name, conff->name, pkg_name(pkg, pnaw_always));
205 : 0 : return true;
206 : : }
207 : : }
208 : 0 : debug(dbg_veryverbose, "dir_has_conffiles no");
209 : 0 : return false;
210 : : }
211 : :
212 : : /*
213 : : * Returns true if the file is used by packages other than pkg,
214 : : * false otherwise.
215 : : */
216 : : bool
217 : 0 : dir_is_used_by_others(struct fsys_namenode *file, struct pkginfo *pkg)
218 : : {
219 : : struct fsys_node_pkgs_iter *iter;
220 : : struct pkginfo *other_pkg;
221 : :
222 [ # # ]: 0 : debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
223 : 0 : pkg ? pkg_name(pkg, pnaw_always) : "<none>");
224 : :
225 : 0 : iter = fsys_node_pkgs_iter_new(file);
226 [ # # ]: 0 : while ((other_pkg = fsys_node_pkgs_iter_next(iter))) {
227 : 0 : debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
228 : : pkg_name(other_pkg, pnaw_always));
229 [ # # ]: 0 : if (other_pkg == pkg)
230 : 0 : continue;
231 : :
232 : 0 : fsys_node_pkgs_iter_free(iter);
233 : 0 : debug(dbg_veryverbose, "dir_is_used_by_others yes");
234 : 0 : return true;
235 : : }
236 : 0 : fsys_node_pkgs_iter_free(iter);
237 : :
238 : 0 : debug(dbg_veryverbose, "dir_is_used_by_others no");
239 : 0 : return false;
240 : : }
241 : :
242 : : /*
243 : : * Returns true if the file is used by pkg, false otherwise.
244 : : */
245 : : bool
246 : 0 : dir_is_used_by_pkg(struct fsys_namenode *file, struct pkginfo *pkg,
247 : : struct fsys_namenode_list *list)
248 : : {
249 : : struct fsys_namenode_list *node;
250 : : size_t namelen;
251 : :
252 [ # # ]: 0 : debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
253 : 0 : file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
254 : :
255 : 0 : namelen = strlen(file->name);
256 : :
257 [ # # ]: 0 : for (node = list; node; node = node->next) {
258 : 0 : debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
259 : 0 : node->namenode->name);
260 : :
261 [ # # ]: 0 : if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
262 [ # # ]: 0 : strlen(node->namenode->name) > namelen &&
263 [ # # ]: 0 : node->namenode->name[namelen] == '/') {
264 : 0 : debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
265 : 0 : return true;
266 : : }
267 : : }
268 : :
269 : 0 : debug(dbg_veryverbose, "dir_is_used_by_pkg no");
270 : :
271 : 0 : return false;
272 : : }
273 : :
274 : : /**
275 : : * Returns whether the conffile is disappearing, because it is obsolete
276 : : * or marked for removal on upgrade.
277 : : */
278 : : bool
279 : 0 : conffile_is_disappearing(struct conffile *conff)
280 : : {
281 : 0 : return conff->flags & (CONFFILE_OBSOLETE | CONFFILE_REMOVE_ON_UPGRADE);
282 : : }
283 : :
284 : : /**
285 : : * Mark a conffile as obsolete.
286 : : *
287 : : * @param pkg The package owning the conffile.
288 : : * @param namenode The namenode for the obsolete conffile.
289 : : */
290 : : void
291 : 0 : conffile_mark_obsolete(struct pkginfo *pkg, struct fsys_namenode *namenode)
292 : : {
293 : : struct conffile *conff;
294 : :
295 [ # # ]: 0 : for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
296 [ # # ]: 0 : if (strcmp(conff->name, namenode->name) == 0) {
297 : 0 : debug(dbg_conff, "marking %s conffile %s as obsolete",
298 : : pkg_name(pkg, pnaw_always), conff->name);
299 : 0 : conff->flags |= CONFFILE_OBSOLETE;
300 : 0 : return;
301 : : }
302 : : }
303 : : }
304 : :
305 : : /**
306 : : * Mark all package conffiles as old.
307 : : *
308 : : * @param pkg The package owning the conffiles.
309 : : */
310 : : void
311 : 0 : pkg_conffiles_mark_old(struct pkginfo *pkg)
312 : : {
313 : : const struct conffile *conff;
314 : :
315 [ # # ]: 0 : for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
316 : : struct fsys_namenode *namenode;
317 : :
318 : 0 : namenode = fsys_hash_find_node(conff->name, FHFF_NONE); /* XXX */
319 : 0 : namenode->flags |= FNNF_OLD_CONFF;
320 [ # # ]: 0 : if (!namenode->oldhash)
321 : 0 : namenode->oldhash = conff->hash;
322 : 0 : debug(dbg_conffdetail, "%s '%s' namenode '%s' flags %o", __func__,
323 : 0 : conff->name, namenode->name, namenode->flags);
324 : : }
325 : 0 : }
326 : :
327 : : void
328 : 0 : log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
329 : : {
330 : 0 : log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
331 : 0 : versiondescribe_c(&pkg->installed.version, vdew_nonambig),
332 : 0 : versiondescribe_c(&pkg->available.version, vdew_nonambig));
333 : 0 : statusfd_send("processing: %s: %s", action,
334 : : pkgbin_name(pkg, pkgbin, pnaw_nonambig));
335 : 0 : }
|