]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/delta/delta.c
machine-id: use static destructor and DEFINE_MAIN_FUNCTION() macro
[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>
3f6fd1ba 5#include <string.h>
ce30c8dc 6#include <sys/prctl.h>
3f6fd1ba 7#include <unistd.h>
7e8d5761 8
b5efdb8a 9#include "alloc-util.h"
a0956174 10#include "dirent-util.h"
3ffd4af2 11#include "fd-util.h"
f4f15635 12#include "fs-util.h"
7e8d5761 13#include "hashmap.h"
8752c575 14#include "locale-util.h"
7e8d5761 15#include "log.h"
7280b076 16#include "main-func.h"
7e8d5761 17#include "pager.h"
6bedfcbb 18#include "parse-util.h"
3f6fd1ba 19#include "path-util.h"
0b452006 20#include "process-util.h"
ce30c8dc 21#include "signal-util.h"
8fcde012 22#include "stat-util.h"
07630cea 23#include "string-util.h"
3f6fd1ba
LP
24#include "strv.h"
25#include "terminal-util.h"
26#include "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
62570f6f 75 r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x);
e1873695
LP
76 if (r < 0)
77 return r;
7e8d5761 78
62570f6f 79 r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y);
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(),
323b7dc9 92 top, 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(),
323b7dc9 102 top, 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(),
323b7dc9 112 top, 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(),
323b7dc9 122 top, 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(),
323b7dc9 132 top, 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
b6e1fff1 171 r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|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
605405c6 202 path = strjoin(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
605405c6 232 p = strjoin(path, "/", *file);
0000ce05
LN
233 if (!p)
234 return -ENOMEM;
f939e9a4 235 d = p + strlen(toppath) + 1;
0000ce05 236
323b7dc9 237 log_debug("Adding at top: %s %s %s", d, 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
323b7dc9 249 log_debug("Adding at bottom: %s %s %s", d, 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",
323b7dc9 273 unit, special_glyph(ARROW), basename(p), 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
349 p = strjoin(path, "/", *t);
e26970a8
TA
350 if (!p)
351 return -ENOMEM;
7e8d5761 352
323b7dc9 353 log_debug("Adding at top: %s %s %s", basename(p), 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
323b7dc9 362 log_debug("Adding at bottom: %s %s %s", basename(p), 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
4cc893e7 373static bool should_skip_path(const char *prefix, const char *suffix) {
349cc4a5 374#if HAVE_SPLIT_USR
b05422a8 375 _cleanup_free_ char *target = NULL;
4cc893e7
FB
376 const char *p;
377 char *dirname;
b05422a8 378
4cc893e7 379 dirname = strjoina(prefix, "/", suffix);
b05422a8 380
4cc893e7
FB
381 if (chase_symlinks(dirname, NULL, 0, &target) < 0)
382 return false;
383
384 NULSTR_FOREACH(p, prefixes) {
385 if (path_startswith(dirname, p))
386 continue;
387
388 if (path_equal(target, strjoina(p, "/", suffix))) {
389 log_debug("%s redirects to %s, skipping.", dirname, target);
390 return true;
391 }
392 }
b05422a8 393#endif
4cc893e7 394 return false;
b05422a8
FS
395}
396
6096dfd6 397static int process_suffix(const char *suffix, const char *onlyprefix) {
7e8d5761
LP
398 const char *p;
399 char *f;
9f5ebb8a
ZJS
400 OrderedHashmap *top, *bottom, *drops;
401 OrderedHashmap *h;
0000ce05 402 char *key;
7e8d5761 403 int r = 0, k;
0000ce05 404 Iterator i, j;
7e8d5761 405 int 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
4cc893e7 425 if (should_skip_path(p, suffix))
b05422a8 426 continue;
7e8d5761 427
605405c6 428 t = strjoin(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
9f5ebb8a 439 ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
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)
9f5ebb8a 459 ORDERED_HASHMAP_FOREACH(o, h, j)
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
9f5ebb8a
ZJS
468 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
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
6096dfd6 515 log_error("Invalid suffix specification %s.", arg);
7e8d5761
LP
516 return -EINVAL;
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"
37ec0fdd
LP
534 "\nSee the %s for details.\n"
535 , program_invocation_short_name
536 , link
537 );
538
539 return 0;
7e8d5761
LP
540}
541
866062b1 542static int parse_flags(const char *flag_str, int flags) {
a2a5291b 543 const char *word, *state;
807f4645
GN
544 size_t l;
545
d0a2e1c3 546 FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) {
a2a5291b 547 if (strneq("masked", word, l))
807f4645 548 flags |= SHOW_MASKED;
a2a5291b 549 else if (strneq ("equivalent", word, l))
c8021373 550 flags |= SHOW_EQUIVALENT;
a2a5291b 551 else if (strneq("redirected", word, l))
c8021373 552 flags |= SHOW_REDIRECTED;
a2a5291b 553 else if (strneq("overridden", word, l))
386da858 554 flags |= SHOW_OVERRIDDEN;
a2a5291b 555 else if (strneq("unchanged", word, l))
807f4645 556 flags |= SHOW_UNCHANGED;
a2a5291b 557 else if (strneq("extended", word, l))
0000ce05 558 flags |= SHOW_EXTENDED;
a2a5291b 559 else if (strneq("default", word, l))
807f4645 560 flags |= SHOW_DEFAULTS;
866062b1
LP
561 else
562 return -EINVAL;
807f4645
GN
563 }
564 return flags;
565}
566
866062b1 567static int parse_argv(int argc, char *argv[]) {
7e8d5761
LP
568
569 enum {
570 ARG_NO_PAGER = 0x100,
807f4645 571 ARG_DIFF,
7e8d5761
LP
572 ARG_VERSION
573 };
574
575 static const struct option options[] = {
576 { "help", no_argument, NULL, 'h' },
577 { "version", no_argument, NULL, ARG_VERSION },
578 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
807f4645
GN
579 { "diff", optional_argument, NULL, ARG_DIFF },
580 { "type", required_argument, NULL, 't' },
eb9da376 581 {}
7e8d5761
LP
582 };
583
584 int c;
585
586 assert(argc >= 1);
587 assert(argv);
588
601185b4 589 while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
7e8d5761
LP
590
591 switch (c) {
592
593 case 'h':
37ec0fdd 594 return help();
7e8d5761
LP
595
596 case ARG_VERSION:
3f6fd1ba 597 return version();
7e8d5761
LP
598
599 case ARG_NO_PAGER:
0221d68a 600 arg_pager_flags |= PAGER_DISABLE;
7e8d5761
LP
601 break;
602
866062b1
LP
603 case 't': {
604 int f;
605 f = parse_flags(optarg, arg_flags);
606 if (f < 0) {
607 log_error("Failed to parse flags field.");
807f4645 608 return -EINVAL;
866062b1
LP
609 }
610 arg_flags = f;
807f4645 611 break;
866062b1 612 }
807f4645
GN
613
614 case ARG_DIFF:
866062b1
LP
615 if (!optarg)
616 arg_diff = 1;
617 else {
618 int b;
619
620 b = parse_boolean(optarg);
621 if (b < 0) {
622 log_error("Failed to parse diff boolean.");
623 return -EINVAL;
82376245
LP
624 }
625
626 arg_diff = b;
807f4645
GN
627 }
628 break;
629
eb9da376 630 case '?':
7e8d5761 631 return -EINVAL;
eb9da376
LP
632
633 default:
634 assert_not_reached("Unhandled option");
7e8d5761 635 }
7e8d5761
LP
636
637 return 1;
638}
639
7280b076 640static int run(int argc, char *argv[]) {
82376245 641 int r, k, n_found = 0;
7e8d5761
LP
642
643 log_parse_environment();
644 log_open();
645
866062b1 646 r = parse_argv(argc, argv);
7e8d5761 647 if (r <= 0)
7280b076 648 return r;
7e8d5761 649
866062b1
LP
650 if (arg_flags == 0)
651 arg_flags = SHOW_DEFAULTS;
652
653 if (arg_diff < 0)
386da858 654 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
866062b1 655 else if (arg_diff)
386da858 656 arg_flags |= SHOW_OVERRIDDEN;
807f4645 657
0221d68a 658 (void) pager_open(arg_pager_flags);
7e8d5761
LP
659
660 if (optind < argc) {
661 int i;
662
663 for (i = optind; i < argc; i++) {
858d36c1 664 path_simplify(argv[i], false);
82376245 665
f939e9a4 666 k = process_suffix_chop(argv[i]);
7e8d5761
LP
667 if (k < 0)
668 r = k;
669 else
670 n_found += k;
671 }
672
673 } else {
6096dfd6
ZJS
674 k = process_suffixes(NULL);
675 if (k < 0)
676 r = k;
677 else
678 n_found += k;
7e8d5761
LP
679 }
680
681 if (r >= 0)
82376245 682 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
7280b076 683 return r;
7e8d5761 684}
7280b076
ZJS
685
686DEFINE_MAIN_FUNCTION(run);