1 /* Copyright (C) 2003-2016 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, see
17 <http://www.gnu.org/licenses/>. */
28 static pthread_barrier_t b
;
29 volatile int in_sh_body
;
30 unsigned long cleanups
;
35 cleanups
= (cleanups
<< 4) | (long) arg
;
39 static void __attribute__((noinline
))
44 pthread_cleanup_push (cl
, (void *) 1L);
47 if (read (fd
[2], &c
, 1) == 1)
49 puts ("read succeeded");
53 pthread_cleanup_pop (0);
60 pthread_cleanup_push (cl
, (void *) 2L);
64 pthread_cleanup_pop (0);
68 static void __attribute__((noinline
))
73 pthread_cleanup_push (cl
, (void *) 3L);
75 int r
= pthread_barrier_wait (&b
);
76 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
78 puts ("child thread: barrier_wait failed");
82 if (read (fd
[0], &c
, 1) == 1)
84 puts ("read succeeded");
90 pthread_cleanup_pop (0);
97 pthread_cleanup_push (cl
, (void *) 4L);
99 pthread_cleanup_pop (0);
109 if (pipe (fd
) != 0 || pipe (fd
+ 2) != 0)
111 puts ("pipe failed");
116 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
118 puts ("create failed");
122 int r
= pthread_barrier_wait (&b
);
123 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
125 puts ("parent thread: barrier_wait failed");
131 r
= pthread_kill (th
, SIGHUP
);
135 printf ("pthread_kill failed %m\n");
139 while (in_sh_body
== 0)
142 if (pthread_cancel (th
) != 0)
144 puts ("cancel failed");
149 if (pthread_join (th
, &ret
) != 0)
151 puts ("join failed");
155 if (ret
!= PTHREAD_CANCELED
)
157 puts ("result is wrong");
161 if (cleanups
!= 0x1234L
)
163 printf ("called cleanups %lx\n", cleanups
);
167 /* The pipe closing must be issued after the cancellation handling to avoid
168 a race condition where the cancellation runs after both pipe ends are
169 closed. In this case the read syscall returns EOF and the cancellation
184 ss
.ss_sp
= malloc (2 * SIGSTKSZ
);
185 if (ss
.ss_sp
== NULL
)
187 puts ("failed to allocate alternate stack");
191 ss
.ss_size
= 2 * SIGSTKSZ
;
192 if (sigaltstack (&ss
, NULL
) < 0)
194 printf ("sigaltstack failed %m\n");
198 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
200 puts ("barrier_init failed");
206 sigemptyset (&sa
.sa_mask
);
209 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
211 puts ("sigaction failed");
215 puts ("sa_flags = 0 test");
220 sigemptyset (&sa
.sa_mask
);
221 sa
.sa_flags
= SA_ONSTACK
;
223 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
225 puts ("sigaction failed");
229 puts ("sa_flags = SA_ONSTACK test");
234 sa
.sa_sigaction
= (void (*)(int, siginfo_t
*, void *)) sh
;
235 sigemptyset (&sa
.sa_mask
);
236 sa
.sa_flags
= SA_SIGINFO
;
238 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
240 puts ("sigaction failed");
244 puts ("sa_flags = SA_SIGINFO test");
248 sa
.sa_sigaction
= (void (*)(int, siginfo_t
*, void *)) sh
;
249 sigemptyset (&sa
.sa_mask
);
250 sa
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
252 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
254 puts ("sigaction failed");
258 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
267 #define TEST_FUNCTION do_test ()
268 #include "../test-skeleton.c"