1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino * are met:
8*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino *
14*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino * SUCH DAMAGE.
25*86d7f5d3SJohn Marino *
26*86d7f5d3SJohn Marino */
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino /* Call random system calls with random arguments */
29*86d7f5d3SJohn Marino
30*86d7f5d3SJohn Marino #include <stdio.h>
31*86d7f5d3SJohn Marino #include <stdlib.h>
32*86d7f5d3SJohn Marino #include <unistd.h>
33*86d7f5d3SJohn Marino #include <fcntl.h>
34*86d7f5d3SJohn Marino #include <sys/stat.h>
35*86d7f5d3SJohn Marino #include <sys/syscall.h>
36*86d7f5d3SJohn Marino #include <sys/resource.h>
37*86d7f5d3SJohn Marino #include <err.h>
38*86d7f5d3SJohn Marino
39*86d7f5d3SJohn Marino #include "stress.h"
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino static char path[128];
42*86d7f5d3SJohn Marino static int num;
43*86d7f5d3SJohn Marino static int starting_dir = 0;
44*86d7f5d3SJohn Marino
45*86d7f5d3SJohn Marino static int ignore[] = {
46*86d7f5d3SJohn Marino SYS_syscall,
47*86d7f5d3SJohn Marino SYS_exit,
48*86d7f5d3SJohn Marino SYS_fork,
49*86d7f5d3SJohn Marino 11, /* 11 is obsolete execv */
50*86d7f5d3SJohn Marino SYS_unmount,
51*86d7f5d3SJohn Marino SYS_reboot,
52*86d7f5d3SJohn Marino SYS_vfork,
53*86d7f5d3SJohn Marino 109, /* 109 is old sigblock */
54*86d7f5d3SJohn Marino 111, /* 111 is old sigsuspend */
55*86d7f5d3SJohn Marino SYS_shutdown,
56*86d7f5d3SJohn Marino SYS___syscall,
57*86d7f5d3SJohn Marino SYS_rfork,
58*86d7f5d3SJohn Marino SYS_sigsuspend,
59*86d7f5d3SJohn Marino // SYS_mac_syscall,
60*86d7f5d3SJohn Marino SYS_sigtimedwait,
61*86d7f5d3SJohn Marino SYS_sigwaitinfo,
62*86d7f5d3SJohn Marino };
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino int
setup(int nb)65*86d7f5d3SJohn Marino setup(int nb)
66*86d7f5d3SJohn Marino {
67*86d7f5d3SJohn Marino int i;
68*86d7f5d3SJohn Marino struct rlimit rl;
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino umask(0);
71*86d7f5d3SJohn Marino sprintf(path,"%s.%05d", getprogname(), getpid());
72*86d7f5d3SJohn Marino (void)mkdir(path, 0770);
73*86d7f5d3SJohn Marino if (chdir(path) == -1)
74*86d7f5d3SJohn Marino err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
75*86d7f5d3SJohn Marino if ((starting_dir = open(".", 0)) < 0)
76*86d7f5d3SJohn Marino err(1, ".");
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino if (op->argc == 1) {
79*86d7f5d3SJohn Marino num = atoi(op->argv[0]);
80*86d7f5d3SJohn Marino for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++)
81*86d7f5d3SJohn Marino if (num == ignore[i]) {
82*86d7f5d3SJohn Marino printf("syscall %d is marked a no test!\n", num);
83*86d7f5d3SJohn Marino exit(1);
84*86d7f5d3SJohn Marino }
85*86d7f5d3SJohn Marino } else {
86*86d7f5d3SJohn Marino num = 0;
87*86d7f5d3SJohn Marino while (num == 0) {
88*86d7f5d3SJohn Marino num = random_int(0, SYS_MAXSYSCALL);
89*86d7f5d3SJohn Marino for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++)
90*86d7f5d3SJohn Marino if (num == ignore[i]) {
91*86d7f5d3SJohn Marino num = 0;
92*86d7f5d3SJohn Marino break;
93*86d7f5d3SJohn Marino }
94*86d7f5d3SJohn Marino }
95*86d7f5d3SJohn Marino }
96*86d7f5d3SJohn Marino if (op->verbose > 1)
97*86d7f5d3SJohn Marino printf("Testing syscall #%d, pid %d\n", num, getpid());
98*86d7f5d3SJohn Marino
99*86d7f5d3SJohn Marino /* Multiple parallel core dump may panic the kernel with:
100*86d7f5d3SJohn Marino panic: kmem_malloc(184320): kmem_map too small: 84426752 total allocated
101*86d7f5d3SJohn Marino */
102*86d7f5d3SJohn Marino rl.rlim_max = rl.rlim_cur = 0;
103*86d7f5d3SJohn Marino if (setrlimit(RLIMIT_CORE, &rl) == -1)
104*86d7f5d3SJohn Marino warn("setrlimit");
105*86d7f5d3SJohn Marino
106*86d7f5d3SJohn Marino setproctitle("#%d", num);
107*86d7f5d3SJohn Marino
108*86d7f5d3SJohn Marino return (0);
109*86d7f5d3SJohn Marino }
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marino void
cleanup(void)112*86d7f5d3SJohn Marino cleanup(void)
113*86d7f5d3SJohn Marino {
114*86d7f5d3SJohn Marino if (starting_dir != 0) {
115*86d7f5d3SJohn Marino if (fchdir(starting_dir) == -1)
116*86d7f5d3SJohn Marino err(1, "fchdir()");
117*86d7f5d3SJohn Marino (void)system("find . -type d -exec chmod 777 {} \\;");
118*86d7f5d3SJohn Marino (void)system("find . -type f -exec chmod 666 {} \\;");
119*86d7f5d3SJohn Marino (void)system("find . -delete");
120*86d7f5d3SJohn Marino
121*86d7f5d3SJohn Marino if (chdir("..") == -1)
122*86d7f5d3SJohn Marino err(1, "chdir(..)");
123*86d7f5d3SJohn Marino if (rmdir(path) == -1)
124*86d7f5d3SJohn Marino err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
125*86d7f5d3SJohn Marino }
126*86d7f5d3SJohn Marino starting_dir = 0;
127*86d7f5d3SJohn Marino }
128*86d7f5d3SJohn Marino
129*86d7f5d3SJohn Marino int
test(void)130*86d7f5d3SJohn Marino test(void)
131*86d7f5d3SJohn Marino {
132*86d7f5d3SJohn Marino int i;
133*86d7f5d3SJohn Marino unsigned int arg1, arg2, arg3, arg4, arg5, arg6, arg7;
134*86d7f5d3SJohn Marino
135*86d7f5d3SJohn Marino for (i = 0; i < 128; i++) {
136*86d7f5d3SJohn Marino arg1 = arc4random();
137*86d7f5d3SJohn Marino arg2 = arc4random();
138*86d7f5d3SJohn Marino arg3 = arc4random();
139*86d7f5d3SJohn Marino arg4 = arc4random();
140*86d7f5d3SJohn Marino arg5 = arc4random();
141*86d7f5d3SJohn Marino arg6 = arc4random();
142*86d7f5d3SJohn Marino arg7 = arc4random();
143*86d7f5d3SJohn Marino
144*86d7f5d3SJohn Marino if (op->verbose > 3)
145*86d7f5d3SJohn Marino printf("%2d : syscall(%3d, %x, %x, %x, %x, %x, %x, %x)\n",
146*86d7f5d3SJohn Marino i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
147*86d7f5d3SJohn Marino syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
148*86d7f5d3SJohn Marino }
149*86d7f5d3SJohn Marino #if 0
150*86d7f5d3SJohn Marino while (done_testing == 0)
151*86d7f5d3SJohn Marino sleep(1);
152*86d7f5d3SJohn Marino #endif
153*86d7f5d3SJohn Marino
154*86d7f5d3SJohn Marino return (0);
155*86d7f5d3SJohn Marino }
156