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