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