From 7fa282e616c303261904cb6158959d5a784dc3ee Mon Sep 17 00:00:00 2001 From: "Alexander V. Chernikov" Date: Wed, 14 Jun 2023 13:00:49 +0000 Subject: ifconfig: remove (most of) the usages of global 'struct ifreq ifr'. It is hard to reason about the contents of 'ifr' at any given time as nearly every function sets random fields or pointers in this structure. Use local on-stack clean 'struct ifreq' for each function instead. Reviewed by: kp Differential Revision: https://reviews.freebsd.org/D40534 MFC after: 2 weeks --- sbin/ifconfig/ifpfsync.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'sbin/ifconfig/ifpfsync.c') diff --git a/sbin/ifconfig/ifpfsync.c b/sbin/ifconfig/ifpfsync.c index 81bf463db117..b97b9925c644 100644 --- a/sbin/ifconfig/ifpfsync.c +++ b/sbin/ifconfig/ifpfsync.c @@ -51,10 +51,11 @@ #include "ifconfig.h" static int -pfsync_do_ioctl(int s, uint cmd, nvlist_t **nvl) +pfsync_do_ioctl(if_ctx *ctx, uint cmd, nvlist_t **nvl) { void *data; size_t nvlen; + struct ifreq ifr = {}; data = nvlist_pack(*nvl, &nvlen); @@ -64,7 +65,7 @@ pfsync_do_ioctl(int s, uint cmd, nvlist_t **nvl) ifr.ifr_cap_nv.length = nvlen; free(data); - if (ioctl(s, cmd, (caddr_t)&ifr) == -1) { + if (ioctl_ctx_ifr(ctx, cmd, &ifr) == -1) { free(ifr.ifr_cap_nv.buffer); return -1; } @@ -172,7 +173,7 @@ setpfsync_syncdev(if_ctx *ctx, const char *val, int dummy __unused) if (strlen(val) > IFNAMSIZ) errx(1, "interface name %s is too long", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_string(nvl, "syncdev")) @@ -180,7 +181,7 @@ setpfsync_syncdev(if_ctx *ctx, const char *val, int dummy __unused) nvlist_add_string(nvl, "syncdev", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); } @@ -189,7 +190,7 @@ unsetpfsync_syncdev(if_ctx *ctx, const char *val __unused, int dummy __unused) { nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_string(nvl, "syncdev")) @@ -197,7 +198,7 @@ unsetpfsync_syncdev(if_ctx *ctx, const char *val __unused, int dummy __unused) nvlist_add_string(nvl, "syncdev", ""); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); } @@ -210,7 +211,7 @@ setpfsync_syncpeer(if_ctx *ctx, const char *val, int dummy __unused) nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if ((ecode = getaddrinfo(val, NULL, NULL, &peerres)) != 0) @@ -239,7 +240,7 @@ setpfsync_syncpeer(if_ctx *ctx, const char *val, int dummy __unused) nvlist_add_nvlist(nvl, "syncpeer", pfsync_sockaddr_to_syncpeer_nvlist(&addr)); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -254,7 +255,7 @@ unsetpfsync_syncpeer(if_ctx *ctx, const char *val __unused, int dummy __unused) nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_nvlist(nvl, "syncpeer")) @@ -263,7 +264,7 @@ unsetpfsync_syncpeer(if_ctx *ctx, const char *val __unused, int dummy __unused) nvlist_add_nvlist(nvl, "syncpeer", pfsync_sockaddr_to_syncpeer_nvlist(&addr)); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -279,13 +280,13 @@ setpfsync_maxupd(if_ctx *ctx, const char *val, int dummy __unused) if ((maxupdates < 0) || (maxupdates > 255)) errx(1, "maxupd %s: out of range", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "maxupdates"); nvlist_add_number(nvl, "maxupdates", maxupdates); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -296,13 +297,13 @@ setpfsync_defer(if_ctx *ctx, const char *val __unused, int d) { nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "flags"); nvlist_add_number(nvl, "flags", d ? PFSYNCF_DEFER : 0); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -317,13 +318,13 @@ setpfsync_version(if_ctx *ctx, const char *val, int dummy __unused) /* Don't verify, kernel knows which versions are supported.*/ version = atoi(val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "version"); nvlist_add_number(nvl, "version", version); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -343,7 +344,7 @@ pfsync_status(if_ctx *ctx) nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) { + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) { nvlist_destroy(nvl); return; } -- cgit v1.3