From: Roy Marples Date: Fri, 5 Jun 2020 11:23:51 +0000 (+0100) Subject: if: Keep the PF_LINK socket open throughout X-Git-Tag: v9.1.2~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1257d32e30c37a3552de5114e38cd2fe684c8f3b;p=thirdparty%2Fdhcpcd.git if: Keep the PF_LINK socket open throughout Saves opening it and closing it each time we discover interfaces. --- diff --git a/src/dhcpcd.c b/src/dhcpcd.c index dddf3281..2dbfb4ed 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -1820,6 +1820,9 @@ main(int argc, char **argv) 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 diff --git a/src/dhcpcd.h b/src/dhcpcd.h index f3ce6c4b..21497559 100644 --- a/src/dhcpcd.h +++ b/src/dhcpcd.h @@ -151,6 +151,9 @@ struct dhcpcd_ctx { 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 diff --git a/src/if.c b/src/if.c index acf8dcac..d0012771 100644 --- a/src/if.c +++ b/src/if.c @@ -107,6 +107,12 @@ if_opensockets(struct dhcpcd_ctx *ctx) 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_closesockets(struct dhcpcd_ctx *ctx) 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 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, 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 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, 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 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, 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 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, TAILQ_INSERT_TAIL(ifs, ifp, next); } -#ifdef IFLR_ACTIVE - close(link_fd); -#endif return ifs; }