]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/delta/delta.c
delta: define main through macro
[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 <string.h>
6 #include <sys/prctl.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "dirent-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "hashmap.h"
14 #include "locale-util.h"
15 #include "log.h"
16 #include "main-func.h"
17 #include "pager.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "process-util.h"
21 #include "signal-util.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "terminal-util.h"
26 #include "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);
76 if (r < 0)
77 return r;
78
79 r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y);
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(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(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(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(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(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_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 = strjoin(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 = strjoin(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(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(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(ARROW), basename(p), 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 = strjoin(path, "/", *t);
350 if (!p)
351 return -ENOMEM;
352
353 log_debug("Adding at top: %s %s %s", basename(p), 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(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 bool should_skip_path(const char *prefix, const char *suffix) {
374 #if HAVE_SPLIT_USR
375 _cleanup_free_ char *target = NULL;
376 const char *p;
377 char *dirname;
378
379 dirname = strjoina(prefix, "/", suffix);
380
381 if (chase_symlinks(dirname, NULL, 0, &target) < 0)
382 return false;
383
384 NULSTR_FOREACH(p, prefixes) {
385 if (path_startswith(dirname, p))
386 continue;
387
388 if (path_equal(target, strjoina(p, "/", suffix))) {
389 log_debug("%s redirects to %s, skipping.", dirname, target);
390 return true;
391 }
392 }
393 #endif
394 return false;
395 }
396
397 static int process_suffix(const char *suffix, const char *onlyprefix) {
398 const char *p;
399 char *f;
400 OrderedHashmap *top, *bottom, *drops;
401 OrderedHashmap *h;
402 char *key;
403 int r = 0, k;
404 Iterator i, j;
405 int n_found = 0;
406 bool dropins;
407
408 assert(suffix);
409 assert(!startswith(suffix, "/"));
410 assert(!strstr(suffix, "//"));
411
412 dropins = nulstr_contains(have_dropins, suffix);
413
414 top = ordered_hashmap_new(&string_hash_ops);
415 bottom = ordered_hashmap_new(&string_hash_ops);
416 drops = ordered_hashmap_new(&string_hash_ops);
417 if (!top || !bottom || !drops) {
418 r = -ENOMEM;
419 goto finish;
420 }
421
422 NULSTR_FOREACH(p, prefixes) {
423 _cleanup_free_ char *t = NULL;
424
425 if (should_skip_path(p, suffix))
426 continue;
427
428 t = strjoin(p, "/", suffix);
429 if (!t) {
430 r = -ENOMEM;
431 goto finish;
432 }
433
434 k = enumerate_dir(top, bottom, drops, t, dropins);
435 if (r == 0)
436 r = k;
437 }
438
439 ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
440 char *o;
441
442 o = ordered_hashmap_get(bottom, key);
443 assert(o);
444
445 if (!onlyprefix || startswith(o, onlyprefix)) {
446 if (path_equal(o, f)) {
447 notify_override_unchanged(f);
448 } else {
449 k = found_override(f, o);
450 if (k < 0)
451 r = k;
452 else
453 n_found += k;
454 }
455 }
456
457 h = ordered_hashmap_get(drops, key);
458 if (h)
459 ORDERED_HASHMAP_FOREACH(o, h, j)
460 if (!onlyprefix || startswith(o, onlyprefix))
461 n_found += notify_override_extended(f, o);
462 }
463
464 finish:
465 ordered_hashmap_free_free(top);
466 ordered_hashmap_free_free(bottom);
467
468 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
469 ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
470 ordered_hashmap_remove(drops, key);
471 free(key);
472 }
473 ordered_hashmap_free(drops);
474
475 return r < 0 ? r : n_found;
476 }
477
478 static int process_suffixes(const char *onlyprefix) {
479 const char *n;
480 int n_found = 0, r;
481
482 NULSTR_FOREACH(n, suffixes) {
483 r = process_suffix(n, onlyprefix);
484 if (r < 0)
485 return r;
486
487 n_found += r;
488 }
489
490 return n_found;
491 }
492
493 static int process_suffix_chop(const char *arg) {
494 const char *p;
495
496 assert(arg);
497
498 if (!path_is_absolute(arg))
499 return process_suffix(arg, NULL);
500
501 /* Strip prefix from the suffix */
502 NULSTR_FOREACH(p, prefixes) {
503 const char *suffix;
504
505 suffix = startswith(arg, p);
506 if (suffix) {
507 suffix += strspn(suffix, "/");
508 if (*suffix)
509 return process_suffix(suffix, p);
510 else
511 return process_suffixes(arg);
512 }
513 }
514
515 log_error("Invalid suffix specification %s.", arg);
516 return -EINVAL;
517 }
518
519 static int help(void) {
520 _cleanup_free_ char *link = NULL;
521 int r;
522
523 r = terminal_urlify_man("systemd-delta", "1", &link);
524 if (r < 0)
525 return log_oom();
526
527 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
528 "Find overridden configuration files.\n\n"
529 " -h --help Show this help\n"
530 " --version Show package version\n"
531 " --no-pager Do not pipe output into a pager\n"
532 " --diff[=1|0] Show a diff when overridden files differ\n"
533 " -t --type=LIST... Only display a selected set of override types\n"
534 "\nSee the %s for details.\n"
535 , program_invocation_short_name
536 , link
537 );
538
539 return 0;
540 }
541
542 static int parse_flags(const char *flag_str, int flags) {
543 const char *word, *state;
544 size_t l;
545
546 FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) {
547 if (strneq("masked", word, l))
548 flags |= SHOW_MASKED;
549 else if (strneq ("equivalent", word, l))
550 flags |= SHOW_EQUIVALENT;
551 else if (strneq("redirected", word, l))
552 flags |= SHOW_REDIRECTED;
553 else if (strneq("overridden", word, l))
554 flags |= SHOW_OVERRIDDEN;
555 else if (strneq("unchanged", word, l))
556 flags |= SHOW_UNCHANGED;
557 else if (strneq("extended", word, l))
558 flags |= SHOW_EXTENDED;
559 else if (strneq("default", word, l))
560 flags |= SHOW_DEFAULTS;
561 else
562 return -EINVAL;
563 }
564 return flags;
565 }
566
567 static int parse_argv(int argc, char *argv[]) {
568
569 enum {
570 ARG_NO_PAGER = 0x100,
571 ARG_DIFF,
572 ARG_VERSION
573 };
574
575 static const struct option options[] = {
576 { "help", no_argument, NULL, 'h' },
577 { "version", no_argument, NULL, ARG_VERSION },
578 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
579 { "diff", optional_argument, NULL, ARG_DIFF },
580 { "type", required_argument, NULL, 't' },
581 {}
582 };
583
584 int c;
585
586 assert(argc >= 1);
587 assert(argv);
588
589 while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
590
591 switch (c) {
592
593 case 'h':
594 return help();
595
596 case ARG_VERSION:
597 return version();
598
599 case ARG_NO_PAGER:
600 arg_pager_flags |= PAGER_DISABLE;
601 break;
602
603 case 't': {
604 int f;
605 f = parse_flags(optarg, arg_flags);
606 if (f < 0) {
607 log_error("Failed to parse flags field.");
608 return -EINVAL;
609 }
610 arg_flags = f;
611 break;
612 }
613
614 case ARG_DIFF:
615 if (!optarg)
616 arg_diff = 1;
617 else {
618 int b;
619
620 b = parse_boolean(optarg);
621 if (b < 0) {
622 log_error("Failed to parse diff boolean.");
623 return -EINVAL;
624 }
625
626 arg_diff = b;
627 }
628 break;
629
630 case '?':
631 return -EINVAL;
632
633 default:
634 assert_not_reached("Unhandled option");
635 }
636
637 return 1;
638 }
639
640 static int run(int argc, char *argv[]) {
641 int r, k, n_found = 0;
642
643 log_parse_environment();
644 log_open();
645
646 r = parse_argv(argc, argv);
647 if (r <= 0)
648 return r;
649
650 if (arg_flags == 0)
651 arg_flags = SHOW_DEFAULTS;
652
653 if (arg_diff < 0)
654 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
655 else if (arg_diff)
656 arg_flags |= SHOW_OVERRIDDEN;
657
658 (void) pager_open(arg_pager_flags);
659
660 if (optind < argc) {
661 int i;
662
663 for (i = optind; i < argc; i++) {
664 path_simplify(argv[i], false);
665
666 k = process_suffix_chop(argv[i]);
667 if (k < 0)
668 r = k;
669 else
670 n_found += k;
671 }
672
673 } else {
674 k = process_suffixes(NULL);
675 if (k < 0)
676 r = k;
677 else
678 n_found += k;
679 }
680
681 if (r >= 0)
682 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
683 return r;
684 }
685
686 DEFINE_MAIN_FUNCTION(run);