1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 425591b823SEivind Eklund #include "opt_compat.h" 43db6a20e2SGarrett Wollman #include "opt_ktrace.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46c87e2930SDavid Greenman #include <sys/kernel.h> 47d2d3e875SBruce Evans #include <sys/sysproto.h> 4836240ea5SDoug Rabson #include <sys/systm.h> 49df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/namei.h> 51df8bae1dSRodney W. Grimes #include <sys/vnode.h> 52cb679c38SJonathan Lemon #include <sys/event.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 542a024a2bSSean Eric Fagan #include <sys/pioctl.h> 55df8bae1dSRodney W. Grimes #include <sys/acct.h> 563ac4d1efSBruce Evans #include <sys/fcntl.h> 57238510fcSJason Evans #include <sys/condvar.h> 58c31146a1SJohn Baldwin #include <sys/lock.h> 5935e0e5b3SJohn Baldwin #include <sys/mutex.h> 60df8bae1dSRodney W. Grimes #include <sys/wait.h> 610384fff8SJason Evans #include <sys/ktr.h> 62df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 63c31146a1SJohn Baldwin #include <sys/resourcevar.h> 646caa8a15SJohn Baldwin #include <sys/smp.h> 65df8bae1dSRodney W. Grimes #include <sys/stat.h> 661005a129SJohn Baldwin #include <sys/sx.h> 671005a129SJohn Baldwin #include <sys/syslog.h> 68d66a5066SPeter Wemm #include <sys/sysent.h> 69c87e2930SDavid Greenman #include <sys/sysctl.h> 70c5edb423SSean Eric Fagan #include <sys/malloc.h> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <machine/cpu.h> 73df8bae1dSRodney W. Grimes 746f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 756f841fb7SMarcel Moolenaar 766f841fb7SMarcel Moolenaar static int coredump __P((struct proc *)); 772c42a146SMarcel Moolenaar static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act, 78645682fdSLuoqi Chen struct sigaction *oact, int old)); 792c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set, 80645682fdSLuoqi Chen sigset_t *oset, int old)); 81f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t)); 826f841fb7SMarcel Moolenaar static int killpg1 __P((struct proc *cp, int sig, int pgid, int all)); 836f841fb7SMarcel Moolenaar static int sig_ffs __P((sigset_t *set)); 846f841fb7SMarcel Moolenaar static int sigprop __P((int sig)); 856f841fb7SMarcel Moolenaar static void stop __P((struct proc *)); 8626f9a767SRodney W. Grimes 87cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 88cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 89cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 90cb679c38SJonathan Lemon 91cb679c38SJonathan Lemon struct filterops sig_filtops = 92cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 93cb679c38SJonathan Lemon 9457308494SJoerg Wunsch static int kern_logsigexit = 1; 953d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 963d177f46SBill Fumerola &kern_logsigexit, 0, 973d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 9857308494SJoerg Wunsch 99df8bae1dSRodney W. Grimes /* 100831d27a9SDon Lewis * Policy -- Can real uid ruid with ucred uc send a signal to process q? 101831d27a9SDon Lewis */ 102831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \ 103831d27a9SDon Lewis ((uc)->cr_uid == 0 || \ 104831d27a9SDon Lewis (ruid) == (q)->p_cred->p_ruid || \ 105831d27a9SDon Lewis (uc)->cr_uid == (q)->p_cred->p_ruid || \ 106831d27a9SDon Lewis (ruid) == (q)->p_ucred->cr_uid || \ 107831d27a9SDon Lewis (uc)->cr_uid == (q)->p_ucred->cr_uid) 108831d27a9SDon Lewis 10922d4b0fbSJohn Polstra int sugid_coredump; 1103d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1113d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 112c87e2930SDavid Greenman 113e5a28db9SPaul Saab static int do_coredump = 1; 114e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 115e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 116e5a28db9SPaul Saab 1172c42a146SMarcel Moolenaar /* 1182c42a146SMarcel Moolenaar * Signal properties and actions. 1192c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1202c42a146SMarcel Moolenaar * according to the following properties: 1212c42a146SMarcel Moolenaar */ 1222c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1232c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1242c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1252c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1262c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1272c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1282c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 129df8bae1dSRodney W. Grimes 1302c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1312c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1322c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1332c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1342c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1352c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1362c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1372c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1382c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1392c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1402c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1412c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1422c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1432c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1442c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1452c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1462c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1472c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1482c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1492c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1502c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1512c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1522c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1532c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1542c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1552c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1562c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1572c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1582c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1592c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1602c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1612c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1622c42a146SMarcel Moolenaar }; 1632c42a146SMarcel Moolenaar 164fbbeeb6cSBruce Evans /* 165fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 166fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 167fbbeeb6cSBruce Evans * action, the process stops in issignal(). 168fbbeeb6cSBruce Evans * 16933510ef1SBruce Evans * MP SAFE. 170fbbeeb6cSBruce Evans */ 171fbbeeb6cSBruce Evans int 172fbbeeb6cSBruce Evans CURSIG(struct proc *p) 173fbbeeb6cSBruce Evans { 174fbbeeb6cSBruce Evans sigset_t tmpset; 175628d2653SJohn Baldwin int r = 0; 176fbbeeb6cSBruce Evans 177628d2653SJohn Baldwin PROC_LOCK(p); 17833510ef1SBruce Evans if (SIGISEMPTY(p->p_siglist)) 179628d2653SJohn Baldwin goto out; 180fbbeeb6cSBruce Evans tmpset = p->p_siglist; 181fbbeeb6cSBruce Evans SIGSETNAND(tmpset, p->p_sigmask); 18233510ef1SBruce Evans if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0) 183628d2653SJohn Baldwin goto out; 184fbbeeb6cSBruce Evans r = issignal(p); 185628d2653SJohn Baldwin out: 186628d2653SJohn Baldwin PROC_UNLOCK(p); 187fbbeeb6cSBruce Evans return (r); 188fbbeeb6cSBruce Evans } 189fbbeeb6cSBruce Evans 1906f841fb7SMarcel Moolenaar static __inline int 1916f841fb7SMarcel Moolenaar sigprop(int sig) 1922c42a146SMarcel Moolenaar { 1936f841fb7SMarcel Moolenaar 1942c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 1952c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 1962c42a146SMarcel Moolenaar return (0); 197df8bae1dSRodney W. Grimes } 1982c42a146SMarcel Moolenaar 1996f841fb7SMarcel Moolenaar static __inline int 2006f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2012c42a146SMarcel Moolenaar { 2022c42a146SMarcel Moolenaar int i; 2032c42a146SMarcel Moolenaar 2046f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2052c42a146SMarcel Moolenaar if (set->__bits[i]) 2062c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 207df8bae1dSRodney W. Grimes return (0); 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes 2102c42a146SMarcel Moolenaar /* 2112c42a146SMarcel Moolenaar * do_sigaction 2122c42a146SMarcel Moolenaar * sigaction 2132c42a146SMarcel Moolenaar * osigaction 2142c42a146SMarcel Moolenaar */ 2152c42a146SMarcel Moolenaar static int 216645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old) 2172c42a146SMarcel Moolenaar struct proc *p; 2182c42a146SMarcel Moolenaar register int sig; 2192c42a146SMarcel Moolenaar struct sigaction *act, *oact; 220645682fdSLuoqi Chen int old; 221df8bae1dSRodney W. Grimes { 222628d2653SJohn Baldwin register struct sigacts *ps; 223df8bae1dSRodney W. Grimes 2242c42a146SMarcel Moolenaar if (sig <= 0 || sig > _SIG_MAXSIG) 2252c42a146SMarcel Moolenaar return (EINVAL); 2262c42a146SMarcel Moolenaar 227628d2653SJohn Baldwin PROC_LOCK(p); 228628d2653SJohn Baldwin ps = p->p_sigacts; 2292c42a146SMarcel Moolenaar if (oact) { 2302c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2312c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2322c42a146SMarcel Moolenaar oact->sa_flags = 0; 2332c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2342c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2352c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2362c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2372c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2382c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2392c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2402c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2412c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2422c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 243645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2442c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 245645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2462c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2472c42a146SMarcel Moolenaar } 2482c42a146SMarcel Moolenaar if (act) { 2492c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 250628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 251628d2653SJohn Baldwin PROC_UNLOCK(p); 2522c42a146SMarcel Moolenaar return (EINVAL); 253628d2653SJohn Baldwin } 2542c42a146SMarcel Moolenaar 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * Change setting atomically. 257df8bae1dSRodney W. Grimes */ 2582c42a146SMarcel Moolenaar 2592c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2602c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2612c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 2622c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2632c42a146SMarcel Moolenaar SIGADDSET(ps->ps_siginfo, sig); 2646f841fb7SMarcel Moolenaar } else { 2652c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = 2662c42a146SMarcel Moolenaar (__sighandler_t *)act->sa_sigaction; 2672c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2682c42a146SMarcel Moolenaar } 2692c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2702c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 271df8bae1dSRodney W. Grimes else 2722c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2732c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2742c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 275df8bae1dSRodney W. Grimes else 2762c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2772c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2782c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2791e41c1b5SSteven Wallace else 2802c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 2812c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 2822c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 283289ccde0SPeter Wemm else 2842c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 285df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 2862c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 2872c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 288df8bae1dSRodney W. Grimes else 2892c42a146SMarcel Moolenaar SIGDELSET(ps->ps_usertramp, seg); 290df8bae1dSRodney W. Grimes #endif 2912c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 2922c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 293645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 2946626c604SJulian Elischer else 295645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 2962c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDWAIT) { 297245f17d4SJoerg Wunsch /* 2982c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 2992c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3002c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3012c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 302245f17d4SJoerg Wunsch */ 303245f17d4SJoerg Wunsch if (p->p_pid == 1) 304645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3056626c604SJulian Elischer else 306645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 307245f17d4SJoerg Wunsch } else 308645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 309df8bae1dSRodney W. Grimes } 310df8bae1dSRodney W. Grimes /* 311df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3122c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3132c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3142c42a146SMarcel Moolenaar * have to restart the process. 315df8bae1dSRodney W. Grimes */ 3162c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3172c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3182c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3192c42a146SMarcel Moolenaar /* never to be seen again */ 3202c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3212c42a146SMarcel Moolenaar if (sig != SIGCONT) 3222c42a146SMarcel Moolenaar /* easier in psignal */ 3232c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3242c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 325645682fdSLuoqi Chen } else { 3262c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3272c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3282c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3292c42a146SMarcel Moolenaar else 3302c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3312c42a146SMarcel Moolenaar } 332645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 333645682fdSLuoqi Chen ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 334645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 335645682fdSLuoqi Chen else 336645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 337df8bae1dSRodney W. Grimes } 338628d2653SJohn Baldwin PROC_UNLOCK(p); 3392c42a146SMarcel Moolenaar return (0); 3402c42a146SMarcel Moolenaar } 3412c42a146SMarcel Moolenaar 3422c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3432c42a146SMarcel Moolenaar struct sigaction_args { 3442c42a146SMarcel Moolenaar int sig; 3452c42a146SMarcel Moolenaar struct sigaction *act; 3462c42a146SMarcel Moolenaar struct sigaction *oact; 3472c42a146SMarcel Moolenaar }; 3482c42a146SMarcel Moolenaar #endif 3492c42a146SMarcel Moolenaar /* ARGSUSED */ 3502c42a146SMarcel Moolenaar int 3512c42a146SMarcel Moolenaar sigaction(p, uap) 3522c42a146SMarcel Moolenaar struct proc *p; 3532c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3542c42a146SMarcel Moolenaar { 3552c42a146SMarcel Moolenaar struct sigaction act, oact; 3562c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3572c42a146SMarcel Moolenaar int error; 3582c42a146SMarcel Moolenaar 3596f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3606f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3612c42a146SMarcel Moolenaar if (actp) { 3626f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3632c42a146SMarcel Moolenaar if (error) 3642c42a146SMarcel Moolenaar return (error); 3652c42a146SMarcel Moolenaar } 366645682fdSLuoqi Chen error = do_sigaction(p, uap->sig, actp, oactp, 0); 3672c42a146SMarcel Moolenaar if (oactp && !error) { 3686f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 3692c42a146SMarcel Moolenaar } 3702c42a146SMarcel Moolenaar return (error); 3712c42a146SMarcel Moolenaar } 3722c42a146SMarcel Moolenaar 37331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 3742c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3752c42a146SMarcel Moolenaar struct osigaction_args { 3762c42a146SMarcel Moolenaar int signum; 3772c42a146SMarcel Moolenaar struct osigaction *nsa; 3782c42a146SMarcel Moolenaar struct osigaction *osa; 3792c42a146SMarcel Moolenaar }; 3802c42a146SMarcel Moolenaar #endif 3812c42a146SMarcel Moolenaar /* ARGSUSED */ 3822c42a146SMarcel Moolenaar int 3832c42a146SMarcel Moolenaar osigaction(p, uap) 3842c42a146SMarcel Moolenaar struct proc *p; 3852c42a146SMarcel Moolenaar register struct osigaction_args *uap; 3862c42a146SMarcel Moolenaar { 3872c42a146SMarcel Moolenaar struct osigaction sa; 3882c42a146SMarcel Moolenaar struct sigaction nsa, osa; 3892c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 3902c42a146SMarcel Moolenaar int error; 3912c42a146SMarcel Moolenaar 3926f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 3936f841fb7SMarcel Moolenaar return (EINVAL); 3946f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 3956f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 3962c42a146SMarcel Moolenaar if (nsap) { 3976f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 3982c42a146SMarcel Moolenaar if (error) 3992c42a146SMarcel Moolenaar return (error); 4002c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4012c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4022c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4032c42a146SMarcel Moolenaar } 404645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 4052c42a146SMarcel Moolenaar if (osap && !error) { 4062c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4072c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4082c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4096f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4102c42a146SMarcel Moolenaar } 4112c42a146SMarcel Moolenaar return (error); 4122c42a146SMarcel Moolenaar } 41331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes /* 416df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 417df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 418df8bae1dSRodney W. Grimes */ 419df8bae1dSRodney W. Grimes void 420df8bae1dSRodney W. Grimes siginit(p) 421df8bae1dSRodney W. Grimes struct proc *p; 422df8bae1dSRodney W. Grimes { 423df8bae1dSRodney W. Grimes register int i; 424df8bae1dSRodney W. Grimes 425628d2653SJohn Baldwin PROC_LOCK(p); 4262c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4272c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4282c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 429628d2653SJohn Baldwin PROC_UNLOCK(p); 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes /* 433df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 434df8bae1dSRodney W. Grimes */ 435df8bae1dSRodney W. Grimes void 436df8bae1dSRodney W. Grimes execsigs(p) 437df8bae1dSRodney W. Grimes register struct proc *p; 438df8bae1dSRodney W. Grimes { 439628d2653SJohn Baldwin register struct sigacts *ps; 4402c42a146SMarcel Moolenaar register int sig; 441df8bae1dSRodney W. Grimes 442df8bae1dSRodney W. Grimes /* 443df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 444df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 445df8bae1dSRodney W. Grimes * and are now ignored by default). 446df8bae1dSRodney W. Grimes */ 447628d2653SJohn Baldwin PROC_LOCK(p); 448628d2653SJohn Baldwin ps = p->p_sigacts; 4492c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4502c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4512c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4522c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4532c42a146SMarcel Moolenaar if (sig != SIGCONT) 4542c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4552c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 456df8bae1dSRodney W. Grimes } 4572c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 458df8bae1dSRodney W. Grimes } 459df8bae1dSRodney W. Grimes /* 460df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 461df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 462df8bae1dSRodney W. Grimes */ 463645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 464645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 465645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 4663b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 46780e907a1SPeter Wemm /* 46880e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 46980e907a1SPeter Wemm */ 470645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 471628d2653SJohn Baldwin PROC_UNLOCK(p); 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes 474df8bae1dSRodney W. Grimes /* 475628d2653SJohn Baldwin * do_sigprocmask() 4767c8fdcbdSMatthew Dillon * 477628d2653SJohn Baldwin * Manipulate signal mask. 478df8bae1dSRodney W. Grimes */ 4792c42a146SMarcel Moolenaar static int 480645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 4812c42a146SMarcel Moolenaar struct proc *p; 4822c42a146SMarcel Moolenaar int how; 4832c42a146SMarcel Moolenaar sigset_t *set, *oset; 484645682fdSLuoqi Chen int old; 4852c42a146SMarcel Moolenaar { 4862c42a146SMarcel Moolenaar int error; 4872c42a146SMarcel Moolenaar 488628d2653SJohn Baldwin PROC_LOCK(p); 4892c42a146SMarcel Moolenaar if (oset != NULL) 4902c42a146SMarcel Moolenaar *oset = p->p_sigmask; 4912c42a146SMarcel Moolenaar 4922c42a146SMarcel Moolenaar error = 0; 4932c42a146SMarcel Moolenaar if (set != NULL) { 4942c42a146SMarcel Moolenaar switch (how) { 4952c42a146SMarcel Moolenaar case SIG_BLOCK: 496645682fdSLuoqi Chen SIG_CANTMASK(*set); 4972c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 4982c42a146SMarcel Moolenaar break; 4992c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5002c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 5012c42a146SMarcel Moolenaar break; 5022c42a146SMarcel Moolenaar case SIG_SETMASK: 503645682fdSLuoqi Chen SIG_CANTMASK(*set); 504645682fdSLuoqi Chen if (old) 505645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 506645682fdSLuoqi Chen else 5072c42a146SMarcel Moolenaar p->p_sigmask = *set; 5082c42a146SMarcel Moolenaar break; 5092c42a146SMarcel Moolenaar default: 5102c42a146SMarcel Moolenaar error = EINVAL; 5112c42a146SMarcel Moolenaar break; 5122c42a146SMarcel Moolenaar } 5132c42a146SMarcel Moolenaar } 514628d2653SJohn Baldwin PROC_UNLOCK(p); 5152c42a146SMarcel Moolenaar return (error); 5162c42a146SMarcel Moolenaar } 5172c42a146SMarcel Moolenaar 5187c8fdcbdSMatthew Dillon /* 5197c8fdcbdSMatthew Dillon * sigprocmask() - MP SAFE 5207c8fdcbdSMatthew Dillon */ 5217c8fdcbdSMatthew Dillon 522d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 523df8bae1dSRodney W. Grimes struct sigprocmask_args { 524df8bae1dSRodney W. Grimes int how; 5252c42a146SMarcel Moolenaar const sigset_t *set; 5262c42a146SMarcel Moolenaar sigset_t *oset; 527df8bae1dSRodney W. Grimes }; 528d2d3e875SBruce Evans #endif 52926f9a767SRodney W. Grimes int 530cb226aaaSPoul-Henning Kamp sigprocmask(p, uap) 531df8bae1dSRodney W. Grimes register struct proc *p; 532df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 533df8bae1dSRodney W. Grimes { 5342c42a146SMarcel Moolenaar sigset_t set, oset; 5352c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5362c42a146SMarcel Moolenaar int error; 537df8bae1dSRodney W. Grimes 5386f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5396f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5402c42a146SMarcel Moolenaar if (setp) { 5416f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5422c42a146SMarcel Moolenaar if (error) 5432c42a146SMarcel Moolenaar return (error); 544df8bae1dSRodney W. Grimes } 545645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5462c42a146SMarcel Moolenaar if (osetp && !error) { 5476f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5482c42a146SMarcel Moolenaar } 5492c42a146SMarcel Moolenaar return (error); 5502c42a146SMarcel Moolenaar } 5512c42a146SMarcel Moolenaar 55231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5537c8fdcbdSMatthew Dillon /* 5547c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 5557c8fdcbdSMatthew Dillon */ 5562c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5572c42a146SMarcel Moolenaar struct osigprocmask_args { 5582c42a146SMarcel Moolenaar int how; 5592c42a146SMarcel Moolenaar osigset_t mask; 5602c42a146SMarcel Moolenaar }; 5612c42a146SMarcel Moolenaar #endif 5622c42a146SMarcel Moolenaar int 5632c42a146SMarcel Moolenaar osigprocmask(p, uap) 5642c42a146SMarcel Moolenaar register struct proc *p; 5652c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 5662c42a146SMarcel Moolenaar { 5672c42a146SMarcel Moolenaar sigset_t set, oset; 5682c42a146SMarcel Moolenaar int error; 5692c42a146SMarcel Moolenaar 5702c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 571645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 5722c42a146SMarcel Moolenaar SIG2OSIG(oset, p->p_retval[0]); 573df8bae1dSRodney W. Grimes return (error); 574df8bae1dSRodney W. Grimes } 57531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 576df8bae1dSRodney W. Grimes 577d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 578df8bae1dSRodney W. Grimes struct sigpending_args { 5792c42a146SMarcel Moolenaar sigset_t *set; 580df8bae1dSRodney W. Grimes }; 581d2d3e875SBruce Evans #endif 582df8bae1dSRodney W. Grimes /* ARGSUSED */ 58326f9a767SRodney W. Grimes int 584cb226aaaSPoul-Henning Kamp sigpending(p, uap) 585df8bae1dSRodney W. Grimes struct proc *p; 586df8bae1dSRodney W. Grimes struct sigpending_args *uap; 587df8bae1dSRodney W. Grimes { 588628d2653SJohn Baldwin sigset_t siglist; 589df8bae1dSRodney W. Grimes 590628d2653SJohn Baldwin PROC_LOCK(p); 591628d2653SJohn Baldwin siglist = p->p_siglist; 592628d2653SJohn Baldwin PROC_UNLOCK(p); 593628d2653SJohn Baldwin return (copyout(&siglist, uap->set, sizeof(sigset_t))); 5942c42a146SMarcel Moolenaar } 5952c42a146SMarcel Moolenaar 59631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5982c42a146SMarcel Moolenaar struct osigpending_args { 5992c42a146SMarcel Moolenaar int dummy; 6002c42a146SMarcel Moolenaar }; 6012c42a146SMarcel Moolenaar #endif 6022c42a146SMarcel Moolenaar /* ARGSUSED */ 6032c42a146SMarcel Moolenaar int 6042c42a146SMarcel Moolenaar osigpending(p, uap) 6052c42a146SMarcel Moolenaar struct proc *p; 6062c42a146SMarcel Moolenaar struct osigpending_args *uap; 6072c42a146SMarcel Moolenaar { 6082c42a146SMarcel Moolenaar 609628d2653SJohn Baldwin PROC_LOCK(p); 6102c42a146SMarcel Moolenaar SIG2OSIG(p->p_siglist, p->p_retval[0]); 611628d2653SJohn Baldwin PROC_UNLOCK(p); 612df8bae1dSRodney W. Grimes return (0); 613df8bae1dSRodney W. Grimes } 61431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 617df8bae1dSRodney W. Grimes /* 618df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 619df8bae1dSRodney W. Grimes */ 620d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 621df8bae1dSRodney W. Grimes struct osigvec_args { 622df8bae1dSRodney W. Grimes int signum; 623df8bae1dSRodney W. Grimes struct sigvec *nsv; 624df8bae1dSRodney W. Grimes struct sigvec *osv; 625df8bae1dSRodney W. Grimes }; 626d2d3e875SBruce Evans #endif 627df8bae1dSRodney W. Grimes /* ARGSUSED */ 62826f9a767SRodney W. Grimes int 629cb226aaaSPoul-Henning Kamp osigvec(p, uap) 630df8bae1dSRodney W. Grimes struct proc *p; 631df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 632df8bae1dSRodney W. Grimes { 633df8bae1dSRodney W. Grimes struct sigvec vec; 6342c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6352c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 6362c42a146SMarcel Moolenaar int error; 637df8bae1dSRodney W. Grimes 6386f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 6396f841fb7SMarcel Moolenaar return (EINVAL); 6406f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 6416f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 6422c42a146SMarcel Moolenaar if (nsap) { 6436f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 6442c42a146SMarcel Moolenaar if (error) 645df8bae1dSRodney W. Grimes return (error); 6462c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 6472c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 6482c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 6492c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 650df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 6512c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 652df8bae1dSRodney W. Grimes #endif 653df8bae1dSRodney W. Grimes } 654645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 6552c42a146SMarcel Moolenaar if (osap && !error) { 6562c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 6572c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 6582c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 6592c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 6602c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 6612c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 6622c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 6632c42a146SMarcel Moolenaar #endif 6646f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 6652c42a146SMarcel Moolenaar } 6662c42a146SMarcel Moolenaar return (error); 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes 669d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 670df8bae1dSRodney W. Grimes struct osigblock_args { 671df8bae1dSRodney W. Grimes int mask; 672df8bae1dSRodney W. Grimes }; 673d2d3e875SBruce Evans #endif 67426f9a767SRodney W. Grimes int 675cb226aaaSPoul-Henning Kamp osigblock(p, uap) 676df8bae1dSRodney W. Grimes register struct proc *p; 677df8bae1dSRodney W. Grimes struct osigblock_args *uap; 678df8bae1dSRodney W. Grimes { 6792c42a146SMarcel Moolenaar sigset_t set; 680df8bae1dSRodney W. Grimes 6812c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 6822c42a146SMarcel Moolenaar SIG_CANTMASK(set); 683628d2653SJohn Baldwin PROC_LOCK(p); 6842c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 6852c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 686628d2653SJohn Baldwin PROC_UNLOCK(p); 687df8bae1dSRodney W. Grimes return (0); 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes 690d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 691df8bae1dSRodney W. Grimes struct osigsetmask_args { 692df8bae1dSRodney W. Grimes int mask; 693df8bae1dSRodney W. Grimes }; 694d2d3e875SBruce Evans #endif 69526f9a767SRodney W. Grimes int 696cb226aaaSPoul-Henning Kamp osigsetmask(p, uap) 697df8bae1dSRodney W. Grimes struct proc *p; 698df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 699df8bae1dSRodney W. Grimes { 7002c42a146SMarcel Moolenaar sigset_t set; 701df8bae1dSRodney W. Grimes 7022c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7032c42a146SMarcel Moolenaar SIG_CANTMASK(set); 704628d2653SJohn Baldwin PROC_LOCK(p); 7052c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 706645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 707628d2653SJohn Baldwin PROC_UNLOCK(p); 708df8bae1dSRodney W. Grimes return (0); 709df8bae1dSRodney W. Grimes } 710df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 711df8bae1dSRodney W. Grimes 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 714df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 715df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 716df8bae1dSRodney W. Grimes */ 717d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 718df8bae1dSRodney W. Grimes struct sigsuspend_args { 7192c42a146SMarcel Moolenaar const sigset_t *sigmask; 720df8bae1dSRodney W. Grimes }; 721d2d3e875SBruce Evans #endif 722df8bae1dSRodney W. Grimes /* ARGSUSED */ 72326f9a767SRodney W. Grimes int 724cb226aaaSPoul-Henning Kamp sigsuspend(p, uap) 725df8bae1dSRodney W. Grimes register struct proc *p; 726df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 727df8bae1dSRodney W. Grimes { 7282c42a146SMarcel Moolenaar sigset_t mask; 729628d2653SJohn Baldwin register struct sigacts *ps; 7302c42a146SMarcel Moolenaar int error; 7312c42a146SMarcel Moolenaar 7326f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 7332c42a146SMarcel Moolenaar if (error) 7342c42a146SMarcel Moolenaar return (error); 735df8bae1dSRodney W. Grimes 736df8bae1dSRodney W. Grimes /* 737645682fdSLuoqi Chen * When returning from sigsuspend, we want 738df8bae1dSRodney W. Grimes * the old mask to be restored after the 739df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 740df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 741df8bae1dSRodney W. Grimes * to indicate this. 742df8bae1dSRodney W. Grimes */ 743628d2653SJohn Baldwin PROC_LOCK(p); 744628d2653SJohn Baldwin ps = p->p_sigacts; 7456626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 746645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 747645682fdSLuoqi Chen 748645682fdSLuoqi Chen SIG_CANTMASK(mask); 7492c42a146SMarcel Moolenaar p->p_sigmask = mask; 750628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 7512c42a146SMarcel Moolenaar /* void */; 752628d2653SJohn Baldwin PROC_UNLOCK(p); 7532c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 7542c42a146SMarcel Moolenaar return (EINTR); 7552c42a146SMarcel Moolenaar } 7562c42a146SMarcel Moolenaar 75731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 7582c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7592c42a146SMarcel Moolenaar struct osigsuspend_args { 7602c42a146SMarcel Moolenaar osigset_t mask; 7612c42a146SMarcel Moolenaar }; 7622c42a146SMarcel Moolenaar #endif 7632c42a146SMarcel Moolenaar /* ARGSUSED */ 7642c42a146SMarcel Moolenaar int 7652c42a146SMarcel Moolenaar osigsuspend(p, uap) 7662c42a146SMarcel Moolenaar register struct proc *p; 7672c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 7682c42a146SMarcel Moolenaar { 769645682fdSLuoqi Chen sigset_t mask; 770628d2653SJohn Baldwin register struct sigacts *ps; 7712c42a146SMarcel Moolenaar 772628d2653SJohn Baldwin PROC_LOCK(p); 773628d2653SJohn Baldwin ps = p->p_sigacts; 7742c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 775645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 776645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 777645682fdSLuoqi Chen SIG_CANTMASK(mask); 778645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 779628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 780df8bae1dSRodney W. Grimes /* void */; 781628d2653SJohn Baldwin PROC_UNLOCK(p); 782df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 783df8bae1dSRodney W. Grimes return (EINTR); 784df8bae1dSRodney W. Grimes } 78531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 788d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 789df8bae1dSRodney W. Grimes struct osigstack_args { 790df8bae1dSRodney W. Grimes struct sigstack *nss; 791df8bae1dSRodney W. Grimes struct sigstack *oss; 792df8bae1dSRodney W. Grimes }; 793d2d3e875SBruce Evans #endif 794df8bae1dSRodney W. Grimes /* ARGSUSED */ 79526f9a767SRodney W. Grimes int 796cb226aaaSPoul-Henning Kamp osigstack(p, uap) 797df8bae1dSRodney W. Grimes struct proc *p; 798df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 799df8bae1dSRodney W. Grimes { 800df8bae1dSRodney W. Grimes struct sigstack ss; 801d034d459SMarcel Moolenaar int error; 802df8bae1dSRodney W. Grimes 803d034d459SMarcel Moolenaar if (uap->oss != NULL) { 804628d2653SJohn Baldwin PROC_LOCK(p); 805645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 806d034d459SMarcel Moolenaar ss.ss_onstack = sigonstack(cpu_getstack(p)); 807628d2653SJohn Baldwin PROC_UNLOCK(p); 808d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 809d034d459SMarcel Moolenaar if (error) 810df8bae1dSRodney W. Grimes return (error); 811d034d459SMarcel Moolenaar } 812d034d459SMarcel Moolenaar 813d034d459SMarcel Moolenaar if (uap->nss != NULL) { 814d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 815d034d459SMarcel Moolenaar return (error); 816628d2653SJohn Baldwin PROC_LOCK(p); 817645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 818645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 819645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 820645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 821628d2653SJohn Baldwin PROC_UNLOCK(p); 822df8bae1dSRodney W. Grimes } 823d034d459SMarcel Moolenaar return (0); 824df8bae1dSRodney W. Grimes } 825df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 826df8bae1dSRodney W. Grimes 827d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 828df8bae1dSRodney W. Grimes struct sigaltstack_args { 8292c42a146SMarcel Moolenaar stack_t *ss; 8302c42a146SMarcel Moolenaar stack_t *oss; 831df8bae1dSRodney W. Grimes }; 832d2d3e875SBruce Evans #endif 833df8bae1dSRodney W. Grimes /* ARGSUSED */ 83426f9a767SRodney W. Grimes int 835cb226aaaSPoul-Henning Kamp sigaltstack(p, uap) 836df8bae1dSRodney W. Grimes struct proc *p; 837df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 838df8bae1dSRodney W. Grimes { 8392c42a146SMarcel Moolenaar stack_t ss; 840d034d459SMarcel Moolenaar int error, oonstack; 841df8bae1dSRodney W. Grimes 842d034d459SMarcel Moolenaar oonstack = sigonstack(cpu_getstack(p)); 843d034d459SMarcel Moolenaar 844d034d459SMarcel Moolenaar if (uap->oss != NULL) { 845628d2653SJohn Baldwin PROC_LOCK(p); 846d034d459SMarcel Moolenaar ss = p->p_sigstk; 847d034d459SMarcel Moolenaar ss.ss_flags = (p->p_flag & P_ALTSTACK) 848d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 849628d2653SJohn Baldwin PROC_UNLOCK(p); 850d034d459SMarcel Moolenaar if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 851df8bae1dSRodney W. Grimes return (error); 852df8bae1dSRodney W. Grimes } 853d034d459SMarcel Moolenaar 854d034d459SMarcel Moolenaar if (uap->ss != NULL) { 855d034d459SMarcel Moolenaar if (oonstack) 856d034d459SMarcel Moolenaar return (EPERM); 857d034d459SMarcel Moolenaar if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 858d034d459SMarcel Moolenaar return (error); 859d034d459SMarcel Moolenaar if ((ss.ss_flags & ~SS_DISABLE) != 0) 860d034d459SMarcel Moolenaar return (EINVAL); 861d034d459SMarcel Moolenaar if (!(ss.ss_flags & SS_DISABLE)) { 862806d7daaSMarcel Moolenaar if (ss.ss_size < p->p_sysent->sv_minsigstksz) 863df8bae1dSRodney W. Grimes return (ENOMEM); 864628d2653SJohn Baldwin PROC_LOCK(p); 865645682fdSLuoqi Chen p->p_sigstk = ss; 866d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 867628d2653SJohn Baldwin PROC_UNLOCK(p); 868628d2653SJohn Baldwin } else { 869628d2653SJohn Baldwin PROC_LOCK(p); 870d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 871628d2653SJohn Baldwin PROC_UNLOCK(p); 872628d2653SJohn Baldwin } 873d034d459SMarcel Moolenaar } 874df8bae1dSRodney W. Grimes return (0); 875df8bae1dSRodney W. Grimes } 876df8bae1dSRodney W. Grimes 877d93f860cSPoul-Henning Kamp /* 878d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 879d93f860cSPoul-Henning Kamp * cp is calling process. 880d93f860cSPoul-Henning Kamp */ 881d93f860cSPoul-Henning Kamp int 8822c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all) 883d93f860cSPoul-Henning Kamp register struct proc *cp; 8842c42a146SMarcel Moolenaar int sig, pgid, all; 885d93f860cSPoul-Henning Kamp { 886d93f860cSPoul-Henning Kamp register struct proc *p; 887d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 888d93f860cSPoul-Henning Kamp int nfound = 0; 889d93f860cSPoul-Henning Kamp 890553629ebSJake Burkholder if (all) { 891d93f860cSPoul-Henning Kamp /* 892d93f860cSPoul-Henning Kamp * broadcast 893d93f860cSPoul-Henning Kamp */ 8941005a129SJohn Baldwin sx_slock(&allproc_lock); 8952e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 896628d2653SJohn Baldwin PROC_LOCK(p); 897628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) { 898628d2653SJohn Baldwin PROC_UNLOCK(p); 899628d2653SJohn Baldwin continue; 900628d2653SJohn Baldwin } 90133a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 902d93f860cSPoul-Henning Kamp nfound++; 90333a9ed9dSJohn Baldwin if (sig) 9042c42a146SMarcel Moolenaar psignal(p, sig); 905628d2653SJohn Baldwin } 90633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 907d93f860cSPoul-Henning Kamp } 9081005a129SJohn Baldwin sx_sunlock(&allproc_lock); 909553629ebSJake Burkholder } else { 910d93f860cSPoul-Henning Kamp if (pgid == 0) 911d93f860cSPoul-Henning Kamp /* 912d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 913d93f860cSPoul-Henning Kamp */ 914d93f860cSPoul-Henning Kamp pgrp = cp->p_pgrp; 915d93f860cSPoul-Henning Kamp else { 916d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 917d93f860cSPoul-Henning Kamp if (pgrp == NULL) 918d93f860cSPoul-Henning Kamp return (ESRCH); 919d93f860cSPoul-Henning Kamp } 9202e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 921628d2653SJohn Baldwin PROC_LOCK(p); 922628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 923628d2653SJohn Baldwin PROC_UNLOCK(p); 924628d2653SJohn Baldwin continue; 925628d2653SJohn Baldwin } 926628d2653SJohn Baldwin mtx_lock_spin(&sched_lock); 927628d2653SJohn Baldwin if (p->p_stat == SZOMB) { 928628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 92933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 930628d2653SJohn Baldwin continue; 931628d2653SJohn Baldwin } 932628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 93333a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 934d93f860cSPoul-Henning Kamp nfound++; 93533a9ed9dSJohn Baldwin if (sig) 9362c42a146SMarcel Moolenaar psignal(p, sig); 937628d2653SJohn Baldwin } 93833a9ed9dSJohn Baldwin PROC_UNLOCK(p); 939d93f860cSPoul-Henning Kamp } 940d93f860cSPoul-Henning Kamp } 941d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 942d93f860cSPoul-Henning Kamp } 943d93f860cSPoul-Henning Kamp 944d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 945df8bae1dSRodney W. Grimes struct kill_args { 946df8bae1dSRodney W. Grimes int pid; 947df8bae1dSRodney W. Grimes int signum; 948df8bae1dSRodney W. Grimes }; 949d2d3e875SBruce Evans #endif 950df8bae1dSRodney W. Grimes /* ARGSUSED */ 95126f9a767SRodney W. Grimes int 952cb226aaaSPoul-Henning Kamp kill(cp, uap) 953df8bae1dSRodney W. Grimes register struct proc *cp; 954df8bae1dSRodney W. Grimes register struct kill_args *uap; 955df8bae1dSRodney W. Grimes { 956df8bae1dSRodney W. Grimes register struct proc *p; 957df8bae1dSRodney W. Grimes 9582c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 959df8bae1dSRodney W. Grimes return (EINVAL); 960df8bae1dSRodney W. Grimes if (uap->pid > 0) { 961df8bae1dSRodney W. Grimes /* kill single process */ 962df8bae1dSRodney W. Grimes if ((p = pfind(uap->pid)) == NULL) 963df8bae1dSRodney W. Grimes return (ESRCH); 96433a9ed9dSJohn Baldwin if (p_cansignal(cp, p, uap->signum)) { 96533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 966df8bae1dSRodney W. Grimes return (EPERM); 96733a9ed9dSJohn Baldwin } 96833a9ed9dSJohn Baldwin if (uap->signum) 969df8bae1dSRodney W. Grimes psignal(p, uap->signum); 970628d2653SJohn Baldwin PROC_UNLOCK(p); 971df8bae1dSRodney W. Grimes return (0); 972df8bae1dSRodney W. Grimes } 973df8bae1dSRodney W. Grimes switch (uap->pid) { 974df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 975df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, 0, 1)); 976df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 977df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, 0, 0)); 978df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 979df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, -uap->pid, 0)); 980df8bae1dSRodney W. Grimes } 981df8bae1dSRodney W. Grimes /* NOTREACHED */ 982df8bae1dSRodney W. Grimes } 983df8bae1dSRodney W. Grimes 984df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 985d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 986df8bae1dSRodney W. Grimes struct okillpg_args { 987df8bae1dSRodney W. Grimes int pgid; 988df8bae1dSRodney W. Grimes int signum; 989df8bae1dSRodney W. Grimes }; 990d2d3e875SBruce Evans #endif 991df8bae1dSRodney W. Grimes /* ARGSUSED */ 99226f9a767SRodney W. Grimes int 993cb226aaaSPoul-Henning Kamp okillpg(p, uap) 994df8bae1dSRodney W. Grimes struct proc *p; 995df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 996df8bae1dSRodney W. Grimes { 997df8bae1dSRodney W. Grimes 9982c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 999df8bae1dSRodney W. Grimes return (EINVAL); 1000df8bae1dSRodney W. Grimes return (killpg1(p, uap->signum, uap->pgid, 0)); 1001df8bae1dSRodney W. Grimes } 1002df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1003df8bae1dSRodney W. Grimes 1004df8bae1dSRodney W. Grimes /* 1005df8bae1dSRodney W. Grimes * Send a signal to a process group. 1006df8bae1dSRodney W. Grimes */ 1007df8bae1dSRodney W. Grimes void 10082c42a146SMarcel Moolenaar gsignal(pgid, sig) 10092c42a146SMarcel Moolenaar int pgid, sig; 1010df8bae1dSRodney W. Grimes { 1011df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1012df8bae1dSRodney W. Grimes 1013df8bae1dSRodney W. Grimes if (pgid && (pgrp = pgfind(pgid))) 10142c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 1017df8bae1dSRodney W. Grimes /* 1018df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1019df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1020df8bae1dSRodney W. Grimes */ 1021df8bae1dSRodney W. Grimes void 10222c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1023df8bae1dSRodney W. Grimes struct pgrp *pgrp; 10242c42a146SMarcel Moolenaar int sig, checkctty; 1025df8bae1dSRodney W. Grimes { 1026df8bae1dSRodney W. Grimes register struct proc *p; 1027df8bae1dSRodney W. Grimes 1028628d2653SJohn Baldwin if (pgrp) { 1029628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1030628d2653SJohn Baldwin PROC_LOCK(p); 1031df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 10322c42a146SMarcel Moolenaar psignal(p, sig); 1033628d2653SJohn Baldwin PROC_UNLOCK(p); 1034628d2653SJohn Baldwin } 1035628d2653SJohn Baldwin } 1036df8bae1dSRodney W. Grimes } 1037df8bae1dSRodney W. Grimes 1038df8bae1dSRodney W. Grimes /* 1039df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1040df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1041df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1042df8bae1dSRodney W. Grimes */ 1043df8bae1dSRodney W. Grimes void 10442c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1045df8bae1dSRodney W. Grimes struct proc *p; 10462c42a146SMarcel Moolenaar register int sig; 10478674077aSJeffrey Hsu u_long code; 1048df8bae1dSRodney W. Grimes { 1049df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1050df8bae1dSRodney W. Grimes 1051628d2653SJohn Baldwin PROC_LOCK(p); 10522c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1053d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1054df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1055df8bae1dSRodney W. Grimes #ifdef KTRACE 1056df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 10572c42a146SMarcel Moolenaar ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 10582c42a146SMarcel Moolenaar &p->p_sigmask, code); 1059df8bae1dSRodney W. Grimes #endif 1060628d2653SJohn Baldwin PROC_UNLOCK(p); /* XXX ??? */ 10612c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 10622c42a146SMarcel Moolenaar &p->p_sigmask, code); 1063628d2653SJohn Baldwin PROC_LOCK(p); 10642c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 10652c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 10662c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 10672c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1068289ccde0SPeter Wemm /* 10692c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1070289ccde0SPeter Wemm */ 10712c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 10722c42a146SMarcel Moolenaar if (sig != SIGCONT && 10732c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 10742c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 10752c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1076dedc04feSPeter Wemm } 10776f841fb7SMarcel Moolenaar } else { 10786626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 10792c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 10802c42a146SMarcel Moolenaar psignal(p, sig); 1081df8bae1dSRodney W. Grimes } 1082628d2653SJohn Baldwin PROC_UNLOCK(p); 1083df8bae1dSRodney W. Grimes } 1084df8bae1dSRodney W. Grimes 1085df8bae1dSRodney W. Grimes /* 1086df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1087df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1088df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1089df8bae1dSRodney W. Grimes * 1090df8bae1dSRodney W. Grimes * Exceptions: 1091df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1092df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1093df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1094df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1095df8bae1dSRodney W. Grimes * 1096df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1097df8bae1dSRodney W. Grimes */ 1098df8bae1dSRodney W. Grimes void 10992c42a146SMarcel Moolenaar psignal(p, sig) 1100df8bae1dSRodney W. Grimes register struct proc *p; 11012c42a146SMarcel Moolenaar register int sig; 1102df8bae1dSRodney W. Grimes { 110340447cd4SJohn Baldwin register int prop; 1104df8bae1dSRodney W. Grimes register sig_t action; 1105df8bae1dSRodney W. Grimes 11062c42a146SMarcel Moolenaar if (sig > _SIG_MAXSIG || sig <= 0) { 11072c42a146SMarcel Moolenaar printf("psignal: signal %d\n", sig); 1108df8bae1dSRodney W. Grimes panic("psignal signal number"); 11092a024a2bSSean Eric Fagan } 11102c42a146SMarcel Moolenaar 1111628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1112cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1113cb679c38SJonathan Lemon 11142c42a146SMarcel Moolenaar prop = sigprop(sig); 1115df8bae1dSRodney W. Grimes 1116df8bae1dSRodney W. Grimes /* 11172a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 11182a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 11192a024a2bSSean Eric Fagan * a chance, as well. 1120df8bae1dSRodney W. Grimes */ 11212a024a2bSSean Eric Fagan if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1122df8bae1dSRodney W. Grimes action = SIG_DFL; 1123df8bae1dSRodney W. Grimes else { 1124df8bae1dSRodney W. Grimes /* 1125df8bae1dSRodney W. Grimes * If the signal is being ignored, 1126df8bae1dSRodney W. Grimes * then we forget about it immediately. 1127df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1128df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1129df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1130df8bae1dSRodney W. Grimes */ 1131628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1132df8bae1dSRodney W. Grimes return; 11332c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1134df8bae1dSRodney W. Grimes action = SIG_HOLD; 11352c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1136df8bae1dSRodney W. Grimes action = SIG_CATCH; 1137df8bae1dSRodney W. Grimes else 1138df8bae1dSRodney W. Grimes action = SIG_DFL; 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes 11419ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1142df8bae1dSRodney W. Grimes if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1143df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1144df8bae1dSRodney W. Grimes p->p_nice = NZERO; 11459ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1146df8bae1dSRodney W. Grimes 1147df8bae1dSRodney W. Grimes if (prop & SA_CONT) 11482c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1149df8bae1dSRodney W. Grimes 1150df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1151df8bae1dSRodney W. Grimes /* 1152df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1153df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1154df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1155df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1156df8bae1dSRodney W. Grimes */ 1157df8bae1dSRodney W. Grimes if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1158628d2653SJohn Baldwin action == SIG_DFL) 1159df8bae1dSRodney W. Grimes return; 11602c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 1161df8bae1dSRodney W. Grimes } 11622c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1163df8bae1dSRodney W. Grimes 1164df8bae1dSRodney W. Grimes /* 1165df8bae1dSRodney W. Grimes * Defer further processing for signals which are held, 1166df8bae1dSRodney W. Grimes * except that stopped processes must be continued by SIGCONT. 1167df8bae1dSRodney W. Grimes */ 11689ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11691c32c37cSJohn Baldwin if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) { 11709ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1171df8bae1dSRodney W. Grimes return; 11721c32c37cSJohn Baldwin } 1173df8bae1dSRodney W. Grimes switch (p->p_stat) { 1174df8bae1dSRodney W. Grimes 1175df8bae1dSRodney W. Grimes case SSLEEP: 1176df8bae1dSRodney W. Grimes /* 1177df8bae1dSRodney W. Grimes * If process is sleeping uninterruptibly 1178df8bae1dSRodney W. Grimes * we can't interrupt the sleep... the signal will 1179df8bae1dSRodney W. Grimes * be noticed when the process returns through 1180df8bae1dSRodney W. Grimes * trap() or syscall(). 1181df8bae1dSRodney W. Grimes */ 118240447cd4SJohn Baldwin if ((p->p_sflag & PS_SINTR) == 0) { 11839ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1184df8bae1dSRodney W. Grimes goto out; 11853e6831f5SJohn Baldwin } 1186df8bae1dSRodney W. Grimes /* 1187df8bae1dSRodney W. Grimes * Process is sleeping and traced... make it runnable 1188df8bae1dSRodney W. Grimes * so it can discover the signal in issignal() and stop 1189df8bae1dSRodney W. Grimes * for the parent. 1190df8bae1dSRodney W. Grimes */ 1191df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED) 1192df8bae1dSRodney W. Grimes goto run; 11939ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1194df8bae1dSRodney W. Grimes /* 1195df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored) and process is 1196df8bae1dSRodney W. Grimes * asleep, we are finished; the process should not 1197df8bae1dSRodney W. Grimes * be awakened. 1198df8bae1dSRodney W. Grimes */ 1199df8bae1dSRodney W. Grimes if ((prop & SA_CONT) && action == SIG_DFL) { 12002c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1201df8bae1dSRodney W. Grimes goto out; 1202df8bae1dSRodney W. Grimes } 1203df8bae1dSRodney W. Grimes /* 1204df8bae1dSRodney W. Grimes * When a sleeping process receives a stop 1205df8bae1dSRodney W. Grimes * signal, process immediately if possible. 1206df8bae1dSRodney W. Grimes * All other (caught or default) signals 1207df8bae1dSRodney W. Grimes * cause the process to run. 1208df8bae1dSRodney W. Grimes */ 1209df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 12103e6831f5SJohn Baldwin if (action != SIG_DFL) 1211df8bae1dSRodney W. Grimes goto runfast; 1212df8bae1dSRodney W. Grimes /* 1213df8bae1dSRodney W. Grimes * If a child holding parent blocked, 1214df8bae1dSRodney W. Grimes * stopping could cause deadlock. 1215df8bae1dSRodney W. Grimes */ 1216df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 1217df8bae1dSRodney W. Grimes goto out; 12182c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 12192c42a146SMarcel Moolenaar p->p_xstat = sig; 1220628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1221628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1222df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1223628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1224628d2653SJohn Baldwin } 12255b3047d5SJohn Baldwin mtx_lock_spin(&sched_lock); 1226df8bae1dSRodney W. Grimes stop(p); 12275b3047d5SJohn Baldwin mtx_unlock_spin(&sched_lock); 1228df8bae1dSRodney W. Grimes goto out; 12293e6831f5SJohn Baldwin } else 1230df8bae1dSRodney W. Grimes goto runfast; 1231df8bae1dSRodney W. Grimes /* NOTREACHED */ 1232df8bae1dSRodney W. Grimes 1233df8bae1dSRodney W. Grimes case SSTOP: 12349ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1235df8bae1dSRodney W. Grimes /* 1236df8bae1dSRodney W. Grimes * If traced process is already stopped, 1237df8bae1dSRodney W. Grimes * then no further action is necessary. 1238df8bae1dSRodney W. Grimes */ 123940447cd4SJohn Baldwin if (p->p_flag & P_TRACED) 1240df8bae1dSRodney W. Grimes goto out; 1241df8bae1dSRodney W. Grimes 1242df8bae1dSRodney W. Grimes /* 1243df8bae1dSRodney W. Grimes * Kill signal always sets processes running. 1244df8bae1dSRodney W. Grimes */ 12452c42a146SMarcel Moolenaar if (sig == SIGKILL) 1246df8bae1dSRodney W. Grimes goto runfast; 1247df8bae1dSRodney W. Grimes 1248df8bae1dSRodney W. Grimes if (prop & SA_CONT) { 1249df8bae1dSRodney W. Grimes /* 1250df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored), we continue the 1251df8bae1dSRodney W. Grimes * process but don't leave the signal in p_siglist, as 1252df8bae1dSRodney W. Grimes * it has no further action. If SIGCONT is held, we 1253df8bae1dSRodney W. Grimes * continue the process and leave the signal in 1254df8bae1dSRodney W. Grimes * p_siglist. If the process catches SIGCONT, let it 1255df8bae1dSRodney W. Grimes * handle the signal itself. If it isn't waiting on 1256df8bae1dSRodney W. Grimes * an event, then it goes back to run state. 1257df8bae1dSRodney W. Grimes * Otherwise, process goes back to sleep state. 1258df8bae1dSRodney W. Grimes */ 1259df8bae1dSRodney W. Grimes if (action == SIG_DFL) 12602c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1261df8bae1dSRodney W. Grimes if (action == SIG_CATCH) 1262df8bae1dSRodney W. Grimes goto runfast; 12639ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 126440447cd4SJohn Baldwin if (p->p_wchan == NULL) 1265df8bae1dSRodney W. Grimes goto run; 1266df8bae1dSRodney W. Grimes p->p_stat = SSLEEP; 12679ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1268df8bae1dSRodney W. Grimes goto out; 1269df8bae1dSRodney W. Grimes } 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1272df8bae1dSRodney W. Grimes /* 1273df8bae1dSRodney W. Grimes * Already stopped, don't need to stop again. 1274df8bae1dSRodney W. Grimes * (If we did the shell could get confused.) 1275df8bae1dSRodney W. Grimes */ 12762c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1277df8bae1dSRodney W. Grimes goto out; 1278df8bae1dSRodney W. Grimes } 1279df8bae1dSRodney W. Grimes 1280df8bae1dSRodney W. Grimes /* 1281df8bae1dSRodney W. Grimes * If process is sleeping interruptibly, then simulate a 1282df8bae1dSRodney W. Grimes * wakeup so that when it is continued, it will be made 1283df8bae1dSRodney W. Grimes * runnable and can look at the signal. But don't make 1284df8bae1dSRodney W. Grimes * the process runnable, leave it stopped. 1285df8bae1dSRodney W. Grimes */ 12869ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 128740447cd4SJohn Baldwin if (p->p_wchan && p->p_sflag & PS_SINTR) { 128840447cd4SJohn Baldwin if (p->p_sflag & PS_CVWAITQ) 1289238510fcSJason Evans cv_waitq_remove(p); 1290238510fcSJason Evans else 1291df8bae1dSRodney W. Grimes unsleep(p); 1292238510fcSJason Evans } 12939ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1294df8bae1dSRodney W. Grimes goto out; 1295df8bae1dSRodney W. Grimes 1296df8bae1dSRodney W. Grimes default: 1297df8bae1dSRodney W. Grimes /* 1298df8bae1dSRodney W. Grimes * SRUN, SIDL, SZOMB do nothing with the signal, 1299df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1300df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1301df8bae1dSRodney W. Grimes */ 1302d2ef4060SBruce Evans if (p->p_stat == SRUN) { 1303df8bae1dSRodney W. Grimes signotify(p); 13043163861cSTor Egge #ifdef SMP 13053163861cSTor Egge forward_signal(p); 13063163861cSTor Egge #endif 13076caa8a15SJohn Baldwin } 13089ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1309df8bae1dSRodney W. Grimes goto out; 1310df8bae1dSRodney W. Grimes } 1311df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1312df8bae1dSRodney W. Grimes 1313df8bae1dSRodney W. Grimes runfast: 1314df8bae1dSRodney W. Grimes /* 1315df8bae1dSRodney W. Grimes * Raise priority to at least PUSER. 1316df8bae1dSRodney W. Grimes */ 13179ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1318d5a08a60SJake Burkholder if (p->p_pri.pri_level > PUSER) 1319d5a08a60SJake Burkholder p->p_pri.pri_level = PUSER; 1320df8bae1dSRodney W. Grimes run: 13213e6831f5SJohn Baldwin /* If we jump here, sched_lock has to be owned. */ 13223e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 1323df8bae1dSRodney W. Grimes setrunnable(p); 13249ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 13253e6831f5SJohn Baldwin out: 13263e6831f5SJohn Baldwin /* If we jump here, sched_lock should not be owned. */ 13273e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_NOTOWNED); 1328df8bae1dSRodney W. Grimes } 1329df8bae1dSRodney W. Grimes 1330df8bae1dSRodney W. Grimes /* 1331df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1332df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1333df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1334df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1335df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1336df8bae1dSRodney W. Grimes * by checking the pending signal masks in the CURSIG macro.) The normal call 1337df8bae1dSRodney W. Grimes * sequence is 1338df8bae1dSRodney W. Grimes * 13392c42a146SMarcel Moolenaar * while (sig = CURSIG(curproc)) 13402c42a146SMarcel Moolenaar * postsig(sig); 1341df8bae1dSRodney W. Grimes */ 134226f9a767SRodney W. Grimes int 1343df8bae1dSRodney W. Grimes issignal(p) 1344df8bae1dSRodney W. Grimes register struct proc *p; 1345df8bae1dSRodney W. Grimes { 13462c42a146SMarcel Moolenaar sigset_t mask; 13472c42a146SMarcel Moolenaar register int sig, prop; 1348df8bae1dSRodney W. Grimes 1349628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1350df8bae1dSRodney W. Grimes for (;;) { 13512a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 13522a024a2bSSean Eric Fagan 13532c42a146SMarcel Moolenaar mask = p->p_siglist; 13542c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1355df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 13562c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 13572c42a146SMarcel Moolenaar if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1358df8bae1dSRodney W. Grimes return (0); 13592c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 13602c42a146SMarcel Moolenaar prop = sigprop(sig); 13612a024a2bSSean Eric Fagan 1362628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 13632a024a2bSSean Eric Fagan 1364df8bae1dSRodney W. Grimes /* 1365df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1366df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1367df8bae1dSRodney W. Grimes */ 13682c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 13692c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1370df8bae1dSRodney W. Grimes continue; 1371df8bae1dSRodney W. Grimes } 1372df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1373df8bae1dSRodney W. Grimes /* 1374df8bae1dSRodney W. Grimes * If traced, always stop, and stay 1375df8bae1dSRodney W. Grimes * stopped until released by the parent. 1376df8bae1dSRodney W. Grimes */ 13772c42a146SMarcel Moolenaar p->p_xstat = sig; 1378628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1379df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1380628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1381df8bae1dSRodney W. Grimes do { 13829ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 13835b3047d5SJohn Baldwin stop(p); 1384628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 13857da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 1386df8bae1dSRodney W. Grimes mi_switch(); 13879ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 138820cdcc5bSJohn Baldwin PICKUP_GIANT(); 1389628d2653SJohn Baldwin PROC_LOCK(p); 13902a024a2bSSean Eric Fagan } while (!trace_req(p) 13912a024a2bSSean Eric Fagan && p->p_flag & P_TRACED); 1392df8bae1dSRodney W. Grimes 1393df8bae1dSRodney W. Grimes /* 1394df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1395df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1396df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1397df8bae1dSRodney W. Grimes */ 1398df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1399df8bae1dSRodney W. Grimes continue; 1400df8bae1dSRodney W. Grimes 1401df8bae1dSRodney W. Grimes /* 1402df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1403df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1404df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1405df8bae1dSRodney W. Grimes */ 14062c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 14072c42a146SMarcel Moolenaar sig = p->p_xstat; 14082c42a146SMarcel Moolenaar if (sig == 0) 1409df8bae1dSRodney W. Grimes continue; 1410df8bae1dSRodney W. Grimes 1411df8bae1dSRodney W. Grimes /* 1412df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1413df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1414df8bae1dSRodney W. Grimes */ 14152c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 14162c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1417df8bae1dSRodney W. Grimes continue; 1418df8bae1dSRodney W. Grimes } 1419df8bae1dSRodney W. Grimes 1420df8bae1dSRodney W. Grimes /* 1421df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1422df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1423df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1424df8bae1dSRodney W. Grimes */ 14252c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1426df8bae1dSRodney W. Grimes 14270b53fbe8SBruce Evans case (int)SIG_DFL: 1428df8bae1dSRodney W. Grimes /* 1429df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1430df8bae1dSRodney W. Grimes */ 1431df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1432df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1433df8bae1dSRodney W. Grimes /* 1434df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1435df8bae1dSRodney W. Grimes * in init? XXX 1436df8bae1dSRodney W. Grimes */ 1437d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 14382c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1439df8bae1dSRodney W. Grimes #endif 1440df8bae1dSRodney W. Grimes break; /* == ignore */ 1441df8bae1dSRodney W. Grimes } 1442df8bae1dSRodney W. Grimes /* 1443df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1444df8bae1dSRodney W. Grimes * with default action, stop here, 1445df8bae1dSRodney W. Grimes * then clear the signal. However, 1446df8bae1dSRodney W. Grimes * if process is member of an orphaned 1447df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1448df8bae1dSRodney W. Grimes */ 1449df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1450df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1451df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1452df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1453df8bae1dSRodney W. Grimes break; /* == ignore */ 14542c42a146SMarcel Moolenaar p->p_xstat = sig; 1455628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1456628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1457df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1458628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1459628d2653SJohn Baldwin } 14609ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 14615b3047d5SJohn Baldwin stop(p); 1462628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 14637da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 1464df8bae1dSRodney W. Grimes mi_switch(); 14659ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 146620cdcc5bSJohn Baldwin PICKUP_GIANT(); 1467628d2653SJohn Baldwin PROC_LOCK(p); 1468df8bae1dSRodney W. Grimes break; 1469df8bae1dSRodney W. Grimes } else if (prop & SA_IGNORE) { 1470df8bae1dSRodney W. Grimes /* 1471df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1472df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1473df8bae1dSRodney W. Grimes */ 1474df8bae1dSRodney W. Grimes break; /* == ignore */ 1475df8bae1dSRodney W. Grimes } else 14762c42a146SMarcel Moolenaar return (sig); 1477df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1478df8bae1dSRodney W. Grimes 14790b53fbe8SBruce Evans case (int)SIG_IGN: 1480df8bae1dSRodney W. Grimes /* 1481df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1482df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1483df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1484df8bae1dSRodney W. Grimes */ 1485df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1486df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1487df8bae1dSRodney W. Grimes printf("issignal\n"); 1488df8bae1dSRodney W. Grimes break; /* == ignore */ 1489df8bae1dSRodney W. Grimes 1490df8bae1dSRodney W. Grimes default: 1491df8bae1dSRodney W. Grimes /* 1492df8bae1dSRodney W. Grimes * This signal has an action, let 1493df8bae1dSRodney W. Grimes * postsig() process it. 1494df8bae1dSRodney W. Grimes */ 14952c42a146SMarcel Moolenaar return (sig); 1496df8bae1dSRodney W. Grimes } 14972c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1498df8bae1dSRodney W. Grimes } 1499df8bae1dSRodney W. Grimes /* NOTREACHED */ 1500df8bae1dSRodney W. Grimes } 1501df8bae1dSRodney W. Grimes 1502df8bae1dSRodney W. Grimes /* 1503df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1504df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 15055b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 15065b3047d5SJohn Baldwin * lock held. 1507df8bae1dSRodney W. Grimes */ 15085b3047d5SJohn Baldwin static void 1509df8bae1dSRodney W. Grimes stop(p) 1510df8bae1dSRodney W. Grimes register struct proc *p; 1511df8bae1dSRodney W. Grimes { 1512df8bae1dSRodney W. Grimes 1513628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 15145b3047d5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 1515df8bae1dSRodney W. Grimes p->p_stat = SSTOP; 1516df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 1517df8bae1dSRodney W. Grimes wakeup((caddr_t)p->p_pptr); 1518df8bae1dSRodney W. Grimes } 1519df8bae1dSRodney W. Grimes 1520df8bae1dSRodney W. Grimes /* 1521df8bae1dSRodney W. Grimes * Take the action for the specified signal 1522df8bae1dSRodney W. Grimes * from the current set of pending signals. 1523df8bae1dSRodney W. Grimes */ 1524df8bae1dSRodney W. Grimes void 15252c42a146SMarcel Moolenaar postsig(sig) 15262c42a146SMarcel Moolenaar register int sig; 1527df8bae1dSRodney W. Grimes { 1528df8bae1dSRodney W. Grimes register struct proc *p = curproc; 1529628d2653SJohn Baldwin struct sigacts *ps; 15302c42a146SMarcel Moolenaar sig_t action; 15312c42a146SMarcel Moolenaar sigset_t returnmask; 15322c42a146SMarcel Moolenaar int code; 1533df8bae1dSRodney W. Grimes 15342c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 15355526d2d9SEivind Eklund 1536628d2653SJohn Baldwin PROC_LOCK(p); 1537628d2653SJohn Baldwin ps = p->p_sigacts; 15382c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 15392c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1540df8bae1dSRodney W. Grimes #ifdef KTRACE 1541df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 1542645682fdSLuoqi Chen ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 15432c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1544df8bae1dSRodney W. Grimes #endif 1545628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 15462a024a2bSSean Eric Fagan 1547df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1548df8bae1dSRodney W. Grimes /* 1549df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1550df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1551df8bae1dSRodney W. Grimes */ 15522c42a146SMarcel Moolenaar sigexit(p, sig); 1553df8bae1dSRodney W. Grimes /* NOTREACHED */ 1554df8bae1dSRodney W. Grimes } else { 1555df8bae1dSRodney W. Grimes /* 1556df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1557df8bae1dSRodney W. Grimes */ 15582c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 15595526d2d9SEivind Eklund ("postsig action")); 1560df8bae1dSRodney W. Grimes /* 1561df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1562645682fdSLuoqi Chen * occurrences of this signal. 1563df8bae1dSRodney W. Grimes * 1564645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1565df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1566645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1567df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1568df8bae1dSRodney W. Grimes */ 1569645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 15706626c604SJulian Elischer returnmask = p->p_oldsigmask; 1571645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1572df8bae1dSRodney W. Grimes } else 1573df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 15742c42a146SMarcel Moolenaar 15752c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15762c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15772c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 15782c42a146SMarcel Moolenaar 15792c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1580289ccde0SPeter Wemm /* 15812c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1582289ccde0SPeter Wemm */ 15832c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 15842c42a146SMarcel Moolenaar if (sig != SIGCONT && 15852c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 15862c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 15872c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1588dedc04feSPeter Wemm } 1589df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 15902c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1591df8bae1dSRodney W. Grimes code = 0; 1592df8bae1dSRodney W. Grimes } else { 15936626c604SJulian Elischer code = p->p_code; 15946626c604SJulian Elischer p->p_code = 0; 15956626c604SJulian Elischer p->p_sig = 0; 1596df8bae1dSRodney W. Grimes } 1597628d2653SJohn Baldwin PROC_UNLOCK(p); 15982c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes } 1601df8bae1dSRodney W. Grimes 1602df8bae1dSRodney W. Grimes /* 1603df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1604df8bae1dSRodney W. Grimes */ 160526f9a767SRodney W. Grimes void 1606df8bae1dSRodney W. Grimes killproc(p, why) 1607df8bae1dSRodney W. Grimes struct proc *p; 1608df8bae1dSRodney W. Grimes char *why; 1609df8bae1dSRodney W. Grimes { 16109081e5e8SJohn Baldwin 16119081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 16120384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 16130384fff8SJason Evans p, p->p_pid, p->p_comm); 1614729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1615729b1e51SDavid Greenman p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1616df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1617df8bae1dSRodney W. Grimes } 1618df8bae1dSRodney W. Grimes 1619df8bae1dSRodney W. Grimes /* 1620df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1621df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1622df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1623df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1624df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1625df8bae1dSRodney W. Grimes * does not return. 1626df8bae1dSRodney W. Grimes */ 162726f9a767SRodney W. Grimes void 16282c42a146SMarcel Moolenaar sigexit(p, sig) 1629df8bae1dSRodney W. Grimes register struct proc *p; 16302c42a146SMarcel Moolenaar int sig; 1631df8bae1dSRodney W. Grimes { 1632df8bae1dSRodney W. Grimes 1633628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1634df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 16352c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 16362c42a146SMarcel Moolenaar p->p_sig = sig; 1637c364e17eSAndrey A. Chernov /* 1638c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1639c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1640c364e17eSAndrey A. Chernov * these messages.) 1641c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1642c364e17eSAndrey A. Chernov */ 1643628d2653SJohn Baldwin PROC_UNLOCK(p); 1644c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1645c31146a1SJohn Baldwin mtx_lock(&Giant); 1646fca666a1SJulian Elischer if (coredump(p) == 0) 16472c42a146SMarcel Moolenaar sig |= WCOREFLAG; 164857308494SJoerg Wunsch if (kern_logsigexit) 164957308494SJoerg Wunsch log(LOG_INFO, 165057308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 16513d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 16523d1b21c6SAndrey A. Chernov p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 16532c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 16542c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1655c31146a1SJohn Baldwin } else { 1656628d2653SJohn Baldwin PROC_UNLOCK(p); 1657628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1658628d2653SJohn Baldwin mtx_lock(&Giant); 1659c31146a1SJohn Baldwin } 16602c42a146SMarcel Moolenaar exit1(p, W_EXITCODE(0, sig)); 1661df8bae1dSRodney W. Grimes /* NOTREACHED */ 1662df8bae1dSRodney W. Grimes } 1663df8bae1dSRodney W. Grimes 1664c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1665c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1666c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1667c5edb423SSean Eric Fagan 1668c5edb423SSean Eric Fagan /* 1669c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1670c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1671c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1672c5edb423SSean Eric Fagan * %N name of process ("name") 1673c5edb423SSean Eric Fagan * %P process id (pid) 1674c5edb423SSean Eric Fagan * %U user id (uid) 1675c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1676c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1677c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1678c5edb423SSean Eric Fagan */ 1679c5edb423SSean Eric Fagan 1680fca666a1SJulian Elischer static char * 1681c5edb423SSean Eric Fagan expand_name(name, uid, pid) 168287f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; { 1683c5edb423SSean Eric Fagan char *temp; 1684c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 1685c5edb423SSean Eric Fagan int i, n; 1686c5edb423SSean Eric Fagan char *format = corefilename; 1687ce38ca0fSAlfred Perlstein size_t namelen; 1688c5edb423SSean Eric Fagan 1689ce38ca0fSAlfred Perlstein temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 16900bfe2990SEivind Eklund if (temp == NULL) 16910bfe2990SEivind Eklund return NULL; 1692ce38ca0fSAlfred Perlstein namelen = strlen(name); 1693ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1694c5edb423SSean Eric Fagan int l; 1695c5edb423SSean Eric Fagan switch (format[i]) { 1696c5edb423SSean Eric Fagan case '%': /* Format character */ 1697c5edb423SSean Eric Fagan i++; 1698c5edb423SSean Eric Fagan switch (format[i]) { 1699c5edb423SSean Eric Fagan case '%': 1700c5edb423SSean Eric Fagan temp[n++] = '%'; 1701c5edb423SSean Eric Fagan break; 1702c5edb423SSean Eric Fagan case 'N': /* process name */ 1703ce38ca0fSAlfred Perlstein if ((n + namelen) > MAXPATHLEN) { 170487f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1705c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1706c5edb423SSean Eric Fagan free(temp, M_TEMP); 1707c5edb423SSean Eric Fagan return NULL; 1708c5edb423SSean Eric Fagan } 1709ce38ca0fSAlfred Perlstein memcpy(temp+n, name, namelen); 1710ce38ca0fSAlfred Perlstein n += namelen; 1711c5edb423SSean Eric Fagan break; 1712c5edb423SSean Eric Fagan case 'P': /* process id */ 1713ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", pid); 1714c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 171587f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1716c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1717c5edb423SSean Eric Fagan free(temp, M_TEMP); 1718c5edb423SSean Eric Fagan return NULL; 1719c5edb423SSean Eric Fagan } 1720c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1721c5edb423SSean Eric Fagan n += l; 1722c5edb423SSean Eric Fagan break; 1723c5edb423SSean Eric Fagan case 'U': /* user id */ 1724ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", uid); 1725c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 172687f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1727c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1728c5edb423SSean Eric Fagan free(temp, M_TEMP); 1729c5edb423SSean Eric Fagan return NULL; 1730c5edb423SSean Eric Fagan } 1731c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1732c5edb423SSean Eric Fagan n += l; 1733c5edb423SSean Eric Fagan break; 1734c5edb423SSean Eric Fagan default: 1735c5edb423SSean Eric Fagan log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1736c5edb423SSean Eric Fagan } 1737c5edb423SSean Eric Fagan break; 1738c5edb423SSean Eric Fagan default: 1739c5edb423SSean Eric Fagan temp[n++] = format[i]; 1740c5edb423SSean Eric Fagan } 1741c5edb423SSean Eric Fagan } 1742ce38ca0fSAlfred Perlstein temp[n] = '\0'; 1743c5edb423SSean Eric Fagan return temp; 1744c5edb423SSean Eric Fagan } 1745c5edb423SSean Eric Fagan 1746df8bae1dSRodney W. Grimes /* 1747fca666a1SJulian Elischer * Dump a process' core. The main routine does some 1748fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 1749fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 1750fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 1751fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 1752fca666a1SJulian Elischer */ 1753fca666a1SJulian Elischer 1754fca666a1SJulian Elischer static int 1755fca666a1SJulian Elischer coredump(p) 1756fca666a1SJulian Elischer register struct proc *p; 1757fca666a1SJulian Elischer { 1758fca666a1SJulian Elischer register struct vnode *vp; 1759da654d90SPoul-Henning Kamp register struct ucred *cred = p->p_ucred; 1760fca666a1SJulian Elischer struct nameidata nd; 1761fca666a1SJulian Elischer struct vattr vattr; 1762e6796b67SKirk McKusick int error, error1, flags; 1763f2a2857bSKirk McKusick struct mount *mp; 1764fca666a1SJulian Elischer char *name; /* name of corefile */ 1765fca666a1SJulian Elischer off_t limit; 1766fca666a1SJulian Elischer 1767628d2653SJohn Baldwin PROC_LOCK(p); 1768628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 1769fca666a1SJulian Elischer 1770628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1771628d2653SJohn Baldwin PROC_UNLOCK(p); 1772fca666a1SJulian Elischer return (EFAULT); 1773628d2653SJohn Baldwin } 1774fca666a1SJulian Elischer 1775fca666a1SJulian Elischer /* 177635a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 177735a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 177835a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 177935a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 178035a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 178135a2598fSSean Eric Fagan * if it is larger than the limit. 1782fca666a1SJulian Elischer */ 178335a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1784628d2653SJohn Baldwin if (limit == 0) { 1785628d2653SJohn Baldwin PROC_UNLOCK(p); 178635a2598fSSean Eric Fagan return 0; 1787628d2653SJohn Baldwin } 1788628d2653SJohn Baldwin PROC_UNLOCK(p); 178935a2598fSSean Eric Fagan 1790f2a2857bSKirk McKusick restart: 1791fca666a1SJulian Elischer name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1792fca666a1SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1793e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 1794e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1795fca666a1SJulian Elischer free(name, M_TEMP); 1796fca666a1SJulian Elischer if (error) 1797fca666a1SJulian Elischer return (error); 1798762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 1799fca666a1SJulian Elischer vp = nd.ni_vp; 1800f2a2857bSKirk McKusick if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1801f2a2857bSKirk McKusick VOP_UNLOCK(vp, 0, p); 1802f2a2857bSKirk McKusick if ((error = vn_close(vp, FWRITE, cred, p)) != 0) 1803f2a2857bSKirk McKusick return (error); 1804f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1805f2a2857bSKirk McKusick return (error); 1806f2a2857bSKirk McKusick goto restart; 1807f2a2857bSKirk McKusick } 1808fca666a1SJulian Elischer 1809fca666a1SJulian Elischer /* Don't dump to non-regular files or files with links. */ 1810fca666a1SJulian Elischer if (vp->v_type != VREG || 1811fca666a1SJulian Elischer VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1812fca666a1SJulian Elischer error = EFAULT; 1813fca666a1SJulian Elischer goto out; 1814fca666a1SJulian Elischer } 1815fca666a1SJulian Elischer VATTR_NULL(&vattr); 1816fca666a1SJulian Elischer vattr.va_size = 0; 1817fca666a1SJulian Elischer VOP_LEASE(vp, p, cred, LEASE_WRITE); 1818fca666a1SJulian Elischer VOP_SETATTR(vp, &vattr, cred, p); 1819628d2653SJohn Baldwin PROC_LOCK(p); 1820fca666a1SJulian Elischer p->p_acflag |= ACORE; 1821628d2653SJohn Baldwin PROC_UNLOCK(p); 1822fca666a1SJulian Elischer 1823fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 1824fca666a1SJulian Elischer p->p_sysent->sv_coredump(p, vp, limit) : 1825fca666a1SJulian Elischer ENOSYS; 1826fca666a1SJulian Elischer 1827fca666a1SJulian Elischer out: 1828fca666a1SJulian Elischer VOP_UNLOCK(vp, 0, p); 1829f2a2857bSKirk McKusick vn_finished_write(mp); 1830fca666a1SJulian Elischer error1 = vn_close(vp, FWRITE, cred, p); 1831fca666a1SJulian Elischer if (error == 0) 1832fca666a1SJulian Elischer error = error1; 1833fca666a1SJulian Elischer return (error); 1834fca666a1SJulian Elischer } 1835fca666a1SJulian Elischer 1836fca666a1SJulian Elischer /* 1837df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 1838df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 1839df8bae1dSRodney W. Grimes */ 1840d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1841df8bae1dSRodney W. Grimes struct nosys_args { 1842df8bae1dSRodney W. Grimes int dummy; 1843df8bae1dSRodney W. Grimes }; 1844d2d3e875SBruce Evans #endif 1845df8bae1dSRodney W. Grimes /* ARGSUSED */ 184626f9a767SRodney W. Grimes int 1847cb226aaaSPoul-Henning Kamp nosys(p, args) 1848df8bae1dSRodney W. Grimes struct proc *p; 1849df8bae1dSRodney W. Grimes struct nosys_args *args; 1850df8bae1dSRodney W. Grimes { 1851df8bae1dSRodney W. Grimes 1852628d2653SJohn Baldwin PROC_LOCK(p); 1853df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 1854628d2653SJohn Baldwin PROC_UNLOCK(p); 1855df8bae1dSRodney W. Grimes return (EINVAL); 1856df8bae1dSRodney W. Grimes } 1857831d27a9SDon Lewis 1858831d27a9SDon Lewis /* 1859831d27a9SDon Lewis * Send a signal to a SIGIO or SIGURG to a process or process group using 1860831d27a9SDon Lewis * stored credentials rather than those of the current process. 1861831d27a9SDon Lewis */ 1862831d27a9SDon Lewis void 18632c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty) 1864831d27a9SDon Lewis struct sigio *sigio; 18652c42a146SMarcel Moolenaar int sig, checkctty; 1866831d27a9SDon Lewis { 1867831d27a9SDon Lewis if (sigio == NULL) 1868831d27a9SDon Lewis return; 1869831d27a9SDon Lewis 1870831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 1871628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 1872831d27a9SDon Lewis if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1873831d27a9SDon Lewis sigio->sio_proc)) 18742c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 1875628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 1876831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 1877831d27a9SDon Lewis struct proc *p; 1878831d27a9SDon Lewis 1879628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 1880628d2653SJohn Baldwin PROC_LOCK(p); 1881831d27a9SDon Lewis if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1882831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 18832c42a146SMarcel Moolenaar psignal(p, sig); 1884628d2653SJohn Baldwin PROC_UNLOCK(p); 1885628d2653SJohn Baldwin } 1886831d27a9SDon Lewis } 1887831d27a9SDon Lewis } 1888cb679c38SJonathan Lemon 1889cb679c38SJonathan Lemon static int 1890cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 1891cb679c38SJonathan Lemon { 1892cb679c38SJonathan Lemon struct proc *p = curproc; 1893cb679c38SJonathan Lemon 1894cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 1895cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 1896cb679c38SJonathan Lemon 1897628d2653SJohn Baldwin PROC_LOCK(p); 1898cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1899628d2653SJohn Baldwin PROC_UNLOCK(p); 1900cb679c38SJonathan Lemon 1901cb679c38SJonathan Lemon return (0); 1902cb679c38SJonathan Lemon } 1903cb679c38SJonathan Lemon 1904cb679c38SJonathan Lemon static void 1905cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 1906cb679c38SJonathan Lemon { 1907cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 1908cb679c38SJonathan Lemon 1909628d2653SJohn Baldwin PROC_LOCK(p); 1910e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1911628d2653SJohn Baldwin PROC_UNLOCK(p); 1912cb679c38SJonathan Lemon } 1913cb679c38SJonathan Lemon 1914cb679c38SJonathan Lemon /* 1915cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 1916cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 1917cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 1918cb679c38SJonathan Lemon * isn't worth the trouble. 1919cb679c38SJonathan Lemon */ 1920cb679c38SJonathan Lemon static int 1921cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 1922cb679c38SJonathan Lemon { 1923cb679c38SJonathan Lemon 1924cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 1925cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 1926cb679c38SJonathan Lemon 1927cb679c38SJonathan Lemon if (kn->kn_id == hint) 1928cb679c38SJonathan Lemon kn->kn_data++; 1929cb679c38SJonathan Lemon } 1930cb679c38SJonathan Lemon return (kn->kn_data != 0); 1931cb679c38SJonathan Lemon } 1932