xref: /titanic_52/usr/src/lib/libc/port/threads/sigaction.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 (nact != NULL && !primary_link_map) {
350 		errno = ENOTSUP;
351 		return (-1);
352 	}
353 
354 	if (sig <= 0 || sig >= NSIG) {
355 		errno = EINVAL;
356 		return (-1);
357 	}
358 
359 	if (!self->ul_vfork)
360 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
361 
362 	oaction = udp->siguaction[sig].sig_uaction;
363 
364 	if (nact != NULL) {
365 		tact = *nact;	/* make a copy so we can modify it */
366 		tactp = &tact;
367 		delete_reserved_signals(&tact.sa_mask);
368 
369 #if !defined(_LP64)
370 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
371 #endif
372 		/*
373 		 * To be compatible with the behavior of SunOS 4.x:
374 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
375 		 * not change the signal's entry in the siguaction array.
376 		 * This allows a child of vfork(2) to set signal handlers
377 		 * to SIG_IGN or SIG_DFL without affecting the parent.
378 		 *
379 		 * This also covers a race condition with some thread
380 		 * setting the signal action to SIG_DFL or SIG_IGN
381 		 * when the thread has also received and deferred
382 		 * that signal.  When the thread takes the deferred
383 		 * signal, even though it has set the action to SIG_DFL
384 		 * or SIG_IGN, it will execute the old signal handler
385 		 * anyway.  This is an inherent signaling race condition
386 		 * and is not a bug.
387 		 *
388 		 * A child of vfork() is not allowed to change signal
389 		 * handlers to anything other than SIG_DFL or SIG_IGN.
390 		 */
391 		if (self->ul_vfork) {
392 			if (tact.sa_sigaction != SIG_IGN)
393 				tact.sa_sigaction = SIG_DFL;
394 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
395 			/*
396 			 * Always catch these signals.
397 			 * We need SIGCANCEL for pthread_cancel() to work.
398 			 * We need SIGAIOCANCEL for aio_cancel() to work.
399 			 */
400 			udp->siguaction[sig].sig_uaction = tact;
401 			if (tact.sa_sigaction == SIG_DFL ||
402 			    tact.sa_sigaction == SIG_IGN)
403 				tact.sa_flags = SA_SIGINFO;
404 			else {
405 				tact.sa_flags |= SA_SIGINFO;
406 				tact.sa_flags &=
407 				    ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
408 			}
409 			tact.sa_sigaction = udp->sigacthandler;
410 			tact.sa_mask = maskset;
411 		} else if (tact.sa_sigaction != SIG_DFL &&
412 		    tact.sa_sigaction != SIG_IGN) {
413 			udp->siguaction[sig].sig_uaction = tact;
414 			tact.sa_flags &= ~SA_NODEFER;
415 			tact.sa_sigaction = udp->sigacthandler;
416 			tact.sa_mask = maskset;
417 		}
418 	}
419 
420 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
421 		udp->siguaction[sig].sig_uaction = oaction;
422 	else if (oact != NULL &&
423 	    oact->sa_sigaction != SIG_DFL &&
424 	    oact->sa_sigaction != SIG_IGN)
425 		*oact = oaction;
426 
427 	/*
428 	 * We detect setting the disposition of SIGIO just to set the
429 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
430 	 */
431 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
432 		_sigio_enabled =
433 		    (tactp->sa_handler != SIG_DFL &&
434 		    tactp->sa_handler != SIG_IGN);
435 	}
436 
437 	if (!self->ul_vfork)
438 		lrw_unlock(&udp->siguaction[sig].sig_lock);
439 	return (rv);
440 }
441 
442 /*
443  * This is a private interface for the linux brand interface.
444  */
445 void
446 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
447     void (**osigacthandler)(int, siginfo_t *, void *))
448 {
449 	ulwp_t *self = curthread;
450 	uberdata_t *udp = self->ul_uberdata;
451 
452 	if (osigacthandler != NULL)
453 		*osigacthandler = udp->sigacthandler;
454 
455 	udp->sigacthandler = nsigacthandler;
456 }
457 
458 /*
459  * Tell the kernel to block all signals.
460  * Use the schedctl interface, or failing that, use __lwp_sigmask().
461  * This action can be rescinded only by making a system call that
462  * sets the signal mask:
463  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
464  *	__sigsuspend() or __pollsys().
465  * In particular, this action cannot be reversed by assigning
466  * scp->sc_sigblock = 0.  That would be a way to lose signals.
467  * See the definition of restore_signals(self).
468  */
469 void
470 block_all_signals(ulwp_t *self)
471 {
472 	volatile sc_shared_t *scp;
473 
474 	enter_critical(self);
475 	if ((scp = self->ul_schedctl) != NULL ||
476 	    (scp = setup_schedctl()) != NULL)
477 		scp->sc_sigblock = 1;
478 	else
479 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
480 	exit_critical(self);
481 }
482 
483 /*
484  * setcontext() has code that forcibly restores the curthread
485  * pointer in a context passed to the setcontext(2) syscall.
486  *
487  * Certain processes may need to disable this feature, so these routines
488  * provide the mechanism to do so.
489  *
490  * (As an example, branded 32-bit x86 processes may use %gs for their own
491  * purposes, so they need to be able to specify a %gs value to be restored
492  * on return from a signal handler via the passed ucontext_t.)
493  */
494 static int setcontext_enforcement = 1;
495 
496 void
497 set_setcontext_enforcement(int on)
498 {
499 	setcontext_enforcement = on;
500 }
501 
502 #pragma weak setcontext = _setcontext
503 int
504 _setcontext(const ucontext_t *ucp)
505 {
506 	ulwp_t *self = curthread;
507 	int ret;
508 	ucontext_t uc;
509 
510 	/*
511 	 * Returning from the main context (uc_link == NULL) causes
512 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
513 	 */
514 	if (ucp == NULL)
515 		_thr_exit(NULL);
516 	(void) memcpy(&uc, ucp, sizeof (uc));
517 
518 	/*
519 	 * Restore previous signal mask and context link.
520 	 */
521 	if (uc.uc_flags & UC_SIGMASK) {
522 		block_all_signals(self);
523 		delete_reserved_signals(&uc.uc_sigmask);
524 		self->ul_sigmask = uc.uc_sigmask;
525 		if (self->ul_cursig) {
526 			/*
527 			 * We have a deferred signal present.
528 			 * The signal mask will be set when the
529 			 * signal is taken in take_deferred_signal().
530 			 */
531 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
532 			uc.uc_flags &= ~UC_SIGMASK;
533 		}
534 	}
535 	self->ul_siglink = uc.uc_link;
536 
537 	/*
538 	 * We don't know where this context structure has been.
539 	 * Preserve the curthread pointer, at least.
540 	 *
541 	 * Allow this feature to be disabled if a particular process
542 	 * requests it.
543 	 */
544 	if (setcontext_enforcement) {
545 #if defined(__sparc)
546 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
547 #elif defined(__amd64)
548 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
549 #elif defined(__i386)
550 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
551 #else
552 #error "none of __sparc, __amd64, __i386 defined"
553 #endif
554 	}
555 
556 	/*
557 	 * Make sure that if we return to a call to __lwp_park()
558 	 * or ___lwp_cond_wait() that it returns right away
559 	 * (giving us a spurious wakeup but not a deadlock).
560 	 */
561 	set_parking_flag(self, 0);
562 	self->ul_sp = 0;
563 	ret = __setcontext(&uc);
564 
565 	/*
566 	 * It is OK for setcontext() to return if the user has not specified
567 	 * UC_CPU.
568 	 */
569 	if (uc.uc_flags & UC_CPU)
570 		thr_panic("setcontext(): __setcontext() returned");
571 	return (ret);
572 }
573 
574 #pragma weak thr_sigsetmask = _thr_sigsetmask
575 #pragma weak pthread_sigmask = _thr_sigsetmask
576 #pragma weak _pthread_sigmask = _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 sigprocmask = _sigprocmask
629 int
630 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
631 {
632 	int error;
633 
634 	/*
635 	 * Guard against children of vfork().
636 	 */
637 	if (curthread->ul_vfork)
638 		return (__lwp_sigmask(how, set, oset));
639 
640 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
641 		errno = error;
642 		return (-1);
643 	}
644 
645 	return (0);
646 }
647 
648 /*
649  * Called at library initialization to set up signal handling.
650  * All we really do is initialize the sig_lock rwlocks.
651  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
652  * However, if any signal handlers were established on alternate
653  * link maps before the primary link map has been initialized,
654  * then inform the kernel of the new sigacthandler.
655  */
656 void
657 signal_init()
658 {
659 	uberdata_t *udp = curthread->ul_uberdata;
660 	struct sigaction *sap;
661 	struct sigaction act;
662 	rwlock_t *rwlp;
663 	int sig;
664 
665 	for (sig = 0; sig < NSIG; sig++) {
666 		rwlp = &udp->siguaction[sig].sig_lock;
667 		rwlp->rwlock_magic = RWL_MAGIC;
668 		rwlp->mutex.mutex_flag = LOCK_INITED;
669 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
670 		sap = &udp->siguaction[sig].sig_uaction;
671 		if (sap->sa_sigaction != SIG_DFL &&
672 		    sap->sa_sigaction != SIG_IGN &&
673 		    __sigaction(sig, NULL, &act) == 0 &&
674 		    act.sa_sigaction != SIG_DFL &&
675 		    act.sa_sigaction != SIG_IGN) {
676 			act = *sap;
677 			act.sa_flags &= ~SA_NODEFER;
678 			act.sa_sigaction = udp->sigacthandler;
679 			act.sa_mask = maskset;
680 			(void) __sigaction(sig, &act, NULL);
681 		}
682 	}
683 }
684 
685 /*
686  * Common code for cancelling self in _sigcancel() and pthread_cancel().
687  * First record the fact that a cancellation is pending.
688  * Then, if cancellation is disabled or if we are holding unprotected
689  * libc locks, just return to defer the cancellation.
690  * Then, if we are at a cancellation point (ul_cancelable) just
691  * return and let _canceloff() do the exit.
692  * Else exit immediately if async mode is in effect.
693  */
694 void
695 do_sigcancel(void)
696 {
697 	ulwp_t *self = curthread;
698 
699 	ASSERT(self->ul_critical == 0);
700 	ASSERT(self->ul_sigdefer == 0);
701 	self->ul_cancel_pending = 1;
702 	if (self->ul_cancel_async &&
703 	    !self->ul_cancel_disabled &&
704 	    self->ul_libc_locks == 0 &&
705 	    !self->ul_cancelable)
706 		_pthread_exit(PTHREAD_CANCELED);
707 	set_cancel_pending_flag(self, 0);
708 }
709 
710 /*
711  * Set up the SIGCANCEL handler for threads cancellation,
712  * needed only when we have more than one thread,
713  * or the SIGAIOCANCEL handler for aio cancellation,
714  * called when aio is initialized, in __uaio_init().
715  */
716 void
717 setup_cancelsig(int sig)
718 {
719 	uberdata_t *udp = curthread->ul_uberdata;
720 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
721 	struct sigaction act;
722 
723 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
724 	lrw_rdlock(rwlp);
725 	act = udp->siguaction[sig].sig_uaction;
726 	lrw_unlock(rwlp);
727 	if (act.sa_sigaction == SIG_DFL ||
728 	    act.sa_sigaction == SIG_IGN)
729 		act.sa_flags = SA_SIGINFO;
730 	else {
731 		act.sa_flags |= SA_SIGINFO;
732 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
733 	}
734 	act.sa_sigaction = udp->sigacthandler;
735 	act.sa_mask = maskset;
736 	(void) __sigaction(sig, &act, NULL);
737 }
738