Very good, that makes things easier for Rob.

> getifaddrs(3) can already give us access to the link state of the
> interface, no need for an additional ioctl(2) call.
> 
> Bonus: getifaddrs(3) and its underlying syscalls can be allowed by
> pledge(2), this is not the case for SIOCGIFDATA.
> 
> Diff tested by Rob Pierce, regress tests pass.  The extra log_debug()
> call isn't intended for commit.
> 
> ok?
> 
> 
> Index: ifstated.c
> ===================================================================
> RCS file: /d/cvs/src/usr.sbin/ifstated/ifstated.c,v
> retrieving revision 1.50
> diff -u -p -p -u -r1.50 ifstated.c
> --- ifstated.c        4 Jul 2017 21:09:52 -0000       1.50
> +++ ifstated.c        21 Jul 2017 13:40:37 -0000
> @@ -600,31 +600,20 @@ void
>  fetch_ifstate(void)
>   {
>       struct ifaddrs *ifap, *ifa;
> -     char *oname = NULL;
> -     int sock = socket(AF_INET, SOCK_DGRAM, 0);
>  
>       if (getifaddrs(&ifap) != 0)
>               err(1, "getifaddrs");
>  
>       for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
> -             struct ifreq ifr;
> -             struct if_data ifrdat;
> -
> -             if (oname && !strcmp(oname, ifa->ifa_name))
> -                     continue;
> -             oname = ifa->ifa_name;
> -
> -             strlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
> -             ifr.ifr_data = (caddr_t)&ifrdat;
> -
> -             if (ioctl(sock, SIOCGIFDATA, (caddr_t)&ifr) == -1)
> -                     continue;
> -
> -             scan_ifstate(if_nametoindex(ifa->ifa_name),
> -                 ifrdat.ifi_link_state, 0);
> +             if (ifa->ifa_addr->sa_family == AF_LINK) {
> +                     struct if_data *ifdata = ifa->ifa_data;
> +                     log_debug("%s: scan_ifstate(%s, %hhu)", __func__,
> +                         ifa->ifa_name, ifdata->ifi_link_state);
> +                     scan_ifstate(if_nametoindex(ifa->ifa_name),
> +                         ifdata->ifi_link_state, 0);
> +             }
>       }
>       freeifaddrs(ifap);
> -     close(sock);
>  }
>  
>  void
> 
> -- 
> jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
> 

Reply via email to