]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e8d5761 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2013 Zbigniew Jędrzejewski-Szmek |
7e8d5761 LP |
4 | ***/ |
5 | ||
6 | #include <errno.h> | |
7e8d5761 | 7 | #include <getopt.h> |
3f6fd1ba | 8 | #include <string.h> |
ce30c8dc | 9 | #include <sys/prctl.h> |
3f6fd1ba | 10 | #include <unistd.h> |
7e8d5761 | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
a0956174 | 13 | #include "dirent-util.h" |
3ffd4af2 | 14 | #include "fd-util.h" |
f4f15635 | 15 | #include "fs-util.h" |
7e8d5761 | 16 | #include "hashmap.h" |
8752c575 | 17 | #include "locale-util.h" |
7e8d5761 LP |
18 | #include "log.h" |
19 | #include "pager.h" | |
6bedfcbb | 20 | #include "parse-util.h" |
3f6fd1ba | 21 | #include "path-util.h" |
0b452006 | 22 | #include "process-util.h" |
ce30c8dc | 23 | #include "signal-util.h" |
8fcde012 | 24 | #include "stat-util.h" |
07630cea | 25 | #include "string-util.h" |
3f6fd1ba LP |
26 | #include "strv.h" |
27 | #include "terminal-util.h" | |
28 | #include "util.h" | |
7e8d5761 | 29 | |
f939e9a4 ZJS |
30 | static 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 | ||
42 | static 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 | ||
54 | static const char have_dropins[] = | |
55 | "systemd/system\0" | |
56 | "systemd/user\0"; | |
57 | ||
7e8d5761 | 58 | static bool arg_no_pager = false; |
866062b1 | 59 | static int arg_diff = -1; |
7e8d5761 | 60 | |
866062b1 | 61 | static 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 | 73 | static int equivalent(const char *a, const char *b) { |
e26970a8 | 74 | _cleanup_free_ char *x = NULL, *y = NULL; |
e1873695 | 75 | int r; |
7e8d5761 | 76 | |
62570f6f | 77 | r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x); |
e1873695 LP |
78 | if (r < 0) |
79 | return r; | |
7e8d5761 | 80 | |
62570f6f | 81 | r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y); |
e1873695 LP |
82 | if (r < 0) |
83 | return r; | |
7e8d5761 | 84 | |
e26970a8 | 85 | return path_equal(x, y); |
7e8d5761 LP |
86 | } |
87 | ||
866062b1 LP |
88 | static 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(), |
323b7dc9 | 94 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
95 | return 1; |
96 | } | |
97 | ||
866062b1 LP |
98 | static 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(), |
323b7dc9 | 104 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
105 | return 1; |
106 | } | |
107 | ||
866062b1 LP |
108 | static 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(), |
323b7dc9 | 114 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
115 | return 1; |
116 | } | |
117 | ||
386da858 NM |
118 | static 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(), |
323b7dc9 | 124 | top, special_glyph(ARROW), bottom); |
807f4645 GN |
125 | return 1; |
126 | } | |
127 | ||
0000ce05 LN |
128 | static 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(), |
323b7dc9 | 134 | top, special_glyph(ARROW), bottom); |
0000ce05 LN |
135 | return 1; |
136 | } | |
137 | ||
866062b1 LP |
138 | static 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 | 146 | static 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 | ||
b6e1fff1 | 173 | r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|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 |
189 | static 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 | ||
605405c6 | 204 | path = strjoin(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 | ||
605405c6 | 234 | p = strjoin(path, "/", *file); |
0000ce05 LN |
235 | if (!p) |
236 | return -ENOMEM; | |
f939e9a4 | 237 | d = p + strlen(toppath) + 1; |
0000ce05 | 238 | |
323b7dc9 | 239 | log_debug("Adding at top: %s %s %s", d, 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 | ||
323b7dc9 | 251 | log_debug("Adding at bottom: %s %s %s", d, 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", |
323b7dc9 | 275 | unit, special_glyph(ARROW), basename(p), 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 |
286 | static 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 ZJS |
294 | _cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
295 | size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0; | |
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) { |
277f2f75 LN |
315 | dirent_ensure_type(d, de); |
316 | ||
9f5ebb8a ZJS |
317 | if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) { |
318 | if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2)) | |
319 | return -ENOMEM; | |
320 | ||
321 | dirs[n_dirs] = strdup(de->d_name); | |
322 | if (!dirs[n_dirs]) | |
323 | return -ENOMEM; | |
324 | n_dirs ++; | |
325 | } | |
0000ce05 | 326 | |
7e8d5761 LP |
327 | if (!dirent_is_file(de)) |
328 | continue; | |
329 | ||
9f5ebb8a ZJS |
330 | if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2)) |
331 | return -ENOMEM; | |
332 | ||
333 | files[n_files] = strdup(de->d_name); | |
334 | if (!files[n_files]) | |
335 | return -ENOMEM; | |
336 | n_files ++; | |
337 | } | |
338 | ||
339 | strv_sort(dirs); | |
340 | strv_sort(files); | |
341 | ||
342 | STRV_FOREACH(t, dirs) { | |
343 | r = enumerate_dir_d(top, bottom, drops, path, *t); | |
344 | if (r < 0) | |
345 | return r; | |
346 | } | |
347 | ||
348 | STRV_FOREACH(t, files) { | |
349 | _cleanup_free_ char *p = NULL; | |
350 | ||
351 | p = strjoin(path, "/", *t); | |
e26970a8 TA |
352 | if (!p) |
353 | return -ENOMEM; | |
7e8d5761 | 354 | |
323b7dc9 | 355 | log_debug("Adding at top: %s %s %s", basename(p), special_glyph(ARROW), p); |
9f5ebb8a ZJS |
356 | r = ordered_hashmap_put(top, basename(p), p); |
357 | if (r >= 0) { | |
7e8d5761 | 358 | p = strdup(p); |
e26970a8 TA |
359 | if (!p) |
360 | return -ENOMEM; | |
9f5ebb8a ZJS |
361 | } else if (r != -EEXIST) |
362 | return r; | |
7e8d5761 | 363 | |
323b7dc9 | 364 | log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(ARROW), p); |
9f5ebb8a ZJS |
365 | free(ordered_hashmap_remove(bottom, basename(p))); |
366 | r = ordered_hashmap_put(bottom, basename(p), p); | |
367 | if (r < 0) | |
368 | return r; | |
369 | p = NULL; | |
7e8d5761 | 370 | } |
9f5ebb8a | 371 | |
8fb3f009 | 372 | return 0; |
7e8d5761 LP |
373 | } |
374 | ||
4cc893e7 | 375 | static bool should_skip_path(const char *prefix, const char *suffix) { |
349cc4a5 | 376 | #if HAVE_SPLIT_USR |
b05422a8 | 377 | _cleanup_free_ char *target = NULL; |
4cc893e7 FB |
378 | const char *p; |
379 | char *dirname; | |
b05422a8 | 380 | |
4cc893e7 | 381 | dirname = strjoina(prefix, "/", suffix); |
b05422a8 | 382 | |
4cc893e7 FB |
383 | if (chase_symlinks(dirname, NULL, 0, &target) < 0) |
384 | return false; | |
385 | ||
386 | NULSTR_FOREACH(p, prefixes) { | |
387 | if (path_startswith(dirname, p)) | |
388 | continue; | |
389 | ||
390 | if (path_equal(target, strjoina(p, "/", suffix))) { | |
391 | log_debug("%s redirects to %s, skipping.", dirname, target); | |
392 | return true; | |
393 | } | |
394 | } | |
b05422a8 | 395 | #endif |
4cc893e7 | 396 | return false; |
b05422a8 FS |
397 | } |
398 | ||
6096dfd6 | 399 | static int process_suffix(const char *suffix, const char *onlyprefix) { |
7e8d5761 LP |
400 | const char *p; |
401 | char *f; | |
9f5ebb8a ZJS |
402 | OrderedHashmap *top, *bottom, *drops; |
403 | OrderedHashmap *h; | |
0000ce05 | 404 | char *key; |
7e8d5761 | 405 | int r = 0, k; |
0000ce05 | 406 | Iterator i, j; |
7e8d5761 | 407 | int n_found = 0; |
f939e9a4 | 408 | bool dropins; |
7e8d5761 | 409 | |
7e8d5761 | 410 | assert(suffix); |
f939e9a4 ZJS |
411 | assert(!startswith(suffix, "/")); |
412 | assert(!strstr(suffix, "//")); | |
7e8d5761 | 413 | |
f939e9a4 | 414 | dropins = nulstr_contains(have_dropins, suffix); |
7e8d5761 | 415 | |
9f5ebb8a ZJS |
416 | top = ordered_hashmap_new(&string_hash_ops); |
417 | bottom = ordered_hashmap_new(&string_hash_ops); | |
418 | drops = ordered_hashmap_new(&string_hash_ops); | |
f939e9a4 | 419 | if (!top || !bottom || !drops) { |
0000ce05 LN |
420 | r = -ENOMEM; |
421 | goto finish; | |
422 | } | |
423 | ||
7e8d5761 | 424 | NULSTR_FOREACH(p, prefixes) { |
e26970a8 | 425 | _cleanup_free_ char *t = NULL; |
b05422a8 | 426 | |
4cc893e7 | 427 | if (should_skip_path(p, suffix)) |
b05422a8 | 428 | continue; |
7e8d5761 | 429 | |
605405c6 | 430 | t = strjoin(p, "/", suffix); |
7e8d5761 LP |
431 | if (!t) { |
432 | r = -ENOMEM; | |
433 | goto finish; | |
434 | } | |
435 | ||
0000ce05 | 436 | k = enumerate_dir(top, bottom, drops, t, dropins); |
f939e9a4 | 437 | if (r == 0) |
7e8d5761 | 438 | r = k; |
7e8d5761 LP |
439 | } |
440 | ||
9f5ebb8a | 441 | ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) { |
7e8d5761 LP |
442 | char *o; |
443 | ||
9f5ebb8a | 444 | o = ordered_hashmap_get(bottom, key); |
7e8d5761 LP |
445 | assert(o); |
446 | ||
6096dfd6 ZJS |
447 | if (!onlyprefix || startswith(o, onlyprefix)) { |
448 | if (path_equal(o, f)) { | |
449 | notify_override_unchanged(f); | |
450 | } else { | |
451 | k = found_override(f, o); | |
452 | if (k < 0) | |
453 | r = k; | |
454 | else | |
455 | n_found += k; | |
456 | } | |
807f4645 | 457 | } |
7e8d5761 | 458 | |
9f5ebb8a | 459 | h = ordered_hashmap_get(drops, key); |
0000ce05 | 460 | if (h) |
9f5ebb8a | 461 | ORDERED_HASHMAP_FOREACH(o, h, j) |
6096dfd6 ZJS |
462 | if (!onlyprefix || startswith(o, onlyprefix)) |
463 | n_found += notify_override_extended(f, o); | |
7e8d5761 LP |
464 | } |
465 | ||
466 | finish: | |
9f5ebb8a ZJS |
467 | ordered_hashmap_free_free(top); |
468 | ordered_hashmap_free_free(bottom); | |
82376245 | 469 | |
9f5ebb8a ZJS |
470 | ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) { |
471 | ordered_hashmap_free_free(ordered_hashmap_remove(drops, key)); | |
472 | ordered_hashmap_remove(drops, key); | |
82376245 | 473 | free(key); |
0000ce05 | 474 | } |
9f5ebb8a | 475 | ordered_hashmap_free(drops); |
82376245 | 476 | |
7e8d5761 LP |
477 | return r < 0 ? r : n_found; |
478 | } | |
479 | ||
6096dfd6 ZJS |
480 | static int process_suffixes(const char *onlyprefix) { |
481 | const char *n; | |
482 | int n_found = 0, r; | |
483 | ||
484 | NULSTR_FOREACH(n, suffixes) { | |
485 | r = process_suffix(n, onlyprefix); | |
486 | if (r < 0) | |
487 | return r; | |
82376245 LP |
488 | |
489 | n_found += r; | |
6096dfd6 | 490 | } |
82376245 | 491 | |
6096dfd6 ZJS |
492 | return n_found; |
493 | } | |
494 | ||
495 | static int process_suffix_chop(const char *arg) { | |
7e8d5761 LP |
496 | const char *p; |
497 | ||
6096dfd6 | 498 | assert(arg); |
7e8d5761 | 499 | |
6096dfd6 ZJS |
500 | if (!path_is_absolute(arg)) |
501 | return process_suffix(arg, NULL); | |
7e8d5761 LP |
502 | |
503 | /* Strip prefix from the suffix */ | |
504 | NULSTR_FOREACH(p, prefixes) { | |
82376245 LP |
505 | const char *suffix; |
506 | ||
507 | suffix = startswith(arg, p); | |
6096dfd6 | 508 | if (suffix) { |
7e8d5761 | 509 | suffix += strspn(suffix, "/"); |
6096dfd6 | 510 | if (*suffix) |
a9d041fa | 511 | return process_suffix(suffix, p); |
6096dfd6 ZJS |
512 | else |
513 | return process_suffixes(arg); | |
7e8d5761 LP |
514 | } |
515 | } | |
516 | ||
6096dfd6 | 517 | log_error("Invalid suffix specification %s.", arg); |
7e8d5761 LP |
518 | return -EINVAL; |
519 | } | |
520 | ||
601185b4 | 521 | static void help(void) { |
7e8d5761 LP |
522 | printf("%s [OPTIONS...] [SUFFIX...]\n\n" |
523 | "Find overridden configuration files.\n\n" | |
524 | " -h --help Show this help\n" | |
525 | " --version Show package version\n" | |
807f4645 | 526 | " --no-pager Do not pipe output into a pager\n" |
386da858 | 527 | " --diff[=1|0] Show a diff when overridden files differ\n" |
601185b4 ZJS |
528 | " -t --type=LIST... Only display a selected set of override types\n" |
529 | , program_invocation_short_name); | |
7e8d5761 LP |
530 | } |
531 | ||
866062b1 | 532 | static int parse_flags(const char *flag_str, int flags) { |
a2a5291b | 533 | const char *word, *state; |
807f4645 GN |
534 | size_t l; |
535 | ||
d0a2e1c3 | 536 | FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) { |
a2a5291b | 537 | if (strneq("masked", word, l)) |
807f4645 | 538 | flags |= SHOW_MASKED; |
a2a5291b | 539 | else if (strneq ("equivalent", word, l)) |
c8021373 | 540 | flags |= SHOW_EQUIVALENT; |
a2a5291b | 541 | else if (strneq("redirected", word, l)) |
c8021373 | 542 | flags |= SHOW_REDIRECTED; |
a2a5291b | 543 | else if (strneq("overridden", word, l)) |
386da858 | 544 | flags |= SHOW_OVERRIDDEN; |
a2a5291b | 545 | else if (strneq("unchanged", word, l)) |
807f4645 | 546 | flags |= SHOW_UNCHANGED; |
a2a5291b | 547 | else if (strneq("extended", word, l)) |
0000ce05 | 548 | flags |= SHOW_EXTENDED; |
a2a5291b | 549 | else if (strneq("default", word, l)) |
807f4645 | 550 | flags |= SHOW_DEFAULTS; |
866062b1 LP |
551 | else |
552 | return -EINVAL; | |
807f4645 GN |
553 | } |
554 | return flags; | |
555 | } | |
556 | ||
866062b1 | 557 | static int parse_argv(int argc, char *argv[]) { |
7e8d5761 LP |
558 | |
559 | enum { | |
560 | ARG_NO_PAGER = 0x100, | |
807f4645 | 561 | ARG_DIFF, |
7e8d5761 LP |
562 | ARG_VERSION |
563 | }; | |
564 | ||
565 | static const struct option options[] = { | |
566 | { "help", no_argument, NULL, 'h' }, | |
567 | { "version", no_argument, NULL, ARG_VERSION }, | |
568 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
807f4645 GN |
569 | { "diff", optional_argument, NULL, ARG_DIFF }, |
570 | { "type", required_argument, NULL, 't' }, | |
eb9da376 | 571 | {} |
7e8d5761 LP |
572 | }; |
573 | ||
574 | int c; | |
575 | ||
576 | assert(argc >= 1); | |
577 | assert(argv); | |
578 | ||
601185b4 | 579 | while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0) |
7e8d5761 LP |
580 | |
581 | switch (c) { | |
582 | ||
583 | case 'h': | |
584 | help(); | |
585 | return 0; | |
586 | ||
587 | case ARG_VERSION: | |
3f6fd1ba | 588 | return version(); |
7e8d5761 LP |
589 | |
590 | case ARG_NO_PAGER: | |
591 | arg_no_pager = true; | |
592 | break; | |
593 | ||
866062b1 LP |
594 | case 't': { |
595 | int f; | |
596 | f = parse_flags(optarg, arg_flags); | |
597 | if (f < 0) { | |
598 | log_error("Failed to parse flags field."); | |
807f4645 | 599 | return -EINVAL; |
866062b1 LP |
600 | } |
601 | arg_flags = f; | |
807f4645 | 602 | break; |
866062b1 | 603 | } |
807f4645 GN |
604 | |
605 | case ARG_DIFF: | |
866062b1 LP |
606 | if (!optarg) |
607 | arg_diff = 1; | |
608 | else { | |
609 | int b; | |
610 | ||
611 | b = parse_boolean(optarg); | |
612 | if (b < 0) { | |
613 | log_error("Failed to parse diff boolean."); | |
614 | return -EINVAL; | |
82376245 LP |
615 | } |
616 | ||
617 | arg_diff = b; | |
807f4645 GN |
618 | } |
619 | break; | |
620 | ||
eb9da376 | 621 | case '?': |
7e8d5761 | 622 | return -EINVAL; |
eb9da376 LP |
623 | |
624 | default: | |
625 | assert_not_reached("Unhandled option"); | |
7e8d5761 | 626 | } |
7e8d5761 LP |
627 | |
628 | return 1; | |
629 | } | |
630 | ||
631 | int main(int argc, char *argv[]) { | |
82376245 | 632 | int r, k, n_found = 0; |
7e8d5761 LP |
633 | |
634 | log_parse_environment(); | |
635 | log_open(); | |
636 | ||
866062b1 | 637 | r = parse_argv(argc, argv); |
7e8d5761 LP |
638 | if (r <= 0) |
639 | goto finish; | |
640 | ||
866062b1 LP |
641 | if (arg_flags == 0) |
642 | arg_flags = SHOW_DEFAULTS; | |
643 | ||
644 | if (arg_diff < 0) | |
386da858 | 645 | arg_diff = !!(arg_flags & SHOW_OVERRIDDEN); |
866062b1 | 646 | else if (arg_diff) |
386da858 | 647 | arg_flags |= SHOW_OVERRIDDEN; |
807f4645 | 648 | |
ee5324aa | 649 | (void) pager_open(arg_no_pager, false); |
7e8d5761 LP |
650 | |
651 | if (optind < argc) { | |
652 | int i; | |
653 | ||
654 | for (i = optind; i < argc; i++) { | |
858d36c1 | 655 | path_simplify(argv[i], false); |
82376245 | 656 | |
f939e9a4 | 657 | k = process_suffix_chop(argv[i]); |
7e8d5761 LP |
658 | if (k < 0) |
659 | r = k; | |
660 | else | |
661 | n_found += k; | |
662 | } | |
663 | ||
664 | } else { | |
6096dfd6 ZJS |
665 | k = process_suffixes(NULL); |
666 | if (k < 0) | |
667 | r = k; | |
668 | else | |
669 | n_found += k; | |
7e8d5761 LP |
670 | } |
671 | ||
672 | if (r >= 0) | |
82376245 | 673 | printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found); |
7e8d5761 LP |
674 | |
675 | finish: | |
676 | pager_close(); | |
677 | ||
678 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
679 | } |