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> 678f19eb88SIan Dowse #include <sys/syscallsubr.h> 681005a129SJohn Baldwin #include <sys/syslog.h> 69d66a5066SPeter Wemm #include <sys/sysent.h> 70c87e2930SDavid Greenman #include <sys/sysctl.h> 71c5edb423SSean Eric Fagan #include <sys/malloc.h> 7206ae1e91SMatthew Dillon #include <sys/unistd.h> 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes #include <machine/cpu.h> 75df8bae1dSRodney W. Grimes 766f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 776f841fb7SMarcel Moolenaar 784d77a549SAlfred Perlstein static int coredump(struct thread *); 794d77a549SAlfred Perlstein static int do_sigprocmask(struct proc *p, int how, sigset_t *set, 804d77a549SAlfred Perlstein sigset_t *oset, int old); 814d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t); 829c1ab3e0SJohn Baldwin static int killpg1(struct thread *td, int sig, int pgid, int all); 834d77a549SAlfred Perlstein static int sig_ffs(sigset_t *set); 844d77a549SAlfred Perlstein static int sigprop(int sig); 854d77a549SAlfred Perlstein static void stop(struct proc *); 86e602ba25SJulian Elischer static void tdsignal(struct thread *td, int sig, sig_t action); 87cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 88cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 89cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 90cb679c38SJonathan Lemon 91cb679c38SJonathan Lemon struct filterops sig_filtops = 92cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 93cb679c38SJonathan Lemon 9457308494SJoerg Wunsch static int kern_logsigexit = 1; 953d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 963d177f46SBill Fumerola &kern_logsigexit, 0, 973d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 9857308494SJoerg Wunsch 992b87b6d4SRobert Watson /* 1002b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1012b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1022b87b6d4SRobert Watson * in the right situations. 1032b87b6d4SRobert Watson */ 1042b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1052b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1062b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1072b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1082b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1092b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1102b87b6d4SRobert Watson 11122d4b0fbSJohn Polstra int sugid_coredump; 1123d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1133d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 114c87e2930SDavid Greenman 115e5a28db9SPaul Saab static int do_coredump = 1; 116e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 117e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 118e5a28db9SPaul Saab 1192c42a146SMarcel Moolenaar /* 1202c42a146SMarcel Moolenaar * Signal properties and actions. 1212c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1222c42a146SMarcel Moolenaar * according to the following properties: 1232c42a146SMarcel Moolenaar */ 1242c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1252c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1262c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1272c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1282c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1292c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1302c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 131df8bae1dSRodney W. Grimes 1322c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1332c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1342c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1352c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1362c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1372c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1382c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1392c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1402c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1412c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1422c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1442c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1452c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1462c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1472c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1482c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1492c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1502c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1512c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1522c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1532c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1542c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1552c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1562c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1572c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1582c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1592c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1602c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1612c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1622c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1632c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1642c42a146SMarcel Moolenaar }; 1652c42a146SMarcel Moolenaar 166fbbeeb6cSBruce Evans /* 167fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 168fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 169fbbeeb6cSBruce Evans * action, the process stops in issignal(). 170e602ba25SJulian Elischer * XXXKSE the check for a pending stop is not done under KSE 171fbbeeb6cSBruce Evans * 17233510ef1SBruce Evans * MP SAFE. 173fbbeeb6cSBruce Evans */ 174fbbeeb6cSBruce Evans int 175e602ba25SJulian Elischer cursig(struct thread *td) 176fbbeeb6cSBruce Evans { 177e602ba25SJulian Elischer struct proc *p = td->td_proc; 178fbbeeb6cSBruce Evans 1792ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 180179235b3SBruce Evans mtx_assert(&sched_lock, MA_NOTOWNED); 181e602ba25SJulian Elischer return (SIGPENDING(p) ? issignal(td) : 0); 182fbbeeb6cSBruce Evans } 183fbbeeb6cSBruce Evans 18479065dbaSBruce Evans /* 18579065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 18679065dbaSBruce Evans * mode. This must be called whenever a signal is added to p_siglist or 18779065dbaSBruce Evans * unmasked in p_sigmask. 18879065dbaSBruce Evans */ 18979065dbaSBruce Evans void 19079065dbaSBruce Evans signotify(struct proc *p) 19179065dbaSBruce Evans { 1924f0db5e0SJulian Elischer struct kse *ke; 1934f0db5e0SJulian Elischer struct ksegrp *kg; 19479065dbaSBruce Evans 19579065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 19679065dbaSBruce Evans mtx_lock_spin(&sched_lock); 19779065dbaSBruce Evans if (SIGPENDING(p)) { 19879065dbaSBruce Evans p->p_sflag |= PS_NEEDSIGCHK; 1994f0db5e0SJulian Elischer /* XXXKSE for now punish all KSEs */ 2004f0db5e0SJulian Elischer FOREACH_KSEGRP_IN_PROC(p, kg) { 2014f0db5e0SJulian Elischer FOREACH_KSE_IN_GROUP(kg, ke) { 2024f0db5e0SJulian Elischer ke->ke_flags |= KEF_ASTPENDING; 2034f0db5e0SJulian Elischer } 2044f0db5e0SJulian Elischer } 20579065dbaSBruce Evans } 20679065dbaSBruce Evans mtx_unlock_spin(&sched_lock); 20779065dbaSBruce Evans } 20879065dbaSBruce Evans 2096f841fb7SMarcel Moolenaar static __inline int 2106f841fb7SMarcel Moolenaar sigprop(int sig) 2112c42a146SMarcel Moolenaar { 2126f841fb7SMarcel Moolenaar 2132c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 2142c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 2152c42a146SMarcel Moolenaar return (0); 216df8bae1dSRodney W. Grimes } 2172c42a146SMarcel Moolenaar 2186f841fb7SMarcel Moolenaar static __inline int 2196f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2202c42a146SMarcel Moolenaar { 2212c42a146SMarcel Moolenaar int i; 2222c42a146SMarcel Moolenaar 2236f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2242c42a146SMarcel Moolenaar if (set->__bits[i]) 2252c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 226df8bae1dSRodney W. Grimes return (0); 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes 2292c42a146SMarcel Moolenaar /* 2308f19eb88SIan Dowse * kern_sigaction 2312c42a146SMarcel Moolenaar * sigaction 2322c42a146SMarcel Moolenaar * osigaction 2332c42a146SMarcel Moolenaar */ 2348f19eb88SIan Dowse int 2358f19eb88SIan Dowse kern_sigaction(td, sig, act, oact, old) 2368f19eb88SIan Dowse struct thread *td; 2372c42a146SMarcel Moolenaar register int sig; 2382c42a146SMarcel Moolenaar struct sigaction *act, *oact; 239645682fdSLuoqi Chen int old; 240df8bae1dSRodney W. Grimes { 241628d2653SJohn Baldwin register struct sigacts *ps; 2428f19eb88SIan Dowse struct proc *p = td->td_proc; 243df8bae1dSRodney W. Grimes 2442899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 2452c42a146SMarcel Moolenaar return (EINVAL); 2462c42a146SMarcel Moolenaar 247628d2653SJohn Baldwin PROC_LOCK(p); 248628d2653SJohn Baldwin ps = p->p_sigacts; 2492c42a146SMarcel Moolenaar if (oact) { 2502c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2512c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2522c42a146SMarcel Moolenaar oact->sa_flags = 0; 2532c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2542c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2552c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2562c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2572c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2582c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2592c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2602c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2612c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2622c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 263645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2642c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 265645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2662c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2672c42a146SMarcel Moolenaar } 2682c42a146SMarcel Moolenaar if (act) { 2692c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 270628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 271628d2653SJohn Baldwin PROC_UNLOCK(p); 2722c42a146SMarcel Moolenaar return (EINVAL); 273628d2653SJohn Baldwin } 2742c42a146SMarcel Moolenaar 275df8bae1dSRodney W. Grimes /* 276df8bae1dSRodney W. Grimes * Change setting atomically. 277df8bae1dSRodney W. Grimes */ 2782c42a146SMarcel Moolenaar 2792c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2802c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2812c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 282aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 283aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 28480f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 28580f42b55SIan Dowse } else { 28680f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2872c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2882c42a146SMarcel Moolenaar } 2892c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2902c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 291df8bae1dSRodney W. Grimes else 2922c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2932c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2942c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 295df8bae1dSRodney W. Grimes else 2962c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2972c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2982c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2991e41c1b5SSteven Wallace else 3002c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 3012c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 3022c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 303289ccde0SPeter Wemm else 3042c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 305df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 3062c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 3072c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 308df8bae1dSRodney W. Grimes else 3098c3d74f4SBruce Evans SIGDELSET(ps->ps_usertramp, sig); 310df8bae1dSRodney W. Grimes #endif 3112c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3122c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 313645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 3146626c604SJulian Elischer else 315645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 316ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 317245f17d4SJoerg Wunsch /* 3182c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3192c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3202c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3212c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 322245f17d4SJoerg Wunsch */ 323245f17d4SJoerg Wunsch if (p->p_pid == 1) 324645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3256626c604SJulian Elischer else 326645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 327245f17d4SJoerg Wunsch } else 328645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 329ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 330ba1551caSIan Dowse p->p_procsig->ps_flag |= PS_CLDSIGIGN; 331ba1551caSIan Dowse else 332ba1551caSIan Dowse p->p_procsig->ps_flag &= ~PS_CLDSIGIGN; 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3362c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3372c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3382c42a146SMarcel Moolenaar * have to restart the process. 339df8bae1dSRodney W. Grimes */ 3402c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3412c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3422c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3432c42a146SMarcel Moolenaar /* never to be seen again */ 3442c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3452c42a146SMarcel Moolenaar if (sig != SIGCONT) 3462c42a146SMarcel Moolenaar /* easier in psignal */ 3472c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3482c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 349645682fdSLuoqi Chen } else { 3502c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3512c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3522c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3532c42a146SMarcel Moolenaar else 3542c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3552c42a146SMarcel Moolenaar } 356e8ebc08fSPeter Wemm #ifdef COMPAT_43 357645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 358645682fdSLuoqi Chen ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 359645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 360645682fdSLuoqi Chen else 361645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 362e8ebc08fSPeter Wemm #endif 363df8bae1dSRodney W. Grimes } 364628d2653SJohn Baldwin PROC_UNLOCK(p); 3652c42a146SMarcel Moolenaar return (0); 3662c42a146SMarcel Moolenaar } 3672c42a146SMarcel Moolenaar 3682c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3692c42a146SMarcel Moolenaar struct sigaction_args { 3702c42a146SMarcel Moolenaar int sig; 3712c42a146SMarcel Moolenaar struct sigaction *act; 3722c42a146SMarcel Moolenaar struct sigaction *oact; 3732c42a146SMarcel Moolenaar }; 3742c42a146SMarcel Moolenaar #endif 375fb99ab88SMatthew Dillon /* 376fb99ab88SMatthew Dillon * MPSAFE 377fb99ab88SMatthew Dillon */ 3782c42a146SMarcel Moolenaar /* ARGSUSED */ 3792c42a146SMarcel Moolenaar int 380b40ce416SJulian Elischer sigaction(td, uap) 381b40ce416SJulian Elischer struct thread *td; 3822c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3832c42a146SMarcel Moolenaar { 3842c42a146SMarcel Moolenaar struct sigaction act, oact; 3852c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3862c42a146SMarcel Moolenaar int error; 3872c42a146SMarcel Moolenaar 388fb99ab88SMatthew Dillon mtx_lock(&Giant); 389fb99ab88SMatthew Dillon 3906f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3916f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3922c42a146SMarcel Moolenaar if (actp) { 3936f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3942c42a146SMarcel Moolenaar if (error) 395fb99ab88SMatthew Dillon goto done2; 3962c42a146SMarcel Moolenaar } 3978f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 3982c42a146SMarcel Moolenaar if (oactp && !error) { 3996f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 4002c42a146SMarcel Moolenaar } 401fb99ab88SMatthew Dillon done2: 402fb99ab88SMatthew Dillon mtx_unlock(&Giant); 4032c42a146SMarcel Moolenaar return (error); 4042c42a146SMarcel Moolenaar } 4052c42a146SMarcel Moolenaar 40631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 4072c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4082c42a146SMarcel Moolenaar struct osigaction_args { 4092c42a146SMarcel Moolenaar int signum; 4102c42a146SMarcel Moolenaar struct osigaction *nsa; 4112c42a146SMarcel Moolenaar struct osigaction *osa; 4122c42a146SMarcel Moolenaar }; 4132c42a146SMarcel Moolenaar #endif 414fb99ab88SMatthew Dillon /* 415fb99ab88SMatthew Dillon * MPSAFE 416fb99ab88SMatthew Dillon */ 4172c42a146SMarcel Moolenaar /* ARGSUSED */ 4182c42a146SMarcel Moolenaar int 419b40ce416SJulian Elischer osigaction(td, uap) 420b40ce416SJulian Elischer struct thread *td; 4212c42a146SMarcel Moolenaar register struct osigaction_args *uap; 4222c42a146SMarcel Moolenaar { 4232c42a146SMarcel Moolenaar struct osigaction sa; 4242c42a146SMarcel Moolenaar struct sigaction nsa, osa; 4252c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 4262c42a146SMarcel Moolenaar int error; 4272c42a146SMarcel Moolenaar 4286f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4296f841fb7SMarcel Moolenaar return (EINVAL); 430fb99ab88SMatthew Dillon 4316f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4326f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 433fb99ab88SMatthew Dillon 434fb99ab88SMatthew Dillon mtx_lock(&Giant); 435fb99ab88SMatthew Dillon 4362c42a146SMarcel Moolenaar if (nsap) { 4376f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4382c42a146SMarcel Moolenaar if (error) 439fb99ab88SMatthew Dillon goto done2; 4402c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4412c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4422c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4432c42a146SMarcel Moolenaar } 4448f19eb88SIan Dowse error = kern_sigaction(td, uap->signum, nsap, osap, 1); 4452c42a146SMarcel Moolenaar if (osap && !error) { 4462c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4472c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4482c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4496f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4502c42a146SMarcel Moolenaar } 451fb99ab88SMatthew Dillon done2: 452fb99ab88SMatthew Dillon mtx_unlock(&Giant); 4532c42a146SMarcel Moolenaar return (error); 4542c42a146SMarcel Moolenaar } 45531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 456df8bae1dSRodney W. Grimes 457df8bae1dSRodney W. Grimes /* 458df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 459df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 460df8bae1dSRodney W. Grimes */ 461df8bae1dSRodney W. Grimes void 462df8bae1dSRodney W. Grimes siginit(p) 463df8bae1dSRodney W. Grimes struct proc *p; 464df8bae1dSRodney W. Grimes { 465df8bae1dSRodney W. Grimes register int i; 466df8bae1dSRodney W. Grimes 467628d2653SJohn Baldwin PROC_LOCK(p); 4682c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4692c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4702c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 471628d2653SJohn Baldwin PROC_UNLOCK(p); 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes 474df8bae1dSRodney W. Grimes /* 475df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 476df8bae1dSRodney W. Grimes */ 477df8bae1dSRodney W. Grimes void 478df8bae1dSRodney W. Grimes execsigs(p) 479df8bae1dSRodney W. Grimes register struct proc *p; 480df8bae1dSRodney W. Grimes { 481628d2653SJohn Baldwin register struct sigacts *ps; 4822c42a146SMarcel Moolenaar register int sig; 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes /* 485df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 486df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 487df8bae1dSRodney W. Grimes * and are now ignored by default). 488df8bae1dSRodney W. Grimes */ 4899b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 490628d2653SJohn Baldwin ps = p->p_sigacts; 4912c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4922c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4932c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4942c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4952c42a146SMarcel Moolenaar if (sig != SIGCONT) 4962c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4972c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 498df8bae1dSRodney W. Grimes } 4992c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 500df8bae1dSRodney W. Grimes } 501df8bae1dSRodney W. Grimes /* 502df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 503df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 504df8bae1dSRodney W. Grimes */ 505645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 506645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 507645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 5083b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 50980e907a1SPeter Wemm /* 51080e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 51180e907a1SPeter Wemm */ 512ba1551caSIan Dowse p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 513c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 514c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes 517df8bae1dSRodney W. Grimes /* 518628d2653SJohn Baldwin * do_sigprocmask() 5197c8fdcbdSMatthew Dillon * 520628d2653SJohn Baldwin * Manipulate signal mask. 521df8bae1dSRodney W. Grimes */ 5222c42a146SMarcel Moolenaar static int 523645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 5242c42a146SMarcel Moolenaar struct proc *p; 5252c42a146SMarcel Moolenaar int how; 5262c42a146SMarcel Moolenaar sigset_t *set, *oset; 527645682fdSLuoqi Chen int old; 5282c42a146SMarcel Moolenaar { 5292c42a146SMarcel Moolenaar int error; 5302c42a146SMarcel Moolenaar 531628d2653SJohn Baldwin PROC_LOCK(p); 5322c42a146SMarcel Moolenaar if (oset != NULL) 5332c42a146SMarcel Moolenaar *oset = p->p_sigmask; 5342c42a146SMarcel Moolenaar 5352c42a146SMarcel Moolenaar error = 0; 5362c42a146SMarcel Moolenaar if (set != NULL) { 5372c42a146SMarcel Moolenaar switch (how) { 5382c42a146SMarcel Moolenaar case SIG_BLOCK: 539645682fdSLuoqi Chen SIG_CANTMASK(*set); 5402c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 5412c42a146SMarcel Moolenaar break; 5422c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5432c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 54479065dbaSBruce Evans signotify(p); 5452c42a146SMarcel Moolenaar break; 5462c42a146SMarcel Moolenaar case SIG_SETMASK: 547645682fdSLuoqi Chen SIG_CANTMASK(*set); 548645682fdSLuoqi Chen if (old) 549645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 550645682fdSLuoqi Chen else 5512c42a146SMarcel Moolenaar p->p_sigmask = *set; 55279065dbaSBruce Evans signotify(p); 5532c42a146SMarcel Moolenaar break; 5542c42a146SMarcel Moolenaar default: 5552c42a146SMarcel Moolenaar error = EINVAL; 5562c42a146SMarcel Moolenaar break; 5572c42a146SMarcel Moolenaar } 5582c42a146SMarcel Moolenaar } 559628d2653SJohn Baldwin PROC_UNLOCK(p); 5602c42a146SMarcel Moolenaar return (error); 5612c42a146SMarcel Moolenaar } 5622c42a146SMarcel Moolenaar 5637c8fdcbdSMatthew Dillon /* 564b40ce416SJulian Elischer * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't) 5657c8fdcbdSMatthew Dillon */ 5667c8fdcbdSMatthew Dillon 567d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 568df8bae1dSRodney W. Grimes struct sigprocmask_args { 569df8bae1dSRodney W. Grimes int how; 5702c42a146SMarcel Moolenaar const sigset_t *set; 5712c42a146SMarcel Moolenaar sigset_t *oset; 572df8bae1dSRodney W. Grimes }; 573d2d3e875SBruce Evans #endif 57426f9a767SRodney W. Grimes int 575b40ce416SJulian Elischer sigprocmask(td, uap) 576b40ce416SJulian Elischer register struct thread *td; 577df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 578df8bae1dSRodney W. Grimes { 579b40ce416SJulian Elischer struct proc *p = td->td_proc; 5802c42a146SMarcel Moolenaar sigset_t set, oset; 5812c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5822c42a146SMarcel Moolenaar int error; 583df8bae1dSRodney W. Grimes 5846f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5856f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5862c42a146SMarcel Moolenaar if (setp) { 5876f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5882c42a146SMarcel Moolenaar if (error) 5892c42a146SMarcel Moolenaar return (error); 590df8bae1dSRodney W. Grimes } 591645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5922c42a146SMarcel Moolenaar if (osetp && !error) { 5936f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5942c42a146SMarcel Moolenaar } 5952c42a146SMarcel Moolenaar return (error); 5962c42a146SMarcel Moolenaar } 5972c42a146SMarcel Moolenaar 59831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5997c8fdcbdSMatthew Dillon /* 6007c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 6017c8fdcbdSMatthew Dillon */ 6022c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6032c42a146SMarcel Moolenaar struct osigprocmask_args { 6042c42a146SMarcel Moolenaar int how; 6052c42a146SMarcel Moolenaar osigset_t mask; 6062c42a146SMarcel Moolenaar }; 6072c42a146SMarcel Moolenaar #endif 6082c42a146SMarcel Moolenaar int 609b40ce416SJulian Elischer osigprocmask(td, uap) 610b40ce416SJulian Elischer register struct thread *td; 6112c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 6122c42a146SMarcel Moolenaar { 613b40ce416SJulian Elischer struct proc *p = td->td_proc; 6142c42a146SMarcel Moolenaar sigset_t set, oset; 6152c42a146SMarcel Moolenaar int error; 6162c42a146SMarcel Moolenaar 6172c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 618645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 619b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 620df8bae1dSRodney W. Grimes return (error); 621df8bae1dSRodney W. Grimes } 62231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 623df8bae1dSRodney W. Grimes 624d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 625df8bae1dSRodney W. Grimes struct sigpending_args { 6262c42a146SMarcel Moolenaar sigset_t *set; 627df8bae1dSRodney W. Grimes }; 628d2d3e875SBruce Evans #endif 629fb99ab88SMatthew Dillon /* 630fb99ab88SMatthew Dillon * MPSAFE 631fb99ab88SMatthew Dillon */ 632df8bae1dSRodney W. Grimes /* ARGSUSED */ 63326f9a767SRodney W. Grimes int 634b40ce416SJulian Elischer sigpending(td, uap) 635b40ce416SJulian Elischer struct thread *td; 636df8bae1dSRodney W. Grimes struct sigpending_args *uap; 637df8bae1dSRodney W. Grimes { 638b40ce416SJulian Elischer struct proc *p = td->td_proc; 639628d2653SJohn Baldwin sigset_t siglist; 640fb99ab88SMatthew Dillon int error; 641df8bae1dSRodney W. Grimes 642fb99ab88SMatthew Dillon mtx_lock(&Giant); 643628d2653SJohn Baldwin PROC_LOCK(p); 644628d2653SJohn Baldwin siglist = p->p_siglist; 645628d2653SJohn Baldwin PROC_UNLOCK(p); 646fb99ab88SMatthew Dillon mtx_unlock(&Giant); 647fb99ab88SMatthew Dillon error = copyout(&siglist, uap->set, sizeof(sigset_t)); 648fb99ab88SMatthew Dillon return(error); 6492c42a146SMarcel Moolenaar } 6502c42a146SMarcel Moolenaar 65131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6522c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6532c42a146SMarcel Moolenaar struct osigpending_args { 6542c42a146SMarcel Moolenaar int dummy; 6552c42a146SMarcel Moolenaar }; 6562c42a146SMarcel Moolenaar #endif 657fb99ab88SMatthew Dillon /* 658fb99ab88SMatthew Dillon * MPSAFE 659fb99ab88SMatthew Dillon */ 6602c42a146SMarcel Moolenaar /* ARGSUSED */ 6612c42a146SMarcel Moolenaar int 662b40ce416SJulian Elischer osigpending(td, uap) 663b40ce416SJulian Elischer struct thread *td; 6642c42a146SMarcel Moolenaar struct osigpending_args *uap; 6652c42a146SMarcel Moolenaar { 666b40ce416SJulian Elischer struct proc *p = td->td_proc; 667b40ce416SJulian Elischer 668fb99ab88SMatthew Dillon mtx_lock(&Giant); 669628d2653SJohn Baldwin PROC_LOCK(p); 670b40ce416SJulian Elischer SIG2OSIG(p->p_siglist, td->td_retval[0]); 671628d2653SJohn Baldwin PROC_UNLOCK(p); 672fb99ab88SMatthew Dillon mtx_unlock(&Giant); 673df8bae1dSRodney W. Grimes return (0); 674df8bae1dSRodney W. Grimes } 67531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 678df8bae1dSRodney W. Grimes /* 679df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 680df8bae1dSRodney W. Grimes */ 681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 682df8bae1dSRodney W. Grimes struct osigvec_args { 683df8bae1dSRodney W. Grimes int signum; 684df8bae1dSRodney W. Grimes struct sigvec *nsv; 685df8bae1dSRodney W. Grimes struct sigvec *osv; 686df8bae1dSRodney W. Grimes }; 687d2d3e875SBruce Evans #endif 688fb99ab88SMatthew Dillon /* 689fb99ab88SMatthew Dillon * MPSAFE 690fb99ab88SMatthew Dillon */ 691df8bae1dSRodney W. Grimes /* ARGSUSED */ 69226f9a767SRodney W. Grimes int 693b40ce416SJulian Elischer osigvec(td, uap) 694b40ce416SJulian Elischer struct thread *td; 695df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 696df8bae1dSRodney W. Grimes { 697df8bae1dSRodney W. Grimes struct sigvec vec; 6982c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6992c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 7002c42a146SMarcel Moolenaar int error; 701df8bae1dSRodney W. Grimes 7026f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 7036f841fb7SMarcel Moolenaar return (EINVAL); 7046f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 7056f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 7062c42a146SMarcel Moolenaar if (nsap) { 7076f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 7082c42a146SMarcel Moolenaar if (error) 709df8bae1dSRodney W. Grimes return (error); 7102c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 7112c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 7122c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 7132c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 714df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 7152c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 716df8bae1dSRodney W. Grimes #endif 717df8bae1dSRodney W. Grimes } 718fb99ab88SMatthew Dillon mtx_lock(&Giant); 7198f19eb88SIan Dowse error = kern_sigaction(td, uap->signum, nsap, osap, 1); 720fb99ab88SMatthew Dillon mtx_unlock(&Giant); 7212c42a146SMarcel Moolenaar if (osap && !error) { 7222c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 7232c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 7242c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 7252c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 7262c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 7272c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 7282c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 7292c42a146SMarcel Moolenaar #endif 7306f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 7312c42a146SMarcel Moolenaar } 7322c42a146SMarcel Moolenaar return (error); 733df8bae1dSRodney W. Grimes } 734df8bae1dSRodney W. Grimes 735d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 736df8bae1dSRodney W. Grimes struct osigblock_args { 737df8bae1dSRodney W. Grimes int mask; 738df8bae1dSRodney W. Grimes }; 739d2d3e875SBruce Evans #endif 740fb99ab88SMatthew Dillon /* 741fb99ab88SMatthew Dillon * MPSAFE 742fb99ab88SMatthew Dillon */ 74326f9a767SRodney W. Grimes int 744b40ce416SJulian Elischer osigblock(td, uap) 745b40ce416SJulian Elischer register struct thread *td; 746df8bae1dSRodney W. Grimes struct osigblock_args *uap; 747df8bae1dSRodney W. Grimes { 748b40ce416SJulian Elischer struct proc *p = td->td_proc; 7492c42a146SMarcel Moolenaar sigset_t set; 750df8bae1dSRodney W. Grimes 7512c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7522c42a146SMarcel Moolenaar SIG_CANTMASK(set); 753fb99ab88SMatthew Dillon mtx_lock(&Giant); 754628d2653SJohn Baldwin PROC_LOCK(p); 755b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 7562c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 757628d2653SJohn Baldwin PROC_UNLOCK(p); 758fb99ab88SMatthew Dillon mtx_unlock(&Giant); 759df8bae1dSRodney W. Grimes return (0); 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes 762d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 763df8bae1dSRodney W. Grimes struct osigsetmask_args { 764df8bae1dSRodney W. Grimes int mask; 765df8bae1dSRodney W. Grimes }; 766d2d3e875SBruce Evans #endif 767fb99ab88SMatthew Dillon /* 768fb99ab88SMatthew Dillon * MPSAFE 769fb99ab88SMatthew Dillon */ 77026f9a767SRodney W. Grimes int 771b40ce416SJulian Elischer osigsetmask(td, uap) 772b40ce416SJulian Elischer struct thread *td; 773df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 774df8bae1dSRodney W. Grimes { 775b40ce416SJulian Elischer struct proc *p = td->td_proc; 7762c42a146SMarcel Moolenaar sigset_t set; 777df8bae1dSRodney W. Grimes 7782c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7792c42a146SMarcel Moolenaar SIG_CANTMASK(set); 780fb99ab88SMatthew Dillon mtx_lock(&Giant); 781628d2653SJohn Baldwin PROC_LOCK(p); 782b40ce416SJulian Elischer SIG2OSIG(p->p_sigmask, td->td_retval[0]); 783645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 78479065dbaSBruce Evans signotify(p); 785628d2653SJohn Baldwin PROC_UNLOCK(p); 786fb99ab88SMatthew Dillon mtx_unlock(&Giant); 787df8bae1dSRodney W. Grimes return (0); 788df8bae1dSRodney W. Grimes } 789df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 790df8bae1dSRodney W. Grimes 791df8bae1dSRodney W. Grimes /* 792df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 793df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 794df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 795b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 796b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 797b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 798df8bae1dSRodney W. Grimes */ 799d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 800df8bae1dSRodney W. Grimes struct sigsuspend_args { 8012c42a146SMarcel Moolenaar const sigset_t *sigmask; 802df8bae1dSRodney W. Grimes }; 803d2d3e875SBruce Evans #endif 804fb99ab88SMatthew Dillon /* 805fb99ab88SMatthew Dillon * MPSAFE 806fb99ab88SMatthew Dillon */ 807df8bae1dSRodney W. Grimes /* ARGSUSED */ 80826f9a767SRodney W. Grimes int 809b40ce416SJulian Elischer sigsuspend(td, uap) 810b40ce416SJulian Elischer struct thread *td; 811df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 812df8bae1dSRodney W. Grimes { 8132c42a146SMarcel Moolenaar sigset_t mask; 8142c42a146SMarcel Moolenaar int error; 8152c42a146SMarcel Moolenaar 8166f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 8172c42a146SMarcel Moolenaar if (error) 8182c42a146SMarcel Moolenaar return (error); 8198f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 8208f19eb88SIan Dowse } 8218f19eb88SIan Dowse 8228f19eb88SIan Dowse int 8238f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 8248f19eb88SIan Dowse { 8258f19eb88SIan Dowse struct proc *p = td->td_proc; 8268f19eb88SIan Dowse register struct sigacts *ps; 827df8bae1dSRodney W. Grimes 828df8bae1dSRodney W. Grimes /* 829645682fdSLuoqi Chen * When returning from sigsuspend, we want 830df8bae1dSRodney W. Grimes * the old mask to be restored after the 831df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 832df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 833df8bae1dSRodney W. Grimes * to indicate this. 834df8bae1dSRodney W. Grimes */ 835fb99ab88SMatthew Dillon mtx_lock(&Giant); 836628d2653SJohn Baldwin PROC_LOCK(p); 837628d2653SJohn Baldwin ps = p->p_sigacts; 8386626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 839645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 840645682fdSLuoqi Chen 841645682fdSLuoqi Chen SIG_CANTMASK(mask); 8422c42a146SMarcel Moolenaar p->p_sigmask = mask; 84379065dbaSBruce Evans signotify(p); 84401609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 8452c42a146SMarcel Moolenaar /* void */; 846628d2653SJohn Baldwin PROC_UNLOCK(p); 847fb99ab88SMatthew Dillon mtx_unlock(&Giant); 8482c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 8492c42a146SMarcel Moolenaar return (EINTR); 8502c42a146SMarcel Moolenaar } 8512c42a146SMarcel Moolenaar 85231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8532c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8542c42a146SMarcel Moolenaar struct osigsuspend_args { 8552c42a146SMarcel Moolenaar osigset_t mask; 8562c42a146SMarcel Moolenaar }; 8572c42a146SMarcel Moolenaar #endif 858fb99ab88SMatthew Dillon /* 859fb99ab88SMatthew Dillon * MPSAFE 860fb99ab88SMatthew Dillon */ 8612c42a146SMarcel Moolenaar /* ARGSUSED */ 8622c42a146SMarcel Moolenaar int 863b40ce416SJulian Elischer osigsuspend(td, uap) 864b40ce416SJulian Elischer struct thread *td; 8652c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 8662c42a146SMarcel Moolenaar { 867b40ce416SJulian Elischer struct proc *p = td->td_proc; 868645682fdSLuoqi Chen sigset_t mask; 869628d2653SJohn Baldwin register struct sigacts *ps; 8702c42a146SMarcel Moolenaar 871fb99ab88SMatthew Dillon mtx_lock(&Giant); 872628d2653SJohn Baldwin PROC_LOCK(p); 873628d2653SJohn Baldwin ps = p->p_sigacts; 8742c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 875645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 876645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 877645682fdSLuoqi Chen SIG_CANTMASK(mask); 878645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 87979065dbaSBruce Evans signotify(p); 88001609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 881df8bae1dSRodney W. Grimes /* void */; 882628d2653SJohn Baldwin PROC_UNLOCK(p); 883fb99ab88SMatthew Dillon mtx_unlock(&Giant); 884df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 885df8bae1dSRodney W. Grimes return (EINTR); 886df8bae1dSRodney W. Grimes } 88731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 890d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 891df8bae1dSRodney W. Grimes struct osigstack_args { 892df8bae1dSRodney W. Grimes struct sigstack *nss; 893df8bae1dSRodney W. Grimes struct sigstack *oss; 894df8bae1dSRodney W. Grimes }; 895d2d3e875SBruce Evans #endif 896fb99ab88SMatthew Dillon /* 897fb99ab88SMatthew Dillon * MPSAFE 898fb99ab88SMatthew Dillon */ 899df8bae1dSRodney W. Grimes /* ARGSUSED */ 90026f9a767SRodney W. Grimes int 901b40ce416SJulian Elischer osigstack(td, uap) 902b40ce416SJulian Elischer struct thread *td; 903df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 904df8bae1dSRodney W. Grimes { 905b40ce416SJulian Elischer struct proc *p = td->td_proc; 906df8bae1dSRodney W. Grimes struct sigstack ss; 907fb99ab88SMatthew Dillon int error = 0; 908fb99ab88SMatthew Dillon 909fb99ab88SMatthew Dillon mtx_lock(&Giant); 910df8bae1dSRodney W. Grimes 911d034d459SMarcel Moolenaar if (uap->oss != NULL) { 912628d2653SJohn Baldwin PROC_LOCK(p); 913645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 914b40ce416SJulian Elischer ss.ss_onstack = sigonstack(cpu_getstack(td)); 915628d2653SJohn Baldwin PROC_UNLOCK(p); 916d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 917d034d459SMarcel Moolenaar if (error) 918fb99ab88SMatthew Dillon goto done2; 919d034d459SMarcel Moolenaar } 920d034d459SMarcel Moolenaar 921d034d459SMarcel Moolenaar if (uap->nss != NULL) { 922d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 923fb99ab88SMatthew Dillon goto done2; 924628d2653SJohn Baldwin PROC_LOCK(p); 925645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 926645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 927645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 928645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 929628d2653SJohn Baldwin PROC_UNLOCK(p); 930df8bae1dSRodney W. Grimes } 931fb99ab88SMatthew Dillon done2: 932fb99ab88SMatthew Dillon mtx_unlock(&Giant); 933fb99ab88SMatthew Dillon return (error); 934df8bae1dSRodney W. Grimes } 935df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 936df8bae1dSRodney W. Grimes 937d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 938df8bae1dSRodney W. Grimes struct sigaltstack_args { 9392c42a146SMarcel Moolenaar stack_t *ss; 9402c42a146SMarcel Moolenaar stack_t *oss; 941df8bae1dSRodney W. Grimes }; 942d2d3e875SBruce Evans #endif 943fb99ab88SMatthew Dillon /* 944fb99ab88SMatthew Dillon * MPSAFE 945fb99ab88SMatthew Dillon */ 946df8bae1dSRodney W. Grimes /* ARGSUSED */ 94726f9a767SRodney W. Grimes int 948b40ce416SJulian Elischer sigaltstack(td, uap) 949b40ce416SJulian Elischer struct thread *td; 950df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 951df8bae1dSRodney W. Grimes { 9528f19eb88SIan Dowse stack_t ss, oss; 9538f19eb88SIan Dowse int error; 9548f19eb88SIan Dowse 9558f19eb88SIan Dowse if (uap->ss != NULL) { 9568f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 9578f19eb88SIan Dowse if (error) 9588f19eb88SIan Dowse return (error); 9598f19eb88SIan Dowse } 9608f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 9618f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 9628f19eb88SIan Dowse if (error) 9638f19eb88SIan Dowse return (error); 9648f19eb88SIan Dowse if (uap->oss != NULL) 9658f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 9668f19eb88SIan Dowse return (error); 9678f19eb88SIan Dowse } 9688f19eb88SIan Dowse 9698f19eb88SIan Dowse int 9708f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 9718f19eb88SIan Dowse { 972b40ce416SJulian Elischer struct proc *p = td->td_proc; 973fb99ab88SMatthew Dillon int oonstack; 974fb99ab88SMatthew Dillon int error = 0; 975fb99ab88SMatthew Dillon 976fb99ab88SMatthew Dillon mtx_lock(&Giant); 977df8bae1dSRodney W. Grimes 978b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 979d034d459SMarcel Moolenaar 9808f19eb88SIan Dowse if (oss != NULL) { 981628d2653SJohn Baldwin PROC_LOCK(p); 9828f19eb88SIan Dowse *oss = p->p_sigstk; 9838f19eb88SIan Dowse oss->ss_flags = (p->p_flag & P_ALTSTACK) 984d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 985628d2653SJohn Baldwin PROC_UNLOCK(p); 986df8bae1dSRodney W. Grimes } 987d034d459SMarcel Moolenaar 9888f19eb88SIan Dowse if (ss != NULL) { 989fb99ab88SMatthew Dillon if (oonstack) { 990fb99ab88SMatthew Dillon error = EPERM; 991fb99ab88SMatthew Dillon goto done2; 992fb99ab88SMatthew Dillon } 9938f19eb88SIan Dowse if ((ss->ss_flags & ~SS_DISABLE) != 0) { 994fb99ab88SMatthew Dillon error = EINVAL; 995fb99ab88SMatthew Dillon goto done2; 996fb99ab88SMatthew Dillon } 9978f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 9988f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 999fb99ab88SMatthew Dillon error = ENOMEM; 1000fb99ab88SMatthew Dillon goto done2; 1001fb99ab88SMatthew Dillon } 1002628d2653SJohn Baldwin PROC_LOCK(p); 10038f19eb88SIan Dowse p->p_sigstk = *ss; 1004d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 1005628d2653SJohn Baldwin PROC_UNLOCK(p); 1006628d2653SJohn Baldwin } else { 1007628d2653SJohn Baldwin PROC_LOCK(p); 1008d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 1009628d2653SJohn Baldwin PROC_UNLOCK(p); 1010628d2653SJohn Baldwin } 1011d034d459SMarcel Moolenaar } 1012fb99ab88SMatthew Dillon done2: 1013fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1014fb99ab88SMatthew Dillon return (error); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 1017d93f860cSPoul-Henning Kamp /* 1018d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1019d93f860cSPoul-Henning Kamp * cp is calling process. 1020d93f860cSPoul-Henning Kamp */ 102137c84183SPoul-Henning Kamp static int 10229c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all) 10239c1ab3e0SJohn Baldwin register struct thread *td; 10242c42a146SMarcel Moolenaar int sig, pgid, all; 1025d93f860cSPoul-Henning Kamp { 1026d93f860cSPoul-Henning Kamp register struct proc *p; 1027d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1028d93f860cSPoul-Henning Kamp int nfound = 0; 1029d93f860cSPoul-Henning Kamp 1030553629ebSJake Burkholder if (all) { 1031d93f860cSPoul-Henning Kamp /* 1032d93f860cSPoul-Henning Kamp * broadcast 1033d93f860cSPoul-Henning Kamp */ 10341005a129SJohn Baldwin sx_slock(&allproc_lock); 10352e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1036628d2653SJohn Baldwin PROC_LOCK(p); 10379c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 10389c1ab3e0SJohn Baldwin p == td->td_proc) { 1039628d2653SJohn Baldwin PROC_UNLOCK(p); 1040628d2653SJohn Baldwin continue; 1041628d2653SJohn Baldwin } 1042f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1043d93f860cSPoul-Henning Kamp nfound++; 104433a9ed9dSJohn Baldwin if (sig) 10452c42a146SMarcel Moolenaar psignal(p, sig); 1046628d2653SJohn Baldwin } 104733a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1048d93f860cSPoul-Henning Kamp } 10491005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1050553629ebSJake Burkholder } else { 1051ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1052f591779bSSeigo Tanimura if (pgid == 0) { 1053d93f860cSPoul-Henning Kamp /* 1054d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1055d93f860cSPoul-Henning Kamp */ 10569c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1057f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1058f591779bSSeigo Tanimura } else { 1059d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1060f591779bSSeigo Tanimura if (pgrp == NULL) { 1061ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1062d93f860cSPoul-Henning Kamp return (ESRCH); 1063d93f860cSPoul-Henning Kamp } 1064f591779bSSeigo Tanimura } 1065ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 10662e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1067628d2653SJohn Baldwin PROC_LOCK(p); 1068628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1069628d2653SJohn Baldwin PROC_UNLOCK(p); 1070628d2653SJohn Baldwin continue; 1071628d2653SJohn Baldwin } 1072e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 107333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1074628d2653SJohn Baldwin continue; 1075628d2653SJohn Baldwin } 1076f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1077d93f860cSPoul-Henning Kamp nfound++; 107833a9ed9dSJohn Baldwin if (sig) 10792c42a146SMarcel Moolenaar psignal(p, sig); 1080628d2653SJohn Baldwin } 108133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1082d93f860cSPoul-Henning Kamp } 1083f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1084d93f860cSPoul-Henning Kamp } 1085d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1086d93f860cSPoul-Henning Kamp } 1087d93f860cSPoul-Henning Kamp 1088d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1089df8bae1dSRodney W. Grimes struct kill_args { 1090df8bae1dSRodney W. Grimes int pid; 1091df8bae1dSRodney W. Grimes int signum; 1092df8bae1dSRodney W. Grimes }; 1093d2d3e875SBruce Evans #endif 1094fb99ab88SMatthew Dillon /* 1095fb99ab88SMatthew Dillon * MPSAFE 1096fb99ab88SMatthew Dillon */ 1097df8bae1dSRodney W. Grimes /* ARGSUSED */ 109826f9a767SRodney W. Grimes int 1099b40ce416SJulian Elischer kill(td, uap) 1100b40ce416SJulian Elischer register struct thread *td; 1101df8bae1dSRodney W. Grimes register struct kill_args *uap; 1102df8bae1dSRodney W. Grimes { 1103df8bae1dSRodney W. Grimes register struct proc *p; 1104fb99ab88SMatthew Dillon int error = 0; 1105df8bae1dSRodney W. Grimes 11066c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1107df8bae1dSRodney W. Grimes return (EINVAL); 1108fb99ab88SMatthew Dillon 1109fb99ab88SMatthew Dillon mtx_lock(&Giant); 1110df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1111df8bae1dSRodney W. Grimes /* kill single process */ 1112fb99ab88SMatthew Dillon if ((p = pfind(uap->pid)) == NULL) { 1113fb99ab88SMatthew Dillon error = ESRCH; 1114f44d9e24SJohn Baldwin } else if ((error = p_cansignal(td, p, uap->signum)) != 0) { 111533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1116fb99ab88SMatthew Dillon } else { 111733a9ed9dSJohn Baldwin if (uap->signum) 1118df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1119628d2653SJohn Baldwin PROC_UNLOCK(p); 1120fb99ab88SMatthew Dillon error = 0; 1121df8bae1dSRodney W. Grimes } 1122fb99ab88SMatthew Dillon } else { 1123df8bae1dSRodney W. Grimes switch (uap->pid) { 1124df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 11259c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 1); 1126fb99ab88SMatthew Dillon break; 1127df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 11289c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 0); 1129fb99ab88SMatthew Dillon break; 1130df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 11319c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, -uap->pid, 0); 1132fb99ab88SMatthew Dillon break; 1133df8bae1dSRodney W. Grimes } 1134fb99ab88SMatthew Dillon } 1135fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1136fb99ab88SMatthew Dillon return(error); 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes 1139df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1140d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1141df8bae1dSRodney W. Grimes struct okillpg_args { 1142df8bae1dSRodney W. Grimes int pgid; 1143df8bae1dSRodney W. Grimes int signum; 1144df8bae1dSRodney W. Grimes }; 1145d2d3e875SBruce Evans #endif 1146fb99ab88SMatthew Dillon /* 1147fb99ab88SMatthew Dillon * MPSAFE 1148fb99ab88SMatthew Dillon */ 1149df8bae1dSRodney W. Grimes /* ARGSUSED */ 115026f9a767SRodney W. Grimes int 1151b40ce416SJulian Elischer okillpg(td, uap) 1152b40ce416SJulian Elischer struct thread *td; 1153df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1154df8bae1dSRodney W. Grimes { 1155fb99ab88SMatthew Dillon int error; 1156df8bae1dSRodney W. Grimes 11576c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1158df8bae1dSRodney W. Grimes return (EINVAL); 1159fb99ab88SMatthew Dillon mtx_lock(&Giant); 11609c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, uap->pgid, 0); 1161fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1162fb99ab88SMatthew Dillon return (error); 1163df8bae1dSRodney W. Grimes } 1164df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1165df8bae1dSRodney W. Grimes 1166df8bae1dSRodney W. Grimes /* 1167df8bae1dSRodney W. Grimes * Send a signal to a process group. 1168df8bae1dSRodney W. Grimes */ 1169df8bae1dSRodney W. Grimes void 11702c42a146SMarcel Moolenaar gsignal(pgid, sig) 11712c42a146SMarcel Moolenaar int pgid, sig; 1172df8bae1dSRodney W. Grimes { 1173df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1174df8bae1dSRodney W. Grimes 1175f591779bSSeigo Tanimura if (pgid != 0) { 1176ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1177f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1178ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1179f591779bSSeigo Tanimura if (pgrp != NULL) { 11802c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1181f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1182f591779bSSeigo Tanimura } 1183f591779bSSeigo Tanimura } 1184df8bae1dSRodney W. Grimes } 1185df8bae1dSRodney W. Grimes 1186df8bae1dSRodney W. Grimes /* 1187df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1188df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1189df8bae1dSRodney W. Grimes */ 1190df8bae1dSRodney W. Grimes void 11912c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1192df8bae1dSRodney W. Grimes struct pgrp *pgrp; 11932c42a146SMarcel Moolenaar int sig, checkctty; 1194df8bae1dSRodney W. Grimes { 1195df8bae1dSRodney W. Grimes register struct proc *p; 1196df8bae1dSRodney W. Grimes 1197628d2653SJohn Baldwin if (pgrp) { 1198f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1199628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1200628d2653SJohn Baldwin PROC_LOCK(p); 1201df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 12022c42a146SMarcel Moolenaar psignal(p, sig); 1203628d2653SJohn Baldwin PROC_UNLOCK(p); 1204628d2653SJohn Baldwin } 1205628d2653SJohn Baldwin } 1206df8bae1dSRodney W. Grimes } 1207df8bae1dSRodney W. Grimes 1208df8bae1dSRodney W. Grimes /* 1209df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1210df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1211df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1212356861dbSMatthew Dillon * 1213356861dbSMatthew Dillon * MPSAFE 1214df8bae1dSRodney W. Grimes */ 1215df8bae1dSRodney W. Grimes void 12162c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1217df8bae1dSRodney W. Grimes struct proc *p; 12182c42a146SMarcel Moolenaar register int sig; 12198674077aSJeffrey Hsu u_long code; 1220df8bae1dSRodney W. Grimes { 1221df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1222df8bae1dSRodney W. Grimes 1223628d2653SJohn Baldwin PROC_LOCK(p); 12242c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1225d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1226df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1227df8bae1dSRodney W. Grimes #ifdef KTRACE 1228374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1229374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 12302c42a146SMarcel Moolenaar &p->p_sigmask, code); 1231df8bae1dSRodney W. Grimes #endif 12322c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 12332c42a146SMarcel Moolenaar &p->p_sigmask, code); 12342c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 12352c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 12362c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 12372c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1238289ccde0SPeter Wemm /* 12398f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1240289ccde0SPeter Wemm */ 12412c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 12422c42a146SMarcel Moolenaar if (sig != SIGCONT && 12432c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 12442c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 12452c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1246dedc04feSPeter Wemm } 12476f841fb7SMarcel Moolenaar } else { 12486626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 12492c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 12502c42a146SMarcel Moolenaar psignal(p, sig); 1251df8bae1dSRodney W. Grimes } 1252628d2653SJohn Baldwin PROC_UNLOCK(p); 1253df8bae1dSRodney W. Grimes } 1254df8bae1dSRodney W. Grimes 1255df8bae1dSRodney W. Grimes /* 1256df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1257df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1258df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1259df8bae1dSRodney W. Grimes * 1260df8bae1dSRodney W. Grimes * Exceptions: 1261df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1262df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1263df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1264df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1265df8bae1dSRodney W. Grimes * 1266df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1267df8bae1dSRodney W. Grimes */ 1268df8bae1dSRodney W. Grimes void 12692c42a146SMarcel Moolenaar psignal(p, sig) 1270df8bae1dSRodney W. Grimes register struct proc *p; 12712c42a146SMarcel Moolenaar register int sig; 1272df8bae1dSRodney W. Grimes { 1273df8bae1dSRodney W. Grimes register sig_t action; 1274b40ce416SJulian Elischer struct thread *td; 1275e602ba25SJulian Elischer register int prop; 1276e602ba25SJulian Elischer 1277df8bae1dSRodney W. Grimes 12782899d606SDag-Erling Smørgrav KASSERT(_SIG_VALID(sig), 12792899d606SDag-Erling Smørgrav ("psignal(): invalid signal %d\n", sig)); 12802c42a146SMarcel Moolenaar 1281628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1282cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1283cb679c38SJonathan Lemon 12842c42a146SMarcel Moolenaar prop = sigprop(sig); 1285df8bae1dSRodney W. Grimes /* 12862a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 12872a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 12882a024a2bSSean Eric Fagan * a chance, as well. 1289df8bae1dSRodney W. Grimes */ 1290b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1291df8bae1dSRodney W. Grimes action = SIG_DFL; 1292b40ce416SJulian Elischer } else { 1293df8bae1dSRodney W. Grimes /* 1294df8bae1dSRodney W. Grimes * If the signal is being ignored, 1295df8bae1dSRodney W. Grimes * then we forget about it immediately. 1296df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1297df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1298df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1299df8bae1dSRodney W. Grimes */ 1300628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1301df8bae1dSRodney W. Grimes return; 13022c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1303df8bae1dSRodney W. Grimes action = SIG_HOLD; 13042c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1305df8bae1dSRodney W. Grimes action = SIG_CATCH; 1306df8bae1dSRodney W. Grimes else 1307df8bae1dSRodney W. Grimes action = SIG_DFL; 1308df8bae1dSRodney W. Grimes } 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes if (prop & SA_CONT) 13112c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1312df8bae1dSRodney W. Grimes 1313df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1314df8bae1dSRodney W. Grimes /* 1315df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1316df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1317df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1318df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1319df8bae1dSRodney W. Grimes */ 1320e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1321e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1322e602ba25SJulian Elischer (action == SIG_DFL)) 1323df8bae1dSRodney W. Grimes return; 13242c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 13256933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1326df8bae1dSRodney W. Grimes } 13272c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1328aa0fa334SJulian Elischer signotify(p); /* uses schedlock */ 1329df8bae1dSRodney W. Grimes 1330df8bae1dSRodney W. Grimes /* 1331e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1332e602ba25SJulian Elischer * component. Most processing occurs when the process next 1333e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1334e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1335e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1336e602ba25SJulian Elischer * We try do the per-process part here. 1337df8bae1dSRodney W. Grimes */ 1338e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1339e602ba25SJulian Elischer /* 1340e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1341e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1342e602ba25SJulian Elischer */ 1343e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1344e602ba25SJulian Elischer /* 1345e602ba25SJulian Elischer * The traced process is already stopped, 1346e602ba25SJulian Elischer * so no further action is necessary. 1347e602ba25SJulian Elischer * No signal can restart us. 1348e602ba25SJulian Elischer */ 1349e602ba25SJulian Elischer goto out; 13501c32c37cSJohn Baldwin } 1351b40ce416SJulian Elischer 1352e602ba25SJulian Elischer if (sig == SIGKILL) { 1353df8bae1dSRodney W. Grimes /* 1354e602ba25SJulian Elischer * SIGKILL sets process running. 1355e602ba25SJulian Elischer * It will die elsewhere. 1356e602ba25SJulian Elischer * All threads must be restarted. 1357df8bae1dSRodney W. Grimes */ 1358e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED; 1359e602ba25SJulian Elischer goto runfast; 1360e602ba25SJulian Elischer } 1361e602ba25SJulian Elischer 1362e602ba25SJulian Elischer if (prop & SA_CONT) { 1363e602ba25SJulian Elischer /* 1364e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 1365e602ba25SJulian Elischer * process but don't leave the signal in p_siglist as 1366e602ba25SJulian Elischer * it has no further action. If SIGCONT is held, we 1367e602ba25SJulian Elischer * continue the process and leave the signal in 1368e602ba25SJulian Elischer * p_siglist. If the process catches SIGCONT, let it 1369e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1370e602ba25SJulian Elischer * an event, it goes back to run state. 1371e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1372e602ba25SJulian Elischer */ 13731279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 13746933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1375e602ba25SJulian Elischer if (action == SIG_DFL) { 1376e602ba25SJulian Elischer SIGDELSET(p->p_siglist, sig); 1377e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1378e602ba25SJulian Elischer /* 1379e602ba25SJulian Elischer * The process wants to catch it so it needs 1380e602ba25SJulian Elischer * to run at least one thread, but which one? 1381e602ba25SJulian Elischer * It would seem that the answer would be to 1382e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1383e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1384e602ba25SJulian Elischer * process, we need to make sure that the 1385e602ba25SJulian Elischer * single thread is runnable asap. 1386e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1387e602ba25SJulian Elischer */ 1388e602ba25SJulian Elischer goto runfast; 1389e602ba25SJulian Elischer } 1390e602ba25SJulian Elischer /* 1391e602ba25SJulian Elischer * The signal is not ignored or caught. 1392e602ba25SJulian Elischer */ 1393e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 139404774f23SJulian Elischer thread_unsuspend(p); 1395e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1396e602ba25SJulian Elischer goto out; 1397e602ba25SJulian Elischer } 1398e602ba25SJulian Elischer 1399e602ba25SJulian Elischer if (prop & SA_STOP) { 1400e602ba25SJulian Elischer /* 1401e602ba25SJulian Elischer * Already stopped, don't need to stop again 1402e602ba25SJulian Elischer * (If we did the shell could get confused). 140304774f23SJulian Elischer * Just make sure the signal STOP bit set. 1404e602ba25SJulian Elischer */ 14051279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 1406e602ba25SJulian Elischer SIGDELSET(p->p_siglist, sig); 1407e602ba25SJulian Elischer goto out; 1408e602ba25SJulian Elischer } 1409e602ba25SJulian Elischer 1410e602ba25SJulian Elischer /* 1411e602ba25SJulian Elischer * All other kinds of signals: 1412e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1413e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1414e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 141504774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1416e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1417e602ba25SJulian Elischer */ 1418e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 1419e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 142071fad9fdSJulian Elischer if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) { 1421e602ba25SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 142271fad9fdSJulian Elischer cv_abort(td); 1423e602ba25SJulian Elischer else 142471fad9fdSJulian Elischer abortsleep(td); 1425e602ba25SJulian Elischer } 1426e602ba25SJulian Elischer } 1427e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1428df8bae1dSRodney W. Grimes goto out; 1429df8bae1dSRodney W. Grimes /* 1430e602ba25SJulian Elischer * XXXKSE What about threads that are waiting on mutexes? 1431e602ba25SJulian Elischer * Shouldn't they abort too? 143204774f23SJulian Elischer * No, hopefully mutexes are short lived.. They'll 143304774f23SJulian Elischer * eventually hit thread_suspend_check(). 1434df8bae1dSRodney W. Grimes */ 1435e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 1436e602ba25SJulian Elischer if (prop & SA_CONT) { 1437df8bae1dSRodney W. Grimes /* 1438e602ba25SJulian Elischer * Already active, don't need to start again. 1439df8bae1dSRodney W. Grimes */ 14402c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1441df8bae1dSRodney W. Grimes goto out; 1442df8bae1dSRodney W. Grimes } 144335c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 144435c32a76SDavid Xu !(prop & SA_STOP)) { 1445721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 1446721e5910SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) 1447721e5910SJulian Elischer tdsignal(td, sig, action); 1448721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1449df8bae1dSRodney W. Grimes goto out; 145004774f23SJulian Elischer } 145135c32a76SDavid Xu if (prop & SA_STOP) { 145235c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 145335c32a76SDavid Xu goto out; 145435c32a76SDavid Xu mtx_lock_spin(&sched_lock); 145535c32a76SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 145671fad9fdSJulian Elischer if (TD_IS_SLEEPING(td) && 145735c32a76SDavid Xu (td->td_flags & TDF_SINTR)) 145835c32a76SDavid Xu thread_suspend_one(td); 145935c32a76SDavid Xu } 146035c32a76SDavid Xu if (p->p_suspcount == p->p_numthreads) { 146135c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 146235c32a76SDavid Xu stop(p); 146335c32a76SDavid Xu p->p_xstat = sig; 146435c32a76SDavid Xu SIGDELSET(p->p_siglist, sig); 146535c32a76SDavid Xu PROC_LOCK(p->p_pptr); 146635c32a76SDavid Xu if ((p->p_pptr->p_procsig->ps_flag & 146735c32a76SDavid Xu PS_NOCLDSTOP) == 0) { 146835c32a76SDavid Xu psignal(p->p_pptr, SIGCHLD); 146935c32a76SDavid Xu } 147035c32a76SDavid Xu PROC_UNLOCK(p->p_pptr); 147135c32a76SDavid Xu } else { 147235c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 147335c32a76SDavid Xu } 147435c32a76SDavid Xu goto out; 147535c32a76SDavid Xu } 1476721e5910SJulian Elischer else 1477df8bae1dSRodney W. Grimes goto runfast; 1478df8bae1dSRodney W. Grimes /* NOTREACHED */ 1479e602ba25SJulian Elischer } else { 1480e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 14812c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1482e602ba25SJulian Elischer goto out; 1483e602ba25SJulian Elischer } 1484e602ba25SJulian Elischer 1485b40ce416SJulian Elischer /* 1486e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1487e602ba25SJulian Elischer * running threads. 1488b40ce416SJulian Elischer */ 1489e602ba25SJulian Elischer 1490e602ba25SJulian Elischer runfast: 1491aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 1492e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) 1493e602ba25SJulian Elischer tdsignal(td, sig, action); 1494e602ba25SJulian Elischer thread_unsuspend(p); 1495e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1496e602ba25SJulian Elischer out: 1497e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1498e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1499e602ba25SJulian Elischer } 1500e602ba25SJulian Elischer 1501e602ba25SJulian Elischer /* 1502e602ba25SJulian Elischer * The force of a signal has been directed against a single 1503e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1504e602ba25SJulian Elischer * out of any sleep it may be in etc. 1505e602ba25SJulian Elischer */ 1506e602ba25SJulian Elischer static void 1507e602ba25SJulian Elischer tdsignal(struct thread *td, int sig, sig_t action) 1508e602ba25SJulian Elischer { 1509e602ba25SJulian Elischer struct proc *p = td->td_proc; 1510e602ba25SJulian Elischer register int prop; 1511e602ba25SJulian Elischer 1512aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1513e602ba25SJulian Elischer prop = sigprop(sig); 1514e602ba25SJulian Elischer /* 1515aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1516e602ba25SJulian Elischer * killed in this lifetime. 1517e602ba25SJulian Elischer */ 1518e602ba25SJulian Elischer if ((action == SIG_DFL) && (prop & SA_KILL)) { 1519e602ba25SJulian Elischer if (td->td_priority > PUSER) { 1520e602ba25SJulian Elischer td->td_priority = PUSER; 1521b40ce416SJulian Elischer } 1522b40ce416SJulian Elischer } 1523e602ba25SJulian Elischer 1524e602ba25SJulian Elischer /* 1525e602ba25SJulian Elischer * Defer further processing for signals which are held, 1526e602ba25SJulian Elischer * except that stopped processes must be continued by SIGCONT. 1527e602ba25SJulian Elischer */ 1528e602ba25SJulian Elischer if (action == SIG_HOLD) { 1529aa0fa334SJulian Elischer return; 1530e602ba25SJulian Elischer } 153171fad9fdSJulian Elischer if (TD_IS_SLEEPING(td)) { 1532e602ba25SJulian Elischer /* 1533e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1534e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1535e602ba25SJulian Elischer * be noticed when the process returns through 1536e602ba25SJulian Elischer * trap() or syscall(). 1537e602ba25SJulian Elischer */ 1538e602ba25SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 1539aa0fa334SJulian Elischer return; 1540e602ba25SJulian Elischer } 1541e602ba25SJulian Elischer /* 1542e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1543e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1544e602ba25SJulian Elischer * for its parent. 1545e602ba25SJulian Elischer */ 1546e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1547e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1548aa0fa334SJulian Elischer } else { 1549aa0fa334SJulian Elischer 1550df8bae1dSRodney W. Grimes /* 1551e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1552e602ba25SJulian Elischer * asleep, we are finished; the process should not 1553e602ba25SJulian Elischer * be awakened. 1554df8bae1dSRodney W. Grimes */ 1555e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 15562c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1557aa0fa334SJulian Elischer return; 1558df8bae1dSRodney W. Grimes } 1559df8bae1dSRodney W. Grimes 1560aa0fa334SJulian Elischer /* 1561aa0fa334SJulian Elischer * Raise priority to at least PUSER. 1562aa0fa334SJulian Elischer */ 1563aa0fa334SJulian Elischer if (td->td_priority > PUSER) { 1564aa0fa334SJulian Elischer td->td_priority = PUSER; 1565aa0fa334SJulian Elischer } 1566aa0fa334SJulian Elischer } 156771fad9fdSJulian Elischer if (td->td_flags & TDF_CVWAITQ) 156871fad9fdSJulian Elischer cv_abort(td); 156971fad9fdSJulian Elischer else 157071fad9fdSJulian Elischer abortsleep(td); 1571aa0fa334SJulian Elischer } 1572aa0fa334SJulian Elischer #ifdef SMP 1573aa0fa334SJulian Elischer else { 1574df8bae1dSRodney W. Grimes /* 1575e602ba25SJulian Elischer * Other states do nothing with the signal immediatly, 1576df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1577df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1578df8bae1dSRodney W. Grimes */ 157971fad9fdSJulian Elischer if (TD_IS_RUNNING(td) && td != curthread) { 1580e602ba25SJulian Elischer forward_signal(td); 1581aa0fa334SJulian Elischer } 15820ac3b636SAndrew Gallatin } 15833163861cSTor Egge #endif 15846caa8a15SJohn Baldwin } 1585df8bae1dSRodney W. Grimes 1586df8bae1dSRodney W. Grimes /* 1587df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1588df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1589df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1590df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1591df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1592628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 1593df8bae1dSRodney W. Grimes * sequence is 1594df8bae1dSRodney W. Grimes * 1595e602ba25SJulian Elischer * while (sig = cursig(curthread)) 15962c42a146SMarcel Moolenaar * postsig(sig); 1597df8bae1dSRodney W. Grimes */ 159826f9a767SRodney W. Grimes int 1599e602ba25SJulian Elischer issignal(td) 1600e602ba25SJulian Elischer struct thread *td; 1601df8bae1dSRodney W. Grimes { 1602e602ba25SJulian Elischer struct proc *p; 16032c42a146SMarcel Moolenaar sigset_t mask; 16042c42a146SMarcel Moolenaar register int sig, prop; 1605df8bae1dSRodney W. Grimes 1606e602ba25SJulian Elischer p = td->td_proc; 1607628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1608b39f3284SJulian Elischer WITNESS_SLEEP(1, &p->p_mtx.mtx_object); 1609df8bae1dSRodney W. Grimes for (;;) { 16102a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 16112a024a2bSSean Eric Fagan 16122c42a146SMarcel Moolenaar mask = p->p_siglist; 16132c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1614df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 16152c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 1616ca18d53eSChad David if (SIGISEMPTY(mask)) /* no signal to send */ 1617df8bae1dSRodney W. Grimes return (0); 16182c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 16192c42a146SMarcel Moolenaar prop = sigprop(sig); 16202a024a2bSSean Eric Fagan 1621628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 16222a024a2bSSean Eric Fagan 1623df8bae1dSRodney W. Grimes /* 1624df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1625df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1626df8bae1dSRodney W. Grimes */ 16272c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 16282c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1629df8bae1dSRodney W. Grimes continue; 1630df8bae1dSRodney W. Grimes } 1631df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1632df8bae1dSRodney W. Grimes /* 1633d8f4f6a4SJonathan Mini * If traced, always stop. 1634df8bae1dSRodney W. Grimes */ 16352c42a146SMarcel Moolenaar p->p_xstat = sig; 1636628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1637df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1638628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 16399ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 16404d492b43SJulian Elischer stop(p); /* uses schedlock too eventually */ 164171fad9fdSJulian Elischer thread_suspend_one(td); 1642c86b6ff5SJohn Baldwin PROC_UNLOCK(p); 1643c86b6ff5SJohn Baldwin DROP_GIANT(); 16442ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1645df8bae1dSRodney W. Grimes mi_switch(); 16469ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 164720cdcc5bSJohn Baldwin PICKUP_GIANT(); 1648628d2653SJohn Baldwin PROC_LOCK(p); 1649df8bae1dSRodney W. Grimes 1650df8bae1dSRodney W. Grimes /* 1651df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1652df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1653df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1654df8bae1dSRodney W. Grimes */ 1655df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1656df8bae1dSRodney W. Grimes continue; 1657df8bae1dSRodney W. Grimes 1658df8bae1dSRodney W. Grimes /* 1659df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1660df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1661df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1662df8bae1dSRodney W. Grimes */ 16632c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 16642c42a146SMarcel Moolenaar sig = p->p_xstat; 16652c42a146SMarcel Moolenaar if (sig == 0) 1666df8bae1dSRodney W. Grimes continue; 1667df8bae1dSRodney W. Grimes 1668df8bae1dSRodney W. Grimes /* 1669df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1670df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1671df8bae1dSRodney W. Grimes */ 16722c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 16732c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1674df8bae1dSRodney W. Grimes continue; 16751c530be4SBruce Evans signotify(p); 1676df8bae1dSRodney W. Grimes } 1677df8bae1dSRodney W. Grimes 1678df8bae1dSRodney W. Grimes /* 1679df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1680df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1681df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1682df8bae1dSRodney W. Grimes */ 16832c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1684df8bae1dSRodney W. Grimes 16850b53fbe8SBruce Evans case (int)SIG_DFL: 1686df8bae1dSRodney W. Grimes /* 1687df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1688df8bae1dSRodney W. Grimes */ 1689df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1690df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1691df8bae1dSRodney W. Grimes /* 1692df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1693df8bae1dSRodney W. Grimes * in init? XXX 1694df8bae1dSRodney W. Grimes */ 1695d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 16962c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1697df8bae1dSRodney W. Grimes #endif 1698df8bae1dSRodney W. Grimes break; /* == ignore */ 1699df8bae1dSRodney W. Grimes } 1700df8bae1dSRodney W. Grimes /* 1701df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1702df8bae1dSRodney W. Grimes * with default action, stop here, 1703df8bae1dSRodney W. Grimes * then clear the signal. However, 1704df8bae1dSRodney W. Grimes * if process is member of an orphaned 1705df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1706df8bae1dSRodney W. Grimes */ 1707df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1708df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1709df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1710df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1711df8bae1dSRodney W. Grimes break; /* == ignore */ 17122c42a146SMarcel Moolenaar p->p_xstat = sig; 1713721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 1714721e5910SJulian Elischer if (p->p_suspcount+1 == p->p_numthreads) { 1715721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1716628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1717e602ba25SJulian Elischer if ((p->p_pptr->p_procsig->ps_flag & 1718e602ba25SJulian Elischer PS_NOCLDSTOP) == 0) { 1719df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1720e602ba25SJulian Elischer } 1721628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1722d9d6e34fSJulian Elischer mtx_lock_spin(&sched_lock); 1723721e5910SJulian Elischer } 17245b3047d5SJohn Baldwin stop(p); 172571fad9fdSJulian Elischer thread_suspend_one(td); 1726721e5910SJulian Elischer PROC_UNLOCK(p); 1727721e5910SJulian Elischer DROP_GIANT(); 1728721e5910SJulian Elischer p->p_stats->p_ru.ru_nivcsw++; 1729721e5910SJulian Elischer mi_switch(); 1730721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1731721e5910SJulian Elischer PICKUP_GIANT(); 1732721e5910SJulian Elischer PROC_LOCK(p); 1733df8bae1dSRodney W. Grimes break; 1734e602ba25SJulian Elischer } else 1735e602ba25SJulian Elischer if (prop & SA_IGNORE) { 1736df8bae1dSRodney W. Grimes /* 1737df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1738df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1739df8bae1dSRodney W. Grimes */ 1740df8bae1dSRodney W. Grimes break; /* == ignore */ 1741df8bae1dSRodney W. Grimes } else 17422c42a146SMarcel Moolenaar return (sig); 1743df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1744df8bae1dSRodney W. Grimes 17450b53fbe8SBruce Evans case (int)SIG_IGN: 1746df8bae1dSRodney W. Grimes /* 1747df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1748df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1749df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1750df8bae1dSRodney W. Grimes */ 1751df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1752df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1753df8bae1dSRodney W. Grimes printf("issignal\n"); 1754df8bae1dSRodney W. Grimes break; /* == ignore */ 1755df8bae1dSRodney W. Grimes 1756df8bae1dSRodney W. Grimes default: 1757df8bae1dSRodney W. Grimes /* 1758df8bae1dSRodney W. Grimes * This signal has an action, let 1759df8bae1dSRodney W. Grimes * postsig() process it. 1760df8bae1dSRodney W. Grimes */ 17612c42a146SMarcel Moolenaar return (sig); 1762df8bae1dSRodney W. Grimes } 17632c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1764df8bae1dSRodney W. Grimes } 1765df8bae1dSRodney W. Grimes /* NOTREACHED */ 1766df8bae1dSRodney W. Grimes } 1767df8bae1dSRodney W. Grimes 1768df8bae1dSRodney W. Grimes /* 1769df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1770df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 17715b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 17725b3047d5SJohn Baldwin * lock held. 1773df8bae1dSRodney W. Grimes */ 17745b3047d5SJohn Baldwin static void 1775df8bae1dSRodney W. Grimes stop(p) 1776df8bae1dSRodney W. Grimes register struct proc *p; 1777df8bae1dSRodney W. Grimes { 1778df8bae1dSRodney W. Grimes 1779628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 17801279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 1781df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 178201609114SAlfred Perlstein wakeup(p->p_pptr); 1783df8bae1dSRodney W. Grimes } 1784df8bae1dSRodney W. Grimes 1785df8bae1dSRodney W. Grimes /* 1786df8bae1dSRodney W. Grimes * Take the action for the specified signal 1787df8bae1dSRodney W. Grimes * from the current set of pending signals. 1788df8bae1dSRodney W. Grimes */ 1789df8bae1dSRodney W. Grimes void 17902c42a146SMarcel Moolenaar postsig(sig) 17912c42a146SMarcel Moolenaar register int sig; 1792df8bae1dSRodney W. Grimes { 1793b40ce416SJulian Elischer struct thread *td = curthread; 1794b40ce416SJulian Elischer register struct proc *p = td->td_proc; 1795628d2653SJohn Baldwin struct sigacts *ps; 17962c42a146SMarcel Moolenaar sig_t action; 17972c42a146SMarcel Moolenaar sigset_t returnmask; 17982c42a146SMarcel Moolenaar int code; 1799df8bae1dSRodney W. Grimes 18002c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 18015526d2d9SEivind Eklund 18022ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1803628d2653SJohn Baldwin ps = p->p_sigacts; 18042c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 18052c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1806df8bae1dSRodney W. Grimes #ifdef KTRACE 1807374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 1808374a15aaSJohn Baldwin ktrpsig(sig, action, p->p_flag & P_OLDMASK ? 18092c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1810df8bae1dSRodney W. Grimes #endif 1811628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 18122a024a2bSSean Eric Fagan 1813df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1814df8bae1dSRodney W. Grimes /* 1815df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1816df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1817df8bae1dSRodney W. Grimes */ 1818b40ce416SJulian Elischer sigexit(td, sig); 1819df8bae1dSRodney W. Grimes /* NOTREACHED */ 1820df8bae1dSRodney W. Grimes } else { 1821df8bae1dSRodney W. Grimes /* 1822df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1823df8bae1dSRodney W. Grimes */ 18242c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 18255526d2d9SEivind Eklund ("postsig action")); 1826df8bae1dSRodney W. Grimes /* 1827df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1828645682fdSLuoqi Chen * occurrences of this signal. 1829df8bae1dSRodney W. Grimes * 1830645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1831df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1832645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1833df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1834df8bae1dSRodney W. Grimes */ 1835645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 18366626c604SJulian Elischer returnmask = p->p_oldsigmask; 1837645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1838df8bae1dSRodney W. Grimes } else 1839df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 18402c42a146SMarcel Moolenaar 18412c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 18422c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 18432c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 18442c42a146SMarcel Moolenaar 18452c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1846289ccde0SPeter Wemm /* 18478f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1848289ccde0SPeter Wemm */ 18492c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 18502c42a146SMarcel Moolenaar if (sig != SIGCONT && 18512c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 18522c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 18532c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1854dedc04feSPeter Wemm } 1855df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 18562c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1857df8bae1dSRodney W. Grimes code = 0; 1858df8bae1dSRodney W. Grimes } else { 18596626c604SJulian Elischer code = p->p_code; 18606626c604SJulian Elischer p->p_code = 0; 18616626c604SJulian Elischer p->p_sig = 0; 1862df8bae1dSRodney W. Grimes } 1863c76e33b6SJonathan Mini if (p->p_flag & P_KSES) 1864c76e33b6SJonathan Mini if (signal_upcall(p, sig)) 1865c76e33b6SJonathan Mini return; 18662c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1867df8bae1dSRodney W. Grimes } 1868df8bae1dSRodney W. Grimes } 1869df8bae1dSRodney W. Grimes 1870df8bae1dSRodney W. Grimes /* 1871df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1872df8bae1dSRodney W. Grimes */ 187326f9a767SRodney W. Grimes void 1874df8bae1dSRodney W. Grimes killproc(p, why) 1875df8bae1dSRodney W. Grimes struct proc *p; 1876df8bae1dSRodney W. Grimes char *why; 1877df8bae1dSRodney W. Grimes { 18789081e5e8SJohn Baldwin 18799081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 18800384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 18810384fff8SJason Evans p, p->p_pid, p->p_comm); 1882729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1883b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1884df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1885df8bae1dSRodney W. Grimes } 1886df8bae1dSRodney W. Grimes 1887df8bae1dSRodney W. Grimes /* 1888df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1889df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1890df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1891df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1892df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1893df8bae1dSRodney W. Grimes * does not return. 1894df8bae1dSRodney W. Grimes */ 189526f9a767SRodney W. Grimes void 1896b40ce416SJulian Elischer sigexit(td, sig) 1897b40ce416SJulian Elischer struct thread *td; 18982c42a146SMarcel Moolenaar int sig; 1899df8bae1dSRodney W. Grimes { 1900b40ce416SJulian Elischer struct proc *p = td->td_proc; 1901df8bae1dSRodney W. Grimes 1902628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1903df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 19042c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 19052c42a146SMarcel Moolenaar p->p_sig = sig; 1906c364e17eSAndrey A. Chernov /* 1907c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1908c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1909c364e17eSAndrey A. Chernov * these messages.) 1910c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1911c364e17eSAndrey A. Chernov */ 1912628d2653SJohn Baldwin PROC_UNLOCK(p); 1913c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1914c31146a1SJohn Baldwin mtx_lock(&Giant); 1915b40ce416SJulian Elischer if (coredump(td) == 0) 19162c42a146SMarcel Moolenaar sig |= WCOREFLAG; 191757308494SJoerg Wunsch if (kern_logsigexit) 191857308494SJoerg Wunsch log(LOG_INFO, 191957308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 19203d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 19219c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 19222c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 19232c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1924c31146a1SJohn Baldwin } else { 1925628d2653SJohn Baldwin PROC_UNLOCK(p); 1926628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1927628d2653SJohn Baldwin mtx_lock(&Giant); 1928c31146a1SJohn Baldwin } 1929b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 1930df8bae1dSRodney W. Grimes /* NOTREACHED */ 1931df8bae1dSRodney W. Grimes } 1932df8bae1dSRodney W. Grimes 1933c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1934c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1935c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1936c5edb423SSean Eric Fagan 1937c5edb423SSean Eric Fagan /* 1938c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1939c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1940c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1941c5edb423SSean Eric Fagan * %N name of process ("name") 1942c5edb423SSean Eric Fagan * %P process id (pid) 1943c5edb423SSean Eric Fagan * %U user id (uid) 1944c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1945c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1946c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1947c5edb423SSean Eric Fagan */ 1948c5edb423SSean Eric Fagan 1949fca666a1SJulian Elischer static char * 1950c5edb423SSean Eric Fagan expand_name(name, uid, pid) 19518b43b535SAlfred Perlstein const char *name; 19528b43b535SAlfred Perlstein uid_t uid; 19538b43b535SAlfred Perlstein pid_t pid; 19548b43b535SAlfred Perlstein { 19558b43b535SAlfred Perlstein const char *format, *appendstr; 1956c5edb423SSean Eric Fagan char *temp; 1957c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 19588b43b535SAlfred Perlstein size_t i, l, n; 1959c5edb423SSean Eric Fagan 19608b43b535SAlfred Perlstein format = corefilename; 19618b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 19620bfe2990SEivind Eklund if (temp == NULL) 19638b43b535SAlfred Perlstein return (NULL); 1964ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1965c5edb423SSean Eric Fagan switch (format[i]) { 1966c5edb423SSean Eric Fagan case '%': /* Format character */ 1967c5edb423SSean Eric Fagan i++; 1968c5edb423SSean Eric Fagan switch (format[i]) { 1969c5edb423SSean Eric Fagan case '%': 19708b43b535SAlfred Perlstein appendstr = "%"; 1971c5edb423SSean Eric Fagan break; 1972c5edb423SSean Eric Fagan case 'N': /* process name */ 19738b43b535SAlfred Perlstein appendstr = name; 1974c5edb423SSean Eric Fagan break; 1975c5edb423SSean Eric Fagan case 'P': /* process id */ 19768b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 19778b43b535SAlfred Perlstein appendstr = buf; 1978c5edb423SSean Eric Fagan break; 1979c5edb423SSean Eric Fagan case 'U': /* user id */ 19808b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 19818b43b535SAlfred Perlstein appendstr = buf; 1982c5edb423SSean Eric Fagan break; 1983c5edb423SSean Eric Fagan default: 19848b43b535SAlfred Perlstein appendstr = ""; 19858b43b535SAlfred Perlstein log(LOG_ERR, 19868b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 19878b43b535SAlfred Perlstein format[i], format); 1988c5edb423SSean Eric Fagan } 19898b43b535SAlfred Perlstein l = strlen(appendstr); 19908b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 19918b43b535SAlfred Perlstein goto toolong; 19928b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 19938b43b535SAlfred Perlstein n += l; 1994c5edb423SSean Eric Fagan break; 1995c5edb423SSean Eric Fagan default: 1996c5edb423SSean Eric Fagan temp[n++] = format[i]; 1997c5edb423SSean Eric Fagan } 1998c5edb423SSean Eric Fagan } 19998b43b535SAlfred Perlstein if (format[i] != '\0') 20008b43b535SAlfred Perlstein goto toolong; 20018b43b535SAlfred Perlstein return (temp); 20028b43b535SAlfred Perlstein toolong: 20038b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 20048b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 20058b43b535SAlfred Perlstein free(temp, M_TEMP); 20068b43b535SAlfred Perlstein return (NULL); 2007c5edb423SSean Eric Fagan } 2008c5edb423SSean Eric Fagan 2009df8bae1dSRodney W. Grimes /* 2010fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2011fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2012fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2013fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2014fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2015fca666a1SJulian Elischer */ 2016fca666a1SJulian Elischer 2017fca666a1SJulian Elischer static int 2018b40ce416SJulian Elischer coredump(struct thread *td) 2019fca666a1SJulian Elischer { 2020b40ce416SJulian Elischer struct proc *p = td->td_proc; 2021fca666a1SJulian Elischer register struct vnode *vp; 20229c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 202306ae1e91SMatthew Dillon struct flock lf; 2024fca666a1SJulian Elischer struct nameidata nd; 2025fca666a1SJulian Elischer struct vattr vattr; 2026e6796b67SKirk McKusick int error, error1, flags; 2027f2a2857bSKirk McKusick struct mount *mp; 2028fca666a1SJulian Elischer char *name; /* name of corefile */ 2029fca666a1SJulian Elischer off_t limit; 2030fca666a1SJulian Elischer 2031628d2653SJohn Baldwin PROC_LOCK(p); 2032628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2033fca666a1SJulian Elischer 2034628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2035628d2653SJohn Baldwin PROC_UNLOCK(p); 2036fca666a1SJulian Elischer return (EFAULT); 2037628d2653SJohn Baldwin } 2038fca666a1SJulian Elischer 2039fca666a1SJulian Elischer /* 204035a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 204135a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 204235a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 204335a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 204435a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 204535a2598fSSean Eric Fagan * if it is larger than the limit. 2046fca666a1SJulian Elischer */ 204735a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2048628d2653SJohn Baldwin if (limit == 0) { 2049628d2653SJohn Baldwin PROC_UNLOCK(p); 205035a2598fSSean Eric Fagan return 0; 2051628d2653SJohn Baldwin } 2052628d2653SJohn Baldwin PROC_UNLOCK(p); 205335a2598fSSean Eric Fagan 2054f2a2857bSKirk McKusick restart: 20559c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2056ccdbd10cSPeter Pentchev if (name == NULL) 2057ccdbd10cSPeter Pentchev return (EINVAL); 2058b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2059e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 2060e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2061fca666a1SJulian Elischer free(name, M_TEMP); 2062fca666a1SJulian Elischer if (error) 2063fca666a1SJulian Elischer return (error); 2064762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2065fca666a1SJulian Elischer vp = nd.ni_vp; 206606ae1e91SMatthew Dillon 2067832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2068832dafadSDon Lewis if (vp->v_type != VREG || 2069832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2070832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2071832dafadSDon Lewis error = EFAULT; 2072832dafadSDon Lewis goto out2; 2073832dafadSDon Lewis } 2074832dafadSDon Lewis 2075b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 207606ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 207706ae1e91SMatthew Dillon lf.l_start = 0; 207806ae1e91SMatthew Dillon lf.l_len = 0; 207906ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 208006ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 208106ae1e91SMatthew Dillon if (error) 208206ae1e91SMatthew Dillon goto out2; 208306ae1e91SMatthew Dillon 208406ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 208506ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 208606ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2087b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2088f2a2857bSKirk McKusick return (error); 2089f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2090f2a2857bSKirk McKusick return (error); 2091f2a2857bSKirk McKusick goto restart; 2092f2a2857bSKirk McKusick } 2093fca666a1SJulian Elischer 2094fca666a1SJulian Elischer VATTR_NULL(&vattr); 2095fca666a1SJulian Elischer vattr.va_size = 0; 209688b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2097b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2098b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 209988b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2100628d2653SJohn Baldwin PROC_LOCK(p); 2101fca666a1SJulian Elischer p->p_acflag |= ACORE; 2102628d2653SJohn Baldwin PROC_UNLOCK(p); 2103fca666a1SJulian Elischer 2104fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2105b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2106fca666a1SJulian Elischer ENOSYS; 2107fca666a1SJulian Elischer 210806ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 210906ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2110f2a2857bSKirk McKusick vn_finished_write(mp); 211106ae1e91SMatthew Dillon out2: 2112b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2113fca666a1SJulian Elischer if (error == 0) 2114fca666a1SJulian Elischer error = error1; 2115fca666a1SJulian Elischer return (error); 2116fca666a1SJulian Elischer } 2117fca666a1SJulian Elischer 2118fca666a1SJulian Elischer /* 2119df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2120df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2121df8bae1dSRodney W. Grimes */ 2122d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2123df8bae1dSRodney W. Grimes struct nosys_args { 2124df8bae1dSRodney W. Grimes int dummy; 2125df8bae1dSRodney W. Grimes }; 2126d2d3e875SBruce Evans #endif 2127fb99ab88SMatthew Dillon /* 2128fb99ab88SMatthew Dillon * MPSAFE 2129fb99ab88SMatthew Dillon */ 2130df8bae1dSRodney W. Grimes /* ARGSUSED */ 213126f9a767SRodney W. Grimes int 2132b40ce416SJulian Elischer nosys(td, args) 2133b40ce416SJulian Elischer struct thread *td; 2134df8bae1dSRodney W. Grimes struct nosys_args *args; 2135df8bae1dSRodney W. Grimes { 2136b40ce416SJulian Elischer struct proc *p = td->td_proc; 2137b40ce416SJulian Elischer 2138fb99ab88SMatthew Dillon mtx_lock(&Giant); 2139628d2653SJohn Baldwin PROC_LOCK(p); 2140df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2141628d2653SJohn Baldwin PROC_UNLOCK(p); 2142fb99ab88SMatthew Dillon mtx_unlock(&Giant); 2143f5216b9aSBruce Evans return (ENOSYS); 2144df8bae1dSRodney W. Grimes } 2145831d27a9SDon Lewis 2146831d27a9SDon Lewis /* 214748f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2148831d27a9SDon Lewis * stored credentials rather than those of the current process. 2149831d27a9SDon Lewis */ 2150831d27a9SDon Lewis void 2151f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2152f1320723SAlfred Perlstein struct sigio **sigiop; 21532c42a146SMarcel Moolenaar int sig, checkctty; 2154831d27a9SDon Lewis { 2155f1320723SAlfred Perlstein struct sigio *sigio; 2156831d27a9SDon Lewis 2157f1320723SAlfred Perlstein SIGIO_LOCK(); 2158f1320723SAlfred Perlstein sigio = *sigiop; 2159f1320723SAlfred Perlstein if (sigio == NULL) { 2160f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2161f1320723SAlfred Perlstein return; 2162f1320723SAlfred Perlstein } 2163831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2164628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 21652b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 21662c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2167628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2168831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2169831d27a9SDon Lewis struct proc *p; 2170831d27a9SDon Lewis 2171f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2172628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2173628d2653SJohn Baldwin PROC_LOCK(p); 21742b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2175831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 21762c42a146SMarcel Moolenaar psignal(p, sig); 2177628d2653SJohn Baldwin PROC_UNLOCK(p); 2178628d2653SJohn Baldwin } 2179f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2180831d27a9SDon Lewis } 2181f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2182831d27a9SDon Lewis } 2183cb679c38SJonathan Lemon 2184cb679c38SJonathan Lemon static int 2185cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2186cb679c38SJonathan Lemon { 2187cb679c38SJonathan Lemon struct proc *p = curproc; 2188cb679c38SJonathan Lemon 2189cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2190cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2191cb679c38SJonathan Lemon 2192628d2653SJohn Baldwin PROC_LOCK(p); 2193cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2194628d2653SJohn Baldwin PROC_UNLOCK(p); 2195cb679c38SJonathan Lemon 2196cb679c38SJonathan Lemon return (0); 2197cb679c38SJonathan Lemon } 2198cb679c38SJonathan Lemon 2199cb679c38SJonathan Lemon static void 2200cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2201cb679c38SJonathan Lemon { 2202cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2203cb679c38SJonathan Lemon 2204628d2653SJohn Baldwin PROC_LOCK(p); 2205e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2206628d2653SJohn Baldwin PROC_UNLOCK(p); 2207cb679c38SJonathan Lemon } 2208cb679c38SJonathan Lemon 2209cb679c38SJonathan Lemon /* 2210cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2211cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2212cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2213cb679c38SJonathan Lemon * isn't worth the trouble. 2214cb679c38SJonathan Lemon */ 2215cb679c38SJonathan Lemon static int 2216cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2217cb679c38SJonathan Lemon { 2218cb679c38SJonathan Lemon 2219cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2220cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2221cb679c38SJonathan Lemon 2222cb679c38SJonathan Lemon if (kn->kn_id == hint) 2223cb679c38SJonathan Lemon kn->kn_data++; 2224cb679c38SJonathan Lemon } 2225cb679c38SJonathan Lemon return (kn->kn_data != 0); 2226cb679c38SJonathan Lemon } 2227