summaryrefslogtreecommitdiffstats
path: root/sys/compat/linux/linux_signal.c
blob: 528bb62d2aa3b98ab16dc0bdd406b9eb23bfd09a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*-
 * Copyright (c) 1994-1995 Søren Schmidt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer 
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software withough specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  $Id: linux_signal.c,v 1.2 1995/06/07 21:27:57 sos Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/signal.h>
#include <sys/signalvar.h>

#include <vm/vm.h>

#include <i386/linux/linux.h>

#define DONTMASK    (sigmask(SIGKILL)|sigmask(SIGSTOP)|sigmask(SIGCHLD))

static sigset_t
linux_to_bsd_sigmask(linux_sigset_t mask) {
    int i;
    sigset_t new = 0;

    for (i = 1; i <= LINUX_NSIG; i++)
	if (mask & (1 << i-1))
	    new |= (1 << (linux_to_bsd_signal[i]-1));
    return new;
}

static linux_sigset_t
bsd_to_linux_sigmask(sigset_t mask) {
    int i;
    sigset_t new = 0;

    for (i = 1; i <= NSIG; i++)
	if (mask & (1 << i-1))
	    new |= (1 << (bsd_to_linux_signal[i]-1));
    return new;
}

struct linux_sigaction_args {
    int sig;
    linux_sigaction_t *nsa;
    linux_sigaction_t *osa;
};

int
linux_sigaction(struct proc *p, struct linux_sigaction_args *args, int *retval)
{
    linux_sigaction_t linux_sa;
    struct sigaction *nsa = NULL, *osa = NULL, bsd_sa;
    struct sigaction_args {
	int sig;
	struct sigaction *nsa;
	struct sigaction *osa;
    } sa;
    int error;
    
#ifdef DEBUG
    printf("Linux-emul(%d): sigaction(%d, *, *)\n", p->p_pid, args->sig);
#endif
    if (args->osa)
	osa = (struct sigaction *)ua_alloc_init(sizeof(struct sigaction));

    if (args->nsa) {
	nsa = (struct sigaction *)ua_alloc(sizeof(struct sigaction));
	if (error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t)))
	    return error;
	bsd_sa.sa_mask = linux_to_bsd_sigmask(linux_sa.sa_mask);
	bsd_sa.sa_handler = linux_sa.sa_handler;
	bsd_sa.sa_flags = 0;
	if (linux_sa.sa_flags & LINUX_SA_NOCLDSTOP)
	    bsd_sa.sa_flags |= SA_NOCLDSTOP;
	if (linux_sa.sa_flags & LINUX_SA_ONSTACK)
	    bsd_sa.sa_flags |= SA_ONSTACK;
	if (linux_sa.sa_flags & LINUX_SA_RESTART)
	    bsd_sa.sa_flags |= SA_RESTART;
	if (error = copyout(&bsd_sa, nsa, sizeof(struct sigaction)))
	    return error;
    }
    sa.sig = linux_to_bsd_signal[args->sig];
    sa.nsa = nsa;
    sa.osa = osa;
    if ((error = sigaction(p, &sa, retval)))
	return error;

    if (args->osa) {
	if (error = copyin(osa, &bsd_sa, sizeof(struct sigaction)))
	    return error;
	linux_sa.sa_handler = bsd_sa.sa_handler;
	linux_sa.sa_restorer = NULL;
	linux_sa.sa_mask = bsd_to_linux_sigmask(bsd_sa.sa_mask);
	linux_sa.sa_flags = 0;
	if (bsd_sa.sa_flags & SA_NOCLDSTOP)
	    linux_sa.sa_flags |= LINUX_SA_NOCLDSTOP;
	if (bsd_sa.sa_flags & SA_ONSTACK)
	    linux_sa.sa_flags |= LINUX_SA_ONSTACK;
	if (bsd_sa.sa_flags & SA_RESTART)
	    linux_sa.sa_flags |= LINUX_SA_RESTART;
	if (error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t)))
	    return error;
    }
    return 0;
}

struct linux_sigprocmask_args {
    int how;
    linux_sigset_t *mask;
    linux_sigset_t *omask;
};

int
linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args,
		  int *retval)
{
    int error, s;
    sigset_t mask;
    sigset_t omask;

#ifdef DEBUG
    printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
#endif
    if (args->omask != NULL) {
	omask = bsd_to_linux_sigmask(p->p_sigmask);
	if (error = copyout(&omask, args->omask, sizeof(sigset_t)))
	    return error;
    }
    if (!(args->mask))
	return 0;
    if (error = copyin(args->mask, &mask, sizeof(linux_sigset_t)))
	return error;

    mask = linux_to_bsd_sigmask(mask);
    s = splhigh();
    switch (args->how) {
    case LINUX_SIG_BLOCK:
	p->p_sigmask |= (mask & ~DONTMASK);
	break;
    case LINUX_SIG_UNBLOCK:
	p->p_sigmask &= ~mask;
	break;
    case LINUX_SIG_SETMASK:
	p->p_sigmask = (mask & ~DONTMASK);
	break;
    default:
	error = EINVAL;
	break;
    }
    splx(s);
    return error;
}

int
linux_siggetmask(struct proc *p, void *args, int *retval)
{
#ifdef DEBUG
    printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
#endif
    *retval = bsd_to_linux_sigmask(p->p_sigmask);
    return 0;
}

struct linux_sigsetmask_args {
    linux_sigset_t mask;
};

int
linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args,int *retval)
{
    int s;

#ifdef DEBUG
    printf("Linux-emul(%d): sigsetmask(%08x)\n", p->p_pid, args->mask);
#endif
    s = splhigh();
    p->p_sigmask = (linux_to_bsd_sigmask(args->mask) & ~DONTMASK);
    splx(s);
    *retval = bsd_to_linux_sigmask(p->p_sigmask);
    return 0;
}

struct linux_sigpending_args {
    linux_sigset_t *mask;
};

int
linux_sigpending(struct proc *p, struct linux_sigpending_args *args,int *retval)
{
    linux_sigset_t linux_sig;

#ifdef DEBUG
    printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
#endif
    linux_sig = bsd_to_linux_sigmask(p->p_siglist & p->p_sigmask);
    return copyout(&linux_sig, args->mask, sizeof(linux_sig));
}

struct linux_sigsuspend_args {
    linux_sigset_t mask;
};

int
linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args,int *retval)
{
    sigset_t tmp;

#ifdef DEBUG
    printf("Linux-emul(%d): sigsuspend(%08x)\n", p->p_pid, args->mask);
#endif
    tmp = linux_to_bsd_sigmask(args->mask);
    return sigsuspend(p, &tmp , retval);
}

struct linux_kill_args {
    int pid;
    int signum;
};

int
linux_kill(struct proc *p, struct linux_kill_args *args, int *retval)
{
    struct {
	int pid;
	int signum;
    } tmp;

#ifdef DEBUG
    printf("Linux-emul(%d): kill(%d, %d)\n", 
	   p->p_pid, args->pid, args->signum);
#endif
    tmp.pid = args->pid;
    tmp.signum = linux_to_bsd_signal[args->signum];
    return kill(p, &tmp, retval);
}
OpenPOWER on IntegriCloud