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