git: 0421ff5ab186 - main - tests/unix_passfd: factor out sysctl(3) read into separate function

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 30 Sep 2022 20:45:04 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=0421ff5ab18691dacb99f1ba8c4b8463bfd08c79

commit 0421ff5ab18691dacb99f1ba8c4b8463bfd08c79
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-09-30 20:29:23 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-09-30 20:43:08 +0000

    tests/unix_passfd: factor out sysctl(3) read into separate function
---
 tests/sys/kern/unix_passfd_test.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/tests/sys/kern/unix_passfd_test.c b/tests/sys/kern/unix_passfd_test.c
index 49f4540bb9d0..875201c4702e 100644
--- a/tests/sys/kern/unix_passfd_test.c
+++ b/tests/sys/kern/unix_passfd_test.c
@@ -268,6 +268,24 @@ recvfd(int sockfd, int *recv_fd, int flags)
 	    CMSG_SPACE(sizeof(int)), flags);
 }
 
+#if TEST_PROTO == SOCK_STREAM
+#define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
+#elif TEST_PROTO == SOCK_DGRAM
+#define	LOCAL_SENDSPACE_SYSCTL	"net.local.dgram.maxdgram"
+#endif
+
+static u_long
+getsendspace(void)
+{
+	u_long sendspace;
+
+	ATF_REQUIRE_MSG(sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
+            &(size_t){sizeof(u_long)}, NULL, 0) != -1,
+	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
+
+	return (sendspace);
+}
+
 /*
  * Put a temporary file into a UNIX domain socket, then take it out and make
  * sure it's the same file.  First time around, don't close the reference
@@ -530,12 +548,6 @@ ATF_TC_BODY(devfs_orphan, tc)
 	closesocketpair(fd);
 }
 
-#if TEST_PROTO == SOCK_STREAM
-#define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
-#elif TEST_PROTO == SOCK_DGRAM
-#define	LOCAL_SENDSPACE_SYSCTL	"net.local.dgram.maxdgram"
-#endif
-
 /*
  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
  * control message to the data. Sender sends large payload using a non-blocking
@@ -551,12 +563,7 @@ ATF_TC_BODY(rights_creds_payload, tc)
 	void *buf;
 	int fd[2], getfd, putfd, rc;
 
-	len = sizeof(sendspace);
-	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
-	    &len, NULL, 0);
-	ATF_REQUIRE_MSG(rc != -1,
-	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
-
+	sendspace = getsendspace();
 	buf = calloc(1, sendspace);
 	ATF_REQUIRE(buf != NULL);