1 /* Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
27 static pthread_barrier_t b
;
28 volatile int in_sh_body
;
29 unsigned long cleanups
;
34 cleanups
= (cleanups
<< 4) | (long) arg
;
38 static void __attribute__((noinline
))
43 pthread_cleanup_push (cl
, (void *) 1L);
46 if (read (fd
[2], &c
, 1) == 1)
48 puts ("read succeeded");
52 pthread_cleanup_pop (0);
59 pthread_cleanup_push (cl
, (void *) 2L);
63 pthread_cleanup_pop (0);
67 static void __attribute__((noinline
))
72 pthread_cleanup_push (cl
, (void *) 3L);
74 int r
= pthread_barrier_wait (&b
);
75 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
77 puts ("child thread: barrier_wait failed");
81 if (read (fd
[0], &c
, 1) == 1)
83 puts ("read succeeded");
89 pthread_cleanup_pop (0);
96 pthread_cleanup_push (cl
, (void *) 4L);
98 pthread_cleanup_pop (0);
108 if (pipe (fd
) != 0 || pipe (fd
+ 2) != 0)
110 puts ("pipe failed");
115 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
117 puts ("create failed");
121 int r
= pthread_barrier_wait (&b
);
122 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
124 puts ("parent thread: barrier_wait failed");
130 r
= pthread_kill (th
, SIGHUP
);
134 printf ("pthread_kill failed %m\n");
138 while (in_sh_body
== 0)
141 if (pthread_cancel (th
) != 0)
143 puts ("cancel failed");
148 if (pthread_join (th
, &ret
) != 0)
150 puts ("join failed");
154 if (ret
!= PTHREAD_CANCELED
)
156 puts ("result is wrong");
160 if (cleanups
!= 0x1234L
)
162 printf ("called cleanups %lx\n", cleanups
);
166 /* The pipe closing must be issued after the cancellation handling to avoid
167 a race condition where the cancellation runs after both pipe ends are
168 closed. In this case the read syscall returns EOF and the cancellation
183 ss
.ss_sp
= malloc (2 * SIGSTKSZ
);
184 if (ss
.ss_sp
== NULL
)
186 puts ("failed to allocate alternate stack");
190 ss
.ss_size
= 2 * SIGSTKSZ
;
191 if (sigaltstack (&ss
, NULL
) < 0)
193 printf ("sigaltstack failed %m\n");
197 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
199 puts ("barrier_init failed");
205 sigemptyset (&sa
.sa_mask
);
208 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
210 puts ("sigaction failed");
214 puts ("sa_flags = 0 test");
219 sigemptyset (&sa
.sa_mask
);
220 sa
.sa_flags
= SA_ONSTACK
;
222 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
224 puts ("sigaction failed");
228 puts ("sa_flags = SA_ONSTACK test");
233 sa
.sa_sigaction
= (void (*)(int, siginfo_t
*, void *)) sh
;
234 sigemptyset (&sa
.sa_mask
);
235 sa
.sa_flags
= SA_SIGINFO
;
237 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
239 puts ("sigaction failed");
243 puts ("sa_flags = SA_SIGINFO test");
247 sa
.sa_sigaction
= (void (*)(int, siginfo_t
*, void *)) sh
;
248 sigemptyset (&sa
.sa_mask
);
249 sa
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
251 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
253 puts ("sigaction failed");
257 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
266 #define TEST_FUNCTION do_test ()
267 #include "../test-skeleton.c"