Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-signal6.c
blob0d4d0f7f941b9f17f75f1012c9ac6624447a3b75
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
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/>. */
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
27 #define N 2
28 static pthread_barrier_t bar;
29 static struct
31 void *p;
32 pthread_t s;
33 } ti[N];
34 static int sig1;
37 static void
38 handler (int sig)
40 pthread_t self = pthread_self ();
41 size_t i;
43 for (i = 0; i < N; ++i)
44 if (ti[i].s == self)
46 if ((uintptr_t) ti[i].p <= (uintptr_t) &self
47 && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
49 puts ("alt stack not used");
50 exit (1);
53 printf ("thread %zu used alt stack for signal %d\n", i, sig);
55 return;
58 puts ("handler: thread not found");
59 exit (1);
63 static void *
64 tf (void *arg)
66 size_t nr = (uintptr_t) arg;
67 if (nr >= N)
69 puts ("wrong nr parameter");
70 exit (1);
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)
80 puts ("tf: sigaddset failed");
81 exit (1);
83 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
85 puts ("tf: sigmask failed");
86 exit (1);
89 void *p = malloc (2 * MINSIGSTKSZ);
90 if (p == NULL)
92 puts ("tf: malloc failed");
93 exit (1);
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)
102 puts ("tf: sigaltstack failed");
103 exit (1);
106 ti[nr].p = p;
107 ti[nr].s = pthread_self ();
109 pthread_barrier_wait (&bar);
111 pthread_barrier_wait (&bar);
113 return NULL;
117 static int
118 do_test (void)
120 sig1 = SIGRTMIN;
121 if (sig1 + N > SIGRTMAX)
123 puts ("too few RT signals");
124 return 0;
127 struct sigaction sa;
128 sa.sa_handler = handler;
129 sa.sa_flags = 0;
130 sigemptyset (&sa.sa_mask);
132 if (sigaction (sig1, &sa, NULL) != 0
133 || sigaction (sig1 + 1, &sa, NULL) != 0
134 || sigaction (sig1 + 2, &sa, NULL) != 0)
136 puts ("sigaction failed");
137 return 1;
140 if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
142 puts ("barrier_init failed");
143 return 1;
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)
151 puts ("create failed");
152 return 1;
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)
162 puts ("main: sigmask failed");
163 return 1;
166 pthread_barrier_wait (&bar);
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);
175 /* Give the signals a chance to be worked on. */
176 sleep (1);
178 pthread_barrier_wait (&bar);
180 for (i = 0; i < N; ++i)
181 if (pthread_join (th[i], NULL) != 0)
183 puts ("join failed");
184 return 1;
187 return 0;
190 #define TEST_FUNCTION do_test ()
191 #include "../test-skeleton.c"