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