xref: /freebsd/tools/test/stress2/misc/sendfile8.sh (revision 19261079b74319502c6ffa1249920079f0f69a72)
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, t, *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	t = 0;
141	if ((buf = malloc(BUFSIZE)) == NULL)
142		err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
143
144	if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
145		err(1, "open(%s)", output);
146
147	for (;;) {
148		if ((n = read(msgsock, buf, BUFSIZE)) < 0)
149			err(1, "read(), %s:%d", __FILE__, __LINE__);
150		t += n;
151		if (n == 0) break;
152
153		if ((write(fd, buf, n)) != n)
154			err(1, "write");
155	}
156	close(msgsock);
157	close(fd);
158	return;
159}
160
161static void
162writer(void) {
163	struct sockaddr_in inetaddr;
164	struct hostent *hostent;
165	struct stat statb;
166	off_t off = 0;
167	size_t size;
168	int i, r, fd;
169	int tcpsock, on;
170
171	on = 1;
172	for (i = 1; i < 5; i++) {
173		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
174			err(1, "socket(), %s:%d", __FILE__, __LINE__);
175
176		if (setsockopt(tcpsock,
177		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
178			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
179
180		size = getpagesize() -4;
181		if (setsockopt(tcpsock,
182		    SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
183			err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
184			    __LINE__);
185
186		hostent = gethostbyname ("localhost");
187		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
188			sizeof (struct in_addr));
189
190		inetaddr.sin_family = AF_INET;
191		inetaddr.sin_port = htons(port);
192		inetaddr.sin_len = sizeof(inetaddr);
193
194		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
195			sizeof(inetaddr));
196		if (r == 0)
197			break;
198		sleep(1);
199		close(tcpsock);
200	}
201	if (r < 0)
202		err(1, "connect(), %s:%d", __FILE__, __LINE__);
203
204        if (stat(input, &statb) != 0)
205                err(1, "stat(%s)", input);
206
207	if ((fd = open(input, O_RDONLY)) == -1)
208		err(1, "open(%s)", input);
209
210	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
211		err(1, "sendfile");
212
213	return;
214}
215
216static void
217test(void)
218{
219	pid_t pid;
220
221	if ((pid = fork()) == 0) {
222		writer();
223		_exit(0);
224	}
225	reader();
226	kill(pid, SIGINT);
227	if (waitpid(pid, NULL, 0) != pid)
228		err(1, "waitpid(%d)", pid);
229
230	_exit(0);
231}
232
233int
234main(int argc, char *argv[])
235{
236	time_t start;
237	int e, i, j, pids[PARALLEL], status;
238
239	if (argc != 4) {
240		fprintf(stderr, "Usage: %s <input file> <output file>\n",
241		    argv[0]);
242		exit(1);
243	}
244	input = argv[1];
245	output = argv[2];
246	port = atoi(argv[3]);
247	e = 0;
248
249	start = time(NULL);
250	while ((time(NULL) - start) < RUNTIME && e == 0) {
251		for (i = 0; i < PARALLEL; i++) {
252			if ((pids[i] = fork()) == 0)
253				test();
254		}
255		for (j = 0; j < 100; j++) {
256			mess();
257			usleep(arc4random() % 10000);
258		}
259		for (i = 0; i < PARALLEL; i++) {
260			if (waitpid(pids[i], &status, 0) == -1)
261				err(1, "waitpid(%d)", pids[i]);
262			e += status == 0 ? 0 : 1;
263		}
264	}
265
266	return (e);
267}
268