sys_sig.c revision 1.1.2.1 1 1.1.2.1 ad /* $NetBSD: sys_sig.c,v 1.1.2.1 2006/10/21 14:26:41 ad Exp $ */
2 1.1.2.1 ad
3 1.1.2.1 ad /*-
4 1.1.2.1 ad * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1.2.1 ad * All rights reserved.
6 1.1.2.1 ad *
7 1.1.2.1 ad * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 ad * modification, are permitted provided that the following conditions
9 1.1.2.1 ad * are met:
10 1.1.2.1 ad * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 ad * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 ad * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 ad * documentation and/or other materials provided with the distribution.
15 1.1.2.1 ad * 3. All advertising materials mentioning features or use of this software
16 1.1.2.1 ad * must display the following acknowledgement:
17 1.1.2.1 ad * This product includes software developed by the NetBSD
18 1.1.2.1 ad * Foundation, Inc. and its contributors.
19 1.1.2.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1.2.1 ad * contributors may be used to endorse or promote products derived
21 1.1.2.1 ad * from this software without specific prior written permission.
22 1.1.2.1 ad *
23 1.1.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1.2.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1.2.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1.2.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1.2.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1.2.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1.2.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1.2.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1.2.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1.2.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1.2.1 ad * POSSIBILITY OF SUCH DAMAGE.
34 1.1.2.1 ad */
35 1.1.2.1 ad
36 1.1.2.1 ad /*
37 1.1.2.1 ad * Copyright (c) 1982, 1986, 1989, 1991, 1993
38 1.1.2.1 ad * The Regents of the University of California. All rights reserved.
39 1.1.2.1 ad * (c) UNIX System Laboratories, Inc.
40 1.1.2.1 ad * All or some portions of this file are derived from material licensed
41 1.1.2.1 ad * to the University of California by American Telephone and Telegraph
42 1.1.2.1 ad * Co. or Unix System Laboratories, Inc. and are reproduced herein with
43 1.1.2.1 ad * the permission of UNIX System Laboratories, Inc.
44 1.1.2.1 ad *
45 1.1.2.1 ad * Redistribution and use in source and binary forms, with or without
46 1.1.2.1 ad * modification, are permitted provided that the following conditions
47 1.1.2.1 ad * are met:
48 1.1.2.1 ad * 1. Redistributions of source code must retain the above copyright
49 1.1.2.1 ad * notice, this list of conditions and the following disclaimer.
50 1.1.2.1 ad * 2. Redistributions in binary form must reproduce the above copyright
51 1.1.2.1 ad * notice, this list of conditions and the following disclaimer in the
52 1.1.2.1 ad * documentation and/or other materials provided with the distribution.
53 1.1.2.1 ad * 3. Neither the name of the University nor the names of its contributors
54 1.1.2.1 ad * may be used to endorse or promote products derived from this software
55 1.1.2.1 ad * without specific prior written permission.
56 1.1.2.1 ad *
57 1.1.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 1.1.2.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 1.1.2.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 1.1.2.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 1.1.2.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 1.1.2.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 1.1.2.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 1.1.2.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 1.1.2.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 1.1.2.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 1.1.2.1 ad * SUCH DAMAGE.
68 1.1.2.1 ad *
69 1.1.2.1 ad * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
70 1.1.2.1 ad */
71 1.1.2.1 ad
72 1.1.2.1 ad #include <sys/cdefs.h>
73 1.1.2.1 ad __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.1.2.1 2006/10/21 14:26:41 ad Exp $");
74 1.1.2.1 ad
75 1.1.2.1 ad #include "opt_ptrace.h"
76 1.1.2.1 ad #include "opt_compat_netbsd.h"
77 1.1.2.1 ad #include "opt_compat_netbsd32.h"
78 1.1.2.1 ad
79 1.1.2.1 ad #include <sys/param.h>
80 1.1.2.1 ad #include <sys/kernel.h>
81 1.1.2.1 ad #include <sys/malloc.h>
82 1.1.2.1 ad #include <sys/signalvar.h>
83 1.1.2.1 ad #include <sys/proc.h>
84 1.1.2.1 ad #include <sys/pool.h>
85 1.1.2.1 ad #include <sys/sa.h>
86 1.1.2.1 ad #include <sys/savar.h>
87 1.1.2.1 ad #include <sys/syscallargs.h>
88 1.1.2.1 ad #include <sys/kauth.h>
89 1.1.2.1 ad #include <sys/wait.h>
90 1.1.2.1 ad
91 1.1.2.1 ad #ifdef COMPAT_16
92 1.1.2.1 ad /* ARGSUSED */
93 1.1.2.1 ad int
94 1.1.2.1 ad compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
95 1.1.2.1 ad {
96 1.1.2.1 ad struct compat_16_sys___sigaction14_args /* {
97 1.1.2.1 ad syscallarg(int) signum;
98 1.1.2.1 ad syscallarg(const struct sigaction *) nsa;
99 1.1.2.1 ad syscallarg(struct sigaction *) osa;
100 1.1.2.1 ad } */ *uap = v;
101 1.1.2.1 ad struct sigaction nsa, osa;
102 1.1.2.1 ad int error;
103 1.1.2.1 ad
104 1.1.2.1 ad if (SCARG(uap, nsa)) {
105 1.1.2.1 ad error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
106 1.1.2.1 ad if (error)
107 1.1.2.1 ad return (error);
108 1.1.2.1 ad }
109 1.1.2.1 ad error = sigaction1(l, SCARG(uap, signum),
110 1.1.2.1 ad SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
111 1.1.2.1 ad NULL, 0);
112 1.1.2.1 ad if (error)
113 1.1.2.1 ad return (error);
114 1.1.2.1 ad if (SCARG(uap, osa)) {
115 1.1.2.1 ad error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
116 1.1.2.1 ad if (error)
117 1.1.2.1 ad return (error);
118 1.1.2.1 ad }
119 1.1.2.1 ad return (0);
120 1.1.2.1 ad }
121 1.1.2.1 ad #endif
122 1.1.2.1 ad
123 1.1.2.1 ad /* ARGSUSED */
124 1.1.2.1 ad int
125 1.1.2.1 ad sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
126 1.1.2.1 ad {
127 1.1.2.1 ad struct sys___sigaction_sigtramp_args /* {
128 1.1.2.1 ad syscallarg(int) signum;
129 1.1.2.1 ad syscallarg(const struct sigaction *) nsa;
130 1.1.2.1 ad syscallarg(struct sigaction *) osa;
131 1.1.2.1 ad syscallarg(void *) tramp;
132 1.1.2.1 ad syscallarg(int) vers;
133 1.1.2.1 ad } */ *uap = v;
134 1.1.2.1 ad struct sigaction nsa, osa;
135 1.1.2.1 ad int error;
136 1.1.2.1 ad
137 1.1.2.1 ad if (SCARG(uap, nsa)) {
138 1.1.2.1 ad error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
139 1.1.2.1 ad if (error)
140 1.1.2.1 ad return (error);
141 1.1.2.1 ad }
142 1.1.2.1 ad error = sigaction1(l, SCARG(uap, signum),
143 1.1.2.1 ad SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
144 1.1.2.1 ad SCARG(uap, tramp), SCARG(uap, vers));
145 1.1.2.1 ad if (error)
146 1.1.2.1 ad return (error);
147 1.1.2.1 ad if (SCARG(uap, osa)) {
148 1.1.2.1 ad error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
149 1.1.2.1 ad if (error)
150 1.1.2.1 ad return (error);
151 1.1.2.1 ad }
152 1.1.2.1 ad return (0);
153 1.1.2.1 ad }
154 1.1.2.1 ad
155 1.1.2.1 ad /*
156 1.1.2.1 ad * Manipulate signal mask. Note that we receive new mask, not pointer, and
157 1.1.2.1 ad * return old mask as return value; the library stub does the rest.
158 1.1.2.1 ad */
159 1.1.2.1 ad int
160 1.1.2.1 ad sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
161 1.1.2.1 ad {
162 1.1.2.1 ad struct sys___sigprocmask14_args /* {
163 1.1.2.1 ad syscallarg(int) how;
164 1.1.2.1 ad syscallarg(const sigset_t *) set;
165 1.1.2.1 ad syscallarg(sigset_t *) oset;
166 1.1.2.1 ad } */ *uap = v;
167 1.1.2.1 ad sigset_t nss, oss;
168 1.1.2.1 ad int error;
169 1.1.2.1 ad
170 1.1.2.1 ad if (SCARG(uap, set)) {
171 1.1.2.1 ad error = copyin(SCARG(uap, set), &nss, sizeof(nss));
172 1.1.2.1 ad if (error)
173 1.1.2.1 ad return (error);
174 1.1.2.1 ad }
175 1.1.2.1 ad error = sigprocmask1(l, SCARG(uap, how),
176 1.1.2.1 ad SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
177 1.1.2.1 ad if (error)
178 1.1.2.1 ad return (error);
179 1.1.2.1 ad if (SCARG(uap, oset)) {
180 1.1.2.1 ad error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
181 1.1.2.1 ad if (error)
182 1.1.2.1 ad return (error);
183 1.1.2.1 ad }
184 1.1.2.1 ad return (0);
185 1.1.2.1 ad }
186 1.1.2.1 ad
187 1.1.2.1 ad /* ARGSUSED */
188 1.1.2.1 ad int
189 1.1.2.1 ad sys___sigpending14(struct lwp *l, void *v, register_t *retval)
190 1.1.2.1 ad {
191 1.1.2.1 ad struct sys___sigpending14_args /* {
192 1.1.2.1 ad syscallarg(sigset_t *) set;
193 1.1.2.1 ad } */ *uap = v;
194 1.1.2.1 ad sigset_t ss;
195 1.1.2.1 ad
196 1.1.2.1 ad sigpending1(l, &ss);
197 1.1.2.1 ad return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
198 1.1.2.1 ad }
199 1.1.2.1 ad
200 1.1.2.1 ad /*
201 1.1.2.1 ad * Suspend process until signal, providing mask to be set in the meantime.
202 1.1.2.1 ad * Note nonstandard calling convention: libc stub passes mask, not pointer,
203 1.1.2.1 ad * to save a copyin.
204 1.1.2.1 ad */
205 1.1.2.1 ad /* ARGSUSED */
206 1.1.2.1 ad int
207 1.1.2.1 ad sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
208 1.1.2.1 ad {
209 1.1.2.1 ad struct sys___sigsuspend14_args /* {
210 1.1.2.1 ad syscallarg(const sigset_t *) set;
211 1.1.2.1 ad } */ *uap = v;
212 1.1.2.1 ad sigset_t ss;
213 1.1.2.1 ad int error;
214 1.1.2.1 ad
215 1.1.2.1 ad if (SCARG(uap, set)) {
216 1.1.2.1 ad error = copyin(SCARG(uap, set), &ss, sizeof(ss));
217 1.1.2.1 ad if (error)
218 1.1.2.1 ad return (error);
219 1.1.2.1 ad }
220 1.1.2.1 ad
221 1.1.2.1 ad return (sigsuspend1(l, SCARG(uap, set) ? &ss : 0));
222 1.1.2.1 ad }
223 1.1.2.1 ad
224 1.1.2.1 ad /* ARGSUSED */
225 1.1.2.1 ad int
226 1.1.2.1 ad sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
227 1.1.2.1 ad {
228 1.1.2.1 ad struct sys___sigaltstack14_args /* {
229 1.1.2.1 ad syscallarg(const struct sigaltstack *) nss;
230 1.1.2.1 ad syscallarg(struct sigaltstack *) oss;
231 1.1.2.1 ad } */ *uap = v;
232 1.1.2.1 ad struct sigaltstack nss, oss;
233 1.1.2.1 ad int error;
234 1.1.2.1 ad
235 1.1.2.1 ad if (SCARG(uap, nss)) {
236 1.1.2.1 ad error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
237 1.1.2.1 ad if (error)
238 1.1.2.1 ad return (error);
239 1.1.2.1 ad }
240 1.1.2.1 ad error = sigaltstack1(l,
241 1.1.2.1 ad SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
242 1.1.2.1 ad if (error)
243 1.1.2.1 ad return (error);
244 1.1.2.1 ad if (SCARG(uap, oss)) {
245 1.1.2.1 ad error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
246 1.1.2.1 ad if (error)
247 1.1.2.1 ad return (error);
248 1.1.2.1 ad }
249 1.1.2.1 ad return (0);
250 1.1.2.1 ad }
251 1.1.2.1 ad
252 1.1.2.1 ad /* ARGSUSED */
253 1.1.2.1 ad int
254 1.1.2.1 ad sys_kill(struct lwp *l, void *v, register_t *retval)
255 1.1.2.1 ad {
256 1.1.2.1 ad struct sys_kill_args /* {
257 1.1.2.1 ad syscallarg(int) pid;
258 1.1.2.1 ad syscallarg(int) signum;
259 1.1.2.1 ad } */ *uap = v;
260 1.1.2.1 ad struct proc *p;
261 1.1.2.1 ad ksiginfo_t ksi;
262 1.1.2.1 ad int signum = SCARG(uap, signum);
263 1.1.2.1 ad int error;
264 1.1.2.1 ad
265 1.1.2.1 ad if ((u_int)signum >= NSIG)
266 1.1.2.1 ad return (EINVAL);
267 1.1.2.1 ad KSI_INIT(&ksi);
268 1.1.2.1 ad ksi.ksi_signo = signum;
269 1.1.2.1 ad ksi.ksi_code = SI_USER;
270 1.1.2.1 ad ksi.ksi_pid = l->l_proc->p_pid;
271 1.1.2.1 ad ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
272 1.1.2.1 ad if (SCARG(uap, pid) > 0) {
273 1.1.2.1 ad /* kill single process */
274 1.1.2.1 ad if ((p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL)) == NULL)
275 1.1.2.1 ad return (ESRCH);
276 1.1.2.1 ad mutex_enter(&p->p_crmutex);
277 1.1.2.1 ad error = kauth_authorize_process(l->l_cred,
278 1.1.2.1 ad KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum,
279 1.1.2.1 ad NULL, NULL);
280 1.1.2.1 ad if (!error && signum) {
281 1.1.2.1 ad mutex_enter(&p->p_smutex);
282 1.1.2.1 ad kpsignal2(p, &ksi);
283 1.1.2.1 ad mutex_exit(&p->p_smutex);
284 1.1.2.1 ad }
285 1.1.2.1 ad mutex_exit(&p->p_crmutex);
286 1.1.2.1 ad rw_exit(&proclist_lock);
287 1.1.2.1 ad return (0);
288 1.1.2.1 ad }
289 1.1.2.1 ad switch (SCARG(uap, pid)) {
290 1.1.2.1 ad case -1: /* broadcast signal */
291 1.1.2.1 ad return (killpg1(l, &ksi, 0, 1));
292 1.1.2.1 ad case 0: /* signal own process group */
293 1.1.2.1 ad return (killpg1(l, &ksi, 0, 0));
294 1.1.2.1 ad default: /* negative explicit process group */
295 1.1.2.1 ad return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
296 1.1.2.1 ad }
297 1.1.2.1 ad /* NOTREACHED */
298 1.1.2.1 ad }
299 1.1.2.1 ad
300 1.1.2.1 ad /*
301 1.1.2.1 ad * Nonexistent system call-- signal process (may want to handle it). Flag
302 1.1.2.1 ad * error in case process won't see signal immediately (blocked or ignored).
303 1.1.2.1 ad *
304 1.1.2.1 ad * XXX This should not be here.
305 1.1.2.1 ad */
306 1.1.2.1 ad #ifndef PTRACE
307 1.1.2.1 ad __weak_alias(sys_ptrace, sys_nosys);
308 1.1.2.1 ad #endif
309 1.1.2.1 ad
310 1.1.2.1 ad /* ARGSUSED */
311 1.1.2.1 ad int
312 1.1.2.1 ad sys_nosys(struct lwp *l, void *v, register_t *retval)
313 1.1.2.1 ad {
314 1.1.2.1 ad struct proc *p;
315 1.1.2.1 ad
316 1.1.2.1 ad p = l->l_proc;
317 1.1.2.1 ad psignal(p, SIGSYS);
318 1.1.2.1 ad return (ENOSYS);
319 1.1.2.1 ad }
320 1.1.2.1 ad
321 1.1.2.1 ad /* ARGSUSED */
322 1.1.2.1 ad int
323 1.1.2.1 ad sys_getcontext(struct lwp *l, void *v, register_t *retval)
324 1.1.2.1 ad {
325 1.1.2.1 ad struct sys_getcontext_args /* {
326 1.1.2.1 ad syscallarg(struct __ucontext *) ucp;
327 1.1.2.1 ad } */ *uap = v;
328 1.1.2.1 ad ucontext_t uc;
329 1.1.2.1 ad
330 1.1.2.1 ad getucontext(l, &uc);
331 1.1.2.1 ad
332 1.1.2.1 ad return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
333 1.1.2.1 ad }
334 1.1.2.1 ad
335 1.1.2.1 ad /* ARGSUSED */
336 1.1.2.1 ad int
337 1.1.2.1 ad sys_setcontext(struct lwp *l, void *v, register_t *retval)
338 1.1.2.1 ad {
339 1.1.2.1 ad struct sys_setcontext_args /* {
340 1.1.2.1 ad syscallarg(const ucontext_t *) ucp;
341 1.1.2.1 ad } */ *uap = v;
342 1.1.2.1 ad ucontext_t uc;
343 1.1.2.1 ad int error;
344 1.1.2.1 ad
345 1.1.2.1 ad if (SCARG(uap, ucp) == NULL) { /* i.e. end of uc_link chain */
346 1.1.2.1 ad /* Acquire the sched state mutex. exit1() will release it. */
347 1.1.2.1 ad mutex_enter(&l->l_proc->p_smutex);
348 1.1.2.1 ad exit1(l, W_EXITCODE(0, 0));
349 1.1.2.1 ad } else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
350 1.1.2.1 ad (error = setucontext(l, &uc)) != 0)
351 1.1.2.1 ad return (error);
352 1.1.2.1 ad
353 1.1.2.1 ad return (EJUSTRETURN);
354 1.1.2.1 ad }
355 1.1.2.1 ad
356 1.1.2.1 ad /*
357 1.1.2.1 ad * sigtimedwait(2) system call, used also for implementation
358 1.1.2.1 ad * of sigwaitinfo() and sigwait().
359 1.1.2.1 ad *
360 1.1.2.1 ad * This only handles single LWP in signal wait. libpthread provides
361 1.1.2.1 ad * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
362 1.1.2.1 ad */
363 1.1.2.1 ad int
364 1.1.2.1 ad sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
365 1.1.2.1 ad {
366 1.1.2.1 ad
367 1.1.2.1 ad return __sigtimedwait1(l, v, retval, copyout, copyin, copyout);
368 1.1.2.1 ad }
369 1.1.2.1 ad
370 1.1.2.1 ad int
371 1.1.2.1 ad sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
372 1.1.2.1 ad struct sigaction *osa, const void *tramp, int vers)
373 1.1.2.1 ad {
374 1.1.2.1 ad struct proc *p;
375 1.1.2.1 ad struct sigacts *ps;
376 1.1.2.1 ad sigset_t tset;
377 1.1.2.1 ad int prop;
378 1.1.2.1 ad
379 1.1.2.1 ad if (signum <= 0 || signum >= NSIG)
380 1.1.2.1 ad return (EINVAL);
381 1.1.2.1 ad
382 1.1.2.1 ad p = l->l_proc;
383 1.1.2.1 ad
384 1.1.2.1 ad /*
385 1.1.2.1 ad * Trampoline ABI version 0 is reserved for the legacy kernel
386 1.1.2.1 ad * provided on-stack trampoline. Conversely, if we are using a
387 1.1.2.1 ad * non-0 ABI version, we must have a trampoline. Only validate the
388 1.1.2.1 ad * vers if a new sigaction was supplied. Emulations use legacy
389 1.1.2.1 ad * kernel trampolines with version 0, alternatively check for that
390 1.1.2.1 ad * too.
391 1.1.2.1 ad */
392 1.1.2.1 ad if ((vers != 0 && tramp == NULL) ||
393 1.1.2.1 ad #ifdef SIGTRAMP_VALID
394 1.1.2.1 ad (nsa != NULL &&
395 1.1.2.1 ad ((vers == 0) ?
396 1.1.2.1 ad (p->p_emul->e_sigcode == NULL) :
397 1.1.2.1 ad !SIGTRAMP_VALID(vers))) ||
398 1.1.2.1 ad #endif
399 1.1.2.1 ad (vers == 0 && tramp != NULL)) {
400 1.1.2.1 ad return (EINVAL);
401 1.1.2.1 ad }
402 1.1.2.1 ad
403 1.1.2.1 ad mutex_enter(&p->p_smutex);
404 1.1.2.1 ad ps = p->p_sigacts;
405 1.1.2.1 ad if (osa)
406 1.1.2.1 ad *osa = SIGACTION_PS(ps, signum);
407 1.1.2.1 ad if (!nsa) {
408 1.1.2.1 ad mutex_exit(&p->p_smutex);
409 1.1.2.1 ad return (0);
410 1.1.2.1 ad }
411 1.1.2.1 ad
412 1.1.2.1 ad prop = sigprop[signum];
413 1.1.2.1 ad if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
414 1.1.2.1 ad mutex_exit(&p->p_smutex);
415 1.1.2.1 ad return (EINVAL);
416 1.1.2.1 ad }
417 1.1.2.1 ad
418 1.1.2.1 ad SIGACTION_PS(ps, signum) = *nsa;
419 1.1.2.1 ad ps->sa_sigdesc[signum].sd_tramp = tramp;
420 1.1.2.1 ad ps->sa_sigdesc[signum].sd_vers = vers;
421 1.1.2.1 ad sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
422 1.1.2.1 ad
423 1.1.2.1 ad if ((prop & SA_NORESET) != 0)
424 1.1.2.1 ad SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
425 1.1.2.1 ad
426 1.1.2.1 ad if (signum == SIGCHLD) {
427 1.1.2.1 ad if (nsa->sa_flags & SA_NOCLDSTOP)
428 1.1.2.1 ad p->p_flag |= P_NOCLDSTOP;
429 1.1.2.1 ad else
430 1.1.2.1 ad p->p_flag &= ~P_NOCLDSTOP;
431 1.1.2.1 ad if (nsa->sa_flags & SA_NOCLDWAIT) {
432 1.1.2.1 ad /*
433 1.1.2.1 ad * Paranoia: since SA_NOCLDWAIT is implemented by
434 1.1.2.1 ad * reparenting the dying child to PID 1 (and trust
435 1.1.2.1 ad * it to reap the zombie), PID 1 itself is forbidden
436 1.1.2.1 ad * to set SA_NOCLDWAIT.
437 1.1.2.1 ad */
438 1.1.2.1 ad if (p->p_pid == 1)
439 1.1.2.1 ad p->p_flag &= ~P_NOCLDWAIT;
440 1.1.2.1 ad else
441 1.1.2.1 ad p->p_flag |= P_NOCLDWAIT;
442 1.1.2.1 ad } else
443 1.1.2.1 ad p->p_flag &= ~P_NOCLDWAIT;
444 1.1.2.1 ad
445 1.1.2.1 ad if (nsa->sa_handler == SIG_IGN) {
446 1.1.2.1 ad /*
447 1.1.2.1 ad * Paranoia: same as above.
448 1.1.2.1 ad */
449 1.1.2.1 ad if (p->p_pid == 1)
450 1.1.2.1 ad p->p_flag &= ~P_CLDSIGIGN;
451 1.1.2.1 ad else
452 1.1.2.1 ad p->p_flag |= P_CLDSIGIGN;
453 1.1.2.1 ad } else
454 1.1.2.1 ad p->p_flag &= ~P_CLDSIGIGN;
455 1.1.2.1 ad }
456 1.1.2.1 ad
457 1.1.2.1 ad if ((nsa->sa_flags & SA_NODEFER) == 0)
458 1.1.2.1 ad sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
459 1.1.2.1 ad else
460 1.1.2.1 ad sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
461 1.1.2.1 ad
462 1.1.2.1 ad /*
463 1.1.2.1 ad * Set bit in p_sigctx.ps_sigignore for signals that are set to
464 1.1.2.1 ad * SIG_IGN, and for signals set to SIG_DFL where the default is to
465 1.1.2.1 ad * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
466 1.1.2.1 ad * we have to restart the process.
467 1.1.2.1 ad */
468 1.1.2.1 ad if (nsa->sa_handler == SIG_IGN ||
469 1.1.2.1 ad (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
470 1.1.2.1 ad /* Never to be seen again. */
471 1.1.2.1 ad sigemptyset(&tset);
472 1.1.2.1 ad sigaddset(&tset, signum);
473 1.1.2.1 ad sigclearall(p, &tset);
474 1.1.2.1 ad if (signum != SIGCONT) {
475 1.1.2.1 ad /* Easier in psignal */
476 1.1.2.1 ad sigaddset(&p->p_sigctx.ps_sigignore, signum);
477 1.1.2.1 ad }
478 1.1.2.1 ad sigdelset(&p->p_sigctx.ps_sigcatch, signum);
479 1.1.2.1 ad } else {
480 1.1.2.1 ad sigdelset(&p->p_sigctx.ps_sigignore, signum);
481 1.1.2.1 ad if (nsa->sa_handler == SIG_DFL)
482 1.1.2.1 ad sigdelset(&p->p_sigctx.ps_sigcatch, signum);
483 1.1.2.1 ad else
484 1.1.2.1 ad sigaddset(&p->p_sigctx.ps_sigcatch, signum);
485 1.1.2.1 ad }
486 1.1.2.1 ad
487 1.1.2.1 ad /*
488 1.1.2.1 ad * Previously held signals may now have become visible. Ensure that
489 1.1.2.1 ad * we check for them before returning to userspace.
490 1.1.2.1 ad */
491 1.1.2.1 ad lwp_lock(l);
492 1.1.2.1 ad signotify(l);
493 1.1.2.1 ad lwp_unlock(l);
494 1.1.2.1 ad
495 1.1.2.1 ad mutex_exit(&p->p_smutex);
496 1.1.2.1 ad return (0);
497 1.1.2.1 ad }
498 1.1.2.1 ad
499 1.1.2.1 ad int
500 1.1.2.1 ad sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
501 1.1.2.1 ad {
502 1.1.2.1 ad struct proc *p = l->l_proc;
503 1.1.2.1 ad int more;
504 1.1.2.1 ad
505 1.1.2.1 ad mutex_enter(&p->p_smutex);
506 1.1.2.1 ad
507 1.1.2.1 ad if (oss)
508 1.1.2.1 ad *oss = l->l_sigmask;
509 1.1.2.1 ad if (nss) {
510 1.1.2.1 ad switch (how) {
511 1.1.2.1 ad case SIG_BLOCK:
512 1.1.2.1 ad sigplusset(nss, &l->l_sigmask);
513 1.1.2.1 ad more = 0;
514 1.1.2.1 ad break;
515 1.1.2.1 ad case SIG_UNBLOCK:
516 1.1.2.1 ad sigminusset(nss, &l->l_sigmask);
517 1.1.2.1 ad more = 1;
518 1.1.2.1 ad break;
519 1.1.2.1 ad case SIG_SETMASK:
520 1.1.2.1 ad l->l_sigmask = *nss;
521 1.1.2.1 ad more = 1;
522 1.1.2.1 ad break;
523 1.1.2.1 ad default:
524 1.1.2.1 ad mutex_exit(&p->p_smutex);
525 1.1.2.1 ad return (EINVAL);
526 1.1.2.1 ad }
527 1.1.2.1 ad sigminusset(&sigcantmask, &l->l_sigmask);
528 1.1.2.1 ad if (more) {
529 1.1.2.1 ad /*
530 1.1.2.1 ad * Pinch any signals from the per-process pending
531 1.1.2.1 ad * list that are now of interest to us.
532 1.1.2.1 ad */
533 1.1.2.1 ad sigpinch(&p->p_sigpend, &l->l_sigpend, &l->l_sigmask);
534 1.1.2.1 ad
535 1.1.2.1 ad /*
536 1.1.2.1 ad * Check for pending signals on return to user.
537 1.1.2.1 ad */
538 1.1.2.1 ad lwp_lock(l);
539 1.1.2.1 ad signotify(l);
540 1.1.2.1 ad lwp_unlock(l);
541 1.1.2.1 ad }
542 1.1.2.1 ad }
543 1.1.2.1 ad
544 1.1.2.1 ad mutex_exit(&p->p_smutex);
545 1.1.2.1 ad
546 1.1.2.1 ad return (0);
547 1.1.2.1 ad }
548 1.1.2.1 ad
549 1.1.2.1 ad void
550 1.1.2.1 ad sigpending1(struct lwp *l, sigset_t *ss)
551 1.1.2.1 ad {
552 1.1.2.1 ad struct proc *p = l->l_proc;
553 1.1.2.1 ad
554 1.1.2.1 ad mutex_enter(&p->p_smutex);
555 1.1.2.1 ad *ss = l->l_sigpend.sp_set;
556 1.1.2.1 ad sigplusset(&p->p_sigpend.sp_set, ss);
557 1.1.2.1 ad sigminusset(&l->l_sigmask, ss);
558 1.1.2.1 ad mutex_exit(&p->p_smutex);
559 1.1.2.1 ad }
560 1.1.2.1 ad
561 1.1.2.1 ad int
562 1.1.2.1 ad sigsuspend1(struct lwp *l, const sigset_t *ss)
563 1.1.2.1 ad {
564 1.1.2.1 ad struct proc *p;
565 1.1.2.1 ad struct sigacts *ps;
566 1.1.2.1 ad
567 1.1.2.1 ad p = l->l_proc;
568 1.1.2.1 ad ps = p->p_sigacts;
569 1.1.2.1 ad
570 1.1.2.1 ad mutex_enter(&p->p_smutex);
571 1.1.2.1 ad
572 1.1.2.1 ad if (ss) {
573 1.1.2.1 ad /*
574 1.1.2.1 ad * When returning from sigpause, we want
575 1.1.2.1 ad * the old mask to be restored after the
576 1.1.2.1 ad * signal handler has finished. Thus, we
577 1.1.2.1 ad * save it here and mark the sigctx structure
578 1.1.2.1 ad * to indicate this.
579 1.1.2.1 ad */
580 1.1.2.1 ad l->l_sigoldmask = l->l_sigmask;
581 1.1.2.1 ad l->l_sigrestore = 1;
582 1.1.2.1 ad l->l_sigmask = *ss;
583 1.1.2.1 ad sigminusset(&sigcantmask, &l->l_sigmask);
584 1.1.2.1 ad lwp_lock(l);
585 1.1.2.1 ad signotify(l);
586 1.1.2.1 ad lwp_unlock(l);
587 1.1.2.1 ad }
588 1.1.2.1 ad
589 1.1.2.1 ad while (mtsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0,
590 1.1.2.1 ad &p->p_smutex) == 0)
591 1.1.2.1 ad /* void */;
592 1.1.2.1 ad
593 1.1.2.1 ad mutex_exit(&p->p_smutex);
594 1.1.2.1 ad
595 1.1.2.1 ad /* always return EINTR rather than ERESTART... */
596 1.1.2.1 ad return (EINTR);
597 1.1.2.1 ad }
598 1.1.2.1 ad
599 1.1.2.1 ad int
600 1.1.2.1 ad sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
601 1.1.2.1 ad struct sigaltstack *oss)
602 1.1.2.1 ad {
603 1.1.2.1 ad
604 1.1.2.1 ad if (oss)
605 1.1.2.1 ad *oss = l->l_sigstk;
606 1.1.2.1 ad
607 1.1.2.1 ad if (nss) {
608 1.1.2.1 ad if (nss->ss_flags & ~SS_ALLBITS)
609 1.1.2.1 ad return (EINVAL);
610 1.1.2.1 ad
611 1.1.2.1 ad if (nss->ss_flags & SS_DISABLE) {
612 1.1.2.1 ad if (l->l_sigstk.ss_flags & SS_ONSTACK)
613 1.1.2.1 ad return (EINVAL);
614 1.1.2.1 ad } else {
615 1.1.2.1 ad if (nss->ss_size < MINSIGSTKSZ)
616 1.1.2.1 ad return (ENOMEM);
617 1.1.2.1 ad }
618 1.1.2.1 ad l->l_sigstk = *nss;
619 1.1.2.1 ad }
620 1.1.2.1 ad
621 1.1.2.1 ad return (0);
622 1.1.2.1 ad }
623 1.1.2.1 ad
624 1.1.2.1 ad int
625 1.1.2.1 ad __sigtimedwait1(struct lwp *l, void *v, register_t *retval,
626 1.1.2.1 ad copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
627 1.1.2.1 ad {
628 1.1.2.1 ad struct sys___sigtimedwait_args /* {
629 1.1.2.1 ad syscallarg(const sigset_t *) set;
630 1.1.2.1 ad syscallarg(siginfo_t *) info;
631 1.1.2.1 ad syscallarg(struct timespec *) timeout;
632 1.1.2.1 ad } */ *uap = v;
633 1.1.2.1 ad sigset_t *waitset;
634 1.1.2.1 ad struct proc *p = l->l_proc;
635 1.1.2.1 ad int error, signum;
636 1.1.2.1 ad int timo = 0;
637 1.1.2.1 ad struct timespec ts, tsstart, tsnow;
638 1.1.2.1 ad ksiginfo_t *ksi;
639 1.1.2.1 ad
640 1.1.2.1 ad memset(&tsstart, 0, sizeof tsstart); /* XXX gcc */
641 1.1.2.1 ad
642 1.1.2.1 ad /*
643 1.1.2.1 ad * Calculate timeout, if it was specified.
644 1.1.2.1 ad */
645 1.1.2.1 ad if (SCARG(uap, timeout)) {
646 1.1.2.1 ad uint64_t ms;
647 1.1.2.1 ad
648 1.1.2.1 ad if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
649 1.1.2.1 ad return (error);
650 1.1.2.1 ad
651 1.1.2.1 ad ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
652 1.1.2.1 ad timo = mstohz(ms);
653 1.1.2.1 ad if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
654 1.1.2.1 ad timo = 1;
655 1.1.2.1 ad if (timo <= 0)
656 1.1.2.1 ad return (EAGAIN);
657 1.1.2.1 ad
658 1.1.2.1 ad /*
659 1.1.2.1 ad * Remember current uptime, it would be used in
660 1.1.2.1 ad * ECANCELED/ERESTART case.
661 1.1.2.1 ad */
662 1.1.2.1 ad getnanouptime(&tsstart);
663 1.1.2.1 ad }
664 1.1.2.1 ad
665 1.1.2.1 ad MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
666 1.1.2.1 ad if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
667 1.1.2.1 ad FREE(waitset, M_TEMP);
668 1.1.2.1 ad return (error);
669 1.1.2.1 ad }
670 1.1.2.1 ad
671 1.1.2.1 ad /*
672 1.1.2.1 ad * Silently ignore SA_CANTMASK signals. psignal1() would ignore
673 1.1.2.1 ad * SA_CANTMASK signals in waitset, we do this only for the below
674 1.1.2.1 ad * siglist check.
675 1.1.2.1 ad */
676 1.1.2.1 ad sigminusset(&sigcantmask, waitset);
677 1.1.2.1 ad
678 1.1.2.1 ad /*
679 1.1.2.1 ad * Allocate a ksi up front. We can't sleep with the mutex held.
680 1.1.2.1 ad */
681 1.1.2.1 ad ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
682 1.1.2.1 ad
683 1.1.2.1 ad /*
684 1.1.2.1 ad * First scan the per-proc and per-LWP lists and check if there is
685 1.1.2.1 ad * signal from our waitset already pending.
686 1.1.2.1 ad */
687 1.1.2.1 ad mutex_enter(&p->p_smutex);
688 1.1.2.1 ad
689 1.1.2.1 ad if ((signum = sigget(&p->p_sigpend, ksi, 0, waitset)) == 0)
690 1.1.2.1 ad signum = sigget(&l->l_sigpend, ksi, 0, waitset);
691 1.1.2.1 ad
692 1.1.2.1 ad if (signum != 0) {
693 1.1.2.1 ad /*
694 1.1.2.1 ad * We found a pending signal - copy it out to the user.
695 1.1.2.1 ad */
696 1.1.2.1 ad mutex_exit(&p->p_smutex);
697 1.1.2.1 ad error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
698 1.1.2.1 ad sizeof(ksi->ksi_info));
699 1.1.2.1 ad pool_put(&ksiginfo_pool, ksi);
700 1.1.2.1 ad return error;
701 1.1.2.1 ad }
702 1.1.2.1 ad
703 1.1.2.1 ad /*
704 1.1.2.1 ad * Set up the sigwait list. Pass pointer to malloced memory here;
705 1.1.2.1 ad * it's not possible to pass pointer to a structure on current
706 1.1.2.1 ad * process's stack, the current LWP might be swapped out when the
707 1.1.2.1 ad * when the signal is delivered.
708 1.1.2.1 ad */
709 1.1.2.1 ad l->l_sigwaited = ksi;
710 1.1.2.1 ad l->l_sigwait = waitset;
711 1.1.2.1 ad
712 1.1.2.1 ad /*
713 1.1.2.1 ad * Wait for signal to arrive. We can either be woken up or time out.
714 1.1.2.1 ad */
715 1.1.2.1 ad error = mtsleep(&l->l_sigwait, PPAUSE|PCATCH, "sigwait", timo,
716 1.1.2.1 ad &p->p_smutex);
717 1.1.2.1 ad
718 1.1.2.1 ad /*
719 1.1.2.1 ad * Need to find out if we woke as a result of lwp_wakeup() or a
720 1.1.2.1 ad * signal outside our wait set.
721 1.1.2.1 ad */
722 1.1.2.1 ad if (l->l_sigwaited != NULL) {
723 1.1.2.1 ad if (error == EINTR) {
724 1.1.2.1 ad /* wakeup via _lwp_wakeup() */
725 1.1.2.1 ad error = ECANCELED;
726 1.1.2.1 ad } else if (!error) {
727 1.1.2.1 ad /* spurious wakeup - arrange for syscall restart */
728 1.1.2.1 ad error = ERESTART;
729 1.1.2.1 ad }
730 1.1.2.1 ad }
731 1.1.2.1 ad
732 1.1.2.1 ad /*
733 1.1.2.1 ad * Clear the sigwait indication and unlock.
734 1.1.2.1 ad */
735 1.1.2.1 ad l->l_sigwait = NULL;
736 1.1.2.1 ad l->l_sigwaited = NULL;
737 1.1.2.1 ad mutex_exit(&p->p_smutex);
738 1.1.2.1 ad
739 1.1.2.1 ad /*
740 1.1.2.1 ad * If the sleep was interrupted (either by signal or wakeup), update
741 1.1.2.1 ad * the timeout and copyout new value back. It would be used when
742 1.1.2.1 ad * the syscall would be restarted or called again.
743 1.1.2.1 ad */
744 1.1.2.1 ad if (timo && (error == ERESTART || error == ECANCELED)) {
745 1.1.2.1 ad getnanouptime(&tsnow);
746 1.1.2.1 ad
747 1.1.2.1 ad /* compute how much time has passed since start */
748 1.1.2.1 ad timespecsub(&tsnow, &tsstart, &tsnow);
749 1.1.2.1 ad /* substract passed time from timeout */
750 1.1.2.1 ad timespecsub(&ts, &tsnow, &ts);
751 1.1.2.1 ad
752 1.1.2.1 ad if (ts.tv_sec < 0)
753 1.1.2.1 ad error = EAGAIN;
754 1.1.2.1 ad else {
755 1.1.2.1 ad /* copy updated timeout to userland */
756 1.1.2.1 ad error = (*put_timeout)(&ts, SCARG(uap, timeout),
757 1.1.2.1 ad sizeof(ts));
758 1.1.2.1 ad }
759 1.1.2.1 ad }
760 1.1.2.1 ad
761 1.1.2.1 ad /*
762 1.1.2.1 ad * If a signal from the wait set arrived, copy it to userland.
763 1.1.2.1 ad * Copy only the used part of siginfo, the padding part is
764 1.1.2.1 ad * left unchanged (userland is not supposed to touch it anyway).
765 1.1.2.1 ad */
766 1.1.2.1 ad FREE(waitset, M_TEMP);
767 1.1.2.1 ad pool_put(&ksiginfo_pool, ksi);
768 1.1.2.1 ad
769 1.1.2.1 ad if (error == 0)
770 1.1.2.1 ad error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
771 1.1.2.1 ad sizeof(ksi->ksi_info));
772 1.1.2.1 ad
773 1.1.2.1 ad return error;
774 1.1.2.1 ad }
775