kern_sig.c revision 1.194 1 1.194 simonb /* $NetBSD: kern_sig.c,v 1.194 2004/04/25 16:42:41 simonb Exp $ */
2 1.29 cgd
3 1.29 cgd /*
4 1.29 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 1.29 cgd * The Regents of the University of California. All rights reserved.
6 1.29 cgd * (c) UNIX System Laboratories, Inc.
7 1.29 cgd * All or some portions of this file are derived from material licensed
8 1.29 cgd * to the University of California by American Telephone and Telegraph
9 1.29 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.29 cgd * the permission of UNIX System Laboratories, Inc.
11 1.29 cgd *
12 1.29 cgd * Redistribution and use in source and binary forms, with or without
13 1.29 cgd * modification, are permitted provided that the following conditions
14 1.29 cgd * are met:
15 1.29 cgd * 1. Redistributions of source code must retain the above copyright
16 1.29 cgd * notice, this list of conditions and the following disclaimer.
17 1.29 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.29 cgd * notice, this list of conditions and the following disclaimer in the
19 1.29 cgd * documentation and/or other materials provided with the distribution.
20 1.146 agc * 3. Neither the name of the University nor the names of its contributors
21 1.29 cgd * may be used to endorse or promote products derived from this software
22 1.29 cgd * without specific prior written permission.
23 1.29 cgd *
24 1.29 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.29 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.29 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.29 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.29 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.29 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.29 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.29 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.29 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.29 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.29 cgd * SUCH DAMAGE.
35 1.29 cgd *
36 1.71 fvdl * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
37 1.29 cgd */
38 1.116 lukem
39 1.116 lukem #include <sys/cdefs.h>
40 1.194 simonb __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.194 2004/04/25 16:42:41 simonb Exp $");
41 1.70 mrg
42 1.73 thorpej #include "opt_ktrace.h"
43 1.74 thorpej #include "opt_compat_sunos.h"
44 1.158 christos #include "opt_compat_netbsd.h"
45 1.95 eeh #include "opt_compat_netbsd32.h"
46 1.29 cgd
47 1.29 cgd #define SIGPROP /* include signal properties table */
48 1.29 cgd #include <sys/param.h>
49 1.29 cgd #include <sys/signalvar.h>
50 1.29 cgd #include <sys/resourcevar.h>
51 1.29 cgd #include <sys/namei.h>
52 1.29 cgd #include <sys/vnode.h>
53 1.29 cgd #include <sys/proc.h>
54 1.29 cgd #include <sys/systm.h>
55 1.29 cgd #include <sys/timeb.h>
56 1.29 cgd #include <sys/times.h>
57 1.29 cgd #include <sys/buf.h>
58 1.29 cgd #include <sys/acct.h>
59 1.29 cgd #include <sys/file.h>
60 1.29 cgd #include <sys/kernel.h>
61 1.29 cgd #include <sys/wait.h>
62 1.29 cgd #include <sys/ktrace.h>
63 1.29 cgd #include <sys/syslog.h>
64 1.29 cgd #include <sys/stat.h>
65 1.29 cgd #include <sys/core.h>
66 1.59 cgd #include <sys/filedesc.h>
67 1.89 thorpej #include <sys/malloc.h>
68 1.89 thorpej #include <sys/pool.h>
69 1.130 thorpej #include <sys/ucontext.h>
70 1.130 thorpej #include <sys/sa.h>
71 1.130 thorpej #include <sys/savar.h>
72 1.118 thorpej #include <sys/exec.h>
73 1.29 cgd
74 1.32 cgd #include <sys/mount.h>
75 1.32 cgd #include <sys/syscallargs.h>
76 1.32 cgd
77 1.29 cgd #include <machine/cpu.h>
78 1.29 cgd
79 1.29 cgd #include <sys/user.h> /* for coredump */
80 1.52 christos
81 1.69 mrg #include <uvm/uvm_extern.h>
82 1.69 mrg
83 1.151 christos static void child_psignal(struct proc *, int);
84 1.112 lukem static int build_corename(struct proc *, char [MAXPATHLEN]);
85 1.152 christos static void ksiginfo_exithook(struct proc *, void *);
86 1.160 christos static void ksiginfo_put(struct proc *, const ksiginfo_t *);
87 1.152 christos static ksiginfo_t *ksiginfo_get(struct proc *, int);
88 1.160 christos static void kpsignal2(struct proc *, const ksiginfo_t *, int);
89 1.152 christos
90 1.190 matt sigset_t contsigmask, stopsigmask, sigcantmask, sigtrapmask;
91 1.29 cgd
92 1.194 simonb POOL_INIT(sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
93 1.194 simonb &pool_allocator_nointr);
94 1.194 simonb POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
95 1.194 simonb &pool_allocator_nointr);
96 1.194 simonb POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL);
97 1.89 thorpej
98 1.29 cgd /*
99 1.29 cgd * Can process p, with pcred pc, send the signal signum to process q?
100 1.29 cgd */
101 1.112 lukem #define CANSIGNAL(p, pc, q, signum) \
102 1.29 cgd ((pc)->pc_ucred->cr_uid == 0 || \
103 1.29 cgd (pc)->p_ruid == (q)->p_cred->p_ruid || \
104 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
105 1.29 cgd (pc)->p_ruid == (q)->p_ucred->cr_uid || \
106 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
107 1.29 cgd ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
108 1.29 cgd
109 1.89 thorpej /*
110 1.155 christos * Remove and return the first ksiginfo element that matches our requested
111 1.155 christos * signal, or return NULL if one not found.
112 1.152 christos */
113 1.152 christos static ksiginfo_t *
114 1.152 christos ksiginfo_get(struct proc *p, int signo)
115 1.152 christos {
116 1.155 christos ksiginfo_t *ksi;
117 1.168 pk int s;
118 1.152 christos
119 1.168 pk s = splsoftclock();
120 1.155 christos simple_lock(&p->p_sigctx.ps_silock);
121 1.155 christos CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
122 1.155 christos if (ksi->ksi_signo == signo) {
123 1.155 christos CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
124 1.168 pk goto out;
125 1.155 christos }
126 1.152 christos }
127 1.168 pk ksi = NULL;
128 1.168 pk out:
129 1.155 christos simple_unlock(&p->p_sigctx.ps_silock);
130 1.168 pk splx(s);
131 1.168 pk return ksi;
132 1.152 christos }
133 1.152 christos
134 1.155 christos /*
135 1.155 christos * Append a new ksiginfo element to the list of pending ksiginfo's, if
136 1.155 christos * we need to (SA_SIGINFO was requested). We replace non RT signals if
137 1.155 christos * they already existed in the queue and we add new entries for RT signals,
138 1.155 christos * or for non RT signals with non-existing entries.
139 1.155 christos */
140 1.152 christos static void
141 1.160 christos ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
142 1.152 christos {
143 1.155 christos ksiginfo_t *kp;
144 1.155 christos struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
145 1.167 pk int s;
146 1.152 christos
147 1.155 christos if ((sa->sa_flags & SA_SIGINFO) == 0)
148 1.155 christos return;
149 1.192 matt /*
150 1.192 matt * If there's no info, don't save it.
151 1.192 matt */
152 1.192 matt if (KSI_EMPTY_P(ksi))
153 1.192 matt return;
154 1.152 christos
155 1.167 pk s = splsoftclock();
156 1.155 christos simple_lock(&p->p_sigctx.ps_silock);
157 1.152 christos #ifdef notyet /* XXX: QUEUING */
158 1.155 christos if (ksi->ksi_signo < SIGRTMIN)
159 1.152 christos #endif
160 1.155 christos {
161 1.155 christos CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
162 1.155 christos if (kp->ksi_signo == ksi->ksi_signo) {
163 1.174 jdolecek KSI_COPY(ksi, kp);
164 1.167 pk goto out;
165 1.155 christos }
166 1.155 christos }
167 1.153 christos }
168 1.155 christos kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
169 1.157 christos if (kp == NULL) {
170 1.157 christos #ifdef DIAGNOSTIC
171 1.157 christos printf("Out of memory allocating siginfo for pid %d\n",
172 1.157 christos p->p_pid);
173 1.157 christos #endif
174 1.167 pk goto out;
175 1.157 christos }
176 1.155 christos *kp = *ksi;
177 1.155 christos CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
178 1.167 pk out:
179 1.155 christos simple_unlock(&p->p_sigctx.ps_silock);
180 1.167 pk splx(s);
181 1.152 christos }
182 1.152 christos
183 1.152 christos /*
184 1.152 christos * free all pending ksiginfo on exit
185 1.152 christos */
186 1.152 christos static void
187 1.152 christos ksiginfo_exithook(struct proc *p, void *v)
188 1.152 christos {
189 1.168 pk int s;
190 1.152 christos
191 1.168 pk s = splsoftclock();
192 1.155 christos simple_lock(&p->p_sigctx.ps_silock);
193 1.155 christos while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
194 1.155 christos ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
195 1.155 christos CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
196 1.152 christos pool_put(&ksiginfo_pool, ksi);
197 1.152 christos }
198 1.155 christos simple_unlock(&p->p_sigctx.ps_silock);
199 1.168 pk splx(s);
200 1.152 christos }
201 1.152 christos
202 1.152 christos /*
203 1.89 thorpej * Initialize signal-related data structures.
204 1.89 thorpej */
205 1.89 thorpej void
206 1.112 lukem signal_init(void)
207 1.89 thorpej {
208 1.194 simonb
209 1.152 christos exithook_establish(ksiginfo_exithook, NULL);
210 1.152 christos exechook_establish(ksiginfo_exithook, NULL);
211 1.190 matt
212 1.190 matt sigaddset(&sigtrapmask, SIGSEGV);
213 1.190 matt sigaddset(&sigtrapmask, SIGBUS);
214 1.190 matt sigaddset(&sigtrapmask, SIGILL);
215 1.190 matt sigaddset(&sigtrapmask, SIGFPE);
216 1.190 matt sigaddset(&sigtrapmask, SIGTRAP);
217 1.89 thorpej }
218 1.89 thorpej
219 1.89 thorpej /*
220 1.109 jdolecek * Create an initial sigctx structure, using the same signal state
221 1.109 jdolecek * as p. If 'share' is set, share the sigctx_proc part, otherwise just
222 1.109 jdolecek * copy it from parent.
223 1.89 thorpej */
224 1.109 jdolecek void
225 1.112 lukem sigactsinit(struct proc *np, struct proc *pp, int share)
226 1.89 thorpej {
227 1.89 thorpej struct sigacts *ps;
228 1.89 thorpej
229 1.109 jdolecek if (share) {
230 1.109 jdolecek np->p_sigacts = pp->p_sigacts;
231 1.109 jdolecek pp->p_sigacts->sa_refcnt++;
232 1.109 jdolecek } else {
233 1.109 jdolecek ps = pool_get(&sigacts_pool, PR_WAITOK);
234 1.109 jdolecek if (pp)
235 1.109 jdolecek memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
236 1.109 jdolecek else
237 1.109 jdolecek memset(ps, '\0', sizeof(struct sigacts));
238 1.109 jdolecek ps->sa_refcnt = 1;
239 1.109 jdolecek np->p_sigacts = ps;
240 1.109 jdolecek }
241 1.89 thorpej }
242 1.89 thorpej
243 1.89 thorpej /*
244 1.109 jdolecek * Make this process not share its sigctx, maintaining all
245 1.89 thorpej * signal state.
246 1.89 thorpej */
247 1.89 thorpej void
248 1.112 lukem sigactsunshare(struct proc *p)
249 1.89 thorpej {
250 1.109 jdolecek struct sigacts *oldps;
251 1.89 thorpej
252 1.109 jdolecek if (p->p_sigacts->sa_refcnt == 1)
253 1.89 thorpej return;
254 1.89 thorpej
255 1.109 jdolecek oldps = p->p_sigacts;
256 1.109 jdolecek sigactsinit(p, NULL, 0);
257 1.109 jdolecek
258 1.109 jdolecek if (--oldps->sa_refcnt == 0)
259 1.109 jdolecek pool_put(&sigacts_pool, oldps);
260 1.89 thorpej }
261 1.89 thorpej
262 1.89 thorpej /*
263 1.109 jdolecek * Release a sigctx structure.
264 1.89 thorpej */
265 1.89 thorpej void
266 1.112 lukem sigactsfree(struct proc *p)
267 1.89 thorpej {
268 1.112 lukem struct sigacts *ps;
269 1.89 thorpej
270 1.112 lukem ps = p->p_sigacts;
271 1.109 jdolecek if (--ps->sa_refcnt > 0)
272 1.89 thorpej return;
273 1.89 thorpej
274 1.89 thorpej pool_put(&sigacts_pool, ps);
275 1.89 thorpej }
276 1.89 thorpej
277 1.79 mycroft int
278 1.112 lukem sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
279 1.162 matt struct sigaction *osa, const void *tramp, int vers)
280 1.79 mycroft {
281 1.112 lukem struct sigacts *ps;
282 1.112 lukem int prop;
283 1.79 mycroft
284 1.112 lukem ps = p->p_sigacts;
285 1.79 mycroft if (signum <= 0 || signum >= NSIG)
286 1.79 mycroft return (EINVAL);
287 1.79 mycroft
288 1.121 thorpej /*
289 1.121 thorpej * Trampoline ABI version 0 is reserved for the legacy
290 1.162 matt * kernel-provided on-stack trampoline. Conversely, if we are
291 1.162 matt * using a non-0 ABI version, we must have a trampoline. Only
292 1.163 christos * validate the vers if a new sigaction was supplied. Emulations
293 1.163 christos * use legacy kernel trampolines with version 0, alternatively
294 1.163 christos * check for that too.
295 1.121 thorpej */
296 1.121 thorpej if ((vers != 0 && tramp == NULL) ||
297 1.161 matt #ifdef SIGTRAMP_VALID
298 1.163 christos (nsa != NULL &&
299 1.163 christos ((vers == 0) ?
300 1.163 christos (p->p_emul->e_sigcode == NULL) :
301 1.163 christos !SIGTRAMP_VALID(vers))) ||
302 1.161 matt #endif
303 1.121 thorpej (vers == 0 && tramp != NULL))
304 1.121 thorpej return (EINVAL);
305 1.121 thorpej
306 1.79 mycroft if (osa)
307 1.109 jdolecek *osa = SIGACTION_PS(ps, signum);
308 1.79 mycroft
309 1.79 mycroft if (nsa) {
310 1.79 mycroft if (nsa->sa_flags & ~SA_ALLBITS)
311 1.79 mycroft return (EINVAL);
312 1.149 kleink
313 1.79 mycroft prop = sigprop[signum];
314 1.79 mycroft if (prop & SA_CANTMASK)
315 1.79 mycroft return (EINVAL);
316 1.79 mycroft
317 1.105 thorpej (void) splsched(); /* XXXSMP */
318 1.109 jdolecek SIGACTION_PS(ps, signum) = *nsa;
319 1.121 thorpej ps->sa_sigdesc[signum].sd_tramp = tramp;
320 1.121 thorpej ps->sa_sigdesc[signum].sd_vers = vers;
321 1.109 jdolecek sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
322 1.79 mycroft if ((prop & SA_NORESET) != 0)
323 1.109 jdolecek SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
324 1.79 mycroft if (signum == SIGCHLD) {
325 1.79 mycroft if (nsa->sa_flags & SA_NOCLDSTOP)
326 1.79 mycroft p->p_flag |= P_NOCLDSTOP;
327 1.79 mycroft else
328 1.79 mycroft p->p_flag &= ~P_NOCLDSTOP;
329 1.82 enami if (nsa->sa_flags & SA_NOCLDWAIT) {
330 1.81 christos /*
331 1.81 christos * Paranoia: since SA_NOCLDWAIT is implemented
332 1.81 christos * by reparenting the dying child to PID 1 (and
333 1.112 lukem * trust it to reap the zombie), PID 1 itself
334 1.112 lukem * is forbidden to set SA_NOCLDWAIT.
335 1.81 christos */
336 1.81 christos if (p->p_pid == 1)
337 1.81 christos p->p_flag &= ~P_NOCLDWAIT;
338 1.81 christos else
339 1.81 christos p->p_flag |= P_NOCLDWAIT;
340 1.81 christos } else
341 1.81 christos p->p_flag &= ~P_NOCLDWAIT;
342 1.79 mycroft }
343 1.79 mycroft if ((nsa->sa_flags & SA_NODEFER) == 0)
344 1.109 jdolecek sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
345 1.79 mycroft else
346 1.109 jdolecek sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
347 1.79 mycroft /*
348 1.112 lukem * Set bit in p_sigctx.ps_sigignore for signals that are set to
349 1.112 lukem * SIG_IGN, and for signals set to SIG_DFL where the default is
350 1.112 lukem * to ignore. However, don't put SIGCONT in
351 1.112 lukem * p_sigctx.ps_sigignore, as we have to restart the process.
352 1.112 lukem */
353 1.79 mycroft if (nsa->sa_handler == SIG_IGN ||
354 1.79 mycroft (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
355 1.112 lukem /* never to be seen again */
356 1.112 lukem sigdelset(&p->p_sigctx.ps_siglist, signum);
357 1.112 lukem if (signum != SIGCONT) {
358 1.112 lukem /* easier in psignal */
359 1.112 lukem sigaddset(&p->p_sigctx.ps_sigignore, signum);
360 1.112 lukem }
361 1.109 jdolecek sigdelset(&p->p_sigctx.ps_sigcatch, signum);
362 1.79 mycroft } else {
363 1.109 jdolecek sigdelset(&p->p_sigctx.ps_sigignore, signum);
364 1.79 mycroft if (nsa->sa_handler == SIG_DFL)
365 1.109 jdolecek sigdelset(&p->p_sigctx.ps_sigcatch, signum);
366 1.79 mycroft else
367 1.109 jdolecek sigaddset(&p->p_sigctx.ps_sigcatch, signum);
368 1.79 mycroft }
369 1.79 mycroft (void) spl0();
370 1.79 mycroft }
371 1.79 mycroft
372 1.79 mycroft return (0);
373 1.79 mycroft }
374 1.79 mycroft
375 1.158 christos #ifdef COMPAT_16
376 1.29 cgd /* ARGSUSED */
377 1.52 christos int
378 1.158 christos compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
379 1.48 thorpej {
380 1.159 nathanw struct compat_16_sys___sigaction14_args /* {
381 1.112 lukem syscallarg(int) signum;
382 1.112 lukem syscallarg(const struct sigaction *) nsa;
383 1.112 lukem syscallarg(struct sigaction *) osa;
384 1.48 thorpej } */ *uap = v;
385 1.130 thorpej struct proc *p;
386 1.112 lukem struct sigaction nsa, osa;
387 1.112 lukem int error;
388 1.29 cgd
389 1.79 mycroft if (SCARG(uap, nsa)) {
390 1.79 mycroft error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
391 1.52 christos if (error)
392 1.29 cgd return (error);
393 1.29 cgd }
394 1.130 thorpej p = l->l_proc;
395 1.79 mycroft error = sigaction1(p, SCARG(uap, signum),
396 1.121 thorpej SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
397 1.121 thorpej NULL, 0);
398 1.121 thorpej if (error)
399 1.121 thorpej return (error);
400 1.121 thorpej if (SCARG(uap, osa)) {
401 1.121 thorpej error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
402 1.121 thorpej if (error)
403 1.121 thorpej return (error);
404 1.121 thorpej }
405 1.121 thorpej return (0);
406 1.121 thorpej }
407 1.158 christos #endif
408 1.121 thorpej
409 1.121 thorpej /* ARGSUSED */
410 1.121 thorpej int
411 1.130 thorpej sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
412 1.121 thorpej {
413 1.121 thorpej struct sys___sigaction_sigtramp_args /* {
414 1.121 thorpej syscallarg(int) signum;
415 1.121 thorpej syscallarg(const struct sigaction *) nsa;
416 1.121 thorpej syscallarg(struct sigaction *) osa;
417 1.121 thorpej syscallarg(void *) tramp;
418 1.121 thorpej syscallarg(int) vers;
419 1.121 thorpej } */ *uap = v;
420 1.130 thorpej struct proc *p = l->l_proc;
421 1.121 thorpej struct sigaction nsa, osa;
422 1.121 thorpej int error;
423 1.121 thorpej
424 1.121 thorpej if (SCARG(uap, nsa)) {
425 1.121 thorpej error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
426 1.121 thorpej if (error)
427 1.121 thorpej return (error);
428 1.121 thorpej }
429 1.121 thorpej error = sigaction1(p, SCARG(uap, signum),
430 1.121 thorpej SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
431 1.121 thorpej SCARG(uap, tramp), SCARG(uap, vers));
432 1.79 mycroft if (error)
433 1.79 mycroft return (error);
434 1.79 mycroft if (SCARG(uap, osa)) {
435 1.79 mycroft error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
436 1.52 christos if (error)
437 1.29 cgd return (error);
438 1.29 cgd }
439 1.29 cgd return (0);
440 1.29 cgd }
441 1.29 cgd
442 1.29 cgd /*
443 1.29 cgd * Initialize signal state for process 0;
444 1.79 mycroft * set to ignore signals that are ignored by default and disable the signal
445 1.79 mycroft * stack.
446 1.29 cgd */
447 1.29 cgd void
448 1.112 lukem siginit(struct proc *p)
449 1.29 cgd {
450 1.112 lukem struct sigacts *ps;
451 1.112 lukem int signum, prop;
452 1.79 mycroft
453 1.112 lukem ps = p->p_sigacts;
454 1.79 mycroft sigemptyset(&contsigmask);
455 1.79 mycroft sigemptyset(&stopsigmask);
456 1.79 mycroft sigemptyset(&sigcantmask);
457 1.85 mycroft for (signum = 1; signum < NSIG; signum++) {
458 1.79 mycroft prop = sigprop[signum];
459 1.79 mycroft if (prop & SA_CONT)
460 1.79 mycroft sigaddset(&contsigmask, signum);
461 1.79 mycroft if (prop & SA_STOP)
462 1.79 mycroft sigaddset(&stopsigmask, signum);
463 1.79 mycroft if (prop & SA_CANTMASK)
464 1.79 mycroft sigaddset(&sigcantmask, signum);
465 1.79 mycroft if (prop & SA_IGNORE && signum != SIGCONT)
466 1.109 jdolecek sigaddset(&p->p_sigctx.ps_sigignore, signum);
467 1.109 jdolecek sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
468 1.109 jdolecek SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
469 1.79 mycroft }
470 1.109 jdolecek sigemptyset(&p->p_sigctx.ps_sigcatch);
471 1.171 jdolecek p->p_sigctx.ps_sigwaited = NULL;
472 1.79 mycroft p->p_flag &= ~P_NOCLDSTOP;
473 1.29 cgd
474 1.79 mycroft /*
475 1.79 mycroft * Reset stack state to the user stack.
476 1.79 mycroft */
477 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
478 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_size = 0;
479 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_sp = 0;
480 1.89 thorpej
481 1.89 thorpej /* One reference. */
482 1.109 jdolecek ps->sa_refcnt = 1;
483 1.29 cgd }
484 1.29 cgd
485 1.29 cgd /*
486 1.29 cgd * Reset signals for an exec of the specified process.
487 1.29 cgd */
488 1.29 cgd void
489 1.112 lukem execsigs(struct proc *p)
490 1.29 cgd {
491 1.112 lukem struct sigacts *ps;
492 1.112 lukem int signum, prop;
493 1.29 cgd
494 1.115 thorpej sigactsunshare(p);
495 1.115 thorpej
496 1.112 lukem ps = p->p_sigacts;
497 1.115 thorpej
498 1.29 cgd /*
499 1.29 cgd * Reset caught signals. Held signals remain held
500 1.109 jdolecek * through p_sigctx.ps_sigmask (unless they were caught,
501 1.29 cgd * and are now ignored by default).
502 1.29 cgd */
503 1.85 mycroft for (signum = 1; signum < NSIG; signum++) {
504 1.109 jdolecek if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
505 1.79 mycroft prop = sigprop[signum];
506 1.79 mycroft if (prop & SA_IGNORE) {
507 1.79 mycroft if ((prop & SA_CONT) == 0)
508 1.112 lukem sigaddset(&p->p_sigctx.ps_sigignore,
509 1.112 lukem signum);
510 1.109 jdolecek sigdelset(&p->p_sigctx.ps_siglist, signum);
511 1.79 mycroft }
512 1.109 jdolecek SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
513 1.29 cgd }
514 1.109 jdolecek sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
515 1.109 jdolecek SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
516 1.29 cgd }
517 1.109 jdolecek sigemptyset(&p->p_sigctx.ps_sigcatch);
518 1.171 jdolecek p->p_sigctx.ps_sigwaited = NULL;
519 1.79 mycroft p->p_flag &= ~P_NOCLDSTOP;
520 1.79 mycroft
521 1.29 cgd /*
522 1.29 cgd * Reset stack state to the user stack.
523 1.29 cgd */
524 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
525 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_size = 0;
526 1.109 jdolecek p->p_sigctx.ps_sigstk.ss_sp = 0;
527 1.29 cgd }
528 1.29 cgd
529 1.79 mycroft int
530 1.112 lukem sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
531 1.79 mycroft {
532 1.79 mycroft
533 1.79 mycroft if (oss)
534 1.109 jdolecek *oss = p->p_sigctx.ps_sigmask;
535 1.79 mycroft
536 1.79 mycroft if (nss) {
537 1.105 thorpej (void)splsched(); /* XXXSMP */
538 1.79 mycroft switch (how) {
539 1.79 mycroft case SIG_BLOCK:
540 1.109 jdolecek sigplusset(nss, &p->p_sigctx.ps_sigmask);
541 1.79 mycroft break;
542 1.79 mycroft case SIG_UNBLOCK:
543 1.109 jdolecek sigminusset(nss, &p->p_sigctx.ps_sigmask);
544 1.110 thorpej CHECKSIGS(p);
545 1.79 mycroft break;
546 1.79 mycroft case SIG_SETMASK:
547 1.109 jdolecek p->p_sigctx.ps_sigmask = *nss;
548 1.110 thorpej CHECKSIGS(p);
549 1.79 mycroft break;
550 1.79 mycroft default:
551 1.104 thorpej (void)spl0(); /* XXXSMP */
552 1.79 mycroft return (EINVAL);
553 1.79 mycroft }
554 1.109 jdolecek sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
555 1.104 thorpej (void)spl0(); /* XXXSMP */
556 1.79 mycroft }
557 1.79 mycroft
558 1.79 mycroft return (0);
559 1.79 mycroft }
560 1.79 mycroft
561 1.29 cgd /*
562 1.29 cgd * Manipulate signal mask.
563 1.29 cgd * Note that we receive new mask, not pointer,
564 1.29 cgd * and return old mask as return value;
565 1.29 cgd * the library stub does the rest.
566 1.29 cgd */
567 1.52 christos int
568 1.130 thorpej sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
569 1.48 thorpej {
570 1.79 mycroft struct sys___sigprocmask14_args /* {
571 1.112 lukem syscallarg(int) how;
572 1.112 lukem syscallarg(const sigset_t *) set;
573 1.112 lukem syscallarg(sigset_t *) oset;
574 1.48 thorpej } */ *uap = v;
575 1.130 thorpej struct proc *p;
576 1.112 lukem sigset_t nss, oss;
577 1.112 lukem int error;
578 1.29 cgd
579 1.79 mycroft if (SCARG(uap, set)) {
580 1.79 mycroft error = copyin(SCARG(uap, set), &nss, sizeof(nss));
581 1.79 mycroft if (error)
582 1.79 mycroft return (error);
583 1.79 mycroft }
584 1.130 thorpej p = l->l_proc;
585 1.79 mycroft error = sigprocmask1(p, SCARG(uap, how),
586 1.79 mycroft SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
587 1.79 mycroft if (error)
588 1.79 mycroft return (error);
589 1.79 mycroft if (SCARG(uap, oset)) {
590 1.79 mycroft error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
591 1.79 mycroft if (error)
592 1.79 mycroft return (error);
593 1.79 mycroft }
594 1.79 mycroft return (0);
595 1.79 mycroft }
596 1.79 mycroft
597 1.79 mycroft void
598 1.112 lukem sigpending1(struct proc *p, sigset_t *ss)
599 1.79 mycroft {
600 1.29 cgd
601 1.109 jdolecek *ss = p->p_sigctx.ps_siglist;
602 1.109 jdolecek sigminusset(&p->p_sigctx.ps_sigmask, ss);
603 1.29 cgd }
604 1.29 cgd
605 1.29 cgd /* ARGSUSED */
606 1.52 christos int
607 1.130 thorpej sys___sigpending14(struct lwp *l, void *v, register_t *retval)
608 1.29 cgd {
609 1.98 augustss struct sys___sigpending14_args /* {
610 1.112 lukem syscallarg(sigset_t *) set;
611 1.79 mycroft } */ *uap = v;
612 1.130 thorpej struct proc *p;
613 1.130 thorpej sigset_t ss;
614 1.79 mycroft
615 1.130 thorpej p = l->l_proc;
616 1.79 mycroft sigpending1(p, &ss);
617 1.79 mycroft return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
618 1.79 mycroft }
619 1.79 mycroft
620 1.79 mycroft int
621 1.112 lukem sigsuspend1(struct proc *p, const sigset_t *ss)
622 1.79 mycroft {
623 1.112 lukem struct sigacts *ps;
624 1.29 cgd
625 1.112 lukem ps = p->p_sigacts;
626 1.79 mycroft if (ss) {
627 1.79 mycroft /*
628 1.79 mycroft * When returning from sigpause, we want
629 1.79 mycroft * the old mask to be restored after the
630 1.79 mycroft * signal handler has finished. Thus, we
631 1.109 jdolecek * save it here and mark the sigctx structure
632 1.79 mycroft * to indicate this.
633 1.79 mycroft */
634 1.109 jdolecek p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
635 1.109 jdolecek p->p_sigctx.ps_flags |= SAS_OLDMASK;
636 1.105 thorpej (void) splsched(); /* XXXSMP */
637 1.109 jdolecek p->p_sigctx.ps_sigmask = *ss;
638 1.110 thorpej CHECKSIGS(p);
639 1.109 jdolecek sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
640 1.104 thorpej (void) spl0(); /* XXXSMP */
641 1.79 mycroft }
642 1.79 mycroft
643 1.79 mycroft while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
644 1.145 nathanw /* void */;
645 1.144 fvdl
646 1.79 mycroft /* always return EINTR rather than ERESTART... */
647 1.79 mycroft return (EINTR);
648 1.29 cgd }
649 1.29 cgd
650 1.29 cgd /*
651 1.29 cgd * Suspend process until signal, providing mask to be set
652 1.29 cgd * in the meantime. Note nonstandard calling convention:
653 1.29 cgd * libc stub passes mask, not pointer, to save a copyin.
654 1.29 cgd */
655 1.29 cgd /* ARGSUSED */
656 1.29 cgd int
657 1.130 thorpej sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
658 1.48 thorpej {
659 1.79 mycroft struct sys___sigsuspend14_args /* {
660 1.112 lukem syscallarg(const sigset_t *) set;
661 1.48 thorpej } */ *uap = v;
662 1.130 thorpej struct proc *p;
663 1.112 lukem sigset_t ss;
664 1.112 lukem int error;
665 1.79 mycroft
666 1.79 mycroft if (SCARG(uap, set)) {
667 1.79 mycroft error = copyin(SCARG(uap, set), &ss, sizeof(ss));
668 1.79 mycroft if (error)
669 1.79 mycroft return (error);
670 1.79 mycroft }
671 1.79 mycroft
672 1.130 thorpej p = l->l_proc;
673 1.79 mycroft return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
674 1.79 mycroft }
675 1.79 mycroft
676 1.79 mycroft int
677 1.112 lukem sigaltstack1(struct proc *p, const struct sigaltstack *nss,
678 1.112 lukem struct sigaltstack *oss)
679 1.79 mycroft {
680 1.112 lukem
681 1.79 mycroft if (oss)
682 1.109 jdolecek *oss = p->p_sigctx.ps_sigstk;
683 1.79 mycroft
684 1.79 mycroft if (nss) {
685 1.79 mycroft if (nss->ss_flags & ~SS_ALLBITS)
686 1.79 mycroft return (EINVAL);
687 1.79 mycroft
688 1.79 mycroft if (nss->ss_flags & SS_DISABLE) {
689 1.109 jdolecek if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
690 1.79 mycroft return (EINVAL);
691 1.79 mycroft } else {
692 1.79 mycroft if (nss->ss_size < MINSIGSTKSZ)
693 1.79 mycroft return (ENOMEM);
694 1.79 mycroft }
695 1.109 jdolecek p->p_sigctx.ps_sigstk = *nss;
696 1.79 mycroft }
697 1.79 mycroft
698 1.79 mycroft return (0);
699 1.29 cgd }
700 1.29 cgd
701 1.29 cgd /* ARGSUSED */
702 1.52 christos int
703 1.130 thorpej sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
704 1.48 thorpej {
705 1.98 augustss struct sys___sigaltstack14_args /* {
706 1.112 lukem syscallarg(const struct sigaltstack *) nss;
707 1.112 lukem syscallarg(struct sigaltstack *) oss;
708 1.48 thorpej } */ *uap = v;
709 1.130 thorpej struct proc *p;
710 1.112 lukem struct sigaltstack nss, oss;
711 1.112 lukem int error;
712 1.29 cgd
713 1.79 mycroft if (SCARG(uap, nss)) {
714 1.79 mycroft error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
715 1.79 mycroft if (error)
716 1.79 mycroft return (error);
717 1.79 mycroft }
718 1.130 thorpej p = l->l_proc;
719 1.79 mycroft error = sigaltstack1(p,
720 1.79 mycroft SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
721 1.52 christos if (error)
722 1.29 cgd return (error);
723 1.79 mycroft if (SCARG(uap, oss)) {
724 1.79 mycroft error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
725 1.79 mycroft if (error)
726 1.79 mycroft return (error);
727 1.29 cgd }
728 1.29 cgd return (0);
729 1.29 cgd }
730 1.29 cgd
731 1.29 cgd /* ARGSUSED */
732 1.29 cgd int
733 1.130 thorpej sys_kill(struct lwp *l, void *v, register_t *retval)
734 1.48 thorpej {
735 1.98 augustss struct sys_kill_args /* {
736 1.112 lukem syscallarg(int) pid;
737 1.112 lukem syscallarg(int) signum;
738 1.48 thorpej } */ *uap = v;
739 1.130 thorpej struct proc *cp, *p;
740 1.112 lukem struct pcred *pc;
741 1.148 christos ksiginfo_t ksi;
742 1.29 cgd
743 1.130 thorpej cp = l->l_proc;
744 1.112 lukem pc = cp->p_cred;
745 1.32 cgd if ((u_int)SCARG(uap, signum) >= NSIG)
746 1.29 cgd return (EINVAL);
747 1.191 matt KSI_INIT(&ksi);
748 1.148 christos ksi.ksi_signo = SCARG(uap, signum);
749 1.148 christos ksi.ksi_code = SI_USER;
750 1.148 christos ksi.ksi_pid = cp->p_pid;
751 1.148 christos ksi.ksi_uid = cp->p_ucred->cr_uid;
752 1.32 cgd if (SCARG(uap, pid) > 0) {
753 1.29 cgd /* kill single process */
754 1.32 cgd if ((p = pfind(SCARG(uap, pid))) == NULL)
755 1.29 cgd return (ESRCH);
756 1.32 cgd if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
757 1.29 cgd return (EPERM);
758 1.32 cgd if (SCARG(uap, signum))
759 1.160 christos kpsignal2(p, &ksi, 1);
760 1.29 cgd return (0);
761 1.29 cgd }
762 1.32 cgd switch (SCARG(uap, pid)) {
763 1.29 cgd case -1: /* broadcast signal */
764 1.148 christos return (killpg1(cp, &ksi, 0, 1));
765 1.29 cgd case 0: /* signal own process group */
766 1.148 christos return (killpg1(cp, &ksi, 0, 0));
767 1.29 cgd default: /* negative explicit process group */
768 1.148 christos return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
769 1.29 cgd }
770 1.29 cgd /* NOTREACHED */
771 1.29 cgd }
772 1.29 cgd
773 1.29 cgd /*
774 1.29 cgd * Common code for kill process group/broadcast kill.
775 1.29 cgd * cp is calling process.
776 1.29 cgd */
777 1.52 christos int
778 1.148 christos killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
779 1.29 cgd {
780 1.112 lukem struct proc *p;
781 1.112 lukem struct pcred *pc;
782 1.112 lukem struct pgrp *pgrp;
783 1.112 lukem int nfound;
784 1.148 christos int signum = ksi->ksi_signo;
785 1.29 cgd
786 1.112 lukem pc = cp->p_cred;
787 1.112 lukem nfound = 0;
788 1.91 thorpej if (all) {
789 1.29 cgd /*
790 1.29 cgd * broadcast
791 1.29 cgd */
792 1.92 thorpej proclist_lock_read();
793 1.124 matt LIST_FOREACH(p, &allproc, p_list) {
794 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
795 1.29 cgd p == cp || !CANSIGNAL(cp, pc, p, signum))
796 1.29 cgd continue;
797 1.29 cgd nfound++;
798 1.29 cgd if (signum)
799 1.160 christos kpsignal2(p, ksi, 1);
800 1.29 cgd }
801 1.91 thorpej proclist_unlock_read();
802 1.91 thorpej } else {
803 1.29 cgd if (pgid == 0)
804 1.29 cgd /*
805 1.29 cgd * zero pgid means send to my process group.
806 1.29 cgd */
807 1.29 cgd pgrp = cp->p_pgrp;
808 1.29 cgd else {
809 1.29 cgd pgrp = pgfind(pgid);
810 1.29 cgd if (pgrp == NULL)
811 1.29 cgd return (ESRCH);
812 1.29 cgd }
813 1.124 matt LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
814 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
815 1.29 cgd !CANSIGNAL(cp, pc, p, signum))
816 1.29 cgd continue;
817 1.29 cgd nfound++;
818 1.90 thorpej if (signum && P_ZOMBIE(p) == 0)
819 1.160 christos kpsignal2(p, ksi, 1);
820 1.29 cgd }
821 1.29 cgd }
822 1.29 cgd return (nfound ? 0 : ESRCH);
823 1.29 cgd }
824 1.29 cgd
825 1.29 cgd /*
826 1.29 cgd * Send a signal to a process group.
827 1.29 cgd */
828 1.29 cgd void
829 1.112 lukem gsignal(int pgid, int signum)
830 1.29 cgd {
831 1.148 christos ksiginfo_t ksi;
832 1.192 matt KSI_INIT_EMPTY(&ksi);
833 1.148 christos ksi.ksi_signo = signum;
834 1.148 christos kgsignal(pgid, &ksi, NULL);
835 1.148 christos }
836 1.148 christos
837 1.148 christos void
838 1.148 christos kgsignal(int pgid, ksiginfo_t *ksi, void *data)
839 1.148 christos {
840 1.29 cgd struct pgrp *pgrp;
841 1.29 cgd
842 1.29 cgd if (pgid && (pgrp = pgfind(pgid)))
843 1.148 christos kpgsignal(pgrp, ksi, data, 0);
844 1.29 cgd }
845 1.29 cgd
846 1.29 cgd /*
847 1.71 fvdl * Send a signal to a process group. If checktty is 1,
848 1.29 cgd * limit to members which have a controlling terminal.
849 1.29 cgd */
850 1.29 cgd void
851 1.148 christos pgsignal(struct pgrp *pgrp, int sig, int checkctty)
852 1.148 christos {
853 1.148 christos ksiginfo_t ksi;
854 1.192 matt KSI_INIT_EMPTY(&ksi);
855 1.148 christos ksi.ksi_signo = sig;
856 1.148 christos kpgsignal(pgrp, &ksi, NULL, checkctty);
857 1.148 christos }
858 1.148 christos
859 1.148 christos void
860 1.148 christos kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
861 1.29 cgd {
862 1.98 augustss struct proc *p;
863 1.29 cgd
864 1.29 cgd if (pgrp)
865 1.124 matt LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
866 1.29 cgd if (checkctty == 0 || p->p_flag & P_CONTROLT)
867 1.148 christos kpsignal(p, ksi, data);
868 1.29 cgd }
869 1.29 cgd
870 1.29 cgd /*
871 1.29 cgd * Send a signal caused by a trap to the current process.
872 1.29 cgd * If it will be caught immediately, deliver it with correct code.
873 1.29 cgd * Otherwise, post it normally.
874 1.29 cgd */
875 1.148 christos void
876 1.160 christos trapsignal(struct lwp *l, const ksiginfo_t *ksi)
877 1.148 christos {
878 1.130 thorpej struct proc *p;
879 1.130 thorpej struct sigacts *ps;
880 1.148 christos int signum = ksi->ksi_signo;
881 1.29 cgd
882 1.166 thorpej KASSERT(KSI_TRAP_P(ksi));
883 1.166 thorpej
884 1.130 thorpej p = l->l_proc;
885 1.112 lukem ps = p->p_sigacts;
886 1.79 mycroft if ((p->p_flag & P_TRACED) == 0 &&
887 1.109 jdolecek sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
888 1.109 jdolecek !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
889 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
890 1.29 cgd #ifdef KTRACE
891 1.29 cgd if (KTRPOINT(p, KTR_PSIG))
892 1.148 christos ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
893 1.157 christos &p->p_sigctx.ps_sigmask, ksi);
894 1.29 cgd #endif
895 1.148 christos kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
896 1.105 thorpej (void) splsched(); /* XXXSMP */
897 1.112 lukem sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
898 1.112 lukem &p->p_sigctx.ps_sigmask);
899 1.109 jdolecek if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
900 1.109 jdolecek sigdelset(&p->p_sigctx.ps_sigcatch, signum);
901 1.45 mycroft if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
902 1.109 jdolecek sigaddset(&p->p_sigctx.ps_sigignore, signum);
903 1.109 jdolecek SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
904 1.45 mycroft }
905 1.104 thorpej (void) spl0(); /* XXXSMP */
906 1.29 cgd } else {
907 1.152 christos p->p_sigctx.ps_lwp = l->l_lid;
908 1.148 christos /* XXX for core dump/debugger */
909 1.152 christos p->p_sigctx.ps_signo = ksi->ksi_signo;
910 1.152 christos p->p_sigctx.ps_code = ksi->ksi_trap;
911 1.160 christos kpsignal2(p, ksi, 1);
912 1.29 cgd }
913 1.29 cgd }
914 1.29 cgd
915 1.29 cgd /*
916 1.151 christos * Fill in signal information and signal the parent for a child status change.
917 1.151 christos */
918 1.151 christos static void
919 1.151 christos child_psignal(struct proc *p, int dolock)
920 1.151 christos {
921 1.151 christos ksiginfo_t ksi;
922 1.151 christos
923 1.191 matt KSI_INIT(&ksi);
924 1.151 christos ksi.ksi_signo = SIGCHLD;
925 1.151 christos ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
926 1.151 christos ksi.ksi_pid = p->p_pid;
927 1.151 christos ksi.ksi_uid = p->p_ucred->cr_uid;
928 1.151 christos ksi.ksi_status = p->p_xstat;
929 1.151 christos ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
930 1.151 christos ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
931 1.160 christos kpsignal2(p->p_pptr, &ksi, dolock);
932 1.151 christos }
933 1.151 christos
934 1.151 christos /*
935 1.29 cgd * Send the signal to the process. If the signal has an action, the action
936 1.29 cgd * is usually performed by the target process rather than the caller; we add
937 1.29 cgd * the signal to the set of pending signals for the process.
938 1.29 cgd *
939 1.29 cgd * Exceptions:
940 1.29 cgd * o When a stop signal is sent to a sleeping process that takes the
941 1.29 cgd * default action, the process is stopped without awakening it.
942 1.29 cgd * o SIGCONT restarts stopped processes (or puts them back to sleep)
943 1.29 cgd * regardless of the signal action (eg, blocked or ignored).
944 1.29 cgd *
945 1.29 cgd * Other ignored signals are discarded immediately.
946 1.104 thorpej *
947 1.104 thorpej * XXXSMP: Invoked as psignal() or sched_psignal().
948 1.29 cgd */
949 1.29 cgd void
950 1.148 christos psignal1(struct proc *p, int signum, int dolock)
951 1.148 christos {
952 1.165 thorpej ksiginfo_t ksi;
953 1.165 thorpej
954 1.192 matt KSI_INIT_EMPTY(&ksi);
955 1.165 thorpej ksi.ksi_signo = signum;
956 1.165 thorpej kpsignal2(p, &ksi, dolock);
957 1.148 christos }
958 1.148 christos
959 1.148 christos void
960 1.160 christos kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
961 1.160 christos {
962 1.165 thorpej
963 1.164 christos if ((p->p_flag & P_WEXIT) == 0 && data) {
964 1.160 christos size_t fd;
965 1.160 christos struct filedesc *fdp = p->p_fd;
966 1.165 thorpej
967 1.160 christos ksi->ksi_fd = -1;
968 1.160 christos for (fd = 0; fd < fdp->fd_nfiles; fd++) {
969 1.160 christos struct file *fp = fdp->fd_ofiles[fd];
970 1.160 christos /* XXX: lock? */
971 1.160 christos if (fp && fp->f_data == data) {
972 1.160 christos ksi->ksi_fd = fd;
973 1.160 christos break;
974 1.160 christos }
975 1.160 christos }
976 1.160 christos }
977 1.160 christos kpsignal2(p, ksi, dolock);
978 1.160 christos }
979 1.160 christos
980 1.160 christos static void
981 1.160 christos kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
982 1.29 cgd {
983 1.170 christos struct lwp *l, *suspended = NULL;
984 1.187 cl struct sadata_vp *vp;
985 1.130 thorpej int s = 0, prop, allsusp;
986 1.112 lukem sig_t action;
987 1.148 christos int signum = ksi->ksi_signo;
988 1.29 cgd
989 1.79 mycroft #ifdef DIAGNOSTIC
990 1.79 mycroft if (signum <= 0 || signum >= NSIG)
991 1.148 christos panic("psignal signal number %d", signum);
992 1.148 christos
993 1.104 thorpej /* XXXSMP: works, but icky */
994 1.104 thorpej if (dolock)
995 1.104 thorpej SCHED_ASSERT_UNLOCKED();
996 1.104 thorpej else
997 1.104 thorpej SCHED_ASSERT_LOCKED();
998 1.79 mycroft #endif
999 1.148 christos
1000 1.183 fvdl /*
1001 1.126 jdolecek * Notify any interested parties in the signal.
1002 1.126 jdolecek */
1003 1.126 jdolecek KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
1004 1.126 jdolecek
1005 1.29 cgd prop = sigprop[signum];
1006 1.29 cgd
1007 1.29 cgd /*
1008 1.29 cgd * If proc is traced, always give parent a chance.
1009 1.29 cgd */
1010 1.190 matt action = SIG_DFL;
1011 1.190 matt if ((p->p_flag & P_TRACED) == 0) {
1012 1.190 matt if (KSI_TRAP_P(ksi)) {
1013 1.190 matt /*
1014 1.190 matt * If the signal was the result of a trap, only catch
1015 1.190 matt * the signal if it isn't masked and there is a
1016 1.190 matt * non-default non-ignore handler installed for it.
1017 1.190 matt * Otherwise take the default action.
1018 1.190 matt */
1019 1.190 matt if (!sigismember(&p->p_sigctx.ps_sigmask, signum) &&
1020 1.190 matt sigismember(&p->p_sigctx.ps_sigcatch, signum))
1021 1.190 matt action = SIG_CATCH;
1022 1.190 matt /*
1023 1.190 matt * If we are to take the default action, reset the
1024 1.190 matt * signal back to its defaults.
1025 1.190 matt */
1026 1.190 matt if (action == SIG_DFL) {
1027 1.190 matt sigdelset(&p->p_sigctx.ps_sigignore, signum);
1028 1.190 matt sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1029 1.190 matt sigdelset(&p->p_sigctx.ps_sigmask, signum);
1030 1.190 matt SIGACTION(p, signum).sa_handler = SIG_DFL;
1031 1.190 matt }
1032 1.190 matt } else {
1033 1.190 matt /*
1034 1.190 matt * If the signal is being ignored,
1035 1.190 matt * then we forget about it immediately.
1036 1.190 matt * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
1037 1.190 matt * and if it is set to SIG_IGN,
1038 1.190 matt * action will be SIG_DFL here.)
1039 1.190 matt */
1040 1.190 matt if (sigismember(&p->p_sigctx.ps_sigignore, signum))
1041 1.190 matt return;
1042 1.190 matt if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1043 1.190 matt action = SIG_HOLD;
1044 1.190 matt else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1045 1.190 matt action = SIG_CATCH;
1046 1.190 matt }
1047 1.190 matt if (action == SIG_DFL) {
1048 1.44 mycroft if (prop & SA_KILL && p->p_nice > NZERO)
1049 1.44 mycroft p->p_nice = NZERO;
1050 1.44 mycroft
1051 1.44 mycroft /*
1052 1.44 mycroft * If sending a tty stop signal to a member of an
1053 1.44 mycroft * orphaned process group, discard the signal here if
1054 1.44 mycroft * the action is default; don't stop the process below
1055 1.44 mycroft * if sleeping, and don't clear any pending SIGCONT.
1056 1.44 mycroft */
1057 1.44 mycroft if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1058 1.44 mycroft return;
1059 1.44 mycroft }
1060 1.193 matt } else {
1061 1.193 matt /*
1062 1.193 matt * If the process is being traced and the signal is being
1063 1.193 matt * caught, make sure to save any ksiginfo.
1064 1.193 matt */
1065 1.193 matt if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1066 1.193 matt ksiginfo_put(p, ksi);
1067 1.29 cgd }
1068 1.29 cgd
1069 1.29 cgd if (prop & SA_CONT)
1070 1.109 jdolecek sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
1071 1.29 cgd
1072 1.44 mycroft if (prop & SA_STOP)
1073 1.109 jdolecek sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
1074 1.44 mycroft
1075 1.29 cgd /*
1076 1.135 jdolecek * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1077 1.171 jdolecek * please!), check if anything waits on it. If yes, save the
1078 1.171 jdolecek * info into provided ps_sigwaited, and wake-up the waiter.
1079 1.135 jdolecek * The signal won't be processed further here.
1080 1.135 jdolecek */
1081 1.135 jdolecek if ((prop & SA_CANTMASK) == 0
1082 1.171 jdolecek && p->p_sigctx.ps_sigwaited
1083 1.171 jdolecek && sigismember(p->p_sigctx.ps_sigwait, signum)
1084 1.152 christos && p->p_stat != SSTOP) {
1085 1.171 jdolecek p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
1086 1.171 jdolecek p->p_sigctx.ps_sigwaited = NULL;
1087 1.135 jdolecek if (dolock)
1088 1.135 jdolecek wakeup_one(&p->p_sigctx.ps_sigwait);
1089 1.135 jdolecek else
1090 1.135 jdolecek sched_wakeup(&p->p_sigctx.ps_sigwait);
1091 1.135 jdolecek return;
1092 1.135 jdolecek }
1093 1.135 jdolecek
1094 1.171 jdolecek sigaddset(&p->p_sigctx.ps_siglist, signum);
1095 1.171 jdolecek
1096 1.171 jdolecek /* CHECKSIGS() is "inlined" here. */
1097 1.171 jdolecek p->p_sigctx.ps_sigcheck = 1;
1098 1.171 jdolecek
1099 1.135 jdolecek /*
1100 1.29 cgd * Defer further processing for signals which are held,
1101 1.29 cgd * except that stopped processes must be continued by SIGCONT.
1102 1.29 cgd */
1103 1.152 christos if (action == SIG_HOLD &&
1104 1.152 christos ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
1105 1.155 christos ksiginfo_put(p, ksi);
1106 1.29 cgd return;
1107 1.152 christos }
1108 1.104 thorpej /* XXXSMP: works, but icky */
1109 1.104 thorpej if (dolock)
1110 1.104 thorpej SCHED_LOCK(s);
1111 1.104 thorpej
1112 1.175 cl if (p->p_flag & P_SA) {
1113 1.175 cl allsusp = 0;
1114 1.187 cl l = NULL;
1115 1.175 cl if (p->p_stat == SACTIVE) {
1116 1.187 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1117 1.187 cl l = vp->savp_lwp;
1118 1.187 cl KDASSERT(l != NULL);
1119 1.187 cl if (l->l_flag & L_SA_IDLE) {
1120 1.187 cl /* wakeup idle LWP */
1121 1.187 cl goto found;
1122 1.187 cl /*NOTREACHED*/
1123 1.187 cl } else if (l->l_flag & L_SA_YIELD) {
1124 1.187 cl /* idle LWP is already waking up */
1125 1.187 cl goto out;
1126 1.187 cl /*NOTREACHED*/
1127 1.187 cl }
1128 1.187 cl }
1129 1.187 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1130 1.187 cl l = vp->savp_lwp;
1131 1.175 cl if (l->l_stat == LSRUN ||
1132 1.175 cl l->l_stat == LSONPROC) {
1133 1.175 cl signotify(p);
1134 1.175 cl goto out;
1135 1.175 cl /*NOTREACHED*/
1136 1.175 cl }
1137 1.175 cl if (l->l_stat == LSSLEEP &&
1138 1.175 cl l->l_flag & L_SINTR) {
1139 1.175 cl /* ok to signal vp lwp */
1140 1.175 cl } else
1141 1.175 cl l = NULL;
1142 1.175 cl }
1143 1.187 cl if (l == NULL)
1144 1.187 cl allsusp = 1;
1145 1.175 cl } else if (p->p_stat == SSTOP) {
1146 1.187 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1147 1.187 cl l = vp->savp_lwp;
1148 1.187 cl if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
1149 1.187 cl break;
1150 1.175 cl l = NULL;
1151 1.187 cl }
1152 1.175 cl }
1153 1.175 cl } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
1154 1.29 cgd /*
1155 1.130 thorpej * At least one LWP is running or on a run queue.
1156 1.130 thorpej * The signal will be noticed when one of them returns
1157 1.130 thorpej * to userspace.
1158 1.29 cgd */
1159 1.130 thorpej signotify(p);
1160 1.130 thorpej /*
1161 1.130 thorpej * The signal will be noticed very soon.
1162 1.29 cgd */
1163 1.130 thorpej goto out;
1164 1.175 cl /*NOTREACHED*/
1165 1.130 thorpej } else {
1166 1.175 cl /*
1167 1.175 cl * Find out if any of the sleeps are interruptable,
1168 1.175 cl * and if all the live LWPs remaining are suspended.
1169 1.175 cl */
1170 1.175 cl allsusp = 1;
1171 1.175 cl LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1172 1.175 cl if (l->l_stat == LSSLEEP &&
1173 1.175 cl l->l_flag & L_SINTR)
1174 1.175 cl break;
1175 1.175 cl if (l->l_stat == LSSUSPENDED)
1176 1.175 cl suspended = l;
1177 1.175 cl else if ((l->l_stat != LSZOMB) &&
1178 1.175 cl (l->l_stat != LSDEAD))
1179 1.144 fvdl allsusp = 0;
1180 1.29 cgd }
1181 1.175 cl }
1182 1.175 cl
1183 1.187 cl found:
1184 1.186 christos switch (p->p_stat) {
1185 1.186 christos case SACTIVE:
1186 1.130 thorpej
1187 1.176 cl if (l != NULL && (p->p_flag & P_TRACED))
1188 1.176 cl goto run;
1189 1.130 thorpej
1190 1.176 cl /*
1191 1.176 cl * If SIGCONT is default (or ignored) and process is
1192 1.176 cl * asleep, we are finished; the process should not
1193 1.176 cl * be awakened.
1194 1.176 cl */
1195 1.176 cl if ((prop & SA_CONT) && action == SIG_DFL) {
1196 1.176 cl sigdelset(&p->p_sigctx.ps_siglist, signum);
1197 1.176 cl goto done;
1198 1.176 cl }
1199 1.176 cl
1200 1.176 cl /*
1201 1.176 cl * When a sleeping process receives a stop
1202 1.176 cl * signal, process immediately if possible.
1203 1.176 cl */
1204 1.176 cl if ((prop & SA_STOP) && action == SIG_DFL) {
1205 1.29 cgd /*
1206 1.176 cl * If a child holding parent blocked,
1207 1.176 cl * stopping could cause deadlock.
1208 1.29 cgd */
1209 1.176 cl if (p->p_flag & P_PPWAIT) {
1210 1.176 cl goto out;
1211 1.130 thorpej }
1212 1.176 cl sigdelset(&p->p_sigctx.ps_siglist, signum);
1213 1.176 cl p->p_xstat = signum;
1214 1.176 cl if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
1215 1.104 thorpej /*
1216 1.176 cl * XXXSMP: recursive call; don't lock
1217 1.176 cl * the second time around.
1218 1.104 thorpej */
1219 1.176 cl child_psignal(p, 0);
1220 1.104 thorpej }
1221 1.180 manu proc_stop(p, 1); /* XXXSMP: recurse? */
1222 1.176 cl goto done;
1223 1.176 cl }
1224 1.29 cgd
1225 1.176 cl if (l == NULL) {
1226 1.130 thorpej /*
1227 1.176 cl * Special case: SIGKILL of a process
1228 1.176 cl * which is entirely composed of
1229 1.176 cl * suspended LWPs should succeed. We
1230 1.176 cl * make this happen by unsuspending one of
1231 1.176 cl * them.
1232 1.130 thorpej */
1233 1.187 cl if (allsusp && (signum == SIGKILL)) {
1234 1.187 cl if (p->p_flag & P_SA) {
1235 1.187 cl /*
1236 1.187 cl * get a suspended lwp from
1237 1.187 cl * the cache to send KILL
1238 1.187 cl * signal
1239 1.187 cl * XXXcl add signal checks at resume points
1240 1.187 cl */
1241 1.187 cl suspended = sa_getcachelwp
1242 1.187 cl (SLIST_FIRST(&p->p_sa->sa_vps));
1243 1.187 cl }
1244 1.176 cl lwp_continue(suspended);
1245 1.187 cl }
1246 1.176 cl goto done;
1247 1.176 cl }
1248 1.176 cl /*
1249 1.176 cl * All other (caught or default) signals
1250 1.176 cl * cause the process to run.
1251 1.176 cl */
1252 1.176 cl goto runfast;
1253 1.176 cl /*NOTREACHED*/
1254 1.186 christos case SSTOP:
1255 1.176 cl /* Process is stopped */
1256 1.176 cl /*
1257 1.176 cl * If traced process is already stopped,
1258 1.176 cl * then no further action is necessary.
1259 1.176 cl */
1260 1.176 cl if (p->p_flag & P_TRACED)
1261 1.176 cl goto done;
1262 1.29 cgd
1263 1.176 cl /*
1264 1.176 cl * Kill signal always sets processes running,
1265 1.176 cl * if possible.
1266 1.176 cl */
1267 1.176 cl if (signum == SIGKILL) {
1268 1.176 cl l = proc_unstop(p);
1269 1.176 cl if (l)
1270 1.176 cl goto runfast;
1271 1.176 cl goto done;
1272 1.176 cl }
1273 1.176 cl
1274 1.176 cl if (prop & SA_CONT) {
1275 1.29 cgd /*
1276 1.176 cl * If SIGCONT is default (or ignored),
1277 1.176 cl * we continue the process but don't
1278 1.176 cl * leave the signal in ps_siglist, as
1279 1.176 cl * it has no further action. If
1280 1.176 cl * SIGCONT is held, we continue the
1281 1.176 cl * process and leave the signal in
1282 1.176 cl * ps_siglist. If the process catches
1283 1.176 cl * SIGCONT, let it handle the signal
1284 1.176 cl * itself. If it isn't waiting on an
1285 1.176 cl * event, then it goes back to run
1286 1.176 cl * state. Otherwise, process goes
1287 1.176 cl * back to sleep state.
1288 1.29 cgd */
1289 1.176 cl if (action == SIG_DFL)
1290 1.176 cl sigdelset(&p->p_sigctx.ps_siglist,
1291 1.176 cl signum);
1292 1.176 cl l = proc_unstop(p);
1293 1.176 cl if (l && (action == SIG_CATCH))
1294 1.176 cl goto runfast;
1295 1.176 cl goto out;
1296 1.176 cl }
1297 1.29 cgd
1298 1.176 cl if (prop & SA_STOP) {
1299 1.29 cgd /*
1300 1.176 cl * Already stopped, don't need to stop again.
1301 1.176 cl * (If we did the shell could get confused.)
1302 1.29 cgd */
1303 1.176 cl sigdelset(&p->p_sigctx.ps_siglist, signum);
1304 1.176 cl goto done;
1305 1.29 cgd }
1306 1.176 cl
1307 1.176 cl /*
1308 1.176 cl * If a lwp is sleeping interruptibly, then
1309 1.176 cl * wake it up; it will run until the kernel
1310 1.176 cl * boundary, where it will stop in issignal(),
1311 1.176 cl * since p->p_stat is still SSTOP. When the
1312 1.176 cl * process is continued, it will be made
1313 1.176 cl * runnable and can look at the signal.
1314 1.176 cl */
1315 1.176 cl if (l)
1316 1.176 cl goto run;
1317 1.176 cl goto out;
1318 1.186 christos case SIDL:
1319 1.186 christos /* Process is being created by fork */
1320 1.186 christos /* XXX: We are not ready to receive signals yet */
1321 1.186 christos goto done;
1322 1.186 christos default:
1323 1.176 cl /* Else what? */
1324 1.176 cl panic("psignal: Invalid process state %d.", p->p_stat);
1325 1.176 cl }
1326 1.29 cgd /*NOTREACHED*/
1327 1.29 cgd
1328 1.112 lukem runfast:
1329 1.152 christos if (action == SIG_CATCH) {
1330 1.155 christos ksiginfo_put(p, ksi);
1331 1.152 christos action = SIG_HOLD;
1332 1.152 christos }
1333 1.29 cgd /*
1334 1.29 cgd * Raise priority to at least PUSER.
1335 1.29 cgd */
1336 1.130 thorpej if (l->l_priority > PUSER)
1337 1.130 thorpej l->l_priority = PUSER;
1338 1.112 lukem run:
1339 1.152 christos if (action == SIG_CATCH) {
1340 1.155 christos ksiginfo_put(p, ksi);
1341 1.152 christos action = SIG_HOLD;
1342 1.152 christos }
1343 1.144 fvdl
1344 1.130 thorpej setrunnable(l); /* XXXSMP: recurse? */
1345 1.112 lukem out:
1346 1.152 christos if (action == SIG_CATCH)
1347 1.155 christos ksiginfo_put(p, ksi);
1348 1.152 christos done:
1349 1.104 thorpej /* XXXSMP: works, but icky */
1350 1.104 thorpej if (dolock)
1351 1.104 thorpej SCHED_UNLOCK(s);
1352 1.29 cgd }
1353 1.29 cgd
1354 1.130 thorpej void
1355 1.160 christos kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1356 1.130 thorpej {
1357 1.130 thorpej struct proc *p = l->l_proc;
1358 1.130 thorpej struct lwp *le, *li;
1359 1.150 cl siginfo_t *si;
1360 1.150 cl int f;
1361 1.130 thorpej
1362 1.130 thorpej if (p->p_flag & P_SA) {
1363 1.144 fvdl
1364 1.144 fvdl /* XXXUPSXXX What if not on sa_vp ? */
1365 1.144 fvdl
1366 1.150 cl f = l->l_flag & L_SA;
1367 1.144 fvdl l->l_flag &= ~L_SA;
1368 1.130 thorpej si = pool_get(&siginfo_pool, PR_WAITOK);
1369 1.166 thorpej si->_info = ksi->ksi_info;
1370 1.130 thorpej le = li = NULL;
1371 1.166 thorpej if (KSI_TRAP_P(ksi))
1372 1.130 thorpej le = l;
1373 1.130 thorpej else
1374 1.130 thorpej li = l;
1375 1.130 thorpej
1376 1.130 thorpej sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1377 1.130 thorpej sizeof(siginfo_t), si);
1378 1.150 cl l->l_flag |= f;
1379 1.130 thorpej return;
1380 1.130 thorpej }
1381 1.130 thorpej
1382 1.148 christos (*p->p_emul->e_sendsig)(ksi, mask);
1383 1.130 thorpej }
1384 1.130 thorpej
1385 1.112 lukem static __inline int firstsig(const sigset_t *);
1386 1.79 mycroft
1387 1.79 mycroft static __inline int
1388 1.112 lukem firstsig(const sigset_t *ss)
1389 1.79 mycroft {
1390 1.79 mycroft int sig;
1391 1.79 mycroft
1392 1.79 mycroft sig = ffs(ss->__bits[0]);
1393 1.79 mycroft if (sig != 0)
1394 1.79 mycroft return (sig);
1395 1.79 mycroft #if NSIG > 33
1396 1.79 mycroft sig = ffs(ss->__bits[1]);
1397 1.79 mycroft if (sig != 0)
1398 1.79 mycroft return (sig + 32);
1399 1.79 mycroft #endif
1400 1.79 mycroft #if NSIG > 65
1401 1.79 mycroft sig = ffs(ss->__bits[2]);
1402 1.79 mycroft if (sig != 0)
1403 1.79 mycroft return (sig + 64);
1404 1.79 mycroft #endif
1405 1.79 mycroft #if NSIG > 97
1406 1.79 mycroft sig = ffs(ss->__bits[3]);
1407 1.79 mycroft if (sig != 0)
1408 1.79 mycroft return (sig + 96);
1409 1.79 mycroft #endif
1410 1.79 mycroft return (0);
1411 1.79 mycroft }
1412 1.79 mycroft
1413 1.29 cgd /*
1414 1.29 cgd * If the current process has received a signal (should be caught or cause
1415 1.29 cgd * termination, should interrupt current syscall), return the signal number.
1416 1.29 cgd * Stop signals with default action are processed immediately, then cleared;
1417 1.29 cgd * they aren't returned. This is checked after each entry to the system for
1418 1.29 cgd * a syscall or trap (though this can usually be done without calling issignal
1419 1.29 cgd * by checking the pending signal masks in the CURSIG macro.) The normal call
1420 1.29 cgd * sequence is
1421 1.29 cgd *
1422 1.130 thorpej * while (signum = CURSIG(curlwp))
1423 1.29 cgd * postsig(signum);
1424 1.29 cgd */
1425 1.29 cgd int
1426 1.130 thorpej issignal(struct lwp *l)
1427 1.29 cgd {
1428 1.130 thorpej struct proc *p = l->l_proc;
1429 1.127 scw int s = 0, signum, prop;
1430 1.130 thorpej int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1431 1.112 lukem sigset_t ss;
1432 1.29 cgd
1433 1.187 cl /* Bail out if we do not own the virtual processor */
1434 1.187 cl if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
1435 1.187 cl return 0;
1436 1.144 fvdl
1437 1.130 thorpej if (p->p_stat == SSTOP) {
1438 1.130 thorpej /*
1439 1.130 thorpej * The process is stopped/stopping. Stop ourselves now that
1440 1.130 thorpej * we're on the kernel/userspace boundary.
1441 1.130 thorpej */
1442 1.130 thorpej if (dolock)
1443 1.130 thorpej SCHED_LOCK(s);
1444 1.130 thorpej l->l_stat = LSSTOP;
1445 1.130 thorpej p->p_nrlwps--;
1446 1.130 thorpej if (p->p_flag & P_TRACED)
1447 1.130 thorpej goto sigtraceswitch;
1448 1.130 thorpej else
1449 1.130 thorpej goto sigswitch;
1450 1.130 thorpej }
1451 1.29 cgd for (;;) {
1452 1.79 mycroft sigpending1(p, &ss);
1453 1.29 cgd if (p->p_flag & P_PPWAIT)
1454 1.79 mycroft sigminusset(&stopsigmask, &ss);
1455 1.79 mycroft signum = firstsig(&ss);
1456 1.79 mycroft if (signum == 0) { /* no signal to send */
1457 1.109 jdolecek p->p_sigctx.ps_sigcheck = 0;
1458 1.119 christos if (locked && dolock)
1459 1.119 christos SCHED_LOCK(s);
1460 1.29 cgd return (0);
1461 1.79 mycroft }
1462 1.112 lukem /* take the signal! */
1463 1.112 lukem sigdelset(&p->p_sigctx.ps_siglist, signum);
1464 1.42 mycroft
1465 1.29 cgd /*
1466 1.29 cgd * We should see pending but ignored signals
1467 1.29 cgd * only if P_TRACED was on when they were posted.
1468 1.29 cgd */
1469 1.109 jdolecek if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1470 1.79 mycroft (p->p_flag & P_TRACED) == 0)
1471 1.29 cgd continue;
1472 1.42 mycroft
1473 1.29 cgd if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1474 1.29 cgd /*
1475 1.29 cgd * If traced, always stop, and stay
1476 1.29 cgd * stopped until released by the debugger.
1477 1.29 cgd */
1478 1.29 cgd p->p_xstat = signum;
1479 1.184 manu
1480 1.184 manu /* Emulation-specific handling of signal trace */
1481 1.184 manu if ((p->p_emul->e_tracesig != NULL) &&
1482 1.184 manu ((*p->p_emul->e_tracesig)(p, signum) != 0))
1483 1.184 manu goto childresumed;
1484 1.184 manu
1485 1.66 mycroft if ((p->p_flag & P_FSTRACE) == 0)
1486 1.151 christos child_psignal(p, dolock);
1487 1.119 christos if (dolock)
1488 1.119 christos SCHED_LOCK(s);
1489 1.180 manu proc_stop(p, 1);
1490 1.130 thorpej sigtraceswitch:
1491 1.130 thorpej mi_switch(l, NULL);
1492 1.114 nathanw SCHED_ASSERT_UNLOCKED();
1493 1.119 christos if (dolock)
1494 1.119 christos splx(s);
1495 1.119 christos else
1496 1.119 christos dolock = 1;
1497 1.29 cgd
1498 1.184 manu childresumed:
1499 1.29 cgd /*
1500 1.42 mycroft * If we are no longer being traced, or the parent
1501 1.42 mycroft * didn't give us a signal, look for more signals.
1502 1.29 cgd */
1503 1.42 mycroft if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1504 1.29 cgd continue;
1505 1.29 cgd
1506 1.29 cgd /*
1507 1.42 mycroft * If the new signal is being masked, look for other
1508 1.42 mycroft * signals.
1509 1.29 cgd */
1510 1.42 mycroft signum = p->p_xstat;
1511 1.130 thorpej p->p_xstat = 0;
1512 1.112 lukem /*
1513 1.112 lukem * `p->p_sigctx.ps_siglist |= mask' is done
1514 1.112 lukem * in setrunnable().
1515 1.112 lukem */
1516 1.109 jdolecek if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1517 1.29 cgd continue;
1518 1.112 lukem /* take the signal! */
1519 1.112 lukem sigdelset(&p->p_sigctx.ps_siglist, signum);
1520 1.29 cgd }
1521 1.29 cgd
1522 1.42 mycroft prop = sigprop[signum];
1523 1.42 mycroft
1524 1.29 cgd /*
1525 1.29 cgd * Decide whether the signal should be returned.
1526 1.29 cgd * Return the signal's number, or fall through
1527 1.29 cgd * to clear it from the pending mask.
1528 1.29 cgd */
1529 1.109 jdolecek switch ((long)SIGACTION(p, signum).sa_handler) {
1530 1.29 cgd
1531 1.33 cgd case (long)SIG_DFL:
1532 1.29 cgd /*
1533 1.29 cgd * Don't take default actions on system processes.
1534 1.29 cgd */
1535 1.29 cgd if (p->p_pid <= 1) {
1536 1.29 cgd #ifdef DIAGNOSTIC
1537 1.29 cgd /*
1538 1.29 cgd * Are you sure you want to ignore SIGSEGV
1539 1.29 cgd * in init? XXX
1540 1.29 cgd */
1541 1.57 christos printf("Process (pid %d) got signal %d\n",
1542 1.29 cgd p->p_pid, signum);
1543 1.29 cgd #endif
1544 1.29 cgd break; /* == ignore */
1545 1.29 cgd }
1546 1.29 cgd /*
1547 1.29 cgd * If there is a pending stop signal to process
1548 1.29 cgd * with default action, stop here,
1549 1.29 cgd * then clear the signal. However,
1550 1.29 cgd * if process is member of an orphaned
1551 1.29 cgd * process group, ignore tty stop signals.
1552 1.29 cgd */
1553 1.29 cgd if (prop & SA_STOP) {
1554 1.29 cgd if (p->p_flag & P_TRACED ||
1555 1.29 cgd (p->p_pgrp->pg_jobc == 0 &&
1556 1.29 cgd prop & SA_TTYSTOP))
1557 1.29 cgd break; /* == ignore */
1558 1.29 cgd p->p_xstat = signum;
1559 1.29 cgd if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1560 1.151 christos child_psignal(p, dolock);
1561 1.119 christos if (dolock)
1562 1.119 christos SCHED_LOCK(s);
1563 1.180 manu proc_stop(p, 1);
1564 1.130 thorpej sigswitch:
1565 1.130 thorpej mi_switch(l, NULL);
1566 1.104 thorpej SCHED_ASSERT_UNLOCKED();
1567 1.119 christos if (dolock)
1568 1.119 christos splx(s);
1569 1.119 christos else
1570 1.119 christos dolock = 1;
1571 1.29 cgd break;
1572 1.29 cgd } else if (prop & SA_IGNORE) {
1573 1.29 cgd /*
1574 1.29 cgd * Except for SIGCONT, shouldn't get here.
1575 1.29 cgd * Default action is to ignore; drop it.
1576 1.29 cgd */
1577 1.29 cgd break; /* == ignore */
1578 1.29 cgd } else
1579 1.42 mycroft goto keep;
1580 1.29 cgd /*NOTREACHED*/
1581 1.29 cgd
1582 1.33 cgd case (long)SIG_IGN:
1583 1.29 cgd /*
1584 1.29 cgd * Masking above should prevent us ever trying
1585 1.29 cgd * to take action on an ignored signal other
1586 1.29 cgd * than SIGCONT, unless process is traced.
1587 1.29 cgd */
1588 1.128 jdolecek #ifdef DEBUG_ISSIGNAL
1589 1.29 cgd if ((prop & SA_CONT) == 0 &&
1590 1.29 cgd (p->p_flag & P_TRACED) == 0)
1591 1.57 christos printf("issignal\n");
1592 1.128 jdolecek #endif
1593 1.29 cgd break; /* == ignore */
1594 1.29 cgd
1595 1.29 cgd default:
1596 1.29 cgd /*
1597 1.29 cgd * This signal has an action, let
1598 1.29 cgd * postsig() process it.
1599 1.29 cgd */
1600 1.42 mycroft goto keep;
1601 1.29 cgd }
1602 1.29 cgd }
1603 1.29 cgd /* NOTREACHED */
1604 1.42 mycroft
1605 1.112 lukem keep:
1606 1.112 lukem /* leave the signal for later */
1607 1.112 lukem sigaddset(&p->p_sigctx.ps_siglist, signum);
1608 1.110 thorpej CHECKSIGS(p);
1609 1.119 christos if (locked && dolock)
1610 1.119 christos SCHED_LOCK(s);
1611 1.42 mycroft return (signum);
1612 1.29 cgd }
1613 1.29 cgd
1614 1.29 cgd /*
1615 1.29 cgd * Put the argument process into the stopped state and notify the parent
1616 1.29 cgd * via wakeup. Signals are handled elsewhere. The process must not be
1617 1.29 cgd * on the run queue.
1618 1.29 cgd */
1619 1.179 christos void
1620 1.180 manu proc_stop(struct proc *p, int wakeup)
1621 1.29 cgd {
1622 1.130 thorpej struct lwp *l;
1623 1.178 dsl struct proc *parent;
1624 1.187 cl struct sadata_vp *vp;
1625 1.29 cgd
1626 1.104 thorpej SCHED_ASSERT_LOCKED();
1627 1.104 thorpej
1628 1.130 thorpej /* XXX lock process LWP state */
1629 1.178 dsl p->p_flag &= ~P_WAITED;
1630 1.29 cgd p->p_stat = SSTOP;
1631 1.178 dsl parent = p->p_pptr;
1632 1.178 dsl parent->p_nstopchild++;
1633 1.130 thorpej
1634 1.175 cl if (p->p_flag & P_SA) {
1635 1.175 cl /*
1636 1.175 cl * Only (try to) put the LWP on the VP in stopped
1637 1.175 cl * state.
1638 1.177 cl * All other LWPs will suspend in sa_setwoken()
1639 1.177 cl * because the VP-LWP in stopped state cannot be
1640 1.177 cl * repossessed.
1641 1.175 cl */
1642 1.187 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1643 1.187 cl l = vp->savp_lwp;
1644 1.187 cl if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
1645 1.187 cl l->l_stat = LSSTOP;
1646 1.187 cl p->p_nrlwps--;
1647 1.187 cl } else if (l->l_stat == LSRUN) {
1648 1.187 cl /* Remove LWP from the run queue */
1649 1.187 cl remrunqueue(l);
1650 1.187 cl l->l_stat = LSSTOP;
1651 1.187 cl p->p_nrlwps--;
1652 1.187 cl } else if (l->l_stat == LSSLEEP &&
1653 1.187 cl l->l_flag & L_SA_IDLE) {
1654 1.187 cl l->l_flag &= ~L_SA_IDLE;
1655 1.187 cl l->l_stat = LSSTOP;
1656 1.187 cl }
1657 1.175 cl }
1658 1.175 cl goto out;
1659 1.175 cl }
1660 1.175 cl
1661 1.130 thorpej /*
1662 1.130 thorpej * Put as many LWP's as possible in stopped state.
1663 1.130 thorpej * Sleeping ones will notice the stopped state as they try to
1664 1.130 thorpej * return to userspace.
1665 1.130 thorpej */
1666 1.175 cl
1667 1.132 jdolecek LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1668 1.175 cl if (l->l_stat == LSONPROC) {
1669 1.130 thorpej /* XXX SMP this assumes that a LWP that is LSONPROC
1670 1.130 thorpej * is curlwp and hence is about to be mi_switched
1671 1.130 thorpej * away; the only callers of proc_stop() are:
1672 1.130 thorpej * - psignal
1673 1.130 thorpej * - issignal()
1674 1.130 thorpej * For the former, proc_stop() is only called when
1675 1.130 thorpej * no processes are running, so we don't worry.
1676 1.130 thorpej * For the latter, proc_stop() is called right
1677 1.130 thorpej * before mi_switch().
1678 1.130 thorpej */
1679 1.130 thorpej l->l_stat = LSSTOP;
1680 1.130 thorpej p->p_nrlwps--;
1681 1.175 cl } else if (l->l_stat == LSRUN) {
1682 1.130 thorpej /* Remove LWP from the run queue */
1683 1.130 thorpej remrunqueue(l);
1684 1.130 thorpej l->l_stat = LSSTOP;
1685 1.130 thorpej p->p_nrlwps--;
1686 1.130 thorpej } else if ((l->l_stat == LSSLEEP) ||
1687 1.130 thorpej (l->l_stat == LSSUSPENDED) ||
1688 1.130 thorpej (l->l_stat == LSZOMB) ||
1689 1.130 thorpej (l->l_stat == LSDEAD)) {
1690 1.130 thorpej /*
1691 1.130 thorpej * Don't do anything; let sleeping LWPs
1692 1.130 thorpej * discover the stopped state of the process
1693 1.130 thorpej * on their way out of the kernel; otherwise,
1694 1.130 thorpej * things like NFS threads that sleep with
1695 1.130 thorpej * locks will block the rest of the system
1696 1.130 thorpej * from getting any work done.
1697 1.130 thorpej *
1698 1.130 thorpej * Suspended/dead/zombie LWPs aren't going
1699 1.130 thorpej * anywhere, so we don't need to touch them.
1700 1.130 thorpej */
1701 1.130 thorpej }
1702 1.130 thorpej #ifdef DIAGNOSTIC
1703 1.130 thorpej else {
1704 1.130 thorpej panic("proc_stop: process %d lwp %d "
1705 1.130 thorpej "in unstoppable state %d.\n",
1706 1.130 thorpej p->p_pid, l->l_lid, l->l_stat);
1707 1.130 thorpej }
1708 1.130 thorpej #endif
1709 1.130 thorpej }
1710 1.175 cl
1711 1.175 cl out:
1712 1.130 thorpej /* XXX unlock process LWP state */
1713 1.144 fvdl
1714 1.180 manu if (wakeup)
1715 1.180 manu sched_wakeup((caddr_t)p->p_pptr);
1716 1.29 cgd }
1717 1.29 cgd
1718 1.133 nathanw /*
1719 1.133 nathanw * Given a process in state SSTOP, set the state back to SACTIVE and
1720 1.133 nathanw * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1721 1.133 nathanw *
1722 1.133 nathanw * If no LWPs ended up runnable (and therefore able to take a signal),
1723 1.133 nathanw * return a LWP that is sleeping interruptably. The caller can wake
1724 1.133 nathanw * that LWP up to take a signal.
1725 1.133 nathanw */
1726 1.130 thorpej struct lwp *
1727 1.139 skrll proc_unstop(struct proc *p)
1728 1.130 thorpej {
1729 1.130 thorpej struct lwp *l, *lr = NULL;
1730 1.187 cl struct sadata_vp *vp;
1731 1.133 nathanw int cantake = 0;
1732 1.130 thorpej
1733 1.130 thorpej SCHED_ASSERT_LOCKED();
1734 1.130 thorpej
1735 1.130 thorpej /*
1736 1.140 nathanw * Our caller wants to be informed if there are only sleeping
1737 1.140 nathanw * and interruptable LWPs left after we have run so that it
1738 1.140 nathanw * can invoke setrunnable() if required - return one of the
1739 1.140 nathanw * interruptable LWPs if this is the case.
1740 1.130 thorpej */
1741 1.130 thorpej
1742 1.178 dsl if (!(p->p_flag & P_WAITED))
1743 1.178 dsl p->p_pptr->p_nstopchild--;
1744 1.130 thorpej p->p_stat = SACTIVE;
1745 1.132 jdolecek LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1746 1.140 nathanw if (l->l_stat == LSRUN) {
1747 1.140 nathanw lr = NULL;
1748 1.133 nathanw cantake = 1;
1749 1.140 nathanw }
1750 1.132 jdolecek if (l->l_stat != LSSTOP)
1751 1.132 jdolecek continue;
1752 1.132 jdolecek
1753 1.133 nathanw if (l->l_wchan != NULL) {
1754 1.133 nathanw l->l_stat = LSSLEEP;
1755 1.133 nathanw if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1756 1.132 jdolecek lr = l;
1757 1.133 nathanw cantake = 1;
1758 1.133 nathanw }
1759 1.133 nathanw } else {
1760 1.140 nathanw setrunnable(l);
1761 1.140 nathanw lr = NULL;
1762 1.133 nathanw cantake = 1;
1763 1.133 nathanw }
1764 1.132 jdolecek }
1765 1.175 cl if (p->p_flag & P_SA) {
1766 1.175 cl /* Only consider returning the LWP on the VP. */
1767 1.187 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1768 1.187 cl lr = vp->savp_lwp;
1769 1.187 cl if (lr->l_stat == LSSLEEP) {
1770 1.187 cl if (lr->l_flag & L_SA_YIELD) {
1771 1.187 cl setrunnable(lr);
1772 1.187 cl break;
1773 1.187 cl } else if (lr->l_flag & L_SINTR)
1774 1.187 cl return lr;
1775 1.187 cl }
1776 1.175 cl }
1777 1.175 cl return NULL;
1778 1.175 cl }
1779 1.130 thorpej return lr;
1780 1.130 thorpej }
1781 1.130 thorpej
1782 1.29 cgd /*
1783 1.29 cgd * Take the action for the specified signal
1784 1.29 cgd * from the current set of pending signals.
1785 1.29 cgd */
1786 1.29 cgd void
1787 1.112 lukem postsig(int signum)
1788 1.29 cgd {
1789 1.130 thorpej struct lwp *l;
1790 1.112 lukem struct proc *p;
1791 1.112 lukem struct sigacts *ps;
1792 1.112 lukem sig_t action;
1793 1.112 lukem sigset_t *returnmask;
1794 1.29 cgd
1795 1.130 thorpej l = curlwp;
1796 1.130 thorpej p = l->l_proc;
1797 1.112 lukem ps = p->p_sigacts;
1798 1.29 cgd #ifdef DIAGNOSTIC
1799 1.29 cgd if (signum == 0)
1800 1.29 cgd panic("postsig");
1801 1.29 cgd #endif
1802 1.106 thorpej
1803 1.130 thorpej KERNEL_PROC_LOCK(l);
1804 1.106 thorpej
1805 1.188 cl #ifdef MULTIPROCESSOR
1806 1.188 cl /*
1807 1.188 cl * On MP, issignal() can return the same signal to multiple
1808 1.188 cl * LWPs. The LWPs will block above waiting for the kernel
1809 1.188 cl * lock and the first LWP which gets through will then remove
1810 1.188 cl * the signal from ps_siglist. All other LWPs exit here.
1811 1.188 cl */
1812 1.188 cl if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
1813 1.188 cl KERNEL_PROC_UNLOCK(l);
1814 1.188 cl return;
1815 1.188 cl }
1816 1.188 cl #endif
1817 1.109 jdolecek sigdelset(&p->p_sigctx.ps_siglist, signum);
1818 1.109 jdolecek action = SIGACTION_PS(ps, signum).sa_handler;
1819 1.157 christos if (action == SIG_DFL) {
1820 1.29 cgd #ifdef KTRACE
1821 1.157 christos if (KTRPOINT(p, KTR_PSIG))
1822 1.157 christos ktrpsig(p, signum, action,
1823 1.157 christos p->p_sigctx.ps_flags & SAS_OLDMASK ?
1824 1.157 christos &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1825 1.157 christos NULL);
1826 1.29 cgd #endif
1827 1.29 cgd /*
1828 1.29 cgd * Default action, where the default is to kill
1829 1.29 cgd * the process. (Other cases were ignored above.)
1830 1.29 cgd */
1831 1.130 thorpej sigexit(l, signum);
1832 1.29 cgd /* NOTREACHED */
1833 1.29 cgd } else {
1834 1.152 christos ksiginfo_t *ksi;
1835 1.29 cgd /*
1836 1.29 cgd * If we get here, the signal must be caught.
1837 1.29 cgd */
1838 1.29 cgd #ifdef DIAGNOSTIC
1839 1.112 lukem if (action == SIG_IGN ||
1840 1.112 lukem sigismember(&p->p_sigctx.ps_sigmask, signum))
1841 1.29 cgd panic("postsig action");
1842 1.29 cgd #endif
1843 1.29 cgd /*
1844 1.29 cgd * Set the new mask value and also defer further
1845 1.138 wiz * occurrences of this signal.
1846 1.29 cgd *
1847 1.29 cgd * Special case: user has done a sigpause. Here the
1848 1.29 cgd * current mask is not of interest, but rather the
1849 1.29 cgd * mask from before the sigpause is what we want
1850 1.29 cgd * restored after the signal processing is completed.
1851 1.29 cgd */
1852 1.109 jdolecek if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1853 1.109 jdolecek returnmask = &p->p_sigctx.ps_oldmask;
1854 1.109 jdolecek p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1855 1.29 cgd } else
1856 1.109 jdolecek returnmask = &p->p_sigctx.ps_sigmask;
1857 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
1858 1.152 christos ksi = ksiginfo_get(p, signum);
1859 1.157 christos #ifdef KTRACE
1860 1.157 christos if (KTRPOINT(p, KTR_PSIG))
1861 1.157 christos ktrpsig(p, signum, action,
1862 1.157 christos p->p_sigctx.ps_flags & SAS_OLDMASK ?
1863 1.157 christos &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1864 1.157 christos ksi);
1865 1.157 christos #endif
1866 1.152 christos if (ksi == NULL) {
1867 1.152 christos ksiginfo_t ksi1;
1868 1.152 christos /*
1869 1.152 christos * we did not save any siginfo for this, either
1870 1.152 christos * because the signal was not caught, or because the
1871 1.152 christos * user did not request SA_SIGINFO
1872 1.152 christos */
1873 1.192 matt KSI_INIT_EMPTY(&ksi1);
1874 1.152 christos ksi1.ksi_signo = signum;
1875 1.152 christos kpsendsig(l, &ksi1, returnmask);
1876 1.29 cgd } else {
1877 1.152 christos kpsendsig(l, ksi, returnmask);
1878 1.152 christos pool_put(&ksiginfo_pool, ksi);
1879 1.152 christos }
1880 1.152 christos p->p_sigctx.ps_lwp = 0;
1881 1.152 christos p->p_sigctx.ps_code = 0;
1882 1.152 christos p->p_sigctx.ps_signo = 0;
1883 1.105 thorpej (void) splsched(); /* XXXSMP */
1884 1.112 lukem sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1885 1.112 lukem &p->p_sigctx.ps_sigmask);
1886 1.109 jdolecek if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1887 1.109 jdolecek sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1888 1.79 mycroft if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1889 1.109 jdolecek sigaddset(&p->p_sigctx.ps_sigignore, signum);
1890 1.109 jdolecek SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1891 1.79 mycroft }
1892 1.104 thorpej (void) spl0(); /* XXXSMP */
1893 1.29 cgd }
1894 1.106 thorpej
1895 1.130 thorpej KERNEL_PROC_UNLOCK(l);
1896 1.29 cgd }
1897 1.29 cgd
1898 1.29 cgd /*
1899 1.29 cgd * Kill the current process for stated reason.
1900 1.29 cgd */
1901 1.52 christos void
1902 1.122 manu killproc(struct proc *p, const char *why)
1903 1.29 cgd {
1904 1.29 cgd log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1905 1.29 cgd uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1906 1.29 cgd psignal(p, SIGKILL);
1907 1.29 cgd }
1908 1.29 cgd
1909 1.29 cgd /*
1910 1.29 cgd * Force the current process to exit with the specified signal, dumping core
1911 1.29 cgd * if appropriate. We bypass the normal tests for masked and caught signals,
1912 1.29 cgd * allowing unrecoverable failures to terminate the process without changing
1913 1.29 cgd * signal state. Mark the accounting record with the signal termination.
1914 1.29 cgd * If dumping core, save the signal number for the debugger. Calls exit and
1915 1.29 cgd * does not return.
1916 1.29 cgd */
1917 1.96 fair
1918 1.97 fair #if defined(DEBUG)
1919 1.96 fair int kern_logsigexit = 1; /* not static to make public for sysctl */
1920 1.96 fair #else
1921 1.96 fair int kern_logsigexit = 0; /* not static to make public for sysctl */
1922 1.96 fair #endif
1923 1.96 fair
1924 1.102 sommerfe static const char logcoredump[] =
1925 1.96 fair "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1926 1.102 sommerfe static const char lognocoredump[] =
1927 1.96 fair "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1928 1.96 fair
1929 1.130 thorpej /* Wrapper function for use in p_userret */
1930 1.130 thorpej static void
1931 1.130 thorpej lwp_coredump_hook(struct lwp *l, void *arg)
1932 1.130 thorpej {
1933 1.130 thorpej int s;
1934 1.130 thorpej
1935 1.130 thorpej /*
1936 1.130 thorpej * Suspend ourselves, so that the kernel stack and therefore
1937 1.130 thorpej * the userland registers saved in the trapframe are around
1938 1.130 thorpej * for coredump() to write them out.
1939 1.130 thorpej */
1940 1.130 thorpej KERNEL_PROC_LOCK(l);
1941 1.130 thorpej l->l_flag &= ~L_DETACHED;
1942 1.130 thorpej SCHED_LOCK(s);
1943 1.130 thorpej l->l_stat = LSSUSPENDED;
1944 1.130 thorpej l->l_proc->p_nrlwps--;
1945 1.130 thorpej /* XXX NJWLWP check if this makes sense here: */
1946 1.130 thorpej l->l_proc->p_stats->p_ru.ru_nvcsw++;
1947 1.130 thorpej mi_switch(l, NULL);
1948 1.130 thorpej SCHED_ASSERT_UNLOCKED();
1949 1.130 thorpej splx(s);
1950 1.130 thorpej
1951 1.130 thorpej lwp_exit(l);
1952 1.130 thorpej }
1953 1.130 thorpej
1954 1.52 christos void
1955 1.130 thorpej sigexit(struct lwp *l, int signum)
1956 1.29 cgd {
1957 1.130 thorpej struct proc *p;
1958 1.144 fvdl #if 0
1959 1.136 nathanw struct lwp *l2;
1960 1.144 fvdl #endif
1961 1.130 thorpej int error, exitsig;
1962 1.130 thorpej
1963 1.130 thorpej p = l->l_proc;
1964 1.130 thorpej
1965 1.130 thorpej /*
1966 1.130 thorpej * Don't permit coredump() or exit1() multiple times
1967 1.130 thorpej * in the same process.
1968 1.130 thorpej */
1969 1.136 nathanw if (p->p_flag & P_WEXIT) {
1970 1.136 nathanw KERNEL_PROC_UNLOCK(l);
1971 1.130 thorpej (*p->p_userret)(l, p->p_userret_arg);
1972 1.136 nathanw }
1973 1.130 thorpej p->p_flag |= P_WEXIT;
1974 1.130 thorpej /* We don't want to switch away from exiting. */
1975 1.130 thorpej /* XXX multiprocessor: stop LWPs on other processors. */
1976 1.144 fvdl #if 0
1977 1.136 nathanw if (p->p_flag & P_SA) {
1978 1.136 nathanw LIST_FOREACH(l2, &p->p_lwps, l_sibling)
1979 1.136 nathanw l2->l_flag &= ~L_SA;
1980 1.130 thorpej p->p_flag &= ~P_SA;
1981 1.130 thorpej }
1982 1.144 fvdl #endif
1983 1.130 thorpej
1984 1.130 thorpej /* Make other LWPs stick around long enough to be dumped */
1985 1.130 thorpej p->p_userret = lwp_coredump_hook;
1986 1.130 thorpej p->p_userret_arg = NULL;
1987 1.96 fair
1988 1.112 lukem exitsig = signum;
1989 1.29 cgd p->p_acflag |= AXSIG;
1990 1.29 cgd if (sigprop[signum] & SA_CORE) {
1991 1.152 christos p->p_sigctx.ps_signo = signum;
1992 1.130 thorpej if ((error = coredump(l)) == 0)
1993 1.102 sommerfe exitsig |= WCOREFLAG;
1994 1.102 sommerfe
1995 1.102 sommerfe if (kern_logsigexit) {
1996 1.123 thorpej /* XXX What if we ever have really large UIDs? */
1997 1.102 sommerfe int uid = p->p_cred && p->p_ucred ?
1998 1.123 thorpej (int) p->p_ucred->cr_uid : -1;
1999 1.102 sommerfe
2000 1.102 sommerfe if (error)
2001 1.102 sommerfe log(LOG_INFO, lognocoredump, p->p_pid,
2002 1.102 sommerfe p->p_comm, uid, signum, error);
2003 1.102 sommerfe else
2004 1.102 sommerfe log(LOG_INFO, logcoredump, p->p_pid,
2005 1.102 sommerfe p->p_comm, uid, signum);
2006 1.96 fair }
2007 1.96 fair
2008 1.29 cgd }
2009 1.96 fair
2010 1.130 thorpej exit1(l, W_EXITCODE(0, exitsig));
2011 1.29 cgd /* NOTREACHED */
2012 1.29 cgd }
2013 1.29 cgd
2014 1.29 cgd /*
2015 1.75 nathanw * Dump core, into a file named "progname.core" or "core" (depending on the
2016 1.75 nathanw * value of shortcorename), unless the process was setuid/setgid.
2017 1.29 cgd */
2018 1.29 cgd int
2019 1.130 thorpej coredump(struct lwp *l)
2020 1.29 cgd {
2021 1.112 lukem struct vnode *vp;
2022 1.130 thorpej struct proc *p;
2023 1.112 lukem struct vmspace *vm;
2024 1.112 lukem struct ucred *cred;
2025 1.112 lukem struct nameidata nd;
2026 1.112 lukem struct vattr vattr;
2027 1.169 hannken struct mount *mp;
2028 1.112 lukem int error, error1;
2029 1.112 lukem char name[MAXPATHLEN];
2030 1.112 lukem
2031 1.130 thorpej p = l->l_proc;
2032 1.112 lukem vm = p->p_vmspace;
2033 1.112 lukem cred = p->p_cred->pc_ucred;
2034 1.29 cgd
2035 1.59 cgd /*
2036 1.59 cgd * Make sure the process has not set-id, to prevent data leaks.
2037 1.59 cgd */
2038 1.58 mrg if (p->p_flag & P_SUGID)
2039 1.59 cgd return (EPERM);
2040 1.59 cgd
2041 1.59 cgd /*
2042 1.59 cgd * Refuse to core if the data + stack + user size is larger than
2043 1.59 cgd * the core dump limit. XXX THIS IS WRONG, because of mapped
2044 1.59 cgd * data.
2045 1.59 cgd */
2046 1.30 deraadt if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
2047 1.29 cgd p->p_rlimit[RLIMIT_CORE].rlim_cur)
2048 1.59 cgd return (EFBIG); /* better error code? */
2049 1.59 cgd
2050 1.169 hannken restart:
2051 1.59 cgd /*
2052 1.59 cgd * The core dump will go in the current working directory. Make
2053 1.80 pk * sure that the directory is still there and that the mount flags
2054 1.80 pk * allow us to write core dumps there.
2055 1.59 cgd */
2056 1.88 thorpej vp = p->p_cwdi->cwdi_cdir;
2057 1.80 pk if (vp->v_mount == NULL ||
2058 1.80 pk (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
2059 1.59 cgd return (EPERM);
2060 1.59 cgd
2061 1.100 sommerfe error = build_corename(p, name);
2062 1.94 bouyer if (error)
2063 1.94 bouyer return error;
2064 1.94 bouyer
2065 1.143 fvdl NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
2066 1.129 christos error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
2067 1.52 christos if (error)
2068 1.29 cgd return (error);
2069 1.29 cgd vp = nd.ni_vp;
2070 1.29 cgd
2071 1.169 hannken if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2072 1.169 hannken VOP_UNLOCK(vp, 0);
2073 1.169 hannken if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
2074 1.169 hannken return (error);
2075 1.169 hannken if ((error = vn_start_write(NULL, &mp,
2076 1.169 hannken V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
2077 1.169 hannken return (error);
2078 1.169 hannken goto restart;
2079 1.169 hannken }
2080 1.169 hannken
2081 1.29 cgd /* Don't dump to non-regular files or files with links. */
2082 1.29 cgd if (vp->v_type != VREG ||
2083 1.143 fvdl VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
2084 1.59 cgd error = EINVAL;
2085 1.29 cgd goto out;
2086 1.29 cgd }
2087 1.29 cgd VATTR_NULL(&vattr);
2088 1.29 cgd vattr.va_size = 0;
2089 1.143 fvdl VOP_LEASE(vp, p, cred, LEASE_WRITE);
2090 1.143 fvdl VOP_SETATTR(vp, &vattr, cred, p);
2091 1.29 cgd p->p_acflag |= ACORE;
2092 1.95 eeh
2093 1.118 thorpej /* Now dump the actual core file. */
2094 1.130 thorpej error = (*p->p_execsw->es_coredump)(l, vp, cred);
2095 1.112 lukem out:
2096 1.71 fvdl VOP_UNLOCK(vp, 0);
2097 1.169 hannken vn_finished_write(mp, 0);
2098 1.143 fvdl error1 = vn_close(vp, FWRITE, cred, p);
2099 1.29 cgd if (error == 0)
2100 1.29 cgd error = error1;
2101 1.29 cgd return (error);
2102 1.29 cgd }
2103 1.29 cgd
2104 1.29 cgd /*
2105 1.29 cgd * Nonexistent system call-- signal process (may want to handle it).
2106 1.29 cgd * Flag error in case process won't see signal immediately (blocked or ignored).
2107 1.29 cgd */
2108 1.29 cgd /* ARGSUSED */
2109 1.29 cgd int
2110 1.130 thorpej sys_nosys(struct lwp *l, void *v, register_t *retval)
2111 1.29 cgd {
2112 1.130 thorpej struct proc *p;
2113 1.29 cgd
2114 1.130 thorpej p = l->l_proc;
2115 1.29 cgd psignal(p, SIGSYS);
2116 1.36 cgd return (ENOSYS);
2117 1.94 bouyer }
2118 1.94 bouyer
2119 1.94 bouyer static int
2120 1.112 lukem build_corename(struct proc *p, char dst[MAXPATHLEN])
2121 1.94 bouyer {
2122 1.112 lukem const char *s;
2123 1.112 lukem char *d, *end;
2124 1.112 lukem int i;
2125 1.100 sommerfe
2126 1.107 enami for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
2127 1.94 bouyer *s != '\0'; s++) {
2128 1.94 bouyer if (*s == '%') {
2129 1.107 enami switch (*(s + 1)) {
2130 1.94 bouyer case 'n':
2131 1.107 enami i = snprintf(d, end - d, "%s", p->p_comm);
2132 1.94 bouyer break;
2133 1.94 bouyer case 'p':
2134 1.107 enami i = snprintf(d, end - d, "%d", p->p_pid);
2135 1.94 bouyer break;
2136 1.94 bouyer case 'u':
2137 1.134 dsl i = snprintf(d, end - d, "%.*s",
2138 1.134 dsl (int)sizeof p->p_pgrp->pg_session->s_login,
2139 1.100 sommerfe p->p_pgrp->pg_session->s_login);
2140 1.94 bouyer break;
2141 1.94 bouyer case 't':
2142 1.107 enami i = snprintf(d, end - d, "%ld",
2143 1.100 sommerfe p->p_stats->p_start.tv_sec);
2144 1.94 bouyer break;
2145 1.94 bouyer default:
2146 1.94 bouyer goto copy;
2147 1.94 bouyer }
2148 1.94 bouyer d += i;
2149 1.94 bouyer s++;
2150 1.94 bouyer } else {
2151 1.112 lukem copy: *d = *s;
2152 1.94 bouyer d++;
2153 1.94 bouyer }
2154 1.107 enami if (d >= end)
2155 1.107 enami return (ENAMETOOLONG);
2156 1.94 bouyer }
2157 1.94 bouyer *d = '\0';
2158 1.130 thorpej return 0;
2159 1.130 thorpej }
2160 1.130 thorpej
2161 1.130 thorpej void
2162 1.130 thorpej getucontext(struct lwp *l, ucontext_t *ucp)
2163 1.130 thorpej {
2164 1.130 thorpej struct proc *p;
2165 1.130 thorpej
2166 1.130 thorpej p = l->l_proc;
2167 1.130 thorpej
2168 1.130 thorpej ucp->uc_flags = 0;
2169 1.130 thorpej ucp->uc_link = l->l_ctxlink;
2170 1.130 thorpej
2171 1.130 thorpej (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
2172 1.130 thorpej ucp->uc_flags |= _UC_SIGMASK;
2173 1.130 thorpej
2174 1.130 thorpej /*
2175 1.130 thorpej * The (unsupplied) definition of the `current execution stack'
2176 1.130 thorpej * in the System V Interface Definition appears to allow returning
2177 1.130 thorpej * the main context stack.
2178 1.130 thorpej */
2179 1.130 thorpej if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
2180 1.130 thorpej ucp->uc_stack.ss_sp = (void *)USRSTACK;
2181 1.130 thorpej ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
2182 1.130 thorpej ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
2183 1.130 thorpej } else {
2184 1.130 thorpej /* Simply copy alternate signal execution stack. */
2185 1.130 thorpej ucp->uc_stack = p->p_sigctx.ps_sigstk;
2186 1.130 thorpej }
2187 1.130 thorpej ucp->uc_flags |= _UC_STACK;
2188 1.130 thorpej
2189 1.130 thorpej cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
2190 1.130 thorpej }
2191 1.130 thorpej
2192 1.130 thorpej /* ARGSUSED */
2193 1.130 thorpej int
2194 1.130 thorpej sys_getcontext(struct lwp *l, void *v, register_t *retval)
2195 1.130 thorpej {
2196 1.130 thorpej struct sys_getcontext_args /* {
2197 1.130 thorpej syscallarg(struct __ucontext *) ucp;
2198 1.130 thorpej } */ *uap = v;
2199 1.130 thorpej ucontext_t uc;
2200 1.130 thorpej
2201 1.130 thorpej getucontext(l, &uc);
2202 1.130 thorpej
2203 1.130 thorpej return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
2204 1.130 thorpej }
2205 1.130 thorpej
2206 1.130 thorpej int
2207 1.130 thorpej setucontext(struct lwp *l, const ucontext_t *ucp)
2208 1.130 thorpej {
2209 1.130 thorpej struct proc *p;
2210 1.130 thorpej int error;
2211 1.130 thorpej
2212 1.130 thorpej p = l->l_proc;
2213 1.130 thorpej if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
2214 1.130 thorpej return (error);
2215 1.130 thorpej l->l_ctxlink = ucp->uc_link;
2216 1.185 matt
2217 1.185 matt if ((ucp->uc_flags & _UC_SIGMASK) != 0)
2218 1.185 matt sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
2219 1.185 matt
2220 1.130 thorpej /*
2221 1.185 matt * If there was stack information, update whether or not we are
2222 1.185 matt * still running on an alternate signal stack.
2223 1.130 thorpej */
2224 1.185 matt if ((ucp->uc_flags & _UC_STACK) != 0) {
2225 1.185 matt if (ucp->uc_stack.ss_flags & SS_ONSTACK)
2226 1.185 matt p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
2227 1.185 matt else
2228 1.185 matt p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
2229 1.185 matt }
2230 1.130 thorpej
2231 1.130 thorpej return 0;
2232 1.130 thorpej }
2233 1.130 thorpej
2234 1.130 thorpej /* ARGSUSED */
2235 1.130 thorpej int
2236 1.130 thorpej sys_setcontext(struct lwp *l, void *v, register_t *retval)
2237 1.130 thorpej {
2238 1.130 thorpej struct sys_setcontext_args /* {
2239 1.130 thorpej syscallarg(const ucontext_t *) ucp;
2240 1.130 thorpej } */ *uap = v;
2241 1.130 thorpej ucontext_t uc;
2242 1.130 thorpej int error;
2243 1.130 thorpej
2244 1.130 thorpej if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
2245 1.130 thorpej exit1(l, W_EXITCODE(0, 0));
2246 1.130 thorpej else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
2247 1.130 thorpej (error = setucontext(l, &uc)) != 0)
2248 1.130 thorpej return (error);
2249 1.130 thorpej
2250 1.130 thorpej return (EJUSTRETURN);
2251 1.108 jdolecek }
2252 1.130 thorpej
2253 1.135 jdolecek /*
2254 1.135 jdolecek * sigtimedwait(2) system call, used also for implementation
2255 1.135 jdolecek * of sigwaitinfo() and sigwait().
2256 1.135 jdolecek *
2257 1.135 jdolecek * This only handles single LWP in signal wait. libpthread provides
2258 1.135 jdolecek * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
2259 1.135 jdolecek */
2260 1.135 jdolecek int
2261 1.135 jdolecek sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
2262 1.135 jdolecek {
2263 1.135 jdolecek struct sys___sigtimedwait_args /* {
2264 1.135 jdolecek syscallarg(const sigset_t *) set;
2265 1.135 jdolecek syscallarg(siginfo_t *) info;
2266 1.135 jdolecek syscallarg(struct timespec *) timeout;
2267 1.135 jdolecek } */ *uap = v;
2268 1.173 jdolecek sigset_t *waitset, twaitset;
2269 1.135 jdolecek struct proc *p = l->l_proc;
2270 1.135 jdolecek int error, signum, s;
2271 1.135 jdolecek int timo = 0;
2272 1.135 jdolecek struct timeval tvstart;
2273 1.135 jdolecek struct timespec ts;
2274 1.171 jdolecek ksiginfo_t *ksi;
2275 1.135 jdolecek
2276 1.173 jdolecek MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
2277 1.173 jdolecek
2278 1.173 jdolecek if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
2279 1.173 jdolecek FREE(waitset, M_TEMP);
2280 1.135 jdolecek return (error);
2281 1.173 jdolecek }
2282 1.135 jdolecek
2283 1.135 jdolecek /*
2284 1.135 jdolecek * Silently ignore SA_CANTMASK signals. psignal1() would
2285 1.135 jdolecek * ignore SA_CANTMASK signals in waitset, we do this
2286 1.135 jdolecek * only for the below siglist check.
2287 1.135 jdolecek */
2288 1.173 jdolecek sigminusset(&sigcantmask, waitset);
2289 1.135 jdolecek
2290 1.135 jdolecek /*
2291 1.135 jdolecek * First scan siglist and check if there is signal from
2292 1.135 jdolecek * our waitset already pending.
2293 1.135 jdolecek */
2294 1.173 jdolecek twaitset = *waitset;
2295 1.135 jdolecek __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
2296 1.135 jdolecek if ((signum = firstsig(&twaitset))) {
2297 1.135 jdolecek /* found pending signal */
2298 1.135 jdolecek sigdelset(&p->p_sigctx.ps_siglist, signum);
2299 1.171 jdolecek ksi = ksiginfo_get(p, signum);
2300 1.171 jdolecek if (!ksi) {
2301 1.171 jdolecek /* No queued siginfo, manufacture one */
2302 1.171 jdolecek ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2303 1.171 jdolecek KSI_INIT(ksi);
2304 1.171 jdolecek ksi->ksi_info._signo = signum;
2305 1.171 jdolecek ksi->ksi_info._code = SI_USER;
2306 1.171 jdolecek }
2307 1.171 jdolecek
2308 1.135 jdolecek goto sig;
2309 1.135 jdolecek }
2310 1.135 jdolecek
2311 1.135 jdolecek /*
2312 1.135 jdolecek * Calculate timeout, if it was specified.
2313 1.135 jdolecek */
2314 1.135 jdolecek if (SCARG(uap, timeout)) {
2315 1.135 jdolecek uint64_t ms;
2316 1.135 jdolecek
2317 1.135 jdolecek if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
2318 1.135 jdolecek return (error);
2319 1.135 jdolecek
2320 1.135 jdolecek ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
2321 1.135 jdolecek timo = mstohz(ms);
2322 1.135 jdolecek if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
2323 1.135 jdolecek timo = 1;
2324 1.135 jdolecek if (timo <= 0)
2325 1.135 jdolecek return (EAGAIN);
2326 1.135 jdolecek
2327 1.135 jdolecek /*
2328 1.135 jdolecek * Remember current mono_time, it would be used in
2329 1.135 jdolecek * ECANCELED/ERESTART case.
2330 1.135 jdolecek */
2331 1.135 jdolecek s = splclock();
2332 1.135 jdolecek tvstart = mono_time;
2333 1.135 jdolecek splx(s);
2334 1.135 jdolecek }
2335 1.135 jdolecek
2336 1.135 jdolecek /*
2337 1.173 jdolecek * Setup ps_sigwait list. Pass pointer to malloced memory
2338 1.173 jdolecek * here; it's not possible to pass pointer to a structure
2339 1.173 jdolecek * on current process's stack, the current process might
2340 1.173 jdolecek * be swapped out at the time the signal would get delivered.
2341 1.135 jdolecek */
2342 1.171 jdolecek ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2343 1.171 jdolecek p->p_sigctx.ps_sigwaited = ksi;
2344 1.173 jdolecek p->p_sigctx.ps_sigwait = waitset;
2345 1.135 jdolecek
2346 1.135 jdolecek /*
2347 1.135 jdolecek * Wait for signal to arrive. We can either be woken up or
2348 1.135 jdolecek * time out.
2349 1.135 jdolecek */
2350 1.135 jdolecek error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
2351 1.135 jdolecek
2352 1.135 jdolecek /*
2353 1.171 jdolecek * Need to find out if we woke as a result of lwp_wakeup()
2354 1.171 jdolecek * or a signal outside our wait set.
2355 1.135 jdolecek */
2356 1.171 jdolecek if (error == EINTR && p->p_sigctx.ps_sigwaited
2357 1.171 jdolecek && !firstsig(&p->p_sigctx.ps_siglist)) {
2358 1.171 jdolecek /* wakeup via _lwp_wakeup() */
2359 1.171 jdolecek error = ECANCELED;
2360 1.171 jdolecek } else if (!error && p->p_sigctx.ps_sigwaited) {
2361 1.171 jdolecek /* spurious wakeup - arrange for syscall restart */
2362 1.171 jdolecek error = ERESTART;
2363 1.171 jdolecek goto fail;
2364 1.135 jdolecek }
2365 1.135 jdolecek
2366 1.135 jdolecek /*
2367 1.171 jdolecek * On error, clear sigwait indication. psignal1() clears it
2368 1.135 jdolecek * in !error case.
2369 1.135 jdolecek */
2370 1.135 jdolecek if (error) {
2371 1.171 jdolecek p->p_sigctx.ps_sigwaited = NULL;
2372 1.135 jdolecek
2373 1.135 jdolecek /*
2374 1.135 jdolecek * If the sleep was interrupted (either by signal or wakeup),
2375 1.135 jdolecek * update the timeout and copyout new value back.
2376 1.135 jdolecek * It would be used when the syscall would be restarted
2377 1.135 jdolecek * or called again.
2378 1.135 jdolecek */
2379 1.135 jdolecek if (timo && (error == ERESTART || error == ECANCELED)) {
2380 1.135 jdolecek struct timeval tvnow, tvtimo;
2381 1.135 jdolecek int err;
2382 1.135 jdolecek
2383 1.135 jdolecek s = splclock();
2384 1.135 jdolecek tvnow = mono_time;
2385 1.135 jdolecek splx(s);
2386 1.135 jdolecek
2387 1.135 jdolecek TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
2388 1.135 jdolecek
2389 1.135 jdolecek /* compute how much time has passed since start */
2390 1.135 jdolecek timersub(&tvnow, &tvstart, &tvnow);
2391 1.135 jdolecek /* substract passed time from timeout */
2392 1.135 jdolecek timersub(&tvtimo, &tvnow, &tvtimo);
2393 1.135 jdolecek
2394 1.171 jdolecek if (tvtimo.tv_sec < 0) {
2395 1.171 jdolecek error = EAGAIN;
2396 1.171 jdolecek goto fail;
2397 1.171 jdolecek }
2398 1.171 jdolecek
2399 1.135 jdolecek TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
2400 1.135 jdolecek
2401 1.135 jdolecek /* copy updated timeout to userland */
2402 1.171 jdolecek if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
2403 1.171 jdolecek error = err;
2404 1.171 jdolecek goto fail;
2405 1.171 jdolecek }
2406 1.135 jdolecek }
2407 1.135 jdolecek
2408 1.171 jdolecek goto fail;
2409 1.135 jdolecek }
2410 1.135 jdolecek
2411 1.135 jdolecek /*
2412 1.135 jdolecek * If a signal from the wait set arrived, copy it to userland.
2413 1.171 jdolecek * Copy only the used part of siginfo, the padding part is
2414 1.171 jdolecek * left unchanged (userland is not supposed to touch it anyway).
2415 1.135 jdolecek */
2416 1.135 jdolecek sig:
2417 1.171 jdolecek error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
2418 1.135 jdolecek
2419 1.171 jdolecek fail:
2420 1.173 jdolecek FREE(waitset, M_TEMP);
2421 1.171 jdolecek pool_put(&ksiginfo_pool, ksi);
2422 1.171 jdolecek p->p_sigctx.ps_sigwait = NULL;
2423 1.135 jdolecek
2424 1.171 jdolecek return (error);
2425 1.135 jdolecek }
2426 1.108 jdolecek
2427 1.108 jdolecek /*
2428 1.108 jdolecek * Returns true if signal is ignored or masked for passed process.
2429 1.108 jdolecek */
2430 1.108 jdolecek int
2431 1.112 lukem sigismasked(struct proc *p, int sig)
2432 1.108 jdolecek {
2433 1.112 lukem
2434 1.117 enami return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2435 1.117 enami sigismember(&p->p_sigctx.ps_sigmask, sig));
2436 1.29 cgd }
2437 1.126 jdolecek
2438 1.126 jdolecek static int
2439 1.126 jdolecek filt_sigattach(struct knote *kn)
2440 1.126 jdolecek {
2441 1.126 jdolecek struct proc *p = curproc;
2442 1.126 jdolecek
2443 1.126 jdolecek kn->kn_ptr.p_proc = p;
2444 1.126 jdolecek kn->kn_flags |= EV_CLEAR; /* automatically set */
2445 1.126 jdolecek
2446 1.126 jdolecek SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2447 1.126 jdolecek
2448 1.126 jdolecek return (0);
2449 1.126 jdolecek }
2450 1.126 jdolecek
2451 1.126 jdolecek static void
2452 1.126 jdolecek filt_sigdetach(struct knote *kn)
2453 1.126 jdolecek {
2454 1.126 jdolecek struct proc *p = kn->kn_ptr.p_proc;
2455 1.126 jdolecek
2456 1.126 jdolecek SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2457 1.126 jdolecek }
2458 1.126 jdolecek
2459 1.126 jdolecek /*
2460 1.126 jdolecek * signal knotes are shared with proc knotes, so we apply a mask to
2461 1.126 jdolecek * the hint in order to differentiate them from process hints. This
2462 1.126 jdolecek * could be avoided by using a signal-specific knote list, but probably
2463 1.126 jdolecek * isn't worth the trouble.
2464 1.126 jdolecek */
2465 1.126 jdolecek static int
2466 1.126 jdolecek filt_signal(struct knote *kn, long hint)
2467 1.126 jdolecek {
2468 1.126 jdolecek
2469 1.126 jdolecek if (hint & NOTE_SIGNAL) {
2470 1.126 jdolecek hint &= ~NOTE_SIGNAL;
2471 1.126 jdolecek
2472 1.126 jdolecek if (kn->kn_id == hint)
2473 1.126 jdolecek kn->kn_data++;
2474 1.126 jdolecek }
2475 1.126 jdolecek return (kn->kn_data != 0);
2476 1.126 jdolecek }
2477 1.126 jdolecek
2478 1.126 jdolecek const struct filterops sig_filtops = {
2479 1.126 jdolecek 0, filt_sigattach, filt_sigdetach, filt_signal
2480 1.126 jdolecek };
2481