chiark / gitweb /
9387d5804baff33fcb8490cac9cff61a69fff842
[elogind.git] / src / test / test-signal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <signal.h>
4 #include <unistd.h>
5
6 //#include "log.h"
7 #include "macro.h"
8 #include "signal-util.h"
9 #include "stdio-util.h"
10 #include "string-util.h"
11 /// Additional includes needed by elogind
12 #include "process-util.h"
13
14 #define info(sig) log_info(#sig " = " STRINGIFY(sig) " = %d", sig)
15
16 static void test_rt_signals(void) {
17         info(SIGRTMIN);
18         info(SIGRTMAX);
19
20         /* We use signals SIGRTMIN+0 to SIGRTMIN+24 unconditionally */
21         assert(SIGRTMAX - SIGRTMIN >= 24);
22 }
23
24 static void test_signal_to_string_one(int val) {
25         const char *p;
26
27         assert_se(p = signal_to_string(val));
28
29         assert_se(signal_from_string(p) == val);
30
31         p = strjoina("SIG", p);
32         assert_se(signal_from_string(p) == val);
33 }
34
35 static void test_signal_from_string_one(const char *s, int val) {
36         const char *p;
37
38         assert_se(signal_from_string(s) == val);
39
40         p = strjoina("SIG", s);
41         assert_se(signal_from_string(p) == val);
42 }
43
44 static void test_signal_from_string_number(const char *s, int val) {
45         const char *p;
46
47         assert_se(signal_from_string(s) == val);
48
49         p = strjoina("SIG", s);
50         assert_se(signal_from_string(p) == -EINVAL);
51 }
52
53 static void test_signal_from_string(void) {
54         char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];
55
56         test_signal_to_string_one(SIGHUP);
57         test_signal_to_string_one(SIGTERM);
58         test_signal_to_string_one(SIGRTMIN);
59         test_signal_to_string_one(SIGRTMIN+3);
60         test_signal_to_string_one(SIGRTMAX-4);
61
62         test_signal_from_string_one("RTMIN", SIGRTMIN);
63         test_signal_from_string_one("RTMAX", SIGRTMAX);
64
65         xsprintf(buf, "RTMIN+%d", SIGRTMAX-SIGRTMIN);
66         test_signal_from_string_one(buf, SIGRTMAX);
67
68         xsprintf(buf, "RTMIN+%d", INT_MAX);
69         test_signal_from_string_one(buf, -ERANGE);
70
71         xsprintf(buf, "RTMAX-%d", SIGRTMAX-SIGRTMIN);
72         test_signal_from_string_one(buf, SIGRTMIN);
73
74         xsprintf(buf, "RTMAX-%d", INT_MAX);
75         test_signal_from_string_one(buf, -ERANGE);
76
77         test_signal_from_string_one("", -EINVAL);
78         test_signal_from_string_one("hup", -EINVAL);
79         test_signal_from_string_one("HOGEHOGE", -EINVAL);
80
81         test_signal_from_string_one("RTMIN-5", -EINVAL);
82         test_signal_from_string_one("RTMIN-    5", -EINVAL);
83         test_signal_from_string_one("RTMIN    -5", -EINVAL);
84         test_signal_from_string_one("RTMIN+    5", -EINVAL);
85         test_signal_from_string_one("RTMIN    +5", -EINVAL);
86         test_signal_from_string_one("RTMIN+100", -ERANGE);
87         test_signal_from_string_one("RTMIN+-3", -EINVAL);
88         test_signal_from_string_one("RTMIN++3", -EINVAL);
89         test_signal_from_string_one("RTMIN+HUP", -EINVAL);
90         test_signal_from_string_one("RTMIN3", -EINVAL);
91
92         test_signal_from_string_one("RTMAX+5", -EINVAL);
93         test_signal_from_string_one("RTMAX+    5", -EINVAL);
94         test_signal_from_string_one("RTMAX    +5", -EINVAL);
95         test_signal_from_string_one("RTMAX-    5", -EINVAL);
96         test_signal_from_string_one("RTMAX    -5", -EINVAL);
97         test_signal_from_string_one("RTMAX-100", -ERANGE);
98         test_signal_from_string_one("RTMAX-+3", -EINVAL);
99         test_signal_from_string_one("RTMAX--3", -EINVAL);
100         test_signal_from_string_one("RTMAX-HUP", -EINVAL);
101
102         test_signal_from_string_number("3", 3);
103         test_signal_from_string_number("+5", 5);
104         test_signal_from_string_number("  +5", 5);
105         test_signal_from_string_number("10000", -ERANGE);
106         test_signal_from_string_number("-2", -ERANGE);
107 }
108
109 static void test_block_signals(void) {
110         sigset_t ss;
111
112         assert_se(sigprocmask(0, NULL, &ss) >= 0);
113
114         assert_se(sigismember(&ss, SIGUSR1) == 0);
115         assert_se(sigismember(&ss, SIGALRM) == 0);
116         assert_se(sigismember(&ss, SIGVTALRM) == 0);
117
118         {
119                 BLOCK_SIGNALS(SIGUSR1, SIGVTALRM);
120
121                 assert_se(sigprocmask(0, NULL, &ss) >= 0);
122                 assert_se(sigismember(&ss, SIGUSR1) == 1);
123                 assert_se(sigismember(&ss, SIGALRM) == 0);
124                 assert_se(sigismember(&ss, SIGVTALRM) == 1);
125
126         }
127
128         assert_se(sigprocmask(0, NULL, &ss) >= 0);
129         assert_se(sigismember(&ss, SIGUSR1) == 0);
130         assert_se(sigismember(&ss, SIGALRM) == 0);
131         assert_se(sigismember(&ss, SIGVTALRM) == 0);
132 }
133
134 static void test_ignore_signals(void) {
135         assert_se(ignore_signals(SIGINT, -1) >= 0);
136         assert_se(kill(getpid_cached(), SIGINT) >= 0);
137         assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
138         assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
139         assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
140         assert_se(kill(getpid_cached(), SIGTERM) >= 0);
141         assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
142         assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
143 }
144
145 int main(int argc, char *argv[]) {
146         test_rt_signals();
147         test_signal_from_string();
148         test_block_signals();
149         test_ignore_signals();
150
151         return 0;
152 }