]>
Commit | Line | Data |
---|---|---|
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" |
a0956174 | 9 | #include "dirent-util.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
f4f15635 | 11 | #include "fs-util.h" |
7e8d5761 | 12 | #include "hashmap.h" |
8752c575 | 13 | #include "locale-util.h" |
7e8d5761 | 14 | #include "log.h" |
7280b076 | 15 | #include "main-func.h" |
d8b4d14d | 16 | #include "nulstr-util.h" |
7e8d5761 | 17 | #include "pager.h" |
c3470872 | 18 | #include "parse-argument.h" |
6bedfcbb | 19 | #include "parse-util.h" |
3f6fd1ba | 20 | #include "path-util.h" |
294bf0c3 | 21 | #include "pretty-print.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" | |
7e8d5761 | 28 | |
f939e9a4 ZJS |
29 | static 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 | ||
41 | static 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 | ||
53 | static const char have_dropins[] = | |
54 | "systemd/system\0" | |
55 | "systemd/user\0"; | |
56 | ||
0221d68a | 57 | static PagerFlags arg_pager_flags = 0; |
866062b1 | 58 | static int arg_diff = -1; |
7e8d5761 | 59 | |
866062b1 | 60 | static 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 | 72 | static int equivalent(const char *a, const char *b) { |
e26970a8 | 73 | _cleanup_free_ char *x = NULL, *y = NULL; |
e1873695 | 74 | int r; |
7e8d5761 | 75 | |
a5648b80 | 76 | r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x, NULL); |
e1873695 LP |
77 | if (r < 0) |
78 | return r; | |
7e8d5761 | 79 | |
a5648b80 | 80 | r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y, NULL); |
e1873695 LP |
81 | if (r < 0) |
82 | return r; | |
7e8d5761 | 83 | |
e26970a8 | 84 | return path_equal(x, y); |
7e8d5761 LP |
85 | } |
86 | ||
866062b1 LP |
87 | static 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 |
97 | static 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 |
107 | static 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 |
117 | static 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 |
127 | static 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 |
137 | static 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 | 145 | static 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 |
188 | static 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 | ||
657ee2d8 | 203 | path = path_join(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 | ||
657ee2d8 | 233 | p = path_join(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 |
285 | static 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 | ||
657ee2d8 | 350 | p = path_join(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 | ||
6d946490 | 374 | static int should_skip_path(const char *prefix, const char *suffix) { |
349cc4a5 | 375 | #if HAVE_SPLIT_USR |
b05422a8 | 376 | _cleanup_free_ char *target = NULL; |
270384b2 | 377 | const char *dirname, *p; |
b05422a8 | 378 | |
270384b2 | 379 | dirname = prefix_roota(prefix, suffix); |
b05422a8 | 380 | |
a5648b80 | 381 | if (chase_symlinks(dirname, NULL, 0, &target, NULL) < 0) |
4cc893e7 FB |
382 | return false; |
383 | ||
384 | NULSTR_FOREACH(p, prefixes) { | |
6d946490 YW |
385 | _cleanup_free_ char *tmp = NULL; |
386 | ||
4cc893e7 FB |
387 | if (path_startswith(dirname, p)) |
388 | continue; | |
389 | ||
6d946490 YW |
390 | tmp = path_join(p, suffix); |
391 | if (!tmp) | |
392 | return -ENOMEM; | |
393 | ||
394 | if (path_equal(target, tmp)) { | |
4cc893e7 FB |
395 | log_debug("%s redirects to %s, skipping.", dirname, target); |
396 | return true; | |
397 | } | |
398 | } | |
b05422a8 | 399 | #endif |
4cc893e7 | 400 | return false; |
b05422a8 FS |
401 | } |
402 | ||
6096dfd6 | 403 | static int process_suffix(const char *suffix, const char *onlyprefix) { |
7e8d5761 | 404 | const char *p; |
90e74a66 ZJS |
405 | char *f, *key; |
406 | OrderedHashmap *top, *bottom, *drops, *h; | |
407 | int r = 0, k, 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 | |
6d946490 | 427 | if (should_skip_path(p, suffix) > 0) |
b05422a8 | 428 | continue; |
7e8d5761 | 429 | |
657ee2d8 | 430 | t = path_join(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 | ||
90e74a66 | 441 | ORDERED_HASHMAP_FOREACH_KEY(f, key, top) { |
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) |
90e74a66 | 461 | ORDERED_HASHMAP_FOREACH(o, h) |
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 | |
90e74a66 | 470 | ORDERED_HASHMAP_FOREACH_KEY(h, key, drops) { |
9f5ebb8a ZJS |
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 | ||
baaa35ad ZJS |
517 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
518 | "Invalid suffix specification %s.", arg); | |
7e8d5761 LP |
519 | } |
520 | ||
37ec0fdd LP |
521 | static int help(void) { |
522 | _cleanup_free_ char *link = NULL; | |
523 | int r; | |
524 | ||
525 | r = terminal_urlify_man("systemd-delta", "1", &link); | |
526 | if (r < 0) | |
527 | return log_oom(); | |
528 | ||
7e8d5761 LP |
529 | printf("%s [OPTIONS...] [SUFFIX...]\n\n" |
530 | "Find overridden configuration files.\n\n" | |
531 | " -h --help Show this help\n" | |
532 | " --version Show package version\n" | |
807f4645 | 533 | " --no-pager Do not pipe output into a pager\n" |
386da858 | 534 | " --diff[=1|0] Show a diff when overridden files differ\n" |
601185b4 | 535 | " -t --type=LIST... Only display a selected set of override types\n" |
bc556335 DDM |
536 | "\nSee the %s for details.\n", |
537 | program_invocation_short_name, | |
538 | link); | |
37ec0fdd LP |
539 | |
540 | return 0; | |
7e8d5761 LP |
541 | } |
542 | ||
866062b1 | 543 | static int parse_flags(const char *flag_str, int flags) { |
cc24f0b8 ZJS |
544 | for (;;) { |
545 | _cleanup_free_ char *word = NULL; | |
546 | int r; | |
807f4645 | 547 | |
cc24f0b8 ZJS |
548 | r = extract_first_word(&flag_str, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS); |
549 | if (r < 0) | |
550 | return r; | |
551 | if (r == 0) | |
552 | return flags; | |
553 | ||
554 | if (streq(word, "masked")) | |
807f4645 | 555 | flags |= SHOW_MASKED; |
cc24f0b8 | 556 | else if (streq(word, "equivalent")) |
c8021373 | 557 | flags |= SHOW_EQUIVALENT; |
cc24f0b8 | 558 | else if (streq(word, "redirected")) |
c8021373 | 559 | flags |= SHOW_REDIRECTED; |
cc24f0b8 | 560 | else if (streq(word, "overridden")) |
386da858 | 561 | flags |= SHOW_OVERRIDDEN; |
cc24f0b8 | 562 | else if (streq(word, "unchanged")) |
807f4645 | 563 | flags |= SHOW_UNCHANGED; |
cc24f0b8 | 564 | else if (streq(word, "extended")) |
0000ce05 | 565 | flags |= SHOW_EXTENDED; |
cc24f0b8 | 566 | else if (streq(word, "default")) |
807f4645 | 567 | flags |= SHOW_DEFAULTS; |
866062b1 LP |
568 | else |
569 | return -EINVAL; | |
807f4645 | 570 | } |
807f4645 GN |
571 | } |
572 | ||
866062b1 | 573 | static int parse_argv(int argc, char *argv[]) { |
7e8d5761 LP |
574 | |
575 | enum { | |
576 | ARG_NO_PAGER = 0x100, | |
807f4645 | 577 | ARG_DIFF, |
7e8d5761 LP |
578 | ARG_VERSION |
579 | }; | |
580 | ||
581 | static const struct option options[] = { | |
582 | { "help", no_argument, NULL, 'h' }, | |
583 | { "version", no_argument, NULL, ARG_VERSION }, | |
584 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
807f4645 GN |
585 | { "diff", optional_argument, NULL, ARG_DIFF }, |
586 | { "type", required_argument, NULL, 't' }, | |
eb9da376 | 587 | {} |
7e8d5761 LP |
588 | }; |
589 | ||
c3470872 | 590 | int c, r; |
7e8d5761 LP |
591 | |
592 | assert(argc >= 1); | |
593 | assert(argv); | |
594 | ||
601185b4 | 595 | while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0) |
7e8d5761 LP |
596 | |
597 | switch (c) { | |
598 | ||
599 | case 'h': | |
37ec0fdd | 600 | return help(); |
7e8d5761 LP |
601 | |
602 | case ARG_VERSION: | |
3f6fd1ba | 603 | return version(); |
7e8d5761 LP |
604 | |
605 | case ARG_NO_PAGER: | |
0221d68a | 606 | arg_pager_flags |= PAGER_DISABLE; |
7e8d5761 LP |
607 | break; |
608 | ||
866062b1 LP |
609 | case 't': { |
610 | int f; | |
611 | f = parse_flags(optarg, arg_flags); | |
baaa35ad ZJS |
612 | if (f < 0) |
613 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
614 | "Failed to parse flags field."); | |
866062b1 | 615 | arg_flags = f; |
807f4645 | 616 | break; |
866062b1 | 617 | } |
807f4645 GN |
618 | |
619 | case ARG_DIFF: | |
c3470872 ZJS |
620 | r = parse_boolean_argument("--diff", optarg, NULL); |
621 | if (r < 0) | |
622 | return r; | |
623 | arg_diff = r; | |
807f4645 GN |
624 | break; |
625 | ||
eb9da376 | 626 | case '?': |
7e8d5761 | 627 | return -EINVAL; |
eb9da376 LP |
628 | |
629 | default: | |
630 | assert_not_reached("Unhandled option"); | |
7e8d5761 | 631 | } |
7e8d5761 LP |
632 | |
633 | return 1; | |
634 | } | |
635 | ||
7280b076 | 636 | static int run(int argc, char *argv[]) { |
82376245 | 637 | int r, k, n_found = 0; |
7e8d5761 | 638 | |
d2acb93d | 639 | log_setup(); |
7e8d5761 | 640 | |
866062b1 | 641 | r = parse_argv(argc, argv); |
7e8d5761 | 642 | if (r <= 0) |
7280b076 | 643 | return r; |
7e8d5761 | 644 | |
866062b1 LP |
645 | if (arg_flags == 0) |
646 | arg_flags = SHOW_DEFAULTS; | |
647 | ||
648 | if (arg_diff < 0) | |
386da858 | 649 | arg_diff = !!(arg_flags & SHOW_OVERRIDDEN); |
866062b1 | 650 | else if (arg_diff) |
386da858 | 651 | arg_flags |= SHOW_OVERRIDDEN; |
807f4645 | 652 | |
0221d68a | 653 | (void) pager_open(arg_pager_flags); |
7e8d5761 LP |
654 | |
655 | if (optind < argc) { | |
656 | int i; | |
657 | ||
658 | for (i = optind; i < argc; i++) { | |
858d36c1 | 659 | path_simplify(argv[i], false); |
82376245 | 660 | |
f939e9a4 | 661 | k = process_suffix_chop(argv[i]); |
7e8d5761 LP |
662 | if (k < 0) |
663 | r = k; | |
664 | else | |
665 | n_found += k; | |
666 | } | |
667 | ||
668 | } else { | |
6096dfd6 ZJS |
669 | k = process_suffixes(NULL); |
670 | if (k < 0) | |
671 | r = k; | |
672 | else | |
673 | n_found += k; | |
7e8d5761 LP |
674 | } |
675 | ||
676 | if (r >= 0) | |
82376245 | 677 | printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found); |
7280b076 | 678 | return r; |
7e8d5761 | 679 | } |
7280b076 ZJS |
680 | |
681 | DEFINE_MAIN_FUNCTION(run); |