]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
fdset: minor modernizations
[thirdparty/systemd.git] / src / shared / fdset.c
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 inline 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 for (size_t i = 0; i < n_fds; i++) {
44 r = fdset_put(s, fds[i]);
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 /* Valgrind's fd might have ended up in this set here, due to fdset_new_fill(). We'll ignore
58 * all failures here, so that the EBADFD that valgrind will return us on close() doesn't
59 * influence us */
60
61 /* When reloading duplicates of the private bus connection fds and suchlike are closed here,
62 * which has no effect at all, since they are only duplicates. So don't be surprised about
63 * these log messages. */
64
65 log_debug("Closing set fd %i", PTR_TO_FD(p));
66 (void) close_nointr(PTR_TO_FD(p));
67 }
68 }
69
70 FDSet* fdset_free(FDSet *s) {
71 fdset_close(s);
72 set_free(MAKE_SET(s));
73 return NULL;
74 }
75
76 int fdset_put(FDSet *s, int fd) {
77 assert(s);
78 assert(fd >= 0);
79
80 /* Avoid integer overflow in FD_TO_PTR() */
81 if (fd == INT_MAX)
82 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Refusing invalid fd: %d", fd);
83
84 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
85 }
86
87 int fdset_consume(FDSet *s, int fd) {
88 int r;
89
90 assert(s);
91 assert(fd >= 0);
92
93 r = fdset_put(s, fd);
94 if (r < 0)
95 safe_close(fd);
96
97 return r;
98 }
99
100 int fdset_put_dup(FDSet *s, int fd) {
101 _cleanup_close_ int copy = -EBADF;
102 int r;
103
104 assert(s);
105 assert(fd >= 0);
106
107 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
108 if (copy < 0)
109 return -errno;
110
111 r = fdset_put(s, copy);
112 if (r < 0)
113 return r;
114
115 return TAKE_FD(copy);
116 }
117
118 bool fdset_contains(FDSet *s, int fd) {
119 assert(s);
120 assert(fd >= 0);
121
122 /* Avoid integer overflow in FD_TO_PTR() */
123 if (fd == INT_MAX) {
124 log_debug("Refusing invalid fd: %d", fd);
125 return false;
126 }
127
128 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
129 }
130
131 int fdset_remove(FDSet *s, int fd) {
132 assert(s);
133 assert(fd >= 0);
134
135 /* Avoid integer overflow in FD_TO_PTR() */
136 if (fd == INT_MAX)
137 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Refusing invalid fd: %d", fd);
138
139 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
140 }
141
142 int fdset_new_fill(
143 int filter_cloexec, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */
144 FDSet **ret) {
145 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
146 _cleanup_closedir_ DIR *d = NULL;
147 int r;
148
149 assert(ret);
150
151 /* Creates an fdset and fills in all currently open file descriptors. */
152
153 d = opendir("/proc/self/fd");
154 if (!d) {
155 if (errno == ENOENT && proc_mounted() == 0)
156 return -ENOSYS;
157
158 return -errno;
159 }
160
161 s = fdset_new();
162 if (!s)
163 return -ENOMEM;
164
165 FOREACH_DIRENT(de, d, return -errno) {
166 int fd;
167
168 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
169 continue;
170
171 fd = parse_fd(de->d_name);
172 if (fd < 0)
173 return fd;
174
175 if (fd < 3)
176 continue;
177 if (fd == dirfd(d))
178 continue;
179
180 if (filter_cloexec >= 0) {
181 int fl;
182
183 /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have
184 * been passed in can be collected and fds which have been created locally can be
185 * ignored, under the assumption that only the latter have O_CLOEXEC set. */
186 fl = fcntl(fd, F_GETFD);
187 if (fl < 0)
188 return -errno;
189
190 if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec)
191 continue;
192 }
193
194 r = fdset_put(s, fd);
195 if (r < 0)
196 return r;
197 }
198
199 *ret = TAKE_PTR(s);
200 return 0;
201 }
202
203 int fdset_cloexec(FDSet *fds, bool b) {
204 void *p;
205 int r;
206
207 assert(fds);
208
209 SET_FOREACH(p, MAKE_SET(fds)) {
210 r = fd_cloexec(PTR_TO_FD(p), b);
211 if (r < 0)
212 return r;
213 }
214
215 return 0;
216 }
217
218 int fdset_new_listen_fds(FDSet **ret, bool unset) {
219 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
220 int n, fd, r;
221
222 assert(ret);
223
224 /* Creates an fdset and fills in all passed file descriptors */
225
226 s = fdset_new();
227 if (!s)
228 return -ENOMEM;
229
230 n = sd_listen_fds(unset);
231 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
232 r = fdset_put(s, fd);
233 if (r < 0)
234 return r;
235 }
236
237 *ret = TAKE_PTR(s);
238 return 0;
239 }
240
241 int fdset_to_array(FDSet *fds, int **ret) {
242 unsigned j = 0, m;
243 void *e;
244 int *a;
245
246 assert(ret);
247
248 m = fdset_size(fds);
249 if (m > INT_MAX) /* We want to be able to return an "int" */
250 return -ENOMEM;
251 if (m == 0) {
252 *ret = NULL; /* suppress array allocation if empty */
253 return 0;
254 }
255
256 a = new(int, m);
257 if (!a)
258 return -ENOMEM;
259
260 SET_FOREACH(e, MAKE_SET(fds))
261 a[j++] = PTR_TO_FD(e);
262
263 assert(j == m);
264
265 *ret = TAKE_PTR(a);
266 return (int) m;
267 }
268
269 int fdset_close_others(FDSet *fds) {
270 _cleanup_free_ int *a = NULL;
271 int n;
272
273 n = fdset_to_array(fds, &a);
274 if (n < 0)
275 return n;
276
277 return close_all_fds(a, n);
278 }
279
280 unsigned fdset_size(FDSet *fds) {
281 return set_size(MAKE_SET(fds));
282 }
283
284 bool fdset_isempty(FDSet *fds) {
285 return set_isempty(MAKE_SET(fds));
286 }
287
288 int fdset_iterate(FDSet *s, Iterator *i) {
289 void *p;
290
291 if (!set_iterate(MAKE_SET(s), i, &p))
292 return -ENOENT;
293
294 return PTR_TO_FD(p);
295 }
296
297 int fdset_steal_first(FDSet *fds) {
298 void *p;
299
300 p = set_steal_first(MAKE_SET(fds));
301 if (!p)
302 return -ENOENT;
303
304 return PTR_TO_FD(p);
305 }