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 2007 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 	 *
87 	 * Locking exceptions:
88 	 * No locking for a child of vfork().
89 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
90 	 * then we assume that this signal was generated by
91 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
92 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
93 	 * then we assume that the signal was generated by
94 	 * a hardware performance counter overflow.
95 	 * In these cases, assume that we need no locking.  It is the
96 	 * monitoring program's responsibility to ensure correctness.
97 	 */
98 	sap = &udp->siguaction[sig].sig_uaction;
99 	if (self->ul_vfork ||
100 	    (sip != NULL &&
101 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
102 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
103 		/* we wish this assignment could be atomic */
104 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
105 	} else {
106 		mutex_t *mp = &udp->siguaction[sig].sig_lock;
107 		lmutex_lock(mp);
108 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
109 		if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
110 			sap->sa_sigaction = SIG_DFL;
111 		lmutex_unlock(mp);
112 	}
113 
114 	/*
115 	 * Set the proper signal mask and call the user's signal handler.
116 	 * (We overrode the user-requested signal mask with maskset
117 	 * so we currently have all blockable signals blocked.)
118 	 *
119 	 * We would like to ASSERT() that the signal is not a member of the
120 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
121 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
122 	 * /proc can override this via PCSSIG, so we don't bother.
123 	 *
124 	 * We would also like to ASSERT() that the signal mask at the previous
125 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
126 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
127 	 * don't bother with that either.
128 	 */
129 	ASSERT(ucp->uc_flags & UC_SIGMASK);
130 	if (self->ul_sigsuspend) {
131 		ucp->uc_sigmask = self->ul_sigmask;
132 		self->ul_sigsuspend = 0;
133 		/* the sigsuspend() or pollsys() signal mask */
134 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
135 	} else {
136 		/* the signal mask at the previous level */
137 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
138 	}
139 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
140 		(void) _private_sigaddset(&uact.sa_mask, sig);
141 	self->ul_sigmask = uact.sa_mask;
142 	self->ul_siglink = ucp;
143 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
144 
145 	/*
146 	 * If this thread has been sent SIGCANCEL from the kernel
147 	 * or from pthread_cancel(), it is being asked to exit.
148 	 * The kernel may send SIGCANCEL without a siginfo struct.
149 	 * If the SIGCANCEL is process-directed (from kill() or
150 	 * sigqueue()), treat it as an ordinary signal.
151 	 */
152 	if (sig == SIGCANCEL) {
153 		if (sip == NULL || SI_FROMKERNEL(sip) ||
154 		    sip->si_code == SI_LWP) {
155 			do_sigcancel();
156 			goto out;
157 		}
158 		/* SIGCANCEL is ignored by default */
159 		if (uact.sa_sigaction == SIG_DFL ||
160 		    uact.sa_sigaction == SIG_IGN)
161 			goto out;
162 	}
163 
164 	/*
165 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
166 	 * we are an aio worker thread, cancel the aio request.
167 	 */
168 	if (sig == SIGAIOCANCEL) {
169 		aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
170 
171 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
172 			_siglongjmp(aiowp->work_jmp_buf, 1);
173 		/* SIGLWP is ignored by default */
174 		if (uact.sa_sigaction == SIG_DFL ||
175 		    uact.sa_sigaction == SIG_IGN)
176 			goto out;
177 	}
178 
179 	if (!(uact.sa_flags & SA_SIGINFO))
180 		sip = NULL;
181 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
182 
183 #if defined(sparc) || defined(__sparc)
184 	/*
185 	 * If this is a floating point exception and the queue
186 	 * is non-empty, pop the top entry from the queue.  This
187 	 * is to maintain expected behavior.
188 	 */
189 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
190 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
191 
192 		if (--fp->fpu_qcnt > 0) {
193 			unsigned char i;
194 			struct fq *fqp;
195 
196 			fqp = fp->fpu_q;
197 			for (i = 0; i < fp->fpu_qcnt; i++)
198 				fqp[i] = fqp[i+1];
199 		}
200 	}
201 #endif	/* sparc */
202 
203 out:
204 	(void) _private_setcontext(ucp);
205 	thr_panic("call_user_handler(): _setcontext() returned");
206 }
207 
208 /*
209  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
210  * zero and a deferred signal has been recorded on the current thread.
211  * We are out of the critical region and are ready to take a signal.
212  * The kernel has all signals blocked on this lwp, but our value of
213  * ul_sigmask is the correct signal mask for the previous context.
214  */
215 void
216 take_deferred_signal(int sig)
217 {
218 	ulwp_t *self = curthread;
219 	siginfo_t siginfo;
220 	siginfo_t *sip;
221 	ucontext_t uc;
222 	volatile int returning;
223 
224 	ASSERT(self->ul_critical == 0);
225 	ASSERT(self->ul_sigdefer == 0);
226 	ASSERT(self->ul_cursig == 0);
227 
228 	returning = 0;
229 	uc.uc_flags = UC_ALL;
230 	/*
231 	 * We call _private_getcontext (a libc-private synonym for
232 	 * _getcontext) rather than _getcontext because we need to
233 	 * avoid the dynamic linker and link auditing problems here.
234 	 */
235 	(void) _private_getcontext(&uc);
236 	/*
237 	 * If the application signal handler calls setcontext() on
238 	 * the ucontext we give it, it returns here, then we return.
239 	 */
240 	if (returning)
241 		return;
242 	returning = 1;
243 	ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
244 	if (self->ul_siginfo.si_signo == 0)
245 		sip = NULL;
246 	else {
247 		(void) _private_memcpy(&siginfo,
248 		    &self->ul_siginfo, sizeof (siginfo));
249 		sip = &siginfo;
250 	}
251 	uc.uc_sigmask = self->ul_sigmask;
252 	call_user_handler(sig, sip, &uc);
253 }
254 
255 void
256 sigacthandler(int sig, siginfo_t *sip, void *uvp)
257 {
258 	ucontext_t *ucp = uvp;
259 	ulwp_t *self = curthread;
260 
261 	/*
262 	 * Do this in case we took a signal while in a cancelable system call.
263 	 * It does no harm if we were not in such a system call.
264 	 */
265 	self->ul_sp = 0;
266 	if (sig != SIGCANCEL)
267 		self->ul_cancel_async = self->ul_save_async;
268 
269 	/*
270 	 * If we are not in a critical region and are
271 	 * not deferring signals, take the signal now.
272 	 */
273 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
274 		call_user_handler(sig, sip, ucp);
275 		return;	/* call_user_handler() cannot return */
276 	}
277 
278 	/*
279 	 * We are in a critical region or we are deferring signals.  When
280 	 * we emerge from the region we will call take_deferred_signal().
281 	 */
282 	ASSERT(self->ul_cursig == 0);
283 	self->ul_cursig = (char)sig;
284 	if (sip != NULL)
285 		(void) _private_memcpy(&self->ul_siginfo,
286 		    sip, sizeof (siginfo_t));
287 	else
288 		self->ul_siginfo.si_signo = 0;
289 
290 	/*
291 	 * Make sure that if we return to a call to __lwp_park()
292 	 * or ___lwp_cond_wait() that it returns right away
293 	 * (giving us a spurious wakeup but not a deadlock).
294 	 */
295 	set_parking_flag(self, 0);
296 
297 	/*
298 	 * Return to the previous context with all signals blocked.
299 	 * We will restore the signal mask in take_deferred_signal().
300 	 * Note that we are calling the system call trap here, not
301 	 * the _setcontext() wrapper.  We don't want to change the
302 	 * thread's ul_sigmask by this operation.
303 	 */
304 	ucp->uc_sigmask = maskset;
305 	(void) __setcontext_syscall(ucp);
306 	thr_panic("sigacthandler(): __setcontext() returned");
307 }
308 
309 #pragma weak sigaction = _libc_sigaction
310 #pragma weak _sigaction = _libc_sigaction
311 int
312 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
313 {
314 	ulwp_t *self = curthread;
315 	uberdata_t *udp = self->ul_uberdata;
316 	struct sigaction oaction;
317 	struct sigaction tact;
318 	struct sigaction *tactp = NULL;
319 	int rv;
320 
321 	if (sig <= 0 || sig >= NSIG) {
322 		errno = EINVAL;
323 		return (-1);
324 	}
325 
326 	if (!self->ul_vfork)
327 		lmutex_lock(&udp->siguaction[sig].sig_lock);
328 
329 	oaction = udp->siguaction[sig].sig_uaction;
330 
331 	if (nact != NULL) {
332 		tact = *nact;	/* make a copy so we can modify it */
333 		tactp = &tact;
334 		delete_reserved_signals(&tact.sa_mask);
335 
336 #if !defined(_LP64)
337 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
338 #endif
339 		/*
340 		 * To be compatible with the behavior of SunOS 4.x:
341 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
342 		 * not change the signal's entry in the siguaction array.
343 		 * This allows a child of vfork(2) to set signal handlers
344 		 * to SIG_IGN or SIG_DFL without affecting the parent.
345 		 *
346 		 * This also covers a race condition with some thread
347 		 * setting the signal action to SIG_DFL or SIG_IGN
348 		 * when the thread has also received and deferred
349 		 * that signal.  When the thread takes the deferred
350 		 * signal, even though it has set the action to SIG_DFL
351 		 * or SIG_IGN, it will execute the old signal handler
352 		 * anyway.  This is an inherent signaling race condition
353 		 * and is not a bug.
354 		 *
355 		 * A child of vfork() is not allowed to change signal
356 		 * handlers to anything other than SIG_DFL or SIG_IGN.
357 		 */
358 		if (self->ul_vfork) {
359 			if (tact.sa_sigaction != SIG_IGN)
360 				tact.sa_sigaction = SIG_DFL;
361 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
362 			/*
363 			 * Always catch these signals.
364 			 * We need SIGCANCEL for pthread_cancel() to work.
365 			 * We need SIGAIOCANCEL for aio_cancel() to work.
366 			 */
367 			udp->siguaction[sig].sig_uaction = tact;
368 			if (tact.sa_sigaction == SIG_DFL ||
369 			    tact.sa_sigaction == SIG_IGN)
370 				tact.sa_flags = SA_SIGINFO;
371 			else {
372 				tact.sa_flags |= SA_SIGINFO;
373 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
374 			}
375 			tact.sa_sigaction = udp->sigacthandler;
376 			tact.sa_mask = maskset;
377 		} else if (tact.sa_sigaction != SIG_DFL &&
378 		    tact.sa_sigaction != SIG_IGN) {
379 			udp->siguaction[sig].sig_uaction = tact;
380 			tact.sa_flags &= ~SA_NODEFER;
381 			tact.sa_sigaction = udp->sigacthandler;
382 			tact.sa_mask = maskset;
383 		}
384 	}
385 
386 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
387 		udp->siguaction[sig].sig_uaction = oaction;
388 	else if (oact != NULL &&
389 	    oact->sa_sigaction != SIG_DFL &&
390 	    oact->sa_sigaction != SIG_IGN)
391 		*oact = oaction;
392 
393 	/*
394 	 * We detect setting the disposition of SIGIO just to set the
395 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
396 	 */
397 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
398 		_sigio_enabled =
399 		    (tactp->sa_handler != SIG_DFL &&
400 		    tactp->sa_handler != SIG_IGN);
401 	}
402 
403 	if (!self->ul_vfork)
404 		lmutex_unlock(&udp->siguaction[sig].sig_lock);
405 	return (rv);
406 }
407 
408 void
409 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
410     void (**osigacthandler)(int, siginfo_t *, void *))
411 {
412 	ulwp_t *self = curthread;
413 	uberdata_t *udp = self->ul_uberdata;
414 
415 	if (osigacthandler != NULL)
416 		*osigacthandler = udp->sigacthandler;
417 
418 	udp->sigacthandler = nsigacthandler;
419 }
420 
421 /*
422  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
423  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
424  * the unprotected (from signals) interval before reaching the kernel,
425  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
426  * the kernel to return immediately from these system calls, giving us
427  * a spurious wakeup but not a deadlock.
428  */
429 void
430 set_parking_flag(ulwp_t *self, int park)
431 {
432 	volatile sc_shared_t *scp;
433 
434 	enter_critical(self);
435 	if ((scp = self->ul_schedctl) != NULL ||
436 	    (scp = setup_schedctl()) != NULL)
437 		scp->sc_park = park;
438 	else if (park == 0)	/* schedctl failed, do it the long way */
439 		__lwp_unpark(self->ul_lwpid);
440 	exit_critical(self);
441 }
442 
443 /*
444  * Tell the kernel to block all signals.
445  * Use the schedctl interface, or failing that, use __lwp_sigmask().
446  * This action can be rescinded only by making a system call that
447  * sets the signal mask:
448  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
449  *	__sigsuspend() or __pollsys().
450  * In particular, this action cannot be reversed by assigning
451  * scp->sc_sigblock = 0.  That would be a way to lose signals.
452  * See the definition of restore_signals(self).
453  */
454 void
455 block_all_signals(ulwp_t *self)
456 {
457 	volatile sc_shared_t *scp;
458 
459 	enter_critical(self);
460 	if ((scp = self->ul_schedctl) != NULL ||
461 	    (scp = setup_schedctl()) != NULL)
462 		scp->sc_sigblock = 1;
463 	else
464 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
465 	exit_critical(self);
466 }
467 
468 /*
469  * _private_setcontext has code that forcibly restores the curthread
470  * pointer in a context passed to the setcontext(2) syscall.
471  *
472  * Certain processes may need to disable this feature, so these routines
473  * provide the mechanism to do so.
474  *
475  * (As an example, branded 32-bit x86 processes may use %gs for their own
476  * purposes, so they need to be able to specify a %gs value to be restored
477  * on return from a signal handler via the passed ucontext_t.)
478  */
479 static int setcontext_enforcement = 1;
480 
481 void
482 set_setcontext_enforcement(int on)
483 {
484 	setcontext_enforcement = on;
485 }
486 
487 #pragma weak setcontext = _private_setcontext
488 #pragma weak _setcontext = _private_setcontext
489 int
490 _private_setcontext(const ucontext_t *ucp)
491 {
492 	ulwp_t *self = curthread;
493 	int ret;
494 	ucontext_t uc;
495 
496 	/*
497 	 * Returning from the main context (uc_link == NULL) causes
498 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
499 	 */
500 	if (ucp == NULL)
501 		_thr_exit(NULL);
502 	(void) _private_memcpy(&uc, ucp, sizeof (uc));
503 
504 	/*
505 	 * Restore previous signal mask and context link.
506 	 */
507 	if (uc.uc_flags & UC_SIGMASK) {
508 		block_all_signals(self);
509 		delete_reserved_signals(&uc.uc_sigmask);
510 		self->ul_sigmask = uc.uc_sigmask;
511 		if (self->ul_cursig) {
512 			/*
513 			 * We have a deferred signal present.
514 			 * The signal mask will be set when the
515 			 * signal is taken in take_deferred_signal().
516 			 */
517 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
518 			uc.uc_flags &= ~UC_SIGMASK;
519 		}
520 	}
521 	self->ul_siglink = uc.uc_link;
522 
523 	/*
524 	 * We don't know where this context structure has been.
525 	 * Preserve the curthread pointer, at least.
526 	 *
527 	 * Allow this feature to be disabled if a particular process
528 	 * requests it.
529 	 */
530 	if (setcontext_enforcement) {
531 #if defined(__sparc)
532 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
533 #elif defined(__amd64)
534 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
535 #elif defined(__i386)
536 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
537 #else
538 #error "none of __sparc, __amd64, __i386 defined"
539 #endif
540 	}
541 
542 	/*
543 	 * Make sure that if we return to a call to __lwp_park()
544 	 * or ___lwp_cond_wait() that it returns right away
545 	 * (giving us a spurious wakeup but not a deadlock).
546 	 */
547 	set_parking_flag(self, 0);
548 	self->ul_sp = 0;
549 	ret = __setcontext_syscall(&uc);
550 
551 	/*
552 	 * It is OK for setcontext() to return if the user has not specified
553 	 * UC_CPU.
554 	 */
555 	if (uc.uc_flags & UC_CPU)
556 		thr_panic("setcontext(): __setcontext() returned");
557 	return (ret);
558 }
559 
560 #pragma weak thr_sigsetmask = _thr_sigsetmask
561 #pragma weak pthread_sigmask = _thr_sigsetmask
562 #pragma weak _pthread_sigmask = _thr_sigsetmask
563 int
564 _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
565 {
566 	ulwp_t *self = curthread;
567 	sigset_t saveset;
568 
569 	if (set == NULL) {
570 		enter_critical(self);
571 		if (oset != NULL)
572 			*oset = self->ul_sigmask;
573 		exit_critical(self);
574 	} else {
575 		switch (how) {
576 		case SIG_BLOCK:
577 		case SIG_UNBLOCK:
578 		case SIG_SETMASK:
579 			break;
580 		default:
581 			return (EINVAL);
582 		}
583 
584 		/*
585 		 * The assignments to self->ul_sigmask must be protected from
586 		 * signals.  The nuances of this code are subtle.  Be careful.
587 		 */
588 		block_all_signals(self);
589 		if (oset != NULL)
590 			saveset = self->ul_sigmask;
591 		switch (how) {
592 		case SIG_BLOCK:
593 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
594 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
595 			break;
596 		case SIG_UNBLOCK:
597 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
598 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
599 			break;
600 		case SIG_SETMASK:
601 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
602 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
603 			break;
604 		}
605 		delete_reserved_signals(&self->ul_sigmask);
606 		if (oset != NULL)
607 			*oset = saveset;
608 		restore_signals(self);
609 	}
610 
611 	return (0);
612 }
613 
614 #pragma weak sigprocmask = _sigprocmask
615 int
616 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
617 {
618 	int error;
619 
620 	/*
621 	 * Guard against children of vfork().
622 	 */
623 	if (curthread->ul_vfork)
624 		return (__lwp_sigmask(how, set, oset));
625 
626 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
627 		errno = error;
628 		return (-1);
629 	}
630 
631 	return (0);
632 }
633 
634 /*
635  * Called at library initialization to set up signal handling.
636  * All we really do is initialize the sig_lock mutexes.
637  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
638  * However, if any signal handlers were established on alternate
639  * link maps before the primary link map has been initialized,
640  * then inform the kernel of the new sigacthandler.
641  */
642 void
643 signal_init()
644 {
645 	uberdata_t *udp = curthread->ul_uberdata;
646 	struct sigaction *sap;
647 	struct sigaction act;
648 	int sig;
649 
650 	for (sig = 0; sig < NSIG; sig++) {
651 		udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC;
652 		sap = &udp->siguaction[sig].sig_uaction;
653 		if (sap->sa_sigaction != SIG_DFL &&
654 		    sap->sa_sigaction != SIG_IGN &&
655 		    __sigaction(sig, NULL, &act) == 0 &&
656 		    act.sa_sigaction != SIG_DFL &&
657 		    act.sa_sigaction != SIG_IGN) {
658 			act = *sap;
659 			act.sa_flags &= ~SA_NODEFER;
660 			act.sa_sigaction = udp->sigacthandler;
661 			act.sa_mask = maskset;
662 			(void) __sigaction(sig, &act, NULL);
663 		}
664 	}
665 }
666 
667 /*
668  * Common code for cancelling self in _sigcancel() and pthread_cancel().
669  * If the thread is at a cancellation point (ul_cancelable) then just
670  * return and let _canceloff() do the exit, else exit immediately if
671  * async mode is in effect.
672  */
673 void
674 do_sigcancel()
675 {
676 	ulwp_t *self = curthread;
677 
678 	ASSERT(self->ul_critical == 0);
679 	ASSERT(self->ul_sigdefer == 0);
680 	self->ul_cancel_pending = 1;
681 	if (self->ul_cancel_async &&
682 	    !self->ul_cancel_disabled &&
683 	    !self->ul_cancelable)
684 		_pthread_exit(PTHREAD_CANCELED);
685 }
686 
687 /*
688  * Set up the SIGCANCEL handler for threads cancellation,
689  * needed only when we have more than one thread,
690  * or the SIGAIOCANCEL handler for aio cancellation,
691  * called when aio is initialized, in __uaio_init().
692  */
693 void
694 setup_cancelsig(int sig)
695 {
696 	uberdata_t *udp = curthread->ul_uberdata;
697 	mutex_t *mp = &udp->siguaction[sig].sig_lock;
698 	struct sigaction act;
699 
700 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
701 	lmutex_lock(mp);
702 	act = udp->siguaction[sig].sig_uaction;
703 	lmutex_unlock(mp);
704 	if (act.sa_sigaction == SIG_DFL ||
705 	    act.sa_sigaction == SIG_IGN)
706 		act.sa_flags = SA_SIGINFO;
707 	else {
708 		act.sa_flags |= SA_SIGINFO;
709 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
710 	}
711 	act.sa_sigaction = udp->sigacthandler;
712 	act.sa_mask = maskset;
713 	(void) __sigaction(sig, &act, NULL);
714 }
715