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 /* Main program for all test programs */
29
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32
33 #include <stdio.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <err.h>
40 #include <errno.h>
41
42 #include "stress.h"
43
44 volatile int done_testing;
45 static int cleanupcalled = 0;
46 char *home;
47
48 static pid_t *r;
49
50 static void
handler(int i __unused)51 handler(int i __unused)
52 {
53 int j;
54
55 done_testing = 1;
56 for (j = 0; j < op->incarnations; j++) {
57 if (op->verbose > 2)
58 printf("handler: kill -HUP %d\n", r[j]);
59 if (r[j] != 0 && kill(r[j], SIGHUP) == -1)
60 if (errno != ESRCH)
61 warn("kill(%d, SIGHUP), %s:%d", r[j], __FILE__, __LINE__);
62 }
63 if (op->kill == 1) {
64 sleep(5);
65 /* test programs may have blocked for the SIGHUP, so try harder */
66 for (j = 0; j < op->incarnations; j++) {
67 if (op->verbose > 2)
68 printf("handler: kill -KILL %d\n", r[j]);
69 if (r[j] != 0)
70 (void) kill(r[j], SIGKILL);
71 }
72 }
73 }
74
75 static void
run_test_handler(int i __unused)76 run_test_handler(int i __unused)
77 {
78
79 done_testing = 1;
80 }
81
82 static void
exit_handler(int i __unused)83 exit_handler(int i __unused)
84 {
85
86 _exit(1);
87 }
88
89 static void
callcleanup(void)90 callcleanup(void)
91 {
92 if (cleanupcalled == 0)
93 cleanup();
94 cleanupcalled = 1;
95 }
96
97 static void
run_tests(int i)98 run_tests(int i)
99 {
100 time_t start;
101 int e;
102
103 signal(SIGHUP, run_test_handler);
104 signal(SIGINT, exit_handler);
105 atexit(callcleanup);
106 setup(i);
107 if ((strcmp(getprogname(), "run") != 0) && (op->nodelay == 0))
108 sleep(random_int(1,10));
109 e = 0;
110 start = time(NULL);
111 while (done_testing == 0 && e == 0 &&
112 (time(NULL) - start) < op->run_time) {
113 e = test();
114 }
115 callcleanup();
116 exit(e);
117 }
118
119 static void
run_incarnations(void)120 run_incarnations(void)
121 {
122 int e, i, s;
123
124 e = 0;
125 signal(SIGHUP, handler);
126 for (i = 0; i < op->incarnations && done_testing == 0; i++) {
127 if ((r[i] = fork()) == 0) {
128 run_tests(i);
129 }
130 if (r[i] < 0) {
131 warn("fork(), %s:%d", __FILE__, __LINE__);
132 r[i] = 0;
133 break;
134 }
135 }
136 for (i = 0; i < op->incarnations; i++) {
137 if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
138 warn("waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
139 if (s != 0)
140 e = 1;
141 }
142
143 exit(e);
144 }
145
146 static int
run_test(void)147 run_test(void)
148 {
149 pid_t p;
150 time_t start;
151 int status = 0;
152
153 if (random_int(1,100) > op->load)
154 return (status);
155
156 show_status();
157
158 start = time(NULL);
159 done_testing = 0;
160 fflush(stdout);
161 rmval();
162 p = fork();
163 if (p == 0)
164 run_incarnations();
165 if (p < 0)
166 err(1, "fork() in %s:%d", __FILE__, __LINE__);
167 while (done_testing != 1 &&
168 (time(NULL) - start) < op->run_time) {
169 sleep(1);
170 if (waitpid(p, &status, WNOHANG) == p)
171 return (status != 0);
172 }
173 if (kill(p, SIGHUP) == -1)
174 warn("kill(%d, SIGHUP), %s:%d", p, __FILE__, __LINE__);
175
176 if (waitpid(p, &status, 0) == -1)
177 err(1, "waitpid(%d), %s:%d", p, __FILE__, __LINE__);
178
179 return (status != 0);
180 }
181
182 int
main(int argc,char ** argv)183 main(int argc, char **argv)
184 {
185 struct stat sb;
186 int status = 0;
187
188 options(argc, argv);
189
190 umask(0);
191 if (stat(op->wd, &sb) == -1) {
192 if (mkdir(op->wd, 0770) == -1)
193 if (errno != EEXIST)
194 err(1, "mkdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
195 } else if ((sb.st_mode & S_IRWXU) == 0)
196 errx(1, "No RWX access to %s", op->wd);
197 if (stat(op->cd, &sb) == -1) {
198 if (mkdir(op->cd, 0770) == -1)
199 if (errno != EEXIST)
200 err(1, "mkdir(%s) %s:%d", op->cd, __FILE__, __LINE__);
201 }
202 if ((home = getcwd(NULL, 0)) == NULL)
203 err(1, "getcwd(), %s:%d", __FILE__, __LINE__);
204 if (chdir(op->wd) == -1)
205 err(1, "chdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
206
207 r = (pid_t *)calloc(1, op->incarnations * sizeof(pid_t));
208
209 status = run_test();
210
211 return (status);
212 }
213