Module Name:    src
Committed By:   ozaki-r
Date:           Tue Aug 20 08:18:24 UTC 2024

Modified Files:
        src/sbin/ifconfig: ifconfig.8 ifconfig.c

Log Message:
ifconfig: show link state on -v

We could guess it through "media" or "status" output, however, we
sometimes want to know it directly for debugging or testing.

It is shown only if the -v option is specified.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sbin/ifconfig/ifconfig.8
cvs rdiff -u -r1.250 -r1.251 src/sbin/ifconfig/ifconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/ifconfig/ifconfig.8
diff -u src/sbin/ifconfig/ifconfig.8:1.129 src/sbin/ifconfig/ifconfig.8:1.130
--- src/sbin/ifconfig/ifconfig.8:1.129	Mon Apr  8 20:28:34 2024
+++ src/sbin/ifconfig/ifconfig.8	Tue Aug 20 08:18:24 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: ifconfig.8,v 1.129 2024/04/08 20:28:34 andvar Exp $
+.\"	$NetBSD: ifconfig.8,v 1.130 2024/08/20 08:18:24 ozaki-r Exp $
 .\"
 .\" Copyright (c) 1983, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)ifconfig.8	8.4 (Berkeley) 6/1/94
 .\"
-.Dd April 8, 2024
+.Dd August 20, 2024
 .Dt IFCONFIG 8
 .Os
 .Sh NAME
@@ -870,8 +870,8 @@ mutually exclusive with all other flags 
 .Pp
 The
 .Fl v
-flag prints statistics on packets sent and received on the given
-interface.
+flag prints additional information like statistics on packets sent and received
+on the interface and the link state of the interface.
 If
 .Fl h
 is used in conjunction with

Index: src/sbin/ifconfig/ifconfig.c
diff -u src/sbin/ifconfig/ifconfig.c:1.250 src/sbin/ifconfig/ifconfig.c:1.251
--- src/sbin/ifconfig/ifconfig.c:1.250	Wed Jan  3 18:10:42 2024
+++ src/sbin/ifconfig/ifconfig.c	Tue Aug 20 08:18:24 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifconfig.c,v 1.250 2024/01/03 18:10:42 andvar Exp $	*/
+/*	$NetBSD: ifconfig.c,v 1.251 2024/08/20 08:18:24 ozaki-r 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.250 2024/01/03 18:10:42 andvar Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.251 2024/08/20 08:18:24 ozaki-r Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -111,6 +111,8 @@ static long wflag_secs, Wflag_secs;
 static char gflags[10 + 26 * 2 + 1] = "AabCdhlNsuvW:w:z";
 bool gflagset[10 + 26 * 2];
 
+static int link_state(prop_dictionary_t);
+static const char *link_state_str(int);
 static int carrier(prop_dictionary_t);
 static int clone_command(prop_dictionary_t, prop_dictionary_t);
 static void do_setifpreference(prop_dictionary_t);
@@ -884,7 +886,7 @@ printall(const char *ifname, prop_dictio
 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
 			continue;
 
-		if (sflag && carrier(env) == LINK_STATE_DOWN)
+		if (sflag && link_state(env) == LINK_STATE_DOWN)
 			continue;
 		idx++;
 		/*
@@ -1199,17 +1201,43 @@ setifmtu(prop_dictionary_t env, prop_dic
 }
 
 static int
-carrier(prop_dictionary_t env)
+link_state(prop_dictionary_t env)
 {
 	struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
 
 	if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
-		return EXIT_FAILURE;
+		return -1;
+
+	return ifdr.ifdr_data.ifi_link_state;
+}
+
+static const char *
+link_state_str(int state)
+{
 
-	if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_DOWN)
+	switch (state) {
+	case LINK_STATE_UNKNOWN:
+		return "unknown";
+	case LINK_STATE_DOWN:
+		return "down";
+	case LINK_STATE_UP:
+		return "up";
+	default: /* Assume -1 */
+		return "error";
+	}
+}
+
+static int
+carrier(prop_dictionary_t env)
+{
+	switch (link_state(env)) {
+	case -1:
+	case LINK_STATE_DOWN:
 		return EXIT_FAILURE;
-	else /* Assume UP if UNKNOWN */
+	default:
+		/* Assume UP if UNKNOWN */
 		return EXIT_SUCCESS;
+	}
 }
 
 static void
@@ -1333,6 +1361,9 @@ status(prop_dictionary_t env, prop_dicti
 		free(p);
 	}
 
+	if (vflag)
+		printf("\tlinkstate: %s\n", link_state_str(link_state(env)));
+
 	media_status(env, oenv);
 
 	if (!vflag && !zflag)

Reply via email to