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