]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/delta/delta.c
util: move some raw memory functions from string-util.h → memory-util.h
[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"
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"
27#include "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
62570f6f 76 r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x);
e1873695
LP
77 if (r < 0)
78 return r;
7e8d5761 79
62570f6f 80 r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y);
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
605405c6 203 path = strjoin(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
605405c6 233 p = strjoin(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
ZJS
293 _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
294 size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0;
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) {
277f2f75
LN
314 dirent_ensure_type(d, de);
315
9f5ebb8a
ZJS
316 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
317 if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2))
318 return -ENOMEM;
319
320 dirs[n_dirs] = strdup(de->d_name);
321 if (!dirs[n_dirs])
322 return -ENOMEM;
323 n_dirs ++;
324 }
0000ce05 325
7e8d5761
LP
326 if (!dirent_is_file(de))
327 continue;
328
9f5ebb8a
ZJS
329 if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2))
330 return -ENOMEM;
331
332 files[n_files] = strdup(de->d_name);
333 if (!files[n_files])
334 return -ENOMEM;
335 n_files ++;
336 }
337
338 strv_sort(dirs);
339 strv_sort(files);
340
341 STRV_FOREACH(t, dirs) {
342 r = enumerate_dir_d(top, bottom, drops, path, *t);
343 if (r < 0)
344 return r;
345 }
346
347 STRV_FOREACH(t, files) {
348 _cleanup_free_ char *p = NULL;
349
350 p = strjoin(path, "/", *t);
e26970a8
TA
351 if (!p)
352 return -ENOMEM;
7e8d5761 353
9a6f746f 354 log_debug("Adding at top: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
9f5ebb8a
ZJS
355 r = ordered_hashmap_put(top, basename(p), p);
356 if (r >= 0) {
7e8d5761 357 p = strdup(p);
e26970a8
TA
358 if (!p)
359 return -ENOMEM;
9f5ebb8a
ZJS
360 } else if (r != -EEXIST)
361 return r;
7e8d5761 362
9a6f746f 363 log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
9f5ebb8a
ZJS
364 free(ordered_hashmap_remove(bottom, basename(p)));
365 r = ordered_hashmap_put(bottom, basename(p), p);
366 if (r < 0)
367 return r;
368 p = NULL;
7e8d5761 369 }
9f5ebb8a 370
8fb3f009 371 return 0;
7e8d5761
LP
372}
373
4cc893e7 374static bool should_skip_path(const char *prefix, const char *suffix) {
349cc4a5 375#if HAVE_SPLIT_USR
b05422a8 376 _cleanup_free_ char *target = NULL;
4cc893e7
FB
377 const char *p;
378 char *dirname;
b05422a8 379
4cc893e7 380 dirname = strjoina(prefix, "/", suffix);
b05422a8 381
4cc893e7
FB
382 if (chase_symlinks(dirname, NULL, 0, &target) < 0)
383 return false;
384
385 NULSTR_FOREACH(p, prefixes) {
386 if (path_startswith(dirname, p))
387 continue;
388
389 if (path_equal(target, strjoina(p, "/", suffix))) {
390 log_debug("%s redirects to %s, skipping.", dirname, target);
391 return true;
392 }
393 }
b05422a8 394#endif
4cc893e7 395 return false;
b05422a8
FS
396}
397
6096dfd6 398static int process_suffix(const char *suffix, const char *onlyprefix) {
7e8d5761
LP
399 const char *p;
400 char *f;
9f5ebb8a
ZJS
401 OrderedHashmap *top, *bottom, *drops;
402 OrderedHashmap *h;
0000ce05 403 char *key;
7e8d5761 404 int r = 0, k;
0000ce05 405 Iterator i, j;
7e8d5761 406 int 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
4cc893e7 426 if (should_skip_path(p, suffix))
b05422a8 427 continue;
7e8d5761 428
605405c6 429 t = strjoin(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
9f5ebb8a 440 ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
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)
9f5ebb8a 460 ORDERED_HASHMAP_FOREACH(o, h, j)
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
9f5ebb8a
ZJS
469 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
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"
37ec0fdd
LP
535 "\nSee the %s for details.\n"
536 , program_invocation_short_name
537 , link
538 );
539
540 return 0;
7e8d5761
LP
541}
542
866062b1 543static int parse_flags(const char *flag_str, int flags) {
a2a5291b 544 const char *word, *state;
807f4645
GN
545 size_t l;
546
d0a2e1c3 547 FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) {
a2a5291b 548 if (strneq("masked", word, l))
807f4645 549 flags |= SHOW_MASKED;
a2a5291b 550 else if (strneq ("equivalent", word, l))
c8021373 551 flags |= SHOW_EQUIVALENT;
a2a5291b 552 else if (strneq("redirected", word, l))
c8021373 553 flags |= SHOW_REDIRECTED;
a2a5291b 554 else if (strneq("overridden", word, l))
386da858 555 flags |= SHOW_OVERRIDDEN;
a2a5291b 556 else if (strneq("unchanged", word, l))
807f4645 557 flags |= SHOW_UNCHANGED;
a2a5291b 558 else if (strneq("extended", word, l))
0000ce05 559 flags |= SHOW_EXTENDED;
a2a5291b 560 else if (strneq("default", word, l))
807f4645 561 flags |= SHOW_DEFAULTS;
866062b1
LP
562 else
563 return -EINVAL;
807f4645
GN
564 }
565 return flags;
566}
567
866062b1 568static int parse_argv(int argc, char *argv[]) {
7e8d5761
LP
569
570 enum {
571 ARG_NO_PAGER = 0x100,
807f4645 572 ARG_DIFF,
7e8d5761
LP
573 ARG_VERSION
574 };
575
576 static const struct option options[] = {
577 { "help", no_argument, NULL, 'h' },
578 { "version", no_argument, NULL, ARG_VERSION },
579 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
807f4645
GN
580 { "diff", optional_argument, NULL, ARG_DIFF },
581 { "type", required_argument, NULL, 't' },
eb9da376 582 {}
7e8d5761
LP
583 };
584
585 int c;
586
587 assert(argc >= 1);
588 assert(argv);
589
601185b4 590 while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
7e8d5761
LP
591
592 switch (c) {
593
594 case 'h':
37ec0fdd 595 return help();
7e8d5761
LP
596
597 case ARG_VERSION:
3f6fd1ba 598 return version();
7e8d5761
LP
599
600 case ARG_NO_PAGER:
0221d68a 601 arg_pager_flags |= PAGER_DISABLE;
7e8d5761
LP
602 break;
603
866062b1
LP
604 case 't': {
605 int f;
606 f = parse_flags(optarg, arg_flags);
baaa35ad
ZJS
607 if (f < 0)
608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
609 "Failed to parse flags field.");
866062b1 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);
baaa35ad
ZJS
621 if (b < 0)
622 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
623 "Failed to parse diff boolean.");
82376245
LP
624
625 arg_diff = b;
807f4645
GN
626 }
627 break;
628
eb9da376 629 case '?':
7e8d5761 630 return -EINVAL;
eb9da376
LP
631
632 default:
633 assert_not_reached("Unhandled option");
7e8d5761 634 }
7e8d5761
LP
635
636 return 1;
637}
638
7280b076 639static int run(int argc, char *argv[]) {
82376245 640 int r, k, n_found = 0;
7e8d5761
LP
641
642 log_parse_environment();
643 log_open();
644
866062b1 645 r = parse_argv(argc, argv);
7e8d5761 646 if (r <= 0)
7280b076 647 return r;
7e8d5761 648
866062b1
LP
649 if (arg_flags == 0)
650 arg_flags = SHOW_DEFAULTS;
651
652 if (arg_diff < 0)
386da858 653 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
866062b1 654 else if (arg_diff)
386da858 655 arg_flags |= SHOW_OVERRIDDEN;
807f4645 656
0221d68a 657 (void) pager_open(arg_pager_flags);
7e8d5761
LP
658
659 if (optind < argc) {
660 int i;
661
662 for (i = optind; i < argc; i++) {
858d36c1 663 path_simplify(argv[i], false);
82376245 664
f939e9a4 665 k = process_suffix_chop(argv[i]);
7e8d5761
LP
666 if (k < 0)
667 r = k;
668 else
669 n_found += k;
670 }
671
672 } else {
6096dfd6
ZJS
673 k = process_suffixes(NULL);
674 if (k < 0)
675 r = k;
676 else
677 n_found += k;
7e8d5761
LP
678 }
679
680 if (r >= 0)
82376245 681 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
7280b076 682 return r;
7e8d5761 683}
7280b076
ZJS
684
685DEFINE_MAIN_FUNCTION(run);