xref: /freebsd/tools/test/stress2/misc/sendfile10.sh (revision 7be9a3b45356747f9fcb6d69a722c1c95f8060bf)
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# Copy of sendfile8.sh with size validation added.
30
31# No problems seen (after 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/sendfile10.c
40mycc -o sendfile10 -Wall -Wextra -O0 -g sendfile10.c || exit 1
41rm -f sendfile10.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 -n md${mdstart}$part > /dev/null
49mount /dev/md${mdstart}$part $mntpoint
50
51cd $mntpoint
52dd if=/dev/random of=template bs=1m count=50 status=none
53/tmp/sendfile10 template 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/sendfile10
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 volatile off_t *share;
87static int port;
88static char *input, *output;
89
90#define BUFSIZE 4096
91#define MX (100 * 1024 * 1024)
92#define OSZ 1
93#define PARALLEL 1
94#define RUNTIME (2 * 60)
95#define SZ 0
96
97static void
98mess(void)
99{
100	off_t length;
101	int fd;
102
103	if ((fd = open(input, O_RDWR)) == -1)
104		err(1, "open(%s)", input);
105	length = arc4random() % MX;
106	if (ftruncate(fd, length) == -1)
107		err(1, "truncate(%jd)", length);
108	share[SZ] = length;
109	close(fd);
110}
111
112static void
113reader(void) {
114	off_t t __unused;
115	int tcpsock, msgsock;
116	int on;
117	socklen_t len;
118	struct sockaddr_in inetaddr, inetpeer;
119	int n, *buf, fd;
120
121	on = 1;
122	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
123		err(1, "socket(), %s:%d", __FILE__, __LINE__);
124
125	if (setsockopt(tcpsock,
126	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
127		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
128
129	inetaddr.sin_family = AF_INET;
130	inetaddr.sin_addr.s_addr = INADDR_ANY;
131	inetaddr.sin_port = htons(port);
132	inetaddr.sin_len = sizeof(inetaddr);
133
134	if (bind(tcpsock,
135	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
136		err(1, "bind(), %s:%d", __FILE__, __LINE__);
137
138	if (listen(tcpsock, 5) < 0)
139		err(1, "listen(), %s:%d", __FILE__, __LINE__);
140
141	len = sizeof(inetpeer);
142	alarm(10);
143	if ((msgsock = accept(tcpsock,
144	    (struct sockaddr *)&inetpeer, &len)) < 0)
145		err(1, "accept(), %s:%d", __FILE__, __LINE__);
146	alarm(0);
147
148	t = 0;
149	if ((buf = malloc(BUFSIZE)) == NULL)
150		err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
151
152	if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
153		err(1, "open(%s)", output);
154
155	for (;;) {
156		if ((n = read(msgsock, buf, BUFSIZE)) < 0)
157			err(1, "read(), %s:%d", __FILE__, __LINE__);
158		t += n;
159		if (n == 0) break;
160
161		if ((write(fd, buf, n)) != n)
162			err(1, "write");
163	}
164	close(msgsock);
165	close(fd);
166#if 0
167	if (t != share[SZ] && t != share[OSZ]) {
168		fprintf(stderr, "1) Send size %lu, original size %lu, "
169		    "receive size %lu\n",
170		    (unsigned long)share[SZ],
171		    (unsigned long)share[OSZ],
172		    (unsigned long)t);
173		exit(1);
174	}
175#endif
176	return;
177}
178
179static void
180writer(void) {
181	struct sockaddr_in inetaddr;
182	struct hostent *hostent;
183	struct stat statb;
184	off_t off = 0;
185	size_t size;
186	int i, r, fd;
187	int tcpsock, on;
188
189	on = 1;
190	for (i = 1; i < 5; i++) {
191		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
192			err(1, "socket(), %s:%d", __FILE__, __LINE__);
193
194		if (setsockopt(tcpsock,
195		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
196			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
197
198		size = getpagesize() -4;
199		if (setsockopt(tcpsock,
200		    SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
201			err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
202			    __LINE__);
203
204		hostent = gethostbyname ("localhost");
205		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
206			sizeof (struct in_addr));
207
208		inetaddr.sin_family = AF_INET;
209		inetaddr.sin_port = htons(port);
210		inetaddr.sin_len = sizeof(inetaddr);
211
212		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
213			sizeof(inetaddr));
214		if (r == 0)
215			break;
216		sleep(1);
217		close(tcpsock);
218	}
219	if (r < 0)
220		err(1, "connect(), %s:%d", __FILE__, __LINE__);
221
222	if ((fd = open(input, O_RDONLY)) == -1)
223		err(1, "open(%s)", input);
224
225        if (fstat(fd, &statb) != 0)
226                err(1, "stat(%s)", input);
227	share[SZ] = statb.st_size;
228	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
229		err(1, "sendfile");
230	close(fd);
231	close(tcpsock);
232
233	return;
234}
235
236static void
237test(void)
238{
239	pid_t pid;
240
241	if ((pid = fork()) == 0) {
242		writer();
243		_exit(0);
244	}
245	reader();
246	kill(pid, SIGINT);
247	if (waitpid(pid, NULL, 0) != pid)
248		err(1, "waitpid(%d)", pid);
249
250	_exit(0);
251}
252
253int
254main(int argc, char *argv[])
255{
256	struct stat statin, statorig, statout;
257	size_t len;
258	time_t start;
259	int e, i, pids[PARALLEL], status;
260	char help[80], *template;
261
262	if (argc != 5) {
263		fprintf(stderr, "Usage: %s <template> <input file> "
264		    "<output file> <port>\n", argv[0]);
265		exit(1);
266	}
267	template = argv[1];
268	input = argv[2];
269	output = argv[3];
270	port = atoi(argv[4]);
271	snprintf(help, sizeof(help), "cp %s %s", template, input);
272	e = 0;
273	len = PAGE_SIZE;
274	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
275	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
276		err(1, "mmap");
277
278	start = time(NULL);
279	while ((time(NULL) - start) < RUNTIME && e == 0) {
280		system(help);
281		if (stat(input, &statin) == -1)
282			err(1, "stat(%s)", input);
283		share[OSZ] = statin.st_size;
284		for (i = 0; i < PARALLEL; i++) {
285			if ((pids[i] = fork()) == 0)
286				test();
287		}
288		usleep(arc4random() % 10000);
289		mess();
290		for (i = 0; i < PARALLEL; i++) {
291			if (waitpid(pids[i], &status, 0) == -1)
292				err(1, "waitpid(%d)", pids[i]);
293			e += status == 0 ? 0 : 1;
294		}
295		if (stat(template, &statorig) == -1)
296			err(1, "stat(%s)", input);
297		if (stat(input, &statin) == -1)
298			err(1, "stat(%s)", input);
299		if (stat(output, &statout) == -1)
300			err(1, "stat(%s)", output);
301		if (statout.st_size >= MX) {
302			fprintf(stderr, "Send size %lu, original size %lu, "
303			    "receive size %lu\n",
304			    (unsigned long)statin.st_size,
305			    (unsigned long)statorig.st_size,
306			    (unsigned long)statout.st_size);
307			system("ls -l | grep -v total");
308			exit(1);
309		}
310	}
311
312	return (e);
313}
314