]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
e97a52619dd339988b42813ec1222a9956017e30
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
13 #include "sd-daemon.h"
15 #include "alloc-util.h"
16 #include "dirent-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
25 #define MAKE_SET(s) ((Set*) s)
26 #define MAKE_FDSET(s) ((FDSet*) s)
28 FDSet
*fdset_new(void) {
29 return MAKE_FDSET(set_new(NULL
));
32 int fdset_new_array(FDSet
**ret
, const int *fds
, unsigned n_fds
) {
43 for (i
= 0; i
< n_fds
; i
++) {
45 r
= fdset_put(s
, fds
[i
]);
47 set_free(MAKE_SET(s
));
56 FDSet
* fdset_free(FDSet
*s
) {
59 while ((p
= set_steal_first(MAKE_SET(s
)))) {
60 /* Valgrind's fd might have ended up in this set here,
61 * due to fdset_new_fill(). We'll ignore all failures
62 * here, so that the EBADFD that valgrind will return
63 * us on close() doesn't influence us */
65 /* When reloading duplicates of the private bus
66 * connection fds and suchlike are closed here, which
67 * has no effect at all, since they are only
68 * duplicates. So don't be surprised about these log
71 log_debug("Closing left-over fd %i", PTR_TO_FD(p
));
72 close_nointr(PTR_TO_FD(p
));
75 set_free(MAKE_SET(s
));
79 int fdset_put(FDSet
*s
, int fd
) {
83 return set_put(MAKE_SET(s
), FD_TO_PTR(fd
));
86 int fdset_put_dup(FDSet
*s
, int fd
) {
92 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
96 r
= fdset_put(s
, copy
);
105 bool fdset_contains(FDSet
*s
, int fd
) {
109 return !!set_get(MAKE_SET(s
), FD_TO_PTR(fd
));
112 int fdset_remove(FDSet
*s
, int fd
) {
116 return set_remove(MAKE_SET(s
), FD_TO_PTR(fd
)) ? fd
: -ENOENT
;
119 int fdset_new_fill(FDSet
**_s
) {
120 _cleanup_closedir_
DIR *d
= NULL
;
127 /* Creates an fdset and fills in all currently open file
130 d
= opendir("/proc/self/fd");
140 FOREACH_DIRENT(de
, d
, return -errno
) {
143 r
= safe_atoi(de
->d_name
, &fd
);
153 r
= fdset_put(s
, fd
);
162 /* We won't close the fds here! */
164 set_free(MAKE_SET(s
));
169 int fdset_cloexec(FDSet
*fds
, bool b
) {
176 SET_FOREACH(p
, MAKE_SET(fds
), i
) {
177 r
= fd_cloexec(PTR_TO_FD(p
), b
);
185 int fdset_new_listen_fds(FDSet
**_s
, bool unset
) {
191 /* Creates an fdset and fills in all passed file descriptors */
199 n
= sd_listen_fds(unset
);
200 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
201 r
= fdset_put(s
, fd
);
211 set_free(MAKE_SET(s
));
216 int fdset_close_others(FDSet
*fds
) {
222 j
= 0, m
= fdset_size(fds
);
223 a
= alloca(sizeof(int) * m
);
224 SET_FOREACH(e
, MAKE_SET(fds
), i
)
225 a
[j
++] = PTR_TO_FD(e
);
229 return close_all_fds(a
, j
);
232 unsigned fdset_size(FDSet
*fds
) {
233 return set_size(MAKE_SET(fds
));
236 bool fdset_isempty(FDSet
*fds
) {
237 return set_isempty(MAKE_SET(fds
));
240 int fdset_iterate(FDSet
*s
, Iterator
*i
) {
243 if (!set_iterate(MAKE_SET(s
), i
, &p
))
249 int fdset_steal_first(FDSet
*fds
) {
252 p
= set_steal_first(MAKE_SET(fds
));