xref: /freebsd/sys/compat/linux/linux_signal.c (revision 2aebedc3ad9e722b272254e6dd3a12e399595e57)
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.13 1998/10/11 04:54:16 jdp 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->sa_mask);
73     bsa->sa_handler = lsa->sa_handler;
74     bsa->sa_flags = 0;
75     if (lsa->sa_flags & LINUX_SA_NOCLDSTOP)
76 	bsa->sa_flags |= SA_NOCLDSTOP;
77     if (lsa->sa_flags & LINUX_SA_ONSTACK)
78 	bsa->sa_flags |= SA_ONSTACK;
79     if (lsa->sa_flags & LINUX_SA_RESTART)
80 	bsa->sa_flags |= SA_RESTART;
81     if (lsa->sa_flags & LINUX_SA_ONESHOT)
82 	bsa->sa_flags |= SA_RESETHAND;
83     if (lsa->sa_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->sa_handler = bsa->sa_handler;
91     lsa->sa_restorer = NULL;	/* unsupported */
92     lsa->sa_mask = bsd_to_linux_sigset(bsa->sa_mask);
93     lsa->sa_flags = 0;
94     if (bsa->sa_flags & SA_NOCLDSTOP)
95 	lsa->sa_flags |= LINUX_SA_NOCLDSTOP;
96     if (bsa->sa_flags & SA_ONSTACK)
97 	lsa->sa_flags |= LINUX_SA_ONSTACK;
98     if (bsa->sa_flags & SA_RESTART)
99 	lsa->sa_flags |= LINUX_SA_RESTART;
100     if (bsa->sa_flags & SA_RESETHAND)
101 	lsa->sa_flags |= LINUX_SA_ONESHOT;
102     if (bsa->sa_flags & SA_NODEFER)
103 	lsa->sa_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 	if (error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t)))
127 	    return error;
128 	linux_to_bsd_sigaction(&linux_sa, &bsd_sa);
129 	if (error = copyout(&bsd_sa, nsa, sizeof(struct sigaction)))
130 	    return error;
131     }
132     sa.signum = linux_to_bsd_signal[args->sig];
133     sa.nsa = nsa;
134     sa.osa = osa;
135     if ((error = sigaction(p, &sa)))
136 	return error;
137 
138     if (args->osa) {
139 	if (error = copyin(osa, &bsd_sa, sizeof(struct sigaction)))
140 	    return error;
141 	bsd_to_linux_sigaction(&bsd_sa, &linux_sa);
142 	if (error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t)))
143 	    return error;
144     }
145     return 0;
146 }
147 
148 int
149 linux_signal(struct proc *p, struct linux_signal_args *args)
150 {
151     caddr_t sg;
152     struct sigaction_args sa_args;
153     struct sigaction *osa, *nsa, tmpsa;
154     int error;
155 
156 #ifdef DEBUG
157     printf("Linux-emul(%ld): signal(%d, %p)\n",
158 	(long)p->p_pid, args->sig, (void *)args->handler);
159 #endif
160     if (args->sig <= 0 || args->sig >= LINUX_NSIG)
161 	return EINVAL;
162     sg = stackgap_init();
163     nsa = stackgap_alloc(&sg, sizeof *nsa);
164     osa = stackgap_alloc(&sg, sizeof *osa);
165 
166     tmpsa.sa_handler = args->handler;
167     tmpsa.sa_mask = (sigset_t) 0;
168     tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER;
169     if ((error = copyout(&tmpsa, nsa, sizeof tmpsa)))
170 	return error;
171 
172     sa_args.signum = linux_to_bsd_signal[args->sig];
173     sa_args.osa = osa;
174     sa_args.nsa = nsa;
175     if ((error = sigaction(p, &sa_args)))
176 	return error;
177 
178     if ((error = copyin(osa, &tmpsa, sizeof *osa)))
179 	return error;
180 
181     p->p_retval[0] = (int)tmpsa.sa_handler;
182 
183     return 0;
184 }
185 
186 
187 int
188 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
189 {
190     int error, s;
191     sigset_t mask;
192     sigset_t omask;
193 
194 #ifdef DEBUG
195     printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
196 #endif
197 
198     p->p_retval[0] = 0;
199 
200     if (args->omask != NULL) {
201 	omask = bsd_to_linux_sigset(p->p_sigmask);
202 	if (error = copyout(&omask, args->omask, sizeof(sigset_t)))
203 	    return error;
204     }
205     if (!(args->mask))
206 	return 0;
207     if (error = copyin(args->mask, &mask, sizeof(linux_sigset_t)))
208 	return error;
209 
210     mask = linux_to_bsd_sigset(mask);
211     s = splhigh();
212     switch (args->how) {
213     case LINUX_SIG_BLOCK:
214 	p->p_sigmask |= (mask & ~sigcantmask);
215 	break;
216     case LINUX_SIG_UNBLOCK:
217 	p->p_sigmask &= ~mask;
218 	break;
219     case LINUX_SIG_SETMASK:
220 	p->p_sigmask = (mask & ~sigcantmask);
221 	break;
222     default:
223 	error = EINVAL;
224 	break;
225     }
226     splx(s);
227     return error;
228 }
229 
230 int
231 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
232 {
233 #ifdef DEBUG
234     printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
235 #endif
236     p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
237     return 0;
238 }
239 
240 int
241 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
242 {
243     int s;
244     sigset_t mask;
245 
246 #ifdef DEBUG
247     printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
248 	(long)p->p_pid, (unsigned long)args->mask);
249 #endif
250     p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
251 
252     mask = linux_to_bsd_sigset(args->mask);
253     s = splhigh();
254     p->p_sigmask = mask & ~sigcantmask;
255     splx(s);
256     return 0;
257 }
258 
259 int
260 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
261 {
262     linux_sigset_t linux_sig;
263 
264 #ifdef DEBUG
265     printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
266 #endif
267     linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask);
268     return copyout(&linux_sig, args->mask, sizeof(linux_sig));
269 }
270 
271 /*
272  * Linux has two extra args, restart and oldmask.  We dont use these,
273  * but it seems that "restart" is actually a context pointer that
274  * enables the signal to happen with a different register set.
275  */
276 int
277 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
278 {
279     struct sigsuspend_args tmp;
280 
281 #ifdef DEBUG
282     printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
283 	(long)p->p_pid, (unsigned long)args->mask);
284 #endif
285     tmp.mask = linux_to_bsd_sigset(args->mask);
286     return sigsuspend(p, &tmp);
287 }
288 
289 int
290 linux_pause(struct proc *p, struct linux_pause_args *args)
291 {
292     struct sigsuspend_args tmp;
293 
294 #ifdef DEBUG
295     printf("Linux-emul(%d): pause()\n", p->p_pid);
296 #endif
297     tmp.mask = p->p_sigmask;
298     return sigsuspend(p, &tmp);
299 }
300 
301 int
302 linux_kill(struct proc *p, struct linux_kill_args *args)
303 {
304     struct kill_args /* {
305 	int pid;
306 	int signum;
307     } */ tmp;
308 
309 #ifdef DEBUG
310     printf("Linux-emul(%d): kill(%d, %d)\n",
311 	   p->p_pid, args->pid, args->signum);
312 #endif
313     if (args->signum < 0 || args->signum >= LINUX_NSIG)
314 	return EINVAL;
315     tmp.pid = args->pid;
316     tmp.signum = linux_to_bsd_signal[args->signum];
317     return kill(p, &tmp);
318 }
319