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