1#!/bin/sh 2 3# 4# Copyright (c) 2009 Peter Holm 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 calls with random arguments, in reverse order 32# Variation of the syscall test program. 33 34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 35 36. ../default.cfg 37 38odir=`pwd` 39cd /tmp 40sed '1,/^EOF/d' < $odir/$0 > syscall2.c 41cc -o syscall2 -Wall -I $odir/../include -L $odir/../lib syscall2.c -lstress -lutil 42rm -f syscall2.c 43 44#kldstat -v | grep -q aio || kldload aio 45#kldstat -v | grep -q mqueuefs || kldload mqueuefs 46 47syscall=`grep SYS_MAXSYSCALL /usr/include/sys/syscall.h | awk '{print $NF}'` 48 49n=$syscall 50[ $# -eq 1 ] && n=$1 51 52rm -f /tmp/syscall2.log 53while [ $n -gt 0 ]; do 54 echo "`date '+%T'` syscall $n" | tee /dev/tty >> /tmp/syscall2.log 55 for i in `jot 5`; do 56 su ${testuser} -c "sh -c \"/tmp/syscall2 -t 30s -i 100 -h -l 100 -k $n\"" 57 done 58 chflags -R 0 $RUNDIR 59 rm -rf $RUNDIR 60 n=$((n - 1)) 61done 62rm -f /tmp/syscall2 63exit 64EOF 65 66/* Call random system calls with random arguments */ 67 68#include <stdio.h> 69#include <stdlib.h> 70#include <unistd.h> 71#include <fcntl.h> 72#include <sys/stat.h> 73#include <sys/syscall.h> 74#include <sys/resource.h> 75#include <err.h> 76 77#include "stress.h" 78 79static char path[128]; 80static int num; 81static int starting_dir = 0; 82uint32_t rb[7][10]; 83 84static int ignore[] = { 85 SYS_syscall, 86 SYS_exit, 87 SYS_fork, 88 11, /* 11 is obsolete execv */ 89 SYS_unmount, 90 SYS_reboot, 91 SYS_vfork, 92 109, /* 109 is old sigblock */ 93 111, /* 111 is old sigsuspend */ 94 SYS_shutdown, 95 SYS___syscall, 96 SYS_rfork, 97 SYS_sigsuspend, 98 SYS_mac_syscall, 99 SYS_sigtimedwait, 100 SYS_sigwaitinfo, 101}; 102 103int 104setup(int nb) 105{ 106 int i; 107 struct rlimit rl; 108 109 umask(0); 110 sprintf(path,"%s.%05d", getprogname(), getpid()); 111 (void)mkdir(path, 0770); 112 if (chdir(path) == -1) 113 err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__); 114 if ((starting_dir = open(".", 0)) < 0) 115 err(1, "."); 116 117 if (op->argc == 1) { 118 num = atoi(op->argv[0]); 119 for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++) 120 if (num == ignore[i]) { 121 printf("syscall %d is marked a no test!\n", num); 122 exit(1); 123 } 124 } else { 125 num = 0; 126 while (num == 0) { 127 num = random_int(0, SYS_MAXSYSCALL); 128 for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++) 129 if (num == ignore[i]) { 130 num = 0; 131 break; 132 } 133 } 134 } 135 if (op->verbose > 1) 136 printf("Testing syscall #%d, pid %d\n", num, getpid()); 137 138 /* Multiple parallel core dump may panic the kernel with: 139 panic: kmem_malloc(184320): kmem_map too small: 84426752 total allocated 140 */ 141 rl.rlim_max = rl.rlim_cur = 0; 142 if (setrlimit(RLIMIT_CORE, &rl) == -1) 143 warn("setrlimit"); 144 145 setproctitle("#%d", num); 146 147 return (0); 148} 149 150void 151cleanup(void) 152{ 153 if (starting_dir != 0) { 154 if (fchdir(starting_dir) == -1) 155 err(1, "fchdir()"); 156 (void)system("find . -type d -exec chmod 777 {} \\;"); 157 (void)system("find . -type f -exec chmod 666 {} \\;"); 158 (void)system("find . -delete"); 159 160 if (chdir("..") == -1) 161 err(1, "chdir(..)"); 162 if (rmdir(path) == -1) 163 err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__); 164 } 165 starting_dir = 0; 166} 167 168void 169rainit(void) 170{ 171 int i, j; 172 173 for (i = 0; i < 7; i++) { 174 for (j = 0; j < 10; j++) { 175 if (arc4random() % 100 > 20) 176 rb[i][j] = arc4random(); 177 else 178 rb[i][j] = (uint32_t) &rb[i][j]; 179 } 180 } 181} 182 183uint32_t 184ra(int i) 185{ 186 uint32_t r; 187 188 r = arc4random(); 189 if ((r & 1) == 0) 190 r = arc4random(); 191 else 192 r = (uint32_t) &rb[i][0]; 193 194 return (r); 195} 196 197int 198test(void) 199{ 200 int i; 201 unsigned int arg1, arg2, arg3, arg4, arg5, arg6, arg7; 202 203 for (i = 0; i < 128; i++) { 204 rainit(); 205 arg1 = ra(0); 206 arg2 = ra(1); 207 arg3 = ra(2); 208 arg4 = ra(3); 209 arg5 = ra(4); 210 arg6 = ra(5); 211 arg7 = ra(6); 212 213 if (op->verbose > 3) 214 printf("%2d : syscall(%3d, %x, %x, %x, %x, %x, %x, %x)\n", 215 i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7); 216 syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7); 217 } 218 219 return (0); 220} 221