1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include "conf-files.h"
17 #include "dirent-util.h"
23 #include "path-util.h"
24 #include "stat-util.h"
25 #include "string-util.h"
27 //#include "terminal-util.h"
30 static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
31 _cleanup_closedir_ DIR *dir = NULL;
38 dirpath = prefix_roota(root, path);
40 dir = opendir(dirpath);
47 FOREACH_DIRENT(de, dir, return -errno) {
50 if (!dirent_is_file_with_suffix(de, suffix)) {
51 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath, de->d_name, strna(suffix));
55 if (flags & CONF_FILES_EXECUTABLE) {
58 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
59 * here, as we care about whether the file is marked executable at all, and not whether it is
60 * executable for us, because if such errors are stuff we should log about. */
62 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
63 log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", dirpath, de->d_name);
67 if (!null_or_empty(&st)) {
68 /* A mask is a symlink to /dev/null or an empty file. It does not even
69 * have to be executable. Other entries must be regular executable files
70 * or symlinks to them. */
71 if (S_ISREG(st.st_mode)) {
72 if ((st.st_mode & 0111) == 0) { /* not executable */
73 log_debug("Ignoring %s/%s, as it is not marked executable.",
78 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
85 p = strjoin(dirpath, "/", de->d_name);
89 r = hashmap_put(h, basename(p), p);
91 log_debug("Skipping overridden file: %s.", p);
97 log_debug("Duplicate file %s", p);
105 static int base_cmp(const void *a, const void *b) {
108 s1 = *(char * const *)a;
109 s2 = *(char * const *)b;
110 return strcmp(basename(s1), basename(s2));
113 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
114 _cleanup_hashmap_free_ Hashmap *fh = NULL;
120 /* This alters the dirs string array */
121 if (!path_strv_resolve_uniq(dirs, root))
124 fh = hashmap_new(&string_hash_ops);
128 STRV_FOREACH(p, dirs) {
129 r = files_add(fh, suffix, root, flags, *p);
133 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
136 files = hashmap_get_strv(fh);
140 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
146 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
147 /* Insert a path into strv, at the place honouring the usual sorting rules:
148 * - we first compare by the basename
149 * - and then we compare by dirname, allowing just one file with the given
151 * This means that we will
152 * - add a new entry if basename(path) was not on the list,
153 * - do nothing if an entry with higher priority was already present,
154 * - do nothing if our new entry matches the existing entry,
155 * - replace the existing entry if our new entry has higher priority.
161 for (i = 0; i < strv_length(*strv); i++) {
164 c = base_cmp(*strv + i, &path);
168 /* Oh, we found our spot and it already contains something. */
169 STRV_FOREACH(dir, dirs) {
172 p1 = path_startswith((*strv)[i], root);
174 /* Skip "/" in *dir, because p1 is without "/" too */
175 p1 = path_startswith(p1, *dir + 1);
177 /* Existing entry with higher priority
178 * or same priority, no need to do anything. */
181 p2 = path_startswith(path, *dir);
183 /* Our new entry has higher priority */
184 t = path_join(root, path, NULL);
188 return free_and_replace((*strv)[i], t);
193 /* Following files have lower priority, let's go insert our
197 /* … we are not there yet, let's continue */
200 t = path_join(root, path, NULL);
204 r = strv_insert(strv, i, t);
210 int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
211 _cleanup_strv_free_ char **d = NULL;
215 d = strv_split_nulstr(dirs);
219 return conf_files_insert(strv, root, d, path);
222 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
223 _cleanup_strv_free_ char **copy = NULL;
227 copy = strv_copy((char**) dirs);
231 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
234 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
235 _cleanup_strv_free_ char **dirs = NULL;
241 dirs = strv_new_ap(dir, ap);
247 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
250 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
251 _cleanup_strv_free_ char **d = NULL;
255 d = strv_split_nulstr(dirs);
259 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
262 int conf_files_list_with_replacement(
265 const char *replacement,
267 char **replace_file) {
269 _cleanup_strv_free_ char **f = NULL;
270 _cleanup_free_ char *p = NULL;
275 assert(replace_file || !replacement);
277 r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
279 return log_error_errno(r, "Failed to enumerate config files: %m");
282 r = conf_files_insert(&f, root, config_dirs, replacement);
284 return log_error_errno(r, "Failed to extend config file list: %m");
286 p = path_join(root, replacement, NULL);
291 *files = TAKE_PTR(f);
293 *replace_file = TAKE_PTR(p);
297 int conf_files_cat(const char *name) {
298 _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
303 NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
304 assert(endswith(dir, "/"));
305 r = strv_extendf(&dirs, "%s%s.d", dir, name);
307 return log_error("Failed to build directory list: %m");
310 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dirs);
312 return log_error_errno(r, "Failed to query file list: %m");
314 name = strjoina("/etc/", name);
317 log_debug("Looking for configuration in:");
318 log_debug(" %s", name);
319 STRV_FOREACH(t, dirs)
320 log_debug(" %s/*.conf", *t);
324 return cat_files(name, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);