xref: /titanic_41/usr/src/lib/libc/port/threads/sigaction.c (revision 10569901907c93e3fa20f6ac0b589b9a5b869a80)
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 <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 	 * Logically, we should acquire and release the signal's
241 	 * sig_lock around this operation to protect the integrity
242 	 * of the signal action while we copy it, as is done below
243 	 * in _libc_sigaction().  However, we may be on a user-level
244 	 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
245 	 * might attempt to sleep on a different sleep queue and
246 	 * that would corrupt the entire sleep queue mechanism.
247 	 *
248 	 * If we are on a sleep queue we will remove ourself from
249 	 * it in call_user_handler(), called from sigacthandler(),
250 	 * before entering the application's signal handler.
251 	 * In the meantime, we must not acquire any locks.
252 	 */
253 	if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
254 		struct sigaction tact = suap->sig_uaction;
255 		tact.sa_flags &= ~SA_NODEFER;
256 		tact.sa_sigaction = self->ul_uberdata->sigacthandler;
257 		tact.sa_mask = maskset;
258 		(void) __sigaction(sig, &tact, NULL);
259 	}
260 
261 	if (self->ul_siginfo.si_signo == 0)
262 		sip = NULL;
263 	else
264 		sip = &self->ul_siginfo;
265 
266 	/* EAGAIN can happen only for a pending SIGSTOP signal */
267 	while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
268 		continue;
269 	if (error)
270 		thr_panic("take_deferred_signal(): __sigresend() failed");
271 }
272 
273 void
274 sigacthandler(int sig, siginfo_t *sip, void *uvp)
275 {
276 	ucontext_t *ucp = uvp;
277 	ulwp_t *self = curthread;
278 
279 	/*
280 	 * Do this in case we took a signal while in a cancelable system call.
281 	 * It does no harm if we were not in such a system call.
282 	 */
283 	self->ul_sp = 0;
284 	if (sig != SIGCANCEL)
285 		self->ul_cancel_async = self->ul_save_async;
286 
287 	/*
288 	 * If we are not in a critical region and are
289 	 * not deferring signals, take the signal now.
290 	 */
291 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
292 		call_user_handler(sig, sip, ucp);
293 		return;	/* call_user_handler() cannot return */
294 	}
295 
296 	/*
297 	 * We are in a critical region or we are deferring signals.  When
298 	 * we emerge from the region we will call take_deferred_signal().
299 	 */
300 	ASSERT(self->ul_cursig == 0);
301 	self->ul_cursig = (char)sig;
302 	if (sip != NULL)
303 		(void) _private_memcpy(&self->ul_siginfo,
304 		    sip, sizeof (siginfo_t));
305 	else
306 		self->ul_siginfo.si_signo = 0;
307 
308 	/*
309 	 * Make sure that if we return to a call to __lwp_park()
310 	 * or ___lwp_cond_wait() that it returns right away
311 	 * (giving us a spurious wakeup but not a deadlock).
312 	 */
313 	set_parking_flag(self, 0);
314 
315 	/*
316 	 * Return to the previous context with all signals blocked.
317 	 * We will restore the signal mask in take_deferred_signal().
318 	 * Note that we are calling the system call trap here, not
319 	 * the _setcontext() wrapper.  We don't want to change the
320 	 * thread's ul_sigmask by this operation.
321 	 */
322 	ucp->uc_sigmask = maskset;
323 	(void) __setcontext_syscall(ucp);
324 	thr_panic("sigacthandler(): __setcontext() returned");
325 }
326 
327 #pragma weak sigaction = _libc_sigaction
328 #pragma weak _sigaction = _libc_sigaction
329 int
330 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
331 {
332 	ulwp_t *self = curthread;
333 	uberdata_t *udp = self->ul_uberdata;
334 	struct sigaction oaction;
335 	struct sigaction tact;
336 	struct sigaction *tactp = NULL;
337 	int rv;
338 
339 	if (sig <= 0 || sig >= NSIG) {
340 		errno = EINVAL;
341 		return (-1);
342 	}
343 
344 	if (!self->ul_vfork)
345 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
346 
347 	oaction = udp->siguaction[sig].sig_uaction;
348 
349 	if (nact != NULL) {
350 		tact = *nact;	/* make a copy so we can modify it */
351 		tactp = &tact;
352 		delete_reserved_signals(&tact.sa_mask);
353 
354 #if !defined(_LP64)
355 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
356 #endif
357 		/*
358 		 * To be compatible with the behavior of SunOS 4.x:
359 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
360 		 * not change the signal's entry in the siguaction array.
361 		 * This allows a child of vfork(2) to set signal handlers
362 		 * to SIG_IGN or SIG_DFL without affecting the parent.
363 		 *
364 		 * This also covers a race condition with some thread
365 		 * setting the signal action to SIG_DFL or SIG_IGN
366 		 * when the thread has also received and deferred
367 		 * that signal.  When the thread takes the deferred
368 		 * signal, even though it has set the action to SIG_DFL
369 		 * or SIG_IGN, it will execute the old signal handler
370 		 * anyway.  This is an inherent signaling race condition
371 		 * and is not a bug.
372 		 *
373 		 * A child of vfork() is not allowed to change signal
374 		 * handlers to anything other than SIG_DFL or SIG_IGN.
375 		 */
376 		if (self->ul_vfork) {
377 			if (tact.sa_sigaction != SIG_IGN)
378 				tact.sa_sigaction = SIG_DFL;
379 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
380 			/*
381 			 * Always catch these signals.
382 			 * We need SIGCANCEL for pthread_cancel() to work.
383 			 * We need SIGAIOCANCEL for aio_cancel() to work.
384 			 */
385 			udp->siguaction[sig].sig_uaction = tact;
386 			if (tact.sa_sigaction == SIG_DFL ||
387 			    tact.sa_sigaction == SIG_IGN)
388 				tact.sa_flags = SA_SIGINFO;
389 			else {
390 				tact.sa_flags |= SA_SIGINFO;
391 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
392 			}
393 			tact.sa_sigaction = udp->sigacthandler;
394 			tact.sa_mask = maskset;
395 		} else if (tact.sa_sigaction != SIG_DFL &&
396 		    tact.sa_sigaction != SIG_IGN) {
397 			udp->siguaction[sig].sig_uaction = tact;
398 			tact.sa_flags &= ~SA_NODEFER;
399 			tact.sa_sigaction = udp->sigacthandler;
400 			tact.sa_mask = maskset;
401 		}
402 	}
403 
404 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
405 		udp->siguaction[sig].sig_uaction = oaction;
406 	else if (oact != NULL &&
407 	    oact->sa_sigaction != SIG_DFL &&
408 	    oact->sa_sigaction != SIG_IGN)
409 		*oact = oaction;
410 
411 	/*
412 	 * We detect setting the disposition of SIGIO just to set the
413 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
414 	 */
415 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
416 		_sigio_enabled =
417 		    (tactp->sa_handler != SIG_DFL &&
418 		    tactp->sa_handler != SIG_IGN);
419 	}
420 
421 	if (!self->ul_vfork)
422 		lrw_unlock(&udp->siguaction[sig].sig_lock);
423 	return (rv);
424 }
425 
426 void
427 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
428     void (**osigacthandler)(int, siginfo_t *, void *))
429 {
430 	ulwp_t *self = curthread;
431 	uberdata_t *udp = self->ul_uberdata;
432 
433 	if (osigacthandler != NULL)
434 		*osigacthandler = udp->sigacthandler;
435 
436 	udp->sigacthandler = nsigacthandler;
437 }
438 
439 /*
440  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
441  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
442  * the unprotected (from signals) interval before reaching the kernel,
443  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
444  * the kernel to return immediately from these system calls, giving us
445  * a spurious wakeup but not a deadlock.
446  */
447 void
448 set_parking_flag(ulwp_t *self, int park)
449 {
450 	volatile sc_shared_t *scp;
451 
452 	enter_critical(self);
453 	if ((scp = self->ul_schedctl) != NULL ||
454 	    (scp = setup_schedctl()) != NULL)
455 		scp->sc_park = park;
456 	else if (park == 0)	/* schedctl failed, do it the long way */
457 		__lwp_unpark(self->ul_lwpid);
458 	exit_critical(self);
459 }
460 
461 /*
462  * Tell the kernel to block all signals.
463  * Use the schedctl interface, or failing that, use __lwp_sigmask().
464  * This action can be rescinded only by making a system call that
465  * sets the signal mask:
466  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
467  *	__sigsuspend() or __pollsys().
468  * In particular, this action cannot be reversed by assigning
469  * scp->sc_sigblock = 0.  That would be a way to lose signals.
470  * See the definition of restore_signals(self).
471  */
472 void
473 block_all_signals(ulwp_t *self)
474 {
475 	volatile sc_shared_t *scp;
476 
477 	enter_critical(self);
478 	if ((scp = self->ul_schedctl) != NULL ||
479 	    (scp = setup_schedctl()) != NULL)
480 		scp->sc_sigblock = 1;
481 	else
482 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
483 	exit_critical(self);
484 }
485 
486 /*
487  * _private_setcontext has code that forcibly restores the curthread
488  * pointer in a context passed to the setcontext(2) syscall.
489  *
490  * Certain processes may need to disable this feature, so these routines
491  * provide the mechanism to do so.
492  *
493  * (As an example, branded 32-bit x86 processes may use %gs for their own
494  * purposes, so they need to be able to specify a %gs value to be restored
495  * on return from a signal handler via the passed ucontext_t.)
496  */
497 static int setcontext_enforcement = 1;
498 
499 void
500 set_setcontext_enforcement(int on)
501 {
502 	setcontext_enforcement = on;
503 }
504 
505 #pragma weak setcontext = _private_setcontext
506 #pragma weak _setcontext = _private_setcontext
507 int
508 _private_setcontext(const ucontext_t *ucp)
509 {
510 	ulwp_t *self = curthread;
511 	int ret;
512 	ucontext_t uc;
513 
514 	/*
515 	 * Returning from the main context (uc_link == NULL) causes
516 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
517 	 */
518 	if (ucp == NULL)
519 		_thr_exit(NULL);
520 	(void) _private_memcpy(&uc, ucp, sizeof (uc));
521 
522 	/*
523 	 * Restore previous signal mask and context link.
524 	 */
525 	if (uc.uc_flags & UC_SIGMASK) {
526 		block_all_signals(self);
527 		delete_reserved_signals(&uc.uc_sigmask);
528 		self->ul_sigmask = uc.uc_sigmask;
529 		if (self->ul_cursig) {
530 			/*
531 			 * We have a deferred signal present.
532 			 * The signal mask will be set when the
533 			 * signal is taken in take_deferred_signal().
534 			 */
535 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
536 			uc.uc_flags &= ~UC_SIGMASK;
537 		}
538 	}
539 	self->ul_siglink = uc.uc_link;
540 
541 	/*
542 	 * We don't know where this context structure has been.
543 	 * Preserve the curthread pointer, at least.
544 	 *
545 	 * Allow this feature to be disabled if a particular process
546 	 * requests it.
547 	 */
548 	if (setcontext_enforcement) {
549 #if defined(__sparc)
550 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
551 #elif defined(__amd64)
552 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
553 #elif defined(__i386)
554 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
555 #else
556 #error "none of __sparc, __amd64, __i386 defined"
557 #endif
558 	}
559 
560 	/*
561 	 * Make sure that if we return to a call to __lwp_park()
562 	 * or ___lwp_cond_wait() that it returns right away
563 	 * (giving us a spurious wakeup but not a deadlock).
564 	 */
565 	set_parking_flag(self, 0);
566 	self->ul_sp = 0;
567 	ret = __setcontext_syscall(&uc);
568 
569 	/*
570 	 * It is OK for setcontext() to return if the user has not specified
571 	 * UC_CPU.
572 	 */
573 	if (uc.uc_flags & UC_CPU)
574 		thr_panic("setcontext(): __setcontext() returned");
575 	return (ret);
576 }
577 
578 #pragma weak thr_sigsetmask = _thr_sigsetmask
579 #pragma weak pthread_sigmask = _thr_sigsetmask
580 #pragma weak _pthread_sigmask = _thr_sigsetmask
581 int
582 _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
583 {
584 	ulwp_t *self = curthread;
585 	sigset_t saveset;
586 
587 	if (set == NULL) {
588 		enter_critical(self);
589 		if (oset != NULL)
590 			*oset = self->ul_sigmask;
591 		exit_critical(self);
592 	} else {
593 		switch (how) {
594 		case SIG_BLOCK:
595 		case SIG_UNBLOCK:
596 		case SIG_SETMASK:
597 			break;
598 		default:
599 			return (EINVAL);
600 		}
601 
602 		/*
603 		 * The assignments to self->ul_sigmask must be protected from
604 		 * signals.  The nuances of this code are subtle.  Be careful.
605 		 */
606 		block_all_signals(self);
607 		if (oset != NULL)
608 			saveset = self->ul_sigmask;
609 		switch (how) {
610 		case SIG_BLOCK:
611 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
612 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
613 			break;
614 		case SIG_UNBLOCK:
615 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
616 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
617 			break;
618 		case SIG_SETMASK:
619 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
620 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
621 			break;
622 		}
623 		delete_reserved_signals(&self->ul_sigmask);
624 		if (oset != NULL)
625 			*oset = saveset;
626 		restore_signals(self);
627 	}
628 
629 	return (0);
630 }
631 
632 #pragma weak sigprocmask = _sigprocmask
633 int
634 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
635 {
636 	int error;
637 
638 	/*
639 	 * Guard against children of vfork().
640 	 */
641 	if (curthread->ul_vfork)
642 		return (__lwp_sigmask(how, set, oset));
643 
644 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
645 		errno = error;
646 		return (-1);
647 	}
648 
649 	return (0);
650 }
651 
652 /*
653  * Called at library initialization to set up signal handling.
654  * All we really do is initialize the sig_lock rwlocks.
655  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
656  * However, if any signal handlers were established on alternate
657  * link maps before the primary link map has been initialized,
658  * then inform the kernel of the new sigacthandler.
659  */
660 void
661 signal_init()
662 {
663 	uberdata_t *udp = curthread->ul_uberdata;
664 	struct sigaction *sap;
665 	struct sigaction act;
666 	rwlock_t *rwlp;
667 	int sig;
668 
669 	for (sig = 0; sig < NSIG; sig++) {
670 		rwlp = &udp->siguaction[sig].sig_lock;
671 		rwlp->rwlock_magic = RWL_MAGIC;
672 		rwlp->mutex.mutex_flag = LOCK_INITED;
673 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
674 		sap = &udp->siguaction[sig].sig_uaction;
675 		if (sap->sa_sigaction != SIG_DFL &&
676 		    sap->sa_sigaction != SIG_IGN &&
677 		    __sigaction(sig, NULL, &act) == 0 &&
678 		    act.sa_sigaction != SIG_DFL &&
679 		    act.sa_sigaction != SIG_IGN) {
680 			act = *sap;
681 			act.sa_flags &= ~SA_NODEFER;
682 			act.sa_sigaction = udp->sigacthandler;
683 			act.sa_mask = maskset;
684 			(void) __sigaction(sig, &act, NULL);
685 		}
686 	}
687 }
688 
689 /*
690  * Common code for cancelling self in _sigcancel() and pthread_cancel().
691  * If the thread is at a cancellation point (ul_cancelable) then just
692  * return and let _canceloff() do the exit, else exit immediately if
693  * async mode is in effect.
694  */
695 void
696 do_sigcancel()
697 {
698 	ulwp_t *self = curthread;
699 
700 	ASSERT(self->ul_critical == 0);
701 	ASSERT(self->ul_sigdefer == 0);
702 	self->ul_cancel_pending = 1;
703 	if (self->ul_cancel_async &&
704 	    !self->ul_cancel_disabled &&
705 	    !self->ul_cancelable)
706 		_pthread_exit(PTHREAD_CANCELED);
707 }
708 
709 /*
710  * Set up the SIGCANCEL handler for threads cancellation,
711  * needed only when we have more than one thread,
712  * or the SIGAIOCANCEL handler for aio cancellation,
713  * called when aio is initialized, in __uaio_init().
714  */
715 void
716 setup_cancelsig(int sig)
717 {
718 	uberdata_t *udp = curthread->ul_uberdata;
719 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
720 	struct sigaction act;
721 
722 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
723 	lrw_rdlock(rwlp);
724 	act = udp->siguaction[sig].sig_uaction;
725 	lrw_unlock(rwlp);
726 	if (act.sa_sigaction == SIG_DFL ||
727 	    act.sa_sigaction == SIG_IGN)
728 		act.sa_flags = SA_SIGINFO;
729 	else {
730 		act.sa_flags |= SA_SIGINFO;
731 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
732 	}
733 	act.sa_sigaction = udp->sigacthandler;
734 	act.sa_mask = maskset;
735 	(void) __sigaction(sig, &act, NULL);
736 }
737