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