xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 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. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | |
| 27 | static int fd[4]; |
| 28 | static pthread_barrier_t b; |
| 29 | volatile int in_sh_body; |
| 30 | unsigned long cleanups; |
| 31 | |
| 32 | static void |
| 33 | cl (void *arg) |
| 34 | { |
| 35 | cleanups = (cleanups << 4) | (long) arg; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | static void __attribute__((noinline)) |
| 40 | sh_body (void) |
| 41 | { |
| 42 | char c; |
| 43 | |
| 44 | pthread_cleanup_push (cl, (void *) 1L); |
| 45 | |
| 46 | in_sh_body = 1; |
| 47 | if (read (fd[2], &c, 1) == 1) |
| 48 | { |
| 49 | puts ("read succeeded"); |
| 50 | exit (1); |
| 51 | } |
| 52 | |
| 53 | pthread_cleanup_pop (0); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static void |
| 58 | sh (int sig) |
| 59 | { |
| 60 | pthread_cleanup_push (cl, (void *) 2L); |
| 61 | sh_body (); |
| 62 | in_sh_body = 0; |
| 63 | |
| 64 | pthread_cleanup_pop (0); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static void __attribute__((noinline)) |
| 69 | tf_body (void) |
| 70 | { |
| 71 | char c; |
| 72 | |
| 73 | pthread_cleanup_push (cl, (void *) 3L); |
| 74 | |
| 75 | int r = pthread_barrier_wait (&b); |
| 76 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 77 | { |
| 78 | puts ("child thread: barrier_wait failed"); |
| 79 | exit (1); |
| 80 | } |
| 81 | |
| 82 | if (read (fd[0], &c, 1) == 1) |
| 83 | { |
| 84 | puts ("read succeeded"); |
| 85 | exit (1); |
| 86 | } |
| 87 | |
| 88 | read (fd[0], &c, 1); |
| 89 | |
| 90 | pthread_cleanup_pop (0); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static void * |
| 95 | tf (void *arg) |
| 96 | { |
| 97 | pthread_cleanup_push (cl, (void *) 4L); |
| 98 | tf_body (); |
| 99 | pthread_cleanup_pop (0); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static int |
| 105 | do_one_test (void) |
| 106 | { |
| 107 | in_sh_body = 0; |
| 108 | cleanups = 0; |
| 109 | if (pipe (fd) != 0 || pipe (fd + 2) != 0) |
| 110 | { |
| 111 | puts ("pipe failed"); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | pthread_t th; |
| 116 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 117 | { |
| 118 | puts ("create failed"); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | int r = pthread_barrier_wait (&b); |
| 123 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 124 | { |
| 125 | puts ("parent thread: barrier_wait failed"); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | sleep (1); |
| 130 | |
| 131 | r = pthread_kill (th, SIGHUP); |
| 132 | if (r) |
| 133 | { |
| 134 | errno = r; |
| 135 | printf ("pthread_kill failed %m\n"); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | while (in_sh_body == 0) |
| 140 | sleep (1); |
| 141 | |
| 142 | if (pthread_cancel (th) != 0) |
| 143 | { |
| 144 | puts ("cancel failed"); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | void *ret; |
| 149 | if (pthread_join (th, &ret) != 0) |
| 150 | { |
| 151 | puts ("join failed"); |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | if (ret != PTHREAD_CANCELED) |
| 156 | { |
| 157 | puts ("result is wrong"); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | if (cleanups != 0x1234L) |
| 162 | { |
| 163 | printf ("called cleanups %lx\n", cleanups); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 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 |
| 170 | must not act. */ |
| 171 | close (fd[0]); |
| 172 | close (fd[1]); |
| 173 | close (fd[2]); |
| 174 | close (fd[3]); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static int |
| 181 | do_test (void) |
| 182 | { |
| 183 | stack_t ss; |
| 184 | ss.ss_sp = malloc (2 * SIGSTKSZ); |
| 185 | if (ss.ss_sp == NULL) |
| 186 | { |
| 187 | puts ("failed to allocate alternate stack"); |
| 188 | return 1; |
| 189 | } |
| 190 | ss.ss_flags = 0; |
| 191 | ss.ss_size = 2 * SIGSTKSZ; |
| 192 | if (sigaltstack (&ss, NULL) < 0) |
| 193 | { |
| 194 | printf ("sigaltstack failed %m\n"); |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | if (pthread_barrier_init (&b, NULL, 2) != 0) |
| 199 | { |
| 200 | puts ("barrier_init failed"); |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | struct sigaction sa; |
| 205 | sa.sa_handler = sh; |
| 206 | sigemptyset (&sa.sa_mask); |
| 207 | sa.sa_flags = 0; |
| 208 | |
| 209 | if (sigaction (SIGHUP, &sa, NULL) != 0) |
| 210 | { |
| 211 | puts ("sigaction failed"); |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | puts ("sa_flags = 0 test"); |
| 216 | if (do_one_test ()) |
| 217 | return 1; |
| 218 | |
| 219 | sa.sa_handler = sh; |
| 220 | sigemptyset (&sa.sa_mask); |
| 221 | sa.sa_flags = SA_ONSTACK; |
| 222 | |
| 223 | if (sigaction (SIGHUP, &sa, NULL) != 0) |
| 224 | { |
| 225 | puts ("sigaction failed"); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | puts ("sa_flags = SA_ONSTACK test"); |
| 230 | if (do_one_test ()) |
| 231 | return 1; |
| 232 | |
| 233 | #ifdef SA_SIGINFO |
| 234 | sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh; |
| 235 | sigemptyset (&sa.sa_mask); |
| 236 | sa.sa_flags = SA_SIGINFO; |
| 237 | |
| 238 | if (sigaction (SIGHUP, &sa, NULL) != 0) |
| 239 | { |
| 240 | puts ("sigaction failed"); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | puts ("sa_flags = SA_SIGINFO test"); |
| 245 | if (do_one_test ()) |
| 246 | return 1; |
| 247 | |
| 248 | sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh; |
| 249 | sigemptyset (&sa.sa_mask); |
| 250 | sa.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 251 | |
| 252 | if (sigaction (SIGHUP, &sa, NULL) != 0) |
| 253 | { |
| 254 | puts ("sigaction failed"); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test"); |
| 259 | if (do_one_test ()) |
| 260 | return 1; |
| 261 | #endif |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | #define TIMEOUT 40 |
| 267 | #define TEST_FUNCTION do_test () |
| 268 | #include "../test-skeleton.c" |