]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Work without SOCK_CLOEXEC again.
authorRoy Marples <roy@marples.name>
Thu, 13 Feb 2014 12:58:58 +0000 (12:58 +0000)
committerRoy Marples <roy@marples.name>
Thu, 13 Feb 2014 12:58:58 +0000 (12:58 +0000)
control.c
dhcp.c
dhcp6.c
if-bsd.c
ipv6nd.c

index 14d07b2c9ff561b882160d3823e3c5d6375eb445..bba78c11314d7cd93ac899bcc7643e89b6f68d89 100644 (file)
--- a/control.c
+++ b/control.c
@@ -133,9 +133,30 @@ static int
 make_sock(struct dhcpcd_ctx *ctx, struct sockaddr_un *sun)
 {
 
+#ifdef SOCK_CLOEXEC
        if ((ctx->control_fd = socket(AF_UNIX,
            SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
                return -1;
+#else
+       int flags;
+
+       if ((ctx->control_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+               return -1;
+       if ((flags = fcntl(ctx->control_fd, F_GETFD, 0)) == -1 ||
+           fcntl(ctx->control_fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+       {
+               close(ctx->control_fd);
+               ctx->control_fd = -1;
+               return -1;
+       }
+       if ((flags = fcntl(ctx->control_fd, F_GETFL, 0)) == -1 ||
+           fcntl(ctx->control_fd, F_SETFL, flags | O_NONBLOCK) == -1)
+       {
+               close(ctx->control_fd);
+               ctx->control_fd = -1;
+               return -1;
+       }
+#endif
        memset(sun, 0, sizeof(*sun));
        sun->sun_family = AF_UNIX;
        strlcpy(sun->sun_path, CONTROLSOCKET, sizeof(sun->sun_path));
diff --git a/dhcp.c b/dhcp.c
index fb642d3998449d87e448bf3f4e116f9772f5b25a..6a10b78119c134590e575e2086206407a2cda48e 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -1360,8 +1360,19 @@ dhcp_openudp(struct dhcpcd_ctx *ctx, struct interface *ifp)
        char *p;
 #endif
 
+#ifdef SOCK_CLOEXEC
        if ((s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
                return -1;
+#else
+       if ((s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+               return -1;
+       if ((n = fcntl(s, F_GETFD, 0)) == -1 ||
+           fcntl(s, F_SETFD, n | FD_CLOEXEC) == -1)
+       {
+               close(s);
+               return -1;
+       }
+#endif
 
        n = 1;
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
diff --git a/dhcp6.c b/dhcp6.c
index 57add6aff7a060ca96ca6b23e3abecc90ae8a578..1db65f8f5567f2ff899fd4c790c6aab11c74016a 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -2447,11 +2447,30 @@ dhcp6_open(struct dhcpcd_ctx *dctx)
 #endif
 
        ctx = dctx->ipv6;
+#ifdef SOCK_CLOEXEC
        ctx->dhcp_fd = socket(PF_INET6,
            SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
            IPPROTO_UDP);
        if (ctx->dhcp_fd == -1)
                return -1;
+#else
+       if ((ctx->dhcp_fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+               return -1;
+       if ((n = fcntl(ctx->dhcp_fd, F_GETFD, 0)) == -1 ||
+           fcntl(ctx->dhcp_fd, F_SETFD, n | FD_CLOEXEC) == -1)
+       {
+               close(ctx->dhcp_fd);
+               ctx->dhcp_fd = -1;
+               return -1;
+       }
+       if ((n = fcntl(ctx->dhcp_fd, F_GETFL, 0)) == -1 ||
+           fcntl(ctx->dhcp_fd, F_SETFL, n | O_NONBLOCK) == -1)
+       {
+               close(ctx->dhcp_fd);
+               ctx->dhcp_fd = -1;
+               return -1;
+       }
+#endif
 
        n = 1;
        if (setsockopt(ctx->dhcp_fd, SOL_SOCKET, SO_REUSEADDR,
index 3af7e95e5ff12a5612d3542caf196ac70f94cbdf..c1102823b0cc863b06b898d690b2cb174fd2a6bf 100644 (file)
--- a/if-bsd.c
+++ b/if-bsd.c
@@ -51,6 +51,7 @@
 #endif
 
 #include <errno.h>
+#include <fcntl.h>
 #include <fnmatch.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -102,7 +103,26 @@ int
 open_link_socket(void)
 {
 
+#ifdef SOCK_CLOEXEC
        return socket(PF_ROUTE, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+#else
+       int s, flags;
+
+       if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
+               return -1;
+       if ((flags = fcntl(s, F_GETFD, 0)) == -1 ||
+           fcntl(s, F_SETFD, flags | FD_CLOEXEC) == -1)
+       {
+               close(s);
+               return -1;
+       }
+       if ((flags = fcntl(s, F_GETFL, 0)) == -1 ||
+           fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
+       {
+               close(s);
+               return -1;
+       }
+#endif
 }
 
 int
index acc2699fe509e0b16f9fb5b86d1100f4a6b2497f..2bcb701d8437ecaf899a6ce5e81d1f0c690bad68 100644 (file)
--- a/ipv6nd.c
+++ b/ipv6nd.c
@@ -39,6 +39,7 @@
 #endif
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -178,10 +179,29 @@ ipv6nd_open(struct dhcpcd_ctx *dctx)
        ctx = dctx->ipv6;
        if (ctx->nd_fd != -1)
                goto unspec;
+#ifdef SOCK_CLOEXEC
        ctx->nd_fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
            IPPROTO_ICMPV6);
        if (ctx->nd_fd == -1)
                return -1;
+#else
+       if ((ctx->nd_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
+               return -1;
+       if ((on = fcntl(ctx->nd_fd, F_GETFD, 0)) == -1 ||
+           fcntl(ctx->nd_fd, F_SETFD, on | FD_CLOEXEC) == -1)
+       {
+               close(ctx->nd_fd);
+               ctx->nd_fd = -1;
+               return -1;
+       }
+       if ((on = fcntl(ctx->nd_fd, F_GETFL, 0)) == -1 ||
+           fcntl(ctx->nd_fd, F_SETFL, on | O_NONBLOCK) == -1)
+       {
+               close(ctx->nd_fd);
+               ctx->nd_fd = -1;
+               return -1;
+       }
+#endif
 
        on = 1;
        if (setsockopt(ctx->nd_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
@@ -210,9 +230,31 @@ unspec:
        ICMP6_FILTER_SETBLOCKALL(&filt);
 
        /* We send DAD requests from the unspecified address. */
-       ctx->unspec_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+#ifdef SOCK_CLOEXEC
+       ctx->unspec_fd = socket(AF_INET6,
+           SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
+           IPPROTO_ICMPV6);
        if (ctx->unspec_fd == -1)
-               goto eexit;
+               return -1;
+#else
+       if ((ctx->unspec_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
+               return -1;
+       if ((on = fcntl(ctx->unspec_fd, F_GETFD, 0)) == -1 ||
+           fcntl(ctx->unspec_fd, F_SETFD, on | FD_CLOEXEC) == -1)
+       {
+               close(ctx->unspec_fd);
+               ctx->unspec_fd = -1;
+               return -1;
+       }
+       if ((on = fcntl(ctx->unspec_fd, F_GETFL, 0)) == -1 ||
+           fcntl(ctx->unspec_fd, F_SETFL, on | O_NONBLOCK) == -1)
+       {
+               close(ctx->unspec_fd);
+               ctx->unspec_fd = -1;
+               return -1;
+       }
+#endif
+       
        if (setsockopt(ctx->unspec_fd, IPPROTO_ICMPV6, ICMP6_FILTER,
            &filt, sizeof(filt)) == -1)
                goto eexit;