xref: /dflybsd-src/test/stress/stress2/testcases/syscall/syscall.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
386d7f5d3SJohn Marino  * All rights reserved.
486d7f5d3SJohn Marino  *
586d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino  * are met:
886d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino  *
1486d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1586d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1686d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1786d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1886d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1986d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2086d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2186d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2286d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2386d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2486d7f5d3SJohn Marino  * SUCH DAMAGE.
2586d7f5d3SJohn Marino  *
2686d7f5d3SJohn Marino  */
2786d7f5d3SJohn Marino 
2886d7f5d3SJohn Marino /* Call random system calls with random arguments */
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino #include <stdio.h>
3186d7f5d3SJohn Marino #include <stdlib.h>
3286d7f5d3SJohn Marino #include <unistd.h>
3386d7f5d3SJohn Marino #include <fcntl.h>
3486d7f5d3SJohn Marino #include <sys/stat.h>
3586d7f5d3SJohn Marino #include <sys/syscall.h>
3686d7f5d3SJohn Marino #include <sys/resource.h>
3786d7f5d3SJohn Marino #include <err.h>
3886d7f5d3SJohn Marino 
3986d7f5d3SJohn Marino #include "stress.h"
4086d7f5d3SJohn Marino 
4186d7f5d3SJohn Marino static char path[128];
4286d7f5d3SJohn Marino static int num;
4386d7f5d3SJohn Marino static int starting_dir = 0;
4486d7f5d3SJohn Marino 
4586d7f5d3SJohn Marino static int ignore[] = {
4686d7f5d3SJohn Marino 	SYS_syscall,
4786d7f5d3SJohn Marino 	SYS_exit,
4886d7f5d3SJohn Marino 	SYS_fork,
4986d7f5d3SJohn Marino 	11,			/* 11 is obsolete execv */
5086d7f5d3SJohn Marino 	SYS_unmount,
5186d7f5d3SJohn Marino 	SYS_reboot,
5286d7f5d3SJohn Marino 	SYS_vfork,
5386d7f5d3SJohn Marino 	109,			/* 109 is old sigblock */
5486d7f5d3SJohn Marino 	111,			/* 111 is old sigsuspend */
5586d7f5d3SJohn Marino 	SYS_shutdown,
5686d7f5d3SJohn Marino 	SYS___syscall,
5786d7f5d3SJohn Marino 	SYS_rfork,
5886d7f5d3SJohn Marino 	SYS_sigsuspend,
5986d7f5d3SJohn Marino //	SYS_mac_syscall,
6086d7f5d3SJohn Marino 	SYS_sigtimedwait,
6186d7f5d3SJohn Marino 	SYS_sigwaitinfo,
6286d7f5d3SJohn Marino };
6386d7f5d3SJohn Marino 
6486d7f5d3SJohn Marino int
setup(int nb)6586d7f5d3SJohn Marino setup(int nb)
6686d7f5d3SJohn Marino {
6786d7f5d3SJohn Marino 	int i;
6886d7f5d3SJohn Marino 	struct rlimit rl;
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino 	umask(0);
7186d7f5d3SJohn Marino 	sprintf(path,"%s.%05d", getprogname(), getpid());
7286d7f5d3SJohn Marino 	(void)mkdir(path, 0770);
7386d7f5d3SJohn Marino 	if (chdir(path) == -1)
7486d7f5d3SJohn Marino 		err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
7586d7f5d3SJohn Marino 	if ((starting_dir = open(".", 0)) < 0)
7686d7f5d3SJohn Marino 		err(1, ".");
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino 	if (op->argc == 1) {
7986d7f5d3SJohn Marino 		num = atoi(op->argv[0]);
8086d7f5d3SJohn Marino 		for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++)
8186d7f5d3SJohn Marino 			if (num == ignore[i]) {
8286d7f5d3SJohn Marino 				printf("syscall %d is marked a no test!\n", num);
8386d7f5d3SJohn Marino 				exit(1);
8486d7f5d3SJohn Marino 			}
8586d7f5d3SJohn Marino 	} else {
8686d7f5d3SJohn Marino 		num = 0;
8786d7f5d3SJohn Marino 		while (num == 0) {
8886d7f5d3SJohn Marino 			num = random_int(0, SYS_MAXSYSCALL);
8986d7f5d3SJohn Marino 			for (i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++)
9086d7f5d3SJohn Marino 				if (num == ignore[i]) {
9186d7f5d3SJohn Marino 					num = 0;
9286d7f5d3SJohn Marino 					break;
9386d7f5d3SJohn Marino 				}
9486d7f5d3SJohn Marino 		}
9586d7f5d3SJohn Marino 	}
9686d7f5d3SJohn Marino 	if (op->verbose > 1)
9786d7f5d3SJohn Marino 		printf("Testing syscall #%d, pid %d\n", num, getpid());
9886d7f5d3SJohn Marino 
9986d7f5d3SJohn Marino 	/* Multiple parallel core dump may panic the kernel with:
10086d7f5d3SJohn Marino 	   panic: kmem_malloc(184320): kmem_map too small: 84426752 total allocated
10186d7f5d3SJohn Marino 	 */
10286d7f5d3SJohn Marino 	rl.rlim_max = rl.rlim_cur = 0;
10386d7f5d3SJohn Marino 	if (setrlimit(RLIMIT_CORE, &rl) == -1)
10486d7f5d3SJohn Marino 		warn("setrlimit");
10586d7f5d3SJohn Marino 
10686d7f5d3SJohn Marino 	setproctitle("#%d", num);
10786d7f5d3SJohn Marino 
10886d7f5d3SJohn Marino 	return (0);
10986d7f5d3SJohn Marino }
11086d7f5d3SJohn Marino 
11186d7f5d3SJohn Marino void
cleanup(void)11286d7f5d3SJohn Marino cleanup(void)
11386d7f5d3SJohn Marino {
11486d7f5d3SJohn Marino 	if (starting_dir != 0) {
11586d7f5d3SJohn Marino 		if (fchdir(starting_dir) == -1)
11686d7f5d3SJohn Marino 			err(1, "fchdir()");
11786d7f5d3SJohn Marino 		(void)system("find . -type d -exec chmod 777 {} \\;");
11886d7f5d3SJohn Marino 		(void)system("find . -type f -exec chmod 666 {} \\;");
11986d7f5d3SJohn Marino 		(void)system("find . -delete");
12086d7f5d3SJohn Marino 
12186d7f5d3SJohn Marino 		if (chdir("..") == -1)
12286d7f5d3SJohn Marino 			err(1, "chdir(..)");
12386d7f5d3SJohn Marino 		if (rmdir(path) == -1)
12486d7f5d3SJohn Marino 			err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
12586d7f5d3SJohn Marino 	}
12686d7f5d3SJohn Marino 	starting_dir = 0;
12786d7f5d3SJohn Marino }
12886d7f5d3SJohn Marino 
12986d7f5d3SJohn Marino int
test(void)13086d7f5d3SJohn Marino test(void)
13186d7f5d3SJohn Marino {
13286d7f5d3SJohn Marino 	int i;
13386d7f5d3SJohn Marino 	unsigned int arg1, arg2, arg3, arg4, arg5, arg6, arg7;
13486d7f5d3SJohn Marino 
13586d7f5d3SJohn Marino 	for (i = 0; i < 128; i++) {
13686d7f5d3SJohn Marino 		arg1 = arc4random();
13786d7f5d3SJohn Marino 		arg2 = arc4random();
13886d7f5d3SJohn Marino 		arg3 = arc4random();
13986d7f5d3SJohn Marino 		arg4 = arc4random();
14086d7f5d3SJohn Marino 		arg5 = arc4random();
14186d7f5d3SJohn Marino 		arg6 = arc4random();
14286d7f5d3SJohn Marino 		arg7 = arc4random();
14386d7f5d3SJohn Marino 
14486d7f5d3SJohn Marino 		if (op->verbose > 3)
14586d7f5d3SJohn Marino 			printf("%2d : syscall(%3d, %x, %x, %x, %x, %x, %x, %x)\n",
14686d7f5d3SJohn Marino 				i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
14786d7f5d3SJohn Marino 		syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
14886d7f5d3SJohn Marino 	}
14986d7f5d3SJohn Marino #if 0
15086d7f5d3SJohn Marino 	while (done_testing == 0)
15186d7f5d3SJohn Marino 		sleep(1);
15286d7f5d3SJohn Marino #endif
15386d7f5d3SJohn Marino 
15486d7f5d3SJohn Marino 	return (0);
15586d7f5d3SJohn Marino }
156