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