xref: /qemu/tests/tcg/multiarch/sigreturn-sigmask.c (revision abb1565d3d863cf210f18f70c4a42b0f39b8ccdb)
1*ef7e76a2SIlya Leoshkevich /*
2*ef7e76a2SIlya Leoshkevich  * Test that sigreturn() does not corrupt the signal mask.
3*ef7e76a2SIlya Leoshkevich  * Block SIGUSR2 and handle SIGUSR1.
4*ef7e76a2SIlya Leoshkevich  * Then sigwait() SIGUSR2, which relies on it remaining blocked.
5*ef7e76a2SIlya Leoshkevich  *
6*ef7e76a2SIlya Leoshkevich  * SPDX-License-Identifier: GPL-2.0-or-later
7*ef7e76a2SIlya Leoshkevich  */
8*ef7e76a2SIlya Leoshkevich #include <assert.h>
9*ef7e76a2SIlya Leoshkevich #include <pthread.h>
10*ef7e76a2SIlya Leoshkevich #include <signal.h>
11*ef7e76a2SIlya Leoshkevich #include <stdlib.h>
12*ef7e76a2SIlya Leoshkevich #include <unistd.h>
13*ef7e76a2SIlya Leoshkevich 
14*ef7e76a2SIlya Leoshkevich int seen_sig = -1;
15*ef7e76a2SIlya Leoshkevich 
signal_func(int sig)16*ef7e76a2SIlya Leoshkevich static void signal_func(int sig)
17*ef7e76a2SIlya Leoshkevich {
18*ef7e76a2SIlya Leoshkevich     seen_sig = sig;
19*ef7e76a2SIlya Leoshkevich }
20*ef7e76a2SIlya Leoshkevich 
thread_func(void * arg)21*ef7e76a2SIlya Leoshkevich static void *thread_func(void *arg)
22*ef7e76a2SIlya Leoshkevich {
23*ef7e76a2SIlya Leoshkevich     kill(getpid(), SIGUSR2);
24*ef7e76a2SIlya Leoshkevich     return NULL;
25*ef7e76a2SIlya Leoshkevich }
26*ef7e76a2SIlya Leoshkevich 
main(void)27*ef7e76a2SIlya Leoshkevich int main(void)
28*ef7e76a2SIlya Leoshkevich {
29*ef7e76a2SIlya Leoshkevich     struct sigaction act = {
30*ef7e76a2SIlya Leoshkevich         .sa_handler = signal_func,
31*ef7e76a2SIlya Leoshkevich     };
32*ef7e76a2SIlya Leoshkevich     pthread_t thread;
33*ef7e76a2SIlya Leoshkevich     sigset_t set;
34*ef7e76a2SIlya Leoshkevich     int sig;
35*ef7e76a2SIlya Leoshkevich 
36*ef7e76a2SIlya Leoshkevich     assert(sigaction(SIGUSR1, &act, NULL) == 0);
37*ef7e76a2SIlya Leoshkevich 
38*ef7e76a2SIlya Leoshkevich     assert(sigemptyset(&set) == 0);
39*ef7e76a2SIlya Leoshkevich     assert(sigaddset(&set, SIGUSR2) == 0);
40*ef7e76a2SIlya Leoshkevich     assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
41*ef7e76a2SIlya Leoshkevich 
42*ef7e76a2SIlya Leoshkevich     kill(getpid(), SIGUSR1);
43*ef7e76a2SIlya Leoshkevich     assert(seen_sig == SIGUSR1);
44*ef7e76a2SIlya Leoshkevich 
45*ef7e76a2SIlya Leoshkevich     assert(pthread_create(&thread, NULL, thread_func, NULL) == 0);
46*ef7e76a2SIlya Leoshkevich     assert(sigwait(&set, &sig) == 0);
47*ef7e76a2SIlya Leoshkevich     assert(sig == SIGUSR2);
48*ef7e76a2SIlya Leoshkevich     assert(pthread_join(thread, NULL) == 0);
49*ef7e76a2SIlya Leoshkevich 
50*ef7e76a2SIlya Leoshkevich     return EXIT_SUCCESS;
51*ef7e76a2SIlya Leoshkevich }
52