1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
13 #include "conf-files.h"
15 #include "dirent-util.h"
21 #include "path-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
26 #include "terminal-util.h"
37 _cleanup_closedir_
DIR *dir
= NULL
;
43 assert((flags
& CONF_FILES_FILTER_MASKED
) == 0 || masked
);
46 dirpath
= prefix_roota(root
, path
);
48 dir
= opendir(dirpath
);
53 return log_debug_errno(errno
, "Failed to open directory '%s': %m", dirpath
);
56 FOREACH_DIRENT(de
, dir
, return -errno
) {
60 /* Does this match the suffix? */
61 if (suffix
&& !endswith(de
->d_name
, suffix
))
64 /* Has this file already been found in an earlier directory? */
65 if (hashmap_contains(h
, de
->d_name
)) {
66 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
70 /* Has this been masked in an earlier directory? */
71 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(masked
, de
->d_name
)) {
72 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
76 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
77 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
78 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
79 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
83 /* Is this a masking entry? */
84 if ((flags
& CONF_FILES_FILTER_MASKED
))
85 if (null_or_empty(&st
)) {
86 /* Mark this one as masked */
87 r
= set_put_strdup(masked
, de
->d_name
);
91 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
95 /* Does this node have the right type? */
96 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
97 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
98 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
99 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath
, de
->d_name
);
103 /* Does this node have the executable bit set? */
104 if (flags
& CONF_FILES_EXECUTABLE
)
105 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
106 * here, as we care about whether the file is marked executable at all, and not whether it is
107 * executable for us, because if so, such errors are stuff we should log about. */
109 if ((st
.st_mode
& 0111) == 0) { /* not executable */
110 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
114 if (flags
& CONF_FILES_BASENAME
) {
115 p
= strdup(de
->d_name
);
121 p
= strjoin(dirpath
, "/", de
->d_name
);
128 r
= hashmap_put(h
, key
, p
);
131 return log_debug_errno(r
, "Failed to add item to hashmap: %m");
140 static int base_cmp(const void *a
, const void *b
) {
143 s1
= *(char * const *)a
;
144 s2
= *(char * const *)b
;
145 return strcmp(basename(s1
), basename(s2
));
148 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
149 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
150 _cleanup_set_free_free_ Set
*masked
= NULL
;
156 /* This alters the dirs string array */
157 if (!path_strv_resolve_uniq(dirs
, root
))
160 fh
= hashmap_new(&path_hash_ops
);
164 if (flags
& CONF_FILES_FILTER_MASKED
) {
165 masked
= set_new(&path_hash_ops
);
170 STRV_FOREACH(p
, dirs
) {
171 r
= files_add(fh
, masked
, suffix
, root
, flags
, *p
);
175 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
178 files
= hashmap_get_strv(fh
);
182 qsort_safe(files
, hashmap_size(fh
), sizeof(char *), base_cmp
);
188 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
189 /* Insert a path into strv, at the place honouring the usual sorting rules:
190 * - we first compare by the basename
191 * - and then we compare by dirname, allowing just one file with the given
193 * This means that we will
194 * - add a new entry if basename(path) was not on the list,
195 * - do nothing if an entry with higher priority was already present,
196 * - do nothing if our new entry matches the existing entry,
197 * - replace the existing entry if our new entry has higher priority.
203 for (i
= 0; i
< strv_length(*strv
); i
++) {
206 c
= base_cmp(*strv
+ i
, &path
);
210 /* Oh, we found our spot and it already contains something. */
211 STRV_FOREACH(dir
, dirs
) {
214 p1
= path_startswith((*strv
)[i
], root
);
216 /* Skip "/" in *dir, because p1 is without "/" too */
217 p1
= path_startswith(p1
, *dir
+ 1);
219 /* Existing entry with higher priority
220 * or same priority, no need to do anything. */
223 p2
= path_startswith(path
, *dir
);
225 /* Our new entry has higher priority */
226 t
= path_join(root
, path
, NULL
);
230 return free_and_replace((*strv
)[i
], t
);
235 /* Following files have lower priority, let's go insert our
239 /* … we are not there yet, let's continue */
242 t
= path_join(root
, path
, NULL
);
246 r
= strv_insert(strv
, i
, t
);
252 int conf_files_insert_nulstr(char ***strv
, const char *root
, const char *dirs
, const char *path
) {
253 _cleanup_strv_free_
char **d
= NULL
;
257 d
= strv_split_nulstr(dirs
);
261 return conf_files_insert(strv
, root
, d
, path
);
264 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
265 _cleanup_strv_free_
char **copy
= NULL
;
269 copy
= strv_copy((char**) dirs
);
273 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
276 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
, ...) {
277 _cleanup_strv_free_
char **dirs
= NULL
;
283 dirs
= strv_new_ap(dir
, ap
);
289 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
292 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
293 _cleanup_strv_free_
char **d
= NULL
;
297 d
= strv_split_nulstr(dirs
);
301 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, d
);
304 int conf_files_list_with_replacement(
307 const char *replacement
,
309 char **replace_file
) {
311 _cleanup_strv_free_
char **f
= NULL
;
312 _cleanup_free_
char *p
= NULL
;
317 assert(replace_file
|| !replacement
);
319 r
= conf_files_list_strv(&f
, ".conf", root
, 0, (const char* const*) config_dirs
);
321 return log_error_errno(r
, "Failed to enumerate config files: %m");
324 r
= conf_files_insert(&f
, root
, config_dirs
, replacement
);
326 return log_error_errno(r
, "Failed to extend config file list: %m");
328 p
= path_join(root
, replacement
, NULL
);
333 *files
= TAKE_PTR(f
);
335 *replace_file
= TAKE_PTR(p
);
339 int conf_files_cat(const char *root
, const char *name
) {
340 _cleanup_strv_free_
char **dirs
= NULL
, **files
= NULL
;
341 _cleanup_free_
char *path
= NULL
;
346 NULSTR_FOREACH(dir
, CONF_PATHS_NULSTR("")) {
347 assert(endswith(dir
, "/"));
348 r
= strv_extendf(&dirs
, "%s%s.d", dir
, name
);
350 return log_error_errno(r
, "Failed to build directory list: %m");
353 r
= conf_files_list_strv(&files
, ".conf", root
, 0, (const char* const*) dirs
);
355 return log_error_errno(r
, "Failed to query file list: %m");
357 path
= path_join(root
, "/etc", name
);
362 log_debug("Looking for configuration in:");
363 log_debug(" %s", path
);
364 STRV_FOREACH(t
, dirs
)
365 log_debug(" %s/*.conf", *t
);
369 return cat_files(path
, files
, CAT_FLAGS_MAIN_FILE_OPTIONAL
);