summaryrefslogtreecommitdiffstats
path: root/sys/compat/linux/linux_signal.c
blob: 9f2200e526a6925321d93ecf56ff9fcda6fcb344 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*-
 * 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.
 *
 * $FreeBSD$
 */

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

#include <i386/linux/linux.h>
#include <i386/linux/linux_proto.h>
#include <i386/linux/linux_util.h>

static sigset_t
linux_to_bsd_sigset(linux_sigset_t mask) {
    int b, l;
    sigset_t new = 0;

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

static linux_sigset_t
bsd_to_linux_sigset(sigset_t mask) {
    int b, l;
    sigset_t new = 0;

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

static void
linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
{
    bsa->sa_mask = linux_to_bsd_sigset(lsa->lsa_mask);
    bsa->sa_handler = lsa->lsa_handler;
    bsa->sa_flags = 0;
    if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
	bsa->sa_flags |= SA_NOCLDSTOP;
    if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
	bsa->sa_flags |= SA_NOCLDWAIT;
    if (lsa->lsa_flags & LINUX_SA_SIGINFO)
	bsa->sa_flags |= SA_SIGINFO;
    if (lsa->lsa_flags & LINUX_SA_ONSTACK)
	bsa->sa_flags |= SA_ONSTACK;
    if (lsa->lsa_flags & LINUX_SA_RESTART)
	bsa->sa_flags |= SA_RESTART;
    if (lsa->lsa_flags & LINUX_SA_ONESHOT)
	bsa->sa_flags |= SA_RESETHAND;
    if (lsa->lsa_flags & LINUX_SA_NOMASK)
	bsa->sa_flags |= SA_NODEFER;
}

static void
bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
{
    lsa->lsa_handler = bsa->sa_handler;
    lsa->lsa_restorer = NULL;	/* unsupported */
    lsa->lsa_mask = bsd_to_linux_sigset(bsa->sa_mask);
    lsa->lsa_flags = 0;
    if (bsa->sa_flags & SA_NOCLDSTOP)
	lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
    if (bsa->sa_flags & SA_NOCLDWAIT)
	lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
    if (bsa->sa_flags & SA_SIGINFO)
	lsa->lsa_flags |= LINUX_SA_SIGINFO;
    if (bsa->sa_flags & SA_ONSTACK)
	lsa->lsa_flags |= LINUX_SA_ONSTACK;
    if (bsa->sa_flags & SA_RESTART)
	lsa->lsa_flags |= LINUX_SA_RESTART;
    if (bsa->sa_flags & SA_RESETHAND)
	lsa->lsa_flags |= LINUX_SA_ONESHOT;
    if (bsa->sa_flags & SA_NODEFER)
	lsa->lsa_flags |= LINUX_SA_NOMASK;
}

static int
linux_do_sigaction(struct proc *p, int linux_sig, linux_sigaction_t *linux_nsa,
		   linux_sigaction_t *linux_osa)
{
    struct sigaction *nsa, *osa, sa;
    struct sigaction_args sa_args;
    int error;
    caddr_t sg = stackgap_init();

    if (linux_sig <= 0 || linux_sig >= LINUX_NSIG)
	return EINVAL;

    if (linux_osa)
	osa = stackgap_alloc(&sg, sizeof(struct sigaction));
    else
	osa = NULL;

    if (linux_nsa) {
	nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
	linux_to_bsd_sigaction(linux_nsa, &sa);
	error = copyout(&sa, nsa, sizeof(struct sigaction));
	if (error)
	    return error;
    }
    else
	nsa = NULL;

    sa_args.signum = linux_to_bsd_signal[linux_sig];
    sa_args.nsa = nsa;
    sa_args.osa = osa;
    error = sigaction(p, &sa_args);
    if (error)
	return error;

    if (linux_osa) {
	error = copyin(osa, &sa, sizeof(struct sigaction));
	if (error)
	    return error;
	bsd_to_linux_sigaction(&sa, linux_osa);
    }

    return 0;
}

int
linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
{
    linux_sigaction_t nsa, osa;
    int error;

#ifdef DEBUG
    printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n",
	   (long)p->p_pid, args->sig, (void *)args->nsa, (void *)args->osa);
#endif

    if (args->nsa) {
	error = copyin(args->nsa, &nsa, sizeof(linux_sigaction_t));
	if (error)
	    return error;
    }

    error = linux_do_sigaction(p, args->sig,
			       args->nsa ? &nsa : NULL,
			       args->osa ? &osa : NULL);
    if (error)
	return error;

    if (args->osa) {
	error = copyout(&osa, args->osa, sizeof(linux_sigaction_t));
	if (error)
	    return error;
    }

    return 0;
}

int
linux_signal(struct proc *p, struct linux_signal_args *args)
{
    linux_sigaction_t nsa, osa;
    int error;

#ifdef DEBUG
    printf("Linux-emul(%ld): signal(%d, %p)\n",
	   (long)p->p_pid, args->sig, (void *)args->handler);
#endif

    nsa.lsa_handler = args->handler;
    nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
    nsa.lsa_mask = NULL;

    error = linux_do_sigaction(p, args->sig, &nsa, &osa);

    p->p_retval[0] = (int)osa.lsa_handler;

    return 0;
}

int
linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args)
{
    linux_sigaction_t nsa, osa;
    linux_new_sigaction_t new_sa;
    int error;

#ifdef DEBUG
    printf("Linux-emul(%ld): rt_sigaction(%d, %p, %p, %d)\n",
	   (long)p->p_pid, args->sig, (void *)args->act,
	   (void *)args->oact, args->sigsetsize);
#endif

    if (args->sigsetsize != sizeof(linux_new_sigset_t))
	return EINVAL;

#ifdef DEBUG
    if (args->sig >= LINUX_NSIG) {
	printf("LINUX(%ld): rt_sigaction: 64-bit signal (%d)\n",
	       (long)p->p_pid, args->sig);
    }
#endif

    if (args->act) {
	error = copyin(args->act, &new_sa, sizeof(linux_new_sigaction_t));
	if (error)
	    return error;

	nsa.lsa_handler = new_sa.lsa_handler;
	nsa.lsa_mask = new_sa.lsa_mask.sig[0];
	nsa.lsa_flags = new_sa.lsa_flags;
	nsa.lsa_restorer = new_sa.lsa_restorer;

#ifdef DEBUG
	if (new_sa.lsa_mask.sig[1] != 0)
	    printf("LINUX(%ld): rt_sigaction: sig[1] = 0x%08lx\n",
		   (long)p->p_pid, new_sa.lsa_mask.sig[1]);
#endif
    }

    error = linux_do_sigaction(p, args->sig,
			       args->act ? &nsa : NULL,
			       args->oact ? &osa : NULL);
    if (error)
	return error;

    if (args->oact) {
	new_sa.lsa_handler = osa.lsa_handler;
	new_sa.lsa_flags = osa.lsa_flags;
	new_sa.lsa_restorer = osa.lsa_restorer;
	new_sa.lsa_mask.sig[0] = osa.lsa_mask;
	new_sa.lsa_mask.sig[1] = 0;
	error = copyout(&osa, args->oact, sizeof(linux_new_sigaction_t));
	if (error)
	    return error;
    }

    return 0;
}

static int
linux_do_sigprocmask(struct proc *p, int how, linux_sigset_t *new,
		     linux_sigset_t *old)
{
    int error = 0, s;
    sigset_t mask;

    p->p_retval[0] = 0;

    if (old != NULL)
	*old = bsd_to_linux_sigset(p->p_sigmask);

    if (new != NULL) {
	mask = linux_to_bsd_sigset(*new);

	s = splhigh();

	switch (how) {
	case LINUX_SIG_BLOCK:
	    p->p_sigmask |= (mask & ~sigcantmask);
	    break;
	case LINUX_SIG_UNBLOCK:
	    p->p_sigmask &= ~mask;
	    break;
	case LINUX_SIG_SETMASK:
	    p->p_sigmask = (mask & ~sigcantmask);
	    break;
	default:
	    error = EINVAL;
	    break;
	}

	splx(s);
    }

    return error;
}

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

#ifdef DEBUG
    printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
#endif

    if (args->mask != NULL) {
	error = copyin(args->mask, &mask, sizeof(linux_sigset_t));
	if (error)
	    return error;
    }

    error = linux_do_sigprocmask(p, args->how,
				 args->mask ? &mask : NULL,
				 args->omask ? &omask : NULL);

    if (!error && args->omask != NULL) {
	error = copyout(&omask, args->omask, sizeof(linux_sigset_t));
    }

    return error;
}

int
linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args)
{
    linux_new_sigset_t new_mask;
    linux_sigset_t old_mask;
    int error;

#ifdef DEBUG
    printf("Linux-emul(%ld): rt_sigprocmask(%d, %p, %p, %d)\n",
	   (long)p->p_pid, args->how, (void *)args->mask,
	   (void *)args->omask, args->sigsetsize);
#endif

    if (args->sigsetsize != sizeof(linux_new_sigset_t))
	return EINVAL;

    if (args->mask != NULL) {
	error = copyin(args->mask, &new_mask, sizeof(linux_new_sigset_t));
	if (error)
	    return error;

#ifdef DEBUG
	if (new_mask.sig[1] != 0)
	    printf("LINUX(%ld): rt_sigprocmask: sig[1] = 0x%08lx\n",
		   (long)p->p_pid, new_mask.sig[1]);
#endif
    }

    error = linux_do_sigprocmask(p, args->how,
				 args->mask ? new_mask.sig : NULL,
				 args->omask ? &old_mask : NULL);

    if (!error && args->omask != NULL) {
	new_mask.sig[0] = old_mask;
	error = copyout(&new_mask, args->omask, sizeof(linux_new_sigset_t));
    }

    return error;
}

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

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

#ifdef DEBUG
    printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
	(long)p->p_pid, (unsigned long)args->mask);
#endif
    p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask);

    mask = linux_to_bsd_sigset(args->mask);
    s = splhigh();
    p->p_sigmask = mask & ~sigcantmask;
    splx(s);
    return 0;
}

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

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

/*
 * Linux has two extra args, restart and oldmask.  We dont use these,
 * but it seems that "restart" is actually a context pointer that
 * enables the signal to happen with a different register set.
 */
int
linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
{
    struct sigsuspend_args tmp;

#ifdef DEBUG
    printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
	(long)p->p_pid, (unsigned long)args->mask);
#endif
    tmp.mask = linux_to_bsd_sigset(args->mask);
    return sigsuspend(p, &tmp);
}

int
linux_pause(struct proc *p, struct linux_pause_args *args)
{
    struct sigsuspend_args tmp;

#ifdef DEBUG
    printf("Linux-emul(%d): pause()\n", p->p_pid);
#endif
    tmp.mask = p->p_sigmask;
    return sigsuspend(p, &tmp);
}

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

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