]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a16e1123 | 2 | |
cf0fbc49 | 3 | #include <errno.h> |
a16e1123 | 4 | #include <fcntl.h> |
11c3a366 | 5 | #include <stddef.h> |
da6c52c5 | 6 | #include <unistd.h> |
a16e1123 | 7 | |
07630cea LP |
8 | #include "sd-daemon.h" |
9 | ||
ae2a15bc | 10 | #include "alloc-util.h" |
8fb3f009 | 11 | #include "dirent-util.h" |
3ffd4af2 LP |
12 | #include "fd-util.h" |
13 | #include "fdset.h" | |
93cc7779 | 14 | #include "log.h" |
3ffd4af2 | 15 | #include "macro.h" |
6bedfcbb | 16 | #include "parse-util.h" |
11c3a366 | 17 | #include "path-util.h" |
93cc7779 | 18 | #include "set.h" |
08a0ebc6 | 19 | #include "stat-util.h" |
a16e1123 LP |
20 | |
21 | #define MAKE_SET(s) ((Set*) s) | |
22 | #define MAKE_FDSET(s) ((FDSet*) s) | |
23 | ||
a16e1123 | 24 | FDSet *fdset_new(void) { |
d5099efc | 25 | return MAKE_FDSET(set_new(NULL)); |
a16e1123 LP |
26 | } |
27 | ||
cf1ab844 | 28 | static void fdset_shallow_freep(FDSet **s) { |
08a0ebc6 LP |
29 | /* Destroys the set, but does not free the fds inside, like fdset_free()! */ |
30 | set_free(MAKE_SET(*ASSERT_PTR(s))); | |
31 | } | |
32 | ||
33 | int fdset_new_array(FDSet **ret, const int fds[], size_t n_fds) { | |
34 | _cleanup_(fdset_shallow_freep) FDSet *s = NULL; | |
a354329f LP |
35 | int r; |
36 | ||
37 | assert(ret); | |
08a0ebc6 | 38 | assert(fds || n_fds == 0); |
a354329f LP |
39 | |
40 | s = fdset_new(); | |
41 | if (!s) | |
42 | return -ENOMEM; | |
43 | ||
1cbd441b MY |
44 | FOREACH_ARRAY(fd, fds, n_fds) { |
45 | r = fdset_put(s, *fd); | |
08a0ebc6 | 46 | if (r < 0) |
a354329f | 47 | return r; |
a354329f LP |
48 | } |
49 | ||
08a0ebc6 | 50 | *ret = TAKE_PTR(s); |
a354329f LP |
51 | return 0; |
52 | } | |
53 | ||
e4077ff6 | 54 | void fdset_close(FDSet *s) { |
a16e1123 LP |
55 | void *p; |
56 | ||
57 | while ((p = set_steal_first(MAKE_SET(s)))) { | |
91a64476 LP |
58 | int fd = PTR_TO_FD(p); |
59 | ||
e4077ff6 LP |
60 | /* Valgrind's fd might have ended up in this set here, due to fdset_new_fill(). We'll ignore |
61 | * all failures here, so that the EBADFD that valgrind will return us on close() doesn't | |
62 | * influence us */ | |
63 | ||
64 | /* When reloading duplicates of the private bus connection fds and suchlike are closed here, | |
65 | * which has no effect at all, since they are only duplicates. So don't be surprised about | |
66 | * these log messages. */ | |
67 | ||
91a64476 LP |
68 | if (DEBUG_LOGGING) { |
69 | _cleanup_free_ char *path = NULL; | |
70 | ||
71 | (void) fd_get_path(fd, &path); | |
72 | log_debug("Closing set fd %i (%s)", fd, strna(path)); | |
73 | } | |
74 | ||
da6c52c5 | 75 | (void) close(fd); |
a16e1123 | 76 | } |
e4077ff6 | 77 | } |
a16e1123 | 78 | |
e4077ff6 LP |
79 | FDSet* fdset_free(FDSet *s) { |
80 | fdset_close(s); | |
a16e1123 | 81 | set_free(MAKE_SET(s)); |
a354329f | 82 | return NULL; |
a16e1123 LP |
83 | } |
84 | ||
85 | int fdset_put(FDSet *s, int fd) { | |
86 | assert(s); | |
87 | assert(fd >= 0); | |
88 | ||
cc938f1c FS |
89 | /* Avoid integer overflow in FD_TO_PTR() */ |
90 | if (fd == INT_MAX) | |
91 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Refusing invalid fd: %d", fd); | |
92 | ||
a16e1123 LP |
93 | return set_put(MAKE_SET(s), FD_TO_PTR(fd)); |
94 | } | |
95 | ||
e829f28c LP |
96 | int fdset_consume(FDSet *s, int fd) { |
97 | int r; | |
98 | ||
99 | assert(s); | |
100 | assert(fd >= 0); | |
101 | ||
102 | r = fdset_put(s, fd); | |
103 | if (r < 0) | |
104 | safe_close(fd); | |
105 | ||
106 | return r; | |
107 | } | |
108 | ||
a16e1123 | 109 | int fdset_put_dup(FDSet *s, int fd) { |
08a0ebc6 LP |
110 | _cleanup_close_ int copy = -EBADF; |
111 | int r; | |
a16e1123 LP |
112 | |
113 | assert(s); | |
114 | assert(fd >= 0); | |
115 | ||
e83c7163 LP |
116 | copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
117 | if (copy < 0) | |
a16e1123 LP |
118 | return -errno; |
119 | ||
e83c7163 | 120 | r = fdset_put(s, copy); |
08a0ebc6 | 121 | if (r < 0) |
a16e1123 | 122 | return r; |
a16e1123 | 123 | |
08a0ebc6 | 124 | return TAKE_FD(copy); |
a16e1123 LP |
125 | } |
126 | ||
127 | bool fdset_contains(FDSet *s, int fd) { | |
128 | assert(s); | |
129 | assert(fd >= 0); | |
130 | ||
cc938f1c FS |
131 | /* Avoid integer overflow in FD_TO_PTR() */ |
132 | if (fd == INT_MAX) { | |
133 | log_debug("Refusing invalid fd: %d", fd); | |
134 | return false; | |
135 | } | |
136 | ||
a16e1123 LP |
137 | return !!set_get(MAKE_SET(s), FD_TO_PTR(fd)); |
138 | } | |
139 | ||
140 | int fdset_remove(FDSet *s, int fd) { | |
141 | assert(s); | |
142 | assert(fd >= 0); | |
143 | ||
cc938f1c FS |
144 | /* Avoid integer overflow in FD_TO_PTR() */ |
145 | if (fd == INT_MAX) | |
146 | return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Refusing invalid fd: %d", fd); | |
147 | ||
a16e1123 LP |
148 | return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT; |
149 | } | |
150 | ||
a3dff21a LP |
151 | int fdset_new_fill( |
152 | int filter_cloexec, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */ | |
153 | FDSet **ret) { | |
a2467ea8 | 154 | |
08a0ebc6 | 155 | _cleanup_(fdset_shallow_freep) FDSet *s = NULL; |
e1d75803 | 156 | _cleanup_closedir_ DIR *d = NULL; |
08a0ebc6 | 157 | int r; |
a16e1123 | 158 | |
08a0ebc6 | 159 | assert(ret); |
a16e1123 | 160 | |
a2467ea8 MY |
161 | /* Creates an fdset and fills in all currently open file descriptors. Also set all collected fds |
162 | * to CLOEXEC. */ | |
a16e1123 | 163 | |
e83c7163 | 164 | d = opendir("/proc/self/fd"); |
08a0ebc6 LP |
165 | if (!d) { |
166 | if (errno == ENOENT && proc_mounted() == 0) | |
167 | return -ENOSYS; | |
168 | ||
a16e1123 | 169 | return -errno; |
08a0ebc6 | 170 | } |
a16e1123 | 171 | |
e83c7163 | 172 | s = fdset_new(); |
08a0ebc6 LP |
173 | if (!s) |
174 | return -ENOMEM; | |
a16e1123 | 175 | |
8fb3f009 | 176 | FOREACH_DIRENT(de, d, return -errno) { |
42dc1cd4 | 177 | int fd; |
a16e1123 | 178 | |
42dc1cd4 LP |
179 | if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN)) |
180 | continue; | |
181 | ||
182 | fd = parse_fd(de->d_name); | |
183 | if (fd < 0) | |
184 | return fd; | |
a16e1123 LP |
185 | |
186 | if (fd < 3) | |
187 | continue; | |
a16e1123 LP |
188 | if (fd == dirfd(d)) |
189 | continue; | |
190 | ||
a3dff21a LP |
191 | if (filter_cloexec >= 0) { |
192 | int fl; | |
193 | ||
194 | /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have | |
195 | * been passed in can be collected and fds which have been created locally can be | |
196 | * ignored, under the assumption that only the latter have O_CLOEXEC set. */ | |
a2467ea8 | 197 | |
a3dff21a LP |
198 | fl = fcntl(fd, F_GETFD); |
199 | if (fl < 0) | |
200 | return -errno; | |
201 | ||
202 | if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec) | |
203 | continue; | |
204 | } | |
205 | ||
a2467ea8 MY |
206 | /* We need to set CLOEXEC manually only if we're collecting non-CLOEXEC fds. */ |
207 | if (filter_cloexec <= 0) { | |
208 | r = fd_cloexec(fd, true); | |
209 | if (r < 0) | |
210 | return r; | |
211 | } | |
212 | ||
e83c7163 LP |
213 | r = fdset_put(s, fd); |
214 | if (r < 0) | |
08a0ebc6 | 215 | return r; |
a16e1123 LP |
216 | } |
217 | ||
08a0ebc6 LP |
218 | *ret = TAKE_PTR(s); |
219 | return 0; | |
a16e1123 LP |
220 | } |
221 | ||
222 | int fdset_cloexec(FDSet *fds, bool b) { | |
a16e1123 LP |
223 | void *p; |
224 | int r; | |
225 | ||
226 | assert(fds); | |
227 | ||
90e74a66 | 228 | SET_FOREACH(p, MAKE_SET(fds)) { |
3cc2aff1 LP |
229 | r = fd_cloexec(PTR_TO_FD(p), b); |
230 | if (r < 0) | |
a16e1123 | 231 | return r; |
3cc2aff1 | 232 | } |
a16e1123 LP |
233 | |
234 | return 0; | |
235 | } | |
e83c7163 | 236 | |
08a0ebc6 LP |
237 | int fdset_new_listen_fds(FDSet **ret, bool unset) { |
238 | _cleanup_(fdset_shallow_freep) FDSet *s = NULL; | |
e83c7163 | 239 | int n, fd, r; |
e83c7163 | 240 | |
08a0ebc6 | 241 | assert(ret); |
e83c7163 LP |
242 | |
243 | /* Creates an fdset and fills in all passed file descriptors */ | |
244 | ||
245 | s = fdset_new(); | |
08a0ebc6 LP |
246 | if (!s) |
247 | return -ENOMEM; | |
e83c7163 LP |
248 | |
249 | n = sd_listen_fds(unset); | |
b3a9d980 | 250 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { |
e83c7163 LP |
251 | r = fdset_put(s, fd); |
252 | if (r < 0) | |
08a0ebc6 | 253 | return r; |
e83c7163 LP |
254 | } |
255 | ||
08a0ebc6 | 256 | *ret = TAKE_PTR(s); |
e83c7163 | 257 | return 0; |
e83c7163 LP |
258 | } |
259 | ||
bdcad22e LP |
260 | int fdset_to_array(FDSet *fds, int **ret) { |
261 | unsigned j = 0, m; | |
08a0ebc6 | 262 | void *e; |
bdcad22e | 263 | int *a; |
e83c7163 | 264 | |
bdcad22e | 265 | assert(ret); |
08a0ebc6 | 266 | |
bdcad22e LP |
267 | m = fdset_size(fds); |
268 | if (m > INT_MAX) /* We want to be able to return an "int" */ | |
269 | return -ENOMEM; | |
270 | if (m == 0) { | |
271 | *ret = NULL; /* suppress array allocation if empty */ | |
272 | return 0; | |
8192548e | 273 | } |
e83c7163 | 274 | |
bdcad22e LP |
275 | a = new(int, m); |
276 | if (!a) | |
277 | return -ENOMEM; | |
278 | ||
279 | SET_FOREACH(e, MAKE_SET(fds)) | |
280 | a[j++] = PTR_TO_FD(e); | |
281 | ||
e83c7163 LP |
282 | assert(j == m); |
283 | ||
bdcad22e LP |
284 | *ret = TAKE_PTR(a); |
285 | return (int) m; | |
286 | } | |
287 | ||
288 | int fdset_close_others(FDSet *fds) { | |
289 | _cleanup_free_ int *a = NULL; | |
290 | int n; | |
291 | ||
292 | n = fdset_to_array(fds, &a); | |
293 | if (n < 0) | |
294 | return n; | |
295 | ||
296 | return close_all_fds(a, n); | |
e83c7163 LP |
297 | } |
298 | ||
299 | unsigned fdset_size(FDSet *fds) { | |
300 | return set_size(MAKE_SET(fds)); | |
301 | } | |
302 | ||
a354329f LP |
303 | bool fdset_isempty(FDSet *fds) { |
304 | return set_isempty(MAKE_SET(fds)); | |
305 | } | |
306 | ||
e83c7163 LP |
307 | int fdset_iterate(FDSet *s, Iterator *i) { |
308 | void *p; | |
309 | ||
8927b1da | 310 | if (!set_iterate(MAKE_SET(s), i, &p)) |
e83c7163 LP |
311 | return -ENOENT; |
312 | ||
313 | return PTR_TO_FD(p); | |
314 | } | |
a354329f LP |
315 | |
316 | int fdset_steal_first(FDSet *fds) { | |
317 | void *p; | |
318 | ||
319 | p = set_steal_first(MAKE_SET(fds)); | |
320 | if (!p) | |
321 | return -ENOENT; | |
322 | ||
323 | return PTR_TO_FD(p); | |
324 | } |