]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/fdset.c
pid1: when taking possession of passed fds check O_CLOEXEC state first
[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
08a0ebc6
LP
27static 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
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)))) {
e4077ff6
LP
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));
a16e1123 67 }
e4077ff6 68}
a16e1123 69
e4077ff6
LP
70FDSet* fdset_free(FDSet *s) {
71 fdset_close(s);
a16e1123 72 set_free(MAKE_SET(s));
a354329f 73 return NULL;
a16e1123
LP
74}
75
76int fdset_put(FDSet *s, int fd) {
77 assert(s);
78 assert(fd >= 0);
79
cc938f1c
FS
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
a16e1123
LP
84 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
85}
86
e829f28c
LP
87int 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
a16e1123 100int fdset_put_dup(FDSet *s, int fd) {
08a0ebc6
LP
101 _cleanup_close_ int copy = -EBADF;
102 int r;
a16e1123
LP
103
104 assert(s);
105 assert(fd >= 0);
106
e83c7163
LP
107 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
108 if (copy < 0)
a16e1123
LP
109 return -errno;
110
e83c7163 111 r = fdset_put(s, copy);
08a0ebc6 112 if (r < 0)
a16e1123 113 return r;
a16e1123 114
08a0ebc6 115 return TAKE_FD(copy);
a16e1123
LP
116}
117
118bool fdset_contains(FDSet *s, int fd) {
119 assert(s);
120 assert(fd >= 0);
121
cc938f1c
FS
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
a16e1123
LP
128 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
129}
130
131int fdset_remove(FDSet *s, int fd) {
132 assert(s);
133 assert(fd >= 0);
134
cc938f1c
FS
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
a16e1123
LP
139 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
140}
141
a3dff21a
LP
142int 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) {
08a0ebc6 145 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e1d75803 146 _cleanup_closedir_ DIR *d = NULL;
08a0ebc6 147 int r;
a16e1123 148
08a0ebc6 149 assert(ret);
a16e1123 150
08a0ebc6 151 /* Creates an fdset and fills in all currently open file descriptors. */
a16e1123 152
e83c7163 153 d = opendir("/proc/self/fd");
08a0ebc6
LP
154 if (!d) {
155 if (errno == ENOENT && proc_mounted() == 0)
156 return -ENOSYS;
157
a16e1123 158 return -errno;
08a0ebc6 159 }
a16e1123 160
e83c7163 161 s = fdset_new();
08a0ebc6
LP
162 if (!s)
163 return -ENOMEM;
a16e1123 164
8fb3f009 165 FOREACH_DIRENT(de, d, return -errno) {
254d1313 166 int fd = -EBADF;
a16e1123 167
e83c7163
LP
168 r = safe_atoi(de->d_name, &fd);
169 if (r < 0)
08a0ebc6 170 return r;
a16e1123
LP
171
172 if (fd < 3)
173 continue;
a16e1123
LP
174 if (fd == dirfd(d))
175 continue;
176
a3dff21a
LP
177 if (filter_cloexec >= 0) {
178 int fl;
179
180 /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have
181 * been passed in can be collected and fds which have been created locally can be
182 * ignored, under the assumption that only the latter have O_CLOEXEC set. */
183 fl = fcntl(fd, F_GETFD);
184 if (fl < 0)
185 return -errno;
186
187 if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec)
188 continue;
189 }
190
e83c7163
LP
191 r = fdset_put(s, fd);
192 if (r < 0)
08a0ebc6 193 return r;
a16e1123
LP
194 }
195
08a0ebc6
LP
196 *ret = TAKE_PTR(s);
197 return 0;
a16e1123
LP
198}
199
200int fdset_cloexec(FDSet *fds, bool b) {
a16e1123
LP
201 void *p;
202 int r;
203
204 assert(fds);
205
90e74a66 206 SET_FOREACH(p, MAKE_SET(fds)) {
3cc2aff1
LP
207 r = fd_cloexec(PTR_TO_FD(p), b);
208 if (r < 0)
a16e1123 209 return r;
3cc2aff1 210 }
a16e1123
LP
211
212 return 0;
213}
e83c7163 214
08a0ebc6
LP
215int fdset_new_listen_fds(FDSet **ret, bool unset) {
216 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e83c7163 217 int n, fd, r;
e83c7163 218
08a0ebc6 219 assert(ret);
e83c7163
LP
220
221 /* Creates an fdset and fills in all passed file descriptors */
222
223 s = fdset_new();
08a0ebc6
LP
224 if (!s)
225 return -ENOMEM;
e83c7163
LP
226
227 n = sd_listen_fds(unset);
228 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
229 r = fdset_put(s, fd);
230 if (r < 0)
08a0ebc6 231 return r;
e83c7163
LP
232 }
233
08a0ebc6 234 *ret = TAKE_PTR(s);
e83c7163 235 return 0;
e83c7163
LP
236}
237
bdcad22e
LP
238int fdset_to_array(FDSet *fds, int **ret) {
239 unsigned j = 0, m;
08a0ebc6 240 void *e;
bdcad22e 241 int *a;
e83c7163 242
bdcad22e 243 assert(ret);
08a0ebc6 244
bdcad22e
LP
245 m = fdset_size(fds);
246 if (m > INT_MAX) /* We want to be able to return an "int" */
247 return -ENOMEM;
248 if (m == 0) {
249 *ret = NULL; /* suppress array allocation if empty */
250 return 0;
8192548e 251 }
e83c7163 252
bdcad22e
LP
253 a = new(int, m);
254 if (!a)
255 return -ENOMEM;
256
257 SET_FOREACH(e, MAKE_SET(fds))
258 a[j++] = PTR_TO_FD(e);
259
e83c7163
LP
260 assert(j == m);
261
bdcad22e
LP
262 *ret = TAKE_PTR(a);
263 return (int) m;
264}
265
266int fdset_close_others(FDSet *fds) {
267 _cleanup_free_ int *a = NULL;
268 int n;
269
270 n = fdset_to_array(fds, &a);
271 if (n < 0)
272 return n;
273
274 return close_all_fds(a, n);
e83c7163
LP
275}
276
277unsigned fdset_size(FDSet *fds) {
278 return set_size(MAKE_SET(fds));
279}
280
a354329f
LP
281bool fdset_isempty(FDSet *fds) {
282 return set_isempty(MAKE_SET(fds));
283}
284
e83c7163
LP
285int fdset_iterate(FDSet *s, Iterator *i) {
286 void *p;
287
8927b1da 288 if (!set_iterate(MAKE_SET(s), i, &p))
e83c7163
LP
289 return -ENOENT;
290
291 return PTR_TO_FD(p);
292}
a354329f
LP
293
294int fdset_steal_first(FDSet *fds) {
295 void *p;
296
297 p = set_steal_first(MAKE_SET(fds));
298 if (!p)
299 return -ENOENT;
300
301 return PTR_TO_FD(p);
302}