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 <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <time.h>
35 #include <sys/param.h>
36 #include <sys/wait.h>
37 #include <err.h>
38 
39 #include "stress.h"
40 
41 #define MAXAV 10
42 static char *av[MAXAV];
43 static int loop = 1;
44 
45 int
46 setup(int nb)
47 {
48 	return (0);
49 }
50 
51 void
52 cleanup(void)
53 {
54 }
55 
56 static char **
57 mkargv(char *name)
58 {
59 	av[0] = name;
60 	av[1] = 0;
61 	return (av);
62 }
63 
64 static void
65 clean(void)
66 {
67 	char buf[132];
68 
69 	/* cleanup after the syscall test */
70 	snprintf(buf, sizeof(buf),
71 		"cd %s; find . -type d -mtime -1 -exec chmod 777 {} \\;", op->wd);
72 	(void)system(buf);
73 	snprintf(buf, sizeof(buf),
74 		"cd %s; find . -type f -mtime -1 -exec chmod 666 {} \\;", op->wd);
75 	(void)system(buf);
76 	snprintf(buf, sizeof(buf),
77 		"cd %s; chflags \"nouappnd nouchg nouunlnk\" .", op->wd);
78 	(void)system(buf);
79 	snprintf(buf, sizeof(buf),
80 		"cd %s; rm -rf syscall.[0-9]* fifo.[0-9]* creat.[0-9]* "
81 		"p[0-9]*.d1 df lock", op->cd);
82 	(void)system(buf);
83 }
84 
85 int
86 test(void)
87 {
88 	int i;
89 	int s;
90 	pid_t *r;
91 	char fullpath[MAXPATHLEN+1];
92 	time_t	t;
93 	char ct[80];
94 	struct tm *tm;
95 
96 	r = (pid_t *)malloc(op->argc * sizeof(pid_t));
97 
98 	(void)time(&t);
99 	tm = localtime(&t);
100 	(void) strftime(ct, sizeof(ct), "%T", tm);
101 	printf("%s Loop #%d\n", ct, loop++);
102 
103 	for (i = 0; i < op->argc; i++) {
104 		if ((r[i] = fork()) == 0) {
105 			snprintf(fullpath, sizeof(fullpath), "%s/%s", home,
106 				op->argv[i]);
107 			if (execv(fullpath, mkargv(basename(op->argv[i]))) == -1)
108 				err(1, "execl(%s), %s:%d", fullpath, __FILE__,
109 					__LINE__);
110 		}
111 		if (r[i] < 0)
112 			err(1, "fork(), %s:%d", __FILE__, __LINE__);
113 	}
114 	for (i = 0; i < op->argc; i++)
115 		if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
116 			err(1, "waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
117 	free(r);
118 
119 	clean();
120 
121 	return (0);
122 }
123