xref: /freebsd/tools/test/stress2/misc/sendfile8.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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
47newfs $newfs_flags md$mdstart > /dev/null
48mount /dev/md$mdstart $mntpoint
49
50cd $mntpoint
51dd if=/dev/random of=in bs=1m count=50 status=none
52/tmp/sendfile8 in out 76543
53s=$?
54cd $odir
55
56for i in `jot 6`; do
57	mount | grep -q "on $mntpoint " || break
58	umount $mntpoint && break || sleep 10
59done
60[ $i -eq 6 ] && exit 1
61mdconfig -d -u $mdstart
62rm -rf /tmp/sendfile8
63exit $s
64
65EOF
66#include <sys/param.h>
67#include <sys/mman.h>
68#include <sys/socket.h>
69#include <sys/stat.h>
70#include <sys/wait.h>
71
72#include <netinet/in.h>
73
74#include <err.h>
75#include <errno.h>
76#include <fcntl.h>
77#include <netdb.h>
78#include <signal.h>
79#include <stdio.h>
80#include <stdlib.h>
81#include <string.h>
82#include <time.h>
83#include <unistd.h>
84
85static int port;
86static char *input, *output;
87
88#define BUFSIZE 4096
89#define PARALLEL 1
90#define RUNTIME (1 * 60)
91
92static void
93mess(void)
94{
95	off_t length;
96	int fd;
97
98	if ((fd = open(input, O_RDWR)) == -1)
99		err(1, "open(%s)", input);
100	length = arc4random() % 100 * 1024 * 1024;
101	if (ftruncate(fd, length) == -1)
102		err(1, "truncate(%jd)", length);
103	close(fd);
104}
105
106static void
107reader(void) {
108	struct sockaddr_in inetaddr, inetpeer;
109	socklen_t len;
110	int tcpsock, msgsock;
111	int on;
112	int n, *buf, fd;
113
114	on = 1;
115	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
116		err(1, "socket(), %s:%d", __FILE__, __LINE__);
117
118	if (setsockopt(tcpsock,
119	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
120		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
121
122	inetaddr.sin_family = AF_INET;
123	inetaddr.sin_addr.s_addr = INADDR_ANY;
124	inetaddr.sin_port = htons(port);
125	inetaddr.sin_len = sizeof(inetaddr);
126
127	if (bind(tcpsock,
128	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
129		err(1, "bind(), %s:%d", __FILE__, __LINE__);
130
131	if (listen(tcpsock, 5) < 0)
132		err(1, "listen(), %s:%d", __FILE__, __LINE__);
133
134	len = sizeof(inetpeer);
135	if ((msgsock = accept(tcpsock,
136	    (struct sockaddr *)&inetpeer, &len)) < 0)
137		err(1, "accept(), %s:%d", __FILE__, __LINE__);
138
139	if ((buf = malloc(BUFSIZE)) == NULL)
140		err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
141
142	if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
143		err(1, "open(%s)", output);
144
145	for (;;) {
146		if ((n = read(msgsock, buf, BUFSIZE)) < 0)
147			err(1, "read(), %s:%d", __FILE__, __LINE__);
148		if (n == 0) break;
149
150		if ((write(fd, buf, n)) != n)
151			err(1, "write");
152	}
153	close(msgsock);
154	close(fd);
155	return;
156}
157
158static void
159writer(void) {
160	struct sockaddr_in inetaddr;
161	struct hostent *hostent;
162	struct stat statb;
163	off_t off = 0;
164	size_t size;
165	int i, r, fd;
166	int tcpsock, on;
167
168	on = 1;
169	for (i = 1; i < 5; i++) {
170		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
171			err(1, "socket(), %s:%d", __FILE__, __LINE__);
172
173		if (setsockopt(tcpsock,
174		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
175			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
176
177		size = getpagesize() -4;
178		if (setsockopt(tcpsock,
179		    SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
180			err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
181			    __LINE__);
182
183		hostent = gethostbyname ("localhost");
184		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
185			sizeof (struct in_addr));
186
187		inetaddr.sin_family = AF_INET;
188		inetaddr.sin_port = htons(port);
189		inetaddr.sin_len = sizeof(inetaddr);
190
191		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
192			sizeof(inetaddr));
193		if (r == 0)
194			break;
195		sleep(1);
196		close(tcpsock);
197	}
198	if (r < 0)
199		err(1, "connect(), %s:%d", __FILE__, __LINE__);
200
201        if (stat(input, &statb) != 0)
202                err(1, "stat(%s)", input);
203
204	if ((fd = open(input, O_RDONLY)) == -1)
205		err(1, "open(%s)", input);
206
207	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
208		err(1, "sendfile");
209
210	return;
211}
212
213static void
214test(void)
215{
216	pid_t pid;
217
218	if ((pid = fork()) == 0) {
219		writer();
220		_exit(0);
221	}
222	reader();
223	kill(pid, SIGINT);
224	if (waitpid(pid, NULL, 0) != pid)
225		err(1, "waitpid(%d)", pid);
226
227	_exit(0);
228}
229
230int
231main(int argc, char *argv[])
232{
233	time_t start;
234	int e, i, j, pids[PARALLEL], status;
235
236	if (argc != 4) {
237		fprintf(stderr, "Usage: %s <input file> <output file>\n",
238		    argv[0]);
239		exit(1);
240	}
241	input = argv[1];
242	output = argv[2];
243	port = atoi(argv[3]);
244	e = 0;
245
246	start = time(NULL);
247	while ((time(NULL) - start) < RUNTIME && e == 0) {
248		for (i = 0; i < PARALLEL; i++) {
249			if ((pids[i] = fork()) == 0)
250				test();
251		}
252		for (j = 0; j < 100; j++) {
253			mess();
254			usleep(arc4random() % 10000);
255		}
256		for (i = 0; i < PARALLEL; i++) {
257			if (waitpid(pids[i], &status, 0) == -1)
258				err(1, "waitpid(%d)", pids[i]);
259			e += status == 0 ? 0 : 1;
260		}
261	}
262
263	return (e);
264}
265