]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/delta/delta.c
tree-wide: drop string.h when string-util.h or friends are included
[thirdparty/systemd.git] / src / delta / delta.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <sys/prctl.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "dirent-util.h"
10 #include "fd-util.h"
11 #include "fs-util.h"
12 #include "hashmap.h"
13 #include "locale-util.h"
14 #include "log.h"
15 #include "main-func.h"
16 #include "nulstr-util.h"
17 #include "pager.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"
25 #include "strv.h"
26 #include "terminal-util.h"
27
28 static const char prefixes[] =
29 "/etc\0"
30 "/run\0"
31 "/usr/local/lib\0"
32 "/usr/local/share\0"
33 "/usr/lib\0"
34 "/usr/share\0"
35 #if HAVE_SPLIT_USR
36 "/lib\0"
37 #endif
38 ;
39
40 static const char suffixes[] =
41 "sysctl.d\0"
42 "tmpfiles.d\0"
43 "modules-load.d\0"
44 "binfmt.d\0"
45 "systemd/system\0"
46 "systemd/user\0"
47 "systemd/system-preset\0"
48 "systemd/user-preset\0"
49 "udev/rules.d\0"
50 "modprobe.d\0";
51
52 static const char have_dropins[] =
53 "systemd/system\0"
54 "systemd/user\0";
55
56 static PagerFlags arg_pager_flags = 0;
57 static int arg_diff = -1;
58
59 static enum {
60 SHOW_MASKED = 1 << 0,
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,
66
67 SHOW_DEFAULTS =
68 (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED)
69 } arg_flags = 0;
70
71 static int equivalent(const char *a, const char *b) {
72 _cleanup_free_ char *x = NULL, *y = NULL;
73 int r;
74
75 r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x, NULL);
76 if (r < 0)
77 return r;
78
79 r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y, NULL);
80 if (r < 0)
81 return r;
82
83 return path_equal(x, y);
84 }
85
86 static int notify_override_masked(const char *top, const char *bottom) {
87 if (!(arg_flags & SHOW_MASKED))
88 return 0;
89
90 printf("%s%s%s %s %s %s\n",
91 ansi_highlight_red(), "[MASKED]", ansi_normal(),
92 top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
93 return 1;
94 }
95
96 static int notify_override_equivalent(const char *top, const char *bottom) {
97 if (!(arg_flags & SHOW_EQUIVALENT))
98 return 0;
99
100 printf("%s%s%s %s %s %s\n",
101 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
102 top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
103 return 1;
104 }
105
106 static int notify_override_redirected(const char *top, const char *bottom) {
107 if (!(arg_flags & SHOW_REDIRECTED))
108 return 0;
109
110 printf("%s%s%s %s %s %s\n",
111 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
112 top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
113 return 1;
114 }
115
116 static int notify_override_overridden(const char *top, const char *bottom) {
117 if (!(arg_flags & SHOW_OVERRIDDEN))
118 return 0;
119
120 printf("%s%s%s %s %s %s\n",
121 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
122 top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
123 return 1;
124 }
125
126 static int notify_override_extended(const char *top, const char *bottom) {
127 if (!(arg_flags & SHOW_EXTENDED))
128 return 0;
129
130 printf("%s%s%s %s %s %s\n",
131 ansi_highlight(), "[EXTENDED]", ansi_normal(),
132 top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
133 return 1;
134 }
135
136 static int notify_override_unchanged(const char *f) {
137 if (!(arg_flags & SHOW_UNCHANGED))
138 return 0;
139
140 printf("[UNCHANGED] %s\n", f);
141 return 1;
142 }
143
144 static int found_override(const char *top, const char *bottom) {
145 _cleanup_free_ char *dest = NULL;
146 pid_t pid;
147 int r;
148
149 assert(top);
150 assert(bottom);
151
152 if (null_or_empty_path(top) > 0)
153 return notify_override_masked(top, bottom);
154
155 r = readlink_malloc(top, &dest);
156 if (r >= 0) {
157 if (equivalent(dest, bottom) > 0)
158 return notify_override_equivalent(top, bottom);
159 else
160 return notify_override_redirected(top, bottom);
161 }
162
163 r = notify_override_overridden(top, bottom);
164 if (!arg_diff)
165 return r;
166
167 putchar('\n');
168
169 fflush(stdout);
170
171 r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
172 if (r < 0)
173 return r;
174 if (r == 0) {
175 execlp("diff", "diff", "-us", "--", bottom, top, NULL);
176 log_open();
177 log_error_errno(errno, "Failed to execute diff: %m");
178 _exit(EXIT_FAILURE);
179 }
180
181 (void) wait_for_terminate_and_check("diff", pid, WAIT_LOG_ABNORMAL);
182 putchar('\n');
183
184 return r;
185 }
186
187 static int enumerate_dir_d(
188 OrderedHashmap *top,
189 OrderedHashmap *bottom,
190 OrderedHashmap *drops,
191 const char *toppath, const char *drop) {
192
193 _cleanup_free_ char *unit = NULL;
194 _cleanup_free_ char *path = NULL;
195 _cleanup_strv_free_ char **list = NULL;
196 char **file;
197 char *c;
198 int r;
199
200 assert(!endswith(drop, "/"));
201
202 path = path_join(toppath, drop);
203 if (!path)
204 return -ENOMEM;
205
206 log_debug("Looking at %s", path);
207
208 unit = strdup(drop);
209 if (!unit)
210 return -ENOMEM;
211
212 c = strrchr(unit, '.');
213 if (!c)
214 return -EINVAL;
215 *c = 0;
216
217 r = get_files_in_directory(path, &list);
218 if (r < 0)
219 return log_error_errno(r, "Failed to enumerate %s: %m", path);
220
221 strv_sort(list);
222
223 STRV_FOREACH(file, list) {
224 OrderedHashmap *h;
225 int k;
226 char *p;
227 char *d;
228
229 if (!endswith(*file, ".conf"))
230 continue;
231
232 p = path_join(path, *file);
233 if (!p)
234 return -ENOMEM;
235 d = p + strlen(toppath) + 1;
236
237 log_debug("Adding at top: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW), p);
238 k = ordered_hashmap_put(top, d, p);
239 if (k >= 0) {
240 p = strdup(p);
241 if (!p)
242 return -ENOMEM;
243 d = p + strlen(toppath) + 1;
244 } else if (k != -EEXIST) {
245 free(p);
246 return k;
247 }
248
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);
252 if (k < 0) {
253 free(p);
254 return k;
255 }
256
257 h = ordered_hashmap_get(drops, unit);
258 if (!h) {
259 h = ordered_hashmap_new(&string_hash_ops);
260 if (!h)
261 return -ENOMEM;
262 ordered_hashmap_put(drops, unit, h);
263 unit = strdup(unit);
264 if (!unit)
265 return -ENOMEM;
266 }
267
268 p = strdup(p);
269 if (!p)
270 return -ENOMEM;
271
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);
275 if (k < 0) {
276 free(p);
277 if (k != -EEXIST)
278 return k;
279 }
280 }
281 return 0;
282 }
283
284 static int enumerate_dir(
285 OrderedHashmap *top,
286 OrderedHashmap *bottom,
287 OrderedHashmap *drops,
288 const char *path, bool dropins) {
289
290 _cleanup_closedir_ DIR *d = NULL;
291 struct dirent *de;
292 _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
293 size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0;
294 char **t;
295 int r;
296
297 assert(top);
298 assert(bottom);
299 assert(drops);
300 assert(path);
301
302 log_debug("Looking at %s", path);
303
304 d = opendir(path);
305 if (!d) {
306 if (errno == ENOENT)
307 return 0;
308
309 return log_error_errno(errno, "Failed to open %s: %m", path);
310 }
311
312 FOREACH_DIRENT_ALL(de, d, return -errno) {
313 dirent_ensure_type(d, de);
314
315 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
316 if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2))
317 return -ENOMEM;
318
319 dirs[n_dirs] = strdup(de->d_name);
320 if (!dirs[n_dirs])
321 return -ENOMEM;
322 n_dirs ++;
323 }
324
325 if (!dirent_is_file(de))
326 continue;
327
328 if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2))
329 return -ENOMEM;
330
331 files[n_files] = strdup(de->d_name);
332 if (!files[n_files])
333 return -ENOMEM;
334 n_files ++;
335 }
336
337 strv_sort(dirs);
338 strv_sort(files);
339
340 STRV_FOREACH(t, dirs) {
341 r = enumerate_dir_d(top, bottom, drops, path, *t);
342 if (r < 0)
343 return r;
344 }
345
346 STRV_FOREACH(t, files) {
347 _cleanup_free_ char *p = NULL;
348
349 p = path_join(path, *t);
350 if (!p)
351 return -ENOMEM;
352
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);
355 if (r >= 0) {
356 p = strdup(p);
357 if (!p)
358 return -ENOMEM;
359 } else if (r != -EEXIST)
360 return r;
361
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);
365 if (r < 0)
366 return r;
367 p = NULL;
368 }
369
370 return 0;
371 }
372
373 static int should_skip_path(const char *prefix, const char *suffix) {
374 #if HAVE_SPLIT_USR
375 _cleanup_free_ char *target = NULL;
376 const char *dirname, *p;
377
378 dirname = prefix_roota(prefix, suffix);
379
380 if (chase_symlinks(dirname, NULL, 0, &target, NULL) < 0)
381 return false;
382
383 NULSTR_FOREACH(p, prefixes) {
384 _cleanup_free_ char *tmp = NULL;
385
386 if (path_startswith(dirname, p))
387 continue;
388
389 tmp = path_join(p, suffix);
390 if (!tmp)
391 return -ENOMEM;
392
393 if (path_equal(target, tmp)) {
394 log_debug("%s redirects to %s, skipping.", dirname, target);
395 return true;
396 }
397 }
398 #endif
399 return false;
400 }
401
402 static int process_suffix(const char *suffix, const char *onlyprefix) {
403 const char *p;
404 char *f;
405 OrderedHashmap *top, *bottom, *drops;
406 OrderedHashmap *h;
407 char *key;
408 int r = 0, k;
409 Iterator i, j;
410 int n_found = 0;
411 bool dropins;
412
413 assert(suffix);
414 assert(!startswith(suffix, "/"));
415 assert(!strstr(suffix, "//"));
416
417 dropins = nulstr_contains(have_dropins, suffix);
418
419 top = ordered_hashmap_new(&string_hash_ops);
420 bottom = ordered_hashmap_new(&string_hash_ops);
421 drops = ordered_hashmap_new(&string_hash_ops);
422 if (!top || !bottom || !drops) {
423 r = -ENOMEM;
424 goto finish;
425 }
426
427 NULSTR_FOREACH(p, prefixes) {
428 _cleanup_free_ char *t = NULL;
429
430 if (should_skip_path(p, suffix) > 0)
431 continue;
432
433 t = path_join(p, suffix);
434 if (!t) {
435 r = -ENOMEM;
436 goto finish;
437 }
438
439 k = enumerate_dir(top, bottom, drops, t, dropins);
440 if (r == 0)
441 r = k;
442 }
443
444 ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
445 char *o;
446
447 o = ordered_hashmap_get(bottom, key);
448 assert(o);
449
450 if (!onlyprefix || startswith(o, onlyprefix)) {
451 if (path_equal(o, f)) {
452 notify_override_unchanged(f);
453 } else {
454 k = found_override(f, o);
455 if (k < 0)
456 r = k;
457 else
458 n_found += k;
459 }
460 }
461
462 h = ordered_hashmap_get(drops, key);
463 if (h)
464 ORDERED_HASHMAP_FOREACH(o, h, j)
465 if (!onlyprefix || startswith(o, onlyprefix))
466 n_found += notify_override_extended(f, o);
467 }
468
469 finish:
470 ordered_hashmap_free_free(top);
471 ordered_hashmap_free_free(bottom);
472
473 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
474 ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
475 ordered_hashmap_remove(drops, key);
476 free(key);
477 }
478 ordered_hashmap_free(drops);
479
480 return r < 0 ? r : n_found;
481 }
482
483 static int process_suffixes(const char *onlyprefix) {
484 const char *n;
485 int n_found = 0, r;
486
487 NULSTR_FOREACH(n, suffixes) {
488 r = process_suffix(n, onlyprefix);
489 if (r < 0)
490 return r;
491
492 n_found += r;
493 }
494
495 return n_found;
496 }
497
498 static int process_suffix_chop(const char *arg) {
499 const char *p;
500
501 assert(arg);
502
503 if (!path_is_absolute(arg))
504 return process_suffix(arg, NULL);
505
506 /* Strip prefix from the suffix */
507 NULSTR_FOREACH(p, prefixes) {
508 const char *suffix;
509
510 suffix = startswith(arg, p);
511 if (suffix) {
512 suffix += strspn(suffix, "/");
513 if (*suffix)
514 return process_suffix(suffix, p);
515 else
516 return process_suffixes(arg);
517 }
518 }
519
520 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
521 "Invalid suffix specification %s.", arg);
522 }
523
524 static int help(void) {
525 _cleanup_free_ char *link = NULL;
526 int r;
527
528 r = terminal_urlify_man("systemd-delta", "1", &link);
529 if (r < 0)
530 return log_oom();
531
532 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
533 "Find overridden configuration files.\n\n"
534 " -h --help Show this help\n"
535 " --version Show package version\n"
536 " --no-pager Do not pipe output into a pager\n"
537 " --diff[=1|0] Show a diff when overridden files differ\n"
538 " -t --type=LIST... Only display a selected set of override types\n"
539 "\nSee the %s for details.\n"
540 , program_invocation_short_name
541 , link
542 );
543
544 return 0;
545 }
546
547 static int parse_flags(const char *flag_str, int flags) {
548 const char *word, *state;
549 size_t l;
550
551 FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) {
552 if (strneq("masked", word, l))
553 flags |= SHOW_MASKED;
554 else if (strneq ("equivalent", word, l))
555 flags |= SHOW_EQUIVALENT;
556 else if (strneq("redirected", word, l))
557 flags |= SHOW_REDIRECTED;
558 else if (strneq("overridden", word, l))
559 flags |= SHOW_OVERRIDDEN;
560 else if (strneq("unchanged", word, l))
561 flags |= SHOW_UNCHANGED;
562 else if (strneq("extended", word, l))
563 flags |= SHOW_EXTENDED;
564 else if (strneq("default", word, l))
565 flags |= SHOW_DEFAULTS;
566 else
567 return -EINVAL;
568 }
569 return flags;
570 }
571
572 static int parse_argv(int argc, char *argv[]) {
573
574 enum {
575 ARG_NO_PAGER = 0x100,
576 ARG_DIFF,
577 ARG_VERSION
578 };
579
580 static const struct option options[] = {
581 { "help", no_argument, NULL, 'h' },
582 { "version", no_argument, NULL, ARG_VERSION },
583 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
584 { "diff", optional_argument, NULL, ARG_DIFF },
585 { "type", required_argument, NULL, 't' },
586 {}
587 };
588
589 int c;
590
591 assert(argc >= 1);
592 assert(argv);
593
594 while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
595
596 switch (c) {
597
598 case 'h':
599 return help();
600
601 case ARG_VERSION:
602 return version();
603
604 case ARG_NO_PAGER:
605 arg_pager_flags |= PAGER_DISABLE;
606 break;
607
608 case 't': {
609 int f;
610 f = parse_flags(optarg, arg_flags);
611 if (f < 0)
612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
613 "Failed to parse flags field.");
614 arg_flags = f;
615 break;
616 }
617
618 case ARG_DIFF:
619 if (!optarg)
620 arg_diff = 1;
621 else {
622 int b;
623
624 b = parse_boolean(optarg);
625 if (b < 0)
626 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
627 "Failed to parse diff boolean.");
628
629 arg_diff = b;
630 }
631 break;
632
633 case '?':
634 return -EINVAL;
635
636 default:
637 assert_not_reached("Unhandled option");
638 }
639
640 return 1;
641 }
642
643 static int run(int argc, char *argv[]) {
644 int r, k, n_found = 0;
645
646 log_show_color(true);
647 log_parse_environment();
648 log_open();
649
650 r = parse_argv(argc, argv);
651 if (r <= 0)
652 return r;
653
654 if (arg_flags == 0)
655 arg_flags = SHOW_DEFAULTS;
656
657 if (arg_diff < 0)
658 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
659 else if (arg_diff)
660 arg_flags |= SHOW_OVERRIDDEN;
661
662 (void) pager_open(arg_pager_flags);
663
664 if (optind < argc) {
665 int i;
666
667 for (i = optind; i < argc; i++) {
668 path_simplify(argv[i], false);
669
670 k = process_suffix_chop(argv[i]);
671 if (k < 0)
672 r = k;
673 else
674 n_found += k;
675 }
676
677 } else {
678 k = process_suffixes(NULL);
679 if (k < 0)
680 r = k;
681 else
682 n_found += k;
683 }
684
685 if (r >= 0)
686 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
687 return r;
688 }
689
690 DEFINE_MAIN_FUNCTION(run);