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
|
/* $OpenBSD: pthread_kill.c,v 1.2 2002/10/23 22:30:04 marc Exp $ */
/* PUBLIC DOMAIN Oct 2002 <marc@snafu.org> */
/*
* Verify that pthread_kill does the right thing, i.e. the signal
* is delivered to the correct thread and proper signal processing
* is performed.
*/
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "test.h"
void
act_handler(int signal, siginfo_t *siginfo, void *context)
{
struct sigaction sa;
char *str;
CHECKe(sigaction(SIGUSR1, NULL, &sa));
ASSERT(sa.sa_handler == SIG_DFL);
ASSERT(siginfo != NULL);
asprintf(&str, "act_handler: signal %d, siginfo %p, context %p\n",
signal, siginfo, context);
write(STDOUT_FILENO, str, strlen(str));
}
void *
thread(void * arg)
{
sigset_t run_mask;
sigset_t suspender_mask;
/* wait for sigusr1 */
SET_NAME(arg);
/* Run with all signals blocked, then suspend for SIGUSR1 */
sigfillset(&run_mask);
CHECKe(sigprocmask(SIG_SETMASK, &run_mask, NULL));
sigfillset(&suspender_mask);
sigdelset(&suspender_mask, SIGUSR1);
for (;;) {
sigsuspend(&suspender_mask);
ASSERT(errno == EINTR);
printf("Thread %s woke up\n", (char*) arg);
}
}
int
main(int argc, char **argv)
{
pthread_t thread1;
pthread_t thread2;
struct sigaction act;
act.sa_sigaction = act_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO | SA_RESETHAND | SA_NODEFER;
CHECKe(sigaction(SIGUSR1, &act, NULL));
CHECKr(pthread_create(&thread1, NULL, thread, "T1"));
CHECKr(pthread_create(&thread2, NULL, thread, "T2"));
sleep(1);
/* Signal handler should run once, both threads should awaken */
CHECKe(kill(getpid(), SIGUSR1));
sleep(1);
/* Signal handler run once, only T1 should awaken */
CHECKe(sigaction(SIGUSR1, &act, NULL));
CHECKr(pthread_kill(thread1, SIGUSR1));
sleep(1);
/* Signal handler run once, only T2 should awaken */
CHECKe(sigaction(SIGUSR1, &act, NULL));
CHECKr(pthread_kill(thread2, SIGUSR1));
sleep(1);
SUCCEED;
}
|