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 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
29 */
30
31 #include "lint.h"
32 #include <sys/feature_tests.h>
33 /*
34 * setcontext() really can return, if UC_CPU is not specified.
35 * Make the compiler shut up about it.
36 */
37 #if defined(__NORETURN)
38 #undef __NORETURN
39 #endif
40 #define __NORETURN
41 #include "thr_uberdata.h"
42 #include "asyncio.h"
43 #include <signal.h>
44 #include <siginfo.h>
45 #include <sys/systm.h>
46
47 /* maskable signals */
48 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
49
50 /*
51 * Return true if the valid signal bits in both sets are the same.
52 */
53 int
sigequalset(const sigset_t * s1,const sigset_t * s2)54 sigequalset(const sigset_t *s1, const sigset_t *s2)
55 {
56 /*
57 * We only test valid signal bits, not rubbish following MAXSIG
58 * (for speed). Algorithm:
59 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
60 */
61 /* see lib/libc/inc/thr_uberdata.h for why this must be true */
62 #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32))
63 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
64 (s1->__sigbits[1] ^ s2->__sigbits[1]) |
65 ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2)));
66 #else
67 #error "fix me: MAXSIG out of bounds"
68 #endif
69 }
70
71 /*
72 * Common code for calling the user-specified signal handler.
73 */
74 void
call_user_handler(int sig,siginfo_t * sip,ucontext_t * ucp)75 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
76 {
77 ulwp_t *self = curthread;
78 uberdata_t *udp = self->ul_uberdata;
79 struct sigaction uact;
80 volatile struct sigaction *sap;
81
82 /*
83 * If we are taking a signal while parked or about to be parked
84 * on __lwp_park() then remove ourself from the sleep queue so
85 * that we can grab locks. The code in mutex_lock_queue() and
86 * cond_wait_common() will detect this and deal with it when
87 * __lwp_park() returns.
88 */
89 unsleep_self();
90 set_parking_flag(self, 0);
91
92 if (__td_event_report(self, TD_CATCHSIG, udp)) {
93 self->ul_td_evbuf.eventnum = TD_CATCHSIG;
94 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
95 tdb_event(TD_CATCHSIG, udp);
96 }
97
98 /*
99 * Get a self-consistent set of flags, handler, and mask
100 * while holding the sig's sig_lock for the least possible time.
101 * We must acquire the sig's sig_lock because some thread running
102 * in sigaction() might be establishing a new signal handler.
103 * The code in sigaction() acquires the writer lock; here
104 * we acquire the readers lock to ehance concurrency in the
105 * face of heavy signal traffic, such as generated by java.
106 *
107 * Locking exceptions:
108 * No locking for a child of vfork().
109 * If the signal is SIGPROF with an si_code of PROF_SIG,
110 * then we assume that this signal was generated by
111 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
112 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
113 * then we assume that the signal was generated by
114 * a hardware performance counter overflow.
115 * In these cases, assume that we need no locking. It is the
116 * monitoring program's responsibility to ensure correctness.
117 */
118 sap = &udp->siguaction[sig].sig_uaction;
119 if (self->ul_vfork ||
120 (sip != NULL &&
121 ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
122 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
123 /* we wish this assignment could be atomic */
124 (void) memcpy(&uact, (void *)sap, sizeof (uact));
125 } else {
126 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
127 lrw_rdlock(rwlp);
128 (void) memcpy(&uact, (void *)sap, sizeof (uact));
129 if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
130 (sap->sa_flags & SA_RESETHAND))
131 sap->sa_handler = SIG_DFL;
132 lrw_unlock(rwlp);
133 }
134
135 /*
136 * Set the proper signal mask and call the user's signal handler.
137 * (We overrode the user-requested signal mask with maskset
138 * so we currently have all blockable signals blocked.)
139 *
140 * We would like to ASSERT() that the signal is not a member of the
141 * signal mask at the previous level (ucp->uc_sigmask) or the specified
142 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
143 * /proc can override this via PCSSIG, so we don't bother.
144 *
145 * We would also like to ASSERT() that the signal mask at the previous
146 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
147 * but /proc can change the thread's signal mask via PCSHOLD, so we
148 * don't bother with that either.
149 */
150 ASSERT(ucp->uc_flags & UC_SIGMASK);
151 if (self->ul_sigsuspend) {
152 ucp->uc_sigmask = self->ul_sigmask;
153 self->ul_sigsuspend = 0;
154 /* the sigsuspend() or pollsys() signal mask */
155 sigorset(&uact.sa_mask, &self->ul_tmpmask);
156 } else {
157 /* the signal mask at the previous level */
158 sigorset(&uact.sa_mask, &ucp->uc_sigmask);
159 }
160 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */
161 (void) sigaddset(&uact.sa_mask, sig);
162 self->ul_sigmask = uact.sa_mask;
163 self->ul_siglink = ucp;
164 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask);
165
166 /*
167 * If this thread has been sent SIGCANCEL from the kernel
168 * or from pthread_cancel(), it is being asked to exit.
169 * The kernel may send SIGCANCEL without a siginfo struct.
170 * If the SIGCANCEL is process-directed (from kill() or
171 * sigqueue()), treat it as an ordinary signal.
172 */
173 if (sig == SIGCANCEL) {
174 if (sip == NULL || SI_FROMKERNEL(sip) ||
175 sip->si_code == SI_LWP) {
176 do_sigcancel();
177 goto out;
178 }
179 /* SIGCANCEL is ignored by default */
180 if (uact.sa_handler == SIG_DFL ||
181 uact.sa_handler == SIG_IGN)
182 goto out;
183 }
184
185 /*
186 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
187 * we are an aio worker thread, cancel the aio request.
188 */
189 if (sig == SIGAIOCANCEL) {
190 aio_worker_t *aiowp = pthread_getspecific(_aio_key);
191
192 if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
193 siglongjmp(aiowp->work_jmp_buf, 1);
194 /* SIGLWP is ignored by default */
195 if (uact.sa_handler == SIG_DFL ||
196 uact.sa_handler == SIG_IGN)
197 goto out;
198 }
199
200 if (!(uact.sa_flags & SA_SIGINFO))
201 sip = NULL;
202 __sighndlr(sig, sip, ucp, uact.sa_sigaction);
203
204 #if defined(sparc) || defined(__sparc)
205 /*
206 * If this is a floating point exception and the queue
207 * is non-empty, pop the top entry from the queue. This
208 * is to maintain expected behavior.
209 */
210 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
211 fpregset_t *fp = &ucp->uc_mcontext.fpregs;
212
213 if (--fp->fpu_qcnt > 0) {
214 unsigned char i;
215 struct _fq *fqp;
216
217 fqp = fp->fpu_q;
218 for (i = 0; i < fp->fpu_qcnt; i++)
219 fqp[i] = fqp[i+1];
220 }
221 }
222 #endif /* sparc */
223
224 out:
225 (void) setcontext(ucp);
226 thr_panic("call_user_handler(): setcontext() returned");
227 }
228
229 /*
230 * take_deferred_signal() is called when ul_critical and ul_sigdefer become
231 * zero and a deferred signal has been recorded on the current thread.
232 * We are out of the critical region and are ready to take a signal.
233 * The kernel has all signals blocked on this lwp, but our value of
234 * ul_sigmask is the correct signal mask for the previous context.
235 *
236 * We call __sigresend() to atomically restore the signal mask and
237 * cause the signal to be sent again with the remembered siginfo.
238 * We will not return successfully from __sigresend() until the
239 * application's signal handler has been run via sigacthandler().
240 */
241 void
take_deferred_signal(int sig)242 take_deferred_signal(int sig)
243 {
244 extern int __sigresend(int, siginfo_t *, sigset_t *);
245 ulwp_t *self = curthread;
246 siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
247 siginfo_t *sip;
248 int error;
249
250 ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
251
252 /*
253 * If the signal handler was established with SA_RESETHAND,
254 * the kernel has reset the handler to SIG_DFL, so we have
255 * to reestablish the handler now so that it will be entered
256 * again when we call __sigresend(), below.
257 *
258 * Logically, we should acquire and release the signal's
259 * sig_lock around this operation to protect the integrity
260 * of the signal action while we copy it, as is done below
261 * in _libc_sigaction(). However, we may be on a user-level
262 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
263 * might attempt to sleep on a different sleep queue and
264 * that would corrupt the entire sleep queue mechanism.
265 *
266 * If we are on a sleep queue we will remove ourself from
267 * it in call_user_handler(), called from sigacthandler(),
268 * before entering the application's signal handler.
269 * In the meantime, we must not acquire any locks.
270 */
271 if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
272 struct sigaction tact = suap->sig_uaction;
273 tact.sa_flags &= ~SA_NODEFER;
274 tact.sa_sigaction = self->ul_uberdata->sigacthandler;
275 tact.sa_mask = maskset;
276 (void) __sigaction(sig, &tact, NULL);
277 }
278
279 if (self->ul_siginfo.si_signo == 0)
280 sip = NULL;
281 else
282 sip = &self->ul_siginfo;
283
284 /* EAGAIN can happen only for a pending SIGSTOP signal */
285 while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
286 continue;
287 if (error)
288 thr_panic("take_deferred_signal(): __sigresend() failed");
289 }
290
291 void
sigacthandler(int sig,siginfo_t * sip,void * uvp)292 sigacthandler(int sig, siginfo_t *sip, void *uvp)
293 {
294 ucontext_t *ucp = uvp;
295 ulwp_t *self = curthread;
296
297 /*
298 * Do this in case we took a signal while in a cancelable system call.
299 * It does no harm if we were not in such a system call.
300 */
301 self->ul_sp = 0;
302 if (sig != SIGCANCEL)
303 self->ul_cancel_async = self->ul_save_async;
304
305 /*
306 * If this thread has performed a longjmp() from a signal handler
307 * back to main level some time in the past, it has left the kernel
308 * thinking that it is still in the signal context. We repair this
309 * possible damage by setting ucp->uc_link to NULL if we know that
310 * we are actually executing at main level (self->ul_siglink == NULL).
311 * See the code for setjmp()/longjmp() for more details.
312 */
313 if (self->ul_siglink == NULL)
314 ucp->uc_link = NULL;
315
316 /*
317 * If we are not in a critical region and are
318 * not deferring signals, take the signal now.
319 */
320 if ((self->ul_critical + self->ul_sigdefer) == 0) {
321 call_user_handler(sig, sip, ucp);
322 /*
323 * On the surface, the following call seems redundant
324 * because call_user_handler() cannot return. However,
325 * we don't want to return from here because the compiler
326 * might recycle our frame. We want to keep it on the
327 * stack to assist debuggers such as pstack in identifying
328 * signal frames. The call to thr_panic() serves to prevent
329 * tail-call optimisation here.
330 */
331 thr_panic("sigacthandler(): call_user_handler() returned");
332 }
333
334 /*
335 * We are in a critical region or we are deferring signals. When
336 * we emerge from the region we will call take_deferred_signal().
337 */
338 ASSERT(self->ul_cursig == 0);
339 self->ul_cursig = (char)sig;
340 if (sip != NULL)
341 (void) memcpy(&self->ul_siginfo,
342 sip, sizeof (siginfo_t));
343 else
344 self->ul_siginfo.si_signo = 0;
345
346 /*
347 * Make sure that if we return to a call to __lwp_park()
348 * or ___lwp_cond_wait() that it returns right away
349 * (giving us a spurious wakeup but not a deadlock).
350 */
351 set_parking_flag(self, 0);
352
353 /*
354 * Return to the previous context with all signals blocked.
355 * We will restore the signal mask in take_deferred_signal().
356 * Note that we are calling the system call trap here, not
357 * the setcontext() wrapper. We don't want to change the
358 * thread's ul_sigmask by this operation.
359 */
360 ucp->uc_sigmask = maskset;
361 (void) __setcontext(ucp);
362 thr_panic("sigacthandler(): __setcontext() returned");
363 }
364
365 #pragma weak _sigaction = sigaction
366 int
sigaction(int sig,const struct sigaction * nact,struct sigaction * oact)367 sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
368 {
369 ulwp_t *self = curthread;
370 uberdata_t *udp = self->ul_uberdata;
371 struct sigaction oaction;
372 struct sigaction tact;
373 struct sigaction *tactp = NULL;
374 int rv;
375
376 if (sig <= 0 || sig >= NSIG) {
377 errno = EINVAL;
378 return (-1);
379 }
380
381 if (!self->ul_vfork)
382 lrw_wrlock(&udp->siguaction[sig].sig_lock);
383
384 oaction = udp->siguaction[sig].sig_uaction;
385
386 if (nact != NULL) {
387 tact = *nact; /* make a copy so we can modify it */
388 tactp = &tact;
389 delete_reserved_signals(&tact.sa_mask);
390
391 #if !defined(_LP64)
392 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */
393 #endif
394 /*
395 * To be compatible with the behavior of SunOS 4.x:
396 * If the new signal handler is SIG_IGN or SIG_DFL, do
397 * not change the signal's entry in the siguaction array.
398 * This allows a child of vfork(2) to set signal handlers
399 * to SIG_IGN or SIG_DFL without affecting the parent.
400 *
401 * This also covers a race condition with some thread
402 * setting the signal action to SIG_DFL or SIG_IGN
403 * when the thread has also received and deferred
404 * that signal. When the thread takes the deferred
405 * signal, even though it has set the action to SIG_DFL
406 * or SIG_IGN, it will execute the old signal handler
407 * anyway. This is an inherent signaling race condition
408 * and is not a bug.
409 *
410 * A child of vfork() is not allowed to change signal
411 * handlers to anything other than SIG_DFL or SIG_IGN.
412 */
413 if (self->ul_vfork) {
414 if (tact.sa_handler != SIG_IGN)
415 tact.sa_handler = SIG_DFL;
416 } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
417 /*
418 * Always catch these signals.
419 * We need SIGCANCEL for pthread_cancel() to work.
420 * We need SIGAIOCANCEL for aio_cancel() to work.
421 */
422 udp->siguaction[sig].sig_uaction = tact;
423 if (tact.sa_handler == SIG_DFL ||
424 tact.sa_handler == SIG_IGN)
425 tact.sa_flags = SA_SIGINFO;
426 else {
427 tact.sa_flags |= SA_SIGINFO;
428 tact.sa_flags &=
429 ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
430 }
431 tact.sa_sigaction = udp->sigacthandler;
432 tact.sa_mask = maskset;
433 } else if (tact.sa_handler != SIG_DFL &&
434 tact.sa_handler != SIG_IGN) {
435 udp->siguaction[sig].sig_uaction = tact;
436 tact.sa_flags &= ~SA_NODEFER;
437 tact.sa_sigaction = udp->sigacthandler;
438 tact.sa_mask = maskset;
439 }
440 }
441
442 if ((rv = __sigaction(sig, tactp, oact)) != 0)
443 udp->siguaction[sig].sig_uaction = oaction;
444 else if (oact != NULL &&
445 oact->sa_handler != SIG_DFL &&
446 oact->sa_handler != SIG_IGN)
447 *oact = oaction;
448
449 /*
450 * We detect setting the disposition of SIGIO just to set the
451 * _sigio_enabled flag for the asynchronous i/o (aio) code.
452 */
453 if (sig == SIGIO && rv == 0 && tactp != NULL) {
454 _sigio_enabled =
455 (tactp->sa_handler != SIG_DFL &&
456 tactp->sa_handler != SIG_IGN);
457 }
458
459 if (!self->ul_vfork)
460 lrw_unlock(&udp->siguaction[sig].sig_lock);
461 return (rv);
462 }
463
464 /*
465 * This is a private interface for the linux brand interface.
466 */
467 void
setsigacthandler(void (* nsigacthandler)(int,siginfo_t *,void *),void (** osigacthandler)(int,siginfo_t *,void *))468 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
469 void (**osigacthandler)(int, siginfo_t *, void *))
470 {
471 ulwp_t *self = curthread;
472 uberdata_t *udp = self->ul_uberdata;
473
474 if (osigacthandler != NULL)
475 *osigacthandler = udp->sigacthandler;
476
477 udp->sigacthandler = nsigacthandler;
478 }
479
480 /*
481 * Tell the kernel to block all signals.
482 * Use the schedctl interface, or failing that, use __lwp_sigmask().
483 * This action can be rescinded only by making a system call that
484 * sets the signal mask:
485 * __lwp_sigmask(), __sigprocmask(), __setcontext(),
486 * __sigsuspend() or __pollsys().
487 * In particular, this action cannot be reversed by assigning
488 * scp->sc_sigblock = 0. That would be a way to lose signals.
489 * See the definition of restore_signals(self).
490 */
491 void
block_all_signals(ulwp_t * self)492 block_all_signals(ulwp_t *self)
493 {
494 volatile sc_shared_t *scp;
495
496 enter_critical(self);
497 if ((scp = self->ul_schedctl) != NULL ||
498 (scp = setup_schedctl()) != NULL)
499 scp->sc_sigblock = 1;
500 else
501 (void) __lwp_sigmask(SIG_SETMASK, &maskset);
502 exit_critical(self);
503 }
504
505 /*
506 * setcontext() has code that forcibly restores the curthread
507 * pointer in a context passed to the setcontext(2) syscall.
508 *
509 * Certain processes may need to disable this feature, so these routines
510 * provide the mechanism to do so.
511 *
512 * (As an example, branded 32-bit x86 processes may use %gs for their own
513 * purposes, so they need to be able to specify a %gs value to be restored
514 * on return from a signal handler via the passed ucontext_t.)
515 */
516 static int setcontext_enforcement = 1;
517
518 void
set_setcontext_enforcement(int on)519 set_setcontext_enforcement(int on)
520 {
521 setcontext_enforcement = on;
522 }
523
524 #pragma weak _setcontext = setcontext
525 int
setcontext(const ucontext_t * ucp)526 setcontext(const ucontext_t *ucp)
527 {
528 ulwp_t *self = curthread;
529 int ret;
530 ucontext_t uc;
531
532 /*
533 * Returning from the main context (uc_link == NULL) causes
534 * the thread to exit. See setcontext(2) and makecontext(3C).
535 */
536 if (ucp == NULL)
537 thr_exit(NULL);
538 (void) memcpy(&uc, ucp, sizeof (uc));
539
540 /*
541 * Restore previous signal mask and context link.
542 */
543 if (uc.uc_flags & UC_SIGMASK) {
544 block_all_signals(self);
545 delete_reserved_signals(&uc.uc_sigmask);
546 self->ul_sigmask = uc.uc_sigmask;
547 if (self->ul_cursig) {
548 /*
549 * We have a deferred signal present.
550 * The signal mask will be set when the
551 * signal is taken in take_deferred_signal().
552 */
553 ASSERT(self->ul_critical + self->ul_sigdefer != 0);
554 uc.uc_flags &= ~UC_SIGMASK;
555 }
556 }
557 self->ul_siglink = uc.uc_link;
558
559 /*
560 * We don't know where this context structure has been.
561 * Preserve the curthread pointer, at least.
562 *
563 * Allow this feature to be disabled if a particular process
564 * requests it.
565 */
566 if (setcontext_enforcement) {
567 #if defined(__sparc)
568 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
569 #elif defined(__amd64)
570 /*
571 * 64-bit processes must have a selector value of zero for %fs
572 * in order to use the 64-bit fs_base (the full 64-bit address
573 * range cannot be expressed in a long mode descriptor).
574 */
575 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0;
576 uc.uc_mcontext.gregs[REG_FSBASE] = (greg_t)self;
577 #elif defined(__i386)
578 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
579 #else
580 #error "none of __sparc, __amd64, __i386 defined"
581 #endif
582 }
583
584 /*
585 * Make sure that if we return to a call to __lwp_park()
586 * or ___lwp_cond_wait() that it returns right away
587 * (giving us a spurious wakeup but not a deadlock).
588 */
589 set_parking_flag(self, 0);
590 self->ul_sp = 0;
591 ret = __setcontext(&uc);
592
593 /*
594 * It is OK for setcontext() to return if the user has not specified
595 * UC_CPU.
596 */
597 if (uc.uc_flags & UC_CPU)
598 thr_panic("setcontext(): __setcontext() returned");
599 return (ret);
600 }
601
602 #pragma weak _thr_sigsetmask = thr_sigsetmask
603 int
thr_sigsetmask(int how,const sigset_t * set,sigset_t * oset)604 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
605 {
606 ulwp_t *self = curthread;
607 sigset_t saveset;
608
609 if (set == NULL) {
610 enter_critical(self);
611 if (oset != NULL)
612 *oset = self->ul_sigmask;
613 exit_critical(self);
614 } else {
615 switch (how) {
616 case SIG_BLOCK:
617 case SIG_UNBLOCK:
618 case SIG_SETMASK:
619 break;
620 default:
621 return (EINVAL);
622 }
623
624 /*
625 * The assignments to self->ul_sigmask must be protected from
626 * signals. The nuances of this code are subtle. Be careful.
627 */
628 block_all_signals(self);
629 if (oset != NULL)
630 saveset = self->ul_sigmask;
631 switch (how) {
632 case SIG_BLOCK:
633 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
634 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
635 self->ul_sigmask.__sigbits[2] |= set->__sigbits[2];
636 self->ul_sigmask.__sigbits[3] |= set->__sigbits[3];
637 break;
638 case SIG_UNBLOCK:
639 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
640 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
641 self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2];
642 self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3];
643 break;
644 case SIG_SETMASK:
645 self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
646 self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
647 self->ul_sigmask.__sigbits[2] = set->__sigbits[2];
648 self->ul_sigmask.__sigbits[3] = set->__sigbits[3];
649 break;
650 }
651 delete_reserved_signals(&self->ul_sigmask);
652 if (oset != NULL)
653 *oset = saveset;
654 restore_signals(self);
655 }
656
657 return (0);
658 }
659
660 #pragma weak _pthread_sigmask = pthread_sigmask
661 int
pthread_sigmask(int how,const sigset_t * set,sigset_t * oset)662 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
663 {
664 return (thr_sigsetmask(how, set, oset));
665 }
666
667 #pragma weak _sigprocmask = sigprocmask
668 int
sigprocmask(int how,const sigset_t * set,sigset_t * oset)669 sigprocmask(int how, const sigset_t *set, sigset_t *oset)
670 {
671 int error;
672
673 /*
674 * Guard against children of vfork().
675 */
676 if (curthread->ul_vfork)
677 return (__sigprocmask(how, set, oset));
678
679 if ((error = thr_sigsetmask(how, set, oset)) != 0) {
680 errno = error;
681 return (-1);
682 }
683
684 return (0);
685 }
686
687 /*
688 * Called at library initialization to set up signal handling.
689 * All we really do is initialize the sig_lock rwlocks.
690 * All signal handlers are either SIG_DFL or SIG_IGN on exec().
691 * However, if any signal handlers were established on alternate
692 * link maps before the primary link map has been initialized,
693 * then inform the kernel of the new sigacthandler.
694 */
695 void
signal_init()696 signal_init()
697 {
698 uberdata_t *udp = curthread->ul_uberdata;
699 struct sigaction *sap;
700 struct sigaction act;
701 rwlock_t *rwlp;
702 int sig;
703
704 for (sig = 0; sig < NSIG; sig++) {
705 rwlp = &udp->siguaction[sig].sig_lock;
706 rwlp->rwlock_magic = RWL_MAGIC;
707 rwlp->mutex.mutex_flag = LOCK_INITED;
708 rwlp->mutex.mutex_magic = MUTEX_MAGIC;
709 sap = &udp->siguaction[sig].sig_uaction;
710 if (sap->sa_handler != SIG_DFL &&
711 sap->sa_handler != SIG_IGN &&
712 __sigaction(sig, NULL, &act) == 0 &&
713 act.sa_handler != SIG_DFL &&
714 act.sa_handler != SIG_IGN) {
715 act = *sap;
716 act.sa_flags &= ~SA_NODEFER;
717 act.sa_sigaction = udp->sigacthandler;
718 act.sa_mask = maskset;
719 (void) __sigaction(sig, &act, NULL);
720 }
721 }
722 }
723
724 /*
725 * Common code for cancelling self in _sigcancel() and pthread_cancel().
726 * First record the fact that a cancellation is pending.
727 * Then, if cancellation is disabled or if we are holding unprotected
728 * libc locks, just return to defer the cancellation.
729 * Then, if we are at a cancellation point (ul_cancelable) just
730 * return and let _canceloff() do the exit.
731 * Else exit immediately if async mode is in effect.
732 */
733 void
do_sigcancel(void)734 do_sigcancel(void)
735 {
736 ulwp_t *self = curthread;
737
738 ASSERT(self->ul_critical == 0);
739 ASSERT(self->ul_sigdefer == 0);
740 self->ul_cancel_pending = 1;
741 if (self->ul_cancel_async &&
742 !self->ul_cancel_disabled &&
743 self->ul_libc_locks == 0 &&
744 !self->ul_cancelable)
745 pthread_exit(PTHREAD_CANCELED);
746 set_cancel_pending_flag(self, 0);
747 }
748
749 /*
750 * Set up the SIGCANCEL handler for threads cancellation,
751 * needed only when we have more than one thread,
752 * or the SIGAIOCANCEL handler for aio cancellation,
753 * called when aio is initialized, in __uaio_init().
754 */
755 void
setup_cancelsig(int sig)756 setup_cancelsig(int sig)
757 {
758 uberdata_t *udp = curthread->ul_uberdata;
759 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
760 struct sigaction act;
761
762 ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
763 lrw_rdlock(rwlp);
764 act = udp->siguaction[sig].sig_uaction;
765 lrw_unlock(rwlp);
766 if (act.sa_handler == SIG_DFL ||
767 act.sa_handler == SIG_IGN)
768 act.sa_flags = SA_SIGINFO;
769 else {
770 act.sa_flags |= SA_SIGINFO;
771 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
772 }
773 act.sa_sigaction = udp->sigacthandler;
774 act.sa_mask = maskset;
775 (void) __sigaction(sig, &act, NULL);
776 }
777