sbin/ifconfig: Get groups with libifconfig

Reviewed by:	kp
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D28965
This commit is contained in:
Ryan Moeller 2021-02-27 08:17:04 +00:00
parent 6f497e47e9
commit 64bacab177

View File

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