sbin/ifconfig: Use a global libifconfig handle

This should eventually replace the socket passed to the various
handlers. In the meantime, making it global avoids repeatedly opening
and closing handles.

Reported by:	kp
Reviewed by:	kp (earlier version)
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D28990
This commit is contained in:
Ryan Moeller 2021-02-28 10:30:09 +00:00
parent 21802a127d
commit 8b22242550
8 changed files with 21 additions and 49 deletions

View File

@ -73,14 +73,9 @@ static void
carp_status(int s)
{
struct carpreq carpr[CARP_MAXVHID];
ifconfig_handle_t *lifh;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1)
goto close;
return;
for (size_t i = 0; i < carpr[0].carpr_count; i++) {
printf("\tcarp: %s vhid %d advbase %d advskew %d",
@ -91,8 +86,6 @@ carp_status(int s)
else
printf("\n");
}
close:
ifconfig_close(lifh);
}
static void

View File

@ -156,19 +156,14 @@ bridge_addresses(int s, const char *prefix)
static void
bridge_status(int s)
{
ifconfig_handle_t *lifh;
struct ifconfig_bridge_status *bridge;
struct ifbropreq *params;
const char *pad, *prefix;
uint8_t lladdr[ETHER_ADDR_LEN];
uint16_t bprio;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_bridge_get_bridge_status(lifh, name, &bridge) == -1)
goto close;
return;
params = bridge->params;
@ -227,8 +222,6 @@ bridge_status(int s)
}
ifconfig_bridge_free_bridge_status(bridge);
close:
ifconfig_close(lifh);
}
static void

View File

@ -57,17 +57,11 @@ typedef enum {
static void
list_cloners(void)
{
ifconfig_handle_t *lifh;
char *cloners;
size_t cloners_count;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_list_cloners(lifh, &cloners, &cloners_count) < 0)
errc(1, ifconfig_err_errno(lifh), "unable to list cloners");
ifconfig_close(lifh);
for (const char *name = cloners;
name < cloners + cloners_count * IFNAMSIZ;

View File

@ -78,8 +78,12 @@ static const char rcsid[] =
#include <string.h>
#include <unistd.h>
#include <libifconfig.h>
#include "ifconfig.h"
ifconfig_handle_t *lifh;
/*
* Since "struct ifreq" is composed of various union members, callers
* should pay special attention to interpret the value.
@ -427,6 +431,10 @@ main(int argc, char *argv[])
f_inet = f_inet6 = f_ether = f_addr = NULL;
matchgroup = nogroup = NULL;
lifh = ifconfig_open();
if (lifh == NULL)
err(EXIT_FAILURE, "ifconfig_open");
envformat = getenv("IFCONFIG_FORMAT");
if (envformat != NULL)
setformat(envformat);
@ -699,6 +707,7 @@ main(int argc, char *argv[])
done:
freeformat();
ifconfig_close(lifh);
exit(exit_code);
}

View File

@ -36,6 +36,10 @@
* $FreeBSD$
*/
#pragma once
#include <libifconfig.h>
#define __constructor __attribute__((constructor))
struct afswtch;
@ -128,6 +132,7 @@ struct option {
};
void opt_register(struct option *);
extern ifconfig_handle_t *lifh;
extern struct ifreq ifr;
extern char name[IFNAMSIZ]; /* name of interface */
extern int allmedia;

View File

@ -86,16 +86,11 @@ unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
static void
getifgroups(int s)
{
ifconfig_handle_t *lifh;
struct ifgroupreq ifgr;
size_t cnt;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_get_groups(lifh, name, &ifgr) == -1)
goto close;
return;
cnt = 0;
for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); ++i) {
@ -112,8 +107,6 @@ getifgroups(int s)
printf("\n");
free(ifgr.ifgr_groups);
close:
ifconfig_close(lifh);
}
static void

View File

@ -219,7 +219,6 @@ static void
lagg_status(int s)
{
struct lagg_protos protos[] = LAGG_PROTOS;
ifconfig_handle_t *lifh;
struct ifconfig_lagg_status *lagg;
struct lagg_reqall *ra;
struct lagg_reqflags *rf;
@ -228,12 +227,8 @@ lagg_status(int s)
struct lacp_opreq *lp;
const char *proto;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_lagg_get_lagg_status(lifh, name, &lagg) == -1)
goto close;
return;
ra = lagg->ra;
rf = lagg->rf;
@ -297,8 +292,6 @@ lagg_status(int s)
}
ifconfig_lagg_free_lagg_status(lagg);
close:
ifconfig_close(lifh);
}
static

View File

@ -61,15 +61,10 @@ sfp_status(int s, struct ifreq *ifr, int verbose)
struct ifconfig_sfp_info_strings strings;
struct ifconfig_sfp_vendor_info vendor_info;
struct ifconfig_sfp_status status;
ifconfig_handle_t *lifh;
size_t channel_count;
lifh = ifconfig_open();
if (lifh == NULL)
return;
if (ifconfig_sfp_get_sfp_info(lifh, name, &info) == -1)
goto close;
return;
ifconfig_sfp_get_sfp_info_strings(&info, &strings);
@ -79,7 +74,7 @@ sfp_status(int s, struct ifreq *ifr, int verbose)
strings.sfp_conn);
if (ifconfig_sfp_get_sfp_vendor_info(lifh, name, &vendor_info) == -1)
goto close;
return;
printf("\tvendor: %s PN: %s SN: %s DATE: %s\n",
vendor_info.name, vendor_info.pn, vendor_info.sn, vendor_info.date);
@ -118,7 +113,7 @@ sfp_status(int s, struct ifreq *ifr, int verbose)
struct ifconfig_sfp_dump dump;
if (ifconfig_sfp_get_sfp_dump(lifh, name, &dump) == -1)
goto close;
return;
if (ifconfig_sfp_id_is_qsfp(info.sfp_id)) {
printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
@ -133,7 +128,4 @@ sfp_status(int s, struct ifreq *ifr, int verbose)
"\t", HD_OMIT_COUNT | HD_OMIT_CHARS);
}
}
close:
ifconfig_close(lifh);
}