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> 7106ae1e91SMatthew Dillon #include <sys/unistd.h> 72df8bae1dSRodney W. Grimes 73df8bae1dSRodney W. Grimes #include <machine/cpu.h> 74df8bae1dSRodney W. Grimes 756f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 766f841fb7SMarcel Moolenaar 77b40ce416SJulian Elischer static int coredump __P((struct thread *)); 782c42a146SMarcel Moolenaar static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act, 79645682fdSLuoqi Chen struct sigaction *oact, int old)); 802c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set, 81645682fdSLuoqi Chen sigset_t *oset, int old)); 82f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t)); 836f841fb7SMarcel Moolenaar static int killpg1 __P((struct proc *cp, int sig, int pgid, int all)); 846f841fb7SMarcel Moolenaar static int sig_ffs __P((sigset_t *set)); 856f841fb7SMarcel Moolenaar static int sigprop __P((int sig)); 866f841fb7SMarcel Moolenaar static void stop __P((struct proc *)); 8726f9a767SRodney W. Grimes 88cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 89cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 90cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 91cb679c38SJonathan Lemon 92cb679c38SJonathan Lemon struct filterops sig_filtops = 93cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 94cb679c38SJonathan Lemon 9557308494SJoerg Wunsch static int kern_logsigexit = 1; 963d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 973d177f46SBill Fumerola &kern_logsigexit, 0, 983d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 9957308494SJoerg Wunsch 100df8bae1dSRodney W. Grimes /* 101b1fc0ec1SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1029844fbc3SRobert Watson * XXX: should use suser(), p_cansignal(). 103831d27a9SDon Lewis */ 104b1fc0ec1SRobert Watson #define CANSIGIO(cr1, cr2) \ 105b1fc0ec1SRobert Watson ((cr1)->cr_uid == 0 || \ 1069844fbc3SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1079844fbc3SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1089844fbc3SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1099844fbc3SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 110831d27a9SDon Lewis 11122d4b0fbSJohn Polstra int sugid_coredump; 1123d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1133d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 114c87e2930SDavid Greenman 115e5a28db9SPaul Saab static int do_coredump = 1; 116e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 117e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 118e5a28db9SPaul Saab 1192c42a146SMarcel Moolenaar /* 1202c42a146SMarcel Moolenaar * Signal properties and actions. 1212c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1222c42a146SMarcel Moolenaar * according to the following properties: 1232c42a146SMarcel Moolenaar */ 1242c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1252c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1262c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1272c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1282c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1292c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1302c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 131df8bae1dSRodney W. Grimes 1322c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1332c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1342c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1352c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1362c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1372c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1382c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1392c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1402c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1412c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1422c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1442c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1452c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1462c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1472c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1482c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1492c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1502c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1512c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1522c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1532c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1542c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1552c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1562c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1572c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1582c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1592c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1602c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1612c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1622c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1632c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1642c42a146SMarcel Moolenaar }; 1652c42a146SMarcel Moolenaar 166fbbeeb6cSBruce Evans /* 167fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 168fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 169fbbeeb6cSBruce Evans * action, the process stops in issignal(). 170fbbeeb6cSBruce Evans * 17133510ef1SBruce Evans * MP SAFE. 172fbbeeb6cSBruce Evans */ 173fbbeeb6cSBruce Evans int 174fbbeeb6cSBruce Evans CURSIG(struct proc *p) 175fbbeeb6cSBruce Evans { 176fbbeeb6cSBruce Evans sigset_t tmpset; 177fbbeeb6cSBruce Evans 1782ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 17933510ef1SBruce Evans if (SIGISEMPTY(p->p_siglist)) 1802ad7d304SJohn Baldwin return (0); 181fbbeeb6cSBruce Evans tmpset = p->p_siglist; 182fbbeeb6cSBruce Evans SIGSETNAND(tmpset, p->p_sigmask); 18333510ef1SBruce Evans if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0) 1842ad7d304SJohn Baldwin return (0); 1852ad7d304SJohn Baldwin return (issignal(p)); 186fbbeeb6cSBruce Evans } 187fbbeeb6cSBruce Evans 1886f841fb7SMarcel Moolenaar static __inline int 1896f841fb7SMarcel Moolenaar sigprop(int sig) 1902c42a146SMarcel Moolenaar { 1916f841fb7SMarcel Moolenaar 1922c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 1932c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 1942c42a146SMarcel Moolenaar return (0); 195df8bae1dSRodney W. Grimes } 1962c42a146SMarcel Moolenaar 1976f841fb7SMarcel Moolenaar static __inline int 1986f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 1992c42a146SMarcel Moolenaar { 2002c42a146SMarcel Moolenaar int i; 2012c42a146SMarcel Moolenaar 2026f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2032c42a146SMarcel Moolenaar if (set->__bits[i]) 2042c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 205df8bae1dSRodney W. Grimes return (0); 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes 2082c42a146SMarcel Moolenaar /* 2092c42a146SMarcel Moolenaar * do_sigaction 2102c42a146SMarcel Moolenaar * sigaction 2112c42a146SMarcel Moolenaar * osigaction 2122c42a146SMarcel Moolenaar */ 2132c42a146SMarcel Moolenaar static int 214645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old) 2152c42a146SMarcel Moolenaar struct proc *p; 2162c42a146SMarcel Moolenaar register int sig; 2172c42a146SMarcel Moolenaar struct sigaction *act, *oact; 218645682fdSLuoqi Chen int old; 219df8bae1dSRodney W. Grimes { 220628d2653SJohn Baldwin register struct sigacts *ps; 221df8bae1dSRodney W. Grimes 2222c42a146SMarcel Moolenaar if (sig <= 0 || sig > _SIG_MAXSIG) 2232c42a146SMarcel Moolenaar return (EINVAL); 2242c42a146SMarcel Moolenaar 225628d2653SJohn Baldwin PROC_LOCK(p); 226628d2653SJohn Baldwin ps = p->p_sigacts; 2272c42a146SMarcel Moolenaar if (oact) { 2282c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2292c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2302c42a146SMarcel Moolenaar oact->sa_flags = 0; 2312c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2322c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2332c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2342c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2352c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2362c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2372c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2382c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2392c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2402c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 241645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2422c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 243645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2442c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2452c42a146SMarcel Moolenaar } 2462c42a146SMarcel Moolenaar if (act) { 2472c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 248628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 249628d2653SJohn Baldwin PROC_UNLOCK(p); 2502c42a146SMarcel Moolenaar return (EINVAL); 251628d2653SJohn Baldwin } 2522c42a146SMarcel Moolenaar 253df8bae1dSRodney W. Grimes /* 254df8bae1dSRodney W. Grimes * Change setting atomically. 255df8bae1dSRodney W. Grimes */ 2562c42a146SMarcel Moolenaar 2572c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2582c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2592c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 260aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 261aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 26280f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 26380f42b55SIan Dowse } else { 26480f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2652c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2662c42a146SMarcel Moolenaar } 2672c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2682c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 269df8bae1dSRodney W. Grimes else 2702c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2712c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2722c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 273df8bae1dSRodney W. Grimes else 2742c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2752c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2762c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2771e41c1b5SSteven Wallace else 2782c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 2792c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 2802c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 281289ccde0SPeter Wemm else 2822c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 283df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 2842c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 2852c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 286df8bae1dSRodney W. Grimes else 2872c42a146SMarcel Moolenaar SIGDELSET(ps->ps_usertramp, seg); 288df8bae1dSRodney W. Grimes #endif 2892c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 2902c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 291645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 2926626c604SJulian Elischer else 293645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 294aa7a4daeSPeter Wemm if ((act->sa_flags & SA_NOCLDWAIT) || 295aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) { 296245f17d4SJoerg Wunsch /* 2972c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 2982c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 2992c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3002c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 301245f17d4SJoerg Wunsch */ 302245f17d4SJoerg Wunsch if (p->p_pid == 1) 303645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3046626c604SJulian Elischer else 305645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 306245f17d4SJoerg Wunsch } else 307645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3112c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3122c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3132c42a146SMarcel Moolenaar * have to restart the process. 314df8bae1dSRodney W. Grimes */ 3152c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3162c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3172c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3182c42a146SMarcel Moolenaar /* never to be seen again */ 3192c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3202c42a146SMarcel Moolenaar if (sig != SIGCONT) 3212c42a146SMarcel Moolenaar /* easier in psignal */ 3222c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3232c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 324645682fdSLuoqi Chen } else { 3252c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3262c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3272c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3282c42a146SMarcel Moolenaar else 3292c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3302c42a146SMarcel Moolenaar } 331e8ebc08fSPeter Wemm #ifdef COMPAT_43 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); 337e8ebc08fSPeter Wemm #endif 338df8bae1dSRodney W. Grimes } 339628d2653SJohn Baldwin PROC_UNLOCK(p); 3402c42a146SMarcel Moolenaar return (0); 3412c42a146SMarcel Moolenaar } 3422c42a146SMarcel Moolenaar 3432c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3442c42a146SMarcel Moolenaar struct sigaction_args { 3452c42a146SMarcel Moolenaar int sig; 3462c42a146SMarcel Moolenaar struct sigaction *act; 3472c42a146SMarcel Moolenaar struct sigaction *oact; 3482c42a146SMarcel Moolenaar }; 3492c42a146SMarcel Moolenaar #endif 350fb99ab88SMatthew Dillon /* 351fb99ab88SMatthew Dillon * MPSAFE 352fb99ab88SMatthew Dillon */ 3532c42a146SMarcel Moolenaar /* ARGSUSED */ 3542c42a146SMarcel Moolenaar int 355b40ce416SJulian Elischer sigaction(td, uap) 356b40ce416SJulian Elischer struct thread *td; 3572c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3582c42a146SMarcel Moolenaar { 359b40ce416SJulian Elischer struct proc *p = td->td_proc; 3602c42a146SMarcel Moolenaar struct sigaction act, oact; 3612c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3622c42a146SMarcel Moolenaar int error; 3632c42a146SMarcel Moolenaar 364fb99ab88SMatthew Dillon mtx_lock(&Giant); 365fb99ab88SMatthew Dillon 3666f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3676f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3682c42a146SMarcel Moolenaar if (actp) { 3696f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3702c42a146SMarcel Moolenaar if (error) 371fb99ab88SMatthew Dillon goto done2; 3722c42a146SMarcel Moolenaar } 373645682fdSLuoqi Chen error = do_sigaction(p, uap->sig, actp, oactp, 0); 3742c42a146SMarcel Moolenaar if (oactp && !error) { 3756f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 3762c42a146SMarcel Moolenaar } 377fb99ab88SMatthew Dillon done2: 378fb99ab88SMatthew Dillon mtx_unlock(&Giant); 3792c42a146SMarcel Moolenaar return (error); 3802c42a146SMarcel Moolenaar } 3812c42a146SMarcel Moolenaar 38231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 3832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3842c42a146SMarcel Moolenaar struct osigaction_args { 3852c42a146SMarcel Moolenaar int signum; 3862c42a146SMarcel Moolenaar struct osigaction *nsa; 3872c42a146SMarcel Moolenaar struct osigaction *osa; 3882c42a146SMarcel Moolenaar }; 3892c42a146SMarcel Moolenaar #endif 390fb99ab88SMatthew Dillon /* 391fb99ab88SMatthew Dillon * MPSAFE 392fb99ab88SMatthew Dillon */ 3932c42a146SMarcel Moolenaar /* ARGSUSED */ 3942c42a146SMarcel Moolenaar int 395b40ce416SJulian Elischer osigaction(td, uap) 396b40ce416SJulian Elischer struct thread *td; 3972c42a146SMarcel Moolenaar register struct osigaction_args *uap; 3982c42a146SMarcel Moolenaar { 399b40ce416SJulian Elischer struct proc *p = td->td_proc; 4002c42a146SMarcel Moolenaar struct osigaction sa; 4012c42a146SMarcel Moolenaar struct sigaction nsa, osa; 4022c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 4032c42a146SMarcel Moolenaar int error; 4042c42a146SMarcel Moolenaar 4056f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4066f841fb7SMarcel Moolenaar return (EINVAL); 407fb99ab88SMatthew Dillon 4086f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4096f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 410fb99ab88SMatthew Dillon 411fb99ab88SMatthew Dillon mtx_lock(&Giant); 412fb99ab88SMatthew Dillon 4132c42a146SMarcel Moolenaar if (nsap) { 4146f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4152c42a146SMarcel Moolenaar if (error) 416fb99ab88SMatthew Dillon goto done2; 4172c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4182c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4192c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4202c42a146SMarcel Moolenaar } 421645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 4222c42a146SMarcel Moolenaar if (osap && !error) { 4232c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4242c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4252c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4266f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4272c42a146SMarcel Moolenaar } 428fb99ab88SMatthew Dillon done2: 429fb99ab88SMatthew Dillon mtx_unlock(&Giant); 4302c42a146SMarcel Moolenaar return (error); 4312c42a146SMarcel Moolenaar } 43231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes /* 435df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 436df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes void 439df8bae1dSRodney W. Grimes siginit(p) 440df8bae1dSRodney W. Grimes struct proc *p; 441df8bae1dSRodney W. Grimes { 442df8bae1dSRodney W. Grimes register int i; 443df8bae1dSRodney W. Grimes 444628d2653SJohn Baldwin PROC_LOCK(p); 4452c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4462c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4472c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 448628d2653SJohn Baldwin PROC_UNLOCK(p); 449df8bae1dSRodney W. Grimes } 450df8bae1dSRodney W. Grimes 451df8bae1dSRodney W. Grimes /* 452df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 453df8bae1dSRodney W. Grimes */ 454df8bae1dSRodney W. Grimes void 455df8bae1dSRodney W. Grimes execsigs(p) 456df8bae1dSRodney W. Grimes register struct proc *p; 457df8bae1dSRodney W. Grimes { 458628d2653SJohn Baldwin register struct sigacts *ps; 4592c42a146SMarcel Moolenaar register int sig; 460df8bae1dSRodney W. Grimes 461df8bae1dSRodney W. Grimes /* 462df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 463df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 464df8bae1dSRodney W. Grimes * and are now ignored by default). 465df8bae1dSRodney W. Grimes */ 466628d2653SJohn Baldwin PROC_LOCK(p); 467628d2653SJohn Baldwin ps = p->p_sigacts; 4682c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4692c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4702c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4712c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4722c42a146SMarcel Moolenaar if (sig != SIGCONT) 4732c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4742c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 475df8bae1dSRodney W. Grimes } 4762c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 477df8bae1dSRodney W. Grimes } 478df8bae1dSRodney W. Grimes /* 479df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 480df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 481df8bae1dSRodney W. Grimes */ 482645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 483645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 484645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 4853b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 48680e907a1SPeter Wemm /* 48780e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 48880e907a1SPeter Wemm */ 489645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 490c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 491c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 492628d2653SJohn Baldwin PROC_UNLOCK(p); 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes 495df8bae1dSRodney W. Grimes /* 496628d2653SJohn Baldwin * do_sigprocmask() 4977c8fdcbdSMatthew Dillon * 498628d2653SJohn Baldwin * Manipulate signal mask. 499df8bae1dSRodney W. Grimes */ 5002c42a146SMarcel Moolenaar static int 501645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 5022c42a146SMarcel Moolenaar struct proc *p; 5032c42a146SMarcel Moolenaar int how; 5042c42a146SMarcel Moolenaar sigset_t *set, *oset; 505645682fdSLuoqi Chen int old; 5062c42a146SMarcel Moolenaar { 5072c42a146SMarcel Moolenaar int error; 5082c42a146SMarcel Moolenaar 509628d2653SJohn Baldwin PROC_LOCK(p); 5102c42a146SMarcel Moolenaar if (oset != NULL) 5112c42a146SMarcel Moolenaar *oset = p->p_sigmask; 5122c42a146SMarcel Moolenaar 5132c42a146SMarcel Moolenaar error = 0; 5142c42a146SMarcel Moolenaar if (set != NULL) { 5152c42a146SMarcel Moolenaar switch (how) { 5162c42a146SMarcel Moolenaar case SIG_BLOCK: 517645682fdSLuoqi Chen SIG_CANTMASK(*set); 5182c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 5192c42a146SMarcel Moolenaar break; 5202c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5212c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 5222c42a146SMarcel Moolenaar break; 5232c42a146SMarcel Moolenaar case SIG_SETMASK: 524645682fdSLuoqi Chen SIG_CANTMASK(*set); 525645682fdSLuoqi Chen if (old) 526645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 527645682fdSLuoqi Chen else 5282c42a146SMarcel Moolenaar p->p_sigmask = *set; 5292c42a146SMarcel Moolenaar break; 5302c42a146SMarcel Moolenaar default: 5312c42a146SMarcel Moolenaar error = EINVAL; 5322c42a146SMarcel Moolenaar break; 5332c42a146SMarcel Moolenaar } 5342c42a146SMarcel Moolenaar } 535628d2653SJohn Baldwin PROC_UNLOCK(p); 5362c42a146SMarcel Moolenaar return (error); 5372c42a146SMarcel Moolenaar } 5382c42a146SMarcel Moolenaar 5397c8fdcbdSMatthew Dillon /* 540b40ce416SJulian Elischer * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't) 5417c8fdcbdSMatthew Dillon */ 5427c8fdcbdSMatthew Dillon 543d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 544df8bae1dSRodney W. Grimes struct sigprocmask_args { 545df8bae1dSRodney W. Grimes int how; 5462c42a146SMarcel Moolenaar const sigset_t *set; 5472c42a146SMarcel Moolenaar sigset_t *oset; 548df8bae1dSRodney W. Grimes }; 549d2d3e875SBruce Evans #endif 55026f9a767SRodney W. Grimes int 551b40ce416SJulian Elischer sigprocmask(td, uap) 552b40ce416SJulian Elischer register struct thread *td; 553df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 554df8bae1dSRodney W. Grimes { 555b40ce416SJulian Elischer struct proc *p = td->td_proc; 5562c42a146SMarcel Moolenaar sigset_t set, oset; 5572c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5582c42a146SMarcel Moolenaar int error; 559df8bae1dSRodney W. Grimes 5606f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5616f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5622c42a146SMarcel Moolenaar if (setp) { 5636f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5642c42a146SMarcel Moolenaar if (error) 5652c42a146SMarcel Moolenaar return (error); 566df8bae1dSRodney W. Grimes } 567645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5682c42a146SMarcel Moolenaar if (osetp && !error) { 5696f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5702c42a146SMarcel Moolenaar } 5712c42a146SMarcel Moolenaar return (error); 5722c42a146SMarcel Moolenaar } 5732c42a146SMarcel Moolenaar 57431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5757c8fdcbdSMatthew Dillon /* 5767c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 5777c8fdcbdSMatthew Dillon */ 5782c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5792c42a146SMarcel Moolenaar struct osigprocmask_args { 5802c42a146SMarcel Moolenaar int how; 5812c42a146SMarcel Moolenaar osigset_t mask; 5822c42a146SMarcel Moolenaar }; 5832c42a146SMarcel Moolenaar #endif 5842c42a146SMarcel Moolenaar int 585b40ce416SJulian Elischer osigprocmask(td, uap) 586b40ce416SJulian Elischer register struct thread *td; 5872c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 5882c42a146SMarcel Moolenaar { 589b40ce416SJulian Elischer struct proc *p = td->td_proc; 5902c42a146SMarcel Moolenaar sigset_t set, oset; 5912c42a146SMarcel Moolenaar int error; 5922c42a146SMarcel Moolenaar 5932c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 594645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 595b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 596df8bae1dSRodney W. Grimes return (error); 597df8bae1dSRodney W. Grimes } 59831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 599df8bae1dSRodney W. Grimes 600d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 601df8bae1dSRodney W. Grimes struct sigpending_args { 6022c42a146SMarcel Moolenaar sigset_t *set; 603df8bae1dSRodney W. Grimes }; 604d2d3e875SBruce Evans #endif 605fb99ab88SMatthew Dillon /* 606fb99ab88SMatthew Dillon * MPSAFE 607fb99ab88SMatthew Dillon */ 608df8bae1dSRodney W. Grimes /* ARGSUSED */ 60926f9a767SRodney W. Grimes int 610b40ce416SJulian Elischer sigpending(td, uap) 611b40ce416SJulian Elischer struct thread *td; 612df8bae1dSRodney W. Grimes struct sigpending_args *uap; 613df8bae1dSRodney W. Grimes { 614b40ce416SJulian Elischer struct proc *p = td->td_proc; 615628d2653SJohn Baldwin sigset_t siglist; 616fb99ab88SMatthew Dillon int error; 617df8bae1dSRodney W. Grimes 618fb99ab88SMatthew Dillon mtx_lock(&Giant); 619628d2653SJohn Baldwin PROC_LOCK(p); 620628d2653SJohn Baldwin siglist = p->p_siglist; 621628d2653SJohn Baldwin PROC_UNLOCK(p); 622fb99ab88SMatthew Dillon mtx_unlock(&Giant); 623fb99ab88SMatthew Dillon error = copyout(&siglist, uap->set, sizeof(sigset_t)); 624fb99ab88SMatthew Dillon return(error); 6252c42a146SMarcel Moolenaar } 6262c42a146SMarcel Moolenaar 62731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6282c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6292c42a146SMarcel Moolenaar struct osigpending_args { 6302c42a146SMarcel Moolenaar int dummy; 6312c42a146SMarcel Moolenaar }; 6322c42a146SMarcel Moolenaar #endif 633fb99ab88SMatthew Dillon /* 634fb99ab88SMatthew Dillon * MPSAFE 635fb99ab88SMatthew Dillon */ 6362c42a146SMarcel Moolenaar /* ARGSUSED */ 6372c42a146SMarcel Moolenaar int 638b40ce416SJulian Elischer osigpending(td, uap) 639b40ce416SJulian Elischer struct thread *td; 6402c42a146SMarcel Moolenaar struct osigpending_args *uap; 6412c42a146SMarcel Moolenaar { 642b40ce416SJulian Elischer struct proc *p = td->td_proc; 643b40ce416SJulian Elischer 644fb99ab88SMatthew Dillon mtx_lock(&Giant); 645628d2653SJohn Baldwin PROC_LOCK(p); 646b40ce416SJulian Elischer SIG2OSIG(p->p_siglist, td->td_retval[0]); 647628d2653SJohn Baldwin PROC_UNLOCK(p); 648fb99ab88SMatthew Dillon mtx_unlock(&Giant); 649df8bae1dSRodney W. Grimes return (0); 650df8bae1dSRodney W. Grimes } 65131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 654df8bae1dSRodney W. Grimes /* 655df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 656df8bae1dSRodney W. Grimes */ 657d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 658df8bae1dSRodney W. Grimes struct osigvec_args { 659df8bae1dSRodney W. Grimes int signum; 660df8bae1dSRodney W. Grimes struct sigvec *nsv; 661df8bae1dSRodney W. Grimes struct sigvec *osv; 662df8bae1dSRodney W. Grimes }; 663d2d3e875SBruce Evans #endif 664fb99ab88SMatthew Dillon /* 665fb99ab88SMatthew Dillon * MPSAFE 666fb99ab88SMatthew Dillon */ 667df8bae1dSRodney W. Grimes /* ARGSUSED */ 66826f9a767SRodney W. Grimes int 669b40ce416SJulian Elischer osigvec(td, uap) 670b40ce416SJulian Elischer struct thread *td; 671df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 672df8bae1dSRodney W. Grimes { 673b40ce416SJulian Elischer struct proc *p = td->td_proc; 674df8bae1dSRodney W. Grimes struct sigvec vec; 6752c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6762c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 6772c42a146SMarcel Moolenaar int error; 678df8bae1dSRodney W. Grimes 6796f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 6806f841fb7SMarcel Moolenaar return (EINVAL); 6816f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 6826f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 6832c42a146SMarcel Moolenaar if (nsap) { 6846f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 6852c42a146SMarcel Moolenaar if (error) 686df8bae1dSRodney W. Grimes return (error); 6872c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 6882c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 6892c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 6902c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 691df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 6922c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 693df8bae1dSRodney W. Grimes #endif 694df8bae1dSRodney W. Grimes } 695fb99ab88SMatthew Dillon mtx_lock(&Giant); 696645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 697fb99ab88SMatthew Dillon mtx_unlock(&Giant); 6982c42a146SMarcel Moolenaar if (osap && !error) { 6992c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 7002c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 7012c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 7022c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 7032c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 7042c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 7052c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 7062c42a146SMarcel Moolenaar #endif 7076f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 7082c42a146SMarcel Moolenaar } 7092c42a146SMarcel Moolenaar return (error); 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes 712d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 713df8bae1dSRodney W. Grimes struct osigblock_args { 714df8bae1dSRodney W. Grimes int mask; 715df8bae1dSRodney W. Grimes }; 716d2d3e875SBruce Evans #endif 717fb99ab88SMatthew Dillon /* 718fb99ab88SMatthew Dillon * MPSAFE 719fb99ab88SMatthew Dillon */ 72026f9a767SRodney W. Grimes int 721b40ce416SJulian Elischer osigblock(td, uap) 722b40ce416SJulian Elischer register struct thread *td; 723df8bae1dSRodney W. Grimes struct osigblock_args *uap; 724df8bae1dSRodney W. Grimes { 725b40ce416SJulian Elischer struct proc *p = td->td_proc; 7262c42a146SMarcel Moolenaar sigset_t set; 727df8bae1dSRodney W. Grimes 7282c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7292c42a146SMarcel Moolenaar SIG_CANTMASK(set); 730fb99ab88SMatthew Dillon mtx_lock(&Giant); 731628d2653SJohn Baldwin PROC_LOCK(p); 732b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 7332c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 734628d2653SJohn Baldwin PROC_UNLOCK(p); 735fb99ab88SMatthew Dillon mtx_unlock(&Giant); 736df8bae1dSRodney W. Grimes return (0); 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes 739d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 740df8bae1dSRodney W. Grimes struct osigsetmask_args { 741df8bae1dSRodney W. Grimes int mask; 742df8bae1dSRodney W. Grimes }; 743d2d3e875SBruce Evans #endif 744fb99ab88SMatthew Dillon /* 745fb99ab88SMatthew Dillon * MPSAFE 746fb99ab88SMatthew Dillon */ 74726f9a767SRodney W. Grimes int 748b40ce416SJulian Elischer osigsetmask(td, uap) 749b40ce416SJulian Elischer struct thread *td; 750df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 751df8bae1dSRodney W. Grimes { 752b40ce416SJulian Elischer struct proc *p = td->td_proc; 7532c42a146SMarcel Moolenaar sigset_t set; 754df8bae1dSRodney W. Grimes 7552c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7562c42a146SMarcel Moolenaar SIG_CANTMASK(set); 757fb99ab88SMatthew Dillon mtx_lock(&Giant); 758628d2653SJohn Baldwin PROC_LOCK(p); 759b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 760645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 761628d2653SJohn Baldwin PROC_UNLOCK(p); 762fb99ab88SMatthew Dillon mtx_unlock(&Giant); 763df8bae1dSRodney W. Grimes return (0); 764df8bae1dSRodney W. Grimes } 765df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 766df8bae1dSRodney W. Grimes 767df8bae1dSRodney W. Grimes /* 768df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 769df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 770df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 771b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 772b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 773b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 774df8bae1dSRodney W. Grimes */ 775d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 776df8bae1dSRodney W. Grimes struct sigsuspend_args { 7772c42a146SMarcel Moolenaar const sigset_t *sigmask; 778df8bae1dSRodney W. Grimes }; 779d2d3e875SBruce Evans #endif 780fb99ab88SMatthew Dillon /* 781fb99ab88SMatthew Dillon * MPSAFE 782fb99ab88SMatthew Dillon */ 783df8bae1dSRodney W. Grimes /* ARGSUSED */ 78426f9a767SRodney W. Grimes int 785b40ce416SJulian Elischer sigsuspend(td, uap) 786b40ce416SJulian Elischer struct thread *td; 787df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 788df8bae1dSRodney W. Grimes { 789b40ce416SJulian Elischer struct proc *p = td->td_proc; 7902c42a146SMarcel Moolenaar sigset_t mask; 791628d2653SJohn Baldwin register struct sigacts *ps; 7922c42a146SMarcel Moolenaar int error; 7932c42a146SMarcel Moolenaar 7946f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 7952c42a146SMarcel Moolenaar if (error) 7962c42a146SMarcel Moolenaar return (error); 797df8bae1dSRodney W. Grimes 798df8bae1dSRodney W. Grimes /* 799645682fdSLuoqi Chen * When returning from sigsuspend, we want 800df8bae1dSRodney W. Grimes * the old mask to be restored after the 801df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 802df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 803df8bae1dSRodney W. Grimes * to indicate this. 804df8bae1dSRodney W. Grimes */ 805fb99ab88SMatthew Dillon mtx_lock(&Giant); 806628d2653SJohn Baldwin PROC_LOCK(p); 807628d2653SJohn Baldwin ps = p->p_sigacts; 8086626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 809645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 810645682fdSLuoqi Chen 811645682fdSLuoqi Chen SIG_CANTMASK(mask); 8122c42a146SMarcel Moolenaar p->p_sigmask = mask; 813628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 8142c42a146SMarcel Moolenaar /* void */; 815628d2653SJohn Baldwin PROC_UNLOCK(p); 816fb99ab88SMatthew Dillon mtx_unlock(&Giant); 8172c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 8182c42a146SMarcel Moolenaar return (EINTR); 8192c42a146SMarcel Moolenaar } 8202c42a146SMarcel Moolenaar 82131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8222c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8232c42a146SMarcel Moolenaar struct osigsuspend_args { 8242c42a146SMarcel Moolenaar osigset_t mask; 8252c42a146SMarcel Moolenaar }; 8262c42a146SMarcel Moolenaar #endif 827fb99ab88SMatthew Dillon /* 828fb99ab88SMatthew Dillon * MPSAFE 829fb99ab88SMatthew Dillon */ 8302c42a146SMarcel Moolenaar /* ARGSUSED */ 8312c42a146SMarcel Moolenaar int 832b40ce416SJulian Elischer osigsuspend(td, uap) 833b40ce416SJulian Elischer struct thread *td; 8342c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 8352c42a146SMarcel Moolenaar { 836b40ce416SJulian Elischer struct proc *p = td->td_proc; 837645682fdSLuoqi Chen sigset_t mask; 838628d2653SJohn Baldwin register struct sigacts *ps; 8392c42a146SMarcel Moolenaar 840fb99ab88SMatthew Dillon mtx_lock(&Giant); 841628d2653SJohn Baldwin PROC_LOCK(p); 842628d2653SJohn Baldwin ps = p->p_sigacts; 8432c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 844645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 845645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 846645682fdSLuoqi Chen SIG_CANTMASK(mask); 847645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 848628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 849df8bae1dSRodney W. Grimes /* void */; 850628d2653SJohn Baldwin PROC_UNLOCK(p); 851fb99ab88SMatthew Dillon mtx_unlock(&Giant); 852df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 853df8bae1dSRodney W. Grimes return (EINTR); 854df8bae1dSRodney W. Grimes } 85531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 858d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 859df8bae1dSRodney W. Grimes struct osigstack_args { 860df8bae1dSRodney W. Grimes struct sigstack *nss; 861df8bae1dSRodney W. Grimes struct sigstack *oss; 862df8bae1dSRodney W. Grimes }; 863d2d3e875SBruce Evans #endif 864fb99ab88SMatthew Dillon /* 865fb99ab88SMatthew Dillon * MPSAFE 866fb99ab88SMatthew Dillon */ 867df8bae1dSRodney W. Grimes /* ARGSUSED */ 86826f9a767SRodney W. Grimes int 869b40ce416SJulian Elischer osigstack(td, uap) 870b40ce416SJulian Elischer struct thread *td; 871df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 872df8bae1dSRodney W. Grimes { 873b40ce416SJulian Elischer struct proc *p = td->td_proc; 874df8bae1dSRodney W. Grimes struct sigstack ss; 875fb99ab88SMatthew Dillon int error = 0; 876fb99ab88SMatthew Dillon 877fb99ab88SMatthew Dillon mtx_lock(&Giant); 878df8bae1dSRodney W. Grimes 879d034d459SMarcel Moolenaar if (uap->oss != NULL) { 880628d2653SJohn Baldwin PROC_LOCK(p); 881645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 882b40ce416SJulian Elischer ss.ss_onstack = sigonstack(cpu_getstack(td)); 883628d2653SJohn Baldwin PROC_UNLOCK(p); 884d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 885d034d459SMarcel Moolenaar if (error) 886fb99ab88SMatthew Dillon goto done2; 887d034d459SMarcel Moolenaar } 888d034d459SMarcel Moolenaar 889d034d459SMarcel Moolenaar if (uap->nss != NULL) { 890d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 891fb99ab88SMatthew Dillon goto done2; 892628d2653SJohn Baldwin PROC_LOCK(p); 893645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 894645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 895645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 896645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 897628d2653SJohn Baldwin PROC_UNLOCK(p); 898df8bae1dSRodney W. Grimes } 899fb99ab88SMatthew Dillon done2: 900fb99ab88SMatthew Dillon mtx_unlock(&Giant); 901fb99ab88SMatthew Dillon return (error); 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 904df8bae1dSRodney W. Grimes 905d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 906df8bae1dSRodney W. Grimes struct sigaltstack_args { 9072c42a146SMarcel Moolenaar stack_t *ss; 9082c42a146SMarcel Moolenaar stack_t *oss; 909df8bae1dSRodney W. Grimes }; 910d2d3e875SBruce Evans #endif 911fb99ab88SMatthew Dillon /* 912fb99ab88SMatthew Dillon * MPSAFE 913fb99ab88SMatthew Dillon */ 914df8bae1dSRodney W. Grimes /* ARGSUSED */ 91526f9a767SRodney W. Grimes int 916b40ce416SJulian Elischer sigaltstack(td, uap) 917b40ce416SJulian Elischer struct thread *td; 918df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 919df8bae1dSRodney W. Grimes { 920b40ce416SJulian Elischer struct proc *p = td->td_proc; 9212c42a146SMarcel Moolenaar stack_t ss; 922fb99ab88SMatthew Dillon int oonstack; 923fb99ab88SMatthew Dillon int error = 0; 924fb99ab88SMatthew Dillon 925fb99ab88SMatthew Dillon mtx_lock(&Giant); 926df8bae1dSRodney W. Grimes 927b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 928d034d459SMarcel Moolenaar 929d034d459SMarcel Moolenaar if (uap->oss != NULL) { 930628d2653SJohn Baldwin PROC_LOCK(p); 931d034d459SMarcel Moolenaar ss = p->p_sigstk; 932d034d459SMarcel Moolenaar ss.ss_flags = (p->p_flag & P_ALTSTACK) 933d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 934628d2653SJohn Baldwin PROC_UNLOCK(p); 935d034d459SMarcel Moolenaar if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 936fb99ab88SMatthew Dillon goto done2; 937df8bae1dSRodney W. Grimes } 938d034d459SMarcel Moolenaar 939d034d459SMarcel Moolenaar if (uap->ss != NULL) { 940fb99ab88SMatthew Dillon if (oonstack) { 941fb99ab88SMatthew Dillon error = EPERM; 942fb99ab88SMatthew Dillon goto done2; 943fb99ab88SMatthew Dillon } 944d034d459SMarcel Moolenaar if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 945fb99ab88SMatthew Dillon goto done2; 946fb99ab88SMatthew Dillon if ((ss.ss_flags & ~SS_DISABLE) != 0) { 947fb99ab88SMatthew Dillon error = EINVAL; 948fb99ab88SMatthew Dillon goto done2; 949fb99ab88SMatthew Dillon } 950d034d459SMarcel Moolenaar if (!(ss.ss_flags & SS_DISABLE)) { 951fb99ab88SMatthew Dillon if (ss.ss_size < p->p_sysent->sv_minsigstksz) { 952fb99ab88SMatthew Dillon error = ENOMEM; 953fb99ab88SMatthew Dillon goto done2; 954fb99ab88SMatthew Dillon } 955628d2653SJohn Baldwin PROC_LOCK(p); 956645682fdSLuoqi Chen p->p_sigstk = ss; 957d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 958628d2653SJohn Baldwin PROC_UNLOCK(p); 959628d2653SJohn Baldwin } else { 960628d2653SJohn Baldwin PROC_LOCK(p); 961d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 962628d2653SJohn Baldwin PROC_UNLOCK(p); 963628d2653SJohn Baldwin } 964d034d459SMarcel Moolenaar } 965fb99ab88SMatthew Dillon done2: 966fb99ab88SMatthew Dillon mtx_unlock(&Giant); 967fb99ab88SMatthew Dillon return (error); 968df8bae1dSRodney W. Grimes } 969df8bae1dSRodney W. Grimes 970d93f860cSPoul-Henning Kamp /* 971d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 972d93f860cSPoul-Henning Kamp * cp is calling process. 973d93f860cSPoul-Henning Kamp */ 974d93f860cSPoul-Henning Kamp int 9752c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all) 976d93f860cSPoul-Henning Kamp register struct proc *cp; 9772c42a146SMarcel Moolenaar int sig, pgid, all; 978d93f860cSPoul-Henning Kamp { 979d93f860cSPoul-Henning Kamp register struct proc *p; 980d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 981d93f860cSPoul-Henning Kamp int nfound = 0; 982d93f860cSPoul-Henning Kamp 983553629ebSJake Burkholder if (all) { 984d93f860cSPoul-Henning Kamp /* 985d93f860cSPoul-Henning Kamp * broadcast 986d93f860cSPoul-Henning Kamp */ 9871005a129SJohn Baldwin sx_slock(&allproc_lock); 9882e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 989628d2653SJohn Baldwin PROC_LOCK(p); 990628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) { 991628d2653SJohn Baldwin PROC_UNLOCK(p); 992628d2653SJohn Baldwin continue; 993628d2653SJohn Baldwin } 99433a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 995d93f860cSPoul-Henning Kamp nfound++; 99633a9ed9dSJohn Baldwin if (sig) 9972c42a146SMarcel Moolenaar psignal(p, sig); 998628d2653SJohn Baldwin } 99933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1000d93f860cSPoul-Henning Kamp } 10011005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1002553629ebSJake Burkholder } else { 1003d93f860cSPoul-Henning Kamp if (pgid == 0) 1004d93f860cSPoul-Henning Kamp /* 1005d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1006d93f860cSPoul-Henning Kamp */ 1007d93f860cSPoul-Henning Kamp pgrp = cp->p_pgrp; 1008d93f860cSPoul-Henning Kamp else { 1009d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1010d93f860cSPoul-Henning Kamp if (pgrp == NULL) 1011d93f860cSPoul-Henning Kamp return (ESRCH); 1012d93f860cSPoul-Henning Kamp } 10132e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1014628d2653SJohn Baldwin PROC_LOCK(p); 1015628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1016628d2653SJohn Baldwin PROC_UNLOCK(p); 1017628d2653SJohn Baldwin continue; 1018628d2653SJohn Baldwin } 1019628d2653SJohn Baldwin mtx_lock_spin(&sched_lock); 1020628d2653SJohn Baldwin if (p->p_stat == SZOMB) { 1021628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 102233a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1023628d2653SJohn Baldwin continue; 1024628d2653SJohn Baldwin } 1025628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 102633a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 1027d93f860cSPoul-Henning Kamp nfound++; 102833a9ed9dSJohn Baldwin if (sig) 10292c42a146SMarcel Moolenaar psignal(p, sig); 1030628d2653SJohn Baldwin } 103133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1032d93f860cSPoul-Henning Kamp } 1033d93f860cSPoul-Henning Kamp } 1034d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1035d93f860cSPoul-Henning Kamp } 1036d93f860cSPoul-Henning Kamp 1037d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1038df8bae1dSRodney W. Grimes struct kill_args { 1039df8bae1dSRodney W. Grimes int pid; 1040df8bae1dSRodney W. Grimes int signum; 1041df8bae1dSRodney W. Grimes }; 1042d2d3e875SBruce Evans #endif 1043fb99ab88SMatthew Dillon /* 1044fb99ab88SMatthew Dillon * MPSAFE 1045fb99ab88SMatthew Dillon */ 1046df8bae1dSRodney W. Grimes /* ARGSUSED */ 104726f9a767SRodney W. Grimes int 1048b40ce416SJulian Elischer kill(td, uap) 1049b40ce416SJulian Elischer register struct thread *td; 1050df8bae1dSRodney W. Grimes register struct kill_args *uap; 1051df8bae1dSRodney W. Grimes { 1052b40ce416SJulian Elischer register struct proc *cp = td->td_proc; 1053df8bae1dSRodney W. Grimes register struct proc *p; 1054fb99ab88SMatthew Dillon int error = 0; 1055df8bae1dSRodney W. Grimes 10562c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 1057df8bae1dSRodney W. Grimes return (EINVAL); 1058fb99ab88SMatthew Dillon 1059fb99ab88SMatthew Dillon mtx_lock(&Giant); 1060df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1061df8bae1dSRodney W. Grimes /* kill single process */ 1062fb99ab88SMatthew Dillon if ((p = pfind(uap->pid)) == NULL) { 1063fb99ab88SMatthew Dillon error = ESRCH; 1064fb99ab88SMatthew Dillon } else if (p_cansignal(cp, p, uap->signum)) { 106533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1066fb99ab88SMatthew Dillon error = EPERM; 1067fb99ab88SMatthew Dillon } else { 106833a9ed9dSJohn Baldwin if (uap->signum) 1069df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1070628d2653SJohn Baldwin PROC_UNLOCK(p); 1071fb99ab88SMatthew Dillon error = 0; 1072df8bae1dSRodney W. Grimes } 1073fb99ab88SMatthew Dillon } else { 1074df8bae1dSRodney W. Grimes switch (uap->pid) { 1075df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 1076fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, 0, 1); 1077fb99ab88SMatthew Dillon break; 1078df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 1079fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, 0, 0); 1080fb99ab88SMatthew Dillon break; 1081df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 1082fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, -uap->pid, 0); 1083fb99ab88SMatthew Dillon break; 1084df8bae1dSRodney W. Grimes } 1085fb99ab88SMatthew Dillon } 1086fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1087fb99ab88SMatthew Dillon return(error); 1088df8bae1dSRodney W. Grimes } 1089df8bae1dSRodney W. Grimes 1090df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1091d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1092df8bae1dSRodney W. Grimes struct okillpg_args { 1093df8bae1dSRodney W. Grimes int pgid; 1094df8bae1dSRodney W. Grimes int signum; 1095df8bae1dSRodney W. Grimes }; 1096d2d3e875SBruce Evans #endif 1097fb99ab88SMatthew Dillon /* 1098fb99ab88SMatthew Dillon * MPSAFE 1099fb99ab88SMatthew Dillon */ 1100df8bae1dSRodney W. Grimes /* ARGSUSED */ 110126f9a767SRodney W. Grimes int 1102b40ce416SJulian Elischer okillpg(td, uap) 1103b40ce416SJulian Elischer struct thread *td; 1104df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1105df8bae1dSRodney W. Grimes { 1106fb99ab88SMatthew Dillon int error; 1107df8bae1dSRodney W. Grimes 11082c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 1109df8bae1dSRodney W. Grimes return (EINVAL); 1110fb99ab88SMatthew Dillon mtx_lock(&Giant); 1111b40ce416SJulian Elischer error = killpg1(td->td_proc, uap->signum, uap->pgid, 0); 1112fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1113fb99ab88SMatthew Dillon return (error); 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1116df8bae1dSRodney W. Grimes 1117df8bae1dSRodney W. Grimes /* 1118df8bae1dSRodney W. Grimes * Send a signal to a process group. 1119df8bae1dSRodney W. Grimes */ 1120df8bae1dSRodney W. Grimes void 11212c42a146SMarcel Moolenaar gsignal(pgid, sig) 11222c42a146SMarcel Moolenaar int pgid, sig; 1123df8bae1dSRodney W. Grimes { 1124df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes if (pgid && (pgrp = pgfind(pgid))) 11272c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1128df8bae1dSRodney W. Grimes } 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes /* 1131df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1132df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1133df8bae1dSRodney W. Grimes */ 1134df8bae1dSRodney W. Grimes void 11352c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1136df8bae1dSRodney W. Grimes struct pgrp *pgrp; 11372c42a146SMarcel Moolenaar int sig, checkctty; 1138df8bae1dSRodney W. Grimes { 1139df8bae1dSRodney W. Grimes register struct proc *p; 1140df8bae1dSRodney W. Grimes 1141628d2653SJohn Baldwin if (pgrp) { 1142628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1143628d2653SJohn Baldwin PROC_LOCK(p); 1144df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 11452c42a146SMarcel Moolenaar psignal(p, sig); 1146628d2653SJohn Baldwin PROC_UNLOCK(p); 1147628d2653SJohn Baldwin } 1148628d2653SJohn Baldwin } 1149df8bae1dSRodney W. Grimes } 1150df8bae1dSRodney W. Grimes 1151df8bae1dSRodney W. Grimes /* 1152df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1153df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1154df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1155356861dbSMatthew Dillon * 1156356861dbSMatthew Dillon * MPSAFE 1157df8bae1dSRodney W. Grimes */ 1158df8bae1dSRodney W. Grimes void 11592c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1160df8bae1dSRodney W. Grimes struct proc *p; 11612c42a146SMarcel Moolenaar register int sig; 11628674077aSJeffrey Hsu u_long code; 1163df8bae1dSRodney W. Grimes { 1164df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1165df8bae1dSRodney W. Grimes 1166356861dbSMatthew Dillon mtx_lock(&Giant); 1167628d2653SJohn Baldwin PROC_LOCK(p); 11682c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1169d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1170df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1171df8bae1dSRodney W. Grimes #ifdef KTRACE 1172df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 11732c42a146SMarcel Moolenaar ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 11742c42a146SMarcel Moolenaar &p->p_sigmask, code); 1175df8bae1dSRodney W. Grimes #endif 11762c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 11772c42a146SMarcel Moolenaar &p->p_sigmask, code); 11782c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 11792c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 11802c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 11812c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1182289ccde0SPeter Wemm /* 11832c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1184289ccde0SPeter Wemm */ 11852c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 11862c42a146SMarcel Moolenaar if (sig != SIGCONT && 11872c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 11882c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 11892c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1190dedc04feSPeter Wemm } 11916f841fb7SMarcel Moolenaar } else { 11926626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 11932c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 11942c42a146SMarcel Moolenaar psignal(p, sig); 1195df8bae1dSRodney W. Grimes } 1196628d2653SJohn Baldwin PROC_UNLOCK(p); 1197356861dbSMatthew Dillon mtx_unlock(&Giant); 1198df8bae1dSRodney W. Grimes } 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes /* 1201df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1202df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1203df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1204df8bae1dSRodney W. Grimes * 1205df8bae1dSRodney W. Grimes * Exceptions: 1206df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1207df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1208df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1209df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1210df8bae1dSRodney W. Grimes * 1211df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1212df8bae1dSRodney W. Grimes */ 1213df8bae1dSRodney W. Grimes void 12142c42a146SMarcel Moolenaar psignal(p, sig) 1215df8bae1dSRodney W. Grimes register struct proc *p; 12162c42a146SMarcel Moolenaar register int sig; 1217df8bae1dSRodney W. Grimes { 121840447cd4SJohn Baldwin register int prop; 1219df8bae1dSRodney W. Grimes register sig_t action; 1220b40ce416SJulian Elischer struct thread *td; 1221b40ce416SJulian Elischer struct ksegrp *kg; 1222df8bae1dSRodney W. Grimes 12232c42a146SMarcel Moolenaar if (sig > _SIG_MAXSIG || sig <= 0) { 12242c42a146SMarcel Moolenaar printf("psignal: signal %d\n", sig); 1225df8bae1dSRodney W. Grimes panic("psignal signal number"); 12262a024a2bSSean Eric Fagan } 12272c42a146SMarcel Moolenaar 1228628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1229cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1230cb679c38SJonathan Lemon 12312c42a146SMarcel Moolenaar prop = sigprop(sig); 1232df8bae1dSRodney W. Grimes 1233df8bae1dSRodney W. Grimes /* 12342a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 12352a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 12362a024a2bSSean Eric Fagan * a chance, as well. 1237df8bae1dSRodney W. Grimes */ 1238b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1239df8bae1dSRodney W. Grimes action = SIG_DFL; 1240b40ce416SJulian Elischer } else { 1241df8bae1dSRodney W. Grimes /* 1242df8bae1dSRodney W. Grimes * If the signal is being ignored, 1243df8bae1dSRodney W. Grimes * then we forget about it immediately. 1244df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1245df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1246df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1247df8bae1dSRodney W. Grimes */ 1248628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1249df8bae1dSRodney W. Grimes return; 12502c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1251df8bae1dSRodney W. Grimes action = SIG_HOLD; 12522c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1253df8bae1dSRodney W. Grimes action = SIG_CATCH; 1254df8bae1dSRodney W. Grimes else 1255df8bae1dSRodney W. Grimes action = SIG_DFL; 1256df8bae1dSRodney W. Grimes } 1257df8bae1dSRodney W. Grimes 1258b40ce416SJulian Elischer /* 1259b40ce416SJulian Elischer * bring the priority of a process up if we want it to get 1260b40ce416SJulian Elischer * killed in this lifetime. 1261b40ce416SJulian Elischer * XXXKSE think if a better way to do this. 1262b40ce416SJulian Elischer * 1263b40ce416SJulian Elischer * What we need to do is see if there is a thread that will 1264b40ce416SJulian Elischer * be able to accept the signal. e.g. 1265b40ce416SJulian Elischer * FOREACH_THREAD_IN_PROC() { 1266b40ce416SJulian Elischer * if runnable, we're done 1267b40ce416SJulian Elischer * else pick one at random. 1268b40ce416SJulian Elischer * } 1269b40ce416SJulian Elischer */ 1270b40ce416SJulian Elischer /* XXXKSE 1271b40ce416SJulian Elischer * For now there is one thread per proc. 1272b40ce416SJulian Elischer * Effectively select one sucker thread.. 1273b40ce416SJulian Elischer */ 1274b40ce416SJulian Elischer td = &p->p_thread; 12759ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1276b40ce416SJulian Elischer if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) && 1277b40ce416SJulian Elischer (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0)) 1278b40ce416SJulian Elischer p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */ 12799ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1280df8bae1dSRodney W. Grimes 1281df8bae1dSRodney W. Grimes if (prop & SA_CONT) 12822c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1283df8bae1dSRodney W. Grimes 1284df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1285df8bae1dSRodney W. Grimes /* 1286df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1287df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1288df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1289df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1290df8bae1dSRodney W. Grimes */ 1291df8bae1dSRodney W. Grimes if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1292628d2653SJohn Baldwin action == SIG_DFL) 1293df8bae1dSRodney W. Grimes return; 12942c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 1295df8bae1dSRodney W. Grimes } 12962c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1297df8bae1dSRodney W. Grimes 1298df8bae1dSRodney W. Grimes /* 1299df8bae1dSRodney W. Grimes * Defer further processing for signals which are held, 1300df8bae1dSRodney W. Grimes * except that stopped processes must be continued by SIGCONT. 1301df8bae1dSRodney W. Grimes */ 13029ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 13031c32c37cSJohn Baldwin if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) { 13049ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1305df8bae1dSRodney W. Grimes return; 13061c32c37cSJohn Baldwin } 1307b40ce416SJulian Elischer 1308df8bae1dSRodney W. Grimes switch (p->p_stat) { 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes case SSLEEP: 1311df8bae1dSRodney W. Grimes /* 1312df8bae1dSRodney W. Grimes * If process is sleeping uninterruptibly 1313df8bae1dSRodney W. Grimes * we can't interrupt the sleep... the signal will 1314df8bae1dSRodney W. Grimes * be noticed when the process returns through 1315df8bae1dSRodney W. Grimes * trap() or syscall(). 1316df8bae1dSRodney W. Grimes */ 1317b40ce416SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 13189ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1319df8bae1dSRodney W. Grimes goto out; 13203e6831f5SJohn Baldwin } 1321df8bae1dSRodney W. Grimes /* 1322df8bae1dSRodney W. Grimes * Process is sleeping and traced... make it runnable 1323df8bae1dSRodney W. Grimes * so it can discover the signal in issignal() and stop 1324df8bae1dSRodney W. Grimes * for the parent. 1325df8bae1dSRodney W. Grimes */ 1326df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED) 1327df8bae1dSRodney W. Grimes goto run; 13289ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1329df8bae1dSRodney W. Grimes /* 1330df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored) and process is 1331df8bae1dSRodney W. Grimes * asleep, we are finished; the process should not 1332df8bae1dSRodney W. Grimes * be awakened. 1333df8bae1dSRodney W. Grimes */ 1334df8bae1dSRodney W. Grimes if ((prop & SA_CONT) && action == SIG_DFL) { 13352c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1336df8bae1dSRodney W. Grimes goto out; 1337df8bae1dSRodney W. Grimes } 1338df8bae1dSRodney W. Grimes /* 1339df8bae1dSRodney W. Grimes * When a sleeping process receives a stop 1340df8bae1dSRodney W. Grimes * signal, process immediately if possible. 1341df8bae1dSRodney W. Grimes * All other (caught or default) signals 1342df8bae1dSRodney W. Grimes * cause the process to run. 1343df8bae1dSRodney W. Grimes */ 1344df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 13453e6831f5SJohn Baldwin if (action != SIG_DFL) 1346df8bae1dSRodney W. Grimes goto runfast; 1347df8bae1dSRodney W. Grimes /* 1348df8bae1dSRodney W. Grimes * If a child holding parent blocked, 1349df8bae1dSRodney W. Grimes * stopping could cause deadlock. 1350df8bae1dSRodney W. Grimes */ 1351df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 1352df8bae1dSRodney W. Grimes goto out; 13532c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 13542c42a146SMarcel Moolenaar p->p_xstat = sig; 1355628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1356628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1357df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1358628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1359628d2653SJohn Baldwin } 13605b3047d5SJohn Baldwin mtx_lock_spin(&sched_lock); 1361df8bae1dSRodney W. Grimes stop(p); 13625b3047d5SJohn Baldwin mtx_unlock_spin(&sched_lock); 1363df8bae1dSRodney W. Grimes goto out; 13643e6831f5SJohn Baldwin } else 1365df8bae1dSRodney W. Grimes goto runfast; 1366df8bae1dSRodney W. Grimes /* NOTREACHED */ 1367df8bae1dSRodney W. Grimes 1368df8bae1dSRodney W. Grimes case SSTOP: 13699ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1370df8bae1dSRodney W. Grimes /* 1371df8bae1dSRodney W. Grimes * If traced process is already stopped, 1372df8bae1dSRodney W. Grimes * then no further action is necessary. 1373df8bae1dSRodney W. Grimes */ 137440447cd4SJohn Baldwin if (p->p_flag & P_TRACED) 1375df8bae1dSRodney W. Grimes goto out; 1376df8bae1dSRodney W. Grimes 1377df8bae1dSRodney W. Grimes /* 1378df8bae1dSRodney W. Grimes * Kill signal always sets processes running. 1379df8bae1dSRodney W. Grimes */ 13802c42a146SMarcel Moolenaar if (sig == SIGKILL) 1381df8bae1dSRodney W. Grimes goto runfast; 1382df8bae1dSRodney W. Grimes 1383df8bae1dSRodney W. Grimes if (prop & SA_CONT) { 1384df8bae1dSRodney W. Grimes /* 1385df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored), we continue the 1386df8bae1dSRodney W. Grimes * process but don't leave the signal in p_siglist, as 1387df8bae1dSRodney W. Grimes * it has no further action. If SIGCONT is held, we 1388df8bae1dSRodney W. Grimes * continue the process and leave the signal in 1389df8bae1dSRodney W. Grimes * p_siglist. If the process catches SIGCONT, let it 1390df8bae1dSRodney W. Grimes * handle the signal itself. If it isn't waiting on 1391df8bae1dSRodney W. Grimes * an event, then it goes back to run state. 1392df8bae1dSRodney W. Grimes * Otherwise, process goes back to sleep state. 1393df8bae1dSRodney W. Grimes */ 1394df8bae1dSRodney W. Grimes if (action == SIG_DFL) 13952c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1396df8bae1dSRodney W. Grimes if (action == SIG_CATCH) 1397df8bae1dSRodney W. Grimes goto runfast; 13989ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1399b40ce416SJulian Elischer /* 1400b40ce416SJulian Elischer * XXXKSE 1401b40ce416SJulian Elischer * do this for each thread. 1402b40ce416SJulian Elischer */ 1403b40ce416SJulian Elischer if (p->p_flag & P_KSES) { 1404b40ce416SJulian Elischer mtx_assert(&sched_lock, 1405b40ce416SJulian Elischer MA_OWNED | MA_NOTRECURSED); 1406b40ce416SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1407b40ce416SJulian Elischer if (td->td_wchan == NULL) { 1408b40ce416SJulian Elischer setrunnable(td); /* XXXKSE */ 1409b40ce416SJulian Elischer } else { 1410b40ce416SJulian Elischer /* mark it as sleeping */ 1411b40ce416SJulian Elischer } 1412b40ce416SJulian Elischer } 1413b40ce416SJulian Elischer mtx_unlock_spin(&sched_lock); 1414b40ce416SJulian Elischer } else { 1415b40ce416SJulian Elischer if (p->p_thread.td_wchan == NULL) 1416df8bae1dSRodney W. Grimes goto run; 1417df8bae1dSRodney W. Grimes p->p_stat = SSLEEP; 14189ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1419b40ce416SJulian Elischer } 1420fdd4e5c6SJulian Elischer goto out; 1421df8bae1dSRodney W. Grimes } 1422df8bae1dSRodney W. Grimes 1423df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1424df8bae1dSRodney W. Grimes /* 1425df8bae1dSRodney W. Grimes * Already stopped, don't need to stop again. 1426df8bae1dSRodney W. Grimes * (If we did the shell could get confused.) 1427df8bae1dSRodney W. Grimes */ 14282c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1429df8bae1dSRodney W. Grimes goto out; 1430df8bae1dSRodney W. Grimes } 1431df8bae1dSRodney W. Grimes 1432df8bae1dSRodney W. Grimes /* 1433df8bae1dSRodney W. Grimes * If process is sleeping interruptibly, then simulate a 1434df8bae1dSRodney W. Grimes * wakeup so that when it is continued, it will be made 1435df8bae1dSRodney W. Grimes * runnable and can look at the signal. But don't make 1436df8bae1dSRodney W. Grimes * the process runnable, leave it stopped. 1437b40ce416SJulian Elischer * XXXKSE should we wake ALL blocked threads? 1438df8bae1dSRodney W. Grimes */ 14399ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1440b40ce416SJulian Elischer if (p->p_flag & P_KSES) { 1441b40ce416SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1442b40ce416SJulian Elischer if (td->td_wchan && (td->td_flags & TDF_SINTR)){ 1443b40ce416SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 1444b40ce416SJulian Elischer cv_waitq_remove(td); 1445238510fcSJason Evans else 1446b40ce416SJulian Elischer unsleep(td); /* XXXKSE */ 1447b40ce416SJulian Elischer } 1448b40ce416SJulian Elischer } 1449b40ce416SJulian Elischer } else { 1450b40ce416SJulian Elischer if (td->td_wchan && td->td_flags & TDF_SINTR) { 1451b40ce416SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 1452b40ce416SJulian Elischer cv_waitq_remove(td); 1453b40ce416SJulian Elischer else 1454b40ce416SJulian Elischer unsleep(td); /* XXXKSE */ 1455b40ce416SJulian Elischer } 1456238510fcSJason Evans } 14579ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1458df8bae1dSRodney W. Grimes goto out; 1459df8bae1dSRodney W. Grimes 1460df8bae1dSRodney W. Grimes default: 1461df8bae1dSRodney W. Grimes /* 1462df8bae1dSRodney W. Grimes * SRUN, SIDL, SZOMB do nothing with the signal, 1463df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1464df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1465df8bae1dSRodney W. Grimes */ 1466d2ef4060SBruce Evans if (p->p_stat == SRUN) { 14673163861cSTor Egge #ifdef SMP 1468b40ce416SJulian Elischer struct kse *ke; 1469b40ce416SJulian Elischer struct thread *td = curthread; 1470b40ce416SJulian Elischer signotify(&p->p_kse); /* XXXKSE */ 1471b40ce416SJulian Elischer /* we should only deliver to one thread.. but which one? */ 1472b40ce416SJulian Elischer FOREACH_KSEGRP_IN_PROC(p, kg) { 1473b40ce416SJulian Elischer FOREACH_KSE_IN_GROUP(kg, ke) { 1474b40ce416SJulian Elischer if (ke->ke_thread == td) { 1475b40ce416SJulian Elischer continue; 1476b40ce416SJulian Elischer } 1477b40ce416SJulian Elischer forward_signal(ke->ke_thread); 1478b40ce416SJulian Elischer } 1479b40ce416SJulian Elischer } 1480b40ce416SJulian Elischer #else 1481b40ce416SJulian Elischer signotify(&p->p_kse); /* XXXKSE */ 14823163861cSTor Egge #endif 14836caa8a15SJohn Baldwin } 14849ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1485df8bae1dSRodney W. Grimes goto out; 1486df8bae1dSRodney W. Grimes } 1487df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1488df8bae1dSRodney W. Grimes 1489df8bae1dSRodney W. Grimes runfast: 1490df8bae1dSRodney W. Grimes /* 1491df8bae1dSRodney W. Grimes * Raise priority to at least PUSER. 1492b40ce416SJulian Elischer * XXXKSE Should we make them all run fast? 1493b40ce416SJulian Elischer * Maybe just one would be enough? 1494df8bae1dSRodney W. Grimes */ 14959ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1496b40ce416SJulian Elischer FOREACH_KSEGRP_IN_PROC(p, kg) { 1497b40ce416SJulian Elischer if (kg->kg_pri.pri_level > PUSER) { 1498b40ce416SJulian Elischer kg->kg_pri.pri_level = PUSER; 1499b40ce416SJulian Elischer } 1500b40ce416SJulian Elischer } 1501df8bae1dSRodney W. Grimes run: 15023e6831f5SJohn Baldwin /* If we jump here, sched_lock has to be owned. */ 15033e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 1504b40ce416SJulian Elischer setrunnable(td); /* XXXKSE */ 15059ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 15063e6831f5SJohn Baldwin out: 15073e6831f5SJohn Baldwin /* If we jump here, sched_lock should not be owned. */ 15083e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_NOTOWNED); 1509df8bae1dSRodney W. Grimes } 1510df8bae1dSRodney W. Grimes 1511df8bae1dSRodney W. Grimes /* 1512df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1513df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1514df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1515df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1516df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1517df8bae1dSRodney W. Grimes * by checking the pending signal masks in the CURSIG macro.) The normal call 1518df8bae1dSRodney W. Grimes * sequence is 1519df8bae1dSRodney W. Grimes * 15202c42a146SMarcel Moolenaar * while (sig = CURSIG(curproc)) 15212c42a146SMarcel Moolenaar * postsig(sig); 1522df8bae1dSRodney W. Grimes */ 152326f9a767SRodney W. Grimes int 1524df8bae1dSRodney W. Grimes issignal(p) 1525df8bae1dSRodney W. Grimes register struct proc *p; 1526df8bae1dSRodney W. Grimes { 15272c42a146SMarcel Moolenaar sigset_t mask; 15282c42a146SMarcel Moolenaar register int sig, prop; 1529df8bae1dSRodney W. Grimes 1530628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1531df8bae1dSRodney W. Grimes for (;;) { 15322a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 15332a024a2bSSean Eric Fagan 15342c42a146SMarcel Moolenaar mask = p->p_siglist; 15352c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1536df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 15372c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 15382c42a146SMarcel Moolenaar if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1539df8bae1dSRodney W. Grimes return (0); 15402c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 15412c42a146SMarcel Moolenaar prop = sigprop(sig); 15422a024a2bSSean Eric Fagan 1543628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 15442a024a2bSSean Eric Fagan 1545df8bae1dSRodney W. Grimes /* 1546df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1547df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1548df8bae1dSRodney W. Grimes */ 15492c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 15502c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1551df8bae1dSRodney W. Grimes continue; 1552df8bae1dSRodney W. Grimes } 1553df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1554df8bae1dSRodney W. Grimes /* 1555df8bae1dSRodney W. Grimes * If traced, always stop, and stay 1556df8bae1dSRodney W. Grimes * stopped until released by the parent. 1557df8bae1dSRodney W. Grimes */ 15582c42a146SMarcel Moolenaar p->p_xstat = sig; 1559628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1560df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1561628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1562df8bae1dSRodney W. Grimes do { 15639ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 15645b3047d5SJohn Baldwin stop(p); 1565628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 15667da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 15672ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1568df8bae1dSRodney W. Grimes mi_switch(); 15699ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 157020cdcc5bSJohn Baldwin PICKUP_GIANT(); 1571628d2653SJohn Baldwin PROC_LOCK(p); 15722a024a2bSSean Eric Fagan } while (!trace_req(p) 15732a024a2bSSean Eric Fagan && p->p_flag & P_TRACED); 1574df8bae1dSRodney W. Grimes 1575df8bae1dSRodney W. Grimes /* 1576df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1577df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1578df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1579df8bae1dSRodney W. Grimes */ 1580df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1581df8bae1dSRodney W. Grimes continue; 1582df8bae1dSRodney W. Grimes 1583df8bae1dSRodney W. Grimes /* 1584df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1585df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1586df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1587df8bae1dSRodney W. Grimes */ 15882c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 15892c42a146SMarcel Moolenaar sig = p->p_xstat; 15902c42a146SMarcel Moolenaar if (sig == 0) 1591df8bae1dSRodney W. Grimes continue; 1592df8bae1dSRodney W. Grimes 1593df8bae1dSRodney W. Grimes /* 1594df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1595df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1596df8bae1dSRodney W. Grimes */ 15972c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 15982c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1599df8bae1dSRodney W. Grimes continue; 1600df8bae1dSRodney W. Grimes } 1601df8bae1dSRodney W. Grimes 1602df8bae1dSRodney W. Grimes /* 1603df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1604df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1605df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1606df8bae1dSRodney W. Grimes */ 16072c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1608df8bae1dSRodney W. Grimes 16090b53fbe8SBruce Evans case (int)SIG_DFL: 1610df8bae1dSRodney W. Grimes /* 1611df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1612df8bae1dSRodney W. Grimes */ 1613df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1614df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1615df8bae1dSRodney W. Grimes /* 1616df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1617df8bae1dSRodney W. Grimes * in init? XXX 1618df8bae1dSRodney W. Grimes */ 1619d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 16202c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1621df8bae1dSRodney W. Grimes #endif 1622df8bae1dSRodney W. Grimes break; /* == ignore */ 1623df8bae1dSRodney W. Grimes } 1624df8bae1dSRodney W. Grimes /* 1625df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1626df8bae1dSRodney W. Grimes * with default action, stop here, 1627df8bae1dSRodney W. Grimes * then clear the signal. However, 1628df8bae1dSRodney W. Grimes * if process is member of an orphaned 1629df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1630df8bae1dSRodney W. Grimes */ 1631df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1632df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1633df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1634df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1635df8bae1dSRodney W. Grimes break; /* == ignore */ 16362c42a146SMarcel Moolenaar p->p_xstat = sig; 1637628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1638628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1639df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1640628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1641628d2653SJohn Baldwin } 16429ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 16435b3047d5SJohn Baldwin stop(p); 1644628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 16457da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 16462ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1647df8bae1dSRodney W. Grimes mi_switch(); 16489ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 164920cdcc5bSJohn Baldwin PICKUP_GIANT(); 1650628d2653SJohn Baldwin PROC_LOCK(p); 1651df8bae1dSRodney W. Grimes break; 1652df8bae1dSRodney W. Grimes } else if (prop & SA_IGNORE) { 1653df8bae1dSRodney W. Grimes /* 1654df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1655df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1656df8bae1dSRodney W. Grimes */ 1657df8bae1dSRodney W. Grimes break; /* == ignore */ 1658df8bae1dSRodney W. Grimes } else 16592c42a146SMarcel Moolenaar return (sig); 1660df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1661df8bae1dSRodney W. Grimes 16620b53fbe8SBruce Evans case (int)SIG_IGN: 1663df8bae1dSRodney W. Grimes /* 1664df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1665df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1666df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1667df8bae1dSRodney W. Grimes */ 1668df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1669df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1670df8bae1dSRodney W. Grimes printf("issignal\n"); 1671df8bae1dSRodney W. Grimes break; /* == ignore */ 1672df8bae1dSRodney W. Grimes 1673df8bae1dSRodney W. Grimes default: 1674df8bae1dSRodney W. Grimes /* 1675df8bae1dSRodney W. Grimes * This signal has an action, let 1676df8bae1dSRodney W. Grimes * postsig() process it. 1677df8bae1dSRodney W. Grimes */ 16782c42a146SMarcel Moolenaar return (sig); 1679df8bae1dSRodney W. Grimes } 16802c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1681df8bae1dSRodney W. Grimes } 1682df8bae1dSRodney W. Grimes /* NOTREACHED */ 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes 1685df8bae1dSRodney W. Grimes /* 1686df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1687df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 16885b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 16895b3047d5SJohn Baldwin * lock held. 1690df8bae1dSRodney W. Grimes */ 16915b3047d5SJohn Baldwin static void 1692df8bae1dSRodney W. Grimes stop(p) 1693df8bae1dSRodney W. Grimes register struct proc *p; 1694df8bae1dSRodney W. Grimes { 1695df8bae1dSRodney W. Grimes 1696628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 16975b3047d5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 1698df8bae1dSRodney W. Grimes p->p_stat = SSTOP; 1699df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 1700df8bae1dSRodney W. Grimes wakeup((caddr_t)p->p_pptr); 1701df8bae1dSRodney W. Grimes } 1702df8bae1dSRodney W. Grimes 1703df8bae1dSRodney W. Grimes /* 1704df8bae1dSRodney W. Grimes * Take the action for the specified signal 1705df8bae1dSRodney W. Grimes * from the current set of pending signals. 1706df8bae1dSRodney W. Grimes */ 1707df8bae1dSRodney W. Grimes void 17082c42a146SMarcel Moolenaar postsig(sig) 17092c42a146SMarcel Moolenaar register int sig; 1710df8bae1dSRodney W. Grimes { 1711b40ce416SJulian Elischer struct thread *td = curthread; 1712b40ce416SJulian Elischer register struct proc *p = td->td_proc; 1713628d2653SJohn Baldwin struct sigacts *ps; 17142c42a146SMarcel Moolenaar sig_t action; 17152c42a146SMarcel Moolenaar sigset_t returnmask; 17162c42a146SMarcel Moolenaar int code; 1717df8bae1dSRodney W. Grimes 17182c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 17195526d2d9SEivind Eklund 17202ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1721628d2653SJohn Baldwin ps = p->p_sigacts; 17222c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 17232c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1724df8bae1dSRodney W. Grimes #ifdef KTRACE 1725df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 1726645682fdSLuoqi Chen ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 17272c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1728df8bae1dSRodney W. Grimes #endif 1729628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 17302a024a2bSSean Eric Fagan 1731df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1732df8bae1dSRodney W. Grimes /* 1733df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1734df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1735df8bae1dSRodney W. Grimes */ 1736b40ce416SJulian Elischer sigexit(td, sig); 1737df8bae1dSRodney W. Grimes /* NOTREACHED */ 1738df8bae1dSRodney W. Grimes } else { 1739df8bae1dSRodney W. Grimes /* 1740df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1741df8bae1dSRodney W. Grimes */ 17422c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 17435526d2d9SEivind Eklund ("postsig action")); 1744df8bae1dSRodney W. Grimes /* 1745df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1746645682fdSLuoqi Chen * occurrences of this signal. 1747df8bae1dSRodney W. Grimes * 1748645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1749df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1750645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1751df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1752df8bae1dSRodney W. Grimes */ 1753645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 17546626c604SJulian Elischer returnmask = p->p_oldsigmask; 1755645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1756df8bae1dSRodney W. Grimes } else 1757df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 17582c42a146SMarcel Moolenaar 17592c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 17602c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 17612c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 17622c42a146SMarcel Moolenaar 17632c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1764289ccde0SPeter Wemm /* 17652c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1766289ccde0SPeter Wemm */ 17672c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 17682c42a146SMarcel Moolenaar if (sig != SIGCONT && 17692c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 17702c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 17712c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1772dedc04feSPeter Wemm } 1773df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 17742c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1775df8bae1dSRodney W. Grimes code = 0; 1776df8bae1dSRodney W. Grimes } else { 17776626c604SJulian Elischer code = p->p_code; 17786626c604SJulian Elischer p->p_code = 0; 17796626c604SJulian Elischer p->p_sig = 0; 1780df8bae1dSRodney W. Grimes } 17812c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes } 1784df8bae1dSRodney W. Grimes 1785df8bae1dSRodney W. Grimes /* 1786df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1787df8bae1dSRodney W. Grimes */ 178826f9a767SRodney W. Grimes void 1789df8bae1dSRodney W. Grimes killproc(p, why) 1790df8bae1dSRodney W. Grimes struct proc *p; 1791df8bae1dSRodney W. Grimes char *why; 1792df8bae1dSRodney W. Grimes { 17939081e5e8SJohn Baldwin 17949081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 17950384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 17960384fff8SJason Evans p, p->p_pid, p->p_comm); 1797729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1798b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1799df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1800df8bae1dSRodney W. Grimes } 1801df8bae1dSRodney W. Grimes 1802df8bae1dSRodney W. Grimes /* 1803df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1804df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1805df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1806df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1807df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1808df8bae1dSRodney W. Grimes * does not return. 1809df8bae1dSRodney W. Grimes */ 181026f9a767SRodney W. Grimes void 1811b40ce416SJulian Elischer sigexit(td, sig) 1812b40ce416SJulian Elischer struct thread *td; 18132c42a146SMarcel Moolenaar int sig; 1814df8bae1dSRodney W. Grimes { 1815b40ce416SJulian Elischer struct proc *p = td->td_proc; 1816df8bae1dSRodney W. Grimes 1817628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1818df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 18192c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 18202c42a146SMarcel Moolenaar p->p_sig = sig; 1821c364e17eSAndrey A. Chernov /* 1822c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1823c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1824c364e17eSAndrey A. Chernov * these messages.) 1825c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1826c364e17eSAndrey A. Chernov */ 1827628d2653SJohn Baldwin PROC_UNLOCK(p); 1828c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1829c31146a1SJohn Baldwin mtx_lock(&Giant); 1830b40ce416SJulian Elischer if (coredump(td) == 0) 18312c42a146SMarcel Moolenaar sig |= WCOREFLAG; 183257308494SJoerg Wunsch if (kern_logsigexit) 183357308494SJoerg Wunsch log(LOG_INFO, 183457308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 18353d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 1836b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, 18372c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 18382c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1839c31146a1SJohn Baldwin } else { 1840628d2653SJohn Baldwin PROC_UNLOCK(p); 1841628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1842628d2653SJohn Baldwin mtx_lock(&Giant); 1843c31146a1SJohn Baldwin } 1844b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 1845df8bae1dSRodney W. Grimes /* NOTREACHED */ 1846df8bae1dSRodney W. Grimes } 1847df8bae1dSRodney W. Grimes 1848c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1849c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1850c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1851c5edb423SSean Eric Fagan 1852c5edb423SSean Eric Fagan /* 1853c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1854c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1855c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1856c5edb423SSean Eric Fagan * %N name of process ("name") 1857c5edb423SSean Eric Fagan * %P process id (pid) 1858c5edb423SSean Eric Fagan * %U user id (uid) 1859c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1860c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1861c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1862c5edb423SSean Eric Fagan */ 1863c5edb423SSean Eric Fagan 1864fca666a1SJulian Elischer static char * 1865c5edb423SSean Eric Fagan expand_name(name, uid, pid) 186687f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; { 1867c5edb423SSean Eric Fagan char *temp; 1868c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 1869c5edb423SSean Eric Fagan int i, n; 1870c5edb423SSean Eric Fagan char *format = corefilename; 1871ce38ca0fSAlfred Perlstein size_t namelen; 1872c5edb423SSean Eric Fagan 1873ce38ca0fSAlfred Perlstein temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 18740bfe2990SEivind Eklund if (temp == NULL) 18750bfe2990SEivind Eklund return NULL; 1876ce38ca0fSAlfred Perlstein namelen = strlen(name); 1877ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1878c5edb423SSean Eric Fagan int l; 1879c5edb423SSean Eric Fagan switch (format[i]) { 1880c5edb423SSean Eric Fagan case '%': /* Format character */ 1881c5edb423SSean Eric Fagan i++; 1882c5edb423SSean Eric Fagan switch (format[i]) { 1883c5edb423SSean Eric Fagan case '%': 1884c5edb423SSean Eric Fagan temp[n++] = '%'; 1885c5edb423SSean Eric Fagan break; 1886c5edb423SSean Eric Fagan case 'N': /* process name */ 1887ce38ca0fSAlfred Perlstein if ((n + namelen) > MAXPATHLEN) { 188887f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1889c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1890c5edb423SSean Eric Fagan free(temp, M_TEMP); 1891c5edb423SSean Eric Fagan return NULL; 1892c5edb423SSean Eric Fagan } 1893ce38ca0fSAlfred Perlstein memcpy(temp+n, name, namelen); 1894ce38ca0fSAlfred Perlstein n += namelen; 1895c5edb423SSean Eric Fagan break; 1896c5edb423SSean Eric Fagan case 'P': /* process id */ 1897ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", pid); 1898c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 189987f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1900c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1901c5edb423SSean Eric Fagan free(temp, M_TEMP); 1902c5edb423SSean Eric Fagan return NULL; 1903c5edb423SSean Eric Fagan } 1904c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1905c5edb423SSean Eric Fagan n += l; 1906c5edb423SSean Eric Fagan break; 1907c5edb423SSean Eric Fagan case 'U': /* user id */ 1908ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", uid); 1909c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 191087f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1911c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1912c5edb423SSean Eric Fagan free(temp, M_TEMP); 1913c5edb423SSean Eric Fagan return NULL; 1914c5edb423SSean Eric Fagan } 1915c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1916c5edb423SSean Eric Fagan n += l; 1917c5edb423SSean Eric Fagan break; 1918c5edb423SSean Eric Fagan default: 1919c5edb423SSean Eric Fagan log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1920c5edb423SSean Eric Fagan } 1921c5edb423SSean Eric Fagan break; 1922c5edb423SSean Eric Fagan default: 1923c5edb423SSean Eric Fagan temp[n++] = format[i]; 1924c5edb423SSean Eric Fagan } 1925c5edb423SSean Eric Fagan } 1926ce38ca0fSAlfred Perlstein temp[n] = '\0'; 1927c5edb423SSean Eric Fagan return temp; 1928c5edb423SSean Eric Fagan } 1929c5edb423SSean Eric Fagan 1930df8bae1dSRodney W. Grimes /* 1931fca666a1SJulian Elischer * Dump a process' core. The main routine does some 1932fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 1933fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 1934fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 1935fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 1936fca666a1SJulian Elischer */ 1937fca666a1SJulian Elischer 1938fca666a1SJulian Elischer static int 1939b40ce416SJulian Elischer coredump(struct thread *td) 1940fca666a1SJulian Elischer { 1941b40ce416SJulian Elischer struct proc *p = td->td_proc; 1942fca666a1SJulian Elischer register struct vnode *vp; 1943da654d90SPoul-Henning Kamp register struct ucred *cred = p->p_ucred; 194406ae1e91SMatthew Dillon struct flock lf; 1945fca666a1SJulian Elischer struct nameidata nd; 1946fca666a1SJulian Elischer struct vattr vattr; 1947e6796b67SKirk McKusick int error, error1, flags; 1948f2a2857bSKirk McKusick struct mount *mp; 1949fca666a1SJulian Elischer char *name; /* name of corefile */ 1950fca666a1SJulian Elischer off_t limit; 1951fca666a1SJulian Elischer 1952628d2653SJohn Baldwin PROC_LOCK(p); 1953628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 1954fca666a1SJulian Elischer 1955628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1956628d2653SJohn Baldwin PROC_UNLOCK(p); 1957fca666a1SJulian Elischer return (EFAULT); 1958628d2653SJohn Baldwin } 1959fca666a1SJulian Elischer 1960fca666a1SJulian Elischer /* 196135a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 196235a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 196335a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 196435a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 196535a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 196635a2598fSSean Eric Fagan * if it is larger than the limit. 1967fca666a1SJulian Elischer */ 196835a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1969628d2653SJohn Baldwin if (limit == 0) { 1970628d2653SJohn Baldwin PROC_UNLOCK(p); 197135a2598fSSean Eric Fagan return 0; 1972628d2653SJohn Baldwin } 1973628d2653SJohn Baldwin PROC_UNLOCK(p); 197435a2598fSSean Eric Fagan 1975f2a2857bSKirk McKusick restart: 1976fca666a1SJulian Elischer name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1977ccdbd10cSPeter Pentchev if (name == NULL) 1978ccdbd10cSPeter Pentchev return (EINVAL); 1979b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 1980e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 1981e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1982fca666a1SJulian Elischer free(name, M_TEMP); 1983fca666a1SJulian Elischer if (error) 1984fca666a1SJulian Elischer return (error); 1985762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 1986fca666a1SJulian Elischer vp = nd.ni_vp; 198706ae1e91SMatthew Dillon 1988b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 198906ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 199006ae1e91SMatthew Dillon lf.l_start = 0; 199106ae1e91SMatthew Dillon lf.l_len = 0; 199206ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 199306ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 199406ae1e91SMatthew Dillon if (error) 199506ae1e91SMatthew Dillon goto out2; 199606ae1e91SMatthew Dillon 199706ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 199806ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 199906ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2000b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2001f2a2857bSKirk McKusick return (error); 2002f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2003f2a2857bSKirk McKusick return (error); 2004f2a2857bSKirk McKusick goto restart; 2005f2a2857bSKirk McKusick } 2006fca666a1SJulian Elischer 2007fca666a1SJulian Elischer /* Don't dump to non-regular files or files with links. */ 2008fca666a1SJulian Elischer if (vp->v_type != VREG || 2009b40ce416SJulian Elischer VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2010fca666a1SJulian Elischer error = EFAULT; 201106ae1e91SMatthew Dillon goto out1; 2012fca666a1SJulian Elischer } 2013fca666a1SJulian Elischer VATTR_NULL(&vattr); 2014fca666a1SJulian Elischer vattr.va_size = 0; 201588b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2016b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2017b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 201888b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2019628d2653SJohn Baldwin PROC_LOCK(p); 2020fca666a1SJulian Elischer p->p_acflag |= ACORE; 2021628d2653SJohn Baldwin PROC_UNLOCK(p); 2022fca666a1SJulian Elischer 2023fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2024b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2025fca666a1SJulian Elischer ENOSYS; 2026fca666a1SJulian Elischer 202706ae1e91SMatthew Dillon out1: 202806ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 202906ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2030f2a2857bSKirk McKusick vn_finished_write(mp); 203106ae1e91SMatthew Dillon out2: 2032b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2033fca666a1SJulian Elischer if (error == 0) 2034fca666a1SJulian Elischer error = error1; 2035fca666a1SJulian Elischer return (error); 2036fca666a1SJulian Elischer } 2037fca666a1SJulian Elischer 2038fca666a1SJulian Elischer /* 2039df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2040df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2041df8bae1dSRodney W. Grimes */ 2042d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2043df8bae1dSRodney W. Grimes struct nosys_args { 2044df8bae1dSRodney W. Grimes int dummy; 2045df8bae1dSRodney W. Grimes }; 2046d2d3e875SBruce Evans #endif 2047fb99ab88SMatthew Dillon /* 2048fb99ab88SMatthew Dillon * MPSAFE 2049fb99ab88SMatthew Dillon */ 2050df8bae1dSRodney W. Grimes /* ARGSUSED */ 205126f9a767SRodney W. Grimes int 2052b40ce416SJulian Elischer nosys(td, args) 2053b40ce416SJulian Elischer struct thread *td; 2054df8bae1dSRodney W. Grimes struct nosys_args *args; 2055df8bae1dSRodney W. Grimes { 2056b40ce416SJulian Elischer struct proc *p = td->td_proc; 2057b40ce416SJulian Elischer 2058fb99ab88SMatthew Dillon mtx_lock(&Giant); 2059628d2653SJohn Baldwin PROC_LOCK(p); 2060df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2061628d2653SJohn Baldwin PROC_UNLOCK(p); 2062fb99ab88SMatthew Dillon mtx_unlock(&Giant); 2063df8bae1dSRodney W. Grimes return (EINVAL); 2064df8bae1dSRodney W. Grimes } 2065831d27a9SDon Lewis 2066831d27a9SDon Lewis /* 2067831d27a9SDon Lewis * Send a signal to a SIGIO or SIGURG to a process or process group using 2068831d27a9SDon Lewis * stored credentials rather than those of the current process. 2069831d27a9SDon Lewis */ 2070831d27a9SDon Lewis void 20712c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty) 2072831d27a9SDon Lewis struct sigio *sigio; 20732c42a146SMarcel Moolenaar int sig, checkctty; 2074831d27a9SDon Lewis { 2075831d27a9SDon Lewis if (sigio == NULL) 2076831d27a9SDon Lewis return; 2077831d27a9SDon Lewis 2078831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2079628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 2080b1fc0ec1SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 20812c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2082628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2083831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2084831d27a9SDon Lewis struct proc *p; 2085831d27a9SDon Lewis 2086628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2087628d2653SJohn Baldwin PROC_LOCK(p); 2088b1fc0ec1SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2089831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 20902c42a146SMarcel Moolenaar psignal(p, sig); 2091628d2653SJohn Baldwin PROC_UNLOCK(p); 2092628d2653SJohn Baldwin } 2093831d27a9SDon Lewis } 2094831d27a9SDon Lewis } 2095cb679c38SJonathan Lemon 2096cb679c38SJonathan Lemon static int 2097cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2098cb679c38SJonathan Lemon { 2099cb679c38SJonathan Lemon struct proc *p = curproc; 2100cb679c38SJonathan Lemon 2101cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2102cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2103cb679c38SJonathan Lemon 2104628d2653SJohn Baldwin PROC_LOCK(p); 2105cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2106628d2653SJohn Baldwin PROC_UNLOCK(p); 2107cb679c38SJonathan Lemon 2108cb679c38SJonathan Lemon return (0); 2109cb679c38SJonathan Lemon } 2110cb679c38SJonathan Lemon 2111cb679c38SJonathan Lemon static void 2112cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2113cb679c38SJonathan Lemon { 2114cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2115cb679c38SJonathan Lemon 2116628d2653SJohn Baldwin PROC_LOCK(p); 2117e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2118628d2653SJohn Baldwin PROC_UNLOCK(p); 2119cb679c38SJonathan Lemon } 2120cb679c38SJonathan Lemon 2121cb679c38SJonathan Lemon /* 2122cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2123cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2124cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2125cb679c38SJonathan Lemon * isn't worth the trouble. 2126cb679c38SJonathan Lemon */ 2127cb679c38SJonathan Lemon static int 2128cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2129cb679c38SJonathan Lemon { 2130cb679c38SJonathan Lemon 2131cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2132cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2133cb679c38SJonathan Lemon 2134cb679c38SJonathan Lemon if (kn->kn_id == hint) 2135cb679c38SJonathan Lemon kn->kn_data++; 2136cb679c38SJonathan Lemon } 2137cb679c38SJonathan Lemon return (kn->kn_data != 0); 2138cb679c38SJonathan Lemon } 2139