xref: /titanic_52/usr/src/cmd/csh/i386/signal.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 /*
18  * 4.3BSD signal compatibility functions
19  *
20  * the implementation interprets signal masks equal to -1 as "all of the
21  * signals in the signal set", thereby allowing signals with numbers
22  * above 32 to be blocked when referenced in code such as:
23  *
24  *	for (i = 0; i < NSIG; i++)
25  *		mask |= sigmask(i)
26  */
27 
28 #include <sys/types.h>
29 #include <sys/siginfo.h>
30 #include <sys/ucontext.h>
31 #include <signal.h>
32 #include "signal.h"
33 #include <errno.h>
34 #include <stdio.h>
35 
36 #define set2mask(setp) ((setp)->__sigbits[0])
37 #define mask2set(mask, setp) \
38 	((mask) == -1 ? sigfillset(setp) : sigemptyset(setp), (((setp)->__sigbits[0]) = (mask)))
39 
40 void (*_siguhandler[NSIG])() = { 0 };
41 
42 /*
43  * sigstack is emulated with sigaltstack by guessing an appropriate
44  * value for the stack size - on machines that have stacks that grow
45  * upwards, the ss_sp arguments for both functions mean the same thing,
46  * (the initial stack pointer sigstack() is also the stack base
47  * sigaltstack()), so a "very large" value should be chosen for the
48  * stack size - on machines that have stacks that grow downwards, the
49  * ss_sp arguments mean opposite things, so 0 should be used (hopefully
50  * these machines don't have hardware stack bounds registers that pay
51  * attention to sigaltstack()'s size argument.
52  */
53 
54 #ifdef sun
55 #define SIGSTACKSIZE	0
56 #endif
57 
58 
59 /*
60  * sigvechandler is the real signal handler installed for all
61  * signals handled in the 4.3BSD compatibility interface - it translates
62  * SVR4 signal hander arguments into 4.3BSD signal handler arguments
63  * and then calls the real handler
64  */
65 
66 static void
67 sigvechandler(sig, sip, ucp)
68 	int sig;
69 	siginfo_t *sip;
70 	ucontext_t *ucp;
71 {
72 	struct sigcontext sc;
73 	int code;
74 	char *addr;
75 	register int i, j;
76 	int gwinswitch = 0;
77 
78 	sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0);
79 	sc.sc_mask = set2mask(&ucp->uc_sigmask);
80 
81 	/*
82 	 * Machine dependent code begins
83 	 */
84 	sc.sc_sp = (int) ucp->uc_mcontext.gregs[UESP];
85 	sc.sc_pc = (int) ucp->uc_mcontext.gregs[EIP];
86 	sc.sc_ps = (int) ucp->uc_mcontext.gregs[EFL];
87 	sc.sc_eax = (int) ucp->uc_mcontext.gregs[EAX];
88 	sc.sc_edx = (int) ucp->uc_mcontext.gregs[EDX];
89 
90 	/*
91 	 * Machine dependent code ends
92 	 */
93 
94 	if (sip != NULL)
95 		if ((code = sip->si_code) == BUS_OBJERR)
96 			code = SEGV_MAKE_ERR(sip->si_errno);
97 
98 	if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS)
99 		if (sip != NULL)
100 			addr = (char *)sip->si_addr;
101 	else
102 		addr = SIG_NOADDR;
103 
104 	(*_siguhandler[sig])(sig, code, &sc, addr);
105 
106 	if (sc.sc_onstack)
107 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
108 	else
109 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
110 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
111 
112 	/*
113 	 * Machine dependent code begins
114 	 */
115 	ucp->uc_mcontext.gregs[UESP] = (int) sc.sc_sp;
116 	ucp->uc_mcontext.gregs[EIP] = (int) sc.sc_pc;
117 	ucp->uc_mcontext.gregs[EFL] = (int) sc.sc_ps;
118 	ucp->uc_mcontext.gregs[EAX] = (int) sc.sc_eax;
119 	ucp->uc_mcontext.gregs[EDX] = (int) sc.sc_edx;
120 	/*
121 	 * Machine dependent code ends
122 	 */
123 
124 	setcontext (ucp);
125 }
126 
127 sigsetmask(mask)
128 	int mask;
129 {
130 	sigset_t oset;
131 	sigset_t nset;
132 
133 	(void) sigprocmask(0, (sigset_t *)0, &nset);
134 	mask2set(mask, &nset);
135 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
136 	return set2mask(&oset);
137 }
138 
139 sigblock(mask)
140 	int mask;
141 {
142 	sigset_t oset;
143 	sigset_t nset;
144 
145 	(void) sigprocmask(0, (sigset_t *)0, &nset);
146 	mask2set(mask, &nset);
147 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
148 	return set2mask(&oset);
149 }
150 
151 sigpause(mask)
152 	int mask;
153 {
154 	sigset_t set;
155 
156 	(void) sigprocmask(0, (sigset_t *)0, &set);
157 	mask2set(mask, &set);
158 	return (sigsuspend(&set));
159 }
160 
161 sigvec(sig, nvec, ovec)
162         int sig;
163         struct sigvec *nvec;
164 	struct sigvec *ovec;
165 {
166         struct sigaction nact;
167         struct sigaction oact;
168         struct sigaction *nactp;
169         void (*ohandler)(), (*nhandler)();
170 
171         if (sig <= 0 || sig >= NSIG) {
172                 errno = EINVAL;
173                 return -1;
174         }
175 
176         ohandler = _siguhandler[sig];
177 
178         if (nvec) {
179 		_sigaction(sig, (struct sigaction *)0, &nact);
180                 nhandler = nvec->sv_handler;
181                 _siguhandler[sig] = nhandler;
182                 if (nhandler != SIG_DFL && nhandler != SIG_IGN)
183                         nact.sa_handler = (void (*)())sigvechandler;
184 		else
185 			nact.sa_handler = nhandler;
186 		mask2set(nvec->sv_mask, &nact.sa_mask);
187 		/*
188 		if ( sig == SIGTSTP || sig == SIGSTOP )
189 			nact.sa_handler = SIG_DFL; 	*/
190 		nact.sa_flags = SA_SIGINFO;
191 		if (!(nvec->sv_flags & SV_INTERRUPT))
192 			nact.sa_flags |= SA_RESTART;
193 		if (nvec->sv_flags & SV_RESETHAND)
194 			nact.sa_flags |= SA_RESETHAND;
195 		if (nvec->sv_flags & SV_ONSTACK)
196 			nact.sa_flags |= SA_ONSTACK;
197 		nactp = &nact;
198         } else
199 		nactp = (struct sigaction *)0;
200 
201         if (_sigaction(sig, nactp, &oact) < 0) {
202                 _siguhandler[sig] = ohandler;
203                 return -1;
204         }
205 
206         if (ovec) {
207 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
208 			ovec->sv_handler = oact.sa_handler;
209 		else
210 			ovec->sv_handler = ohandler;
211 		ovec->sv_mask = set2mask(&oact.sa_mask);
212 		ovec->sv_flags = 0;
213 		if (oact.sa_flags & SA_ONSTACK)
214 			ovec->sv_flags |= SV_ONSTACK;
215 		if (oact.sa_flags & SA_RESETHAND)
216 			ovec->sv_flags |= SV_RESETHAND;
217 		if (!(oact.sa_flags & SA_RESTART))
218 			ovec->sv_flags |= SV_INTERRUPT;
219 	}
220 
221         return 0;
222 }
223 
224 
225 void (*
226 signal(s, a))()
227         int s;
228         void (*a)();
229 {
230         struct sigvec osv;
231 	struct sigvec nsv;
232         static int mask[NSIG];
233         static int flags[NSIG];
234 
235 	nsv.sv_handler = a;
236 	nsv.sv_mask = mask[s];
237 	nsv.sv_flags = flags[s];
238         if (sigvec(s, &nsv, &osv) < 0)
239                 return (SIG_ERR);
240         if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
241                 mask[s] = nsv.sv_mask = osv.sv_mask;
242                 flags[s] = nsv.sv_flags = osv.sv_flags & ~SV_RESETHAND;
243                 if (sigvec(s, &nsv, (struct sigvec *)0) < 0)
244                         return (SIG_ERR);
245         }
246         return (osv.sv_handler);
247 }
248