]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/fdset.c
logind-session-device: use _cleanup_close_
[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
1cbd441b
MY
43 FOREACH_ARRAY(fd, fds, n_fds) {
44 r = fdset_put(s, *fd);
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) {
a2467ea8 153
08a0ebc6 154 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e1d75803 155 _cleanup_closedir_ DIR *d = NULL;
08a0ebc6 156 int r;
a16e1123 157
08a0ebc6 158 assert(ret);
a16e1123 159
a2467ea8
MY
160 /* Creates an fdset and fills in all currently open file descriptors. Also set all collected fds
161 * to CLOEXEC. */
a16e1123 162
e83c7163 163 d = opendir("/proc/self/fd");
08a0ebc6
LP
164 if (!d) {
165 if (errno == ENOENT && proc_mounted() == 0)
166 return -ENOSYS;
167
a16e1123 168 return -errno;
08a0ebc6 169 }
a16e1123 170
e83c7163 171 s = fdset_new();
08a0ebc6
LP
172 if (!s)
173 return -ENOMEM;
a16e1123 174
8fb3f009 175 FOREACH_DIRENT(de, d, return -errno) {
42dc1cd4 176 int fd;
a16e1123 177
42dc1cd4
LP
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;
a16e1123
LP
184
185 if (fd < 3)
186 continue;
a16e1123
LP
187 if (fd == dirfd(d))
188 continue;
189
a3dff21a
LP
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. */
a2467ea8 196
a3dff21a
LP
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
a2467ea8
MY
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
e83c7163
LP
212 r = fdset_put(s, fd);
213 if (r < 0)
08a0ebc6 214 return r;
a16e1123
LP
215 }
216
08a0ebc6
LP
217 *ret = TAKE_PTR(s);
218 return 0;
a16e1123
LP
219}
220
221int fdset_cloexec(FDSet *fds, bool b) {
a16e1123
LP
222 void *p;
223 int r;
224
225 assert(fds);
226
90e74a66 227 SET_FOREACH(p, MAKE_SET(fds)) {
3cc2aff1
LP
228 r = fd_cloexec(PTR_TO_FD(p), b);
229 if (r < 0)
a16e1123 230 return r;
3cc2aff1 231 }
a16e1123
LP
232
233 return 0;
234}
e83c7163 235
08a0ebc6
LP
236int fdset_new_listen_fds(FDSet **ret, bool unset) {
237 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e83c7163 238 int n, fd, r;
e83c7163 239
08a0ebc6 240 assert(ret);
e83c7163
LP
241
242 /* Creates an fdset and fills in all passed file descriptors */
243
244 s = fdset_new();
08a0ebc6
LP
245 if (!s)
246 return -ENOMEM;
e83c7163
LP
247
248 n = sd_listen_fds(unset);
b3a9d980 249 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
e83c7163
LP
250 r = fdset_put(s, fd);
251 if (r < 0)
08a0ebc6 252 return r;
e83c7163
LP
253 }
254
08a0ebc6 255 *ret = TAKE_PTR(s);
e83c7163 256 return 0;
e83c7163
LP
257}
258
bdcad22e
LP
259int fdset_to_array(FDSet *fds, int **ret) {
260 unsigned j = 0, m;
08a0ebc6 261 void *e;
bdcad22e 262 int *a;
e83c7163 263
bdcad22e 264 assert(ret);
08a0ebc6 265
bdcad22e
LP
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;
8192548e 272 }
e83c7163 273
bdcad22e
LP
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
e83c7163
LP
281 assert(j == m);
282
bdcad22e
LP
283 *ret = TAKE_PTR(a);
284 return (int) m;
285}
286
287int 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);
e83c7163
LP
296}
297
298unsigned fdset_size(FDSet *fds) {
299 return set_size(MAKE_SET(fds));
300}
301
a354329f
LP
302bool fdset_isempty(FDSet *fds) {
303 return set_isempty(MAKE_SET(fds));
304}
305
e83c7163
LP
306int fdset_iterate(FDSet *s, Iterator *i) {
307 void *p;
308
8927b1da 309 if (!set_iterate(MAKE_SET(s), i, &p))
e83c7163
LP
310 return -ENOENT;
311
312 return PTR_TO_FD(p);
313}
a354329f
LP
314
315int 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}