git: c368d3f20fc0 - main - tests/sendfile: factor out tcp_socketpair()

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 03 May 2024 14:45:22 UTC
The branch main has been updated by glebius:

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

commit c368d3f20fc015941a82d112600c3cac740d9c3e
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-05-03 14:45:06 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-05-03 14:45:06 +0000

    tests/sendfile: factor out tcp_socketpair()
    
    It creates a pair of connected TCP sockets for later testing.  No
    functional change.
    
    Reviewed by:            chs
    Differential Revision:  https://reviews.freebsd.org/D45054
---
 tests/sys/kern/sendfile_helper.c | 119 +++++++++++++++++++++------------------
 1 file changed, 65 insertions(+), 54 deletions(-)

diff --git a/tests/sys/kern/sendfile_helper.c b/tests/sys/kern/sendfile_helper.c
index 13d10eac6673..6a3b4a1ac880 100644
--- a/tests/sys/kern/sendfile_helper.c
+++ b/tests/sys/kern/sendfile_helper.c
@@ -42,29 +42,73 @@
 #include <string.h>
 #include <unistd.h>
 
-static int ls;
 static char buf[1024*1024];
-static volatile bool accept_done = false;
+ssize_t readlen;
 static volatile bool read_done = false;
 
-static void *
-server(void *arg)
+static int
+tcp_socketpair(int *sv)
 {
-	struct sockaddr_in sin;
-	ssize_t rv;
-	socklen_t slen;
-	int ss;
-	ssize_t readlen = (uintptr_t)arg;
+	struct sockaddr_in sin = {
+		.sin_len = sizeof(struct sockaddr_in),
+		.sin_family = AF_INET,
+		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+	};
+	int flags;
+	int ls;
+
+	ls = socket(PF_INET, SOCK_STREAM, 0);
+	if (ls < 0)
+		err(1, "socket ls");
+
+	if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1},
+	    sizeof(int)) < 0)
+		err(1, "SO_REUSEADDR");
+
+	if (bind(ls, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+		err(1, "bind ls");
+
+	if (getsockname(ls, (struct sockaddr *)&sin,
+	    &(socklen_t){ sizeof(sin) }) < 0)
+		err(1, "getsockname");
 
-	slen = sizeof(sin);
-	ss = accept(ls, (void *)&sin, &slen);
-	if (ss < 0)
+	if (listen(ls, 5) < 0)
+		err(1, "listen ls");
+
+	sv[0] = socket(PF_INET, SOCK_STREAM, 0);
+	if (sv[0] < 0)
+		err(1, "socket cs");
+
+	flags = fcntl(sv[0], F_GETFL);
+	flags |= O_NONBLOCK;
+	if (fcntl(sv[0], F_SETFL, flags) == -1)
+		err(1, "fcntl +O_NONBLOCK");
+
+	if (connect(sv[0], (void *)&sin, sizeof(sin)) != -1 ||
+	    errno != EINPROGRESS)
+		err(1, "connect cs");
+
+	sv[1] = accept(ls, NULL, 0);
+	if (sv[1] < 0)
 		err(1, "accept ls");
 
-	accept_done = true;
+	flags &= ~O_NONBLOCK;
+	if (fcntl(sv[0], F_SETFL, flags) == -1)
+		err(1, "fcntl -O_NONBLOCK");
+
+	close(ls);
+
+	return (0);
+}
+
+static void *
+receiver(void *arg)
+{
+	int s = *(int *)arg;
+	ssize_t rv;
 
 	do {
-		rv = read(ss, buf, sizeof(buf));
+		rv = read(s, buf, sizeof(buf));
 		if (rv == -1)
 			err(2, "read receiver");
 		if (rv == 0)
@@ -81,61 +125,28 @@ int
 main(int argc, char **argv)
 {
 	pthread_t pt;
-	struct sockaddr_in sin;
-	off_t start, len;
-	socklen_t slen;
-	int fd, cs, on, flags, error;
+	off_t start;
+	int fd, ss[2], flags, error;
 
 	if (argc != 5)
 		errx(1, "usage: %s <file> <start> <len> <flags>",
 		    getprogname());
 
 	start = strtoull(argv[2], NULL, 0);
-	len = strtoull(argv[3], NULL, 0);
+	readlen = strtoull(argv[3], NULL, 0);
 	flags = strtoul(argv[4], NULL, 0);
 
 	fd = open(argv[1], O_RDONLY);
 	if (fd < 0)
-		err(1, "open");		
-
-	ls = socket(PF_INET, SOCK_STREAM, 0);
-	if (ls < 0)
-		err(1, "socket ls");
-
-	on = 1;
-	if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
-	    (socklen_t)sizeof(on)) < 0)
-		err(1, "SO_REUSEADDR");
-
-	sin.sin_len = sizeof(sin);
-	sin.sin_family = AF_INET;
-	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	sin.sin_port = 0;
-	if (bind(ls, (void *)&sin, sizeof(sin)) < 0)
-		err(1, "bind ls");
-
-	slen = sizeof(sin);
-	if (getsockname(ls, (void *)&sin, &slen) < 0)
-		err(1, "getsockname");
+		err(1, "open");
 
-	if (listen(ls, 5) < 0)
-		err(1, "listen ls");
+	tcp_socketpair(ss);
 
-	error = pthread_create(&pt, NULL, server, (void *)(uintptr_t)len);
+	error = pthread_create(&pt, NULL, receiver, &ss[1]);
 	if (error)
 		errc(1, error, "pthread_create");
 
-	cs = socket(PF_INET, SOCK_STREAM, 0);
-	if (cs < 0)
-		err(1, "socket cs");
-
-	if (connect(cs, (void *)&sin, sizeof(sin)) < 0)
-		err(1, "connect cs");
-
-	while (!accept_done)
-		usleep(1000);
-
-	if (sendfile(fd, cs, start, len, NULL, NULL, flags) < 0)
+	if (sendfile(fd, ss[0], start, readlen, NULL, NULL, flags) < 0)
 		err(3, "sendfile");
 
 	while (!read_done)