]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/conf-files.c
tree-wide: be more careful with the type of array sizes
[thirdparty/systemd.git] / src / basic / conf-files.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2c21044f
KS
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
2c21044f
KS
6***/
7
07630cea 8#include <dirent.h>
2c21044f 9#include <errno.h>
11c3a366 10#include <stdarg.h>
2c21044f 11#include <stdio.h>
07630cea
LP
12#include <stdlib.h>
13#include <string.h>
2c21044f 14
3ffd4af2 15#include "conf-files.h"
a0956174 16#include "dirent-util.h"
3ffd4af2 17#include "fd-util.h"
07630cea
LP
18#include "hashmap.h"
19#include "log.h"
2c21044f 20#include "macro.h"
2c21044f 21#include "missing.h"
9eb977db 22#include "path-util.h"
b5084605 23#include "stat-util.h"
07630cea
LP
24#include "string-util.h"
25#include "strv.h"
26#include "util.h"
2c21044f 27
b5084605 28static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
fabe5c0e 29 _cleanup_closedir_ DIR *dir = NULL;
1d13f648 30 const char *dirpath;
31d5192d 31 struct dirent *de;
1d13f648 32 int r;
e02caf30 33
cba2ef02 34 assert(path);
cebed500 35
1d13f648 36 dirpath = prefix_roota(root, path);
cebed500 37
cba2ef02 38 dir = opendir(dirpath);
2c21044f
KS
39 if (!dir) {
40 if (errno == ENOENT)
41 return 0;
42 return -errno;
43 }
44
31d5192d 45 FOREACH_DIRENT(de, dir, return -errno) {
2c21044f
KS
46 char *p;
47
e34aa8ed
LP
48 if (!dirent_is_file_with_suffix(de, suffix)) {
49 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath, de->d_name, strna(suffix));
2c21044f 50 continue;
e34aa8ed 51 }
2c21044f 52
b5084605
LP
53 if (flags & CONF_FILES_EXECUTABLE) {
54 struct stat st;
55
56 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
57 * here, as we care about whether the file is marked executable at all, and not whether it is
58 * executable for us, because if such errors are stuff we should log about. */
59
60 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
61 log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", dirpath, de->d_name);
62 continue;
63 }
64
6a464351
ZJS
65 if (!null_or_empty(&st)) {
66 /* A mask is a symlink to /dev/null or an empty file. It does not even
67 * have to be executable. Other entries must be regular executable files
68 * or symlinks to them. */
69 if (S_ISREG(st.st_mode)) {
70 if ((st.st_mode & 0111) == 0) { /* not executable */
71 log_debug("Ignoring %s/%s, as it is not marked executable.",
72 dirpath, de->d_name);
73 continue;
74 }
75 } else {
76 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
77 dirpath, de->d_name);
b5084605
LP
78 continue;
79 }
b5084605
LP
80 }
81 }
82
605405c6 83 p = strjoin(dirpath, "/", de->d_name);
fabe5c0e 84 if (!p)
e02caf30 85 return -ENOMEM;
2c21044f 86
2b6bf07d 87 r = hashmap_put(h, basename(p), p);
fabe5c0e
LP
88 if (r == -EEXIST) {
89 log_debug("Skipping overridden file: %s.", p);
90 free(p);
91 } else if (r < 0) {
92 free(p);
93 return r;
94 } else if (r == 0) {
95 log_debug("Duplicate file %s", p);
2c21044f
KS
96 free(p);
97 }
98 }
99
e02caf30 100 return 0;
2c21044f
KS
101}
102
103static int base_cmp(const void *a, const void *b) {
104 const char *s1, *s2;
105
106 s1 = *(char * const *)a;
107 s2 = *(char * const *)b;
2b6bf07d 108 return strcmp(basename(s1), basename(s2));
2c21044f
KS
109}
110
b5084605 111static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
e1d75803 112 _cleanup_hashmap_free_ Hashmap *fh = NULL;
fabe5c0e 113 char **files, **p;
578ac060 114 int r;
2c21044f 115
fabe5c0e 116 assert(strv);
fabe5c0e
LP
117
118 /* This alters the dirs string array */
7d8da2c9 119 if (!path_strv_resolve_uniq(dirs, root))
fabe5c0e 120 return -ENOMEM;
2c21044f 121
d5099efc 122 fh = hashmap_new(&string_hash_ops);
fabe5c0e
LP
123 if (!fh)
124 return -ENOMEM;
2c21044f
KS
125
126 STRV_FOREACH(p, dirs) {
b5084605 127 r = files_add(fh, suffix, root, flags, *p);
31d5192d 128 if (r == -ENOMEM)
fabe5c0e 129 return r;
31d5192d
LP
130 if (r < 0)
131 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
2c21044f
KS
132 }
133
134 files = hashmap_get_strv(fh);
31d5192d 135 if (!files)
fabe5c0e 136 return -ENOMEM;
fabe5c0e 137
7ff7394d 138 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
fabe5c0e 139 *strv = files;
2c21044f 140
fabe5c0e
LP
141 return 0;
142}
143
a6d8474f 144int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
d16a1c1b
ZJS
145 /* Insert a path into strv, at the place honouring the usual sorting rules:
146 * - we first compare by the basename
147 * - and then we compare by dirname, allowing just one file with the given
148 * basename.
149 * This means that we will
150 * - add a new entry if basename(path) was not on the list,
151 * - do nothing if an entry with higher priority was already present,
152 * - do nothing if our new entry matches the existing entry,
153 * - replace the existing entry if our new entry has higher priority.
154 */
da6053d0 155 size_t i;
d16a1c1b 156 char *t;
d16a1c1b
ZJS
157 int r;
158
159 for (i = 0; i < strv_length(*strv); i++) {
160 int c;
161
162 c = base_cmp(*strv + i, &path);
163 if (c == 0) {
a6d8474f 164 char **dir;
d16a1c1b
ZJS
165
166 /* Oh, we found our spot and it already contains something. */
a6d8474f 167 STRV_FOREACH(dir, dirs) {
d16a1c1b
ZJS
168 char *p1, *p2;
169
170 p1 = path_startswith((*strv)[i], root);
171 if (p1)
a6d8474f
ZJS
172 /* Skip "/" in *dir, because p1 is without "/" too */
173 p1 = path_startswith(p1, *dir + 1);
d16a1c1b
ZJS
174 if (p1)
175 /* Existing entry with higher priority
176 * or same priority, no need to do anything. */
177 return 0;
178
a6d8474f 179 p2 = path_startswith(path, *dir);
d16a1c1b
ZJS
180 if (p2) {
181 /* Our new entry has higher priority */
182 t = path_join(root, path, NULL);
183 if (!t)
184 return log_oom();
185
186 return free_and_replace((*strv)[i], t);
187 }
188 }
189
190 } else if (c > 0)
191 /* Following files have lower priority, let's go insert our
192 * new entry. */
193 break;
194
195 /* … we are not there yet, let's continue */
196 }
197
198 t = path_join(root, path, NULL);
199 if (!t)
200 return log_oom();
201
202 r = strv_insert(strv, i, t);
203 if (r < 0)
204 free(t);
205 return r;
206}
207
a6d8474f
ZJS
208int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
209 _cleanup_strv_free_ char **d = NULL;
210
211 assert(strv);
212
213 d = strv_split_nulstr(dirs);
214 if (!d)
215 return -ENOMEM;
216
217 return conf_files_insert(strv, root, d, path);
218}
219
b5084605 220int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
fabe5c0e
LP
221 _cleanup_strv_free_ char **copy = NULL;
222
223 assert(strv);
fabe5c0e
LP
224
225 copy = strv_copy((char**) dirs);
226 if (!copy)
227 return -ENOMEM;
228
b5084605 229 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
2c21044f
KS
230}
231
b5084605 232int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
8201ad81
LP
233 _cleanup_strv_free_ char **dirs = NULL;
234 va_list ap;
fabe5c0e
LP
235
236 assert(strv);
2c21044f 237
8201ad81
LP
238 va_start(ap, dir);
239 dirs = strv_new_ap(dir, ap);
240 va_end(ap);
241
242 if (!dirs)
243 return -ENOMEM;
fabe5c0e 244
b5084605 245 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
fabe5c0e 246}
2c21044f 247
a6d8474f
ZJS
248int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
249 _cleanup_strv_free_ char **d = NULL;
fabe5c0e
LP
250
251 assert(strv);
fabe5c0e 252
a6d8474f
ZJS
253 d = strv_split_nulstr(dirs);
254 if (!d)
fabe5c0e 255 return -ENOMEM;
2c21044f 256
a6d8474f 257 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
2c21044f 258}