diff --git a/sbin/ifconfig/carp.c b/sbin/ifconfig/carp.c --- a/sbin/ifconfig/carp.c +++ b/sbin/ifconfig/carp.c @@ -73,14 +73,9 @@ 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 @@ else printf("\n"); } -close: - ifconfig_close(lifh); } static void diff --git a/sbin/ifconfig/ifbridge.c b/sbin/ifconfig/ifbridge.c --- a/sbin/ifconfig/ifbridge.c +++ b/sbin/ifconfig/ifbridge.c @@ -156,19 +156,14 @@ 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 @@ } ifconfig_bridge_free_bridge_status(bridge); -close: - ifconfig_close(lifh); } static void diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c --- a/sbin/ifconfig/ifclone.c +++ b/sbin/ifconfig/ifclone.c @@ -57,17 +57,11 @@ 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; diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -36,6 +36,10 @@ * $FreeBSD$ */ +#pragma once + +#include + #define __constructor __attribute__((constructor)) struct afswtch; @@ -128,6 +132,7 @@ }; void opt_register(struct option *); +extern ifconfig_handle_t *lifh; extern struct ifreq ifr; extern char name[IFNAMSIZ]; /* name of interface */ extern int allmedia; diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -78,8 +78,12 @@ #include #include +#include + #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 @@ 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 @@ done: freeformat(); + ifconfig_close(lifh); exit(exit_code); } diff --git a/sbin/ifconfig/ifgroup.c b/sbin/ifconfig/ifgroup.c --- a/sbin/ifconfig/ifgroup.c +++ b/sbin/ifconfig/ifgroup.c @@ -86,16 +86,11 @@ 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 @@ printf("\n"); free(ifgr.ifgr_groups); -close: - ifconfig_close(lifh); } static void diff --git a/sbin/ifconfig/iflagg.c b/sbin/ifconfig/iflagg.c --- a/sbin/ifconfig/iflagg.c +++ b/sbin/ifconfig/iflagg.c @@ -219,7 +219,6 @@ 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 @@ 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 @@ } ifconfig_lagg_free_lagg_status(lagg); -close: - ifconfig_close(lifh); } static diff --git a/sbin/ifconfig/sfp.c b/sbin/ifconfig/sfp.c --- a/sbin/ifconfig/sfp.c +++ b/sbin/ifconfig/sfp.c @@ -61,15 +61,10 @@ 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 @@ 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 @@ 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 @@ "\t", HD_OMIT_COUNT | HD_OMIT_CHARS); } } - -close: - ifconfig_close(lifh); }