Annotation of src/sys/kern/kern_sig.c, Revision 1.121
1.121 ! thorpej 1: /* $NetBSD: kern_sig.c,v 1.120 2002/03/08 20:48:40 thorpej Exp $ */
1.29 cgd 2:
3: /*
4: * Copyright (c) 1982, 1986, 1989, 1991, 1993
5: * The Regents of the University of California. All rights reserved.
6: * (c) UNIX System Laboratories, Inc.
7: * All or some portions of this file are derived from material licensed
8: * to the University of California by American Telephone and Telegraph
9: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10: * the permission of UNIX System Laboratories, Inc.
11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
20: * 3. All advertising materials mentioning features or use of this software
21: * must display the following acknowledgement:
22: * This product includes software developed by the University of
23: * California, Berkeley and its contributors.
24: * 4. Neither the name of the University nor the names of its contributors
25: * may be used to endorse or promote products derived from this software
26: * without specific prior written permission.
27: *
28: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38: * SUCH DAMAGE.
39: *
1.71 fvdl 40: * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
1.29 cgd 41: */
1.116 lukem 42:
43: #include <sys/cdefs.h>
1.121 ! thorpej 44: __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.120 2002/03/08 20:48:40 thorpej Exp $");
1.70 mrg 45:
1.73 thorpej 46: #include "opt_ktrace.h"
1.74 thorpej 47: #include "opt_compat_sunos.h"
1.95 eeh 48: #include "opt_compat_netbsd32.h"
1.29 cgd 49:
50: #define SIGPROP /* include signal properties table */
51: #include <sys/param.h>
52: #include <sys/signalvar.h>
53: #include <sys/resourcevar.h>
54: #include <sys/namei.h>
55: #include <sys/vnode.h>
56: #include <sys/proc.h>
57: #include <sys/systm.h>
58: #include <sys/timeb.h>
59: #include <sys/times.h>
60: #include <sys/buf.h>
61: #include <sys/acct.h>
62: #include <sys/file.h>
63: #include <sys/kernel.h>
64: #include <sys/wait.h>
65: #include <sys/ktrace.h>
66: #include <sys/syslog.h>
67: #include <sys/stat.h>
68: #include <sys/core.h>
1.59 cgd 69: #include <sys/filedesc.h>
1.89 thorpej 70: #include <sys/malloc.h>
71: #include <sys/pool.h>
1.118 thorpej 72: #include <sys/exec.h>
1.29 cgd 73:
1.32 cgd 74: #include <sys/mount.h>
75: #include <sys/syscallargs.h>
76:
1.29 cgd 77: #include <machine/cpu.h>
78:
79: #include <sys/user.h> /* for coredump */
1.52 christos 80:
1.69 mrg 81: #include <uvm/uvm_extern.h>
82:
1.112 lukem 83: static void proc_stop(struct proc *p);
84: void killproc(struct proc *, char *);
85: static int build_corename(struct proc *, char [MAXPATHLEN]);
86: sigset_t contsigmask, stopsigmask, sigcantmask;
1.29 cgd 87:
1.112 lukem 88: struct pool sigacts_pool; /* memory pool for sigacts structures */
1.89 thorpej 89:
1.29 cgd 90: /*
91: * Can process p, with pcred pc, send the signal signum to process q?
92: */
1.112 lukem 93: #define CANSIGNAL(p, pc, q, signum) \
1.29 cgd 94: ((pc)->pc_ucred->cr_uid == 0 || \
95: (pc)->p_ruid == (q)->p_cred->p_ruid || \
96: (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
97: (pc)->p_ruid == (q)->p_ucred->cr_uid || \
98: (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
99: ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
100:
1.89 thorpej 101: /*
102: * Initialize signal-related data structures.
103: */
104: void
1.112 lukem 105: signal_init(void)
1.89 thorpej 106: {
1.112 lukem 107:
1.89 thorpej 108: pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
1.120 thorpej 109: &pool_allocator_nointr);
1.89 thorpej 110: }
111:
112: /*
1.109 jdolecek 113: * Create an initial sigctx structure, using the same signal state
114: * as p. If 'share' is set, share the sigctx_proc part, otherwise just
115: * copy it from parent.
1.89 thorpej 116: */
1.109 jdolecek 117: void
1.112 lukem 118: sigactsinit(struct proc *np, struct proc *pp, int share)
1.89 thorpej 119: {
120: struct sigacts *ps;
121:
1.109 jdolecek 122: if (share) {
123: np->p_sigacts = pp->p_sigacts;
124: pp->p_sigacts->sa_refcnt++;
125: } else {
126: ps = pool_get(&sigacts_pool, PR_WAITOK);
127: if (pp)
128: memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
129: else
130: memset(ps, '\0', sizeof(struct sigacts));
131: ps->sa_refcnt = 1;
132: np->p_sigacts = ps;
133: }
1.89 thorpej 134: }
135:
136: /*
1.109 jdolecek 137: * Make this process not share its sigctx, maintaining all
1.89 thorpej 138: * signal state.
139: */
140: void
1.112 lukem 141: sigactsunshare(struct proc *p)
1.89 thorpej 142: {
1.109 jdolecek 143: struct sigacts *oldps;
1.89 thorpej 144:
1.109 jdolecek 145: if (p->p_sigacts->sa_refcnt == 1)
1.89 thorpej 146: return;
147:
1.109 jdolecek 148: oldps = p->p_sigacts;
149: sigactsinit(p, NULL, 0);
150:
151: if (--oldps->sa_refcnt == 0)
152: pool_put(&sigacts_pool, oldps);
1.89 thorpej 153: }
154:
155: /*
1.109 jdolecek 156: * Release a sigctx structure.
1.89 thorpej 157: */
158: void
1.112 lukem 159: sigactsfree(struct proc *p)
1.89 thorpej 160: {
1.112 lukem 161: struct sigacts *ps;
1.89 thorpej 162:
1.112 lukem 163: ps = p->p_sigacts;
1.109 jdolecek 164: if (--ps->sa_refcnt > 0)
1.89 thorpej 165: return;
166:
167: pool_put(&sigacts_pool, ps);
168: }
169:
1.79 mycroft 170: int
1.112 lukem 171: sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
1.121 ! thorpej 172: struct sigaction *osa, void *tramp, int vers)
1.79 mycroft 173: {
1.112 lukem 174: struct sigacts *ps;
175: int prop;
1.79 mycroft 176:
1.112 lukem 177: ps = p->p_sigacts;
1.79 mycroft 178: if (signum <= 0 || signum >= NSIG)
179: return (EINVAL);
180:
1.121 ! thorpej 181: /*
! 182: * Trampoline ABI version 0 is reserved for the legacy
! 183: * kernel-provided on-stack trampoline. Conversely, if
! 184: * we are using a non-0 ABI version, we must have a
! 185: * trampoline.
! 186: */
! 187: if ((vers != 0 && tramp == NULL) ||
! 188: (vers == 0 && tramp != NULL))
! 189: return (EINVAL);
! 190:
1.79 mycroft 191: if (osa)
1.109 jdolecek 192: *osa = SIGACTION_PS(ps, signum);
1.79 mycroft 193:
194: if (nsa) {
195: if (nsa->sa_flags & ~SA_ALLBITS)
196: return (EINVAL);
197:
198: prop = sigprop[signum];
199: if (prop & SA_CANTMASK)
200: return (EINVAL);
201:
1.105 thorpej 202: (void) splsched(); /* XXXSMP */
1.109 jdolecek 203: SIGACTION_PS(ps, signum) = *nsa;
1.121 ! thorpej 204: ps->sa_sigdesc[signum].sd_tramp = tramp;
! 205: ps->sa_sigdesc[signum].sd_vers = vers;
1.109 jdolecek 206: sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
1.79 mycroft 207: if ((prop & SA_NORESET) != 0)
1.109 jdolecek 208: SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
1.79 mycroft 209: if (signum == SIGCHLD) {
210: if (nsa->sa_flags & SA_NOCLDSTOP)
211: p->p_flag |= P_NOCLDSTOP;
212: else
213: p->p_flag &= ~P_NOCLDSTOP;
1.82 enami 214: if (nsa->sa_flags & SA_NOCLDWAIT) {
1.81 christos 215: /*
216: * Paranoia: since SA_NOCLDWAIT is implemented
217: * by reparenting the dying child to PID 1 (and
1.112 lukem 218: * trust it to reap the zombie), PID 1 itself
219: * is forbidden to set SA_NOCLDWAIT.
1.81 christos 220: */
221: if (p->p_pid == 1)
222: p->p_flag &= ~P_NOCLDWAIT;
223: else
224: p->p_flag |= P_NOCLDWAIT;
225: } else
226: p->p_flag &= ~P_NOCLDWAIT;
1.79 mycroft 227: }
228: if ((nsa->sa_flags & SA_NODEFER) == 0)
1.109 jdolecek 229: sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
1.79 mycroft 230: else
1.109 jdolecek 231: sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
1.79 mycroft 232: /*
1.112 lukem 233: * Set bit in p_sigctx.ps_sigignore for signals that are set to
234: * SIG_IGN, and for signals set to SIG_DFL where the default is
235: * to ignore. However, don't put SIGCONT in
236: * p_sigctx.ps_sigignore, as we have to restart the process.
237: */
1.79 mycroft 238: if (nsa->sa_handler == SIG_IGN ||
239: (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
1.112 lukem 240: /* never to be seen again */
241: sigdelset(&p->p_sigctx.ps_siglist, signum);
242: if (signum != SIGCONT) {
243: /* easier in psignal */
244: sigaddset(&p->p_sigctx.ps_sigignore, signum);
245: }
1.109 jdolecek 246: sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79 mycroft 247: } else {
1.109 jdolecek 248: sigdelset(&p->p_sigctx.ps_sigignore, signum);
1.79 mycroft 249: if (nsa->sa_handler == SIG_DFL)
1.109 jdolecek 250: sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79 mycroft 251: else
1.109 jdolecek 252: sigaddset(&p->p_sigctx.ps_sigcatch, signum);
1.79 mycroft 253: }
254: (void) spl0();
255: }
256:
257: return (0);
258: }
259:
1.29 cgd 260: /* ARGSUSED */
1.52 christos 261: int
1.112 lukem 262: sys___sigaction14(struct proc *p, void *v, register_t *retval)
1.48 thorpej 263: {
1.98 augustss 264: struct sys___sigaction14_args /* {
1.112 lukem 265: syscallarg(int) signum;
266: syscallarg(const struct sigaction *) nsa;
267: syscallarg(struct sigaction *) osa;
1.48 thorpej 268: } */ *uap = v;
1.112 lukem 269: struct sigaction nsa, osa;
270: int error;
1.29 cgd 271:
1.79 mycroft 272: if (SCARG(uap, nsa)) {
273: error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
1.52 christos 274: if (error)
1.29 cgd 275: return (error);
276: }
1.79 mycroft 277: error = sigaction1(p, SCARG(uap, signum),
1.121 ! thorpej 278: SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
! 279: NULL, 0);
! 280: if (error)
! 281: return (error);
! 282: if (SCARG(uap, osa)) {
! 283: error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
! 284: if (error)
! 285: return (error);
! 286: }
! 287: return (0);
! 288: }
! 289:
! 290: /* ARGSUSED */
! 291: int
! 292: sys___sigaction_sigtramp(struct proc *p, void *v, register_t *retval)
! 293: {
! 294: struct sys___sigaction_sigtramp_args /* {
! 295: syscallarg(int) signum;
! 296: syscallarg(const struct sigaction *) nsa;
! 297: syscallarg(struct sigaction *) osa;
! 298: syscallarg(void *) tramp;
! 299: syscallarg(int) vers;
! 300: } */ *uap = v;
! 301: struct sigaction nsa, osa;
! 302: int error;
! 303:
! 304: if (SCARG(uap, nsa)) {
! 305: error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
! 306: if (error)
! 307: return (error);
! 308: }
! 309: error = sigaction1(p, SCARG(uap, signum),
! 310: SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
! 311: SCARG(uap, tramp), SCARG(uap, vers));
1.79 mycroft 312: if (error)
313: return (error);
314: if (SCARG(uap, osa)) {
315: error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
1.52 christos 316: if (error)
1.29 cgd 317: return (error);
318: }
319: return (0);
320: }
321:
322: /*
323: * Initialize signal state for process 0;
1.79 mycroft 324: * set to ignore signals that are ignored by default and disable the signal
325: * stack.
1.29 cgd 326: */
327: void
1.112 lukem 328: siginit(struct proc *p)
1.29 cgd 329: {
1.112 lukem 330: struct sigacts *ps;
331: int signum, prop;
1.79 mycroft 332:
1.112 lukem 333: ps = p->p_sigacts;
1.79 mycroft 334: sigemptyset(&contsigmask);
335: sigemptyset(&stopsigmask);
336: sigemptyset(&sigcantmask);
1.85 mycroft 337: for (signum = 1; signum < NSIG; signum++) {
1.79 mycroft 338: prop = sigprop[signum];
339: if (prop & SA_CONT)
340: sigaddset(&contsigmask, signum);
341: if (prop & SA_STOP)
342: sigaddset(&stopsigmask, signum);
343: if (prop & SA_CANTMASK)
344: sigaddset(&sigcantmask, signum);
345: if (prop & SA_IGNORE && signum != SIGCONT)
1.109 jdolecek 346: sigaddset(&p->p_sigctx.ps_sigignore, signum);
347: sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
348: SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
1.79 mycroft 349: }
1.109 jdolecek 350: sigemptyset(&p->p_sigctx.ps_sigcatch);
1.79 mycroft 351: p->p_flag &= ~P_NOCLDSTOP;
1.29 cgd 352:
1.79 mycroft 353: /*
354: * Reset stack state to the user stack.
355: */
1.109 jdolecek 356: p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
357: p->p_sigctx.ps_sigstk.ss_size = 0;
358: p->p_sigctx.ps_sigstk.ss_sp = 0;
1.89 thorpej 359:
360: /* One reference. */
1.109 jdolecek 361: ps->sa_refcnt = 1;
1.29 cgd 362: }
363:
364: /*
365: * Reset signals for an exec of the specified process.
366: */
367: void
1.112 lukem 368: execsigs(struct proc *p)
1.29 cgd 369: {
1.112 lukem 370: struct sigacts *ps;
371: int signum, prop;
1.29 cgd 372:
1.115 thorpej 373: sigactsunshare(p);
374:
1.112 lukem 375: ps = p->p_sigacts;
1.115 thorpej 376:
1.29 cgd 377: /*
378: * Reset caught signals. Held signals remain held
1.109 jdolecek 379: * through p_sigctx.ps_sigmask (unless they were caught,
1.29 cgd 380: * and are now ignored by default).
381: */
1.85 mycroft 382: for (signum = 1; signum < NSIG; signum++) {
1.109 jdolecek 383: if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
1.79 mycroft 384: prop = sigprop[signum];
385: if (prop & SA_IGNORE) {
386: if ((prop & SA_CONT) == 0)
1.112 lukem 387: sigaddset(&p->p_sigctx.ps_sigignore,
388: signum);
1.109 jdolecek 389: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.79 mycroft 390: }
1.109 jdolecek 391: SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.29 cgd 392: }
1.109 jdolecek 393: sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
394: SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
1.29 cgd 395: }
1.109 jdolecek 396: sigemptyset(&p->p_sigctx.ps_sigcatch);
1.79 mycroft 397: p->p_flag &= ~P_NOCLDSTOP;
398:
1.29 cgd 399: /*
400: * Reset stack state to the user stack.
401: */
1.109 jdolecek 402: p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
403: p->p_sigctx.ps_sigstk.ss_size = 0;
404: p->p_sigctx.ps_sigstk.ss_sp = 0;
1.29 cgd 405: }
406:
1.79 mycroft 407: int
1.112 lukem 408: sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
1.79 mycroft 409: {
410:
411: if (oss)
1.109 jdolecek 412: *oss = p->p_sigctx.ps_sigmask;
1.79 mycroft 413:
414: if (nss) {
1.105 thorpej 415: (void)splsched(); /* XXXSMP */
1.79 mycroft 416: switch (how) {
417: case SIG_BLOCK:
1.109 jdolecek 418: sigplusset(nss, &p->p_sigctx.ps_sigmask);
1.79 mycroft 419: break;
420: case SIG_UNBLOCK:
1.109 jdolecek 421: sigminusset(nss, &p->p_sigctx.ps_sigmask);
1.110 thorpej 422: CHECKSIGS(p);
1.79 mycroft 423: break;
424: case SIG_SETMASK:
1.109 jdolecek 425: p->p_sigctx.ps_sigmask = *nss;
1.110 thorpej 426: CHECKSIGS(p);
1.79 mycroft 427: break;
428: default:
1.104 thorpej 429: (void)spl0(); /* XXXSMP */
1.79 mycroft 430: return (EINVAL);
431: }
1.109 jdolecek 432: sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
1.104 thorpej 433: (void)spl0(); /* XXXSMP */
1.79 mycroft 434: }
435:
436: return (0);
437: }
438:
1.29 cgd 439: /*
440: * Manipulate signal mask.
441: * Note that we receive new mask, not pointer,
442: * and return old mask as return value;
443: * the library stub does the rest.
444: */
1.52 christos 445: int
1.112 lukem 446: sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
1.48 thorpej 447: {
1.79 mycroft 448: struct sys___sigprocmask14_args /* {
1.112 lukem 449: syscallarg(int) how;
450: syscallarg(const sigset_t *) set;
451: syscallarg(sigset_t *) oset;
1.48 thorpej 452: } */ *uap = v;
1.112 lukem 453: sigset_t nss, oss;
454: int error;
1.29 cgd 455:
1.79 mycroft 456: if (SCARG(uap, set)) {
457: error = copyin(SCARG(uap, set), &nss, sizeof(nss));
458: if (error)
459: return (error);
460: }
461: error = sigprocmask1(p, SCARG(uap, how),
462: SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
463: if (error)
464: return (error);
465: if (SCARG(uap, oset)) {
466: error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
467: if (error)
468: return (error);
469: }
470: return (0);
471: }
472:
473: void
1.112 lukem 474: sigpending1(struct proc *p, sigset_t *ss)
1.79 mycroft 475: {
1.29 cgd 476:
1.109 jdolecek 477: *ss = p->p_sigctx.ps_siglist;
478: sigminusset(&p->p_sigctx.ps_sigmask, ss);
1.29 cgd 479: }
480:
481: /* ARGSUSED */
1.52 christos 482: int
1.112 lukem 483: sys___sigpending14(struct proc *p, void *v, register_t *retval)
1.29 cgd 484: {
1.98 augustss 485: struct sys___sigpending14_args /* {
1.112 lukem 486: syscallarg(sigset_t *) set;
1.79 mycroft 487: } */ *uap = v;
488: sigset_t ss;
489:
490: sigpending1(p, &ss);
491: return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
492: }
493:
494: int
1.112 lukem 495: sigsuspend1(struct proc *p, const sigset_t *ss)
1.79 mycroft 496: {
1.112 lukem 497: struct sigacts *ps;
1.29 cgd 498:
1.112 lukem 499: ps = p->p_sigacts;
1.79 mycroft 500: if (ss) {
501: /*
502: * When returning from sigpause, we want
503: * the old mask to be restored after the
504: * signal handler has finished. Thus, we
1.109 jdolecek 505: * save it here and mark the sigctx structure
1.79 mycroft 506: * to indicate this.
507: */
1.109 jdolecek 508: p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
509: p->p_sigctx.ps_flags |= SAS_OLDMASK;
1.105 thorpej 510: (void) splsched(); /* XXXSMP */
1.109 jdolecek 511: p->p_sigctx.ps_sigmask = *ss;
1.110 thorpej 512: CHECKSIGS(p);
1.109 jdolecek 513: sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
1.104 thorpej 514: (void) spl0(); /* XXXSMP */
1.79 mycroft 515: }
516:
517: while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
518: /* void */;
519: /* always return EINTR rather than ERESTART... */
520: return (EINTR);
1.29 cgd 521: }
522:
523: /*
524: * Suspend process until signal, providing mask to be set
525: * in the meantime. Note nonstandard calling convention:
526: * libc stub passes mask, not pointer, to save a copyin.
527: */
528: /* ARGSUSED */
529: int
1.112 lukem 530: sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
1.48 thorpej 531: {
1.79 mycroft 532: struct sys___sigsuspend14_args /* {
1.112 lukem 533: syscallarg(const sigset_t *) set;
1.48 thorpej 534: } */ *uap = v;
1.112 lukem 535: sigset_t ss;
536: int error;
1.79 mycroft 537:
538: if (SCARG(uap, set)) {
539: error = copyin(SCARG(uap, set), &ss, sizeof(ss));
540: if (error)
541: return (error);
542: }
543:
544: return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
545: }
546:
547: int
1.112 lukem 548: sigaltstack1(struct proc *p, const struct sigaltstack *nss,
549: struct sigaltstack *oss)
1.79 mycroft 550: {
1.112 lukem 551:
1.79 mycroft 552: if (oss)
1.109 jdolecek 553: *oss = p->p_sigctx.ps_sigstk;
1.79 mycroft 554:
555: if (nss) {
556: if (nss->ss_flags & ~SS_ALLBITS)
557: return (EINVAL);
558:
559: if (nss->ss_flags & SS_DISABLE) {
1.109 jdolecek 560: if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
1.79 mycroft 561: return (EINVAL);
562: } else {
563: if (nss->ss_size < MINSIGSTKSZ)
564: return (ENOMEM);
565: }
1.109 jdolecek 566: p->p_sigctx.ps_sigstk = *nss;
1.79 mycroft 567: }
568:
569: return (0);
1.29 cgd 570: }
571:
572: /* ARGSUSED */
1.52 christos 573: int
1.112 lukem 574: sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
1.48 thorpej 575: {
1.98 augustss 576: struct sys___sigaltstack14_args /* {
1.112 lukem 577: syscallarg(const struct sigaltstack *) nss;
578: syscallarg(struct sigaltstack *) oss;
1.48 thorpej 579: } */ *uap = v;
1.112 lukem 580: struct sigaltstack nss, oss;
581: int error;
1.29 cgd 582:
1.79 mycroft 583: if (SCARG(uap, nss)) {
584: error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
585: if (error)
586: return (error);
587: }
588: error = sigaltstack1(p,
589: SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
1.52 christos 590: if (error)
1.29 cgd 591: return (error);
1.79 mycroft 592: if (SCARG(uap, oss)) {
593: error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
594: if (error)
595: return (error);
1.29 cgd 596: }
597: return (0);
598: }
599:
600: /* ARGSUSED */
601: int
1.112 lukem 602: sys_kill(struct proc *cp, void *v, register_t *retval)
1.48 thorpej 603: {
1.98 augustss 604: struct sys_kill_args /* {
1.112 lukem 605: syscallarg(int) pid;
606: syscallarg(int) signum;
1.48 thorpej 607: } */ *uap = v;
1.112 lukem 608: struct proc *p;
609: struct pcred *pc;
1.29 cgd 610:
1.112 lukem 611: pc = cp->p_cred;
1.32 cgd 612: if ((u_int)SCARG(uap, signum) >= NSIG)
1.29 cgd 613: return (EINVAL);
1.32 cgd 614: if (SCARG(uap, pid) > 0) {
1.29 cgd 615: /* kill single process */
1.32 cgd 616: if ((p = pfind(SCARG(uap, pid))) == NULL)
1.29 cgd 617: return (ESRCH);
1.32 cgd 618: if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
1.29 cgd 619: return (EPERM);
1.32 cgd 620: if (SCARG(uap, signum))
621: psignal(p, SCARG(uap, signum));
1.29 cgd 622: return (0);
623: }
1.32 cgd 624: switch (SCARG(uap, pid)) {
1.29 cgd 625: case -1: /* broadcast signal */
1.32 cgd 626: return (killpg1(cp, SCARG(uap, signum), 0, 1));
1.29 cgd 627: case 0: /* signal own process group */
1.32 cgd 628: return (killpg1(cp, SCARG(uap, signum), 0, 0));
1.29 cgd 629: default: /* negative explicit process group */
1.32 cgd 630: return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
1.29 cgd 631: }
632: /* NOTREACHED */
633: }
634:
635: /*
636: * Common code for kill process group/broadcast kill.
637: * cp is calling process.
638: */
1.52 christos 639: int
1.112 lukem 640: killpg1(struct proc *cp, int signum, int pgid, int all)
1.29 cgd 641: {
1.112 lukem 642: struct proc *p;
643: struct pcred *pc;
644: struct pgrp *pgrp;
645: int nfound;
1.29 cgd 646:
1.112 lukem 647: pc = cp->p_cred;
648: nfound = 0;
1.91 thorpej 649: if (all) {
1.29 cgd 650: /*
651: * broadcast
652: */
1.92 thorpej 653: proclist_lock_read();
1.31 mycroft 654: for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1.29 cgd 655: if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
656: p == cp || !CANSIGNAL(cp, pc, p, signum))
657: continue;
658: nfound++;
659: if (signum)
660: psignal(p, signum);
661: }
1.91 thorpej 662: proclist_unlock_read();
663: } else {
1.29 cgd 664: if (pgid == 0)
665: /*
666: * zero pgid means send to my process group.
667: */
668: pgrp = cp->p_pgrp;
669: else {
670: pgrp = pgfind(pgid);
671: if (pgrp == NULL)
672: return (ESRCH);
673: }
1.112 lukem 674: for (p = pgrp->pg_members.lh_first;
675: p != 0;
676: p = p->p_pglist.le_next) {
1.29 cgd 677: if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
678: !CANSIGNAL(cp, pc, p, signum))
679: continue;
680: nfound++;
1.90 thorpej 681: if (signum && P_ZOMBIE(p) == 0)
1.29 cgd 682: psignal(p, signum);
683: }
684: }
685: return (nfound ? 0 : ESRCH);
686: }
687:
688: /*
689: * Send a signal to a process group.
690: */
691: void
1.112 lukem 692: gsignal(int pgid, int signum)
1.29 cgd 693: {
694: struct pgrp *pgrp;
695:
696: if (pgid && (pgrp = pgfind(pgid)))
697: pgsignal(pgrp, signum, 0);
698: }
699:
700: /*
1.71 fvdl 701: * Send a signal to a process group. If checktty is 1,
1.29 cgd 702: * limit to members which have a controlling terminal.
703: */
704: void
1.112 lukem 705: pgsignal(struct pgrp *pgrp, int signum, int checkctty)
1.29 cgd 706: {
1.98 augustss 707: struct proc *p;
1.29 cgd 708:
709: if (pgrp)
1.112 lukem 710: for (p = pgrp->pg_members.lh_first; p != 0;
711: p = p->p_pglist.le_next)
1.29 cgd 712: if (checkctty == 0 || p->p_flag & P_CONTROLT)
713: psignal(p, signum);
714: }
715:
716: /*
717: * Send a signal caused by a trap to the current process.
718: * If it will be caught immediately, deliver it with correct code.
719: * Otherwise, post it normally.
720: */
721: void
1.112 lukem 722: trapsignal(struct proc *p, int signum, u_long code)
1.29 cgd 723: {
1.112 lukem 724: struct sigacts *ps;
1.29 cgd 725:
1.112 lukem 726: ps = p->p_sigacts;
1.79 mycroft 727: if ((p->p_flag & P_TRACED) == 0 &&
1.109 jdolecek 728: sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
729: !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
1.29 cgd 730: p->p_stats->p_ru.ru_nsignals++;
731: #ifdef KTRACE
732: if (KTRPOINT(p, KTR_PSIG))
1.100 sommerfe 733: ktrpsig(p, signum,
1.109 jdolecek 734: SIGACTION_PS(ps, signum).sa_handler,
735: &p->p_sigctx.ps_sigmask, code);
1.29 cgd 736: #endif
1.121 ! thorpej 737: (*p->p_emul->e_sendsig)(signum, &p->p_sigctx.ps_sigmask,
! 738: code);
1.105 thorpej 739: (void) splsched(); /* XXXSMP */
1.112 lukem 740: sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
741: &p->p_sigctx.ps_sigmask);
1.109 jdolecek 742: if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
743: sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.45 mycroft 744: if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1.109 jdolecek 745: sigaddset(&p->p_sigctx.ps_sigignore, signum);
746: SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.45 mycroft 747: }
1.104 thorpej 748: (void) spl0(); /* XXXSMP */
1.29 cgd 749: } else {
1.109 jdolecek 750: p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */
751: p->p_sigctx.ps_sig = signum; /* XXX to verify code */
1.29 cgd 752: psignal(p, signum);
753: }
754: }
755:
756: /*
757: * Send the signal to the process. If the signal has an action, the action
758: * is usually performed by the target process rather than the caller; we add
759: * the signal to the set of pending signals for the process.
760: *
761: * Exceptions:
762: * o When a stop signal is sent to a sleeping process that takes the
763: * default action, the process is stopped without awakening it.
764: * o SIGCONT restarts stopped processes (or puts them back to sleep)
765: * regardless of the signal action (eg, blocked or ignored).
766: *
767: * Other ignored signals are discarded immediately.
1.104 thorpej 768: *
769: * XXXSMP: Invoked as psignal() or sched_psignal().
1.29 cgd 770: */
771: void
1.112 lukem 772: psignal1(struct proc *p, int signum,
773: int dolock) /* XXXSMP: works, but icky */
1.29 cgd 774: {
1.112 lukem 775: int s, prop;
776: sig_t action;
1.29 cgd 777:
1.79 mycroft 778: #ifdef DIAGNOSTIC
779: if (signum <= 0 || signum >= NSIG)
1.29 cgd 780: panic("psignal signal number");
1.104 thorpej 781:
782: /* XXXSMP: works, but icky */
783: if (dolock)
784: SCHED_ASSERT_UNLOCKED();
785: else
786: SCHED_ASSERT_LOCKED();
1.79 mycroft 787: #endif
1.29 cgd 788: prop = sigprop[signum];
789:
790: /*
791: * If proc is traced, always give parent a chance.
792: */
793: if (p->p_flag & P_TRACED)
794: action = SIG_DFL;
795: else {
796: /*
797: * If the signal is being ignored,
798: * then we forget about it immediately.
1.109 jdolecek 799: * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
1.29 cgd 800: * and if it is set to SIG_IGN,
801: * action will be SIG_DFL here.)
802: */
1.109 jdolecek 803: if (sigismember(&p->p_sigctx.ps_sigignore, signum))
1.29 cgd 804: return;
1.109 jdolecek 805: if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1.29 cgd 806: action = SIG_HOLD;
1.109 jdolecek 807: else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1.29 cgd 808: action = SIG_CATCH;
1.44 mycroft 809: else {
1.29 cgd 810: action = SIG_DFL;
1.44 mycroft 811:
812: if (prop & SA_KILL && p->p_nice > NZERO)
813: p->p_nice = NZERO;
814:
815: /*
816: * If sending a tty stop signal to a member of an
817: * orphaned process group, discard the signal here if
818: * the action is default; don't stop the process below
819: * if sleeping, and don't clear any pending SIGCONT.
820: */
821: if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
822: return;
823: }
1.29 cgd 824: }
825:
826: if (prop & SA_CONT)
1.109 jdolecek 827: sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
1.29 cgd 828:
1.44 mycroft 829: if (prop & SA_STOP)
1.109 jdolecek 830: sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
1.44 mycroft 831:
1.109 jdolecek 832: sigaddset(&p->p_sigctx.ps_siglist, signum);
1.110 thorpej 833:
834: /* CHECKSIGS() is "inlined" here. */
1.109 jdolecek 835: p->p_sigctx.ps_sigcheck = 1;
1.29 cgd 836:
837: /*
838: * Defer further processing for signals which are held,
839: * except that stopped processes must be continued by SIGCONT.
840: */
841: if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
842: return;
1.104 thorpej 843:
844: /* XXXSMP: works, but icky */
845: if (dolock)
846: SCHED_LOCK(s);
847:
1.29 cgd 848: switch (p->p_stat) {
849: case SSLEEP:
850: /*
851: * If process is sleeping uninterruptibly
852: * we can't interrupt the sleep... the signal will
853: * be noticed when the process returns through
854: * trap() or syscall().
855: */
856: if ((p->p_flag & P_SINTR) == 0)
857: goto out;
858: /*
859: * Process is sleeping and traced... make it runnable
860: * so it can discover the signal in issignal() and stop
861: * for the parent.
862: */
863: if (p->p_flag & P_TRACED)
864: goto run;
865: /*
866: * If SIGCONT is default (or ignored) and process is
867: * asleep, we are finished; the process should not
868: * be awakened.
869: */
870: if ((prop & SA_CONT) && action == SIG_DFL) {
1.109 jdolecek 871: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29 cgd 872: goto out;
873: }
874: /*
875: * When a sleeping process receives a stop
876: * signal, process immediately if possible.
877: */
1.34 mycroft 878: if ((prop & SA_STOP) && action == SIG_DFL) {
1.29 cgd 879: /*
880: * If a child holding parent blocked,
881: * stopping could cause deadlock.
882: */
883: if (p->p_flag & P_PPWAIT)
884: goto out;
1.109 jdolecek 885: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29 cgd 886: p->p_xstat = signum;
1.104 thorpej 887: if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
888: /*
889: * XXXSMP: recursive call; don't lock
890: * the second time around.
891: */
892: sched_psignal(p->p_pptr, SIGCHLD);
893: }
894: proc_stop(p); /* XXXSMP: recurse? */
1.29 cgd 895: goto out;
1.34 mycroft 896: }
897: /*
898: * All other (caught or default) signals
899: * cause the process to run.
900: */
901: goto runfast;
1.29 cgd 902: /*NOTREACHED*/
903:
904: case SSTOP:
905: /*
906: * If traced process is already stopped,
907: * then no further action is necessary.
908: */
909: if (p->p_flag & P_TRACED)
910: goto out;
911:
912: /*
913: * Kill signal always sets processes running.
914: */
915: if (signum == SIGKILL)
916: goto runfast;
917:
918: if (prop & SA_CONT) {
919: /*
920: * If SIGCONT is default (or ignored), we continue the
1.109 jdolecek 921: * process but don't leave the signal in p_sigctx.ps_siglist, as
1.29 cgd 922: * it has no further action. If SIGCONT is held, we
923: * continue the process and leave the signal in
1.109 jdolecek 924: * p_sigctx.ps_siglist. If the process catches SIGCONT, let it
1.29 cgd 925: * handle the signal itself. If it isn't waiting on
926: * an event, then it goes back to run state.
927: * Otherwise, process goes back to sleep state.
928: */
929: if (action == SIG_DFL)
1.109 jdolecek 930: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29 cgd 931: if (action == SIG_CATCH)
932: goto runfast;
933: if (p->p_wchan == 0)
934: goto run;
935: p->p_stat = SSLEEP;
936: goto out;
937: }
938:
939: if (prop & SA_STOP) {
940: /*
941: * Already stopped, don't need to stop again.
942: * (If we did the shell could get confused.)
943: */
1.109 jdolecek 944: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29 cgd 945: goto out;
946: }
947:
948: /*
949: * If process is sleeping interruptibly, then simulate a
950: * wakeup so that when it is continued, it will be made
951: * runnable and can look at the signal. But don't make
952: * the process runnable, leave it stopped.
953: */
954: if (p->p_wchan && p->p_flag & P_SINTR)
955: unsleep(p);
956: goto out;
1.110 thorpej 957: #ifdef __HAVE_AST_PERPROC
958: case SONPROC:
959: case SRUN:
960: case SIDL:
961: /*
962: * SONPROC: We're running, notice the signal when
963: * we return back to userspace.
964: *
965: * SRUN, SIDL: Notice the signal when we run again
966: * and return to back to userspace.
967: */
968: signotify(p);
969: goto out;
1.29 cgd 970:
1.110 thorpej 971: default:
972: /*
973: * SDEAD, SZOMB: The signal will never be noticed.
974: */
975: goto out;
976: #else /* ! __HAVE_AST_PERPROC */
1.99 thorpej 977: case SONPROC:
978: /*
979: * We're running; notice the signal.
980: */
981: signotify(p);
982: goto out;
983:
1.29 cgd 984: default:
985: /*
1.99 thorpej 986: * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
1.29 cgd 987: * It will either never be noticed, or noticed very soon.
988: */
989: goto out;
1.110 thorpej 990: #endif /* __HAVE_AST_PERPROC */
1.29 cgd 991: }
992: /*NOTREACHED*/
993:
1.112 lukem 994: runfast:
1.29 cgd 995: /*
996: * Raise priority to at least PUSER.
997: */
998: if (p->p_priority > PUSER)
999: p->p_priority = PUSER;
1.112 lukem 1000: run:
1.104 thorpej 1001: setrunnable(p); /* XXXSMP: recurse? */
1.112 lukem 1002: out:
1.104 thorpej 1003: /* XXXSMP: works, but icky */
1004: if (dolock)
1005: SCHED_UNLOCK(s);
1.29 cgd 1006: }
1007:
1.112 lukem 1008: static __inline int firstsig(const sigset_t *);
1.79 mycroft 1009:
1010: static __inline int
1.112 lukem 1011: firstsig(const sigset_t *ss)
1.79 mycroft 1012: {
1013: int sig;
1014:
1015: sig = ffs(ss->__bits[0]);
1016: if (sig != 0)
1017: return (sig);
1018: #if NSIG > 33
1019: sig = ffs(ss->__bits[1]);
1020: if (sig != 0)
1021: return (sig + 32);
1022: #endif
1023: #if NSIG > 65
1024: sig = ffs(ss->__bits[2]);
1025: if (sig != 0)
1026: return (sig + 64);
1027: #endif
1028: #if NSIG > 97
1029: sig = ffs(ss->__bits[3]);
1030: if (sig != 0)
1031: return (sig + 96);
1032: #endif
1033: return (0);
1034: }
1035:
1.29 cgd 1036: /*
1037: * If the current process has received a signal (should be caught or cause
1038: * termination, should interrupt current syscall), return the signal number.
1039: * Stop signals with default action are processed immediately, then cleared;
1040: * they aren't returned. This is checked after each entry to the system for
1041: * a syscall or trap (though this can usually be done without calling issignal
1042: * by checking the pending signal masks in the CURSIG macro.) The normal call
1043: * sequence is
1044: *
1045: * while (signum = CURSIG(curproc))
1046: * postsig(signum);
1047: */
1048: int
1.112 lukem 1049: issignal(struct proc *p)
1.29 cgd 1050: {
1.112 lukem 1051: int s, signum, prop;
1.119 christos 1052: int dolock = (p->p_flag & P_SINTR) == 0, locked = !dolock;
1.112 lukem 1053: sigset_t ss;
1.29 cgd 1054:
1055: for (;;) {
1.79 mycroft 1056: sigpending1(p, &ss);
1.29 cgd 1057: if (p->p_flag & P_PPWAIT)
1.79 mycroft 1058: sigminusset(&stopsigmask, &ss);
1059: signum = firstsig(&ss);
1060: if (signum == 0) { /* no signal to send */
1.109 jdolecek 1061: p->p_sigctx.ps_sigcheck = 0;
1.119 christos 1062: if (locked && dolock)
1063: SCHED_LOCK(s);
1.29 cgd 1064: return (0);
1.79 mycroft 1065: }
1.112 lukem 1066: /* take the signal! */
1067: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.42 mycroft 1068:
1.29 cgd 1069: /*
1070: * We should see pending but ignored signals
1071: * only if P_TRACED was on when they were posted.
1072: */
1.109 jdolecek 1073: if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1.79 mycroft 1074: (p->p_flag & P_TRACED) == 0)
1.29 cgd 1075: continue;
1.42 mycroft 1076:
1.29 cgd 1077: if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1078: /*
1079: * If traced, always stop, and stay
1080: * stopped until released by the debugger.
1081: */
1082: p->p_xstat = signum;
1.66 mycroft 1083: if ((p->p_flag & P_FSTRACE) == 0)
1.119 christos 1084: psignal1(p->p_pptr, SIGCHLD, dolock);
1085: if (dolock)
1086: SCHED_LOCK(s);
1.114 nathanw 1087: proc_stop(p);
1088: mi_switch(p);
1089: SCHED_ASSERT_UNLOCKED();
1.119 christos 1090: if (dolock)
1091: splx(s);
1092: else
1093: dolock = 1;
1.29 cgd 1094:
1095: /*
1.42 mycroft 1096: * If we are no longer being traced, or the parent
1097: * didn't give us a signal, look for more signals.
1.29 cgd 1098: */
1.42 mycroft 1099: if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1.29 cgd 1100: continue;
1101:
1102: /*
1.42 mycroft 1103: * If the new signal is being masked, look for other
1104: * signals.
1.29 cgd 1105: */
1.42 mycroft 1106: signum = p->p_xstat;
1.112 lukem 1107: /*
1108: * `p->p_sigctx.ps_siglist |= mask' is done
1109: * in setrunnable().
1110: */
1.109 jdolecek 1111: if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1.29 cgd 1112: continue;
1.112 lukem 1113: /* take the signal! */
1114: sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29 cgd 1115: }
1116:
1.42 mycroft 1117: prop = sigprop[signum];
1118:
1.29 cgd 1119: /*
1120: * Decide whether the signal should be returned.
1121: * Return the signal's number, or fall through
1122: * to clear it from the pending mask.
1123: */
1.109 jdolecek 1124: switch ((long)SIGACTION(p, signum).sa_handler) {
1.29 cgd 1125:
1.33 cgd 1126: case (long)SIG_DFL:
1.29 cgd 1127: /*
1128: * Don't take default actions on system processes.
1129: */
1130: if (p->p_pid <= 1) {
1131: #ifdef DIAGNOSTIC
1132: /*
1133: * Are you sure you want to ignore SIGSEGV
1134: * in init? XXX
1135: */
1.57 christos 1136: printf("Process (pid %d) got signal %d\n",
1.29 cgd 1137: p->p_pid, signum);
1138: #endif
1139: break; /* == ignore */
1140: }
1141: /*
1142: * If there is a pending stop signal to process
1143: * with default action, stop here,
1144: * then clear the signal. However,
1145: * if process is member of an orphaned
1146: * process group, ignore tty stop signals.
1147: */
1148: if (prop & SA_STOP) {
1149: if (p->p_flag & P_TRACED ||
1150: (p->p_pgrp->pg_jobc == 0 &&
1151: prop & SA_TTYSTOP))
1152: break; /* == ignore */
1153: p->p_xstat = signum;
1154: if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1.119 christos 1155: psignal1(p->p_pptr, SIGCHLD, dolock);
1156: if (dolock)
1157: SCHED_LOCK(s);
1.104 thorpej 1158: proc_stop(p);
1.100 sommerfe 1159: mi_switch(p);
1.104 thorpej 1160: SCHED_ASSERT_UNLOCKED();
1.119 christos 1161: if (dolock)
1162: splx(s);
1163: else
1164: dolock = 1;
1.29 cgd 1165: break;
1166: } else if (prop & SA_IGNORE) {
1167: /*
1168: * Except for SIGCONT, shouldn't get here.
1169: * Default action is to ignore; drop it.
1170: */
1171: break; /* == ignore */
1172: } else
1.42 mycroft 1173: goto keep;
1.29 cgd 1174: /*NOTREACHED*/
1175:
1.33 cgd 1176: case (long)SIG_IGN:
1.29 cgd 1177: /*
1178: * Masking above should prevent us ever trying
1179: * to take action on an ignored signal other
1180: * than SIGCONT, unless process is traced.
1181: */
1182: if ((prop & SA_CONT) == 0 &&
1183: (p->p_flag & P_TRACED) == 0)
1.57 christos 1184: printf("issignal\n");
1.29 cgd 1185: break; /* == ignore */
1186:
1187: default:
1188: /*
1189: * This signal has an action, let
1190: * postsig() process it.
1191: */
1.42 mycroft 1192: goto keep;
1.29 cgd 1193: }
1194: }
1195: /* NOTREACHED */
1.42 mycroft 1196:
1.112 lukem 1197: keep:
1198: /* leave the signal for later */
1199: sigaddset(&p->p_sigctx.ps_siglist, signum);
1.110 thorpej 1200: CHECKSIGS(p);
1.119 christos 1201: if (locked && dolock)
1202: SCHED_LOCK(s);
1.42 mycroft 1203: return (signum);
1.29 cgd 1204: }
1205:
1206: /*
1207: * Put the argument process into the stopped state and notify the parent
1208: * via wakeup. Signals are handled elsewhere. The process must not be
1209: * on the run queue.
1210: */
1.104 thorpej 1211: static void
1.112 lukem 1212: proc_stop(struct proc *p)
1.29 cgd 1213: {
1214:
1.104 thorpej 1215: SCHED_ASSERT_LOCKED();
1216:
1.29 cgd 1217: p->p_stat = SSTOP;
1218: p->p_flag &= ~P_WAITED;
1.104 thorpej 1219: sched_wakeup((caddr_t)p->p_pptr);
1.29 cgd 1220: }
1221:
1222: /*
1223: * Take the action for the specified signal
1224: * from the current set of pending signals.
1225: */
1226: void
1.112 lukem 1227: postsig(int signum)
1.29 cgd 1228: {
1.112 lukem 1229: struct proc *p;
1230: struct sigacts *ps;
1231: sig_t action;
1232: u_long code;
1233: sigset_t *returnmask;
1.29 cgd 1234:
1.112 lukem 1235: p = curproc;
1236: ps = p->p_sigacts;
1.29 cgd 1237: #ifdef DIAGNOSTIC
1238: if (signum == 0)
1239: panic("postsig");
1240: #endif
1.106 thorpej 1241:
1242: KERNEL_PROC_LOCK(p);
1243:
1.109 jdolecek 1244: sigdelset(&p->p_sigctx.ps_siglist, signum);
1245: action = SIGACTION_PS(ps, signum).sa_handler;
1.29 cgd 1246: #ifdef KTRACE
1247: if (KTRPOINT(p, KTR_PSIG))
1.100 sommerfe 1248: ktrpsig(p,
1.109 jdolecek 1249: signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1250: &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1.29 cgd 1251: #endif
1252: if (action == SIG_DFL) {
1253: /*
1254: * Default action, where the default is to kill
1255: * the process. (Other cases were ignored above.)
1256: */
1257: sigexit(p, signum);
1258: /* NOTREACHED */
1259: } else {
1260: /*
1261: * If we get here, the signal must be caught.
1262: */
1263: #ifdef DIAGNOSTIC
1.112 lukem 1264: if (action == SIG_IGN ||
1265: sigismember(&p->p_sigctx.ps_sigmask, signum))
1.29 cgd 1266: panic("postsig action");
1267: #endif
1268: /*
1269: * Set the new mask value and also defer further
1270: * occurences of this signal.
1271: *
1272: * Special case: user has done a sigpause. Here the
1273: * current mask is not of interest, but rather the
1274: * mask from before the sigpause is what we want
1275: * restored after the signal processing is completed.
1276: */
1.109 jdolecek 1277: if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1278: returnmask = &p->p_sigctx.ps_oldmask;
1279: p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1.29 cgd 1280: } else
1.109 jdolecek 1281: returnmask = &p->p_sigctx.ps_sigmask;
1.29 cgd 1282: p->p_stats->p_ru.ru_nsignals++;
1.109 jdolecek 1283: if (p->p_sigctx.ps_sig != signum) {
1.29 cgd 1284: code = 0;
1285: } else {
1.109 jdolecek 1286: code = p->p_sigctx.ps_code;
1287: p->p_sigctx.ps_code = 0;
1288: p->p_sigctx.ps_sig = 0;
1.29 cgd 1289: }
1.121 ! thorpej 1290: (*p->p_emul->e_sendsig)(signum, returnmask, code);
1.105 thorpej 1291: (void) splsched(); /* XXXSMP */
1.112 lukem 1292: sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1293: &p->p_sigctx.ps_sigmask);
1.109 jdolecek 1294: if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1295: sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79 mycroft 1296: if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1.109 jdolecek 1297: sigaddset(&p->p_sigctx.ps_sigignore, signum);
1298: SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.79 mycroft 1299: }
1.104 thorpej 1300: (void) spl0(); /* XXXSMP */
1.29 cgd 1301: }
1.106 thorpej 1302:
1303: KERNEL_PROC_UNLOCK(p);
1.29 cgd 1304: }
1305:
1306: /*
1307: * Kill the current process for stated reason.
1308: */
1.52 christos 1309: void
1.112 lukem 1310: killproc(struct proc *p, char *why)
1.29 cgd 1311: {
1312:
1313: log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1314: uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1315: psignal(p, SIGKILL);
1316: }
1317:
1318: /*
1319: * Force the current process to exit with the specified signal, dumping core
1320: * if appropriate. We bypass the normal tests for masked and caught signals,
1321: * allowing unrecoverable failures to terminate the process without changing
1322: * signal state. Mark the accounting record with the signal termination.
1323: * If dumping core, save the signal number for the debugger. Calls exit and
1324: * does not return.
1325: */
1.96 fair 1326:
1.97 fair 1327: #if defined(DEBUG)
1.96 fair 1328: int kern_logsigexit = 1; /* not static to make public for sysctl */
1329: #else
1330: int kern_logsigexit = 0; /* not static to make public for sysctl */
1331: #endif
1332:
1.102 sommerfe 1333: static const char logcoredump[] =
1.96 fair 1334: "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1.102 sommerfe 1335: static const char lognocoredump[] =
1.96 fair 1336: "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1337:
1.52 christos 1338: void
1.112 lukem 1339: sigexit(struct proc *p, int signum)
1.29 cgd 1340: {
1.112 lukem 1341: int error, exitsig;
1.96 fair 1342:
1.112 lukem 1343: exitsig = signum;
1.29 cgd 1344: p->p_acflag |= AXSIG;
1345: if (sigprop[signum] & SA_CORE) {
1.109 jdolecek 1346: p->p_sigctx.ps_sig = signum;
1.102 sommerfe 1347: if ((error = coredump(p)) == 0)
1348: exitsig |= WCOREFLAG;
1349:
1350: if (kern_logsigexit) {
1351: int uid = p->p_cred && p->p_ucred ?
1352: p->p_ucred->cr_uid : -1;
1353:
1354: if (error)
1355: log(LOG_INFO, lognocoredump, p->p_pid,
1356: p->p_comm, uid, signum, error);
1357: else
1358: log(LOG_INFO, logcoredump, p->p_pid,
1359: p->p_comm, uid, signum);
1.96 fair 1360: }
1361:
1.29 cgd 1362: }
1.96 fair 1363:
1.102 sommerfe 1364: exit1(p, W_EXITCODE(0, exitsig));
1.29 cgd 1365: /* NOTREACHED */
1366: }
1367:
1368: /*
1.75 nathanw 1369: * Dump core, into a file named "progname.core" or "core" (depending on the
1370: * value of shortcorename), unless the process was setuid/setgid.
1.29 cgd 1371: */
1372: int
1.112 lukem 1373: coredump(struct proc *p)
1.29 cgd 1374: {
1.112 lukem 1375: struct vnode *vp;
1376: struct vmspace *vm;
1377: struct ucred *cred;
1378: struct nameidata nd;
1379: struct vattr vattr;
1380: int error, error1;
1381: char name[MAXPATHLEN];
1382:
1383: vm = p->p_vmspace;
1384: cred = p->p_cred->pc_ucred;
1.29 cgd 1385:
1.59 cgd 1386: /*
1387: * Make sure the process has not set-id, to prevent data leaks.
1388: */
1.58 mrg 1389: if (p->p_flag & P_SUGID)
1.59 cgd 1390: return (EPERM);
1391:
1392: /*
1393: * Refuse to core if the data + stack + user size is larger than
1394: * the core dump limit. XXX THIS IS WRONG, because of mapped
1395: * data.
1396: */
1.30 deraadt 1397: if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1.29 cgd 1398: p->p_rlimit[RLIMIT_CORE].rlim_cur)
1.59 cgd 1399: return (EFBIG); /* better error code? */
1400:
1401: /*
1402: * The core dump will go in the current working directory. Make
1.80 pk 1403: * sure that the directory is still there and that the mount flags
1404: * allow us to write core dumps there.
1.59 cgd 1405: */
1.88 thorpej 1406: vp = p->p_cwdi->cwdi_cdir;
1.80 pk 1407: if (vp->v_mount == NULL ||
1408: (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1.59 cgd 1409: return (EPERM);
1410:
1.100 sommerfe 1411: error = build_corename(p, name);
1.94 bouyer 1412: if (error)
1413: return error;
1414:
1.93 bouyer 1415: NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1416: error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
1.52 christos 1417: if (error)
1.29 cgd 1418: return (error);
1419: vp = nd.ni_vp;
1420:
1421: /* Don't dump to non-regular files or files with links. */
1422: if (vp->v_type != VREG ||
1423: VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1.59 cgd 1424: error = EINVAL;
1.29 cgd 1425: goto out;
1426: }
1427: VATTR_NULL(&vattr);
1428: vattr.va_size = 0;
1.37 mycroft 1429: VOP_LEASE(vp, p, cred, LEASE_WRITE);
1.29 cgd 1430: VOP_SETATTR(vp, &vattr, cred, p);
1431: p->p_acflag |= ACORE;
1.95 eeh 1432:
1.118 thorpej 1433: /* Now dump the actual core file. */
1434: error = (*p->p_execsw->es_coredump)(p, vp, cred);
1.112 lukem 1435: out:
1.71 fvdl 1436: VOP_UNLOCK(vp, 0);
1.29 cgd 1437: error1 = vn_close(vp, FWRITE, cred, p);
1438: if (error == 0)
1439: error = error1;
1440: return (error);
1441: }
1442:
1443: /*
1444: * Nonexistent system call-- signal process (may want to handle it).
1445: * Flag error in case process won't see signal immediately (blocked or ignored).
1446: */
1447: /* ARGSUSED */
1448: int
1.112 lukem 1449: sys_nosys(struct proc *p, void *v, register_t *retval)
1.29 cgd 1450: {
1451:
1452: psignal(p, SIGSYS);
1.36 cgd 1453: return (ENOSYS);
1.94 bouyer 1454: }
1455:
1456: static int
1.112 lukem 1457: build_corename(struct proc *p, char dst[MAXPATHLEN])
1.94 bouyer 1458: {
1.112 lukem 1459: const char *s;
1460: char *d, *end;
1461: int i;
1.100 sommerfe 1462:
1.107 enami 1463: for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1.94 bouyer 1464: *s != '\0'; s++) {
1465: if (*s == '%') {
1.107 enami 1466: switch (*(s + 1)) {
1.94 bouyer 1467: case 'n':
1.107 enami 1468: i = snprintf(d, end - d, "%s", p->p_comm);
1.94 bouyer 1469: break;
1470: case 'p':
1.107 enami 1471: i = snprintf(d, end - d, "%d", p->p_pid);
1.94 bouyer 1472: break;
1473: case 'u':
1.107 enami 1474: i = snprintf(d, end - d, "%s",
1.100 sommerfe 1475: p->p_pgrp->pg_session->s_login);
1.94 bouyer 1476: break;
1477: case 't':
1.107 enami 1478: i = snprintf(d, end - d, "%ld",
1.100 sommerfe 1479: p->p_stats->p_start.tv_sec);
1.94 bouyer 1480: break;
1481: default:
1482: goto copy;
1483: }
1484: d += i;
1485: s++;
1486: } else {
1.112 lukem 1487: copy: *d = *s;
1.94 bouyer 1488: d++;
1489: }
1.107 enami 1490: if (d >= end)
1491: return (ENAMETOOLONG);
1.94 bouyer 1492: }
1493: *d = '\0';
1.107 enami 1494: return (0);
1.108 jdolecek 1495: }
1496:
1497: /*
1498: * Returns true if signal is ignored or masked for passed process.
1499: */
1500: int
1.112 lukem 1501: sigismasked(struct proc *p, int sig)
1.108 jdolecek 1502: {
1.112 lukem 1503:
1.117 enami 1504: return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1505: sigismember(&p->p_sigctx.ps_sigmask, sig));
1.29 cgd 1506: }
CVSweb <webmaster@jp.NetBSD.org>