1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "sd-daemon.h"
33 #define MAKE_SET(s) ((Set*) s)
34 #define MAKE_FDSET(s) ((FDSet*) s)
36 /* Make sure we can distuingish fd 0 and NULL */
37 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
38 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
40 FDSet *fdset_new(void) {
41 return MAKE_FDSET(set_new(NULL));
44 void fdset_free(FDSet *s) {
47 while ((p = set_steal_first(MAKE_SET(s)))) {
48 /* Valgrind's fd might have ended up in this set here,
49 * due to fdset_new_fill(). We'll ignore all failures
50 * here, so that the EBADFD that valgrind will return
51 * us on close() doesn't influence us */
53 /* When reloading duplicates of the private bus
54 * connection fds and suchlike are closed here, which
55 * has no effect at all, since they are only
56 * duplicates. So don't be surprised about these log
59 log_debug("Closing left-over fd %i", PTR_TO_FD(p));
60 close_nointr(PTR_TO_FD(p));
63 set_free(MAKE_SET(s));
66 int fdset_put(FDSet *s, int fd) {
70 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
73 int fdset_put_dup(FDSet *s, int fd) {
79 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
83 r = fdset_put(s, copy);
92 bool fdset_contains(FDSet *s, int fd) {
96 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
99 int fdset_remove(FDSet *s, int fd) {
103 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
106 int fdset_new_fill(FDSet **_s) {
107 _cleanup_closedir_ DIR *d = NULL;
114 /* Creates an fdset and fills in all currently open file
117 d = opendir("/proc/self/fd");
127 while ((de = readdir(d))) {
130 if (ignore_file(de->d_name))
133 r = safe_atoi(de->d_name, &fd);
143 r = fdset_put(s, fd);
153 /* We won't close the fds here! */
155 set_free(MAKE_SET(s));
160 int fdset_cloexec(FDSet *fds, bool b) {
167 SET_FOREACH(p, MAKE_SET(fds), i)
168 if ((r = fd_cloexec(PTR_TO_FD(p), b)) < 0)
174 int fdset_new_listen_fds(FDSet **_s, bool unset) {
180 /* Creates an fdset and fills in all passed file descriptors */
188 n = sd_listen_fds(unset);
189 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
190 r = fdset_put(s, fd);
201 set_free(MAKE_SET(s));
206 int fdset_close_others(FDSet *fds) {
212 j = 0, m = fdset_size(fds);
213 a = alloca(sizeof(int) * m);
214 SET_FOREACH(e, MAKE_SET(fds), i)
215 a[j++] = PTR_TO_FD(e);
219 return close_all_fds(a, j);
222 unsigned fdset_size(FDSet *fds) {
223 return set_size(MAKE_SET(fds));
226 int fdset_iterate(FDSet *s, Iterator *i) {
229 p = set_iterate(MAKE_SET(s), i);