xref: /freebsd/tools/test/stress2/misc/sendfile.sh (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
1#!/bin/sh
2
3#
4# Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
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# Test scenario for sendfile livelock seen on 7.2-STABLE for non SMP
30
31# Scenario by kib@
32
33. ../default.cfg
34
35odir=`pwd`
36
37cd /tmp
38sed '1,/^EOF/d' < $odir/$0 > sendfile.c
39mycc -o sendfile -Wall -O0 sendfile.c -pthread
40rm -f sendfile.c
41[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
42cd $RUNDIR
43
44in=inputFile
45out=outputFile
46
47for i in 1 2 3 4 8 16 1k 2k 3k 4k 5k 1m 2m 3m 4m 5m ; do
48	rm -f $in $out
49	dd if=/dev/random of=$in bs=$i count=1 status=none
50	/tmp/sendfile $in $out 12345
51	cmp $in $out || { echo FAIL; ls -l $in $out; }
52	rm -f $in $out
53done
54rm -f /tmp/sendfile
55exit
56EOF
57#include <err.h>
58#include <fcntl.h>
59#include <netdb.h>
60#include <netinet/in.h>
61#include <signal.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <sys/param.h>
66#include <sys/socket.h>
67#include <sys/stat.h>
68#include <unistd.h>
69
70int port;
71char *inputFile;
72char *outputFile;
73int bufsize = 4096;
74
75static void
76reader(void) {
77	int tcpsock, msgsock;
78	int on;
79	socklen_t len;
80	struct sockaddr_in inetaddr, inetpeer;
81	int n, t __unused, *buf, fd;
82
83	on = 1;
84	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
85		err(1, "socket(), %s:%d", __FILE__, __LINE__);
86
87	if (setsockopt(tcpsock,
88	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
89		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
90
91	inetaddr.sin_family = AF_INET;
92	inetaddr.sin_addr.s_addr = INADDR_ANY;
93	inetaddr.sin_port = htons(port);
94	inetaddr.sin_len = sizeof(inetaddr);
95
96	if (bind(tcpsock,
97	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
98		err(1, "bind(), %s:%d", __FILE__, __LINE__);
99
100	if (listen(tcpsock, 5) < 0)
101		err(1, "listen(), %s:%d", __FILE__, __LINE__);
102
103	len = sizeof(inetpeer);
104	if ((msgsock = accept(tcpsock,
105	    (struct sockaddr *)&inetpeer, &len)) < 0)
106		err(1, "accept(), %s:%d", __FILE__, __LINE__);
107
108	t = 0;
109	if ((buf = malloc(bufsize)) == NULL)
110		err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
111
112	if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
113		err(1, "open(%s)", outputFile);
114
115	for (;;) {
116		if ((n = read(msgsock, buf, bufsize)) < 0)
117			err(1, "read(), %s:%d", __FILE__, __LINE__);
118		t += n;
119		if (n == 0)
120			break;
121
122		if ((write(fd, buf, n)) != n)
123			err(1, "write");
124	}
125	close(msgsock);
126	close(fd);
127	return;
128}
129
130static void
131writer(void) {
132	int tcpsock, on;
133	struct sockaddr_in inetaddr;
134	struct hostent *hostent;
135	struct stat statb;
136	int i, r, fd;
137	off_t off = 0;
138#if 1
139	size_t size;
140#endif
141
142	on = 1;
143	for (i = 1; i < 5; i++) {
144		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
145			err(1, "socket(), %s:%d", __FILE__, __LINE__);
146
147		if (setsockopt(tcpsock,
148		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
149			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
150
151#if 1		/* livelock trigger */
152		size = getpagesize() -4;
153		if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
154		    sizeof(size)) < 0)
155			err(1, "setsockopt(SO_SNDBUF), %s:%d",
156			    __FILE__, __LINE__);
157#endif
158
159		hostent = gethostbyname ("localhost");
160		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
161			sizeof (struct in_addr));
162
163		inetaddr.sin_family = AF_INET;
164		inetaddr.sin_port = htons(port);
165		inetaddr.sin_len = sizeof(inetaddr);
166
167		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
168			sizeof(inetaddr));
169		if (r == 0)
170			break;
171		sleep(1);
172		close(tcpsock);
173	}
174	if (r < 0)
175		err(1, "connect(), %s:%d", __FILE__, __LINE__);
176
177        if (stat(inputFile, &statb) != 0)
178                err(1, "stat(%s)", inputFile);
179
180	if ((fd = open(inputFile, O_RDONLY)) == -1)
181		err(1, "open(%s)", inputFile);
182
183	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
184		err(1, "sendfile");
185
186	return;
187}
188
189int
190main(int argc, char **argv)
191{
192	pid_t pid;
193
194	if (argc != 4) {
195		fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
196		    argv[0]);
197		return (1);
198	}
199	inputFile = argv[1];
200	outputFile = argv[2];
201	port = atoi(argv[3]);
202
203	if ((pid = fork()) == 0) {
204		writer();
205		exit(EXIT_SUCCESS);
206	} else if (pid > 0) {
207		reader();
208		kill(pid, SIGINT);
209	} else
210		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
211
212	return (0);
213}
214