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