1 /*        $NetBSD: kern_sig_13.c,v 1.22 2021/09/07 11:43:02 riastradh Exp $     */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: kern_sig_13.c,v 1.22 2021/09/07 11:43:02 riastradh Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/signal.h>
42 #include <sys/signalvar.h>
43 #include <sys/systm.h>
44 
45 #include <sys/syscall.h>
46 #include <sys/syscallvar.h>
47 #include <sys/syscallargs.h>
48 
49 #include <machine/limits.h>
50 
51 #include <compat/sys/signal.h>
52 #include <compat/sys/signalvar.h>
53 #include <compat/common/compat_util.h>
54 #include <compat/common/compat_sigaltstack.h>
55 #include <compat/common/compat_mod.h>
56 
57 static const struct syscall_package kern_sig_13_syscalls[] = {
58         { SYS_compat_13_sigaction13, 0, (sy_call_t *)compat_13_sys_sigaction },
59         { SYS_compat_13_sigaltstack13, 0,
60               (sy_call_t *)compat_13_sys_sigaltstack },
61         { SYS_compat_13_sigpending13, 0,
62               (sy_call_t *)compat_13_sys_sigpending },
63         { SYS_compat_13_sigprocmask13, 0,
64               (sy_call_t *)compat_13_sys_sigprocmask },
65         { SYS_compat_13_sigsuspend13, 0,
66               (sy_call_t *)compat_13_sys_sigsuspend },
67           /* compat_13_sigreturn13 is in MD code! */
68         { SYS_compat_13_sigreturn13, 0, (sy_call_t *)compat_13_sys_sigreturn },
69           { 0, 0, NULL }
70 };
71 
72 void
native_sigset13_to_sigset(const sigset13_t * oss,sigset_t * ss)73 native_sigset13_to_sigset(const sigset13_t *oss, sigset_t *ss)
74 {
75 
76           memset(ss, 0, sizeof(*ss));
77           ss->__bits[0] = *oss;
78           ss->__bits[1] = 0;
79           ss->__bits[2] = 0;
80           ss->__bits[3] = 0;
81 }
82 
83 void
native_sigset_to_sigset13(const sigset_t * ss,sigset13_t * oss)84 native_sigset_to_sigset13(const sigset_t *ss, sigset13_t *oss)
85 {
86 
87           *oss = ss->__bits[0];
88 }
89 
90 void
native_sigaction13_to_sigaction(const struct sigaction13 * osa,struct sigaction * sa)91 native_sigaction13_to_sigaction(const struct sigaction13 *osa, struct sigaction *sa)
92 {
93 
94           memset(sa, 0, sizeof(*sa));
95           sa->sa_handler = osa->osa_handler;
96           native_sigset13_to_sigset(&osa->osa_mask, &sa->sa_mask);
97           sa->sa_flags = osa->osa_flags;
98 }
99 
100 void
native_sigaction_to_sigaction13(const struct sigaction * sa,struct sigaction13 * osa)101 native_sigaction_to_sigaction13(const struct sigaction *sa, struct sigaction13 *osa)
102 {
103 
104           memset(osa, 0, sizeof(*osa));
105           osa->osa_handler = sa->sa_handler;
106           native_sigset_to_sigset13(&sa->sa_mask, &osa->osa_mask);
107           osa->osa_flags = sa->sa_flags;
108 }
109 
110 int
compat_13_sys_sigaltstack(struct lwp * l,const struct compat_13_sys_sigaltstack_args * uap,register_t * retval)111 compat_13_sys_sigaltstack(struct lwp *l, const struct compat_13_sys_sigaltstack_args *uap, register_t *retval)
112 {
113           /* {
114                     syscallarg(const struct sigaltstack13 *) nss;
115                     syscallarg(struct sigaltstack13 *) oss;
116           } */
117           compat_sigaltstack(uap, sigaltstack13, SS_ONSTACK, SS_DISABLE);
118 }
119 
120 int
compat_13_sys_sigaction(struct lwp * l,const struct compat_13_sys_sigaction_args * uap,register_t * retval)121 compat_13_sys_sigaction(struct lwp *l, const struct compat_13_sys_sigaction_args *uap, register_t *retval)
122 {
123           /* {
124                     syscallarg(int) signum;
125                     syscallarg(const struct sigaction13 *) nsa;
126                     syscallarg(struct sigaction13 *) osa;
127           } */
128           struct sigaction13 nesa, oesa;
129           struct sigaction nbsa, obsa;
130           int error;
131 
132           if (SCARG(uap, nsa)) {
133                     error = copyin(SCARG(uap, nsa), &nesa, sizeof(nesa));
134                     if (error)
135                               return (error);
136                     native_sigaction13_to_sigaction(&nesa, &nbsa);
137           }
138           error = sigaction1(l, SCARG(uap, signum),
139               SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
140               NULL, 0);
141           if (error)
142                     return (error);
143           if (SCARG(uap, osa)) {
144                     native_sigaction_to_sigaction13(&obsa, &oesa);
145                     error = copyout(&oesa, SCARG(uap, osa), sizeof(oesa));
146                     if (error)
147                               return (error);
148           }
149           return (0);
150 }
151 
152 int
compat_13_sys_sigprocmask(struct lwp * l,const struct compat_13_sys_sigprocmask_args * uap,register_t * retval)153 compat_13_sys_sigprocmask(struct lwp *l, const struct compat_13_sys_sigprocmask_args *uap, register_t *retval)
154 {
155           /* {
156                     syscallarg(int) how;
157                     syscallarg(int) mask;
158           } */
159           struct proc *p = l->l_proc;
160           sigset13_t ness, oess;
161           sigset_t nbss, obss;
162           int error;
163 
164           ness = SCARG(uap, mask);
165           native_sigset13_to_sigset(&ness, &nbss);
166           mutex_enter(p->p_lock);
167           error = sigprocmask1(l, SCARG(uap, how), &nbss, &obss);
168           mutex_exit(p->p_lock);
169           if (error)
170                     return (error);
171           native_sigset_to_sigset13(&obss, &oess);
172           *retval = oess;
173           return (0);
174 }
175 
176 int
compat_13_sys_sigpending(struct lwp * l,const void * v,register_t * retval)177 compat_13_sys_sigpending(struct lwp *l, const void *v, register_t *retval)
178 {
179           sigset13_t ess;
180           sigset_t bss;
181 
182           sigpending1(l, &bss);
183           native_sigset_to_sigset13(&bss, &ess);
184           *retval = ess;
185           return (0);
186 }
187 
188 int
compat_13_sys_sigsuspend(struct lwp * l,const struct compat_13_sys_sigsuspend_args * uap,register_t * retval)189 compat_13_sys_sigsuspend(struct lwp *l, const struct compat_13_sys_sigsuspend_args *uap, register_t *retval)
190 {
191           /* {
192                     syscallarg(sigset13_t) mask;
193           } */
194           sigset13_t ess;
195           sigset_t bss;
196 
197           ess = SCARG(uap, mask);
198           native_sigset13_to_sigset(&ess, &bss);
199           return (sigsuspend1(l, &bss));
200 }
201 
202 int
kern_sig_13_init(void)203 kern_sig_13_init(void)
204 {
205 
206           return syscall_establish(NULL, kern_sig_13_syscalls);
207 }
208 
209 int
kern_sig_13_fini(void)210 kern_sig_13_fini(void)
211 {
212 
213           return syscall_disestablish(NULL, kern_sig_13_syscalls);
214 }
215