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