chiark / gitweb /
fds: Provide cloexec() and use it in udp.c and tun.c
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 22 Sep 2014 14:51:30 +0000 (15:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 2 Oct 2014 01:23:52 +0000 (02:23 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
secnet.h
tun.c
udp.c
util.c

index 194341c434670902e772b58a956fbfbe6fd4b050..ce01d24e31749e1bb8a00db3444f1c5cb449bcab 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -9,6 +9,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <sys/poll.h>
 #include <sys/types.h>
 #include <sys/time.h>
@@ -136,6 +138,8 @@ extern char *safe_strdup(const char *string, const char *message);
 extern void *safe_malloc(size_t size, const char *message);
 extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
 
+void setcloexec(int fd); /* cannot fail */
+
 extern int sys_cmd(const char *file, const char *argc, ...);
 
 extern uint64_t now_global;
diff --git a/tun.c b/tun.c
index d4070f2ef405d7e8316bf23a84837189a05eddef..ffc195ca5a4f3c777db13b5ebe77de94f2f5d5a9 100644 (file)
--- a/tun.c
+++ b/tun.c
@@ -340,6 +340,8 @@ static void tun_phase_hook(void *sst, uint32_t newphase)
        st->interface_name=safe_malloc(10,"tun_apply");
        sprintf(st->interface_name,"tun%d",ppa);
        st->fd=tun_fd;
+       setcloexec(if_ifd);
+       setcloexec(ip_ifd);
 #else
        fatal("tun_phase_hook: TUN_FLAVOUR_STREAMS unexpected");
 #endif /* HAVE_TUN_STREAMS */
@@ -350,6 +352,8 @@ static void tun_phase_hook(void *sst, uint32_t newphase)
        to set the TUN device's address, and route to add routes to all
        our networks. */
 
+    setcloexec(st->fd);
+
     hostaddr=ipaddr_to_string(st->nl.local_address);
     secnetaddr=ipaddr_to_string(st->nl.secnet_address);
     snprintf(mtu,sizeof(mtu),"%d",st->nl.mtu);
diff --git a/udp.c b/udp.c
index 552a58e000d78d372cc5def7de0afa7512785ccb..bb82026c144d0df1f56f9f52e9401dd2fae91691 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -224,10 +224,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
        fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
                     st->loc.file,st->loc.line);
     }
-    if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
-       fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
-                    st->loc.file,st->loc.line);
-    }
+    setcloexec(st->fd);
 
     FILLZERO(addr);
     addr.sin_family=AF_INET;
diff --git a/util.c b/util.c
index 094870ff213cbc41c9f011aa1b316f646ee30992..fc6f6264cf433ba84211858df529b986b377b714 100644 (file)
--- a/util.c
+++ b/util.c
@@ -162,6 +162,13 @@ int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen)
     return i;
 }
 
+void setcloexec(int fd) {
+    int r=fcntl(fd, F_GETFD);
+    if (r<0) fatal_perror("fcntl(,F_GETFD) failed");
+    r=fcntl(fd, F_SETFD, r|FD_CLOEXEC);
+    if (r<0) fatal_perror("fcntl(,F_SETFD,|FD_CLOEXEC) failed");
+}
+
 static const char *phases[NR_PHASES]={
     "PHASE_INIT",
     "PHASE_GETOPTS",