1 /* Copyright (C) 2003-2024 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");
87 pthread_cleanup_pop (0);
94 pthread_cleanup_push (cl
, (void *) 4L);
96 pthread_cleanup_pop (0);
106 if (pipe (fd
) != 0 || pipe (fd
+ 2) != 0)
108 puts ("pipe failed");
113 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
115 puts ("create failed");
119 int r
= pthread_barrier_wait (&b
);
120 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
122 puts ("parent thread: barrier_wait failed");
128 r
= pthread_kill (th
, SIGHUP
);
132 printf ("pthread_kill failed %m\n");
136 while (in_sh_body
== 0)
139 if (pthread_cancel (th
) != 0)
141 puts ("cancel failed");
146 if (pthread_join (th
, &ret
) != 0)
148 puts ("join failed");
152 if (ret
!= PTHREAD_CANCELED
)
154 puts ("result is wrong");
158 if (cleanups
!= 0x1234L
)
160 printf ("called cleanups %lx\n", cleanups
);
164 /* The pipe closing must be issued after the cancellation handling to avoid
165 a race condition where the cancellation runs after both pipe ends are
166 closed. In this case the read syscall returns EOF and the cancellation
181 ss
.ss_sp
= malloc (2 * SIGSTKSZ
);
182 if (ss
.ss_sp
== NULL
)
184 puts ("failed to allocate alternate stack");
188 ss
.ss_size
= 2 * SIGSTKSZ
;
189 if (sigaltstack (&ss
, NULL
) < 0)
191 printf ("sigaltstack failed %m\n");
195 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
197 puts ("barrier_init failed");
203 sigemptyset (&sa
.sa_mask
);
206 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
208 puts ("sigaction failed");
212 puts ("sa_flags = 0 test");
217 sigemptyset (&sa
.sa_mask
);
218 sa
.sa_flags
= SA_ONSTACK
;
220 if (sigaction (SIGHUP
, &sa
, NULL
) != 0)
222 puts ("sigaction failed");
226 puts ("sa_flags = SA_ONSTACK test");
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");
241 puts ("sa_flags = SA_SIGINFO test");
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");
255 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
264 #define TEST_FUNCTION do_test ()
265 #include "../test-skeleton.c"