]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
BSD: Don't keep PF LINK socket open needlessly
authorRoy Marples <roy@marples.name>
Tue, 19 Feb 2019 08:07:37 +0000 (08:07 +0000)
committerRoy Marples <roy@marples.name>
Tue, 19 Feb 2019 08:07:37 +0000 (08:07 +0000)
Saves a tiny bit of resource when dhcpcd is running.

src/dhcpcd.c
src/dhcpcd.h
src/if.c

index 6d86baa460c218968d1abedc793e750b3de812f2..384107a80c283d1d1009909f9d4f55ee8be86abd 100644 (file)
@@ -1607,9 +1607,6 @@ main(int argc, char **argv)
        ctx.cffile = CONFIG;
        ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
        ctx.pf_inet_fd = -1;
-#ifdef IFLR_ACTIVE
-       ctx.pf_link_fd = -1;
-#endif
 
        TAILQ_INIT(&ctx.control_fds);
 #ifdef PLUGIN_DEV
index b135f1b7b8d57c41c1f3ee284d35cf5e38dbf0ec..02b830c0d1b3c3d5aaa4a87a3077b304e6812796 100644 (file)
@@ -142,9 +142,6 @@ struct dhcpcd_ctx {
        struct rt_head froutes; /* free routes for re-use */
 
        int pf_inet_fd;
-#ifdef IFLR_ACTIVE
-       int pf_link_fd;
-#endif
        void *priv;
        int link_fd;
        int seq;        /* route message sequence no */
index f6a7bb1eaa786ded8d07e7553c065dadfc23463f..42b3e7b3b3b16326c8ac6c8c651a87681ab22974 100644 (file)
--- a/src/if.c
+++ b/src/if.c
@@ -109,12 +109,6 @@ if_opensockets(struct dhcpcd_ctx *ctx)
        if (ctx->pf_inet_fd == -1)
                return -1;
 
-#ifdef IFLR_ACTIVE
-       ctx->pf_link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-       if (ctx->pf_link_fd == -1)
-               return -1;
-#endif
-
        return 0;
 }
 
@@ -124,10 +118,6 @@ if_closesockets(struct dhcpcd_ctx *ctx)
 
        if (ctx->pf_inet_fd != -1)
                close(ctx->pf_inet_fd);
-#ifdef IFLR_ACTIVE
-       if (ctx->pf_link_fd != -1)
-               close(ctx->pf_link_fd);
-#endif
 
        if (ctx->priv) {
                if_closesockets_os(ctx);
@@ -342,12 +332,10 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
        struct ifreq ifr;
 #endif
 #ifdef IFLR_ACTIVE
-       struct if_laddrreq iflr;
+       struct if_laddrreq iflr = { .flags = IFLR_PREFIX };
+       int link_fd;
 #endif
 
-#ifdef IFLR_ACTIVE
-       memset(&iflr, 0, sizeof(iflr));
-#endif
 #elif AF_PACKET
        const struct sockaddr_ll *sll;
 #endif
@@ -356,11 +344,21 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
                logerr(__func__);
                return NULL;
        }
-       TAILQ_INIT(ifs);
        if (getifaddrs(ifaddrs) == -1) {
                logerr(__func__);
-               goto out;
+               free(ifs);
+               return NULL;
        }
+       TAILQ_INIT(ifs);
+
+#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) {
@@ -457,7 +455,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(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 ||
+                       if (ioctl(link_fd, SIOCGLIFADDR, &iflr) == -1 ||
                            !(iflr.flags & IFLR_ACTIVE))
                        {
                                if_free(ifp);
@@ -610,7 +608,9 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
                TAILQ_INSERT_TAIL(ifs, ifp, next);
        }
 
-out:
+#ifdef IFLR_ACTIVE
+       close(link_fd);
+#endif
        return ifs;
 }