*** empty log message ***
[arla.git] / tests / intr-read.c
blob516ff601b377de634c055f46146a91b0a685a8d5
1 /*
2 * Copyright (c) 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <dirent.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 #include <fcntl.h>
49 #include <unistd.h>
51 #include <roken.h>
53 #include <err.h>
55 RCSID("$Id$");
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;
63 static RETSIGTYPE
64 sigalrm(int foo)
66 signal(SIGALRM, sigalrm);
67 set_alarm = 1;
70 static RETSIGTYPE
71 sigchld(int foo)
73 int status;
75 signal(SIGALRM, sigalrm);
76 dead_children--;
77 wait3(&status, WNOHANG, NULL);
82 static void
83 try_read(const char *filename)
85 int fd;
87 fd = open(filename, O_RDONLY);
88 if (fd < 0) {
89 if (errno == EINTR)
90 err(1, "open %s was interrupted", filename);
91 } else {
92 close(fd);
96 static void
97 find(const char *dirname)
99 DIR *dir;
100 struct dirent *dp;
102 dir = opendir(dirname);
103 if (dir == NULL)
104 err (1, "opendir %s", dirname);
105 while ((dp = readdir(dir)) != NULL) {
106 char fname[MAXPATHLEN];
107 struct stat sb;
109 if (set_alarm) {
110 alarm(1);
111 set_alarm = 0;
113 if (strcmp (dp->d_name, ".") == 0
114 || strcmp (dp->d_name, "..") == 0)
115 continue;
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))
120 find(fname);
121 else
122 try_read(fname);
124 closedir(dir);
128 main(int argc, char **argv)
130 struct sigaction sa;
131 pid_t *children = NULL;
132 int i;
134 setprogname (argv[0]);
136 if (argc < 2)
137 errx(1, "argc < 2");
139 sa.sa_handler = sigalrm;
140 sigfillset(&sa.sa_mask);
141 sa.sa_flags = 0;
142 sigaction(SIGALRM, &sa, NULL);
144 sa.sa_handler = sigchld;
145 sigfillset(&sa.sa_mask);
146 sa.sa_flags = 0;
147 sigaction(SIGCHLD, &sa, NULL);
149 while (argc > 1) {
150 if (strcmp(argv[1], "--alarm") == 0)
151 set_alarm = 1;
152 else if (strcmp(argv[1], "--child") == 0)
153 do_children = 1;
154 else
155 break;
156 argc--;
157 argv++;
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
166 if (do_children) {
167 pid_t pid, ppid;
169 children = emalloc(sizeof(pid_t) * num_children);
171 ppid = getpid();
173 for (i = 0 ; i < num_children; i++) {
174 pid = fork();
175 switch (pid) {
176 case 0:
177 srandom(getpid() * time(NULL));
178 i = random() % 10 + 3;
179 while(--i) {
180 sleep(1);
181 if (kill(ppid, 0) < 0)
182 _exit(1);
184 _exit(0);
185 case -1:
186 err(1, "fork");
187 default:
188 children[i] = pid;
189 break;
194 while (--argc)
195 find(*++argv);
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.
204 if (do_children) {
205 sa.sa_handler = SIG_IGN;
206 sigfillset(&sa.sa_mask);
207 sa.sa_flags = 0;
208 sigaction(SIGCHLD, &sa, NULL);
210 for (i = 0 ; i < num_children; i++) {
211 if (kill(children[i], SIGTERM) >= 0) {
212 int status;
213 wait4(children[i], &status, 0, NULL);
218 return 0;