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