]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
Merge pull request #29530 from poettering/debug-log-tweaks
[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 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 int fd = PTR_TO_FD(p);
58
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
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);
75 }
76 }
77
78 FDSet* fdset_free(FDSet *s) {
79 fdset_close(s);
80 set_free(MAKE_SET(s));
81 return NULL;
82 }
83
84 int fdset_put(FDSet *s, int fd) {
85 assert(s);
86 assert(fd >= 0);
87
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
92 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
93 }
94
95 int 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
108 int fdset_put_dup(FDSet *s, int fd) {
109 _cleanup_close_ int copy = -EBADF;
110 int r;
111
112 assert(s);
113 assert(fd >= 0);
114
115 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
116 if (copy < 0)
117 return -errno;
118
119 r = fdset_put(s, copy);
120 if (r < 0)
121 return r;
122
123 return TAKE_FD(copy);
124 }
125
126 bool fdset_contains(FDSet *s, int fd) {
127 assert(s);
128 assert(fd >= 0);
129
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
136 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
137 }
138
139 int fdset_remove(FDSet *s, int fd) {
140 assert(s);
141 assert(fd >= 0);
142
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
147 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
148 }
149
150 int 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) {
153 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
154 _cleanup_closedir_ DIR *d = NULL;
155 int r;
156
157 assert(ret);
158
159 /* Creates an fdset and fills in all currently open file descriptors. */
160
161 d = opendir("/proc/self/fd");
162 if (!d) {
163 if (errno == ENOENT && proc_mounted() == 0)
164 return -ENOSYS;
165
166 return -errno;
167 }
168
169 s = fdset_new();
170 if (!s)
171 return -ENOMEM;
172
173 FOREACH_DIRENT(de, d, return -errno) {
174 int fd;
175
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;
182
183 if (fd < 3)
184 continue;
185 if (fd == dirfd(d))
186 continue;
187
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
202 r = fdset_put(s, fd);
203 if (r < 0)
204 return r;
205 }
206
207 *ret = TAKE_PTR(s);
208 return 0;
209 }
210
211 int fdset_cloexec(FDSet *fds, bool b) {
212 void *p;
213 int r;
214
215 assert(fds);
216
217 SET_FOREACH(p, MAKE_SET(fds)) {
218 r = fd_cloexec(PTR_TO_FD(p), b);
219 if (r < 0)
220 return r;
221 }
222
223 return 0;
224 }
225
226 int fdset_new_listen_fds(FDSet **ret, bool unset) {
227 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
228 int n, fd, r;
229
230 assert(ret);
231
232 /* Creates an fdset and fills in all passed file descriptors */
233
234 s = fdset_new();
235 if (!s)
236 return -ENOMEM;
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)
242 return r;
243 }
244
245 *ret = TAKE_PTR(s);
246 return 0;
247 }
248
249 int fdset_to_array(FDSet *fds, int **ret) {
250 unsigned j = 0, m;
251 void *e;
252 int *a;
253
254 assert(ret);
255
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;
262 }
263
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
271 assert(j == m);
272
273 *ret = TAKE_PTR(a);
274 return (int) m;
275 }
276
277 int 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);
286 }
287
288 unsigned fdset_size(FDSet *fds) {
289 return set_size(MAKE_SET(fds));
290 }
291
292 bool fdset_isempty(FDSet *fds) {
293 return set_isempty(MAKE_SET(fds));
294 }
295
296 int fdset_iterate(FDSet *s, Iterator *i) {
297 void *p;
298
299 if (!set_iterate(MAKE_SET(s), i, &p))
300 return -ENOENT;
301
302 return PTR_TO_FD(p);
303 }
304
305 int 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 }