linux_signal.c revision 37950
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 *
2837950Sbde *  $Id: linux_signal.c,v 1.10 1997/11/06 19:29:00 phk 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
4614331Speter    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
6014331Speter    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
11914331Speter
1209313Ssos    if (args->osa)
12114331Speter	osa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
1229313Ssos
1239313Ssos    if (args->nsa) {
12414331Speter	nsa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction));
1259313Ssos	if (error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t)))
1269313Ssos	    return error;
12714331Speter	linux_to_bsd_sigaction(&linux_sa, &bsd_sa);
1289313Ssos	if (error = copyout(&bsd_sa, nsa, sizeof(struct sigaction)))
1299313Ssos	    return error;
1309313Ssos    }
13112858Speter    sa.signum = linux_to_bsd_signal[args->sig];
1329313Ssos    sa.nsa = nsa;
1339313Ssos    sa.osa = osa;
13430994Sphk    if ((error = sigaction(p, &sa)))
1359313Ssos	return error;
1369313Ssos
1379313Ssos    if (args->osa) {
1389313Ssos	if (error = copyin(osa, &bsd_sa, sizeof(struct sigaction)))
1399313Ssos	    return error;
14014331Speter	bsd_to_linux_sigaction(&bsd_sa, &linux_sa);
1419313Ssos	if (error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t)))
1429313Ssos	    return error;
1439313Ssos    }
1449313Ssos    return 0;
1459313Ssos}
1469313Ssos
14714331Speterint
14830994Sphklinux_signal(struct proc *p, struct linux_signal_args *args)
14914331Speter{
15014331Speter    caddr_t sg;
15114331Speter    struct sigaction_args sa_args;
15214331Speter    struct sigaction *osa, *nsa, tmpsa;
15314331Speter    int error;
1549313Ssos
15514331Speter#ifdef DEBUG
15637950Sbde    printf("Linux-emul(%ld): signal(%d, %p)\n",
15737950Sbde	(long)p->p_pid, args->sig, (void *)args->handler);
15814331Speter#endif
15914331Speter    sg = stackgap_init();
16014331Speter    nsa = stackgap_alloc(&sg, sizeof *nsa);
16114331Speter    osa = stackgap_alloc(&sg, sizeof *osa);
16214331Speter
16314331Speter    tmpsa.sa_handler = args->handler;
16414331Speter    tmpsa.sa_mask = (sigset_t) 0;
16514331Speter    tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER;
16614331Speter    if ((error = copyout(&tmpsa, nsa, sizeof tmpsa)))
16714331Speter	return error;
16814331Speter
16914331Speter    sa_args.signum = linux_to_bsd_signal[args->sig];
17014331Speter    sa_args.osa = osa;
17114331Speter    sa_args.nsa = nsa;
17230994Sphk    if ((error = sigaction(p, &sa_args)))
17314331Speter	return error;
17414331Speter
17514331Speter    if ((error = copyin(osa, &tmpsa, sizeof *osa)))
17614331Speter	return error;
17714331Speter
17830994Sphk    p->p_retval[0] = (int)tmpsa.sa_handler;
17914331Speter
18014331Speter    return 0;
18114331Speter}
18214331Speter
18314331Speter
1849313Ssosint
18530994Sphklinux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
1869313Ssos{
1879313Ssos    int error, s;
1889313Ssos    sigset_t mask;
1899313Ssos    sigset_t omask;
1909313Ssos
1919313Ssos#ifdef DEBUG
1929313Ssos    printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
1939313Ssos#endif
19414331Speter
19530994Sphk    p->p_retval[0] = 0;
19614331Speter
1979313Ssos    if (args->omask != NULL) {
19814331Speter	omask = bsd_to_linux_sigset(p->p_sigmask);
1999313Ssos	if (error = copyout(&omask, args->omask, sizeof(sigset_t)))
2009313Ssos	    return error;
2019313Ssos    }
2029313Ssos    if (!(args->mask))
2039313Ssos	return 0;
2049313Ssos    if (error = copyin(args->mask, &mask, sizeof(linux_sigset_t)))
2059313Ssos	return error;
2069313Ssos
20714331Speter    mask = linux_to_bsd_sigset(mask);
2089313Ssos    s = splhigh();
2099313Ssos    switch (args->how) {
2109313Ssos    case LINUX_SIG_BLOCK:
21114331Speter	p->p_sigmask |= (mask & ~sigcantmask);
2129313Ssos	break;
2139313Ssos    case LINUX_SIG_UNBLOCK:
2149313Ssos	p->p_sigmask &= ~mask;
2159313Ssos	break;
2169313Ssos    case LINUX_SIG_SETMASK:
21714331Speter	p->p_sigmask = (mask & ~sigcantmask);
2189313Ssos	break;
2199313Ssos    default:
2209313Ssos	error = EINVAL;
2219313Ssos	break;
2229313Ssos    }
2239313Ssos    splx(s);
2249313Ssos    return error;
2259313Ssos}
2269313Ssos
2279313Ssosint
22830994Sphklinux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
2299313Ssos{
2309313Ssos#ifdef DEBUG
2319313Ssos    printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
2329313Ssos#endif
23330994Sphk    p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
2349313Ssos    return 0;
2359313Ssos}
2369313Ssos
2379313Ssosint
23830994Sphklinux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
2399313Ssos{
2409313Ssos    int s;
24114331Speter    sigset_t mask;
2429313Ssos
2439313Ssos#ifdef DEBUG
24437950Sbde    printf("Linux-emul(%ld): sigsetmask(%p)\n",
24537950Sbde	(long)p->p_pid, (void *)args->mask);
2469313Ssos#endif
24730994Sphk    p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);
24814331Speter
24914331Speter    mask = linux_to_bsd_sigset(args->mask);
2509313Ssos    s = splhigh();
25114331Speter    p->p_sigmask = mask & ~sigcantmask;
2529313Ssos    splx(s);
2539313Ssos    return 0;
2549313Ssos}
2559313Ssos
2569313Ssosint
25730994Sphklinux_sigpending(struct proc *p, struct linux_sigpending_args *args)
2589313Ssos{
2599313Ssos    linux_sigset_t linux_sig;
2609313Ssos
2619313Ssos#ifdef DEBUG
2629313Ssos    printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
2639313Ssos#endif
26414331Speter    linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask);
2659313Ssos    return copyout(&linux_sig, args->mask, sizeof(linux_sig));
2669313Ssos}
2679313Ssos
26814381Speter/*
26914381Speter * Linux has two extra args, restart and oldmask.  We dont use these,
27014381Speter * but it seems that "restart" is actually a context pointer that
27114381Speter * enables the signal to happen with a different register set.
27214381Speter */
2739313Ssosint
27430994Sphklinux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
2759313Ssos{
27614331Speter    struct sigsuspend_args tmp;
2779313Ssos
2789313Ssos#ifdef DEBUG
27937950Sbde    printf("Linux-emul(%ld): sigsuspend(%p)\n",
28037950Sbde	(long)p->p_pid, (void *)args->mask);
2819313Ssos#endif
28214331Speter    tmp.mask = linux_to_bsd_sigset(args->mask);
28330994Sphk    return sigsuspend(p, &tmp);
2849313Ssos}
2859313Ssos
28614331Speterint
28730994Sphklinux_pause(struct proc *p, struct linux_pause_args *args)
28814331Speter{
28914331Speter    struct sigsuspend_args tmp;
2909313Ssos
29114331Speter#ifdef DEBUG
29214331Speter    printf("Linux-emul(%d): pause()\n", p->p_pid);
29314331Speter#endif
29414331Speter    tmp.mask = p->p_sigmask;
29530994Sphk    return sigsuspend(p, &tmp);
29614331Speter}
29714331Speter
2989313Ssosint
29930994Sphklinux_kill(struct proc *p, struct linux_kill_args *args)
3009313Ssos{
30112858Speter    struct kill_args /* {
3029313Ssos	int pid;
3039313Ssos	int signum;
30412858Speter    } */ tmp;
3059313Ssos
3069313Ssos#ifdef DEBUG
3079313Ssos    printf("Linux-emul(%d): kill(%d, %d)\n",
3089313Ssos	   p->p_pid, args->pid, args->signum);
3099313Ssos#endif
3109313Ssos    tmp.pid = args->pid;
3119313Ssos    tmp.signum = linux_to_bsd_signal[args->signum];
31230994Sphk    return kill(p, &tmp);
3139313Ssos}
314