git: 4106282ec41d - main - ifconfig: remove global 'printifname' variable.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Tue, 13 Jun 2023 06:27:00 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=4106282ec41d92b98c29e25316d11d93600a8f23

commit 4106282ec41d92b98c29e25316d11d93600a8f23
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-06-13 06:18:05 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-06-13 06:18:10 +0000

    ifconfig: remove global 'printifname' variable.
    
    This variable was used to print the created interface name in the
    atexit(3) handler. The interface name was calculated in the
    ifclonecreate() by matching old & new names.
    
    This change alter the implementation the following way:
    1) the function responsible for the interface creation (ifcreate_ioctl)
     updates all necessary state internally. This removes the need for the
     name manipulation hack in wlan_create().
    2) As atexit(3) handler does not accept any parameters, explicitly store
     the name to print in the ifname_to_print variable read by the atexit(3)
     handler.
    
    Reviewed By: kp
    Differential Revision: https://reviews.freebsd.org/D40431
    MFC after:      2 weeks
---
 sbin/ifconfig/ifclone.c     |  9 ---------
 sbin/ifconfig/ifconfig.c    | 26 +++++++++++++++++++++-----
 sbin/ifconfig/ifconfig.h    |  9 +++++----
 sbin/ifconfig/ifieee80211.c |  7 -------
 4 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c
index 7939ff95a065..f09b91b4181b 100644
--- a/sbin/ifconfig/ifclone.c
+++ b/sbin/ifconfig/ifclone.c
@@ -148,15 +148,6 @@ ifclonecreate(if_ctx *ctx, void *arg __unused)
 	} else {
 		dcp->clone_cb(ctx, &ifr);
 	}
-
-	/*
-	 * If we get a different name back than we put in, update record and
-	 * indicate it should be printed later.
-	 */
-	if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
-		strlcpy(name, ifr.ifr_name, sizeof(name));
-		printifname = 1;
-	}
 }
 
 static void
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index 869eb44289d7..28677e57065c 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -102,12 +102,13 @@ static int	setmask;
 static int	doalias;
 static int	clearaddr;
 int	newaddr = 1;
-int	printifname = 0;
 
 struct ifconfig_args global_args;
 
 int	exit_code = 0;
 
+static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */
+
 /* Formatter Strings */
 char	*f_inet, *f_inet6, *f_ether, *f_addr;
 
@@ -198,9 +199,22 @@ usage(void)
 	exit(1);
 }
 
+static void
+ifname_update(if_ctx *ctx, const char *name)
+{
+	strlcpy(ctx->_ifname_storage_ioctl, name, sizeof(ctx->_ifname_storage_ioctl));
+	ctx->ifname = ctx->_ifname_storage_ioctl;
+
+	strlcpy(ifname_to_print, name, sizeof(ifname_to_print));
+}
+
 void
 ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr)
 {
+	char ifname_orig[IFNAMSIZ];
+
+	strlcpy(ifname_orig, ifr->ifr_name, sizeof(ifname_orig));
+
 	if (ioctl(ctx->io_s, SIOCIFCREATE2, ifr) < 0) {
 		switch (errno) {
 		case EEXIST:
@@ -209,6 +223,9 @@ ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr)
 			err(1, "SIOCIFCREATE2 (%s)", ifr->ifr_name);
 		}
 	}
+
+	if (strncmp(ifname_orig, ifr->ifr_name, sizeof(ifname_orig)) != 0)
+		ifname_update(ctx, ifr->ifr_name);
 }
 
 #ifdef WITHOUT_NETLINK
@@ -402,8 +419,8 @@ sortifaddrs(struct ifaddrs *list,
 static void
 printifnamemaybe(void)
 {
-	if (printifname)
-		printf("%s\n", name);
+	if (ifname_to_print[0] != '\0')
+		printf("%s\n", ifname_to_print);
 }
 
 static void
@@ -1520,8 +1537,7 @@ setifname(if_ctx *ctx, const char *val, int dummy __unused)
 		free(newname);
 		err(1, "ioctl SIOCSIFNAME (set name)");
 	}
-	printifname = 1;
-	strlcpy(name, newname, sizeof(name));
+	ifname_update(ctx, newname);
 	free(newname);
 }
 
diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h
index 4910a816d3f1..7b9c64cdaef1 100644
--- a/sbin/ifconfig/ifconfig.h
+++ b/sbin/ifconfig/ifconfig.h
@@ -58,10 +58,12 @@ struct ifconfig_args;
 struct ifconfig_context {
 	struct ifconfig_args	*args;
 	const struct afswtch	*afp;
-	int			io_s;	/* fd to use for ioctl() */
-	struct snl_state	*io_ss;	/* NETLINK_ROUTE socket */
+	int			io_s;		/* fd to use for ioctl() */
+	struct snl_state	*io_ss;		/* NETLINK_ROUTE socket */
+	char			*ifname;	/* Current interface name */
+	char			_ifname_storage_ioctl[IFNAMSIZ];
 };
-typedef const struct ifconfig_context if_ctx;
+typedef struct ifconfig_context if_ctx;
 
 typedef	void c_func(if_ctx *ctx, const char *cmd, int arg);
 typedef	void c_func2(if_ctx *ctx, const char *arg1, const char *arg2);
@@ -251,7 +253,6 @@ extern	struct ifreq ifr;
 extern	char name[IFNAMSIZ];	/* name of interface */
 extern	int allmedia;
 extern	int newaddr;
-extern	int printifname;
 extern	int exit_code;
 extern struct ifconfig_args global_args;
 extern	char *f_inet, *f_inet6, *f_ether, *f_addr;
diff --git a/sbin/ifconfig/ifieee80211.c b/sbin/ifconfig/ifieee80211.c
index dfedb0b82a92..4f2af7148ece 100644
--- a/sbin/ifconfig/ifieee80211.c
+++ b/sbin/ifconfig/ifieee80211.c
@@ -5800,7 +5800,6 @@ static void
 wlan_create(if_ctx *ctx, struct ifreq *ifr)
 {
 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
-	char orig_name[IFNAMSIZ];
 
 	if (params.icp_parent[0] == '\0')
 		errx(1, "must specify a parent device (wlandev) when creating "
@@ -5811,13 +5810,7 @@ wlan_create(if_ctx *ctx, struct ifreq *ifr)
 	ifr->ifr_data = (caddr_t) &params;
 	ifcreate_ioctl(ctx, ifr);
 
-	/* XXX preserve original name for ifclonecreate(). */
-	strlcpy(orig_name, name, sizeof(orig_name));
-	strlcpy(name, ifr->ifr_name, sizeof(name));
-
 	setdefregdomain(ctx);
-
-	strlcpy(name, orig_name, sizeof(name));
 }
 
 static void