Debian Codegraph @

src/basic/conf-files.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "alloc-util.h"
7 #include "chase.h"
8 #include "conf-files.h"
9 #include "dirent-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "glyph-util.h"
14 #include "hashmap.h"
15 #include "log.h"
16 #include "nulstr-util.h"
17 #include "path-util.h"
18 #include "set.h"
19 #include "stat-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22
23 ConfFile* conf_file_free(ConfFile *c) {
24 if (!c)
25 return NULL;
26
27 free(c->filename);
28 free(c->result);
29 free(c->original_path);
30 free(c->resolved_path);
31 safe_close(c->fd);
32
33 return mfree(c);
34 }
35
36 DEFINE_POINTER_ARRAY_FREE_FUNC(ConfFile*, conf_file_free);
37
38 static int conf_files_log_level(ConfFilesFlags flags) {
39 return FLAGS_SET(flags, CONF_FILES_WARN) ? LOG_WARNING : LOG_DEBUG;
40 }
41
42 static int prepare_dirs(
43 const char *root,
44 ConfFilesFlags flags,
45 char * const *dirs,
46 char **ret_root,
47 int *ret_rfd,
48 char ***ret_dirs) {
49
50 _cleanup_free_ char *root_abs = NULL;
51 _cleanup_strv_free_ char **dirs_abs = NULL;
52 int r;
53
54 assert(ret_root);
55 assert(ret_rfd);
56 assert(ret_dirs || strv_isempty(dirs));
57
58 int log_level = conf_files_log_level(flags);
59
60 r = empty_or_root_harder_to_null(&root);
61 if (r < 0)
62 return log_full_errno(log_level, r, "Failed to determine if '%s' points to the root directory: %m", strempty(root));
63
64 if (ret_dirs) {
65 dirs_abs = strv_copy(dirs);
66 if (!dirs_abs)
67 return log_oom_full(log_level);
68 }
69
70 _cleanup_close_ int rfd = XAT_FDROOT;
71 if (root) {
72 /* When a non-trivial root is specified, we will prefix the result later. Hence, it is not
73 * necessary to modify each config directories here. but needs to normalize the root directory. */
74 r = path_make_absolute_cwd(root, &root_abs);
75 if (r < 0)
76 return log_full_errno(log_level, r, "Failed to make '%s' absolute: %m", root);
77
78 path_simplify(root_abs);
79
80 rfd = open(root, O_CLOEXEC|O_DIRECTORY|O_PATH);
81 if (rfd < 0)
82 return log_full_errno(log_level, errno, "Failed to open '%s': %m", root_abs);
83
84 } else if (ret_dirs) {
85 /* When an empty root or "/" is specified, we will open "/" below, hence we need to make
86 * each config directory absolute if relative. */
87 r = path_strv_make_absolute_cwd(dirs_abs);
88 if (r < 0)
89 return log_full_errno(log_level, r, "Failed to make directories absolute: %m");
90 }
91
92 *ret_root = TAKE_PTR(root_abs);
93 *ret_rfd = TAKE_FD(rfd);
94 if (ret_dirs)
95 *ret_dirs = TAKE_PTR(dirs_abs);
96 return 0;
97 }
98
99 static int conf_file_prefix_root(ConfFile *c, const char *root, ConfFilesFlags flags) {
100 char *p;
101 int r;
102
103 assert(c);
104
105 int log_level = conf_files_log_level(flags);
106
107 r = chaseat_prefix_root(c->result, root, &p);
108 if (r < 0)
109 return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
110 free_and_replace(c->result, p);
111
112 r = chaseat_prefix_root(c->resolved_path, root, &p);
113 if (r < 0)
114 return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->resolved_path, root);
115 free_and_replace(c->resolved_path, p);
116
117 /* Do not use chaseat_prefix_root(), as it is for the result of chaseat(), but the path is not chased. */
118 p = path_join(empty_to_root(root), skip_leading_slash(c->original_path));
119 if (!p)
120 return log_oom_full(log_level);
121
122 path_simplify(p);
123 return free_and_replace(c->original_path, p);
124 }
125
126 static bool conf_files_need_stat(ConfFilesFlags flags) {
127 return (flags & (CONF_FILES_FILTER_MASKED | CONF_FILES_REGULAR | CONF_FILES_DIRECTORY | CONF_FILES_EXECUTABLE)) != 0;
128 }
129
130 static ChaseFlags conf_files_chase_flags(ConfFilesFlags flags) {
131 ChaseFlags chase_flags = 0;
132
133 if (!conf_files_need_stat(flags) || FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK))
134 /* Even if no verification is requested, let's unconditionally call chaseat(),
135 * to drop unsafe symlinks. */
136 chase_flags |= CHASE_NONEXISTENT;
137
138 return chase_flags;
139 }
140
141 static int conf_file_chase_and_verify(
142 const char *root, /* for logging, can be NULL */
143 int rfd,
144 const char *original_path, /* for logging */
145 const char *path,
146 const char *name,
147 Set **masked, /* optional */
148 ConfFilesFlags flags,
149 char **ret_path,
150 int *ret_fd,
151 struct stat *ret_stat) {
152
153 _cleanup_free_ char *resolved_path = NULL;
154 _cleanup_close_ int fd = -EBADF;
155 struct stat st = {};
156 int r;
157
158 assert(wildcard_fd_is_valid(rfd));
159 assert(original_path);
160 assert(path);
161 assert(name);
162
163 int log_level = conf_files_log_level(flags);
164
165 root = empty_to_root(root);
166
167 r = chaseat(rfd, rfd, path, conf_files_chase_flags(flags), &resolved_path, &fd);
168 if (r < 0)
169 return log_full_errno(log_level, r, "Failed to chase '%s%s': %m",
170 root, skip_leading_slash(original_path));
171 if (r == 0) {
172 if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK)) {
173 /* If the path points to /dev/null in a image or so, then the device node may not exist. */
174 if (path_equal(skip_leading_slash(resolved_path), "dev/null")) {
175 if (masked) {
176 /* Mark this one as masked */
177 r = set_put_strdup(masked, name);
178 if (r < 0)
179 return log_oom_full(log_level);
180 }
181
182 return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
183 "File '%s%s' is a mask (symlink to /dev/null).",
184 root, skip_leading_slash(original_path));
185 }
186 }
187
188 if (conf_files_need_stat(flags))
189 /* If we need to have stat, skip the entry. */
190 return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOENT), "Failed to chase '%s%s': %m",
191 root, skip_leading_slash(original_path));
192 }
193
194 /* Even if we do not need stat, let's take stat now. The caller may use the info later. */
195 if (fd >= 0 && fstat(fd, &st) < 0)
196 return log_full_errno(log_level, errno, "Failed to stat '%s%s': %m",
197 root, skip_leading_slash(original_path));
198
199 /* Is this a masking entry? */
200 if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK) && stat_may_be_dev_null(&st)) {
201 if (masked) {
202 /* Mark this one as masked */
203 r = set_put_strdup(masked, name);
204 if (r < 0)
205 return log_oom_full(log_level);
206 }
207
208 return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
209 "File '%s%s' is a mask (symlink to /dev/null).",
210 root, skip_leading_slash(original_path));
211 }
212
213 if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_EMPTY) && stat_is_empty(&st)) {
214 if (masked) {
215 /* Mark this one as masked */
216 r = set_put_strdup(masked, name);
217 if (r < 0)
218 return log_oom_full(log_level);
219 }
220
221 return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
222 "File '%s%s' is a mask (an empty file).",
223 root, skip_leading_slash(original_path));
224 }
225
226 if (FLAGS_SET(flags, CONF_FILES_REGULAR|CONF_FILES_DIRECTORY)) {
227 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
228 return log_debug_errno(SYNTHETIC_ERRNO(EBADFD),
229 "File '%s%s' is neither a regular file or directory.",
230 root, skip_leading_slash(original_path));
231 } else {
232 /* Is this node a regular file? */
233 if (FLAGS_SET(flags, CONF_FILES_REGULAR)) {
234 r = stat_verify_regular(&st);
235 if (r < 0)
236 return log_debug_errno(r, "File '%s%s' is not a regular file: %m",
237 root, skip_leading_slash(original_path));
238 }
239
240 /* Is this node a directory? */
241 if (FLAGS_SET(flags, CONF_FILES_DIRECTORY)) {
242 r = stat_verify_directory(&st);
243 if (r < 0)
244 return log_debug_errno(r, "File '%s%s' is not a directory: %m",
245 root, skip_leading_slash(original_path));
246 }
247 }
248
249 /* Does this node have the executable bit set?
250 * As requested: check if the file is marked executable. Note that we don't check access(X_OK) here,
251 * as we care about whether the file is marked executable at all, and not whether it is executable
252 * for us, because if so, such errors are stuff we should log about. */
253 if (FLAGS_SET(flags, CONF_FILES_EXECUTABLE) && (st.st_mode & 0111) == 0)
254 return log_debug_errno(SYNTHETIC_ERRNO(ENOEXEC),
255 "File '%s%s' is not marked executable.",
256 root, skip_leading_slash(original_path));
257
258 if (ret_path)
259 *ret_path = TAKE_PTR(resolved_path);
260 if (ret_fd)
261 *ret_fd = TAKE_FD(fd);
262 if (ret_stat)
263 *ret_stat = st;
264
265 return 0;
266 }
267
268 int conf_file_new_at(
269 const char *path,
270 const char *root,
271 int rfd,
272 ConfFilesFlags flags,
273 ConfFile **ret) {
274 int r;
275
276 assert(path);
277 assert(wildcard_fd_is_valid(rfd));
278 assert(ret);
279
280 int log_level = conf_files_log_level(flags);
281
282 _cleanup_free_ char *_root = NULL;
283 if (DEBUG_LOGGING && !root) {
284 (void) fd_get_path(rfd, &_root);
285 root = _root;
286 }
287
288 _cleanup_(conf_file_freep) ConfFile *c = new(ConfFile, 1);
289 if (!c)
290 return log_oom_full(log_level);
291
292 *c = (ConfFile) {
293 .original_path = strdup(path),
294 .fd = -EBADF,
295 };
296
297 if (!c->original_path)
298 return log_oom_full(log_level);
299
300 r = path_extract_filename(path, &c->filename);
301 if (r < 0)
302 return log_full_errno(log_level, r, "Failed to extract filename from '%s': %m", path);
303
304 _cleanup_free_ char *dirpath = NULL, *resolved_dirpath = NULL;
305 r = path_extract_directory(path, &dirpath);
306 if (r < 0 && r != -EDESTADDRREQ)
307 return log_full_errno(log_level, r, "Failed to extract directory from '%s': %m", path);
308 if (r >= 0) {
309 r = chaseat(rfd, rfd, dirpath,
310 CHASE_MUST_BE_DIRECTORY | conf_files_chase_flags(flags),
311 &resolved_dirpath, /* ret_fd= */ NULL);
312 if (r < 0)
313 return log_full_errno(log_level, r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(dirpath));
314 }
315
316 c->result = path_join(resolved_dirpath, c->filename);
317 if (!c->result)
318 return log_oom_full(log_level);
319
320 r = conf_file_chase_and_verify(
321 root,
322 rfd,
323 c->original_path,
324 c->result,
325 c->filename,
326 /* masked= */ NULL,
327 flags,
328 &c->resolved_path,
329 &c->fd,
330 &c->st);
331 if (r < 0)
332 return r;
333
334 *ret = TAKE_PTR(c);
335 return 0;
336 }
337
338 int conf_file_new(const char *path, const char *root, ConfFilesFlags flags, ConfFile **ret) {
339 int r;
340
341 assert(path);
342 assert(ret);
343
344 _cleanup_free_ char *root_abs = NULL;
345 _cleanup_close_ int rfd = -EBADF;
346 r = prepare_dirs(root, flags, /* dirs= */ NULL, &root_abs, &rfd, /* ret_dirs= */ NULL);
347 if (r < 0)
348 return r;
349
350 _cleanup_free_ char *path_abs = NULL;
351 if (!root_abs) {
352 r = path_make_absolute_cwd(path, &path_abs);
353 if (r < 0)
354 return log_full_errno(conf_files_log_level(flags), r, "Failed to make '%s' absolute: %m", path);
355
356 path = path_abs;
357 }
358
359 _cleanup_(conf_file_freep) ConfFile *c = NULL;
360 r = conf_file_new_at(path, root_abs, rfd, flags, &c);
361 if (r < 0)
362 return r;
363
364 r = conf_file_prefix_root(c, root_abs, flags);
365 if (r < 0)
366 return r;
367
368 *ret = TAKE_PTR(c);
369 return 0;
370 }
371
372 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
373 conf_file_hash_ops,
374 char, string_hash_func, string_compare_func,
375 ConfFile, conf_file_free);
376
377 static int files_add(
378 DIR *dir,
379 const char *original_dirpath,
380 const char *resolved_dirpath,
381 const char *root, /* for logging, can be NULL */
382 int rfd,
383 Hashmap **files,
384 Set **masked,
385 const char *suffix,
386 ConfFilesFlags flags) {
387
388 int r;
389
390 assert(dir);
391 assert(original_dirpath);
392 assert(resolved_dirpath);
393 assert(wildcard_fd_is_valid(rfd));
394 assert(files);
395 assert(masked);
396
397 int log_level = conf_files_log_level(flags);
398
399 root = empty_to_root(root);
400
401 FOREACH_DIRENT(de, dir, return log_full_errno(log_level, errno, "Failed to read directory '%s%s': %m",
402 root, skip_leading_slash(original_dirpath))) {
403
404 _cleanup_free_ char *original_path = path_join(original_dirpath, de->d_name);
405 if (!original_path)
406 return log_oom_full(log_level);
407
408 /* Does this match the suffix? */
409 if (suffix && !endswith(de->d_name, suffix)) {
410 log_debug("Skipping file '%s%s', suffix is not '%s'.", root, skip_leading_slash(original_path), suffix);
411 continue;
412 }
413
414 /* Has this file already been found in an earlier directory? */
415 if (hashmap_contains(*files, de->d_name)) {
416 log_debug("Skipping overridden file '%s%s'.", root, skip_leading_slash(original_path));
417 continue;
418 }
419
420 /* Has this been masked in an earlier directory? */
421 if ((flags & CONF_FILES_FILTER_MASKED) != 0 && set_contains(*masked, de->d_name)) {
422 log_debug("File '%s%s' is masked by previous entry.", root, skip_leading_slash(original_path));
423 continue;
424 }
425
426 _cleanup_free_ char *p = path_join(resolved_dirpath, de->d_name);
427 if (!p)
428 return log_oom_full(log_level);
429
430 _cleanup_free_ char *resolved_path = NULL;
431 _cleanup_close_ int fd = -EBADF;
432 struct stat st;
433 r = conf_file_chase_and_verify(
434 root,
435 rfd,
436 original_path,
437 p,
438 de->d_name,
439 masked,
440 flags,
441 &resolved_path,
442 &fd,
443 &st);
444 if (r == -ENOMEM)
445 return r;
446 if (r < 0)
447 continue;
448
449 _cleanup_(conf_file_freep) ConfFile *c = new(ConfFile, 1);
450 if (!c)
451 return log_oom_full(log_level);
452
453 *c = (ConfFile) {
454 .filename = strdup(de->d_name),
455 .result = TAKE_PTR(p),
456 .original_path = TAKE_PTR(original_path),
457 .resolved_path = TAKE_PTR(resolved_path),
458 .fd = TAKE_FD(fd),
459 .st = st,
460 };
461
462 if (!c->filename)
463 return log_oom_full(log_level);
464
465 r = hashmap_ensure_put(files, &conf_file_hash_ops, c->filename, c);
466 if (r < 0) {
467 assert(r == -ENOMEM);
468 return log_oom_full(log_level);
469 }
470 assert(r > 0);
471
472 TAKE_PTR(c);
473 }
474
475 return 0;
476 }
477
478 static int dump_files(Hashmap *fh, const char *root, ConfFilesFlags flags, ConfFile ***ret_files, size_t *ret_n_files) {
479 ConfFile **files = NULL;
480 size_t n_files = 0;
481 int r;
482
483 CLEANUP_ARRAY(files, n_files, conf_file_free_array);
484
485 assert(ret_files);
486 assert(ret_n_files);
487
488 /* The entries in the array given by hashmap_dump_sorted() are still owned by the hashmap. */
489 r = hashmap_dump_sorted(fh, (void***) &files, &n_files);
490 if (r < 0)
491 return log_oom_full(conf_files_log_level(flags));
492
493 /* Hence, we need to remove them from the hashmap. */
494 FOREACH_ARRAY(i, files, n_files)
495 assert_se(hashmap_remove(fh, (*i)->filename) == *i);
496
497 if (root)
498 FOREACH_ARRAY(i, files, n_files) {
499 r = conf_file_prefix_root(*i, root, flags);
500 if (r < 0)
501 return r;
502 }
503
504 *ret_files = TAKE_PTR(files);
505 *ret_n_files = n_files;
506 return 0;
507 }
508
509 static int copy_and_sort_files_from_hashmap(
510 Hashmap *fh,
511 const char *suffix,
512 const char *root,
513 ConfFilesFlags flags,
514 char ***ret) {
515
516 _cleanup_strv_free_ char **results = NULL;
517 _cleanup_free_ ConfFile **files = NULL;
518 size_t n_files = 0, n_results = 0;
519 int r;
520
521 assert(ret);
522
523 int log_level = conf_files_log_level(flags);
524
525 /* The entries in the array given by hashmap_dump_sorted() are still owned by the hashmap.
526 * Hence, do not use conf_file_free_array() for 'entries' */
527 r = hashmap_dump_sorted(fh, (void***) &files, &n_files);
528 if (r < 0)
529 return log_oom_full(log_level);
530
531 FOREACH_ARRAY(i, files, n_files) {
532 ConfFile *c = *i;
533 const char *add = NULL;
534
535 if (FLAGS_SET(flags, CONF_FILES_BASENAME))
536 add = c->filename;
537 else if (root && !FLAGS_SET(flags, CONF_FILES_DONT_PREFIX_ROOT)) {
538 _cleanup_free_ char *p = NULL;
539
540 r = chaseat_prefix_root(c->result, root, &p);
541 if (r < 0)
542 return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
543
544 if (FLAGS_SET(flags, CONF_FILES_TRUNCATE_SUFFIX) && suffix) {
545 char *e = endswith(p, suffix);
546 if (!e)
547 continue;
548
549 *e = 0;
550 }
551
552 if (strv_consume_with_size(&results, &n_results, TAKE_PTR(p)) < 0)
553 return log_oom_full(log_level);
554
555 continue;
556 } else
557 add = c->result;
558
559 if (FLAGS_SET(flags, CONF_FILES_TRUNCATE_SUFFIX)) {
560 const char *e = endswith(add, suffix);
561 if (!e)
562 continue;
563
564 _cleanup_free_ char *n = strndup(add, e - add);
565 if (!n)
566 return log_oom_full(log_level);
567
568 r = strv_consume_with_size(&results, &n_results, TAKE_PTR(n));
569 } else
570 r = strv_extend_with_size(&results, &n_results, add);
571 if (r < 0)
572 return log_oom_full(log_level);
573 }
574
575 *ret = TAKE_PTR(results);
576 return 0;
577 }
578
579 static int insert_replacement(Hashmap **fh, ConfFile *replacement, ConfFilesFlags flags, const ConfFile **ret) {
580 _cleanup_(conf_file_freep) ConfFile *c = ASSERT_PTR(replacement);
581 int r;
582
583 assert(fh);
584 assert(ret);
585
586 /* This consumes the input ConfFile. */
587
588 ConfFile *existing = hashmap_get(*fh, c->filename);
589 if (existing) {
590 log_debug("An entry with higher priority '%s' -> '%s' already exists, ignoring the replacement: %s",
591 existing->filename, existing->result, c->original_path);
592 *ret = NULL;
593 return 0;
594 }
595
596 r = hashmap_ensure_put(fh, &conf_file_hash_ops, c->filename, c);
597 if (r < 0) {
598 assert(r == -ENOMEM);
599 return log_oom_full(conf_files_log_level(flags));
600 }
601 assert(r > 0);
602
603 log_debug("Inserted replacement: '%s' -> '%s'", c->filename, c->result);
604
605 *ret = TAKE_PTR(c);
606 return 0;
607 }
608
609 static int conf_files_list_impl(
610 const char *suffix,
611 const char *root, /* for logging, can be NULL */
612 int rfd,
613 ConfFilesFlags flags,
614 const char * const *dirs,
615 const char *replacement,
616 Hashmap **ret,
617 const ConfFile **ret_inserted) {
618
619 _cleanup_hashmap_free_ Hashmap *fh = NULL;
620 _cleanup_set_free_ Set *masked = NULL;
621 _cleanup_(conf_file_freep) ConfFile *c = NULL;
622 const ConfFile *inserted = NULL;
623 int r;
624
625 assert(wildcard_fd_is_valid(rfd));
626 assert(ret);
627
628 root = empty_to_root(root);
629
630 if (replacement) {
631 r = conf_file_new_at(replacement, root, rfd, flags & CONF_FILES_WARN, &c);
632 if (r < 0)
633 return r;
634 }
635
636 STRV_FOREACH(p, dirs) {
637 _cleanup_closedir_ DIR *dir = NULL;
638 _cleanup_free_ char *path = NULL;
639
640 r = chase_and_opendirat(rfd, rfd, *p, 0, &path, &dir);
641 if (r < 0) {
642 if (r != -ENOENT)
643 log_full_errno(conf_files_log_level(flags), r,
644 "Failed to chase and open directory '%s%s', ignoring: %m",
645 root, skip_leading_slash(*p));
646 continue;
647 }
648
649 if (c && streq_ptr(path_startswith(c->result, path), c->filename)) {
650 r = insert_replacement(&fh, TAKE_PTR(c), flags, &inserted);
651 if (r < 0)
652 return r;
653 }
654
655 r = files_add(dir, *p, path, root, rfd, &fh, &masked, suffix, flags);
656 if (r == -ENOMEM)
657 return r;
658 }
659
660 if (c) {
661 r = insert_replacement(&fh, TAKE_PTR(c), flags, &inserted);
662 if (r < 0)
663 return r;
664 }
665
666 *ret = TAKE_PTR(fh);
667 if (ret_inserted)
668 *ret_inserted = inserted;
669 return 0;
670 }
671
672 int conf_files_list_strv(
673 char ***ret,
674 const char *suffix,
675 const char *root,
676 ConfFilesFlags flags,
677 const char * const *dirs) {
678
679 _cleanup_hashmap_free_ Hashmap *fh = NULL;
680 _cleanup_close_ int rfd = -EBADF;
681 _cleanup_free_ char *root_abs = NULL;
682 _cleanup_strv_free_ char **dirs_abs = NULL;
683 int r;
684
685 assert(ret);
686
687 r = prepare_dirs(root, flags, (char**) dirs, &root_abs, &rfd, &dirs_abs);
688 if (r < 0)
689 return r;
690
691 r = conf_files_list_impl(suffix, root_abs, rfd, flags, (const char * const *) dirs_abs,
692 /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
693 if (r < 0)
694 return r;
695
696 return copy_and_sort_files_from_hashmap(fh, suffix, empty_to_root(root_abs), flags, ret);
697 }
698
699 int conf_files_list_strv_full(
700 const char *suffix,
701 const char *root,
702 ConfFilesFlags flags,
703 const char * const *dirs,
704 ConfFile ***ret_files,
705 size_t *ret_n_files) {
706
707 _cleanup_hashmap_free_ Hashmap *fh = NULL;
708 _cleanup_close_ int rfd = -EBADF;
709 _cleanup_free_ char *root_abs = NULL;
710 _cleanup_strv_free_ char **dirs_abs = NULL;
711 int r;
712
713 assert(ret_files);
714 assert(ret_n_files);
715
716 r = prepare_dirs(root, flags, (char**) dirs, &root_abs, &rfd, &dirs_abs);
717 if (r < 0)
718 return r;
719
720 r = conf_files_list_impl(suffix, root_abs, rfd, flags, (const char * const *) dirs_abs,
721 /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
722 if (r < 0)
723 return r;
724
725 return dump_files(fh, empty_to_root(root_abs), flags, ret_files, ret_n_files);
726 }
727
728 int conf_files_list_strv_at(
729 char ***ret,
730 const char *suffix,
731 int rfd,
732 ConfFilesFlags flags,
733 const char * const *dirs) {
734
735 _cleanup_hashmap_free_ Hashmap *fh = NULL;
736 _cleanup_free_ char *root = NULL;
737 int r;
738
739 assert(wildcard_fd_is_valid(rfd));
740 assert(ret);
741
742 if (DEBUG_LOGGING)
743 (void) fd_get_path(rfd, &root); /* for logging */
744
745 r = conf_files_list_impl(suffix, root, rfd, flags, dirs, /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
746 if (r < 0)
747 return r;
748
749 return copy_and_sort_files_from_hashmap(fh, suffix, /* root= */ NULL, flags, ret);
750 }
751
752 int conf_files_list_strv_at_full(
753 const char *suffix,
754 int rfd,
755 ConfFilesFlags flags,
756 const char * const *dirs,
757 ConfFile ***ret_files,
758 size_t *ret_n_files) {
759
760 _cleanup_hashmap_free_ Hashmap *fh = NULL;
761 _cleanup_free_ char *root = NULL;
762 int r;
763
764 assert(wildcard_fd_is_valid(rfd));
765 assert(ret_files);
766 assert(ret_n_files);
767
768 if (DEBUG_LOGGING)
769 (void) fd_get_path(rfd, &root); /* for logging */
770
771 r = conf_files_list_impl(suffix, root, rfd, flags, dirs, /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
772 if (r < 0)
773 return r;
774
775 return dump_files(fh, /* root= */ NULL, flags, ret_files, ret_n_files);
776 }
777
778 int conf_files_list(char ***ret, const char *suffix, const char *root, ConfFilesFlags flags, const char *dir) {
779 return conf_files_list_strv(ret, suffix, root, flags, STRV_MAKE_CONST(dir));
780 }
781
782 int conf_files_list_full(const char *suffix, const char *root, ConfFilesFlags flags, const char *dir, ConfFile ***ret_files, size_t *ret_n_files) {
783 return conf_files_list_strv_full(suffix, root, flags, STRV_MAKE_CONST(dir), ret_files, ret_n_files);
784 }
785
786 int conf_files_list_at(char ***ret, const char *suffix, int rfd, ConfFilesFlags flags, const char *dir) {
787 return conf_files_list_strv_at(ret, suffix, rfd, flags, STRV_MAKE_CONST(dir));
788 }
789
790 int conf_files_list_at_full(const char *suffix, int rfd, ConfFilesFlags flags, const char *dir, ConfFile ***ret_files, size_t *ret_n_files) {
791 return conf_files_list_strv_at_full(suffix, rfd, flags, STRV_MAKE_CONST(dir), ret_files, ret_n_files);
792 }
793
794 int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, ConfFilesFlags flags, const char *dirs) {
795 _cleanup_strv_free_ char **d = NULL;
796
797 assert(ret);
798
799 d = strv_split_nulstr(dirs);
800 if (!d)
801 return log_oom_full(conf_files_log_level(flags));
802
803 return conf_files_list_strv(ret, suffix, root, flags, (const char**) d);
804 }
805
806 int conf_files_list_nulstr_full(const char *suffix, const char *root, ConfFilesFlags flags, const char *dirs, ConfFile ***ret_files, size_t *ret_n_files) {
807 _cleanup_strv_free_ char **d = NULL;
808
809 assert(ret_files);
810 assert(ret_n_files);
811
812 d = strv_split_nulstr(dirs);
813 if (!d)
814 return log_oom_full(conf_files_log_level(flags));
815
816 return conf_files_list_strv_full(suffix, root, flags, (const char**) d, ret_files, ret_n_files);
817 }
818
819 int conf_files_list_nulstr_at(char ***ret, const char *suffix, int rfd, ConfFilesFlags flags, const char *dirs) {
820 _cleanup_strv_free_ char **d = NULL;
821
822 assert(ret);
823
824 d = strv_split_nulstr(dirs);
825 if (!d)
826 return log_oom_full(conf_files_log_level(flags));
827
828 return conf_files_list_strv_at(ret, suffix, rfd, flags, (const char**) d);
829 }
830
831 int conf_files_list_nulstr_at_full(const char *suffix, int rfd, ConfFilesFlags flags, const char *dirs, ConfFile ***ret_files, size_t *ret_n_files) {
832 _cleanup_strv_free_ char **d = NULL;
833
834 assert(ret_files);
835 assert(ret_n_files);
836
837 d = strv_split_nulstr(dirs);
838 if (!d)
839 return log_oom_full(conf_files_log_level(flags));
840
841 return conf_files_list_strv_at_full(suffix, rfd, flags, (const char**) d, ret_files, ret_n_files);
842 }
843
844 int conf_files_list_with_replacement(
845 const char *root,
846 char **config_dirs,
847 const char *replacement,
848 char ***ret_files,
849 char **ret_inserted) {
850
851 _cleanup_hashmap_free_ Hashmap *fh = NULL;
852 _cleanup_free_ char *inserted = NULL;
853 ConfFilesFlags flags = CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED_BY_SYMLINK | CONF_FILES_WARN;
854 _cleanup_close_ int rfd = -EBADF;
855 _cleanup_free_ char *root_abs = NULL;
856 _cleanup_strv_free_ char **dirs_abs = NULL;
857 const ConfFile *c = NULL;
858 int r;
859
860 assert(ret_files);
861
862 r = prepare_dirs(root, flags, config_dirs, &root_abs, &rfd, &dirs_abs);
863 if (r < 0)
864 return r;
865
866 r = conf_files_list_impl(".conf", root_abs, rfd, flags, (const char * const *) dirs_abs,
867 replacement, &fh, ret_inserted ? &c : NULL);
868 if (r < 0)
869 return r;
870
871 if (c) {
872 r = chaseat_prefix_root(c->result, root_abs, &inserted);
873 if (r < 0)
874 return log_full_errno(conf_files_log_level(flags), r,
875 "Failed to prefix '%s' with root '%s': %m",
876 c->result, empty_to_root(root_abs));
877 }
878
879 r = copy_and_sort_files_from_hashmap(fh, ".conf", empty_to_root(root_abs), flags, ret_files);
880 if (r < 0)
881 return r;
882
883 if (ret_inserted)
884 *ret_inserted = TAKE_PTR(inserted);
885 return 0;
886 }
887
888 int conf_files_list_dropins(
889 char ***ret,
890 const char *dropin_dirname,
891 const char *root,
892 int root_fd,
893 ConfFilesFlags flags,
894 const char * const *dirs) {
895
896 _cleanup_strv_free_ char **dropin_dirs = NULL;
897 const char *suffix;
898 int r;
899
900 assert(ret);
901 assert(dropin_dirname);
902 assert(dirs);
903
904 suffix = strjoina("/", dropin_dirname);
905 r = strv_extend_strv_concat(&dropin_dirs, dirs, suffix);
906 if (r < 0)
907 return log_oom_full(conf_files_log_level(flags));
908
909 return conf_files_list_strv_at(ret, ".conf", root_fd, flags, (const char* const*) dropin_dirs);
910 }
911
912 /**
913 * Open and read a config file.
914 *
915 * The <fn> argument may be:
916 * - '-', meaning stdin.
917 * - a file name without a path. In this case <config_dirs> are searched.
918 * - a path, either relative or absolute. In this case <fn> is opened directly.
919 *
920 * This method is only suitable for configuration files which have a flat layout without dropins.
921 */
922 int conf_file_read(
923 const char *root,
924 const char **config_dirs,
925 const char *fn,
926 parse_line_t parse_line,
927 void *userdata,
928 bool ignore_enoent,
929 bool *invalid_config) {
930
931 _cleanup_fclose_ FILE *_f = NULL;
932 _cleanup_free_ char *_fn = NULL;
933 unsigned v = 0;
934 FILE *f;
935 int r = 0;
936
937 assert(fn);
938
939 if (streq(fn, "-")) {
940 f = stdin;
941 fn = "<stdin>";
942
943 log_debug("Reading config from stdin%s", glyph(GLYPH_ELLIPSIS));
944
945 } else if (is_path(fn)) {
946 r = path_make_absolute_cwd(fn, &_fn);
947 if (r < 0)
948 return log_error_errno(r, "Failed to make path absolute: %m");
949 fn = _fn;
950
951 f = _f = fopen(fn, "re");
952 if (!_f)
953 r = -errno;
954 else
955 log_debug("Reading config file \"%s\"%s", fn, glyph(GLYPH_ELLIPSIS));
956
957 } else {
958 r = search_and_fopen(fn, "re", root, config_dirs, &_f, &_fn);
959 if (r >= 0) {
960 f = _f;
961 fn = _fn;
962 log_debug("Reading config file \"%s\"%s", fn, glyph(GLYPH_ELLIPSIS));
963 }
964 }
965
966 if (r == -ENOENT && ignore_enoent) {
967 log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn);
968 return 0; /* No error, but nothing happened. */
969 }
970 if (r < 0)
971 return log_error_errno(r, "Failed to read '%s': %m", fn);
972
973 r = 1; /* We entered the part where we may modify state. */
974
975 for (;;) {
976 _cleanup_free_ char *line = NULL;
977 bool invalid_line = false;
978 int k;
979
980 k = read_stripped_line(f, LONG_LINE_MAX, &line);
981 if (k < 0)
982 return log_error_errno(k, "Failed to read '%s': %m", fn);
983 if (k == 0)
984 break;
985
986 v++;
987
988 if (IN_SET(line[0], 0, '#'))
989 continue;
990
991 k = parse_line(fn, v, line, invalid_config ? &invalid_line : NULL, userdata);
992 if (k < 0 && invalid_line)
993 /* Allow reporting with a special code if the caller requested this. */
994 *invalid_config = true;
995 else
996 /* The first error, if any, becomes our return value. */
997 RET_GATHER(r, k);
998 }
999
1000 if (ferror(f))
1001 RET_GATHER(r, log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read from file %s.", fn));
1002
1003 return r;
1004 }