1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/prctl.h>
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "signal-util.h"
37 #include "string-util.h"
39 #include "terminal-util.h"
42 static const char prefixes
[] =
54 static const char suffixes
[] =
61 "systemd/system-preset\0"
62 "systemd/user-preset\0"
66 static const char have_dropins
[] =
70 static bool arg_no_pager
= false;
71 static int arg_diff
= -1;
75 SHOW_EQUIVALENT
= 1 << 1,
76 SHOW_REDIRECTED
= 1 << 2,
77 SHOW_OVERRIDDEN
= 1 << 3,
78 SHOW_UNCHANGED
= 1 << 4,
79 SHOW_EXTENDED
= 1 << 5,
82 (SHOW_MASKED
| SHOW_EQUIVALENT
| SHOW_REDIRECTED
| SHOW_OVERRIDDEN
| SHOW_EXTENDED
)
85 static void pager_open_if_enabled(void) {
93 static int equivalent(const char *a
, const char *b
) {
94 _cleanup_free_
char *x
= NULL
, *y
= NULL
;
96 x
= canonicalize_file_name(a
);
100 y
= canonicalize_file_name(b
);
104 return path_equal(x
, y
);
107 static int notify_override_masked(const char *top
, const char *bottom
) {
108 if (!(arg_flags
& SHOW_MASKED
))
111 printf("%s%s%s %s %s %s\n",
112 ansi_highlight_red(), "[MASKED]", ansi_normal(),
113 top
, draw_special_char(DRAW_ARROW
), bottom
);
117 static int notify_override_equivalent(const char *top
, const char *bottom
) {
118 if (!(arg_flags
& SHOW_EQUIVALENT
))
121 printf("%s%s%s %s %s %s\n",
122 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
123 top
, draw_special_char(DRAW_ARROW
), bottom
);
127 static int notify_override_redirected(const char *top
, const char *bottom
) {
128 if (!(arg_flags
& SHOW_REDIRECTED
))
131 printf("%s%s%s %s %s %s\n",
132 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
133 top
, draw_special_char(DRAW_ARROW
), bottom
);
137 static int notify_override_overridden(const char *top
, const char *bottom
) {
138 if (!(arg_flags
& SHOW_OVERRIDDEN
))
141 printf("%s%s%s %s %s %s\n",
142 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
143 top
, draw_special_char(DRAW_ARROW
), bottom
);
147 static int notify_override_extended(const char *top
, const char *bottom
) {
148 if (!(arg_flags
& SHOW_EXTENDED
))
151 printf("%s%s%s %s %s %s\n",
152 ansi_highlight(), "[EXTENDED]", ansi_normal(),
153 top
, draw_special_char(DRAW_ARROW
), bottom
);
157 static int notify_override_unchanged(const char *f
) {
158 if (!(arg_flags
& SHOW_UNCHANGED
))
161 printf("[UNCHANGED] %s\n", f
);
165 static int found_override(const char *top
, const char *bottom
) {
166 _cleanup_free_
char *dest
= NULL
;
173 if (null_or_empty_path(top
) > 0)
174 return notify_override_masked(top
, bottom
);
176 k
= readlink_malloc(top
, &dest
);
178 if (equivalent(dest
, bottom
) > 0)
179 return notify_override_equivalent(top
, bottom
);
181 return notify_override_redirected(top
, bottom
);
184 k
= notify_override_overridden(top
, bottom
);
194 return log_error_errno(errno
, "Failed to fork off diff: %m");
197 (void) reset_all_signal_handlers();
198 (void) reset_signal_mask();
199 assert_se(prctl(PR_SET_PDEATHSIG
, SIGTERM
) == 0);
201 execlp("diff", "diff", "-us", "--", bottom
, top
, NULL
);
202 log_error_errno(errno
, "Failed to execute diff: %m");
206 wait_for_terminate_and_warn("diff", pid
, false);
212 static int enumerate_dir_d(Hashmap
*top
, Hashmap
*bottom
, Hashmap
*drops
, const char *toppath
, const char *drop
) {
213 _cleanup_free_
char *unit
= NULL
;
214 _cleanup_free_
char *path
= NULL
;
215 _cleanup_strv_free_
char **list
= NULL
;
220 assert(!endswith(drop
, "/"));
222 path
= strjoin(toppath
, "/", drop
, NULL
);
226 log_debug("Looking at %s", path
);
232 c
= strrchr(unit
, '.');
237 r
= get_files_in_directory(path
, &list
);
239 return log_error_errno(r
, "Failed to enumerate %s: %m", path
);
241 STRV_FOREACH(file
, list
) {
247 if (!endswith(*file
, ".conf"))
250 p
= strjoin(path
, "/", *file
, NULL
);
253 d
= p
+ strlen(toppath
) + 1;
255 log_debug("Adding at top: %s %s %s", d
, draw_special_char(DRAW_ARROW
), p
);
256 k
= hashmap_put(top
, d
, p
);
261 d
= p
+ strlen(toppath
) + 1;
262 } else if (k
!= -EEXIST
) {
267 log_debug("Adding at bottom: %s %s %s", d
, draw_special_char(DRAW_ARROW
), p
);
268 free(hashmap_remove(bottom
, d
));
269 k
= hashmap_put(bottom
, d
, p
);
275 h
= hashmap_get(drops
, unit
);
277 h
= hashmap_new(&string_hash_ops
);
280 hashmap_put(drops
, unit
, h
);
290 log_debug("Adding to drops: %s %s %s %s %s",
291 unit
, draw_special_char(DRAW_ARROW
), basename(p
), draw_special_char(DRAW_ARROW
), p
);
292 k
= hashmap_put(h
, basename(p
), p
);
302 static int enumerate_dir(Hashmap
*top
, Hashmap
*bottom
, Hashmap
*drops
, const char *path
, bool dropins
) {
303 _cleanup_closedir_
DIR *d
;
310 log_debug("Looking at %s", path
);
317 log_error_errno(errno
, "Failed to open %s: %m", path
);
331 dirent_ensure_type(d
, de
);
333 if (dropins
&& de
->d_type
== DT_DIR
&& endswith(de
->d_name
, ".d"))
334 enumerate_dir_d(top
, bottom
, drops
, path
, de
->d_name
);
336 if (!dirent_is_file(de
))
339 p
= strjoin(path
, "/", de
->d_name
, NULL
);
343 log_debug("Adding at top: %s %s %s", basename(p
), draw_special_char(DRAW_ARROW
), p
);
344 k
= hashmap_put(top
, basename(p
), p
);
349 } else if (k
!= -EEXIST
) {
354 log_debug("Adding at bottom: %s %s %s", basename(p
), draw_special_char(DRAW_ARROW
), p
);
355 free(hashmap_remove(bottom
, basename(p
)));
356 k
= hashmap_put(bottom
, basename(p
), p
);
364 static int process_suffix(const char *suffix
, const char *onlyprefix
) {
367 Hashmap
*top
, *bottom
, *drops
;
376 assert(!startswith(suffix
, "/"));
377 assert(!strstr(suffix
, "//"));
379 dropins
= nulstr_contains(have_dropins
, suffix
);
381 top
= hashmap_new(&string_hash_ops
);
382 bottom
= hashmap_new(&string_hash_ops
);
383 drops
= hashmap_new(&string_hash_ops
);
384 if (!top
|| !bottom
|| !drops
) {
389 NULSTR_FOREACH(p
, prefixes
) {
390 _cleanup_free_
char *t
= NULL
;
392 t
= strjoin(p
, "/", suffix
, NULL
);
398 k
= enumerate_dir(top
, bottom
, drops
, t
, dropins
);
403 HASHMAP_FOREACH_KEY(f
, key
, top
, i
) {
406 o
= hashmap_get(bottom
, key
);
409 if (!onlyprefix
|| startswith(o
, onlyprefix
)) {
410 if (path_equal(o
, f
)) {
411 notify_override_unchanged(f
);
413 k
= found_override(f
, o
);
421 h
= hashmap_get(drops
, key
);
423 HASHMAP_FOREACH(o
, h
, j
)
424 if (!onlyprefix
|| startswith(o
, onlyprefix
))
425 n_found
+= notify_override_extended(f
, o
);
430 hashmap_free_free(top
);
432 hashmap_free_free(bottom
);
434 HASHMAP_FOREACH_KEY(h
, key
, drops
, i
){
435 hashmap_free_free(hashmap_remove(drops
, key
));
436 hashmap_remove(drops
, key
);
441 return r
< 0 ? r
: n_found
;
444 static int process_suffixes(const char *onlyprefix
) {
448 NULSTR_FOREACH(n
, suffixes
) {
449 r
= process_suffix(n
, onlyprefix
);
458 static int process_suffix_chop(const char *arg
) {
463 if (!path_is_absolute(arg
))
464 return process_suffix(arg
, NULL
);
466 /* Strip prefix from the suffix */
467 NULSTR_FOREACH(p
, prefixes
) {
468 const char *suffix
= startswith(arg
, p
);
470 suffix
+= strspn(suffix
, "/");
472 return process_suffix(suffix
, NULL
);
474 return process_suffixes(arg
);
478 log_error("Invalid suffix specification %s.", arg
);
482 static void help(void) {
483 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
484 "Find overridden configuration files.\n\n"
485 " -h --help Show this help\n"
486 " --version Show package version\n"
487 " --no-pager Do not pipe output into a pager\n"
488 " --diff[=1|0] Show a diff when overridden files differ\n"
489 " -t --type=LIST... Only display a selected set of override types\n"
490 , program_invocation_short_name
);
493 static int parse_flags(const char *flag_str
, int flags
) {
494 const char *word
, *state
;
497 FOREACH_WORD_SEPARATOR(word
, l
, flag_str
, ",", state
) {
498 if (strneq("masked", word
, l
))
499 flags
|= SHOW_MASKED
;
500 else if (strneq ("equivalent", word
, l
))
501 flags
|= SHOW_EQUIVALENT
;
502 else if (strneq("redirected", word
, l
))
503 flags
|= SHOW_REDIRECTED
;
504 else if (strneq("overridden", word
, l
))
505 flags
|= SHOW_OVERRIDDEN
;
506 else if (strneq("unchanged", word
, l
))
507 flags
|= SHOW_UNCHANGED
;
508 else if (strneq("extended", word
, l
))
509 flags
|= SHOW_EXTENDED
;
510 else if (strneq("default", word
, l
))
511 flags
|= SHOW_DEFAULTS
;
518 static int parse_argv(int argc
, char *argv
[]) {
521 ARG_NO_PAGER
= 0x100,
526 static const struct option options
[] = {
527 { "help", no_argument
, NULL
, 'h' },
528 { "version", no_argument
, NULL
, ARG_VERSION
},
529 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
530 { "diff", optional_argument
, NULL
, ARG_DIFF
},
531 { "type", required_argument
, NULL
, 't' },
540 while ((c
= getopt_long(argc
, argv
, "ht:", options
, NULL
)) >= 0)
557 f
= parse_flags(optarg
, arg_flags
);
559 log_error("Failed to parse flags field.");
572 b
= parse_boolean(optarg
);
574 log_error("Failed to parse diff boolean.");
587 assert_not_reached("Unhandled option");
593 int main(int argc
, char *argv
[]) {
597 log_parse_environment();
600 r
= parse_argv(argc
, argv
);
605 arg_flags
= SHOW_DEFAULTS
;
608 arg_diff
= !!(arg_flags
& SHOW_OVERRIDDEN
);
610 arg_flags
|= SHOW_OVERRIDDEN
;
612 pager_open_if_enabled();
617 for (i
= optind
; i
< argc
; i
++) {
618 path_kill_slashes(argv
[i
]);
619 k
= process_suffix_chop(argv
[i
]);
627 k
= process_suffixes(NULL
);
635 printf("%s%i overridden configuration files found.\n",
636 n_found
? "\n" : "", n_found
);
641 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;