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