]>
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" |
f4351959 | 9 | #include "chase-symlinks.h" |
a0956174 | 10 | #include "dirent-util.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
f4f15635 | 12 | #include "fs-util.h" |
d8e32c47 | 13 | #include "glyph-util.h" |
7e8d5761 | 14 | #include "hashmap.h" |
7e8d5761 | 15 | #include "log.h" |
7280b076 | 16 | #include "main-func.h" |
d8b4d14d | 17 | #include "nulstr-util.h" |
7e8d5761 | 18 | #include "pager.h" |
c3470872 | 19 | #include "parse-argument.h" |
6bedfcbb | 20 | #include "parse-util.h" |
3f6fd1ba | 21 | #include "path-util.h" |
294bf0c3 | 22 | #include "pretty-print.h" |
0b452006 | 23 | #include "process-util.h" |
ce30c8dc | 24 | #include "signal-util.h" |
8fcde012 | 25 | #include "stat-util.h" |
07630cea | 26 | #include "string-util.h" |
3f6fd1ba LP |
27 | #include "strv.h" |
28 | #include "terminal-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 | ||
0221d68a | 58 | static PagerFlags arg_pager_flags = 0; |
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 | |
a5648b80 | 77 | r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x, NULL); |
e1873695 LP |
78 | if (r < 0) |
79 | return r; | |
7e8d5761 | 80 | |
a5648b80 | 81 | r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y, NULL); |
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(), |
fc03e80c | 94 | top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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(), |
fc03e80c | 104 | top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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(), |
fc03e80c | 114 | top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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(), |
fc03e80c | 124 | top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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(), |
fc03e80c | 134 | top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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 | ||
0672e2c6 | 173 | r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|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; | |
0000ce05 LN |
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 | |
fc03e80c | 238 | log_debug("Adding at top: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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 | ||
fc03e80c | 250 | log_debug("Adding at bottom: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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", |
fc03e80c | 274 | unit, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), 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; | |
9f5ebb8a | 292 | _cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
319a4f4b | 293 | size_t n_files = 0, n_dirs = 0; |
9f5ebb8a | 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) { |
9f5ebb8a | 312 | if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) { |
319a4f4b | 313 | if (!GREEDY_REALLOC0(dirs, n_dirs + 2)) |
9f5ebb8a ZJS |
314 | return -ENOMEM; |
315 | ||
316 | dirs[n_dirs] = strdup(de->d_name); | |
317 | if (!dirs[n_dirs]) | |
318 | return -ENOMEM; | |
319 | n_dirs ++; | |
320 | } | |
0000ce05 | 321 | |
7e8d5761 LP |
322 | if (!dirent_is_file(de)) |
323 | continue; | |
324 | ||
319a4f4b | 325 | if (!GREEDY_REALLOC0(files, n_files + 2)) |
9f5ebb8a ZJS |
326 | return -ENOMEM; |
327 | ||
328 | files[n_files] = strdup(de->d_name); | |
329 | if (!files[n_files]) | |
330 | return -ENOMEM; | |
331 | n_files ++; | |
332 | } | |
333 | ||
334 | strv_sort(dirs); | |
335 | strv_sort(files); | |
336 | ||
337 | STRV_FOREACH(t, dirs) { | |
338 | r = enumerate_dir_d(top, bottom, drops, path, *t); | |
339 | if (r < 0) | |
340 | return r; | |
341 | } | |
342 | ||
343 | STRV_FOREACH(t, files) { | |
344 | _cleanup_free_ char *p = NULL; | |
345 | ||
657ee2d8 | 346 | p = path_join(path, *t); |
e26970a8 TA |
347 | if (!p) |
348 | return -ENOMEM; | |
7e8d5761 | 349 | |
fc03e80c | 350 | log_debug("Adding at top: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p); |
9f5ebb8a ZJS |
351 | r = ordered_hashmap_put(top, basename(p), p); |
352 | if (r >= 0) { | |
7e8d5761 | 353 | p = strdup(p); |
e26970a8 TA |
354 | if (!p) |
355 | return -ENOMEM; | |
9f5ebb8a ZJS |
356 | } else if (r != -EEXIST) |
357 | return r; | |
7e8d5761 | 358 | |
fc03e80c | 359 | log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p); |
9f5ebb8a ZJS |
360 | free(ordered_hashmap_remove(bottom, basename(p))); |
361 | r = ordered_hashmap_put(bottom, basename(p), p); | |
362 | if (r < 0) | |
363 | return r; | |
364 | p = NULL; | |
7e8d5761 | 365 | } |
9f5ebb8a | 366 | |
8fb3f009 | 367 | return 0; |
7e8d5761 LP |
368 | } |
369 | ||
6d946490 | 370 | static int should_skip_path(const char *prefix, const char *suffix) { |
349cc4a5 | 371 | #if HAVE_SPLIT_USR |
b05422a8 | 372 | _cleanup_free_ char *target = NULL; |
270384b2 | 373 | const char *dirname, *p; |
b05422a8 | 374 | |
270384b2 | 375 | dirname = prefix_roota(prefix, suffix); |
b05422a8 | 376 | |
a5648b80 | 377 | if (chase_symlinks(dirname, NULL, 0, &target, NULL) < 0) |
4cc893e7 FB |
378 | return false; |
379 | ||
380 | NULSTR_FOREACH(p, prefixes) { | |
6d946490 YW |
381 | _cleanup_free_ char *tmp = NULL; |
382 | ||
4cc893e7 FB |
383 | if (path_startswith(dirname, p)) |
384 | continue; | |
385 | ||
6d946490 YW |
386 | tmp = path_join(p, suffix); |
387 | if (!tmp) | |
388 | return -ENOMEM; | |
389 | ||
390 | if (path_equal(target, tmp)) { | |
4cc893e7 FB |
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 | 400 | const char *p; |
90e74a66 ZJS |
401 | char *f, *key; |
402 | OrderedHashmap *top, *bottom, *drops, *h; | |
403 | int r = 0, k, n_found = 0; | |
f939e9a4 | 404 | bool dropins; |
7e8d5761 | 405 | |
7e8d5761 | 406 | assert(suffix); |
f939e9a4 ZJS |
407 | assert(!startswith(suffix, "/")); |
408 | assert(!strstr(suffix, "//")); | |
7e8d5761 | 409 | |
f939e9a4 | 410 | dropins = nulstr_contains(have_dropins, suffix); |
7e8d5761 | 411 | |
9f5ebb8a ZJS |
412 | top = ordered_hashmap_new(&string_hash_ops); |
413 | bottom = ordered_hashmap_new(&string_hash_ops); | |
414 | drops = ordered_hashmap_new(&string_hash_ops); | |
f939e9a4 | 415 | if (!top || !bottom || !drops) { |
0000ce05 LN |
416 | r = -ENOMEM; |
417 | goto finish; | |
418 | } | |
419 | ||
7e8d5761 | 420 | NULSTR_FOREACH(p, prefixes) { |
e26970a8 | 421 | _cleanup_free_ char *t = NULL; |
b05422a8 | 422 | |
6d946490 | 423 | if (should_skip_path(p, suffix) > 0) |
b05422a8 | 424 | continue; |
7e8d5761 | 425 | |
657ee2d8 | 426 | t = path_join(p, suffix); |
7e8d5761 LP |
427 | if (!t) { |
428 | r = -ENOMEM; | |
429 | goto finish; | |
430 | } | |
431 | ||
0000ce05 | 432 | k = enumerate_dir(top, bottom, drops, t, dropins); |
f939e9a4 | 433 | if (r == 0) |
7e8d5761 | 434 | r = k; |
7e8d5761 LP |
435 | } |
436 | ||
90e74a66 | 437 | ORDERED_HASHMAP_FOREACH_KEY(f, key, top) { |
7e8d5761 LP |
438 | char *o; |
439 | ||
9f5ebb8a | 440 | o = ordered_hashmap_get(bottom, key); |
7e8d5761 LP |
441 | assert(o); |
442 | ||
6096dfd6 ZJS |
443 | if (!onlyprefix || startswith(o, onlyprefix)) { |
444 | if (path_equal(o, f)) { | |
445 | notify_override_unchanged(f); | |
446 | } else { | |
447 | k = found_override(f, o); | |
448 | if (k < 0) | |
449 | r = k; | |
450 | else | |
451 | n_found += k; | |
452 | } | |
807f4645 | 453 | } |
7e8d5761 | 454 | |
9f5ebb8a | 455 | h = ordered_hashmap_get(drops, key); |
0000ce05 | 456 | if (h) |
90e74a66 | 457 | ORDERED_HASHMAP_FOREACH(o, h) |
6096dfd6 ZJS |
458 | if (!onlyprefix || startswith(o, onlyprefix)) |
459 | n_found += notify_override_extended(f, o); | |
7e8d5761 LP |
460 | } |
461 | ||
462 | finish: | |
9f5ebb8a ZJS |
463 | ordered_hashmap_free_free(top); |
464 | ordered_hashmap_free_free(bottom); | |
82376245 | 465 | |
90e74a66 | 466 | ORDERED_HASHMAP_FOREACH_KEY(h, key, drops) { |
9f5ebb8a ZJS |
467 | ordered_hashmap_free_free(ordered_hashmap_remove(drops, key)); |
468 | ordered_hashmap_remove(drops, key); | |
82376245 | 469 | free(key); |
0000ce05 | 470 | } |
9f5ebb8a | 471 | ordered_hashmap_free(drops); |
82376245 | 472 | |
7e8d5761 LP |
473 | return r < 0 ? r : n_found; |
474 | } | |
475 | ||
6096dfd6 ZJS |
476 | static int process_suffixes(const char *onlyprefix) { |
477 | const char *n; | |
478 | int n_found = 0, r; | |
479 | ||
480 | NULSTR_FOREACH(n, suffixes) { | |
481 | r = process_suffix(n, onlyprefix); | |
482 | if (r < 0) | |
483 | return r; | |
82376245 LP |
484 | |
485 | n_found += r; | |
6096dfd6 | 486 | } |
82376245 | 487 | |
6096dfd6 ZJS |
488 | return n_found; |
489 | } | |
490 | ||
491 | static int process_suffix_chop(const char *arg) { | |
7e8d5761 LP |
492 | const char *p; |
493 | ||
6096dfd6 | 494 | assert(arg); |
7e8d5761 | 495 | |
6096dfd6 ZJS |
496 | if (!path_is_absolute(arg)) |
497 | return process_suffix(arg, NULL); | |
7e8d5761 LP |
498 | |
499 | /* Strip prefix from the suffix */ | |
500 | NULSTR_FOREACH(p, prefixes) { | |
82376245 LP |
501 | const char *suffix; |
502 | ||
503 | suffix = startswith(arg, p); | |
6096dfd6 | 504 | if (suffix) { |
7e8d5761 | 505 | suffix += strspn(suffix, "/"); |
6096dfd6 | 506 | if (*suffix) |
a9d041fa | 507 | return process_suffix(suffix, p); |
6096dfd6 ZJS |
508 | else |
509 | return process_suffixes(arg); | |
7e8d5761 LP |
510 | } |
511 | } | |
512 | ||
baaa35ad ZJS |
513 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
514 | "Invalid suffix specification %s.", arg); | |
7e8d5761 LP |
515 | } |
516 | ||
37ec0fdd LP |
517 | static int help(void) { |
518 | _cleanup_free_ char *link = NULL; | |
519 | int r; | |
520 | ||
521 | r = terminal_urlify_man("systemd-delta", "1", &link); | |
522 | if (r < 0) | |
523 | return log_oom(); | |
524 | ||
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 | 531 | " -t --type=LIST... Only display a selected set of override types\n" |
bc556335 DDM |
532 | "\nSee the %s for details.\n", |
533 | program_invocation_short_name, | |
534 | link); | |
37ec0fdd LP |
535 | |
536 | return 0; | |
7e8d5761 LP |
537 | } |
538 | ||
866062b1 | 539 | static int parse_flags(const char *flag_str, int flags) { |
cc24f0b8 ZJS |
540 | for (;;) { |
541 | _cleanup_free_ char *word = NULL; | |
542 | int r; | |
807f4645 | 543 | |
cc24f0b8 ZJS |
544 | r = extract_first_word(&flag_str, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS); |
545 | if (r < 0) | |
546 | return r; | |
547 | if (r == 0) | |
548 | return flags; | |
549 | ||
550 | if (streq(word, "masked")) | |
807f4645 | 551 | flags |= SHOW_MASKED; |
cc24f0b8 | 552 | else if (streq(word, "equivalent")) |
c8021373 | 553 | flags |= SHOW_EQUIVALENT; |
cc24f0b8 | 554 | else if (streq(word, "redirected")) |
c8021373 | 555 | flags |= SHOW_REDIRECTED; |
cc24f0b8 | 556 | else if (streq(word, "overridden")) |
386da858 | 557 | flags |= SHOW_OVERRIDDEN; |
cc24f0b8 | 558 | else if (streq(word, "unchanged")) |
807f4645 | 559 | flags |= SHOW_UNCHANGED; |
cc24f0b8 | 560 | else if (streq(word, "extended")) |
0000ce05 | 561 | flags |= SHOW_EXTENDED; |
cc24f0b8 | 562 | else if (streq(word, "default")) |
807f4645 | 563 | flags |= SHOW_DEFAULTS; |
866062b1 LP |
564 | else |
565 | return -EINVAL; | |
807f4645 | 566 | } |
807f4645 GN |
567 | } |
568 | ||
866062b1 | 569 | static int parse_argv(int argc, char *argv[]) { |
7e8d5761 LP |
570 | |
571 | enum { | |
572 | ARG_NO_PAGER = 0x100, | |
807f4645 | 573 | ARG_DIFF, |
7e8d5761 LP |
574 | ARG_VERSION |
575 | }; | |
576 | ||
577 | static const struct option options[] = { | |
578 | { "help", no_argument, NULL, 'h' }, | |
579 | { "version", no_argument, NULL, ARG_VERSION }, | |
580 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
807f4645 GN |
581 | { "diff", optional_argument, NULL, ARG_DIFF }, |
582 | { "type", required_argument, NULL, 't' }, | |
eb9da376 | 583 | {} |
7e8d5761 LP |
584 | }; |
585 | ||
c3470872 | 586 | int c, r; |
7e8d5761 LP |
587 | |
588 | assert(argc >= 1); | |
589 | assert(argv); | |
590 | ||
601185b4 | 591 | while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0) |
7e8d5761 LP |
592 | |
593 | switch (c) { | |
594 | ||
595 | case 'h': | |
37ec0fdd | 596 | return help(); |
7e8d5761 LP |
597 | |
598 | case ARG_VERSION: | |
3f6fd1ba | 599 | return version(); |
7e8d5761 LP |
600 | |
601 | case ARG_NO_PAGER: | |
0221d68a | 602 | arg_pager_flags |= PAGER_DISABLE; |
7e8d5761 LP |
603 | break; |
604 | ||
866062b1 LP |
605 | case 't': { |
606 | int f; | |
607 | f = parse_flags(optarg, arg_flags); | |
baaa35ad ZJS |
608 | if (f < 0) |
609 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
610 | "Failed to parse flags field."); | |
866062b1 | 611 | arg_flags = f; |
807f4645 | 612 | break; |
866062b1 | 613 | } |
807f4645 GN |
614 | |
615 | case ARG_DIFF: | |
c3470872 ZJS |
616 | r = parse_boolean_argument("--diff", optarg, NULL); |
617 | if (r < 0) | |
618 | return r; | |
619 | arg_diff = r; | |
807f4645 GN |
620 | break; |
621 | ||
eb9da376 | 622 | case '?': |
7e8d5761 | 623 | return -EINVAL; |
eb9da376 LP |
624 | |
625 | default: | |
04499a70 | 626 | assert_not_reached(); |
7e8d5761 | 627 | } |
7e8d5761 LP |
628 | |
629 | return 1; | |
630 | } | |
631 | ||
7280b076 | 632 | static int run(int argc, char *argv[]) { |
82376245 | 633 | int r, k, n_found = 0; |
7e8d5761 | 634 | |
d2acb93d | 635 | log_setup(); |
7e8d5761 | 636 | |
866062b1 | 637 | r = parse_argv(argc, argv); |
7e8d5761 | 638 | if (r <= 0) |
7280b076 | 639 | return r; |
7e8d5761 | 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 | |
384c2c32 | 649 | pager_open(arg_pager_flags); |
7e8d5761 LP |
650 | |
651 | if (optind < argc) { | |
652 | int i; | |
653 | ||
654 | for (i = optind; i < argc; i++) { | |
4ff361cc | 655 | path_simplify(argv[i]); |
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); |
7280b076 | 674 | return r; |
7e8d5761 | 675 | } |
7280b076 ZJS |
676 | |
677 | DEFINE_MAIN_FUNCTION(run); |