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