From nobody Wed Jun 14 09:16:21 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Qh0Cn5FBcz4d3L8; Wed, 14 Jun 2023 09:16:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Qh0Cn4JLfz3mtt; Wed, 14 Jun 2023 09:16:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686734181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=esmDN7mF2eMb6rRiCTBG8TOGKG3uFm3LVKR06gfqcP4=; b=kxWrQvtOl8B6f0C3TKqtntTzRzN7mSg5EcUwCt3qqvMiowtmJRkPr9JgO/YCUQXzp2a6Cz xPYI1zUlKJX98U3sN3SHuLul+eKX07bCAShqriRm8VWj/mgNAzmMjixyDukS0/utvpYujn KqPjR7taeWBX+4+8NV64AARdOo8GKHbB2DsQvM3njJqUkw6RaPvHT6AmScaWOLN5M+XnnV ekPSRkT0bxvrCp9XUE9sRGKdedZffZC7d48n/bn6JiLTnurIFf9UgCvzCMfm/3IqJL6vc6 s+ZyWTrM8yuvpK+DT50U+BBAzFu2jsG3Klc490DSZkcDT/3jXgYK3CfyWoebkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686734181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=esmDN7mF2eMb6rRiCTBG8TOGKG3uFm3LVKR06gfqcP4=; b=TvyR38uNL5DJktk5+WSX4KBtVUsFDKqHADIrqRZOC5+AuQba27SHoHIFV9ralxnkA46gfC UwjFzUiQLejWc6iX5ujOwHt/fFoFXZsFkIBl6Obcs4A8jTPyVXj3U47NbSQwwtft+TL+IC Dp/eTw14glsxrHD8WtxyFIS6JIqD0EF1YhY3fodQ6AHTVeLyZ6R9bnBw6B+ujKULfQa37z nJcqe2ymY+6bSTgNwmuxR8H313zGG8/6ZC0/FY88Nc+JdTKXiwC+eLh66ud2PqrgYCcXM7 JZGpmRm5mlJXcT8TkycM9EmpJ96HncrcapdzggdkGh8Y2dyfX03YWykRX3peXg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686734181; a=rsa-sha256; cv=none; b=n5V7E70cBsbxTqlAG5h8zHZZsHSTo0MOVZ2asNN5n+hTQWQEUWM/6eAMPoeUbpDLqhEhkG CUBhsAXnsaXYzU6cCmSaHX87u6FZdI1QfVp4fXyTeQUJHPtUHmnPqcaL4pnA4G8m+IUKHJ fyHNNbVRsGCGfLYm5VH7nt723k8QOHsf0IQeHYSnj8jr0pE3952UDPN7E5sNwYTTmHrdq6 m1u8yybDaQQx8I5OUORhQa9bq6s5OpaF5eiK9GPJ/yJW18DXf8Sr1a433vBvYcYLc36vbw cEZ7lCGyFkI7uM2SVl+tZH3kxo6hSn6ulm8gNODAxFy/n6TOB2gRs98pCPV10g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Qh0Cn2jD7zyQw; Wed, 14 Jun 2023 09:16:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35E9GLFT086856; Wed, 14 Jun 2023 09:16:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35E9GLIW086855; Wed, 14 Jun 2023 09:16:21 GMT (envelope-from git) Date: Wed, 14 Jun 2023 09:16:21 GMT Message-Id: <202306140916.35E9GLIW086855@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: bbad5525fabf - main - ifconfig: start ifconfig context from main() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bbad5525fabfc8dd75032dc9aaa2cc1d33e62461 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=bbad5525fabfc8dd75032dc9aaa2cc1d33e62461 commit bbad5525fabfc8dd75032dc9aaa2cc1d33e62461 Author: Alexander V. Chernikov AuthorDate: 2023-06-14 07:40:12 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-06-14 09:15:58 +0000 ifconfig: start ifconfig context from main() Differential Revision: https://reviews.freebsd.org/D40440 MFC after: 2 weeks --- sbin/ifconfig/ifconfig.c | 60 +++++++++++++++++++--------------------- sbin/ifconfig/ifconfig.h | 4 +-- sbin/ifconfig/ifconfig_netlink.c | 7 +++-- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 7b01b2d547ba..4f1d95c43a63 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -111,8 +111,8 @@ static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */ char *f_inet, *f_inet6, *f_ether, *f_addr; #ifdef WITHOUT_NETLINK -static void list_interfaces_ioctl(struct ifconfig_args *args); -static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, +static void list_interfaces_ioctl(if_ctx *ctx); +static void status(if_ctx *ctx, const struct sockaddr_dl *sdl, struct ifaddrs *ifa); #endif static _Noreturn void usage(void); @@ -422,12 +422,12 @@ printifnamemaybe(void) } static void -list_interfaces(struct ifconfig_args *args) +list_interfaces(if_ctx *ctx) { #ifdef WITHOUT_NETLINK - list_interfaces_ioctl(args); + list_interfaces_ioctl(ctx); #else - list_interfaces_nl(args); + list_interfaces_nl(ctx->args); #endif } @@ -571,19 +571,12 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) } static int -ifconfig_wrapper(struct ifconfig_args *args, int iscreate, - const struct afswtch *uafp) +ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp) { - struct ifconfig_context ctx = { - .args = args, - .io_s = -1, - .ifname = args->ifname, - }; - #ifdef WITHOUT_NETLINK - return (ifconfig(&ctx, iscreate, uafp)); + return (ifconfig_ioctl(ctx, iscreate, uafp)); #else - return (ifconfig_wrapper_nl(&ctx, iscreate, uafp)); + return (ifconfig_nl(ctx, iscreate, uafp)); #endif } @@ -616,6 +609,11 @@ main(int ac, char *av[]) struct ifconfig_args _args = {}; struct ifconfig_args *args = &_args; + struct ifconfig_context ctx = { + .args = args, + .io_s = -1, + }; + f_inet = f_inet6 = f_ether = f_addr = NULL; lifh = ifconfig_open(); @@ -647,6 +645,7 @@ main(int ac, char *av[]) if (!args->all && !args->namesonly) { /* not listing, need an argument */ args->ifname = args_pop(args); + ctx.ifname = args->ifname; /* check and maybe load support for this interface */ ifmaybeload(args, args->ifname); @@ -662,7 +661,7 @@ main(int ac, char *av[]) if (isnametoolong(args->ifname)) errx(1, "%s: cloning name too long", args->ifname); - ifconfig_wrapper(args, 1, NULL); + ifconfig(&ctx, 1, NULL); exit(exit_code); } #ifdef JAIL @@ -675,7 +674,7 @@ main(int ac, char *av[]) if (isnametoolong(args->ifname)) errx(1, "%s: interface name too long", args->ifname); - ifconfig_wrapper(args, 0, NULL); + ifconfig(&ctx, 0, NULL); exit(exit_code); } #endif @@ -714,14 +713,14 @@ main(int ac, char *av[]) if (!(((flags & IFF_CANTCONFIG) != 0) || (args->downonly && (flags & IFF_UP) != 0) || (args->uponly && (flags & IFF_UP) == 0))) - ifconfig_wrapper(args, 0, args->afp); + ifconfig(&ctx, 0, args->afp); } goto done; } args->allfamilies = args->afp == NULL; - list_interfaces(args); + list_interfaces(&ctx); done: freeformat(); @@ -771,13 +770,14 @@ match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sd } static void -list_interfaces_ioctl(struct ifconfig_args *args) +list_interfaces_ioctl(if_ctx *ctx) { struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); struct ifaddrs *ifap, *sifap, *ifa; struct ifa_order_elt *cur, *tmp; char *namecp = NULL; int ifindex; + struct ifconfig_args *args = ctx->args; if (getifaddrs(&ifap) != 0) err(EXIT_FAILURE, "getifaddrs"); @@ -840,9 +840,9 @@ list_interfaces_ioctl(struct ifconfig_args *args) ifindex++; if (args->argc > 0) - ifconfig_wrapper(args, 0, args->afp); + ifconfig(ctx, 0, args->afp); else - status(args, sdl, ifa); + status(ctx, sdl, ifa); } if (args->namesonly) printf("\n"); @@ -1089,7 +1089,7 @@ addifaddr(if_ctx *ctx, const struct afswtch *afp) } int -ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp) +ifconfig_ioctl(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp) { const struct afswtch *afp, *nafp; const struct cmd *p; @@ -1717,11 +1717,12 @@ print_description(int s) * specified, show only it; otherwise, show them all. */ static void -status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, +status(if_ctx *ctx, const struct sockaddr_dl *sdl, struct ifaddrs *ifa) { struct ifaddrs *ift; - int s; + int s, old_s; + struct ifconfig_args *args = ctx->args; bool allfamilies = args->afp == NULL; char *ifname = ifa->ifa_name; @@ -1735,12 +1736,8 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); if (s < 0) err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); - - struct ifconfig_context _ctx = { - .io_s = s, - .ifname = ifname, - }; - struct ifconfig_context *ctx = &_ctx; + old_s = ctx->io_s; + ctx->io_s = s; printf("%s: ", ifname); printb("flags", ifa->ifa_flags, IFFBITS); @@ -1794,6 +1791,7 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, sfp_status(ctx); close(s); + ctx->io_s = old_s; return; } #endif diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h index 187743a315c5..a65969ab4c56 100644 --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -274,7 +274,7 @@ void sfp_status(if_ctx *ctx); struct sockaddr_dl; bool match_ether(const struct sockaddr_dl *sdl); bool match_if_flags(struct ifconfig_args *args, int if_flags); -int ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp); +int ifconfig_ioctl(if_ctx *ctx, int iscreate, const struct afswtch *uafp); bool group_member(const char *ifname, const char *match, const char *nomatch); void print_ifcap(struct ifconfig_args *args, int s); void tunnel_status(if_ctx *ctx); @@ -285,7 +285,7 @@ void print_metric(int s); /* Netlink-related functions */ void list_interfaces_nl(struct ifconfig_args *args); -int ifconfig_wrapper_nl(if_ctx *ctx, int iscreate, +int ifconfig_nl(if_ctx *ctx, int iscreate, const struct afswtch *uafp); uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname); diff --git a/sbin/ifconfig/ifconfig_netlink.c b/sbin/ifconfig/ifconfig_netlink.c index 1635a4cb80c7..a7d3cfe6f300 100644 --- a/sbin/ifconfig/ifconfig_netlink.c +++ b/sbin/ifconfig/ifconfig_netlink.c @@ -123,7 +123,7 @@ nl_init_socket(struct snl_state *ss) } int -ifconfig_wrapper_nl(if_ctx *ctx, int iscreate, +ifconfig_nl(if_ctx *ctx, int iscreate, const struct afswtch *uafp) { struct snl_state ss = {}; @@ -131,9 +131,10 @@ ifconfig_wrapper_nl(if_ctx *ctx, int iscreate, nl_init_socket(&ss); ctx->io_ss = &ss; - int error = ifconfig(ctx, iscreate, uafp); + int error = ifconfig_ioctl(ctx, iscreate, uafp); snl_free(&ss); + ctx->io_ss = NULL; return (error); } @@ -447,7 +448,7 @@ list_interfaces_nl(struct ifconfig_args *args) } else if (args->argc == 0) status_nl(ctx, iface); else - ifconfig(ctx, 0, args->afp); + ifconfig_ioctl(ctx, 0, args->afp); } if (args->namesonly) printf("\n");