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 _sigtimedwait(const sigset_t *set, siginfo_t *info, 56 const struct timespec * timeout); 57 int __sigwaitinfo(const sigset_t *set, siginfo_t *info); 58 int _sigwaitinfo(const sigset_t *set, siginfo_t *info); 59 int __sigwait(const sigset_t *set, int *sig); 60 int _sigwait(const sigset_t *set, int *sig); 61 int __sigsuspend(const sigset_t *sigmask); 62 63 64 static void 65 sigcancel_handler(int sig __unused, 66 siginfo_t *info __unused, ucontext_t *ucp __unused) 67 { 68 struct pthread *curthread = _get_curthread(); 69 70 if (curthread->cancel_defer && curthread->cancel_pending) 71 thr_wake(curthread->tid); 72 curthread->in_sigcancel_handler++; 73 _thr_ast(curthread); 74 curthread->in_sigcancel_handler--; 75 } 76 77 void 78 _thr_ast(struct pthread *curthread) 79 { 80 if (!THR_IN_CRITICAL(curthread)) { 81 _thr_testcancel(curthread); 82 if (__predict_false((curthread->flags & 83 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED)) 84 == THR_FLAGS_NEED_SUSPEND)) 85 _thr_suspend_check(curthread); 86 } 87 } 88 89 void 90 _thr_suspend_check(struct pthread *curthread) 91 { 92 long cycle; 93 int err; 94 95 if (curthread->force_exit) 96 return; 97 98 err = errno; 99 /* 100 * Blocks SIGCANCEL which other threads must send. 101 */ 102 _thr_signal_block(curthread); 103 104 /* 105 * Increase critical_count, here we don't use THR_LOCK/UNLOCK 106 * because we are leaf code, we don't want to recursively call 107 * ourself. 108 */ 109 curthread->critical_count++; 110 THR_UMUTEX_LOCK(curthread, &(curthread)->lock); 111 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | 112 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) { 113 curthread->cycle++; 114 cycle = curthread->cycle; 115 116 /* Wake the thread suspending us. */ 117 _thr_umtx_wake(&curthread->cycle, INT_MAX); 118 119 /* 120 * if we are from pthread_exit, we don't want to 121 * suspend, just go and die. 122 */ 123 if (curthread->state == PS_DEAD) 124 break; 125 curthread->flags |= THR_FLAGS_SUSPENDED; 126 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 127 _thr_umtx_wait(&curthread->cycle, cycle, NULL); 128 THR_UMUTEX_LOCK(curthread, &(curthread)->lock); 129 curthread->flags &= ~THR_FLAGS_SUSPENDED; 130 } 131 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); 132 curthread->critical_count--; 133 134 /* 135 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and 136 * a new signal frame will nest us, this seems a problem because 137 * stack will grow and overflow, but because kernel will automatically 138 * mask the SIGCANCEL when delivering the signal, so we at most only 139 * have one nesting signal frame, this should be fine. 140 */ 141 _thr_signal_unblock(curthread); 142 errno = err; 143 } 144 145 void 146 _thr_signal_init(void) 147 { 148 struct sigaction act; 149 150 /* Install cancel handler. */ 151 SIGEMPTYSET(act.sa_mask); 152 act.sa_flags = SA_SIGINFO | SA_RESTART; 153 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler; 154 __sys_sigaction(SIGCANCEL, &act, NULL); 155 } 156 157 void 158 _thr_signal_deinit(void) 159 { 160 } 161 162 __weak_reference(___pause, pause); 163 164 int 165 ___pause(void) 166 { 167 struct pthread *curthread = _get_curthread(); 168 int ret; 169 170 _thr_cancel_enter(curthread); 171 ret = __pause(); 172 _thr_cancel_leave(curthread); 173 174 return ret; 175 } 176 177 __weak_reference(_raise, raise); 178 179 int 180 _raise(int sig) 181 { 182 int ret; 183 184 if (!_thr_isthreaded()) 185 ret = kill(getpid(), sig); 186 else 187 ret = _thr_send_sig(_get_curthread(), sig); 188 return (ret); 189 } 190 191 __weak_reference(_sigaction, sigaction); 192 193 int 194 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) 195 { 196 /* Check if the signal number is out of range: */ 197 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) { 198 /* Return an invalid argument: */ 199 errno = EINVAL; 200 return (-1); 201 } 202 203 return __sys_sigaction(sig, act, oact); 204 } 205 206 __weak_reference(_sigprocmask, sigprocmask); 207 208 int 209 _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 210 { 211 const sigset_t *p = set; 212 sigset_t newset; 213 214 if (how != SIG_UNBLOCK) { 215 if (set != NULL) { 216 newset = *set; 217 SIGDELSET(newset, SIGCANCEL); 218 p = &newset; 219 } 220 } 221 return (__sys_sigprocmask(how, p, oset)); 222 } 223 224 __weak_reference(_pthread_sigmask, pthread_sigmask); 225 226 int 227 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 228 { 229 if (_sigprocmask(how, set, oset)) 230 return (errno); 231 return (0); 232 } 233 234 __weak_reference(__sigsuspend, sigsuspend); 235 236 int 237 _sigsuspend(const sigset_t * set) 238 { 239 sigset_t newset; 240 const sigset_t *pset; 241 int ret; 242 243 if (SIGISMEMBER(*set, SIGCANCEL)) { 244 newset = *set; 245 SIGDELSET(newset, SIGCANCEL); 246 pset = &newset; 247 } else 248 pset = set; 249 250 ret = __sys_sigsuspend(pset); 251 252 return (ret); 253 } 254 255 int 256 __sigsuspend(const sigset_t * set) 257 { 258 struct pthread *curthread = _get_curthread(); 259 sigset_t newset; 260 const sigset_t *pset; 261 int ret; 262 263 if (SIGISMEMBER(*set, SIGCANCEL)) { 264 newset = *set; 265 SIGDELSET(newset, SIGCANCEL); 266 pset = &newset; 267 } else 268 pset = set; 269 270 _thr_cancel_enter(curthread); 271 ret = __sys_sigsuspend(pset); 272 _thr_cancel_leave(curthread); 273 274 return (ret); 275 } 276 277 __weak_reference(__sigwait, sigwait); 278 __weak_reference(__sigtimedwait, sigtimedwait); 279 __weak_reference(__sigwaitinfo, sigwaitinfo); 280 281 int 282 _sigtimedwait(const sigset_t *set, siginfo_t *info, 283 const struct timespec * timeout) 284 { 285 sigset_t newset; 286 const sigset_t *pset; 287 int ret; 288 289 if (SIGISMEMBER(*set, SIGCANCEL)) { 290 newset = *set; 291 SIGDELSET(newset, SIGCANCEL); 292 pset = &newset; 293 } else 294 pset = set; 295 ret = __sys_sigtimedwait(pset, info, timeout); 296 return (ret); 297 } 298 299 int 300 __sigtimedwait(const sigset_t *set, siginfo_t *info, 301 const struct timespec * timeout) 302 { 303 struct pthread *curthread = _get_curthread(); 304 sigset_t newset; 305 const sigset_t *pset; 306 int ret; 307 308 if (SIGISMEMBER(*set, SIGCANCEL)) { 309 newset = *set; 310 SIGDELSET(newset, SIGCANCEL); 311 pset = &newset; 312 } else 313 pset = set; 314 _thr_cancel_enter(curthread); 315 ret = __sys_sigtimedwait(pset, info, timeout); 316 _thr_cancel_leave(curthread); 317 return (ret); 318 } 319 320 int 321 _sigwaitinfo(const sigset_t *set, siginfo_t *info) 322 { 323 sigset_t newset; 324 const sigset_t *pset; 325 int ret; 326 327 if (SIGISMEMBER(*set, SIGCANCEL)) { 328 newset = *set; 329 SIGDELSET(newset, SIGCANCEL); 330 pset = &newset; 331 } else 332 pset = set; 333 334 ret = __sys_sigwaitinfo(pset, info); 335 return (ret); 336 } 337 338 int 339 __sigwaitinfo(const sigset_t *set, siginfo_t *info) 340 { 341 struct pthread *curthread = _get_curthread(); 342 sigset_t newset; 343 const sigset_t *pset; 344 int ret; 345 346 if (SIGISMEMBER(*set, SIGCANCEL)) { 347 newset = *set; 348 SIGDELSET(newset, SIGCANCEL); 349 pset = &newset; 350 } else 351 pset = set; 352 353 _thr_cancel_enter(curthread); 354 ret = __sys_sigwaitinfo(pset, info); 355 _thr_cancel_leave(curthread); 356 return (ret); 357 } 358 359 int 360 _sigwait(const sigset_t *set, int *sig) 361 { 362 sigset_t newset; 363 const sigset_t *pset; 364 int ret; 365 366 if (SIGISMEMBER(*set, SIGCANCEL)) { 367 newset = *set; 368 SIGDELSET(newset, SIGCANCEL); 369 pset = &newset; 370 } else 371 pset = set; 372 373 ret = __sys_sigwait(pset, sig); 374 return (ret); 375 } 376 377 int 378 __sigwait(const sigset_t *set, int *sig) 379 { 380 struct pthread *curthread = _get_curthread(); 381 sigset_t newset; 382 const sigset_t *pset; 383 int ret; 384 385 if (SIGISMEMBER(*set, SIGCANCEL)) { 386 newset = *set; 387 SIGDELSET(newset, SIGCANCEL); 388 pset = &newset; 389 } else 390 pset = set; 391 392 _thr_cancel_enter(curthread); 393 ret = __sys_sigwait(pset, sig); 394 _thr_cancel_leave(curthread); 395 return (ret); 396 } 397