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