? dhcpcd.diff
? x
Index: control.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/control.c,v
retrieving revision 1.9
diff -u -u -r1.9 control.c
--- control.c	16 May 2015 23:31:32 -0000	1.9
+++ control.c	3 Aug 2015 06:51:12 -0000
@@ -46,6 +46,7 @@
 #include "dhcpcd.h"
 #include "control.h"
 #include "eloop.h"
+#include "if.h"
 
 #ifndef SUN_LEN
 #define SUN_LEN(su) \
@@ -210,28 +211,8 @@
 {
 	int fd;
 
-#ifdef SOCK_CLOEXEC
-	if ((fd = socket(AF_UNIX,
-	    SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
+	if ((fd = xsocket(AF_UNIX, SOCK_STREAM, 0, O_NONBLOCK|O_CLOEXEC)) == -1)
 		return -1;
-#else
-	int flags;
-
-	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
-		return -1;
-	if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
-	    fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
-	{
-		close(fd);
-	        return -1;
-	}
-	if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
-	    fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
-	{
-		close(fd);
-	        return -1;
-	}
-#endif
 	memset(sa, 0, sizeof(*sa));
 	sa->sun_family = AF_UNIX;
 	if (unpriv)
Index: dhcp.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/dhcp.c,v
retrieving revision 1.32
diff -u -u -r1.32 dhcp.c
--- dhcp.c	9 Jul 2015 10:15:34 -0000	1.32
+++ dhcp.c	3 Aug 2015 06:51:12 -0000
@@ -1483,19 +1483,8 @@
 	char *p;
 #endif
 
-#ifdef SOCK_CLOEXEC
-	if ((s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP, O_CLOEXEC)) == -1)
 		return -1;
-#else
-	if ((s = socket(PF_INET, 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)
Index: dhcp6.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/dhcp6.c,v
retrieving revision 1.14
diff -u -u -r1.14 dhcp6.c
--- dhcp6.c	9 Jul 2015 10:15:34 -0000	1.14
+++ dhcp6.c	3 Aug 2015 06:51:12 -0000
@@ -43,6 +43,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #define ELOOP_QUEUE 4
 #include "config.h"
@@ -3073,30 +3074,10 @@
 #endif
 
 	ctx = dctx->ipv6;
-#ifdef SOCK_CLOEXEC
-	ctx->dhcp_fd = socket(PF_INET6,
-	    SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
-	    IPPROTO_UDP);
+	ctx->dhcp_fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP,
+	    O_NONBLOCK|O_CLOEXEC);
 	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: if-bsd.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/if-bsd.c,v
retrieving revision 1.22
diff -u -u -r1.22 if-bsd.c
--- if-bsd.c	9 Jul 2015 10:15:34 -0000	1.22
+++ if-bsd.c	3 Aug 2015 06:51:12 -0000
@@ -127,27 +127,7 @@
 if_openlinksocket(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;
-	}
-	return s;
-#endif
+	return xsocket(PF_ROUTE, SOCK_RAW, 0, O_NONBLOCK|O_CLOEXEC);
 }
 
 #if defined(INET) || defined(INET6)
@@ -175,7 +155,7 @@
 	char nwid[IEEE80211_NWID_LEN + 1];
 #endif
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 
 #if defined(SIOCG80211NWID) /* NetBSD */
@@ -244,7 +224,7 @@
 	int s, r;
 	struct ifmediareq ifmr;
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 	memset(&ifmr, 0, sizeof(ifmr));
 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
@@ -453,7 +433,7 @@
 	int s, r;
 	struct in_aliasreq ifra;
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 
 	memset(&ifra, 0, sizeof(ifra));
@@ -551,7 +531,7 @@
 	size_t l;
 	int s, retval;
 
-	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
+	if ((s = xsocket(PF_ROUTE, SOCK_RAW, 0, O_CLOEXEC)) == -1)
 		return -1;
 
 #define ADDSU {								      \
@@ -717,7 +697,7 @@
 	struct ifreq ifr;
 	struct sockaddr_in *sin;
 
-	s = socket(PF_INET, SOCK_DGRAM, 0);
+	s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC);
 	flags = -1;
 	if (s != -1) {
 		memset(&ifr, 0, sizeof(ifr));
@@ -781,7 +761,7 @@
 	struct in6_aliasreq ifa;
 	struct in6_addr mask;
 
-	if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET6, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 
 	memset(&ifa, 0, sizeof(ifa));
@@ -939,7 +919,7 @@
 	size_t l;
 	int s, retval;
 
-	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
+	if ((s = xsocket(PF_ROUTE, SOCK_RAW, 0, O_CLOEXEC)) == -1)
 		return -1;
 
 #define ADDSU {								      \
@@ -1073,18 +1053,19 @@
 	int s, flags;
 	struct in6_ifreq ifr6;
 
-	s = socket(PF_INET6, SOCK_DGRAM, 0);
-	flags = -1;
-	if (s != -1) {
-		memset(&ifr6, 0, sizeof(ifr6));
-		strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
-		ifr6.ifr_addr.sin6_family = AF_INET6;
-		ifr6.ifr_addr.sin6_addr = *addr;
-		ifa_scope(&ifr6.ifr_addr, ifp->index);
-		if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) != -1)
-			flags = ifr6.ifr_ifru.ifru_flags6;
-		close(s);
-	}
+	s = xsocket(PF_INET6, SOCK_DGRAM, 0, O_CLOEXEC);
+	if (s == -1)
+		return -1;
+	memset(&ifr6, 0, sizeof(ifr6));
+	strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
+	ifr6.ifr_addr.sin6_family = AF_INET6;
+	ifr6.ifr_addr.sin6_addr = *addr;
+	ifa_scope(&ifr6.ifr_addr, ifp->index);
+	if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) != -1)
+		flags = ifr6.ifr_ifru.ifru_flags6;
+	else
+		flags = -1;
+	close(s);
 	return flags;
 }
 
@@ -1094,42 +1075,41 @@
 	int s, r;
 	struct in6_ifreq ifr6;
 
-	s = socket(PF_INET6, SOCK_DGRAM, 0);
+	s = xsocket(PF_INET6, SOCK_DGRAM, 0, O_CLOEXEC);
+	if (s == -1)
+		return -1;
 	r = -1;
-	if (s != -1) {
-		memset(&ifr6, 0, sizeof(ifr6));
-		strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
-		ifr6.ifr_addr.sin6_family = AF_INET6;
-		ifr6.ifr_addr.sin6_addr = ia->addr;
-		ifa_scope(&ifr6.ifr_addr, ia->iface->index);
-		if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) != -1) {
-			time_t t;
-			struct in6_addrlifetime *lifetime;
-
-			t = time(NULL);
-			lifetime = &ifr6.ifr_ifru.ifru_lifetime;
-
-			if (lifetime->ia6t_preferred)
-				ia->prefix_pltime =
-				    (uint32_t)(lifetime->ia6t_preferred -
-				    MIN(t, lifetime->ia6t_preferred));
-			else
-				ia->prefix_pltime = ND6_INFINITE_LIFETIME;
-			if (lifetime->ia6t_expire) {
-				ia->prefix_vltime =
-				    (uint32_t)(lifetime->ia6t_expire -
-				    MIN(t, lifetime->ia6t_expire));
-				/* Calculate the created time */
-				clock_gettime(CLOCK_MONOTONIC, &ia->created);
-				ia->created.tv_sec -=
-				    lifetime->ia6t_vltime - ia->prefix_vltime;
-			} else
-				ia->prefix_vltime = ND6_INFINITE_LIFETIME;
+	memset(&ifr6, 0, sizeof(ifr6));
+	strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
+	ifr6.ifr_addr.sin6_family = AF_INET6;
+	ifr6.ifr_addr.sin6_addr = ia->addr;
+	ifa_scope(&ifr6.ifr_addr, ia->iface->index);
+	if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1)
+		goto out;
+
+	time_t t;
+	struct in6_addrlifetime *lifetime;
+
+	t = time(NULL);
+	lifetime = &ifr6.ifr_ifru.ifru_lifetime;
+
+	if (lifetime->ia6t_preferred)
+		ia->prefix_pltime = (uint32_t)(lifetime->ia6t_preferred -
+		    MIN(t, lifetime->ia6t_preferred));
+	else
+		ia->prefix_pltime = ND6_INFINITE_LIFETIME;
+	if (lifetime->ia6t_expire) {
+		ia->prefix_vltime = (uint32_t)(lifetime->ia6t_expire -
+		    MIN(t, lifetime->ia6t_expire));
+		/* Calculate the created time */
+		clock_gettime(CLOCK_MONOTONIC, &ia->created);
+		ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime;
+	} else
+		ia->prefix_vltime = ND6_INFINITE_LIFETIME;
 
-			r = 0;
-		}
-		close(s);
-	}
+	r = 0;
+out:
+	close(s);
 	return r;
 }
 #endif
@@ -1672,7 +1652,7 @@
 {
 	int s, r;
 
-	if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET6, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 	r = _if_checkipv6(s, ctx, ifp, own);
 	close(s);
Index: if.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/if.c,v
retrieving revision 1.14
diff -u -u -r1.14 if.c
--- if.c	9 Jul 2015 10:15:34 -0000	1.14
+++ if.c	3 Aug 2015 06:51:12 -0000
@@ -61,6 +61,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "config.h"
 #include "common.h"
@@ -103,7 +104,7 @@
 	struct ifmediareq ifmr;
 #endif
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return LINK_UNKNOWN;
 	memset(&ifr, 0, sizeof(ifr));
 	strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
@@ -134,7 +135,7 @@
 	struct ifreq ifr;
 	int s, r;
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 	memset(&ifr, 0, sizeof(ifr));
 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
@@ -249,11 +250,11 @@
 #endif
 
 #ifdef SIOCGIFPRIORITY
-	if ((s_inet = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s_inet = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return NULL;
 #endif
 #ifdef IFLR_ACTIVE
-	if ((s_link = socket(PF_LINK, SOCK_DGRAM, 0)) == -1) {
+	if ((s_link = xsocket(PF_LINK, SOCK_DGRAM, 0, O_CLOEXEC)) == -1) {
 #ifdef SIOCGIFPRIORITY
 		close(s_inet);
 #endif
@@ -586,7 +587,7 @@
 	int s, r;
 	struct ifreq ifr;
 
-	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
+	if ((s = xsocket(PF_INET, SOCK_DGRAM, 0, O_CLOEXEC)) == -1)
 		return -1;
 	memset(&ifr, 0, sizeof(ifr));
 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
@@ -667,3 +668,31 @@
 	}
 	TAILQ_CONCAT(ctx->ifaces, &sorted, next);
 }
+
+int
+xsocket(int domain, int type, int protocol, int flags)
+{
+#ifdef SOCK_CLOEXEC
+	if (flags & O_CLOEXEC)
+		type |= SOCK_CLOEXEC;
+	if (flags & O_NONBLOCK)
+		type |= SOCK_NONBLOCK;
+
+	return socket(domain, type, protocol);
+#else
+	int s, xflags;
+
+	if ((s = socket(domain, type, protocol)) == -1)
+		return -1;
+	if ((flags & O_CLOEXEC) && (xflags = fcntl(s, F_GETFD, 0)) == -1 ||
+	    fcntl(s, F_SETFD, xlags | FD_CLOEXEC) == -1)
+		goto out;
+	if ((flags & O_NONBLOCK) && (xflags = fcntl(s, F_GETFL, 0)) == -1 ||
+	    fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1)
+		goto out;
+	return s;
+out:
+	close(s);
+	return -1;
+#endif
+}
Index: if.h
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/if.h,v
retrieving revision 1.11
diff -u -u -r1.11 if.h
--- if.h	9 Jul 2015 10:15:34 -0000	1.11
+++ if.h	3 Aug 2015 06:51:12 -0000
@@ -163,4 +163,5 @@
 #endif
 
 int if_machinearch(char *, size_t);
+int xsocket(int, int, int, int);
 #endif
Index: ipv6nd.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/ipv6nd.c,v
retrieving revision 1.25
diff -u -u -r1.25 ipv6nd.c
--- ipv6nd.c	9 Jul 2015 10:15:34 -0000	1.25
+++ ipv6nd.c	3 Aug 2015 06:51:12 -0000
@@ -192,29 +192,10 @@
 	ctx = dctx->ipv6;
 	if (ctx->nd_fd != -1)
 		return ctx->nd_fd;
-#ifdef SOCK_CLOEXEC
-	ctx->nd_fd = socket(PF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
-	    IPPROTO_ICMPV6);
+	ctx->nd_fd = xsocket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6,
+	    O_NONBLOCK|O_CLOEXEC);
 	if (ctx->nd_fd == -1)
 		return -1;
-#else
-	if ((ctx->nd_fd = socket(PF_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
 
 	/* RFC4861 4.1 */
 	on = 255;