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