From: Sage Weil Date: Fri, 7 Jul 2017 12:35:56 +0000 (-0400) Subject: common/fork_function: close all fds in children X-Git-Tag: v12.1.1~110^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=55f76d5ad89b9a8c2a4a7d25b4f97c15d7e7d148;p=ceph.git common/fork_function: close all fds in children Not strictly necessary, but a tidier. Signed-off-by: Sage Weil --- diff --git a/src/common/fork_function.h b/src/common/fork_function.h index 56b8a3bbf3e5..fa214f788750 100644 --- a/src/common/fork_function.h +++ b/src/common/fork_function.h @@ -46,6 +46,21 @@ static inline int fork_function( } // we are forker (first child) + + // close all fds + int maxfd = sysconf(_SC_OPEN_MAX); + if (maxfd == -1) + maxfd = 16384; + for (int fd = 0; fd <= maxfd; fd++) { + if (fd == STDIN_FILENO) + continue; + if (fd == STDOUT_FILENO) + continue; + if (fd == STDERR_FILENO) + continue; + ::close(fd); + } + sigset_t mask, oldmask; int pid;