2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
28 * $Id: linux_signal.c,v 1.16 1999/07/06 06:54:00 cracauer Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysproto.h>
35 #include <sys/signalvar.h>
37 #include <i386/linux/linux.h>
38 #include <i386/linux/linux_proto.h>
39 #include <i386/linux/linux_util.h>
42 linux_to_bsd_sigset(linux_sigset_t mask) {
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));
56 bsd_to_linux_sigset(sigset_t mask) {
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));
70 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
72 bsa->sa_mask = linux_to_bsd_sigset(lsa->lsa_mask);
73 bsa->sa_handler = lsa->lsa_handler;
75 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
76 bsa->sa_flags |= SA_NOCLDSTOP;
77 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
78 bsa->sa_flags |= SA_NOCLDWAIT;
79 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
80 bsa->sa_flags |= SA_SIGINFO;
81 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
82 bsa->sa_flags |= SA_ONSTACK;
83 if (lsa->lsa_flags & LINUX_SA_RESTART)
84 bsa->sa_flags |= SA_RESTART;
85 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
86 bsa->sa_flags |= SA_RESETHAND;
87 if (lsa->lsa_flags & LINUX_SA_NOMASK)
88 bsa->sa_flags |= SA_NODEFER;
92 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
94 lsa->lsa_handler = bsa->sa_handler;
95 lsa->lsa_restorer = NULL; /* unsupported */
96 lsa->lsa_mask = bsd_to_linux_sigset(bsa->sa_mask);
98 if (bsa->sa_flags & SA_NOCLDSTOP)
99 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
100 if (bsa->sa_flags & SA_NOCLDWAIT)
101 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
102 if (bsa->sa_flags & SA_SIGINFO)
103 lsa->lsa_flags |= LINUX_SA_SIGINFO;
104 if (bsa->sa_flags & SA_ONSTACK)
105 lsa->lsa_flags |= LINUX_SA_ONSTACK;
106 if (bsa->sa_flags & SA_RESTART)
107 lsa->lsa_flags |= LINUX_SA_RESTART;
108 if (bsa->sa_flags & SA_RESETHAND)
109 lsa->lsa_flags |= LINUX_SA_ONESHOT;
110 if (bsa->sa_flags & SA_NODEFER)
111 lsa->lsa_flags |= LINUX_SA_NOMASK;
115 linux_do_sigaction(struct proc *p, int linux_sig, linux_sigaction_t *linux_nsa,
116 linux_sigaction_t *linux_osa)
118 struct sigaction *nsa, *osa, sa;
119 struct sigaction_args sa_args;
121 caddr_t sg = stackgap_init();
123 if (linux_sig <= 0 || linux_sig >= LINUX_NSIG)
127 osa = stackgap_alloc(&sg, sizeof(struct sigaction));
132 nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
133 linux_to_bsd_sigaction(linux_nsa, &sa);
134 error = copyout(&sa, nsa, sizeof(struct sigaction));
141 sa_args.signum = linux_to_bsd_signal[linux_sig];
144 error = sigaction(p, &sa_args);
149 error = copyin(osa, &sa, sizeof(struct sigaction));
152 bsd_to_linux_sigaction(&sa, linux_osa);
159 linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
161 linux_sigaction_t nsa, osa;
165 printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n",
166 (long)p->p_pid, args->sig, (void *)args->nsa, (void *)args->osa);
170 error = copyin(args->nsa, &nsa, sizeof(linux_sigaction_t));
175 error = linux_do_sigaction(p, args->sig,
176 args->nsa ? &nsa : NULL,
177 args->osa ? &osa : NULL);
182 error = copyout(&osa, args->osa, sizeof(linux_sigaction_t));
191 linux_signal(struct proc *p, struct linux_signal_args *args)
193 linux_sigaction_t nsa, osa;
197 printf("Linux-emul(%ld): signal(%d, %p)\n",
198 (long)p->p_pid, args->sig, (void *)args->handler);
201 nsa.lsa_handler = args->handler;
202 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
205 error = linux_do_sigaction(p, args->sig, &nsa, &osa);
207 p->p_retval[0] = (int)osa.lsa_handler;
213 linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args)
215 linux_sigaction_t nsa, osa;
216 linux_new_sigaction_t new_sa;
220 printf("Linux-emul(%ld): rt_sigaction(%d, %p, %p, %d)\n",
221 (long)p->p_pid, args->sig, (void *)args->act,
222 (void *)args->oact, args->sigsetsize);
225 if (args->sigsetsize != sizeof(linux_new_sigset_t))
229 if (args->sig >= LINUX_NSIG) {
230 printf("LINUX(%ld): rt_sigaction: 64-bit signal (%d)\n",
231 (long)p->p_pid, args->sig);
236 error = copyin(args->act, &new_sa, sizeof(linux_new_sigaction_t));
240 nsa.lsa_handler = new_sa.lsa_handler;
241 nsa.lsa_mask = new_sa.lsa_mask.sig[0];
242 nsa.lsa_flags = new_sa.lsa_flags;
243 nsa.lsa_restorer = new_sa.lsa_restorer;
246 if (new_sa.lsa_mask.sig[1] != 0)
247 printf("LINUX(%ld): rt_sigaction: sig[1] = 0x%08lx\n",
248 (long)p->p_pid, new_sa.lsa_mask.sig[1]);
252 error = linux_do_sigaction(p, args->sig,
253 args->act ? &nsa : NULL,
254 args->oact ? &osa : NULL);
259 new_sa.lsa_handler = osa.lsa_handler;
260 new_sa.lsa_flags = osa.lsa_flags;
261 new_sa.lsa_restorer = osa.lsa_restorer;
262 new_sa.lsa_mask.sig[0] = osa.lsa_mask;
263 new_sa.lsa_mask.sig[1] = 0;
264 error = copyout(&osa, args->oact, sizeof(linux_new_sigaction_t));
273 linux_do_sigprocmask(struct proc *p, int how, linux_sigset_t *new,
282 *old = bsd_to_linux_sigset(p->p_sigmask);
285 mask = linux_to_bsd_sigset(*new);
290 case LINUX_SIG_BLOCK:
291 p->p_sigmask |= (mask & ~sigcantmask);
293 case LINUX_SIG_UNBLOCK:
294 p->p_sigmask &= ~mask;
296 case LINUX_SIG_SETMASK:
297 p->p_sigmask = (mask & ~sigcantmask);
311 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
314 linux_sigset_t omask;
318 printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
321 if (args->mask != NULL) {
322 error = copyin(args->mask, &mask, sizeof(linux_sigset_t));
327 error = linux_do_sigprocmask(p, args->how,
328 args->mask ? &mask : NULL,
329 args->omask ? &omask : NULL);
331 if (!error && args->omask != NULL) {
332 error = copyout(&omask, args->omask, sizeof(linux_sigset_t));
339 linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args)
341 linux_new_sigset_t new_mask;
342 linux_sigset_t old_mask;
346 printf("Linux-emul(%ld): rt_sigprocmask(%d, %p, %p, %d)\n",
347 (long)p->p_pid, args->how, (void *)args->mask,
348 (void *)args->omask, args->sigsetsize);
351 if (args->sigsetsize != sizeof(linux_new_sigset_t))
354 if (args->mask != NULL) {
355 error = copyin(args->mask, &new_mask, sizeof(linux_new_sigset_t));
360 if (new_mask.sig[1] != 0)
361 printf("LINUX(%ld): rt_sigprocmask: sig[1] = 0x%08lx\n",
362 (long)p->p_pid, new_mask.sig[1]);
366 error = linux_do_sigprocmask(p, args->how,
367 args->mask ? new_mask.sig : NULL,
368 args->omask ? &old_mask : NULL);
370 if (!error && args->omask != NULL) {
371 new_mask.sig[0] = old_mask;
372 error = copyout(&new_mask, args->omask, sizeof(linux_new_sigset_t));
379 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
382 printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
384 p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
389 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
395 printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
396 (long)p->p_pid, (unsigned long)args->mask);
398 p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
400 mask = linux_to_bsd_sigset(args->mask);
402 p->p_sigmask = mask & ~sigcantmask;
408 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
410 linux_sigset_t linux_sig;
413 printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
415 linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask);
416 return copyout(&linux_sig, args->mask, sizeof(linux_sig));
420 * Linux has two extra args, restart and oldmask. We dont use these,
421 * but it seems that "restart" is actually a context pointer that
422 * enables the signal to happen with a different register set.
425 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
427 struct sigsuspend_args tmp;
430 printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
431 (long)p->p_pid, (unsigned long)args->mask);
433 tmp.mask = linux_to_bsd_sigset(args->mask);
434 return sigsuspend(p, &tmp);
438 linux_pause(struct proc *p, struct linux_pause_args *args)
440 struct sigsuspend_args tmp;
443 printf("Linux-emul(%d): pause()\n", p->p_pid);
445 tmp.mask = p->p_sigmask;
446 return sigsuspend(p, &tmp);
450 linux_kill(struct proc *p, struct linux_kill_args *args)
452 struct kill_args /* {
458 printf("Linux-emul(%d): kill(%d, %d)\n",
459 p->p_pid, args->pid, args->signum);
461 if (args->signum < 0 || args->signum >= LINUX_NSIG)
464 tmp.signum = linux_to_bsd_signal[args->signum];
465 return kill(p, &tmp);