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