1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Zbigniew Jędrzejewski-Szmek
12 #include "alloc-util.h"
13 #include "dirent-util.h"
17 #include "locale-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "signal-util.h"
24 #include "stat-util.h"
25 #include "string-util.h"
27 #include "terminal-util.h"
30 static const char prefixes
[] =
42 static const char suffixes
[] =
49 "systemd/system-preset\0"
50 "systemd/user-preset\0"
54 static const char have_dropins
[] =
58 static bool arg_no_pager
= false;
59 static int arg_diff
= -1;
63 SHOW_EQUIVALENT
= 1 << 1,
64 SHOW_REDIRECTED
= 1 << 2,
65 SHOW_OVERRIDDEN
= 1 << 3,
66 SHOW_UNCHANGED
= 1 << 4,
67 SHOW_EXTENDED
= 1 << 5,
70 (SHOW_MASKED
| SHOW_EQUIVALENT
| SHOW_REDIRECTED
| SHOW_OVERRIDDEN
| SHOW_EXTENDED
)
73 static int equivalent(const char *a
, const char *b
) {
74 _cleanup_free_
char *x
= NULL
, *y
= NULL
;
77 r
= chase_symlinks(a
, NULL
, CHASE_TRAIL_SLASH
, &x
);
81 r
= chase_symlinks(b
, NULL
, CHASE_TRAIL_SLASH
, &y
);
85 return path_equal(x
, y
);
88 static int notify_override_masked(const char *top
, const char *bottom
) {
89 if (!(arg_flags
& SHOW_MASKED
))
92 printf("%s%s%s %s %s %s\n",
93 ansi_highlight_red(), "[MASKED]", ansi_normal(),
94 top
, special_glyph(ARROW
), bottom
);
98 static int notify_override_equivalent(const char *top
, const char *bottom
) {
99 if (!(arg_flags
& SHOW_EQUIVALENT
))
102 printf("%s%s%s %s %s %s\n",
103 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
104 top
, special_glyph(ARROW
), bottom
);
108 static int notify_override_redirected(const char *top
, const char *bottom
) {
109 if (!(arg_flags
& SHOW_REDIRECTED
))
112 printf("%s%s%s %s %s %s\n",
113 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
114 top
, special_glyph(ARROW
), bottom
);
118 static int notify_override_overridden(const char *top
, const char *bottom
) {
119 if (!(arg_flags
& SHOW_OVERRIDDEN
))
122 printf("%s%s%s %s %s %s\n",
123 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
124 top
, special_glyph(ARROW
), bottom
);
128 static int notify_override_extended(const char *top
, const char *bottom
) {
129 if (!(arg_flags
& SHOW_EXTENDED
))
132 printf("%s%s%s %s %s %s\n",
133 ansi_highlight(), "[EXTENDED]", ansi_normal(),
134 top
, special_glyph(ARROW
), bottom
);
138 static int notify_override_unchanged(const char *f
) {
139 if (!(arg_flags
& SHOW_UNCHANGED
))
142 printf("[UNCHANGED] %s\n", f
);
146 static int found_override(const char *top
, const char *bottom
) {
147 _cleanup_free_
char *dest
= NULL
;
154 if (null_or_empty_path(top
) > 0)
155 return notify_override_masked(top
, bottom
);
157 r
= readlink_malloc(top
, &dest
);
159 if (equivalent(dest
, bottom
) > 0)
160 return notify_override_equivalent(top
, bottom
);
162 return notify_override_redirected(top
, bottom
);
165 r
= notify_override_overridden(top
, bottom
);
173 r
= safe_fork("(diff)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
177 execlp("diff", "diff", "-us", "--", bottom
, top
, NULL
);
179 log_error_errno(errno
, "Failed to execute diff: %m");
183 (void) wait_for_terminate_and_check("diff", pid
, WAIT_LOG_ABNORMAL
);
189 static int enumerate_dir_d(
191 OrderedHashmap
*bottom
,
192 OrderedHashmap
*drops
,
193 const char *toppath
, const char *drop
) {
195 _cleanup_free_
char *unit
= NULL
;
196 _cleanup_free_
char *path
= NULL
;
197 _cleanup_strv_free_
char **list
= NULL
;
202 assert(!endswith(drop
, "/"));
204 path
= strjoin(toppath
, "/", drop
);
208 log_debug("Looking at %s", path
);
214 c
= strrchr(unit
, '.');
219 r
= get_files_in_directory(path
, &list
);
221 return log_error_errno(r
, "Failed to enumerate %s: %m", path
);
225 STRV_FOREACH(file
, list
) {
231 if (!endswith(*file
, ".conf"))
234 p
= strjoin(path
, "/", *file
);
237 d
= p
+ strlen(toppath
) + 1;
239 log_debug("Adding at top: %s %s %s", d
, special_glyph(ARROW
), p
);
240 k
= ordered_hashmap_put(top
, d
, p
);
245 d
= p
+ strlen(toppath
) + 1;
246 } else if (k
!= -EEXIST
) {
251 log_debug("Adding at bottom: %s %s %s", d
, special_glyph(ARROW
), p
);
252 free(ordered_hashmap_remove(bottom
, d
));
253 k
= ordered_hashmap_put(bottom
, d
, p
);
259 h
= ordered_hashmap_get(drops
, unit
);
261 h
= ordered_hashmap_new(&string_hash_ops
);
264 ordered_hashmap_put(drops
, unit
, h
);
274 log_debug("Adding to drops: %s %s %s %s %s",
275 unit
, special_glyph(ARROW
), basename(p
), special_glyph(ARROW
), p
);
276 k
= ordered_hashmap_put(h
, basename(p
), p
);
286 static int enumerate_dir(
288 OrderedHashmap
*bottom
,
289 OrderedHashmap
*drops
,
290 const char *path
, bool dropins
) {
292 _cleanup_closedir_
DIR *d
= NULL
;
294 _cleanup_strv_free_
char **files
= NULL
, **dirs
= NULL
;
295 size_t n_files
= 0, allocated_files
= 0, n_dirs
= 0, allocated_dirs
= 0;
304 log_debug("Looking at %s", path
);
311 return log_error_errno(errno
, "Failed to open %s: %m", path
);
314 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
315 dirent_ensure_type(d
, de
);
317 if (dropins
&& de
->d_type
== DT_DIR
&& endswith(de
->d_name
, ".d")) {
318 if (!GREEDY_REALLOC0(dirs
, allocated_dirs
, n_dirs
+ 2))
321 dirs
[n_dirs
] = strdup(de
->d_name
);
327 if (!dirent_is_file(de
))
330 if (!GREEDY_REALLOC0(files
, allocated_files
, n_files
+ 2))
333 files
[n_files
] = strdup(de
->d_name
);
342 STRV_FOREACH(t
, dirs
) {
343 r
= enumerate_dir_d(top
, bottom
, drops
, path
, *t
);
348 STRV_FOREACH(t
, files
) {
349 _cleanup_free_
char *p
= NULL
;
351 p
= strjoin(path
, "/", *t
);
355 log_debug("Adding at top: %s %s %s", basename(p
), special_glyph(ARROW
), p
);
356 r
= ordered_hashmap_put(top
, basename(p
), p
);
361 } else if (r
!= -EEXIST
)
364 log_debug("Adding at bottom: %s %s %s", basename(p
), special_glyph(ARROW
), p
);
365 free(ordered_hashmap_remove(bottom
, basename(p
)));
366 r
= ordered_hashmap_put(bottom
, basename(p
), p
);
375 static bool should_skip_path(const char *prefix
, const char *suffix
) {
377 _cleanup_free_
char *target
= NULL
;
381 dirname
= strjoina(prefix
, "/", suffix
);
383 if (chase_symlinks(dirname
, NULL
, 0, &target
) < 0)
386 NULSTR_FOREACH(p
, prefixes
) {
387 if (path_startswith(dirname
, p
))
390 if (path_equal(target
, strjoina(p
, "/", suffix
))) {
391 log_debug("%s redirects to %s, skipping.", dirname
, target
);
399 static int process_suffix(const char *suffix
, const char *onlyprefix
) {
402 OrderedHashmap
*top
, *bottom
, *drops
;
411 assert(!startswith(suffix
, "/"));
412 assert(!strstr(suffix
, "//"));
414 dropins
= nulstr_contains(have_dropins
, suffix
);
416 top
= ordered_hashmap_new(&string_hash_ops
);
417 bottom
= ordered_hashmap_new(&string_hash_ops
);
418 drops
= ordered_hashmap_new(&string_hash_ops
);
419 if (!top
|| !bottom
|| !drops
) {
424 NULSTR_FOREACH(p
, prefixes
) {
425 _cleanup_free_
char *t
= NULL
;
427 if (should_skip_path(p
, suffix
))
430 t
= strjoin(p
, "/", suffix
);
436 k
= enumerate_dir(top
, bottom
, drops
, t
, dropins
);
441 ORDERED_HASHMAP_FOREACH_KEY(f
, key
, top
, i
) {
444 o
= ordered_hashmap_get(bottom
, key
);
447 if (!onlyprefix
|| startswith(o
, onlyprefix
)) {
448 if (path_equal(o
, f
)) {
449 notify_override_unchanged(f
);
451 k
= found_override(f
, o
);
459 h
= ordered_hashmap_get(drops
, key
);
461 ORDERED_HASHMAP_FOREACH(o
, h
, j
)
462 if (!onlyprefix
|| startswith(o
, onlyprefix
))
463 n_found
+= notify_override_extended(f
, o
);
467 ordered_hashmap_free_free(top
);
468 ordered_hashmap_free_free(bottom
);
470 ORDERED_HASHMAP_FOREACH_KEY(h
, key
, drops
, i
) {
471 ordered_hashmap_free_free(ordered_hashmap_remove(drops
, key
));
472 ordered_hashmap_remove(drops
, key
);
475 ordered_hashmap_free(drops
);
477 return r
< 0 ? r
: n_found
;
480 static int process_suffixes(const char *onlyprefix
) {
484 NULSTR_FOREACH(n
, suffixes
) {
485 r
= process_suffix(n
, onlyprefix
);
495 static int process_suffix_chop(const char *arg
) {
500 if (!path_is_absolute(arg
))
501 return process_suffix(arg
, NULL
);
503 /* Strip prefix from the suffix */
504 NULSTR_FOREACH(p
, prefixes
) {
507 suffix
= startswith(arg
, p
);
509 suffix
+= strspn(suffix
, "/");
511 return process_suffix(suffix
, p
);
513 return process_suffixes(arg
);
517 log_error("Invalid suffix specification %s.", arg
);
521 static void help(void) {
522 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
523 "Find overridden configuration files.\n\n"
524 " -h --help Show this help\n"
525 " --version Show package version\n"
526 " --no-pager Do not pipe output into a pager\n"
527 " --diff[=1|0] Show a diff when overridden files differ\n"
528 " -t --type=LIST... Only display a selected set of override types\n"
529 , program_invocation_short_name
);
532 static int parse_flags(const char *flag_str
, int flags
) {
533 const char *word
, *state
;
536 FOREACH_WORD_SEPARATOR(word
, l
, flag_str
, ",", state
) {
537 if (strneq("masked", word
, l
))
538 flags
|= SHOW_MASKED
;
539 else if (strneq ("equivalent", word
, l
))
540 flags
|= SHOW_EQUIVALENT
;
541 else if (strneq("redirected", word
, l
))
542 flags
|= SHOW_REDIRECTED
;
543 else if (strneq("overridden", word
, l
))
544 flags
|= SHOW_OVERRIDDEN
;
545 else if (strneq("unchanged", word
, l
))
546 flags
|= SHOW_UNCHANGED
;
547 else if (strneq("extended", word
, l
))
548 flags
|= SHOW_EXTENDED
;
549 else if (strneq("default", word
, l
))
550 flags
|= SHOW_DEFAULTS
;
557 static int parse_argv(int argc
, char *argv
[]) {
560 ARG_NO_PAGER
= 0x100,
565 static const struct option options
[] = {
566 { "help", no_argument
, NULL
, 'h' },
567 { "version", no_argument
, NULL
, ARG_VERSION
},
568 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
569 { "diff", optional_argument
, NULL
, ARG_DIFF
},
570 { "type", required_argument
, NULL
, 't' },
579 while ((c
= getopt_long(argc
, argv
, "ht:", options
, NULL
)) >= 0)
596 f
= parse_flags(optarg
, arg_flags
);
598 log_error("Failed to parse flags field.");
611 b
= parse_boolean(optarg
);
613 log_error("Failed to parse diff boolean.");
625 assert_not_reached("Unhandled option");
631 int main(int argc
, char *argv
[]) {
632 int r
, k
, n_found
= 0;
634 log_parse_environment();
637 r
= parse_argv(argc
, argv
);
642 arg_flags
= SHOW_DEFAULTS
;
645 arg_diff
= !!(arg_flags
& SHOW_OVERRIDDEN
);
647 arg_flags
|= SHOW_OVERRIDDEN
;
649 (void) pager_open(arg_no_pager
, false);
654 for (i
= optind
; i
< argc
; i
++) {
655 path_simplify(argv
[i
], false);
657 k
= process_suffix_chop(argv
[i
]);
665 k
= process_suffixes(NULL
);
673 printf("%s%i overridden configuration files found.\n", n_found
? "\n" : "", n_found
);
678 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;