1 /* 2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include "namespace.h" 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/signalvar.h> 33 #include <signal.h> 34 #include <errno.h> 35 #include <fcntl.h> 36 #include <unistd.h> 37 #include <string.h> 38 #include <pthread.h> 39 #include "un-namespace.h" 40 41 #include "thr_private.h" 42 43 /* #define DEBUG_SIGNAL */ 44 #ifdef DEBUG_SIGNAL 45 #define DBG_MSG stdout_debug 46 #else 47 #define DBG_MSG(x...) 48 #endif 49 50 extern int __pause(void); 51 int ___pause(void); 52 int _raise(int); 53 int __sigtimedwait(const sigset_t *set, siginfo_t *info, 54 const struct timespec * timeout); 55 int __sigwaitinfo(const sigset_t *set, siginfo_t *info); 56 int __sigwait(const sigset_t *set, int *sig); 57 58 static void 59 sigcancel_handler(int sig __unused, 60 siginfo_t *info __unused, ucontext_t *ucp __unused) 61 { 62 struct pthread *curthread = _get_curthread(); 63 64 _thr_ast(curthread); 65 } 66 67 void 68 _thr_ast(struct pthread *curthread) 69 { 70 if (!THR_IN_CRITICAL(curthread)) { 71 if (__predict_false( 72 SHOULD_ASYNC_CANCEL(curthread->cancelflags))) 73 _pthread_testcancel(); 74 if (__predict_false((curthread->flags & 75 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED)) 76 == THR_FLAGS_NEED_SUSPEND)) 77 _thr_suspend_check(curthread); 78 } 79 } 80 81 void 82 _thr_suspend_check(struct pthread *curthread) 83 { 84 umtx_t cycle; 85 int err; 86 87 err = errno; 88 /* 89 * Blocks SIGCANCEL which other threads must send. 90 */ 91 _thr_signal_block(curthread); 92 93 /* 94 * Increase critical_count, here we don't use THR_LOCK/UNLOCK 95 * because we are leaf code, we don't want to recursively call 96 * ourself. 97 */ 98 curthread->critical_count++; 99 THR_UMUTEX_LOCK(curthread, &(curthread)->lock); 100 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | 101 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) { 102 curthread->cycle++; 103 cycle = curthread->cycle; 104 105 /* Wake the thread suspending us. */ 106 _thr_umtx_wake(&curthread->cycle, INT_MAX); 107 108 /* 109 * if we are from pthread_exit, we don't want to 110 * suspend, just go and die. 111 */ 112 if (curthread->state == PS_DEAD) 113 break; 114 curthread->flags |= THR_FLAGS_SUSPENDED; 115 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 116 _thr_umtx_wait(&curthread->cycle, cycle, NULL); 117 THR_UMUTEX_LOCK(curthread, &(curthread)->lock); 118 curthread->flags &= ~THR_FLAGS_SUSPENDED; 119 } 120 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 121 curthread->critical_count--; 122 123 /* 124 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and 125 * a new signal frame will nest us, this seems a problem because 126 * stack will grow and overflow, but because kernel will automatically 127 * mask the SIGCANCEL when delivering the signal, so we at most only 128 * have one nesting signal frame, this should be fine. 129 */ 130 _thr_signal_unblock(curthread); 131 errno = err; 132 } 133 134 void 135 _thr_signal_init(void) 136 { 137 struct sigaction act; 138 139 /* Install cancel handler. */ 140 SIGEMPTYSET(act.sa_mask); 141 act.sa_flags = SA_SIGINFO | SA_RESTART; 142 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler; 143 __sys_sigaction(SIGCANCEL, &act, NULL); 144 } 145 146 void 147 _thr_signal_deinit(void) 148 { 149 } 150 151 __weak_reference(___pause, pause); 152 153 int 154 ___pause(void) 155 { 156 struct pthread *curthread = _get_curthread(); 157 int oldcancel; 158 int ret; 159 160 oldcancel = _thr_cancel_enter(curthread); 161 ret = __pause(); 162 _thr_cancel_leave(curthread, oldcancel); 163 164 return ret; 165 } 166 167 __weak_reference(_raise, raise); 168 169 int 170 _raise(int sig) 171 { 172 int ret; 173 174 if (!_thr_isthreaded()) 175 ret = kill(getpid(), sig); 176 else 177 ret = _thr_send_sig(_get_curthread(), sig); 178 return (ret); 179 } 180 181 __weak_reference(_sigaction, sigaction); 182 183 int 184 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) 185 { 186 /* Check if the signal number is out of range: */ 187 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) { 188 /* Return an invalid argument: */ 189 errno = EINVAL; 190 return (-1); 191 } 192 193 return __sys_sigaction(sig, act, oact); 194 } 195 196 __weak_reference(_sigprocmask, sigprocmask); 197 198 int 199 _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 200 { 201 const sigset_t *p = set; 202 sigset_t newset; 203 204 if (how != SIG_UNBLOCK) { 205 if (set != NULL) { 206 newset = *set; 207 SIGDELSET(newset, SIGCANCEL); 208 p = &newset; 209 } 210 } 211 return (__sys_sigprocmask(how, p, oset)); 212 } 213 214 __weak_reference(_pthread_sigmask, pthread_sigmask); 215 216 int 217 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 218 { 219 if (_sigprocmask(how, set, oset)) 220 return (errno); 221 return (0); 222 } 223 224 __weak_reference(__sigsuspend, sigsuspend); 225 226 int 227 _sigsuspend(const sigset_t * set) 228 { 229 sigset_t newset; 230 const sigset_t *pset; 231 int ret; 232 233 if (SIGISMEMBER(*set, SIGCANCEL)) { 234 newset = *set; 235 SIGDELSET(newset, SIGCANCEL); 236 pset = &newset; 237 } else 238 pset = set; 239 240 ret = __sys_sigsuspend(pset); 241 242 return (ret); 243 } 244 245 int 246 __sigsuspend(const sigset_t * set) 247 { 248 struct pthread *curthread = _get_curthread(); 249 sigset_t newset; 250 const sigset_t *pset; 251 int oldcancel; 252 int ret; 253 254 if (SIGISMEMBER(*set, SIGCANCEL)) { 255 newset = *set; 256 SIGDELSET(newset, SIGCANCEL); 257 pset = &newset; 258 } else 259 pset = set; 260 261 oldcancel = _thr_cancel_enter(curthread); 262 ret = __sys_sigsuspend(pset); 263 _thr_cancel_leave(curthread, oldcancel); 264 265 return (ret); 266 } 267 268 __weak_reference(__sigwait, sigwait); 269 __weak_reference(__sigtimedwait, sigtimedwait); 270 __weak_reference(__sigwaitinfo, sigwaitinfo); 271 272 int 273 _sigtimedwait(const sigset_t *set, siginfo_t *info, 274 const struct timespec * timeout) 275 { 276 sigset_t newset; 277 const sigset_t *pset; 278 int ret; 279 280 if (SIGISMEMBER(*set, SIGCANCEL)) { 281 newset = *set; 282 SIGDELSET(newset, SIGCANCEL); 283 pset = &newset; 284 } else 285 pset = set; 286 ret = __sys_sigtimedwait(pset, info, timeout); 287 return (ret); 288 } 289 290 int 291 __sigtimedwait(const sigset_t *set, siginfo_t *info, 292 const struct timespec * timeout) 293 { 294 struct pthread *curthread = _get_curthread(); 295 sigset_t newset; 296 const sigset_t *pset; 297 int oldcancel; 298 int ret; 299 300 if (SIGISMEMBER(*set, SIGCANCEL)) { 301 newset = *set; 302 SIGDELSET(newset, SIGCANCEL); 303 pset = &newset; 304 } else 305 pset = set; 306 oldcancel = _thr_cancel_enter(curthread); 307 ret = __sys_sigtimedwait(pset, info, timeout); 308 _thr_cancel_leave(curthread, oldcancel); 309 return (ret); 310 } 311 312 int 313 _sigwaitinfo(const sigset_t *set, siginfo_t *info) 314 { 315 sigset_t newset; 316 const sigset_t *pset; 317 int ret; 318 319 if (SIGISMEMBER(*set, SIGCANCEL)) { 320 newset = *set; 321 SIGDELSET(newset, SIGCANCEL); 322 pset = &newset; 323 } else 324 pset = set; 325 326 ret = __sys_sigwaitinfo(pset, info); 327 return (ret); 328 } 329 330 int 331 __sigwaitinfo(const sigset_t *set, siginfo_t *info) 332 { 333 struct pthread *curthread = _get_curthread(); 334 sigset_t newset; 335 const sigset_t *pset; 336 int oldcancel; 337 int ret; 338 339 if (SIGISMEMBER(*set, SIGCANCEL)) { 340 newset = *set; 341 SIGDELSET(newset, SIGCANCEL); 342 pset = &newset; 343 } else 344 pset = set; 345 346 oldcancel = _thr_cancel_enter(curthread); 347 ret = __sys_sigwaitinfo(pset, info); 348 _thr_cancel_leave(curthread, oldcancel); 349 return (ret); 350 } 351 352 int 353 _sigwait(const sigset_t *set, int *sig) 354 { 355 sigset_t newset; 356 const sigset_t *pset; 357 int ret; 358 359 if (SIGISMEMBER(*set, SIGCANCEL)) { 360 newset = *set; 361 SIGDELSET(newset, SIGCANCEL); 362 pset = &newset; 363 } else 364 pset = set; 365 366 ret = __sys_sigwait(pset, sig); 367 return (ret); 368 } 369 370 int 371 __sigwait(const sigset_t *set, int *sig) 372 { 373 struct pthread *curthread = _get_curthread(); 374 sigset_t newset; 375 const sigset_t *pset; 376 int oldcancel; 377 int ret; 378 379 if (SIGISMEMBER(*set, SIGCANCEL)) { 380 newset = *set; 381 SIGDELSET(newset, SIGCANCEL); 382 pset = &newset; 383 } else 384 pset = set; 385 386 oldcancel = _thr_cancel_enter(curthread); 387 ret = __sys_sigwait(pset, sig); 388 _thr_cancel_leave(curthread, oldcancel); 389 return (ret); 390 } 391