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 if (!null_or_empty(&st
)) {
78 /* A mask is a symlink to /dev/null or an empty file. It does not even
79 * have to be executable. Other entries must be regular executable files
80 * or symlinks to them. */
81 if (S_ISREG(st
.st_mode
)) {
82 if ((st
.st_mode
& 0111) == 0) { /* not executable */
83 log_debug("Ignoring %s/%s, as it is not marked executable.",
88 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
95 p
= strjoin(dirpath
, "/", de
->d_name
);
99 r
= hashmap_put(h
, basename(p
), p
);
101 log_debug("Skipping overridden file: %s.", p
);
107 log_debug("Duplicate file %s", p
);
115 static int base_cmp(const void *a
, const void *b
) {
118 s1
= *(char * const *)a
;
119 s2
= *(char * const *)b
;
120 return strcmp(basename(s1
), basename(s2
));
123 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
124 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
130 /* This alters the dirs string array */
131 if (!path_strv_resolve_uniq(dirs
, root
))
134 fh
= hashmap_new(&string_hash_ops
);
138 STRV_FOREACH(p
, dirs
) {
139 r
= files_add(fh
, suffix
, root
, flags
, *p
);
143 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
146 files
= hashmap_get_strv(fh
);
150 qsort_safe(files
, hashmap_size(fh
), sizeof(char *), base_cmp
);
156 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
157 _cleanup_strv_free_
char **copy
= NULL
;
161 copy
= strv_copy((char**) dirs
);
165 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
168 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
, ...) {
169 _cleanup_strv_free_
char **dirs
= NULL
;
175 dirs
= strv_new_ap(dir
, ap
);
181 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
184 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *d
) {
185 _cleanup_strv_free_
char **dirs
= NULL
;
189 dirs
= strv_split_nulstr(d
);
193 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);