Skip to content

Commit

Permalink
Close inherited file handles
Browse files Browse the repository at this point in the history
  • Loading branch information
neocturne committed May 17, 2012
1 parent f863ed2 commit d8a3a03
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/fastd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <unistd.h>

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d8a3a03

Please sign in to comment.