xref: /freebsd/tools/test/stress2/testcases/run/run.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /* Control program to run all the other test cases */
29 
30 #include <sys/param.h>
31 #include <sys/wait.h>
32 
33 #include <err.h>
34 #include <libgen.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 #include "stress.h"
41 
42 #define MAXAV 10
43 static char *av[MAXAV];
44 static int loop = 1;
45 
46 int
47 setup(int nb __unused)
48 {
49 	return (0);
50 }
51 
52 void
53 cleanup(void)
54 {
55 }
56 
57 static char **
58 mkargv(char *name)
59 {
60 	av[0] = name;
61 	av[1] = 0;
62 	return (av);
63 }
64 
65 static void
66 clean(void)
67 {
68 	char buf[132];
69 
70 	snprintf(buf, sizeof(buf),
71 		"cd %s; rm -rf syscall.[0-9]* fifo.[0-9]* creat.[0-9]* "
72 		"p[0-9]*.d1 df lock", op->cd);
73 	(void)system(buf);
74 }
75 
76 int
77 test(void)
78 {
79 	struct tm *tm;
80 	pid_t *r;
81 	time_t	t;
82 	int i;
83 	int s;
84 	char fullpath[MAXPATHLEN+1];
85 	char ct[80];
86 
87 	r = (pid_t *)malloc(op->argc * sizeof(pid_t));
88 
89 	(void)time(&t);
90 	tm = localtime(&t);
91 	(void) strftime(ct, sizeof(ct), "%T", tm);
92 	printf("%s Loop #%d\n", ct, loop++);
93 	fflush(stdout);
94 
95 	for (i = 0; i < op->argc; i++) {
96 		r[i] = 0;
97 		if (op->argv[i][0] == 0)
98 			continue;
99 		if ((r[i] = fork()) == 0) {
100 			snprintf(fullpath, sizeof(fullpath), "%s/%s", home,
101 				op->argv[i]);
102 			if (execv(fullpath, mkargv(basename(op->argv[i]))) == -1)
103 				err(1, "execl(%s), %s:%d", fullpath, __FILE__,
104 					__LINE__);
105 		}
106 		if (r[i] < 0)
107 			err(1, "fork(), %s:%d", __FILE__, __LINE__);
108 	}
109 	for (i = 0; i < op->argc; i++)
110 		if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
111 			err(1, "waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
112 	free(r);
113 
114 	clean();
115 
116 	return (0);
117 }
118