1 /*- 2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/param.h> 29 #include <sys/socket.h> 30 #include <sys/wait.h> 31 32 #include <netinet/in.h> 33 34 #include <err.h> 35 #include <errno.h> 36 #include <netdb.h> 37 #include <signal.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #include "stress.h" 44 45 #define NB (400 * 1024 * 1024) 46 47 static int port; 48 static int bufsize; 49 50 static void 51 reader(void) { 52 struct sockaddr_in inetaddr, inetpeer; 53 socklen_t len; 54 int on; 55 int n, t, *buf; 56 int tcpsock, msgsock; 57 58 alarm(op->run_time + 30); 59 on = 1; 60 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 61 err(1, "socket(), %s:%d", __FILE__, __LINE__); 62 63 if (setsockopt(tcpsock, 64 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 65 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 66 67 inetaddr.sin_family = AF_INET; 68 inetaddr.sin_addr.s_addr = INADDR_ANY; 69 inetaddr.sin_port = htons(port); 70 inetaddr.sin_len = sizeof(inetaddr); 71 72 if (bind(tcpsock, 73 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) 74 err(1, "bind(), %s:%d", __FILE__, __LINE__); 75 76 if (listen(tcpsock, 5) < 0) 77 err(1, "listen(), %s:%d", __FILE__, __LINE__); 78 79 if ((random_int(1,100) > 60) || (op->hog == 1)) { 80 usleep(random_int(1000000,1000000) * 60); 81 } 82 83 len = sizeof(inetpeer); 84 if ((msgsock = accept(tcpsock, 85 (struct sockaddr *)&inetpeer, &len)) < 0) 86 err(1, "accept(), %s:%d", __FILE__, __LINE__); 87 88 t = 0; 89 if ((buf = malloc(bufsize)) == NULL) 90 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); 91 while (done_testing == 0) { 92 if ((n = read(msgsock, buf, bufsize)) < 0) 93 err(1, "read(), %s:%d", __FILE__, __LINE__); 94 t += n; 95 if (n == 0) break; 96 } 97 close(msgsock); 98 return; 99 } 100 101 static void 102 writer(void) { 103 struct sockaddr_in inetaddr; 104 struct hostent *hostent; 105 int i, *buf, r; 106 int tcpsock, on; 107 108 alarm(op->run_time + 30); 109 on = 1; 110 for (i = 1; i < 5; i++) { 111 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 112 err(1, "socket(), %s:%d", __FILE__, __LINE__); 113 114 if (setsockopt(tcpsock, 115 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 116 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 117 118 hostent = gethostbyname ("localhost"); 119 bzero((char *) &inetaddr, sizeof(inetaddr)); 120 memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, 121 sizeof (struct in_addr)); 122 123 inetaddr.sin_family = AF_INET; 124 inetaddr.sin_port = htons(port); 125 inetaddr.sin_len = sizeof(inetaddr); 126 127 r = connect(tcpsock, (struct sockaddr *) &inetaddr, 128 sizeof(inetaddr)); 129 if (r == 0) 130 break; 131 sleep(1); 132 close(tcpsock); 133 } 134 if (r < 0) 135 err(1, "connect(), %s:%d", __FILE__, __LINE__); 136 137 if ((buf = malloc(bufsize)) == NULL) 138 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); 139 for (i = 0; i < bufsize / (int)sizeof(int); i++) 140 buf[i] = i; 141 142 for (;;) { 143 for (i = 0; i < NB; i+= bufsize) { 144 if (write(tcpsock, buf, bufsize) < 0) { 145 if (errno == EPIPE) 146 return; 147 if (errno != ECONNRESET) 148 err(1, "write(%d), %s:%d", tcpsock, 149 __FILE__, __LINE__); 150 _exit(EXIT_SUCCESS); 151 } 152 } 153 } 154 return; 155 } 156 157 int 158 setup(int nb) 159 { 160 port = 12340 + nb; 161 bufsize = 2 << random_int(1, 12); 162 return (0); 163 } 164 165 void 166 cleanup(void) 167 { 168 } 169 170 int 171 test(void) 172 { 173 pid_t pid; 174 175 if ((pid = fork()) == 0) { 176 writer(); 177 _exit(EXIT_SUCCESS); 178 179 } else if (pid > 0) { 180 reader(); 181 kill(pid, SIGINT); 182 if (waitpid(pid, NULL, 0) != pid) 183 err(1, "waitpid(%d)", pid); 184 } else 185 err(1, "fork(), %s:%d", __FILE__, __LINE__); 186 187 return (0); 188 } 189