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