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