Factor out closing all file descriptors for later optimisation.

This commit is contained in:
Simon Kelley
2020-03-02 17:10:25 +00:00
parent c992ed4bef
commit 0541a1adf7
4 changed files with 19 additions and 16 deletions

View File

@@ -138,20 +138,18 @@ int main (int argc, char **argv)
}
#endif
/* Close any file descriptors we inherited apart from std{in|out|err}
Ensure that at least stdin, stdout and stderr (fd 0, 1, 2) exist,
/* Ensure that at least stdin, stdout and stderr (fd 0, 1, 2) exist,
otherwise file descriptors we create can end up being 0, 1, or 2
and then get accidentally closed later when we make 0, 1, and 2
open to /dev/null. Normally we'll be started with 0, 1 and 2 open,
but it's not guaranteed. By opening /dev/null three times, we
ensure that we're not using those fds for real stuff. */
for (i = 0; i < max_fd; i++)
if (i != STDOUT_FILENO && i != STDERR_FILENO && i != STDIN_FILENO)
close(i);
else
open("/dev/null", O_RDWR);
for (i = 0; i < 3; i++)
open("/dev/null", O_RDWR);
/* Close any file descriptors we inherited apart from std{in|out|err} */
close_fds(max_fd, -1, -1, -1);
#ifndef HAVE_LINUX_NETWORK
# if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR))
if (!option_bool(OPT_NOWILD))