xref: /freebsd/tests/sys/kern/sendfile_helper.c (revision b4e38a41f584ad4391c04b8cfec81f46176b18b0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Netflix, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <sys/uio.h>
34 
35 #include <netinet/in.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <pthread.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static int ls;
48 static char buf[1024*1024];
49 static volatile bool accept_done = false;
50 static volatile bool read_done = false;
51 
52 static void *
53 server(void *arg)
54 {
55 	struct sockaddr_in sin;
56 	ssize_t rv;
57 	socklen_t slen;
58 	int ss;
59 	ssize_t readlen = (uintptr_t)arg;
60 
61 	slen = sizeof(sin);
62 	ss = accept(ls, (void *)&sin, &slen);
63 	if (ss < 0)
64 		err(1, "accept ls");
65 
66 	accept_done = true;
67 
68 	do {
69 		rv = read(ss, buf, sizeof(buf));
70 		if (rv == -1)
71 			err(2, "read receiver");
72 		if (rv == 0)
73 			break;
74 		readlen -= rv;
75 	} while (readlen != 0);
76 
77 	read_done = true;
78 
79 	return NULL;
80 }
81 
82 int
83 main(int argc, char **argv)
84 {
85 	pthread_t pt;
86 	struct sockaddr_in sin;
87 	off_t start, len;
88 	socklen_t slen;
89 	int fd, cs, on, flags, error;
90 
91 	if (argc != 5)
92 		errx(1, "usage: %s <file> <start> <len> <flags>",
93 		    getprogname());
94 
95 	start = strtoull(argv[2], NULL, 0);
96 	len = strtoull(argv[3], NULL, 0);
97 	flags = strtoul(argv[4], NULL, 0);
98 
99 	fd = open(argv[1], O_RDONLY);
100 	if (fd < 0)
101 		err(1, "open");
102 
103 	ls = socket(PF_INET, SOCK_STREAM, 0);
104 	if (ls < 0)
105 		err(1, "socket ls");
106 
107 	on = 1;
108 	if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
109 	    (socklen_t)sizeof(on)) < 0)
110 		err(1, "SO_REUSEADDR");
111 
112 	sin.sin_len = sizeof(sin);
113 	sin.sin_family = AF_INET;
114 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
115 	sin.sin_port = 0;
116 	if (bind(ls, (void *)&sin, sizeof(sin)) < 0)
117 		err(1, "bind ls");
118 
119 	slen = sizeof(sin);
120 	if (getsockname(ls, (void *)&sin, &slen) < 0)
121 		err(1, "getsockname");
122 
123 	if (listen(ls, 5) < 0)
124 		err(1, "listen ls");
125 
126 	error = pthread_create(&pt, NULL, server, (void *)(uintptr_t)len);
127 	if (error)
128 		errc(1, error, "pthread_create");
129 
130 	cs = socket(PF_INET, SOCK_STREAM, 0);
131 	if (cs < 0)
132 		err(1, "socket cs");
133 
134 	if (connect(cs, (void *)&sin, sizeof(sin)) < 0)
135 		err(1, "connect cs");
136 
137 	while (!accept_done)
138 		usleep(1000);
139 
140 	if (sendfile(fd, cs, start, len, NULL, NULL, flags) < 0)
141 		err(3, "sendfile");
142 
143 	while (!read_done)
144 		usleep(1000);
145 
146 	exit(0);
147 }
148