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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* $OpenBSD: sigpthread.c,v 1.1 2019/05/13 19:40:22 bluhm Exp $ */
/*
* Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void __dead usage(void);
void handler(int);
void *runner(void *);
void __dead
usage(void)
{
fprintf(stderr, "sigpthread [-bSsU] [-k kill] -t threads [-u unblock] "
"[-w waiter]\n"
" -b block signal to make it pending\n"
" -k kill thread to kill, else process\n"
" -S sleep in each thread before suspend\n"
" -s sleep in main before kill\n"
" -t threads number of threads to run\n"
" -U sleep in thread before unblock\n"
" -u unblock thread to unblock, else unblock all\n"
" -w waiter use sigwait in thread\n"
);
exit(1);
}
int blocksignal = 0;
int threadmax, threadunblock = -1, threadwaiter = -1;
int sleepthread, sleepmain, sleepunblock;
sigset_t set, oset;
pthread_t *threads;
volatile sig_atomic_t *signaled;
int
main(int argc, char *argv[])
{
struct sigaction act;
int ch, ret, tnum, threadkill = -1;
long arg;
void *val;
const char *errstr;
while ((ch = getopt(argc, argv, "bk:Sst:Uu:w:")) != -1) {
switch (ch) {
case 'b':
blocksignal = 1;
break;
case 'k':
threadkill = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "thread to kill is %s: %s",
errstr, optarg);
break;
case 'S':
sleepthread = 1;
break;
case 's':
sleepmain = 1;
break;
case 't':
threadmax = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "number of threads is %s: %s",
errstr, optarg);
break;
case 'U':
sleepunblock = 1;
break;
case 'u':
threadunblock = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "thread to unblock is %s: %s",
errstr, optarg);
break;
case 'w':
threadwaiter = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "thread to wait is %s: %s",
errstr, optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 0)
errx(1, "more arguments than expected");
if (threadmax == 0)
errx(1, "number of threads required");
if (threadkill >= threadmax)
errx(1, "thread to kill greater than number of threads");
if (threadunblock >= threadmax)
errx(1, "thread to unblock greater than number of threads");
if (threadwaiter >= threadmax)
errx(1, "thread to wait greater than number of threads");
if (!blocksignal && threadunblock >= 0)
errx(1, "do not unblock thread without blocked signals");
if (!blocksignal && threadwaiter >= 0)
errx(1, "do not wait in thread without blocked signals");
if (threadunblock >= 0 && threadwaiter >= 0)
errx(1, "do not unblock and wait together");
if (sleepunblock && threadwaiter >= 0)
errx(1, "do not sleep before unblock and wait together");
/* Make sure that we do not hang forever. */
ret = alarm(10);
if (ret == -1)
err(1, "alarm");
if (sigemptyset(&set) == -1)
err(1, "sigemptyset");
if (sigaddset(&set, SIGUSR1) == -1)
err(1, "sigaddset");
/* Either deliver SIGUSR2 immediately, or mark it pending. */
if (blocksignal) {
if (sigaddset(&set, SIGUSR2) == -1)
err(1, "sigaddset");
}
/* Block both SIGUSR1 and SIGUSR2 with set. */
if (sigprocmask(SIG_BLOCK, &set, &oset) == -1)
err(1, "sigprocmask");
/* Prepare to wait for SIGUSR1, but block SIGUSR2 with oset. */
if (sigaddset(&oset, SIGUSR2) == -1)
err(1, "sigaddset");
/* Unblock or wait for SIGUSR2 */
if (sigemptyset(&set) == -1)
err(1, "sigemptyset");
if (sigaddset(&set, SIGUSR2) == -1)
err(1, "sigaddset");
memset(&act, 0, sizeof(act));
act.sa_handler = handler;
if (sigaction(SIGUSR1, &act, NULL) == -1)
err(1, "sigaction SIGUSR1");
if (sigaction(SIGUSR2, &act, NULL) == -1)
err(1, "sigaction SIGUSR2");
signaled = calloc(threadmax, sizeof(*signaled));
if (signaled == NULL)
err(1, "calloc signaled");
threads = calloc(threadmax, sizeof(*threads));
if (threads == NULL)
err(1, "calloc threads");
for (tnum = 1; tnum < threadmax; tnum++) {
arg = tnum;
errno = pthread_create(&threads[tnum], NULL, runner,
(void *)arg);
if (errno)
err(1, "pthread_create %d", tnum);
}
/* Handle the main thread like thread 0. */
threads[0] = pthread_self();
/* Test what happens if thread is running when killed. */
if (sleepmain)
sleep(1);
/* All threads are still alive. */
if (threadkill < 0) {
if (kill(getpid(), SIGUSR2) == -1)
err(1, "kill SIGUSR2");
} else {
errno = pthread_kill(threads[threadkill], SIGUSR2);
if (errno)
err(1, "pthread_kill %d SIGUSR2", tnum);
}
/* Sending SIGUSR1 means threads can continue and finish. */
for (tnum = 0; tnum < threadmax; tnum++) {
errno = pthread_kill(threads[tnum], SIGUSR1);
if (errno)
err(1, "pthread_kill %d SIGUSR1", tnum);
}
val = runner(0);
ret = (int)val;
for (tnum = 1; tnum < threadmax; tnum++) {
errno = pthread_join(threads[tnum], &val);
if (errno)
err(1, "pthread_join %d", tnum);
ret = (int)val;
if (ret)
errx(1, "pthread %d returned %d", tnum, ret);
}
free(threads);
for (tnum = 0; tnum < threadmax; tnum++) {
int i;
for (i = 0; i < signaled[tnum]; i++)
printf("signal %d\n", tnum);
}
free((void *)signaled);
return 0;
}
void
handler(int sig)
{
pthread_t tid;
int tnum;
tid = pthread_self();
for (tnum = 0; tnum < threadmax; tnum++) {
if (tid == threads[tnum])
break;
}
switch (sig) {
case SIGUSR1:
break;
case SIGUSR2:
signaled[tnum]++;
break;
default:
errx(1, "unexpected signal %d thread %d", sig, tnum);
}
}
void *
runner(void *arg)
{
int tnum = (int)arg;
/* Test what happens if thread is running when killed. */
if (sleepthread)
sleep(1);
if (tnum == threadwaiter) {
int sig;
if (sigwait(&set, &sig) != 0)
err(1, "sigwait thread %d", tnum);
if (sig != SIGUSR2)
errx(1, "unexpected signal %d thread %d", sig, tnum);
signaled[tnum]++;
}
/*
* Wait for SIGUSER1, continue to block SIGUSER2.
* The thread is keeps running until it gets SIGUSER1.
*/
if (sigsuspend(&oset) != -1 || errno != EINTR)
err(1, "sigsuspend thread %d", tnum);
if ((threadunblock < 0 || tnum == threadunblock) && threadwaiter < 0) {
/* Test what happens if other threads exit before unblock. */
if (sleepunblock)
sleep(1);
/* Also unblock SIGUSER2, if this thread should get it. */
if (pthread_sigmask(SIG_UNBLOCK, &set, NULL) == -1)
err(1, "pthread_sigmask thread %d", tnum);
}
return (void *)0;
}
|