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