1 /* Testcase checks, if setcontext(), swapcontext() restores signal-mask
2    and if pending signals are delivered after those calls.
3    Copyright (C) 2015-2021 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
check(const char * funcName)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, &set);
37 
38   if (sigismember (&set, SIGUSR2) != 0)
39     {
40       printf ("FAIL: SIGUSR2 is blocked after %s.\n", funcName);
41       exit (1);
42     }
43 
44   if (sigismember (&set, SIGUSR1) != 1)
45     {
46       printf ("FAIL: SIGUSR1 is not blocked after %s.\n", funcName);
47       exit (1);
48     }
49 }
50 
51 static void
signalmask(int how,int signum)52 signalmask (int how, int signum)
53 {
54   sigset_t set;
55   sigemptyset (&set);
56   sigaddset (&set, signum);
57   if (sigprocmask (how, &set, NULL) != 0)
58     {
59       printf ("FAIL: sigprocmaks (%d, %d, NULL): %m\n", how, signum);
60       exit (1);
61     }
62 }
63 
64 static void
signalpending(int signum,const char * msg)65 signalpending (int signum, const char *msg)
66 {
67   sigset_t set;
68   sigemptyset (&set);
69   if (sigpending (&set) != 0)
70     {
71       printf ("FAIL: sigpending: %m\n");
72       exit (1);
73     }
74   if (sigismember (&set, SIGUSR2) != 1)
75     {
76       printf ("FAIL: Signal %d is not pending %s\n", signum, msg);
77       exit (1);
78     }
79 }
80 
81 static void
handler(int signum)82 handler (int __attribute__ ((unused)) signum)
83 {
84   handlerCalled ++;
85 }
86 
87 static int
do_test(void)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, &action, NULL);
107 
108   if (getcontext (&ctx) != 0)
109     {
110       printf ("FAIL: getcontext: %m\n");
111       exit (1);
112     }
113 
114   global++;
115 
116   if (global == 1)
117     {
118       puts ("after getcontext");
119 
120       /* block SIGUSR2  */
121       signalmask (SIG_BLOCK, SIGUSR2);
122 
123       /* send SIGUSR2 to me  */
124       handlerCalled = 0;
125       kill (pid, SIGUSR2);
126 
127       /* was SIGUSR2 handler called?  */
128       if (handlerCalled != 0)
129 	{
130 	  puts ("FAIL: signal handler was called, but signal was blocked.");
131 	  exit (1);
132 	}
133 
134       /* is SIGUSR2 pending?  */
135       signalpending (SIGUSR2, "before setcontext");
136 
137       /* SIGUSR2 will be unblocked by setcontext-call.  */
138       if (setcontext (&ctx) != 0)
139 	{
140 	  printf ("FAIL: setcontext: %m\n");
141 	  exit (1);
142 	}
143     }
144   else if (global == 2)
145     {
146       puts ("after setcontext");
147 
148       /* check SIGUSR1/2  */
149       check ("setcontext");
150 
151       /* was SIGUSR2 handler called? */
152       if (handlerCalled != 1)
153 	{
154 	  puts ("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, SIGUSR2);
164 
165       /* was SIGUSR2 handler called?  */
166       if (handlerCalled != 0)
167 	{
168 	  puts ("FAIL: signal handler was called, but signal was blocked.");
169 	  exit (1);
170 	}
171 
172       /* is SIGUSR2 pending?  */
173       signalpending (SIGUSR2, "before swapcontext");
174 
175       if (swapcontext (&oldctx, &ctx) != 0)
176 	{
177 	  printf ("FAIL: swapcontext: %m\n");
178 	  exit (1);
179 	}
180 
181       puts ("after returned from swapcontext");
182 
183       if (global != 3)
184 	{
185 	  puts ("FAIL: returned from swapcontext without ctx-context called.");
186 	  exit (1);
187 	}
188 
189       puts ("test succeeded");
190       return 0;
191     }
192   else if ( global != 3 )
193     {
194       puts ("FAIL: 'global' not incremented three times");
195       exit (1);
196     }
197 
198   puts ("after swapcontext");
199   /* check SIGUSR1/2  */
200   check ("swapcontext");
201 
202   /* was SIGUSR2 handler called? */
203   if (handlerCalled != 1)
204     {
205       puts ("FAIL: signal handler was not called after swapcontext.");
206       exit (1);
207     }
208 
209   /* check sigmask in old context of swapcontext-call  */
210   if (sigismember (&oldctx.uc_sigmask, SIGUSR2) != 1)
211     {
212       puts ("FAIL: SIGUSR2 is not blocked in oldctx.uc_sigmask.");
213       exit (1);
214     }
215 
216   if (sigismember (&oldctx.uc_sigmask, SIGUSR1) != 1)
217     {
218       puts ("FAIL: SIGUSR1 is not blocked in oldctx.uc_sigmaks.");
219       exit (1);
220     }
221 
222   /* change to old context, which was gathered by swapcontext() call.  */
223   setcontext (&oldctx);
224 
225   puts ("FAIL: returned from setcontext (&oldctx)");
226   exit (1);
227 }
228 
229 #define TEST_FUNCTION do_test ()
230 #include "../test-skeleton.c"
231