1 /*
2  * Test the lowest and the highest real-time signals.
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include <assert.h>
7 #include <signal.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 
14 /* For hexagon and microblaze. */
15 #ifndef __SIGRTMIN
16 #define __SIGRTMIN 32
17 #endif
18 
19 extern char **environ;
20 
21 static bool seen_sigrtmin, seen_sigrtmax;
22 
23 static void handle_signal(int sig)
24 {
25     if (sig == SIGRTMIN) {
26         seen_sigrtmin = true;
27     } else if (sig == SIGRTMAX) {
28         seen_sigrtmax = true;
29     } else {
30         _exit(1);
31     }
32 }
33 
34 int main(int argc, char **argv)
35 {
36     char *qemu = getenv("QEMU");
37     struct sigaction act;
38 
39     assert(qemu);
40 
41     if (!getenv("QEMU_RTSIG_MAP")) {
42         char **new_argv = malloc((argc + 2) + sizeof(char *));
43         int tsig1, hsig1, count1, tsig2, hsig2, count2;
44         char rt_sigmap[64];
45 
46         /* Re-exec with a mapping that includes SIGRTMIN and SIGRTMAX. */
47         new_argv[0] = qemu;
48         memcpy(&new_argv[1], argv, (argc + 1) * sizeof(char *));
49         tsig1 = __SIGRTMIN;
50         /* The host must have a few signals starting from this one. */
51         hsig1 = 36;
52         count1 = SIGRTMIN - __SIGRTMIN + 1;
53         tsig2 = SIGRTMAX;
54         hsig2 = hsig1 + count1;
55         count2 = 1;
56         snprintf(rt_sigmap, sizeof(rt_sigmap), "%d %d %d,%d %d %d",
57                  tsig1, hsig1, count1, tsig2, hsig2, count2);
58         setenv("QEMU_RTSIG_MAP", rt_sigmap, 0);
59         assert(execve(new_argv[0], new_argv, environ) == 0);
60         return EXIT_FAILURE;
61     }
62 
63     memset(&act, 0, sizeof(act));
64     act.sa_handler = handle_signal;
65     assert(sigaction(SIGRTMIN, &act, NULL) == 0);
66     assert(sigaction(SIGRTMAX, &act, NULL) == 0);
67 
68     assert(kill(getpid(), SIGRTMIN) == 0);
69     assert(seen_sigrtmin);
70     assert(kill(getpid(), SIGRTMAX) == 0);
71     assert(seen_sigrtmax);
72 
73     return EXIT_SUCCESS;
74 }
75