2 * Copyright (c) 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <sys/types.h>
57 static int num_children
= 50;
59 static sig_atomic_t set_alarm
= 0;
60 static sig_atomic_t dead_children
= 0;
61 static int do_children
= 0;
66 signal(SIGALRM
, sigalrm
);
75 signal(SIGALRM
, sigalrm
);
77 wait3(&status
, WNOHANG
, NULL
);
83 try_read(const char *filename
)
87 fd
= open(filename
, O_RDONLY
);
90 err(1, "open %s was interrupted", filename
);
97 find(const char *dirname
)
102 dir
= opendir(dirname
);
104 err (1, "opendir %s", dirname
);
105 while ((dp
= readdir(dir
)) != NULL
) {
106 char fname
[MAXPATHLEN
];
113 if (strcmp (dp
->d_name
, ".") == 0
114 || strcmp (dp
->d_name
, "..") == 0)
116 snprintf(fname
, sizeof(fname
), "%s/%s", dirname
, dp
->d_name
);
117 if (lstat(fname
, &sb
) < 0)
118 err(1, "stat %s", fname
);
119 if (S_ISDIR(sb
.st_mode
))
128 main(int argc
, char **argv
)
131 pid_t
*children
= NULL
;
134 setprogname (argv
[0]);
139 sa
.sa_handler
= sigalrm
;
140 sigfillset(&sa
.sa_mask
);
142 sigaction(SIGALRM
, &sa
, NULL
);
144 sa
.sa_handler
= sigchld
;
145 sigfillset(&sa
.sa_mask
);
147 sigaction(SIGCHLD
, &sa
, NULL
);
150 if (strcmp(argv
[1], "--alarm") == 0)
152 else if (strcmp(argv
[1], "--child") == 0)
161 * fork child, child figure out how long to sleep (max 13 seconds)
162 * every second the child checks if the parent is alive or to make
163 * sure it don't stay around too long
169 children
= emalloc(sizeof(pid_t
) * num_children
);
173 for (i
= 0 ; i
< num_children
; i
++) {
177 srandom(getpid() * time(NULL
));
178 i
= random() % 10 + 3;
181 if (kill(ppid
, 0) < 0)
198 * On Windows, the pwd of the children will be in the test
199 * directory and then it can't be removed. Also its seems kind of
200 * silly that there are processes changing round just to die a
201 * couple of seconds after the test has finished.
205 sa
.sa_handler
= SIG_IGN
;
206 sigfillset(&sa
.sa_mask
);
208 sigaction(SIGCHLD
, &sa
, NULL
);
210 for (i
= 0 ; i
< num_children
; i
++) {
211 if (kill(children
[i
], SIGTERM
) >= 0) {
213 wait4(children
[i
], &status
, 0, NULL
);