]>
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 inline 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 /* Valgrind's fd might have ended up in this set here, due to fdset_new_fill(). We'll ignore
58 * all failures here, so that the EBADFD that valgrind will return us on close() doesn't
61 /* When reloading duplicates of the private bus connection fds and suchlike are closed here,
62 * which has no effect at all, since they are only duplicates. So don't be surprised about
63 * these log messages. */
65 log_debug("Closing set fd %i", PTR_TO_FD(p
));
66 (void) close_nointr(PTR_TO_FD(p
));
70 FDSet
* fdset_free(FDSet
*s
) {
72 set_free(MAKE_SET(s
));
76 int fdset_put(FDSet
*s
, int fd
) {
80 /* Avoid integer overflow in FD_TO_PTR() */
82 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Refusing invalid fd: %d", fd
);
84 return set_put(MAKE_SET(s
), FD_TO_PTR(fd
));
87 int fdset_consume(FDSet
*s
, int fd
) {
100 int fdset_put_dup(FDSet
*s
, int fd
) {
101 _cleanup_close_
int copy
= -EBADF
;
107 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
111 r
= fdset_put(s
, copy
);
115 return TAKE_FD(copy
);
118 bool fdset_contains(FDSet
*s
, int fd
) {
122 /* Avoid integer overflow in FD_TO_PTR() */
124 log_debug("Refusing invalid fd: %d", fd
);
128 return !!set_get(MAKE_SET(s
), FD_TO_PTR(fd
));
131 int fdset_remove(FDSet
*s
, int fd
) {
135 /* Avoid integer overflow in FD_TO_PTR() */
137 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT
), "Refusing invalid fd: %d", fd
);
139 return set_remove(MAKE_SET(s
), FD_TO_PTR(fd
)) ? fd
: -ENOENT
;
143 int filter_cloexec
, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */
145 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
146 _cleanup_closedir_
DIR *d
= NULL
;
151 /* Creates an fdset and fills in all currently open file descriptors. */
153 d
= opendir("/proc/self/fd");
155 if (errno
== ENOENT
&& proc_mounted() == 0)
165 FOREACH_DIRENT(de
, d
, return -errno
) {
168 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
171 fd
= parse_fd(de
->d_name
);
180 if (filter_cloexec
>= 0) {
183 /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have
184 * been passed in can be collected and fds which have been created locally can be
185 * ignored, under the assumption that only the latter have O_CLOEXEC set. */
186 fl
= fcntl(fd
, F_GETFD
);
190 if (FLAGS_SET(fl
, FD_CLOEXEC
) != !!filter_cloexec
)
194 r
= fdset_put(s
, fd
);
203 int fdset_cloexec(FDSet
*fds
, bool b
) {
209 SET_FOREACH(p
, MAKE_SET(fds
)) {
210 r
= fd_cloexec(PTR_TO_FD(p
), b
);
218 int fdset_new_listen_fds(FDSet
**ret
, bool unset
) {
219 _cleanup_(fdset_shallow_freep
) FDSet
*s
= NULL
;
224 /* Creates an fdset and fills in all passed file descriptors */
230 n
= sd_listen_fds(unset
);
231 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
232 r
= fdset_put(s
, fd
);
241 int fdset_to_array(FDSet
*fds
, int **ret
) {
249 if (m
> INT_MAX
) /* We want to be able to return an "int" */
252 *ret
= NULL
; /* suppress array allocation if empty */
260 SET_FOREACH(e
, MAKE_SET(fds
))
261 a
[j
++] = PTR_TO_FD(e
);
269 int fdset_close_others(FDSet
*fds
) {
270 _cleanup_free_
int *a
= NULL
;
273 n
= fdset_to_array(fds
, &a
);
277 return close_all_fds(a
, n
);
280 unsigned fdset_size(FDSet
*fds
) {
281 return set_size(MAKE_SET(fds
));
284 bool fdset_isempty(FDSet
*fds
) {
285 return set_isempty(MAKE_SET(fds
));
288 int fdset_iterate(FDSet
*s
, Iterator
*i
) {
291 if (!set_iterate(MAKE_SET(s
), i
, &p
))
297 int fdset_steal_first(FDSet
*fds
) {
300 p
= set_steal_first(MAKE_SET(fds
));