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