lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@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 <pthread.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | |
| 27 | #ifdef SIGRTMIN |
| 28 | |
| 29 | # define N 2 |
| 30 | static pthread_barrier_t bar; |
| 31 | static struct |
| 32 | { |
| 33 | void *p; |
| 34 | pthread_t s; |
| 35 | } ti[N]; |
| 36 | static int sig1; |
| 37 | |
| 38 | |
| 39 | static void |
| 40 | handler (int sig) |
| 41 | { |
| 42 | pthread_t self = pthread_self (); |
| 43 | size_t i; |
| 44 | |
| 45 | for (i = 0; i < N; ++i) |
| 46 | if (ti[i].s == self) |
| 47 | { |
| 48 | if ((uintptr_t) ti[i].p <= (uintptr_t) &self |
| 49 | && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self) |
| 50 | { |
| 51 | puts ("alt stack not used"); |
| 52 | exit (1); |
| 53 | } |
| 54 | |
| 55 | printf ("thread %zu used alt stack for signal %d\n", i, sig); |
| 56 | |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | puts ("handler: thread not found"); |
| 61 | exit (1); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static void * |
| 66 | tf (void *arg) |
| 67 | { |
| 68 | size_t nr = (uintptr_t) arg; |
| 69 | if (nr >= N) |
| 70 | { |
| 71 | puts ("wrong nr parameter"); |
| 72 | exit (1); |
| 73 | } |
| 74 | |
| 75 | sigset_t ss; |
| 76 | sigemptyset (&ss); |
| 77 | size_t i; |
| 78 | for (i = 0; i < N; ++i) |
| 79 | if (i != nr) |
| 80 | if (sigaddset (&ss, sig1 + i) != 0) |
| 81 | { |
| 82 | puts ("tf: sigaddset failed"); |
| 83 | exit (1); |
| 84 | } |
| 85 | if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) |
| 86 | { |
| 87 | puts ("tf: sigmask failed"); |
| 88 | exit (1); |
| 89 | } |
| 90 | |
| 91 | void *p = malloc (2 * MINSIGSTKSZ); |
| 92 | if (p == NULL) |
| 93 | { |
| 94 | puts ("tf: malloc failed"); |
| 95 | exit (1); |
| 96 | } |
| 97 | |
| 98 | stack_t s; |
| 99 | s.ss_sp = p; |
| 100 | s.ss_size = 2 * MINSIGSTKSZ; |
| 101 | s.ss_flags = 0; |
| 102 | if (sigaltstack (&s, NULL) != 0) |
| 103 | { |
| 104 | puts ("tf: sigaltstack failed"); |
| 105 | exit (1); |
| 106 | } |
| 107 | |
| 108 | ti[nr].p = p; |
| 109 | ti[nr].s = pthread_self (); |
| 110 | |
| 111 | pthread_barrier_wait (&bar); |
| 112 | |
| 113 | pthread_barrier_wait (&bar); |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static int |
| 120 | do_test (void) |
| 121 | { |
| 122 | sig1 = SIGRTMIN; |
| 123 | if (sig1 + N > SIGRTMAX) |
| 124 | { |
| 125 | puts ("too few RT signals"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | struct sigaction sa; |
| 130 | sa.sa_handler = handler; |
| 131 | sa.sa_flags = 0; |
| 132 | sigemptyset (&sa.sa_mask); |
| 133 | |
| 134 | if (sigaction (sig1, &sa, NULL) != 0 |
| 135 | || sigaction (sig1 + 1, &sa, NULL) != 0 |
| 136 | || sigaction (sig1 + 2, &sa, NULL) != 0) |
| 137 | { |
| 138 | puts ("sigaction failed"); |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | if (pthread_barrier_init (&bar, NULL, 1 + N) != 0) |
| 143 | { |
| 144 | puts ("barrier_init failed"); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | pthread_t th[N]; |
| 149 | size_t i; |
| 150 | for (i = 0; i < N; ++i) |
| 151 | if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0) |
| 152 | { |
| 153 | puts ("create failed"); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | /* Block the three signals. */ |
| 158 | sigset_t ss; |
| 159 | sigemptyset (&ss); |
| 160 | for (i = 0; i <= N; ++i) |
| 161 | sigaddset (&ss, sig1 + i); |
| 162 | if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) |
| 163 | { |
| 164 | puts ("main: sigmask failed"); |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | pthread_barrier_wait (&bar); |
| 169 | |
| 170 | /* Send some signals. */ |
| 171 | pid_t me = getpid (); |
| 172 | kill (me, sig1 + N); |
| 173 | for (i = 0; i < N; ++i) |
| 174 | kill (me, sig1 + i); |
| 175 | kill (me, sig1 + N); |
| 176 | |
| 177 | /* Give the signals a chance to be worked on. */ |
| 178 | sleep (1); |
| 179 | |
| 180 | pthread_barrier_wait (&bar); |
| 181 | |
| 182 | for (i = 0; i < N; ++i) |
| 183 | if (pthread_join (th[i], NULL) != 0) |
| 184 | { |
| 185 | puts ("join failed"); |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | # define TEST_FUNCTION do_test () |
| 193 | |
| 194 | #else |
| 195 | # define TEST_FUNCTION 0 |
| 196 | #endif |
| 197 | #include "../test-skeleton.c" |