From: Roy Marples Date: Fri, 29 Nov 2019 22:07:08 +0000 (+0000) Subject: common: add close_std X-Git-Tag: v9.0.0~193 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=608c60a2ab01efacf941ad313038f024f66107cc;p=thirdparty%2Fdhcpcd.git common: add close_std Closed stdin, stdout and stderr. --- diff --git a/src/common.c b/src/common.c index 8b45a307..6a686b8c 100644 --- a/src/common.c +++ b/src/common.c @@ -30,6 +30,8 @@ #include #include +#include +#include #include #include @@ -41,6 +43,24 @@ /* Most route(4) messages are less than 256 bytes. */ #define IOVEC_BUFSIZ 256 +int +close_std(void) +{ + int fd, error = 0; + + if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) + return -1; + + if (dup2(fd, STDIN_FILENO) == -1) + error = -1; + if (dup2(fd, STDOUT_FILENO) == -1) + error = -1; + if (dup2(fd, STDERR_FILENO) == -1) + error = -1; + close(fd); + return error; +} + const char * hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen) { diff --git a/src/common.h b/src/common.h index 4e8bcdea..44566c25 100644 --- a/src/common.h +++ b/src/common.h @@ -190,6 +190,7 @@ # endif #endif +int close_std(void); void get_line_free(void); extern int clock_monotonic; int get_monotonic(struct timespec *);