]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e8d5761 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2012 Lennart Poettering | |
6096dfd6 | 6 | Copyright 2013 Zbigniew Jędrzejewski-Szmek |
7e8d5761 LP |
7 | ***/ |
8 | ||
9 | #include <errno.h> | |
7e8d5761 | 10 | #include <getopt.h> |
3f6fd1ba | 11 | #include <string.h> |
ce30c8dc | 12 | #include <sys/prctl.h> |
3f6fd1ba | 13 | #include <unistd.h> |
7e8d5761 | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
a0956174 | 16 | #include "dirent-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
f4f15635 | 18 | #include "fs-util.h" |
7e8d5761 | 19 | #include "hashmap.h" |
8752c575 | 20 | #include "locale-util.h" |
7e8d5761 LP |
21 | #include "log.h" |
22 | #include "pager.h" | |
6bedfcbb | 23 | #include "parse-util.h" |
3f6fd1ba | 24 | #include "path-util.h" |
0b452006 | 25 | #include "process-util.h" |
ce30c8dc | 26 | #include "signal-util.h" |
8fcde012 | 27 | #include "stat-util.h" |
07630cea | 28 | #include "string-util.h" |
3f6fd1ba LP |
29 | #include "strv.h" |
30 | #include "terminal-util.h" | |
31 | #include "util.h" | |
7e8d5761 | 32 | |
f939e9a4 ZJS |
33 | static const char prefixes[] = |
34 | "/etc\0" | |
35 | "/run\0" | |
36 | "/usr/local/lib\0" | |
37 | "/usr/local/share\0" | |
38 | "/usr/lib\0" | |
39 | "/usr/share\0" | |
349cc4a5 | 40 | #if HAVE_SPLIT_USR |
f939e9a4 ZJS |
41 | "/lib\0" |
42 | #endif | |
43 | ; | |
44 | ||
45 | static const char suffixes[] = | |
46 | "sysctl.d\0" | |
47 | "tmpfiles.d\0" | |
48 | "modules-load.d\0" | |
49 | "binfmt.d\0" | |
50 | "systemd/system\0" | |
51 | "systemd/user\0" | |
52 | "systemd/system-preset\0" | |
53 | "systemd/user-preset\0" | |
54 | "udev/rules.d\0" | |
55 | "modprobe.d\0"; | |
56 | ||
57 | static const char have_dropins[] = | |
58 | "systemd/system\0" | |
59 | "systemd/user\0"; | |
60 | ||
7e8d5761 | 61 | static bool arg_no_pager = false; |
866062b1 | 62 | static int arg_diff = -1; |
7e8d5761 | 63 | |
866062b1 | 64 | static enum { |
4c4e6431 | 65 | SHOW_MASKED = 1 << 0, |
c8021373 LP |
66 | SHOW_EQUIVALENT = 1 << 1, |
67 | SHOW_REDIRECTED = 1 << 2, | |
386da858 | 68 | SHOW_OVERRIDDEN = 1 << 3, |
4c4e6431 | 69 | SHOW_UNCHANGED = 1 << 4, |
0000ce05 | 70 | SHOW_EXTENDED = 1 << 5, |
4c4e6431 LP |
71 | |
72 | SHOW_DEFAULTS = | |
0000ce05 | 73 | (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED) |
866062b1 | 74 | } arg_flags = 0; |
4c4e6431 | 75 | |
7e8d5761 | 76 | static int equivalent(const char *a, const char *b) { |
e26970a8 | 77 | _cleanup_free_ char *x = NULL, *y = NULL; |
e1873695 | 78 | int r; |
7e8d5761 | 79 | |
62570f6f | 80 | r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x); |
e1873695 LP |
81 | if (r < 0) |
82 | return r; | |
7e8d5761 | 83 | |
62570f6f | 84 | r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y); |
e1873695 LP |
85 | if (r < 0) |
86 | return r; | |
7e8d5761 | 87 | |
e26970a8 | 88 | return path_equal(x, y); |
7e8d5761 LP |
89 | } |
90 | ||
866062b1 LP |
91 | static int notify_override_masked(const char *top, const char *bottom) { |
92 | if (!(arg_flags & SHOW_MASKED)) | |
807f4645 GN |
93 | return 0; |
94 | ||
00a5cc3a | 95 | printf("%s%s%s %s %s %s\n", |
1fc464f6 | 96 | ansi_highlight_red(), "[MASKED]", ansi_normal(), |
323b7dc9 | 97 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
98 | return 1; |
99 | } | |
100 | ||
866062b1 LP |
101 | static int notify_override_equivalent(const char *top, const char *bottom) { |
102 | if (!(arg_flags & SHOW_EQUIVALENT)) | |
807f4645 GN |
103 | return 0; |
104 | ||
00a5cc3a | 105 | printf("%s%s%s %s %s %s\n", |
1fc464f6 | 106 | ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(), |
323b7dc9 | 107 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
108 | return 1; |
109 | } | |
110 | ||
866062b1 LP |
111 | static int notify_override_redirected(const char *top, const char *bottom) { |
112 | if (!(arg_flags & SHOW_REDIRECTED)) | |
807f4645 GN |
113 | return 0; |
114 | ||
9b6e0ce5 | 115 | printf("%s%s%s %s %s %s\n", |
1fc464f6 | 116 | ansi_highlight(), "[REDIRECTED]", ansi_normal(), |
323b7dc9 | 117 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
118 | return 1; |
119 | } | |
120 | ||
386da858 NM |
121 | static int notify_override_overridden(const char *top, const char *bottom) { |
122 | if (!(arg_flags & SHOW_OVERRIDDEN)) | |
807f4645 GN |
123 | return 0; |
124 | ||
00a5cc3a | 125 | printf("%s%s%s %s %s %s\n", |
1fc464f6 | 126 | ansi_highlight(), "[OVERRIDDEN]", ansi_normal(), |
323b7dc9 | 127 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
128 | return 1; |
129 | } | |
130 | ||
0000ce05 LN |
131 | static int notify_override_extended(const char *top, const char *bottom) { |
132 | if (!(arg_flags & SHOW_EXTENDED)) | |
133 | return 0; | |
134 | ||
00a5cc3a | 135 | printf("%s%s%s %s %s %s\n", |
1fc464f6 | 136 | ansi_highlight(), "[EXTENDED]", ansi_normal(), |
323b7dc9 | 137 | top, special_glyph(ARROW), bottom); |
0000ce05 LN |
138 | return 1; |
139 | } | |
140 | ||
866062b1 LP |
141 | static int notify_override_unchanged(const char *f) { |
142 | if (!(arg_flags & SHOW_UNCHANGED)) | |
807f4645 GN |
143 | return 0; |
144 | ||
8e812a23 | 145 | printf("[UNCHANGED] %s\n", f); |
807f4645 GN |
146 | return 1; |
147 | } | |
148 | ||
866062b1 | 149 | static int found_override(const char *top, const char *bottom) { |
e26970a8 | 150 | _cleanup_free_ char *dest = NULL; |
7e8d5761 | 151 | pid_t pid; |
4c253ed1 | 152 | int r; |
7e8d5761 LP |
153 | |
154 | assert(top); | |
155 | assert(bottom); | |
156 | ||
8fd57568 ZJS |
157 | if (null_or_empty_path(top) > 0) |
158 | return notify_override_masked(top, bottom); | |
7e8d5761 | 159 | |
4c253ed1 LP |
160 | r = readlink_malloc(top, &dest); |
161 | if (r >= 0) { | |
7e8d5761 | 162 | if (equivalent(dest, bottom) > 0) |
8fd57568 | 163 | return notify_override_equivalent(top, bottom); |
7e8d5761 | 164 | else |
8fd57568 | 165 | return notify_override_redirected(top, bottom); |
7e8d5761 LP |
166 | } |
167 | ||
4c253ed1 | 168 | r = notify_override_overridden(top, bottom); |
866062b1 | 169 | if (!arg_diff) |
4c253ed1 | 170 | return r; |
7e8d5761 LP |
171 | |
172 | putchar('\n'); | |
173 | ||
174 | fflush(stdout); | |
175 | ||
b6e1fff1 | 176 | r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid); |
4c253ed1 | 177 | if (r < 0) |
b6e1fff1 | 178 | return r; |
4c253ed1 | 179 | if (r == 0) { |
7e8d5761 | 180 | execlp("diff", "diff", "-us", "--", bottom, top, NULL); |
0b1f3c76 | 181 | log_open(); |
56f64d95 | 182 | log_error_errno(errno, "Failed to execute diff: %m"); |
ce30c8dc | 183 | _exit(EXIT_FAILURE); |
7e8d5761 LP |
184 | } |
185 | ||
7d4904fe | 186 | (void) wait_for_terminate_and_check("diff", pid, WAIT_LOG_ABNORMAL); |
7e8d5761 LP |
187 | putchar('\n'); |
188 | ||
4c253ed1 | 189 | return r; |
7e8d5761 LP |
190 | } |
191 | ||
9f5ebb8a ZJS |
192 | static int enumerate_dir_d( |
193 | OrderedHashmap *top, | |
194 | OrderedHashmap *bottom, | |
195 | OrderedHashmap *drops, | |
196 | const char *toppath, const char *drop) { | |
197 | ||
f939e9a4 | 198 | _cleanup_free_ char *unit = NULL; |
0000ce05 LN |
199 | _cleanup_free_ char *path = NULL; |
200 | _cleanup_strv_free_ char **list = NULL; | |
201 | char **file; | |
202 | char *c; | |
203 | int r; | |
204 | ||
f939e9a4 ZJS |
205 | assert(!endswith(drop, "/")); |
206 | ||
605405c6 | 207 | path = strjoin(toppath, "/", drop); |
0000ce05 LN |
208 | if (!path) |
209 | return -ENOMEM; | |
210 | ||
f939e9a4 | 211 | log_debug("Looking at %s", path); |
0000ce05 | 212 | |
f939e9a4 ZJS |
213 | unit = strdup(drop); |
214 | if (!unit) | |
0000ce05 LN |
215 | return -ENOMEM; |
216 | ||
f939e9a4 | 217 | c = strrchr(unit, '.'); |
0000ce05 LN |
218 | if (!c) |
219 | return -EINVAL; | |
220 | *c = 0; | |
221 | ||
222 | r = get_files_in_directory(path, &list); | |
23bbb0de MS |
223 | if (r < 0) |
224 | return log_error_errno(r, "Failed to enumerate %s: %m", path); | |
0000ce05 | 225 | |
9f5ebb8a ZJS |
226 | strv_sort(list); |
227 | ||
0000ce05 | 228 | STRV_FOREACH(file, list) { |
9f5ebb8a | 229 | OrderedHashmap *h; |
0000ce05 LN |
230 | int k; |
231 | char *p; | |
232 | char *d; | |
233 | ||
234 | if (!endswith(*file, ".conf")) | |
235 | continue; | |
236 | ||
605405c6 | 237 | p = strjoin(path, "/", *file); |
0000ce05 LN |
238 | if (!p) |
239 | return -ENOMEM; | |
f939e9a4 | 240 | d = p + strlen(toppath) + 1; |
0000ce05 | 241 | |
323b7dc9 | 242 | log_debug("Adding at top: %s %s %s", d, special_glyph(ARROW), p); |
9f5ebb8a | 243 | k = ordered_hashmap_put(top, d, p); |
0000ce05 LN |
244 | if (k >= 0) { |
245 | p = strdup(p); | |
246 | if (!p) | |
247 | return -ENOMEM; | |
f939e9a4 | 248 | d = p + strlen(toppath) + 1; |
0000ce05 LN |
249 | } else if (k != -EEXIST) { |
250 | free(p); | |
251 | return k; | |
252 | } | |
253 | ||
323b7dc9 | 254 | log_debug("Adding at bottom: %s %s %s", d, special_glyph(ARROW), p); |
9f5ebb8a ZJS |
255 | free(ordered_hashmap_remove(bottom, d)); |
256 | k = ordered_hashmap_put(bottom, d, p); | |
0000ce05 LN |
257 | if (k < 0) { |
258 | free(p); | |
259 | return k; | |
260 | } | |
261 | ||
9f5ebb8a | 262 | h = ordered_hashmap_get(drops, unit); |
0000ce05 | 263 | if (!h) { |
9f5ebb8a | 264 | h = ordered_hashmap_new(&string_hash_ops); |
0000ce05 LN |
265 | if (!h) |
266 | return -ENOMEM; | |
9f5ebb8a | 267 | ordered_hashmap_put(drops, unit, h); |
f939e9a4 ZJS |
268 | unit = strdup(unit); |
269 | if (!unit) | |
0000ce05 LN |
270 | return -ENOMEM; |
271 | } | |
272 | ||
273 | p = strdup(p); | |
274 | if (!p) | |
275 | return -ENOMEM; | |
276 | ||
00a5cc3a | 277 | log_debug("Adding to drops: %s %s %s %s %s", |
323b7dc9 | 278 | unit, special_glyph(ARROW), basename(p), special_glyph(ARROW), p); |
9f5ebb8a | 279 | k = ordered_hashmap_put(h, basename(p), p); |
0000ce05 LN |
280 | if (k < 0) { |
281 | free(p); | |
282 | if (k != -EEXIST) | |
283 | return k; | |
284 | } | |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
9f5ebb8a ZJS |
289 | static int enumerate_dir( |
290 | OrderedHashmap *top, | |
291 | OrderedHashmap *bottom, | |
292 | OrderedHashmap *drops, | |
293 | const char *path, bool dropins) { | |
294 | ||
295 | _cleanup_closedir_ DIR *d = NULL; | |
8fb3f009 | 296 | struct dirent *de; |
9f5ebb8a ZJS |
297 | _cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
298 | size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0; | |
299 | char **t; | |
300 | int r; | |
7e8d5761 LP |
301 | |
302 | assert(top); | |
303 | assert(bottom); | |
0000ce05 | 304 | assert(drops); |
7e8d5761 LP |
305 | assert(path); |
306 | ||
f939e9a4 ZJS |
307 | log_debug("Looking at %s", path); |
308 | ||
7e8d5761 LP |
309 | d = opendir(path); |
310 | if (!d) { | |
311 | if (errno == ENOENT) | |
312 | return 0; | |
313 | ||
e1427b13 | 314 | return log_error_errno(errno, "Failed to open %s: %m", path); |
7e8d5761 LP |
315 | } |
316 | ||
8fb3f009 | 317 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
277f2f75 LN |
318 | dirent_ensure_type(d, de); |
319 | ||
9f5ebb8a ZJS |
320 | if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) { |
321 | if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2)) | |
322 | return -ENOMEM; | |
323 | ||
324 | dirs[n_dirs] = strdup(de->d_name); | |
325 | if (!dirs[n_dirs]) | |
326 | return -ENOMEM; | |
327 | n_dirs ++; | |
328 | } | |
0000ce05 | 329 | |
7e8d5761 LP |
330 | if (!dirent_is_file(de)) |
331 | continue; | |
332 | ||
9f5ebb8a ZJS |
333 | if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2)) |
334 | return -ENOMEM; | |
335 | ||
336 | files[n_files] = strdup(de->d_name); | |
337 | if (!files[n_files]) | |
338 | return -ENOMEM; | |
339 | n_files ++; | |
340 | } | |
341 | ||
342 | strv_sort(dirs); | |
343 | strv_sort(files); | |
344 | ||
345 | STRV_FOREACH(t, dirs) { | |
346 | r = enumerate_dir_d(top, bottom, drops, path, *t); | |
347 | if (r < 0) | |
348 | return r; | |
349 | } | |
350 | ||
351 | STRV_FOREACH(t, files) { | |
352 | _cleanup_free_ char *p = NULL; | |
353 | ||
354 | p = strjoin(path, "/", *t); | |
e26970a8 TA |
355 | if (!p) |
356 | return -ENOMEM; | |
7e8d5761 | 357 | |
323b7dc9 | 358 | log_debug("Adding at top: %s %s %s", basename(p), special_glyph(ARROW), p); |
9f5ebb8a ZJS |
359 | r = ordered_hashmap_put(top, basename(p), p); |
360 | if (r >= 0) { | |
7e8d5761 | 361 | p = strdup(p); |
e26970a8 TA |
362 | if (!p) |
363 | return -ENOMEM; | |
9f5ebb8a ZJS |
364 | } else if (r != -EEXIST) |
365 | return r; | |
7e8d5761 | 366 | |
323b7dc9 | 367 | log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(ARROW), p); |
9f5ebb8a ZJS |
368 | free(ordered_hashmap_remove(bottom, basename(p))); |
369 | r = ordered_hashmap_put(bottom, basename(p), p); | |
370 | if (r < 0) | |
371 | return r; | |
372 | p = NULL; | |
7e8d5761 | 373 | } |
9f5ebb8a | 374 | |
8fb3f009 | 375 | return 0; |
7e8d5761 LP |
376 | } |
377 | ||
4cc893e7 | 378 | static bool should_skip_path(const char *prefix, const char *suffix) { |
349cc4a5 | 379 | #if HAVE_SPLIT_USR |
b05422a8 | 380 | _cleanup_free_ char *target = NULL; |
4cc893e7 FB |
381 | const char *p; |
382 | char *dirname; | |
b05422a8 | 383 | |
4cc893e7 | 384 | dirname = strjoina(prefix, "/", suffix); |
b05422a8 | 385 | |
4cc893e7 FB |
386 | if (chase_symlinks(dirname, NULL, 0, &target) < 0) |
387 | return false; | |
388 | ||
389 | NULSTR_FOREACH(p, prefixes) { | |
390 | if (path_startswith(dirname, p)) | |
391 | continue; | |
392 | ||
393 | if (path_equal(target, strjoina(p, "/", suffix))) { | |
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 | 402 | static 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 | |
4cc893e7 | 430 | if (should_skip_path(p, suffix)) |
b05422a8 | 431 | continue; |
7e8d5761 | 432 | |
605405c6 | 433 | t = strjoin(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 | ||
469 | finish: | |
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 |
483 | static 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 | ||
498 | static 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 | ||
6096dfd6 | 520 | log_error("Invalid suffix specification %s.", arg); |
7e8d5761 LP |
521 | return -EINVAL; |
522 | } | |
523 | ||
601185b4 | 524 | static void help(void) { |
7e8d5761 LP |
525 | printf("%s [OPTIONS...] [SUFFIX...]\n\n" |
526 | "Find overridden configuration files.\n\n" | |
527 | " -h --help Show this help\n" | |
528 | " --version Show package version\n" | |
807f4645 | 529 | " --no-pager Do not pipe output into a pager\n" |
386da858 | 530 | " --diff[=1|0] Show a diff when overridden files differ\n" |
601185b4 ZJS |
531 | " -t --type=LIST... Only display a selected set of override types\n" |
532 | , program_invocation_short_name); | |
7e8d5761 LP |
533 | } |
534 | ||
866062b1 | 535 | static int parse_flags(const char *flag_str, int flags) { |
a2a5291b | 536 | const char *word, *state; |
807f4645 GN |
537 | size_t l; |
538 | ||
d0a2e1c3 | 539 | FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) { |
a2a5291b | 540 | if (strneq("masked", word, l)) |
807f4645 | 541 | flags |= SHOW_MASKED; |
a2a5291b | 542 | else if (strneq ("equivalent", word, l)) |
c8021373 | 543 | flags |= SHOW_EQUIVALENT; |
a2a5291b | 544 | else if (strneq("redirected", word, l)) |
c8021373 | 545 | flags |= SHOW_REDIRECTED; |
a2a5291b | 546 | else if (strneq("overridden", word, l)) |
386da858 | 547 | flags |= SHOW_OVERRIDDEN; |
a2a5291b | 548 | else if (strneq("unchanged", word, l)) |
807f4645 | 549 | flags |= SHOW_UNCHANGED; |
a2a5291b | 550 | else if (strneq("extended", word, l)) |
0000ce05 | 551 | flags |= SHOW_EXTENDED; |
a2a5291b | 552 | else if (strneq("default", word, l)) |
807f4645 | 553 | flags |= SHOW_DEFAULTS; |
866062b1 LP |
554 | else |
555 | return -EINVAL; | |
807f4645 GN |
556 | } |
557 | return flags; | |
558 | } | |
559 | ||
866062b1 | 560 | static int parse_argv(int argc, char *argv[]) { |
7e8d5761 LP |
561 | |
562 | enum { | |
563 | ARG_NO_PAGER = 0x100, | |
807f4645 | 564 | ARG_DIFF, |
7e8d5761 LP |
565 | ARG_VERSION |
566 | }; | |
567 | ||
568 | static const struct option options[] = { | |
569 | { "help", no_argument, NULL, 'h' }, | |
570 | { "version", no_argument, NULL, ARG_VERSION }, | |
571 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
807f4645 GN |
572 | { "diff", optional_argument, NULL, ARG_DIFF }, |
573 | { "type", required_argument, NULL, 't' }, | |
eb9da376 | 574 | {} |
7e8d5761 LP |
575 | }; |
576 | ||
577 | int c; | |
578 | ||
579 | assert(argc >= 1); | |
580 | assert(argv); | |
581 | ||
601185b4 | 582 | while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0) |
7e8d5761 LP |
583 | |
584 | switch (c) { | |
585 | ||
586 | case 'h': | |
587 | help(); | |
588 | return 0; | |
589 | ||
590 | case ARG_VERSION: | |
3f6fd1ba | 591 | return version(); |
7e8d5761 LP |
592 | |
593 | case ARG_NO_PAGER: | |
594 | arg_no_pager = true; | |
595 | break; | |
596 | ||
866062b1 LP |
597 | case 't': { |
598 | int f; | |
599 | f = parse_flags(optarg, arg_flags); | |
600 | if (f < 0) { | |
601 | log_error("Failed to parse flags field."); | |
807f4645 | 602 | return -EINVAL; |
866062b1 LP |
603 | } |
604 | arg_flags = f; | |
807f4645 | 605 | break; |
866062b1 | 606 | } |
807f4645 GN |
607 | |
608 | case ARG_DIFF: | |
866062b1 LP |
609 | if (!optarg) |
610 | arg_diff = 1; | |
611 | else { | |
612 | int b; | |
613 | ||
614 | b = parse_boolean(optarg); | |
615 | if (b < 0) { | |
616 | log_error("Failed to parse diff boolean."); | |
617 | return -EINVAL; | |
82376245 LP |
618 | } |
619 | ||
620 | arg_diff = b; | |
807f4645 GN |
621 | } |
622 | break; | |
623 | ||
eb9da376 | 624 | case '?': |
7e8d5761 | 625 | return -EINVAL; |
eb9da376 LP |
626 | |
627 | default: | |
628 | assert_not_reached("Unhandled option"); | |
7e8d5761 | 629 | } |
7e8d5761 LP |
630 | |
631 | return 1; | |
632 | } | |
633 | ||
634 | int main(int argc, char *argv[]) { | |
82376245 | 635 | int r, k, n_found = 0; |
7e8d5761 LP |
636 | |
637 | log_parse_environment(); | |
638 | log_open(); | |
639 | ||
866062b1 | 640 | r = parse_argv(argc, argv); |
7e8d5761 LP |
641 | if (r <= 0) |
642 | goto finish; | |
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 | |
ee5324aa | 652 | (void) pager_open(arg_no_pager, false); |
7e8d5761 LP |
653 | |
654 | if (optind < argc) { | |
655 | int i; | |
656 | ||
657 | for (i = optind; i < argc; i++) { | |
858d36c1 | 658 | path_simplify(argv[i], false); |
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); |
7e8d5761 LP |
677 | |
678 | finish: | |
679 | pager_close(); | |
680 | ||
681 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
682 | } |