1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 425591b823SEivind Eklund #include "opt_compat.h" 43db6a20e2SGarrett Wollman #include "opt_ktrace.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46c87e2930SDavid Greenman #include <sys/kernel.h> 47d2d3e875SBruce Evans #include <sys/sysproto.h> 4836240ea5SDoug Rabson #include <sys/systm.h> 49df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/namei.h> 51df8bae1dSRodney W. Grimes #include <sys/vnode.h> 52cb679c38SJonathan Lemon #include <sys/event.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 542a024a2bSSean Eric Fagan #include <sys/pioctl.h> 55df8bae1dSRodney W. Grimes #include <sys/acct.h> 563ac4d1efSBruce Evans #include <sys/fcntl.h> 57238510fcSJason Evans #include <sys/condvar.h> 58c31146a1SJohn Baldwin #include <sys/lock.h> 5935e0e5b3SJohn Baldwin #include <sys/mutex.h> 60df8bae1dSRodney W. Grimes #include <sys/wait.h> 610384fff8SJason Evans #include <sys/ktr.h> 62df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 63c31146a1SJohn Baldwin #include <sys/resourcevar.h> 646caa8a15SJohn Baldwin #include <sys/smp.h> 65df8bae1dSRodney W. Grimes #include <sys/stat.h> 661005a129SJohn Baldwin #include <sys/sx.h> 671005a129SJohn Baldwin #include <sys/syslog.h> 68d66a5066SPeter Wemm #include <sys/sysent.h> 69c87e2930SDavid Greenman #include <sys/sysctl.h> 70c5edb423SSean Eric Fagan #include <sys/malloc.h> 7106ae1e91SMatthew Dillon #include <sys/unistd.h> 72df8bae1dSRodney W. Grimes 73df8bae1dSRodney W. Grimes #include <machine/cpu.h> 74df8bae1dSRodney W. Grimes 756f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 766f841fb7SMarcel Moolenaar 776f841fb7SMarcel Moolenaar static int coredump __P((struct proc *)); 782c42a146SMarcel Moolenaar static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act, 79645682fdSLuoqi Chen struct sigaction *oact, int old)); 802c42a146SMarcel Moolenaar static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set, 81645682fdSLuoqi Chen sigset_t *oset, int old)); 82f3a6cf70SSean Eric Fagan static char *expand_name __P((const char *, uid_t, pid_t)); 836f841fb7SMarcel Moolenaar static int killpg1 __P((struct proc *cp, int sig, int pgid, int all)); 846f841fb7SMarcel Moolenaar static int sig_ffs __P((sigset_t *set)); 856f841fb7SMarcel Moolenaar static int sigprop __P((int sig)); 866f841fb7SMarcel Moolenaar static void stop __P((struct proc *)); 8726f9a767SRodney W. Grimes 88cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 89cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 90cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 91cb679c38SJonathan Lemon 92cb679c38SJonathan Lemon struct filterops sig_filtops = 93cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 94cb679c38SJonathan Lemon 9557308494SJoerg Wunsch static int kern_logsigexit = 1; 963d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 973d177f46SBill Fumerola &kern_logsigexit, 0, 983d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 9957308494SJoerg Wunsch 100df8bae1dSRodney W. Grimes /* 101b1fc0ec1SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 102831d27a9SDon Lewis */ 103b1fc0ec1SRobert Watson #define CANSIGIO(cr1, cr2) \ 104b1fc0ec1SRobert Watson ((cr1)->cr_uid == 0 || \ 105b1fc0ec1SRobert Watson (cr2)->cr_ruid == (cr2)->cr_ruid || \ 106b1fc0ec1SRobert Watson (cr2)->cr_uid == (cr2)->cr_ruid || \ 107b1fc0ec1SRobert Watson (cr2)->cr_ruid == (cr2)->cr_uid || \ 108b1fc0ec1SRobert Watson (cr2)->cr_uid == (cr2)->cr_uid) 109831d27a9SDon Lewis 11022d4b0fbSJohn Polstra int sugid_coredump; 1113d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1123d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 113c87e2930SDavid Greenman 114e5a28db9SPaul Saab static int do_coredump = 1; 115e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 116e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 117e5a28db9SPaul Saab 1182c42a146SMarcel Moolenaar /* 1192c42a146SMarcel Moolenaar * Signal properties and actions. 1202c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1212c42a146SMarcel Moolenaar * according to the following properties: 1222c42a146SMarcel Moolenaar */ 1232c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1242c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1252c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1262c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1272c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1282c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1292c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 130df8bae1dSRodney W. Grimes 1312c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1322c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1332c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1342c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1352c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1362c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1372c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1382c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1392c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1402c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1412c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1422c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1442c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1452c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1462c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1472c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1482c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1492c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1502c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1512c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1522c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1532c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1542c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1552c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1562c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1572c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1582c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1592c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1602c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1612c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1622c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1632c42a146SMarcel Moolenaar }; 1642c42a146SMarcel Moolenaar 165fbbeeb6cSBruce Evans /* 166fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 167fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 168fbbeeb6cSBruce Evans * action, the process stops in issignal(). 169fbbeeb6cSBruce Evans * 17033510ef1SBruce Evans * MP SAFE. 171fbbeeb6cSBruce Evans */ 172fbbeeb6cSBruce Evans int 173fbbeeb6cSBruce Evans CURSIG(struct proc *p) 174fbbeeb6cSBruce Evans { 175fbbeeb6cSBruce Evans sigset_t tmpset; 176fbbeeb6cSBruce Evans 1772ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 17833510ef1SBruce Evans if (SIGISEMPTY(p->p_siglist)) 1792ad7d304SJohn Baldwin return (0); 180fbbeeb6cSBruce Evans tmpset = p->p_siglist; 181fbbeeb6cSBruce Evans SIGSETNAND(tmpset, p->p_sigmask); 18233510ef1SBruce Evans if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0) 1832ad7d304SJohn Baldwin return (0); 1842ad7d304SJohn Baldwin return (issignal(p)); 185fbbeeb6cSBruce Evans } 186fbbeeb6cSBruce Evans 1876f841fb7SMarcel Moolenaar static __inline int 1886f841fb7SMarcel Moolenaar sigprop(int sig) 1892c42a146SMarcel Moolenaar { 1906f841fb7SMarcel Moolenaar 1912c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 1922c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 1932c42a146SMarcel Moolenaar return (0); 194df8bae1dSRodney W. Grimes } 1952c42a146SMarcel Moolenaar 1966f841fb7SMarcel Moolenaar static __inline int 1976f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 1982c42a146SMarcel Moolenaar { 1992c42a146SMarcel Moolenaar int i; 2002c42a146SMarcel Moolenaar 2016f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2022c42a146SMarcel Moolenaar if (set->__bits[i]) 2032c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 204df8bae1dSRodney W. Grimes return (0); 205df8bae1dSRodney W. Grimes } 206df8bae1dSRodney W. Grimes 2072c42a146SMarcel Moolenaar /* 2082c42a146SMarcel Moolenaar * do_sigaction 2092c42a146SMarcel Moolenaar * sigaction 2102c42a146SMarcel Moolenaar * osigaction 2112c42a146SMarcel Moolenaar */ 2122c42a146SMarcel Moolenaar static int 213645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old) 2142c42a146SMarcel Moolenaar struct proc *p; 2152c42a146SMarcel Moolenaar register int sig; 2162c42a146SMarcel Moolenaar struct sigaction *act, *oact; 217645682fdSLuoqi Chen int old; 218df8bae1dSRodney W. Grimes { 219628d2653SJohn Baldwin register struct sigacts *ps; 220df8bae1dSRodney W. Grimes 2212c42a146SMarcel Moolenaar if (sig <= 0 || sig > _SIG_MAXSIG) 2222c42a146SMarcel Moolenaar return (EINVAL); 2232c42a146SMarcel Moolenaar 224628d2653SJohn Baldwin PROC_LOCK(p); 225628d2653SJohn Baldwin ps = p->p_sigacts; 2262c42a146SMarcel Moolenaar if (oact) { 2272c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2282c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2292c42a146SMarcel Moolenaar oact->sa_flags = 0; 2302c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2312c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2322c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2332c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2342c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2352c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2362c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2372c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2382c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2392c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 240645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2412c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 242645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2432c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2442c42a146SMarcel Moolenaar } 2452c42a146SMarcel Moolenaar if (act) { 2462c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 247628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 248628d2653SJohn Baldwin PROC_UNLOCK(p); 2492c42a146SMarcel Moolenaar return (EINVAL); 250628d2653SJohn Baldwin } 2512c42a146SMarcel Moolenaar 252df8bae1dSRodney W. Grimes /* 253df8bae1dSRodney W. Grimes * Change setting atomically. 254df8bae1dSRodney W. Grimes */ 2552c42a146SMarcel Moolenaar 2562c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2572c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2582c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 259aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2604fec48c6SMatthew Dillon SIGADDSET(ps->ps_siginfo, sig); 2614fec48c6SMatthew Dillon } else { 262aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 263aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 2642c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2652c42a146SMarcel Moolenaar } 2662c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2672c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 268df8bae1dSRodney W. Grimes else 2692c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2702c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2712c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 272df8bae1dSRodney W. Grimes else 2732c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2742c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2752c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2761e41c1b5SSteven Wallace else 2772c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 2782c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 2792c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 280289ccde0SPeter Wemm else 2812c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 282df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 2832c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 2842c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 285df8bae1dSRodney W. Grimes else 2862c42a146SMarcel Moolenaar SIGDELSET(ps->ps_usertramp, seg); 287df8bae1dSRodney W. Grimes #endif 2882c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 2892c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 290645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 2916626c604SJulian Elischer else 292645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 293aa7a4daeSPeter Wemm if ((act->sa_flags & SA_NOCLDWAIT) || 294aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) { 295245f17d4SJoerg Wunsch /* 2962c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 2972c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 2982c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 2992c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 300245f17d4SJoerg Wunsch */ 301245f17d4SJoerg Wunsch if (p->p_pid == 1) 302645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3036626c604SJulian Elischer else 304645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 305245f17d4SJoerg Wunsch } else 306645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes /* 309df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3102c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3112c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3122c42a146SMarcel Moolenaar * have to restart the process. 313df8bae1dSRodney W. Grimes */ 3142c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3152c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3162c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3172c42a146SMarcel Moolenaar /* never to be seen again */ 3182c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3192c42a146SMarcel Moolenaar if (sig != SIGCONT) 3202c42a146SMarcel Moolenaar /* easier in psignal */ 3212c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3222c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 323645682fdSLuoqi Chen } else { 3242c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3252c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3262c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3272c42a146SMarcel Moolenaar else 3282c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3292c42a146SMarcel Moolenaar } 330e8ebc08fSPeter Wemm #ifdef COMPAT_43 331645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 332645682fdSLuoqi Chen ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 333645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 334645682fdSLuoqi Chen else 335645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 336e8ebc08fSPeter Wemm #endif 337df8bae1dSRodney W. Grimes } 338628d2653SJohn Baldwin PROC_UNLOCK(p); 3392c42a146SMarcel Moolenaar return (0); 3402c42a146SMarcel Moolenaar } 3412c42a146SMarcel Moolenaar 3422c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3432c42a146SMarcel Moolenaar struct sigaction_args { 3442c42a146SMarcel Moolenaar int sig; 3452c42a146SMarcel Moolenaar struct sigaction *act; 3462c42a146SMarcel Moolenaar struct sigaction *oact; 3472c42a146SMarcel Moolenaar }; 3482c42a146SMarcel Moolenaar #endif 349fb99ab88SMatthew Dillon /* 350fb99ab88SMatthew Dillon * MPSAFE 351fb99ab88SMatthew Dillon */ 3522c42a146SMarcel Moolenaar /* ARGSUSED */ 3532c42a146SMarcel Moolenaar int 3542c42a146SMarcel Moolenaar sigaction(p, uap) 3552c42a146SMarcel Moolenaar struct proc *p; 3562c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3572c42a146SMarcel Moolenaar { 3582c42a146SMarcel Moolenaar struct sigaction act, oact; 3592c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3602c42a146SMarcel Moolenaar int error; 3612c42a146SMarcel Moolenaar 362fb99ab88SMatthew Dillon mtx_lock(&Giant); 363fb99ab88SMatthew Dillon 3646f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3656f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3662c42a146SMarcel Moolenaar if (actp) { 3676f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3682c42a146SMarcel Moolenaar if (error) 369fb99ab88SMatthew Dillon goto done2; 3702c42a146SMarcel Moolenaar } 371645682fdSLuoqi Chen error = do_sigaction(p, uap->sig, actp, oactp, 0); 3722c42a146SMarcel Moolenaar if (oactp && !error) { 3736f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 3742c42a146SMarcel Moolenaar } 375fb99ab88SMatthew Dillon done2: 376fb99ab88SMatthew Dillon mtx_unlock(&Giant); 3772c42a146SMarcel Moolenaar return (error); 3782c42a146SMarcel Moolenaar } 3792c42a146SMarcel Moolenaar 38031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 3812c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3822c42a146SMarcel Moolenaar struct osigaction_args { 3832c42a146SMarcel Moolenaar int signum; 3842c42a146SMarcel Moolenaar struct osigaction *nsa; 3852c42a146SMarcel Moolenaar struct osigaction *osa; 3862c42a146SMarcel Moolenaar }; 3872c42a146SMarcel Moolenaar #endif 388fb99ab88SMatthew Dillon /* 389fb99ab88SMatthew Dillon * MPSAFE 390fb99ab88SMatthew Dillon */ 3912c42a146SMarcel Moolenaar /* ARGSUSED */ 3922c42a146SMarcel Moolenaar int 3932c42a146SMarcel Moolenaar osigaction(p, uap) 3942c42a146SMarcel Moolenaar struct proc *p; 3952c42a146SMarcel Moolenaar register struct osigaction_args *uap; 3962c42a146SMarcel Moolenaar { 3972c42a146SMarcel Moolenaar struct osigaction sa; 3982c42a146SMarcel Moolenaar struct sigaction nsa, osa; 3992c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 4002c42a146SMarcel Moolenaar int error; 4012c42a146SMarcel Moolenaar 4026f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4036f841fb7SMarcel Moolenaar return (EINVAL); 404fb99ab88SMatthew Dillon 4056f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4066f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 407fb99ab88SMatthew Dillon 408fb99ab88SMatthew Dillon mtx_lock(&Giant); 409fb99ab88SMatthew Dillon 4102c42a146SMarcel Moolenaar if (nsap) { 4116f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4122c42a146SMarcel Moolenaar if (error) 413fb99ab88SMatthew Dillon goto done2; 4142c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4152c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4162c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4172c42a146SMarcel Moolenaar } 418645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 4192c42a146SMarcel Moolenaar if (osap && !error) { 4202c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4212c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4222c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4236f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4242c42a146SMarcel Moolenaar } 425fb99ab88SMatthew Dillon done2: 426fb99ab88SMatthew Dillon mtx_unlock(&Giant); 4272c42a146SMarcel Moolenaar return (error); 4282c42a146SMarcel Moolenaar } 42931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 433df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 434df8bae1dSRodney W. Grimes */ 435df8bae1dSRodney W. Grimes void 436df8bae1dSRodney W. Grimes siginit(p) 437df8bae1dSRodney W. Grimes struct proc *p; 438df8bae1dSRodney W. Grimes { 439df8bae1dSRodney W. Grimes register int i; 440df8bae1dSRodney W. Grimes 441628d2653SJohn Baldwin PROC_LOCK(p); 4422c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4432c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4442c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 445628d2653SJohn Baldwin PROC_UNLOCK(p); 446df8bae1dSRodney W. Grimes } 447df8bae1dSRodney W. Grimes 448df8bae1dSRodney W. Grimes /* 449df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 450df8bae1dSRodney W. Grimes */ 451df8bae1dSRodney W. Grimes void 452df8bae1dSRodney W. Grimes execsigs(p) 453df8bae1dSRodney W. Grimes register struct proc *p; 454df8bae1dSRodney W. Grimes { 455628d2653SJohn Baldwin register struct sigacts *ps; 4562c42a146SMarcel Moolenaar register int sig; 457df8bae1dSRodney W. Grimes 458df8bae1dSRodney W. Grimes /* 459df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 460df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 461df8bae1dSRodney W. Grimes * and are now ignored by default). 462df8bae1dSRodney W. Grimes */ 463628d2653SJohn Baldwin PROC_LOCK(p); 464628d2653SJohn Baldwin ps = p->p_sigacts; 4652c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4662c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4672c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4682c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4692c42a146SMarcel Moolenaar if (sig != SIGCONT) 4702c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4712c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 472df8bae1dSRodney W. Grimes } 4732c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 474df8bae1dSRodney W. Grimes } 475df8bae1dSRodney W. Grimes /* 476df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 477df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 478df8bae1dSRodney W. Grimes */ 479645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 480645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 481645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 4823b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 48380e907a1SPeter Wemm /* 48480e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 48580e907a1SPeter Wemm */ 486645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 487c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 488c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 489628d2653SJohn Baldwin PROC_UNLOCK(p); 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 493628d2653SJohn Baldwin * do_sigprocmask() 4947c8fdcbdSMatthew Dillon * 495628d2653SJohn Baldwin * Manipulate signal mask. 496df8bae1dSRodney W. Grimes */ 4972c42a146SMarcel Moolenaar static int 498645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 4992c42a146SMarcel Moolenaar struct proc *p; 5002c42a146SMarcel Moolenaar int how; 5012c42a146SMarcel Moolenaar sigset_t *set, *oset; 502645682fdSLuoqi Chen int old; 5032c42a146SMarcel Moolenaar { 5042c42a146SMarcel Moolenaar int error; 5052c42a146SMarcel Moolenaar 506628d2653SJohn Baldwin PROC_LOCK(p); 5072c42a146SMarcel Moolenaar if (oset != NULL) 5082c42a146SMarcel Moolenaar *oset = p->p_sigmask; 5092c42a146SMarcel Moolenaar 5102c42a146SMarcel Moolenaar error = 0; 5112c42a146SMarcel Moolenaar if (set != NULL) { 5122c42a146SMarcel Moolenaar switch (how) { 5132c42a146SMarcel Moolenaar case SIG_BLOCK: 514645682fdSLuoqi Chen SIG_CANTMASK(*set); 5152c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 5162c42a146SMarcel Moolenaar break; 5172c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5182c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 5192c42a146SMarcel Moolenaar break; 5202c42a146SMarcel Moolenaar case SIG_SETMASK: 521645682fdSLuoqi Chen SIG_CANTMASK(*set); 522645682fdSLuoqi Chen if (old) 523645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 524645682fdSLuoqi Chen else 5252c42a146SMarcel Moolenaar p->p_sigmask = *set; 5262c42a146SMarcel Moolenaar break; 5272c42a146SMarcel Moolenaar default: 5282c42a146SMarcel Moolenaar error = EINVAL; 5292c42a146SMarcel Moolenaar break; 5302c42a146SMarcel Moolenaar } 5312c42a146SMarcel Moolenaar } 532628d2653SJohn Baldwin PROC_UNLOCK(p); 5332c42a146SMarcel Moolenaar return (error); 5342c42a146SMarcel Moolenaar } 5352c42a146SMarcel Moolenaar 5367c8fdcbdSMatthew Dillon /* 5377c8fdcbdSMatthew Dillon * sigprocmask() - MP SAFE 5387c8fdcbdSMatthew Dillon */ 5397c8fdcbdSMatthew Dillon 540d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 541df8bae1dSRodney W. Grimes struct sigprocmask_args { 542df8bae1dSRodney W. Grimes int how; 5432c42a146SMarcel Moolenaar const sigset_t *set; 5442c42a146SMarcel Moolenaar sigset_t *oset; 545df8bae1dSRodney W. Grimes }; 546d2d3e875SBruce Evans #endif 54726f9a767SRodney W. Grimes int 548cb226aaaSPoul-Henning Kamp sigprocmask(p, uap) 549df8bae1dSRodney W. Grimes register struct proc *p; 550df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 551df8bae1dSRodney W. Grimes { 5522c42a146SMarcel Moolenaar sigset_t set, oset; 5532c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5542c42a146SMarcel Moolenaar int error; 555df8bae1dSRodney W. Grimes 5566f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5576f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5582c42a146SMarcel Moolenaar if (setp) { 5596f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5602c42a146SMarcel Moolenaar if (error) 5612c42a146SMarcel Moolenaar return (error); 562df8bae1dSRodney W. Grimes } 563645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5642c42a146SMarcel Moolenaar if (osetp && !error) { 5656f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5662c42a146SMarcel Moolenaar } 5672c42a146SMarcel Moolenaar return (error); 5682c42a146SMarcel Moolenaar } 5692c42a146SMarcel Moolenaar 57031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5717c8fdcbdSMatthew Dillon /* 5727c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 5737c8fdcbdSMatthew Dillon */ 5742c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5752c42a146SMarcel Moolenaar struct osigprocmask_args { 5762c42a146SMarcel Moolenaar int how; 5772c42a146SMarcel Moolenaar osigset_t mask; 5782c42a146SMarcel Moolenaar }; 5792c42a146SMarcel Moolenaar #endif 5802c42a146SMarcel Moolenaar int 5812c42a146SMarcel Moolenaar osigprocmask(p, uap) 5822c42a146SMarcel Moolenaar register struct proc *p; 5832c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 5842c42a146SMarcel Moolenaar { 5852c42a146SMarcel Moolenaar sigset_t set, oset; 5862c42a146SMarcel Moolenaar int error; 5872c42a146SMarcel Moolenaar 5882c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 589645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 5902c42a146SMarcel Moolenaar SIG2OSIG(oset, p->p_retval[0]); 591df8bae1dSRodney W. Grimes return (error); 592df8bae1dSRodney W. Grimes } 59331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 594df8bae1dSRodney W. Grimes 595d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 596df8bae1dSRodney W. Grimes struct sigpending_args { 5972c42a146SMarcel Moolenaar sigset_t *set; 598df8bae1dSRodney W. Grimes }; 599d2d3e875SBruce Evans #endif 600fb99ab88SMatthew Dillon /* 601fb99ab88SMatthew Dillon * MPSAFE 602fb99ab88SMatthew Dillon */ 603df8bae1dSRodney W. Grimes /* ARGSUSED */ 60426f9a767SRodney W. Grimes int 605cb226aaaSPoul-Henning Kamp sigpending(p, uap) 606df8bae1dSRodney W. Grimes struct proc *p; 607df8bae1dSRodney W. Grimes struct sigpending_args *uap; 608df8bae1dSRodney W. Grimes { 609628d2653SJohn Baldwin sigset_t siglist; 610fb99ab88SMatthew Dillon int error; 611df8bae1dSRodney W. Grimes 612fb99ab88SMatthew Dillon mtx_lock(&Giant); 613628d2653SJohn Baldwin PROC_LOCK(p); 614628d2653SJohn Baldwin siglist = p->p_siglist; 615628d2653SJohn Baldwin PROC_UNLOCK(p); 616fb99ab88SMatthew Dillon mtx_unlock(&Giant); 617fb99ab88SMatthew Dillon error = copyout(&siglist, uap->set, sizeof(sigset_t)); 618fb99ab88SMatthew Dillon return(error); 6192c42a146SMarcel Moolenaar } 6202c42a146SMarcel Moolenaar 62131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6222c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6232c42a146SMarcel Moolenaar struct osigpending_args { 6242c42a146SMarcel Moolenaar int dummy; 6252c42a146SMarcel Moolenaar }; 6262c42a146SMarcel Moolenaar #endif 627fb99ab88SMatthew Dillon /* 628fb99ab88SMatthew Dillon * MPSAFE 629fb99ab88SMatthew Dillon */ 6302c42a146SMarcel Moolenaar /* ARGSUSED */ 6312c42a146SMarcel Moolenaar int 6322c42a146SMarcel Moolenaar osigpending(p, uap) 6332c42a146SMarcel Moolenaar struct proc *p; 6342c42a146SMarcel Moolenaar struct osigpending_args *uap; 6352c42a146SMarcel Moolenaar { 636fb99ab88SMatthew Dillon mtx_lock(&Giant); 637628d2653SJohn Baldwin PROC_LOCK(p); 6382c42a146SMarcel Moolenaar SIG2OSIG(p->p_siglist, p->p_retval[0]); 639628d2653SJohn Baldwin PROC_UNLOCK(p); 640fb99ab88SMatthew Dillon mtx_unlock(&Giant); 641df8bae1dSRodney W. Grimes return (0); 642df8bae1dSRodney W. Grimes } 64331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 648df8bae1dSRodney W. Grimes */ 649d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 650df8bae1dSRodney W. Grimes struct osigvec_args { 651df8bae1dSRodney W. Grimes int signum; 652df8bae1dSRodney W. Grimes struct sigvec *nsv; 653df8bae1dSRodney W. Grimes struct sigvec *osv; 654df8bae1dSRodney W. Grimes }; 655d2d3e875SBruce Evans #endif 656fb99ab88SMatthew Dillon /* 657fb99ab88SMatthew Dillon * MPSAFE 658fb99ab88SMatthew Dillon */ 659df8bae1dSRodney W. Grimes /* ARGSUSED */ 66026f9a767SRodney W. Grimes int 661cb226aaaSPoul-Henning Kamp osigvec(p, uap) 662df8bae1dSRodney W. Grimes struct proc *p; 663df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 664df8bae1dSRodney W. Grimes { 665df8bae1dSRodney W. Grimes struct sigvec vec; 6662c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6672c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 6682c42a146SMarcel Moolenaar int error; 669df8bae1dSRodney W. Grimes 6706f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 6716f841fb7SMarcel Moolenaar return (EINVAL); 6726f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 6736f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 6742c42a146SMarcel Moolenaar if (nsap) { 6756f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 6762c42a146SMarcel Moolenaar if (error) 677df8bae1dSRodney W. Grimes return (error); 6782c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 6792c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 6802c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 6812c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 682df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 6832c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 684df8bae1dSRodney W. Grimes #endif 685df8bae1dSRodney W. Grimes } 686fb99ab88SMatthew Dillon mtx_lock(&Giant); 687645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 688fb99ab88SMatthew Dillon mtx_unlock(&Giant); 6892c42a146SMarcel Moolenaar if (osap && !error) { 6902c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 6912c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 6922c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 6932c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 6942c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 6952c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 6962c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 6972c42a146SMarcel Moolenaar #endif 6986f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 6992c42a146SMarcel Moolenaar } 7002c42a146SMarcel Moolenaar return (error); 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes 703d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 704df8bae1dSRodney W. Grimes struct osigblock_args { 705df8bae1dSRodney W. Grimes int mask; 706df8bae1dSRodney W. Grimes }; 707d2d3e875SBruce Evans #endif 708fb99ab88SMatthew Dillon /* 709fb99ab88SMatthew Dillon * MPSAFE 710fb99ab88SMatthew Dillon */ 71126f9a767SRodney W. Grimes int 712cb226aaaSPoul-Henning Kamp osigblock(p, uap) 713df8bae1dSRodney W. Grimes register struct proc *p; 714df8bae1dSRodney W. Grimes struct osigblock_args *uap; 715df8bae1dSRodney W. Grimes { 7162c42a146SMarcel Moolenaar sigset_t set; 717df8bae1dSRodney W. Grimes 7182c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7192c42a146SMarcel Moolenaar SIG_CANTMASK(set); 720fb99ab88SMatthew Dillon mtx_lock(&Giant); 721628d2653SJohn Baldwin PROC_LOCK(p); 7222c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 7232c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 724628d2653SJohn Baldwin PROC_UNLOCK(p); 725fb99ab88SMatthew Dillon mtx_unlock(&Giant); 726df8bae1dSRodney W. Grimes return (0); 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes 729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 730df8bae1dSRodney W. Grimes struct osigsetmask_args { 731df8bae1dSRodney W. Grimes int mask; 732df8bae1dSRodney W. Grimes }; 733d2d3e875SBruce Evans #endif 734fb99ab88SMatthew Dillon /* 735fb99ab88SMatthew Dillon * MPSAFE 736fb99ab88SMatthew Dillon */ 73726f9a767SRodney W. Grimes int 738cb226aaaSPoul-Henning Kamp osigsetmask(p, uap) 739df8bae1dSRodney W. Grimes struct proc *p; 740df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 741df8bae1dSRodney W. Grimes { 7422c42a146SMarcel Moolenaar sigset_t set; 743df8bae1dSRodney W. Grimes 7442c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7452c42a146SMarcel Moolenaar SIG_CANTMASK(set); 746fb99ab88SMatthew Dillon mtx_lock(&Giant); 747628d2653SJohn Baldwin PROC_LOCK(p); 7482c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 749645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 750628d2653SJohn Baldwin PROC_UNLOCK(p); 751fb99ab88SMatthew Dillon mtx_unlock(&Giant); 752df8bae1dSRodney W. Grimes return (0); 753df8bae1dSRodney W. Grimes } 754df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 755df8bae1dSRodney W. Grimes 756df8bae1dSRodney W. Grimes /* 757df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 758df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 759df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 760df8bae1dSRodney W. Grimes */ 761d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 762df8bae1dSRodney W. Grimes struct sigsuspend_args { 7632c42a146SMarcel Moolenaar const sigset_t *sigmask; 764df8bae1dSRodney W. Grimes }; 765d2d3e875SBruce Evans #endif 766fb99ab88SMatthew Dillon /* 767fb99ab88SMatthew Dillon * MPSAFE 768fb99ab88SMatthew Dillon */ 769df8bae1dSRodney W. Grimes /* ARGSUSED */ 77026f9a767SRodney W. Grimes int 771cb226aaaSPoul-Henning Kamp sigsuspend(p, uap) 772df8bae1dSRodney W. Grimes register struct proc *p; 773df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 774df8bae1dSRodney W. Grimes { 7752c42a146SMarcel Moolenaar sigset_t mask; 776628d2653SJohn Baldwin register struct sigacts *ps; 7772c42a146SMarcel Moolenaar int error; 7782c42a146SMarcel Moolenaar 7796f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 7802c42a146SMarcel Moolenaar if (error) 7812c42a146SMarcel Moolenaar return (error); 782df8bae1dSRodney W. Grimes 783df8bae1dSRodney W. Grimes /* 784645682fdSLuoqi Chen * When returning from sigsuspend, we want 785df8bae1dSRodney W. Grimes * the old mask to be restored after the 786df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 787df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 788df8bae1dSRodney W. Grimes * to indicate this. 789df8bae1dSRodney W. Grimes */ 790fb99ab88SMatthew Dillon mtx_lock(&Giant); 791628d2653SJohn Baldwin PROC_LOCK(p); 792628d2653SJohn Baldwin ps = p->p_sigacts; 7936626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 794645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 795645682fdSLuoqi Chen 796645682fdSLuoqi Chen SIG_CANTMASK(mask); 7972c42a146SMarcel Moolenaar p->p_sigmask = mask; 798628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 7992c42a146SMarcel Moolenaar /* void */; 800628d2653SJohn Baldwin PROC_UNLOCK(p); 801fb99ab88SMatthew Dillon mtx_unlock(&Giant); 8022c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 8032c42a146SMarcel Moolenaar return (EINTR); 8042c42a146SMarcel Moolenaar } 8052c42a146SMarcel Moolenaar 80631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8072c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8082c42a146SMarcel Moolenaar struct osigsuspend_args { 8092c42a146SMarcel Moolenaar osigset_t mask; 8102c42a146SMarcel Moolenaar }; 8112c42a146SMarcel Moolenaar #endif 812fb99ab88SMatthew Dillon /* 813fb99ab88SMatthew Dillon * MPSAFE 814fb99ab88SMatthew Dillon */ 8152c42a146SMarcel Moolenaar /* ARGSUSED */ 8162c42a146SMarcel Moolenaar int 8172c42a146SMarcel Moolenaar osigsuspend(p, uap) 8182c42a146SMarcel Moolenaar register struct proc *p; 8192c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 8202c42a146SMarcel Moolenaar { 821645682fdSLuoqi Chen sigset_t mask; 822628d2653SJohn Baldwin register struct sigacts *ps; 8232c42a146SMarcel Moolenaar 824fb99ab88SMatthew Dillon mtx_lock(&Giant); 825628d2653SJohn Baldwin PROC_LOCK(p); 826628d2653SJohn Baldwin ps = p->p_sigacts; 8272c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 828645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 829645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 830645682fdSLuoqi Chen SIG_CANTMASK(mask); 831645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 832628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 833df8bae1dSRodney W. Grimes /* void */; 834628d2653SJohn Baldwin PROC_UNLOCK(p); 835fb99ab88SMatthew Dillon mtx_unlock(&Giant); 836df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 837df8bae1dSRodney W. Grimes return (EINTR); 838df8bae1dSRodney W. Grimes } 83931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 840df8bae1dSRodney W. Grimes 841df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 842d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 843df8bae1dSRodney W. Grimes struct osigstack_args { 844df8bae1dSRodney W. Grimes struct sigstack *nss; 845df8bae1dSRodney W. Grimes struct sigstack *oss; 846df8bae1dSRodney W. Grimes }; 847d2d3e875SBruce Evans #endif 848fb99ab88SMatthew Dillon /* 849fb99ab88SMatthew Dillon * MPSAFE 850fb99ab88SMatthew Dillon */ 851df8bae1dSRodney W. Grimes /* ARGSUSED */ 85226f9a767SRodney W. Grimes int 853cb226aaaSPoul-Henning Kamp osigstack(p, uap) 854df8bae1dSRodney W. Grimes struct proc *p; 855df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 856df8bae1dSRodney W. Grimes { 857df8bae1dSRodney W. Grimes struct sigstack ss; 858fb99ab88SMatthew Dillon int error = 0; 859fb99ab88SMatthew Dillon 860fb99ab88SMatthew Dillon mtx_lock(&Giant); 861df8bae1dSRodney W. Grimes 862d034d459SMarcel Moolenaar if (uap->oss != NULL) { 863628d2653SJohn Baldwin PROC_LOCK(p); 864645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 865d034d459SMarcel Moolenaar ss.ss_onstack = sigonstack(cpu_getstack(p)); 866628d2653SJohn Baldwin PROC_UNLOCK(p); 867d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 868d034d459SMarcel Moolenaar if (error) 869fb99ab88SMatthew Dillon goto done2; 870d034d459SMarcel Moolenaar } 871d034d459SMarcel Moolenaar 872d034d459SMarcel Moolenaar if (uap->nss != NULL) { 873d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 874fb99ab88SMatthew Dillon goto done2; 875628d2653SJohn Baldwin PROC_LOCK(p); 876645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 877645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 878645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 879645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 880628d2653SJohn Baldwin PROC_UNLOCK(p); 881df8bae1dSRodney W. Grimes } 882fb99ab88SMatthew Dillon done2: 883fb99ab88SMatthew Dillon mtx_unlock(&Giant); 884fb99ab88SMatthew Dillon return (error); 885df8bae1dSRodney W. Grimes } 886df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 887df8bae1dSRodney W. Grimes 888d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 889df8bae1dSRodney W. Grimes struct sigaltstack_args { 8902c42a146SMarcel Moolenaar stack_t *ss; 8912c42a146SMarcel Moolenaar stack_t *oss; 892df8bae1dSRodney W. Grimes }; 893d2d3e875SBruce Evans #endif 894fb99ab88SMatthew Dillon /* 895fb99ab88SMatthew Dillon * MPSAFE 896fb99ab88SMatthew Dillon */ 897df8bae1dSRodney W. Grimes /* ARGSUSED */ 89826f9a767SRodney W. Grimes int 899cb226aaaSPoul-Henning Kamp sigaltstack(p, uap) 900df8bae1dSRodney W. Grimes struct proc *p; 901df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 902df8bae1dSRodney W. Grimes { 9032c42a146SMarcel Moolenaar stack_t ss; 904fb99ab88SMatthew Dillon int oonstack; 905fb99ab88SMatthew Dillon int error = 0; 906fb99ab88SMatthew Dillon 907fb99ab88SMatthew Dillon mtx_lock(&Giant); 908df8bae1dSRodney W. Grimes 909d034d459SMarcel Moolenaar oonstack = sigonstack(cpu_getstack(p)); 910d034d459SMarcel Moolenaar 911d034d459SMarcel Moolenaar if (uap->oss != NULL) { 912628d2653SJohn Baldwin PROC_LOCK(p); 913d034d459SMarcel Moolenaar ss = p->p_sigstk; 914d034d459SMarcel Moolenaar ss.ss_flags = (p->p_flag & P_ALTSTACK) 915d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 916628d2653SJohn Baldwin PROC_UNLOCK(p); 917d034d459SMarcel Moolenaar if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 918fb99ab88SMatthew Dillon goto done2; 919df8bae1dSRodney W. Grimes } 920d034d459SMarcel Moolenaar 921d034d459SMarcel Moolenaar if (uap->ss != NULL) { 922fb99ab88SMatthew Dillon if (oonstack) { 923fb99ab88SMatthew Dillon error = EPERM; 924fb99ab88SMatthew Dillon goto done2; 925fb99ab88SMatthew Dillon } 926d034d459SMarcel Moolenaar if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 927fb99ab88SMatthew Dillon goto done2; 928fb99ab88SMatthew Dillon if ((ss.ss_flags & ~SS_DISABLE) != 0) { 929fb99ab88SMatthew Dillon error = EINVAL; 930fb99ab88SMatthew Dillon goto done2; 931fb99ab88SMatthew Dillon } 932d034d459SMarcel Moolenaar if (!(ss.ss_flags & SS_DISABLE)) { 933fb99ab88SMatthew Dillon if (ss.ss_size < p->p_sysent->sv_minsigstksz) { 934fb99ab88SMatthew Dillon error = ENOMEM; 935fb99ab88SMatthew Dillon goto done2; 936fb99ab88SMatthew Dillon } 937628d2653SJohn Baldwin PROC_LOCK(p); 938645682fdSLuoqi Chen p->p_sigstk = ss; 939d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 940628d2653SJohn Baldwin PROC_UNLOCK(p); 941628d2653SJohn Baldwin } else { 942628d2653SJohn Baldwin PROC_LOCK(p); 943d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 944628d2653SJohn Baldwin PROC_UNLOCK(p); 945628d2653SJohn Baldwin } 946d034d459SMarcel Moolenaar } 947fb99ab88SMatthew Dillon done2: 948fb99ab88SMatthew Dillon mtx_unlock(&Giant); 949fb99ab88SMatthew Dillon return (error); 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes 952d93f860cSPoul-Henning Kamp /* 953d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 954d93f860cSPoul-Henning Kamp * cp is calling process. 955d93f860cSPoul-Henning Kamp */ 956d93f860cSPoul-Henning Kamp int 9572c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all) 958d93f860cSPoul-Henning Kamp register struct proc *cp; 9592c42a146SMarcel Moolenaar int sig, pgid, all; 960d93f860cSPoul-Henning Kamp { 961d93f860cSPoul-Henning Kamp register struct proc *p; 962d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 963d93f860cSPoul-Henning Kamp int nfound = 0; 964d93f860cSPoul-Henning Kamp 965553629ebSJake Burkholder if (all) { 966d93f860cSPoul-Henning Kamp /* 967d93f860cSPoul-Henning Kamp * broadcast 968d93f860cSPoul-Henning Kamp */ 9691005a129SJohn Baldwin sx_slock(&allproc_lock); 9702e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 971628d2653SJohn Baldwin PROC_LOCK(p); 972628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) { 973628d2653SJohn Baldwin PROC_UNLOCK(p); 974628d2653SJohn Baldwin continue; 975628d2653SJohn Baldwin } 97633a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 977d93f860cSPoul-Henning Kamp nfound++; 97833a9ed9dSJohn Baldwin if (sig) 9792c42a146SMarcel Moolenaar psignal(p, sig); 980628d2653SJohn Baldwin } 98133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 982d93f860cSPoul-Henning Kamp } 9831005a129SJohn Baldwin sx_sunlock(&allproc_lock); 984553629ebSJake Burkholder } else { 985d93f860cSPoul-Henning Kamp if (pgid == 0) 986d93f860cSPoul-Henning Kamp /* 987d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 988d93f860cSPoul-Henning Kamp */ 989d93f860cSPoul-Henning Kamp pgrp = cp->p_pgrp; 990d93f860cSPoul-Henning Kamp else { 991d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 992d93f860cSPoul-Henning Kamp if (pgrp == NULL) 993d93f860cSPoul-Henning Kamp return (ESRCH); 994d93f860cSPoul-Henning Kamp } 9952e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 996628d2653SJohn Baldwin PROC_LOCK(p); 997628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 998628d2653SJohn Baldwin PROC_UNLOCK(p); 999628d2653SJohn Baldwin continue; 1000628d2653SJohn Baldwin } 1001628d2653SJohn Baldwin mtx_lock_spin(&sched_lock); 1002628d2653SJohn Baldwin if (p->p_stat == SZOMB) { 1003628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 100433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1005628d2653SJohn Baldwin continue; 1006628d2653SJohn Baldwin } 1007628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 100833a9ed9dSJohn Baldwin if (p_cansignal(cp, p, sig) == 0) { 1009d93f860cSPoul-Henning Kamp nfound++; 101033a9ed9dSJohn Baldwin if (sig) 10112c42a146SMarcel Moolenaar psignal(p, sig); 1012628d2653SJohn Baldwin } 101333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1014d93f860cSPoul-Henning Kamp } 1015d93f860cSPoul-Henning Kamp } 1016d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1017d93f860cSPoul-Henning Kamp } 1018d93f860cSPoul-Henning Kamp 1019d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1020df8bae1dSRodney W. Grimes struct kill_args { 1021df8bae1dSRodney W. Grimes int pid; 1022df8bae1dSRodney W. Grimes int signum; 1023df8bae1dSRodney W. Grimes }; 1024d2d3e875SBruce Evans #endif 1025fb99ab88SMatthew Dillon /* 1026fb99ab88SMatthew Dillon * MPSAFE 1027fb99ab88SMatthew Dillon */ 1028df8bae1dSRodney W. Grimes /* ARGSUSED */ 102926f9a767SRodney W. Grimes int 1030cb226aaaSPoul-Henning Kamp kill(cp, uap) 1031df8bae1dSRodney W. Grimes register struct proc *cp; 1032df8bae1dSRodney W. Grimes register struct kill_args *uap; 1033df8bae1dSRodney W. Grimes { 1034df8bae1dSRodney W. Grimes register struct proc *p; 1035fb99ab88SMatthew Dillon int error = 0; 1036df8bae1dSRodney W. Grimes 10372c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 1038df8bae1dSRodney W. Grimes return (EINVAL); 1039fb99ab88SMatthew Dillon 1040fb99ab88SMatthew Dillon mtx_lock(&Giant); 1041df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1042df8bae1dSRodney W. Grimes /* kill single process */ 1043fb99ab88SMatthew Dillon if ((p = pfind(uap->pid)) == NULL) { 1044fb99ab88SMatthew Dillon error = ESRCH; 1045fb99ab88SMatthew Dillon } else if (p_cansignal(cp, p, uap->signum)) { 104633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1047fb99ab88SMatthew Dillon error = EPERM; 1048fb99ab88SMatthew Dillon } else { 104933a9ed9dSJohn Baldwin if (uap->signum) 1050df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1051628d2653SJohn Baldwin PROC_UNLOCK(p); 1052fb99ab88SMatthew Dillon error = 0; 1053df8bae1dSRodney W. Grimes } 1054fb99ab88SMatthew Dillon } else { 1055df8bae1dSRodney W. Grimes switch (uap->pid) { 1056df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 1057fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, 0, 1); 1058fb99ab88SMatthew Dillon break; 1059df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 1060fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, 0, 0); 1061fb99ab88SMatthew Dillon break; 1062df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 1063fb99ab88SMatthew Dillon error = killpg1(cp, uap->signum, -uap->pid, 0); 1064fb99ab88SMatthew Dillon break; 1065df8bae1dSRodney W. Grimes } 1066fb99ab88SMatthew Dillon } 1067fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1068fb99ab88SMatthew Dillon return(error); 1069df8bae1dSRodney W. Grimes } 1070df8bae1dSRodney W. Grimes 1071df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1072d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1073df8bae1dSRodney W. Grimes struct okillpg_args { 1074df8bae1dSRodney W. Grimes int pgid; 1075df8bae1dSRodney W. Grimes int signum; 1076df8bae1dSRodney W. Grimes }; 1077d2d3e875SBruce Evans #endif 1078fb99ab88SMatthew Dillon /* 1079fb99ab88SMatthew Dillon * MPSAFE 1080fb99ab88SMatthew Dillon */ 1081df8bae1dSRodney W. Grimes /* ARGSUSED */ 108226f9a767SRodney W. Grimes int 1083cb226aaaSPoul-Henning Kamp okillpg(p, uap) 1084df8bae1dSRodney W. Grimes struct proc *p; 1085df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1086df8bae1dSRodney W. Grimes { 1087fb99ab88SMatthew Dillon int error; 1088df8bae1dSRodney W. Grimes 10892c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 1090df8bae1dSRodney W. Grimes return (EINVAL); 1091fb99ab88SMatthew Dillon mtx_lock(&Giant); 1092fb99ab88SMatthew Dillon error = killpg1(p, uap->signum, uap->pgid, 0); 1093fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1094fb99ab88SMatthew Dillon return (error); 1095df8bae1dSRodney W. Grimes } 1096df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1097df8bae1dSRodney W. Grimes 1098df8bae1dSRodney W. Grimes /* 1099df8bae1dSRodney W. Grimes * Send a signal to a process group. 1100df8bae1dSRodney W. Grimes */ 1101df8bae1dSRodney W. Grimes void 11022c42a146SMarcel Moolenaar gsignal(pgid, sig) 11032c42a146SMarcel Moolenaar int pgid, sig; 1104df8bae1dSRodney W. Grimes { 1105df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1106df8bae1dSRodney W. Grimes 1107df8bae1dSRodney W. Grimes if (pgid && (pgrp = pgfind(pgid))) 11082c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes 1111df8bae1dSRodney W. Grimes /* 1112df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1113df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1114df8bae1dSRodney W. Grimes */ 1115df8bae1dSRodney W. Grimes void 11162c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1117df8bae1dSRodney W. Grimes struct pgrp *pgrp; 11182c42a146SMarcel Moolenaar int sig, checkctty; 1119df8bae1dSRodney W. Grimes { 1120df8bae1dSRodney W. Grimes register struct proc *p; 1121df8bae1dSRodney W. Grimes 1122628d2653SJohn Baldwin if (pgrp) { 1123628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1124628d2653SJohn Baldwin PROC_LOCK(p); 1125df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 11262c42a146SMarcel Moolenaar psignal(p, sig); 1127628d2653SJohn Baldwin PROC_UNLOCK(p); 1128628d2653SJohn Baldwin } 1129628d2653SJohn Baldwin } 1130df8bae1dSRodney W. Grimes } 1131df8bae1dSRodney W. Grimes 1132df8bae1dSRodney W. Grimes /* 1133df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1134df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1135df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1136356861dbSMatthew Dillon * 1137356861dbSMatthew Dillon * MPSAFE 1138df8bae1dSRodney W. Grimes */ 1139df8bae1dSRodney W. Grimes void 11402c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1141df8bae1dSRodney W. Grimes struct proc *p; 11422c42a146SMarcel Moolenaar register int sig; 11438674077aSJeffrey Hsu u_long code; 1144df8bae1dSRodney W. Grimes { 1145df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1146df8bae1dSRodney W. Grimes 1147356861dbSMatthew Dillon mtx_lock(&Giant); 1148628d2653SJohn Baldwin PROC_LOCK(p); 11492c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1150d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1151df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1152df8bae1dSRodney W. Grimes #ifdef KTRACE 1153df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 11542c42a146SMarcel Moolenaar ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 11552c42a146SMarcel Moolenaar &p->p_sigmask, code); 1156df8bae1dSRodney W. Grimes #endif 11572c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 11582c42a146SMarcel Moolenaar &p->p_sigmask, code); 11592c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 11602c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 11612c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 11622c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1163289ccde0SPeter Wemm /* 11642c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1165289ccde0SPeter Wemm */ 11662c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 11672c42a146SMarcel Moolenaar if (sig != SIGCONT && 11682c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 11692c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 11702c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1171dedc04feSPeter Wemm } 11726f841fb7SMarcel Moolenaar } else { 11736626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 11742c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 11752c42a146SMarcel Moolenaar psignal(p, sig); 1176df8bae1dSRodney W. Grimes } 1177628d2653SJohn Baldwin PROC_UNLOCK(p); 1178356861dbSMatthew Dillon mtx_unlock(&Giant); 1179df8bae1dSRodney W. Grimes } 1180df8bae1dSRodney W. Grimes 1181df8bae1dSRodney W. Grimes /* 1182df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1183df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1184df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1185df8bae1dSRodney W. Grimes * 1186df8bae1dSRodney W. Grimes * Exceptions: 1187df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1188df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1189df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1190df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1191df8bae1dSRodney W. Grimes * 1192df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1193df8bae1dSRodney W. Grimes */ 1194df8bae1dSRodney W. Grimes void 11952c42a146SMarcel Moolenaar psignal(p, sig) 1196df8bae1dSRodney W. Grimes register struct proc *p; 11972c42a146SMarcel Moolenaar register int sig; 1198df8bae1dSRodney W. Grimes { 119940447cd4SJohn Baldwin register int prop; 1200df8bae1dSRodney W. Grimes register sig_t action; 1201df8bae1dSRodney W. Grimes 12022c42a146SMarcel Moolenaar if (sig > _SIG_MAXSIG || sig <= 0) { 12032c42a146SMarcel Moolenaar printf("psignal: signal %d\n", sig); 1204df8bae1dSRodney W. Grimes panic("psignal signal number"); 12052a024a2bSSean Eric Fagan } 12062c42a146SMarcel Moolenaar 1207628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1208cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1209cb679c38SJonathan Lemon 12102c42a146SMarcel Moolenaar prop = sigprop(sig); 1211df8bae1dSRodney W. Grimes 1212df8bae1dSRodney W. Grimes /* 12132a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 12142a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 12152a024a2bSSean Eric Fagan * a chance, as well. 1216df8bae1dSRodney W. Grimes */ 12172a024a2bSSean Eric Fagan if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1218df8bae1dSRodney W. Grimes action = SIG_DFL; 1219df8bae1dSRodney W. Grimes else { 1220df8bae1dSRodney W. Grimes /* 1221df8bae1dSRodney W. Grimes * If the signal is being ignored, 1222df8bae1dSRodney W. Grimes * then we forget about it immediately. 1223df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1224df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1225df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1226df8bae1dSRodney W. Grimes */ 1227628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1228df8bae1dSRodney W. Grimes return; 12292c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1230df8bae1dSRodney W. Grimes action = SIG_HOLD; 12312c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1232df8bae1dSRodney W. Grimes action = SIG_CATCH; 1233df8bae1dSRodney W. Grimes else 1234df8bae1dSRodney W. Grimes action = SIG_DFL; 1235df8bae1dSRodney W. Grimes } 1236df8bae1dSRodney W. Grimes 12379ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1238df8bae1dSRodney W. Grimes if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1239df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1240df8bae1dSRodney W. Grimes p->p_nice = NZERO; 12419ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1242df8bae1dSRodney W. Grimes 1243df8bae1dSRodney W. Grimes if (prop & SA_CONT) 12442c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1245df8bae1dSRodney W. Grimes 1246df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1247df8bae1dSRodney W. Grimes /* 1248df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1249df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1250df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1251df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1252df8bae1dSRodney W. Grimes */ 1253df8bae1dSRodney W. Grimes if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1254628d2653SJohn Baldwin action == SIG_DFL) 1255df8bae1dSRodney W. Grimes return; 12562c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 1257df8bae1dSRodney W. Grimes } 12582c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1259df8bae1dSRodney W. Grimes 1260df8bae1dSRodney W. Grimes /* 1261df8bae1dSRodney W. Grimes * Defer further processing for signals which are held, 1262df8bae1dSRodney W. Grimes * except that stopped processes must be continued by SIGCONT. 1263df8bae1dSRodney W. Grimes */ 12649ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 12651c32c37cSJohn Baldwin if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) { 12669ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1267df8bae1dSRodney W. Grimes return; 12681c32c37cSJohn Baldwin } 1269df8bae1dSRodney W. Grimes switch (p->p_stat) { 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes case SSLEEP: 1272df8bae1dSRodney W. Grimes /* 1273df8bae1dSRodney W. Grimes * If process is sleeping uninterruptibly 1274df8bae1dSRodney W. Grimes * we can't interrupt the sleep... the signal will 1275df8bae1dSRodney W. Grimes * be noticed when the process returns through 1276df8bae1dSRodney W. Grimes * trap() or syscall(). 1277df8bae1dSRodney W. Grimes */ 127840447cd4SJohn Baldwin if ((p->p_sflag & PS_SINTR) == 0) { 12799ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1280df8bae1dSRodney W. Grimes goto out; 12813e6831f5SJohn Baldwin } 1282df8bae1dSRodney W. Grimes /* 1283df8bae1dSRodney W. Grimes * Process is sleeping and traced... make it runnable 1284df8bae1dSRodney W. Grimes * so it can discover the signal in issignal() and stop 1285df8bae1dSRodney W. Grimes * for the parent. 1286df8bae1dSRodney W. Grimes */ 1287df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED) 1288df8bae1dSRodney W. Grimes goto run; 12899ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1290df8bae1dSRodney W. Grimes /* 1291df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored) and process is 1292df8bae1dSRodney W. Grimes * asleep, we are finished; the process should not 1293df8bae1dSRodney W. Grimes * be awakened. 1294df8bae1dSRodney W. Grimes */ 1295df8bae1dSRodney W. Grimes if ((prop & SA_CONT) && action == SIG_DFL) { 12962c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1297df8bae1dSRodney W. Grimes goto out; 1298df8bae1dSRodney W. Grimes } 1299df8bae1dSRodney W. Grimes /* 1300df8bae1dSRodney W. Grimes * When a sleeping process receives a stop 1301df8bae1dSRodney W. Grimes * signal, process immediately if possible. 1302df8bae1dSRodney W. Grimes * All other (caught or default) signals 1303df8bae1dSRodney W. Grimes * cause the process to run. 1304df8bae1dSRodney W. Grimes */ 1305df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 13063e6831f5SJohn Baldwin if (action != SIG_DFL) 1307df8bae1dSRodney W. Grimes goto runfast; 1308df8bae1dSRodney W. Grimes /* 1309df8bae1dSRodney W. Grimes * If a child holding parent blocked, 1310df8bae1dSRodney W. Grimes * stopping could cause deadlock. 1311df8bae1dSRodney W. Grimes */ 1312df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 1313df8bae1dSRodney W. Grimes goto out; 13142c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 13152c42a146SMarcel Moolenaar p->p_xstat = sig; 1316628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1317628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1318df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1319628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1320628d2653SJohn Baldwin } 13215b3047d5SJohn Baldwin mtx_lock_spin(&sched_lock); 1322df8bae1dSRodney W. Grimes stop(p); 13235b3047d5SJohn Baldwin mtx_unlock_spin(&sched_lock); 1324df8bae1dSRodney W. Grimes goto out; 13253e6831f5SJohn Baldwin } else 1326df8bae1dSRodney W. Grimes goto runfast; 1327df8bae1dSRodney W. Grimes /* NOTREACHED */ 1328df8bae1dSRodney W. Grimes 1329df8bae1dSRodney W. Grimes case SSTOP: 13309ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1331df8bae1dSRodney W. Grimes /* 1332df8bae1dSRodney W. Grimes * If traced process is already stopped, 1333df8bae1dSRodney W. Grimes * then no further action is necessary. 1334df8bae1dSRodney W. Grimes */ 133540447cd4SJohn Baldwin if (p->p_flag & P_TRACED) 1336df8bae1dSRodney W. Grimes goto out; 1337df8bae1dSRodney W. Grimes 1338df8bae1dSRodney W. Grimes /* 1339df8bae1dSRodney W. Grimes * Kill signal always sets processes running. 1340df8bae1dSRodney W. Grimes */ 13412c42a146SMarcel Moolenaar if (sig == SIGKILL) 1342df8bae1dSRodney W. Grimes goto runfast; 1343df8bae1dSRodney W. Grimes 1344df8bae1dSRodney W. Grimes if (prop & SA_CONT) { 1345df8bae1dSRodney W. Grimes /* 1346df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored), we continue the 1347df8bae1dSRodney W. Grimes * process but don't leave the signal in p_siglist, as 1348df8bae1dSRodney W. Grimes * it has no further action. If SIGCONT is held, we 1349df8bae1dSRodney W. Grimes * continue the process and leave the signal in 1350df8bae1dSRodney W. Grimes * p_siglist. If the process catches SIGCONT, let it 1351df8bae1dSRodney W. Grimes * handle the signal itself. If it isn't waiting on 1352df8bae1dSRodney W. Grimes * an event, then it goes back to run state. 1353df8bae1dSRodney W. Grimes * Otherwise, process goes back to sleep state. 1354df8bae1dSRodney W. Grimes */ 1355df8bae1dSRodney W. Grimes if (action == SIG_DFL) 13562c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1357df8bae1dSRodney W. Grimes if (action == SIG_CATCH) 1358df8bae1dSRodney W. Grimes goto runfast; 13599ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 136040447cd4SJohn Baldwin if (p->p_wchan == NULL) 1361df8bae1dSRodney W. Grimes goto run; 1362df8bae1dSRodney W. Grimes p->p_stat = SSLEEP; 13639ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1364df8bae1dSRodney W. Grimes goto out; 1365df8bae1dSRodney W. Grimes } 1366df8bae1dSRodney W. Grimes 1367df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1368df8bae1dSRodney W. Grimes /* 1369df8bae1dSRodney W. Grimes * Already stopped, don't need to stop again. 1370df8bae1dSRodney W. Grimes * (If we did the shell could get confused.) 1371df8bae1dSRodney W. Grimes */ 13722c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1373df8bae1dSRodney W. Grimes goto out; 1374df8bae1dSRodney W. Grimes } 1375df8bae1dSRodney W. Grimes 1376df8bae1dSRodney W. Grimes /* 1377df8bae1dSRodney W. Grimes * If process is sleeping interruptibly, then simulate a 1378df8bae1dSRodney W. Grimes * wakeup so that when it is continued, it will be made 1379df8bae1dSRodney W. Grimes * runnable and can look at the signal. But don't make 1380df8bae1dSRodney W. Grimes * the process runnable, leave it stopped. 1381df8bae1dSRodney W. Grimes */ 13829ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 138340447cd4SJohn Baldwin if (p->p_wchan && p->p_sflag & PS_SINTR) { 138440447cd4SJohn Baldwin if (p->p_sflag & PS_CVWAITQ) 1385238510fcSJason Evans cv_waitq_remove(p); 1386238510fcSJason Evans else 1387df8bae1dSRodney W. Grimes unsleep(p); 1388238510fcSJason Evans } 13899ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1390df8bae1dSRodney W. Grimes goto out; 1391df8bae1dSRodney W. Grimes 1392df8bae1dSRodney W. Grimes default: 1393df8bae1dSRodney W. Grimes /* 1394df8bae1dSRodney W. Grimes * SRUN, SIDL, SZOMB do nothing with the signal, 1395df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1396df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1397df8bae1dSRodney W. Grimes */ 1398d2ef4060SBruce Evans if (p->p_stat == SRUN) { 1399df8bae1dSRodney W. Grimes signotify(p); 14003163861cSTor Egge #ifdef SMP 14013163861cSTor Egge forward_signal(p); 14023163861cSTor Egge #endif 14036caa8a15SJohn Baldwin } 14049ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1405df8bae1dSRodney W. Grimes goto out; 1406df8bae1dSRodney W. Grimes } 1407df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1408df8bae1dSRodney W. Grimes 1409df8bae1dSRodney W. Grimes runfast: 1410df8bae1dSRodney W. Grimes /* 1411df8bae1dSRodney W. Grimes * Raise priority to at least PUSER. 1412df8bae1dSRodney W. Grimes */ 14139ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1414d5a08a60SJake Burkholder if (p->p_pri.pri_level > PUSER) 1415d5a08a60SJake Burkholder p->p_pri.pri_level = PUSER; 1416df8bae1dSRodney W. Grimes run: 14173e6831f5SJohn Baldwin /* If we jump here, sched_lock has to be owned. */ 14183e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 1419df8bae1dSRodney W. Grimes setrunnable(p); 14209ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 14213e6831f5SJohn Baldwin out: 14223e6831f5SJohn Baldwin /* If we jump here, sched_lock should not be owned. */ 14233e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_NOTOWNED); 1424df8bae1dSRodney W. Grimes } 1425df8bae1dSRodney W. Grimes 1426df8bae1dSRodney W. Grimes /* 1427df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1428df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1429df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1430df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1431df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1432df8bae1dSRodney W. Grimes * by checking the pending signal masks in the CURSIG macro.) The normal call 1433df8bae1dSRodney W. Grimes * sequence is 1434df8bae1dSRodney W. Grimes * 14352c42a146SMarcel Moolenaar * while (sig = CURSIG(curproc)) 14362c42a146SMarcel Moolenaar * postsig(sig); 1437df8bae1dSRodney W. Grimes */ 143826f9a767SRodney W. Grimes int 1439df8bae1dSRodney W. Grimes issignal(p) 1440df8bae1dSRodney W. Grimes register struct proc *p; 1441df8bae1dSRodney W. Grimes { 14422c42a146SMarcel Moolenaar sigset_t mask; 14432c42a146SMarcel Moolenaar register int sig, prop; 1444df8bae1dSRodney W. Grimes 1445628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1446df8bae1dSRodney W. Grimes for (;;) { 14472a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 14482a024a2bSSean Eric Fagan 14492c42a146SMarcel Moolenaar mask = p->p_siglist; 14502c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1451df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 14522c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 14532c42a146SMarcel Moolenaar if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1454df8bae1dSRodney W. Grimes return (0); 14552c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 14562c42a146SMarcel Moolenaar prop = sigprop(sig); 14572a024a2bSSean Eric Fagan 1458628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 14592a024a2bSSean Eric Fagan 1460df8bae1dSRodney W. Grimes /* 1461df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1462df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1463df8bae1dSRodney W. Grimes */ 14642c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 14652c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1466df8bae1dSRodney W. Grimes continue; 1467df8bae1dSRodney W. Grimes } 1468df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1469df8bae1dSRodney W. Grimes /* 1470df8bae1dSRodney W. Grimes * If traced, always stop, and stay 1471df8bae1dSRodney W. Grimes * stopped until released by the parent. 1472df8bae1dSRodney W. Grimes */ 14732c42a146SMarcel Moolenaar p->p_xstat = sig; 1474628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1475df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1476628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1477df8bae1dSRodney W. Grimes do { 14789ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 14795b3047d5SJohn Baldwin stop(p); 1480628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 14817da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 14822ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1483df8bae1dSRodney W. Grimes mi_switch(); 14849ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 148520cdcc5bSJohn Baldwin PICKUP_GIANT(); 1486628d2653SJohn Baldwin PROC_LOCK(p); 14872a024a2bSSean Eric Fagan } while (!trace_req(p) 14882a024a2bSSean Eric Fagan && p->p_flag & P_TRACED); 1489df8bae1dSRodney W. Grimes 1490df8bae1dSRodney W. Grimes /* 1491df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1492df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1493df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1494df8bae1dSRodney W. Grimes */ 1495df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1496df8bae1dSRodney W. Grimes continue; 1497df8bae1dSRodney W. Grimes 1498df8bae1dSRodney W. Grimes /* 1499df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1500df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1501df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1502df8bae1dSRodney W. Grimes */ 15032c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 15042c42a146SMarcel Moolenaar sig = p->p_xstat; 15052c42a146SMarcel Moolenaar if (sig == 0) 1506df8bae1dSRodney W. Grimes continue; 1507df8bae1dSRodney W. Grimes 1508df8bae1dSRodney W. Grimes /* 1509df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1510df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1511df8bae1dSRodney W. Grimes */ 15122c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 15132c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1514df8bae1dSRodney W. Grimes continue; 1515df8bae1dSRodney W. Grimes } 1516df8bae1dSRodney W. Grimes 1517df8bae1dSRodney W. Grimes /* 1518df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1519df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1520df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1521df8bae1dSRodney W. Grimes */ 15222c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1523df8bae1dSRodney W. Grimes 15240b53fbe8SBruce Evans case (int)SIG_DFL: 1525df8bae1dSRodney W. Grimes /* 1526df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1527df8bae1dSRodney W. Grimes */ 1528df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1529df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1530df8bae1dSRodney W. Grimes /* 1531df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1532df8bae1dSRodney W. Grimes * in init? XXX 1533df8bae1dSRodney W. Grimes */ 1534d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 15352c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1536df8bae1dSRodney W. Grimes #endif 1537df8bae1dSRodney W. Grimes break; /* == ignore */ 1538df8bae1dSRodney W. Grimes } 1539df8bae1dSRodney W. Grimes /* 1540df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1541df8bae1dSRodney W. Grimes * with default action, stop here, 1542df8bae1dSRodney W. Grimes * then clear the signal. However, 1543df8bae1dSRodney W. Grimes * if process is member of an orphaned 1544df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1545df8bae1dSRodney W. Grimes */ 1546df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1547df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1548df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1549df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1550df8bae1dSRodney W. Grimes break; /* == ignore */ 15512c42a146SMarcel Moolenaar p->p_xstat = sig; 1552628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1553628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1554df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1555628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1556628d2653SJohn Baldwin } 15579ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 15585b3047d5SJohn Baldwin stop(p); 1559628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 15607da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 15612ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 1562df8bae1dSRodney W. Grimes mi_switch(); 15639ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 156420cdcc5bSJohn Baldwin PICKUP_GIANT(); 1565628d2653SJohn Baldwin PROC_LOCK(p); 1566df8bae1dSRodney W. Grimes break; 1567df8bae1dSRodney W. Grimes } else if (prop & SA_IGNORE) { 1568df8bae1dSRodney W. Grimes /* 1569df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1570df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1571df8bae1dSRodney W. Grimes */ 1572df8bae1dSRodney W. Grimes break; /* == ignore */ 1573df8bae1dSRodney W. Grimes } else 15742c42a146SMarcel Moolenaar return (sig); 1575df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1576df8bae1dSRodney W. Grimes 15770b53fbe8SBruce Evans case (int)SIG_IGN: 1578df8bae1dSRodney W. Grimes /* 1579df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1580df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1581df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1582df8bae1dSRodney W. Grimes */ 1583df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1584df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1585df8bae1dSRodney W. Grimes printf("issignal\n"); 1586df8bae1dSRodney W. Grimes break; /* == ignore */ 1587df8bae1dSRodney W. Grimes 1588df8bae1dSRodney W. Grimes default: 1589df8bae1dSRodney W. Grimes /* 1590df8bae1dSRodney W. Grimes * This signal has an action, let 1591df8bae1dSRodney W. Grimes * postsig() process it. 1592df8bae1dSRodney W. Grimes */ 15932c42a146SMarcel Moolenaar return (sig); 1594df8bae1dSRodney W. Grimes } 15952c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1596df8bae1dSRodney W. Grimes } 1597df8bae1dSRodney W. Grimes /* NOTREACHED */ 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes 1600df8bae1dSRodney W. Grimes /* 1601df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1602df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 16035b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 16045b3047d5SJohn Baldwin * lock held. 1605df8bae1dSRodney W. Grimes */ 16065b3047d5SJohn Baldwin static void 1607df8bae1dSRodney W. Grimes stop(p) 1608df8bae1dSRodney W. Grimes register struct proc *p; 1609df8bae1dSRodney W. Grimes { 1610df8bae1dSRodney W. Grimes 1611628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 16125b3047d5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 1613df8bae1dSRodney W. Grimes p->p_stat = SSTOP; 1614df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 1615df8bae1dSRodney W. Grimes wakeup((caddr_t)p->p_pptr); 1616df8bae1dSRodney W. Grimes } 1617df8bae1dSRodney W. Grimes 1618df8bae1dSRodney W. Grimes /* 1619df8bae1dSRodney W. Grimes * Take the action for the specified signal 1620df8bae1dSRodney W. Grimes * from the current set of pending signals. 1621df8bae1dSRodney W. Grimes */ 1622df8bae1dSRodney W. Grimes void 16232c42a146SMarcel Moolenaar postsig(sig) 16242c42a146SMarcel Moolenaar register int sig; 1625df8bae1dSRodney W. Grimes { 1626df8bae1dSRodney W. Grimes register struct proc *p = curproc; 1627628d2653SJohn Baldwin struct sigacts *ps; 16282c42a146SMarcel Moolenaar sig_t action; 16292c42a146SMarcel Moolenaar sigset_t returnmask; 16302c42a146SMarcel Moolenaar int code; 1631df8bae1dSRodney W. Grimes 16322c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 16335526d2d9SEivind Eklund 16342ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1635628d2653SJohn Baldwin ps = p->p_sigacts; 16362c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 16372c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1638df8bae1dSRodney W. Grimes #ifdef KTRACE 1639df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 1640645682fdSLuoqi Chen ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 16412c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1642df8bae1dSRodney W. Grimes #endif 1643628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 16442a024a2bSSean Eric Fagan 1645df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1646df8bae1dSRodney W. Grimes /* 1647df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1648df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1649df8bae1dSRodney W. Grimes */ 16502c42a146SMarcel Moolenaar sigexit(p, sig); 1651df8bae1dSRodney W. Grimes /* NOTREACHED */ 1652df8bae1dSRodney W. Grimes } else { 1653df8bae1dSRodney W. Grimes /* 1654df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1655df8bae1dSRodney W. Grimes */ 16562c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 16575526d2d9SEivind Eklund ("postsig action")); 1658df8bae1dSRodney W. Grimes /* 1659df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1660645682fdSLuoqi Chen * occurrences of this signal. 1661df8bae1dSRodney W. Grimes * 1662645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1663df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1664645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1665df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1666df8bae1dSRodney W. Grimes */ 1667645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 16686626c604SJulian Elischer returnmask = p->p_oldsigmask; 1669645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1670df8bae1dSRodney W. Grimes } else 1671df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 16722c42a146SMarcel Moolenaar 16732c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 16742c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 16752c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 16762c42a146SMarcel Moolenaar 16772c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1678289ccde0SPeter Wemm /* 16792c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1680289ccde0SPeter Wemm */ 16812c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 16822c42a146SMarcel Moolenaar if (sig != SIGCONT && 16832c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 16842c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 16852c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1686dedc04feSPeter Wemm } 1687df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 16882c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1689df8bae1dSRodney W. Grimes code = 0; 1690df8bae1dSRodney W. Grimes } else { 16916626c604SJulian Elischer code = p->p_code; 16926626c604SJulian Elischer p->p_code = 0; 16936626c604SJulian Elischer p->p_sig = 0; 1694df8bae1dSRodney W. Grimes } 16952c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1696df8bae1dSRodney W. Grimes } 1697df8bae1dSRodney W. Grimes } 1698df8bae1dSRodney W. Grimes 1699df8bae1dSRodney W. Grimes /* 1700df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1701df8bae1dSRodney W. Grimes */ 170226f9a767SRodney W. Grimes void 1703df8bae1dSRodney W. Grimes killproc(p, why) 1704df8bae1dSRodney W. Grimes struct proc *p; 1705df8bae1dSRodney W. Grimes char *why; 1706df8bae1dSRodney W. Grimes { 17079081e5e8SJohn Baldwin 17089081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 17090384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 17100384fff8SJason Evans p, p->p_pid, p->p_comm); 1711729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1712b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1713df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1714df8bae1dSRodney W. Grimes } 1715df8bae1dSRodney W. Grimes 1716df8bae1dSRodney W. Grimes /* 1717df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1718df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1719df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1720df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1721df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1722df8bae1dSRodney W. Grimes * does not return. 1723df8bae1dSRodney W. Grimes */ 172426f9a767SRodney W. Grimes void 17252c42a146SMarcel Moolenaar sigexit(p, sig) 1726df8bae1dSRodney W. Grimes register struct proc *p; 17272c42a146SMarcel Moolenaar int sig; 1728df8bae1dSRodney W. Grimes { 1729df8bae1dSRodney W. Grimes 1730628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1731df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 17322c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 17332c42a146SMarcel Moolenaar p->p_sig = sig; 1734c364e17eSAndrey A. Chernov /* 1735c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1736c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1737c364e17eSAndrey A. Chernov * these messages.) 1738c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1739c364e17eSAndrey A. Chernov */ 1740628d2653SJohn Baldwin PROC_UNLOCK(p); 1741c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1742c31146a1SJohn Baldwin mtx_lock(&Giant); 1743fca666a1SJulian Elischer if (coredump(p) == 0) 17442c42a146SMarcel Moolenaar sig |= WCOREFLAG; 174557308494SJoerg Wunsch if (kern_logsigexit) 174657308494SJoerg Wunsch log(LOG_INFO, 174757308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 17483d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 1749b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, 17502c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 17512c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1752c31146a1SJohn Baldwin } else { 1753628d2653SJohn Baldwin PROC_UNLOCK(p); 1754628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1755628d2653SJohn Baldwin mtx_lock(&Giant); 1756c31146a1SJohn Baldwin } 17572c42a146SMarcel Moolenaar exit1(p, W_EXITCODE(0, sig)); 1758df8bae1dSRodney W. Grimes /* NOTREACHED */ 1759df8bae1dSRodney W. Grimes } 1760df8bae1dSRodney W. Grimes 1761c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1762c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1763c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1764c5edb423SSean Eric Fagan 1765c5edb423SSean Eric Fagan /* 1766c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1767c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1768c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1769c5edb423SSean Eric Fagan * %N name of process ("name") 1770c5edb423SSean Eric Fagan * %P process id (pid) 1771c5edb423SSean Eric Fagan * %U user id (uid) 1772c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1773c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1774c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1775c5edb423SSean Eric Fagan */ 1776c5edb423SSean Eric Fagan 1777fca666a1SJulian Elischer static char * 1778c5edb423SSean Eric Fagan expand_name(name, uid, pid) 177987f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; { 1780c5edb423SSean Eric Fagan char *temp; 1781c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 1782c5edb423SSean Eric Fagan int i, n; 1783c5edb423SSean Eric Fagan char *format = corefilename; 1784ce38ca0fSAlfred Perlstein size_t namelen; 1785c5edb423SSean Eric Fagan 1786ce38ca0fSAlfred Perlstein temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 17870bfe2990SEivind Eklund if (temp == NULL) 17880bfe2990SEivind Eklund return NULL; 1789ce38ca0fSAlfred Perlstein namelen = strlen(name); 1790ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1791c5edb423SSean Eric Fagan int l; 1792c5edb423SSean Eric Fagan switch (format[i]) { 1793c5edb423SSean Eric Fagan case '%': /* Format character */ 1794c5edb423SSean Eric Fagan i++; 1795c5edb423SSean Eric Fagan switch (format[i]) { 1796c5edb423SSean Eric Fagan case '%': 1797c5edb423SSean Eric Fagan temp[n++] = '%'; 1798c5edb423SSean Eric Fagan break; 1799c5edb423SSean Eric Fagan case 'N': /* process name */ 1800ce38ca0fSAlfred Perlstein if ((n + namelen) > MAXPATHLEN) { 180187f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1802c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1803c5edb423SSean Eric Fagan free(temp, M_TEMP); 1804c5edb423SSean Eric Fagan return NULL; 1805c5edb423SSean Eric Fagan } 1806ce38ca0fSAlfred Perlstein memcpy(temp+n, name, namelen); 1807ce38ca0fSAlfred Perlstein n += namelen; 1808c5edb423SSean Eric Fagan break; 1809c5edb423SSean Eric Fagan case 'P': /* process id */ 1810ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", pid); 1811c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 181287f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1813c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1814c5edb423SSean Eric Fagan free(temp, M_TEMP); 1815c5edb423SSean Eric Fagan return NULL; 1816c5edb423SSean Eric Fagan } 1817c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1818c5edb423SSean Eric Fagan n += l; 1819c5edb423SSean Eric Fagan break; 1820c5edb423SSean Eric Fagan case 'U': /* user id */ 1821ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", uid); 1822c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 182387f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1824c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1825c5edb423SSean Eric Fagan free(temp, M_TEMP); 1826c5edb423SSean Eric Fagan return NULL; 1827c5edb423SSean Eric Fagan } 1828c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1829c5edb423SSean Eric Fagan n += l; 1830c5edb423SSean Eric Fagan break; 1831c5edb423SSean Eric Fagan default: 1832c5edb423SSean Eric Fagan log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1833c5edb423SSean Eric Fagan } 1834c5edb423SSean Eric Fagan break; 1835c5edb423SSean Eric Fagan default: 1836c5edb423SSean Eric Fagan temp[n++] = format[i]; 1837c5edb423SSean Eric Fagan } 1838c5edb423SSean Eric Fagan } 1839ce38ca0fSAlfred Perlstein temp[n] = '\0'; 1840c5edb423SSean Eric Fagan return temp; 1841c5edb423SSean Eric Fagan } 1842c5edb423SSean Eric Fagan 1843df8bae1dSRodney W. Grimes /* 1844fca666a1SJulian Elischer * Dump a process' core. The main routine does some 1845fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 1846fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 1847fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 1848fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 1849fca666a1SJulian Elischer */ 1850fca666a1SJulian Elischer 1851fca666a1SJulian Elischer static int 1852fca666a1SJulian Elischer coredump(p) 1853fca666a1SJulian Elischer register struct proc *p; 1854fca666a1SJulian Elischer { 1855fca666a1SJulian Elischer register struct vnode *vp; 1856da654d90SPoul-Henning Kamp register struct ucred *cred = p->p_ucred; 185706ae1e91SMatthew Dillon struct flock lf; 1858fca666a1SJulian Elischer struct nameidata nd; 1859fca666a1SJulian Elischer struct vattr vattr; 1860e6796b67SKirk McKusick int error, error1, flags; 1861f2a2857bSKirk McKusick struct mount *mp; 1862fca666a1SJulian Elischer char *name; /* name of corefile */ 1863fca666a1SJulian Elischer off_t limit; 1864fca666a1SJulian Elischer 1865628d2653SJohn Baldwin PROC_LOCK(p); 1866628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 1867fca666a1SJulian Elischer 1868628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1869628d2653SJohn Baldwin PROC_UNLOCK(p); 1870fca666a1SJulian Elischer return (EFAULT); 1871628d2653SJohn Baldwin } 1872fca666a1SJulian Elischer 1873fca666a1SJulian Elischer /* 187435a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 187535a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 187635a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 187735a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 187835a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 187935a2598fSSean Eric Fagan * if it is larger than the limit. 1880fca666a1SJulian Elischer */ 188135a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1882628d2653SJohn Baldwin if (limit == 0) { 1883628d2653SJohn Baldwin PROC_UNLOCK(p); 188435a2598fSSean Eric Fagan return 0; 1885628d2653SJohn Baldwin } 1886628d2653SJohn Baldwin PROC_UNLOCK(p); 188735a2598fSSean Eric Fagan 1888f2a2857bSKirk McKusick restart: 1889fca666a1SJulian Elischer name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1890ccdbd10cSPeter Pentchev if (name == NULL) 1891ccdbd10cSPeter Pentchev return (EINVAL); 1892fca666a1SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1893e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 1894e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1895fca666a1SJulian Elischer free(name, M_TEMP); 1896fca666a1SJulian Elischer if (error) 1897fca666a1SJulian Elischer return (error); 1898762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 1899fca666a1SJulian Elischer vp = nd.ni_vp; 190006ae1e91SMatthew Dillon 1901f2a2857bSKirk McKusick VOP_UNLOCK(vp, 0, p); 190206ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 190306ae1e91SMatthew Dillon lf.l_start = 0; 190406ae1e91SMatthew Dillon lf.l_len = 0; 190506ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 190606ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 190706ae1e91SMatthew Dillon if (error) 190806ae1e91SMatthew Dillon goto out2; 190906ae1e91SMatthew Dillon 191006ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 191106ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 191206ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 1913f2a2857bSKirk McKusick if ((error = vn_close(vp, FWRITE, cred, p)) != 0) 1914f2a2857bSKirk McKusick return (error); 1915f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1916f2a2857bSKirk McKusick return (error); 1917f2a2857bSKirk McKusick goto restart; 1918f2a2857bSKirk McKusick } 1919fca666a1SJulian Elischer 1920fca666a1SJulian Elischer /* Don't dump to non-regular files or files with links. */ 1921fca666a1SJulian Elischer if (vp->v_type != VREG || 1922fca666a1SJulian Elischer VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1923fca666a1SJulian Elischer error = EFAULT; 192406ae1e91SMatthew Dillon goto out1; 1925fca666a1SJulian Elischer } 1926fca666a1SJulian Elischer VATTR_NULL(&vattr); 1927fca666a1SJulian Elischer vattr.va_size = 0; 1928fca666a1SJulian Elischer VOP_LEASE(vp, p, cred, LEASE_WRITE); 1929fca666a1SJulian Elischer VOP_SETATTR(vp, &vattr, cred, p); 1930628d2653SJohn Baldwin PROC_LOCK(p); 1931fca666a1SJulian Elischer p->p_acflag |= ACORE; 1932628d2653SJohn Baldwin PROC_UNLOCK(p); 1933fca666a1SJulian Elischer 1934fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 1935fca666a1SJulian Elischer p->p_sysent->sv_coredump(p, vp, limit) : 1936fca666a1SJulian Elischer ENOSYS; 1937fca666a1SJulian Elischer 193806ae1e91SMatthew Dillon out1: 193906ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 194006ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 1941f2a2857bSKirk McKusick vn_finished_write(mp); 194206ae1e91SMatthew Dillon out2: 1943fca666a1SJulian Elischer error1 = vn_close(vp, FWRITE, cred, p); 1944fca666a1SJulian Elischer if (error == 0) 1945fca666a1SJulian Elischer error = error1; 1946fca666a1SJulian Elischer return (error); 1947fca666a1SJulian Elischer } 1948fca666a1SJulian Elischer 1949fca666a1SJulian Elischer /* 1950df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 1951df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 1952df8bae1dSRodney W. Grimes */ 1953d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1954df8bae1dSRodney W. Grimes struct nosys_args { 1955df8bae1dSRodney W. Grimes int dummy; 1956df8bae1dSRodney W. Grimes }; 1957d2d3e875SBruce Evans #endif 1958fb99ab88SMatthew Dillon /* 1959fb99ab88SMatthew Dillon * MPSAFE 1960fb99ab88SMatthew Dillon */ 1961df8bae1dSRodney W. Grimes /* ARGSUSED */ 196226f9a767SRodney W. Grimes int 1963cb226aaaSPoul-Henning Kamp nosys(p, args) 1964df8bae1dSRodney W. Grimes struct proc *p; 1965df8bae1dSRodney W. Grimes struct nosys_args *args; 1966df8bae1dSRodney W. Grimes { 1967fb99ab88SMatthew Dillon mtx_lock(&Giant); 1968628d2653SJohn Baldwin PROC_LOCK(p); 1969df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 1970628d2653SJohn Baldwin PROC_UNLOCK(p); 1971fb99ab88SMatthew Dillon mtx_unlock(&Giant); 1972df8bae1dSRodney W. Grimes return (EINVAL); 1973df8bae1dSRodney W. Grimes } 1974831d27a9SDon Lewis 1975831d27a9SDon Lewis /* 1976831d27a9SDon Lewis * Send a signal to a SIGIO or SIGURG to a process or process group using 1977831d27a9SDon Lewis * stored credentials rather than those of the current process. 1978831d27a9SDon Lewis */ 1979831d27a9SDon Lewis void 19802c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty) 1981831d27a9SDon Lewis struct sigio *sigio; 19822c42a146SMarcel Moolenaar int sig, checkctty; 1983831d27a9SDon Lewis { 1984831d27a9SDon Lewis if (sigio == NULL) 1985831d27a9SDon Lewis return; 1986831d27a9SDon Lewis 1987831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 1988628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 1989b1fc0ec1SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 19902c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 1991628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 1992831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 1993831d27a9SDon Lewis struct proc *p; 1994831d27a9SDon Lewis 1995628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 1996628d2653SJohn Baldwin PROC_LOCK(p); 1997b1fc0ec1SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 1998831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 19992c42a146SMarcel Moolenaar psignal(p, sig); 2000628d2653SJohn Baldwin PROC_UNLOCK(p); 2001628d2653SJohn Baldwin } 2002831d27a9SDon Lewis } 2003831d27a9SDon Lewis } 2004cb679c38SJonathan Lemon 2005cb679c38SJonathan Lemon static int 2006cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2007cb679c38SJonathan Lemon { 2008cb679c38SJonathan Lemon struct proc *p = curproc; 2009cb679c38SJonathan Lemon 2010cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2011cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2012cb679c38SJonathan Lemon 2013628d2653SJohn Baldwin PROC_LOCK(p); 2014cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2015628d2653SJohn Baldwin PROC_UNLOCK(p); 2016cb679c38SJonathan Lemon 2017cb679c38SJonathan Lemon return (0); 2018cb679c38SJonathan Lemon } 2019cb679c38SJonathan Lemon 2020cb679c38SJonathan Lemon static void 2021cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2022cb679c38SJonathan Lemon { 2023cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2024cb679c38SJonathan Lemon 2025628d2653SJohn Baldwin PROC_LOCK(p); 2026e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2027628d2653SJohn Baldwin PROC_UNLOCK(p); 2028cb679c38SJonathan Lemon } 2029cb679c38SJonathan Lemon 2030cb679c38SJonathan Lemon /* 2031cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2032cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2033cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2034cb679c38SJonathan Lemon * isn't worth the trouble. 2035cb679c38SJonathan Lemon */ 2036cb679c38SJonathan Lemon static int 2037cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2038cb679c38SJonathan Lemon { 2039cb679c38SJonathan Lemon 2040cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2041cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2042cb679c38SJonathan Lemon 2043cb679c38SJonathan Lemon if (kn->kn_id == hint) 2044cb679c38SJonathan Lemon kn->kn_data++; 2045cb679c38SJonathan Lemon } 2046cb679c38SJonathan Lemon return (kn->kn_data != 0); 2047cb679c38SJonathan Lemon } 2048