1/*	$NetBSD: linux_sigaction.c,v 1.36 2021/09/23 06:56:27 ryo Exp $	*/
2
3/*-
4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: linux_sigaction.c,v 1.36 2021/09/23 06:56:27 ryo Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/proc.h>
42#include <sys/filedesc.h>
43#include <sys/mount.h>
44#include <sys/kernel.h>
45#include <sys/signal.h>
46#include <sys/signalvar.h>
47
48#include <sys/syscallargs.h>
49
50#include <compat/linux/common/linux_types.h>
51#include <compat/linux/common/linux_signal.h>
52#include <compat/linux/common/linux_util.h>
53#include <compat/linux/common/linux_ipc.h>
54#include <compat/linux/common/linux_sem.h>
55
56#include <compat/linux/linux_syscallargs.h>
57
58/* Used on: alpha (aka osf_sigaction), arm, i386, m68k, mips, ppc */
59/* Not used on: aarch64, sparc, sparc64, amd64 */
60
61/*
62 * The Linux sigaction() system call. Do the usual conversions,
63 * and just call sigaction().
64 */
65int
66linux_sys_sigaction(struct lwp *l, const struct linux_sys_sigaction_args *uap, register_t *retval)
67{
68	/* {
69		syscallarg(int) signum;
70		syscallarg(const struct linux_old_sigaction *) nsa;
71		syscallarg(struct linux_old_sigaction *) osa;
72	} */
73	struct linux_old_sigaction nlsa, olsa;
74	struct sigaction nbsa, obsa;
75	int error, sig;
76
77	/* XXX XAX handle switch to RT signal handler */
78	/* XXX XAX and update emuldata->ps_siginfo. */
79
80	if (SCARG(uap, nsa)) {
81		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
82		if (error)
83			return (error);
84		linux_old_to_native_sigaction(&nbsa, &nlsa);
85	}
86	sig = SCARG(uap, signum);
87	/*
88	 * XXX: Linux has 33 realtime signals, the go binary wants to
89	 * reset all of them; nothing else uses the last RT signal, so for
90	 * now ignore it.
91	 */
92	if (sig == LINUX__NSIG) {
93		uprintf("%s: setting signal %d ignored\n", __func__, sig);
94		sig--;	/* back to 63 which is ignored */
95	}
96	if (sig < 0 || sig >= LINUX__NSIG)
97		return (EINVAL);
98	if (sig > 0 && !linux_to_native_signo[sig]) {
99		/* Pretend that we did something useful for unknown signals. */
100		obsa.sa_handler = SIG_IGN;
101		sigemptyset(&obsa.sa_mask);
102		obsa.sa_flags = 0;
103	} else {
104		error = sigaction1(l, linux_to_native_signo[sig],
105		    SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
106		    NULL, 0);
107		if (error)
108			return (error);
109	}
110	if (SCARG(uap, osa)) {
111		native_to_linux_old_sigaction(&olsa, &obsa);
112		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
113		if (error)
114			return (error);
115	}
116	return (0);
117}
118