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