1 /* Testcase checks, if setcontext(), swapcontext() restores signal-mask
2 and if pending signals are delivered after those calls.
3 Copyright (C) 2018-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <sys/types.h>
28 volatile sig_atomic_t handlerCalled;
31 check (const char *funcName)
35 /* check if SIGUSR2 is unblocked after setcontext-call. */
36 sigprocmask (SIG_BLOCK, NULL, &set);
38 if (sigismember (&set, SIGUSR2) != 0)
40 printf ("FAIL: SIGUSR2 is blocked after %s.\n", funcName);
44 if (sigismember (&set, SIGUSR1) != 1)
46 printf ("FAIL: SIGUSR1 is not blocked after %s.\n", funcName);
52 signalmask (int how, int signum)
56 sigaddset (&set, signum);
57 if (sigprocmask (how, &set, NULL) != 0)
59 printf ("FAIL: sigprocmaks (%d, %d, NULL): %m\n", how, signum);
65 signalpending (int signum, const char *msg)
69 if (sigpending (&set) != 0)
71 printf ("FAIL: sigpending: %m\n");
74 if (sigismember (&set, SIGUSR2) != 1)
76 printf ("FAIL: Signal %d is not pending %s\n", signum, msg);
82 handler (int __attribute__ ((unused)) signum)
90 ucontext_t ctx, oldctx;
91 struct sigaction action;
97 signalmask (SIG_UNBLOCK, SIGUSR2);
100 signalmask (SIG_BLOCK, SIGUSR1);
102 /* register handler for SIGUSR2 */
104 action.sa_handler = handler;
105 sigemptyset (&action.sa_mask);
106 sigaction (SIGUSR2, &action, NULL);
108 if (getcontext (&ctx) != 0)
110 printf ("FAIL: getcontext: %m\n");
118 puts ("after getcontext");
121 signalmask (SIG_BLOCK, SIGUSR2);
123 /* send SIGUSR2 to me */
127 /* was SIGUSR2 handler called? */
128 if (handlerCalled != 0)
130 puts ("FAIL: signal handler was called, but signal was blocked.");
134 /* is SIGUSR2 pending? */
135 signalpending (SIGUSR2, "before setcontext");
137 /* SIGUSR2 will be unblocked by setcontext-call. */
138 if (setcontext (&ctx) != 0)
140 printf ("FAIL: setcontext: %m\n");
144 else if (global == 2)
146 puts ("after setcontext");
148 /* check SIGUSR1/2 */
149 check ("setcontext");
151 /* was SIGUSR2 handler called? */
152 if (handlerCalled != 1)
154 puts ("FAIL: signal handler was not called after setcontext.");
159 signalmask (SIG_BLOCK, SIGUSR2);
161 /* send SIGUSR2 to me */
165 /* was SIGUSR2 handler called? */
166 if (handlerCalled != 0)
168 puts ("FAIL: signal handler was called, but signal was blocked.");
172 /* is SIGUSR2 pending? */
173 signalpending (SIGUSR2, "before swapcontext");
175 if (swapcontext (&oldctx, &ctx) != 0)
177 printf ("FAIL: swapcontext: %m\n");
181 puts ("FAIL: returned from (&oldctx, &ctx)");
184 else if ( global != 3 )
186 puts ("FAIL: 'global' not incremented three times");
190 puts ("after swapcontext");
191 /* check SIGUSR1/2 */
192 check ("swapcontext");
194 /* was SIGUSR2 handler called? */
195 if (handlerCalled != 1)
197 puts ("FAIL: signal handler was not called after swapcontext.");
201 /* check sigmask in old context of swapcontext-call */
202 if (sigismember (&oldctx.uc_sigmask, SIGUSR2) != 1)
204 puts ("FAIL: SIGUSR2 is not blocked in oldctx.uc_sigmask.");
208 if (sigismember (&oldctx.uc_sigmask, SIGUSR1) != 1)
210 puts ("FAIL: SIGUSR1 is not blocked in oldctx.uc_sigmaks.");
217 #include <support/test-driver.c>