kern_sig_43.c revision 1.36 1 1.36 maxv /* $NetBSD: kern_sig_43.c,v 1.36 2020/01/01 14:52:38 maxv Exp $ */
2 1.1 christos
3 1.9 mycroft /*-
4 1.9 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.9 mycroft * All rights reserved.
6 1.9 mycroft *
7 1.9 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.9 mycroft * by Charles M. Hannum.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.9 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.9 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.9 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.9 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.9 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.9 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.9 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.9 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.9 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.9 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.16 lukem
32 1.16 lukem #include <sys/cdefs.h>
33 1.36 maxv __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.36 2020/01/01 14:52:38 maxv Exp $");
34 1.8 jonathan
35 1.15 mrg #if defined(_KERNEL_OPT)
36 1.8 jonathan #include "opt_compat_netbsd.h"
37 1.14 jdolecek #endif
38 1.1 christos
39 1.1 christos #include <sys/param.h>
40 1.1 christos #include <sys/signalvar.h>
41 1.1 christos #include <sys/resourcevar.h>
42 1.1 christos #include <sys/namei.h>
43 1.1 christos #include <sys/vnode.h>
44 1.1 christos #include <sys/proc.h>
45 1.1 christos #include <sys/systm.h>
46 1.1 christos #include <sys/timeb.h>
47 1.1 christos #include <sys/times.h>
48 1.1 christos #include <sys/buf.h>
49 1.1 christos #include <sys/acct.h>
50 1.1 christos #include <sys/file.h>
51 1.1 christos #include <sys/kernel.h>
52 1.1 christos #include <sys/wait.h>
53 1.1 christos #include <sys/ktrace.h>
54 1.1 christos #include <sys/syslog.h>
55 1.1 christos #include <sys/stat.h>
56 1.1 christos #include <sys/core.h>
57 1.22 elad #include <sys/kauth.h>
58 1.1 christos
59 1.35 pgoyette #include <sys/syscall.h>
60 1.35 pgoyette #include <sys/syscallvar.h>
61 1.1 christos #include <sys/syscallargs.h>
62 1.35 pgoyette #include <sys/compat_stub.h>
63 1.1 christos
64 1.27 ad #include <sys/cpu.h>
65 1.1 christos
66 1.20 christos #include <compat/sys/signal.h>
67 1.35 pgoyette #include <compat/common/compat_mod.h>
68 1.20 christos
69 1.28 dsl void compat_43_sigmask_to_sigset(const int *, sigset_t *);
70 1.28 dsl void compat_43_sigset_to_sigmask(const sigset_t *, int *);
71 1.28 dsl void compat_43_sigvec_to_sigaction(const struct sigvec *, struct sigaction *);
72 1.28 dsl void compat_43_sigaction_to_sigvec(const struct sigaction *, struct sigvec *);
73 1.28 dsl void compat_43_sigstack_to_sigaltstack(const struct sigstack *, struct sigaltstack *);
74 1.28 dsl void compat_43_sigaltstack_to_sigstack(const struct sigaltstack *, struct sigstack *);
75 1.9 mycroft
76 1.35 pgoyette static struct syscall_package kern_sig_43_syscalls[] = {
77 1.35 pgoyette { SYS_compat_43_osigblock, 0, (sy_call_t *)compat_43_sys_sigblock },
78 1.35 pgoyette { SYS_compat_43_osigsetmask, 0, (sy_call_t *)compat_43_sys_sigsetmask },
79 1.35 pgoyette { SYS_compat_43_osigstack, 0, (sy_call_t *)compat_43_sys_sigstack },
80 1.35 pgoyette { SYS_compat_43_osigvec, 0, (sy_call_t *)compat_43_sys_sigvec },
81 1.35 pgoyette { SYS_compat_43_okillpg, 0, (sy_call_t *)compat_43_sys_killpg },
82 1.35 pgoyette { 0, 0, NULL }
83 1.35 pgoyette };
84 1.35 pgoyette
85 1.9 mycroft void
86 1.29 dsl compat_43_sigmask_to_sigset(const int *sm, sigset_t *ss)
87 1.9 mycroft {
88 1.9 mycroft
89 1.9 mycroft ss->__bits[0] = *sm;
90 1.9 mycroft ss->__bits[1] = 0;
91 1.9 mycroft ss->__bits[2] = 0;
92 1.9 mycroft ss->__bits[3] = 0;
93 1.9 mycroft }
94 1.9 mycroft
95 1.9 mycroft void
96 1.29 dsl compat_43_sigset_to_sigmask(const sigset_t *ss, int *sm)
97 1.9 mycroft {
98 1.9 mycroft
99 1.9 mycroft *sm = ss->__bits[0];
100 1.9 mycroft }
101 1.9 mycroft
102 1.9 mycroft void
103 1.29 dsl compat_43_sigvec_to_sigaction(const struct sigvec *sv, struct sigaction *sa)
104 1.9 mycroft {
105 1.9 mycroft sa->sa_handler = sv->sv_handler;
106 1.9 mycroft compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
107 1.9 mycroft sa->sa_flags = sv->sv_flags ^ SA_RESTART;
108 1.9 mycroft }
109 1.9 mycroft
110 1.9 mycroft void
111 1.29 dsl compat_43_sigaction_to_sigvec(const struct sigaction *sa, struct sigvec *sv)
112 1.9 mycroft {
113 1.9 mycroft sv->sv_handler = sa->sa_handler;
114 1.9 mycroft compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
115 1.9 mycroft sv->sv_flags = sa->sa_flags ^ SA_RESTART;
116 1.9 mycroft }
117 1.9 mycroft
118 1.9 mycroft void
119 1.29 dsl compat_43_sigstack_to_sigaltstack(const struct sigstack *ss, struct sigaltstack *sa)
120 1.9 mycroft {
121 1.9 mycroft sa->ss_sp = ss->ss_sp;
122 1.11 christos sa->ss_size = SIGSTKSZ; /* Use the recommended size */
123 1.9 mycroft sa->ss_flags = 0;
124 1.9 mycroft if (ss->ss_onstack)
125 1.9 mycroft sa->ss_flags |= SS_ONSTACK;
126 1.9 mycroft }
127 1.9 mycroft
128 1.9 mycroft void
129 1.29 dsl compat_43_sigaltstack_to_sigstack(const struct sigaltstack *sa, struct sigstack *ss)
130 1.9 mycroft {
131 1.36 maxv memset(ss, 0, sizeof(*ss));
132 1.9 mycroft ss->ss_sp = sa->ss_sp;
133 1.9 mycroft if (sa->ss_flags & SS_ONSTACK)
134 1.9 mycroft ss->ss_onstack = 1;
135 1.9 mycroft else
136 1.9 mycroft ss->ss_onstack = 0;
137 1.9 mycroft }
138 1.9 mycroft
139 1.1 christos int
140 1.30 dsl compat_43_sys_sigblock(struct lwp *l, const struct compat_43_sys_sigblock_args *uap, register_t *retval)
141 1.4 thorpej {
142 1.30 dsl /* {
143 1.1 christos syscallarg(int) mask;
144 1.30 dsl } */
145 1.18 thorpej struct proc *p = l->l_proc;
146 1.9 mycroft int nsm, osm;
147 1.9 mycroft sigset_t nss, oss;
148 1.9 mycroft int error;
149 1.9 mycroft
150 1.9 mycroft nsm = SCARG(uap, mask);
151 1.9 mycroft compat_43_sigmask_to_sigset(&nsm, &nss);
152 1.31 ad mutex_enter(p->p_lock);
153 1.26 ad error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
154 1.31 ad mutex_exit(p->p_lock);
155 1.9 mycroft if (error)
156 1.9 mycroft return (error);
157 1.9 mycroft compat_43_sigset_to_sigmask(&oss, &osm);
158 1.9 mycroft *retval = osm;
159 1.1 christos return (0);
160 1.1 christos }
161 1.1 christos
162 1.1 christos int
163 1.30 dsl compat_43_sys_sigsetmask(struct lwp *l, const struct compat_43_sys_sigsetmask_args *uap, register_t *retval)
164 1.4 thorpej {
165 1.30 dsl /* {
166 1.1 christos syscallarg(int) mask;
167 1.30 dsl } */
168 1.18 thorpej struct proc *p = l->l_proc;
169 1.9 mycroft int nsm, osm;
170 1.9 mycroft sigset_t nss, oss;
171 1.9 mycroft int error;
172 1.9 mycroft
173 1.9 mycroft nsm = SCARG(uap, mask);
174 1.9 mycroft compat_43_sigmask_to_sigset(&nsm, &nss);
175 1.31 ad mutex_enter(p->p_lock);
176 1.26 ad error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
177 1.31 ad mutex_exit(p->p_lock);
178 1.9 mycroft if (error)
179 1.9 mycroft return (error);
180 1.9 mycroft compat_43_sigset_to_sigmask(&oss, &osm);
181 1.9 mycroft *retval = osm;
182 1.1 christos return (0);
183 1.1 christos }
184 1.1 christos
185 1.1 christos /* ARGSUSED */
186 1.1 christos int
187 1.30 dsl compat_43_sys_sigstack(struct lwp *l, const struct compat_43_sys_sigstack_args *uap, register_t *retval)
188 1.4 thorpej {
189 1.30 dsl /* {
190 1.1 christos syscallarg(struct sigstack *) nss;
191 1.1 christos syscallarg(struct sigstack *) oss;
192 1.30 dsl } */
193 1.9 mycroft struct sigstack nss, oss;
194 1.9 mycroft struct sigaltstack nsa, osa;
195 1.9 mycroft int error;
196 1.9 mycroft
197 1.9 mycroft if (SCARG(uap, nss)) {
198 1.9 mycroft error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
199 1.9 mycroft if (error)
200 1.9 mycroft return (error);
201 1.9 mycroft compat_43_sigstack_to_sigaltstack(&nss, &nsa);
202 1.9 mycroft }
203 1.26 ad error = sigaltstack1(l,
204 1.9 mycroft SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
205 1.7 christos if (error)
206 1.3 mycroft return (error);
207 1.9 mycroft if (SCARG(uap, oss)) {
208 1.9 mycroft compat_43_sigaltstack_to_sigstack(&osa, &oss);
209 1.10 pk error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
210 1.9 mycroft if (error)
211 1.9 mycroft return (error);
212 1.9 mycroft }
213 1.3 mycroft return (0);
214 1.1 christos }
215 1.1 christos
216 1.1 christos /*
217 1.1 christos * Generalized interface signal handler, 4.3-compatible.
218 1.1 christos */
219 1.1 christos /* ARGSUSED */
220 1.1 christos int
221 1.30 dsl compat_43_sys_sigvec(struct lwp *l, const struct compat_43_sys_sigvec_args *uap, register_t *retval)
222 1.4 thorpej {
223 1.30 dsl /* {
224 1.1 christos syscallarg(int) signum;
225 1.9 mycroft syscallarg(const struct sigvec *) nsv;
226 1.1 christos syscallarg(struct sigvec *) osv;
227 1.30 dsl } */
228 1.9 mycroft struct sigvec nsv, osv;
229 1.9 mycroft struct sigaction nsa, osa;
230 1.9 mycroft int error;
231 1.9 mycroft
232 1.9 mycroft if (SCARG(uap, nsv)) {
233 1.9 mycroft error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
234 1.7 christos if (error)
235 1.1 christos return (error);
236 1.9 mycroft compat_43_sigvec_to_sigaction(&nsv, &nsa);
237 1.1 christos }
238 1.26 ad error = sigaction1(l, SCARG(uap, signum),
239 1.17 thorpej SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
240 1.17 thorpej NULL, 0);
241 1.9 mycroft if (error)
242 1.9 mycroft return (error);
243 1.9 mycroft if (SCARG(uap, osv)) {
244 1.9 mycroft compat_43_sigaction_to_sigvec(&osa, &osv);
245 1.10 pk error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
246 1.7 christos if (error)
247 1.1 christos return (error);
248 1.1 christos }
249 1.1 christos return (0);
250 1.1 christos }
251 1.1 christos
252 1.1 christos
253 1.1 christos /* ARGSUSED */
254 1.1 christos int
255 1.30 dsl compat_43_sys_killpg(struct lwp *l, const struct compat_43_sys_killpg_args *uap, register_t *retval)
256 1.4 thorpej {
257 1.30 dsl /* {
258 1.1 christos syscallarg(int) pgid;
259 1.1 christos syscallarg(int) signum;
260 1.30 dsl } */
261 1.19 christos ksiginfo_t ksi;
262 1.30 dsl int pgid = SCARG(uap, pgid);
263 1.1 christos
264 1.35 pgoyette pgid &= kern_sig_43_pgid_mask;
265 1.1 christos
266 1.1 christos if ((u_int)SCARG(uap, signum) >= NSIG)
267 1.1 christos return (EINVAL);
268 1.19 christos memset(&ksi, 0, sizeof(ksi));
269 1.19 christos ksi.ksi_signo = SCARG(uap, signum);
270 1.19 christos ksi.ksi_code = SI_USER;
271 1.23 ad ksi.ksi_pid = l->l_proc->p_pid;
272 1.23 ad ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
273 1.30 dsl return killpg1(l, &ksi, pgid, 0);
274 1.1 christos }
275 1.35 pgoyette
276 1.35 pgoyette int
277 1.35 pgoyette kern_sig_43_init(void)
278 1.35 pgoyette {
279 1.35 pgoyette
280 1.35 pgoyette return syscall_establish(NULL, kern_sig_43_syscalls);
281 1.35 pgoyette }
282 1.35 pgoyette
283 1.35 pgoyette int
284 1.35 pgoyette kern_sig_43_fini(void)
285 1.35 pgoyette {
286 1.35 pgoyette
287 1.35 pgoyette return syscall_disestablish(NULL, kern_sig_43_syscalls);
288 1.35 pgoyette }
289