fdset.c revision 3cc2aff1abff9e34f9fec282d970204dc1eab6f1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include "set.h"
#include "util.h"
#include "macro.h"
#include "fdset.h"
#include "sd-daemon.h"
#define MAKE_FDSET(s) ((FDSet*) s)
/* Make sure we can distinguish fd 0 and NULL */
}
unsigned i;
FDSet *s;
int r;
s = fdset_new();
if (!s)
return -ENOMEM;
for (i = 0; i < n_fds; i++) {
if (r < 0) {
return r;
}
}
*ret = s;
return 0;
}
void *p;
while ((p = set_steal_first(MAKE_SET(s)))) {
/* Valgrind's fd might have ended up in this set here,
* due to fdset_new_fill(). We'll ignore all failures
* here, so that the EBADFD that valgrind will return
* us on close() doesn't influence us */
/* When reloading duplicates of the private bus
* connection fds and suchlike are closed here, which
* has no effect at all, since they are only
* duplicates. So don't be surprised about these log
* messages. */
close_nointr(PTR_TO_FD(p));
}
return NULL;
}
assert(s);
}
int r;
assert(s);
if (r <= 0)
safe_close(fd);
return r;
}
int copy, r;
assert(s);
if (copy < 0)
return -errno;
if (r < 0) {
return r;
}
return copy;
}
assert(s);
}
assert(s);
}
int r = 0;
FDSet *s;
/* Creates an fdset and fills in all currently open file
* descriptors. */
if (!d)
return -errno;
s = fdset_new();
if (!s) {
r = -ENOMEM;
goto finish;
}
int fd = -1;
continue;
if (r < 0)
goto finish;
if (fd < 3)
continue;
continue;
if (r < 0)
goto finish;
}
r = 0;
*_s = s;
s = NULL;
/* We won't close the fds here! */
if (s)
return r;
}
Iterator i;
void *p;
int r;
r = fd_cloexec(PTR_TO_FD(p), b);
if (r < 0)
return r;
}
return 0;
}
int n, fd, r;
FDSet *s;
/* Creates an fdset and fills in all passed file descriptors */
s = fdset_new();
if (!s) {
r = -ENOMEM;
goto fail;
}
n = sd_listen_fds(unset);
if (r < 0)
goto fail;
}
*_s = s;
return 0;
fail:
if (s)
return r;
}
void *e;
Iterator i;
int *a;
unsigned j, m;
j = 0, m = fdset_size(fds);
a = alloca(sizeof(int) * m);
a[j++] = PTR_TO_FD(e);
assert(j == m);
return close_all_fds(a, j);
}
}
}
void *p;
if (!set_iterate(MAKE_SET(s), i, &p))
return -ENOENT;
return PTR_TO_FD(p);
}
void *p;
if (!p)
return -ENOENT;
return PTR_TO_FD(p);
}