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