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 7623eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43) 7723eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)" 7823eeeff7SPeter Wemm #endif 7923eeeff7SPeter Wemm 806f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 816f841fb7SMarcel Moolenaar 824d77a549SAlfred Perlstein static int coredump(struct thread *); 834d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t); 849c1ab3e0SJohn Baldwin static int killpg1(struct thread *td, int sig, int pgid, int all); 854d77a549SAlfred Perlstein static int sigprop(int sig); 864d77a549SAlfred Perlstein static void stop(struct proc *); 874093529dSJeff Roberson static void tdsigwakeup(struct thread *td, int sig, sig_t action); 88cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 89cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 90cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 914093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop); 92cb679c38SJonathan Lemon 93cb679c38SJonathan Lemon struct filterops sig_filtops = 94cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 95cb679c38SJonathan Lemon 9657308494SJoerg Wunsch static int kern_logsigexit = 1; 973d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 983d177f46SBill Fumerola &kern_logsigexit, 0, 993d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 10057308494SJoerg Wunsch 1012b87b6d4SRobert Watson /* 1022b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1032b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1042b87b6d4SRobert Watson * in the right situations. 1052b87b6d4SRobert Watson */ 1062b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1072b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1082b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1092b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1102b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1112b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1122b87b6d4SRobert Watson 11322d4b0fbSJohn Polstra int sugid_coredump; 1143d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1153d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 116c87e2930SDavid Greenman 117e5a28db9SPaul Saab static int do_coredump = 1; 118e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 119e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 120e5a28db9SPaul Saab 1212c42a146SMarcel Moolenaar /* 1222c42a146SMarcel Moolenaar * Signal properties and actions. 1232c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1242c42a146SMarcel Moolenaar * according to the following properties: 1252c42a146SMarcel Moolenaar */ 1262c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1272c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1282c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1292c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1302c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1312c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1322c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 133da33176fSJeff Roberson #define SA_PROC 0x80 /* deliverable to any thread */ 134df8bae1dSRodney W. Grimes 1352c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 136da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGHUP */ 137da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGINT */ 138da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */ 1392c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1402c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1412c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 142da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 144da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGKILL */ 1452c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1462c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1472c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 148da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPIPE */ 149da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGALRM */ 150da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGTERM */ 151da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGURG */ 152da33176fSJeff Roberson SA_STOP|SA_PROC, /* SIGSTOP */ 153da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */ 154da33176fSJeff Roberson SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */ 155da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGCHLD */ 156da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */ 157da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */ 158da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGIO */ 1592c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1602c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 161da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGVTALRM */ 162da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPROF */ 163da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGWINCH */ 164da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGINFO */ 165da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR1 */ 166da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR2 */ 1672c42a146SMarcel Moolenaar }; 1682c42a146SMarcel Moolenaar 169fbbeeb6cSBruce Evans /* 170fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 171fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 172fbbeeb6cSBruce Evans * action, the process stops in issignal(). 173e602ba25SJulian Elischer * XXXKSE the check for a pending stop is not done under KSE 174fbbeeb6cSBruce Evans * 17533510ef1SBruce Evans * MP SAFE. 176fbbeeb6cSBruce Evans */ 177fbbeeb6cSBruce Evans int 178e602ba25SJulian Elischer cursig(struct thread *td) 179fbbeeb6cSBruce Evans { 180e602ba25SJulian Elischer struct proc *p = td->td_proc; 181fbbeeb6cSBruce Evans 1822ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 183179235b3SBruce Evans mtx_assert(&sched_lock, MA_NOTOWNED); 1844093529dSJeff Roberson return (SIGPENDING(td) ? issignal(td) : 0); 185fbbeeb6cSBruce Evans } 186fbbeeb6cSBruce Evans 18779065dbaSBruce Evans /* 18879065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 1894093529dSJeff Roberson * mode. This must be called whenever a signal is added to td_siglist or 1904093529dSJeff Roberson * unmasked in td_sigmask. 19179065dbaSBruce Evans */ 19279065dbaSBruce Evans void 1934093529dSJeff Roberson signotify(struct thread *td) 19479065dbaSBruce Evans { 1954093529dSJeff Roberson struct proc *p; 1964093529dSJeff Roberson sigset_t set; 1974093529dSJeff Roberson 1984093529dSJeff Roberson p = td->td_proc; 19979065dbaSBruce Evans 20079065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 2014093529dSJeff Roberson 2024093529dSJeff Roberson /* 2034093529dSJeff Roberson * If our mask changed we may have to move signal that were 2044093529dSJeff Roberson * previously masked by all threads to our siglist. 2054093529dSJeff Roberson */ 2064093529dSJeff Roberson set = p->p_siglist; 2074093529dSJeff Roberson SIGSETNAND(set, td->td_sigmask); 2084093529dSJeff Roberson SIGSETNAND(p->p_siglist, set); 2094093529dSJeff Roberson SIGSETOR(td->td_siglist, set); 2104093529dSJeff Roberson 21179065dbaSBruce Evans mtx_lock_spin(&sched_lock); 2124093529dSJeff Roberson if (SIGPENDING(td)) 2134093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 21479065dbaSBruce Evans mtx_unlock_spin(&sched_lock); 21579065dbaSBruce Evans } 21679065dbaSBruce Evans 2176f841fb7SMarcel Moolenaar static __inline int 2186f841fb7SMarcel Moolenaar sigprop(int sig) 2192c42a146SMarcel Moolenaar { 2206f841fb7SMarcel Moolenaar 2212c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 2222c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 2232c42a146SMarcel Moolenaar return (0); 224df8bae1dSRodney W. Grimes } 2252c42a146SMarcel Moolenaar 2264093529dSJeff Roberson int 2276f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2282c42a146SMarcel Moolenaar { 2292c42a146SMarcel Moolenaar int i; 2302c42a146SMarcel Moolenaar 2316f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2322c42a146SMarcel Moolenaar if (set->__bits[i]) 2332c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 234df8bae1dSRodney W. Grimes return (0); 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes 2372c42a146SMarcel Moolenaar /* 2388f19eb88SIan Dowse * kern_sigaction 2392c42a146SMarcel Moolenaar * sigaction 24023eeeff7SPeter Wemm * freebsd4_sigaction 2412c42a146SMarcel Moolenaar * osigaction 2422c42a146SMarcel Moolenaar */ 2438f19eb88SIan Dowse int 24423eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags) 2458f19eb88SIan Dowse struct thread *td; 2462c42a146SMarcel Moolenaar register int sig; 2472c42a146SMarcel Moolenaar struct sigaction *act, *oact; 24823eeeff7SPeter Wemm int flags; 249df8bae1dSRodney W. Grimes { 250628d2653SJohn Baldwin register struct sigacts *ps; 2514093529dSJeff Roberson struct thread *td0; 2528f19eb88SIan Dowse struct proc *p = td->td_proc; 253df8bae1dSRodney W. Grimes 2542899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 2552c42a146SMarcel Moolenaar return (EINVAL); 2562c42a146SMarcel Moolenaar 257628d2653SJohn Baldwin PROC_LOCK(p); 258628d2653SJohn Baldwin ps = p->p_sigacts; 2592c42a146SMarcel Moolenaar if (oact) { 2602c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2612c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2622c42a146SMarcel Moolenaar oact->sa_flags = 0; 2632c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2642c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2652c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2662c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2672c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2682c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2692c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2702c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2712c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2722c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 273645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2742c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 275645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2762c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2772c42a146SMarcel Moolenaar } 2782c42a146SMarcel Moolenaar if (act) { 2792c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 280628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 281628d2653SJohn Baldwin PROC_UNLOCK(p); 2822c42a146SMarcel Moolenaar return (EINVAL); 283628d2653SJohn Baldwin } 2842c42a146SMarcel Moolenaar 285df8bae1dSRodney W. Grimes /* 286df8bae1dSRodney W. Grimes * Change setting atomically. 287df8bae1dSRodney W. Grimes */ 2882c42a146SMarcel Moolenaar 2892c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2902c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2912c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 292aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 293aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 29480f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 29580f42b55SIan Dowse } else { 29680f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2972c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2982c42a146SMarcel Moolenaar } 2992c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 3002c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 301df8bae1dSRodney W. Grimes else 3022c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 3032c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 3042c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 305df8bae1dSRodney W. Grimes else 3062c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 3072c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 3082c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 3091e41c1b5SSteven Wallace else 3102c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 3112c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 3122c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 313289ccde0SPeter Wemm else 3142c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 315df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 3162c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 3172c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 318df8bae1dSRodney W. Grimes else 3198c3d74f4SBruce Evans SIGDELSET(ps->ps_usertramp, sig); 320df8bae1dSRodney W. Grimes #endif 3212c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3222c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 323645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 3246626c604SJulian Elischer else 325645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 326ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 327245f17d4SJoerg Wunsch /* 3282c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3292c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3302c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3312c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 332245f17d4SJoerg Wunsch */ 333245f17d4SJoerg Wunsch if (p->p_pid == 1) 334645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3356626c604SJulian Elischer else 336645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 337245f17d4SJoerg Wunsch } else 338645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 339ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 340ba1551caSIan Dowse p->p_procsig->ps_flag |= PS_CLDSIGIGN; 341ba1551caSIan Dowse else 342ba1551caSIan Dowse p->p_procsig->ps_flag &= ~PS_CLDSIGIGN; 343df8bae1dSRodney W. Grimes } 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3462c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3472c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3482c42a146SMarcel Moolenaar * have to restart the process. 349df8bae1dSRodney W. Grimes */ 3502c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3512c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3522c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3532c42a146SMarcel Moolenaar /* never to be seen again */ 3541d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 3554093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 3564093529dSJeff Roberson SIGDELSET(td0->td_siglist, sig); 3572c42a146SMarcel Moolenaar if (sig != SIGCONT) 3582c42a146SMarcel Moolenaar /* easier in psignal */ 3592c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3602c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 361645682fdSLuoqi Chen } else { 3622c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3632c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3642c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3652c42a146SMarcel Moolenaar else 3662c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3672c42a146SMarcel Moolenaar } 36823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 36923eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 37023eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 37123eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 37223eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 37323eeeff7SPeter Wemm else 37423eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 37523eeeff7SPeter Wemm #endif 376e8ebc08fSPeter Wemm #ifdef COMPAT_43 377645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 37823eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 37923eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 380645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 381645682fdSLuoqi Chen else 382645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 383e8ebc08fSPeter Wemm #endif 384df8bae1dSRodney W. Grimes } 385628d2653SJohn Baldwin PROC_UNLOCK(p); 3862c42a146SMarcel Moolenaar return (0); 3872c42a146SMarcel Moolenaar } 3882c42a146SMarcel Moolenaar 3892c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3902c42a146SMarcel Moolenaar struct sigaction_args { 3912c42a146SMarcel Moolenaar int sig; 3922c42a146SMarcel Moolenaar struct sigaction *act; 3932c42a146SMarcel Moolenaar struct sigaction *oact; 3942c42a146SMarcel Moolenaar }; 3952c42a146SMarcel Moolenaar #endif 396fb99ab88SMatthew Dillon /* 397fb99ab88SMatthew Dillon * MPSAFE 398fb99ab88SMatthew Dillon */ 3992c42a146SMarcel Moolenaar int 400b40ce416SJulian Elischer sigaction(td, uap) 401b40ce416SJulian Elischer struct thread *td; 4022c42a146SMarcel Moolenaar register struct sigaction_args *uap; 4032c42a146SMarcel Moolenaar { 4042c42a146SMarcel Moolenaar struct sigaction act, oact; 4052c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 4062c42a146SMarcel Moolenaar int error; 4072c42a146SMarcel Moolenaar 4086f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 4096f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 4102c42a146SMarcel Moolenaar if (actp) { 4116f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 4122c42a146SMarcel Moolenaar if (error) 41344443757STim J. Robbins return (error); 4142c42a146SMarcel Moolenaar } 41544443757STim J. Robbins mtx_lock(&Giant); 4168f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 41744443757STim J. Robbins mtx_unlock(&Giant); 4182c42a146SMarcel Moolenaar if (oactp && !error) { 4196f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 4202c42a146SMarcel Moolenaar } 4212c42a146SMarcel Moolenaar return (error); 4222c42a146SMarcel Moolenaar } 4232c42a146SMarcel Moolenaar 42423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 42523eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 42623eeeff7SPeter Wemm struct freebsd4_sigaction_args { 42723eeeff7SPeter Wemm int sig; 42823eeeff7SPeter Wemm struct sigaction *act; 42923eeeff7SPeter Wemm struct sigaction *oact; 43023eeeff7SPeter Wemm }; 43123eeeff7SPeter Wemm #endif 43223eeeff7SPeter Wemm /* 43323eeeff7SPeter Wemm * MPSAFE 43423eeeff7SPeter Wemm */ 43523eeeff7SPeter Wemm int 43623eeeff7SPeter Wemm freebsd4_sigaction(td, uap) 43723eeeff7SPeter Wemm struct thread *td; 43823eeeff7SPeter Wemm register struct freebsd4_sigaction_args *uap; 43923eeeff7SPeter Wemm { 44023eeeff7SPeter Wemm struct sigaction act, oact; 44123eeeff7SPeter Wemm register struct sigaction *actp, *oactp; 44223eeeff7SPeter Wemm int error; 44323eeeff7SPeter Wemm 44423eeeff7SPeter Wemm 44523eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 44623eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 44723eeeff7SPeter Wemm if (actp) { 44823eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 44923eeeff7SPeter Wemm if (error) 45044443757STim J. Robbins return (error); 45123eeeff7SPeter Wemm } 45244443757STim J. Robbins mtx_lock(&Giant); 45323eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 45444443757STim J. Robbins mtx_unlock(&Giant); 45523eeeff7SPeter Wemm if (oactp && !error) { 45623eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 45723eeeff7SPeter Wemm } 45823eeeff7SPeter Wemm return (error); 45923eeeff7SPeter Wemm } 46023eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 46123eeeff7SPeter Wemm 46231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 4632c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4642c42a146SMarcel Moolenaar struct osigaction_args { 4652c42a146SMarcel Moolenaar int signum; 4662c42a146SMarcel Moolenaar struct osigaction *nsa; 4672c42a146SMarcel Moolenaar struct osigaction *osa; 4682c42a146SMarcel Moolenaar }; 4692c42a146SMarcel Moolenaar #endif 470fb99ab88SMatthew Dillon /* 471fb99ab88SMatthew Dillon * MPSAFE 472fb99ab88SMatthew Dillon */ 4732c42a146SMarcel Moolenaar int 474b40ce416SJulian Elischer osigaction(td, uap) 475b40ce416SJulian Elischer struct thread *td; 4762c42a146SMarcel Moolenaar register struct osigaction_args *uap; 4772c42a146SMarcel Moolenaar { 4782c42a146SMarcel Moolenaar struct osigaction sa; 4792c42a146SMarcel Moolenaar struct sigaction nsa, osa; 4802c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 4812c42a146SMarcel Moolenaar int error; 4822c42a146SMarcel Moolenaar 4836f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4846f841fb7SMarcel Moolenaar return (EINVAL); 485fb99ab88SMatthew Dillon 4866f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4876f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 488fb99ab88SMatthew Dillon 4892c42a146SMarcel Moolenaar if (nsap) { 4906f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4912c42a146SMarcel Moolenaar if (error) 49244443757STim J. Robbins return (error); 4932c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4942c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4952c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4962c42a146SMarcel Moolenaar } 49744443757STim J. Robbins mtx_lock(&Giant); 49823eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 49944443757STim J. Robbins mtx_unlock(&Giant); 5002c42a146SMarcel Moolenaar if (osap && !error) { 5012c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 5022c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 5032c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 5046f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 5052c42a146SMarcel Moolenaar } 5062c42a146SMarcel Moolenaar return (error); 5072c42a146SMarcel Moolenaar } 50823eeeff7SPeter Wemm 50923eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__) 51023eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 51123eeeff7SPeter Wemm int 51223eeeff7SPeter Wemm osigreturn(td, uap) 51323eeeff7SPeter Wemm struct thread *td; 51423eeeff7SPeter Wemm struct osigreturn_args *uap; 51523eeeff7SPeter Wemm { 51623eeeff7SPeter Wemm 51723eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 51823eeeff7SPeter Wemm } 51923eeeff7SPeter Wemm #endif 52031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 521df8bae1dSRodney W. Grimes 522df8bae1dSRodney W. Grimes /* 523df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 524df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 525df8bae1dSRodney W. Grimes */ 526df8bae1dSRodney W. Grimes void 527df8bae1dSRodney W. Grimes siginit(p) 528df8bae1dSRodney W. Grimes struct proc *p; 529df8bae1dSRodney W. Grimes { 530df8bae1dSRodney W. Grimes register int i; 531df8bae1dSRodney W. Grimes 532628d2653SJohn Baldwin PROC_LOCK(p); 5332c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 5342c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 5352c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 536628d2653SJohn Baldwin PROC_UNLOCK(p); 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 541df8bae1dSRodney W. Grimes */ 542df8bae1dSRodney W. Grimes void 543df8bae1dSRodney W. Grimes execsigs(p) 544df8bae1dSRodney W. Grimes register struct proc *p; 545df8bae1dSRodney W. Grimes { 546628d2653SJohn Baldwin register struct sigacts *ps; 5472c42a146SMarcel Moolenaar register int sig; 548df8bae1dSRodney W. Grimes 549df8bae1dSRodney W. Grimes /* 550df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 5514093529dSJeff Roberson * through td_sigmask (unless they were caught, 552df8bae1dSRodney W. Grimes * and are now ignored by default). 553df8bae1dSRodney W. Grimes */ 5549b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 555628d2653SJohn Baldwin ps = p->p_sigacts; 5562c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 5572c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 5582c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 5592c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 5602c42a146SMarcel Moolenaar if (sig != SIGCONT) 5612c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 5621d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 5634093529dSJeff Roberson /* 5644093529dSJeff Roberson * There is only one thread at this point. 5654093529dSJeff Roberson */ 5664093529dSJeff Roberson SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig); 567df8bae1dSRodney W. Grimes } 5682c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 569df8bae1dSRodney W. Grimes } 570df8bae1dSRodney W. Grimes /* 5714093529dSJeff Roberson * Clear out the td's sigmask. Normal processes use the proc sigmask. 5724093529dSJeff Roberson */ 5734093529dSJeff Roberson SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask); 5744093529dSJeff Roberson /* 575df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 576df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 577df8bae1dSRodney W. Grimes */ 578645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 579645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 580645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 5813b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 58280e907a1SPeter Wemm /* 58380e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 58480e907a1SPeter Wemm */ 585ba1551caSIan Dowse p->p_procsig->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 586c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 587c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes /* 591628d2653SJohn Baldwin * do_sigprocmask() 5927c8fdcbdSMatthew Dillon * 593628d2653SJohn Baldwin * Manipulate signal mask. 594df8bae1dSRodney W. Grimes */ 5954093529dSJeff Roberson int 5964093529dSJeff Roberson do_sigprocmask(td, how, set, oset, old) 5974093529dSJeff Roberson struct thread *td; 5982c42a146SMarcel Moolenaar int how; 5992c42a146SMarcel Moolenaar sigset_t *set, *oset; 600645682fdSLuoqi Chen int old; 6012c42a146SMarcel Moolenaar { 6022c42a146SMarcel Moolenaar int error; 6032c42a146SMarcel Moolenaar 6044093529dSJeff Roberson PROC_LOCK(td->td_proc); 6052c42a146SMarcel Moolenaar if (oset != NULL) 6064093529dSJeff Roberson *oset = td->td_sigmask; 6072c42a146SMarcel Moolenaar 6082c42a146SMarcel Moolenaar error = 0; 6092c42a146SMarcel Moolenaar if (set != NULL) { 6102c42a146SMarcel Moolenaar switch (how) { 6112c42a146SMarcel Moolenaar case SIG_BLOCK: 612645682fdSLuoqi Chen SIG_CANTMASK(*set); 6134093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 6142c42a146SMarcel Moolenaar break; 6152c42a146SMarcel Moolenaar case SIG_UNBLOCK: 6164093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 6174093529dSJeff Roberson signotify(td); 6182c42a146SMarcel Moolenaar break; 6192c42a146SMarcel Moolenaar case SIG_SETMASK: 620645682fdSLuoqi Chen SIG_CANTMASK(*set); 621645682fdSLuoqi Chen if (old) 6224093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 623645682fdSLuoqi Chen else 6244093529dSJeff Roberson td->td_sigmask = *set; 6254093529dSJeff Roberson signotify(td); 6262c42a146SMarcel Moolenaar break; 6272c42a146SMarcel Moolenaar default: 6282c42a146SMarcel Moolenaar error = EINVAL; 6292c42a146SMarcel Moolenaar break; 6302c42a146SMarcel Moolenaar } 6312c42a146SMarcel Moolenaar } 6324093529dSJeff Roberson PROC_UNLOCK(td->td_proc); 6332c42a146SMarcel Moolenaar return (error); 6342c42a146SMarcel Moolenaar } 6352c42a146SMarcel Moolenaar 6367c8fdcbdSMatthew Dillon /* 637b40ce416SJulian Elischer * sigprocmask() - MP SAFE (XXXKSE not under KSE it isn't) 6387c8fdcbdSMatthew Dillon */ 6397c8fdcbdSMatthew Dillon 640d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 641df8bae1dSRodney W. Grimes struct sigprocmask_args { 642df8bae1dSRodney W. Grimes int how; 6432c42a146SMarcel Moolenaar const sigset_t *set; 6442c42a146SMarcel Moolenaar sigset_t *oset; 645df8bae1dSRodney W. Grimes }; 646d2d3e875SBruce Evans #endif 64726f9a767SRodney W. Grimes int 648b40ce416SJulian Elischer sigprocmask(td, uap) 649b40ce416SJulian Elischer register struct thread *td; 650df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 651df8bae1dSRodney W. Grimes { 6522c42a146SMarcel Moolenaar sigset_t set, oset; 6532c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 6542c42a146SMarcel Moolenaar int error; 655df8bae1dSRodney W. Grimes 6566f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 6576f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 6582c42a146SMarcel Moolenaar if (setp) { 6596f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 6602c42a146SMarcel Moolenaar if (error) 6612c42a146SMarcel Moolenaar return (error); 662df8bae1dSRodney W. Grimes } 6634093529dSJeff Roberson error = do_sigprocmask(td, uap->how, setp, osetp, 0); 6642c42a146SMarcel Moolenaar if (osetp && !error) { 6656f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 6662c42a146SMarcel Moolenaar } 6672c42a146SMarcel Moolenaar return (error); 6682c42a146SMarcel Moolenaar } 6692c42a146SMarcel Moolenaar 67031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6717c8fdcbdSMatthew Dillon /* 6727c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 6737c8fdcbdSMatthew Dillon */ 6742c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6752c42a146SMarcel Moolenaar struct osigprocmask_args { 6762c42a146SMarcel Moolenaar int how; 6772c42a146SMarcel Moolenaar osigset_t mask; 6782c42a146SMarcel Moolenaar }; 6792c42a146SMarcel Moolenaar #endif 6802c42a146SMarcel Moolenaar int 681b40ce416SJulian Elischer osigprocmask(td, uap) 682b40ce416SJulian Elischer register struct thread *td; 6832c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 6842c42a146SMarcel Moolenaar { 6852c42a146SMarcel Moolenaar sigset_t set, oset; 6862c42a146SMarcel Moolenaar int error; 6872c42a146SMarcel Moolenaar 6882c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 6894093529dSJeff Roberson error = do_sigprocmask(td, uap->how, &set, &oset, 1); 690b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 691df8bae1dSRodney W. Grimes return (error); 692df8bae1dSRodney W. Grimes } 69331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 694df8bae1dSRodney W. Grimes 695d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 696df8bae1dSRodney W. Grimes struct sigpending_args { 6972c42a146SMarcel Moolenaar sigset_t *set; 698df8bae1dSRodney W. Grimes }; 699d2d3e875SBruce Evans #endif 700fb99ab88SMatthew Dillon /* 701fb99ab88SMatthew Dillon * MPSAFE 702fb99ab88SMatthew Dillon */ 70326f9a767SRodney W. Grimes int 704b40ce416SJulian Elischer sigpending(td, uap) 705b40ce416SJulian Elischer struct thread *td; 706df8bae1dSRodney W. Grimes struct sigpending_args *uap; 707df8bae1dSRodney W. Grimes { 708b40ce416SJulian Elischer struct proc *p = td->td_proc; 709628d2653SJohn Baldwin sigset_t siglist; 710df8bae1dSRodney W. Grimes 711628d2653SJohn Baldwin PROC_LOCK(p); 7121d9c5696SJuli Mallett siglist = p->p_siglist; 7134093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 714628d2653SJohn Baldwin PROC_UNLOCK(p); 71548e8f774STim J. Robbins return (copyout(&siglist, uap->set, sizeof(sigset_t))); 7162c42a146SMarcel Moolenaar } 7172c42a146SMarcel Moolenaar 71831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 7192c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7202c42a146SMarcel Moolenaar struct osigpending_args { 7212c42a146SMarcel Moolenaar int dummy; 7222c42a146SMarcel Moolenaar }; 7232c42a146SMarcel Moolenaar #endif 724fb99ab88SMatthew Dillon /* 725fb99ab88SMatthew Dillon * MPSAFE 726fb99ab88SMatthew Dillon */ 7272c42a146SMarcel Moolenaar int 728b40ce416SJulian Elischer osigpending(td, uap) 729b40ce416SJulian Elischer struct thread *td; 7302c42a146SMarcel Moolenaar struct osigpending_args *uap; 7312c42a146SMarcel Moolenaar { 732b40ce416SJulian Elischer struct proc *p = td->td_proc; 7334093529dSJeff Roberson sigset_t siglist; 734b40ce416SJulian Elischer 735628d2653SJohn Baldwin PROC_LOCK(p); 7364093529dSJeff Roberson siglist = p->p_siglist; 7374093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 7384093529dSJeff Roberson SIG2OSIG(siglist, td->td_retval[0]); 739628d2653SJohn Baldwin PROC_UNLOCK(p); 740df8bae1dSRodney W. Grimes return (0); 741df8bae1dSRodney W. Grimes } 74231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 743df8bae1dSRodney W. Grimes 744df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 745df8bae1dSRodney W. Grimes /* 746df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 747df8bae1dSRodney W. Grimes */ 748d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 749df8bae1dSRodney W. Grimes struct osigvec_args { 750df8bae1dSRodney W. Grimes int signum; 751df8bae1dSRodney W. Grimes struct sigvec *nsv; 752df8bae1dSRodney W. Grimes struct sigvec *osv; 753df8bae1dSRodney W. Grimes }; 754d2d3e875SBruce Evans #endif 755fb99ab88SMatthew Dillon /* 756fb99ab88SMatthew Dillon * MPSAFE 757fb99ab88SMatthew Dillon */ 758df8bae1dSRodney W. Grimes /* ARGSUSED */ 75926f9a767SRodney W. Grimes int 760b40ce416SJulian Elischer osigvec(td, uap) 761b40ce416SJulian Elischer struct thread *td; 762df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 763df8bae1dSRodney W. Grimes { 764df8bae1dSRodney W. Grimes struct sigvec vec; 7652c42a146SMarcel Moolenaar struct sigaction nsa, osa; 7662c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 7672c42a146SMarcel Moolenaar int error; 768df8bae1dSRodney W. Grimes 7696f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 7706f841fb7SMarcel Moolenaar return (EINVAL); 7716f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 7726f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 7732c42a146SMarcel Moolenaar if (nsap) { 7746f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 7752c42a146SMarcel Moolenaar if (error) 776df8bae1dSRodney W. Grimes return (error); 7772c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 7782c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 7792c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 7802c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 781df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 7822c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 783df8bae1dSRodney W. Grimes #endif 784df8bae1dSRodney W. Grimes } 785fb99ab88SMatthew Dillon mtx_lock(&Giant); 7868f19eb88SIan Dowse error = kern_sigaction(td, uap->signum, nsap, osap, 1); 787fb99ab88SMatthew Dillon mtx_unlock(&Giant); 7882c42a146SMarcel Moolenaar if (osap && !error) { 7892c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 7902c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 7912c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 7922c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 7932c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 7942c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 7952c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 7962c42a146SMarcel Moolenaar #endif 7976f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 7982c42a146SMarcel Moolenaar } 7992c42a146SMarcel Moolenaar return (error); 800df8bae1dSRodney W. Grimes } 801df8bae1dSRodney W. Grimes 802d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 803df8bae1dSRodney W. Grimes struct osigblock_args { 804df8bae1dSRodney W. Grimes int mask; 805df8bae1dSRodney W. Grimes }; 806d2d3e875SBruce Evans #endif 807fb99ab88SMatthew Dillon /* 808fb99ab88SMatthew Dillon * MPSAFE 809fb99ab88SMatthew Dillon */ 81026f9a767SRodney W. Grimes int 811b40ce416SJulian Elischer osigblock(td, uap) 812b40ce416SJulian Elischer register struct thread *td; 813df8bae1dSRodney W. Grimes struct osigblock_args *uap; 814df8bae1dSRodney W. Grimes { 815b40ce416SJulian Elischer struct proc *p = td->td_proc; 8162c42a146SMarcel Moolenaar sigset_t set; 817df8bae1dSRodney W. Grimes 8182c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 8192c42a146SMarcel Moolenaar SIG_CANTMASK(set); 820fb99ab88SMatthew Dillon mtx_lock(&Giant); 821628d2653SJohn Baldwin PROC_LOCK(p); 8224093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 8234093529dSJeff Roberson SIGSETOR(td->td_sigmask, set); 824628d2653SJohn Baldwin PROC_UNLOCK(p); 825fb99ab88SMatthew Dillon mtx_unlock(&Giant); 826df8bae1dSRodney W. Grimes return (0); 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes 829d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 830df8bae1dSRodney W. Grimes struct osigsetmask_args { 831df8bae1dSRodney W. Grimes int mask; 832df8bae1dSRodney W. Grimes }; 833d2d3e875SBruce Evans #endif 834fb99ab88SMatthew Dillon /* 835fb99ab88SMatthew Dillon * MPSAFE 836fb99ab88SMatthew Dillon */ 83726f9a767SRodney W. Grimes int 838b40ce416SJulian Elischer osigsetmask(td, uap) 839b40ce416SJulian Elischer struct thread *td; 840df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 841df8bae1dSRodney W. Grimes { 842b40ce416SJulian Elischer struct proc *p = td->td_proc; 8432c42a146SMarcel Moolenaar sigset_t set; 844df8bae1dSRodney W. Grimes 8452c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 8462c42a146SMarcel Moolenaar SIG_CANTMASK(set); 847fb99ab88SMatthew Dillon mtx_lock(&Giant); 848628d2653SJohn Baldwin PROC_LOCK(p); 8494093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 8504093529dSJeff Roberson SIGSETLO(td->td_sigmask, set); 8514093529dSJeff Roberson signotify(td); 852628d2653SJohn Baldwin PROC_UNLOCK(p); 853fb99ab88SMatthew Dillon mtx_unlock(&Giant); 854df8bae1dSRodney W. Grimes return (0); 855df8bae1dSRodney W. Grimes } 856df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 857df8bae1dSRodney W. Grimes 858df8bae1dSRodney W. Grimes /* 859df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 860df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 861df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 862b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 863b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 864b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 865df8bae1dSRodney W. Grimes */ 866d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 867df8bae1dSRodney W. Grimes struct sigsuspend_args { 8682c42a146SMarcel Moolenaar const sigset_t *sigmask; 869df8bae1dSRodney W. Grimes }; 870d2d3e875SBruce Evans #endif 871fb99ab88SMatthew Dillon /* 872fb99ab88SMatthew Dillon * MPSAFE 873fb99ab88SMatthew Dillon */ 874df8bae1dSRodney W. Grimes /* ARGSUSED */ 87526f9a767SRodney W. Grimes int 876b40ce416SJulian Elischer sigsuspend(td, uap) 877b40ce416SJulian Elischer struct thread *td; 878df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 879df8bae1dSRodney W. Grimes { 8802c42a146SMarcel Moolenaar sigset_t mask; 8812c42a146SMarcel Moolenaar int error; 8822c42a146SMarcel Moolenaar 8836f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 8842c42a146SMarcel Moolenaar if (error) 8852c42a146SMarcel Moolenaar return (error); 8868f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 8878f19eb88SIan Dowse } 8888f19eb88SIan Dowse 8898f19eb88SIan Dowse int 8908f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 8918f19eb88SIan Dowse { 8928f19eb88SIan Dowse struct proc *p = td->td_proc; 8938f19eb88SIan Dowse register struct sigacts *ps; 894df8bae1dSRodney W. Grimes 895df8bae1dSRodney W. Grimes /* 896645682fdSLuoqi Chen * When returning from sigsuspend, we want 897df8bae1dSRodney W. Grimes * the old mask to be restored after the 898df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 899df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 900df8bae1dSRodney W. Grimes * to indicate this. 901df8bae1dSRodney W. Grimes */ 902fb99ab88SMatthew Dillon mtx_lock(&Giant); 903628d2653SJohn Baldwin PROC_LOCK(p); 904628d2653SJohn Baldwin ps = p->p_sigacts; 9054093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 9064093529dSJeff Roberson mtx_lock_spin(&sched_lock); 9074093529dSJeff Roberson td->td_flags |= TDF_OLDMASK; 9084093529dSJeff Roberson mtx_unlock_spin(&sched_lock); 909645682fdSLuoqi Chen SIG_CANTMASK(mask); 9104093529dSJeff Roberson td->td_sigmask = mask; 9114093529dSJeff Roberson signotify(td); 91201609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 9132c42a146SMarcel Moolenaar /* void */; 914628d2653SJohn Baldwin PROC_UNLOCK(p); 915fb99ab88SMatthew Dillon mtx_unlock(&Giant); 9162c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 9172c42a146SMarcel Moolenaar return (EINTR); 9182c42a146SMarcel Moolenaar } 9192c42a146SMarcel Moolenaar 92031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9212c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9222c42a146SMarcel Moolenaar struct osigsuspend_args { 9232c42a146SMarcel Moolenaar osigset_t mask; 9242c42a146SMarcel Moolenaar }; 9252c42a146SMarcel Moolenaar #endif 926fb99ab88SMatthew Dillon /* 927fb99ab88SMatthew Dillon * MPSAFE 928fb99ab88SMatthew Dillon */ 9292c42a146SMarcel Moolenaar /* ARGSUSED */ 9302c42a146SMarcel Moolenaar int 931b40ce416SJulian Elischer osigsuspend(td, uap) 932b40ce416SJulian Elischer struct thread *td; 9332c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 9342c42a146SMarcel Moolenaar { 935b40ce416SJulian Elischer struct proc *p = td->td_proc; 936645682fdSLuoqi Chen sigset_t mask; 937628d2653SJohn Baldwin register struct sigacts *ps; 9382c42a146SMarcel Moolenaar 939fb99ab88SMatthew Dillon mtx_lock(&Giant); 940628d2653SJohn Baldwin PROC_LOCK(p); 941628d2653SJohn Baldwin ps = p->p_sigacts; 9424093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 9434093529dSJeff Roberson mtx_lock_spin(&sched_lock); 9444093529dSJeff Roberson td->td_flags |= TDF_OLDMASK; 9454093529dSJeff Roberson mtx_unlock_spin(&sched_lock); 946645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 947645682fdSLuoqi Chen SIG_CANTMASK(mask); 9484093529dSJeff Roberson SIGSETLO(td->td_sigmask, mask); 9494093529dSJeff Roberson signotify(td); 95001609114SAlfred Perlstein while (msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 951df8bae1dSRodney W. Grimes /* void */; 952628d2653SJohn Baldwin PROC_UNLOCK(p); 953fb99ab88SMatthew Dillon mtx_unlock(&Giant); 954df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 955df8bae1dSRodney W. Grimes return (EINTR); 956df8bae1dSRodney W. Grimes } 95731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 958df8bae1dSRodney W. Grimes 959df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 960d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 961df8bae1dSRodney W. Grimes struct osigstack_args { 962df8bae1dSRodney W. Grimes struct sigstack *nss; 963df8bae1dSRodney W. Grimes struct sigstack *oss; 964df8bae1dSRodney W. Grimes }; 965d2d3e875SBruce Evans #endif 966fb99ab88SMatthew Dillon /* 967fb99ab88SMatthew Dillon * MPSAFE 968fb99ab88SMatthew Dillon */ 969df8bae1dSRodney W. Grimes /* ARGSUSED */ 97026f9a767SRodney W. Grimes int 971b40ce416SJulian Elischer osigstack(td, uap) 972b40ce416SJulian Elischer struct thread *td; 973df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 974df8bae1dSRodney W. Grimes { 975b40ce416SJulian Elischer struct proc *p = td->td_proc; 976df8bae1dSRodney W. Grimes struct sigstack ss; 977fb99ab88SMatthew Dillon int error = 0; 978fb99ab88SMatthew Dillon 979fb99ab88SMatthew Dillon mtx_lock(&Giant); 980df8bae1dSRodney W. Grimes 981d034d459SMarcel Moolenaar if (uap->oss != NULL) { 982628d2653SJohn Baldwin PROC_LOCK(p); 983645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 984b40ce416SJulian Elischer ss.ss_onstack = sigonstack(cpu_getstack(td)); 985628d2653SJohn Baldwin PROC_UNLOCK(p); 986d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 987d034d459SMarcel Moolenaar if (error) 988fb99ab88SMatthew Dillon goto done2; 989d034d459SMarcel Moolenaar } 990d034d459SMarcel Moolenaar 991d034d459SMarcel Moolenaar if (uap->nss != NULL) { 992d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 993fb99ab88SMatthew Dillon goto done2; 994628d2653SJohn Baldwin PROC_LOCK(p); 995645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 996645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 997645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 998645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 999628d2653SJohn Baldwin PROC_UNLOCK(p); 1000df8bae1dSRodney W. Grimes } 1001fb99ab88SMatthew Dillon done2: 1002fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1003fb99ab88SMatthew Dillon return (error); 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1006df8bae1dSRodney W. Grimes 1007d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1008df8bae1dSRodney W. Grimes struct sigaltstack_args { 10092c42a146SMarcel Moolenaar stack_t *ss; 10102c42a146SMarcel Moolenaar stack_t *oss; 1011df8bae1dSRodney W. Grimes }; 1012d2d3e875SBruce Evans #endif 1013fb99ab88SMatthew Dillon /* 1014fb99ab88SMatthew Dillon * MPSAFE 1015fb99ab88SMatthew Dillon */ 1016df8bae1dSRodney W. Grimes /* ARGSUSED */ 101726f9a767SRodney W. Grimes int 1018b40ce416SJulian Elischer sigaltstack(td, uap) 1019b40ce416SJulian Elischer struct thread *td; 1020df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1021df8bae1dSRodney W. Grimes { 10228f19eb88SIan Dowse stack_t ss, oss; 10238f19eb88SIan Dowse int error; 10248f19eb88SIan Dowse 10258f19eb88SIan Dowse if (uap->ss != NULL) { 10268f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 10278f19eb88SIan Dowse if (error) 10288f19eb88SIan Dowse return (error); 10298f19eb88SIan Dowse } 10308f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 10318f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 10328f19eb88SIan Dowse if (error) 10338f19eb88SIan Dowse return (error); 10348f19eb88SIan Dowse if (uap->oss != NULL) 10358f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 10368f19eb88SIan Dowse return (error); 10378f19eb88SIan Dowse } 10388f19eb88SIan Dowse 10398f19eb88SIan Dowse int 10408f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 10418f19eb88SIan Dowse { 1042b40ce416SJulian Elischer struct proc *p = td->td_proc; 1043fb99ab88SMatthew Dillon int oonstack; 1044fb99ab88SMatthew Dillon int error = 0; 1045fb99ab88SMatthew Dillon 1046fb99ab88SMatthew Dillon mtx_lock(&Giant); 1047df8bae1dSRodney W. Grimes 1048b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1049d034d459SMarcel Moolenaar 10508f19eb88SIan Dowse if (oss != NULL) { 1051628d2653SJohn Baldwin PROC_LOCK(p); 10528f19eb88SIan Dowse *oss = p->p_sigstk; 10538f19eb88SIan Dowse oss->ss_flags = (p->p_flag & P_ALTSTACK) 1054d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1055628d2653SJohn Baldwin PROC_UNLOCK(p); 1056df8bae1dSRodney W. Grimes } 1057d034d459SMarcel Moolenaar 10588f19eb88SIan Dowse if (ss != NULL) { 1059fb99ab88SMatthew Dillon if (oonstack) { 1060fb99ab88SMatthew Dillon error = EPERM; 1061fb99ab88SMatthew Dillon goto done2; 1062fb99ab88SMatthew Dillon } 10638f19eb88SIan Dowse if ((ss->ss_flags & ~SS_DISABLE) != 0) { 1064fb99ab88SMatthew Dillon error = EINVAL; 1065fb99ab88SMatthew Dillon goto done2; 1066fb99ab88SMatthew Dillon } 10678f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 10688f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1069fb99ab88SMatthew Dillon error = ENOMEM; 1070fb99ab88SMatthew Dillon goto done2; 1071fb99ab88SMatthew Dillon } 1072628d2653SJohn Baldwin PROC_LOCK(p); 10738f19eb88SIan Dowse p->p_sigstk = *ss; 1074d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 1075628d2653SJohn Baldwin PROC_UNLOCK(p); 1076628d2653SJohn Baldwin } else { 1077628d2653SJohn Baldwin PROC_LOCK(p); 1078d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 1079628d2653SJohn Baldwin PROC_UNLOCK(p); 1080628d2653SJohn Baldwin } 1081d034d459SMarcel Moolenaar } 1082fb99ab88SMatthew Dillon done2: 1083fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1084fb99ab88SMatthew Dillon return (error); 1085df8bae1dSRodney W. Grimes } 1086df8bae1dSRodney W. Grimes 1087d93f860cSPoul-Henning Kamp /* 1088d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1089d93f860cSPoul-Henning Kamp * cp is calling process. 1090d93f860cSPoul-Henning Kamp */ 109137c84183SPoul-Henning Kamp static int 10929c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all) 10939c1ab3e0SJohn Baldwin register struct thread *td; 10942c42a146SMarcel Moolenaar int sig, pgid, all; 1095d93f860cSPoul-Henning Kamp { 1096d93f860cSPoul-Henning Kamp register struct proc *p; 1097d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1098d93f860cSPoul-Henning Kamp int nfound = 0; 1099d93f860cSPoul-Henning Kamp 1100553629ebSJake Burkholder if (all) { 1101d93f860cSPoul-Henning Kamp /* 1102d93f860cSPoul-Henning Kamp * broadcast 1103d93f860cSPoul-Henning Kamp */ 11041005a129SJohn Baldwin sx_slock(&allproc_lock); 11052e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1106628d2653SJohn Baldwin PROC_LOCK(p); 11079c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 11089c1ab3e0SJohn Baldwin p == td->td_proc) { 1109628d2653SJohn Baldwin PROC_UNLOCK(p); 1110628d2653SJohn Baldwin continue; 1111628d2653SJohn Baldwin } 1112f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1113d93f860cSPoul-Henning Kamp nfound++; 111433a9ed9dSJohn Baldwin if (sig) 11152c42a146SMarcel Moolenaar psignal(p, sig); 1116628d2653SJohn Baldwin } 111733a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1118d93f860cSPoul-Henning Kamp } 11191005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1120553629ebSJake Burkholder } else { 1121ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1122f591779bSSeigo Tanimura if (pgid == 0) { 1123d93f860cSPoul-Henning Kamp /* 1124d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1125d93f860cSPoul-Henning Kamp */ 11269c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1127f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1128f591779bSSeigo Tanimura } else { 1129d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1130f591779bSSeigo Tanimura if (pgrp == NULL) { 1131ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1132d93f860cSPoul-Henning Kamp return (ESRCH); 1133d93f860cSPoul-Henning Kamp } 1134f591779bSSeigo Tanimura } 1135ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 11362e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1137628d2653SJohn Baldwin PROC_LOCK(p); 1138628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1139628d2653SJohn Baldwin PROC_UNLOCK(p); 1140628d2653SJohn Baldwin continue; 1141628d2653SJohn Baldwin } 1142e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 114333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1144628d2653SJohn Baldwin continue; 1145628d2653SJohn Baldwin } 1146f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1147d93f860cSPoul-Henning Kamp nfound++; 114833a9ed9dSJohn Baldwin if (sig) 11492c42a146SMarcel Moolenaar psignal(p, sig); 1150628d2653SJohn Baldwin } 115133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1152d93f860cSPoul-Henning Kamp } 1153f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1154d93f860cSPoul-Henning Kamp } 1155d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1156d93f860cSPoul-Henning Kamp } 1157d93f860cSPoul-Henning Kamp 1158d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1159df8bae1dSRodney W. Grimes struct kill_args { 1160df8bae1dSRodney W. Grimes int pid; 1161df8bae1dSRodney W. Grimes int signum; 1162df8bae1dSRodney W. Grimes }; 1163d2d3e875SBruce Evans #endif 1164fb99ab88SMatthew Dillon /* 1165fb99ab88SMatthew Dillon * MPSAFE 1166fb99ab88SMatthew Dillon */ 1167df8bae1dSRodney W. Grimes /* ARGSUSED */ 116826f9a767SRodney W. Grimes int 1169b40ce416SJulian Elischer kill(td, uap) 1170b40ce416SJulian Elischer register struct thread *td; 1171df8bae1dSRodney W. Grimes register struct kill_args *uap; 1172df8bae1dSRodney W. Grimes { 1173df8bae1dSRodney W. Grimes register struct proc *p; 1174fb99ab88SMatthew Dillon int error = 0; 1175df8bae1dSRodney W. Grimes 11766c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1177df8bae1dSRodney W. Grimes return (EINVAL); 1178fb99ab88SMatthew Dillon 1179fb99ab88SMatthew Dillon mtx_lock(&Giant); 1180df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1181df8bae1dSRodney W. Grimes /* kill single process */ 1182fb99ab88SMatthew Dillon if ((p = pfind(uap->pid)) == NULL) { 1183fb99ab88SMatthew Dillon error = ESRCH; 1184f44d9e24SJohn Baldwin } else if ((error = p_cansignal(td, p, uap->signum)) != 0) { 118533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1186fb99ab88SMatthew Dillon } else { 118733a9ed9dSJohn Baldwin if (uap->signum) 1188df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1189628d2653SJohn Baldwin PROC_UNLOCK(p); 1190fb99ab88SMatthew Dillon error = 0; 1191df8bae1dSRodney W. Grimes } 1192fb99ab88SMatthew Dillon } else { 1193df8bae1dSRodney W. Grimes switch (uap->pid) { 1194df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 11959c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 1); 1196fb99ab88SMatthew Dillon break; 1197df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 11989c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, 0, 0); 1199fb99ab88SMatthew Dillon break; 1200df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 12019c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, -uap->pid, 0); 1202fb99ab88SMatthew Dillon break; 1203df8bae1dSRodney W. Grimes } 1204fb99ab88SMatthew Dillon } 1205fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1206fb99ab88SMatthew Dillon return(error); 1207df8bae1dSRodney W. Grimes } 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1210d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1211df8bae1dSRodney W. Grimes struct okillpg_args { 1212df8bae1dSRodney W. Grimes int pgid; 1213df8bae1dSRodney W. Grimes int signum; 1214df8bae1dSRodney W. Grimes }; 1215d2d3e875SBruce Evans #endif 1216fb99ab88SMatthew Dillon /* 1217fb99ab88SMatthew Dillon * MPSAFE 1218fb99ab88SMatthew Dillon */ 1219df8bae1dSRodney W. Grimes /* ARGSUSED */ 122026f9a767SRodney W. Grimes int 1221b40ce416SJulian Elischer okillpg(td, uap) 1222b40ce416SJulian Elischer struct thread *td; 1223df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1224df8bae1dSRodney W. Grimes { 1225fb99ab88SMatthew Dillon int error; 1226df8bae1dSRodney W. Grimes 12276c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1228df8bae1dSRodney W. Grimes return (EINVAL); 1229fb99ab88SMatthew Dillon mtx_lock(&Giant); 12309c1ab3e0SJohn Baldwin error = killpg1(td, uap->signum, uap->pgid, 0); 1231fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1232fb99ab88SMatthew Dillon return (error); 1233df8bae1dSRodney W. Grimes } 1234df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1235df8bae1dSRodney W. Grimes 1236df8bae1dSRodney W. Grimes /* 1237df8bae1dSRodney W. Grimes * Send a signal to a process group. 1238df8bae1dSRodney W. Grimes */ 1239df8bae1dSRodney W. Grimes void 12402c42a146SMarcel Moolenaar gsignal(pgid, sig) 12412c42a146SMarcel Moolenaar int pgid, sig; 1242df8bae1dSRodney W. Grimes { 1243df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1244df8bae1dSRodney W. Grimes 1245f591779bSSeigo Tanimura if (pgid != 0) { 1246ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1247f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1248ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1249f591779bSSeigo Tanimura if (pgrp != NULL) { 12502c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1251f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1252f591779bSSeigo Tanimura } 1253f591779bSSeigo Tanimura } 1254df8bae1dSRodney W. Grimes } 1255df8bae1dSRodney W. Grimes 1256df8bae1dSRodney W. Grimes /* 1257df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1258df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1259df8bae1dSRodney W. Grimes */ 1260df8bae1dSRodney W. Grimes void 12612c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1262df8bae1dSRodney W. Grimes struct pgrp *pgrp; 12632c42a146SMarcel Moolenaar int sig, checkctty; 1264df8bae1dSRodney W. Grimes { 1265df8bae1dSRodney W. Grimes register struct proc *p; 1266df8bae1dSRodney W. Grimes 1267628d2653SJohn Baldwin if (pgrp) { 1268f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1269628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1270628d2653SJohn Baldwin PROC_LOCK(p); 1271df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 12722c42a146SMarcel Moolenaar psignal(p, sig); 1273628d2653SJohn Baldwin PROC_UNLOCK(p); 1274628d2653SJohn Baldwin } 1275628d2653SJohn Baldwin } 1276df8bae1dSRodney W. Grimes } 1277df8bae1dSRodney W. Grimes 1278df8bae1dSRodney W. Grimes /* 12791bf4700bSJeff Roberson * Send a signal caused by a trap to the current thread. 1280df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1281df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1282356861dbSMatthew Dillon * 1283356861dbSMatthew Dillon * MPSAFE 1284df8bae1dSRodney W. Grimes */ 1285df8bae1dSRodney W. Grimes void 12861bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code) 1287df8bae1dSRodney W. Grimes { 12881bf4700bSJeff Roberson struct sigacts *ps; 12891bf4700bSJeff Roberson struct proc *p; 12901bf4700bSJeff Roberson 12911bf4700bSJeff Roberson p = td->td_proc; 1292df8bae1dSRodney W. Grimes 1293628d2653SJohn Baldwin PROC_LOCK(p); 1294ef3dab76STim J. Robbins ps = p->p_sigacts; 12952c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 12964093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1297df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1298df8bae1dSRodney W. Grimes #ifdef KTRACE 1299374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1300374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 13014093529dSJeff Roberson &td->td_sigmask, code); 1302df8bae1dSRodney W. Grimes #endif 1303a88b260aSJuli Mallett (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 13044093529dSJeff Roberson &td->td_sigmask, code); 13054093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 13062c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 13074093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 13082c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1309289ccde0SPeter Wemm /* 13108f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1311289ccde0SPeter Wemm */ 13122c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 13132c42a146SMarcel Moolenaar if (sig != SIGCONT && 13142c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 13152c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 13162c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1317dedc04feSPeter Wemm } 13186f841fb7SMarcel Moolenaar } else { 13196626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 13202c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 13214093529dSJeff Roberson tdsignal(td, sig); 1322df8bae1dSRodney W. Grimes } 1323628d2653SJohn Baldwin PROC_UNLOCK(p); 1324df8bae1dSRodney W. Grimes } 1325df8bae1dSRodney W. Grimes 13264093529dSJeff Roberson static struct thread * 13274093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 13284093529dSJeff Roberson { 13294093529dSJeff Roberson struct thread *td; 13304093529dSJeff Roberson 13314093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 13324093529dSJeff Roberson 13334093529dSJeff Roberson /* 13344093529dSJeff Roberson * If we know the signal is bound for a specific thread then we 13354093529dSJeff Roberson * assume that we are in that threads context. This is the case 13364093529dSJeff Roberson * for SIGXCPU, SIGILL, etc. Otherwise someone did a kill() from 13374093529dSJeff Roberson * userland and the real thread doesn't actually matter. 13384093529dSJeff Roberson */ 13394093529dSJeff Roberson if ((prop & SA_PROC) != 0 && curthread->td_proc == p) 13404093529dSJeff Roberson return (curthread); 13414093529dSJeff Roberson 13424093529dSJeff Roberson /* 13434093529dSJeff Roberson * We should search for the first thread that is blocked in 13444093529dSJeff Roberson * sigsuspend with this signal unmasked. 13454093529dSJeff Roberson */ 13464093529dSJeff Roberson 13474093529dSJeff Roberson /* XXX */ 13484093529dSJeff Roberson 13494093529dSJeff Roberson /* 13504093529dSJeff Roberson * Find the first thread in the proc that doesn't have this signal 13514093529dSJeff Roberson * masked. 13524093529dSJeff Roberson */ 13534093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) 13544093529dSJeff Roberson if (!SIGISMEMBER(td->td_sigmask, sig)) 13554093529dSJeff Roberson return (td); 13564093529dSJeff Roberson 13574093529dSJeff Roberson return (FIRST_THREAD_IN_PROC(p)); 13584093529dSJeff Roberson } 13594093529dSJeff Roberson 1360df8bae1dSRodney W. Grimes /* 1361df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1362df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1363df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1364df8bae1dSRodney W. Grimes * 1365df8bae1dSRodney W. Grimes * Exceptions: 1366df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1367df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1368df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1369df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1370df8bae1dSRodney W. Grimes * 1371df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1372df8bae1dSRodney W. Grimes */ 1373df8bae1dSRodney W. Grimes void 13744093529dSJeff Roberson psignal(struct proc *p, int sig) 1375df8bae1dSRodney W. Grimes { 1376b40ce416SJulian Elischer struct thread *td; 13774093529dSJeff Roberson int prop; 13784093529dSJeff Roberson 13794093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 13804093529dSJeff Roberson prop = sigprop(sig); 13814093529dSJeff Roberson 13824093529dSJeff Roberson /* 13834093529dSJeff Roberson * Find a thread to deliver the signal to. 13844093529dSJeff Roberson */ 13854093529dSJeff Roberson td = sigtd(p, sig, prop); 13864093529dSJeff Roberson 13874093529dSJeff Roberson tdsignal(td, sig); 13884093529dSJeff Roberson } 13894093529dSJeff Roberson 13904093529dSJeff Roberson void 13914093529dSJeff Roberson tdsignal(struct thread *td, int sig) 13924093529dSJeff Roberson { 13934093529dSJeff Roberson struct proc *p; 13944093529dSJeff Roberson register sig_t action; 13954093529dSJeff Roberson sigset_t *siglist; 13964093529dSJeff Roberson struct thread *td0; 1397e602ba25SJulian Elischer register int prop; 1398e602ba25SJulian Elischer 1399df8bae1dSRodney W. Grimes 14002899d606SDag-Erling Smørgrav KASSERT(_SIG_VALID(sig), 14014093529dSJeff Roberson ("tdsignal(): invalid signal %d\n", sig)); 14024093529dSJeff Roberson 14034093529dSJeff Roberson p = td->td_proc; 14042c42a146SMarcel Moolenaar 1405628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1406cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1407cb679c38SJonathan Lemon 14082c42a146SMarcel Moolenaar prop = sigprop(sig); 14094093529dSJeff Roberson 14104093529dSJeff Roberson /* 14114093529dSJeff Roberson * If this thread is blocking this signal then we'll leave it in the 14124093529dSJeff Roberson * proc so that we can find it in the first thread that unblocks it. 14134093529dSJeff Roberson */ 14144093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 14154093529dSJeff Roberson siglist = &p->p_siglist; 14164093529dSJeff Roberson else 14174093529dSJeff Roberson siglist = &td->td_siglist; 14184093529dSJeff Roberson 1419df8bae1dSRodney W. Grimes /* 14202a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 14212a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 14222a024a2bSSean Eric Fagan * a chance, as well. 1423df8bae1dSRodney W. Grimes */ 1424b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1425df8bae1dSRodney W. Grimes action = SIG_DFL; 1426b40ce416SJulian Elischer } else { 1427df8bae1dSRodney W. Grimes /* 1428df8bae1dSRodney W. Grimes * If the signal is being ignored, 1429df8bae1dSRodney W. Grimes * then we forget about it immediately. 1430df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1431df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1432df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1433df8bae1dSRodney W. Grimes */ 1434628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1435df8bae1dSRodney W. Grimes return; 14364093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 1437df8bae1dSRodney W. Grimes action = SIG_HOLD; 14382c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1439df8bae1dSRodney W. Grimes action = SIG_CATCH; 1440df8bae1dSRodney W. Grimes else 1441df8bae1dSRodney W. Grimes action = SIG_DFL; 1442df8bae1dSRodney W. Grimes } 1443df8bae1dSRodney W. Grimes 14444093529dSJeff Roberson if (prop & SA_CONT) { 14451d9c5696SJuli Mallett SIG_STOPSIGMASK(p->p_siglist); 14464093529dSJeff Roberson /* 14474093529dSJeff Roberson * XXX Should investigate leaving STOP and CONT sigs only in 14484093529dSJeff Roberson * the proc's siglist. 14494093529dSJeff Roberson */ 14504093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 14514093529dSJeff Roberson SIG_STOPSIGMASK(td0->td_siglist); 14524093529dSJeff Roberson } 1453df8bae1dSRodney W. Grimes 1454df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1455df8bae1dSRodney W. Grimes /* 1456df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1457df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1458df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1459df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1460df8bae1dSRodney W. Grimes */ 1461e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1462e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1463e602ba25SJulian Elischer (action == SIG_DFL)) 1464df8bae1dSRodney W. Grimes return; 14651d9c5696SJuli Mallett SIG_CONTSIGMASK(p->p_siglist); 14664093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 14674093529dSJeff Roberson SIG_CONTSIGMASK(td0->td_siglist); 14686933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1469df8bae1dSRodney W. Grimes } 14704093529dSJeff Roberson SIGADDSET(*siglist, sig); 14714093529dSJeff Roberson signotify(td); /* uses schedlock */ 1472df8bae1dSRodney W. Grimes 1473df8bae1dSRodney W. Grimes /* 1474e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1475e602ba25SJulian Elischer * component. Most processing occurs when the process next 1476e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1477e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1478e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1479e602ba25SJulian Elischer * We try do the per-process part here. 1480df8bae1dSRodney W. Grimes */ 1481e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1482e602ba25SJulian Elischer /* 1483e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1484e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1485e602ba25SJulian Elischer */ 1486e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1487e602ba25SJulian Elischer /* 1488e602ba25SJulian Elischer * The traced process is already stopped, 1489e602ba25SJulian Elischer * so no further action is necessary. 1490e602ba25SJulian Elischer * No signal can restart us. 1491e602ba25SJulian Elischer */ 1492e602ba25SJulian Elischer goto out; 14931c32c37cSJohn Baldwin } 1494b40ce416SJulian Elischer 1495e602ba25SJulian Elischer if (sig == SIGKILL) { 1496df8bae1dSRodney W. Grimes /* 1497e602ba25SJulian Elischer * SIGKILL sets process running. 1498e602ba25SJulian Elischer * It will die elsewhere. 1499e602ba25SJulian Elischer * All threads must be restarted. 1500df8bae1dSRodney W. Grimes */ 1501e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED; 1502e602ba25SJulian Elischer goto runfast; 1503e602ba25SJulian Elischer } 1504e602ba25SJulian Elischer 1505e602ba25SJulian Elischer if (prop & SA_CONT) { 1506e602ba25SJulian Elischer /* 1507e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 15084093529dSJeff Roberson * process but don't leave the signal in siglist as 15091d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 1510e602ba25SJulian Elischer * continue the process and leave the signal in 15114093529dSJeff Roberson * siglist. If the process catches SIGCONT, let it 1512e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1513e602ba25SJulian Elischer * an event, it goes back to run state. 1514e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1515e602ba25SJulian Elischer */ 15161279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 15176933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1518e602ba25SJulian Elischer if (action == SIG_DFL) { 15194093529dSJeff Roberson SIGDELSET(*siglist, sig); 1520e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1521e602ba25SJulian Elischer /* 1522e602ba25SJulian Elischer * The process wants to catch it so it needs 1523e602ba25SJulian Elischer * to run at least one thread, but which one? 1524e602ba25SJulian Elischer * It would seem that the answer would be to 1525e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1526e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1527e602ba25SJulian Elischer * process, we need to make sure that the 1528e602ba25SJulian Elischer * single thread is runnable asap. 1529e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1530e602ba25SJulian Elischer */ 1531e602ba25SJulian Elischer goto runfast; 1532e602ba25SJulian Elischer } 1533e602ba25SJulian Elischer /* 1534e602ba25SJulian Elischer * The signal is not ignored or caught. 1535e602ba25SJulian Elischer */ 1536e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 153704774f23SJulian Elischer thread_unsuspend(p); 1538e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1539e602ba25SJulian Elischer goto out; 1540e602ba25SJulian Elischer } 1541e602ba25SJulian Elischer 1542e602ba25SJulian Elischer if (prop & SA_STOP) { 1543e602ba25SJulian Elischer /* 1544e602ba25SJulian Elischer * Already stopped, don't need to stop again 1545e602ba25SJulian Elischer * (If we did the shell could get confused). 154604774f23SJulian Elischer * Just make sure the signal STOP bit set. 1547e602ba25SJulian Elischer */ 15481279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 15494093529dSJeff Roberson SIGDELSET(*siglist, sig); 1550e602ba25SJulian Elischer goto out; 1551e602ba25SJulian Elischer } 1552e602ba25SJulian Elischer 1553e602ba25SJulian Elischer /* 1554e602ba25SJulian Elischer * All other kinds of signals: 1555e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1556e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1557e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 155804774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1559e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1560e602ba25SJulian Elischer */ 1561e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 156271fad9fdSJulian Elischer if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) { 1563e602ba25SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 156471fad9fdSJulian Elischer cv_abort(td); 1565e602ba25SJulian Elischer else 156671fad9fdSJulian Elischer abortsleep(td); 1567e602ba25SJulian Elischer } 1568e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1569df8bae1dSRodney W. Grimes goto out; 1570df8bae1dSRodney W. Grimes /* 1571e602ba25SJulian Elischer * XXXKSE What about threads that are waiting on mutexes? 1572e602ba25SJulian Elischer * Shouldn't they abort too? 157304774f23SJulian Elischer * No, hopefully mutexes are short lived.. They'll 157404774f23SJulian Elischer * eventually hit thread_suspend_check(). 1575df8bae1dSRodney W. Grimes */ 1576e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 157735c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 157835c32a76SDavid Xu !(prop & SA_STOP)) { 1579721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 15804093529dSJeff Roberson tdsigwakeup(td, sig, action); 1581721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1582df8bae1dSRodney W. Grimes goto out; 158304774f23SJulian Elischer } 158435c32a76SDavid Xu if (prop & SA_STOP) { 158535c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 158635c32a76SDavid Xu goto out; 1587e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 1588e574e444SDavid Xu p->p_xstat = sig; 158935c32a76SDavid Xu mtx_lock_spin(&sched_lock); 15904093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) { 15914093529dSJeff Roberson if (TD_IS_SLEEPING(td0) && 159235c32a76SDavid Xu (td->td_flags & TDF_SINTR)) 15934093529dSJeff Roberson thread_suspend_one(td0); 159435c32a76SDavid Xu } 1595e574e444SDavid Xu thread_stopped(p); 15964093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 1597e574e444SDavid Xu SIGDELSET(p->p_siglist, p->p_xstat); 15984093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 15994093529dSJeff Roberson SIGDELSET(td0->td_siglist, p->p_xstat); 16004093529dSJeff Roberson } 160135c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 160235c32a76SDavid Xu goto out; 160335c32a76SDavid Xu } 1604721e5910SJulian Elischer else 1605df8bae1dSRodney W. Grimes goto runfast; 1606df8bae1dSRodney W. Grimes /* NOTREACHED */ 1607e602ba25SJulian Elischer } else { 1608e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 16094093529dSJeff Roberson SIGDELSET(*siglist, sig); 1610e602ba25SJulian Elischer goto out; 1611e602ba25SJulian Elischer } 1612e602ba25SJulian Elischer 1613b40ce416SJulian Elischer /* 1614e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1615e602ba25SJulian Elischer * running threads. 1616b40ce416SJulian Elischer */ 1617e602ba25SJulian Elischer 1618e602ba25SJulian Elischer runfast: 1619aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 16204093529dSJeff Roberson tdsigwakeup(td, sig, action); 1621e602ba25SJulian Elischer thread_unsuspend(p); 1622e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1623e602ba25SJulian Elischer out: 1624e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1625e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1626e602ba25SJulian Elischer } 1627e602ba25SJulian Elischer 1628e602ba25SJulian Elischer /* 1629e602ba25SJulian Elischer * The force of a signal has been directed against a single 1630e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1631e602ba25SJulian Elischer * out of any sleep it may be in etc. 1632e602ba25SJulian Elischer */ 1633e602ba25SJulian Elischer static void 16344093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action) 1635e602ba25SJulian Elischer { 1636e602ba25SJulian Elischer struct proc *p = td->td_proc; 1637e602ba25SJulian Elischer register int prop; 1638e602ba25SJulian Elischer 1639aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1640e602ba25SJulian Elischer prop = sigprop(sig); 1641e602ba25SJulian Elischer /* 1642aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1643e602ba25SJulian Elischer * killed in this lifetime. 1644e602ba25SJulian Elischer */ 1645e602ba25SJulian Elischer if ((action == SIG_DFL) && (prop & SA_KILL)) { 1646e602ba25SJulian Elischer if (td->td_priority > PUSER) { 1647e602ba25SJulian Elischer td->td_priority = PUSER; 1648b40ce416SJulian Elischer } 1649b40ce416SJulian Elischer } 1650e602ba25SJulian Elischer 1651e602ba25SJulian Elischer /* 1652e602ba25SJulian Elischer * Defer further processing for signals which are held, 1653e602ba25SJulian Elischer * except that stopped processes must be continued by SIGCONT. 1654e602ba25SJulian Elischer */ 1655e602ba25SJulian Elischer if (action == SIG_HOLD) { 1656aa0fa334SJulian Elischer return; 1657e602ba25SJulian Elischer } 165871fad9fdSJulian Elischer if (TD_IS_SLEEPING(td)) { 1659e602ba25SJulian Elischer /* 1660e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1661e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1662e602ba25SJulian Elischer * be noticed when the process returns through 1663e602ba25SJulian Elischer * trap() or syscall(). 1664e602ba25SJulian Elischer */ 1665e602ba25SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 1666aa0fa334SJulian Elischer return; 1667e602ba25SJulian Elischer } 1668e602ba25SJulian Elischer /* 1669e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1670e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1671e602ba25SJulian Elischer * for its parent. 1672e602ba25SJulian Elischer */ 1673e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1674e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1675aa0fa334SJulian Elischer } else { 1676aa0fa334SJulian Elischer 1677df8bae1dSRodney W. Grimes /* 1678e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1679e602ba25SJulian Elischer * asleep, we are finished; the process should not 1680e602ba25SJulian Elischer * be awakened. 1681df8bae1dSRodney W. Grimes */ 1682e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 16831d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 16844093529dSJeff Roberson /* 16854093529dSJeff Roberson * It may be on either list in this state. 16864093529dSJeff Roberson * Remove from both for now. 16874093529dSJeff Roberson */ 16884093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 1689aa0fa334SJulian Elischer return; 1690df8bae1dSRodney W. Grimes } 1691df8bae1dSRodney W. Grimes 1692aa0fa334SJulian Elischer /* 1693aa0fa334SJulian Elischer * Raise priority to at least PUSER. 1694aa0fa334SJulian Elischer */ 1695aa0fa334SJulian Elischer if (td->td_priority > PUSER) { 1696aa0fa334SJulian Elischer td->td_priority = PUSER; 1697aa0fa334SJulian Elischer } 1698aa0fa334SJulian Elischer } 169971fad9fdSJulian Elischer if (td->td_flags & TDF_CVWAITQ) 170071fad9fdSJulian Elischer cv_abort(td); 170171fad9fdSJulian Elischer else 170271fad9fdSJulian Elischer abortsleep(td); 1703aa0fa334SJulian Elischer } 1704aa0fa334SJulian Elischer #ifdef SMP 1705aa0fa334SJulian Elischer else { 1706df8bae1dSRodney W. Grimes /* 1707e602ba25SJulian Elischer * Other states do nothing with the signal immediatly, 1708df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1709df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1710df8bae1dSRodney W. Grimes */ 171171fad9fdSJulian Elischer if (TD_IS_RUNNING(td) && td != curthread) { 1712e602ba25SJulian Elischer forward_signal(td); 1713aa0fa334SJulian Elischer } 17140ac3b636SAndrew Gallatin } 17153163861cSTor Egge #endif 17166caa8a15SJohn Baldwin } 1717df8bae1dSRodney W. Grimes 1718df8bae1dSRodney W. Grimes /* 1719df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1720df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1721df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1722df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1723df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1724628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 1725df8bae1dSRodney W. Grimes * sequence is 1726df8bae1dSRodney W. Grimes * 1727e602ba25SJulian Elischer * while (sig = cursig(curthread)) 17282c42a146SMarcel Moolenaar * postsig(sig); 1729df8bae1dSRodney W. Grimes */ 173026f9a767SRodney W. Grimes int 1731e602ba25SJulian Elischer issignal(td) 1732e602ba25SJulian Elischer struct thread *td; 1733df8bae1dSRodney W. Grimes { 1734e602ba25SJulian Elischer struct proc *p; 17354093529dSJeff Roberson sigset_t sigpending; 17362c42a146SMarcel Moolenaar register int sig, prop; 1737df8bae1dSRodney W. Grimes 1738e602ba25SJulian Elischer p = td->td_proc; 1739628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 174026306795SJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &p->p_mtx.mtx_object, 174126306795SJohn Baldwin "Checking for signals"); 1742df8bae1dSRodney W. Grimes for (;;) { 17432a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 17442a024a2bSSean Eric Fagan 17454093529dSJeff Roberson sigpending = td->td_siglist; 17464093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 17474093529dSJeff Roberson 1748df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 17494093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 17504093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 1751df8bae1dSRodney W. Grimes return (0); 17524093529dSJeff Roberson sig = sig_ffs(&sigpending); 17532c42a146SMarcel Moolenaar prop = sigprop(sig); 17542a024a2bSSean Eric Fagan 1755628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 17562a024a2bSSean Eric Fagan 1757df8bae1dSRodney W. Grimes /* 1758df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1759df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1760df8bae1dSRodney W. Grimes */ 17612c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 17624093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 1763df8bae1dSRodney W. Grimes continue; 1764df8bae1dSRodney W. Grimes } 1765df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1766df8bae1dSRodney W. Grimes /* 1767d8f4f6a4SJonathan Mini * If traced, always stop. 1768df8bae1dSRodney W. Grimes */ 17692c42a146SMarcel Moolenaar p->p_xstat = sig; 1770628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1771df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1772628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 17739ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 17744d492b43SJulian Elischer stop(p); /* uses schedlock too eventually */ 177571fad9fdSJulian Elischer thread_suspend_one(td); 1776c86b6ff5SJohn Baldwin PROC_UNLOCK(p); 1777c86b6ff5SJohn Baldwin DROP_GIANT(); 17782ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1779df8bae1dSRodney W. Grimes mi_switch(); 17809ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 178120cdcc5bSJohn Baldwin PICKUP_GIANT(); 1782628d2653SJohn Baldwin PROC_LOCK(p); 1783df8bae1dSRodney W. Grimes 1784df8bae1dSRodney W. Grimes /* 1785df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1786df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1787df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1788df8bae1dSRodney W. Grimes */ 1789df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1790df8bae1dSRodney W. Grimes continue; 1791df8bae1dSRodney W. Grimes 1792df8bae1dSRodney W. Grimes /* 1793df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1794df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1795df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1796df8bae1dSRodney W. Grimes */ 17974093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* clear old signal */ 17982c42a146SMarcel Moolenaar sig = p->p_xstat; 17992c42a146SMarcel Moolenaar if (sig == 0) 1800df8bae1dSRodney W. Grimes continue; 1801df8bae1dSRodney W. Grimes 1802df8bae1dSRodney W. Grimes /* 18034093529dSJeff Roberson * Put the new signal into td_siglist. If the 18041d9c5696SJuli Mallett * signal is being masked, look for other signals. 1805df8bae1dSRodney W. Grimes */ 18064093529dSJeff Roberson SIGADDSET(td->td_siglist, sig); 18074093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 1808df8bae1dSRodney W. Grimes continue; 18094093529dSJeff Roberson signotify(td); 1810df8bae1dSRodney W. Grimes } 1811df8bae1dSRodney W. Grimes 1812df8bae1dSRodney W. Grimes /* 1813df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1814df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1815df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1816df8bae1dSRodney W. Grimes */ 1817d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1818df8bae1dSRodney W. Grimes 1819d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 1820df8bae1dSRodney W. Grimes /* 1821df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1822df8bae1dSRodney W. Grimes */ 1823df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1824df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1825df8bae1dSRodney W. Grimes /* 1826df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1827df8bae1dSRodney W. Grimes * in init? XXX 1828df8bae1dSRodney W. Grimes */ 1829d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 18302c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1831df8bae1dSRodney W. Grimes #endif 1832df8bae1dSRodney W. Grimes break; /* == ignore */ 1833df8bae1dSRodney W. Grimes } 1834df8bae1dSRodney W. Grimes /* 1835df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1836df8bae1dSRodney W. Grimes * with default action, stop here, 1837df8bae1dSRodney W. Grimes * then clear the signal. However, 1838df8bae1dSRodney W. Grimes * if process is member of an orphaned 1839df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1840df8bae1dSRodney W. Grimes */ 1841df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1842df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1843df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1844df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1845df8bae1dSRodney W. Grimes break; /* == ignore */ 1846e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 18472c42a146SMarcel Moolenaar p->p_xstat = sig; 1848721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 1849e574e444SDavid Xu thread_stopped(p); 185071fad9fdSJulian Elischer thread_suspend_one(td); 1851721e5910SJulian Elischer PROC_UNLOCK(p); 1852721e5910SJulian Elischer DROP_GIANT(); 1853721e5910SJulian Elischer p->p_stats->p_ru.ru_nivcsw++; 1854721e5910SJulian Elischer mi_switch(); 1855721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1856721e5910SJulian Elischer PICKUP_GIANT(); 1857721e5910SJulian Elischer PROC_LOCK(p); 1858df8bae1dSRodney W. Grimes break; 185921b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 1860df8bae1dSRodney W. Grimes /* 1861df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1862df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1863df8bae1dSRodney W. Grimes */ 1864df8bae1dSRodney W. Grimes break; /* == ignore */ 1865df8bae1dSRodney W. Grimes } else 18662c42a146SMarcel Moolenaar return (sig); 1867df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1868df8bae1dSRodney W. Grimes 1869d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 1870df8bae1dSRodney W. Grimes /* 1871df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1872df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1873df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1874df8bae1dSRodney W. Grimes */ 1875df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1876df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1877df8bae1dSRodney W. Grimes printf("issignal\n"); 1878df8bae1dSRodney W. Grimes break; /* == ignore */ 1879df8bae1dSRodney W. Grimes 1880df8bae1dSRodney W. Grimes default: 1881df8bae1dSRodney W. Grimes /* 1882df8bae1dSRodney W. Grimes * This signal has an action, let 1883df8bae1dSRodney W. Grimes * postsig() process it. 1884df8bae1dSRodney W. Grimes */ 18852c42a146SMarcel Moolenaar return (sig); 1886df8bae1dSRodney W. Grimes } 18874093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* take the signal! */ 1888df8bae1dSRodney W. Grimes } 1889df8bae1dSRodney W. Grimes /* NOTREACHED */ 1890df8bae1dSRodney W. Grimes } 1891df8bae1dSRodney W. Grimes 1892df8bae1dSRodney W. Grimes /* 1893df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1894df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 18955b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 18965b3047d5SJohn Baldwin * lock held. 1897df8bae1dSRodney W. Grimes */ 18985b3047d5SJohn Baldwin static void 1899e574e444SDavid Xu stop(struct proc *p) 1900df8bae1dSRodney W. Grimes { 1901df8bae1dSRodney W. Grimes 1902628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 19031279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 1904df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 190501609114SAlfred Perlstein wakeup(p->p_pptr); 1906df8bae1dSRodney W. Grimes } 1907df8bae1dSRodney W. Grimes 1908e574e444SDavid Xu void 1909e574e444SDavid Xu thread_stopped(struct proc *p) 1910e574e444SDavid Xu { 1911e574e444SDavid Xu struct proc *p1 = curthread->td_proc; 1912e574e444SDavid Xu int n; 1913e574e444SDavid Xu 1914e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1915e574e444SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 1916e574e444SDavid Xu n = p->p_suspcount; 1917e574e444SDavid Xu if (p == p1) 1918e574e444SDavid Xu n++; 1919e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 1920e574e444SDavid Xu mtx_unlock_spin(&sched_lock); 1921e574e444SDavid Xu stop(p); 1922e574e444SDavid Xu PROC_LOCK(p->p_pptr); 1923e574e444SDavid Xu if ((p->p_pptr->p_procsig->ps_flag & 1924e574e444SDavid Xu PS_NOCLDSTOP) == 0) { 1925e574e444SDavid Xu psignal(p->p_pptr, SIGCHLD); 1926e574e444SDavid Xu } 1927e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 1928e574e444SDavid Xu mtx_lock_spin(&sched_lock); 1929e574e444SDavid Xu } 1930e574e444SDavid Xu } 1931e574e444SDavid Xu 1932df8bae1dSRodney W. Grimes /* 1933df8bae1dSRodney W. Grimes * Take the action for the specified signal 1934df8bae1dSRodney W. Grimes * from the current set of pending signals. 1935df8bae1dSRodney W. Grimes */ 1936df8bae1dSRodney W. Grimes void 19372c42a146SMarcel Moolenaar postsig(sig) 19382c42a146SMarcel Moolenaar register int sig; 1939df8bae1dSRodney W. Grimes { 1940b40ce416SJulian Elischer struct thread *td = curthread; 1941b40ce416SJulian Elischer register struct proc *p = td->td_proc; 1942628d2653SJohn Baldwin struct sigacts *ps; 19432c42a146SMarcel Moolenaar sig_t action; 19442c42a146SMarcel Moolenaar sigset_t returnmask; 19452c42a146SMarcel Moolenaar int code; 1946df8bae1dSRodney W. Grimes 19472c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 19485526d2d9SEivind Eklund 19492ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1950628d2653SJohn Baldwin ps = p->p_sigacts; 19514093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 19522c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1953df8bae1dSRodney W. Grimes #ifdef KTRACE 1954374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 19554093529dSJeff Roberson ktrpsig(sig, action, td->td_flags & TDF_OLDMASK ? 19564093529dSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 1957df8bae1dSRodney W. Grimes #endif 1958628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 19592a024a2bSSean Eric Fagan 1960df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1961df8bae1dSRodney W. Grimes /* 1962df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1963df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1964df8bae1dSRodney W. Grimes */ 1965b40ce416SJulian Elischer sigexit(td, sig); 1966df8bae1dSRodney W. Grimes /* NOTREACHED */ 1967df8bae1dSRodney W. Grimes } else { 1968df8bae1dSRodney W. Grimes /* 1969df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1970df8bae1dSRodney W. Grimes */ 19714093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 19725526d2d9SEivind Eklund ("postsig action")); 1973df8bae1dSRodney W. Grimes /* 1974df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1975645682fdSLuoqi Chen * occurrences of this signal. 1976df8bae1dSRodney W. Grimes * 1977645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1978df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1979645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1980df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1981df8bae1dSRodney W. Grimes */ 19824093529dSJeff Roberson mtx_lock_spin(&sched_lock); 19834093529dSJeff Roberson if (td->td_flags & TDF_OLDMASK) { 19844093529dSJeff Roberson returnmask = td->td_oldsigmask; 19854093529dSJeff Roberson td->td_flags &= ~TDF_OLDMASK; 1986df8bae1dSRodney W. Grimes } else 19874093529dSJeff Roberson returnmask = td->td_sigmask; 19884093529dSJeff Roberson mtx_unlock_spin(&sched_lock); 19892c42a146SMarcel Moolenaar 19904093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 19912c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 19924093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 19932c42a146SMarcel Moolenaar 19942c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1995289ccde0SPeter Wemm /* 19968f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1997289ccde0SPeter Wemm */ 19982c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 19992c42a146SMarcel Moolenaar if (sig != SIGCONT && 20002c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 20012c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 20022c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2003dedc04feSPeter Wemm } 2004df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 20052c42a146SMarcel Moolenaar if (p->p_sig != sig) { 2006df8bae1dSRodney W. Grimes code = 0; 2007df8bae1dSRodney W. Grimes } else { 20086626c604SJulian Elischer code = p->p_code; 20096626c604SJulian Elischer p->p_code = 0; 20106626c604SJulian Elischer p->p_sig = 0; 2011df8bae1dSRodney W. Grimes } 2012ac2e4153SJulian Elischer if (p->p_flag & P_THREADED) 201358a3c273SJeff Roberson thread_signal_add(curthread, sig); 201458a3c273SJeff Roberson else 201558a3c273SJeff Roberson (*p->p_sysent->sv_sendsig)(action, sig, 201658a3c273SJeff Roberson &returnmask, code); 2017df8bae1dSRodney W. Grimes } 2018df8bae1dSRodney W. Grimes } 2019df8bae1dSRodney W. Grimes 2020df8bae1dSRodney W. Grimes /* 2021df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2022df8bae1dSRodney W. Grimes */ 202326f9a767SRodney W. Grimes void 2024df8bae1dSRodney W. Grimes killproc(p, why) 2025df8bae1dSRodney W. Grimes struct proc *p; 2026df8bae1dSRodney W. Grimes char *why; 2027df8bae1dSRodney W. Grimes { 20289081e5e8SJohn Baldwin 20299081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 20300384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 20310384fff8SJason Evans p, p->p_pid, p->p_comm); 2032729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2033b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2034df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2039df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2040df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2041df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2042df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2043df8bae1dSRodney W. Grimes * does not return. 2044df8bae1dSRodney W. Grimes */ 204526f9a767SRodney W. Grimes void 2046b40ce416SJulian Elischer sigexit(td, sig) 2047b40ce416SJulian Elischer struct thread *td; 20482c42a146SMarcel Moolenaar int sig; 2049df8bae1dSRodney W. Grimes { 2050b40ce416SJulian Elischer struct proc *p = td->td_proc; 2051df8bae1dSRodney W. Grimes 2052628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2053df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 20542c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 20552c42a146SMarcel Moolenaar p->p_sig = sig; 2056c364e17eSAndrey A. Chernov /* 2057c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2058c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2059c364e17eSAndrey A. Chernov * these messages.) 2060c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 2061c364e17eSAndrey A. Chernov */ 2062628d2653SJohn Baldwin PROC_UNLOCK(p); 2063c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 2064c31146a1SJohn Baldwin mtx_lock(&Giant); 2065b40ce416SJulian Elischer if (coredump(td) == 0) 20662c42a146SMarcel Moolenaar sig |= WCOREFLAG; 206757308494SJoerg Wunsch if (kern_logsigexit) 206857308494SJoerg Wunsch log(LOG_INFO, 206957308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 20703d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 20719c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 20722c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 20732c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 2074c31146a1SJohn Baldwin } else { 2075628d2653SJohn Baldwin PROC_UNLOCK(p); 2076628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 2077628d2653SJohn Baldwin mtx_lock(&Giant); 2078c31146a1SJohn Baldwin } 2079b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2080df8bae1dSRodney W. Grimes /* NOTREACHED */ 2081df8bae1dSRodney W. Grimes } 2082df8bae1dSRodney W. Grimes 2083c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 2084c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2085c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 2086c5edb423SSean Eric Fagan 2087c5edb423SSean Eric Fagan /* 2088c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 2089c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 2090c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 2091c5edb423SSean Eric Fagan * %N name of process ("name") 2092c5edb423SSean Eric Fagan * %P process id (pid) 2093c5edb423SSean Eric Fagan * %U user id (uid) 2094c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 2095c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2096c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 2097c5edb423SSean Eric Fagan */ 2098c5edb423SSean Eric Fagan 2099fca666a1SJulian Elischer static char * 2100c5edb423SSean Eric Fagan expand_name(name, uid, pid) 21018b43b535SAlfred Perlstein const char *name; 21028b43b535SAlfred Perlstein uid_t uid; 21038b43b535SAlfred Perlstein pid_t pid; 21048b43b535SAlfred Perlstein { 21058b43b535SAlfred Perlstein const char *format, *appendstr; 2106c5edb423SSean Eric Fagan char *temp; 2107c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 21088b43b535SAlfred Perlstein size_t i, l, n; 2109c5edb423SSean Eric Fagan 21108b43b535SAlfred Perlstein format = corefilename; 21118b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 21120bfe2990SEivind Eklund if (temp == NULL) 21138b43b535SAlfred Perlstein return (NULL); 2114ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2115c5edb423SSean Eric Fagan switch (format[i]) { 2116c5edb423SSean Eric Fagan case '%': /* Format character */ 2117c5edb423SSean Eric Fagan i++; 2118c5edb423SSean Eric Fagan switch (format[i]) { 2119c5edb423SSean Eric Fagan case '%': 21208b43b535SAlfred Perlstein appendstr = "%"; 2121c5edb423SSean Eric Fagan break; 2122c5edb423SSean Eric Fagan case 'N': /* process name */ 21238b43b535SAlfred Perlstein appendstr = name; 2124c5edb423SSean Eric Fagan break; 2125c5edb423SSean Eric Fagan case 'P': /* process id */ 21268b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 21278b43b535SAlfred Perlstein appendstr = buf; 2128c5edb423SSean Eric Fagan break; 2129c5edb423SSean Eric Fagan case 'U': /* user id */ 21308b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 21318b43b535SAlfred Perlstein appendstr = buf; 2132c5edb423SSean Eric Fagan break; 2133c5edb423SSean Eric Fagan default: 21348b43b535SAlfred Perlstein appendstr = ""; 21358b43b535SAlfred Perlstein log(LOG_ERR, 21368b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 21378b43b535SAlfred Perlstein format[i], format); 2138c5edb423SSean Eric Fagan } 21398b43b535SAlfred Perlstein l = strlen(appendstr); 21408b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 21418b43b535SAlfred Perlstein goto toolong; 21428b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 21438b43b535SAlfred Perlstein n += l; 2144c5edb423SSean Eric Fagan break; 2145c5edb423SSean Eric Fagan default: 2146c5edb423SSean Eric Fagan temp[n++] = format[i]; 2147c5edb423SSean Eric Fagan } 2148c5edb423SSean Eric Fagan } 21498b43b535SAlfred Perlstein if (format[i] != '\0') 21508b43b535SAlfred Perlstein goto toolong; 21518b43b535SAlfred Perlstein return (temp); 21528b43b535SAlfred Perlstein toolong: 21538b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 21548b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 21558b43b535SAlfred Perlstein free(temp, M_TEMP); 21568b43b535SAlfred Perlstein return (NULL); 2157c5edb423SSean Eric Fagan } 2158c5edb423SSean Eric Fagan 2159df8bae1dSRodney W. Grimes /* 2160fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2161fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2162fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2163fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2164fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2165fca666a1SJulian Elischer */ 2166fca666a1SJulian Elischer 2167fca666a1SJulian Elischer static int 2168b40ce416SJulian Elischer coredump(struct thread *td) 2169fca666a1SJulian Elischer { 2170b40ce416SJulian Elischer struct proc *p = td->td_proc; 2171fca666a1SJulian Elischer register struct vnode *vp; 21729c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 217306ae1e91SMatthew Dillon struct flock lf; 2174fca666a1SJulian Elischer struct nameidata nd; 2175fca666a1SJulian Elischer struct vattr vattr; 2176e6796b67SKirk McKusick int error, error1, flags; 2177f2a2857bSKirk McKusick struct mount *mp; 2178fca666a1SJulian Elischer char *name; /* name of corefile */ 2179fca666a1SJulian Elischer off_t limit; 2180fca666a1SJulian Elischer 2181628d2653SJohn Baldwin PROC_LOCK(p); 2182628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2183fca666a1SJulian Elischer 2184628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2185628d2653SJohn Baldwin PROC_UNLOCK(p); 2186fca666a1SJulian Elischer return (EFAULT); 2187628d2653SJohn Baldwin } 2188fca666a1SJulian Elischer 2189fca666a1SJulian Elischer /* 219035a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 219135a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 219235a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 219335a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 219435a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 219535a2598fSSean Eric Fagan * if it is larger than the limit. 2196fca666a1SJulian Elischer */ 219735a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2198628d2653SJohn Baldwin if (limit == 0) { 2199628d2653SJohn Baldwin PROC_UNLOCK(p); 220035a2598fSSean Eric Fagan return 0; 2201628d2653SJohn Baldwin } 2202628d2653SJohn Baldwin PROC_UNLOCK(p); 220335a2598fSSean Eric Fagan 2204f2a2857bSKirk McKusick restart: 22059c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2206ccdbd10cSPeter Pentchev if (name == NULL) 2207ccdbd10cSPeter Pentchev return (EINVAL); 2208b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2209e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 2210e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2211fca666a1SJulian Elischer free(name, M_TEMP); 2212fca666a1SJulian Elischer if (error) 2213fca666a1SJulian Elischer return (error); 2214762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2215fca666a1SJulian Elischer vp = nd.ni_vp; 221606ae1e91SMatthew Dillon 2217832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2218832dafadSDon Lewis if (vp->v_type != VREG || 2219832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2220832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2221832dafadSDon Lewis error = EFAULT; 2222832dafadSDon Lewis goto out2; 2223832dafadSDon Lewis } 2224832dafadSDon Lewis 2225b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 222606ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 222706ae1e91SMatthew Dillon lf.l_start = 0; 222806ae1e91SMatthew Dillon lf.l_len = 0; 222906ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 223006ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 223106ae1e91SMatthew Dillon if (error) 223206ae1e91SMatthew Dillon goto out2; 223306ae1e91SMatthew Dillon 223406ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 223506ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 223606ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2237b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2238f2a2857bSKirk McKusick return (error); 2239f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2240f2a2857bSKirk McKusick return (error); 2241f2a2857bSKirk McKusick goto restart; 2242f2a2857bSKirk McKusick } 2243fca666a1SJulian Elischer 2244fca666a1SJulian Elischer VATTR_NULL(&vattr); 2245fca666a1SJulian Elischer vattr.va_size = 0; 224688b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2247b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2248b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 224988b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2250628d2653SJohn Baldwin PROC_LOCK(p); 2251fca666a1SJulian Elischer p->p_acflag |= ACORE; 2252628d2653SJohn Baldwin PROC_UNLOCK(p); 2253fca666a1SJulian Elischer 2254fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2255b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2256fca666a1SJulian Elischer ENOSYS; 2257fca666a1SJulian Elischer 225806ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 225906ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2260f2a2857bSKirk McKusick vn_finished_write(mp); 226106ae1e91SMatthew Dillon out2: 2262b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2263fca666a1SJulian Elischer if (error == 0) 2264fca666a1SJulian Elischer error = error1; 2265fca666a1SJulian Elischer return (error); 2266fca666a1SJulian Elischer } 2267fca666a1SJulian Elischer 2268fca666a1SJulian Elischer /* 2269df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2270df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2271df8bae1dSRodney W. Grimes */ 2272d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2273df8bae1dSRodney W. Grimes struct nosys_args { 2274df8bae1dSRodney W. Grimes int dummy; 2275df8bae1dSRodney W. Grimes }; 2276d2d3e875SBruce Evans #endif 2277fb99ab88SMatthew Dillon /* 2278fb99ab88SMatthew Dillon * MPSAFE 2279fb99ab88SMatthew Dillon */ 2280df8bae1dSRodney W. Grimes /* ARGSUSED */ 228126f9a767SRodney W. Grimes int 2282b40ce416SJulian Elischer nosys(td, args) 2283b40ce416SJulian Elischer struct thread *td; 2284df8bae1dSRodney W. Grimes struct nosys_args *args; 2285df8bae1dSRodney W. Grimes { 2286b40ce416SJulian Elischer struct proc *p = td->td_proc; 2287b40ce416SJulian Elischer 2288fb99ab88SMatthew Dillon mtx_lock(&Giant); 2289628d2653SJohn Baldwin PROC_LOCK(p); 2290df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2291628d2653SJohn Baldwin PROC_UNLOCK(p); 2292fb99ab88SMatthew Dillon mtx_unlock(&Giant); 2293f5216b9aSBruce Evans return (ENOSYS); 2294df8bae1dSRodney W. Grimes } 2295831d27a9SDon Lewis 2296831d27a9SDon Lewis /* 229748f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2298831d27a9SDon Lewis * stored credentials rather than those of the current process. 2299831d27a9SDon Lewis */ 2300831d27a9SDon Lewis void 2301f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2302f1320723SAlfred Perlstein struct sigio **sigiop; 23032c42a146SMarcel Moolenaar int sig, checkctty; 2304831d27a9SDon Lewis { 2305f1320723SAlfred Perlstein struct sigio *sigio; 2306831d27a9SDon Lewis 2307f1320723SAlfred Perlstein SIGIO_LOCK(); 2308f1320723SAlfred Perlstein sigio = *sigiop; 2309f1320723SAlfred Perlstein if (sigio == NULL) { 2310f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2311f1320723SAlfred Perlstein return; 2312f1320723SAlfred Perlstein } 2313831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2314628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 23152b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 23162c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2317628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2318831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2319831d27a9SDon Lewis struct proc *p; 2320831d27a9SDon Lewis 2321f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2322628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2323628d2653SJohn Baldwin PROC_LOCK(p); 23242b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2325831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 23262c42a146SMarcel Moolenaar psignal(p, sig); 2327628d2653SJohn Baldwin PROC_UNLOCK(p); 2328628d2653SJohn Baldwin } 2329f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2330831d27a9SDon Lewis } 2331f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2332831d27a9SDon Lewis } 2333cb679c38SJonathan Lemon 2334cb679c38SJonathan Lemon static int 2335cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2336cb679c38SJonathan Lemon { 2337cb679c38SJonathan Lemon struct proc *p = curproc; 2338cb679c38SJonathan Lemon 2339cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2340cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2341cb679c38SJonathan Lemon 2342628d2653SJohn Baldwin PROC_LOCK(p); 2343cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2344628d2653SJohn Baldwin PROC_UNLOCK(p); 2345cb679c38SJonathan Lemon 2346cb679c38SJonathan Lemon return (0); 2347cb679c38SJonathan Lemon } 2348cb679c38SJonathan Lemon 2349cb679c38SJonathan Lemon static void 2350cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2351cb679c38SJonathan Lemon { 2352cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2353cb679c38SJonathan Lemon 2354628d2653SJohn Baldwin PROC_LOCK(p); 2355e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2356628d2653SJohn Baldwin PROC_UNLOCK(p); 2357cb679c38SJonathan Lemon } 2358cb679c38SJonathan Lemon 2359cb679c38SJonathan Lemon /* 2360cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2361cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2362cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2363cb679c38SJonathan Lemon * isn't worth the trouble. 2364cb679c38SJonathan Lemon */ 2365cb679c38SJonathan Lemon static int 2366cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2367cb679c38SJonathan Lemon { 2368cb679c38SJonathan Lemon 2369cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2370cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2371cb679c38SJonathan Lemon 2372cb679c38SJonathan Lemon if (kn->kn_id == hint) 2373cb679c38SJonathan Lemon kn->kn_data++; 2374cb679c38SJonathan Lemon } 2375cb679c38SJonathan Lemon return (kn->kn_data != 0); 2376cb679c38SJonathan Lemon } 2377