1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "dirent-util.h"
13 #include "locale-util.h"
15 #include "main-func.h"
16 #include "nulstr-util.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "pretty-print.h"
21 #include "process-util.h"
22 #include "signal-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
26 #include "terminal-util.h"
28 static const char prefixes
[] =
40 static const char suffixes
[] =
47 "systemd/system-preset\0"
48 "systemd/user-preset\0"
52 static const char have_dropins
[] =
56 static PagerFlags arg_pager_flags
= 0;
57 static int arg_diff
= -1;
61 SHOW_EQUIVALENT
= 1 << 1,
62 SHOW_REDIRECTED
= 1 << 2,
63 SHOW_OVERRIDDEN
= 1 << 3,
64 SHOW_UNCHANGED
= 1 << 4,
65 SHOW_EXTENDED
= 1 << 5,
68 (SHOW_MASKED
| SHOW_EQUIVALENT
| SHOW_REDIRECTED
| SHOW_OVERRIDDEN
| SHOW_EXTENDED
)
71 static int equivalent(const char *a
, const char *b
) {
72 _cleanup_free_
char *x
= NULL
, *y
= NULL
;
75 r
= chase_symlinks(a
, NULL
, CHASE_TRAIL_SLASH
, &x
, NULL
);
79 r
= chase_symlinks(b
, NULL
, CHASE_TRAIL_SLASH
, &y
, NULL
);
83 return path_equal(x
, y
);
86 static int notify_override_masked(const char *top
, const char *bottom
) {
87 if (!(arg_flags
& SHOW_MASKED
))
90 printf("%s%s%s %s %s %s\n",
91 ansi_highlight_red(), "[MASKED]", ansi_normal(),
92 top
, special_glyph(SPECIAL_GLYPH_ARROW
), bottom
);
96 static int notify_override_equivalent(const char *top
, const char *bottom
) {
97 if (!(arg_flags
& SHOW_EQUIVALENT
))
100 printf("%s%s%s %s %s %s\n",
101 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
102 top
, special_glyph(SPECIAL_GLYPH_ARROW
), bottom
);
106 static int notify_override_redirected(const char *top
, const char *bottom
) {
107 if (!(arg_flags
& SHOW_REDIRECTED
))
110 printf("%s%s%s %s %s %s\n",
111 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
112 top
, special_glyph(SPECIAL_GLYPH_ARROW
), bottom
);
116 static int notify_override_overridden(const char *top
, const char *bottom
) {
117 if (!(arg_flags
& SHOW_OVERRIDDEN
))
120 printf("%s%s%s %s %s %s\n",
121 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
122 top
, special_glyph(SPECIAL_GLYPH_ARROW
), bottom
);
126 static int notify_override_extended(const char *top
, const char *bottom
) {
127 if (!(arg_flags
& SHOW_EXTENDED
))
130 printf("%s%s%s %s %s %s\n",
131 ansi_highlight(), "[EXTENDED]", ansi_normal(),
132 top
, special_glyph(SPECIAL_GLYPH_ARROW
), bottom
);
136 static int notify_override_unchanged(const char *f
) {
137 if (!(arg_flags
& SHOW_UNCHANGED
))
140 printf("[UNCHANGED] %s\n", f
);
144 static int found_override(const char *top
, const char *bottom
) {
145 _cleanup_free_
char *dest
= NULL
;
152 if (null_or_empty_path(top
) > 0)
153 return notify_override_masked(top
, bottom
);
155 r
= readlink_malloc(top
, &dest
);
157 if (equivalent(dest
, bottom
) > 0)
158 return notify_override_equivalent(top
, bottom
);
160 return notify_override_redirected(top
, bottom
);
163 r
= notify_override_overridden(top
, bottom
);
171 r
= safe_fork("(diff)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
175 execlp("diff", "diff", "-us", "--", bottom
, top
, NULL
);
177 log_error_errno(errno
, "Failed to execute diff: %m");
181 (void) wait_for_terminate_and_check("diff", pid
, WAIT_LOG_ABNORMAL
);
187 static int enumerate_dir_d(
189 OrderedHashmap
*bottom
,
190 OrderedHashmap
*drops
,
191 const char *toppath
, const char *drop
) {
193 _cleanup_free_
char *unit
= NULL
;
194 _cleanup_free_
char *path
= NULL
;
195 _cleanup_strv_free_
char **list
= NULL
;
200 assert(!endswith(drop
, "/"));
202 path
= path_join(toppath
, drop
);
206 log_debug("Looking at %s", path
);
212 c
= strrchr(unit
, '.');
217 r
= get_files_in_directory(path
, &list
);
219 return log_error_errno(r
, "Failed to enumerate %s: %m", path
);
223 STRV_FOREACH(file
, list
) {
229 if (!endswith(*file
, ".conf"))
232 p
= path_join(path
, *file
);
235 d
= p
+ strlen(toppath
) + 1;
237 log_debug("Adding at top: %s %s %s", d
, special_glyph(SPECIAL_GLYPH_ARROW
), p
);
238 k
= ordered_hashmap_put(top
, d
, p
);
243 d
= p
+ strlen(toppath
) + 1;
244 } else if (k
!= -EEXIST
) {
249 log_debug("Adding at bottom: %s %s %s", d
, special_glyph(SPECIAL_GLYPH_ARROW
), p
);
250 free(ordered_hashmap_remove(bottom
, d
));
251 k
= ordered_hashmap_put(bottom
, d
, p
);
257 h
= ordered_hashmap_get(drops
, unit
);
259 h
= ordered_hashmap_new(&string_hash_ops
);
262 ordered_hashmap_put(drops
, unit
, h
);
272 log_debug("Adding to drops: %s %s %s %s %s",
273 unit
, special_glyph(SPECIAL_GLYPH_ARROW
), basename(p
), special_glyph(SPECIAL_GLYPH_ARROW
), p
);
274 k
= ordered_hashmap_put(h
, basename(p
), p
);
284 static int enumerate_dir(
286 OrderedHashmap
*bottom
,
287 OrderedHashmap
*drops
,
288 const char *path
, bool dropins
) {
290 _cleanup_closedir_
DIR *d
= NULL
;
292 _cleanup_strv_free_
char **files
= NULL
, **dirs
= NULL
;
293 size_t n_files
= 0, allocated_files
= 0, n_dirs
= 0, allocated_dirs
= 0;
302 log_debug("Looking at %s", path
);
309 return log_error_errno(errno
, "Failed to open %s: %m", path
);
312 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
313 dirent_ensure_type(d
, de
);
315 if (dropins
&& de
->d_type
== DT_DIR
&& endswith(de
->d_name
, ".d")) {
316 if (!GREEDY_REALLOC0(dirs
, allocated_dirs
, n_dirs
+ 2))
319 dirs
[n_dirs
] = strdup(de
->d_name
);
325 if (!dirent_is_file(de
))
328 if (!GREEDY_REALLOC0(files
, allocated_files
, n_files
+ 2))
331 files
[n_files
] = strdup(de
->d_name
);
340 STRV_FOREACH(t
, dirs
) {
341 r
= enumerate_dir_d(top
, bottom
, drops
, path
, *t
);
346 STRV_FOREACH(t
, files
) {
347 _cleanup_free_
char *p
= NULL
;
349 p
= path_join(path
, *t
);
353 log_debug("Adding at top: %s %s %s", basename(p
), special_glyph(SPECIAL_GLYPH_ARROW
), p
);
354 r
= ordered_hashmap_put(top
, basename(p
), p
);
359 } else if (r
!= -EEXIST
)
362 log_debug("Adding at bottom: %s %s %s", basename(p
), special_glyph(SPECIAL_GLYPH_ARROW
), p
);
363 free(ordered_hashmap_remove(bottom
, basename(p
)));
364 r
= ordered_hashmap_put(bottom
, basename(p
), p
);
373 static int should_skip_path(const char *prefix
, const char *suffix
) {
375 _cleanup_free_
char *target
= NULL
;
376 const char *dirname
, *p
;
378 dirname
= prefix_roota(prefix
, suffix
);
380 if (chase_symlinks(dirname
, NULL
, 0, &target
, NULL
) < 0)
383 NULSTR_FOREACH(p
, prefixes
) {
384 _cleanup_free_
char *tmp
= NULL
;
386 if (path_startswith(dirname
, p
))
389 tmp
= path_join(p
, suffix
);
393 if (path_equal(target
, tmp
)) {
394 log_debug("%s redirects to %s, skipping.", dirname
, target
);
402 static int process_suffix(const char *suffix
, const char *onlyprefix
) {
405 OrderedHashmap
*top
, *bottom
, *drops
, *h
;
406 int r
= 0, k
, n_found
= 0;
410 assert(!startswith(suffix
, "/"));
411 assert(!strstr(suffix
, "//"));
413 dropins
= nulstr_contains(have_dropins
, suffix
);
415 top
= ordered_hashmap_new(&string_hash_ops
);
416 bottom
= ordered_hashmap_new(&string_hash_ops
);
417 drops
= ordered_hashmap_new(&string_hash_ops
);
418 if (!top
|| !bottom
|| !drops
) {
423 NULSTR_FOREACH(p
, prefixes
) {
424 _cleanup_free_
char *t
= NULL
;
426 if (should_skip_path(p
, suffix
) > 0)
429 t
= path_join(p
, suffix
);
435 k
= enumerate_dir(top
, bottom
, drops
, t
, dropins
);
440 ORDERED_HASHMAP_FOREACH_KEY(f
, key
, top
) {
443 o
= ordered_hashmap_get(bottom
, key
);
446 if (!onlyprefix
|| startswith(o
, onlyprefix
)) {
447 if (path_equal(o
, f
)) {
448 notify_override_unchanged(f
);
450 k
= found_override(f
, o
);
458 h
= ordered_hashmap_get(drops
, key
);
460 ORDERED_HASHMAP_FOREACH(o
, h
)
461 if (!onlyprefix
|| startswith(o
, onlyprefix
))
462 n_found
+= notify_override_extended(f
, o
);
466 ordered_hashmap_free_free(top
);
467 ordered_hashmap_free_free(bottom
);
469 ORDERED_HASHMAP_FOREACH_KEY(h
, key
, drops
) {
470 ordered_hashmap_free_free(ordered_hashmap_remove(drops
, key
));
471 ordered_hashmap_remove(drops
, key
);
474 ordered_hashmap_free(drops
);
476 return r
< 0 ? r
: n_found
;
479 static int process_suffixes(const char *onlyprefix
) {
483 NULSTR_FOREACH(n
, suffixes
) {
484 r
= process_suffix(n
, onlyprefix
);
494 static int process_suffix_chop(const char *arg
) {
499 if (!path_is_absolute(arg
))
500 return process_suffix(arg
, NULL
);
502 /* Strip prefix from the suffix */
503 NULSTR_FOREACH(p
, prefixes
) {
506 suffix
= startswith(arg
, p
);
508 suffix
+= strspn(suffix
, "/");
510 return process_suffix(suffix
, p
);
512 return process_suffixes(arg
);
516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
517 "Invalid suffix specification %s.", arg
);
520 static int help(void) {
521 _cleanup_free_
char *link
= NULL
;
524 r
= terminal_urlify_man("systemd-delta", "1", &link
);
528 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
529 "Find overridden configuration files.\n\n"
530 " -h --help Show this help\n"
531 " --version Show package version\n"
532 " --no-pager Do not pipe output into a pager\n"
533 " --diff[=1|0] Show a diff when overridden files differ\n"
534 " -t --type=LIST... Only display a selected set of override types\n"
535 "\nSee the %s for details.\n"
536 , program_invocation_short_name
543 static int parse_flags(const char *flag_str
, int flags
) {
545 _cleanup_free_
char *word
= NULL
;
548 r
= extract_first_word(&flag_str
, &word
, ",", EXTRACT_DONT_COALESCE_SEPARATORS
);
554 if (streq(word
, "masked"))
555 flags
|= SHOW_MASKED
;
556 else if (streq(word
, "equivalent"))
557 flags
|= SHOW_EQUIVALENT
;
558 else if (streq(word
, "redirected"))
559 flags
|= SHOW_REDIRECTED
;
560 else if (streq(word
, "overridden"))
561 flags
|= SHOW_OVERRIDDEN
;
562 else if (streq(word
, "unchanged"))
563 flags
|= SHOW_UNCHANGED
;
564 else if (streq(word
, "extended"))
565 flags
|= SHOW_EXTENDED
;
566 else if (streq(word
, "default"))
567 flags
|= SHOW_DEFAULTS
;
573 static int parse_argv(int argc
, char *argv
[]) {
576 ARG_NO_PAGER
= 0x100,
581 static const struct option options
[] = {
582 { "help", no_argument
, NULL
, 'h' },
583 { "version", no_argument
, NULL
, ARG_VERSION
},
584 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
585 { "diff", optional_argument
, NULL
, ARG_DIFF
},
586 { "type", required_argument
, NULL
, 't' },
595 while ((c
= getopt_long(argc
, argv
, "ht:", options
, NULL
)) >= 0)
606 arg_pager_flags
|= PAGER_DISABLE
;
611 f
= parse_flags(optarg
, arg_flags
);
613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
614 "Failed to parse flags field.");
625 b
= parse_boolean(optarg
);
627 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
628 "Failed to parse diff boolean.");
638 assert_not_reached("Unhandled option");
644 static int run(int argc
, char *argv
[]) {
645 int r
, k
, n_found
= 0;
649 r
= parse_argv(argc
, argv
);
654 arg_flags
= SHOW_DEFAULTS
;
657 arg_diff
= !!(arg_flags
& SHOW_OVERRIDDEN
);
659 arg_flags
|= SHOW_OVERRIDDEN
;
661 (void) pager_open(arg_pager_flags
);
666 for (i
= optind
; i
< argc
; i
++) {
667 path_simplify(argv
[i
], false);
669 k
= process_suffix_chop(argv
[i
]);
677 k
= process_suffixes(NULL
);
685 printf("%s%i overridden configuration files found.\n", n_found
? "\n" : "", n_found
);
689 DEFINE_MAIN_FUNCTION(run
);