Print this page
5281 incorrect realtime signal delivery
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/threads/sigaction.c
+++ new/usr/src/lib/libc/port/threads/sigaction.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 + * Copyright 2014 Ryan Zezeski. All rights reserved.
23 24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27
27 28 #include "lint.h"
28 29 #include <sys/feature_tests.h>
29 30 /*
30 31 * setcontext() really can return, if UC_CPU is not specified.
31 32 * Make the compiler shut up about it.
32 33 */
33 34 #if defined(__NORETURN)
34 35 #undef __NORETURN
35 36 #endif
36 37 #define __NORETURN
37 38 #include "thr_uberdata.h"
38 39 #include "asyncio.h"
39 40 #include <signal.h>
40 41 #include <siginfo.h>
41 42 #include <sys/systm.h>
42 43
43 44 /* maskable signals */
44 45 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
45 46
46 47 /*
47 48 * Return true if the valid signal bits in both sets are the same.
48 49 */
49 50 int
50 51 sigequalset(const sigset_t *s1, const sigset_t *s2)
51 52 {
52 53 /*
53 54 * We only test valid signal bits, not rubbish following MAXSIG
54 55 * (for speed). Algorithm:
55 56 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
56 57 */
57 58 /* see lib/libc/inc/thr_uberdata.h for why this must be true */
58 59 #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32))
59 60 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
60 61 (s1->__sigbits[1] ^ s2->__sigbits[1]) |
61 62 ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2)));
62 63 #else
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
63 64 #error "fix me: MAXSIG out of bounds"
64 65 #endif
65 66 }
66 67
67 68 /*
68 69 * Common code for calling the user-specified signal handler.
69 70 */
70 71 void
71 72 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
72 73 {
74 + int i;
73 75 ulwp_t *self = curthread;
74 76 uberdata_t *udp = self->ul_uberdata;
75 77 struct sigaction uact;
76 78 volatile struct sigaction *sap;
77 79
78 80 /*
79 81 * If we are taking a signal while parked or about to be parked
80 82 * on __lwp_park() then remove ourself from the sleep queue so
81 83 * that we can grab locks. The code in mutex_lock_queue() and
82 84 * cond_wait_common() will detect this and deal with it when
83 85 * __lwp_park() returns.
84 86 */
85 87 unsleep_self();
86 88 set_parking_flag(self, 0);
87 89
88 90 if (__td_event_report(self, TD_CATCHSIG, udp)) {
89 91 self->ul_td_evbuf.eventnum = TD_CATCHSIG;
90 92 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
91 93 tdb_event(TD_CATCHSIG, udp);
92 94 }
93 95
94 96 /*
95 97 * Get a self-consistent set of flags, handler, and mask
96 98 * while holding the sig's sig_lock for the least possible time.
97 99 * We must acquire the sig's sig_lock because some thread running
98 100 * in sigaction() might be establishing a new signal handler.
99 101 * The code in sigaction() acquires the writer lock; here
100 102 * we acquire the readers lock to ehance concurrency in the
101 103 * face of heavy signal traffic, such as generated by java.
102 104 *
103 105 * Locking exceptions:
104 106 * No locking for a child of vfork().
105 107 * If the signal is SIGPROF with an si_code of PROF_SIG,
106 108 * then we assume that this signal was generated by
107 109 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
108 110 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
109 111 * then we assume that the signal was generated by
110 112 * a hardware performance counter overflow.
111 113 * In these cases, assume that we need no locking. It is the
112 114 * monitoring program's responsibility to ensure correctness.
113 115 */
114 116 sap = &udp->siguaction[sig].sig_uaction;
115 117 if (self->ul_vfork ||
116 118 (sip != NULL &&
117 119 ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
118 120 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
119 121 /* we wish this assignment could be atomic */
120 122 (void) memcpy(&uact, (void *)sap, sizeof (uact));
121 123 } else {
122 124 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
123 125 lrw_rdlock(rwlp);
124 126 (void) memcpy(&uact, (void *)sap, sizeof (uact));
125 127 if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
126 128 (sap->sa_flags & SA_RESETHAND))
127 129 sap->sa_sigaction = SIG_DFL;
128 130 lrw_unlock(rwlp);
129 131 }
130 132
131 133 /*
132 134 * Set the proper signal mask and call the user's signal handler.
133 135 * (We overrode the user-requested signal mask with maskset
134 136 * so we currently have all blockable signals blocked.)
135 137 *
136 138 * We would like to ASSERT() that the signal is not a member of the
137 139 * signal mask at the previous level (ucp->uc_sigmask) or the specified
138 140 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
139 141 * /proc can override this via PCSSIG, so we don't bother.
140 142 *
141 143 * We would also like to ASSERT() that the signal mask at the previous
142 144 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
143 145 * but /proc can change the thread's signal mask via PCSHOLD, so we
144 146 * don't bother with that either.
145 147 */
146 148 ASSERT(ucp->uc_flags & UC_SIGMASK);
147 149 if (self->ul_sigsuspend) {
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
148 150 ucp->uc_sigmask = self->ul_sigmask;
149 151 self->ul_sigsuspend = 0;
150 152 /* the sigsuspend() or pollsys() signal mask */
151 153 sigorset(&uact.sa_mask, &self->ul_tmpmask);
152 154 } else {
153 155 /* the signal mask at the previous level */
154 156 sigorset(&uact.sa_mask, &ucp->uc_sigmask);
155 157 }
156 158 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */
157 159 (void) sigaddset(&uact.sa_mask, sig);
160 +
161 + /*
162 + * Enforce the proper order for realtime signals. Lower signals
163 + * have higher priority and multiple instances of the same signal
164 + * must arrive in FIFO order (NODEFER does not apply).
165 + *
166 + * See section 2.4.2 of POSIX.
167 + */
168 + if ((sig >= SIGRTMIN) && (sig <= SIGRTMAX)) {
169 + for (i = sig; i <= SIGRTMAX; i++) {
170 + (void) sigaddset(&uact.sa_mask, i);
171 + }
172 + }
173 +
158 174 self->ul_sigmask = uact.sa_mask;
159 175 self->ul_siglink = ucp;
160 176 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask);
161 177
162 178 /*
163 179 * If this thread has been sent SIGCANCEL from the kernel
164 180 * or from pthread_cancel(), it is being asked to exit.
165 181 * The kernel may send SIGCANCEL without a siginfo struct.
166 182 * If the SIGCANCEL is process-directed (from kill() or
167 183 * sigqueue()), treat it as an ordinary signal.
168 184 */
169 185 if (sig == SIGCANCEL) {
170 186 if (sip == NULL || SI_FROMKERNEL(sip) ||
171 187 sip->si_code == SI_LWP) {
172 188 do_sigcancel();
173 189 goto out;
174 190 }
175 191 /* SIGCANCEL is ignored by default */
176 192 if (uact.sa_sigaction == SIG_DFL ||
177 193 uact.sa_sigaction == SIG_IGN)
178 194 goto out;
179 195 }
180 196
181 197 /*
182 198 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
183 199 * we are an aio worker thread, cancel the aio request.
184 200 */
185 201 if (sig == SIGAIOCANCEL) {
186 202 aio_worker_t *aiowp = pthread_getspecific(_aio_key);
187 203
188 204 if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
189 205 siglongjmp(aiowp->work_jmp_buf, 1);
190 206 /* SIGLWP is ignored by default */
191 207 if (uact.sa_sigaction == SIG_DFL ||
192 208 uact.sa_sigaction == SIG_IGN)
193 209 goto out;
194 210 }
195 211
196 212 if (!(uact.sa_flags & SA_SIGINFO))
197 213 sip = NULL;
198 214 __sighndlr(sig, sip, ucp, uact.sa_sigaction);
199 215
200 216 #if defined(sparc) || defined(__sparc)
201 217 /*
202 218 * If this is a floating point exception and the queue
203 219 * is non-empty, pop the top entry from the queue. This
204 220 * is to maintain expected behavior.
205 221 */
206 222 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
207 223 fpregset_t *fp = &ucp->uc_mcontext.fpregs;
208 224
209 225 if (--fp->fpu_qcnt > 0) {
210 226 unsigned char i;
211 227 struct fq *fqp;
212 228
213 229 fqp = fp->fpu_q;
214 230 for (i = 0; i < fp->fpu_qcnt; i++)
215 231 fqp[i] = fqp[i+1];
216 232 }
217 233 }
218 234 #endif /* sparc */
219 235
220 236 out:
221 237 (void) setcontext(ucp);
222 238 thr_panic("call_user_handler(): setcontext() returned");
223 239 }
224 240
225 241 /*
226 242 * take_deferred_signal() is called when ul_critical and ul_sigdefer become
227 243 * zero and a deferred signal has been recorded on the current thread.
228 244 * We are out of the critical region and are ready to take a signal.
229 245 * The kernel has all signals blocked on this lwp, but our value of
230 246 * ul_sigmask is the correct signal mask for the previous context.
231 247 *
232 248 * We call __sigresend() to atomically restore the signal mask and
233 249 * cause the signal to be sent again with the remembered siginfo.
234 250 * We will not return successfully from __sigresend() until the
235 251 * application's signal handler has been run via sigacthandler().
236 252 */
237 253 void
238 254 take_deferred_signal(int sig)
239 255 {
240 256 extern int __sigresend(int, siginfo_t *, sigset_t *);
241 257 ulwp_t *self = curthread;
242 258 siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
243 259 siginfo_t *sip;
244 260 int error;
245 261
246 262 ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
247 263
248 264 /*
249 265 * If the signal handler was established with SA_RESETHAND,
250 266 * the kernel has reset the handler to SIG_DFL, so we have
251 267 * to reestablish the handler now so that it will be entered
252 268 * again when we call __sigresend(), below.
253 269 *
254 270 * Logically, we should acquire and release the signal's
255 271 * sig_lock around this operation to protect the integrity
256 272 * of the signal action while we copy it, as is done below
257 273 * in _libc_sigaction(). However, we may be on a user-level
258 274 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
259 275 * might attempt to sleep on a different sleep queue and
260 276 * that would corrupt the entire sleep queue mechanism.
261 277 *
262 278 * If we are on a sleep queue we will remove ourself from
263 279 * it in call_user_handler(), called from sigacthandler(),
264 280 * before entering the application's signal handler.
265 281 * In the meantime, we must not acquire any locks.
266 282 */
267 283 if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
268 284 struct sigaction tact = suap->sig_uaction;
269 285 tact.sa_flags &= ~SA_NODEFER;
270 286 tact.sa_sigaction = self->ul_uberdata->sigacthandler;
271 287 tact.sa_mask = maskset;
272 288 (void) __sigaction(sig, &tact, NULL);
273 289 }
274 290
275 291 if (self->ul_siginfo.si_signo == 0)
276 292 sip = NULL;
277 293 else
278 294 sip = &self->ul_siginfo;
279 295
280 296 /* EAGAIN can happen only for a pending SIGSTOP signal */
281 297 while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
282 298 continue;
283 299 if (error)
284 300 thr_panic("take_deferred_signal(): __sigresend() failed");
285 301 }
286 302
287 303 void
288 304 sigacthandler(int sig, siginfo_t *sip, void *uvp)
289 305 {
290 306 ucontext_t *ucp = uvp;
291 307 ulwp_t *self = curthread;
292 308
293 309 /*
294 310 * Do this in case we took a signal while in a cancelable system call.
295 311 * It does no harm if we were not in such a system call.
296 312 */
297 313 self->ul_sp = 0;
298 314 if (sig != SIGCANCEL)
299 315 self->ul_cancel_async = self->ul_save_async;
300 316
301 317 /*
302 318 * If this thread has performed a longjmp() from a signal handler
303 319 * back to main level some time in the past, it has left the kernel
304 320 * thinking that it is still in the signal context. We repair this
305 321 * possible damage by setting ucp->uc_link to NULL if we know that
306 322 * we are actually executing at main level (self->ul_siglink == NULL).
307 323 * See the code for setjmp()/longjmp() for more details.
308 324 */
309 325 if (self->ul_siglink == NULL)
310 326 ucp->uc_link = NULL;
311 327
312 328 /*
313 329 * If we are not in a critical region and are
314 330 * not deferring signals, take the signal now.
315 331 */
316 332 if ((self->ul_critical + self->ul_sigdefer) == 0) {
317 333 call_user_handler(sig, sip, ucp);
318 334 /*
319 335 * On the surface, the following call seems redundant
320 336 * because call_user_handler() cannot return. However,
321 337 * we don't want to return from here because the compiler
322 338 * might recycle our frame. We want to keep it on the
323 339 * stack to assist debuggers such as pstack in identifying
324 340 * signal frames. The call to thr_panic() serves to prevent
325 341 * tail-call optimisation here.
326 342 */
327 343 thr_panic("sigacthandler(): call_user_handler() returned");
328 344 }
329 345
330 346 /*
331 347 * We are in a critical region or we are deferring signals. When
332 348 * we emerge from the region we will call take_deferred_signal().
333 349 */
334 350 ASSERT(self->ul_cursig == 0);
335 351 self->ul_cursig = (char)sig;
336 352 if (sip != NULL)
337 353 (void) memcpy(&self->ul_siginfo,
338 354 sip, sizeof (siginfo_t));
339 355 else
340 356 self->ul_siginfo.si_signo = 0;
341 357
342 358 /*
343 359 * Make sure that if we return to a call to __lwp_park()
344 360 * or ___lwp_cond_wait() that it returns right away
345 361 * (giving us a spurious wakeup but not a deadlock).
346 362 */
347 363 set_parking_flag(self, 0);
348 364
349 365 /*
350 366 * Return to the previous context with all signals blocked.
351 367 * We will restore the signal mask in take_deferred_signal().
352 368 * Note that we are calling the system call trap here, not
353 369 * the setcontext() wrapper. We don't want to change the
354 370 * thread's ul_sigmask by this operation.
355 371 */
356 372 ucp->uc_sigmask = maskset;
357 373 (void) __setcontext(ucp);
358 374 thr_panic("sigacthandler(): __setcontext() returned");
359 375 }
360 376
361 377 #pragma weak _sigaction = sigaction
362 378 int
363 379 sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
364 380 {
365 381 ulwp_t *self = curthread;
366 382 uberdata_t *udp = self->ul_uberdata;
367 383 struct sigaction oaction;
368 384 struct sigaction tact;
369 385 struct sigaction *tactp = NULL;
370 386 int rv;
371 387
372 388 if (sig <= 0 || sig >= NSIG) {
373 389 errno = EINVAL;
374 390 return (-1);
375 391 }
376 392
377 393 if (!self->ul_vfork)
378 394 lrw_wrlock(&udp->siguaction[sig].sig_lock);
379 395
380 396 oaction = udp->siguaction[sig].sig_uaction;
381 397
382 398 if (nact != NULL) {
383 399 tact = *nact; /* make a copy so we can modify it */
384 400 tactp = &tact;
385 401 delete_reserved_signals(&tact.sa_mask);
386 402
387 403 #if !defined(_LP64)
388 404 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */
389 405 #endif
390 406 /*
391 407 * To be compatible with the behavior of SunOS 4.x:
392 408 * If the new signal handler is SIG_IGN or SIG_DFL, do
393 409 * not change the signal's entry in the siguaction array.
394 410 * This allows a child of vfork(2) to set signal handlers
395 411 * to SIG_IGN or SIG_DFL without affecting the parent.
396 412 *
397 413 * This also covers a race condition with some thread
398 414 * setting the signal action to SIG_DFL or SIG_IGN
399 415 * when the thread has also received and deferred
400 416 * that signal. When the thread takes the deferred
401 417 * signal, even though it has set the action to SIG_DFL
402 418 * or SIG_IGN, it will execute the old signal handler
403 419 * anyway. This is an inherent signaling race condition
404 420 * and is not a bug.
405 421 *
406 422 * A child of vfork() is not allowed to change signal
407 423 * handlers to anything other than SIG_DFL or SIG_IGN.
408 424 */
409 425 if (self->ul_vfork) {
410 426 if (tact.sa_sigaction != SIG_IGN)
411 427 tact.sa_sigaction = SIG_DFL;
412 428 } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
413 429 /*
414 430 * Always catch these signals.
415 431 * We need SIGCANCEL for pthread_cancel() to work.
416 432 * We need SIGAIOCANCEL for aio_cancel() to work.
417 433 */
418 434 udp->siguaction[sig].sig_uaction = tact;
419 435 if (tact.sa_sigaction == SIG_DFL ||
420 436 tact.sa_sigaction == SIG_IGN)
421 437 tact.sa_flags = SA_SIGINFO;
422 438 else {
423 439 tact.sa_flags |= SA_SIGINFO;
424 440 tact.sa_flags &=
425 441 ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
426 442 }
427 443 tact.sa_sigaction = udp->sigacthandler;
428 444 tact.sa_mask = maskset;
429 445 } else if (tact.sa_sigaction != SIG_DFL &&
430 446 tact.sa_sigaction != SIG_IGN) {
431 447 udp->siguaction[sig].sig_uaction = tact;
432 448 tact.sa_flags &= ~SA_NODEFER;
433 449 tact.sa_sigaction = udp->sigacthandler;
434 450 tact.sa_mask = maskset;
435 451 }
436 452 }
437 453
438 454 if ((rv = __sigaction(sig, tactp, oact)) != 0)
439 455 udp->siguaction[sig].sig_uaction = oaction;
440 456 else if (oact != NULL &&
441 457 oact->sa_sigaction != SIG_DFL &&
442 458 oact->sa_sigaction != SIG_IGN)
443 459 *oact = oaction;
444 460
445 461 /*
446 462 * We detect setting the disposition of SIGIO just to set the
447 463 * _sigio_enabled flag for the asynchronous i/o (aio) code.
448 464 */
449 465 if (sig == SIGIO && rv == 0 && tactp != NULL) {
450 466 _sigio_enabled =
451 467 (tactp->sa_handler != SIG_DFL &&
452 468 tactp->sa_handler != SIG_IGN);
453 469 }
454 470
455 471 if (!self->ul_vfork)
456 472 lrw_unlock(&udp->siguaction[sig].sig_lock);
457 473 return (rv);
458 474 }
459 475
460 476 /*
461 477 * This is a private interface for the linux brand interface.
462 478 */
463 479 void
464 480 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
465 481 void (**osigacthandler)(int, siginfo_t *, void *))
466 482 {
467 483 ulwp_t *self = curthread;
468 484 uberdata_t *udp = self->ul_uberdata;
469 485
470 486 if (osigacthandler != NULL)
471 487 *osigacthandler = udp->sigacthandler;
472 488
473 489 udp->sigacthandler = nsigacthandler;
474 490 }
475 491
476 492 /*
477 493 * Tell the kernel to block all signals.
478 494 * Use the schedctl interface, or failing that, use __lwp_sigmask().
479 495 * This action can be rescinded only by making a system call that
480 496 * sets the signal mask:
481 497 * __lwp_sigmask(), __sigprocmask(), __setcontext(),
482 498 * __sigsuspend() or __pollsys().
483 499 * In particular, this action cannot be reversed by assigning
484 500 * scp->sc_sigblock = 0. That would be a way to lose signals.
485 501 * See the definition of restore_signals(self).
486 502 */
487 503 void
488 504 block_all_signals(ulwp_t *self)
489 505 {
490 506 volatile sc_shared_t *scp;
491 507
492 508 enter_critical(self);
493 509 if ((scp = self->ul_schedctl) != NULL ||
494 510 (scp = setup_schedctl()) != NULL)
495 511 scp->sc_sigblock = 1;
496 512 else
497 513 (void) __lwp_sigmask(SIG_SETMASK, &maskset);
498 514 exit_critical(self);
499 515 }
500 516
501 517 /*
502 518 * setcontext() has code that forcibly restores the curthread
503 519 * pointer in a context passed to the setcontext(2) syscall.
504 520 *
505 521 * Certain processes may need to disable this feature, so these routines
506 522 * provide the mechanism to do so.
507 523 *
508 524 * (As an example, branded 32-bit x86 processes may use %gs for their own
509 525 * purposes, so they need to be able to specify a %gs value to be restored
510 526 * on return from a signal handler via the passed ucontext_t.)
511 527 */
512 528 static int setcontext_enforcement = 1;
513 529
514 530 void
515 531 set_setcontext_enforcement(int on)
516 532 {
517 533 setcontext_enforcement = on;
518 534 }
519 535
520 536 #pragma weak _setcontext = setcontext
521 537 int
522 538 setcontext(const ucontext_t *ucp)
523 539 {
524 540 ulwp_t *self = curthread;
525 541 int ret;
526 542 ucontext_t uc;
527 543
528 544 /*
529 545 * Returning from the main context (uc_link == NULL) causes
530 546 * the thread to exit. See setcontext(2) and makecontext(3C).
531 547 */
532 548 if (ucp == NULL)
533 549 thr_exit(NULL);
534 550 (void) memcpy(&uc, ucp, sizeof (uc));
535 551
536 552 /*
537 553 * Restore previous signal mask and context link.
538 554 */
539 555 if (uc.uc_flags & UC_SIGMASK) {
540 556 block_all_signals(self);
541 557 delete_reserved_signals(&uc.uc_sigmask);
542 558 self->ul_sigmask = uc.uc_sigmask;
543 559 if (self->ul_cursig) {
544 560 /*
545 561 * We have a deferred signal present.
546 562 * The signal mask will be set when the
547 563 * signal is taken in take_deferred_signal().
548 564 */
549 565 ASSERT(self->ul_critical + self->ul_sigdefer != 0);
550 566 uc.uc_flags &= ~UC_SIGMASK;
551 567 }
552 568 }
553 569 self->ul_siglink = uc.uc_link;
554 570
555 571 /*
556 572 * We don't know where this context structure has been.
557 573 * Preserve the curthread pointer, at least.
558 574 *
559 575 * Allow this feature to be disabled if a particular process
560 576 * requests it.
561 577 */
562 578 if (setcontext_enforcement) {
563 579 #if defined(__sparc)
564 580 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
565 581 #elif defined(__amd64)
566 582 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
567 583 #elif defined(__i386)
568 584 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
569 585 #else
570 586 #error "none of __sparc, __amd64, __i386 defined"
571 587 #endif
572 588 }
573 589
574 590 /*
575 591 * Make sure that if we return to a call to __lwp_park()
576 592 * or ___lwp_cond_wait() that it returns right away
577 593 * (giving us a spurious wakeup but not a deadlock).
578 594 */
579 595 set_parking_flag(self, 0);
580 596 self->ul_sp = 0;
581 597 ret = __setcontext(&uc);
582 598
583 599 /*
584 600 * It is OK for setcontext() to return if the user has not specified
585 601 * UC_CPU.
586 602 */
587 603 if (uc.uc_flags & UC_CPU)
588 604 thr_panic("setcontext(): __setcontext() returned");
589 605 return (ret);
590 606 }
591 607
592 608 #pragma weak _thr_sigsetmask = thr_sigsetmask
593 609 int
594 610 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
595 611 {
596 612 ulwp_t *self = curthread;
597 613 sigset_t saveset;
598 614
599 615 if (set == NULL) {
600 616 enter_critical(self);
601 617 if (oset != NULL)
602 618 *oset = self->ul_sigmask;
603 619 exit_critical(self);
604 620 } else {
605 621 switch (how) {
606 622 case SIG_BLOCK:
607 623 case SIG_UNBLOCK:
608 624 case SIG_SETMASK:
609 625 break;
610 626 default:
611 627 return (EINVAL);
612 628 }
613 629
614 630 /*
615 631 * The assignments to self->ul_sigmask must be protected from
616 632 * signals. The nuances of this code are subtle. Be careful.
617 633 */
618 634 block_all_signals(self);
619 635 if (oset != NULL)
620 636 saveset = self->ul_sigmask;
621 637 switch (how) {
622 638 case SIG_BLOCK:
623 639 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
624 640 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
625 641 self->ul_sigmask.__sigbits[2] |= set->__sigbits[2];
626 642 self->ul_sigmask.__sigbits[3] |= set->__sigbits[3];
627 643 break;
628 644 case SIG_UNBLOCK:
629 645 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
630 646 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
631 647 self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2];
632 648 self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3];
633 649 break;
634 650 case SIG_SETMASK:
635 651 self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
636 652 self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
637 653 self->ul_sigmask.__sigbits[2] = set->__sigbits[2];
638 654 self->ul_sigmask.__sigbits[3] = set->__sigbits[3];
639 655 break;
640 656 }
641 657 delete_reserved_signals(&self->ul_sigmask);
642 658 if (oset != NULL)
643 659 *oset = saveset;
644 660 restore_signals(self);
645 661 }
646 662
647 663 return (0);
648 664 }
649 665
650 666 #pragma weak _pthread_sigmask = pthread_sigmask
651 667 int
652 668 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
653 669 {
654 670 return (thr_sigsetmask(how, set, oset));
655 671 }
656 672
657 673 #pragma weak _sigprocmask = sigprocmask
658 674 int
659 675 sigprocmask(int how, const sigset_t *set, sigset_t *oset)
660 676 {
661 677 int error;
662 678
663 679 /*
664 680 * Guard against children of vfork().
665 681 */
666 682 if (curthread->ul_vfork)
667 683 return (__sigprocmask(how, set, oset));
668 684
669 685 if ((error = thr_sigsetmask(how, set, oset)) != 0) {
670 686 errno = error;
671 687 return (-1);
672 688 }
673 689
674 690 return (0);
675 691 }
676 692
677 693 /*
678 694 * Called at library initialization to set up signal handling.
679 695 * All we really do is initialize the sig_lock rwlocks.
680 696 * All signal handlers are either SIG_DFL or SIG_IGN on exec().
681 697 * However, if any signal handlers were established on alternate
682 698 * link maps before the primary link map has been initialized,
683 699 * then inform the kernel of the new sigacthandler.
684 700 */
685 701 void
686 702 signal_init()
687 703 {
688 704 uberdata_t *udp = curthread->ul_uberdata;
689 705 struct sigaction *sap;
690 706 struct sigaction act;
691 707 rwlock_t *rwlp;
692 708 int sig;
693 709
694 710 for (sig = 0; sig < NSIG; sig++) {
695 711 rwlp = &udp->siguaction[sig].sig_lock;
696 712 rwlp->rwlock_magic = RWL_MAGIC;
697 713 rwlp->mutex.mutex_flag = LOCK_INITED;
698 714 rwlp->mutex.mutex_magic = MUTEX_MAGIC;
699 715 sap = &udp->siguaction[sig].sig_uaction;
700 716 if (sap->sa_sigaction != SIG_DFL &&
701 717 sap->sa_sigaction != SIG_IGN &&
702 718 __sigaction(sig, NULL, &act) == 0 &&
703 719 act.sa_sigaction != SIG_DFL &&
704 720 act.sa_sigaction != SIG_IGN) {
705 721 act = *sap;
706 722 act.sa_flags &= ~SA_NODEFER;
707 723 act.sa_sigaction = udp->sigacthandler;
708 724 act.sa_mask = maskset;
709 725 (void) __sigaction(sig, &act, NULL);
710 726 }
711 727 }
712 728 }
713 729
714 730 /*
715 731 * Common code for cancelling self in _sigcancel() and pthread_cancel().
716 732 * First record the fact that a cancellation is pending.
717 733 * Then, if cancellation is disabled or if we are holding unprotected
718 734 * libc locks, just return to defer the cancellation.
719 735 * Then, if we are at a cancellation point (ul_cancelable) just
720 736 * return and let _canceloff() do the exit.
721 737 * Else exit immediately if async mode is in effect.
722 738 */
723 739 void
724 740 do_sigcancel(void)
725 741 {
726 742 ulwp_t *self = curthread;
727 743
728 744 ASSERT(self->ul_critical == 0);
729 745 ASSERT(self->ul_sigdefer == 0);
730 746 self->ul_cancel_pending = 1;
731 747 if (self->ul_cancel_async &&
732 748 !self->ul_cancel_disabled &&
733 749 self->ul_libc_locks == 0 &&
734 750 !self->ul_cancelable)
735 751 pthread_exit(PTHREAD_CANCELED);
736 752 set_cancel_pending_flag(self, 0);
737 753 }
738 754
739 755 /*
740 756 * Set up the SIGCANCEL handler for threads cancellation,
741 757 * needed only when we have more than one thread,
742 758 * or the SIGAIOCANCEL handler for aio cancellation,
743 759 * called when aio is initialized, in __uaio_init().
744 760 */
745 761 void
746 762 setup_cancelsig(int sig)
747 763 {
748 764 uberdata_t *udp = curthread->ul_uberdata;
749 765 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
750 766 struct sigaction act;
751 767
752 768 ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
753 769 lrw_rdlock(rwlp);
754 770 act = udp->siguaction[sig].sig_uaction;
755 771 lrw_unlock(rwlp);
756 772 if (act.sa_sigaction == SIG_DFL ||
757 773 act.sa_sigaction == SIG_IGN)
758 774 act.sa_flags = SA_SIGINFO;
759 775 else {
760 776 act.sa_flags |= SA_SIGINFO;
761 777 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
762 778 }
763 779 act.sa_sigaction = udp->sigacthandler;
764 780 act.sa_mask = maskset;
765 781 (void) __sigaction(sig, &act, NULL);
766 782 }
↓ open down ↓ |
599 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX