1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26
27 #define N 2
28 static pthread_barrier_t bar;
29 static struct
30 {
31 void *p;
32 pthread_t s;
33 } ti[N];
34 static int sig1;
35
36
37 static void
handler(int sig)38 handler (int sig)
39 {
40 pthread_t self = pthread_self ();
41 size_t i;
42
43 for (i = 0; i < N; ++i)
44 if (ti[i].s == self)
45 {
46 if ((uintptr_t) ti[i].p <= (uintptr_t) &self
47 && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
48 {
49 puts ("alt stack not used");
50 exit (1);
51 }
52
53 printf ("thread %zu used alt stack for signal %d\n", i, sig);
54
55 return;
56 }
57
58 puts ("handler: thread not found");
59 exit (1);
60 }
61
62
63 static void *
tf(void * arg)64 tf (void *arg)
65 {
66 size_t nr = (uintptr_t) arg;
67 if (nr >= N)
68 {
69 puts ("wrong nr parameter");
70 exit (1);
71 }
72
73 sigset_t ss;
74 sigemptyset (&ss);
75 size_t i;
76 for (i = 0; i < N; ++i)
77 if (i != nr)
78 if (sigaddset (&ss, sig1 + i) != 0)
79 {
80 puts ("tf: sigaddset failed");
81 exit (1);
82 }
83 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
84 {
85 puts ("tf: sigmask failed");
86 exit (1);
87 }
88
89 void *p = malloc (2 * MINSIGSTKSZ);
90 if (p == NULL)
91 {
92 puts ("tf: malloc failed");
93 exit (1);
94 }
95
96 stack_t s;
97 s.ss_sp = p;
98 s.ss_size = 2 * MINSIGSTKSZ;
99 s.ss_flags = 0;
100 if (sigaltstack (&s, NULL) != 0)
101 {
102 puts ("tf: sigaltstack failed");
103 exit (1);
104 }
105
106 ti[nr].p = p;
107 ti[nr].s = pthread_self ();
108
109 pthread_barrier_wait (&bar);
110
111 pthread_barrier_wait (&bar);
112
113 return NULL;
114 }
115
116
117 static int
do_test(void)118 do_test (void)
119 {
120 sig1 = SIGRTMIN;
121 if (sig1 + N > SIGRTMAX)
122 {
123 puts ("too few RT signals");
124 return 0;
125 }
126
127 struct sigaction sa;
128 sa.sa_handler = handler;
129 sa.sa_flags = 0;
130 sigemptyset (&sa.sa_mask);
131
132 if (sigaction (sig1, &sa, NULL) != 0
133 || sigaction (sig1 + 1, &sa, NULL) != 0
134 || sigaction (sig1 + 2, &sa, NULL) != 0)
135 {
136 puts ("sigaction failed");
137 return 1;
138 }
139
140 if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
141 {
142 puts ("barrier_init failed");
143 return 1;
144 }
145
146 pthread_t th[N];
147 size_t i;
148 for (i = 0; i < N; ++i)
149 if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
150 {
151 puts ("create failed");
152 return 1;
153 }
154
155 /* Block the three signals. */
156 sigset_t ss;
157 sigemptyset (&ss);
158 for (i = 0; i <= N; ++i)
159 sigaddset (&ss, sig1 + i);
160 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
161 {
162 puts ("main: sigmask failed");
163 return 1;
164 }
165
166 pthread_barrier_wait (&bar);
167
168 /* Send some signals. */
169 pid_t me = getpid ();
170 kill (me, sig1 + N);
171 for (i = 0; i < N; ++i)
172 kill (me, sig1 + i);
173 kill (me, sig1 + N);
174
175 /* Give the signals a chance to be worked on. */
176 sleep (1);
177
178 pthread_barrier_wait (&bar);
179
180 for (i = 0; i < N; ++i)
181 if (pthread_join (th[i], NULL) != 0)
182 {
183 puts ("join failed");
184 return 1;
185 }
186
187 return 0;
188 }
189
190 #define TEST_FUNCTION do_test ()
191 #include "../test-skeleton.c"
192