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