2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "conf-files.h"
28 #include "dirent-util.h"
34 #include "path-util.h"
35 #include "stat-util.h"
36 #include "string-util.h"
40 static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
41 _cleanup_closedir_ DIR *dir = NULL;
48 dirpath = prefix_roota(root, path);
50 dir = opendir(dirpath);
57 FOREACH_DIRENT(de, dir, return -errno) {
60 if (!dirent_is_file_with_suffix(de, suffix)) {
61 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath, de->d_name, strna(suffix));
65 if (flags & CONF_FILES_EXECUTABLE) {
68 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
69 * here, as we care about whether the file is marked executable at all, and not whether it is
70 * executable for us, because if such errors are stuff we should log about. */
72 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
73 log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", dirpath, de->d_name);
77 /* We only want executable regular files (or symlinks to them), or symlinks to /dev/null */
78 if (S_ISREG(st.st_mode)) {
79 if ((st.st_mode & 0111) == 0) { /* not executable */
80 log_debug("Ignoring %s/%s, as it is not marked executable.", dirpath, de->d_name);
84 } else if (!null_or_empty(&st)) { /* /dev/null? */
85 log_debug("Ignoring %s/%s, as it is not a regular file (or symlink to /dev/null).", dirpath, de->d_name);
90 p = strjoin(dirpath, "/", de->d_name);
94 r = hashmap_put(h, basename(p), p);
96 log_debug("Skipping overridden file: %s.", p);
102 log_debug("Duplicate file %s", p);
110 static int base_cmp(const void *a, const void *b) {
113 s1 = *(char * const *)a;
114 s2 = *(char * const *)b;
115 return strcmp(basename(s1), basename(s2));
118 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
119 _cleanup_hashmap_free_ Hashmap *fh = NULL;
125 /* This alters the dirs string array */
126 if (!path_strv_resolve_uniq(dirs, root))
129 fh = hashmap_new(&string_hash_ops);
133 STRV_FOREACH(p, dirs) {
134 r = files_add(fh, suffix, root, flags, *p);
138 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
141 files = hashmap_get_strv(fh);
145 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
151 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
152 _cleanup_strv_free_ char **copy = NULL;
156 copy = strv_copy((char**) dirs);
160 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
163 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
164 _cleanup_strv_free_ char **dirs = NULL;
170 dirs = strv_new_ap(dir, ap);
176 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
179 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *d) {
180 _cleanup_strv_free_ char **dirs = NULL;
184 dirs = strv_split_nulstr(d);
188 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);