]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/delta/delta.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[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-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, *key;
405 OrderedHashmap *top, *bottom, *drops, *h;
406 int r = 0, k, n_found = 0;
407 bool dropins;
408
409 assert(suffix);
410 assert(!startswith(suffix, "/"));
411 assert(!strstr(suffix, "//"));
412
413 dropins = nulstr_contains(have_dropins, suffix);
414
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) {
419 r = -ENOMEM;
420 goto finish;
421 }
422
423 NULSTR_FOREACH(p, prefixes) {
424 _cleanup_free_ char *t = NULL;
425
426 if (should_skip_path(p, suffix) > 0)
427 continue;
428
429 t = path_join(p, suffix);
430 if (!t) {
431 r = -ENOMEM;
432 goto finish;
433 }
434
435 k = enumerate_dir(top, bottom, drops, t, dropins);
436 if (r == 0)
437 r = k;
438 }
439
440 ORDERED_HASHMAP_FOREACH_KEY(f, key, top) {
441 char *o;
442
443 o = ordered_hashmap_get(bottom, key);
444 assert(o);
445
446 if (!onlyprefix || startswith(o, onlyprefix)) {
447 if (path_equal(o, f)) {
448 notify_override_unchanged(f);
449 } else {
450 k = found_override(f, o);
451 if (k < 0)
452 r = k;
453 else
454 n_found += k;
455 }
456 }
457
458 h = ordered_hashmap_get(drops, key);
459 if (h)
460 ORDERED_HASHMAP_FOREACH(o, h)
461 if (!onlyprefix || startswith(o, onlyprefix))
462 n_found += notify_override_extended(f, o);
463 }
464
465 finish:
466 ordered_hashmap_free_free(top);
467 ordered_hashmap_free_free(bottom);
468
469 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops) {
470 ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
471 ordered_hashmap_remove(drops, key);
472 free(key);
473 }
474 ordered_hashmap_free(drops);
475
476 return r < 0 ? r : n_found;
477 }
478
479 static int process_suffixes(const char *onlyprefix) {
480 const char *n;
481 int n_found = 0, r;
482
483 NULSTR_FOREACH(n, suffixes) {
484 r = process_suffix(n, onlyprefix);
485 if (r < 0)
486 return r;
487
488 n_found += r;
489 }
490
491 return n_found;
492 }
493
494 static int process_suffix_chop(const char *arg) {
495 const char *p;
496
497 assert(arg);
498
499 if (!path_is_absolute(arg))
500 return process_suffix(arg, NULL);
501
502 /* Strip prefix from the suffix */
503 NULSTR_FOREACH(p, prefixes) {
504 const char *suffix;
505
506 suffix = startswith(arg, p);
507 if (suffix) {
508 suffix += strspn(suffix, "/");
509 if (*suffix)
510 return process_suffix(suffix, p);
511 else
512 return process_suffixes(arg);
513 }
514 }
515
516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
517 "Invalid suffix specification %s.", arg);
518 }
519
520 static int help(void) {
521 _cleanup_free_ char *link = NULL;
522 int r;
523
524 r = terminal_urlify_man("systemd-delta", "1", &link);
525 if (r < 0)
526 return log_oom();
527
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
537 , link
538 );
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;
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 if (!optarg)
621 arg_diff = 1;
622 else {
623 int b;
624
625 b = parse_boolean(optarg);
626 if (b < 0)
627 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
628 "Failed to parse diff boolean.");
629
630 arg_diff = b;
631 }
632 break;
633
634 case '?':
635 return -EINVAL;
636
637 default:
638 assert_not_reached("Unhandled option");
639 }
640
641 return 1;
642 }
643
644 static int run(int argc, char *argv[]) {
645 int r, k, n_found = 0;
646
647 log_setup_cli();
648
649 r = parse_argv(argc, argv);
650 if (r <= 0)
651 return r;
652
653 if (arg_flags == 0)
654 arg_flags = SHOW_DEFAULTS;
655
656 if (arg_diff < 0)
657 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
658 else if (arg_diff)
659 arg_flags |= SHOW_OVERRIDDEN;
660
661 (void) pager_open(arg_pager_flags);
662
663 if (optind < argc) {
664 int i;
665
666 for (i = optind; i < argc; i++) {
667 path_simplify(argv[i], false);
668
669 k = process_suffix_chop(argv[i]);
670 if (k < 0)
671 r = k;
672 else
673 n_found += k;
674 }
675
676 } else {
677 k = process_suffixes(NULL);
678 if (k < 0)
679 r = k;
680 else
681 n_found += k;
682 }
683
684 if (r >= 0)
685 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
686 return r;
687 }
688
689 DEFINE_MAIN_FUNCTION(run);