4 # Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
31 # Test scenario for sendfile livelock seen on 7.2-STABLE for non SMP
40 sed '1,/^EOF/d' < $odir/$0 > sendfile.c
41 cc -o sendfile -Wall sendfile.c -pthread
43 [ -d "$RUNDIR" ] || mkdir -p $RUNDIR
49 for i in 1 2 3 4 8 16 1k 2k 3k 4k 5k 1m 2m 3m 4m 5m ; do
51 dd if=/dev/random of=$in bs=$i count=1 2>&1 | \
52 egrep -v "records|transferred"
53 /tmp/sendfile $in $out 12345
54 cmp $in $out || ls -l $in $out
63 #include <netinet/in.h>
68 #include <sys/param.h>
69 #include <sys/socket.h>
83 struct sockaddr_in inetaddr, inetpeer;
87 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
88 err(1, "socket(), %s:%d", __FILE__, __LINE__);
90 if (setsockopt(tcpsock,
91 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
92 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
94 inetaddr.sin_family = AF_INET;
95 inetaddr.sin_addr.s_addr = INADDR_ANY;
96 inetaddr.sin_port = htons(port);
97 inetaddr.sin_len = sizeof(inetaddr);
100 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
101 err(1, "bind(), %s:%d", __FILE__, __LINE__);
103 if (listen(tcpsock, 5) < 0)
104 err(1, "listen(), %s:%d", __FILE__, __LINE__);
106 len = sizeof(inetpeer);
107 if ((msgsock = accept(tcpsock,
108 (struct sockaddr *)&inetpeer, &len)) < 0)
109 err(1, "accept(), %s:%d", __FILE__, __LINE__);
112 if ((buf = malloc(bufsize)) == NULL)
113 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
115 if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
116 err(1, "open(%s)", outputFile);
119 if ((n = read(msgsock, buf, bufsize)) < 0)
120 err(1, "read(), %s:%d", __FILE__, __LINE__);
124 if ((write(fd, buf, n)) != n)
135 struct sockaddr_in inetaddr;
136 struct hostent *hostent;
145 for (i = 1; i < 5; i++) {
146 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
147 err(1, "socket(), %s:%d", __FILE__, __LINE__);
149 if (setsockopt(tcpsock,
150 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
151 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
153 #if 1 /* livelock trigger */
154 size = getpagesize() -4;
155 if (setsockopt(tcpsock,
156 SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
157 err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__, __LINE__);
160 hostent = gethostbyname ("localhost");
161 memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
162 sizeof (struct in_addr));
164 inetaddr.sin_family = AF_INET;
165 inetaddr.sin_addr.s_addr = INADDR_ANY;
166 inetaddr.sin_port = htons(port);
167 inetaddr.sin_len = sizeof(inetaddr);
169 r = connect(tcpsock, (struct sockaddr *) &inetaddr,
177 err(1, "connect(), %s:%d", __FILE__, __LINE__);
179 if (stat(inputFile, &statb) != 0)
180 err(1, "stat(%s)", inputFile);
182 if ((fd = open(inputFile, O_RDONLY)) == -1)
183 err(1, "open(%s)", inputFile);
185 if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
192 main(int argc, char **argv)
197 fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
201 outputFile = argv[2];
202 port = atoi(argv[3]);
204 if ((pid = fork()) == 0) {
208 } else if (pid > 0) {
212 err(1, "fork(), %s:%d", __FILE__, __LINE__);