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