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