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