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