m68k: update libm test ULPs
[glibc.git] / stdlib / tst-setcontext2.c
blob8582cc0c1c69bbfcb7de2c41be68a6e8055ea0d5
1 /* Testcase checks, if setcontext(), swapcontext() restores signal-mask
2 and if pending signals are delivered after those calls.
3 Copyright (C) 2015 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 <http://www.gnu.org/licenses/>. */
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>
27 volatile int global;
28 volatile sig_atomic_t handlerCalled;
30 static void
31 check (const char *funcName)
33 sigset_t set;
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);
41 exit (1);
44 if (sigismember (&set, SIGUSR1) != 1)
46 printf ("FAIL: SIGUSR1 is not blocked after %s.\n", funcName);
47 exit (1);
51 static void
52 signalmask (int how, int signum)
54 sigset_t set;
55 sigemptyset (&set);
56 sigaddset (&set, signum);
57 if (sigprocmask (how, &set, NULL) != 0)
59 printf ("FAIL: sigprocmaks (%d, %d, NULL): %m\n", how, signum);
60 exit (1);
64 static void
65 signalpending (int signum, const char *msg)
67 sigset_t set;
68 sigemptyset (&set);
69 if (sigpending (&set) != 0)
71 printf ("FAIL: sigpending: %m\n");
72 exit (1);
74 if (sigismember (&set, SIGUSR2) != 1)
76 printf ("FAIL: Signal %d is not pending %s\n", signum, msg);
77 exit (1);
81 static void
82 handler (int __attribute__ ((unused)) signum)
84 handlerCalled ++;
87 static int
88 do_test (void)
90 ucontext_t ctx, oldctx;
91 struct sigaction action;
92 pid_t pid;
94 pid = getpid ();
96 /* unblock SIGUSR2 */
97 signalmask (SIG_UNBLOCK, SIGUSR2);
99 /* block SIGUSR1 */
100 signalmask (SIG_BLOCK, SIGUSR1);
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);
108 if (getcontext (&ctx) != 0)
110 printf ("FAIL: getcontext: %m\n");
111 exit (1);
114 global++;
116 if (global == 1)
118 puts ("after getcontext");
120 /* block SIGUSR2 */
121 signalmask (SIG_BLOCK, SIGUSR2);
123 /* send SIGUSR2 to me */
124 handlerCalled = 0;
125 kill (pid, SIGUSR2);
127 /* was SIGUSR2 handler called? */
128 if (handlerCalled != 0)
130 puts ("FAIL: signal handler was called, but signal was blocked.");
131 exit (1);
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");
141 exit (1);
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.");
155 exit (1);
158 /* block SIGUSR2 */
159 signalmask (SIG_BLOCK, SIGUSR2);
161 /* send SIGUSR2 to me */
162 handlerCalled = 0;
163 kill (pid, SIGUSR2);
165 /* was SIGUSR2 handler called? */
166 if (handlerCalled != 0)
168 puts ("FAIL: signal handler was called, but signal was blocked.");
169 exit (1);
172 /* is SIGUSR2 pending? */
173 signalpending (SIGUSR2, "before swapcontext");
175 if (swapcontext (&oldctx, &ctx) != 0)
177 printf ("FAIL: swapcontext: %m\n");
178 exit (1);
181 puts ("after returned from swapcontext");
183 if (global != 3)
185 puts ("FAIL: returned from swapcontext without ctx-context called.");
186 exit (1);
189 puts ("test succeeded");
190 return 0;
192 else if ( global != 3 )
194 puts ("FAIL: 'global' not incremented three times");
195 exit (1);
198 puts ("after swapcontext");
199 /* check SIGUSR1/2 */
200 check ("swapcontext");
202 /* was SIGUSR2 handler called? */
203 if (handlerCalled != 1)
205 puts ("FAIL: signal handler was not called after swapcontext.");
206 exit (1);
209 /* check sigmask in old context of swapcontext-call */
210 if (sigismember (&oldctx.uc_sigmask, SIGUSR2) != 1)
212 puts ("FAIL: SIGUSR2 is not blocked in oldctx.uc_sigmask.");
213 exit (1);
216 if (sigismember (&oldctx.uc_sigmask, SIGUSR1) != 1)
218 puts ("FAIL: SIGUSR1 is not blocked in oldctx.uc_sigmaks.");
219 exit (1);
222 /* change to old context, which was gathered by swapcontext() call. */
223 setcontext (&oldctx);
225 puts ("FAIL: returned from setcontext (&oldctx)");
226 exit (1);
229 #define TEST_FUNCTION do_test ()
230 #include "../test-skeleton.c"