1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "conf-files.h"
12 #include "dirent-util.h"
18 #include "path-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
23 #include "terminal-util.h"
34 _cleanup_closedir_
DIR *dir
= NULL
;
40 assert((flags
& CONF_FILES_FILTER_MASKED
) == 0 || masked
);
43 dirpath
= prefix_roota(root
, path
);
45 dir
= opendir(dirpath
);
50 return log_debug_errno(errno
, "Failed to open directory '%s': %m", dirpath
);
53 FOREACH_DIRENT(de
, dir
, return -errno
) {
57 /* Does this match the suffix? */
58 if (suffix
&& !endswith(de
->d_name
, suffix
))
61 /* Has this file already been found in an earlier directory? */
62 if (hashmap_contains(h
, de
->d_name
)) {
63 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
67 /* Has this been masked in an earlier directory? */
68 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(masked
, de
->d_name
)) {
69 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
73 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
74 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
75 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
76 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
80 /* Is this a masking entry? */
81 if ((flags
& CONF_FILES_FILTER_MASKED
))
82 if (null_or_empty(&st
)) {
83 /* Mark this one as masked */
84 r
= set_put_strdup(masked
, de
->d_name
);
88 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
92 /* Does this node have the right type? */
93 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
94 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
95 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
96 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath
, de
->d_name
);
100 /* Does this node have the executable bit set? */
101 if (flags
& CONF_FILES_EXECUTABLE
)
102 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
103 * here, as we care about whether the file is marked executable at all, and not whether it is
104 * executable for us, because if so, such errors are stuff we should log about. */
106 if ((st
.st_mode
& 0111) == 0) { /* not executable */
107 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
111 if (flags
& CONF_FILES_BASENAME
) {
112 p
= strdup(de
->d_name
);
118 p
= strjoin(dirpath
, "/", de
->d_name
);
125 r
= hashmap_put(h
, key
, p
);
128 return log_debug_errno(r
, "Failed to add item to hashmap: %m");
137 static int base_cmp(const void *a
, const void *b
) {
140 s1
= *(char * const *)a
;
141 s2
= *(char * const *)b
;
142 return strcmp(basename(s1
), basename(s2
));
145 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
146 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
147 _cleanup_set_free_free_ Set
*masked
= NULL
;
153 /* This alters the dirs string array */
154 if (!path_strv_resolve_uniq(dirs
, root
))
157 fh
= hashmap_new(&path_hash_ops
);
161 if (flags
& CONF_FILES_FILTER_MASKED
) {
162 masked
= set_new(&path_hash_ops
);
167 STRV_FOREACH(p
, dirs
) {
168 r
= files_add(fh
, masked
, suffix
, root
, flags
, *p
);
172 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
175 files
= hashmap_get_strv(fh
);
179 qsort_safe(files
, hashmap_size(fh
), sizeof(char *), base_cmp
);
185 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
186 /* Insert a path into strv, at the place honouring the usual sorting rules:
187 * - we first compare by the basename
188 * - and then we compare by dirname, allowing just one file with the given
190 * This means that we will
191 * - add a new entry if basename(path) was not on the list,
192 * - do nothing if an entry with higher priority was already present,
193 * - do nothing if our new entry matches the existing entry,
194 * - replace the existing entry if our new entry has higher priority.
200 for (i
= 0; i
< strv_length(*strv
); i
++) {
203 c
= base_cmp(*strv
+ i
, &path
);
207 /* Oh, we found our spot and it already contains something. */
208 STRV_FOREACH(dir
, dirs
) {
211 p1
= path_startswith((*strv
)[i
], root
);
213 /* Skip "/" in *dir, because p1 is without "/" too */
214 p1
= path_startswith(p1
, *dir
+ 1);
216 /* Existing entry with higher priority
217 * or same priority, no need to do anything. */
220 p2
= path_startswith(path
, *dir
);
222 /* Our new entry has higher priority */
223 t
= path_join(root
, path
, NULL
);
227 return free_and_replace((*strv
)[i
], t
);
232 /* Following files have lower priority, let's go insert our
236 /* … we are not there yet, let's continue */
239 t
= path_join(root
, path
, NULL
);
243 r
= strv_insert(strv
, i
, t
);
249 int conf_files_insert_nulstr(char ***strv
, const char *root
, const char *dirs
, const char *path
) {
250 _cleanup_strv_free_
char **d
= NULL
;
254 d
= strv_split_nulstr(dirs
);
258 return conf_files_insert(strv
, root
, d
, path
);
261 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
262 _cleanup_strv_free_
char **copy
= NULL
;
266 copy
= strv_copy((char**) dirs
);
270 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
273 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
, ...) {
274 _cleanup_strv_free_
char **dirs
= NULL
;
280 dirs
= strv_new_ap(dir
, ap
);
286 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
289 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
290 _cleanup_strv_free_
char **d
= NULL
;
294 d
= strv_split_nulstr(dirs
);
298 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, d
);
301 int conf_files_list_with_replacement(
304 const char *replacement
,
306 char **replace_file
) {
308 _cleanup_strv_free_
char **f
= NULL
;
309 _cleanup_free_
char *p
= NULL
;
314 assert(replace_file
|| !replacement
);
316 r
= conf_files_list_strv(&f
, ".conf", root
, 0, (const char* const*) config_dirs
);
318 return log_error_errno(r
, "Failed to enumerate config files: %m");
321 r
= conf_files_insert(&f
, root
, config_dirs
, replacement
);
323 return log_error_errno(r
, "Failed to extend config file list: %m");
325 p
= path_join(root
, replacement
, NULL
);
330 *files
= TAKE_PTR(f
);
332 *replace_file
= TAKE_PTR(p
);
336 int conf_files_cat(const char *root
, const char *name
) {
337 _cleanup_strv_free_
char **dirs
= NULL
, **files
= NULL
;
338 _cleanup_free_
char *path
= NULL
;
343 NULSTR_FOREACH(dir
, CONF_PATHS_NULSTR("")) {
344 assert(endswith(dir
, "/"));
345 r
= strv_extendf(&dirs
, "%s%s.d", dir
, name
);
347 return log_error_errno(r
, "Failed to build directory list: %m");
350 r
= conf_files_list_strv(&files
, ".conf", root
, 0, (const char* const*) dirs
);
352 return log_error_errno(r
, "Failed to query file list: %m");
354 path
= path_join(root
, "/etc", name
);
359 log_debug("Looking for configuration in:");
360 log_debug(" %s", path
);
361 STRV_FOREACH(t
, dirs
)
362 log_debug(" %s/*.conf", *t
);
366 return cat_files(path
, files
, CAT_FLAGS_MAIN_FILE_OPTIONAL
);