Add GLIBC_2.9 version tag for libutil.
[glibc/history.git] / nptl / tst-cancel21.c
blobcc00cc1683207e3e383119e660a618a26f77110a
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 <sys/wait.h>
26 #include <unistd.h>
29 static int fd[4];
30 static pthread_barrier_t b;
31 volatile int in_sh_body;
32 unsigned long cleanups;
34 static void
35 cl (void *arg)
37 cleanups = (cleanups << 4) | (long) arg;
41 static void __attribute__((noinline))
42 sh_body (void)
44 char c;
46 pthread_cleanup_push (cl, (void *) 1L);
48 in_sh_body = 1;
49 if (read (fd[2], &c, 1) == 1)
51 puts ("read succeeded");
52 exit (1);
55 pthread_cleanup_pop (0);
59 static void
60 sh (int sig)
62 pthread_cleanup_push (cl, (void *) 2L);
63 sh_body ();
64 in_sh_body = 0;
66 pthread_cleanup_pop (0);
70 static void __attribute__((noinline))
71 tf_body (void)
73 char c;
75 pthread_cleanup_push (cl, (void *) 3L);
77 int r = pthread_barrier_wait (&b);
78 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
80 puts ("child thread: barrier_wait failed");
81 exit (1);
84 if (read (fd[0], &c, 1) == 1)
86 puts ("read succeeded");
87 exit (1);
90 read (fd[0], &c, 1);
92 pthread_cleanup_pop (0);
96 static void *
97 tf (void *arg)
99 pthread_t th = (pthread_t) arg;
101 int r = pthread_barrier_wait (&b);
102 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
104 puts ("parent thread: barrier_wait failed");
105 exit (1);
108 sleep (1);
110 r = pthread_kill (th, SIGHUP);
111 if (r)
113 errno = r;
114 printf ("pthread_kill failed %m\n");
115 exit (1);
118 while (in_sh_body == 0)
119 sleep (1);
121 if (pthread_cancel (th) != 0)
123 puts ("cancel failed");
124 exit (1);
127 /* This will cause the read in the initial thread to return. */
128 close (fd[0]);
129 close (fd[1]);
130 close (fd[2]);
131 close (fd[3]);
133 void *ret;
134 if (pthread_join (th, &ret) != 0)
136 puts ("join failed");
137 exit (1);
140 if (ret != PTHREAD_CANCELED)
142 puts ("result is wrong");
143 exit (1);
146 if (cleanups != 0x1234L)
148 printf ("called cleanups %lx\n", cleanups);
149 exit (1);
152 if (pthread_barrier_destroy (&b))
154 puts ("barrier destroy failed");
155 exit (1);
158 exit (0);
162 static int
163 do_one_test (void)
165 in_sh_body = 0;
167 pid_t pid = fork ();
169 if (pid == -1)
171 printf ("fork failed: %m\n");
172 return 1;
175 if (pid)
177 int status;
178 if (waitpid (pid, &status, 0) < 0)
180 printf ("waitpid failed %m\n");
181 return 1;
184 return !WIFEXITED (status) || WEXITSTATUS (status);
187 if (pthread_barrier_init (&b, NULL, 2) != 0)
189 puts ("barrier_init failed");
190 exit (1);
193 cleanups = 0;
194 if (pipe (fd) != 0 || pipe (fd + 2) != 0)
196 puts ("pipe failed");
197 exit (1);
200 pthread_t th;
201 if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
203 puts ("create failed");
204 exit (1);
207 pthread_cleanup_push (cl, (void *) 4L);
208 tf_body ();
209 pthread_cleanup_pop (0);
210 exit (1);
214 static int
215 do_test (void)
217 stack_t ss;
218 ss.ss_sp = malloc (2 * SIGSTKSZ);
219 if (ss.ss_sp == NULL)
221 puts ("failed to allocate alternate stack");
222 return 1;
224 ss.ss_flags = 0;
225 ss.ss_size = 2 * SIGSTKSZ;
226 if (sigaltstack (&ss, NULL) < 0)
228 printf ("sigaltstack failed %m\n");
229 return 1;
232 struct sigaction sa;
233 sa.sa_handler = sh;
234 sigemptyset (&sa.sa_mask);
235 sa.sa_flags = 0;
237 if (sigaction (SIGHUP, &sa, NULL) != 0)
239 puts ("sigaction failed");
240 return 1;
243 puts ("sa_flags = 0 test");
244 if (do_one_test ())
245 return 1;
247 sa.sa_handler = sh;
248 sigemptyset (&sa.sa_mask);
249 sa.sa_flags = SA_ONSTACK;
251 if (sigaction (SIGHUP, &sa, NULL) != 0)
253 puts ("sigaction failed");
254 return 1;
257 puts ("sa_flags = SA_ONSTACK test");
258 if (do_one_test ())
259 return 1;
261 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
262 sigemptyset (&sa.sa_mask);
263 sa.sa_flags = SA_SIGINFO;
265 if (sigaction (SIGHUP, &sa, NULL) != 0)
267 puts ("sigaction failed");
268 return 1;
271 puts ("sa_flags = SA_SIGINFO test");
272 if (do_one_test ())
273 return 1;
275 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
276 sigemptyset (&sa.sa_mask);
277 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
279 if (sigaction (SIGHUP, &sa, NULL) != 0)
281 puts ("sigaction failed");
282 return 1;
285 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
286 if (do_one_test ())
287 return 1;
289 return 0;
292 #define TIMEOUT 40
293 #define TEST_FUNCTION do_test ()
294 #include "../test-skeleton.c"