1#!/bin/sh 2 3# 4# Copyright (c) 2017 Dell EMC Isilon 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# "kmem are D+ 0 0:00,10 /tmp/sendfile9 in out 76543" seen. 30# Fixed in r315910 31 32. ../default.cfg 33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 34 35dir=/tmp 36odir=`pwd` 37cd $dir 38sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile9.c 39mycc -o sendfile9 -Wall -Wextra -O0 -g sendfile9.c || exit 1 40rm -f sendfile9.c 41cd $odir 42 43mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 44[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 45mdconfig -a -t swap -s 1g -u $mdstart || exit 1 46newfs $newfs_flags -n md$mdstart > /dev/null 47mount /dev/md$mdstart $mntpoint 48 49cd $mntpoint 50dd if=/dev/random of=in bs=1m count=50 status=none 51/tmp/sendfile9 in out 76543 52s=$? 53cd $odir 54 55for i in `jot 6`; do 56 mount | grep -q "on $mntpoint " || break 57 umount $mntpoint && break || sleep 10 58done 59[ $i -eq 6 ] && exit 1 60mdconfig -d -u $mdstart 61rm -rf /tmp/sendfile9 62exit $s 63 64EOF 65#include <sys/param.h> 66#include <sys/mman.h> 67#include <sys/socket.h> 68#include <sys/stat.h> 69#include <sys/wait.h> 70 71#include <netinet/in.h> 72 73#include <err.h> 74#include <errno.h> 75#include <fcntl.h> 76#include <netdb.h> 77#include <signal.h> 78#include <stdio.h> 79#include <stdlib.h> 80#include <string.h> 81#include <time.h> 82#include <unistd.h> 83 84static int port; 85static char *input, *output; 86 87#define BUFSIZE 4096 88#define PARALLEL 1 89#define RUNTIME (1 * 60) 90 91static void 92mess(void) 93{ 94 off_t length; 95 int fd; 96 97 if ((fd = open(input, O_RDWR)) == -1) 98 err(1, "open(%s)", input); 99 length = arc4random() % 100 * 1024 * 1024; 100 if (ftruncate(fd, length) == -1) 101 err(1, "truncate(%jd)", length); 102 close(fd); 103} 104 105static void 106reader(void) { 107 struct sockaddr_in inetaddr, inetpeer; 108 socklen_t len; 109 int tcpsock, msgsock; 110 int on; 111 int n, *buf, fd; 112 113 on = 1; 114 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 115 err(1, "socket(), %s:%d", __FILE__, __LINE__); 116 117 if (setsockopt(tcpsock, 118 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 119 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 120 121 inetaddr.sin_family = AF_INET; 122 inetaddr.sin_addr.s_addr = INADDR_ANY; 123 inetaddr.sin_port = htons(port); 124 inetaddr.sin_len = sizeof(inetaddr); 125 126 if (bind(tcpsock, 127 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) 128 err(1, "bind(), %s:%d", __FILE__, __LINE__); 129 130 if (listen(tcpsock, 5) < 0) 131 err(1, "listen(), %s:%d", __FILE__, __LINE__); 132 133 len = sizeof(inetpeer); 134 if ((msgsock = accept(tcpsock, 135 (struct sockaddr *)&inetpeer, &len)) < 0) 136 err(1, "accept(), %s:%d", __FILE__, __LINE__); 137 138 if ((buf = malloc(BUFSIZE)) == NULL) 139 err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__); 140 141 if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1) 142 err(1, "open(%s)", output); 143 144 for (;;) { 145 if ((n = read(msgsock, buf, BUFSIZE)) < 0) 146 err(1, "read(), %s:%d", __FILE__, __LINE__); 147 if (n == 0) break; 148 149 if ((write(fd, buf, n)) != n) 150 err(1, "write"); 151 } 152 close(msgsock); 153 close(fd); 154 return; 155} 156 157static void 158writer(void) { 159 struct sockaddr_in inetaddr; 160 struct hostent *hostent; 161 struct stat statb; 162 off_t off = 0; 163 size_t size; 164 int i, r, fd; 165 int tcpsock, on; 166 167 on = 1; 168 for (i = 1; i < 5; i++) { 169 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 170 err(1, "socket(), %s:%d", __FILE__, __LINE__); 171 172 if (setsockopt(tcpsock, 173 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 174 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 175 176 size = getpagesize() -4; 177 if (setsockopt(tcpsock, 178 SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0) 179 err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__, 180 __LINE__); 181 182 hostent = gethostbyname ("localhost"); 183 memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, 184 sizeof (struct in_addr)); 185 186 inetaddr.sin_family = AF_INET; 187 inetaddr.sin_port = htons(port); 188 inetaddr.sin_len = sizeof(inetaddr); 189 190 r = connect(tcpsock, (struct sockaddr *) &inetaddr, 191 sizeof(inetaddr)); 192 if (r == 0) 193 break; 194 sleep(1); 195 close(tcpsock); 196 } 197 if (r < 0) 198 err(1, "connect(), %s:%d", __FILE__, __LINE__); 199 200 if (stat(input, &statb) != 0) 201 err(1, "stat(%s)", input); 202 203 if ((fd = open(input, O_RDONLY)) == -1) 204 err(1, "open(%s)", input); 205 206 if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1) 207 err(1, "sendfile"); 208 209 return; 210} 211 212static void 213test(void) 214{ 215 pid_t pid; 216 217 if ((pid = fork()) == 0) { 218 writer(); 219 _exit(0); 220 } 221 reader(); 222 kill(pid, SIGINT); 223 if (waitpid(pid, NULL, 0) != pid) 224 err(1, "waitpid(%d)", pid); 225 226 _exit(0); 227} 228 229int 230main(int argc, char *argv[]) 231{ 232 time_t start; 233 int e, i, j, pids[PARALLEL], status; 234 235 if (argc != 4) { 236 fprintf(stderr, "Usage: %s <input file> <output file>\n", 237 argv[0]); 238 exit(1); 239 } 240 input = argv[1]; 241 output = argv[2]; 242 port = atoi(argv[3]); 243 e = 0; 244 245 start = time(NULL); 246 while ((time(NULL) - start) < RUNTIME && e == 0) { 247 for (i = 0; i < PARALLEL; i++) { 248 if ((pids[i] = fork()) == 0) 249 test(); 250 } 251 for (j = 0; j < 100; j++) { 252 mess(); 253 usleep(arc4random() % 10000); 254 } 255 for (i = 0; i < PARALLEL; i++) { 256 if (waitpid(pids[i], &status, 0) == -1) 257 err(1, "waitpid(%d)", pids[i]); 258 e += status == 0 ? 0 : 1; 259 } 260 } 261 262 return (e); 263} 264