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 || sig == SIGAIOCANCEL) &&
113 		    (sap->sa_flags & SA_RESETHAND))
114 			sap->sa_sigaction = SIG_DFL;
115 		lrw_unlock(rwlp);
116 	}
117 
118 	/*
119 	 * Set the proper signal mask and call the user's signal handler.
120 	 * (We overrode the user-requested signal mask with maskset
121 	 * so we currently have all blockable signals blocked.)
122 	 *
123 	 * We would like to ASSERT() that the signal is not a member of the
124 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
125 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
126 	 * /proc can override this via PCSSIG, so we don't bother.
127 	 *
128 	 * We would also like to ASSERT() that the signal mask at the previous
129 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
130 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
131 	 * don't bother with that either.
132 	 */
133 	ASSERT(ucp->uc_flags & UC_SIGMASK);
134 	if (self->ul_sigsuspend) {
135 		ucp->uc_sigmask = self->ul_sigmask;
136 		self->ul_sigsuspend = 0;
137 		/* the sigsuspend() or pollsys() signal mask */
138 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
139 	} else {
140 		/* the signal mask at the previous level */
141 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
142 	}
143 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
144 		(void) _private_sigaddset(&uact.sa_mask, sig);
145 	self->ul_sigmask = uact.sa_mask;
146 	self->ul_siglink = ucp;
147 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
148 
149 	/*
150 	 * If this thread has been sent SIGCANCEL from the kernel
151 	 * or from pthread_cancel(), it is being asked to exit.
152 	 * The kernel may send SIGCANCEL without a siginfo struct.
153 	 * If the SIGCANCEL is process-directed (from kill() or
154 	 * sigqueue()), treat it as an ordinary signal.
155 	 */
156 	if (sig == SIGCANCEL) {
157 		if (sip == NULL || SI_FROMKERNEL(sip) ||
158 		    sip->si_code == SI_LWP) {
159 			do_sigcancel();
160 			goto out;
161 		}
162 		/* SIGCANCEL is ignored by default */
163 		if (uact.sa_sigaction == SIG_DFL ||
164 		    uact.sa_sigaction == SIG_IGN)
165 			goto out;
166 	}
167 
168 	/*
169 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
170 	 * we are an aio worker thread, cancel the aio request.
171 	 */
172 	if (sig == SIGAIOCANCEL) {
173 		aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
174 
175 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
176 			_siglongjmp(aiowp->work_jmp_buf, 1);
177 		/* SIGLWP is ignored by default */
178 		if (uact.sa_sigaction == SIG_DFL ||
179 		    uact.sa_sigaction == SIG_IGN)
180 			goto out;
181 	}
182 
183 	if (!(uact.sa_flags & SA_SIGINFO))
184 		sip = NULL;
185 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
186 
187 #if defined(sparc) || defined(__sparc)
188 	/*
189 	 * If this is a floating point exception and the queue
190 	 * is non-empty, pop the top entry from the queue.  This
191 	 * is to maintain expected behavior.
192 	 */
193 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
194 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
195 
196 		if (--fp->fpu_qcnt > 0) {
197 			unsigned char i;
198 			struct fq *fqp;
199 
200 			fqp = fp->fpu_q;
201 			for (i = 0; i < fp->fpu_qcnt; i++)
202 				fqp[i] = fqp[i+1];
203 		}
204 	}
205 #endif	/* sparc */
206 
207 out:
208 	(void) _private_setcontext(ucp);
209 	thr_panic("call_user_handler(): _setcontext() returned");
210 }
211 
212 /*
213  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
214  * zero and a deferred signal has been recorded on the current thread.
215  * We are out of the critical region and are ready to take a signal.
216  * The kernel has all signals blocked on this lwp, but our value of
217  * ul_sigmask is the correct signal mask for the previous context.
218  *
219  * We call __sigresend() to atomically restore the signal mask and
220  * cause the signal to be sent again with the remembered siginfo.
221  * We will not return successfully from __sigresend() until the
222  * application's signal handler has been run via sigacthandler().
223  */
224 void
225 take_deferred_signal(int sig)
226 {
227 	extern int __sigresend(int, siginfo_t *, sigset_t *);
228 	ulwp_t *self = curthread;
229 	siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
230 	siginfo_t *sip;
231 	int error;
232 
233 	ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
234 
235 	/*
236 	 * If the signal handler was established with SA_RESETHAND,
237 	 * the kernel has reset the handler to SIG_DFL, so we have
238 	 * to reestablish the handler now so that it will be entered
239 	 * again when we call __sigresend(), below.
240 	 *
241 	 * Logically, we should acquire and release the signal's
242 	 * sig_lock around this operation to protect the integrity
243 	 * of the signal action while we copy it, as is done below
244 	 * in _libc_sigaction().  However, we may be on a user-level
245 	 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
246 	 * might attempt to sleep on a different sleep queue and
247 	 * that would corrupt the entire sleep queue mechanism.
248 	 *
249 	 * If we are on a sleep queue we will remove ourself from
250 	 * it in call_user_handler(), called from sigacthandler(),
251 	 * before entering the application's signal handler.
252 	 * In the meantime, we must not acquire any locks.
253 	 */
254 	if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
255 		struct sigaction tact = suap->sig_uaction;
256 		tact.sa_flags &= ~SA_NODEFER;
257 		tact.sa_sigaction = self->ul_uberdata->sigacthandler;
258 		tact.sa_mask = maskset;
259 		(void) __sigaction(sig, &tact, NULL);
260 	}
261 
262 	if (self->ul_siginfo.si_signo == 0)
263 		sip = NULL;
264 	else
265 		sip = &self->ul_siginfo;
266 
267 	/* EAGAIN can happen only for a pending SIGSTOP signal */
268 	while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
269 		continue;
270 	if (error)
271 		thr_panic("take_deferred_signal(): __sigresend() failed");
272 }
273 
274 void
275 sigacthandler(int sig, siginfo_t *sip, void *uvp)
276 {
277 	ucontext_t *ucp = uvp;
278 	ulwp_t *self = curthread;
279 
280 	/*
281 	 * Do this in case we took a signal while in a cancelable system call.
282 	 * It does no harm if we were not in such a system call.
283 	 */
284 	self->ul_sp = 0;
285 	if (sig != SIGCANCEL)
286 		self->ul_cancel_async = self->ul_save_async;
287 
288 	/*
289 	 * If we are not in a critical region and are
290 	 * not deferring signals, take the signal now.
291 	 */
292 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
293 		call_user_handler(sig, sip, ucp);
294 		/*
295 		 * On the surface, the following call seems redundant
296 		 * because call_user_handler() cannot return. However,
297 		 * we don't want to return from here because the compiler
298 		 * might recycle our frame. We want to keep it on the
299 		 * stack to assist debuggers such as pstack in identifying
300 		 * signal frames. The call to thr_panic() serves to prevent
301 		 * tail-call optimisation here.
302 		 */
303 		thr_panic("sigacthandler(): call_user_handler() returned");
304 	}
305 
306 	/*
307 	 * We are in a critical region or we are deferring signals.  When
308 	 * we emerge from the region we will call take_deferred_signal().
309 	 */
310 	ASSERT(self->ul_cursig == 0);
311 	self->ul_cursig = (char)sig;
312 	if (sip != NULL)
313 		(void) _private_memcpy(&self->ul_siginfo,
314 		    sip, sizeof (siginfo_t));
315 	else
316 		self->ul_siginfo.si_signo = 0;
317 
318 	/*
319 	 * Make sure that if we return to a call to __lwp_park()
320 	 * or ___lwp_cond_wait() that it returns right away
321 	 * (giving us a spurious wakeup but not a deadlock).
322 	 */
323 	set_parking_flag(self, 0);
324 
325 	/*
326 	 * Return to the previous context with all signals blocked.
327 	 * We will restore the signal mask in take_deferred_signal().
328 	 * Note that we are calling the system call trap here, not
329 	 * the _setcontext() wrapper.  We don't want to change the
330 	 * thread's ul_sigmask by this operation.
331 	 */
332 	ucp->uc_sigmask = maskset;
333 	(void) __setcontext_syscall(ucp);
334 	thr_panic("sigacthandler(): __setcontext() returned");
335 }
336 
337 #pragma weak sigaction = _libc_sigaction
338 #pragma weak _sigaction = _libc_sigaction
339 int
340 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
341 {
342 	ulwp_t *self = curthread;
343 	uberdata_t *udp = self->ul_uberdata;
344 	struct sigaction oaction;
345 	struct sigaction tact;
346 	struct sigaction *tactp = NULL;
347 	int rv;
348 
349 	if (sig <= 0 || sig >= NSIG) {
350 		errno = EINVAL;
351 		return (-1);
352 	}
353 
354 	if (!self->ul_vfork)
355 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
356 
357 	oaction = udp->siguaction[sig].sig_uaction;
358 
359 	if (nact != NULL) {
360 		tact = *nact;	/* make a copy so we can modify it */
361 		tactp = &tact;
362 		delete_reserved_signals(&tact.sa_mask);
363 
364 #if !defined(_LP64)
365 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
366 #endif
367 		/*
368 		 * To be compatible with the behavior of SunOS 4.x:
369 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
370 		 * not change the signal's entry in the siguaction array.
371 		 * This allows a child of vfork(2) to set signal handlers
372 		 * to SIG_IGN or SIG_DFL without affecting the parent.
373 		 *
374 		 * This also covers a race condition with some thread
375 		 * setting the signal action to SIG_DFL or SIG_IGN
376 		 * when the thread has also received and deferred
377 		 * that signal.  When the thread takes the deferred
378 		 * signal, even though it has set the action to SIG_DFL
379 		 * or SIG_IGN, it will execute the old signal handler
380 		 * anyway.  This is an inherent signaling race condition
381 		 * and is not a bug.
382 		 *
383 		 * A child of vfork() is not allowed to change signal
384 		 * handlers to anything other than SIG_DFL or SIG_IGN.
385 		 */
386 		if (self->ul_vfork) {
387 			if (tact.sa_sigaction != SIG_IGN)
388 				tact.sa_sigaction = SIG_DFL;
389 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
390 			/*
391 			 * Always catch these signals.
392 			 * We need SIGCANCEL for pthread_cancel() to work.
393 			 * We need SIGAIOCANCEL for aio_cancel() to work.
394 			 */
395 			udp->siguaction[sig].sig_uaction = tact;
396 			if (tact.sa_sigaction == SIG_DFL ||
397 			    tact.sa_sigaction == SIG_IGN)
398 				tact.sa_flags = SA_SIGINFO;
399 			else {
400 				tact.sa_flags |= SA_SIGINFO;
401 				tact.sa_flags &=
402 				    ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
403 			}
404 			tact.sa_sigaction = udp->sigacthandler;
405 			tact.sa_mask = maskset;
406 		} else if (tact.sa_sigaction != SIG_DFL &&
407 		    tact.sa_sigaction != SIG_IGN) {
408 			udp->siguaction[sig].sig_uaction = tact;
409 			tact.sa_flags &= ~SA_NODEFER;
410 			tact.sa_sigaction = udp->sigacthandler;
411 			tact.sa_mask = maskset;
412 		}
413 	}
414 
415 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
416 		udp->siguaction[sig].sig_uaction = oaction;
417 	else if (oact != NULL &&
418 	    oact->sa_sigaction != SIG_DFL &&
419 	    oact->sa_sigaction != SIG_IGN)
420 		*oact = oaction;
421 
422 	/*
423 	 * We detect setting the disposition of SIGIO just to set the
424 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
425 	 */
426 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
427 		_sigio_enabled =
428 		    (tactp->sa_handler != SIG_DFL &&
429 		    tactp->sa_handler != SIG_IGN);
430 	}
431 
432 	if (!self->ul_vfork)
433 		lrw_unlock(&udp->siguaction[sig].sig_lock);
434 	return (rv);
435 }
436 
437 void
438 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
439     void (**osigacthandler)(int, siginfo_t *, void *))
440 {
441 	ulwp_t *self = curthread;
442 	uberdata_t *udp = self->ul_uberdata;
443 
444 	if (osigacthandler != NULL)
445 		*osigacthandler = udp->sigacthandler;
446 
447 	udp->sigacthandler = nsigacthandler;
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  * First record the fact that a cancellation is pending.
681  * Then, if cancellation is disabled or if we are holding unprotected
682  * libc locks, just return to defer the cancellation.
683  * Then, if we are at a cancellation point (ul_cancelable) just
684  * return and let _canceloff() do the exit.
685  * Else exit immediately if async mode is in effect.
686  */
687 void
688 do_sigcancel(void)
689 {
690 	ulwp_t *self = curthread;
691 
692 	ASSERT(self->ul_critical == 0);
693 	ASSERT(self->ul_sigdefer == 0);
694 	self->ul_cancel_pending = 1;
695 	if (self->ul_cancel_async &&
696 	    !self->ul_cancel_disabled &&
697 	    self->ul_libc_locks == 0 &&
698 	    !self->ul_cancelable)
699 		_pthread_exit(PTHREAD_CANCELED);
700 	set_cancel_pending_flag(self, 0);
701 }
702 
703 /*
704  * Set up the SIGCANCEL handler for threads cancellation,
705  * needed only when we have more than one thread,
706  * or the SIGAIOCANCEL handler for aio cancellation,
707  * called when aio is initialized, in __uaio_init().
708  */
709 void
710 setup_cancelsig(int sig)
711 {
712 	uberdata_t *udp = curthread->ul_uberdata;
713 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
714 	struct sigaction act;
715 
716 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
717 	lrw_rdlock(rwlp);
718 	act = udp->siguaction[sig].sig_uaction;
719 	lrw_unlock(rwlp);
720 	if (act.sa_sigaction == SIG_DFL ||
721 	    act.sa_sigaction == SIG_IGN)
722 		act.sa_flags = SA_SIGINFO;
723 	else {
724 		act.sa_flags |= SA_SIGINFO;
725 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
726 	}
727 	act.sa_sigaction = udp->sigacthandler;
728 	act.sa_mask = maskset;
729 	(void) __sigaction(sig, &act, NULL);
730 }
731