]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/fdset.c
core/exec-invoke: rename flags_fds to flag_fds
[thirdparty/systemd.git] / src / shared / fdset.c
CommitLineData
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>
a16e1123 6
07630cea
LP
7#include "sd-daemon.h"
8
ae2a15bc 9#include "alloc-util.h"
8fb3f009 10#include "dirent-util.h"
3ffd4af2
LP
11#include "fd-util.h"
12#include "fdset.h"
93cc7779 13#include "log.h"
3ffd4af2 14#include "macro.h"
6bedfcbb 15#include "parse-util.h"
11c3a366 16#include "path-util.h"
93cc7779 17#include "set.h"
08a0ebc6 18#include "stat-util.h"
a16e1123
LP
19
20#define MAKE_SET(s) ((Set*) s)
21#define MAKE_FDSET(s) ((FDSet*) s)
22
a16e1123 23FDSet *fdset_new(void) {
d5099efc 24 return MAKE_FDSET(set_new(NULL));
a16e1123
LP
25}
26
cf1ab844 27static void fdset_shallow_freep(FDSet **s) {
08a0ebc6
LP
28 /* Destroys the set, but does not free the fds inside, like fdset_free()! */
29 set_free(MAKE_SET(*ASSERT_PTR(s)));
30}
31
32int fdset_new_array(FDSet **ret, const int fds[], size_t n_fds) {
33 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
a354329f
LP
34 int r;
35
36 assert(ret);
08a0ebc6 37 assert(fds || n_fds == 0);
a354329f
LP
38
39 s = fdset_new();
40 if (!s)
41 return -ENOMEM;
42
08a0ebc6 43 for (size_t i = 0; i < n_fds; i++) {
a354329f 44 r = fdset_put(s, fds[i]);
08a0ebc6 45 if (r < 0)
a354329f 46 return r;
a354329f
LP
47 }
48
08a0ebc6 49 *ret = TAKE_PTR(s);
a354329f
LP
50 return 0;
51}
52
e4077ff6 53void fdset_close(FDSet *s) {
a16e1123
LP
54 void *p;
55
56 while ((p = set_steal_first(MAKE_SET(s)))) {
91a64476
LP
57 int fd = PTR_TO_FD(p);
58
e4077ff6
LP
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
91a64476
LP
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);
a16e1123 75 }
e4077ff6 76}
a16e1123 77
e4077ff6
LP
78FDSet* fdset_free(FDSet *s) {
79 fdset_close(s);
a16e1123 80 set_free(MAKE_SET(s));
a354329f 81 return NULL;
a16e1123
LP
82}
83
84int fdset_put(FDSet *s, int fd) {
85 assert(s);
86 assert(fd >= 0);
87
cc938f1c
FS
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
a16e1123
LP
92 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
93}
94
e829f28c
LP
95int 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
a16e1123 108int fdset_put_dup(FDSet *s, int fd) {
08a0ebc6
LP
109 _cleanup_close_ int copy = -EBADF;
110 int r;
a16e1123
LP
111
112 assert(s);
113 assert(fd >= 0);
114
e83c7163
LP
115 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
116 if (copy < 0)
a16e1123
LP
117 return -errno;
118
e83c7163 119 r = fdset_put(s, copy);
08a0ebc6 120 if (r < 0)
a16e1123 121 return r;
a16e1123 122
08a0ebc6 123 return TAKE_FD(copy);
a16e1123
LP
124}
125
126bool fdset_contains(FDSet *s, int fd) {
127 assert(s);
128 assert(fd >= 0);
129
cc938f1c
FS
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
a16e1123
LP
136 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
137}
138
139int fdset_remove(FDSet *s, int fd) {
140 assert(s);
141 assert(fd >= 0);
142
cc938f1c
FS
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
a16e1123
LP
147 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
148}
149
a3dff21a
LP
150int 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) {
08a0ebc6 153 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e1d75803 154 _cleanup_closedir_ DIR *d = NULL;
08a0ebc6 155 int r;
a16e1123 156
08a0ebc6 157 assert(ret);
a16e1123 158
08a0ebc6 159 /* Creates an fdset and fills in all currently open file descriptors. */
a16e1123 160
e83c7163 161 d = opendir("/proc/self/fd");
08a0ebc6
LP
162 if (!d) {
163 if (errno == ENOENT && proc_mounted() == 0)
164 return -ENOSYS;
165
a16e1123 166 return -errno;
08a0ebc6 167 }
a16e1123 168
e83c7163 169 s = fdset_new();
08a0ebc6
LP
170 if (!s)
171 return -ENOMEM;
a16e1123 172
8fb3f009 173 FOREACH_DIRENT(de, d, return -errno) {
42dc1cd4 174 int fd;
a16e1123 175
42dc1cd4
LP
176 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
177 continue;
178
179 fd = parse_fd(de->d_name);
180 if (fd < 0)
181 return fd;
a16e1123
LP
182
183 if (fd < 3)
184 continue;
a16e1123
LP
185 if (fd == dirfd(d))
186 continue;
187
a3dff21a
LP
188 if (filter_cloexec >= 0) {
189 int fl;
190
191 /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have
192 * been passed in can be collected and fds which have been created locally can be
193 * ignored, under the assumption that only the latter have O_CLOEXEC set. */
194 fl = fcntl(fd, F_GETFD);
195 if (fl < 0)
196 return -errno;
197
198 if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec)
199 continue;
200 }
201
e83c7163
LP
202 r = fdset_put(s, fd);
203 if (r < 0)
08a0ebc6 204 return r;
a16e1123
LP
205 }
206
08a0ebc6
LP
207 *ret = TAKE_PTR(s);
208 return 0;
a16e1123
LP
209}
210
211int fdset_cloexec(FDSet *fds, bool b) {
a16e1123
LP
212 void *p;
213 int r;
214
215 assert(fds);
216
90e74a66 217 SET_FOREACH(p, MAKE_SET(fds)) {
3cc2aff1
LP
218 r = fd_cloexec(PTR_TO_FD(p), b);
219 if (r < 0)
a16e1123 220 return r;
3cc2aff1 221 }
a16e1123
LP
222
223 return 0;
224}
e83c7163 225
08a0ebc6
LP
226int fdset_new_listen_fds(FDSet **ret, bool unset) {
227 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e83c7163 228 int n, fd, r;
e83c7163 229
08a0ebc6 230 assert(ret);
e83c7163
LP
231
232 /* Creates an fdset and fills in all passed file descriptors */
233
234 s = fdset_new();
08a0ebc6
LP
235 if (!s)
236 return -ENOMEM;
e83c7163
LP
237
238 n = sd_listen_fds(unset);
239 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
240 r = fdset_put(s, fd);
241 if (r < 0)
08a0ebc6 242 return r;
e83c7163
LP
243 }
244
08a0ebc6 245 *ret = TAKE_PTR(s);
e83c7163 246 return 0;
e83c7163
LP
247}
248
bdcad22e
LP
249int fdset_to_array(FDSet *fds, int **ret) {
250 unsigned j = 0, m;
08a0ebc6 251 void *e;
bdcad22e 252 int *a;
e83c7163 253
bdcad22e 254 assert(ret);
08a0ebc6 255
bdcad22e
LP
256 m = fdset_size(fds);
257 if (m > INT_MAX) /* We want to be able to return an "int" */
258 return -ENOMEM;
259 if (m == 0) {
260 *ret = NULL; /* suppress array allocation if empty */
261 return 0;
8192548e 262 }
e83c7163 263
bdcad22e
LP
264 a = new(int, m);
265 if (!a)
266 return -ENOMEM;
267
268 SET_FOREACH(e, MAKE_SET(fds))
269 a[j++] = PTR_TO_FD(e);
270
e83c7163
LP
271 assert(j == m);
272
bdcad22e
LP
273 *ret = TAKE_PTR(a);
274 return (int) m;
275}
276
277int fdset_close_others(FDSet *fds) {
278 _cleanup_free_ int *a = NULL;
279 int n;
280
281 n = fdset_to_array(fds, &a);
282 if (n < 0)
283 return n;
284
285 return close_all_fds(a, n);
e83c7163
LP
286}
287
288unsigned fdset_size(FDSet *fds) {
289 return set_size(MAKE_SET(fds));
290}
291
a354329f
LP
292bool fdset_isempty(FDSet *fds) {
293 return set_isempty(MAKE_SET(fds));
294}
295
e83c7163
LP
296int fdset_iterate(FDSet *s, Iterator *i) {
297 void *p;
298
8927b1da 299 if (!set_iterate(MAKE_SET(s), i, &p))
e83c7163
LP
300 return -ENOENT;
301
302 return PTR_TO_FD(p);
303}
a354329f
LP
304
305int fdset_steal_first(FDSet *fds) {
306 void *p;
307
308 p = set_steal_first(MAKE_SET(fds));
309 if (!p)
310 return -ENOENT;
311
312 return PTR_TO_FD(p);
313}