17c478bd9Sstevel@tonic-gate /* 26c02b4a4Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate /* 107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement 127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 137c478bd9Sstevel@tonic-gate */ 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate /* 167c478bd9Sstevel@tonic-gate * 4.3BSD signal compatibility functions 177c478bd9Sstevel@tonic-gate * 187c478bd9Sstevel@tonic-gate * the implementation interprets signal masks equal to -1 as "all of the 197c478bd9Sstevel@tonic-gate * signals in the signal set", thereby allowing signals with numbers 207c478bd9Sstevel@tonic-gate * above 32 to be blocked when referenced in code such as: 217c478bd9Sstevel@tonic-gate * 227c478bd9Sstevel@tonic-gate * for (i = 0; i < NSIG; i++) 237c478bd9Sstevel@tonic-gate * mask |= sigmask(i) 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <sys/siginfo.h> 28*bc0e9132SGordon Ross #include <ucontext.h> 297c478bd9Sstevel@tonic-gate #include <signal.h> 307c478bd9Sstevel@tonic-gate #include "signal.h" 317c478bd9Sstevel@tonic-gate #include <errno.h> 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #define set2mask(setp) ((setp)->__sigbits[0]) 357c478bd9Sstevel@tonic-gate #define mask2set(mask, setp) \ 367c478bd9Sstevel@tonic-gate ((mask) == -1 ? sigfillset(setp) : sigemptyset(setp), (((setp)->__sigbits[0]) = (mask))) 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate void (*_siguhandler[NSIG])() = { 0 }; 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * sigstack is emulated with sigaltstack by guessing an appropriate 427c478bd9Sstevel@tonic-gate * value for the stack size - on machines that have stacks that grow 437c478bd9Sstevel@tonic-gate * upwards, the ss_sp arguments for both functions mean the same thing, 447c478bd9Sstevel@tonic-gate * (the initial stack pointer sigstack() is also the stack base 457c478bd9Sstevel@tonic-gate * sigaltstack()), so a "very large" value should be chosen for the 467c478bd9Sstevel@tonic-gate * stack size - on machines that have stacks that grow downwards, the 477c478bd9Sstevel@tonic-gate * ss_sp arguments mean opposite things, so 0 should be used (hopefully 487c478bd9Sstevel@tonic-gate * these machines don't have hardware stack bounds registers that pay 497c478bd9Sstevel@tonic-gate * attention to sigaltstack()'s size argument. 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #ifdef sun 537c478bd9Sstevel@tonic-gate #define SIGSTACKSIZE 0 547c478bd9Sstevel@tonic-gate #endif 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * sigvechandler is the real signal handler installed for all 597c478bd9Sstevel@tonic-gate * signals handled in the 4.3BSD compatibility interface - it translates 607c478bd9Sstevel@tonic-gate * SVR4 signal hander arguments into 4.3BSD signal handler arguments 617c478bd9Sstevel@tonic-gate * and then calls the real handler 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static void 656c02b4a4Smuffin sigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp) 667c478bd9Sstevel@tonic-gate { 677c478bd9Sstevel@tonic-gate struct sigcontext sc; 687c478bd9Sstevel@tonic-gate int code; 697c478bd9Sstevel@tonic-gate char *addr; 706c02b4a4Smuffin int i, j; 717c478bd9Sstevel@tonic-gate int gwinswitch = 0; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0); 747c478bd9Sstevel@tonic-gate sc.sc_mask = set2mask(&ucp->uc_sigmask); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Machine dependent code begins 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate sc.sc_sp = ucp->uc_mcontext.gregs[REG_O6]; 807c478bd9Sstevel@tonic-gate sc.sc_pc = ucp->uc_mcontext.gregs[REG_PC]; 817c478bd9Sstevel@tonic-gate sc.sc_npc = ucp->uc_mcontext.gregs[REG_nPC]; 827c478bd9Sstevel@tonic-gate sc.sc_psr = ucp->uc_mcontext.gregs[REG_PSR]; 837c478bd9Sstevel@tonic-gate sc.sc_g1 = ucp->uc_mcontext.gregs[REG_G1]; 847c478bd9Sstevel@tonic-gate sc.sc_o0 = ucp->uc_mcontext.gregs[REG_O0]; 857c478bd9Sstevel@tonic-gate if (ucp->uc_mcontext.gwins != (gwindows_t *)0) { 867c478bd9Sstevel@tonic-gate gwinswitch = 1; 877c478bd9Sstevel@tonic-gate sc.sc_wbcnt = ucp->uc_mcontext.gwins->wbcnt; 887c478bd9Sstevel@tonic-gate for (i = 0; i < MAXWINDOW; i++) { 897c478bd9Sstevel@tonic-gate for (j = 0; j < 16; j++) 907c478bd9Sstevel@tonic-gate sc.sc_spbuf[i][j] = (int)ucp->uc_mcontext.gwins->spbuf[j]; 917c478bd9Sstevel@tonic-gate for (j = 0; j < 8; j++) 927c478bd9Sstevel@tonic-gate sc.sc_wbuf[i][j] = ucp->uc_mcontext.gwins->wbuf[i].rw_local[j]; 937c478bd9Sstevel@tonic-gate for (j = 0; j < 8; j++) 947c478bd9Sstevel@tonic-gate sc.sc_wbuf[i][j+8] = ucp->uc_mcontext.gwins->wbuf[i].rw_in[j]; 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Machine dependent code ends 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (sip != NULL) 1027c478bd9Sstevel@tonic-gate if ((code = sip->si_code) == BUS_OBJERR) 1037c478bd9Sstevel@tonic-gate code = SEGV_MAKE_ERR(sip->si_errno); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS) 1067c478bd9Sstevel@tonic-gate if (sip != NULL) 1077c478bd9Sstevel@tonic-gate addr = (char *)sip->si_addr; 1087c478bd9Sstevel@tonic-gate else 1097c478bd9Sstevel@tonic-gate addr = SIG_NOADDR; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate (*_siguhandler[sig])(sig, code, &sc, addr); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (sc.sc_onstack) 1147c478bd9Sstevel@tonic-gate ucp->uc_stack.ss_flags |= SS_ONSTACK; 1157c478bd9Sstevel@tonic-gate else 1167c478bd9Sstevel@tonic-gate ucp->uc_stack.ss_flags &= ~SS_ONSTACK; 1177c478bd9Sstevel@tonic-gate mask2set(sc.sc_mask, &ucp->uc_sigmask); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Machine dependent code begins 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_O6] = sc.sc_sp; 1237c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_PC] = sc.sc_pc; 1247c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_nPC] = sc.sc_npc; 1257c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_PSR] = sc.sc_psr; 1267c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_G1] = sc.sc_g1; 1277c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_O0] = sc.sc_o0; 1287c478bd9Sstevel@tonic-gate if (gwinswitch == 1) { 1297c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gwins->wbcnt = sc.sc_wbcnt; 1307c478bd9Sstevel@tonic-gate for (i = 0; i < MAXWINDOW; i++) { 1317c478bd9Sstevel@tonic-gate for (j = 0; j < 16; j++) 1327c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gwins->spbuf[j] = (greg_t *)sc.sc_spbuf[i][j]; 1337c478bd9Sstevel@tonic-gate for (j = 0; j < 8; j++) 1347c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gwins->wbuf[i].rw_local[j] = sc.sc_wbuf[i][j]; 1357c478bd9Sstevel@tonic-gate for (j = 0; j < 8; j++) 1367c478bd9Sstevel@tonic-gate ucp->uc_mcontext.gwins->wbuf[i].rw_in[j] = sc.sc_wbuf[i][j+8]; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * Machine dependent code ends 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate setcontext (ucp); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1466c02b4a4Smuffin int 1476c02b4a4Smuffin sigsetmask(int mask) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate sigset_t oset; 1507c478bd9Sstevel@tonic-gate sigset_t nset; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &nset); 1537c478bd9Sstevel@tonic-gate mask2set(mask, &nset); 1547c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &nset, &oset); 1557c478bd9Sstevel@tonic-gate return set2mask(&oset); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1586c02b4a4Smuffin int 1596c02b4a4Smuffin sigblock(int mask) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate sigset_t oset; 1627c478bd9Sstevel@tonic-gate sigset_t nset; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &nset); 1657c478bd9Sstevel@tonic-gate mask2set(mask, &nset); 1667c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &nset, &oset); 1677c478bd9Sstevel@tonic-gate return set2mask(&oset); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1706c02b4a4Smuffin int 1716c02b4a4Smuffin sigpause(int mask) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate sigset_t set; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &set); 1767c478bd9Sstevel@tonic-gate mask2set(mask, &set); 1777c478bd9Sstevel@tonic-gate return (sigsuspend(&set)); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1806c02b4a4Smuffin int 1816c02b4a4Smuffin sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate struct sigaction nact; 1847c478bd9Sstevel@tonic-gate struct sigaction oact; 1857c478bd9Sstevel@tonic-gate struct sigaction *nactp; 1867c478bd9Sstevel@tonic-gate void (*ohandler)(), (*nhandler)(); 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate if (sig <= 0 || sig >= NSIG) { 1897c478bd9Sstevel@tonic-gate errno = EINVAL; 1907c478bd9Sstevel@tonic-gate return -1; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate ohandler = _siguhandler[sig]; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (nvec) { 1967c478bd9Sstevel@tonic-gate _sigaction(sig, (struct sigaction *)0, &nact); 1977c478bd9Sstevel@tonic-gate nhandler = nvec->sv_handler; 1987c478bd9Sstevel@tonic-gate _siguhandler[sig] = nhandler; 1997c478bd9Sstevel@tonic-gate if (nhandler != SIG_DFL && nhandler != SIG_IGN) 2007c478bd9Sstevel@tonic-gate nact.sa_handler = (void (*)())sigvechandler; 2017c478bd9Sstevel@tonic-gate else 2027c478bd9Sstevel@tonic-gate nact.sa_handler = nhandler; 2037c478bd9Sstevel@tonic-gate mask2set(nvec->sv_mask, &nact.sa_mask); 2047c478bd9Sstevel@tonic-gate /* 2057c478bd9Sstevel@tonic-gate if ( sig == SIGTSTP || sig == SIGSTOP ) 2067c478bd9Sstevel@tonic-gate nact.sa_handler = SIG_DFL; */ 2077c478bd9Sstevel@tonic-gate nact.sa_flags = SA_SIGINFO; 2087c478bd9Sstevel@tonic-gate if (!(nvec->sv_flags & SV_INTERRUPT)) 2097c478bd9Sstevel@tonic-gate nact.sa_flags |= SA_RESTART; 2107c478bd9Sstevel@tonic-gate if (nvec->sv_flags & SV_RESETHAND) 2117c478bd9Sstevel@tonic-gate nact.sa_flags |= SA_RESETHAND; 2127c478bd9Sstevel@tonic-gate if (nvec->sv_flags & SV_ONSTACK) 2137c478bd9Sstevel@tonic-gate nact.sa_flags |= SA_ONSTACK; 2147c478bd9Sstevel@tonic-gate nactp = &nact; 2157c478bd9Sstevel@tonic-gate } else 2167c478bd9Sstevel@tonic-gate nactp = (struct sigaction *)0; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (_sigaction(sig, nactp, &oact) < 0) { 2197c478bd9Sstevel@tonic-gate _siguhandler[sig] = ohandler; 2207c478bd9Sstevel@tonic-gate return -1; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (ovec) { 2247c478bd9Sstevel@tonic-gate if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN) 2257c478bd9Sstevel@tonic-gate ovec->sv_handler = oact.sa_handler; 2267c478bd9Sstevel@tonic-gate else 2277c478bd9Sstevel@tonic-gate ovec->sv_handler = ohandler; 2287c478bd9Sstevel@tonic-gate ovec->sv_mask = set2mask(&oact.sa_mask); 2297c478bd9Sstevel@tonic-gate ovec->sv_flags = 0; 2307c478bd9Sstevel@tonic-gate if (oact.sa_flags & SA_ONSTACK) 2317c478bd9Sstevel@tonic-gate ovec->sv_flags |= SV_ONSTACK; 2327c478bd9Sstevel@tonic-gate if (oact.sa_flags & SA_RESETHAND) 2337c478bd9Sstevel@tonic-gate ovec->sv_flags |= SV_RESETHAND; 2347c478bd9Sstevel@tonic-gate if (!(oact.sa_flags & SA_RESTART)) 2357c478bd9Sstevel@tonic-gate ovec->sv_flags |= SV_INTERRUPT; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate return 0; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate void (* 2436c02b4a4Smuffin signal(int s, void (*a)()))() 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate struct sigvec osv; 2467c478bd9Sstevel@tonic-gate struct sigvec nsv; 2477c478bd9Sstevel@tonic-gate static int mask[NSIG]; 2487c478bd9Sstevel@tonic-gate static int flags[NSIG]; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate nsv.sv_handler = a; 2517c478bd9Sstevel@tonic-gate nsv.sv_mask = mask[s]; 2527c478bd9Sstevel@tonic-gate nsv.sv_flags = flags[s]; 2537c478bd9Sstevel@tonic-gate if (sigvec(s, &nsv, &osv) < 0) 2547c478bd9Sstevel@tonic-gate return (SIG_ERR); 2557c478bd9Sstevel@tonic-gate if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) { 2567c478bd9Sstevel@tonic-gate mask[s] = nsv.sv_mask = osv.sv_mask; 2577c478bd9Sstevel@tonic-gate flags[s] = nsv.sv_flags = osv.sv_flags & ~SV_RESETHAND; 2587c478bd9Sstevel@tonic-gate if (sigvec(s, &nsv, (struct sigvec *)0) < 0) 2597c478bd9Sstevel@tonic-gate return (SIG_ERR); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate return (osv.sv_handler); 2627c478bd9Sstevel@tonic-gate } 263