1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "dirent-util.h"
14 #include "locale-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "process-util.h"
20 #include "signal-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
24 #include "terminal-util.h"
27 static const char prefixes
[] =
39 static const char suffixes
[] =
46 "systemd/system-preset\0"
47 "systemd/user-preset\0"
51 static const char have_dropins
[] =
55 static bool arg_no_pager
= false;
56 static int arg_diff
= -1;
60 SHOW_EQUIVALENT
= 1 << 1,
61 SHOW_REDIRECTED
= 1 << 2,
62 SHOW_OVERRIDDEN
= 1 << 3,
63 SHOW_UNCHANGED
= 1 << 4,
64 SHOW_EXTENDED
= 1 << 5,
67 (SHOW_MASKED
| SHOW_EQUIVALENT
| SHOW_REDIRECTED
| SHOW_OVERRIDDEN
| SHOW_EXTENDED
)
70 static int equivalent(const char *a
, const char *b
) {
71 _cleanup_free_
char *x
= NULL
, *y
= NULL
;
74 r
= chase_symlinks(a
, NULL
, CHASE_TRAIL_SLASH
, &x
);
78 r
= chase_symlinks(b
, NULL
, CHASE_TRAIL_SLASH
, &y
);
82 return path_equal(x
, y
);
85 static int notify_override_masked(const char *top
, const char *bottom
) {
86 if (!(arg_flags
& SHOW_MASKED
))
89 printf("%s%s%s %s %s %s\n",
90 ansi_highlight_red(), "[MASKED]", ansi_normal(),
91 top
, special_glyph(ARROW
), bottom
);
95 static int notify_override_equivalent(const char *top
, const char *bottom
) {
96 if (!(arg_flags
& SHOW_EQUIVALENT
))
99 printf("%s%s%s %s %s %s\n",
100 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
101 top
, special_glyph(ARROW
), bottom
);
105 static int notify_override_redirected(const char *top
, const char *bottom
) {
106 if (!(arg_flags
& SHOW_REDIRECTED
))
109 printf("%s%s%s %s %s %s\n",
110 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
111 top
, special_glyph(ARROW
), bottom
);
115 static int notify_override_overridden(const char *top
, const char *bottom
) {
116 if (!(arg_flags
& SHOW_OVERRIDDEN
))
119 printf("%s%s%s %s %s %s\n",
120 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
121 top
, special_glyph(ARROW
), bottom
);
125 static int notify_override_extended(const char *top
, const char *bottom
) {
126 if (!(arg_flags
& SHOW_EXTENDED
))
129 printf("%s%s%s %s %s %s\n",
130 ansi_highlight(), "[EXTENDED]", ansi_normal(),
131 top
, special_glyph(ARROW
), bottom
);
135 static int notify_override_unchanged(const char *f
) {
136 if (!(arg_flags
& SHOW_UNCHANGED
))
139 printf("[UNCHANGED] %s\n", f
);
143 static int found_override(const char *top
, const char *bottom
) {
144 _cleanup_free_
char *dest
= NULL
;
151 if (null_or_empty_path(top
) > 0)
152 return notify_override_masked(top
, bottom
);
154 r
= readlink_malloc(top
, &dest
);
156 if (equivalent(dest
, bottom
) > 0)
157 return notify_override_equivalent(top
, bottom
);
159 return notify_override_redirected(top
, bottom
);
162 r
= notify_override_overridden(top
, bottom
);
170 r
= safe_fork("(diff)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
174 execlp("diff", "diff", "-us", "--", bottom
, top
, NULL
);
176 log_error_errno(errno
, "Failed to execute diff: %m");
180 (void) wait_for_terminate_and_check("diff", pid
, WAIT_LOG_ABNORMAL
);
186 static int enumerate_dir_d(
188 OrderedHashmap
*bottom
,
189 OrderedHashmap
*drops
,
190 const char *toppath
, const char *drop
) {
192 _cleanup_free_
char *unit
= NULL
;
193 _cleanup_free_
char *path
= NULL
;
194 _cleanup_strv_free_
char **list
= NULL
;
199 assert(!endswith(drop
, "/"));
201 path
= strjoin(toppath
, "/", drop
);
205 log_debug("Looking at %s", path
);
211 c
= strrchr(unit
, '.');
216 r
= get_files_in_directory(path
, &list
);
218 return log_error_errno(r
, "Failed to enumerate %s: %m", path
);
222 STRV_FOREACH(file
, list
) {
228 if (!endswith(*file
, ".conf"))
231 p
= strjoin(path
, "/", *file
);
234 d
= p
+ strlen(toppath
) + 1;
236 log_debug("Adding at top: %s %s %s", d
, special_glyph(ARROW
), p
);
237 k
= ordered_hashmap_put(top
, d
, p
);
242 d
= p
+ strlen(toppath
) + 1;
243 } else if (k
!= -EEXIST
) {
248 log_debug("Adding at bottom: %s %s %s", d
, special_glyph(ARROW
), p
);
249 free(ordered_hashmap_remove(bottom
, d
));
250 k
= ordered_hashmap_put(bottom
, d
, p
);
256 h
= ordered_hashmap_get(drops
, unit
);
258 h
= ordered_hashmap_new(&string_hash_ops
);
261 ordered_hashmap_put(drops
, unit
, h
);
271 log_debug("Adding to drops: %s %s %s %s %s",
272 unit
, special_glyph(ARROW
), basename(p
), special_glyph(ARROW
), p
);
273 k
= ordered_hashmap_put(h
, basename(p
), p
);
283 static int enumerate_dir(
285 OrderedHashmap
*bottom
,
286 OrderedHashmap
*drops
,
287 const char *path
, bool dropins
) {
289 _cleanup_closedir_
DIR *d
= NULL
;
291 _cleanup_strv_free_
char **files
= NULL
, **dirs
= NULL
;
292 size_t n_files
= 0, allocated_files
= 0, n_dirs
= 0, allocated_dirs
= 0;
301 log_debug("Looking at %s", path
);
308 return log_error_errno(errno
, "Failed to open %s: %m", path
);
311 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
312 dirent_ensure_type(d
, de
);
314 if (dropins
&& de
->d_type
== DT_DIR
&& endswith(de
->d_name
, ".d")) {
315 if (!GREEDY_REALLOC0(dirs
, allocated_dirs
, n_dirs
+ 2))
318 dirs
[n_dirs
] = strdup(de
->d_name
);
324 if (!dirent_is_file(de
))
327 if (!GREEDY_REALLOC0(files
, allocated_files
, n_files
+ 2))
330 files
[n_files
] = strdup(de
->d_name
);
339 STRV_FOREACH(t
, dirs
) {
340 r
= enumerate_dir_d(top
, bottom
, drops
, path
, *t
);
345 STRV_FOREACH(t
, files
) {
346 _cleanup_free_
char *p
= NULL
;
348 p
= strjoin(path
, "/", *t
);
352 log_debug("Adding at top: %s %s %s", basename(p
), special_glyph(ARROW
), p
);
353 r
= ordered_hashmap_put(top
, basename(p
), p
);
358 } else if (r
!= -EEXIST
)
361 log_debug("Adding at bottom: %s %s %s", basename(p
), special_glyph(ARROW
), p
);
362 free(ordered_hashmap_remove(bottom
, basename(p
)));
363 r
= ordered_hashmap_put(bottom
, basename(p
), p
);
372 static bool should_skip_path(const char *prefix
, const char *suffix
) {
374 _cleanup_free_
char *target
= NULL
;
378 dirname
= strjoina(prefix
, "/", suffix
);
380 if (chase_symlinks(dirname
, NULL
, 0, &target
) < 0)
383 NULSTR_FOREACH(p
, prefixes
) {
384 if (path_startswith(dirname
, p
))
387 if (path_equal(target
, strjoina(p
, "/", suffix
))) {
388 log_debug("%s redirects to %s, skipping.", dirname
, target
);
396 static int process_suffix(const char *suffix
, const char *onlyprefix
) {
399 OrderedHashmap
*top
, *bottom
, *drops
;
408 assert(!startswith(suffix
, "/"));
409 assert(!strstr(suffix
, "//"));
411 dropins
= nulstr_contains(have_dropins
, suffix
);
413 top
= ordered_hashmap_new(&string_hash_ops
);
414 bottom
= ordered_hashmap_new(&string_hash_ops
);
415 drops
= ordered_hashmap_new(&string_hash_ops
);
416 if (!top
|| !bottom
|| !drops
) {
421 NULSTR_FOREACH(p
, prefixes
) {
422 _cleanup_free_
char *t
= NULL
;
424 if (should_skip_path(p
, suffix
))
427 t
= strjoin(p
, "/", suffix
);
433 k
= enumerate_dir(top
, bottom
, drops
, t
, dropins
);
438 ORDERED_HASHMAP_FOREACH_KEY(f
, key
, top
, i
) {
441 o
= ordered_hashmap_get(bottom
, key
);
444 if (!onlyprefix
|| startswith(o
, onlyprefix
)) {
445 if (path_equal(o
, f
)) {
446 notify_override_unchanged(f
);
448 k
= found_override(f
, o
);
456 h
= ordered_hashmap_get(drops
, key
);
458 ORDERED_HASHMAP_FOREACH(o
, h
, j
)
459 if (!onlyprefix
|| startswith(o
, onlyprefix
))
460 n_found
+= notify_override_extended(f
, o
);
464 ordered_hashmap_free_free(top
);
465 ordered_hashmap_free_free(bottom
);
467 ORDERED_HASHMAP_FOREACH_KEY(h
, key
, drops
, i
) {
468 ordered_hashmap_free_free(ordered_hashmap_remove(drops
, key
));
469 ordered_hashmap_remove(drops
, key
);
472 ordered_hashmap_free(drops
);
474 return r
< 0 ? r
: n_found
;
477 static int process_suffixes(const char *onlyprefix
) {
481 NULSTR_FOREACH(n
, suffixes
) {
482 r
= process_suffix(n
, onlyprefix
);
492 static int process_suffix_chop(const char *arg
) {
497 if (!path_is_absolute(arg
))
498 return process_suffix(arg
, NULL
);
500 /* Strip prefix from the suffix */
501 NULSTR_FOREACH(p
, prefixes
) {
504 suffix
= startswith(arg
, p
);
506 suffix
+= strspn(suffix
, "/");
508 return process_suffix(suffix
, p
);
510 return process_suffixes(arg
);
514 log_error("Invalid suffix specification %s.", arg
);
518 static void help(void) {
519 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
520 "Find overridden configuration files.\n\n"
521 " -h --help Show this help\n"
522 " --version Show package version\n"
523 " --no-pager Do not pipe output into a pager\n"
524 " --diff[=1|0] Show a diff when overridden files differ\n"
525 " -t --type=LIST... Only display a selected set of override types\n"
526 , program_invocation_short_name
);
529 static int parse_flags(const char *flag_str
, int flags
) {
530 const char *word
, *state
;
533 FOREACH_WORD_SEPARATOR(word
, l
, flag_str
, ",", state
) {
534 if (strneq("masked", word
, l
))
535 flags
|= SHOW_MASKED
;
536 else if (strneq ("equivalent", word
, l
))
537 flags
|= SHOW_EQUIVALENT
;
538 else if (strneq("redirected", word
, l
))
539 flags
|= SHOW_REDIRECTED
;
540 else if (strneq("overridden", word
, l
))
541 flags
|= SHOW_OVERRIDDEN
;
542 else if (strneq("unchanged", word
, l
))
543 flags
|= SHOW_UNCHANGED
;
544 else if (strneq("extended", word
, l
))
545 flags
|= SHOW_EXTENDED
;
546 else if (strneq("default", word
, l
))
547 flags
|= SHOW_DEFAULTS
;
554 static int parse_argv(int argc
, char *argv
[]) {
557 ARG_NO_PAGER
= 0x100,
562 static const struct option options
[] = {
563 { "help", no_argument
, NULL
, 'h' },
564 { "version", no_argument
, NULL
, ARG_VERSION
},
565 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
566 { "diff", optional_argument
, NULL
, ARG_DIFF
},
567 { "type", required_argument
, NULL
, 't' },
576 while ((c
= getopt_long(argc
, argv
, "ht:", options
, NULL
)) >= 0)
593 f
= parse_flags(optarg
, arg_flags
);
595 log_error("Failed to parse flags field.");
608 b
= parse_boolean(optarg
);
610 log_error("Failed to parse diff boolean.");
622 assert_not_reached("Unhandled option");
628 int main(int argc
, char *argv
[]) {
629 int r
, k
, n_found
= 0;
631 log_parse_environment();
634 r
= parse_argv(argc
, argv
);
639 arg_flags
= SHOW_DEFAULTS
;
642 arg_diff
= !!(arg_flags
& SHOW_OVERRIDDEN
);
644 arg_flags
|= SHOW_OVERRIDDEN
;
646 (void) pager_open(arg_no_pager
, false);
651 for (i
= optind
; i
< argc
; i
++) {
652 path_simplify(argv
[i
], false);
654 k
= process_suffix_chop(argv
[i
]);
662 k
= process_suffixes(NULL
);
670 printf("%s%i overridden configuration files found.\n", n_found
? "\n" : "", n_found
);
675 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;