]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "dirent-util.h"
16 #include "parse-util.h"
17 #include "path-util.h"
19 #include "stat-util.h"
21 #define MAKE_SET(s) ((Set*) s)
22 #define MAKE_FDSET(s) ((FDSet*) s)
24 FDSet
*fdset_new(void) {
25 return MAKE_FDSET(set_new(NULL
));
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
)));
33 int fdset_new_array(FDSet
**ret
, const int fds
[], size_t n_fds
) {
34 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
38 assert(fds
|| n_fds
== 0);
44 FOREACH_ARRAY(fd
, fds
, n_fds
) {
45 r
= fdset_put(s
, *fd
);
54 void fdset_close(FDSet
*s
) {
57 while ((p
= set_steal_first(MAKE_SET(s
)))) {
58 int fd
= PTR_TO_FD(p
);
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
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. */
69 _cleanup_free_
char *path
= NULL
;
71 (void) fd_get_path(fd
, &path
);
72 log_debug("Closing set fd %i (%s)", fd
, strna(path
));
79 FDSet
* fdset_free(FDSet
*s
) {
81 set_free(MAKE_SET(s
));
85 int fdset_put(FDSet
*s
, int fd
) {
89 /* Avoid integer overflow in FD_TO_PTR() */
91 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Refusing invalid fd: %d", fd
);
93 return set_put(MAKE_SET(s
), FD_TO_PTR(fd
));
96 int fdset_consume(FDSet
*s
, int fd
) {
102 r
= fdset_put(s
, fd
);
109 int fdset_put_dup(FDSet
*s
, int fd
) {
110 _cleanup_close_
int copy
= -EBADF
;
116 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
120 r
= fdset_put(s
, copy
);
124 return TAKE_FD(copy
);
127 bool fdset_contains(FDSet
*s
, int fd
) {
131 /* Avoid integer overflow in FD_TO_PTR() */
133 log_debug("Refusing invalid fd: %d", fd
);
137 return !!set_get(MAKE_SET(s
), FD_TO_PTR(fd
));
140 int fdset_remove(FDSet
*s
, int fd
) {
144 /* Avoid integer overflow in FD_TO_PTR() */
146 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT
), "Refusing invalid fd: %d", fd
);
148 return set_remove(MAKE_SET(s
), FD_TO_PTR(fd
)) ? fd
: -ENOENT
;
152 int filter_cloexec
, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */
155 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
156 _cleanup_closedir_
DIR *d
= NULL
;
161 /* Creates an fdset and fills in all currently open file descriptors. Also set all collected fds
164 d
= opendir("/proc/self/fd");
166 if (errno
== ENOENT
&& proc_mounted() == 0)
176 FOREACH_DIRENT(de
, d
, return -errno
) {
179 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
182 fd
= parse_fd(de
->d_name
);
191 if (filter_cloexec
>= 0) {
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. */
198 fl
= fcntl(fd
, F_GETFD
);
202 if (FLAGS_SET(fl
, FD_CLOEXEC
) != !!filter_cloexec
)
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);
213 r
= fdset_put(s
, fd
);
222 int fdset_cloexec(FDSet
*fds
, bool b
) {
228 SET_FOREACH(p
, MAKE_SET(fds
)) {
229 r
= fd_cloexec(PTR_TO_FD(p
), b
);
237 int fdset_new_listen_fds(FDSet
**ret
, bool unset
) {
238 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
243 /* Creates an fdset and fills in all passed file descriptors */
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
);
260 int fdset_to_array(FDSet
*fds
, int **ret
) {
268 if (m
> INT_MAX
) /* We want to be able to return an "int" */
271 *ret
= NULL
; /* suppress array allocation if empty */
279 SET_FOREACH(e
, MAKE_SET(fds
))
280 a
[j
++] = PTR_TO_FD(e
);
288 int fdset_close_others(FDSet
*fds
) {
289 _cleanup_free_
int *a
= NULL
;
292 n
= fdset_to_array(fds
, &a
);
296 return close_all_fds(a
, n
);
299 unsigned fdset_size(FDSet
*fds
) {
300 return set_size(MAKE_SET(fds
));
303 bool fdset_isempty(FDSet
*fds
) {
304 return set_isempty(MAKE_SET(fds
));
307 int fdset_iterate(FDSet
*s
, Iterator
*i
) {
310 if (!set_iterate(MAKE_SET(s
), i
, &p
))
316 int fdset_steal_first(FDSet
*fds
) {
319 p
= set_steal_first(MAKE_SET(fds
));