Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / pounder21 / src / randasyscall / randasys.c
blob3133ca97770c7e445a3a0a36a0c74fae74c2384d
1 /*
2 * Program that makes random system calls with random arguments.
3 */
5 /*
6 * Copyright (C) 2003-2006 IBM
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
24 #include <signal.h>
25 #include <limits.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <syscall.h>
33 #include <stdint.h>
34 #include <stdlib.h>
36 unsigned long callnum, args[6];
38 int seed_random(void) {
39 int fp;
40 long seed;
42 fp = open("/dev/urandom", O_RDONLY);
43 if (fp < 0) {
44 perror("/dev/urandom");
45 return 0;
48 if (read(fp, &seed, sizeof(seed)) != sizeof(seed)) {
49 perror("read random seed");
50 return 0;
53 close(fp);
54 srand(seed);
56 return 1;
59 void get_big_randnum(void *buf, unsigned int size) {
60 uint32_t *x = buf;
61 int i;
63 for (i = 0; i < size; i += 4, x++) {
64 *x = (unsigned long)((float)UINT_MAX * (rand() / (RAND_MAX + 1.0)));
68 unsigned long get_randnum(unsigned long min, unsigned long max) {
69 return min + (unsigned long)((float)max * (rand() / (RAND_MAX + 1.0)));
72 int find_syscall(void) {
73 int x;
75 badcall:
76 x = get_randnum(0, 384);
78 /* poorly implemented blacklist */
79 switch (x) {
80 /* don't screw with signal handling */
81 #ifdef SYS_signal
82 case SYS_signal:
83 #endif
84 #ifdef SYS_sigaction
85 case SYS_sigaction:
86 #endif
87 #ifdef SYS_sigsuspend
88 case SYS_sigsuspend:
89 #endif
90 #ifdef SYS_sigpending
91 case SYS_sigpending:
92 #endif
93 #ifdef SYS_sigreturn
94 case SYS_sigreturn:
95 #endif
96 #ifdef SYS_sigprocmask
97 case SYS_sigprocmask:
98 #endif
99 #ifdef SYS_rt_sigreturn
100 case SYS_rt_sigreturn:
101 #endif
102 #ifdef SYS_rt_sigaction
103 case SYS_rt_sigaction:
104 #endif
105 #ifdef SYS_rt_sigprocmask
106 case SYS_rt_sigprocmask:
107 #endif
108 #ifdef SYS_rt_sigpending
109 case SYS_rt_sigpending:
110 #endif
111 #ifdef SYS_rt_sigtimedwait
112 case SYS_rt_sigtimedwait:
113 #endif
114 #ifdef SYS_rt_sigqueueinfo
115 case SYS_rt_sigqueueinfo:
116 #endif
117 #ifdef SYS_rt_sigsuspend
118 case SYS_rt_sigsuspend:
119 #endif
120 #ifdef SYS_sigaltstack
121 case SYS_sigaltstack:
122 #endif
123 #ifdef SYS_settimeofday
124 case SYS_settimeofday:
125 #endif
127 /* don't exit the program :P */
128 #ifdef SYS_exit
129 case SYS_exit:
130 #endif
131 #ifdef SYS_exit_group
132 case SYS_exit_group:
133 #endif
135 /* don't put it to sleep either */
136 #ifdef SYS_pause
137 case SYS_pause:
138 #endif
139 #ifdef SYS_select
140 case SYS_select:
141 #endif
142 #ifdef SYS_read
143 case SYS_read:
144 #endif
145 #ifdef SYS_write
146 case SYS_write:
147 #endif
149 /* these can fill the process table */
150 #ifdef SYS_fork
151 case SYS_fork:
152 #endif
153 #ifdef SYS_vfork
154 case SYS_vfork:
155 #endif
156 #ifdef SYS_clone
157 case SYS_clone:
158 #endif
160 /* This causes OOM conditions */
161 #if 1
162 #ifdef SYS_brk
163 case SYS_brk:
164 #endif
165 #endif
167 /* these get our program killed */
168 #ifdef SYS_vm86
169 case SYS_vm86:
170 #endif
171 #ifdef SYS_vm86old
172 case SYS_vm86old:
173 #endif
174 goto badcall;
177 return x;
180 void bogus_signal_handler(int signum) {
181 fprintf(stderr, " Signal %d on syscall(%lu, 0x%lX, 0x%lX, 0x%lX, 0x%lX, 0x%lX, 0x%lX).\n",
182 signum, callnum, args[0], args[1], args[2], args[3],
183 args[4], args[5]);
186 void real_signal_handler(int signum) {
187 exit(0);
190 void install_signal_handlers(void) {
191 int x;
192 struct sigaction zig;
194 memset(&zig, 0x00, sizeof(zig));
195 zig.sa_handler = bogus_signal_handler;
196 for (x = 0; x < 64; x++) {
197 sigaction(x, &zig, NULL);
200 zig.sa_handler = real_signal_handler;
201 sigaction(SIGINT, &zig, NULL);
202 sigaction(SIGTERM, &zig, NULL);
205 int main(int argc, char *argv[]) {
206 int i;
207 int debug = 0, zero_mode = 0;
209 if (!seed_random()) {
210 return 1;
213 for (i = 1; i < argc; i++) {
214 if (!strcmp(argv[i], "-d"))
215 debug = 1;
216 else if(!strcmp(argv[i], "-z"))
217 zero_mode = 1;
220 memset(args, 0, sizeof(unsigned long) * 6);
222 install_signal_handlers();
224 while(1) {
225 callnum = find_syscall();
226 if (!zero_mode)
227 get_big_randnum(&args[0], sizeof(unsigned long) * 6);
229 if (debug) {
230 printf("syscall(%lu, 0x%lX, 0x%lX, 0x%lX, 0x%lX, "
231 "0x%lX, 0x%lX); \n",
232 callnum, args[0], args[1], args[2], args[3],
233 args[4], args[5]);
234 fflush(stdout);
237 syscall(callnum, args[0], args[1], args[2],
238 args[3], args[4], args[5]);
241 return 0;