]>
git.proxmox.com Git - systemd.git/blob - src/basic/fdset.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "sd-daemon.h"
32 #include "parse-util.h"
33 #include "path-util.h"
36 #define MAKE_SET(s) ((Set*) s)
37 #define MAKE_FDSET(s) ((FDSet*) s)
39 FDSet
*fdset_new(void) {
40 return MAKE_FDSET(set_new(NULL
));
43 int fdset_new_array(FDSet
**ret
, const int *fds
, unsigned n_fds
) {
54 for (i
= 0; i
< n_fds
; i
++) {
56 r
= fdset_put(s
, fds
[i
]);
58 set_free(MAKE_SET(s
));
67 FDSet
* fdset_free(FDSet
*s
) {
70 while ((p
= set_steal_first(MAKE_SET(s
)))) {
71 /* Valgrind's fd might have ended up in this set here,
72 * due to fdset_new_fill(). We'll ignore all failures
73 * here, so that the EBADFD that valgrind will return
74 * us on close() doesn't influence us */
76 /* When reloading duplicates of the private bus
77 * connection fds and suchlike are closed here, which
78 * has no effect at all, since they are only
79 * duplicates. So don't be surprised about these log
82 log_debug("Closing left-over fd %i", PTR_TO_FD(p
));
83 close_nointr(PTR_TO_FD(p
));
86 set_free(MAKE_SET(s
));
90 int fdset_put(FDSet
*s
, int fd
) {
94 return set_put(MAKE_SET(s
), FD_TO_PTR(fd
));
97 int fdset_consume(FDSet
*s
, int fd
) {
103 r
= fdset_put(s
, fd
);
110 int fdset_put_dup(FDSet
*s
, int fd
) {
116 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
120 r
= fdset_put(s
, copy
);
129 bool fdset_contains(FDSet
*s
, int fd
) {
133 return !!set_get(MAKE_SET(s
), FD_TO_PTR(fd
));
136 int fdset_remove(FDSet
*s
, int fd
) {
140 return set_remove(MAKE_SET(s
), FD_TO_PTR(fd
)) ? fd
: -ENOENT
;
143 int fdset_new_fill(FDSet
**_s
) {
144 _cleanup_closedir_
DIR *d
= NULL
;
151 /* Creates an fdset and fills in all currently open file
154 d
= opendir("/proc/self/fd");
164 while ((de
= readdir(d
))) {
167 if (hidden_file(de
->d_name
))
170 r
= safe_atoi(de
->d_name
, &fd
);
180 r
= fdset_put(s
, fd
);
190 /* We won't close the fds here! */
192 set_free(MAKE_SET(s
));
197 int fdset_cloexec(FDSet
*fds
, bool b
) {
204 SET_FOREACH(p
, MAKE_SET(fds
), i
) {
205 r
= fd_cloexec(PTR_TO_FD(p
), b
);
213 int fdset_new_listen_fds(FDSet
**_s
, bool unset
) {
219 /* Creates an fdset and fills in all passed file descriptors */
227 n
= sd_listen_fds(unset
);
228 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
229 r
= fdset_put(s
, fd
);
240 set_free(MAKE_SET(s
));
245 int fdset_close_others(FDSet
*fds
) {
251 j
= 0, m
= fdset_size(fds
);
252 a
= alloca(sizeof(int) * m
);
253 SET_FOREACH(e
, MAKE_SET(fds
), i
)
254 a
[j
++] = PTR_TO_FD(e
);
258 return close_all_fds(a
, j
);
261 unsigned fdset_size(FDSet
*fds
) {
262 return set_size(MAKE_SET(fds
));
265 bool fdset_isempty(FDSet
*fds
) {
266 return set_isempty(MAKE_SET(fds
));
269 int fdset_iterate(FDSet
*s
, Iterator
*i
) {
272 if (!set_iterate(MAKE_SET(s
), i
, &p
))
278 int fdset_steal_first(FDSet
*fds
) {
281 p
= set_steal_first(MAKE_SET(fds
));