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 774d77a549SAlfred Perlstein static int coredump(struct thread *); 784d77a549SAlfred Perlstein static int do_sigaction(struct proc *p, int sig, struct sigaction *act, 794d77a549SAlfred Perlstein struct sigaction *oact, int old); 804d77a549SAlfred Perlstein static int do_sigprocmask(struct proc *p, int how, sigset_t *set, 814d77a549SAlfred Perlstein sigset_t *oset, int old); 824d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t); 839c1ab3e0SJohn Baldwin static int killpg1(struct thread *td, int sig, int pgid, int all); 844d77a549SAlfred Perlstein static int sig_ffs(sigset_t *set); 854d77a549SAlfred Perlstein static int sigprop(int sig); 864d77a549SAlfred Perlstein static void stop(struct proc *); 87e602ba25SJulian Elischer static void tdsignal(struct thread *td, int sig, sig_t action); 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 1002b87b6d4SRobert Watson /* 1012b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1022b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1032b87b6d4SRobert Watson * in the right situations. 1042b87b6d4SRobert Watson */ 1052b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1062b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1072b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1082b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1092b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1102b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1112b87b6d4SRobert Watson 11222d4b0fbSJohn Polstra int sugid_coredump; 1133d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1143d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 115c87e2930SDavid Greenman 116e5a28db9SPaul Saab static int do_coredump = 1; 117e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 118e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 119e5a28db9SPaul Saab 1202c42a146SMarcel Moolenaar /* 1212c42a146SMarcel Moolenaar * Signal properties and actions. 1222c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1232c42a146SMarcel Moolenaar * according to the following properties: 1242c42a146SMarcel Moolenaar */ 1252c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1262c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1272c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1282c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1292c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1302c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1312c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 132df8bae1dSRodney W. Grimes 1332c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1342c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1352c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1362c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1372c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1382c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1392c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1402c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1412c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1422c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1442c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1452c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1462c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1472c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1482c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1492c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1502c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1512c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1522c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1532c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1542c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1552c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1562c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1572c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1582c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1592c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1602c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1612c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1622c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1632c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1642c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1652c42a146SMarcel Moolenaar }; 1662c42a146SMarcel Moolenaar 167fbbeeb6cSBruce Evans /* 168fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 169fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 170fbbeeb6cSBruce Evans * action, the process stops in issignal(). 171e602ba25SJulian Elischer * XXXKSE the check for a pending stop is not done under KSE 172fbbeeb6cSBruce Evans * 17333510ef1SBruce Evans * MP SAFE. 174fbbeeb6cSBruce Evans */ 175fbbeeb6cSBruce Evans int 176e602ba25SJulian Elischer cursig(struct thread *td) 177fbbeeb6cSBruce Evans { 178e602ba25SJulian Elischer struct proc *p = td->td_proc; 179fbbeeb6cSBruce Evans 1802ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 181179235b3SBruce Evans mtx_assert(&sched_lock, MA_NOTOWNED); 182e602ba25SJulian Elischer return (SIGPENDING(p) ? issignal(td) : 0); 183fbbeeb6cSBruce Evans } 184fbbeeb6cSBruce Evans 18579065dbaSBruce Evans /* 18679065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 18779065dbaSBruce Evans * mode. This must be called whenever a signal is added to p_siglist or 18879065dbaSBruce Evans * unmasked in p_sigmask. 18979065dbaSBruce Evans */ 19079065dbaSBruce Evans void 19179065dbaSBruce Evans signotify(struct proc *p) 19279065dbaSBruce Evans { 19379065dbaSBruce Evans 19479065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 19579065dbaSBruce Evans mtx_lock_spin(&sched_lock); 19679065dbaSBruce Evans if (SIGPENDING(p)) { 19779065dbaSBruce Evans p->p_sflag |= PS_NEEDSIGCHK; 19879065dbaSBruce Evans p->p_kse.ke_flags |= KEF_ASTPENDING; /* XXXKSE */ 19979065dbaSBruce Evans } 20079065dbaSBruce Evans mtx_unlock_spin(&sched_lock); 20179065dbaSBruce Evans } 20279065dbaSBruce Evans 2036f841fb7SMarcel Moolenaar static __inline int 2046f841fb7SMarcel Moolenaar sigprop(int sig) 2052c42a146SMarcel Moolenaar { 2066f841fb7SMarcel Moolenaar 2072c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 2082c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 2092c42a146SMarcel Moolenaar return (0); 210df8bae1dSRodney W. Grimes } 2112c42a146SMarcel Moolenaar 2126f841fb7SMarcel Moolenaar static __inline int 2136f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2142c42a146SMarcel Moolenaar { 2152c42a146SMarcel Moolenaar int i; 2162c42a146SMarcel Moolenaar 2176f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2182c42a146SMarcel Moolenaar if (set->__bits[i]) 2192c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 220df8bae1dSRodney W. Grimes return (0); 221df8bae1dSRodney W. Grimes } 222df8bae1dSRodney W. Grimes 2232c42a146SMarcel Moolenaar /* 2242c42a146SMarcel Moolenaar * do_sigaction 2252c42a146SMarcel Moolenaar * sigaction 2262c42a146SMarcel Moolenaar * osigaction 2272c42a146SMarcel Moolenaar */ 2282c42a146SMarcel Moolenaar static int 229645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old) 2302c42a146SMarcel Moolenaar struct proc *p; 2312c42a146SMarcel Moolenaar register int sig; 2322c42a146SMarcel Moolenaar struct sigaction *act, *oact; 233645682fdSLuoqi Chen int old; 234df8bae1dSRodney W. Grimes { 235628d2653SJohn Baldwin register struct sigacts *ps; 236df8bae1dSRodney W. Grimes 2372899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 2382c42a146SMarcel Moolenaar return (EINVAL); 2392c42a146SMarcel Moolenaar 240628d2653SJohn Baldwin PROC_LOCK(p); 241628d2653SJohn Baldwin ps = p->p_sigacts; 2422c42a146SMarcel Moolenaar if (oact) { 2432c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2442c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2452c42a146SMarcel Moolenaar oact->sa_flags = 0; 2462c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2472c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2482c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2492c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2502c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2512c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2522c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2532c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2542c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2552c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 256645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2572c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 258645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2592c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2602c42a146SMarcel Moolenaar } 2612c42a146SMarcel Moolenaar if (act) { 2622c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 263628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 264628d2653SJohn Baldwin PROC_UNLOCK(p); 2652c42a146SMarcel Moolenaar return (EINVAL); 266628d2653SJohn Baldwin } 2672c42a146SMarcel Moolenaar 268df8bae1dSRodney W. Grimes /* 269df8bae1dSRodney W. Grimes * Change setting atomically. 270df8bae1dSRodney W. Grimes */ 2712c42a146SMarcel Moolenaar 2722c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2732c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2742c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 275aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 276aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 27780f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 27880f42b55SIan Dowse } else { 27980f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2802c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2812c42a146SMarcel Moolenaar } 2822c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2832c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 284df8bae1dSRodney W. Grimes else 2852c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2862c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2872c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 288df8bae1dSRodney W. Grimes else 2892c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2902c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2912c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2921e41c1b5SSteven Wallace else 2932c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 2942c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 2952c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 296289ccde0SPeter Wemm else 2972c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 298df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 2992c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 3002c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 301df8bae1dSRodney W. Grimes else 3028c3d74f4SBruce Evans SIGDELSET(ps->ps_usertramp, sig); 303df8bae1dSRodney W. Grimes #endif 3042c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3052c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 306645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 3076626c604SJulian Elischer else 308645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 309ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 310245f17d4SJoerg Wunsch /* 3112c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3122c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3132c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3142c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 315245f17d4SJoerg Wunsch */ 316245f17d4SJoerg Wunsch if (p->p_pid == 1) 317645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3186626c604SJulian Elischer else 319645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 320245f17d4SJoerg Wunsch } else 321645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 322ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 323ba1551caSIan Dowse p->p_procsig->ps_flag |= PS_CLDSIGIGN; 324ba1551caSIan Dowse else 325ba1551caSIan Dowse p->p_procsig->ps_flag &= ~PS_CLDSIGIGN; 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes /* 328df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3292c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3302c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3312c42a146SMarcel Moolenaar * have to restart the process. 332df8bae1dSRodney W. Grimes */ 3332c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3342c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3352c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3362c42a146SMarcel Moolenaar /* never to be seen again */ 3372c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3382c42a146SMarcel Moolenaar if (sig != SIGCONT) 3392c42a146SMarcel Moolenaar /* easier in psignal */ 3402c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3412c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 342645682fdSLuoqi Chen } else { 3432c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3442c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3452c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3462c42a146SMarcel Moolenaar else 3472c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3482c42a146SMarcel Moolenaar } 349e8ebc08fSPeter Wemm #ifdef COMPAT_43 350645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 351645682fdSLuoqi Chen ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 352645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 353645682fdSLuoqi Chen else 354645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 355e8ebc08fSPeter Wemm #endif 356df8bae1dSRodney W. Grimes } 357628d2653SJohn Baldwin PROC_UNLOCK(p); 3582c42a146SMarcel Moolenaar return (0); 3592c42a146SMarcel Moolenaar } 3602c42a146SMarcel Moolenaar 3612c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3622c42a146SMarcel Moolenaar struct sigaction_args { 3632c42a146SMarcel Moolenaar int sig; 3642c42a146SMarcel Moolenaar struct sigaction *act; 3652c42a146SMarcel Moolenaar struct sigaction *oact; 3662c42a146SMarcel Moolenaar }; 3672c42a146SMarcel Moolenaar #endif 368fb99ab88SMatthew Dillon /* 369fb99ab88SMatthew Dillon * MPSAFE 370fb99ab88SMatthew Dillon */ 3712c42a146SMarcel Moolenaar /* ARGSUSED */ 3722c42a146SMarcel Moolenaar int 373b40ce416SJulian Elischer sigaction(td, uap) 374b40ce416SJulian Elischer struct thread *td; 3752c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3762c42a146SMarcel Moolenaar { 377b40ce416SJulian Elischer struct proc *p = td->td_proc; 3782c42a146SMarcel Moolenaar struct sigaction act, oact; 3792c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3802c42a146SMarcel Moolenaar int error; 3812c42a146SMarcel Moolenaar 382fb99ab88SMatthew Dillon mtx_lock(&Giant); 383fb99ab88SMatthew Dillon 3846f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3856f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3862c42a146SMarcel Moolenaar if (actp) { 3876f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3882c42a146SMarcel Moolenaar if (error) 389fb99ab88SMatthew Dillon goto done2; 3902c42a146SMarcel Moolenaar } 391645682fdSLuoqi Chen error = do_sigaction(p, uap->sig, actp, oactp, 0); 3922c42a146SMarcel Moolenaar if (oactp && !error) { 3936f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 3942c42a146SMarcel Moolenaar } 395fb99ab88SMatthew Dillon done2: 396fb99ab88SMatthew Dillon mtx_unlock(&Giant); 3972c42a146SMarcel Moolenaar return (error); 3982c42a146SMarcel Moolenaar } 3992c42a146SMarcel Moolenaar 40031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 4012c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4022c42a146SMarcel Moolenaar struct osigaction_args { 4032c42a146SMarcel Moolenaar int signum; 4042c42a146SMarcel Moolenaar struct osigaction *nsa; 4052c42a146SMarcel Moolenaar struct osigaction *osa; 4062c42a146SMarcel Moolenaar }; 4072c42a146SMarcel Moolenaar #endif 408fb99ab88SMatthew Dillon /* 409fb99ab88SMatthew Dillon * MPSAFE 410fb99ab88SMatthew Dillon */ 4112c42a146SMarcel Moolenaar /* ARGSUSED */ 4122c42a146SMarcel Moolenaar int 413b40ce416SJulian Elischer osigaction(td, uap) 414b40ce416SJulian Elischer struct thread *td; 4152c42a146SMarcel Moolenaar register struct osigaction_args *uap; 4162c42a146SMarcel Moolenaar { 417b40ce416SJulian Elischer struct proc *p = td->td_proc; 4182c42a146SMarcel Moolenaar struct osigaction sa; 4192c42a146SMarcel Moolenaar struct sigaction nsa, osa; 4202c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 4212c42a146SMarcel Moolenaar int error; 4222c42a146SMarcel Moolenaar 4236f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4246f841fb7SMarcel Moolenaar return (EINVAL); 425fb99ab88SMatthew Dillon 4266f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4276f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 428fb99ab88SMatthew Dillon 429fb99ab88SMatthew Dillon mtx_lock(&Giant); 430fb99ab88SMatthew Dillon 4312c42a146SMarcel Moolenaar if (nsap) { 4326f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4332c42a146SMarcel Moolenaar if (error) 434fb99ab88SMatthew Dillon goto done2; 4352c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4362c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4372c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4382c42a146SMarcel Moolenaar } 439645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 4402c42a146SMarcel Moolenaar if (osap && !error) { 4412c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4422c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4432c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4446f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4452c42a146SMarcel Moolenaar } 446fb99ab88SMatthew Dillon done2: 447fb99ab88SMatthew Dillon mtx_unlock(&Giant); 4482c42a146SMarcel Moolenaar return (error); 4492c42a146SMarcel Moolenaar } 45031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 451df8bae1dSRodney W. Grimes 452df8bae1dSRodney W. Grimes /* 453df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 454df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 455df8bae1dSRodney W. Grimes */ 456df8bae1dSRodney W. Grimes void 457df8bae1dSRodney W. Grimes siginit(p) 458df8bae1dSRodney W. Grimes struct proc *p; 459df8bae1dSRodney W. Grimes { 460df8bae1dSRodney W. Grimes register int i; 461df8bae1dSRodney W. Grimes 462628d2653SJohn Baldwin PROC_LOCK(p); 4632c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4642c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4652c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 466628d2653SJohn Baldwin PROC_UNLOCK(p); 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 471df8bae1dSRodney W. Grimes */ 472df8bae1dSRodney W. Grimes void 473df8bae1dSRodney W. Grimes execsigs(p) 474df8bae1dSRodney W. Grimes register struct proc *p; 475df8bae1dSRodney W. Grimes { 476628d2653SJohn Baldwin register struct sigacts *ps; 4772c42a146SMarcel Moolenaar register int sig; 478df8bae1dSRodney W. Grimes 479df8bae1dSRodney W. Grimes /* 480df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 481df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 482df8bae1dSRodney W. Grimes * and are now ignored by default). 483df8bae1dSRodney W. Grimes */ 4849b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 485628d2653SJohn Baldwin ps = p->p_sigacts; 4862c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4872c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4882c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4892c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4902c42a146SMarcel Moolenaar if (sig != SIGCONT) 4912c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4922c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 493df8bae1dSRodney W. Grimes } 4942c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 498df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 499df8bae1dSRodney W. Grimes */ 500645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 501645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 502645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 5033b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 50480e907a1SPeter Wemm /* 50580e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 50680e907a1SPeter Wemm */ 507ba1551caSIan Dowse p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 508c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 509c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes /* 513628d2653SJohn Baldwin * do_sigprocmask() 5147c8fdcbdSMatthew Dillon * 515628d2653SJohn Baldwin * Manipulate signal mask. 516df8bae1dSRodney W. Grimes */ 5172c42a146SMarcel Moolenaar static int 518645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 5192c42a146SMarcel Moolenaar struct proc *p; 5202c42a146SMarcel Moolenaar int how; 5212c42a146SMarcel Moolenaar sigset_t *set, *oset; 522645682fdSLuoqi Chen int old; 5232c42a146SMarcel Moolenaar { 5242c42a146SMarcel Moolenaar int error; 5252c42a146SMarcel Moolenaar 526628d2653SJohn Baldwin PROC_LOCK(p); 5272c42a146SMarcel Moolenaar if (oset != NULL) 5282c42a146SMarcel Moolenaar *oset = p->p_sigmask; 5292c42a146SMarcel Moolenaar 5302c42a146SMarcel Moolenaar error = 0; 5312c42a146SMarcel Moolenaar if (set != NULL) { 5322c42a146SMarcel Moolenaar switch (how) { 5332c42a146SMarcel Moolenaar case SIG_BLOCK: 534645682fdSLuoqi Chen SIG_CANTMASK(*set); 5352c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 5362c42a146SMarcel Moolenaar break; 5372c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5382c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 53979065dbaSBruce Evans signotify(p); 5402c42a146SMarcel Moolenaar break; 5412c42a146SMarcel Moolenaar case SIG_SETMASK: 542645682fdSLuoqi Chen SIG_CANTMASK(*set); 543645682fdSLuoqi Chen if (old) 544645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 545645682fdSLuoqi Chen else 5462c42a146SMarcel Moolenaar p->p_sigmask = *set; 54779065dbaSBruce Evans signotify(p); 5482c42a146SMarcel Moolenaar break; 5492c42a146SMarcel Moolenaar default: 5502c42a146SMarcel Moolenaar error = EINVAL; 5512c42a146SMarcel Moolenaar break; 5522c42a146SMarcel Moolenaar } 5532c42a146SMarcel Moolenaar } 554628d2653SJohn Baldwin PROC_UNLOCK(p); 5552c42a146SMarcel Moolenaar return (error); 5562c42a146SMarcel Moolenaar } 5572c42a146SMarcel Moolenaar 5587c8fdcbdSMatthew Dillon /* 559b40ce416SJulian Elischer * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't) 5607c8fdcbdSMatthew Dillon */ 5617c8fdcbdSMatthew Dillon 562d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 563df8bae1dSRodney W. Grimes struct sigprocmask_args { 564df8bae1dSRodney W. Grimes int how; 5652c42a146SMarcel Moolenaar const sigset_t *set; 5662c42a146SMarcel Moolenaar sigset_t *oset; 567df8bae1dSRodney W. Grimes }; 568d2d3e875SBruce Evans #endif 56926f9a767SRodney W. Grimes int 570b40ce416SJulian Elischer sigprocmask(td, uap) 571b40ce416SJulian Elischer register struct thread *td; 572df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 573df8bae1dSRodney W. Grimes { 574b40ce416SJulian Elischer struct proc *p = td->td_proc; 5752c42a146SMarcel Moolenaar sigset_t set, oset; 5762c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5772c42a146SMarcel Moolenaar int error; 578df8bae1dSRodney W. Grimes 5796f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5806f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5812c42a146SMarcel Moolenaar if (setp) { 5826f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5832c42a146SMarcel Moolenaar if (error) 5842c42a146SMarcel Moolenaar return (error); 585df8bae1dSRodney W. Grimes } 586645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5872c42a146SMarcel Moolenaar if (osetp && !error) { 5886f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5892c42a146SMarcel Moolenaar } 5902c42a146SMarcel Moolenaar return (error); 5912c42a146SMarcel Moolenaar } 5922c42a146SMarcel Moolenaar 59331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5947c8fdcbdSMatthew Dillon /* 5957c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 5967c8fdcbdSMatthew Dillon */ 5972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5982c42a146SMarcel Moolenaar struct osigprocmask_args { 5992c42a146SMarcel Moolenaar int how; 6002c42a146SMarcel Moolenaar osigset_t mask; 6012c42a146SMarcel Moolenaar }; 6022c42a146SMarcel Moolenaar #endif 6032c42a146SMarcel Moolenaar int 604b40ce416SJulian Elischer osigprocmask(td, uap) 605b40ce416SJulian Elischer register struct thread *td; 6062c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 6072c42a146SMarcel Moolenaar { 608b40ce416SJulian Elischer struct proc *p = td->td_proc; 6092c42a146SMarcel Moolenaar sigset_t set, oset; 6102c42a146SMarcel Moolenaar int error; 6112c42a146SMarcel Moolenaar 6122c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 613645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 614b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 615df8bae1dSRodney W. Grimes return (error); 616df8bae1dSRodney W. Grimes } 61731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 618df8bae1dSRodney W. Grimes 619d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 620df8bae1dSRodney W. Grimes struct sigpending_args { 6212c42a146SMarcel Moolenaar sigset_t *set; 622df8bae1dSRodney W. Grimes }; 623d2d3e875SBruce Evans #endif 624fb99ab88SMatthew Dillon /* 625fb99ab88SMatthew Dillon * MPSAFE 626fb99ab88SMatthew Dillon */ 627df8bae1dSRodney W. Grimes /* ARGSUSED */ 62826f9a767SRodney W. Grimes int 629b40ce416SJulian Elischer sigpending(td, uap) 630b40ce416SJulian Elischer struct thread *td; 631df8bae1dSRodney W. Grimes struct sigpending_args *uap; 632df8bae1dSRodney W. Grimes { 633b40ce416SJulian Elischer struct proc *p = td->td_proc; 634628d2653SJohn Baldwin sigset_t siglist; 635fb99ab88SMatthew Dillon int error; 636df8bae1dSRodney W. Grimes 637fb99ab88SMatthew Dillon mtx_lock(&Giant); 638628d2653SJohn Baldwin PROC_LOCK(p); 639628d2653SJohn Baldwin siglist = p->p_siglist; 640628d2653SJohn Baldwin PROC_UNLOCK(p); 641fb99ab88SMatthew Dillon mtx_unlock(&Giant); 642fb99ab88SMatthew Dillon error = copyout(&siglist, uap->set, sizeof(sigset_t)); 643fb99ab88SMatthew Dillon return(error); 6442c42a146SMarcel Moolenaar } 6452c42a146SMarcel Moolenaar 64631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6472c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6482c42a146SMarcel Moolenaar struct osigpending_args { 6492c42a146SMarcel Moolenaar int dummy; 6502c42a146SMarcel Moolenaar }; 6512c42a146SMarcel Moolenaar #endif 652fb99ab88SMatthew Dillon /* 653fb99ab88SMatthew Dillon * MPSAFE 654fb99ab88SMatthew Dillon */ 6552c42a146SMarcel Moolenaar /* ARGSUSED */ 6562c42a146SMarcel Moolenaar int 657b40ce416SJulian Elischer osigpending(td, uap) 658b40ce416SJulian Elischer struct thread *td; 6592c42a146SMarcel Moolenaar struct osigpending_args *uap; 6602c42a146SMarcel Moolenaar { 661b40ce416SJulian Elischer struct proc *p = td->td_proc; 662b40ce416SJulian Elischer 663fb99ab88SMatthew Dillon mtx_lock(&Giant); 664628d2653SJohn Baldwin PROC_LOCK(p); 665b40ce416SJulian Elischer SIG2OSIG(p->p_siglist, td->td_retval[0]); 666628d2653SJohn Baldwin PROC_UNLOCK(p); 667fb99ab88SMatthew Dillon mtx_unlock(&Giant); 668df8bae1dSRodney W. Grimes return (0); 669df8bae1dSRodney W. Grimes } 67031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 673df8bae1dSRodney W. Grimes /* 674df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 675df8bae1dSRodney W. Grimes */ 676d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 677df8bae1dSRodney W. Grimes struct osigvec_args { 678df8bae1dSRodney W. Grimes int signum; 679df8bae1dSRodney W. Grimes struct sigvec *nsv; 680df8bae1dSRodney W. Grimes struct sigvec *osv; 681df8bae1dSRodney W. Grimes }; 682d2d3e875SBruce Evans #endif 683fb99ab88SMatthew Dillon /* 684fb99ab88SMatthew Dillon * MPSAFE 685fb99ab88SMatthew Dillon */ 686df8bae1dSRodney W. Grimes /* ARGSUSED */ 68726f9a767SRodney W. Grimes int 688b40ce416SJulian Elischer osigvec(td, uap) 689b40ce416SJulian Elischer struct thread *td; 690df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 691df8bae1dSRodney W. Grimes { 692b40ce416SJulian Elischer struct proc *p = td->td_proc; 693df8bae1dSRodney W. Grimes struct sigvec vec; 6942c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6952c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 6962c42a146SMarcel Moolenaar int error; 697df8bae1dSRodney W. Grimes 6986f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 6996f841fb7SMarcel Moolenaar return (EINVAL); 7006f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 7016f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 7022c42a146SMarcel Moolenaar if (nsap) { 7036f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 7042c42a146SMarcel Moolenaar if (error) 705df8bae1dSRodney W. Grimes return (error); 7062c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 7072c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 7082c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 7092c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 710df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 7112c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 712df8bae1dSRodney W. Grimes #endif 713df8bae1dSRodney W. Grimes } 714fb99ab88SMatthew Dillon mtx_lock(&Giant); 715645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 716fb99ab88SMatthew Dillon mtx_unlock(&Giant); 7172c42a146SMarcel Moolenaar if (osap && !error) { 7182c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 7192c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 7202c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 7212c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 7222c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 7232c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 7242c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 7252c42a146SMarcel Moolenaar #endif 7266f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 7272c42a146SMarcel Moolenaar } 7282c42a146SMarcel Moolenaar return (error); 729df8bae1dSRodney W. Grimes } 730df8bae1dSRodney W. Grimes 731d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 732df8bae1dSRodney W. Grimes struct osigblock_args { 733df8bae1dSRodney W. Grimes int mask; 734df8bae1dSRodney W. Grimes }; 735d2d3e875SBruce Evans #endif 736fb99ab88SMatthew Dillon /* 737fb99ab88SMatthew Dillon * MPSAFE 738fb99ab88SMatthew Dillon */ 73926f9a767SRodney W. Grimes int 740b40ce416SJulian Elischer osigblock(td, uap) 741b40ce416SJulian Elischer register struct thread *td; 742df8bae1dSRodney W. Grimes struct osigblock_args *uap; 743df8bae1dSRodney W. Grimes { 744b40ce416SJulian Elischer struct proc *p = td->td_proc; 7452c42a146SMarcel Moolenaar sigset_t set; 746df8bae1dSRodney W. Grimes 7472c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7482c42a146SMarcel Moolenaar SIG_CANTMASK(set); 749fb99ab88SMatthew Dillon mtx_lock(&Giant); 750628d2653SJohn Baldwin PROC_LOCK(p); 751b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 7522c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 753628d2653SJohn Baldwin PROC_UNLOCK(p); 754fb99ab88SMatthew Dillon mtx_unlock(&Giant); 755df8bae1dSRodney W. Grimes return (0); 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes 758d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 759df8bae1dSRodney W. Grimes struct osigsetmask_args { 760df8bae1dSRodney W. Grimes int mask; 761df8bae1dSRodney W. Grimes }; 762d2d3e875SBruce Evans #endif 763fb99ab88SMatthew Dillon /* 764fb99ab88SMatthew Dillon * MPSAFE 765fb99ab88SMatthew Dillon */ 76626f9a767SRodney W. Grimes int 767b40ce416SJulian Elischer osigsetmask(td, uap) 768b40ce416SJulian Elischer struct thread *td; 769df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 770df8bae1dSRodney W. Grimes { 771b40ce416SJulian Elischer struct proc *p = td->td_proc; 7722c42a146SMarcel Moolenaar sigset_t set; 773df8bae1dSRodney W. Grimes 7742c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7752c42a146SMarcel Moolenaar SIG_CANTMASK(set); 776fb99ab88SMatthew Dillon mtx_lock(&Giant); 777628d2653SJohn Baldwin PROC_LOCK(p); 778b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 779645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 78079065dbaSBruce Evans signotify(p); 781628d2653SJohn Baldwin PROC_UNLOCK(p); 782fb99ab88SMatthew Dillon mtx_unlock(&Giant); 783df8bae1dSRodney W. Grimes return (0); 784df8bae1dSRodney W. Grimes } 785df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes /* 788df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 789df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 790df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 791b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 792b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 793b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 794df8bae1dSRodney W. Grimes */ 795d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 796df8bae1dSRodney W. Grimes struct sigsuspend_args { 7972c42a146SMarcel Moolenaar const sigset_t *sigmask; 798df8bae1dSRodney W. Grimes }; 799d2d3e875SBruce Evans #endif 800fb99ab88SMatthew Dillon /* 801fb99ab88SMatthew Dillon * MPSAFE 802fb99ab88SMatthew Dillon */ 803df8bae1dSRodney W. Grimes /* ARGSUSED */ 80426f9a767SRodney W. Grimes int 805b40ce416SJulian Elischer sigsuspend(td, uap) 806b40ce416SJulian Elischer struct thread *td; 807df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 808df8bae1dSRodney W. Grimes { 809b40ce416SJulian Elischer struct proc *p = td->td_proc; 8102c42a146SMarcel Moolenaar sigset_t mask; 811628d2653SJohn Baldwin register struct sigacts *ps; 8122c42a146SMarcel Moolenaar int error; 8132c42a146SMarcel Moolenaar 8146f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 8152c42a146SMarcel Moolenaar if (error) 8162c42a146SMarcel Moolenaar return (error); 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes /* 819645682fdSLuoqi Chen * When returning from sigsuspend, we want 820df8bae1dSRodney W. Grimes * the old mask to be restored after the 821df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 822df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 823df8bae1dSRodney W. Grimes * to indicate this. 824df8bae1dSRodney W. Grimes */ 825fb99ab88SMatthew Dillon mtx_lock(&Giant); 826628d2653SJohn Baldwin PROC_LOCK(p); 827628d2653SJohn Baldwin ps = p->p_sigacts; 8286626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 829645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 830645682fdSLuoqi Chen 831645682fdSLuoqi Chen SIG_CANTMASK(mask); 8322c42a146SMarcel Moolenaar p->p_sigmask = mask; 83379065dbaSBruce Evans signotify(p); 83401609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 8352c42a146SMarcel Moolenaar /* void */; 836628d2653SJohn Baldwin PROC_UNLOCK(p); 837fb99ab88SMatthew Dillon mtx_unlock(&Giant); 8382c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 8392c42a146SMarcel Moolenaar return (EINTR); 8402c42a146SMarcel Moolenaar } 8412c42a146SMarcel Moolenaar 84231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8432c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8442c42a146SMarcel Moolenaar struct osigsuspend_args { 8452c42a146SMarcel Moolenaar osigset_t mask; 8462c42a146SMarcel Moolenaar }; 8472c42a146SMarcel Moolenaar #endif 848fb99ab88SMatthew Dillon /* 849fb99ab88SMatthew Dillon * MPSAFE 850fb99ab88SMatthew Dillon */ 8512c42a146SMarcel Moolenaar /* ARGSUSED */ 8522c42a146SMarcel Moolenaar int 853b40ce416SJulian Elischer osigsuspend(td, uap) 854b40ce416SJulian Elischer struct thread *td; 8552c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 8562c42a146SMarcel Moolenaar { 857b40ce416SJulian Elischer struct proc *p = td->td_proc; 858645682fdSLuoqi Chen sigset_t mask; 859628d2653SJohn Baldwin register struct sigacts *ps; 8602c42a146SMarcel Moolenaar 861fb99ab88SMatthew Dillon mtx_lock(&Giant); 862628d2653SJohn Baldwin PROC_LOCK(p); 863628d2653SJohn Baldwin ps = p->p_sigacts; 8642c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 865645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 866645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 867645682fdSLuoqi Chen SIG_CANTMASK(mask); 868645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 86979065dbaSBruce Evans signotify(p); 87001609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 871df8bae1dSRodney W. Grimes /* void */; 872628d2653SJohn Baldwin PROC_UNLOCK(p); 873fb99ab88SMatthew Dillon mtx_unlock(&Giant); 874df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 875df8bae1dSRodney W. Grimes return (EINTR); 876df8bae1dSRodney W. Grimes } 87731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 878df8bae1dSRodney W. Grimes 879df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 880d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 881df8bae1dSRodney W. Grimes struct osigstack_args { 882df8bae1dSRodney W. Grimes struct sigstack *nss; 883df8bae1dSRodney W. Grimes struct sigstack *oss; 884df8bae1dSRodney W. Grimes }; 885d2d3e875SBruce Evans #endif 886fb99ab88SMatthew Dillon /* 887fb99ab88SMatthew Dillon * MPSAFE 888fb99ab88SMatthew Dillon */ 889df8bae1dSRodney W. Grimes /* ARGSUSED */ 89026f9a767SRodney W. Grimes int 891b40ce416SJulian Elischer osigstack(td, uap) 892b40ce416SJulian Elischer struct thread *td; 893df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 894df8bae1dSRodney W. Grimes { 895b40ce416SJulian Elischer struct proc *p = td->td_proc; 896df8bae1dSRodney W. Grimes struct sigstack ss; 897fb99ab88SMatthew Dillon int error = 0; 898fb99ab88SMatthew Dillon 899fb99ab88SMatthew Dillon mtx_lock(&Giant); 900df8bae1dSRodney W. Grimes 901d034d459SMarcel Moolenaar if (uap->oss != NULL) { 902628d2653SJohn Baldwin PROC_LOCK(p); 903645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 904b40ce416SJulian Elischer ss.ss_onstack = sigonstack(cpu_getstack(td)); 905628d2653SJohn Baldwin PROC_UNLOCK(p); 906d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 907d034d459SMarcel Moolenaar if (error) 908fb99ab88SMatthew Dillon goto done2; 909d034d459SMarcel Moolenaar } 910d034d459SMarcel Moolenaar 911d034d459SMarcel Moolenaar if (uap->nss != NULL) { 912d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 913fb99ab88SMatthew Dillon goto done2; 914628d2653SJohn Baldwin PROC_LOCK(p); 915645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 916645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 917645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 918645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 919628d2653SJohn Baldwin PROC_UNLOCK(p); 920df8bae1dSRodney W. Grimes } 921fb99ab88SMatthew Dillon done2: 922fb99ab88SMatthew Dillon mtx_unlock(&Giant); 923fb99ab88SMatthew Dillon return (error); 924df8bae1dSRodney W. Grimes } 925df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 926df8bae1dSRodney W. Grimes 927d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 928df8bae1dSRodney W. Grimes struct sigaltstack_args { 9292c42a146SMarcel Moolenaar stack_t *ss; 9302c42a146SMarcel Moolenaar stack_t *oss; 931df8bae1dSRodney W. Grimes }; 932d2d3e875SBruce Evans #endif 933fb99ab88SMatthew Dillon /* 934fb99ab88SMatthew Dillon * MPSAFE 935fb99ab88SMatthew Dillon */ 936df8bae1dSRodney W. Grimes /* ARGSUSED */ 93726f9a767SRodney W. Grimes int 938b40ce416SJulian Elischer sigaltstack(td, uap) 939b40ce416SJulian Elischer struct thread *td; 940df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 941df8bae1dSRodney W. Grimes { 942b40ce416SJulian Elischer struct proc *p = td->td_proc; 9432c42a146SMarcel Moolenaar stack_t ss; 944fb99ab88SMatthew Dillon int oonstack; 945fb99ab88SMatthew Dillon int error = 0; 946fb99ab88SMatthew Dillon 947fb99ab88SMatthew Dillon mtx_lock(&Giant); 948df8bae1dSRodney W. Grimes 949b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 950d034d459SMarcel Moolenaar 951d034d459SMarcel Moolenaar if (uap->oss != NULL) { 952628d2653SJohn Baldwin PROC_LOCK(p); 953d034d459SMarcel Moolenaar ss = p->p_sigstk; 954d034d459SMarcel Moolenaar ss.ss_flags = (p->p_flag & P_ALTSTACK) 955d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 956628d2653SJohn Baldwin PROC_UNLOCK(p); 957d034d459SMarcel Moolenaar if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 958fb99ab88SMatthew Dillon goto done2; 959df8bae1dSRodney W. Grimes } 960d034d459SMarcel Moolenaar 961d034d459SMarcel Moolenaar if (uap->ss != NULL) { 962fb99ab88SMatthew Dillon if (oonstack) { 963fb99ab88SMatthew Dillon error = EPERM; 964fb99ab88SMatthew Dillon goto done2; 965fb99ab88SMatthew Dillon } 966d034d459SMarcel Moolenaar if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 967fb99ab88SMatthew Dillon goto done2; 968fb99ab88SMatthew Dillon if ((ss.ss_flags & ~SS_DISABLE) != 0) { 969fb99ab88SMatthew Dillon error = EINVAL; 970fb99ab88SMatthew Dillon goto done2; 971fb99ab88SMatthew Dillon } 972d034d459SMarcel Moolenaar if (!(ss.ss_flags & SS_DISABLE)) { 973fb99ab88SMatthew Dillon if (ss.ss_size < p->p_sysent->sv_minsigstksz) { 974fb99ab88SMatthew Dillon error = ENOMEM; 975fb99ab88SMatthew Dillon goto done2; 976fb99ab88SMatthew Dillon } 977628d2653SJohn Baldwin PROC_LOCK(p); 978645682fdSLuoqi Chen p->p_sigstk = ss; 979d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 980628d2653SJohn Baldwin PROC_UNLOCK(p); 981628d2653SJohn Baldwin } else { 982628d2653SJohn Baldwin PROC_LOCK(p); 983d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 984628d2653SJohn Baldwin PROC_UNLOCK(p); 985628d2653SJohn Baldwin } 986d034d459SMarcel Moolenaar } 987fb99ab88SMatthew Dillon done2: 988fb99ab88SMatthew Dillon mtx_unlock(&Giant); 989fb99ab88SMatthew Dillon return (error); 990df8bae1dSRodney W. Grimes } 991df8bae1dSRodney W. Grimes 992d93f860cSPoul-Henning Kamp /* 993d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 994d93f860cSPoul-Henning Kamp * cp is calling process. 995d93f860cSPoul-Henning Kamp */ 996d93f860cSPoul-Henning Kamp int 9979c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all) 9989c1ab3e0SJohn Baldwin register struct thread *td; 9992c42a146SMarcel Moolenaar int sig, pgid, all; 1000d93f860cSPoul-Henning Kamp { 1001d93f860cSPoul-Henning Kamp register struct proc *p; 1002d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1003d93f860cSPoul-Henning Kamp int nfound = 0; 1004d93f860cSPoul-Henning Kamp 1005553629ebSJake Burkholder if (all) { 1006d93f860cSPoul-Henning Kamp /* 1007d93f860cSPoul-Henning Kamp * broadcast 1008d93f860cSPoul-Henning Kamp */ 10091005a129SJohn Baldwin sx_slock(&allproc_lock); 10102e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1011628d2653SJohn Baldwin PROC_LOCK(p); 10129c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 10139c1ab3e0SJohn Baldwin p == td->td_proc) { 1014628d2653SJohn Baldwin PROC_UNLOCK(p); 1015628d2653SJohn Baldwin continue; 1016628d2653SJohn Baldwin } 1017f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1018d93f860cSPoul-Henning Kamp nfound++; 101933a9ed9dSJohn Baldwin if (sig) 10202c42a146SMarcel Moolenaar psignal(p, sig); 1021628d2653SJohn Baldwin } 102233a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1023d93f860cSPoul-Henning Kamp } 10241005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1025553629ebSJake Burkholder } else { 1026ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1027f591779bSSeigo Tanimura if (pgid == 0) { 1028d93f860cSPoul-Henning Kamp /* 1029d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1030d93f860cSPoul-Henning Kamp */ 10319c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1032f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1033f591779bSSeigo Tanimura } else { 1034d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1035f591779bSSeigo Tanimura if (pgrp == NULL) { 1036ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1037d93f860cSPoul-Henning Kamp return (ESRCH); 1038d93f860cSPoul-Henning Kamp } 1039f591779bSSeigo Tanimura } 1040ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 10412e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1042628d2653SJohn Baldwin PROC_LOCK(p); 1043628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1044628d2653SJohn Baldwin PROC_UNLOCK(p); 1045628d2653SJohn Baldwin continue; 1046628d2653SJohn Baldwin } 1047e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 104833a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1049628d2653SJohn Baldwin continue; 1050628d2653SJohn Baldwin } 1051f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1052d93f860cSPoul-Henning Kamp nfound++; 105333a9ed9dSJohn Baldwin if (sig) 10542c42a146SMarcel Moolenaar psignal(p, sig); 1055628d2653SJohn Baldwin } 105633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1057d93f860cSPoul-Henning Kamp } 1058f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1059d93f860cSPoul-Henning Kamp } 1060d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1061d93f860cSPoul-Henning Kamp } 1062d93f860cSPoul-Henning Kamp 1063d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1064df8bae1dSRodney W. Grimes struct kill_args { 1065df8bae1dSRodney W. Grimes int pid; 1066df8bae1dSRodney W. Grimes int signum; 1067df8bae1dSRodney W. Grimes }; 1068d2d3e875SBruce Evans #endif 1069fb99ab88SMatthew Dillon /* 1070fb99ab88SMatthew Dillon * MPSAFE 1071fb99ab88SMatthew Dillon */ 1072df8bae1dSRodney W. Grimes /* ARGSUSED */ 107326f9a767SRodney W. Grimes int 1074b40ce416SJulian Elischer kill(td, uap) 1075b40ce416SJulian Elischer register struct thread *td; 1076df8bae1dSRodney W. Grimes register struct kill_args *uap; 1077df8bae1dSRodney W. Grimes { 1078df8bae1dSRodney W. Grimes register struct proc *p; 1079fb99ab88SMatthew Dillon int error = 0; 1080df8bae1dSRodney W. Grimes 10816c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1082df8bae1dSRodney W. Grimes return (EINVAL); 1083fb99ab88SMatthew Dillon 1084fb99ab88SMatthew Dillon mtx_lock(&Giant); 1085df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1086df8bae1dSRodney W. Grimes /* kill single process */ 1087fb99ab88SMatthew Dillon if ((p = pfind(uap->pid)) == NULL) { 1088fb99ab88SMatthew Dillon error = ESRCH; 1089f44d9e24SJohn Baldwin } else if ((error = p_cansignal(td, p, uap->signum)) != 0) { 109033a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1091fb99ab88SMatthew Dillon } else { 109233a9ed9dSJohn Baldwin if (uap->signum) 1093df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1094628d2653SJohn Baldwin PROC_UNLOCK(p); 1095fb99ab88SMatthew Dillon error = 0; 1096df8bae1dSRodney W. Grimes } 1097fb99ab88SMatthew Dillon } else { 1098df8bae1dSRodney W. Grimes switch (uap->pid) { 1099df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 11009c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 1); 1101fb99ab88SMatthew Dillon break; 1102df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 11039c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 0); 1104fb99ab88SMatthew Dillon break; 1105df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 11069c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, -uap->pid, 0); 1107fb99ab88SMatthew Dillon break; 1108df8bae1dSRodney W. Grimes } 1109fb99ab88SMatthew Dillon } 1110fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1111fb99ab88SMatthew Dillon return(error); 1112df8bae1dSRodney W. Grimes } 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1116df8bae1dSRodney W. Grimes struct okillpg_args { 1117df8bae1dSRodney W. Grimes int pgid; 1118df8bae1dSRodney W. Grimes int signum; 1119df8bae1dSRodney W. Grimes }; 1120d2d3e875SBruce Evans #endif 1121fb99ab88SMatthew Dillon /* 1122fb99ab88SMatthew Dillon * MPSAFE 1123fb99ab88SMatthew Dillon */ 1124df8bae1dSRodney W. Grimes /* ARGSUSED */ 112526f9a767SRodney W. Grimes int 1126b40ce416SJulian Elischer okillpg(td, uap) 1127b40ce416SJulian Elischer struct thread *td; 1128df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1129df8bae1dSRodney W. Grimes { 1130fb99ab88SMatthew Dillon int error; 1131df8bae1dSRodney W. Grimes 11326c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1133df8bae1dSRodney W. Grimes return (EINVAL); 1134fb99ab88SMatthew Dillon mtx_lock(&Giant); 11359c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, uap->pgid, 0); 1136fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1137fb99ab88SMatthew Dillon return (error); 1138df8bae1dSRodney W. Grimes } 1139df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1140df8bae1dSRodney W. Grimes 1141df8bae1dSRodney W. Grimes /* 1142df8bae1dSRodney W. Grimes * Send a signal to a process group. 1143df8bae1dSRodney W. Grimes */ 1144df8bae1dSRodney W. Grimes void 11452c42a146SMarcel Moolenaar gsignal(pgid, sig) 11462c42a146SMarcel Moolenaar int pgid, sig; 1147df8bae1dSRodney W. Grimes { 1148df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1149df8bae1dSRodney W. Grimes 1150f591779bSSeigo Tanimura if (pgid != 0) { 1151ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1152f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1153ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1154f591779bSSeigo Tanimura if (pgrp != NULL) { 11552c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1156f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1157f591779bSSeigo Tanimura } 1158f591779bSSeigo Tanimura } 1159df8bae1dSRodney W. Grimes } 1160df8bae1dSRodney W. Grimes 1161df8bae1dSRodney W. Grimes /* 1162df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1163df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1164df8bae1dSRodney W. Grimes */ 1165df8bae1dSRodney W. Grimes void 11662c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1167df8bae1dSRodney W. Grimes struct pgrp *pgrp; 11682c42a146SMarcel Moolenaar int sig, checkctty; 1169df8bae1dSRodney W. Grimes { 1170df8bae1dSRodney W. Grimes register struct proc *p; 1171df8bae1dSRodney W. Grimes 1172628d2653SJohn Baldwin if (pgrp) { 1173f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1174628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1175628d2653SJohn Baldwin PROC_LOCK(p); 1176df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 11772c42a146SMarcel Moolenaar psignal(p, sig); 1178628d2653SJohn Baldwin PROC_UNLOCK(p); 1179628d2653SJohn Baldwin } 1180628d2653SJohn Baldwin } 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes 1183df8bae1dSRodney W. Grimes /* 1184df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1185df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1186df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1187356861dbSMatthew Dillon * 1188356861dbSMatthew Dillon * MPSAFE 1189df8bae1dSRodney W. Grimes */ 1190df8bae1dSRodney W. Grimes void 11912c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1192df8bae1dSRodney W. Grimes struct proc *p; 11932c42a146SMarcel Moolenaar register int sig; 11948674077aSJeffrey Hsu u_long code; 1195df8bae1dSRodney W. Grimes { 1196df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1197df8bae1dSRodney W. Grimes 1198628d2653SJohn Baldwin PROC_LOCK(p); 11992c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1200d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1201df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1202df8bae1dSRodney W. Grimes #ifdef KTRACE 1203374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1204374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 12052c42a146SMarcel Moolenaar &p->p_sigmask, code); 1206df8bae1dSRodney W. Grimes #endif 12072c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 12082c42a146SMarcel Moolenaar &p->p_sigmask, code); 12092c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 12102c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 12112c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 12122c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1213289ccde0SPeter Wemm /* 12142c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1215289ccde0SPeter Wemm */ 12162c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 12172c42a146SMarcel Moolenaar if (sig != SIGCONT && 12182c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 12192c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 12202c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1221dedc04feSPeter Wemm } 12226f841fb7SMarcel Moolenaar } else { 12236626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 12242c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 12252c42a146SMarcel Moolenaar psignal(p, sig); 1226df8bae1dSRodney W. Grimes } 1227628d2653SJohn Baldwin PROC_UNLOCK(p); 1228df8bae1dSRodney W. Grimes } 1229df8bae1dSRodney W. Grimes 1230df8bae1dSRodney W. Grimes /* 1231df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1232df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1233df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1234df8bae1dSRodney W. Grimes * 1235df8bae1dSRodney W. Grimes * Exceptions: 1236df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1237df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1238df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1239df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1240df8bae1dSRodney W. Grimes * 1241df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1242df8bae1dSRodney W. Grimes */ 1243df8bae1dSRodney W. Grimes void 12442c42a146SMarcel Moolenaar psignal(p, sig) 1245df8bae1dSRodney W. Grimes register struct proc *p; 12462c42a146SMarcel Moolenaar register int sig; 1247df8bae1dSRodney W. Grimes { 1248df8bae1dSRodney W. Grimes register sig_t action; 1249b40ce416SJulian Elischer struct thread *td; 1250e602ba25SJulian Elischer register int prop; 1251e602ba25SJulian Elischer 1252df8bae1dSRodney W. Grimes 12532899d606SDag-Erling Smørgrav KASSERT(_SIG_VALID(sig), 12542899d606SDag-Erling Smørgrav ("psignal(): invalid signal %d\n", sig)); 12552c42a146SMarcel Moolenaar 1256628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1257cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1258cb679c38SJonathan Lemon 12592c42a146SMarcel Moolenaar prop = sigprop(sig); 1260df8bae1dSRodney W. Grimes /* 12612a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 12622a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 12632a024a2bSSean Eric Fagan * a chance, as well. 1264df8bae1dSRodney W. Grimes */ 1265b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1266df8bae1dSRodney W. Grimes action = SIG_DFL; 1267b40ce416SJulian Elischer } else { 1268df8bae1dSRodney W. Grimes /* 1269df8bae1dSRodney W. Grimes * If the signal is being ignored, 1270df8bae1dSRodney W. Grimes * then we forget about it immediately. 1271df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1272df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1273df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1274df8bae1dSRodney W. Grimes */ 1275628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1276df8bae1dSRodney W. Grimes return; 12772c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1278df8bae1dSRodney W. Grimes action = SIG_HOLD; 12792c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1280df8bae1dSRodney W. Grimes action = SIG_CATCH; 1281df8bae1dSRodney W. Grimes else 1282df8bae1dSRodney W. Grimes action = SIG_DFL; 1283df8bae1dSRodney W. Grimes } 1284df8bae1dSRodney W. Grimes 1285df8bae1dSRodney W. Grimes if (prop & SA_CONT) 12862c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1287df8bae1dSRodney W. Grimes 1288df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1289df8bae1dSRodney W. Grimes /* 1290df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1291df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1292df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1293df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1294df8bae1dSRodney W. Grimes */ 1295e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1296e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1297e602ba25SJulian Elischer (action == SIG_DFL)) 1298df8bae1dSRodney W. Grimes return; 12992c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 1300df8bae1dSRodney W. Grimes } 13012c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1302aa0fa334SJulian Elischer signotify(p); /* uses schedlock */ 1303df8bae1dSRodney W. Grimes 1304df8bae1dSRodney W. Grimes /* 1305e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1306e602ba25SJulian Elischer * component. Most processing occurs when the process next 1307e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1308e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1309e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1310e602ba25SJulian Elischer * We try do the per-process part here. 1311df8bae1dSRodney W. Grimes */ 1312e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1313e602ba25SJulian Elischer /* 1314e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1315e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1316e602ba25SJulian Elischer */ 1317e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1318e602ba25SJulian Elischer /* 1319e602ba25SJulian Elischer * The traced process is already stopped, 1320e602ba25SJulian Elischer * so no further action is necessary. 1321e602ba25SJulian Elischer * No signal can restart us. 1322e602ba25SJulian Elischer */ 1323e602ba25SJulian Elischer goto out; 13241c32c37cSJohn Baldwin } 1325b40ce416SJulian Elischer 1326e602ba25SJulian Elischer if (sig == SIGKILL) { 1327df8bae1dSRodney W. Grimes /* 1328e602ba25SJulian Elischer * SIGKILL sets process running. 1329e602ba25SJulian Elischer * It will die elsewhere. 1330e602ba25SJulian Elischer * All threads must be restarted. 1331df8bae1dSRodney W. Grimes */ 1332e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED; 1333e602ba25SJulian Elischer goto runfast; 1334e602ba25SJulian Elischer } 1335e602ba25SJulian Elischer 1336e602ba25SJulian Elischer if (prop & SA_CONT) { 1337e602ba25SJulian Elischer /* 1338e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 1339e602ba25SJulian Elischer * process but don't leave the signal in p_siglist as 1340e602ba25SJulian Elischer * it has no further action. If SIGCONT is held, we 1341e602ba25SJulian Elischer * continue the process and leave the signal in 1342e602ba25SJulian Elischer * p_siglist. If the process catches SIGCONT, let it 1343e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1344e602ba25SJulian Elischer * an event, it goes back to run state. 1345e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1346e602ba25SJulian Elischer */ 1347e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_SGNL; 1348e602ba25SJulian Elischer if (action == SIG_DFL) { 1349e602ba25SJulian Elischer SIGDELSET(p->p_siglist, sig); 1350e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1351e602ba25SJulian Elischer /* 1352e602ba25SJulian Elischer * The process wants to catch it so it needs 1353e602ba25SJulian Elischer * to run at least one thread, but which one? 1354e602ba25SJulian Elischer * It would seem that the answer would be to 1355e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1356e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1357e602ba25SJulian Elischer * process, we need to make sure that the 1358e602ba25SJulian Elischer * single thread is runnable asap. 1359e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1360e602ba25SJulian Elischer */ 1361e602ba25SJulian Elischer goto runfast; 1362e602ba25SJulian Elischer } 1363e602ba25SJulian Elischer /* 1364e602ba25SJulian Elischer * The signal is not ignored or caught. 1365e602ba25SJulian Elischer */ 1366e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 136704774f23SJulian Elischer thread_unsuspend(p); 1368e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1369e602ba25SJulian Elischer goto out; 1370e602ba25SJulian Elischer } 1371e602ba25SJulian Elischer 1372e602ba25SJulian Elischer if (prop & SA_STOP) { 1373e602ba25SJulian Elischer /* 1374e602ba25SJulian Elischer * Already stopped, don't need to stop again 1375e602ba25SJulian Elischer * (If we did the shell could get confused). 137604774f23SJulian Elischer * Just make sure the signal STOP bit set. 1377e602ba25SJulian Elischer */ 137804774f23SJulian Elischer p->p_flag |= P_STOPPED_SGNL; 1379e602ba25SJulian Elischer SIGDELSET(p->p_siglist, sig); 1380e602ba25SJulian Elischer goto out; 1381e602ba25SJulian Elischer } 1382e602ba25SJulian Elischer 1383e602ba25SJulian Elischer /* 1384e602ba25SJulian Elischer * All other kinds of signals: 1385e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1386e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1387e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 138804774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1389e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1390e602ba25SJulian Elischer */ 1391e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 1392e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1393e602ba25SJulian Elischer if (td->td_wchan && (td->td_flags & TDF_SINTR)) { 1394e602ba25SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 1395e602ba25SJulian Elischer cv_waitq_remove(td); 1396e602ba25SJulian Elischer else 1397e602ba25SJulian Elischer unsleep(td); 1398e602ba25SJulian Elischer setrunnable(td); 1399e602ba25SJulian Elischer } 1400e602ba25SJulian Elischer } 1401e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1402df8bae1dSRodney W. Grimes goto out; 1403df8bae1dSRodney W. Grimes /* 1404e602ba25SJulian Elischer * XXXKSE What about threads that are waiting on mutexes? 1405e602ba25SJulian Elischer * Shouldn't they abort too? 140604774f23SJulian Elischer * No, hopefully mutexes are short lived.. They'll 140704774f23SJulian Elischer * eventually hit thread_suspend_check(). 1408df8bae1dSRodney W. Grimes */ 1409e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 1410e602ba25SJulian Elischer if (prop & SA_CONT) { 1411df8bae1dSRodney W. Grimes /* 1412e602ba25SJulian Elischer * Already active, don't need to start again. 1413df8bae1dSRodney W. Grimes */ 14142c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1415df8bae1dSRodney W. Grimes goto out; 1416df8bae1dSRodney W. Grimes } 1417df8bae1dSRodney W. Grimes /* 1418df8bae1dSRodney W. Grimes * When a sleeping process receives a stop 1419df8bae1dSRodney W. Grimes * signal, process immediately if possible. 1420df8bae1dSRodney W. Grimes * All other (caught or default) signals 1421df8bae1dSRodney W. Grimes * cause the process to run. 1422df8bae1dSRodney W. Grimes */ 1423df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 142404774f23SJulian Elischer int should_signal = 1; 14253e6831f5SJohn Baldwin if (action != SIG_DFL) 1426df8bae1dSRodney W. Grimes goto runfast; 1427e602ba25SJulian Elischer 1428df8bae1dSRodney W. Grimes /* 1429df8bae1dSRodney W. Grimes * If a child holding parent blocked, 1430df8bae1dSRodney W. Grimes * stopping could cause deadlock. 1431df8bae1dSRodney W. Grimes */ 1432df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 1433df8bae1dSRodney W. Grimes goto out; 14342c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 14352c42a146SMarcel Moolenaar p->p_xstat = sig; 143604774f23SJulian Elischer PROC_LOCK(p->p_pptr); /* XXX un-needed? */ 143704774f23SJulian Elischer #if 0 143804774f23SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 143904774f23SJulian Elischer if (td->td_state == TDS_RUNNING) { 144004774f23SJulian Elischer /* 144104774f23SJulian Elischer * all other states must be in 144204774f23SJulian Elischer * the kernel 144304774f23SJulian Elischer */ 144404774f23SJulian Elischer should_signal = 0; 144504774f23SJulian Elischer break; 144604774f23SJulian Elischer } 144704774f23SJulian Elischer } 144804774f23SJulian Elischer /* don't enable until the equivalent code is in thread_suspend_check() */ 144904774f23SJulian Elischer #endif 145004774f23SJulian Elischer if (!(p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) && 145104774f23SJulian Elischer should_signal) 1452df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1453628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1454df8bae1dSRodney W. Grimes stop(p); 1455df8bae1dSRodney W. Grimes goto out; 14563e6831f5SJohn Baldwin } else 1457df8bae1dSRodney W. Grimes goto runfast; 1458df8bae1dSRodney W. Grimes /* NOTREACHED */ 1459e602ba25SJulian Elischer } else { 1460e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 14612c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1462e602ba25SJulian Elischer goto out; 1463e602ba25SJulian Elischer } 1464e602ba25SJulian Elischer 1465b40ce416SJulian Elischer /* 1466e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1467e602ba25SJulian Elischer * running threads. 1468b40ce416SJulian Elischer */ 1469e602ba25SJulian Elischer 1470e602ba25SJulian Elischer runfast: 1471aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 1472e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) 1473e602ba25SJulian Elischer tdsignal(td, sig, action); 1474e602ba25SJulian Elischer thread_unsuspend(p); 1475e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1476e602ba25SJulian Elischer out: 1477e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1478e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1479e602ba25SJulian Elischer } 1480e602ba25SJulian Elischer 1481e602ba25SJulian Elischer /* 1482e602ba25SJulian Elischer * The force of a signal has been directed against a single 1483e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1484e602ba25SJulian Elischer * out of any sleep it may be in etc. 1485e602ba25SJulian Elischer */ 1486e602ba25SJulian Elischer static void 1487e602ba25SJulian Elischer tdsignal(struct thread *td, int sig, sig_t action) 1488e602ba25SJulian Elischer { 1489e602ba25SJulian Elischer struct proc *p = td->td_proc; 1490e602ba25SJulian Elischer register int prop; 1491e602ba25SJulian Elischer 1492aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1493e602ba25SJulian Elischer prop = sigprop(sig); 1494e602ba25SJulian Elischer /* 1495aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1496e602ba25SJulian Elischer * killed in this lifetime. 1497e602ba25SJulian Elischer */ 1498e602ba25SJulian Elischer if ((action == SIG_DFL) && (prop & SA_KILL)) { 1499e602ba25SJulian Elischer if (td->td_priority > PUSER) { 1500e602ba25SJulian Elischer td->td_priority = PUSER; 1501b40ce416SJulian Elischer } 1502b40ce416SJulian Elischer } 1503e602ba25SJulian Elischer 1504e602ba25SJulian Elischer /* 1505e602ba25SJulian Elischer * Defer further processing for signals which are held, 1506e602ba25SJulian Elischer * except that stopped processes must be continued by SIGCONT. 1507e602ba25SJulian Elischer */ 1508e602ba25SJulian Elischer if (action == SIG_HOLD) { 1509aa0fa334SJulian Elischer return; 1510e602ba25SJulian Elischer } 1511e602ba25SJulian Elischer if (td->td_state == TDS_SLP) { 1512e602ba25SJulian Elischer /* 1513e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1514e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1515e602ba25SJulian Elischer * be noticed when the process returns through 1516e602ba25SJulian Elischer * trap() or syscall(). 1517e602ba25SJulian Elischer */ 1518e602ba25SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 1519aa0fa334SJulian Elischer return; 1520e602ba25SJulian Elischer } 1521e602ba25SJulian Elischer /* 1522e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1523e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1524e602ba25SJulian Elischer * for its parent. 1525e602ba25SJulian Elischer */ 1526e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1527e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1528aa0fa334SJulian Elischer } else { 1529aa0fa334SJulian Elischer 1530df8bae1dSRodney W. Grimes /* 1531e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1532e602ba25SJulian Elischer * asleep, we are finished; the process should not 1533e602ba25SJulian Elischer * be awakened. 1534df8bae1dSRodney W. Grimes */ 1535e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 15362c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1537aa0fa334SJulian Elischer return; 1538df8bae1dSRodney W. Grimes } 1539df8bae1dSRodney W. Grimes 1540aa0fa334SJulian Elischer /* 1541aa0fa334SJulian Elischer * Raise priority to at least PUSER. 1542aa0fa334SJulian Elischer */ 1543aa0fa334SJulian Elischer if (td->td_priority > PUSER) { 1544aa0fa334SJulian Elischer td->td_priority = PUSER; 1545aa0fa334SJulian Elischer } 1546aa0fa334SJulian Elischer } 1547aa0fa334SJulian Elischer setrunnable(td); 1548aa0fa334SJulian Elischer } 1549aa0fa334SJulian Elischer #ifdef SMP 1550aa0fa334SJulian Elischer else { 1551df8bae1dSRodney W. Grimes /* 1552e602ba25SJulian Elischer * Other states do nothing with the signal immediatly, 1553df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1554df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1555df8bae1dSRodney W. Grimes */ 15560ac3b636SAndrew Gallatin if (td->td_state == TDS_RUNNING && td != curthread) { 1557e602ba25SJulian Elischer forward_signal(td); 1558aa0fa334SJulian Elischer } 15590ac3b636SAndrew Gallatin } 15603163861cSTor Egge #endif 15616caa8a15SJohn Baldwin } 1562df8bae1dSRodney W. Grimes 1563df8bae1dSRodney W. Grimes /* 1564df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1565df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1566df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1567df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1568df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1569628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 1570df8bae1dSRodney W. Grimes * sequence is 1571df8bae1dSRodney W. Grimes * 1572e602ba25SJulian Elischer * while (sig = cursig(curthread)) 15732c42a146SMarcel Moolenaar * postsig(sig); 1574df8bae1dSRodney W. Grimes */ 157526f9a767SRodney W. Grimes int 1576e602ba25SJulian Elischer issignal(td) 1577e602ba25SJulian Elischer struct thread *td; 1578df8bae1dSRodney W. Grimes { 1579e602ba25SJulian Elischer struct proc *p; 15802c42a146SMarcel Moolenaar sigset_t mask; 15812c42a146SMarcel Moolenaar register int sig, prop; 1582df8bae1dSRodney W. Grimes 1583e602ba25SJulian Elischer p = td->td_proc; 1584628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1585df8bae1dSRodney W. Grimes for (;;) { 15862a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 15872a024a2bSSean Eric Fagan 15882c42a146SMarcel Moolenaar mask = p->p_siglist; 15892c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1590df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 15912c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 1592ca18d53eSChad David if (SIGISEMPTY(mask)) /* no signal to send */ 1593df8bae1dSRodney W. Grimes return (0); 15942c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 15952c42a146SMarcel Moolenaar prop = sigprop(sig); 15962a024a2bSSean Eric Fagan 1597628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 15982a024a2bSSean Eric Fagan 1599df8bae1dSRodney W. Grimes /* 1600df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1601df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1602df8bae1dSRodney W. Grimes */ 16032c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 16042c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1605df8bae1dSRodney W. Grimes continue; 1606df8bae1dSRodney W. Grimes } 1607df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1608df8bae1dSRodney W. Grimes /* 1609d8f4f6a4SJonathan Mini * If traced, always stop. 1610df8bae1dSRodney W. Grimes */ 16112c42a146SMarcel Moolenaar p->p_xstat = sig; 1612628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1613df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1614628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 16159ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 16164d492b43SJulian Elischer stop(p); /* uses schedlock too eventually */ 1617e602ba25SJulian Elischer td->td_state = TDS_UNQUEUED; 1618c86b6ff5SJohn Baldwin PROC_UNLOCK(p); 1619c86b6ff5SJohn Baldwin DROP_GIANT(); 16202ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1621df8bae1dSRodney W. Grimes mi_switch(); 16229ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 162320cdcc5bSJohn Baldwin PICKUP_GIANT(); 1624628d2653SJohn Baldwin PROC_LOCK(p); 1625df8bae1dSRodney W. Grimes 1626df8bae1dSRodney W. Grimes /* 1627df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1628df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1629df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1630df8bae1dSRodney W. Grimes */ 1631df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1632df8bae1dSRodney W. Grimes continue; 1633df8bae1dSRodney W. Grimes 1634df8bae1dSRodney W. Grimes /* 1635df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1636df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1637df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1638df8bae1dSRodney W. Grimes */ 16392c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 16402c42a146SMarcel Moolenaar sig = p->p_xstat; 16412c42a146SMarcel Moolenaar if (sig == 0) 1642df8bae1dSRodney W. Grimes continue; 1643df8bae1dSRodney W. Grimes 1644df8bae1dSRodney W. Grimes /* 1645df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1646df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1647df8bae1dSRodney W. Grimes */ 16482c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 16492c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1650df8bae1dSRodney W. Grimes continue; 1651df8bae1dSRodney W. Grimes } 1652df8bae1dSRodney W. Grimes 1653df8bae1dSRodney W. Grimes /* 1654df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1655df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1656df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1657df8bae1dSRodney W. Grimes */ 16582c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1659df8bae1dSRodney W. Grimes 16600b53fbe8SBruce Evans case (int)SIG_DFL: 1661df8bae1dSRodney W. Grimes /* 1662df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1663df8bae1dSRodney W. Grimes */ 1664df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1665df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1666df8bae1dSRodney W. Grimes /* 1667df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1668df8bae1dSRodney W. Grimes * in init? XXX 1669df8bae1dSRodney W. Grimes */ 1670d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 16712c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1672df8bae1dSRodney W. Grimes #endif 1673df8bae1dSRodney W. Grimes break; /* == ignore */ 1674df8bae1dSRodney W. Grimes } 1675df8bae1dSRodney W. Grimes /* 1676df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1677df8bae1dSRodney W. Grimes * with default action, stop here, 1678df8bae1dSRodney W. Grimes * then clear the signal. However, 1679df8bae1dSRodney W. Grimes * if process is member of an orphaned 1680df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1681df8bae1dSRodney W. Grimes */ 1682df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1683df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1684df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1685df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1686df8bae1dSRodney W. Grimes break; /* == ignore */ 16872c42a146SMarcel Moolenaar p->p_xstat = sig; 1688628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1689e602ba25SJulian Elischer if ((p->p_pptr->p_procsig->ps_flag & 1690e602ba25SJulian Elischer PS_NOCLDSTOP) == 0) { 1691df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1692e602ba25SJulian Elischer } 1693628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 16945b3047d5SJohn Baldwin stop(p); 1695df8bae1dSRodney W. Grimes break; 1696e602ba25SJulian Elischer } else 1697e602ba25SJulian Elischer if (prop & SA_IGNORE) { 1698df8bae1dSRodney W. Grimes /* 1699df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1700df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1701df8bae1dSRodney W. Grimes */ 1702df8bae1dSRodney W. Grimes break; /* == ignore */ 1703df8bae1dSRodney W. Grimes } else 17042c42a146SMarcel Moolenaar return (sig); 1705df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1706df8bae1dSRodney W. Grimes 17070b53fbe8SBruce Evans case (int)SIG_IGN: 1708df8bae1dSRodney W. Grimes /* 1709df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1710df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1711df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1712df8bae1dSRodney W. Grimes */ 1713df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1714df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1715df8bae1dSRodney W. Grimes printf("issignal\n"); 1716df8bae1dSRodney W. Grimes break; /* == ignore */ 1717df8bae1dSRodney W. Grimes 1718df8bae1dSRodney W. Grimes default: 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * This signal has an action, let 1721df8bae1dSRodney W. Grimes * postsig() process it. 1722df8bae1dSRodney W. Grimes */ 17232c42a146SMarcel Moolenaar return (sig); 1724df8bae1dSRodney W. Grimes } 17252c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1726df8bae1dSRodney W. Grimes } 1727df8bae1dSRodney W. Grimes /* NOTREACHED */ 1728df8bae1dSRodney W. Grimes } 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes /* 1731df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1732df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 17335b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 17345b3047d5SJohn Baldwin * lock held. 1735df8bae1dSRodney W. Grimes */ 17365b3047d5SJohn Baldwin static void 1737df8bae1dSRodney W. Grimes stop(p) 1738df8bae1dSRodney W. Grimes register struct proc *p; 1739df8bae1dSRodney W. Grimes { 1740df8bae1dSRodney W. Grimes 1741628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1742e602ba25SJulian Elischer p->p_flag |= P_STOPPED_SGNL; 1743df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 174401609114SAlfred Perlstein wakeup(p->p_pptr); 1745df8bae1dSRodney W. Grimes } 1746df8bae1dSRodney W. Grimes 1747df8bae1dSRodney W. Grimes /* 1748df8bae1dSRodney W. Grimes * Take the action for the specified signal 1749df8bae1dSRodney W. Grimes * from the current set of pending signals. 1750df8bae1dSRodney W. Grimes */ 1751df8bae1dSRodney W. Grimes void 17522c42a146SMarcel Moolenaar postsig(sig) 17532c42a146SMarcel Moolenaar register int sig; 1754df8bae1dSRodney W. Grimes { 1755b40ce416SJulian Elischer struct thread *td = curthread; 1756b40ce416SJulian Elischer register struct proc *p = td->td_proc; 1757628d2653SJohn Baldwin struct sigacts *ps; 17582c42a146SMarcel Moolenaar sig_t action; 17592c42a146SMarcel Moolenaar sigset_t returnmask; 17602c42a146SMarcel Moolenaar int code; 1761df8bae1dSRodney W. Grimes 17622c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 17635526d2d9SEivind Eklund 17642ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1765628d2653SJohn Baldwin ps = p->p_sigacts; 17662c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 17672c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1768df8bae1dSRodney W. Grimes #ifdef KTRACE 1769374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 1770374a15aaSJohn Baldwin ktrpsig(sig, action, p->p_flag & P_OLDMASK ? 17712c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1772df8bae1dSRodney W. Grimes #endif 1773628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 17742a024a2bSSean Eric Fagan 1775df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1776df8bae1dSRodney W. Grimes /* 1777df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1778df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1779df8bae1dSRodney W. Grimes */ 1780b40ce416SJulian Elischer sigexit(td, sig); 1781df8bae1dSRodney W. Grimes /* NOTREACHED */ 1782df8bae1dSRodney W. Grimes } else { 1783df8bae1dSRodney W. Grimes /* 1784df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1785df8bae1dSRodney W. Grimes */ 17862c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 17875526d2d9SEivind Eklund ("postsig action")); 1788df8bae1dSRodney W. Grimes /* 1789df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1790645682fdSLuoqi Chen * occurrences of this signal. 1791df8bae1dSRodney W. Grimes * 1792645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1793df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1794645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1795df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1796df8bae1dSRodney W. Grimes */ 1797645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 17986626c604SJulian Elischer returnmask = p->p_oldsigmask; 1799645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1800df8bae1dSRodney W. Grimes } else 1801df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 18022c42a146SMarcel Moolenaar 18032c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 18042c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 18052c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 18062c42a146SMarcel Moolenaar 18072c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1808289ccde0SPeter Wemm /* 18092c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1810289ccde0SPeter Wemm */ 18112c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 18122c42a146SMarcel Moolenaar if (sig != SIGCONT && 18132c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 18142c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 18152c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1816dedc04feSPeter Wemm } 1817df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 18182c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1819df8bae1dSRodney W. Grimes code = 0; 1820df8bae1dSRodney W. Grimes } else { 18216626c604SJulian Elischer code = p->p_code; 18226626c604SJulian Elischer p->p_code = 0; 18236626c604SJulian Elischer p->p_sig = 0; 1824df8bae1dSRodney W. Grimes } 18252c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1826df8bae1dSRodney W. Grimes } 1827df8bae1dSRodney W. Grimes } 1828df8bae1dSRodney W. Grimes 1829df8bae1dSRodney W. Grimes /* 1830df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1831df8bae1dSRodney W. Grimes */ 183226f9a767SRodney W. Grimes void 1833df8bae1dSRodney W. Grimes killproc(p, why) 1834df8bae1dSRodney W. Grimes struct proc *p; 1835df8bae1dSRodney W. Grimes char *why; 1836df8bae1dSRodney W. Grimes { 18379081e5e8SJohn Baldwin 18389081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 18390384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 18400384fff8SJason Evans p, p->p_pid, p->p_comm); 1841729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1842b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1843df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1844df8bae1dSRodney W. Grimes } 1845df8bae1dSRodney W. Grimes 1846df8bae1dSRodney W. Grimes /* 1847df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1848df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1849df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1850df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1851df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1852df8bae1dSRodney W. Grimes * does not return. 1853df8bae1dSRodney W. Grimes */ 185426f9a767SRodney W. Grimes void 1855b40ce416SJulian Elischer sigexit(td, sig) 1856b40ce416SJulian Elischer struct thread *td; 18572c42a146SMarcel Moolenaar int sig; 1858df8bae1dSRodney W. Grimes { 1859b40ce416SJulian Elischer struct proc *p = td->td_proc; 1860df8bae1dSRodney W. Grimes 1861628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1862df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 18632c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 18642c42a146SMarcel Moolenaar p->p_sig = sig; 1865c364e17eSAndrey A. Chernov /* 1866c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1867c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1868c364e17eSAndrey A. Chernov * these messages.) 1869c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1870c364e17eSAndrey A. Chernov */ 1871628d2653SJohn Baldwin PROC_UNLOCK(p); 1872c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1873c31146a1SJohn Baldwin mtx_lock(&Giant); 1874b40ce416SJulian Elischer if (coredump(td) == 0) 18752c42a146SMarcel Moolenaar sig |= WCOREFLAG; 187657308494SJoerg Wunsch if (kern_logsigexit) 187757308494SJoerg Wunsch log(LOG_INFO, 187857308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 18793d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 18809c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 18812c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 18822c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1883c31146a1SJohn Baldwin } else { 1884628d2653SJohn Baldwin PROC_UNLOCK(p); 1885628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1886628d2653SJohn Baldwin mtx_lock(&Giant); 1887c31146a1SJohn Baldwin } 1888b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 1889df8bae1dSRodney W. Grimes /* NOTREACHED */ 1890df8bae1dSRodney W. Grimes } 1891df8bae1dSRodney W. Grimes 1892c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1893c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1894c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1895c5edb423SSean Eric Fagan 1896c5edb423SSean Eric Fagan /* 1897c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1898c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1899c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1900c5edb423SSean Eric Fagan * %N name of process ("name") 1901c5edb423SSean Eric Fagan * %P process id (pid) 1902c5edb423SSean Eric Fagan * %U user id (uid) 1903c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1904c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1905c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1906c5edb423SSean Eric Fagan */ 1907c5edb423SSean Eric Fagan 1908fca666a1SJulian Elischer static char * 1909c5edb423SSean Eric Fagan expand_name(name, uid, pid) 19108b43b535SAlfred Perlstein const char *name; 19118b43b535SAlfred Perlstein uid_t uid; 19128b43b535SAlfred Perlstein pid_t pid; 19138b43b535SAlfred Perlstein { 19148b43b535SAlfred Perlstein const char *format, *appendstr; 1915c5edb423SSean Eric Fagan char *temp; 1916c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 19178b43b535SAlfred Perlstein size_t i, l, n; 1918c5edb423SSean Eric Fagan 19198b43b535SAlfred Perlstein format = corefilename; 19208b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 19210bfe2990SEivind Eklund if (temp == NULL) 19228b43b535SAlfred Perlstein return (NULL); 1923ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1924c5edb423SSean Eric Fagan switch (format[i]) { 1925c5edb423SSean Eric Fagan case '%': /* Format character */ 1926c5edb423SSean Eric Fagan i++; 1927c5edb423SSean Eric Fagan switch (format[i]) { 1928c5edb423SSean Eric Fagan case '%': 19298b43b535SAlfred Perlstein appendstr = "%"; 1930c5edb423SSean Eric Fagan break; 1931c5edb423SSean Eric Fagan case 'N': /* process name */ 19328b43b535SAlfred Perlstein appendstr = name; 1933c5edb423SSean Eric Fagan break; 1934c5edb423SSean Eric Fagan case 'P': /* process id */ 19358b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 19368b43b535SAlfred Perlstein appendstr = buf; 1937c5edb423SSean Eric Fagan break; 1938c5edb423SSean Eric Fagan case 'U': /* user id */ 19398b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 19408b43b535SAlfred Perlstein appendstr = buf; 1941c5edb423SSean Eric Fagan break; 1942c5edb423SSean Eric Fagan default: 19438b43b535SAlfred Perlstein appendstr = ""; 19448b43b535SAlfred Perlstein log(LOG_ERR, 19458b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 19468b43b535SAlfred Perlstein format[i], format); 1947c5edb423SSean Eric Fagan } 19488b43b535SAlfred Perlstein l = strlen(appendstr); 19498b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 19508b43b535SAlfred Perlstein goto toolong; 19518b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 19528b43b535SAlfred Perlstein n += l; 1953c5edb423SSean Eric Fagan break; 1954c5edb423SSean Eric Fagan default: 1955c5edb423SSean Eric Fagan temp[n++] = format[i]; 1956c5edb423SSean Eric Fagan } 1957c5edb423SSean Eric Fagan } 19588b43b535SAlfred Perlstein if (format[i] != '\0') 19598b43b535SAlfred Perlstein goto toolong; 19608b43b535SAlfred Perlstein return (temp); 19618b43b535SAlfred Perlstein toolong: 19628b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 19638b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 19648b43b535SAlfred Perlstein free(temp, M_TEMP); 19658b43b535SAlfred Perlstein return (NULL); 1966c5edb423SSean Eric Fagan } 1967c5edb423SSean Eric Fagan 1968df8bae1dSRodney W. Grimes /* 1969fca666a1SJulian Elischer * Dump a process' core. The main routine does some 1970fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 1971fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 1972fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 1973fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 1974fca666a1SJulian Elischer */ 1975fca666a1SJulian Elischer 1976fca666a1SJulian Elischer static int 1977b40ce416SJulian Elischer coredump(struct thread *td) 1978fca666a1SJulian Elischer { 1979b40ce416SJulian Elischer struct proc *p = td->td_proc; 1980fca666a1SJulian Elischer register struct vnode *vp; 19819c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 198206ae1e91SMatthew Dillon struct flock lf; 1983fca666a1SJulian Elischer struct nameidata nd; 1984fca666a1SJulian Elischer struct vattr vattr; 1985e6796b67SKirk McKusick int error, error1, flags; 1986f2a2857bSKirk McKusick struct mount *mp; 1987fca666a1SJulian Elischer char *name; /* name of corefile */ 1988fca666a1SJulian Elischer off_t limit; 1989fca666a1SJulian Elischer 1990628d2653SJohn Baldwin PROC_LOCK(p); 1991628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 1992fca666a1SJulian Elischer 1993628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1994628d2653SJohn Baldwin PROC_UNLOCK(p); 1995fca666a1SJulian Elischer return (EFAULT); 1996628d2653SJohn Baldwin } 1997fca666a1SJulian Elischer 1998fca666a1SJulian Elischer /* 199935a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 200035a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 200135a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 200235a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 200335a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 200435a2598fSSean Eric Fagan * if it is larger than the limit. 2005fca666a1SJulian Elischer */ 200635a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2007628d2653SJohn Baldwin if (limit == 0) { 2008628d2653SJohn Baldwin PROC_UNLOCK(p); 200935a2598fSSean Eric Fagan return 0; 2010628d2653SJohn Baldwin } 2011628d2653SJohn Baldwin PROC_UNLOCK(p); 201235a2598fSSean Eric Fagan 2013f2a2857bSKirk McKusick restart: 20149c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2015ccdbd10cSPeter Pentchev if (name == NULL) 2016ccdbd10cSPeter Pentchev return (EINVAL); 2017b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2018e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 2019e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2020fca666a1SJulian Elischer free(name, M_TEMP); 2021fca666a1SJulian Elischer if (error) 2022fca666a1SJulian Elischer return (error); 2023762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2024fca666a1SJulian Elischer vp = nd.ni_vp; 202506ae1e91SMatthew Dillon 2026832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2027832dafadSDon Lewis if (vp->v_type != VREG || 2028832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2029832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2030832dafadSDon Lewis error = EFAULT; 2031832dafadSDon Lewis goto out2; 2032832dafadSDon Lewis } 2033832dafadSDon Lewis 2034b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 203506ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 203606ae1e91SMatthew Dillon lf.l_start = 0; 203706ae1e91SMatthew Dillon lf.l_len = 0; 203806ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 203906ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 204006ae1e91SMatthew Dillon if (error) 204106ae1e91SMatthew Dillon goto out2; 204206ae1e91SMatthew Dillon 204306ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 204406ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 204506ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2046b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2047f2a2857bSKirk McKusick return (error); 2048f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2049f2a2857bSKirk McKusick return (error); 2050f2a2857bSKirk McKusick goto restart; 2051f2a2857bSKirk McKusick } 2052fca666a1SJulian Elischer 2053fca666a1SJulian Elischer VATTR_NULL(&vattr); 2054fca666a1SJulian Elischer vattr.va_size = 0; 205588b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2056b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2057b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 205888b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2059628d2653SJohn Baldwin PROC_LOCK(p); 2060fca666a1SJulian Elischer p->p_acflag |= ACORE; 2061628d2653SJohn Baldwin PROC_UNLOCK(p); 2062fca666a1SJulian Elischer 2063fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2064b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2065fca666a1SJulian Elischer ENOSYS; 2066fca666a1SJulian Elischer 206706ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 206806ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2069f2a2857bSKirk McKusick vn_finished_write(mp); 207006ae1e91SMatthew Dillon out2: 2071b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2072fca666a1SJulian Elischer if (error == 0) 2073fca666a1SJulian Elischer error = error1; 2074fca666a1SJulian Elischer return (error); 2075fca666a1SJulian Elischer } 2076fca666a1SJulian Elischer 2077fca666a1SJulian Elischer /* 2078df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2079df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2080df8bae1dSRodney W. Grimes */ 2081d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2082df8bae1dSRodney W. Grimes struct nosys_args { 2083df8bae1dSRodney W. Grimes int dummy; 2084df8bae1dSRodney W. Grimes }; 2085d2d3e875SBruce Evans #endif 2086fb99ab88SMatthew Dillon /* 2087fb99ab88SMatthew Dillon * MPSAFE 2088fb99ab88SMatthew Dillon */ 2089df8bae1dSRodney W. Grimes /* ARGSUSED */ 209026f9a767SRodney W. Grimes int 2091b40ce416SJulian Elischer nosys(td, args) 2092b40ce416SJulian Elischer struct thread *td; 2093df8bae1dSRodney W. Grimes struct nosys_args *args; 2094df8bae1dSRodney W. Grimes { 2095b40ce416SJulian Elischer struct proc *p = td->td_proc; 2096b40ce416SJulian Elischer 2097fb99ab88SMatthew Dillon mtx_lock(&Giant); 2098628d2653SJohn Baldwin PROC_LOCK(p); 2099df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2100628d2653SJohn Baldwin PROC_UNLOCK(p); 2101fb99ab88SMatthew Dillon mtx_unlock(&Giant); 2102f5216b9aSBruce Evans return (ENOSYS); 2103df8bae1dSRodney W. Grimes } 2104831d27a9SDon Lewis 2105831d27a9SDon Lewis /* 210648f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2107831d27a9SDon Lewis * stored credentials rather than those of the current process. 2108831d27a9SDon Lewis */ 2109831d27a9SDon Lewis void 2110f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2111f1320723SAlfred Perlstein struct sigio **sigiop; 21122c42a146SMarcel Moolenaar int sig, checkctty; 2113831d27a9SDon Lewis { 2114f1320723SAlfred Perlstein struct sigio *sigio; 2115831d27a9SDon Lewis 2116f1320723SAlfred Perlstein SIGIO_LOCK(); 2117f1320723SAlfred Perlstein sigio = *sigiop; 2118f1320723SAlfred Perlstein if (sigio == NULL) { 2119f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2120f1320723SAlfred Perlstein return; 2121f1320723SAlfred Perlstein } 2122831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2123628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 21242b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 21252c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2126628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2127831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2128831d27a9SDon Lewis struct proc *p; 2129831d27a9SDon Lewis 2130f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2131628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2132628d2653SJohn Baldwin PROC_LOCK(p); 21332b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2134831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 21352c42a146SMarcel Moolenaar psignal(p, sig); 2136628d2653SJohn Baldwin PROC_UNLOCK(p); 2137628d2653SJohn Baldwin } 2138f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2139831d27a9SDon Lewis } 2140f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2141831d27a9SDon Lewis } 2142cb679c38SJonathan Lemon 2143cb679c38SJonathan Lemon static int 2144cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2145cb679c38SJonathan Lemon { 2146cb679c38SJonathan Lemon struct proc *p = curproc; 2147cb679c38SJonathan Lemon 2148cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2149cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2150cb679c38SJonathan Lemon 2151628d2653SJohn Baldwin PROC_LOCK(p); 2152cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2153628d2653SJohn Baldwin PROC_UNLOCK(p); 2154cb679c38SJonathan Lemon 2155cb679c38SJonathan Lemon return (0); 2156cb679c38SJonathan Lemon } 2157cb679c38SJonathan Lemon 2158cb679c38SJonathan Lemon static void 2159cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2160cb679c38SJonathan Lemon { 2161cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2162cb679c38SJonathan Lemon 2163628d2653SJohn Baldwin PROC_LOCK(p); 2164e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2165628d2653SJohn Baldwin PROC_UNLOCK(p); 2166cb679c38SJonathan Lemon } 2167cb679c38SJonathan Lemon 2168cb679c38SJonathan Lemon /* 2169cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2170cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2171cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2172cb679c38SJonathan Lemon * isn't worth the trouble. 2173cb679c38SJonathan Lemon */ 2174cb679c38SJonathan Lemon static int 2175cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2176cb679c38SJonathan Lemon { 2177cb679c38SJonathan Lemon 2178cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2179cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2180cb679c38SJonathan Lemon 2181cb679c38SJonathan Lemon if (kn->kn_id == hint) 2182cb679c38SJonathan Lemon kn->kn_data++; 2183cb679c38SJonathan Lemon } 2184cb679c38SJonathan Lemon return (kn->kn_data != 0); 2185cb679c38SJonathan Lemon } 2186