xref: /freebsd/tests/sys/kern/sendfile_helper.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/sysctl.h>
31 #include <sys/uio.h>
32 
33 #include <netinet/in.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <pthread.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 static int ls;
46 static char buf[1024*1024];
47 static volatile bool accept_done = false;
48 static volatile bool read_done = false;
49 
50 static void *
51 server(void *arg)
52 {
53 	struct sockaddr_in sin;
54 	ssize_t rv;
55 	socklen_t slen;
56 	int ss;
57 	ssize_t readlen = (uintptr_t)arg;
58 
59 	slen = sizeof(sin);
60 	ss = accept(ls, (void *)&sin, &slen);
61 	if (ss < 0)
62 		err(1, "accept ls");
63 
64 	accept_done = true;
65 
66 	do {
67 		rv = read(ss, buf, sizeof(buf));
68 		if (rv == -1)
69 			err(2, "read receiver");
70 		if (rv == 0)
71 			break;
72 		readlen -= rv;
73 	} while (readlen != 0);
74 
75 	read_done = true;
76 
77 	return NULL;
78 }
79 
80 int
81 main(int argc, char **argv)
82 {
83 	pthread_t pt;
84 	struct sockaddr_in sin;
85 	off_t start, len;
86 	socklen_t slen;
87 	int fd, cs, on, flags, error;
88 
89 	if (argc != 5)
90 		errx(1, "usage: %s <file> <start> <len> <flags>",
91 		    getprogname());
92 
93 	start = strtoull(argv[2], NULL, 0);
94 	len = strtoull(argv[3], NULL, 0);
95 	flags = strtoul(argv[4], NULL, 0);
96 
97 	fd = open(argv[1], O_RDONLY);
98 	if (fd < 0)
99 		err(1, "open");
100 
101 	ls = socket(PF_INET, SOCK_STREAM, 0);
102 	if (ls < 0)
103 		err(1, "socket ls");
104 
105 	on = 1;
106 	if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
107 	    (socklen_t)sizeof(on)) < 0)
108 		err(1, "SO_REUSEADDR");
109 
110 	sin.sin_len = sizeof(sin);
111 	sin.sin_family = AF_INET;
112 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
113 	sin.sin_port = 0;
114 	if (bind(ls, (void *)&sin, sizeof(sin)) < 0)
115 		err(1, "bind ls");
116 
117 	slen = sizeof(sin);
118 	if (getsockname(ls, (void *)&sin, &slen) < 0)
119 		err(1, "getsockname");
120 
121 	if (listen(ls, 5) < 0)
122 		err(1, "listen ls");
123 
124 	error = pthread_create(&pt, NULL, server, (void *)(uintptr_t)len);
125 	if (error)
126 		errc(1, error, "pthread_create");
127 
128 	cs = socket(PF_INET, SOCK_STREAM, 0);
129 	if (cs < 0)
130 		err(1, "socket cs");
131 
132 	if (connect(cs, (void *)&sin, sizeof(sin)) < 0)
133 		err(1, "connect cs");
134 
135 	while (!accept_done)
136 		usleep(1000);
137 
138 	if (sendfile(fd, cs, start, len, NULL, NULL, flags) < 0)
139 		err(3, "sendfile");
140 
141 	while (!read_done)
142 		usleep(1000);
143 
144 	exit(0);
145 }
146