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