]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
common: add close_std
authorRoy Marples <roy@marples.name>
Fri, 29 Nov 2019 22:07:08 +0000 (22:07 +0000)
committerRoy Marples <roy@marples.name>
Fri, 29 Nov 2019 22:07:08 +0000 (22:07 +0000)
Closed stdin, stdout and stderr.

src/common.c
src/common.h

index 8b45a30709c586c6a6a969c262a86dceee01f02e..6a686b8c46bfc5a1791e9a1514d254b7582aa522 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 /* 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)
 {
index 4e8bcdead667a67b1a5ef7fd091d035837996533..44566c25fe09ccb75eb610e1406db1b12bb93347 100644 (file)
 # endif
 #endif
 
+int close_std(void);
 void get_line_free(void);
 extern int clock_monotonic;
 int get_monotonic(struct timespec *);