git: 64bacab177f7 - main - sbin/ifconfig: Get groups with libifconfig

Ryan Moeller freqlabs at FreeBSD.org
Sun Feb 28 20:40:26 UTC 2021


The branch main has been updated by freqlabs:

URL: https://cgit.FreeBSD.org/src/commit/?id=64bacab177f7c743af3268a3e1ffcddaf77a68d0

commit 64bacab177f7c743af3268a3e1ffcddaf77a68d0
Author:     Ryan Moeller <freqlabs at FreeBSD.org>
AuthorDate: 2021-02-27 08:17:04 +0000
Commit:     Ryan Moeller <freqlabs at FreeBSD.org>
CommitDate: 2021-02-28 20:38:50 +0000

    sbin/ifconfig: Get groups with libifconfig
    
    Reviewed by:    kp
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D28965
---
 sbin/ifconfig/ifgroup.c | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/sbin/ifconfig/ifgroup.c b/sbin/ifconfig/ifgroup.c
index 50d17ca6429e..2b13227af4f3 100644
--- a/sbin/ifconfig/ifgroup.c
+++ b/sbin/ifconfig/ifgroup.c
@@ -43,6 +43,8 @@ static const char rcsid[] =
 #include <string.h>
 #include <unistd.h>
 
+#include <libifconfig.h>
+
 #include "ifconfig.h"
 
 /* ARGSUSED */
@@ -84,33 +86,21 @@ unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
 static void
 getifgroups(int s)
 {
-	int			 len, cnt;
-	struct ifgroupreq	 ifgr;
-	struct ifg_req		*ifg;
-
-	memset(&ifgr, 0, sizeof(ifgr));
-	strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
+	ifconfig_handle_t *lifh;
+	struct ifgroupreq ifgr;
+	size_t cnt;
 
-	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
-		if (errno == EINVAL || errno == ENOTTY)
-			return;
-		else
-			err(1, "SIOCGIFGROUP");
-	}
+	lifh = ifconfig_open();
+	if (lifh == NULL)
+		return;
 
-	len = ifgr.ifgr_len;
-	ifgr.ifgr_groups =
-	    (struct ifg_req *)calloc(len / sizeof(struct ifg_req),
-	    sizeof(struct ifg_req));
-	if (ifgr.ifgr_groups == NULL)
-		err(1, "getifgroups");
-	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
-		err(1, "SIOCGIFGROUP");
+	if (ifconfig_get_groups(lifh, name, &ifgr) == -1)
+		goto close;
 
 	cnt = 0;
-	ifg = ifgr.ifgr_groups;
-	for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
-		len -= sizeof(struct ifg_req);
+	for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); ++i) {
+		struct ifg_req *ifg = &ifgr.ifgr_groups[i];
+
 		if (strcmp(ifg->ifgrq_group, "all")) {
 			if (cnt == 0)
 				printf("\tgroups:");
@@ -122,6 +112,8 @@ getifgroups(int s)
 		printf("\n");
 
 	free(ifgr.ifgr_groups);
+close:
+	ifconfig_close(lifh);
 }
 
 static void


More information about the dev-commits-src-all mailing list