1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* $OpenBSD: signal-stress.c,v 1.3 2008/04/13 00:22:17 djm Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2004 Public Domain.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
int nprocs, nsigs;
pid_t *pids;
pid_t next, prev;
sig_atomic_t usr1, usr2;
void
sighand(int sig)
{
if (sig == SIGUSR1 && ++usr1 <= nsigs) {
if (kill(next, sig))
_exit(1);
}
if (sig == SIGUSR2 && ++usr2 <= nsigs) {
if (kill(prev, sig))
_exit(1);
}
}
void
do_child(void)
{
int i;
/*
* Step 1 - suspend and wait for SIGCONT so that all siblings have
* been started before the next step.
*/
raise(SIGSTOP);
/* Find our neighbours. */
for (i = 0; i < nprocs; i++) {
if (pids[i] != getpid())
continue;
if (i + 1 == nprocs)
next = pids[0];
else
next = pids[i + 1];
if (i == 0)
prev = pids[nprocs - 1];
else
prev = pids[i - 1];
}
signal(SIGUSR1, sighand);
signal(SIGUSR2, sighand);
/* Step 2 - wait again until everyone is ready. */
raise(SIGSTOP);
while (usr1 < nsigs || usr2 < nsigs)
pause();
/* Step 3 - wait again until everyone is ready. */
raise(SIGSTOP);
}
void
wait_stopped(pid_t pid)
{
int status;
if (waitpid(pid, &status, WUNTRACED) != pid)
err(1, "waitpid");
if (!WIFSTOPPED(status))
errx(1, "child %d not stopped", pid);
}
void
cleanup(void)
{
int i;
for (i = 0; i < nprocs; i++)
kill(pids[i], 9);
}
void
alrmhand(int sig)
{
cleanup();
_exit(1);
}
int
main()
{
int i;
pid_t pid;
nprocs = 35;
nsigs = 1000;
if ((pids = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED, -1, 0)) == MAP_FAILED)
err(1, "mmap");
for (i = 0; i < nprocs; i++) {
switch((pid = fork())) {
case 0:
do_child();
_exit(0);
case -1:
err(1, "fork");
}
pids[i] = pid;
}
atexit(cleanup);
signal(SIGALRM, alrmhand);
alarm(120); /* Die after two minutes. */
/* Step 1. Wait until all children have went to sleep */
for (i = 0; i < nprocs; i++)
wait_stopped(pids[i]);
/* And wake them */
for (i = 0; i < nprocs; i++)
kill(pids[i], SIGCONT);
/* Step 2. Repeat. */
for (i = 0; i < nprocs; i++)
wait_stopped(pids[i]);
for (i = 0; i < nprocs; i++)
kill(pids[i], SIGCONT);
/*
* Now all children are ready for action.
* Send the first signals and wait until they all exit.
*/
kill(pids[arc4random_uniform(nprocs)], SIGUSR1);
kill(pids[arc4random_uniform(nprocs)], SIGUSR2);
/*
* The signal game is running, now insert noise in the process.
*/
for (i = 0; i < nprocs; i++) {
pid_t pid = pids[arc4random_uniform(nprocs)];
kill(pid, SIGSTOP);
wait_stopped(pid);
kill(pid, SIGCONT);
}
/* Step 3. Repeat. */
for (i = 0; i < nprocs; i++)
wait_stopped(pids[i]);
for (i = 0; i < nprocs; i++)
kill(pids[i], SIGCONT);
/* Wait for everyone to finish. */
for (i = 0; i < nprocs; i++) {
int status;
if (waitpid(pids[i], &status, WUNTRACED) != pids[i])
err(1, "waitpid");
if (!WIFEXITED(status))
errx(1, "child %d not stopped (%d)", pids[i], status);
if (WEXITSTATUS(status) != 0)
warnx("child %d status: %d", i, status);
}
return (0);
}
|