xref: /titanic_51/usr/src/cmd/csh/i386/signal.c (revision bc0e91320069f0bcaee43e80a7ea686d9efa2d08)
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 = (int) ucp->uc_mcontext.gregs[UESP];
807c478bd9Sstevel@tonic-gate 	sc.sc_pc = (int) ucp->uc_mcontext.gregs[EIP];
817c478bd9Sstevel@tonic-gate 	sc.sc_ps = (int) ucp->uc_mcontext.gregs[EFL];
827c478bd9Sstevel@tonic-gate 	sc.sc_eax = (int) ucp->uc_mcontext.gregs[EAX];
837c478bd9Sstevel@tonic-gate 	sc.sc_edx = (int) ucp->uc_mcontext.gregs[EDX];
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * Machine dependent code ends
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (sip != NULL)
907c478bd9Sstevel@tonic-gate 		if ((code = sip->si_code) == BUS_OBJERR)
917c478bd9Sstevel@tonic-gate 			code = SEGV_MAKE_ERR(sip->si_errno);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS)
947c478bd9Sstevel@tonic-gate 		if (sip != NULL)
957c478bd9Sstevel@tonic-gate 			addr = (char *)sip->si_addr;
967c478bd9Sstevel@tonic-gate 	else
977c478bd9Sstevel@tonic-gate 		addr = SIG_NOADDR;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	(*_siguhandler[sig])(sig, code, &sc, addr);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (sc.sc_onstack)
1027c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
1037c478bd9Sstevel@tonic-gate 	else
1047c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
1057c478bd9Sstevel@tonic-gate 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Machine dependent code begins
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (int) sc.sc_sp;
1117c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (int) sc.sc_pc;
1127c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = (int) sc.sc_ps;
1137c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = (int) sc.sc_eax;
1147c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = (int) sc.sc_edx;
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * Machine dependent code ends
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	setcontext (ucp);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1226c02b4a4Smuffin int
1236c02b4a4Smuffin sigsetmask(int mask)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	sigset_t oset;
1267c478bd9Sstevel@tonic-gate 	sigset_t nset;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1297c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
1307c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
1317c478bd9Sstevel@tonic-gate 	return set2mask(&oset);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1346c02b4a4Smuffin int
1356c02b4a4Smuffin sigblock(int mask)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	sigset_t oset;
1387c478bd9Sstevel@tonic-gate 	sigset_t nset;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1417c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
1427c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
1437c478bd9Sstevel@tonic-gate 	return set2mask(&oset);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1466c02b4a4Smuffin int
1476c02b4a4Smuffin sigpause(int mask)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	sigset_t set;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &set);
1527c478bd9Sstevel@tonic-gate 	mask2set(mask, &set);
1537c478bd9Sstevel@tonic-gate 	return (sigsuspend(&set));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1566c02b4a4Smuffin int
1576c02b4a4Smuffin sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate         struct sigaction nact;
1607c478bd9Sstevel@tonic-gate         struct sigaction oact;
1617c478bd9Sstevel@tonic-gate         struct sigaction *nactp;
1627c478bd9Sstevel@tonic-gate         void (*ohandler)(), (*nhandler)();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate         if (sig <= 0 || sig >= NSIG) {
1657c478bd9Sstevel@tonic-gate                 errno = EINVAL;
1667c478bd9Sstevel@tonic-gate                 return -1;
1677c478bd9Sstevel@tonic-gate         }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate         ohandler = _siguhandler[sig];
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate         if (nvec) {
1727c478bd9Sstevel@tonic-gate 		_sigaction(sig, (struct sigaction *)0, &nact);
1737c478bd9Sstevel@tonic-gate                 nhandler = nvec->sv_handler;
1747c478bd9Sstevel@tonic-gate                 _siguhandler[sig] = nhandler;
1757c478bd9Sstevel@tonic-gate                 if (nhandler != SIG_DFL && nhandler != SIG_IGN)
1767c478bd9Sstevel@tonic-gate                         nact.sa_handler = (void (*)())sigvechandler;
1777c478bd9Sstevel@tonic-gate 		else
1787c478bd9Sstevel@tonic-gate 			nact.sa_handler = nhandler;
1797c478bd9Sstevel@tonic-gate 		mask2set(nvec->sv_mask, &nact.sa_mask);
1807c478bd9Sstevel@tonic-gate 		/*
1817c478bd9Sstevel@tonic-gate 		if ( sig == SIGTSTP || sig == SIGSTOP )
1827c478bd9Sstevel@tonic-gate 			nact.sa_handler = SIG_DFL; 	*/
1837c478bd9Sstevel@tonic-gate 		nact.sa_flags = SA_SIGINFO;
1847c478bd9Sstevel@tonic-gate 		if (!(nvec->sv_flags & SV_INTERRUPT))
1857c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
1867c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_RESETHAND)
1877c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESETHAND;
1887c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_ONSTACK)
1897c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_ONSTACK;
1907c478bd9Sstevel@tonic-gate 		nactp = &nact;
1917c478bd9Sstevel@tonic-gate         } else
1927c478bd9Sstevel@tonic-gate 		nactp = (struct sigaction *)0;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate         if (_sigaction(sig, nactp, &oact) < 0) {
1957c478bd9Sstevel@tonic-gate                 _siguhandler[sig] = ohandler;
1967c478bd9Sstevel@tonic-gate                 return -1;
1977c478bd9Sstevel@tonic-gate         }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate         if (ovec) {
2007c478bd9Sstevel@tonic-gate 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
2017c478bd9Sstevel@tonic-gate 			ovec->sv_handler = oact.sa_handler;
2027c478bd9Sstevel@tonic-gate 		else
2037c478bd9Sstevel@tonic-gate 			ovec->sv_handler = ohandler;
2047c478bd9Sstevel@tonic-gate 		ovec->sv_mask = set2mask(&oact.sa_mask);
2057c478bd9Sstevel@tonic-gate 		ovec->sv_flags = 0;
2067c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_ONSTACK)
2077c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_ONSTACK;
2087c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_RESETHAND)
2097c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_RESETHAND;
2107c478bd9Sstevel@tonic-gate 		if (!(oact.sa_flags & SA_RESTART))
2117c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_INTERRUPT;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate         return 0;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate void (*
2196c02b4a4Smuffin signal(int s, void (*a)()))()
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate         struct sigvec osv;
2227c478bd9Sstevel@tonic-gate 	struct sigvec nsv;
2237c478bd9Sstevel@tonic-gate         static int mask[NSIG];
2247c478bd9Sstevel@tonic-gate         static int flags[NSIG];
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	nsv.sv_handler = a;
2277c478bd9Sstevel@tonic-gate 	nsv.sv_mask = mask[s];
2287c478bd9Sstevel@tonic-gate 	nsv.sv_flags = flags[s];
2297c478bd9Sstevel@tonic-gate         if (sigvec(s, &nsv, &osv) < 0)
2307c478bd9Sstevel@tonic-gate                 return (SIG_ERR);
2317c478bd9Sstevel@tonic-gate         if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
2327c478bd9Sstevel@tonic-gate                 mask[s] = nsv.sv_mask = osv.sv_mask;
2337c478bd9Sstevel@tonic-gate                 flags[s] = nsv.sv_flags = osv.sv_flags & ~SV_RESETHAND;
2347c478bd9Sstevel@tonic-gate                 if (sigvec(s, &nsv, (struct sigvec *)0) < 0)
2357c478bd9Sstevel@tonic-gate                         return (SIG_ERR);
2367c478bd9Sstevel@tonic-gate         }
2377c478bd9Sstevel@tonic-gate         return (osv.sv_handler);
2387c478bd9Sstevel@tonic-gate }
239