xref: /freebsd/tools/test/stress2/misc/sendfile12.sh (revision 8d72c409cd8591e600758d4be5396bb9038db907)
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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Test scenario suggestion by alc@
32
33# sendfile(2) using SF_NOCACHE and a mmap'ed file with touched pages.
34# "panic: vm_page_free_toq: freeing mapped page 0xc49e53bc" seen:
35# https://people.freebsd.org/~pho/stress/log/sendfile12.txt
36
37. ../default.cfg
38
39odir=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $odir/$0 > sendfile12.c
42mycc -o sendfile12 -Wall -O0 sendfile12.c -pthread || exit 1
43rm -f sendfile12.c
44
45set -e
46mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
47[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 2g -u $mdstart
49newfs $newfs_flags /dev/md$mdstart > /dev/null
50
51mount /dev/md${mdstart} $mntpoint
52chmod 777 $mntpoint
53set +e
54
55cd $mntpoint
56dd if=/dev/zero of=file bs=1m count=512 status=none
57(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100) > /dev/null &
58sleep 5
59/tmp/sendfile12 file output 12345; s=$?
60cd $odir
61while pkill swap; do
62	sleep 1
63done
64wait
65
66while mount | grep $mntpoint | grep -q /dev/md; do
67	umount $mntpoint || sleep 1
68done
69checkfs /dev/md${mdstart} || s=$?
70mdconfig -d -u $mdstart
71rm -f /tmp/sendfile12
72exit $s
73EOF
74#include <sys/param.h>
75#include <sys/mman.h>
76#include <sys/socket.h>
77#include <sys/stat.h>
78#include <sys/wait.h>
79
80#include <netinet/in.h>
81
82#include <err.h>
83#include <fcntl.h>
84#include <netdb.h>
85#include <signal.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90
91#define BUFSIZE 8192
92
93static int port;
94static char *inputFile;
95static char *outputFile;
96
97static void
98reader(void) {
99	struct sockaddr_in inetaddr, inetpeer;
100	socklen_t len;
101	int tcpsock, msgsock;
102	int *buf, fd, n, on, t __unused;
103
104	on = 1;
105	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
106		err(1, "socket(), %s:%d", __FILE__, __LINE__);
107
108	if (setsockopt(tcpsock,
109	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
110		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
111
112	inetaddr.sin_family = AF_INET;
113	inetaddr.sin_addr.s_addr = INADDR_ANY;
114	inetaddr.sin_port = htons(port);
115	inetaddr.sin_len = sizeof(inetaddr);
116
117	if (bind(tcpsock,
118	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
119		err(1, "bind(), %s:%d", __FILE__, __LINE__);
120
121	if (listen(tcpsock, 5) < 0)
122		err(1, "listen(), %s:%d", __FILE__, __LINE__);
123
124	len = sizeof(inetpeer);
125	if ((msgsock = accept(tcpsock,
126	    (struct sockaddr *)&inetpeer, &len)) < 0)
127		err(1, "accept(), %s:%d", __FILE__, __LINE__);
128
129	t = 0;
130	if ((buf = malloc(BUFSIZE)) == NULL)
131		err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
132
133	if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) ==
134	    -1)
135		err(1, "open(%s)", outputFile);
136
137	for (;;) {
138		if ((n = read(msgsock, buf, BUFSIZE)) < 0)
139			err(1, "read(), %s:%d", __FILE__, __LINE__);
140		t += n;
141		if (n == 0) break;
142
143		if ((write(fd, buf, n)) != n)
144			err(1, "write");
145	}
146	close(msgsock);
147	close(fd);
148	return;
149}
150
151static void
152writer(void) {
153	struct sockaddr_in inetaddr;
154	struct hostent *hostent;
155	struct stat statb;
156	off_t off = 0;
157	size_t size;
158	int i, fd, on, r, tcpsock;
159	char *cp;
160
161	on = 1;
162	for (i = 1; i < 5; i++) {
163		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
164			err(1, "socket(), %s:%d", __FILE__, __LINE__);
165
166		if (setsockopt(tcpsock,
167		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
168			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
169
170		size = getpagesize() -4;
171		if (setsockopt(tcpsock,
172		    SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
173			err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
174			    __LINE__);
175
176		hostent = gethostbyname ("localhost");
177		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
178			sizeof (struct in_addr));
179
180		inetaddr.sin_family = AF_INET;
181		inetaddr.sin_port = htons(port);
182		inetaddr.sin_len = sizeof(inetaddr);
183
184		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
185			sizeof(inetaddr));
186		if (r == 0)
187			break;
188		sleep(1);
189		close(tcpsock);
190	}
191	if (r < 0)
192		err(1, "connect(), %s:%d", __FILE__, __LINE__);
193
194        if (stat(inputFile, &statb) != 0)
195                err(1, "stat(%s)", inputFile);
196
197	if ((fd = open(inputFile, O_RDWR)) == -1)
198		err(1, "open(%s)", inputFile);
199
200	if ((cp = mmap(NULL, statb.st_size, PROT_READ | PROT_WRITE,
201	    MAP_SHARED, fd, 0)) == MAP_FAILED)
202		err(1, "mmap");
203	cp[0] = 1;
204
205	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, SF_NOCACHE)
206	    == -1)
207		err(1, "sendfile");
208
209	return;
210}
211
212int
213main(int argc, char **argv)
214{
215	pid_t pid;
216	int e, s;
217
218	if (argc != 4) {
219		fprintf(stderr,
220		    "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
221			return (1);
222	}
223	e = 0;
224	inputFile = argv[1];
225	outputFile = argv[2];
226	port = atoi(argv[3]);
227
228	if ((pid = fork()) == 0) {
229		writer();
230		exit(EXIT_SUCCESS);
231	} else if (pid > 0) {
232		reader();
233		kill(pid, SIGINT);
234		waitpid(pid, &s, 0);
235		if (s != 0)
236			e = 1;
237	} else
238		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
239
240	return (e);
241}
242