From d8a3a034a13aaabe5877c6b3cdf73e0582e6425f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 17 May 2012 21:30:10 +0200 Subject: Close inherited file handles --- src/fastd.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/fastd.c b/src/fastd.c index 24faf83..0ea9e26 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -614,10 +615,34 @@ static void maintenance(fastd_context *ctx) { } +static void close_fds(fastd_context *ctx) { + struct rlimit rl; + int fd, maxfd; + + if (getrlimit(RLIMIT_NOFILE, &rl) > 0) + maxfd = (int)rl.rlim_max; + else + maxfd = sysconf(_SC_OPEN_MAX); + + for (fd = 3; fd < maxfd; fd++) { + if (close(fd) < 0) { + if (errno == EINTR) { + fd--; + continue; + } + + if (errno != EBADF) + pr_error_errno(ctx, "close"); + } + } +} + int main(int argc, char *argv[]) { fastd_context ctx; memset(&ctx, 0, sizeof(ctx)); + close_fds(&ctx); + fastd_random_bytes(&ctx, &ctx.randseed, sizeof(ctx.randseed), false); init_signals(&ctx); -- cgit v1.2.3