]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
10 #include "dirent-util.h"
15 #include "parse-util.h"
16 #include "path-util.h"
18 #include "stat-util.h"
20 #define MAKE_SET(s) ((Set*) s)
21 #define MAKE_FDSET(s) ((FDSet*) s)
23 FDSet
*fdset_new(void) {
24 return MAKE_FDSET(set_new(NULL
));
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
)));
32 int fdset_new_array(FDSet
**ret
, const int fds
[], size_t n_fds
) {
33 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
37 assert(fds
|| n_fds
== 0);
43 for (size_t i
= 0; i
< n_fds
; i
++) {
44 r
= fdset_put(s
, fds
[i
]);
53 void fdset_close(FDSet
*s
) {
56 while ((p
= set_steal_first(MAKE_SET(s
)))) {
57 int fd
= PTR_TO_FD(p
);
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
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. */
68 _cleanup_free_
char *path
= NULL
;
70 (void) fd_get_path(fd
, &path
);
71 log_debug("Closing set fd %i (%s)", fd
, strna(path
));
74 (void) close_nointr(fd
);
78 FDSet
* fdset_free(FDSet
*s
) {
80 set_free(MAKE_SET(s
));
84 int fdset_put(FDSet
*s
, int fd
) {
88 /* Avoid integer overflow in FD_TO_PTR() */
90 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Refusing invalid fd: %d", fd
);
92 return set_put(MAKE_SET(s
), FD_TO_PTR(fd
));
95 int fdset_consume(FDSet
*s
, int fd
) {
101 r
= fdset_put(s
, fd
);
108 int fdset_put_dup(FDSet
*s
, int fd
) {
109 _cleanup_close_
int copy
= -EBADF
;
115 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
119 r
= fdset_put(s
, copy
);
123 return TAKE_FD(copy
);
126 bool fdset_contains(FDSet
*s
, int fd
) {
130 /* Avoid integer overflow in FD_TO_PTR() */
132 log_debug("Refusing invalid fd: %d", fd
);
136 return !!set_get(MAKE_SET(s
), FD_TO_PTR(fd
));
139 int fdset_remove(FDSet
*s
, int fd
) {
143 /* Avoid integer overflow in FD_TO_PTR() */
145 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT
), "Refusing invalid fd: %d", fd
);
147 return set_remove(MAKE_SET(s
), FD_TO_PTR(fd
)) ? fd
: -ENOENT
;
151 int filter_cloexec
, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */
153 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
154 _cleanup_closedir_
DIR *d
= NULL
;
159 /* Creates an fdset and fills in all currently open file descriptors. */
161 d
= opendir("/proc/self/fd");
163 if (errno
== ENOENT
&& proc_mounted() == 0)
173 FOREACH_DIRENT(de
, d
, return -errno
) {
176 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
179 fd
= parse_fd(de
->d_name
);
188 if (filter_cloexec
>= 0) {
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
);
198 if (FLAGS_SET(fl
, FD_CLOEXEC
) != !!filter_cloexec
)
202 r
= fdset_put(s
, fd
);
211 int fdset_cloexec(FDSet
*fds
, bool b
) {
217 SET_FOREACH(p
, MAKE_SET(fds
)) {
218 r
= fd_cloexec(PTR_TO_FD(p
), b
);
226 int fdset_new_listen_fds(FDSet
**ret
, bool unset
) {
227 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
232 /* Creates an fdset and fills in all passed file descriptors */
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
);
249 int fdset_to_array(FDSet
*fds
, int **ret
) {
257 if (m
> INT_MAX
) /* We want to be able to return an "int" */
260 *ret
= NULL
; /* suppress array allocation if empty */
268 SET_FOREACH(e
, MAKE_SET(fds
))
269 a
[j
++] = PTR_TO_FD(e
);
277 int fdset_close_others(FDSet
*fds
) {
278 _cleanup_free_
int *a
= NULL
;
281 n
= fdset_to_array(fds
, &a
);
285 return close_all_fds(a
, n
);
288 unsigned fdset_size(FDSet
*fds
) {
289 return set_size(MAKE_SET(fds
));
292 bool fdset_isempty(FDSet
*fds
) {
293 return set_isempty(MAKE_SET(fds
));
296 int fdset_iterate(FDSet
*s
, Iterator
*i
) {
299 if (!set_iterate(MAKE_SET(s
), i
, &p
))
305 int fdset_steal_first(FDSet
*fds
) {
308 p
= set_steal_first(MAKE_SET(fds
));