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