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