xref: /freebsd/sys/compat/linux/linux_signal.c (revision 98edb3e17869504d0ada58932efa96b71f899181)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: linux_signal.c,v 1.15 1999/05/06 18:44:26 peter Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysproto.h>
34 #include <sys/proc.h>
35 #include <sys/signalvar.h>
36 
37 #include <i386/linux/linux.h>
38 #include <i386/linux/linux_proto.h>
39 #include <i386/linux/linux_util.h>
40 
41 static sigset_t
42 linux_to_bsd_sigset(linux_sigset_t mask) {
43     int b, l;
44     sigset_t new = 0;
45 
46     for (l = 1; l < LINUX_NSIG; l++) {
47 	if (mask & (1 << (l - 1))) {
48 	    if ((b = linux_to_bsd_signal[l]))
49 		new |= (1 << (b - 1));
50 	}
51     }
52     return new;
53 }
54 
55 static linux_sigset_t
56 bsd_to_linux_sigset(sigset_t mask) {
57     int b, l;
58     sigset_t new = 0;
59 
60     for (b = 1; b < NSIG; b++) {
61 	if (mask & (1 << (b - 1))) {
62 	    if ((l = bsd_to_linux_signal[b]))
63 		new |= (1 << (l - 1));
64 	}
65     }
66     return new;
67 }
68 
69 static void
70 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
71 {
72     bsa->sa_mask = linux_to_bsd_sigset(lsa->lsa_mask);
73     bsa->sa_handler = lsa->lsa_handler;
74     bsa->sa_flags = 0;
75     if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
76 	bsa->sa_flags |= SA_NOCLDSTOP;
77     if (lsa->lsa_flags & LINUX_SA_ONSTACK)
78 	bsa->sa_flags |= SA_ONSTACK;
79     if (lsa->lsa_flags & LINUX_SA_RESTART)
80 	bsa->sa_flags |= SA_RESTART;
81     if (lsa->lsa_flags & LINUX_SA_ONESHOT)
82 	bsa->sa_flags |= SA_RESETHAND;
83     if (lsa->lsa_flags & LINUX_SA_NOMASK)
84 	bsa->sa_flags |= SA_NODEFER;
85 }
86 
87 static void
88 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
89 {
90     lsa->lsa_handler = bsa->sa_handler;
91     lsa->lsa_restorer = NULL;	/* unsupported */
92     lsa->lsa_mask = bsd_to_linux_sigset(bsa->sa_mask);
93     lsa->lsa_flags = 0;
94     if (bsa->sa_flags & SA_NOCLDSTOP)
95 	lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
96     if (bsa->sa_flags & SA_ONSTACK)
97 	lsa->lsa_flags |= LINUX_SA_ONSTACK;
98     if (bsa->sa_flags & SA_RESTART)
99 	lsa->lsa_flags |= LINUX_SA_RESTART;
100     if (bsa->sa_flags & SA_RESETHAND)
101 	lsa->lsa_flags |= LINUX_SA_ONESHOT;
102     if (bsa->sa_flags & SA_NODEFER)
103 	lsa->lsa_flags |= LINUX_SA_NOMASK;
104 }
105 
106 int
107 linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
108 {
109     linux_sigaction_t linux_sa;
110     struct sigaction *nsa = NULL, *osa = NULL, bsd_sa;
111     struct sigaction_args sa;
112     int error;
113     caddr_t sg = stackgap_init();
114 
115 #ifdef DEBUG
116     printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n",
117 	(long)p->p_pid, args->sig, (void *)args->nsa, (void *)args->osa);
118 #endif
119     if (args->sig <= 0 || args->sig >= LINUX_NSIG)
120 	return EINVAL;
121     if (args->osa)
122 	osa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
123 
124     if (args->nsa) {
125 	nsa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
126 	error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t));
127 	if (error)
128 	    return error;
129 	linux_to_bsd_sigaction(&linux_sa, &bsd_sa);
130 	error = copyout(&bsd_sa, nsa, sizeof(struct sigaction));
131 	if (error)
132 	    return error;
133     }
134     sa.signum = linux_to_bsd_signal[args->sig];
135     sa.nsa = nsa;
136     sa.osa = osa;
137     error = sigaction(p, &sa);
138     if (error)
139 	return error;
140 
141     if (args->osa) {
142 	error = copyin(osa, &bsd_sa, sizeof(struct sigaction));
143 	if (error)
144 	    return error;
145 	bsd_to_linux_sigaction(&bsd_sa, &linux_sa);
146 	error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t));
147 	if (error)
148 	    return error;
149     }
150     return 0;
151 }
152 
153 int
154 linux_signal(struct proc *p, struct linux_signal_args *args)
155 {
156     caddr_t sg;
157     struct sigaction_args sa_args;
158     struct sigaction *osa, *nsa, tmpsa;
159     int error;
160 
161 #ifdef DEBUG
162     printf("Linux-emul(%ld): signal(%d, %p)\n",
163 	(long)p->p_pid, args->sig, (void *)args->handler);
164 #endif
165     if (args->sig <= 0 || args->sig >= LINUX_NSIG)
166 	return EINVAL;
167     sg = stackgap_init();
168     nsa = stackgap_alloc(&sg, sizeof *nsa);
169     osa = stackgap_alloc(&sg, sizeof *osa);
170 
171     tmpsa.sa_handler = args->handler;
172     tmpsa.sa_mask = (sigset_t) 0;
173     tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER;
174     if ((error = copyout(&tmpsa, nsa, sizeof tmpsa)))
175 	return error;
176 
177     sa_args.signum = linux_to_bsd_signal[args->sig];
178     sa_args.osa = osa;
179     sa_args.nsa = nsa;
180     if ((error = sigaction(p, &sa_args)))
181 	return error;
182 
183     if ((error = copyin(osa, &tmpsa, sizeof *osa)))
184 	return error;
185 
186     p->p_retval[0] = (int)tmpsa.sa_handler;
187 
188     return 0;
189 }
190 
191 
192 int
193 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
194 {
195     int error, s;
196     sigset_t mask;
197     sigset_t omask;
198 
199 #ifdef DEBUG
200     printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
201 #endif
202 
203     p->p_retval[0] = 0;
204 
205     if (args->omask != NULL) {
206 	omask = bsd_to_linux_sigset(p->p_sigmask);
207 	error = copyout(&omask, args->omask, sizeof(sigset_t));
208 	if (error)
209 	    return error;
210     }
211     if (!(args->mask))
212 	return 0;
213     error = copyin(args->mask, &mask, sizeof(linux_sigset_t));
214     if (error)
215 	return error;
216 
217     mask = linux_to_bsd_sigset(mask);
218     s = splhigh();
219     switch (args->how) {
220     case LINUX_SIG_BLOCK:
221 	p->p_sigmask |= (mask & ~sigcantmask);
222 	break;
223     case LINUX_SIG_UNBLOCK:
224 	p->p_sigmask &= ~mask;
225 	break;
226     case LINUX_SIG_SETMASK:
227 	p->p_sigmask = (mask & ~sigcantmask);
228 	break;
229     default:
230 	error = EINVAL;
231 	break;
232     }
233     splx(s);
234     return error;
235 }
236 
237 int
238 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
239 {
240 #ifdef DEBUG
241     printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
242 #endif
243     p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
244     return 0;
245 }
246 
247 int
248 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
249 {
250     int s;
251     sigset_t mask;
252 
253 #ifdef DEBUG
254     printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
255 	(long)p->p_pid, (unsigned long)args->mask);
256 #endif
257     p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
258 
259     mask = linux_to_bsd_sigset(args->mask);
260     s = splhigh();
261     p->p_sigmask = mask & ~sigcantmask;
262     splx(s);
263     return 0;
264 }
265 
266 int
267 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
268 {
269     linux_sigset_t linux_sig;
270 
271 #ifdef DEBUG
272     printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
273 #endif
274     linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask);
275     return copyout(&linux_sig, args->mask, sizeof(linux_sig));
276 }
277 
278 /*
279  * Linux has two extra args, restart and oldmask.  We dont use these,
280  * but it seems that "restart" is actually a context pointer that
281  * enables the signal to happen with a different register set.
282  */
283 int
284 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
285 {
286     struct sigsuspend_args tmp;
287 
288 #ifdef DEBUG
289     printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
290 	(long)p->p_pid, (unsigned long)args->mask);
291 #endif
292     tmp.mask = linux_to_bsd_sigset(args->mask);
293     return sigsuspend(p, &tmp);
294 }
295 
296 int
297 linux_pause(struct proc *p, struct linux_pause_args *args)
298 {
299     struct sigsuspend_args tmp;
300 
301 #ifdef DEBUG
302     printf("Linux-emul(%d): pause()\n", p->p_pid);
303 #endif
304     tmp.mask = p->p_sigmask;
305     return sigsuspend(p, &tmp);
306 }
307 
308 int
309 linux_kill(struct proc *p, struct linux_kill_args *args)
310 {
311     struct kill_args /* {
312 	int pid;
313 	int signum;
314     } */ tmp;
315 
316 #ifdef DEBUG
317     printf("Linux-emul(%d): kill(%d, %d)\n",
318 	   p->p_pid, args->pid, args->signum);
319 #endif
320     if (args->signum < 0 || args->signum >= LINUX_NSIG)
321 	return EINVAL;
322     tmp.pid = args->pid;
323     tmp.signum = linux_to_bsd_signal[args->signum];
324     return kill(p, &tmp);
325 }
326