Annotation of src/sys/kern/kern_sig.c, Revision 1.344
1.344 ! kamil 1: /* $NetBSD: kern_sig.c,v 1.343 2018/05/06 13:40:51 kamil Exp $ */
1.243 ad 2:
3: /*-
1.277 ad 4: * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
1.243 ad 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Andrew Doran.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
1.29 cgd 31:
32: /*
33: * Copyright (c) 1982, 1986, 1989, 1991, 1993
34: * The Regents of the University of California. All rights reserved.
35: * (c) UNIX System Laboratories, Inc.
36: * All or some portions of this file are derived from material licensed
37: * to the University of California by American Telephone and Telegraph
38: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39: * the permission of UNIX System Laboratories, Inc.
40: *
41: * Redistribution and use in source and binary forms, with or without
42: * modification, are permitted provided that the following conditions
43: * are met:
44: * 1. Redistributions of source code must retain the above copyright
45: * notice, this list of conditions and the following disclaimer.
46: * 2. Redistributions in binary form must reproduce the above copyright
47: * notice, this list of conditions and the following disclaimer in the
48: * documentation and/or other materials provided with the distribution.
1.146 agc 49: * 3. Neither the name of the University nor the names of its contributors
1.29 cgd 50: * may be used to endorse or promote products derived from this software
51: * without specific prior written permission.
52: *
53: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63: * SUCH DAMAGE.
64: *
1.71 fvdl 65: * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
1.29 cgd 66: */
1.116 lukem 67:
1.308 rmind 68: /*
69: * Signal subsystem.
70: */
71:
1.116 lukem 72: #include <sys/cdefs.h>
1.344 ! kamil 73: __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.343 2018/05/06 13:40:51 kamil Exp $");
1.70 mrg 74:
1.227 matt 75: #include "opt_ptrace.h"
1.320 christos 76: #include "opt_dtrace.h"
1.74 thorpej 77: #include "opt_compat_sunos.h"
1.158 christos 78: #include "opt_compat_netbsd.h"
1.202 perry 79: #include "opt_compat_netbsd32.h"
1.240 elad 80: #include "opt_pax.h"
1.29 cgd 81:
82: #define SIGPROP /* include signal properties table */
83: #include <sys/param.h>
84: #include <sys/signalvar.h>
85: #include <sys/proc.h>
1.334 christos 86: #include <sys/ptrace.h>
1.29 cgd 87: #include <sys/systm.h>
88: #include <sys/wait.h>
89: #include <sys/ktrace.h>
90: #include <sys/syslog.h>
1.59 cgd 91: #include <sys/filedesc.h>
1.243 ad 92: #include <sys/file.h>
1.89 thorpej 93: #include <sys/pool.h>
1.130 thorpej 94: #include <sys/ucontext.h>
1.118 thorpej 95: #include <sys/exec.h>
1.220 elad 96: #include <sys/kauth.h>
1.243 ad 97: #include <sys/acct.h>
98: #include <sys/callout.h>
1.260 ad 99: #include <sys/atomic.h>
1.258 ad 100: #include <sys/cpu.h>
1.290 ad 101: #include <sys/module.h>
1.303 darran 102: #include <sys/sdt.h>
1.29 cgd 103:
1.240 elad 104: #ifdef PAX_SEGVGUARD
105: #include <sys/pax.h>
106: #endif /* PAX_SEGVGUARD */
107:
1.307 uebayasi 108: #include <uvm/uvm_extern.h>
1.69 mrg 109:
1.328 christos 110: #define SIGQUEUE_MAX 32
1.308 rmind 111: static pool_cache_t sigacts_cache __read_mostly;
112: static pool_cache_t ksiginfo_cache __read_mostly;
113: static callout_t proc_stop_ch __cacheline_aligned;
114:
115: sigset_t contsigmask __cacheline_aligned;
116: static sigset_t stopsigmask __cacheline_aligned;
117: sigset_t sigcantmask __cacheline_aligned;
118:
1.243 ad 119: static void ksiginfo_exechook(struct proc *, void *);
1.334 christos 120: static void proc_stop(struct proc *, int);
1.342 kamil 121: static void proc_stop_done(struct proc *, int);
1.243 ad 122: static void proc_stop_callout(void *);
1.297 rmind 123: static int sigchecktrace(void);
1.317 rmind 124: static int sigpost(struct lwp *, sig_t, int, int);
1.328 christos 125: static int sigput(sigpend_t *, struct proc *, ksiginfo_t *);
1.297 rmind 126: static int sigunwait(struct proc *, const ksiginfo_t *);
1.152 christos 127:
1.243 ad 128: static void sigacts_poolpage_free(struct pool *, void *);
129: static void *sigacts_poolpage_alloc(struct pool *, int);
1.196 skrll 130:
1.290 ad 131: void (*sendsig_sigcontext_vec)(const struct ksiginfo *, const sigset_t *);
132: int (*coredump_vec)(struct lwp *, const char *) =
133: (int (*)(struct lwp *, const char *))enosys;
134:
1.303 darran 135: /*
136: * DTrace SDT provider definitions
137: */
1.320 christos 138: SDT_PROVIDER_DECLARE(proc);
139: SDT_PROBE_DEFINE3(proc, kernel, , signal__send,
140: "struct lwp *", /* target thread */
141: "struct proc *", /* target process */
142: "int"); /* signal */
143: SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
144: "struct lwp *", /* target thread */
145: "struct proc *", /* target process */
146: "int"); /* signal */
147: SDT_PROBE_DEFINE3(proc, kernel, , signal__handle,
148: "int", /* signal */
149: "ksiginfo_t *", /* signal info */
150: "void (*)(void)"); /* handler address */
1.303 darran 151:
152:
1.196 skrll 153: static struct pool_allocator sigactspool_allocator = {
1.301 rmind 154: .pa_alloc = sigacts_poolpage_alloc,
155: .pa_free = sigacts_poolpage_free
1.196 skrll 156: };
157:
1.243 ad 158: #ifdef DEBUG
159: int kern_logsigexit = 1;
160: #else
161: int kern_logsigexit = 0;
162: #endif
1.89 thorpej 163:
1.301 rmind 164: static const char logcoredump[] =
1.243 ad 165: "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1.301 rmind 166: static const char lognocoredump[] =
1.243 ad 167: "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1.237 yamt 168:
1.299 elad 169: static kauth_listener_t signal_listener;
170:
171: static int
172: signal_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
173: void *arg0, void *arg1, void *arg2, void *arg3)
174: {
175: struct proc *p;
176: int result, signum;
177:
178: result = KAUTH_RESULT_DEFER;
179: p = arg0;
180: signum = (int)(unsigned long)arg1;
181:
182: if (action != KAUTH_PROCESS_SIGNAL)
183: return result;
184:
185: if (kauth_cred_uidmatch(cred, p->p_cred) ||
186: (signum == SIGCONT && (curproc->p_session == p->p_session)))
187: result = KAUTH_RESULT_ALLOW;
188:
189: return result;
190: }
191:
1.29 cgd 192: /*
1.243 ad 193: * signal_init:
194: *
1.301 rmind 195: * Initialize global signal-related data structures.
1.152 christos 196: */
1.243 ad 197: void
198: signal_init(void)
1.152 christos 199: {
200:
1.243 ad 201: sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
1.152 christos 202:
1.279 ad 203: sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
204: "sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
205: &sigactspool_allocator : NULL, IPL_NONE, NULL, NULL, NULL);
1.286 ad 206: ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
207: "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
208:
1.243 ad 209: exechook_establish(ksiginfo_exechook, NULL);
1.152 christos 210:
1.265 ad 211: callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
1.243 ad 212: callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
1.299 elad 213:
214: signal_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
215: signal_listener_cb, NULL);
1.152 christos 216: }
217:
218: /*
1.243 ad 219: * sigacts_poolpage_alloc:
220: *
1.301 rmind 221: * Allocate a page for the sigacts memory pool.
1.152 christos 222: */
1.243 ad 223: static void *
224: sigacts_poolpage_alloc(struct pool *pp, int flags)
1.152 christos 225: {
226:
1.243 ad 227: return (void *)uvm_km_alloc(kernel_map,
1.301 rmind 228: PAGE_SIZE * 2, PAGE_SIZE * 2,
1.243 ad 229: ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
230: | UVM_KMF_WIRED);
1.152 christos 231: }
232:
233: /*
1.243 ad 234: * sigacts_poolpage_free:
235: *
1.301 rmind 236: * Free a page on behalf of the sigacts memory pool.
1.89 thorpej 237: */
1.243 ad 238: static void
239: sigacts_poolpage_free(struct pool *pp, void *v)
1.89 thorpej 240: {
1.279 ad 241:
1.301 rmind 242: uvm_km_free(kernel_map, (vaddr_t)v, PAGE_SIZE * 2, UVM_KMF_WIRED);
1.89 thorpej 243: }
244:
245: /*
1.243 ad 246: * sigactsinit:
1.301 rmind 247: *
248: * Create an initial sigacts structure, using the same signal state
249: * as of specified process. If 'share' is set, share the sigacts by
250: * holding a reference, otherwise just copy it from parent.
1.89 thorpej 251: */
1.243 ad 252: struct sigacts *
253: sigactsinit(struct proc *pp, int share)
1.89 thorpej 254: {
1.302 rmind 255: struct sigacts *ps = pp->p_sigacts, *ps2;
1.89 thorpej 256:
1.302 rmind 257: if (__predict_false(share)) {
1.279 ad 258: atomic_inc_uint(&ps->sa_refcnt);
1.302 rmind 259: return ps;
1.109 jdolecek 260: }
1.302 rmind 261: ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
262: mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
263: ps2->sa_refcnt = 1;
1.243 ad 264:
1.302 rmind 265: mutex_enter(&ps->sa_mutex);
266: memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
267: mutex_exit(&ps->sa_mutex);
1.259 ad 268: return ps2;
1.89 thorpej 269: }
270:
271: /*
1.243 ad 272: * sigactsunshare:
1.301 rmind 273: *
274: * Make this process not share its sigacts, maintaining all signal state.
1.89 thorpej 275: */
276: void
1.112 lukem 277: sigactsunshare(struct proc *p)
1.89 thorpej 278: {
1.302 rmind 279: struct sigacts *ps, *oldps = p->p_sigacts;
1.243 ad 280:
1.302 rmind 281: if (__predict_true(oldps->sa_refcnt == 1))
1.89 thorpej 282: return;
1.302 rmind 283:
1.279 ad 284: ps = pool_cache_get(sigacts_cache, PR_WAITOK);
1.262 ad 285: mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
1.316 reinoud 286: memcpy(ps->sa_sigdesc, oldps->sa_sigdesc, sizeof(ps->sa_sigdesc));
1.302 rmind 287: ps->sa_refcnt = 1;
288:
1.243 ad 289: p->p_sigacts = ps;
290: sigactsfree(oldps);
1.89 thorpej 291: }
292:
293: /*
1.243 ad 294: * sigactsfree;
295: *
1.301 rmind 296: * Release a sigacts structure.
1.89 thorpej 297: */
298: void
1.195 pk 299: sigactsfree(struct sigacts *ps)
1.89 thorpej 300: {
301:
1.279 ad 302: if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
1.243 ad 303: mutex_destroy(&ps->sa_mutex);
1.279 ad 304: pool_cache_put(sigacts_cache, ps);
1.29 cgd 305: }
306: }
307:
308: /*
1.243 ad 309: * siginit:
310: *
311: * Initialize signal state for process 0; set to ignore signals that
312: * are ignored by default and disable the signal stack. Locking not
313: * required as the system is still cold.
1.29 cgd 314: */
315: void
1.112 lukem 316: siginit(struct proc *p)
1.29 cgd 317: {
1.243 ad 318: struct lwp *l;
319: struct sigacts *ps;
320: int signo, prop;
1.79 mycroft 321:
1.112 lukem 322: ps = p->p_sigacts;
1.79 mycroft 323: sigemptyset(&contsigmask);
324: sigemptyset(&stopsigmask);
325: sigemptyset(&sigcantmask);
1.243 ad 326: for (signo = 1; signo < NSIG; signo++) {
327: prop = sigprop[signo];
1.79 mycroft 328: if (prop & SA_CONT)
1.243 ad 329: sigaddset(&contsigmask, signo);
1.79 mycroft 330: if (prop & SA_STOP)
1.243 ad 331: sigaddset(&stopsigmask, signo);
1.79 mycroft 332: if (prop & SA_CANTMASK)
1.243 ad 333: sigaddset(&sigcantmask, signo);
334: if (prop & SA_IGNORE && signo != SIGCONT)
335: sigaddset(&p->p_sigctx.ps_sigignore, signo);
336: sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
337: SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
1.79 mycroft 338: }
1.109 jdolecek 339: sigemptyset(&p->p_sigctx.ps_sigcatch);
1.243 ad 340: p->p_sflag &= ~PS_NOCLDSTOP;
341:
342: ksiginfo_queue_init(&p->p_sigpend.sp_info);
343: sigemptyset(&p->p_sigpend.sp_set);
1.29 cgd 344:
1.79 mycroft 345: /*
1.243 ad 346: * Reset per LWP state.
1.79 mycroft 347: */
1.243 ad 348: l = LIST_FIRST(&p->p_lwps);
349: l->l_sigwaited = NULL;
1.336 christos 350: l->l_sigstk = SS_INIT;
1.243 ad 351: ksiginfo_queue_init(&l->l_sigpend.sp_info);
352: sigemptyset(&l->l_sigpend.sp_set);
1.89 thorpej 353:
354: /* One reference. */
1.109 jdolecek 355: ps->sa_refcnt = 1;
1.29 cgd 356: }
357:
358: /*
1.243 ad 359: * execsigs:
360: *
361: * Reset signals for an exec of the specified process.
1.29 cgd 362: */
363: void
1.112 lukem 364: execsigs(struct proc *p)
1.29 cgd 365: {
1.243 ad 366: struct sigacts *ps;
367: struct lwp *l;
368: int signo, prop;
369: sigset_t tset;
370: ksiginfoq_t kq;
371:
372: KASSERT(p->p_nlwps == 1);
373:
1.115 thorpej 374: sigactsunshare(p);
1.112 lukem 375: ps = p->p_sigacts;
1.115 thorpej 376:
1.29 cgd 377: /*
1.243 ad 378: * Reset caught signals. Held signals remain held through
379: * l->l_sigmask (unless they were caught, and are now ignored
380: * by default).
1.259 ad 381: *
382: * No need to lock yet, the process has only one LWP and
383: * at this point the sigacts are private to the process.
1.243 ad 384: */
385: sigemptyset(&tset);
386: for (signo = 1; signo < NSIG; signo++) {
387: if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
388: prop = sigprop[signo];
1.79 mycroft 389: if (prop & SA_IGNORE) {
390: if ((prop & SA_CONT) == 0)
1.112 lukem 391: sigaddset(&p->p_sigctx.ps_sigignore,
1.243 ad 392: signo);
393: sigaddset(&tset, signo);
1.79 mycroft 394: }
1.243 ad 395: SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1.29 cgd 396: }
1.243 ad 397: sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
398: SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
1.29 cgd 399: }
1.243 ad 400: ksiginfo_queue_init(&kq);
1.259 ad 401:
1.277 ad 402: mutex_enter(p->p_lock);
1.243 ad 403: sigclearall(p, &tset, &kq);
1.109 jdolecek 404: sigemptyset(&p->p_sigctx.ps_sigcatch);
1.205 christos 405:
406: /*
407: * Reset no zombies if child dies flag as Solaris does.
408: */
1.246 pavel 409: p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
1.205 christos 410: if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
411: SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
1.79 mycroft 412:
1.29 cgd 413: /*
1.243 ad 414: * Reset per-LWP state.
1.29 cgd 415: */
1.243 ad 416: l = LIST_FIRST(&p->p_lwps);
417: l->l_sigwaited = NULL;
1.336 christos 418: l->l_sigstk = SS_INIT;
1.243 ad 419: ksiginfo_queue_init(&l->l_sigpend.sp_info);
420: sigemptyset(&l->l_sigpend.sp_set);
1.277 ad 421: mutex_exit(p->p_lock);
1.243 ad 422:
423: ksiginfo_queue_drain(&kq);
1.29 cgd 424: }
425:
1.243 ad 426: /*
427: * ksiginfo_exechook:
428: *
429: * Free all pending ksiginfo entries from a process on exec.
430: * Additionally, drain any unused ksiginfo structures in the
431: * system back to the pool.
432: *
433: * XXX This should not be a hook, every process has signals.
434: */
435: static void
436: ksiginfo_exechook(struct proc *p, void *v)
1.79 mycroft 437: {
1.243 ad 438: ksiginfoq_t kq;
1.79 mycroft 439:
1.243 ad 440: ksiginfo_queue_init(&kq);
1.79 mycroft 441:
1.277 ad 442: mutex_enter(p->p_lock);
1.243 ad 443: sigclearall(p, NULL, &kq);
1.277 ad 444: mutex_exit(p->p_lock);
1.79 mycroft 445:
1.243 ad 446: ksiginfo_queue_drain(&kq);
1.79 mycroft 447: }
1.202 perry 448:
1.29 cgd 449: /*
1.243 ad 450: * ksiginfo_alloc:
451: *
452: * Allocate a new ksiginfo structure from the pool, and optionally copy
453: * an existing one. If the existing ksiginfo_t is from the pool, and
454: * has not been queued somewhere, then just return it. Additionally,
455: * if the existing ksiginfo_t does not contain any information beyond
456: * the signal number, then just return it.
1.29 cgd 457: */
1.243 ad 458: ksiginfo_t *
459: ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
1.48 thorpej 460: {
1.243 ad 461: ksiginfo_t *kp;
1.29 cgd 462:
1.243 ad 463: if (ok != NULL) {
464: if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
465: KSI_FROMPOOL)
1.301 rmind 466: return ok;
1.243 ad 467: if (KSI_EMPTY_P(ok))
468: return ok;
1.79 mycroft 469: }
1.243 ad 470:
1.286 ad 471: kp = pool_cache_get(ksiginfo_cache, flags);
1.243 ad 472: if (kp == NULL) {
473: #ifdef DIAGNOSTIC
474: printf("Out of memory allocating ksiginfo for pid %d\n",
475: p->p_pid);
476: #endif
477: return NULL;
1.79 mycroft 478: }
1.243 ad 479:
480: if (ok != NULL) {
481: memcpy(kp, ok, sizeof(*kp));
482: kp->ksi_flags &= ~KSI_QUEUED;
483: } else
484: KSI_INIT_EMPTY(kp);
485:
486: kp->ksi_flags |= KSI_FROMPOOL;
487:
488: return kp;
1.79 mycroft 489: }
490:
1.243 ad 491: /*
492: * ksiginfo_free:
493: *
494: * If the given ksiginfo_t is from the pool and has not been queued,
495: * then free it.
496: */
1.79 mycroft 497: void
1.243 ad 498: ksiginfo_free(ksiginfo_t *kp)
1.79 mycroft 499: {
1.29 cgd 500:
1.243 ad 501: if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
502: return;
1.286 ad 503: pool_cache_put(ksiginfo_cache, kp);
1.29 cgd 504: }
505:
1.243 ad 506: /*
507: * ksiginfo_queue_drain:
508: *
509: * Drain a non-empty ksiginfo_t queue.
510: */
511: void
512: ksiginfo_queue_drain0(ksiginfoq_t *kq)
1.29 cgd 513: {
1.243 ad 514: ksiginfo_t *ksi;
1.79 mycroft 515:
1.319 christos 516: KASSERT(!TAILQ_EMPTY(kq));
1.243 ad 517:
1.319 christos 518: while (!TAILQ_EMPTY(kq)) {
519: ksi = TAILQ_FIRST(kq);
520: TAILQ_REMOVE(kq, ksi, ksi_list);
1.286 ad 521: pool_cache_put(ksiginfo_cache, ksi);
1.243 ad 522: }
1.79 mycroft 523: }
524:
1.328 christos 525: static int
1.314 christos 526: siggetinfo(sigpend_t *sp, ksiginfo_t *out, int signo)
527: {
1.329 hannken 528: ksiginfo_t *ksi, *nksi;
1.314 christos 529:
530: if (sp == NULL)
531: goto out;
532:
533: /* Find siginfo and copy it out. */
1.328 christos 534: int count = 0;
1.329 hannken 535: TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, nksi) {
1.314 christos 536: if (ksi->ksi_signo != signo)
537: continue;
1.328 christos 538: if (count++ > 0) /* Only remove the first, count all of them */
539: continue;
1.319 christos 540: TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
1.314 christos 541: KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
542: KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
543: ksi->ksi_flags &= ~KSI_QUEUED;
544: if (out != NULL) {
545: memcpy(out, ksi, sizeof(*out));
546: out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
547: }
1.328 christos 548: ksiginfo_free(ksi);
1.314 christos 549: }
1.328 christos 550: if (count)
551: return count;
1.314 christos 552:
553: out:
554: /* If there is no siginfo, then manufacture it. */
555: if (out != NULL) {
556: KSI_INIT(out);
557: out->ksi_info._signo = signo;
558: out->ksi_info._code = SI_NOINFO;
559: }
1.328 christos 560: return 0;
1.314 christos 561: }
562:
1.243 ad 563: /*
564: * sigget:
565: *
566: * Fetch the first pending signal from a set. Optionally, also fetch
567: * or manufacture a ksiginfo element. Returns the number of the first
568: * pending signal, or zero.
569: */
570: int
1.270 yamt 571: sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
1.243 ad 572: {
573: sigset_t tset;
1.328 christos 574: int count;
1.243 ad 575:
576: /* If there's no pending set, the signal is from the debugger. */
1.287 christos 577: if (sp == NULL)
578: goto out;
1.243 ad 579:
580: /* Construct mask from signo, and 'mask'. */
581: if (signo == 0) {
582: if (mask != NULL) {
583: tset = *mask;
584: __sigandset(&sp->sp_set, &tset);
585: } else
586: tset = sp->sp_set;
1.301 rmind 587:
588: /* If there are no signals pending - return. */
1.243 ad 589: if ((signo = firstsig(&tset)) == 0)
1.287 christos 590: goto out;
1.243 ad 591: } else {
592: KASSERT(sigismember(&sp->sp_set, signo));
593: }
594:
595: sigdelset(&sp->sp_set, signo);
1.287 christos 596: out:
1.328 christos 597: count = siggetinfo(sp, out, signo);
598: if (count > 1)
599: sigaddset(&sp->sp_set, signo);
1.243 ad 600: return signo;
1.29 cgd 601: }
602:
603: /*
1.243 ad 604: * sigput:
1.301 rmind 605: *
1.296 drochner 606: * Append a new ksiginfo element to the list of pending ksiginfo's.
1.29 cgd 607: */
1.328 christos 608: static int
1.243 ad 609: sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
1.48 thorpej 610: {
1.243 ad 611: ksiginfo_t *kp;
612:
1.277 ad 613: KASSERT(mutex_owned(p->p_lock));
1.243 ad 614: KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
615:
616: sigaddset(&sp->sp_set, ksi->ksi_signo);
617:
618: /*
1.296 drochner 619: * If there is no siginfo, we are done.
1.243 ad 620: */
1.296 drochner 621: if (KSI_EMPTY_P(ksi))
1.328 christos 622: return 0;
1.243 ad 623:
624: KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
1.79 mycroft 625:
1.328 christos 626: size_t count = 0;
627: TAILQ_FOREACH(kp, &sp->sp_info, ksi_list) {
628: count++;
629: if (ksi->ksi_signo >= SIGRTMIN && ksi->ksi_signo <= SIGRTMAX)
630: continue;
631: if (kp->ksi_signo == ksi->ksi_signo) {
632: KSI_COPY(ksi, kp);
633: kp->ksi_flags |= KSI_QUEUED;
634: return 0;
635: }
636: }
637:
638: if (count >= SIGQUEUE_MAX) {
639: #ifdef DIAGNOSTIC
640: printf("%s(%d): Signal queue is full signal=%d\n",
641: p->p_comm, p->p_pid, ksi->ksi_signo);
1.243 ad 642: #endif
1.328 christos 643: return EAGAIN;
1.79 mycroft 644: }
1.243 ad 645: ksi->ksi_flags |= KSI_QUEUED;
1.319 christos 646: TAILQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
1.328 christos 647:
648: return 0;
1.79 mycroft 649: }
650:
1.243 ad 651: /*
652: * sigclear:
653: *
654: * Clear all pending signals in the specified set.
655: */
656: void
1.270 yamt 657: sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
1.79 mycroft 658: {
1.243 ad 659: ksiginfo_t *ksi, *next;
1.112 lukem 660:
1.243 ad 661: if (mask == NULL)
662: sigemptyset(&sp->sp_set);
663: else
664: sigminusset(mask, &sp->sp_set);
1.79 mycroft 665:
1.319 christos 666: TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
1.243 ad 667: if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
1.319 christos 668: TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
1.243 ad 669: KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
670: KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
1.319 christos 671: TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
1.79 mycroft 672: }
673: }
1.243 ad 674: }
675:
676: /*
677: * sigclearall:
678: *
679: * Clear all pending signals in the specified set from a process and
680: * its LWPs.
681: */
682: void
1.270 yamt 683: sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
1.243 ad 684: {
685: struct lwp *l;
686:
1.277 ad 687: KASSERT(mutex_owned(p->p_lock));
1.79 mycroft 688:
1.243 ad 689: sigclear(&p->p_sigpend, mask, kq);
690:
691: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
692: sigclear(&l->l_sigpend, mask, kq);
693: }
1.29 cgd 694: }
695:
1.243 ad 696: /*
697: * sigispending:
698: *
1.311 christos 699: * Return the first signal number if there are pending signals for the
700: * current LWP. May be called unlocked provided that LW_PENDSIG is set,
701: * and that the signal has been posted to the appopriate queue before
702: * LW_PENDSIG is set.
1.243 ad 703: */
1.52 christos 704: int
1.243 ad 705: sigispending(struct lwp *l, int signo)
1.48 thorpej 706: {
1.243 ad 707: struct proc *p = l->l_proc;
708: sigset_t tset;
709:
1.260 ad 710: membar_consumer();
1.243 ad 711:
712: tset = l->l_sigpend.sp_set;
713: sigplusset(&p->p_sigpend.sp_set, &tset);
714: sigminusset(&p->p_sigctx.ps_sigignore, &tset);
715: sigminusset(&l->l_sigmask, &tset);
716:
717: if (signo == 0) {
1.312 rmind 718: return firstsig(&tset);
719: }
720: return sigismember(&tset, signo) ? signo : 0;
1.243 ad 721: }
722:
723: void
724: getucontext(struct lwp *l, ucontext_t *ucp)
725: {
726: struct proc *p = l->l_proc;
727:
1.277 ad 728: KASSERT(mutex_owned(p->p_lock));
1.243 ad 729:
730: ucp->uc_flags = 0;
731: ucp->uc_link = l->l_ctxlink;
1.317 rmind 732: ucp->uc_sigmask = l->l_sigmask;
1.243 ad 733: ucp->uc_flags |= _UC_SIGMASK;
734:
735: /*
736: * The (unsupplied) definition of the `current execution stack'
737: * in the System V Interface Definition appears to allow returning
738: * the main context stack.
739: */
740: if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
1.263 christos 741: ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
1.243 ad 742: ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
743: ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
744: } else {
745: /* Simply copy alternate signal execution stack. */
746: ucp->uc_stack = l->l_sigstk;
1.79 mycroft 747: }
1.243 ad 748: ucp->uc_flags |= _UC_STACK;
1.277 ad 749: mutex_exit(p->p_lock);
1.243 ad 750: cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1.277 ad 751: mutex_enter(p->p_lock);
1.29 cgd 752: }
753:
754: int
1.243 ad 755: setucontext(struct lwp *l, const ucontext_t *ucp)
1.48 thorpej 756: {
1.243 ad 757: struct proc *p = l->l_proc;
1.223 yamt 758: int error;
1.29 cgd 759:
1.277 ad 760: KASSERT(mutex_owned(p->p_lock));
1.243 ad 761:
762: if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
763: error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
764: if (error != 0)
1.223 yamt 765: return error;
1.29 cgd 766: }
1.243 ad 767:
1.277 ad 768: mutex_exit(p->p_lock);
1.243 ad 769: error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
1.277 ad 770: mutex_enter(p->p_lock);
1.243 ad 771: if (error != 0)
772: return (error);
773:
774: l->l_ctxlink = ucp->uc_link;
775:
776: /*
777: * If there was stack information, update whether or not we are
778: * still running on an alternate signal stack.
779: */
780: if ((ucp->uc_flags & _UC_STACK) != 0) {
781: if (ucp->uc_stack.ss_flags & SS_ONSTACK)
782: l->l_sigstk.ss_flags |= SS_ONSTACK;
783: else
784: l->l_sigstk.ss_flags &= ~SS_ONSTACK;
785: }
786:
787: return 0;
1.29 cgd 788: }
789:
790: /*
1.301 rmind 791: * killpg1: common code for kill process group/broadcast kill.
1.29 cgd 792: */
1.52 christos 793: int
1.224 ad 794: killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
1.29 cgd 795: {
1.224 ad 796: struct proc *p, *cp;
1.220 elad 797: kauth_cred_t pc;
1.112 lukem 798: struct pgrp *pgrp;
799: int nfound;
1.243 ad 800: int signo = ksi->ksi_signo;
1.202 perry 801:
1.224 ad 802: cp = l->l_proc;
803: pc = l->l_cred;
1.112 lukem 804: nfound = 0;
1.243 ad 805:
1.276 ad 806: mutex_enter(proc_lock);
1.91 thorpej 807: if (all) {
1.202 perry 808: /*
1.301 rmind 809: * Broadcast.
1.29 cgd 810: */
1.199 yamt 811: PROCLIST_FOREACH(p, &allproc) {
1.283 ad 812: if (p->p_pid <= 1 || p == cp ||
1.304 yamt 813: (p->p_flag & PK_SYSTEM) != 0)
1.29 cgd 814: continue;
1.277 ad 815: mutex_enter(p->p_lock);
1.243 ad 816: if (kauth_authorize_process(pc,
1.264 elad 817: KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
818: NULL) == 0) {
1.243 ad 819: nfound++;
1.277 ad 820: if (signo)
1.243 ad 821: kpsignal2(p, ksi);
822: }
1.277 ad 823: mutex_exit(p->p_lock);
1.29 cgd 824: }
1.91 thorpej 825: } else {
1.202 perry 826: if (pgid == 0)
1.301 rmind 827: /* Zero pgid means send to my process group. */
1.29 cgd 828: pgrp = cp->p_pgrp;
829: else {
1.306 rmind 830: pgrp = pgrp_find(pgid);
1.29 cgd 831: if (pgrp == NULL)
1.243 ad 832: goto out;
1.29 cgd 833: }
1.124 matt 834: LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1.246 pavel 835: if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
1.29 cgd 836: continue;
1.277 ad 837: mutex_enter(p->p_lock);
1.264 elad 838: if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
839: p, KAUTH_ARG(signo), NULL, NULL) == 0) {
1.243 ad 840: nfound++;
1.277 ad 841: if (signo && P_ZOMBIE(p) == 0)
842: kpsignal2(p, ksi);
1.243 ad 843: }
1.277 ad 844: mutex_exit(p->p_lock);
1.29 cgd 845: }
846: }
1.301 rmind 847: out:
1.276 ad 848: mutex_exit(proc_lock);
1.301 rmind 849: return nfound ? 0 : ESRCH;
1.29 cgd 850: }
851:
852: /*
1.301 rmind 853: * Send a signal to a process group. If checktty is set, limit to members
1.243 ad 854: * which have a controlling terminal.
1.29 cgd 855: */
856: void
1.243 ad 857: pgsignal(struct pgrp *pgrp, int sig, int checkctty)
1.29 cgd 858: {
1.148 christos 859: ksiginfo_t ksi;
860:
1.276 ad 861: KASSERT(!cpu_intr_p());
862: KASSERT(mutex_owned(proc_lock));
1.29 cgd 863:
1.192 matt 864: KSI_INIT_EMPTY(&ksi);
1.148 christos 865: ksi.ksi_signo = sig;
866: kpgsignal(pgrp, &ksi, NULL, checkctty);
867: }
868:
869: void
870: kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
1.29 cgd 871: {
1.98 augustss 872: struct proc *p;
1.29 cgd 873:
1.276 ad 874: KASSERT(!cpu_intr_p());
875: KASSERT(mutex_owned(proc_lock));
1.301 rmind 876: KASSERT(pgrp != NULL);
1.243 ad 877:
1.297 rmind 878: LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
879: if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
880: kpsignal(p, ksi, data);
1.29 cgd 881: }
882:
883: /*
1.243 ad 884: * Send a signal caused by a trap to the current LWP. If it will be caught
885: * immediately, deliver it with correct code. Otherwise, post it normally.
1.29 cgd 886: */
1.148 christos 887: void
1.243 ad 888: trapsignal(struct lwp *l, ksiginfo_t *ksi)
1.148 christos 889: {
1.130 thorpej 890: struct proc *p;
891: struct sigacts *ps;
1.243 ad 892: int signo = ksi->ksi_signo;
1.288 wrstuden 893: sigset_t *mask;
1.29 cgd 894:
1.166 thorpej 895: KASSERT(KSI_TRAP_P(ksi));
896:
1.243 ad 897: ksi->ksi_lid = l->l_lid;
1.130 thorpej 898: p = l->l_proc;
1.243 ad 899:
1.276 ad 900: KASSERT(!cpu_intr_p());
901: mutex_enter(proc_lock);
1.277 ad 902: mutex_enter(p->p_lock);
1.317 rmind 903: mask = &l->l_sigmask;
1.112 lukem 904: ps = p->p_sigacts;
1.317 rmind 905:
1.339 christos 906: const bool traced = (p->p_slflag & PSL_TRACED) != 0;
907: const bool caught = sigismember(&p->p_sigctx.ps_sigcatch, signo);
908: const bool masked = sigismember(mask, signo);
909: if (!traced && caught && !masked) {
1.276 ad 910: mutex_exit(proc_lock);
1.275 ad 911: l->l_ru.ru_nsignals++;
1.288 wrstuden 912: kpsendsig(l, ksi, mask);
1.277 ad 913: mutex_exit(p->p_lock);
1.325 christos 914: if (ktrpoint(KTR_PSIG)) {
1.330 martin 915: if (p->p_emul->e_ktrpsig)
916: p->p_emul->e_ktrpsig(signo,
917: SIGACTION_PS(ps, signo).sa_handler,
918: mask, ksi);
919: else
920: ktrpsig(signo,
921: SIGACTION_PS(ps, signo).sa_handler,
922: mask, ksi);
1.325 christos 923: }
1.339 christos 924: return;
925: }
926:
927: /*
928: * If the signal is masked or ignored, then unmask it and
929: * reset it to the default action so that the process or
930: * its tracer will be notified.
931: */
932: const bool ignored = SIGACTION_PS(ps, signo).sa_handler == SIG_IGN;
933: if (masked || ignored) {
934: mutex_enter(&ps->sa_mutex);
935: sigdelset(mask, signo);
936: sigdelset(&p->p_sigctx.ps_sigcatch, signo);
937: sigdelset(&p->p_sigctx.ps_sigignore, signo);
938: sigdelset(&SIGACTION_PS(ps, signo).sa_mask, signo);
939: SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
940: mutex_exit(&ps->sa_mutex);
1.29 cgd 941: }
1.339 christos 942:
943: kpsignal2(p, ksi);
944: mutex_exit(p->p_lock);
945: mutex_exit(proc_lock);
1.29 cgd 946: }
947:
948: /*
1.151 christos 949: * Fill in signal information and signal the parent for a child status change.
950: */
1.216 christos 951: void
1.243 ad 952: child_psignal(struct proc *p, int mask)
1.151 christos 953: {
954: ksiginfo_t ksi;
1.243 ad 955: struct proc *q;
1.322 christos 956: int xsig;
1.243 ad 957:
1.276 ad 958: KASSERT(mutex_owned(proc_lock));
1.277 ad 959: KASSERT(mutex_owned(p->p_lock));
1.243 ad 960:
1.322 christos 961: xsig = p->p_xsig;
1.151 christos 962:
1.191 matt 963: KSI_INIT(&ksi);
1.151 christos 964: ksi.ksi_signo = SIGCHLD;
1.322 christos 965: ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
1.151 christos 966: ksi.ksi_pid = p->p_pid;
1.220 elad 967: ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
1.322 christos 968: ksi.ksi_status = xsig;
1.151 christos 969: ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
970: ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1.243 ad 971:
972: q = p->p_pptr;
973:
1.277 ad 974: mutex_exit(p->p_lock);
975: mutex_enter(q->p_lock);
1.243 ad 976:
977: if ((q->p_sflag & mask) == 0)
978: kpsignal2(q, &ksi);
979:
1.277 ad 980: mutex_exit(q->p_lock);
981: mutex_enter(p->p_lock);
1.151 christos 982: }
983:
1.29 cgd 984: void
1.243 ad 985: psignal(struct proc *p, int signo)
1.148 christos 986: {
1.165 thorpej 987: ksiginfo_t ksi;
988:
1.276 ad 989: KASSERT(!cpu_intr_p());
990: KASSERT(mutex_owned(proc_lock));
1.243 ad 991:
1.192 matt 992: KSI_INIT_EMPTY(&ksi);
1.243 ad 993: ksi.ksi_signo = signo;
1.277 ad 994: mutex_enter(p->p_lock);
1.234 yamt 995: kpsignal2(p, &ksi);
1.277 ad 996: mutex_exit(p->p_lock);
1.148 christos 997: }
998:
999: void
1.234 yamt 1000: kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
1.160 christos 1001: {
1.274 ad 1002: fdfile_t *ff;
1003: file_t *fp;
1.298 ad 1004: fdtab_t *dt;
1.165 thorpej 1005:
1.276 ad 1006: KASSERT(!cpu_intr_p());
1007: KASSERT(mutex_owned(proc_lock));
1.243 ad 1008:
1009: if ((p->p_sflag & PS_WEXIT) == 0 && data) {
1.160 christos 1010: size_t fd;
1.274 ad 1011: filedesc_t *fdp = p->p_fd;
1.165 thorpej 1012:
1.274 ad 1013: /* XXXSMP locking */
1.160 christos 1014: ksi->ksi_fd = -1;
1.298 ad 1015: dt = fdp->fd_dt;
1016: for (fd = 0; fd < dt->dt_nfiles; fd++) {
1017: if ((ff = dt->dt_ff[fd]) == NULL)
1.274 ad 1018: continue;
1019: if ((fp = ff->ff_file) == NULL)
1020: continue;
1021: if (fp->f_data == data) {
1.160 christos 1022: ksi->ksi_fd = fd;
1023: break;
1024: }
1025: }
1026: }
1.277 ad 1027: mutex_enter(p->p_lock);
1.234 yamt 1028: kpsignal2(p, ksi);
1.277 ad 1029: mutex_exit(p->p_lock);
1.160 christos 1030: }
1031:
1.243 ad 1032: /*
1033: * sigismasked:
1034: *
1.301 rmind 1035: * Returns true if signal is ignored or masked for the specified LWP.
1.243 ad 1036: */
1037: int
1038: sigismasked(struct lwp *l, int sig)
1.29 cgd 1039: {
1.243 ad 1040: struct proc *p = l->l_proc;
1041:
1.317 rmind 1042: return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1043: sigismember(&l->l_sigmask, sig);
1.243 ad 1044: }
1.29 cgd 1045:
1.243 ad 1046: /*
1047: * sigpost:
1048: *
1.301 rmind 1049: * Post a pending signal to an LWP. Returns non-zero if the LWP may
1050: * be able to take the signal.
1.243 ad 1051: */
1.297 rmind 1052: static int
1.317 rmind 1053: sigpost(struct lwp *l, sig_t action, int prop, int sig)
1.243 ad 1054: {
1055: int rv, masked;
1.288 wrstuden 1056: struct proc *p = l->l_proc;
1.148 christos 1057:
1.288 wrstuden 1058: KASSERT(mutex_owned(p->p_lock));
1.148 christos 1059:
1.183 fvdl 1060: /*
1.243 ad 1061: * If the LWP is on the way out, sigclear() will be busy draining all
1062: * pending signals. Don't give it more.
1.126 jdolecek 1063: */
1.243 ad 1064: if (l->l_refcnt == 0)
1065: return 0;
1066:
1.320 christos 1067: SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
1.303 darran 1068:
1.289 wrstuden 1069: /*
1.243 ad 1070: * Have the LWP check for signals. This ensures that even if no LWP
1071: * is found to take the signal immediately, it should be taken soon.
1072: */
1.293 ad 1073: lwp_lock(l);
1.246 pavel 1074: l->l_flag |= LW_PENDSIG;
1.29 cgd 1075:
1076: /*
1.297 rmind 1077: * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1078: * Note: SIGKILL and SIGSTOP cannot be masked.
1.29 cgd 1079: */
1.317 rmind 1080: masked = sigismember(&l->l_sigmask, sig);
1.243 ad 1081: if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
1082: lwp_unlock(l);
1083: return 0;
1084: }
1.198 jdolecek 1085:
1.243 ad 1086: /*
1.247 ad 1087: * If killing the process, make it run fast.
1088: */
1089: if (__predict_false((prop & SA_KILL) != 0) &&
1.266 rmind 1090: action == SIG_DFL && l->l_priority < MAXPRI_USER) {
1091: KASSERT(l->l_class == SCHED_OTHER);
1092: lwp_changepri(l, MAXPRI_USER);
1093: }
1.247 ad 1094:
1095: /*
1.243 ad 1096: * If the LWP is running or on a run queue, then we win. If it's
1097: * sleeping interruptably, wake it and make it take the signal. If
1098: * the sleep isn't interruptable, then the chances are it will get
1099: * to see the signal soon anyhow. If suspended, it can't take the
1100: * signal right now. If it's LWP private or for all LWPs, save it
1101: * for later; otherwise punt.
1102: */
1103: rv = 0;
1104:
1105: switch (l->l_stat) {
1106: case LSRUN:
1107: case LSONPROC:
1108: lwp_need_userret(l);
1109: rv = 1;
1110: break;
1111:
1112: case LSSLEEP:
1.246 pavel 1113: if ((l->l_flag & LW_SINTR) != 0) {
1.243 ad 1114: /* setrunnable() will release the lock. */
1115: setrunnable(l);
1116: return 1;
1.232 mrg 1117: }
1.243 ad 1118: break;
1119:
1120: case LSSUSPENDED:
1.309 yamt 1121: if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
1.243 ad 1122: /* lwp_continue() will release the lock. */
1123: lwp_continue(l);
1124: return 1;
1.190 matt 1125: }
1.243 ad 1126: break;
1127:
1128: case LSSTOP:
1129: if ((prop & SA_STOP) != 0)
1130: break;
1.198 jdolecek 1131:
1132: /*
1.243 ad 1133: * If the LWP is stopped and we are sending a continue
1134: * signal, then start it again.
1.198 jdolecek 1135: */
1.243 ad 1136: if ((prop & SA_CONT) != 0) {
1137: if (l->l_wchan != NULL) {
1138: l->l_stat = LSSLEEP;
1.288 wrstuden 1139: p->p_nrlwps++;
1.243 ad 1140: rv = 1;
1141: break;
1142: }
1143: /* setrunnable() will release the lock. */
1144: setrunnable(l);
1145: return 1;
1.246 pavel 1146: } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1.243 ad 1147: /* setrunnable() will release the lock. */
1148: setrunnable(l);
1149: return 1;
1150: }
1151: break;
1.198 jdolecek 1152:
1.243 ad 1153: default:
1154: break;
1155: }
1.44 mycroft 1156:
1.243 ad 1157: lwp_unlock(l);
1158: return rv;
1159: }
1.29 cgd 1160:
1.243 ad 1161: /*
1162: * Notify an LWP that it has a pending signal.
1163: */
1164: void
1165: signotify(struct lwp *l)
1166: {
1.250 ad 1167: KASSERT(lwp_locked(l, NULL));
1.29 cgd 1168:
1.246 pavel 1169: l->l_flag |= LW_PENDSIG;
1.243 ad 1170: lwp_need_userret(l);
1171: }
1.44 mycroft 1172:
1.243 ad 1173: /*
1174: * Find an LWP within process p that is waiting on signal ksi, and hand
1175: * it on.
1176: */
1.297 rmind 1177: static int
1.243 ad 1178: sigunwait(struct proc *p, const ksiginfo_t *ksi)
1179: {
1180: struct lwp *l;
1181: int signo;
1.135 jdolecek 1182:
1.277 ad 1183: KASSERT(mutex_owned(p->p_lock));
1.171 jdolecek 1184:
1.243 ad 1185: signo = ksi->ksi_signo;
1186:
1187: if (ksi->ksi_lid != 0) {
1188: /*
1189: * Signal came via _lwp_kill(). Find the LWP and see if
1190: * it's interested.
1191: */
1192: if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
1193: return 0;
1194: if (l->l_sigwaited == NULL ||
1195: !sigismember(&l->l_sigwaitset, signo))
1196: return 0;
1197: } else {
1198: /*
1199: * Look for any LWP that may be interested.
1200: */
1201: LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
1202: KASSERT(l->l_sigwaited != NULL);
1203: if (sigismember(&l->l_sigwaitset, signo))
1204: break;
1205: }
1206: }
1207:
1208: if (l != NULL) {
1209: l->l_sigwaited->ksi_info = ksi->ksi_info;
1210: l->l_sigwaited = NULL;
1211: LIST_REMOVE(l, l_sigwaiter);
1212: cv_signal(&l->l_sigcv);
1213: return 1;
1214: }
1215:
1216: return 0;
1217: }
1218:
1219: /*
1220: * Send the signal to the process. If the signal has an action, the action
1221: * is usually performed by the target process rather than the caller; we add
1222: * the signal to the set of pending signals for the process.
1223: *
1224: * Exceptions:
1225: * o When a stop signal is sent to a sleeping process that takes the
1226: * default action, the process is stopped without awakening it.
1227: * o SIGCONT restarts stopped processes (or puts them back to sleep)
1228: * regardless of the signal action (eg, blocked or ignored).
1229: *
1230: * Other ignored signals are discarded immediately.
1231: */
1.328 christos 1232: int
1.243 ad 1233: kpsignal2(struct proc *p, ksiginfo_t *ksi)
1234: {
1.301 rmind 1235: int prop, signo = ksi->ksi_signo;
1.320 christos 1236: struct lwp *l = NULL;
1.243 ad 1237: ksiginfo_t *kp;
1.301 rmind 1238: lwpid_t lid;
1.243 ad 1239: sig_t action;
1.335 martin 1240: bool toall, debtrap = false;
1.328 christos 1241: int error = 0;
1.243 ad 1242:
1.276 ad 1243: KASSERT(!cpu_intr_p());
1244: KASSERT(mutex_owned(proc_lock));
1.277 ad 1245: KASSERT(mutex_owned(p->p_lock));
1.243 ad 1246: KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
1247: KASSERT(signo > 0 && signo < NSIG);
1.171 jdolecek 1248:
1.135 jdolecek 1249: /*
1.243 ad 1250: * If the process is being created by fork, is a zombie or is
1251: * exiting, then just drop the signal here and bail out.
1.29 cgd 1252: */
1.335 martin 1253: if (p->p_stat == SIDL && signo == SIGTRAP
1254: && (p->p_slflag & PSL_TRACED)) {
1255: /* allow an initial SIGTRAP for traced processes */
1256: debtrap = true;
1257: } else if (p->p_stat != SACTIVE && p->p_stat != SSTOP) {
1.328 christos 1258: return 0;
1.335 martin 1259: }
1.231 mrg 1260:
1.331 christos 1261: /* XXX for core dump/debugger */
1262: p->p_sigctx.ps_lwp = ksi->ksi_lid;
1.332 kamil 1263: p->p_sigctx.ps_info = ksi->ksi_info;
1.331 christos 1264:
1.231 mrg 1265: /*
1.243 ad 1266: * Notify any interested parties of the signal.
1.301 rmind 1267: */
1.243 ad 1268: KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
1269:
1270: /*
1271: * Some signals including SIGKILL must act on the entire process.
1.231 mrg 1272: */
1.243 ad 1273: kp = NULL;
1274: prop = sigprop[signo];
1275: toall = ((prop & SA_TOALL) != 0);
1.301 rmind 1276: lid = toall ? 0 : ksi->ksi_lid;
1.231 mrg 1277:
1.243 ad 1278: /*
1279: * If proc is traced, always give parent a chance.
1280: */
1281: if (p->p_slflag & PSL_TRACED) {
1282: action = SIG_DFL;
1.104 thorpej 1283:
1.243 ad 1284: if (lid == 0) {
1285: /*
1286: * If the process is being traced and the signal
1287: * is being caught, make sure to save any ksiginfo.
1288: */
1289: if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1.320 christos 1290: goto discard;
1.328 christos 1291: if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
1292: goto out;
1.243 ad 1293: }
1294: } else {
1295:
1.29 cgd 1296: /*
1.243 ad 1297: * If the signal is being ignored, then drop it. Note: we
1298: * don't set SIGCONT in ps_sigignore, and if it is set to
1299: * SIG_IGN, action will be SIG_DFL here.
1.29 cgd 1300: */
1.243 ad 1301: if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1.320 christos 1302: goto discard;
1.243 ad 1303:
1304: else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
1305: action = SIG_CATCH;
1306: else {
1307: action = SIG_DFL;
1308:
1309: /*
1310: * If sending a tty stop signal to a member of an
1311: * orphaned process group, discard the signal here if
1312: * the action is default; don't stop the process below
1313: * if sleeping, and don't clear any pending SIGCONT.
1314: */
1.276 ad 1315: if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1.320 christos 1316: goto discard;
1.243 ad 1317:
1318: if (prop & SA_KILL && p->p_nice > NZERO)
1319: p->p_nice = NZERO;
1.29 cgd 1320: }
1.175 cl 1321: }
1322:
1.243 ad 1323: /*
1324: * If stopping or continuing a process, discard any pending
1325: * signals that would do the inverse.
1326: */
1327: if ((prop & (SA_CONT | SA_STOP)) != 0) {
1.301 rmind 1328: ksiginfoq_t kq;
1329:
1.243 ad 1330: ksiginfo_queue_init(&kq);
1331: if ((prop & SA_CONT) != 0)
1332: sigclear(&p->p_sigpend, &stopsigmask, &kq);
1333: if ((prop & SA_STOP) != 0)
1334: sigclear(&p->p_sigpend, &contsigmask, &kq);
1335: ksiginfo_queue_drain(&kq); /* XXXSMP */
1336: }
1337:
1.317 rmind 1338: /*
1.243 ad 1339: * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1340: * please!), check if any LWPs are waiting on it. If yes, pass on
1341: * the signal info. The signal won't be processed further here.
1342: */
1343: if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
1344: p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
1345: sigunwait(p, ksi))
1.320 christos 1346: goto discard;
1.243 ad 1347:
1348: /*
1349: * XXXSMP Should be allocated by the caller, we're holding locks
1350: * here.
1351: */
1352: if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1.320 christos 1353: goto discard;
1.243 ad 1354:
1355: /*
1356: * LWP private signals are easy - just find the LWP and post
1357: * the signal to it.
1358: */
1359: if (lid != 0) {
1.335 martin 1360: if (__predict_false(debtrap)) {
1361: l = LIST_FIRST(&p->p_lwps);
1362: if (l->l_lid != lid)
1363: l = NULL;
1364: } else {
1365: l = lwp_find(p, lid);
1366: }
1.243 ad 1367: if (l != NULL) {
1.328 christos 1368: if ((error = sigput(&l->l_sigpend, p, kp)) != 0)
1369: goto out;
1.260 ad 1370: membar_producer();
1.339 christos 1371: if (sigpost(l, action, prop, kp->ksi_signo) != 0)
1372: signo = -1;
1.243 ad 1373: }
1374: goto out;
1375: }
1.130 thorpej 1376:
1.243 ad 1377: /*
1.288 wrstuden 1378: * Some signals go to all LWPs, even if posted with _lwp_kill()
1379: * or for an SA process.
1.243 ad 1380: */
1381: if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1382: if ((p->p_slflag & PSL_TRACED) != 0)
1383: goto deliver;
1.202 perry 1384:
1.176 cl 1385: /*
1386: * If SIGCONT is default (or ignored) and process is
1387: * asleep, we are finished; the process should not
1388: * be awakened.
1389: */
1.243 ad 1390: if ((prop & SA_CONT) != 0 && action == SIG_DFL)
1391: goto out;
1392: } else {
1.176 cl 1393: /*
1.300 rmind 1394: * Process is stopped or stopping.
1395: * - If traced, then no action is needed, unless killing.
1396: * - Run the process only if sending SIGCONT or SIGKILL.
1.176 cl 1397: */
1.300 rmind 1398: if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) {
1.243 ad 1399: goto out;
1.300 rmind 1400: }
1.297 rmind 1401: if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1.243 ad 1402: /*
1.321 pgoyette 1403: * Re-adjust p_nstopchild if the process was
1404: * stopped but not yet collected by its parent.
1.243 ad 1405: */
1.321 pgoyette 1406: if (p->p_stat == SSTOP && !p->p_waited)
1407: p->p_pptr->p_nstopchild--;
1.243 ad 1408: p->p_stat = SACTIVE;
1409: p->p_sflag &= ~PS_STOPPING;
1.300 rmind 1410: if (p->p_slflag & PSL_TRACED) {
1411: KASSERT(signo == SIGKILL);
1412: goto deliver;
1413: }
1.29 cgd 1414: /*
1.297 rmind 1415: * Do not make signal pending if SIGCONT is default.
1416: *
1417: * If the process catches SIGCONT, let it handle the
1418: * signal itself (if waiting on event - process runs,
1419: * otherwise continues sleeping).
1.29 cgd 1420: */
1.324 christos 1421: if ((prop & SA_CONT) != 0) {
1422: p->p_xsig = SIGCONT;
1.326 christos 1423: p->p_sflag |= PS_CONTINUED;
1.324 christos 1424: child_psignal(p, 0);
1425: if (action == SIG_DFL) {
1426: KASSERT(signo != SIGKILL);
1427: goto deliver;
1428: }
1.297 rmind 1429: }
1.243 ad 1430: } else if ((prop & SA_STOP) != 0) {
1.29 cgd 1431: /*
1.176 cl 1432: * Already stopped, don't need to stop again.
1433: * (If we did the shell could get confused.)
1.29 cgd 1434: */
1.243 ad 1435: goto out;
1.297 rmind 1436: }
1.243 ad 1437: }
1.297 rmind 1438: /*
1439: * Make signal pending.
1440: */
1.300 rmind 1441: KASSERT((p->p_slflag & PSL_TRACED) == 0);
1.328 christos 1442: if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
1443: goto out;
1.301 rmind 1444: deliver:
1.243 ad 1445: /*
1.271 yamt 1446: * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1.243 ad 1447: * visible on the per process list (for sigispending()). This
1448: * is unlikely to be needed in practice, but...
1449: */
1.260 ad 1450: membar_producer();
1.29 cgd 1451:
1452: /*
1.243 ad 1453: * Try to find an LWP that can take the signal.
1.29 cgd 1454: */
1.301 rmind 1455: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.317 rmind 1456: if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
1.301 rmind 1457: break;
1.288 wrstuden 1458: }
1.320 christos 1459: signo = -1;
1.301 rmind 1460: out:
1461: /*
1462: * If the ksiginfo wasn't used, then bin it. XXXSMP freeing memory
1463: * with locks held. The caller should take care of this.
1464: */
1465: ksiginfo_free(kp);
1.320 christos 1466: if (signo == -1)
1.328 christos 1467: return error;
1.320 christos 1468: discard:
1469: SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
1.328 christos 1470: return error;
1.29 cgd 1471: }
1472:
1.243 ad 1473: void
1474: kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1.209 chs 1475: {
1.243 ad 1476: struct proc *p = l->l_proc;
1477:
1.277 ad 1478: KASSERT(mutex_owned(p->p_lock));
1.243 ad 1479: (*p->p_emul->e_sendsig)(ksi, mask);
1.209 chs 1480: }
1481:
1.243 ad 1482: /*
1.272 yamt 1483: * Stop any LWPs sleeping interruptably.
1484: */
1485: static void
1486: proc_stop_lwps(struct proc *p)
1487: {
1488: struct lwp *l;
1489:
1.277 ad 1490: KASSERT(mutex_owned(p->p_lock));
1.272 yamt 1491: KASSERT((p->p_sflag & PS_STOPPING) != 0);
1492:
1493: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1494: lwp_lock(l);
1495: if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
1496: l->l_stat = LSSTOP;
1497: p->p_nrlwps--;
1498: }
1499: lwp_unlock(l);
1500: }
1501: }
1502:
1503: /*
1504: * Finish stopping of a process. Mark it stopped and notify the parent.
1505: *
1.277 ad 1506: * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1.272 yamt 1507: */
1508: static void
1.340 kamil 1509: proc_stop_done(struct proc *p, int ppmask)
1.272 yamt 1510: {
1511:
1.276 ad 1512: KASSERT(mutex_owned(proc_lock));
1.277 ad 1513: KASSERT(mutex_owned(p->p_lock));
1.272 yamt 1514: KASSERT((p->p_sflag & PS_STOPPING) != 0);
1515: KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
1516:
1517: p->p_sflag &= ~PS_STOPPING;
1518: p->p_stat = SSTOP;
1519: p->p_waited = 0;
1520: p->p_pptr->p_nstopchild++;
1521: if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
1.340 kamil 1522: /* child_psignal drops p_lock briefly. */
1523: child_psignal(p, ppmask);
1.272 yamt 1524: cv_broadcast(&p->p_pptr->p_waitcv);
1525: }
1526: }
1527:
1528: /*
1.243 ad 1529: * Stop the current process and switch away when being stopped or traced.
1530: */
1.342 kamil 1531: void
1532: sigswitch(int ppmask, int signo, bool relock)
1.209 chs 1533: {
1.272 yamt 1534: struct lwp *l = curlwp;
1.243 ad 1535: struct proc *p = l->l_proc;
1.245 ad 1536: int biglocks;
1.243 ad 1537:
1.277 ad 1538: KASSERT(mutex_owned(p->p_lock));
1.250 ad 1539: KASSERT(l->l_stat == LSONPROC);
1540: KASSERT(p->p_nrlwps > 0);
1.243 ad 1541:
1542: /*
1543: * On entry we know that the process needs to stop. If it's
1544: * the result of a 'sideways' stop signal that has been sourced
1545: * through issignal(), then stop other LWPs in the process too.
1546: */
1547: if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1548: KASSERT(signo != 0);
1.334 christos 1549: proc_stop(p, signo);
1.250 ad 1550: KASSERT(p->p_nrlwps > 0);
1.243 ad 1551: }
1552:
1553: /*
1554: * If we are the last live LWP, and the stop was a result of
1555: * a new signal, then signal the parent.
1556: */
1557: if ((p->p_sflag & PS_STOPPING) != 0) {
1.342 kamil 1558: if (relock && !mutex_tryenter(proc_lock)) {
1.277 ad 1559: mutex_exit(p->p_lock);
1.276 ad 1560: mutex_enter(proc_lock);
1.277 ad 1561: mutex_enter(p->p_lock);
1.243 ad 1562: }
1563:
1564: if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1.272 yamt 1565: /*
1566: * Note that proc_stop_done() can drop
1.277 ad 1567: * p->p_lock briefly.
1.272 yamt 1568: */
1.340 kamil 1569: proc_stop_done(p, ppmask);
1.243 ad 1570: }
1571:
1.276 ad 1572: mutex_exit(proc_lock);
1.243 ad 1573: }
1574:
1575: /*
1576: * Unlock and switch away.
1577: */
1.245 ad 1578: KERNEL_UNLOCK_ALL(l, &biglocks);
1.243 ad 1579: if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1580: p->p_nrlwps--;
1581: lwp_lock(l);
1582: KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
1583: l->l_stat = LSSTOP;
1584: lwp_unlock(l);
1585: }
1586:
1.277 ad 1587: mutex_exit(p->p_lock);
1.243 ad 1588: lwp_lock(l);
1.253 yamt 1589: mi_switch(l);
1.245 ad 1590: KERNEL_LOCK(biglocks, l);
1.277 ad 1591: mutex_enter(p->p_lock);
1.209 chs 1592: }
1593:
1.243 ad 1594: /*
1595: * Check for a signal from the debugger.
1596: */
1.297 rmind 1597: static int
1.293 ad 1598: sigchecktrace(void)
1.130 thorpej 1599: {
1.243 ad 1600: struct lwp *l = curlwp;
1.130 thorpej 1601: struct proc *p = l->l_proc;
1.243 ad 1602: int signo;
1603:
1.277 ad 1604: KASSERT(mutex_owned(p->p_lock));
1.130 thorpej 1605:
1.292 ad 1606: /* If there's a pending SIGKILL, process it immediately. */
1607: if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
1608: return 0;
1609:
1.243 ad 1610: /*
1611: * If we are no longer being traced, or the parent didn't
1.305 christos 1612: * give us a signal, or we're stopping, look for more signals.
1.243 ad 1613: */
1.322 christos 1614: if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
1.305 christos 1615: (p->p_sflag & PS_STOPPING) != 0)
1.243 ad 1616: return 0;
1.130 thorpej 1617:
1.243 ad 1618: /*
1619: * If the new signal is being masked, look for other signals.
1620: * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1621: */
1.322 christos 1622: signo = p->p_xsig;
1623: p->p_xsig = 0;
1.317 rmind 1624: if (sigismember(&l->l_sigmask, signo)) {
1.243 ad 1625: signo = 0;
1.317 rmind 1626: }
1.243 ad 1627: return signo;
1.79 mycroft 1628: }
1629:
1.29 cgd 1630: /*
1631: * If the current process has received a signal (should be caught or cause
1632: * termination, should interrupt current syscall), return the signal number.
1.243 ad 1633: *
1.29 cgd 1634: * Stop signals with default action are processed immediately, then cleared;
1635: * they aren't returned. This is checked after each entry to the system for
1.243 ad 1636: * a syscall or trap.
1637: *
1638: * We will also return -1 if the process is exiting and the current LWP must
1639: * follow suit.
1.29 cgd 1640: */
1641: int
1.130 thorpej 1642: issignal(struct lwp *l)
1.29 cgd 1643: {
1.293 ad 1644: struct proc *p;
1645: int signo, prop;
1646: sigpend_t *sp;
1.243 ad 1647: sigset_t ss;
1648:
1.293 ad 1649: p = l->l_proc;
1650: sp = NULL;
1651: signo = 0;
1652:
1653: KASSERT(p == curproc);
1.277 ad 1654: KASSERT(mutex_owned(p->p_lock));
1.29 cgd 1655:
1.243 ad 1656: for (;;) {
1657: /* Discard any signals that we have decided not to take. */
1.314 christos 1658: if (signo != 0) {
1.243 ad 1659: (void)sigget(sp, NULL, signo, NULL);
1.314 christos 1660: }
1.144 fvdl 1661:
1.243 ad 1662: /*
1663: * If the process is stopped/stopping, then stop ourselves
1664: * now that we're on the kernel/userspace boundary. When
1665: * we awaken, check for a signal from the debugger.
1666: */
1667: if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1.342 kamil 1668: sigswitch(PS_NOCLDSTOP, 0, true);
1.293 ad 1669: signo = sigchecktrace();
1.243 ad 1670: } else
1671: signo = 0;
1.238 ad 1672:
1.293 ad 1673: /* Signals from the debugger are "out of band". */
1674: sp = NULL;
1675:
1.130 thorpej 1676: /*
1.243 ad 1677: * If the debugger didn't provide a signal, find a pending
1678: * signal from our set. Check per-LWP signals first, and
1679: * then per-process.
1680: */
1681: if (signo == 0) {
1682: sp = &l->l_sigpend;
1683: ss = sp->sp_set;
1.285 ad 1684: if ((p->p_lflag & PL_PPWAIT) != 0)
1.243 ad 1685: sigminusset(&stopsigmask, &ss);
1686: sigminusset(&l->l_sigmask, &ss);
1687:
1688: if ((signo = firstsig(&ss)) == 0) {
1689: sp = &p->p_sigpend;
1690: ss = sp->sp_set;
1.285 ad 1691: if ((p->p_lflag & PL_PPWAIT) != 0)
1.243 ad 1692: sigminusset(&stopsigmask, &ss);
1693: sigminusset(&l->l_sigmask, &ss);
1694:
1695: if ((signo = firstsig(&ss)) == 0) {
1696: /*
1697: * No signal pending - clear the
1698: * indicator and bail out.
1699: */
1700: lwp_lock(l);
1.246 pavel 1701: l->l_flag &= ~LW_PENDSIG;
1.243 ad 1702: lwp_unlock(l);
1703: sp = NULL;
1704: break;
1705: }
1706: }
1.79 mycroft 1707: }
1.42 mycroft 1708:
1.29 cgd 1709: /*
1.243 ad 1710: * We should see pending but ignored signals only if
1711: * we are being traced.
1.29 cgd 1712: */
1.243 ad 1713: if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
1714: (p->p_slflag & PSL_TRACED) == 0) {
1715: /* Discard the signal. */
1.29 cgd 1716: continue;
1.243 ad 1717: }
1.42 mycroft 1718:
1.243 ad 1719: /*
1720: * If traced, always stop, and stay stopped until released
1.342 kamil 1721: * by the debugger. If the our parent is our debugger waiting
1722: * for us and we vforked, don't hang as we could deadlock.
1.243 ad 1723: */
1.344 ! kamil 1724: if (ISSET(p->p_slflag, PSL_TRACED) && signo != SIGKILL &&
! 1725: !(ISSET(p->p_lflag, PL_PPWAIT) &&
! 1726: (p->p_pptr == p->p_opptr))) {
1.313 christos 1727: /*
1728: * Take the signal, but don't remove it from the
1729: * siginfo queue, because the debugger can send
1730: * it later.
1731: */
1.315 christos 1732: if (sp)
1733: sigdelset(&sp->sp_set, signo);
1.322 christos 1734: p->p_xsig = signo;
1.184 manu 1735:
1.343 kamil 1736: /* Handling of signal trace */
1737: sigswitch(0, signo, true);
1.29 cgd 1738:
1.243 ad 1739: /* Check for a signal from the debugger. */
1.293 ad 1740: if ((signo = sigchecktrace()) == 0)
1.29 cgd 1741: continue;
1.293 ad 1742:
1743: /* Signals from the debugger are "out of band". */
1744: sp = NULL;
1.29 cgd 1745: }
1746:
1.243 ad 1747: prop = sigprop[signo];
1.42 mycroft 1748:
1.29 cgd 1749: /*
1750: * Decide whether the signal should be returned.
1751: */
1.243 ad 1752: switch ((long)SIGACTION(p, signo).sa_handler) {
1.33 cgd 1753: case (long)SIG_DFL:
1.29 cgd 1754: /*
1755: * Don't take default actions on system processes.
1756: */
1757: if (p->p_pid <= 1) {
1758: #ifdef DIAGNOSTIC
1759: /*
1760: * Are you sure you want to ignore SIGSEGV
1761: * in init? XXX
1762: */
1.243 ad 1763: printf_nolog("Process (pid %d) got sig %d\n",
1764: p->p_pid, signo);
1.29 cgd 1765: #endif
1.243 ad 1766: continue;
1.29 cgd 1767: }
1.243 ad 1768:
1.29 cgd 1769: /*
1.243 ad 1770: * If there is a pending stop signal to process with
1771: * default action, stop here, then clear the signal.
1772: * However, if process is member of an orphaned
1.29 cgd 1773: * process group, ignore tty stop signals.
1774: */
1775: if (prop & SA_STOP) {
1.276 ad 1776: /*
1777: * XXX Don't hold proc_lock for p_lflag,
1778: * but it's not a big deal.
1779: */
1.243 ad 1780: if (p->p_slflag & PSL_TRACED ||
1.301 rmind 1781: ((p->p_lflag & PL_ORPHANPG) != 0 &&
1.243 ad 1782: prop & SA_TTYSTOP)) {
1.301 rmind 1783: /* Ignore the signal. */
1.243 ad 1784: continue;
1785: }
1786: /* Take the signal. */
1787: (void)sigget(sp, NULL, signo, NULL);
1.322 christos 1788: p->p_xsig = signo;
1.327 christos 1789: p->p_sflag &= ~PS_CONTINUED;
1.243 ad 1790: signo = 0;
1.342 kamil 1791: sigswitch(PS_NOCLDSTOP, p->p_xsig, true);
1.29 cgd 1792: } else if (prop & SA_IGNORE) {
1793: /*
1794: * Except for SIGCONT, shouldn't get here.
1795: * Default action is to ignore; drop it.
1796: */
1.243 ad 1797: continue;
1798: }
1799: break;
1.29 cgd 1800:
1.33 cgd 1801: case (long)SIG_IGN:
1.243 ad 1802: #ifdef DEBUG_ISSIGNAL
1.29 cgd 1803: /*
1804: * Masking above should prevent us ever trying
1805: * to take action on an ignored signal other
1806: * than SIGCONT, unless process is traced.
1807: */
1808: if ((prop & SA_CONT) == 0 &&
1.243 ad 1809: (p->p_slflag & PSL_TRACED) == 0)
1810: printf_nolog("issignal\n");
1.128 jdolecek 1811: #endif
1.243 ad 1812: continue;
1.29 cgd 1813:
1814: default:
1815: /*
1.243 ad 1816: * This signal has an action, let postsig() process
1817: * it.
1.29 cgd 1818: */
1.243 ad 1819: break;
1.29 cgd 1820: }
1.243 ad 1821:
1822: break;
1.29 cgd 1823: }
1.42 mycroft 1824:
1.243 ad 1825: l->l_sigpendset = sp;
1826: return signo;
1.29 cgd 1827: }
1828:
1829: /*
1.243 ad 1830: * Take the action for the specified signal
1831: * from the current set of pending signals.
1.29 cgd 1832: */
1.179 christos 1833: void
1.243 ad 1834: postsig(int signo)
1.29 cgd 1835: {
1.243 ad 1836: struct lwp *l;
1837: struct proc *p;
1838: struct sigacts *ps;
1839: sig_t action;
1840: sigset_t *returnmask;
1841: ksiginfo_t ksi;
1842:
1843: l = curlwp;
1844: p = l->l_proc;
1845: ps = p->p_sigacts;
1846:
1.277 ad 1847: KASSERT(mutex_owned(p->p_lock));
1.243 ad 1848: KASSERT(signo > 0);
1849:
1850: /*
1851: * Set the new mask value and also defer further occurrences of this
1852: * signal.
1853: *
1.268 yamt 1854: * Special case: user has done a sigsuspend. Here the current mask is
1.293 ad 1855: * not of interest, but rather the mask from before the sigsuspend is
1.243 ad 1856: * what we want restored after the signal processing is completed.
1857: */
1858: if (l->l_sigrestore) {
1859: returnmask = &l->l_sigoldmask;
1860: l->l_sigrestore = 0;
1861: } else
1862: returnmask = &l->l_sigmask;
1.29 cgd 1863:
1.243 ad 1864: /*
1865: * Commit to taking the signal before releasing the mutex.
1866: */
1867: action = SIGACTION_PS(ps, signo).sa_handler;
1.275 ad 1868: l->l_ru.ru_nsignals++;
1.314 christos 1869: if (l->l_sigpendset == NULL) {
1.313 christos 1870: /* From the debugger */
1.332 kamil 1871: if (p->p_sigctx.ps_faked &&
1872: signo == p->p_sigctx.ps_info._signo) {
1873: KSI_INIT(&ksi);
1874: ksi.ksi_info = p->p_sigctx.ps_info;
1875: ksi.ksi_lid = p->p_sigctx.ps_lwp;
1876: p->p_sigctx.ps_faked = false;
1877: } else {
1878: if (!siggetinfo(&l->l_sigpend, &ksi, signo))
1879: (void)siggetinfo(&p->p_sigpend, &ksi, signo);
1880: }
1.314 christos 1881: } else
1882: sigget(l->l_sigpendset, &ksi, signo, NULL);
1.104 thorpej 1883:
1.255 ad 1884: if (ktrpoint(KTR_PSIG)) {
1.277 ad 1885: mutex_exit(p->p_lock);
1.330 martin 1886: if (p->p_emul->e_ktrpsig)
1887: p->p_emul->e_ktrpsig(signo, action,
1888: returnmask, &ksi);
1889: else
1890: ktrpsig(signo, action, returnmask, &ksi);
1.277 ad 1891: mutex_enter(p->p_lock);
1.243 ad 1892: }
1.130 thorpej 1893:
1.320 christos 1894: SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
1895:
1.243 ad 1896: if (action == SIG_DFL) {
1.175 cl 1897: /*
1.243 ad 1898: * Default action, where the default is to kill
1899: * the process. (Other cases were ignored above.)
1.175 cl 1900: */
1.243 ad 1901: sigexit(l, signo);
1902: return;
1.175 cl 1903: }
1904:
1.202 perry 1905: /*
1.243 ad 1906: * If we get here, the signal must be caught.
1.130 thorpej 1907: */
1908: #ifdef DIAGNOSTIC
1.243 ad 1909: if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
1910: panic("postsig action");
1.130 thorpej 1911: #endif
1.144 fvdl 1912:
1.243 ad 1913: kpsendsig(l, &ksi, returnmask);
1.29 cgd 1914: }
1915:
1.133 nathanw 1916: /*
1.290 ad 1917: * sendsig:
1918: *
1919: * Default signal delivery method for NetBSD.
1920: */
1921: void
1922: sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
1923: {
1924: struct sigacts *sa;
1925: int sig;
1926:
1927: sig = ksi->ksi_signo;
1928: sa = curproc->p_sigacts;
1929:
1930: switch (sa->sa_sigdesc[sig].sd_vers) {
1931: case 0:
1932: case 1:
1933: /* Compat for 1.6 and earlier. */
1934: if (sendsig_sigcontext_vec == NULL) {
1935: break;
1936: }
1937: (*sendsig_sigcontext_vec)(ksi, mask);
1938: return;
1939: case 2:
1.291 ad 1940: case 3:
1.290 ad 1941: sendsig_siginfo(ksi, mask);
1942: return;
1943: default:
1944: break;
1945: }
1946:
1947: printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
1948: sigexit(curlwp, SIGILL);
1949: }
1950:
1951: /*
1.243 ad 1952: * sendsig_reset:
1.133 nathanw 1953: *
1.243 ad 1954: * Reset the signal action. Called from emulation specific sendsig()
1955: * before unlocking to deliver the signal.
1.29 cgd 1956: */
1957: void
1.243 ad 1958: sendsig_reset(struct lwp *l, int signo)
1.29 cgd 1959: {
1.243 ad 1960: struct proc *p = l->l_proc;
1961: struct sigacts *ps = p->p_sigacts;
1.29 cgd 1962:
1.277 ad 1963: KASSERT(mutex_owned(p->p_lock));
1.106 thorpej 1964:
1.243 ad 1965: p->p_sigctx.ps_lwp = 0;
1.332 kamil 1966: memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
1.243 ad 1967:
1.259 ad 1968: mutex_enter(&ps->sa_mutex);
1.317 rmind 1969: sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
1.243 ad 1970: if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
1971: sigdelset(&p->p_sigctx.ps_sigcatch, signo);
1972: if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
1973: sigaddset(&p->p_sigctx.ps_sigignore, signo);
1974: SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1.29 cgd 1975: }
1.259 ad 1976: mutex_exit(&ps->sa_mutex);
1.29 cgd 1977: }
1978:
1979: /*
1980: * Kill the current process for stated reason.
1981: */
1.52 christos 1982: void
1.122 manu 1983: killproc(struct proc *p, const char *why)
1.29 cgd 1984: {
1.276 ad 1985:
1986: KASSERT(mutex_owned(proc_lock));
1987:
1.29 cgd 1988: log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1.243 ad 1989: uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
1.29 cgd 1990: psignal(p, SIGKILL);
1991: }
1992:
1993: /*
1994: * Force the current process to exit with the specified signal, dumping core
1.243 ad 1995: * if appropriate. We bypass the normal tests for masked and caught
1996: * signals, allowing unrecoverable failures to terminate the process without
1997: * changing signal state. Mark the accounting record with the signal
1998: * termination. If dumping core, save the signal number for the debugger.
1999: * Calls exit and does not return.
1.29 cgd 2000: */
1.243 ad 2001: void
2002: sigexit(struct lwp *l, int signo)
2003: {
1.323 christos 2004: int exitsig, error, docore;
1.243 ad 2005: struct proc *p;
2006: struct lwp *t;
1.96 fair 2007:
1.243 ad 2008: p = l->l_proc;
1.96 fair 2009:
1.277 ad 2010: KASSERT(mutex_owned(p->p_lock));
1.243 ad 2011: KERNEL_UNLOCK_ALL(l, NULL);
1.96 fair 2012:
1.243 ad 2013: /*
2014: * Don't permit coredump() multiple times in the same process.
2015: * Call back into sigexit, where we will be suspended until
2016: * the deed is done. Note that this is a recursive call, but
1.246 pavel 2017: * LW_WCORE will prevent us from coming back this way.
1.243 ad 2018: */
2019: if ((p->p_sflag & PS_WCORE) != 0) {
2020: lwp_lock(l);
1.246 pavel 2021: l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
1.243 ad 2022: lwp_unlock(l);
1.277 ad 2023: mutex_exit(p->p_lock);
1.243 ad 2024: lwp_userret(l);
1.281 ad 2025: panic("sigexit 1");
2026: /* NOTREACHED */
2027: }
2028:
2029: /* If process is already on the way out, then bail now. */
2030: if ((p->p_sflag & PS_WEXIT) != 0) {
2031: mutex_exit(p->p_lock);
2032: lwp_exit(l);
2033: panic("sigexit 2");
1.243 ad 2034: /* NOTREACHED */
2035: }
1.130 thorpej 2036:
2037: /*
1.243 ad 2038: * Prepare all other LWPs for exit. If dumping core, suspend them
2039: * so that their registers are available long enough to be dumped.
2040: */
2041: if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
2042: p->p_sflag |= PS_WCORE;
2043: for (;;) {
2044: LIST_FOREACH(t, &p->p_lwps, l_sibling) {
2045: lwp_lock(t);
2046: if (t == l) {
1.246 pavel 2047: t->l_flag &= ~LW_WSUSPEND;
1.243 ad 2048: lwp_unlock(t);
2049: continue;
2050: }
1.246 pavel 2051: t->l_flag |= (LW_WCORE | LW_WEXIT);
1.243 ad 2052: lwp_suspend(l, t);
2053: }
1.130 thorpej 2054:
1.243 ad 2055: if (p->p_nrlwps == 1)
2056: break;
1.130 thorpej 2057:
1.243 ad 2058: /*
2059: * Kick any LWPs sitting in lwp_wait1(), and wait
2060: * for everyone else to stop before proceeding.
2061: */
2062: p->p_nlwpwait++;
2063: cv_broadcast(&p->p_lwpcv);
1.277 ad 2064: cv_wait(&p->p_lwpcv, p->p_lock);
1.243 ad 2065: p->p_nlwpwait--;
2066: }
2067: }
1.130 thorpej 2068:
1.243 ad 2069: exitsig = signo;
2070: p->p_acflag |= AXSIG;
1.332 kamil 2071: memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
2072: p->p_sigctx.ps_info._signo = signo;
2073: p->p_sigctx.ps_info._code = SI_NOINFO;
1.130 thorpej 2074:
1.243 ad 2075: if (docore) {
1.281 ad 2076: mutex_exit(p->p_lock);
1.323 christos 2077: error = (*coredump_vec)(l, NULL);
1.102 sommerfe 2078:
2079: if (kern_logsigexit) {
1.224 ad 2080: int uid = l->l_cred ?
2081: (int)kauth_cred_geteuid(l->l_cred) : -1;
1.102 sommerfe 2082:
1.202 perry 2083: if (error)
1.102 sommerfe 2084: log(LOG_INFO, lognocoredump, p->p_pid,
1.243 ad 2085: p->p_comm, uid, signo, error);
1.102 sommerfe 2086: else
2087: log(LOG_INFO, logcoredump, p->p_pid,
1.243 ad 2088: p->p_comm, uid, signo);
1.96 fair 2089: }
2090:
1.240 elad 2091: #ifdef PAX_SEGVGUARD
1.249 thorpej 2092: pax_segvguard(l, p->p_textvp, p->p_comm, true);
1.240 elad 2093: #endif /* PAX_SEGVGUARD */
1.281 ad 2094: /* Acquire the sched state mutex. exit1() will release it. */
2095: mutex_enter(p->p_lock);
1.323 christos 2096: if (error == 0)
2097: p->p_sflag |= PS_COREDUMP;
1.29 cgd 2098: }
1.96 fair 2099:
1.243 ad 2100: /* No longer dumping core. */
2101: p->p_sflag &= ~PS_WCORE;
2102:
1.323 christos 2103: exit1(l, 0, exitsig);
1.29 cgd 2104: /* NOTREACHED */
2105: }
2106:
2107: /*
1.243 ad 2108: * Put process 'p' into the stopped state and optionally, notify the parent.
1.29 cgd 2109: */
1.243 ad 2110: void
1.334 christos 2111: proc_stop(struct proc *p, int signo)
1.29 cgd 2112: {
1.243 ad 2113: struct lwp *l;
1.112 lukem 2114:
1.277 ad 2115: KASSERT(mutex_owned(p->p_lock));
1.29 cgd 2116:
1.59 cgd 2117: /*
1.243 ad 2118: * First off, set the stopping indicator and bring all sleeping
2119: * LWPs to a halt so they are included in p->p_nrlwps. We musn't
2120: * unlock between here and the p->p_nrlwps check below.
1.59 cgd 2121: */
1.333 christos 2122: p->p_sflag |= PS_STOPPING | PS_NOTIFYSTOP;
1.260 ad 2123: membar_producer();
1.59 cgd 2124:
1.272 yamt 2125: proc_stop_lwps(p);
1.59 cgd 2126:
2127: /*
1.243 ad 2128: * If there are no LWPs available to take the signal, then we
2129: * signal the parent process immediately. Otherwise, the last
2130: * LWP to stop will take care of it.
1.59 cgd 2131: */
2132:
1.334 christos 2133: if (p->p_nrlwps == 0) {
1.340 kamil 2134: proc_stop_done(p, PS_NOCLDSTOP);
1.243 ad 2135: } else {
2136: /*
2137: * Have the remaining LWPs come to a halt, and trigger
2138: * proc_stop_callout() to ensure that they do.
2139: */
1.317 rmind 2140: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2141: sigpost(l, SIG_DFL, SA_STOP, signo);
2142: }
1.243 ad 2143: callout_schedule(&proc_stop_ch, 1);
1.169 hannken 2144: }
1.29 cgd 2145: }
2146:
2147: /*
1.243 ad 2148: * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2149: * but wait for them to come to a halt at the kernel-user boundary. This is
2150: * to allow LWPs to release any locks that they may hold before stopping.
2151: *
2152: * Non-interruptable sleeps can be long, and there is the potential for an
2153: * LWP to begin sleeping interruptably soon after the process has been set
2154: * stopping (PS_STOPPING). These LWPs will not notice that the process is
2155: * stopping, and so complete halt of the process and the return of status
2156: * information to the parent could be delayed indefinitely.
2157: *
2158: * To handle this race, proc_stop_callout() runs once per tick while there
1.256 ad 2159: * are stopping processes in the system. It sets LWPs that are sleeping
1.243 ad 2160: * interruptably into the LSSTOP state.
2161: *
2162: * Note that we are not concerned about keeping all LWPs stopped while the
2163: * process is stopped: stopped LWPs can awaken briefly to handle signals.
2164: * What we do need to ensure is that all LWPs in a stopping process have
2165: * stopped at least once, so that notification can be sent to the parent
2166: * process.
1.29 cgd 2167: */
1.243 ad 2168: static void
2169: proc_stop_callout(void *cookie)
1.29 cgd 2170: {
1.248 thorpej 2171: bool more, restart;
1.243 ad 2172: struct proc *p;
1.29 cgd 2173:
1.243 ad 2174: (void)cookie;
1.94 bouyer 2175:
1.243 ad 2176: do {
1.249 thorpej 2177: restart = false;
2178: more = false;
1.130 thorpej 2179:
1.276 ad 2180: mutex_enter(proc_lock);
1.243 ad 2181: PROCLIST_FOREACH(p, &allproc) {
1.277 ad 2182: mutex_enter(p->p_lock);
1.130 thorpej 2183:
1.243 ad 2184: if ((p->p_sflag & PS_STOPPING) == 0) {
1.277 ad 2185: mutex_exit(p->p_lock);
1.243 ad 2186: continue;
2187: }
1.130 thorpej 2188:
1.243 ad 2189: /* Stop any LWPs sleeping interruptably. */
1.272 yamt 2190: proc_stop_lwps(p);
1.243 ad 2191: if (p->p_nrlwps == 0) {
2192: /*
2193: * We brought the process to a halt.
2194: * Mark it as stopped and notify the
2195: * parent.
2196: */
2197: if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
2198: /*
1.272 yamt 2199: * Note that proc_stop_done() will
1.277 ad 2200: * drop p->p_lock briefly.
1.243 ad 2201: * Arrange to restart and check
2202: * all processes again.
2203: */
1.249 thorpej 2204: restart = true;
1.243 ad 2205: }
1.340 kamil 2206: proc_stop_done(p, PS_NOCLDSTOP);
1.243 ad 2207: } else
1.249 thorpej 2208: more = true;
1.130 thorpej 2209:
1.277 ad 2210: mutex_exit(p->p_lock);
1.243 ad 2211: if (restart)
2212: break;
2213: }
1.276 ad 2214: mutex_exit(proc_lock);
1.243 ad 2215: } while (restart);
1.185 matt 2216:
1.130 thorpej 2217: /*
1.243 ad 2218: * If we noted processes that are stopping but still have
2219: * running LWPs, then arrange to check again in 1 tick.
1.130 thorpej 2220: */
1.243 ad 2221: if (more)
2222: callout_schedule(&proc_stop_ch, 1);
1.108 jdolecek 2223: }
1.130 thorpej 2224:
1.135 jdolecek 2225: /*
1.243 ad 2226: * Given a process in state SSTOP, set the state back to SACTIVE and
2227: * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1.135 jdolecek 2228: */
1.243 ad 2229: void
2230: proc_unstop(struct proc *p)
1.135 jdolecek 2231: {
1.243 ad 2232: struct lwp *l;
2233: int sig;
1.208 cube 2234:
1.276 ad 2235: KASSERT(mutex_owned(proc_lock));
1.277 ad 2236: KASSERT(mutex_owned(p->p_lock));
1.135 jdolecek 2237:
1.243 ad 2238: p->p_stat = SACTIVE;
2239: p->p_sflag &= ~PS_STOPPING;
1.322 christos 2240: sig = p->p_xsig;
1.219 mrg 2241:
1.243 ad 2242: if (!p->p_waited)
2243: p->p_pptr->p_nstopchild--;
1.173 jdolecek 2244:
1.243 ad 2245: LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2246: lwp_lock(l);
2247: if (l->l_stat != LSSTOP) {
2248: lwp_unlock(l);
2249: continue;
1.171 jdolecek 2250: }
1.243 ad 2251: if (l->l_wchan == NULL) {
2252: setrunnable(l);
2253: continue;
1.241 enami 2254: }
1.246 pavel 2255: if (sig && (l->l_flag & LW_SINTR) != 0) {
1.301 rmind 2256: setrunnable(l);
2257: sig = 0;
1.250 ad 2258: } else {
2259: l->l_stat = LSSLEEP;
2260: p->p_nrlwps++;
1.243 ad 2261: lwp_unlock(l);
1.250 ad 2262: }
1.135 jdolecek 2263: }
1.29 cgd 2264: }
1.126 jdolecek 2265:
1.334 christos 2266: void
2267: proc_stoptrace(int trapno)
2268: {
2269: struct lwp *l = curlwp;
1.341 kamil 2270: struct proc *p = l->l_proc;
2271: struct sigacts *ps;
2272: sigset_t *mask;
2273: sig_t action;
2274: ksiginfo_t ksi;
2275: const int signo = SIGTRAP;
2276:
2277: KASSERT((trapno == TRAP_SCE) || (trapno == TRAP_SCX));
2278:
2279: KSI_INIT_TRAP(&ksi);
2280: ksi.ksi_lid = l->l_lid;
2281: ksi.ksi_info._signo = signo;
2282: ksi.ksi_info._code = trapno;
1.334 christos 2283:
2284: mutex_enter(p->p_lock);
2285:
1.341 kamil 2286: /* Needed for ktrace */
2287: ps = p->p_sigacts;
2288: action = SIGACTION_PS(ps, signo).sa_handler;
2289: mask = &l->l_sigmask;
2290:
2291: /* initproc (PID1) cannot became a debugger */
2292: KASSERT(p->p_pptr != initproc);
2293:
2294: KASSERT(ISSET(p->p_slflag, PSL_TRACED));
2295: KASSERT(ISSET(p->p_slflag, PSL_SYSCALL));
2296:
2297: p->p_xsig = signo;
2298: p->p_sigctx.ps_lwp = ksi.ksi_lid;
2299: p->p_sigctx.ps_info = ksi.ksi_info;
1.342 kamil 2300: sigswitch(0, signo, true);
1.334 christos 2301: mutex_exit(p->p_lock);
1.341 kamil 2302:
2303: if (ktrpoint(KTR_PSIG)) {
2304: if (p->p_emul->e_ktrpsig)
2305: p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
2306: else
2307: ktrpsig(signo, action, mask, &ksi);
2308: }
1.334 christos 2309: }
2310:
1.126 jdolecek 2311: static int
2312: filt_sigattach(struct knote *kn)
2313: {
2314: struct proc *p = curproc;
2315:
1.274 ad 2316: kn->kn_obj = p;
1.301 rmind 2317: kn->kn_flags |= EV_CLEAR; /* automatically set */
1.126 jdolecek 2318:
1.277 ad 2319: mutex_enter(p->p_lock);
1.126 jdolecek 2320: SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1.277 ad 2321: mutex_exit(p->p_lock);
1.126 jdolecek 2322:
1.301 rmind 2323: return 0;
1.126 jdolecek 2324: }
2325:
2326: static void
2327: filt_sigdetach(struct knote *kn)
2328: {
1.274 ad 2329: struct proc *p = kn->kn_obj;
1.126 jdolecek 2330:
1.277 ad 2331: mutex_enter(p->p_lock);
1.126 jdolecek 2332: SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1.277 ad 2333: mutex_exit(p->p_lock);
1.126 jdolecek 2334: }
2335:
2336: /*
1.301 rmind 2337: * Signal knotes are shared with proc knotes, so we apply a mask to
1.126 jdolecek 2338: * the hint in order to differentiate them from process hints. This
2339: * could be avoided by using a signal-specific knote list, but probably
2340: * isn't worth the trouble.
2341: */
2342: static int
2343: filt_signal(struct knote *kn, long hint)
2344: {
2345:
2346: if (hint & NOTE_SIGNAL) {
2347: hint &= ~NOTE_SIGNAL;
2348:
2349: if (kn->kn_id == hint)
2350: kn->kn_data++;
2351: }
2352: return (kn->kn_data != 0);
2353: }
2354:
2355: const struct filterops sig_filtops = {
1.338 maya 2356: .f_isfd = 0,
2357: .f_attach = filt_sigattach,
2358: .f_detach = filt_sigdetach,
2359: .f_event = filt_signal,
1.126 jdolecek 2360: };
CVSweb <webmaster@jp.NetBSD.org>