xref: /freebsd/tools/test/stress2/misc/sendfile8.sh (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# sendfile(2) + parallel file size change. Bug 217789.
30# "panic: Assertion size > 0 failed at ../../../kern/subr_vmem.c:1082" seen:
31# Fixed in r315910
32
33. ../default.cfg
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile8.c
40mycc -o sendfile8 -Wall -Wextra -O0 -g sendfile8.c || exit 1
41rm -f sendfile8.c
42cd $odir
43
44mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
45[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
46mdconfig -a -t swap -s 1g -u $mdstart || exit 1
47bsdlabel -w md$mdstart auto
48newfs $newfs_flags md${mdstart}$part > /dev/null
49mount /dev/md${mdstart}$part $mntpoint
50
51cd $mntpoint
52dd if=/dev/random of=in bs=1m count=50 status=none
53/tmp/sendfile8 in out 76543
54s=$?
55cd $odir
56
57for i in `jot 6`; do
58	mount | grep -q "on $mntpoint " || break
59	umount $mntpoint && break || sleep 10
60done
61[ $i -eq 6 ] && exit 1
62mdconfig -d -u $mdstart
63rm -rf /tmp/sendfile8
64exit $s
65
66EOF
67#include <sys/param.h>
68#include <sys/mman.h>
69#include <sys/socket.h>
70#include <sys/stat.h>
71#include <sys/wait.h>
72
73#include <netinet/in.h>
74
75#include <err.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <netdb.h>
79#include <signal.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <time.h>
84#include <unistd.h>
85
86static int port;
87static char *input, *output;
88
89#define BUFSIZE 4096
90#define PARALLEL 1
91#define RUNTIME (1 * 60)
92
93static void
94mess(void)
95{
96	off_t length;
97	int fd;
98
99	if ((fd = open(input, O_RDWR)) == -1)
100		err(1, "open(%s)", input);
101	length = arc4random() % 100 * 1024 * 1024;
102	if (ftruncate(fd, length) == -1)
103		err(1, "truncate(%jd)", length);
104	close(fd);
105}
106
107static void
108reader(void) {
109	struct sockaddr_in inetaddr, inetpeer;
110	socklen_t len;
111	int tcpsock, msgsock;
112	int on;
113	int n, *buf, fd;
114
115	on = 1;
116	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
117		err(1, "socket(), %s:%d", __FILE__, __LINE__);
118
119	if (setsockopt(tcpsock,
120	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
121		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
122
123	inetaddr.sin_family = AF_INET;
124	inetaddr.sin_addr.s_addr = INADDR_ANY;
125	inetaddr.sin_port = htons(port);
126	inetaddr.sin_len = sizeof(inetaddr);
127
128	if (bind(tcpsock,
129	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
130		err(1, "bind(), %s:%d", __FILE__, __LINE__);
131
132	if (listen(tcpsock, 5) < 0)
133		err(1, "listen(), %s:%d", __FILE__, __LINE__);
134
135	len = sizeof(inetpeer);
136	if ((msgsock = accept(tcpsock,
137	    (struct sockaddr *)&inetpeer, &len)) < 0)
138		err(1, "accept(), %s:%d", __FILE__, __LINE__);
139
140	if ((buf = malloc(BUFSIZE)) == NULL)
141		err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
142
143	if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
144		err(1, "open(%s)", output);
145
146	for (;;) {
147		if ((n = read(msgsock, buf, BUFSIZE)) < 0)
148			err(1, "read(), %s:%d", __FILE__, __LINE__);
149		if (n == 0) break;
150
151		if ((write(fd, buf, n)) != n)
152			err(1, "write");
153	}
154	close(msgsock);
155	close(fd);
156	return;
157}
158
159static void
160writer(void) {
161	struct sockaddr_in inetaddr;
162	struct hostent *hostent;
163	struct stat statb;
164	off_t off = 0;
165	size_t size;
166	int i, r, fd;
167	int tcpsock, on;
168
169	on = 1;
170	for (i = 1; i < 5; i++) {
171		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
172			err(1, "socket(), %s:%d", __FILE__, __LINE__);
173
174		if (setsockopt(tcpsock,
175		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
176			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
177
178		size = getpagesize() -4;
179		if (setsockopt(tcpsock,
180		    SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
181			err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
182			    __LINE__);
183
184		hostent = gethostbyname ("localhost");
185		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
186			sizeof (struct in_addr));
187
188		inetaddr.sin_family = AF_INET;
189		inetaddr.sin_port = htons(port);
190		inetaddr.sin_len = sizeof(inetaddr);
191
192		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
193			sizeof(inetaddr));
194		if (r == 0)
195			break;
196		sleep(1);
197		close(tcpsock);
198	}
199	if (r < 0)
200		err(1, "connect(), %s:%d", __FILE__, __LINE__);
201
202        if (stat(input, &statb) != 0)
203                err(1, "stat(%s)", input);
204
205	if ((fd = open(input, O_RDONLY)) == -1)
206		err(1, "open(%s)", input);
207
208	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
209		err(1, "sendfile");
210
211	return;
212}
213
214static void
215test(void)
216{
217	pid_t pid;
218
219	if ((pid = fork()) == 0) {
220		writer();
221		_exit(0);
222	}
223	reader();
224	kill(pid, SIGINT);
225	if (waitpid(pid, NULL, 0) != pid)
226		err(1, "waitpid(%d)", pid);
227
228	_exit(0);
229}
230
231int
232main(int argc, char *argv[])
233{
234	time_t start;
235	int e, i, j, pids[PARALLEL], status;
236
237	if (argc != 4) {
238		fprintf(stderr, "Usage: %s <input file> <output file>\n",
239		    argv[0]);
240		exit(1);
241	}
242	input = argv[1];
243	output = argv[2];
244	port = atoi(argv[3]);
245	e = 0;
246
247	start = time(NULL);
248	while ((time(NULL) - start) < RUNTIME && e == 0) {
249		for (i = 0; i < PARALLEL; i++) {
250			if ((pids[i] = fork()) == 0)
251				test();
252		}
253		for (j = 0; j < 100; j++) {
254			mess();
255			usleep(arc4random() % 10000);
256		}
257		for (i = 0; i < PARALLEL; i++) {
258			if (waitpid(pids[i], &status, 0) == -1)
259				err(1, "waitpid(%d)", pids[i]);
260			e += status == 0 ? 0 : 1;
261		}
262	}
263
264	return (e);
265}
266