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 #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 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 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, 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 but it's not guaranteed. By opening /dev/null three times, we
ensure that we're not using those fds for real stuff. */ ensure that we're not using those fds for real stuff. */
for (i = 0; i < max_fd; i++) for (i = 0; i < 3; i++)
if (i != STDOUT_FILENO && i != STDERR_FILENO && i != STDIN_FILENO)
close(i);
else
open("/dev/null", O_RDWR); 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 #ifndef HAVE_LINUX_NETWORK
# if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR)) # if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR))
if (!option_bool(OPT_NOWILD)) if (!option_bool(OPT_NOWILD))

View File

@@ -1283,7 +1283,7 @@ int memcmp_masked(unsigned char *a, unsigned char *b, int len,
int expand_buf(struct iovec *iov, size_t size); int expand_buf(struct iovec *iov, size_t size);
char *print_mac(char *buff, unsigned char *mac, int len); char *print_mac(char *buff, unsigned char *mac, int len);
int read_write(int fd, unsigned char *packet, int size, int rw); int read_write(int fd, unsigned char *packet, int size, int rw);
void close_fds(long max_fd, int spare1, int spare2, int spare3);
int wildcard_match(const char* wildcard, const char* match); int wildcard_match(const char* wildcard, const char* match);
int wildcard_matchn(const char* wildcard, const char* match, int num); int wildcard_matchn(const char* wildcard, const char* match, int num);

View File

@@ -131,11 +131,7 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
Don't close err_fd, in case the lua-init fails. Don't close err_fd, in case the lua-init fails.
Note that we have to do this before lua init Note that we have to do this before lua init
so we don't close any lua fds. */ so we don't close any lua fds. */
for (max_fd--; max_fd >= 0; max_fd--) close_fds(max_fd, pipefd[0], event_fd, err_fd);
if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO &&
max_fd != STDIN_FILENO && max_fd != pipefd[0] &&
max_fd != event_fd && max_fd != err_fd)
close(max_fd);
#ifdef HAVE_LUASCRIPT #ifdef HAVE_LUASCRIPT
if (daemon->luascript) if (daemon->luascript)

View File

@@ -705,6 +705,15 @@ int read_write(int fd, unsigned char *packet, int size, int rw)
return 1; return 1;
} }
/* close all fds except STDIN, STDOUT and STDERR, spare1, spare2 and spare3 */
void close_fds(long max_fd, int spare1, int spare2, int spare3)
{
for (max_fd--; max_fd >= 0; max_fd--)
if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO && max_fd != STDIN_FILENO &&
max_fd != spare1 && max_fd != spare2 && max_fd != spare3)
close(max_fd);
}
/* Basically match a string value against a wildcard pattern. */ /* Basically match a string value against a wildcard pattern. */
int wildcard_match(const char* wildcard, const char* match) int wildcard_match(const char* wildcard, const char* match)
{ {