Debian Codegraph @

src/delta/delta.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "chase.h"
8 #include "dirent-util.h"
9 #include "errno-util.h"
10 #include "extract-word.h"
11 #include "fd-util.h"
12 #include "format-table.h"
13 #include "fs-util.h"
14 #include "glyph-util.h"
15 #include "hashmap.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "nulstr-util.h"
19 #include "options.h"
20 #include "pager.h"
21 #include "parse-argument.h"
22 #include "path-util.h"
23 #include "pidref.h"
24 #include "pretty-print.h"
25 #include "process-util.h"
26 #include "stat-util.h"
27 #include "string-util.h"
28 #include "strv.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 ;
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(a, NULL, CHASE_TRAIL_SLASH, &x, NULL);
75 if (r < 0)
76 return r;
77
78 r = chase(b, NULL, CHASE_TRAIL_SLASH, &y, NULL);
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, glyph(GLYPH_ARROW_RIGHT), 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, glyph(GLYPH_ARROW_RIGHT), 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, glyph(GLYPH_ARROW_RIGHT), 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, glyph(GLYPH_ARROW_RIGHT), 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, glyph(GLYPH_ARROW_RIGHT), 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 int r;
146
147 assert(top);
148 assert(bottom);
149
150 if (null_or_empty_path(top) > 0)
151 return notify_override_masked(top, bottom);
152
153 r = readlink_malloc(top, &dest);
154 if (r >= 0) {
155 if (equivalent(dest, bottom) > 0)
156 return notify_override_equivalent(top, bottom);
157 else
158 return notify_override_redirected(top, bottom);
159 }
160
161 r = notify_override_overridden(top, bottom);
162 if (!arg_diff)
163 return r;
164
165 putchar('\n');
166
167 fflush(stdout);
168
169 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
170 r = pidref_safe_fork(
171 "(diff)",
172 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG,
173 &pidref);
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) pidref_wait_for_terminate_and_check("diff", &pidref, WAIT_LOG_ABNORMAL);
184 putchar('\n');
185
186 return r;
187 }
188
189 DEFINE_PRIVATE_HASH_OPS_FULL(
190 drop_hash_ops,
191 char, string_hash_func, string_compare_func, free,
192 OrderedHashmap, ordered_hashmap_free);
193
194 static int path_put(OrderedHashmap **h, const char *dir_path, const char *filename, bool override) {
195 int r;
196
197 assert(h);
198 assert(dir_path);
199 assert(filename);
200
201 if (override) {
202 _unused_ _cleanup_free_ char *old = NULL;
203 free(ordered_hashmap_remove2(*h, filename, (void**) &old));
204 } else if (ordered_hashmap_contains(*h, filename))
205 return 0;
206
207 _cleanup_free_ char *p = path_join(dir_path, filename);
208 if (!p)
209 return -ENOMEM;
210
211 _cleanup_free_ char *f = strdup(filename);
212 if (!f)
213 return -ENOMEM;
214
215 r = ordered_hashmap_ensure_put(h, &path_hash_ops_free_free, f, p);
216 if (r < 0)
217 return r;
218 assert(r > 0);
219
220 TAKE_PTR(f);
221 TAKE_PTR(p);
222 return 1;
223 }
224
225 static int enumerate_dir_d(
226 OrderedHashmap **top,
227 OrderedHashmap **bottom,
228 OrderedHashmap **drops,
229 const char *toppath,
230 const char *drop) {
231
232 _cleanup_free_ char *unit = NULL;
233 _cleanup_free_ char *path = NULL;
234 _cleanup_strv_free_ char **list = NULL;
235 char *c;
236 int r;
237
238 assert(!endswith(drop, "/"));
239
240 path = path_join(toppath, drop);
241 if (!path)
242 return -ENOMEM;
243
244 log_debug("Looking at %s", path);
245
246 unit = strdup(drop);
247 if (!unit)
248 return -ENOMEM;
249
250 c = strrchr(unit, '.');
251 if (!c)
252 return -EINVAL;
253 *c = 0;
254
255 r = get_files_in_directory(path, &list);
256 if (r < 0)
257 return log_error_errno(r, "Failed to enumerate %s: %m", path);
258
259 strv_sort(list);
260
261 STRV_FOREACH(file, list) {
262
263 if (!endswith(*file, ".conf"))
264 continue;
265
266 log_debug("Adding at top: %s %s %s/%s", *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
267 r = path_put(top, path, *file, /* override= */ false);
268 if (r < 0)
269 return r;
270
271 log_debug("Adding at bottom: %s %s %s/%s", *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
272 r = path_put(bottom, path, *file, /* override= */ true);
273 if (r < 0)
274 return r;
275
276 OrderedHashmap *h = ordered_hashmap_get(*drops, unit);
277 if (!h) {
278 h = ordered_hashmap_new(&path_hash_ops_free_free);
279 if (!h)
280 return -ENOMEM;
281 r = ordered_hashmap_ensure_put(drops, &drop_hash_ops, unit, h);
282 if (r < 0) {
283 ordered_hashmap_free(h);
284 return r;
285 }
286 unit = strdup(unit);
287 if (!unit)
288 return -ENOMEM;
289 }
290
291 log_debug("Adding to drops: %s %s %s %s %s/%s",
292 unit, glyph(GLYPH_ARROW_RIGHT), *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
293 r = path_put(&h, path, *file, /* override= */ false);
294 if (r < 0)
295 return r;
296 }
297 return 0;
298 }
299
300 static int enumerate_dir(
301 OrderedHashmap **top,
302 OrderedHashmap **bottom,
303 OrderedHashmap **drops,
304 const char *path, bool dropins) {
305
306 _cleanup_closedir_ DIR *d = NULL;
307 _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
308 size_t n_files = 0, n_dirs = 0;
309 int r;
310
311 assert(top);
312 assert(bottom);
313 assert(drops);
314 assert(path);
315
316 log_debug("Looking at %s", path);
317
318 d = opendir(path);
319 if (!d) {
320 if (errno == ENOENT)
321 return 0;
322
323 return log_error_errno(errno, "Failed to open %s: %m", path);
324 }
325
326 FOREACH_DIRENT_ALL(de, d, return -errno) {
327 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
328 if (!GREEDY_REALLOC0(dirs, n_dirs + 2))
329 return -ENOMEM;
330
331 dirs[n_dirs] = strdup(de->d_name);
332 if (!dirs[n_dirs])
333 return -ENOMEM;
334 n_dirs++;
335 }
336
337 if (!dirent_is_file(de))
338 continue;
339
340 if (!GREEDY_REALLOC0(files, n_files + 2))
341 return -ENOMEM;
342
343 files[n_files] = strdup(de->d_name);
344 if (!files[n_files])
345 return -ENOMEM;
346 n_files++;
347 }
348
349 strv_sort(dirs);
350 strv_sort(files);
351
352 STRV_FOREACH(t, dirs) {
353 r = enumerate_dir_d(top, bottom, drops, path, *t);
354 if (r < 0)
355 return r;
356 }
357
358 STRV_FOREACH(t, files) {
359 log_debug("Adding at top: %s %s %s/%s", *t, glyph(GLYPH_ARROW_RIGHT), path, *t);
360 r = path_put(top, path, *t, /* override= */ false);
361 if (r < 0)
362 return r;
363
364 log_debug("Adding at bottom: %s %s %s/%s", *t, glyph(GLYPH_ARROW_RIGHT), path, *t);
365 r = path_put(bottom, path, *t, /* override= */ true);
366 if (r < 0)
367 return r;
368 }
369
370 return 0;
371 }
372
373 static int process_suffix(const char *suffix, const char *onlyprefix) {
374 int r, ret = 0;
375
376 assert(suffix);
377 assert(!startswith(suffix, "/"));
378 assert(!strstr(suffix, "//"));
379
380 bool dropins = nulstr_contains(have_dropins, suffix);
381
382 _cleanup_ordered_hashmap_free_ OrderedHashmap *top = NULL, *bottom = NULL, *drops = NULL;
383 NULSTR_FOREACH(p, prefixes) {
384 _cleanup_free_ char *t = NULL;
385
386 t = path_join(p, suffix);
387 if (!t)
388 return -ENOMEM;
389
390 RET_GATHER(ret, enumerate_dir(&top, &bottom, &drops, t, dropins));
391 }
392
393 int n_found = 0;
394 char *f, *key;
395 ORDERED_HASHMAP_FOREACH_KEY(f, key, top) {
396 char *o;
397
398 o = ordered_hashmap_get(bottom, key);
399 assert(o);
400
401 if (!onlyprefix || startswith(o, onlyprefix)) {
402 if (path_equal(o, f)) {
403 notify_override_unchanged(f);
404 } else {
405 r = found_override(f, o);
406 if (r < 0)
407 RET_GATHER(ret, r);
408 else
409 n_found += r;
410 }
411 }
412
413 OrderedHashmap *h = ordered_hashmap_get(drops, key);
414 if (h)
415 ORDERED_HASHMAP_FOREACH(o, h)
416 if (!onlyprefix || startswith(o, onlyprefix))
417 n_found += notify_override_extended(f, o);
418 }
419
420 return ret < 0 ? ret : n_found;
421 }
422
423 static int process_suffixes(const char *onlyprefix) {
424 int n_found = 0, r;
425
426 NULSTR_FOREACH(n, suffixes) {
427 r = process_suffix(n, onlyprefix);
428 if (r < 0)
429 return r;
430
431 n_found += r;
432 }
433
434 return n_found;
435 }
436
437 static int process_suffix_chop(const char *arg) {
438 assert(arg);
439
440 if (!path_is_absolute(arg))
441 return process_suffix(arg, NULL);
442
443 /* Strip prefix from the suffix */
444 NULSTR_FOREACH(p, prefixes) {
445 const char *suffix;
446
447 suffix = startswith(arg, p);
448 if (suffix) {
449 suffix += strspn(suffix, "/");
450 if (*suffix)
451 return process_suffix(suffix, p);
452 else
453 return process_suffixes(arg);
454 }
455 }
456
457 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
458 "Invalid suffix specification %s.", arg);
459 }
460
461 static int help(void) {
462 _cleanup_free_ char *link = NULL;
463 _cleanup_(table_unrefp) Table *options = NULL;
464 int r;
465
466 r = terminal_urlify_man("systemd-delta", "1", &link);
467 if (r < 0)
468 return log_oom();
469
470 r = option_parser_get_help_table(&options);
471 if (r < 0)
472 return r;
473
474 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
475 "Find overridden configuration files.\n\n",
476 program_invocation_short_name);
477
478 r = table_print_or_warn(options);
479 if (r < 0)
480 return r;
481
482 printf("\nSee the %s for details.\n", link);
483 return 0;
484 }
485
486 static int parse_flags(const char *flag_str, int flags) {
487 for (;;) {
488 _cleanup_free_ char *word = NULL;
489 int r;
490
491 r = extract_first_word(&flag_str, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS);
492 if (r < 0)
493 return r;
494 if (r == 0)
495 return flags;
496
497 if (streq(word, "masked"))
498 flags |= SHOW_MASKED;
499 else if (streq(word, "equivalent"))
500 flags |= SHOW_EQUIVALENT;
501 else if (streq(word, "redirected"))
502 flags |= SHOW_REDIRECTED;
503 else if (streq(word, "overridden"))
504 flags |= SHOW_OVERRIDDEN;
505 else if (streq(word, "unchanged"))
506 flags |= SHOW_UNCHANGED;
507 else if (streq(word, "extended"))
508 flags |= SHOW_EXTENDED;
509 else if (streq(word, "default"))
510 flags |= SHOW_DEFAULTS;
511 else
512 return -EINVAL;
513 }
514 }
515
516 static int parse_argv(int argc, char *argv[], char ***ret_args) {
517 assert(argc >= 0);
518 assert(argv);
519
520 OptionParser opts = { argc, argv };
521 int r;
522
523 FOREACH_OPTION_OR_RETURN(c, &opts)
524 switch (c) {
525
526 OPTION_COMMON_HELP:
527 return help();
528
529 OPTION_COMMON_VERSION:
530 return version();
531
532 OPTION_COMMON_NO_PAGER:
533 arg_pager_flags |= PAGER_DISABLE;
534 break;
535
536 OPTION('t', "type", "TYPE...", "Only display a selected set of override types"):
537 r = parse_flags(opts.arg, arg_flags);
538 if (r < 0)
539 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
540 "Failed to parse flags field.");
541 arg_flags = r;
542 break;
543
544 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "diff", "yes|no",
545 "Show a diff when overridden files differ"):
546 r = parse_boolean_argument("--diff", opts.arg, NULL);
547 if (r < 0)
548 return r;
549 arg_diff = r;
550 break;
551 }
552
553 *ret_args = option_parser_get_args(&opts);
554 return 1;
555 }
556
557 static int run(int argc, char *argv[]) {
558 int r, k, n_found = 0;
559
560 log_setup();
561
562 char **args = NULL;
563 r = parse_argv(argc, argv, &args);
564 if (r <= 0)
565 return r;
566
567 if (arg_flags == 0)
568 arg_flags = SHOW_DEFAULTS;
569
570 if (arg_diff < 0)
571 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
572 else if (arg_diff)
573 arg_flags |= SHOW_OVERRIDDEN;
574
575 pager_open(arg_pager_flags);
576
577 if (!strv_isempty(args)) {
578 STRV_FOREACH(i, args) {
579 path_simplify(*i);
580
581 k = process_suffix_chop(*i);
582 if (k < 0)
583 r = k;
584 else
585 n_found += k;
586 }
587 } else {
588 k = process_suffixes(NULL);
589 if (k < 0)
590 r = k;
591 else
592 n_found += k;
593 }
594
595 if (r >= 0)
596 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
597 return r;
598 }
599
600 DEFINE_MAIN_FUNCTION(run);