From: Roy Marples Date: Wed, 5 Nov 2008 13:09:18 +0000 (+0000) Subject: Save a few bytes by moving close_fds to bind where it's really used. X-Git-Tag: v5.0.0~194 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c93e8528e09c322d105b00711a26cb54f5926422;p=thirdparty%2Fdhcpcd.git Save a few bytes by moving close_fds to bind where it's really used. --- diff --git a/bind.c b/bind.c index f05439b8..b8c4f913 100644 --- a/bind.c +++ b/bind.c @@ -25,6 +25,11 @@ * SUCH DAMAGE. */ +#include +#include +#ifdef BSD +# include +#endif #include #include #include @@ -41,6 +46,10 @@ #include "net.h" #include "signals.h" +#ifndef _PATH_DEVNULL +# define _PATH_DEVNULL "/dev/null" +#endif + #ifndef THERE_IS_NO_FORK pid_t daemonise(void) @@ -49,7 +58,7 @@ daemonise(void) sigset_t full; sigset_t old; char buf = '\0'; - int sidpipe[2]; + int sidpipe[2], fd; if (options & DHCPCD_DAEMONISED || !(options & DHCPCD_DAEMONISE)) return 0; @@ -72,7 +81,7 @@ daemonise(void) close(sidpipe[0]); write(sidpipe[1], &buf, 1); close(sidpipe[1]); - close_fds(); + break; default: signal_reset(); @@ -80,6 +89,13 @@ daemonise(void) close(sidpipe[1]); read(sidpipe[0], &buf, 1); close(sidpipe[0]); + if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + if (fd > STDERR_FILENO) + close(fd); + } break; } /* Done with the fd now */ diff --git a/common.c b/common.c index 47082cbb..bfd0b9c7 100644 --- a/common.c +++ b/common.c @@ -138,23 +138,6 @@ closefrom(int fd) } #endif -/* Close our fd's */ -int -close_fds(void) -{ - int fd; - - if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) - return -1; - - dup2(fd, fileno(stdin)); - dup2(fd, fileno(stdout)); - dup2(fd, fileno(stderr)); - if (fd > 2) - close(fd); - return 0; -} - int set_cloexec(int fd) { diff --git a/common.h b/common.h index 1685c19f..453d6b77 100644 --- a/common.h +++ b/common.h @@ -84,7 +84,6 @@ size_t strlcpy(char *, const char *, size_t); int closefrom(int); #endif -int close_fds(void); int set_cloexec(int); int set_nonblock(int); ssize_t get_line(char **, size_t *, FILE *);