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