changeset 5311:fd78486b12a7 draft

if: Keep the PF_LINK socket open throughout Saves opening it and closing it each time we discover interfaces.
author Roy Marples <roy@marples.name>
date Fri, 05 Jun 2020 12:23:51 +0100
parents 0a6bde63868b
children b336a280de82
files src/dhcpcd.c src/dhcpcd.h src/if.c
diffstat 3 files changed, 15 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/dhcpcd.c	Fri Jun 05 11:12:21 2020 +0100
+++ b/src/dhcpcd.c	Fri Jun 05 12:23:51 2020 +0100
@@ -1820,6 +1820,9 @@
 	ctx.script = UNCONST(dhcpcd_default_script);
 	ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
 	ctx.pf_inet_fd = -1;
+#ifdef PF_LINK
+	ctx.pf_link_fd = -1;
+#endif
 
 	TAILQ_INIT(&ctx.control_fds);
 #ifdef USE_SIGNALS
--- a/src/dhcpcd.h	Fri Jun 05 11:12:21 2020 +0100
+++ b/src/dhcpcd.h	Fri Jun 05 12:23:51 2020 +0100
@@ -151,6 +151,9 @@
 	size_t rt_order;	/* route order storage */
 
 	int pf_inet_fd;
+#ifdef PF_LINK
+	int pf_link_fd;
+#endif
 	void *priv;
 	int link_fd;
 #ifndef SMALL
--- a/src/if.c	Fri Jun 05 11:12:21 2020 +0100
+++ b/src/if.c	Fri Jun 05 12:23:51 2020 +0100
@@ -107,6 +107,12 @@
 	if (if_opensockets_os(ctx) == -1)
 		return -1;
 
+#ifdef PF_LINK
+	ctx->pf_link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+	if (ctx->pf_link_fd == -1)
+		return -1;
+#endif
+
 	/* We use this socket for some operations without INET. */
 	ctx->pf_inet_fd = xsocket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
 	if (ctx->pf_inet_fd == -1)
@@ -121,6 +127,8 @@
 
 	if (ctx->pf_inet_fd != -1)
 		close(ctx->pf_inet_fd);
+	if (ctx->pf_link_fd != -1)
+		close(ctx->pf_link_fd);
 
 	if (ctx->priv) {
 		if_closesockets_os(ctx);
@@ -379,7 +387,6 @@
 	const struct sockaddr_dl *sdl;
 #ifdef IFLR_ACTIVE
 	struct if_laddrreq iflr = { .flags = IFLR_PREFIX };
-	int link_fd;
 #endif
 #elif defined(AF_PACKET)
 	const struct sockaddr_ll *sll;
@@ -409,15 +416,6 @@
 		return NULL;
 	}
 
-#ifdef IFLR_ACTIVE
-	link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-	if (link_fd == -1) {
-		logerr(__func__);
-		free(ifs);
-		return NULL;
-	}
-#endif
-
 	for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
 		if (ifa->ifa_addr != NULL) {
 #ifdef AF_LINK
@@ -514,7 +512,7 @@
 			    MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
 			iflr.flags = IFLR_PREFIX;
 			iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY;
-			if (ioctl(link_fd, SIOCGLIFADDR, &iflr) == -1 ||
+			if (ioctl(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 ||
 			    !(iflr.flags & IFLR_ACTIVE))
 			{
 				if_free(ifp);
@@ -648,9 +646,6 @@
 		TAILQ_INSERT_TAIL(ifs, ifp, next);
 	}
 
-#ifdef IFLR_ACTIVE
-	close(link_fd);
-#endif
 	return ifs;
 }