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