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
|
#include "common/config.h"
#include "common/signal.h"
#include "global/signal_handler.h"
#include "test/unit.h"
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
static volatile sig_atomic_t got_sigusr1 = 0;
static void handle_sigusr1(int signo)
{
got_sigusr1 = 1;
}
TEST(SignalApi, SimpleInstall)
{
install_sighandler(SIGPIPE, handle_sigusr1, 0);
}
TEST(SignalApi, SimpleInstallAndTest)
{
install_sighandler(SIGPIPE, handle_sigusr1, 0);
// SIGPIPE starts out blocked
int ret = kill(getpid(), SIGPIPE);
ASSERT_EQ(ret, 0);
ASSERT_EQ(got_sigusr1, 0);
// handle SIGPIPE
sigset_t mask;
sigemptyset(&mask);
ret = sigsuspend(&mask);
if (ret == -1)
ret = errno;
// we should have gotten it
ASSERT_EQ(ret, EINTR);
ASSERT_EQ(got_sigusr1, 1);
}
TEST(SignalEffects, ErrnoTest1)
{
}
bool usr1 = false;
bool usr2 = false;
void reset()
{
usr1 = false;
usr2 = false;
}
void testhandler(int signal)
{
switch (signal) {
case SIGUSR1:
usr1 = true;
break;
case SIGUSR2:
usr2 = true;
break;
default:
assert(0 == "unexpected signal");
}
}
TEST(SignalHandler, Single)
{
reset();
init_async_signal_handler();
register_async_signal_handler(SIGUSR1, testhandler);
ASSERT_TRUE(usr1 == false);
int ret = kill(getpid(), SIGUSR1);
ASSERT_EQ(ret, 0);
sleep(1);
ASSERT_TRUE(usr1 == true);
unregister_async_signal_handler(SIGUSR1, testhandler);
shutdown_async_signal_handler();
}
TEST(SignalHandler, Multiple)
{
int ret;
reset();
init_async_signal_handler();
register_async_signal_handler(SIGUSR1, testhandler);
register_async_signal_handler(SIGUSR2, testhandler);
ASSERT_TRUE(usr1 == false);
ASSERT_TRUE(usr2 == false);
ret = kill(getpid(), SIGUSR1);
ASSERT_EQ(ret, 0);
ret = kill(getpid(), SIGUSR2);
ASSERT_EQ(ret, 0);
sleep(1);
ASSERT_TRUE(usr1 == true);
ASSERT_TRUE(usr2 == true);
unregister_async_signal_handler(SIGUSR1, testhandler);
unregister_async_signal_handler(SIGUSR2, testhandler);
shutdown_async_signal_handler();
}
|