linux_signal.c revision 46571
19313Ssos/*-
29313Ssos * Copyright (c) 1994-1995 S�ren Schmidt
39313Ssos * All rights reserved.
49313Ssos *
59313Ssos * Redistribution and use in source and binary forms, with or without
69313Ssos * modification, are permitted provided that the following conditions
79313Ssos * are met:
89313Ssos * 1. Redistributions of source code must retain the above copyright
99313Ssos *    notice, this list of conditions and the following disclaimer
109313Ssos *    in this position and unchanged.
119313Ssos * 2. Redistributions in binary form must reproduce the above copyright
129313Ssos *    notice, this list of conditions and the following disclaimer in the
139313Ssos *    documentation and/or other materials provided with the distribution.
149313Ssos * 3. The name of the author may not be used to endorse or promote products
159313Ssos *    derived from this software withough specific prior written permission
169313Ssos *
179313Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189313Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199313Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209313Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219313Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229313Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239313Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249313Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259313Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269313Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279313Ssos *
2846571Speter *  $Id: linux_signal.c,v 1.14 1998/12/21 19:21:36 sos Exp $
299313Ssos */
309313Ssos
319313Ssos#include <sys/param.h>
329313Ssos#include <sys/systm.h>
3312458Sbde#include <sys/sysproto.h>
349313Ssos#include <sys/proc.h>
359313Ssos#include <sys/signalvar.h>
369313Ssos
379313Ssos#include <i386/linux/linux.h>
3814331Speter#include <i386/linux/linux_proto.h>
3914331Speter#include <i386/linux/linux_util.h>
409313Ssos
419313Ssosstatic sigset_t
4214331Speterlinux_to_bsd_sigset(linux_sigset_t mask) {
4314331Speter    int b, l;
449313Ssos    sigset_t new = 0;
459313Ssos
4640203Sjdp    for (l = 1; l < LINUX_NSIG; l++) {
4714331Speter	if (mask & (1 << (l - 1))) {
4814331Speter	    if ((b = linux_to_bsd_signal[l]))
4914331Speter		new |= (1 << (b - 1));
5014331Speter	}
5114331Speter    }
529313Ssos    return new;
539313Ssos}
549313Ssos
559313Ssosstatic linux_sigset_t
5614331Speterbsd_to_linux_sigset(sigset_t mask) {
5714331Speter    int b, l;
589313Ssos    sigset_t new = 0;
599313Ssos
6040203Sjdp    for (b = 1; b < NSIG; b++) {
6114331Speter	if (mask & (1 << (b - 1))) {
6214331Speter	    if ((l = bsd_to_linux_signal[b]))
6314331Speter		new |= (1 << (l - 1));
6414331Speter	}
6514331Speter    }
669313Ssos    return new;
679313Ssos}
689313Ssos
6914342Speterstatic void
7014331Speterlinux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
7114331Speter{
7214331Speter    bsa->sa_mask = linux_to_bsd_sigset(lsa->sa_mask);
7314331Speter    bsa->sa_handler = lsa->sa_handler;
7414331Speter    bsa->sa_flags = 0;
7514331Speter    if (lsa->sa_flags & LINUX_SA_NOCLDSTOP)
7614331Speter	bsa->sa_flags |= SA_NOCLDSTOP;
7714331Speter    if (lsa->sa_flags & LINUX_SA_ONSTACK)
7814331Speter	bsa->sa_flags |= SA_ONSTACK;
7914331Speter    if (lsa->sa_flags & LINUX_SA_RESTART)
8014331Speter	bsa->sa_flags |= SA_RESTART;
8114331Speter    if (lsa->sa_flags & LINUX_SA_ONESHOT)
8214331Speter	bsa->sa_flags |= SA_RESETHAND;
8314331Speter    if (lsa->sa_flags & LINUX_SA_NOMASK)
8414331Speter	bsa->sa_flags |= SA_NODEFER;
8514331Speter}
869313Ssos
8714342Speterstatic void
8814331Speterbsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
8914331Speter{
9014331Speter    lsa->sa_handler = bsa->sa_handler;
9114331Speter    lsa->sa_restorer = NULL;	/* unsupported */
9214331Speter    lsa->sa_mask = bsd_to_linux_sigset(bsa->sa_mask);
9314331Speter    lsa->sa_flags = 0;
9414331Speter    if (bsa->sa_flags & SA_NOCLDSTOP)
9514331Speter	lsa->sa_flags |= LINUX_SA_NOCLDSTOP;
9614331Speter    if (bsa->sa_flags & SA_ONSTACK)
9714331Speter	lsa->sa_flags |= LINUX_SA_ONSTACK;
9814331Speter    if (bsa->sa_flags & SA_RESTART)
9914331Speter	lsa->sa_flags |= LINUX_SA_RESTART;
10014331Speter    if (bsa->sa_flags & SA_RESETHAND)
10114331Speter	lsa->sa_flags |= LINUX_SA_ONESHOT;
10214331Speter    if (bsa->sa_flags & SA_NODEFER)
10314331Speter	lsa->sa_flags |= LINUX_SA_NOMASK;
10414331Speter}
10514331Speter
1069313Ssosint
10730994Sphklinux_sigaction(struct proc *p, struct linux_sigaction_args *args)
1089313Ssos{
1099313Ssos    linux_sigaction_t linux_sa;
1109313Ssos    struct sigaction *nsa = NULL, *osa = NULL, bsd_sa;
11114331Speter    struct sigaction_args sa;
1129313Ssos    int error;
11314331Speter    caddr_t sg = stackgap_init();
1149313Ssos
1159313Ssos#ifdef DEBUG
11637950Sbde    printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n",
11737950Sbde	(long)p->p_pid, args->sig, (void *)args->nsa, (void *)args->osa);
1189313Ssos#endif
11940203Sjdp    if (args->sig <= 0 || args->sig >= LINUX_NSIG)
12040203Sjdp	return EINVAL;
1219313Ssos    if (args->osa)
12214331Speter	osa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
1239313Ssos
1249313Ssos    if (args->nsa) {
12514331Speter	nsa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
12646571Speter	error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t));
12746571Speter	if (error)
1289313Ssos	    return error;
12914331Speter	linux_to_bsd_sigaction(&linux_sa, &bsd_sa);
13046571Speter	error = copyout(&bsd_sa, nsa, sizeof(struct sigaction));
13146571Speter	if (error)
1329313Ssos	    return error;
1339313Ssos    }
13412858Speter    sa.signum = linux_to_bsd_signal[args->sig];
1359313Ssos    sa.nsa = nsa;
1369313Ssos    sa.osa = osa;
13746571Speter    error = sigaction(p, &sa);
13846571Speter    if (error)
1399313Ssos	return error;
1409313Ssos
1419313Ssos    if (args->osa) {
14246571Speter	error = copyin(osa, &bsd_sa, sizeof(struct sigaction));
14346571Speter	if (error)
1449313Ssos	    return error;
14514331Speter	bsd_to_linux_sigaction(&bsd_sa, &linux_sa);
14646571Speter	error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t));
14746571Speter	if (error)
1489313Ssos	    return error;
1499313Ssos    }
1509313Ssos    return 0;
1519313Ssos}
1529313Ssos
15314331Speterint
15430994Sphklinux_signal(struct proc *p, struct linux_signal_args *args)
15514331Speter{
15614331Speter    caddr_t sg;
15714331Speter    struct sigaction_args sa_args;
15814331Speter    struct sigaction *osa, *nsa, tmpsa;
15914331Speter    int error;
1609313Ssos
16114331Speter#ifdef DEBUG
16237950Sbde    printf("Linux-emul(%ld): signal(%d, %p)\n",
16337950Sbde	(long)p->p_pid, args->sig, (void *)args->handler);
16414331Speter#endif
16540203Sjdp    if (args->sig <= 0 || args->sig >= LINUX_NSIG)
16640203Sjdp	return EINVAL;
16714331Speter    sg = stackgap_init();
16814331Speter    nsa = stackgap_alloc(&sg, sizeof *nsa);
16914331Speter    osa = stackgap_alloc(&sg, sizeof *osa);
17014331Speter
17114331Speter    tmpsa.sa_handler = args->handler;
17214331Speter    tmpsa.sa_mask = (sigset_t) 0;
17314331Speter    tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER;
17414331Speter    if ((error = copyout(&tmpsa, nsa, sizeof tmpsa)))
17514331Speter	return error;
17614331Speter
17714331Speter    sa_args.signum = linux_to_bsd_signal[args->sig];
17814331Speter    sa_args.osa = osa;
17914331Speter    sa_args.nsa = nsa;
18030994Sphk    if ((error = sigaction(p, &sa_args)))
18114331Speter	return error;
18214331Speter
18314331Speter    if ((error = copyin(osa, &tmpsa, sizeof *osa)))
18414331Speter	return error;
18514331Speter
18630994Sphk    p->p_retval[0] = (int)tmpsa.sa_handler;
18714331Speter
18814331Speter    return 0;
18914331Speter}
19014331Speter
19114331Speter
1929313Ssosint
19330994Sphklinux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
1949313Ssos{
1959313Ssos    int error, s;
1969313Ssos    sigset_t mask;
1979313Ssos    sigset_t omask;
1989313Ssos
1999313Ssos#ifdef DEBUG
2009313Ssos    printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
2019313Ssos#endif
20214331Speter
20330994Sphk    p->p_retval[0] = 0;
20414331Speter
2059313Ssos    if (args->omask != NULL) {
20614331Speter	omask = bsd_to_linux_sigset(p->p_sigmask);
20746571Speter	error = copyout(&omask, args->omask, sizeof(sigset_t));
20846571Speter	if (error)
2099313Ssos	    return error;
2109313Ssos    }
2119313Ssos    if (!(args->mask))
2129313Ssos	return 0;
21346571Speter    error = copyin(args->mask, &mask, sizeof(linux_sigset_t));
21446571Speter    if (error)
2159313Ssos	return error;
2169313Ssos
21714331Speter    mask = linux_to_bsd_sigset(mask);
2189313Ssos    s = splhigh();
2199313Ssos    switch (args->how) {
2209313Ssos    case LINUX_SIG_BLOCK:
22114331Speter	p->p_sigmask |= (mask & ~sigcantmask);
2229313Ssos	break;
2239313Ssos    case LINUX_SIG_UNBLOCK:
2249313Ssos	p->p_sigmask &= ~mask;
2259313Ssos	break;
2269313Ssos    case LINUX_SIG_SETMASK:
22714331Speter	p->p_sigmask = (mask & ~sigcantmask);
2289313Ssos	break;
2299313Ssos    default:
2309313Ssos	error = EINVAL;
2319313Ssos	break;
2329313Ssos    }
2339313Ssos    splx(s);
2349313Ssos    return error;
2359313Ssos}
2369313Ssos
2379313Ssosint
23830994Sphklinux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
2399313Ssos{
2409313Ssos#ifdef DEBUG
2419313Ssos    printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
2429313Ssos#endif
24330994Sphk    p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
2449313Ssos    return 0;
2459313Ssos}
2469313Ssos
2479313Ssosint
24830994Sphklinux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
2499313Ssos{
2509313Ssos    int s;
25114331Speter    sigset_t mask;
2529313Ssos
2539313Ssos#ifdef DEBUG
25438344Sbde    printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
25538344Sbde	(long)p->p_pid, (unsigned long)args->mask);
2569313Ssos#endif
25730994Sphk    p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
25814331Speter
25914331Speter    mask = linux_to_bsd_sigset(args->mask);
2609313Ssos    s = splhigh();
26114331Speter    p->p_sigmask = mask & ~sigcantmask;
2629313Ssos    splx(s);
2639313Ssos    return 0;
2649313Ssos}
2659313Ssos
2669313Ssosint
26730994Sphklinux_sigpending(struct proc *p, struct linux_sigpending_args *args)
2689313Ssos{
2699313Ssos    linux_sigset_t linux_sig;
2709313Ssos
2719313Ssos#ifdef DEBUG
2729313Ssos    printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
2739313Ssos#endif
27414331Speter    linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask);
2759313Ssos    return copyout(&linux_sig, args->mask, sizeof(linux_sig));
2769313Ssos}
2779313Ssos
27814381Speter/*
27914381Speter * Linux has two extra args, restart and oldmask.  We dont use these,
28014381Speter * but it seems that "restart" is actually a context pointer that
28114381Speter * enables the signal to happen with a different register set.
28214381Speter */
2839313Ssosint
28430994Sphklinux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
2859313Ssos{
28614331Speter    struct sigsuspend_args tmp;
2879313Ssos
2889313Ssos#ifdef DEBUG
28938344Sbde    printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
29038344Sbde	(long)p->p_pid, (unsigned long)args->mask);
2919313Ssos#endif
29214331Speter    tmp.mask = linux_to_bsd_sigset(args->mask);
29330994Sphk    return sigsuspend(p, &tmp);
2949313Ssos}
2959313Ssos
29614331Speterint
29730994Sphklinux_pause(struct proc *p, struct linux_pause_args *args)
29814331Speter{
29914331Speter    struct sigsuspend_args tmp;
3009313Ssos
30114331Speter#ifdef DEBUG
30214331Speter    printf("Linux-emul(%d): pause()\n", p->p_pid);
30314331Speter#endif
30414331Speter    tmp.mask = p->p_sigmask;
30530994Sphk    return sigsuspend(p, &tmp);
30614331Speter}
30714331Speter
3089313Ssosint
30930994Sphklinux_kill(struct proc *p, struct linux_kill_args *args)
3109313Ssos{
31112858Speter    struct kill_args /* {
3129313Ssos	int pid;
3139313Ssos	int signum;
31412858Speter    } */ tmp;
3159313Ssos
3169313Ssos#ifdef DEBUG
3179313Ssos    printf("Linux-emul(%d): kill(%d, %d)\n",
3189313Ssos	   p->p_pid, args->pid, args->signum);
3199313Ssos#endif
32041986Ssos    if (args->signum < 0 || args->signum >= LINUX_NSIG)
32140203Sjdp	return EINVAL;
3229313Ssos    tmp.pid = args->pid;
3239313Ssos    tmp.signum = linux_to_bsd_signal[args->signum];
32430994Sphk    return kill(p, &tmp);
3259313Ssos}
326