1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "signal-util.h"
9 /// Additional includes needed by elogind
10 //#include "stdio-util.h"
11 //#include "string-util.h"
12 //#include "stdio-util.h"
13 //#include "string-util.h"
14 //#include "stdio-util.h"
15 //#include "string-util.h"
16 //#include "stdio-util.h"
17 //#include "string-util.h"
18 //#include "stdio-util.h"
19 //#include "string-util.h"
20 #include "process-util.h"
22 #define info(sig) log_info(#sig " = " STRINGIFY(sig) " = %d", sig)
24 static void test_rt_signals(void) {
28 /* We use signals SIGRTMIN+0 to SIGRTMIN+24 unconditionally */
29 assert(SIGRTMAX - SIGRTMIN >= 24);
32 static void test_signal_to_string_one(int val) {
35 assert_se(p = signal_to_string(val));
37 assert_se(signal_from_string(p) == val);
39 p = strjoina("SIG", p);
40 assert_se(signal_from_string(p) == val);
43 static void test_signal_from_string_one(const char *s, int val) {
46 assert_se(signal_from_string(s) == val);
48 p = strjoina("SIG", s);
49 assert_se(signal_from_string(p) == val);
52 static void test_signal_from_string_number(const char *s, int val) {
55 assert_se(signal_from_string(s) == val);
57 p = strjoina("SIG", s);
58 assert_se(signal_from_string(p) == -EINVAL);
61 static void test_signal_from_string(void) {
62 char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];
64 test_signal_to_string_one(SIGHUP);
65 test_signal_to_string_one(SIGTERM);
66 test_signal_to_string_one(SIGRTMIN);
67 test_signal_to_string_one(SIGRTMIN+3);
68 test_signal_to_string_one(SIGRTMAX-4);
70 test_signal_from_string_one("RTMIN", SIGRTMIN);
71 test_signal_from_string_one("RTMAX", SIGRTMAX);
73 xsprintf(buf, "RTMIN+%d", SIGRTMAX-SIGRTMIN);
74 test_signal_from_string_one(buf, SIGRTMAX);
76 xsprintf(buf, "RTMIN+%d", INT_MAX);
77 test_signal_from_string_one(buf, -ERANGE);
79 xsprintf(buf, "RTMAX-%d", SIGRTMAX-SIGRTMIN);
80 test_signal_from_string_one(buf, SIGRTMIN);
82 xsprintf(buf, "RTMAX-%d", INT_MAX);
83 test_signal_from_string_one(buf, -ERANGE);
85 test_signal_from_string_one("", -EINVAL);
86 test_signal_from_string_one("hup", -EINVAL);
87 test_signal_from_string_one("HOGEHOGE", -EINVAL);
89 test_signal_from_string_one("RTMIN-5", -EINVAL);
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+100", -ERANGE);
95 test_signal_from_string_one("RTMIN+-3", -EINVAL);
96 test_signal_from_string_one("RTMIN++3", -EINVAL);
97 test_signal_from_string_one("RTMIN+HUP", -EINVAL);
98 test_signal_from_string_one("RTMIN3", -EINVAL);
100 test_signal_from_string_one("RTMAX+5", -EINVAL);
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-100", -ERANGE);
106 test_signal_from_string_one("RTMAX-+3", -EINVAL);
107 test_signal_from_string_one("RTMAX--3", -EINVAL);
108 test_signal_from_string_one("RTMAX-HUP", -EINVAL);
110 test_signal_from_string_number("3", 3);
111 test_signal_from_string_number("+5", 5);
112 test_signal_from_string_number(" +5", 5);
113 test_signal_from_string_number("10000", -ERANGE);
114 test_signal_from_string_number("-2", -ERANGE);
117 static void test_block_signals(void) {
120 assert_se(sigprocmask(0, NULL, &ss) >= 0);
122 assert_se(sigismember(&ss, SIGUSR1) == 0);
123 assert_se(sigismember(&ss, SIGALRM) == 0);
124 assert_se(sigismember(&ss, SIGVTALRM) == 0);
127 BLOCK_SIGNALS(SIGUSR1, SIGVTALRM);
129 assert_se(sigprocmask(0, NULL, &ss) >= 0);
130 assert_se(sigismember(&ss, SIGUSR1) == 1);
131 assert_se(sigismember(&ss, SIGALRM) == 0);
132 assert_se(sigismember(&ss, SIGVTALRM) == 1);
136 assert_se(sigprocmask(0, NULL, &ss) >= 0);
137 assert_se(sigismember(&ss, SIGUSR1) == 0);
138 assert_se(sigismember(&ss, SIGALRM) == 0);
139 assert_se(sigismember(&ss, SIGVTALRM) == 0);
142 static void test_ignore_signals(void) {
143 assert_se(ignore_signals(SIGINT, -1) >= 0);
144 assert_se(kill(getpid_cached(), SIGINT) >= 0);
145 assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
146 assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
147 assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
148 assert_se(kill(getpid_cached(), SIGTERM) >= 0);
149 assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
150 assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
153 int main(int argc, char *argv[]) {
155 test_signal_from_string();
156 test_block_signals();
157 test_ignore_signals();