* configure.in: Avoid variable named BASH.
[glibc.git] / nptl / tst-cancel20.c
blobd88cb9caf85323fa491a74dd4e471d1f40103b33
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
28 static int fd[4];
29 static pthread_barrier_t b;
30 volatile int in_sh_body;
31 unsigned long cleanups;
33 static void
34 cl (void *arg)
36 cleanups = (cleanups << 4) | (long) arg;
40 static void __attribute__((noinline))
41 sh_body (void)
43 char c;
45 pthread_cleanup_push (cl, (void *) 1L);
47 in_sh_body = 1;
48 if (read (fd[2], &c, 1) == 1)
50 puts ("read succeeded");
51 exit (1);
54 pthread_cleanup_pop (0);
58 static void
59 sh (int sig)
61 pthread_cleanup_push (cl, (void *) 2L);
62 sh_body ();
63 in_sh_body = 0;
65 pthread_cleanup_pop (0);
69 static void __attribute__((noinline))
70 tf_body (void)
72 char c;
74 pthread_cleanup_push (cl, (void *) 3L);
76 int r = pthread_barrier_wait (&b);
77 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
79 puts ("child thread: barrier_wait failed");
80 exit (1);
83 if (read (fd[0], &c, 1) == 1)
85 puts ("read succeeded");
86 exit (1);
89 read (fd[0], &c, 1);
91 pthread_cleanup_pop (0);
95 static void *
96 tf (void *arg)
98 pthread_cleanup_push (cl, (void *) 4L);
99 tf_body ();
100 pthread_cleanup_pop (0);
101 return NULL;
105 static int
106 do_one_test (void)
108 in_sh_body = 0;
109 cleanups = 0;
110 if (pipe (fd) != 0 || pipe (fd + 2) != 0)
112 puts ("pipe failed");
113 return 1;
116 pthread_t th;
117 if (pthread_create (&th, NULL, tf, NULL) != 0)
119 puts ("create failed");
120 return 1;
123 int r = pthread_barrier_wait (&b);
124 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
126 puts ("parent thread: barrier_wait failed");
127 return 1;
130 sleep (1);
132 r = pthread_kill (th, SIGHUP);
133 if (r)
135 errno = r;
136 printf ("pthread_kill failed %m\n");
137 return 1;
140 while (in_sh_body == 0)
141 sleep (1);
143 if (pthread_cancel (th) != 0)
145 puts ("cancel failed");
146 return 1;
149 /* This will cause the read in the child to return. */
150 close (fd[0]);
151 close (fd[1]);
152 close (fd[2]);
153 close (fd[3]);
155 void *ret;
156 if (pthread_join (th, &ret) != 0)
158 puts ("join failed");
159 return 1;
162 if (ret != PTHREAD_CANCELED)
164 puts ("result is wrong");
165 return 1;
168 if (cleanups != 0x1234L)
170 printf ("called cleanups %lx\n", cleanups);
171 return 1;
174 return 0;
178 static int
179 do_test (void)
181 stack_t ss;
182 ss.ss_sp = malloc (2 * SIGSTKSZ);
183 if (ss.ss_sp == NULL)
185 puts ("failed to allocate alternate stack");
186 return 1;
188 ss.ss_flags = 0;
189 ss.ss_size = 2 * SIGSTKSZ;
190 if (sigaltstack (&ss, NULL) < 0)
192 printf ("sigaltstack failed %m\n");
193 return 1;
196 if (pthread_barrier_init (&b, NULL, 2) != 0)
198 puts ("barrier_init failed");
199 return 1;
202 struct sigaction sa;
203 sa.sa_handler = sh;
204 sigemptyset (&sa.sa_mask);
205 sa.sa_flags = 0;
207 if (sigaction (SIGHUP, &sa, NULL) != 0)
209 puts ("sigaction failed");
210 return 1;
213 puts ("sa_flags = 0 test");
214 if (do_one_test ())
215 return 1;
217 sa.sa_handler = sh;
218 sigemptyset (&sa.sa_mask);
219 sa.sa_flags = SA_ONSTACK;
221 if (sigaction (SIGHUP, &sa, NULL) != 0)
223 puts ("sigaction failed");
224 return 1;
227 puts ("sa_flags = SA_ONSTACK test");
228 if (do_one_test ())
229 return 1;
231 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
232 sigemptyset (&sa.sa_mask);
233 sa.sa_flags = SA_SIGINFO;
235 if (sigaction (SIGHUP, &sa, NULL) != 0)
237 puts ("sigaction failed");
238 return 1;
241 puts ("sa_flags = SA_SIGINFO test");
242 if (do_one_test ())
243 return 1;
245 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
246 sigemptyset (&sa.sa_mask);
247 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
249 if (sigaction (SIGHUP, &sa, NULL) != 0)
251 puts ("sigaction failed");
252 return 1;
255 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
256 if (do_one_test ())
257 return 1;
259 return 0;
262 #define TIMEOUT 40
263 #define TEST_FUNCTION do_test ()
264 #include "../test-skeleton.c"