xref: /titanic_53/usr/src/cmd/csh/i386/signal.c (revision 6c02b4a4b46fecc2fa6bf1ab6b5e3255ad1d0767)
17c478bd9Sstevel@tonic-gate /*
2*6c02b4a4Smuffin  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate /*
187c478bd9Sstevel@tonic-gate  * 4.3BSD signal compatibility functions
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * the implementation interprets signal masks equal to -1 as "all of the
217c478bd9Sstevel@tonic-gate  * signals in the signal set", thereby allowing signals with numbers
227c478bd9Sstevel@tonic-gate  * above 32 to be blocked when referenced in code such as:
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  *	for (i = 0; i < NSIG; i++)
257c478bd9Sstevel@tonic-gate  *		mask |= sigmask(i)
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
307c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include "signal.h"
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define set2mask(setp) ((setp)->__sigbits[0])
377c478bd9Sstevel@tonic-gate #define mask2set(mask, setp) \
387c478bd9Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : sigemptyset(setp), (((setp)->__sigbits[0]) = (mask)))
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate void (*_siguhandler[NSIG])() = { 0 };
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * sigstack is emulated with sigaltstack by guessing an appropriate
447c478bd9Sstevel@tonic-gate  * value for the stack size - on machines that have stacks that grow
457c478bd9Sstevel@tonic-gate  * upwards, the ss_sp arguments for both functions mean the same thing,
467c478bd9Sstevel@tonic-gate  * (the initial stack pointer sigstack() is also the stack base
477c478bd9Sstevel@tonic-gate  * sigaltstack()), so a "very large" value should be chosen for the
487c478bd9Sstevel@tonic-gate  * stack size - on machines that have stacks that grow downwards, the
497c478bd9Sstevel@tonic-gate  * ss_sp arguments mean opposite things, so 0 should be used (hopefully
507c478bd9Sstevel@tonic-gate  * these machines don't have hardware stack bounds registers that pay
517c478bd9Sstevel@tonic-gate  * attention to sigaltstack()'s size argument.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef sun
557c478bd9Sstevel@tonic-gate #define SIGSTACKSIZE	0
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * sigvechandler is the real signal handler installed for all
617c478bd9Sstevel@tonic-gate  * signals handled in the 4.3BSD compatibility interface - it translates
627c478bd9Sstevel@tonic-gate  * SVR4 signal hander arguments into 4.3BSD signal handler arguments
637c478bd9Sstevel@tonic-gate  * and then calls the real handler
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static void
67*6c02b4a4Smuffin sigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	struct sigcontext sc;
707c478bd9Sstevel@tonic-gate 	int code;
717c478bd9Sstevel@tonic-gate 	char *addr;
72*6c02b4a4Smuffin 	int i, j;
737c478bd9Sstevel@tonic-gate 	int gwinswitch = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0);
767c478bd9Sstevel@tonic-gate 	sc.sc_mask = set2mask(&ucp->uc_sigmask);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/*
797c478bd9Sstevel@tonic-gate 	 * Machine dependent code begins
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	sc.sc_sp = (int) ucp->uc_mcontext.gregs[UESP];
827c478bd9Sstevel@tonic-gate 	sc.sc_pc = (int) ucp->uc_mcontext.gregs[EIP];
837c478bd9Sstevel@tonic-gate 	sc.sc_ps = (int) ucp->uc_mcontext.gregs[EFL];
847c478bd9Sstevel@tonic-gate 	sc.sc_eax = (int) ucp->uc_mcontext.gregs[EAX];
857c478bd9Sstevel@tonic-gate 	sc.sc_edx = (int) ucp->uc_mcontext.gregs[EDX];
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Machine dependent code ends
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (sip != NULL)
927c478bd9Sstevel@tonic-gate 		if ((code = sip->si_code) == BUS_OBJERR)
937c478bd9Sstevel@tonic-gate 			code = SEGV_MAKE_ERR(sip->si_errno);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS)
967c478bd9Sstevel@tonic-gate 		if (sip != NULL)
977c478bd9Sstevel@tonic-gate 			addr = (char *)sip->si_addr;
987c478bd9Sstevel@tonic-gate 	else
997c478bd9Sstevel@tonic-gate 		addr = SIG_NOADDR;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	(*_siguhandler[sig])(sig, code, &sc, addr);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (sc.sc_onstack)
1047c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
1057c478bd9Sstevel@tonic-gate 	else
1067c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
1077c478bd9Sstevel@tonic-gate 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * Machine dependent code begins
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (int) sc.sc_sp;
1137c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (int) sc.sc_pc;
1147c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = (int) sc.sc_ps;
1157c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = (int) sc.sc_eax;
1167c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = (int) sc.sc_edx;
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Machine dependent code ends
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	setcontext (ucp);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
124*6c02b4a4Smuffin int
125*6c02b4a4Smuffin sigsetmask(int mask)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	sigset_t oset;
1287c478bd9Sstevel@tonic-gate 	sigset_t nset;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1317c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
1327c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
1337c478bd9Sstevel@tonic-gate 	return set2mask(&oset);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
136*6c02b4a4Smuffin int
137*6c02b4a4Smuffin sigblock(int mask)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	sigset_t oset;
1407c478bd9Sstevel@tonic-gate 	sigset_t nset;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1437c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
1447c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
1457c478bd9Sstevel@tonic-gate 	return set2mask(&oset);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
148*6c02b4a4Smuffin int
149*6c02b4a4Smuffin sigpause(int mask)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	sigset_t set;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &set);
1547c478bd9Sstevel@tonic-gate 	mask2set(mask, &set);
1557c478bd9Sstevel@tonic-gate 	return (sigsuspend(&set));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
158*6c02b4a4Smuffin int
159*6c02b4a4Smuffin sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate         struct sigaction nact;
1627c478bd9Sstevel@tonic-gate         struct sigaction oact;
1637c478bd9Sstevel@tonic-gate         struct sigaction *nactp;
1647c478bd9Sstevel@tonic-gate         void (*ohandler)(), (*nhandler)();
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate         if (sig <= 0 || sig >= NSIG) {
1677c478bd9Sstevel@tonic-gate                 errno = EINVAL;
1687c478bd9Sstevel@tonic-gate                 return -1;
1697c478bd9Sstevel@tonic-gate         }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate         ohandler = _siguhandler[sig];
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate         if (nvec) {
1747c478bd9Sstevel@tonic-gate 		_sigaction(sig, (struct sigaction *)0, &nact);
1757c478bd9Sstevel@tonic-gate                 nhandler = nvec->sv_handler;
1767c478bd9Sstevel@tonic-gate                 _siguhandler[sig] = nhandler;
1777c478bd9Sstevel@tonic-gate                 if (nhandler != SIG_DFL && nhandler != SIG_IGN)
1787c478bd9Sstevel@tonic-gate                         nact.sa_handler = (void (*)())sigvechandler;
1797c478bd9Sstevel@tonic-gate 		else
1807c478bd9Sstevel@tonic-gate 			nact.sa_handler = nhandler;
1817c478bd9Sstevel@tonic-gate 		mask2set(nvec->sv_mask, &nact.sa_mask);
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		if ( sig == SIGTSTP || sig == SIGSTOP )
1847c478bd9Sstevel@tonic-gate 			nact.sa_handler = SIG_DFL; 	*/
1857c478bd9Sstevel@tonic-gate 		nact.sa_flags = SA_SIGINFO;
1867c478bd9Sstevel@tonic-gate 		if (!(nvec->sv_flags & SV_INTERRUPT))
1877c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
1887c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_RESETHAND)
1897c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESETHAND;
1907c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_ONSTACK)
1917c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_ONSTACK;
1927c478bd9Sstevel@tonic-gate 		nactp = &nact;
1937c478bd9Sstevel@tonic-gate         } else
1947c478bd9Sstevel@tonic-gate 		nactp = (struct sigaction *)0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate         if (_sigaction(sig, nactp, &oact) < 0) {
1977c478bd9Sstevel@tonic-gate                 _siguhandler[sig] = ohandler;
1987c478bd9Sstevel@tonic-gate                 return -1;
1997c478bd9Sstevel@tonic-gate         }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate         if (ovec) {
2027c478bd9Sstevel@tonic-gate 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
2037c478bd9Sstevel@tonic-gate 			ovec->sv_handler = oact.sa_handler;
2047c478bd9Sstevel@tonic-gate 		else
2057c478bd9Sstevel@tonic-gate 			ovec->sv_handler = ohandler;
2067c478bd9Sstevel@tonic-gate 		ovec->sv_mask = set2mask(&oact.sa_mask);
2077c478bd9Sstevel@tonic-gate 		ovec->sv_flags = 0;
2087c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_ONSTACK)
2097c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_ONSTACK;
2107c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_RESETHAND)
2117c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_RESETHAND;
2127c478bd9Sstevel@tonic-gate 		if (!(oact.sa_flags & SA_RESTART))
2137c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_INTERRUPT;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate         return 0;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate void (*
221*6c02b4a4Smuffin signal(int s, void (*a)()))()
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate         struct sigvec osv;
2247c478bd9Sstevel@tonic-gate 	struct sigvec nsv;
2257c478bd9Sstevel@tonic-gate         static int mask[NSIG];
2267c478bd9Sstevel@tonic-gate         static int flags[NSIG];
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	nsv.sv_handler = a;
2297c478bd9Sstevel@tonic-gate 	nsv.sv_mask = mask[s];
2307c478bd9Sstevel@tonic-gate 	nsv.sv_flags = flags[s];
2317c478bd9Sstevel@tonic-gate         if (sigvec(s, &nsv, &osv) < 0)
2327c478bd9Sstevel@tonic-gate                 return (SIG_ERR);
2337c478bd9Sstevel@tonic-gate         if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
2347c478bd9Sstevel@tonic-gate                 mask[s] = nsv.sv_mask = osv.sv_mask;
2357c478bd9Sstevel@tonic-gate                 flags[s] = nsv.sv_flags = osv.sv_flags & ~SV_RESETHAND;
2367c478bd9Sstevel@tonic-gate                 if (sigvec(s, &nsv, (struct sigvec *)0) < 0)
2377c478bd9Sstevel@tonic-gate                         return (SIG_ERR);
2387c478bd9Sstevel@tonic-gate         }
2397c478bd9Sstevel@tonic-gate         return (osv.sv_handler);
2407c478bd9Sstevel@tonic-gate }
241