Debian Codegraph @

src/shared/fdset.c

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