xref: /freebsd/tools/test/stress2/misc/sendfile3.sh (revision 10ff414c)
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, t, *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	t = 0;
119	if ((buf = malloc(bufsize)) == NULL)
120		err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
121
122	if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
123		err(1, "open(%s)", outputFile);
124
125	for (;;) {
126		if ((n = read(msgsock, buf, bufsize)) < 0)
127			err(1, "read(), %s:%d", __FILE__, __LINE__);
128		t += n;
129		if (n == 0) break;
130
131		if ((write(fd, buf, n)) != n)
132			err(1, "write");
133	}
134	close(msgsock);
135	close(fd);
136	return;
137}
138
139static void
140writer(void) {
141	int tcpsock, on;
142	struct sockaddr_in inetaddr;
143	struct hostent *hostent;
144	struct stat statb;
145	int i, r, fd;
146	off_t off = 0;
147
148	on = 1;
149	for (i = 0; i < 5; i++) {
150		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
151			err(1, "socket(), %s:%d", __FILE__, __LINE__);
152
153		if (setsockopt(tcpsock,
154		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
155			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
156
157		bzero((char *) &inetaddr, sizeof(inetaddr));
158		hostent = gethostbyname ("localhost");
159		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
160			sizeof (struct in_addr));
161
162		inetaddr.sin_family = AF_INET;
163		inetaddr.sin_port = htons(port);
164		inetaddr.sin_len = sizeof(inetaddr);
165
166		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
167			sizeof(inetaddr));
168		if (r == 0)
169			break;
170		sleep(1);
171		close(tcpsock);
172	}
173	if (r < 0)
174		err(1, "connect(), %s:%d", __FILE__, __LINE__);
175
176        if (stat(inputFile, &statb) != 0)
177                err(1, "stat(%s)", inputFile);
178
179	if ((fd = open(inputFile, O_RDONLY)) == -1)
180		err(1, "open(%s)", inputFile);
181
182	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
183		err(1, "sendfile");
184}
185
186int
187main(int argc, char **argv)
188{
189	pid_t pid;
190
191	if (argc != 4) {
192		fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
193			return (1);
194	}
195	inputFile = argv[1];
196	outputFile = argv[2];
197	port = atoi(argv[3]);
198
199	if ((pid = fork()) == 0) {
200		writer();
201		exit(EXIT_SUCCESS);
202	} else if (pid > 0) {
203		reader();
204		kill(pid, SIGINT);
205		waitpid(pid, NULL, 0);
206	} else
207		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
208
209	return (0);
210}
211