1 | /* Testcase checks, if setcontext(), swapcontext() restores signal-mask |
2 | and if pending signals are delivered after those calls. |
3 | Copyright (C) 2018-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <sys/types.h> |
23 | #include <signal.h> |
24 | #include <ucontext.h> |
25 | #include <unistd.h> |
26 | |
27 | volatile int global; |
28 | volatile sig_atomic_t handlerCalled; |
29 | |
30 | static void |
31 | check (const char *funcName) |
32 | { |
33 | sigset_t set; |
34 | |
35 | /* check if SIGUSR2 is unblocked after setcontext-call. */ |
36 | sigprocmask (SIG_BLOCK, NULL, oset: &set); |
37 | |
38 | if (sigismember (&set, SIGUSR2) != 0) |
39 | { |
40 | printf (format: "FAIL: SIGUSR2 is blocked after %s.\n" , funcName); |
41 | exit (1); |
42 | } |
43 | |
44 | if (sigismember (&set, SIGUSR1) != 1) |
45 | { |
46 | printf (format: "FAIL: SIGUSR1 is not blocked after %s.\n" , funcName); |
47 | exit (1); |
48 | } |
49 | } |
50 | |
51 | static void |
52 | signalmask (int how, int signum) |
53 | { |
54 | sigset_t set; |
55 | sigemptyset (&set); |
56 | sigaddset (&set, signum); |
57 | if (sigprocmask (how: how, set: &set, NULL) != 0) |
58 | { |
59 | printf (format: "FAIL: sigprocmaks (%d, %d, NULL): %m\n" , how, signum); |
60 | exit (1); |
61 | } |
62 | } |
63 | |
64 | static void |
65 | signalpending (int signum, const char *msg) |
66 | { |
67 | sigset_t set; |
68 | sigemptyset (&set); |
69 | if (sigpending (set: &set) != 0) |
70 | { |
71 | printf (format: "FAIL: sigpending: %m\n" ); |
72 | exit (1); |
73 | } |
74 | if (sigismember (&set, SIGUSR2) != 1) |
75 | { |
76 | printf (format: "FAIL: Signal %d is not pending %s\n" , signum, msg); |
77 | exit (1); |
78 | } |
79 | } |
80 | |
81 | static void |
82 | handler (int __attribute__ ((unused)) signum) |
83 | { |
84 | handlerCalled ++; |
85 | } |
86 | |
87 | static int |
88 | do_test (void) |
89 | { |
90 | ucontext_t ctx, oldctx; |
91 | struct sigaction action; |
92 | pid_t pid; |
93 | |
94 | pid = getpid (); |
95 | |
96 | /* unblock SIGUSR2 */ |
97 | signalmask (SIG_UNBLOCK, SIGUSR2); |
98 | |
99 | /* block SIGUSR1 */ |
100 | signalmask (SIG_BLOCK, SIGUSR1); |
101 | |
102 | /* register handler for SIGUSR2 */ |
103 | action.sa_flags = 0; |
104 | action.sa_handler = handler; |
105 | sigemptyset (&action.sa_mask); |
106 | sigaction (SIGUSR2, act: &action, NULL); |
107 | |
108 | if (getcontext (ucp: &ctx) != 0) |
109 | { |
110 | printf (format: "FAIL: getcontext: %m\n" ); |
111 | exit (1); |
112 | } |
113 | |
114 | global++; |
115 | |
116 | if (global == 1) |
117 | { |
118 | puts (s: "after getcontext" ); |
119 | |
120 | /* block SIGUSR2 */ |
121 | signalmask (SIG_BLOCK, SIGUSR2); |
122 | |
123 | /* send SIGUSR2 to me */ |
124 | handlerCalled = 0; |
125 | kill (pid: pid, SIGUSR2); |
126 | |
127 | /* was SIGUSR2 handler called? */ |
128 | if (handlerCalled != 0) |
129 | { |
130 | puts (s: "FAIL: signal handler was called, but signal was blocked." ); |
131 | exit (1); |
132 | } |
133 | |
134 | /* is SIGUSR2 pending? */ |
135 | signalpending (SIGUSR2, msg: "before setcontext" ); |
136 | |
137 | /* SIGUSR2 will be unblocked by setcontext-call. */ |
138 | if (setcontext (&ctx) != 0) |
139 | { |
140 | printf (format: "FAIL: setcontext: %m\n" ); |
141 | exit (1); |
142 | } |
143 | } |
144 | else if (global == 2) |
145 | { |
146 | puts (s: "after setcontext" ); |
147 | |
148 | /* check SIGUSR1/2 */ |
149 | check (funcName: "setcontext" ); |
150 | |
151 | /* was SIGUSR2 handler called? */ |
152 | if (handlerCalled != 1) |
153 | { |
154 | puts (s: "FAIL: signal handler was not called after setcontext." ); |
155 | exit (1); |
156 | } |
157 | |
158 | /* block SIGUSR2 */ |
159 | signalmask (SIG_BLOCK, SIGUSR2); |
160 | |
161 | /* send SIGUSR2 to me */ |
162 | handlerCalled = 0; |
163 | kill (pid: pid, SIGUSR2); |
164 | |
165 | /* was SIGUSR2 handler called? */ |
166 | if (handlerCalled != 0) |
167 | { |
168 | puts (s: "FAIL: signal handler was called, but signal was blocked." ); |
169 | exit (1); |
170 | } |
171 | |
172 | /* is SIGUSR2 pending? */ |
173 | signalpending (SIGUSR2, msg: "before swapcontext" ); |
174 | |
175 | if (swapcontext (oucp: &oldctx, ucp: &ctx) != 0) |
176 | { |
177 | printf (format: "FAIL: swapcontext: %m\n" ); |
178 | exit (1); |
179 | } |
180 | |
181 | puts (s: "FAIL: returned from (&oldctx, &ctx)" ); |
182 | exit (1); |
183 | } |
184 | else if ( global != 3 ) |
185 | { |
186 | puts (s: "FAIL: 'global' not incremented three times" ); |
187 | exit (1); |
188 | } |
189 | |
190 | puts (s: "after swapcontext" ); |
191 | /* check SIGUSR1/2 */ |
192 | check (funcName: "swapcontext" ); |
193 | |
194 | /* was SIGUSR2 handler called? */ |
195 | if (handlerCalled != 1) |
196 | { |
197 | puts (s: "FAIL: signal handler was not called after swapcontext." ); |
198 | exit (1); |
199 | } |
200 | |
201 | /* check sigmask in old context of swapcontext-call */ |
202 | if (sigismember (&oldctx.uc_sigmask, SIGUSR2) != 1) |
203 | { |
204 | puts (s: "FAIL: SIGUSR2 is not blocked in oldctx.uc_sigmask." ); |
205 | exit (1); |
206 | } |
207 | |
208 | if (sigismember (&oldctx.uc_sigmask, SIGUSR1) != 1) |
209 | { |
210 | puts (s: "FAIL: SIGUSR1 is not blocked in oldctx.uc_sigmaks." ); |
211 | exit (1); |
212 | } |
213 | |
214 | return 0; |
215 | } |
216 | |
217 | #include <support/test-driver.c> |
218 | |