Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/ifconfig ifconfig: Just look at if_data->ifi_link_state...



details:   https://anonhg.NetBSD.org/src/rev/8ab935fde3f5
branches:  trunk
changeset: 955416:8ab935fde3f5
user:      roy <roy%NetBSD.org@localhost>
date:      Mon Sep 28 13:50:22 2020 +0000

description:
ifconfig: Just look at if_data->ifi_link_state for carrier

It should be the sole source of truth.
if_data is also carried in ifa_data from getifaddrs(3) which saves
more ioctl calls.

diffstat:

 sbin/ifconfig/ifconfig.c |  44 +++++++++++++++-----------------------------
 1 files changed, 15 insertions(+), 29 deletions(-)

diffs (97 lines):

diff -r b2fe2669b777 -r 8ab935fde3f5 sbin/ifconfig/ifconfig.c
--- a/sbin/ifconfig/ifconfig.c  Mon Sep 28 12:47:49 2020 +0000
+++ b/sbin/ifconfig/ifconfig.c  Mon Sep 28 13:50:22 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ifconfig.c,v 1.246 2020/09/26 23:43:26 roy Exp $       */
+/*     $NetBSD: ifconfig.c,v 1.247 2020/09/28 13:50:22 roy Exp $       */
 
 /*-
  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: ifconfig.c,v 1.246 2020/09/26 23:43:26 roy Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.247 2020/09/28 13:50:22 roy Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -852,6 +852,7 @@
        struct ifaddrs *ifap, *ifa;
        struct ifreq ifr;
        const struct sockaddr_dl *sdl = NULL;
+       const struct if_data *ifi = NULL;
        prop_dictionary_t env, oenv;
        int idx;
        char *p;
@@ -880,8 +881,10 @@
 
                if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
                        continue;
-               if (ifa->ifa_addr->sa_family == AF_LINK)
+               if (ifa->ifa_addr->sa_family == AF_LINK) {
                        sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
+                       ifi = (const struct if_data *)ifa->ifa_data;
+               }
                if (p && strcmp(p, ifa->ifa_name) == 0)
                        continue;
                if (!prop_dictionary_set_string(env, "if", ifa->ifa_name))
@@ -895,7 +898,8 @@
                if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
                        continue;
 
-               if (sflag && carrier(env))
+               if (sflag && (ifi == NULL ||
+                             ifi->ifi_link_state == LINK_STATE_DOWN))
                        continue;
                idx++;
                /*
@@ -910,6 +914,7 @@
 
                status(sdl, ifa->ifa_data, env, oenv);
                sdl = NULL;
+               ifi = NULL;
        }
        if (lflag)
                printf("\n");
@@ -1213,34 +1218,15 @@
 static int
 carrier(prop_dictionary_t env)
 {
-       struct ifmediareq ifmr = { .ifm_status = 0 };
+       struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
 
-       if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
-               /*
-                * Interface doesn't support SIOC{G,S}IFMEDIA;
-                * check link state.
-                */
-               struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
+       if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
+               return EXIT_FAILURE;
 
-               if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
-                       return EXIT_FAILURE;
-               if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_DOWN)
-                       return EXIT_FAILURE;
-               else
-                       return EXIT_SUCCESS;
-       }
-       if ((ifmr.ifm_status & IFM_AVALID) == 0) {
-               /*
-                * Interface doesn't report media-valid status.
-                * assume ok.
-                */
+       if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_DOWN)
+               return EXIT_FAILURE;
+       else /* Assume UP if UNKNOWN */
                return EXIT_SUCCESS;
-       }
-       /* otherwise, return ok for active, not-ok if not active. */
-       if (ifmr.ifm_status & IFM_ACTIVE)
-               return EXIT_SUCCESS;
-       else
-               return EXIT_FAILURE;
 }
 
 static void



Home | Main Index | Thread Index | Old Index