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> 5735e0e5b3SJohn Baldwin #include <sys/ipl.h> 58238510fcSJason Evans #include <sys/condvar.h> 59c31146a1SJohn Baldwin #include <sys/lock.h> 6035e0e5b3SJohn Baldwin #include <sys/mutex.h> 61df8bae1dSRodney W. Grimes #include <sys/wait.h> 620384fff8SJason Evans #include <sys/ktr.h> 63df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 64c31146a1SJohn Baldwin #include <sys/resourcevar.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> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <machine/cpu.h> 733163861cSTor Egge #include <machine/smp.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 /* 1012c42a146SMarcel Moolenaar * Can process p, with pcred pc, send the signal sig to process q? 102df8bae1dSRodney W. Grimes */ 103a9e0361bSPoul-Henning Kamp #define CANSIGNAL(p, q, sig) \ 104387d2c03SRobert Watson (!p_can(p, q, P_CAN_KILL, NULL) || \ 105a9e0361bSPoul-Henning Kamp ((sig) == SIGCONT && (q)->p_session == (p)->p_session)) 106df8bae1dSRodney W. Grimes 107831d27a9SDon Lewis /* 108831d27a9SDon Lewis * Policy -- Can real uid ruid with ucred uc send a signal to process q? 109831d27a9SDon Lewis */ 110831d27a9SDon Lewis #define CANSIGIO(ruid, uc, q) \ 111831d27a9SDon Lewis ((uc)->cr_uid == 0 || \ 112831d27a9SDon Lewis (ruid) == (q)->p_cred->p_ruid || \ 113831d27a9SDon Lewis (uc)->cr_uid == (q)->p_cred->p_ruid || \ 114831d27a9SDon Lewis (ruid) == (q)->p_ucred->cr_uid || \ 115831d27a9SDon Lewis (uc)->cr_uid == (q)->p_ucred->cr_uid) 116831d27a9SDon Lewis 11722d4b0fbSJohn Polstra int sugid_coredump; 1183d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1193d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 120c87e2930SDavid Greenman 121e5a28db9SPaul Saab static int do_coredump = 1; 122e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 123e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 124e5a28db9SPaul Saab 1252c42a146SMarcel Moolenaar /* 1262c42a146SMarcel Moolenaar * Signal properties and actions. 1272c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1282c42a146SMarcel Moolenaar * according to the following properties: 1292c42a146SMarcel Moolenaar */ 1302c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1312c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1322c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1332c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1342c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1352c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1362c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 137df8bae1dSRodney W. Grimes 1382c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 1392c42a146SMarcel Moolenaar SA_KILL, /* SIGHUP */ 1402c42a146SMarcel Moolenaar SA_KILL, /* SIGINT */ 1412c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGQUIT */ 1422c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1432c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1442c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 1452c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGEMT */ 1462c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 1472c42a146SMarcel Moolenaar SA_KILL, /* SIGKILL */ 1482c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1492c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1502c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 1512c42a146SMarcel Moolenaar SA_KILL, /* SIGPIPE */ 1522c42a146SMarcel Moolenaar SA_KILL, /* SIGALRM */ 1532c42a146SMarcel Moolenaar SA_KILL, /* SIGTERM */ 1542c42a146SMarcel Moolenaar SA_IGNORE, /* SIGURG */ 1552c42a146SMarcel Moolenaar SA_STOP, /* SIGSTOP */ 1562c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 1572c42a146SMarcel Moolenaar SA_IGNORE|SA_CONT, /* SIGCONT */ 1582c42a146SMarcel Moolenaar SA_IGNORE, /* SIGCHLD */ 1592c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 1602c42a146SMarcel Moolenaar SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 1612c42a146SMarcel Moolenaar SA_IGNORE, /* SIGIO */ 1622c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1632c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 1642c42a146SMarcel Moolenaar SA_KILL, /* SIGVTALRM */ 1652c42a146SMarcel Moolenaar SA_KILL, /* SIGPROF */ 1662c42a146SMarcel Moolenaar SA_IGNORE, /* SIGWINCH */ 1672c42a146SMarcel Moolenaar SA_IGNORE, /* SIGINFO */ 1682c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR1 */ 1692c42a146SMarcel Moolenaar SA_KILL, /* SIGUSR2 */ 1702c42a146SMarcel Moolenaar }; 1712c42a146SMarcel Moolenaar 172fbbeeb6cSBruce Evans /* 173fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 174fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 175fbbeeb6cSBruce Evans * action, the process stops in issignal(). 176fbbeeb6cSBruce Evans * 17733510ef1SBruce Evans * MP SAFE. 178fbbeeb6cSBruce Evans */ 179fbbeeb6cSBruce Evans int 180fbbeeb6cSBruce Evans CURSIG(struct proc *p) 181fbbeeb6cSBruce Evans { 182fbbeeb6cSBruce Evans sigset_t tmpset; 183628d2653SJohn Baldwin int r = 0; 184fbbeeb6cSBruce Evans 185628d2653SJohn Baldwin PROC_LOCK(p); 18633510ef1SBruce Evans if (SIGISEMPTY(p->p_siglist)) 187628d2653SJohn Baldwin goto out; 188fbbeeb6cSBruce Evans tmpset = p->p_siglist; 189fbbeeb6cSBruce Evans SIGSETNAND(tmpset, p->p_sigmask); 19033510ef1SBruce Evans if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0) 191628d2653SJohn Baldwin goto out; 192fbbeeb6cSBruce Evans r = issignal(p); 193628d2653SJohn Baldwin out: 194628d2653SJohn Baldwin PROC_UNLOCK(p); 195fbbeeb6cSBruce Evans return (r); 196fbbeeb6cSBruce Evans } 197fbbeeb6cSBruce Evans 1986f841fb7SMarcel Moolenaar static __inline int 1996f841fb7SMarcel Moolenaar sigprop(int sig) 2002c42a146SMarcel Moolenaar { 2016f841fb7SMarcel Moolenaar 2022c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 2032c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 2042c42a146SMarcel Moolenaar return (0); 205df8bae1dSRodney W. Grimes } 2062c42a146SMarcel Moolenaar 2076f841fb7SMarcel Moolenaar static __inline int 2086f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2092c42a146SMarcel Moolenaar { 2102c42a146SMarcel Moolenaar int i; 2112c42a146SMarcel Moolenaar 2126f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2132c42a146SMarcel Moolenaar if (set->__bits[i]) 2142c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 215df8bae1dSRodney W. Grimes return (0); 216df8bae1dSRodney W. Grimes } 217df8bae1dSRodney W. Grimes 2182c42a146SMarcel Moolenaar /* 2192c42a146SMarcel Moolenaar * do_sigaction 2202c42a146SMarcel Moolenaar * sigaction 2212c42a146SMarcel Moolenaar * osigaction 2222c42a146SMarcel Moolenaar */ 2232c42a146SMarcel Moolenaar static int 224645682fdSLuoqi Chen do_sigaction(p, sig, act, oact, old) 2252c42a146SMarcel Moolenaar struct proc *p; 2262c42a146SMarcel Moolenaar register int sig; 2272c42a146SMarcel Moolenaar struct sigaction *act, *oact; 228645682fdSLuoqi Chen int old; 229df8bae1dSRodney W. Grimes { 230628d2653SJohn Baldwin register struct sigacts *ps; 231df8bae1dSRodney W. Grimes 2322c42a146SMarcel Moolenaar if (sig <= 0 || sig > _SIG_MAXSIG) 2332c42a146SMarcel Moolenaar return (EINVAL); 2342c42a146SMarcel Moolenaar 235628d2653SJohn Baldwin PROC_LOCK(p); 236628d2653SJohn Baldwin ps = p->p_sigacts; 2372c42a146SMarcel Moolenaar if (oact) { 2382c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2392c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2402c42a146SMarcel Moolenaar oact->sa_flags = 0; 2412c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 2422c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 2432c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 2442c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 2452c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 2462c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 2472c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 2482c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 2492c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 2502c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 251645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 2522c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 253645682fdSLuoqi Chen if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 2542c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 2552c42a146SMarcel Moolenaar } 2562c42a146SMarcel Moolenaar if (act) { 2572c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 258628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 259628d2653SJohn Baldwin PROC_UNLOCK(p); 2602c42a146SMarcel Moolenaar return (EINVAL); 261628d2653SJohn Baldwin } 2622c42a146SMarcel Moolenaar 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * Change setting atomically. 265df8bae1dSRodney W. Grimes */ 2662c42a146SMarcel Moolenaar 2672c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 2682c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 2692c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 2702c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 2712c42a146SMarcel Moolenaar SIGADDSET(ps->ps_siginfo, sig); 2726f841fb7SMarcel Moolenaar } else { 2732c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = 2742c42a146SMarcel Moolenaar (__sighandler_t *)act->sa_sigaction; 2752c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 2762c42a146SMarcel Moolenaar } 2772c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 2782c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 279df8bae1dSRodney W. Grimes else 2802c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 2812c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 2822c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 283df8bae1dSRodney W. Grimes else 2842c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 2852c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 2862c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 2871e41c1b5SSteven Wallace else 2882c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 2892c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 2902c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 291289ccde0SPeter Wemm else 2922c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 293df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 2942c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 2952c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 296df8bae1dSRodney W. Grimes else 2972c42a146SMarcel Moolenaar SIGDELSET(ps->ps_usertramp, seg); 298df8bae1dSRodney W. Grimes #endif 2992c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3002c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 301645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDSTOP; 3026626c604SJulian Elischer else 303645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 3042c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDWAIT) { 305245f17d4SJoerg Wunsch /* 3062c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3072c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3082c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3092c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 310245f17d4SJoerg Wunsch */ 311245f17d4SJoerg Wunsch if (p->p_pid == 1) 312645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 3136626c604SJulian Elischer else 314645682fdSLuoqi Chen p->p_procsig->ps_flag |= PS_NOCLDWAIT; 315245f17d4SJoerg Wunsch } else 316645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes /* 319df8bae1dSRodney W. Grimes * Set bit in p_sigignore for signals that are set to SIG_IGN, 3202c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 3212c42a146SMarcel Moolenaar * ignore. However, don't put SIGCONT in p_sigignore, as we 3222c42a146SMarcel Moolenaar * have to restart the process. 323df8bae1dSRodney W. Grimes */ 3242c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3252c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3262c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3272c42a146SMarcel Moolenaar /* never to be seen again */ 3282c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 3292c42a146SMarcel Moolenaar if (sig != SIGCONT) 3302c42a146SMarcel Moolenaar /* easier in psignal */ 3312c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 3322c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 333645682fdSLuoqi Chen } else { 3342c42a146SMarcel Moolenaar SIGDELSET(p->p_sigignore, sig); 3352c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 3362c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 3372c42a146SMarcel Moolenaar else 3382c42a146SMarcel Moolenaar SIGADDSET(p->p_sigcatch, sig); 3392c42a146SMarcel Moolenaar } 340645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 341645682fdSLuoqi Chen ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 342645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 343645682fdSLuoqi Chen else 344645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 345df8bae1dSRodney W. Grimes } 346628d2653SJohn Baldwin PROC_UNLOCK(p); 3472c42a146SMarcel Moolenaar return (0); 3482c42a146SMarcel Moolenaar } 3492c42a146SMarcel Moolenaar 3502c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3512c42a146SMarcel Moolenaar struct sigaction_args { 3522c42a146SMarcel Moolenaar int sig; 3532c42a146SMarcel Moolenaar struct sigaction *act; 3542c42a146SMarcel Moolenaar struct sigaction *oact; 3552c42a146SMarcel Moolenaar }; 3562c42a146SMarcel Moolenaar #endif 3572c42a146SMarcel Moolenaar /* ARGSUSED */ 3582c42a146SMarcel Moolenaar int 3592c42a146SMarcel Moolenaar sigaction(p, uap) 3602c42a146SMarcel Moolenaar struct proc *p; 3612c42a146SMarcel Moolenaar register struct sigaction_args *uap; 3622c42a146SMarcel Moolenaar { 3632c42a146SMarcel Moolenaar struct sigaction act, oact; 3642c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 3652c42a146SMarcel Moolenaar int error; 3662c42a146SMarcel Moolenaar 3676f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 3686f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 3692c42a146SMarcel Moolenaar if (actp) { 3706f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 3712c42a146SMarcel Moolenaar if (error) 3722c42a146SMarcel Moolenaar return (error); 3732c42a146SMarcel Moolenaar } 374645682fdSLuoqi Chen error = do_sigaction(p, uap->sig, actp, oactp, 0); 3752c42a146SMarcel Moolenaar if (oactp && !error) { 3766f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 3772c42a146SMarcel Moolenaar } 3782c42a146SMarcel Moolenaar return (error); 3792c42a146SMarcel Moolenaar } 3802c42a146SMarcel Moolenaar 38131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 3822c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 3832c42a146SMarcel Moolenaar struct osigaction_args { 3842c42a146SMarcel Moolenaar int signum; 3852c42a146SMarcel Moolenaar struct osigaction *nsa; 3862c42a146SMarcel Moolenaar struct osigaction *osa; 3872c42a146SMarcel Moolenaar }; 3882c42a146SMarcel Moolenaar #endif 3892c42a146SMarcel Moolenaar /* ARGSUSED */ 3902c42a146SMarcel Moolenaar int 3912c42a146SMarcel Moolenaar osigaction(p, uap) 3922c42a146SMarcel Moolenaar struct proc *p; 3932c42a146SMarcel Moolenaar register struct osigaction_args *uap; 3942c42a146SMarcel Moolenaar { 3952c42a146SMarcel Moolenaar struct osigaction sa; 3962c42a146SMarcel Moolenaar struct sigaction nsa, osa; 3972c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 3982c42a146SMarcel Moolenaar int error; 3992c42a146SMarcel Moolenaar 4006f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 4016f841fb7SMarcel Moolenaar return (EINVAL); 4026f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 4036f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 4042c42a146SMarcel Moolenaar if (nsap) { 4056f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 4062c42a146SMarcel Moolenaar if (error) 4072c42a146SMarcel Moolenaar return (error); 4082c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 4092c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 4102c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 4112c42a146SMarcel Moolenaar } 412645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 4132c42a146SMarcel Moolenaar if (osap && !error) { 4142c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 4152c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 4162c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 4176f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 4182c42a146SMarcel Moolenaar } 4192c42a146SMarcel Moolenaar return (error); 4202c42a146SMarcel Moolenaar } 42131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes /* 424df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 425df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 426df8bae1dSRodney W. Grimes */ 427df8bae1dSRodney W. Grimes void 428df8bae1dSRodney W. Grimes siginit(p) 429df8bae1dSRodney W. Grimes struct proc *p; 430df8bae1dSRodney W. Grimes { 431df8bae1dSRodney W. Grimes register int i; 432df8bae1dSRodney W. Grimes 433628d2653SJohn Baldwin PROC_LOCK(p); 4342c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 4352c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 4362c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, i); 437628d2653SJohn Baldwin PROC_UNLOCK(p); 438df8bae1dSRodney W. Grimes } 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes /* 441df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 442df8bae1dSRodney W. Grimes */ 443df8bae1dSRodney W. Grimes void 444df8bae1dSRodney W. Grimes execsigs(p) 445df8bae1dSRodney W. Grimes register struct proc *p; 446df8bae1dSRodney W. Grimes { 447628d2653SJohn Baldwin register struct sigacts *ps; 4482c42a146SMarcel Moolenaar register int sig; 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes /* 451df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 452df8bae1dSRodney W. Grimes * through p_sigmask (unless they were caught, 453df8bae1dSRodney W. Grimes * and are now ignored by default). 454df8bae1dSRodney W. Grimes */ 455628d2653SJohn Baldwin PROC_LOCK(p); 456628d2653SJohn Baldwin ps = p->p_sigacts; 4572c42a146SMarcel Moolenaar while (SIGNOTEMPTY(p->p_sigcatch)) { 4582c42a146SMarcel Moolenaar sig = sig_ffs(&p->p_sigcatch); 4592c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 4602c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 4612c42a146SMarcel Moolenaar if (sig != SIGCONT) 4622c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 4632c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 464df8bae1dSRodney W. Grimes } 4652c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 466df8bae1dSRodney W. Grimes } 467df8bae1dSRodney W. Grimes /* 468df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 469df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 470df8bae1dSRodney W. Grimes */ 471645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 472645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 473645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 47480e907a1SPeter Wemm /* 47580e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 47680e907a1SPeter Wemm */ 477645682fdSLuoqi Chen p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 478628d2653SJohn Baldwin PROC_UNLOCK(p); 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes /* 482628d2653SJohn Baldwin * do_sigprocmask() 4837c8fdcbdSMatthew Dillon * 484628d2653SJohn Baldwin * Manipulate signal mask. 485df8bae1dSRodney W. Grimes */ 4862c42a146SMarcel Moolenaar static int 487645682fdSLuoqi Chen do_sigprocmask(p, how, set, oset, old) 4882c42a146SMarcel Moolenaar struct proc *p; 4892c42a146SMarcel Moolenaar int how; 4902c42a146SMarcel Moolenaar sigset_t *set, *oset; 491645682fdSLuoqi Chen int old; 4922c42a146SMarcel Moolenaar { 4932c42a146SMarcel Moolenaar int error; 4942c42a146SMarcel Moolenaar 495628d2653SJohn Baldwin PROC_LOCK(p); 4962c42a146SMarcel Moolenaar if (oset != NULL) 4972c42a146SMarcel Moolenaar *oset = p->p_sigmask; 4982c42a146SMarcel Moolenaar 4992c42a146SMarcel Moolenaar error = 0; 5002c42a146SMarcel Moolenaar if (set != NULL) { 5012c42a146SMarcel Moolenaar switch (how) { 5022c42a146SMarcel Moolenaar case SIG_BLOCK: 503645682fdSLuoqi Chen SIG_CANTMASK(*set); 5042c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, *set); 5052c42a146SMarcel Moolenaar break; 5062c42a146SMarcel Moolenaar case SIG_UNBLOCK: 5072c42a146SMarcel Moolenaar SIGSETNAND(p->p_sigmask, *set); 5082c42a146SMarcel Moolenaar break; 5092c42a146SMarcel Moolenaar case SIG_SETMASK: 510645682fdSLuoqi Chen SIG_CANTMASK(*set); 511645682fdSLuoqi Chen if (old) 512645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, *set); 513645682fdSLuoqi Chen else 5142c42a146SMarcel Moolenaar p->p_sigmask = *set; 5152c42a146SMarcel Moolenaar break; 5162c42a146SMarcel Moolenaar default: 5172c42a146SMarcel Moolenaar error = EINVAL; 5182c42a146SMarcel Moolenaar break; 5192c42a146SMarcel Moolenaar } 5202c42a146SMarcel Moolenaar } 521628d2653SJohn Baldwin PROC_UNLOCK(p); 5222c42a146SMarcel Moolenaar return (error); 5232c42a146SMarcel Moolenaar } 5242c42a146SMarcel Moolenaar 5257c8fdcbdSMatthew Dillon /* 5267c8fdcbdSMatthew Dillon * sigprocmask() - MP SAFE 5277c8fdcbdSMatthew Dillon */ 5287c8fdcbdSMatthew Dillon 529d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 530df8bae1dSRodney W. Grimes struct sigprocmask_args { 531df8bae1dSRodney W. Grimes int how; 5322c42a146SMarcel Moolenaar const sigset_t *set; 5332c42a146SMarcel Moolenaar sigset_t *oset; 534df8bae1dSRodney W. Grimes }; 535d2d3e875SBruce Evans #endif 53626f9a767SRodney W. Grimes int 537cb226aaaSPoul-Henning Kamp sigprocmask(p, uap) 538df8bae1dSRodney W. Grimes register struct proc *p; 539df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 540df8bae1dSRodney W. Grimes { 5412c42a146SMarcel Moolenaar sigset_t set, oset; 5422c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 5432c42a146SMarcel Moolenaar int error; 544df8bae1dSRodney W. Grimes 5456f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 5466f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 5472c42a146SMarcel Moolenaar if (setp) { 5486f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 5492c42a146SMarcel Moolenaar if (error) 5502c42a146SMarcel Moolenaar return (error); 551df8bae1dSRodney W. Grimes } 552645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, setp, osetp, 0); 5532c42a146SMarcel Moolenaar if (osetp && !error) { 5546f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 5552c42a146SMarcel Moolenaar } 5562c42a146SMarcel Moolenaar return (error); 5572c42a146SMarcel Moolenaar } 5582c42a146SMarcel Moolenaar 55931c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5607c8fdcbdSMatthew Dillon /* 5617c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 5627c8fdcbdSMatthew Dillon */ 5632c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5642c42a146SMarcel Moolenaar struct osigprocmask_args { 5652c42a146SMarcel Moolenaar int how; 5662c42a146SMarcel Moolenaar osigset_t mask; 5672c42a146SMarcel Moolenaar }; 5682c42a146SMarcel Moolenaar #endif 5692c42a146SMarcel Moolenaar int 5702c42a146SMarcel Moolenaar osigprocmask(p, uap) 5712c42a146SMarcel Moolenaar register struct proc *p; 5722c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 5732c42a146SMarcel Moolenaar { 5742c42a146SMarcel Moolenaar sigset_t set, oset; 5752c42a146SMarcel Moolenaar int error; 5762c42a146SMarcel Moolenaar 5772c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 578645682fdSLuoqi Chen error = do_sigprocmask(p, uap->how, &set, &oset, 1); 5792c42a146SMarcel Moolenaar SIG2OSIG(oset, p->p_retval[0]); 580df8bae1dSRodney W. Grimes return (error); 581df8bae1dSRodney W. Grimes } 58231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 583df8bae1dSRodney W. Grimes 584d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 585df8bae1dSRodney W. Grimes struct sigpending_args { 5862c42a146SMarcel Moolenaar sigset_t *set; 587df8bae1dSRodney W. Grimes }; 588d2d3e875SBruce Evans #endif 589df8bae1dSRodney W. Grimes /* ARGSUSED */ 59026f9a767SRodney W. Grimes int 591cb226aaaSPoul-Henning Kamp sigpending(p, uap) 592df8bae1dSRodney W. Grimes struct proc *p; 593df8bae1dSRodney W. Grimes struct sigpending_args *uap; 594df8bae1dSRodney W. Grimes { 595628d2653SJohn Baldwin sigset_t siglist; 596df8bae1dSRodney W. Grimes 597628d2653SJohn Baldwin PROC_LOCK(p); 598628d2653SJohn Baldwin siglist = p->p_siglist; 599628d2653SJohn Baldwin PROC_UNLOCK(p); 600628d2653SJohn Baldwin return (copyout(&siglist, uap->set, sizeof(sigset_t))); 6012c42a146SMarcel Moolenaar } 6022c42a146SMarcel Moolenaar 60331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 6042c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 6052c42a146SMarcel Moolenaar struct osigpending_args { 6062c42a146SMarcel Moolenaar int dummy; 6072c42a146SMarcel Moolenaar }; 6082c42a146SMarcel Moolenaar #endif 6092c42a146SMarcel Moolenaar /* ARGSUSED */ 6102c42a146SMarcel Moolenaar int 6112c42a146SMarcel Moolenaar osigpending(p, uap) 6122c42a146SMarcel Moolenaar struct proc *p; 6132c42a146SMarcel Moolenaar struct osigpending_args *uap; 6142c42a146SMarcel Moolenaar { 6152c42a146SMarcel Moolenaar 616628d2653SJohn Baldwin PROC_LOCK(p); 6172c42a146SMarcel Moolenaar SIG2OSIG(p->p_siglist, p->p_retval[0]); 618628d2653SJohn Baldwin PROC_UNLOCK(p); 619df8bae1dSRodney W. Grimes return (0); 620df8bae1dSRodney W. Grimes } 62131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 624df8bae1dSRodney W. Grimes /* 625df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 626df8bae1dSRodney W. Grimes */ 627d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 628df8bae1dSRodney W. Grimes struct osigvec_args { 629df8bae1dSRodney W. Grimes int signum; 630df8bae1dSRodney W. Grimes struct sigvec *nsv; 631df8bae1dSRodney W. Grimes struct sigvec *osv; 632df8bae1dSRodney W. Grimes }; 633d2d3e875SBruce Evans #endif 634df8bae1dSRodney W. Grimes /* ARGSUSED */ 63526f9a767SRodney W. Grimes int 636cb226aaaSPoul-Henning Kamp osigvec(p, uap) 637df8bae1dSRodney W. Grimes struct proc *p; 638df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 639df8bae1dSRodney W. Grimes { 640df8bae1dSRodney W. Grimes struct sigvec vec; 6412c42a146SMarcel Moolenaar struct sigaction nsa, osa; 6422c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 6432c42a146SMarcel Moolenaar int error; 644df8bae1dSRodney W. Grimes 6456f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 6466f841fb7SMarcel Moolenaar return (EINVAL); 6476f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 6486f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 6492c42a146SMarcel Moolenaar if (nsap) { 6506f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 6512c42a146SMarcel Moolenaar if (error) 652df8bae1dSRodney W. Grimes return (error); 6532c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 6542c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 6552c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 6562c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 657df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 6582c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 659df8bae1dSRodney W. Grimes #endif 660df8bae1dSRodney W. Grimes } 661645682fdSLuoqi Chen error = do_sigaction(p, uap->signum, nsap, osap, 1); 6622c42a146SMarcel Moolenaar if (osap && !error) { 6632c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 6642c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 6652c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 6662c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 6672c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 6682c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 6692c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 6702c42a146SMarcel Moolenaar #endif 6716f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 6722c42a146SMarcel Moolenaar } 6732c42a146SMarcel Moolenaar return (error); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 676d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 677df8bae1dSRodney W. Grimes struct osigblock_args { 678df8bae1dSRodney W. Grimes int mask; 679df8bae1dSRodney W. Grimes }; 680d2d3e875SBruce Evans #endif 68126f9a767SRodney W. Grimes int 682cb226aaaSPoul-Henning Kamp osigblock(p, uap) 683df8bae1dSRodney W. Grimes register struct proc *p; 684df8bae1dSRodney W. Grimes struct osigblock_args *uap; 685df8bae1dSRodney W. Grimes { 6862c42a146SMarcel Moolenaar sigset_t set; 687df8bae1dSRodney W. Grimes 6882c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 6892c42a146SMarcel Moolenaar SIG_CANTMASK(set); 690628d2653SJohn Baldwin PROC_LOCK(p); 6912c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 6922c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, set); 693628d2653SJohn Baldwin PROC_UNLOCK(p); 694df8bae1dSRodney W. Grimes return (0); 695df8bae1dSRodney W. Grimes } 696df8bae1dSRodney W. Grimes 697d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 698df8bae1dSRodney W. Grimes struct osigsetmask_args { 699df8bae1dSRodney W. Grimes int mask; 700df8bae1dSRodney W. Grimes }; 701d2d3e875SBruce Evans #endif 70226f9a767SRodney W. Grimes int 703cb226aaaSPoul-Henning Kamp osigsetmask(p, uap) 704df8bae1dSRodney W. Grimes struct proc *p; 705df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 706df8bae1dSRodney W. Grimes { 7072c42a146SMarcel Moolenaar sigset_t set; 708df8bae1dSRodney W. Grimes 7092c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 7102c42a146SMarcel Moolenaar SIG_CANTMASK(set); 711628d2653SJohn Baldwin PROC_LOCK(p); 7122c42a146SMarcel Moolenaar SIG2OSIG(p->p_sigmask, p->p_retval[0]); 713645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, set); 714628d2653SJohn Baldwin PROC_UNLOCK(p); 715df8bae1dSRodney W. Grimes return (0); 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 718df8bae1dSRodney W. Grimes 719df8bae1dSRodney W. Grimes /* 720df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 721df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 722df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 723df8bae1dSRodney W. Grimes */ 724d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 725df8bae1dSRodney W. Grimes struct sigsuspend_args { 7262c42a146SMarcel Moolenaar const sigset_t *sigmask; 727df8bae1dSRodney W. Grimes }; 728d2d3e875SBruce Evans #endif 729df8bae1dSRodney W. Grimes /* ARGSUSED */ 73026f9a767SRodney W. Grimes int 731cb226aaaSPoul-Henning Kamp sigsuspend(p, uap) 732df8bae1dSRodney W. Grimes register struct proc *p; 733df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 734df8bae1dSRodney W. Grimes { 7352c42a146SMarcel Moolenaar sigset_t mask; 736628d2653SJohn Baldwin register struct sigacts *ps; 7372c42a146SMarcel Moolenaar int error; 7382c42a146SMarcel Moolenaar 7396f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 7402c42a146SMarcel Moolenaar if (error) 7412c42a146SMarcel Moolenaar return (error); 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes /* 744645682fdSLuoqi Chen * When returning from sigsuspend, we want 745df8bae1dSRodney W. Grimes * the old mask to be restored after the 746df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 747df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 748df8bae1dSRodney W. Grimes * to indicate this. 749df8bae1dSRodney W. Grimes */ 750628d2653SJohn Baldwin PROC_LOCK(p); 751628d2653SJohn Baldwin ps = p->p_sigacts; 7526626c604SJulian Elischer p->p_oldsigmask = p->p_sigmask; 753645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 754645682fdSLuoqi Chen 755645682fdSLuoqi Chen SIG_CANTMASK(mask); 7562c42a146SMarcel Moolenaar p->p_sigmask = mask; 757628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 7582c42a146SMarcel Moolenaar /* void */; 759628d2653SJohn Baldwin PROC_UNLOCK(p); 7602c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 7612c42a146SMarcel Moolenaar return (EINTR); 7622c42a146SMarcel Moolenaar } 7632c42a146SMarcel Moolenaar 76431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 7652c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7662c42a146SMarcel Moolenaar struct osigsuspend_args { 7672c42a146SMarcel Moolenaar osigset_t mask; 7682c42a146SMarcel Moolenaar }; 7692c42a146SMarcel Moolenaar #endif 7702c42a146SMarcel Moolenaar /* ARGSUSED */ 7712c42a146SMarcel Moolenaar int 7722c42a146SMarcel Moolenaar osigsuspend(p, uap) 7732c42a146SMarcel Moolenaar register struct proc *p; 7742c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 7752c42a146SMarcel Moolenaar { 776645682fdSLuoqi Chen sigset_t mask; 777628d2653SJohn Baldwin register struct sigacts *ps; 7782c42a146SMarcel Moolenaar 779628d2653SJohn Baldwin PROC_LOCK(p); 780628d2653SJohn Baldwin ps = p->p_sigacts; 7812c42a146SMarcel Moolenaar p->p_oldsigmask = p->p_sigmask; 782645682fdSLuoqi Chen p->p_flag |= P_OLDMASK; 783645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 784645682fdSLuoqi Chen SIG_CANTMASK(mask); 785645682fdSLuoqi Chen SIGSETLO(p->p_sigmask, mask); 786628d2653SJohn Baldwin while (msleep((caddr_t) ps, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 787df8bae1dSRodney W. Grimes /* void */; 788628d2653SJohn Baldwin PROC_UNLOCK(p); 789df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 790df8bae1dSRodney W. Grimes return (EINTR); 791df8bae1dSRodney W. Grimes } 79231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 793df8bae1dSRodney W. Grimes 794df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 795d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 796df8bae1dSRodney W. Grimes struct osigstack_args { 797df8bae1dSRodney W. Grimes struct sigstack *nss; 798df8bae1dSRodney W. Grimes struct sigstack *oss; 799df8bae1dSRodney W. Grimes }; 800d2d3e875SBruce Evans #endif 801df8bae1dSRodney W. Grimes /* ARGSUSED */ 80226f9a767SRodney W. Grimes int 803cb226aaaSPoul-Henning Kamp osigstack(p, uap) 804df8bae1dSRodney W. Grimes struct proc *p; 805df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 806df8bae1dSRodney W. Grimes { 807df8bae1dSRodney W. Grimes struct sigstack ss; 808d034d459SMarcel Moolenaar int error; 809df8bae1dSRodney W. Grimes 810d034d459SMarcel Moolenaar if (uap->oss != NULL) { 811628d2653SJohn Baldwin PROC_LOCK(p); 812645682fdSLuoqi Chen ss.ss_sp = p->p_sigstk.ss_sp; 813d034d459SMarcel Moolenaar ss.ss_onstack = sigonstack(cpu_getstack(p)); 814628d2653SJohn Baldwin PROC_UNLOCK(p); 815d034d459SMarcel Moolenaar error = copyout(&ss, uap->oss, sizeof(struct sigstack)); 816d034d459SMarcel Moolenaar if (error) 817df8bae1dSRodney W. Grimes return (error); 818d034d459SMarcel Moolenaar } 819d034d459SMarcel Moolenaar 820d034d459SMarcel Moolenaar if (uap->nss != NULL) { 821d034d459SMarcel Moolenaar if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0) 822d034d459SMarcel Moolenaar return (error); 823628d2653SJohn Baldwin PROC_LOCK(p); 824645682fdSLuoqi Chen p->p_sigstk.ss_sp = ss.ss_sp; 825645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 826645682fdSLuoqi Chen p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 827645682fdSLuoqi Chen p->p_flag |= P_ALTSTACK; 828628d2653SJohn Baldwin PROC_UNLOCK(p); 829df8bae1dSRodney W. Grimes } 830d034d459SMarcel Moolenaar return (0); 831df8bae1dSRodney W. Grimes } 832df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 833df8bae1dSRodney W. Grimes 834d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 835df8bae1dSRodney W. Grimes struct sigaltstack_args { 8362c42a146SMarcel Moolenaar stack_t *ss; 8372c42a146SMarcel Moolenaar stack_t *oss; 838df8bae1dSRodney W. Grimes }; 839d2d3e875SBruce Evans #endif 840df8bae1dSRodney W. Grimes /* ARGSUSED */ 84126f9a767SRodney W. Grimes int 842cb226aaaSPoul-Henning Kamp sigaltstack(p, uap) 843df8bae1dSRodney W. Grimes struct proc *p; 844df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 845df8bae1dSRodney W. Grimes { 8462c42a146SMarcel Moolenaar stack_t ss; 847d034d459SMarcel Moolenaar int error, oonstack; 848df8bae1dSRodney W. Grimes 849d034d459SMarcel Moolenaar oonstack = sigonstack(cpu_getstack(p)); 850d034d459SMarcel Moolenaar 851d034d459SMarcel Moolenaar if (uap->oss != NULL) { 852628d2653SJohn Baldwin PROC_LOCK(p); 853d034d459SMarcel Moolenaar ss = p->p_sigstk; 854d034d459SMarcel Moolenaar ss.ss_flags = (p->p_flag & P_ALTSTACK) 855d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 856628d2653SJohn Baldwin PROC_UNLOCK(p); 857d034d459SMarcel Moolenaar if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0) 858df8bae1dSRodney W. Grimes return (error); 859df8bae1dSRodney W. Grimes } 860d034d459SMarcel Moolenaar 861d034d459SMarcel Moolenaar if (uap->ss != NULL) { 862d034d459SMarcel Moolenaar if (oonstack) 863d034d459SMarcel Moolenaar return (EPERM); 864d034d459SMarcel Moolenaar if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0) 865d034d459SMarcel Moolenaar return (error); 866d034d459SMarcel Moolenaar if ((ss.ss_flags & ~SS_DISABLE) != 0) 867d034d459SMarcel Moolenaar return (EINVAL); 868d034d459SMarcel Moolenaar if (!(ss.ss_flags & SS_DISABLE)) { 869806d7daaSMarcel Moolenaar if (ss.ss_size < p->p_sysent->sv_minsigstksz) 870df8bae1dSRodney W. Grimes return (ENOMEM); 871628d2653SJohn Baldwin PROC_LOCK(p); 872645682fdSLuoqi Chen p->p_sigstk = ss; 873d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 874628d2653SJohn Baldwin PROC_UNLOCK(p); 875628d2653SJohn Baldwin } else { 876628d2653SJohn Baldwin PROC_LOCK(p); 877d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 878628d2653SJohn Baldwin PROC_UNLOCK(p); 879628d2653SJohn Baldwin } 880d034d459SMarcel Moolenaar } 881df8bae1dSRodney W. Grimes return (0); 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes 884d93f860cSPoul-Henning Kamp /* 885d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 886d93f860cSPoul-Henning Kamp * cp is calling process. 887d93f860cSPoul-Henning Kamp */ 888d93f860cSPoul-Henning Kamp int 8892c42a146SMarcel Moolenaar killpg1(cp, sig, pgid, all) 890d93f860cSPoul-Henning Kamp register struct proc *cp; 8912c42a146SMarcel Moolenaar int sig, pgid, all; 892d93f860cSPoul-Henning Kamp { 893d93f860cSPoul-Henning Kamp register struct proc *p; 894d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 895d93f860cSPoul-Henning Kamp int nfound = 0; 896d93f860cSPoul-Henning Kamp 897553629ebSJake Burkholder if (all) { 898d93f860cSPoul-Henning Kamp /* 899d93f860cSPoul-Henning Kamp * broadcast 900d93f860cSPoul-Henning Kamp */ 9011005a129SJohn Baldwin sx_slock(&allproc_lock); 9022e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 903628d2653SJohn Baldwin PROC_LOCK(p); 904628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp) { 905628d2653SJohn Baldwin PROC_UNLOCK(p); 906628d2653SJohn Baldwin continue; 907628d2653SJohn Baldwin } 908628d2653SJohn Baldwin PROC_UNLOCK(p); 909628d2653SJohn Baldwin /* 910628d2653SJohn Baldwin * XXX: this locking needs work.. specifically the 911628d2653SJohn Baldwin * session checks.. 912628d2653SJohn Baldwin */ 913628d2653SJohn Baldwin if (!CANSIGNAL(cp, p, sig)) 914d93f860cSPoul-Henning Kamp continue; 915d93f860cSPoul-Henning Kamp nfound++; 916628d2653SJohn Baldwin if (sig) { 917628d2653SJohn Baldwin PROC_LOCK(p); 9182c42a146SMarcel Moolenaar psignal(p, sig); 919628d2653SJohn Baldwin PROC_UNLOCK(p); 920628d2653SJohn Baldwin } 921d93f860cSPoul-Henning Kamp } 9221005a129SJohn Baldwin sx_sunlock(&allproc_lock); 923553629ebSJake Burkholder } else { 924d93f860cSPoul-Henning Kamp if (pgid == 0) 925d93f860cSPoul-Henning Kamp /* 926d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 927d93f860cSPoul-Henning Kamp */ 928d93f860cSPoul-Henning Kamp pgrp = cp->p_pgrp; 929d93f860cSPoul-Henning Kamp else { 930d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 931d93f860cSPoul-Henning Kamp if (pgrp == NULL) 932d93f860cSPoul-Henning Kamp return (ESRCH); 933d93f860cSPoul-Henning Kamp } 9342e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 935628d2653SJohn Baldwin PROC_LOCK(p); 936628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 937628d2653SJohn Baldwin PROC_UNLOCK(p); 938628d2653SJohn Baldwin continue; 939628d2653SJohn Baldwin } 940628d2653SJohn Baldwin PROC_UNLOCK(p); 941628d2653SJohn Baldwin mtx_lock_spin(&sched_lock); 942628d2653SJohn Baldwin if (p->p_stat == SZOMB) { 943628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 944628d2653SJohn Baldwin continue; 945628d2653SJohn Baldwin } 946628d2653SJohn Baldwin mtx_unlock_spin(&sched_lock); 947628d2653SJohn Baldwin /* XXX: locking b0rked */ 948628d2653SJohn Baldwin if (!CANSIGNAL(cp, p, sig)) 949d93f860cSPoul-Henning Kamp continue; 950d93f860cSPoul-Henning Kamp nfound++; 951628d2653SJohn Baldwin if (sig) { 952628d2653SJohn Baldwin PROC_LOCK(p); 9532c42a146SMarcel Moolenaar psignal(p, sig); 954628d2653SJohn Baldwin PROC_UNLOCK(p); 955628d2653SJohn Baldwin } 956d93f860cSPoul-Henning Kamp } 957d93f860cSPoul-Henning Kamp } 958d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 959d93f860cSPoul-Henning Kamp } 960d93f860cSPoul-Henning Kamp 961d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 962df8bae1dSRodney W. Grimes struct kill_args { 963df8bae1dSRodney W. Grimes int pid; 964df8bae1dSRodney W. Grimes int signum; 965df8bae1dSRodney W. Grimes }; 966d2d3e875SBruce Evans #endif 967df8bae1dSRodney W. Grimes /* ARGSUSED */ 96826f9a767SRodney W. Grimes int 969cb226aaaSPoul-Henning Kamp kill(cp, uap) 970df8bae1dSRodney W. Grimes register struct proc *cp; 971df8bae1dSRodney W. Grimes register struct kill_args *uap; 972df8bae1dSRodney W. Grimes { 973df8bae1dSRodney W. Grimes register struct proc *p; 974df8bae1dSRodney W. Grimes 9752c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 976df8bae1dSRodney W. Grimes return (EINVAL); 977df8bae1dSRodney W. Grimes if (uap->pid > 0) { 978df8bae1dSRodney W. Grimes /* kill single process */ 979df8bae1dSRodney W. Grimes if ((p = pfind(uap->pid)) == NULL) 980df8bae1dSRodney W. Grimes return (ESRCH); 981628d2653SJohn Baldwin /* XXX: locking b0rked */ 982a9e0361bSPoul-Henning Kamp if (!CANSIGNAL(cp, p, uap->signum)) 983df8bae1dSRodney W. Grimes return (EPERM); 984628d2653SJohn Baldwin if (uap->signum) { 985628d2653SJohn Baldwin PROC_LOCK(p); 986df8bae1dSRodney W. Grimes psignal(p, uap->signum); 987628d2653SJohn Baldwin PROC_UNLOCK(p); 988628d2653SJohn Baldwin } 989df8bae1dSRodney W. Grimes return (0); 990df8bae1dSRodney W. Grimes } 991df8bae1dSRodney W. Grimes switch (uap->pid) { 992df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 993df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, 0, 1)); 994df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 995df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, 0, 0)); 996df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 997df8bae1dSRodney W. Grimes return (killpg1(cp, uap->signum, -uap->pid, 0)); 998df8bae1dSRodney W. Grimes } 999df8bae1dSRodney W. Grimes /* NOTREACHED */ 1000df8bae1dSRodney W. Grimes } 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1003d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1004df8bae1dSRodney W. Grimes struct okillpg_args { 1005df8bae1dSRodney W. Grimes int pgid; 1006df8bae1dSRodney W. Grimes int signum; 1007df8bae1dSRodney W. Grimes }; 1008d2d3e875SBruce Evans #endif 1009df8bae1dSRodney W. Grimes /* ARGSUSED */ 101026f9a767SRodney W. Grimes int 1011cb226aaaSPoul-Henning Kamp okillpg(p, uap) 1012df8bae1dSRodney W. Grimes struct proc *p; 1013df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1014df8bae1dSRodney W. Grimes { 1015df8bae1dSRodney W. Grimes 10162c42a146SMarcel Moolenaar if ((u_int)uap->signum > _SIG_MAXSIG) 1017df8bae1dSRodney W. Grimes return (EINVAL); 1018df8bae1dSRodney W. Grimes return (killpg1(p, uap->signum, uap->pgid, 0)); 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes /* 1023df8bae1dSRodney W. Grimes * Send a signal to a process group. 1024df8bae1dSRodney W. Grimes */ 1025df8bae1dSRodney W. Grimes void 10262c42a146SMarcel Moolenaar gsignal(pgid, sig) 10272c42a146SMarcel Moolenaar int pgid, sig; 1028df8bae1dSRodney W. Grimes { 1029df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1030df8bae1dSRodney W. Grimes 1031df8bae1dSRodney W. Grimes if (pgid && (pgrp = pgfind(pgid))) 10322c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1033df8bae1dSRodney W. Grimes } 1034df8bae1dSRodney W. Grimes 1035df8bae1dSRodney W. Grimes /* 1036df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1037df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1038df8bae1dSRodney W. Grimes */ 1039df8bae1dSRodney W. Grimes void 10402c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1041df8bae1dSRodney W. Grimes struct pgrp *pgrp; 10422c42a146SMarcel Moolenaar int sig, checkctty; 1043df8bae1dSRodney W. Grimes { 1044df8bae1dSRodney W. Grimes register struct proc *p; 1045df8bae1dSRodney W. Grimes 1046628d2653SJohn Baldwin if (pgrp) { 1047628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1048628d2653SJohn Baldwin PROC_LOCK(p); 1049df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 10502c42a146SMarcel Moolenaar psignal(p, sig); 1051628d2653SJohn Baldwin PROC_UNLOCK(p); 1052628d2653SJohn Baldwin } 1053628d2653SJohn Baldwin } 1054df8bae1dSRodney W. Grimes } 1055df8bae1dSRodney W. Grimes 1056df8bae1dSRodney W. Grimes /* 1057df8bae1dSRodney W. Grimes * Send a signal caused by a trap to the current process. 1058df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1059df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1060df8bae1dSRodney W. Grimes */ 1061df8bae1dSRodney W. Grimes void 10622c42a146SMarcel Moolenaar trapsignal(p, sig, code) 1063df8bae1dSRodney W. Grimes struct proc *p; 10642c42a146SMarcel Moolenaar register int sig; 10658674077aSJeffrey Hsu u_long code; 1066df8bae1dSRodney W. Grimes { 1067df8bae1dSRodney W. Grimes register struct sigacts *ps = p->p_sigacts; 1068df8bae1dSRodney W. Grimes 1069628d2653SJohn Baldwin PROC_LOCK(p); 10702c42a146SMarcel Moolenaar if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 1071d96cfeaeSMarcel Moolenaar !SIGISMEMBER(p->p_sigmask, sig)) { 1072df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1073df8bae1dSRodney W. Grimes #ifdef KTRACE 1074df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 10752c42a146SMarcel Moolenaar ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 10762c42a146SMarcel Moolenaar &p->p_sigmask, code); 1077df8bae1dSRodney W. Grimes #endif 1078628d2653SJohn Baldwin PROC_UNLOCK(p); /* XXX ??? */ 10792c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 10802c42a146SMarcel Moolenaar &p->p_sigmask, code); 1081628d2653SJohn Baldwin PROC_LOCK(p); 10822c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 10832c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 10842c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 10852c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1086289ccde0SPeter Wemm /* 10872c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1088289ccde0SPeter Wemm */ 10892c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 10902c42a146SMarcel Moolenaar if (sig != SIGCONT && 10912c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 10922c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 10932c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1094dedc04feSPeter Wemm } 10956f841fb7SMarcel Moolenaar } else { 10966626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 10972c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 10982c42a146SMarcel Moolenaar psignal(p, sig); 1099df8bae1dSRodney W. Grimes } 1100628d2653SJohn Baldwin PROC_UNLOCK(p); 1101df8bae1dSRodney W. Grimes } 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes /* 1104df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1105df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1106df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1107df8bae1dSRodney W. Grimes * 1108df8bae1dSRodney W. Grimes * Exceptions: 1109df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1110df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1111df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1112df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1113df8bae1dSRodney W. Grimes * 1114df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 1115df8bae1dSRodney W. Grimes */ 1116df8bae1dSRodney W. Grimes void 11172c42a146SMarcel Moolenaar psignal(p, sig) 1118df8bae1dSRodney W. Grimes register struct proc *p; 11192c42a146SMarcel Moolenaar register int sig; 1120df8bae1dSRodney W. Grimes { 112140447cd4SJohn Baldwin register int prop; 1122df8bae1dSRodney W. Grimes register sig_t action; 1123df8bae1dSRodney W. Grimes 11242c42a146SMarcel Moolenaar if (sig > _SIG_MAXSIG || sig <= 0) { 11252c42a146SMarcel Moolenaar printf("psignal: signal %d\n", sig); 1126df8bae1dSRodney W. Grimes panic("psignal signal number"); 11272a024a2bSSean Eric Fagan } 11282c42a146SMarcel Moolenaar 1129628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1130cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1131cb679c38SJonathan Lemon 11322c42a146SMarcel Moolenaar prop = sigprop(sig); 1133df8bae1dSRodney W. Grimes 1134df8bae1dSRodney W. Grimes /* 11352a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 11362a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 11372a024a2bSSean Eric Fagan * a chance, as well. 1138df8bae1dSRodney W. Grimes */ 11392a024a2bSSean Eric Fagan if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1140df8bae1dSRodney W. Grimes action = SIG_DFL; 1141df8bae1dSRodney W. Grimes else { 1142df8bae1dSRodney W. Grimes /* 1143df8bae1dSRodney W. Grimes * If the signal is being ignored, 1144df8bae1dSRodney W. Grimes * then we forget about it immediately. 1145df8bae1dSRodney W. Grimes * (Note: we don't set SIGCONT in p_sigignore, 1146df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1147df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1148df8bae1dSRodney W. Grimes */ 1149628d2653SJohn Baldwin if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1150df8bae1dSRodney W. Grimes return; 11512c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1152df8bae1dSRodney W. Grimes action = SIG_HOLD; 11532c42a146SMarcel Moolenaar else if (SIGISMEMBER(p->p_sigcatch, sig)) 1154df8bae1dSRodney W. Grimes action = SIG_CATCH; 1155df8bae1dSRodney W. Grimes else 1156df8bae1dSRodney W. Grimes action = SIG_DFL; 1157df8bae1dSRodney W. Grimes } 1158df8bae1dSRodney W. Grimes 11599ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1160df8bae1dSRodney W. Grimes if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1161df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1162df8bae1dSRodney W. Grimes p->p_nice = NZERO; 11639ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1164df8bae1dSRodney W. Grimes 1165df8bae1dSRodney W. Grimes if (prop & SA_CONT) 11662c42a146SMarcel Moolenaar SIG_STOPSIGMASK(p->p_siglist); 1167df8bae1dSRodney W. Grimes 1168df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1169df8bae1dSRodney W. Grimes /* 1170df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1171df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1172df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1173df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1174df8bae1dSRodney W. Grimes */ 1175df8bae1dSRodney W. Grimes if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1176628d2653SJohn Baldwin action == SIG_DFL) 1177df8bae1dSRodney W. Grimes return; 11782c42a146SMarcel Moolenaar SIG_CONTSIGMASK(p->p_siglist); 1179df8bae1dSRodney W. Grimes } 11802c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 1181df8bae1dSRodney W. Grimes 1182df8bae1dSRodney W. Grimes /* 1183df8bae1dSRodney W. Grimes * Defer further processing for signals which are held, 1184df8bae1dSRodney W. Grimes * except that stopped processes must be continued by SIGCONT. 1185df8bae1dSRodney W. Grimes */ 11869ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11871c32c37cSJohn Baldwin if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) { 11889ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1189df8bae1dSRodney W. Grimes return; 11901c32c37cSJohn Baldwin } 1191df8bae1dSRodney W. Grimes switch (p->p_stat) { 1192df8bae1dSRodney W. Grimes 1193df8bae1dSRodney W. Grimes case SSLEEP: 1194df8bae1dSRodney W. Grimes /* 1195df8bae1dSRodney W. Grimes * If process is sleeping uninterruptibly 1196df8bae1dSRodney W. Grimes * we can't interrupt the sleep... the signal will 1197df8bae1dSRodney W. Grimes * be noticed when the process returns through 1198df8bae1dSRodney W. Grimes * trap() or syscall(). 1199df8bae1dSRodney W. Grimes */ 120040447cd4SJohn Baldwin if ((p->p_sflag & PS_SINTR) == 0) { 12019ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1202df8bae1dSRodney W. Grimes goto out; 12033e6831f5SJohn Baldwin } 1204df8bae1dSRodney W. Grimes /* 1205df8bae1dSRodney W. Grimes * Process is sleeping and traced... make it runnable 1206df8bae1dSRodney W. Grimes * so it can discover the signal in issignal() and stop 1207df8bae1dSRodney W. Grimes * for the parent. 1208df8bae1dSRodney W. Grimes */ 1209df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED) 1210df8bae1dSRodney W. Grimes goto run; 12119ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1212df8bae1dSRodney W. Grimes /* 1213df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored) and process is 1214df8bae1dSRodney W. Grimes * asleep, we are finished; the process should not 1215df8bae1dSRodney W. Grimes * be awakened. 1216df8bae1dSRodney W. Grimes */ 1217df8bae1dSRodney W. Grimes if ((prop & SA_CONT) && action == SIG_DFL) { 12182c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1219df8bae1dSRodney W. Grimes goto out; 1220df8bae1dSRodney W. Grimes } 1221df8bae1dSRodney W. Grimes /* 1222df8bae1dSRodney W. Grimes * When a sleeping process receives a stop 1223df8bae1dSRodney W. Grimes * signal, process immediately if possible. 1224df8bae1dSRodney W. Grimes * All other (caught or default) signals 1225df8bae1dSRodney W. Grimes * cause the process to run. 1226df8bae1dSRodney W. Grimes */ 1227df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 12283e6831f5SJohn Baldwin if (action != SIG_DFL) 1229df8bae1dSRodney W. Grimes goto runfast; 1230df8bae1dSRodney W. Grimes /* 1231df8bae1dSRodney W. Grimes * If a child holding parent blocked, 1232df8bae1dSRodney W. Grimes * stopping could cause deadlock. 1233df8bae1dSRodney W. Grimes */ 1234df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 1235df8bae1dSRodney W. Grimes goto out; 12362c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 12372c42a146SMarcel Moolenaar p->p_xstat = sig; 1238628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1239628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1240df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1241628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1242628d2653SJohn Baldwin } 1243df8bae1dSRodney W. Grimes stop(p); 1244df8bae1dSRodney W. Grimes goto out; 12453e6831f5SJohn Baldwin } else 1246df8bae1dSRodney W. Grimes goto runfast; 1247df8bae1dSRodney W. Grimes /* NOTREACHED */ 1248df8bae1dSRodney W. Grimes 1249df8bae1dSRodney W. Grimes case SSTOP: 12509ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1251df8bae1dSRodney W. Grimes /* 1252df8bae1dSRodney W. Grimes * If traced process is already stopped, 1253df8bae1dSRodney W. Grimes * then no further action is necessary. 1254df8bae1dSRodney W. Grimes */ 125540447cd4SJohn Baldwin if (p->p_flag & P_TRACED) 1256df8bae1dSRodney W. Grimes goto out; 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes /* 1259df8bae1dSRodney W. Grimes * Kill signal always sets processes running. 1260df8bae1dSRodney W. Grimes */ 12612c42a146SMarcel Moolenaar if (sig == SIGKILL) 1262df8bae1dSRodney W. Grimes goto runfast; 1263df8bae1dSRodney W. Grimes 1264df8bae1dSRodney W. Grimes if (prop & SA_CONT) { 1265df8bae1dSRodney W. Grimes /* 1266df8bae1dSRodney W. Grimes * If SIGCONT is default (or ignored), we continue the 1267df8bae1dSRodney W. Grimes * process but don't leave the signal in p_siglist, as 1268df8bae1dSRodney W. Grimes * it has no further action. If SIGCONT is held, we 1269df8bae1dSRodney W. Grimes * continue the process and leave the signal in 1270df8bae1dSRodney W. Grimes * p_siglist. If the process catches SIGCONT, let it 1271df8bae1dSRodney W. Grimes * handle the signal itself. If it isn't waiting on 1272df8bae1dSRodney W. Grimes * an event, then it goes back to run state. 1273df8bae1dSRodney W. Grimes * Otherwise, process goes back to sleep state. 1274df8bae1dSRodney W. Grimes */ 1275df8bae1dSRodney W. Grimes if (action == SIG_DFL) 12762c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1277df8bae1dSRodney W. Grimes if (action == SIG_CATCH) 1278df8bae1dSRodney W. Grimes goto runfast; 12799ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 128040447cd4SJohn Baldwin if (p->p_wchan == NULL) 1281df8bae1dSRodney W. Grimes goto run; 1282df8bae1dSRodney W. Grimes p->p_stat = SSLEEP; 12839ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1284df8bae1dSRodney W. Grimes goto out; 1285df8bae1dSRodney W. Grimes } 1286df8bae1dSRodney W. Grimes 1287df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1288df8bae1dSRodney W. Grimes /* 1289df8bae1dSRodney W. Grimes * Already stopped, don't need to stop again. 1290df8bae1dSRodney W. Grimes * (If we did the shell could get confused.) 1291df8bae1dSRodney W. Grimes */ 12922c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1293df8bae1dSRodney W. Grimes goto out; 1294df8bae1dSRodney W. Grimes } 1295df8bae1dSRodney W. Grimes 1296df8bae1dSRodney W. Grimes /* 1297df8bae1dSRodney W. Grimes * If process is sleeping interruptibly, then simulate a 1298df8bae1dSRodney W. Grimes * wakeup so that when it is continued, it will be made 1299df8bae1dSRodney W. Grimes * runnable and can look at the signal. But don't make 1300df8bae1dSRodney W. Grimes * the process runnable, leave it stopped. 1301df8bae1dSRodney W. Grimes */ 13029ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 130340447cd4SJohn Baldwin if (p->p_wchan && p->p_sflag & PS_SINTR) { 130440447cd4SJohn Baldwin if (p->p_sflag & PS_CVWAITQ) 1305238510fcSJason Evans cv_waitq_remove(p); 1306238510fcSJason Evans else 1307df8bae1dSRodney W. Grimes unsleep(p); 1308238510fcSJason Evans } 13099ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1310df8bae1dSRodney W. Grimes goto out; 1311df8bae1dSRodney W. Grimes 1312df8bae1dSRodney W. Grimes default: 1313df8bae1dSRodney W. Grimes /* 1314df8bae1dSRodney W. Grimes * SRUN, SIDL, SZOMB do nothing with the signal, 1315df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1316df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1317df8bae1dSRodney W. Grimes */ 1318d2ef4060SBruce Evans if (p->p_stat == SRUN) { 1319df8bae1dSRodney W. Grimes signotify(p); 1320142ba5f3SJohn Baldwin mtx_unlock_spin(&sched_lock); 13213163861cSTor Egge #ifdef SMP 13223163861cSTor Egge forward_signal(p); 13233163861cSTor Egge #endif 1324d2ef4060SBruce Evans } else 13259ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1326df8bae1dSRodney W. Grimes goto out; 1327df8bae1dSRodney W. Grimes } 1328df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1329df8bae1dSRodney W. Grimes 1330df8bae1dSRodney W. Grimes runfast: 1331df8bae1dSRodney W. Grimes /* 1332df8bae1dSRodney W. Grimes * Raise priority to at least PUSER. 1333df8bae1dSRodney W. Grimes */ 13349ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1335d5a08a60SJake Burkholder if (p->p_pri.pri_level > PUSER) 1336d5a08a60SJake Burkholder p->p_pri.pri_level = PUSER; 1337df8bae1dSRodney W. Grimes run: 13383e6831f5SJohn Baldwin /* If we jump here, sched_lock has to be owned. */ 13393e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 1340df8bae1dSRodney W. Grimes setrunnable(p); 13419ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 13423e6831f5SJohn Baldwin out: 13433e6831f5SJohn Baldwin /* If we jump here, sched_lock should not be owned. */ 13443e6831f5SJohn Baldwin mtx_assert(&sched_lock, MA_NOTOWNED); 1345df8bae1dSRodney W. Grimes } 1346df8bae1dSRodney W. Grimes 1347df8bae1dSRodney W. Grimes /* 1348df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1349df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1350df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1351df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1352df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1353df8bae1dSRodney W. Grimes * by checking the pending signal masks in the CURSIG macro.) The normal call 1354df8bae1dSRodney W. Grimes * sequence is 1355df8bae1dSRodney W. Grimes * 13562c42a146SMarcel Moolenaar * while (sig = CURSIG(curproc)) 13572c42a146SMarcel Moolenaar * postsig(sig); 1358df8bae1dSRodney W. Grimes */ 135926f9a767SRodney W. Grimes int 1360df8bae1dSRodney W. Grimes issignal(p) 1361df8bae1dSRodney W. Grimes register struct proc *p; 1362df8bae1dSRodney W. Grimes { 13632c42a146SMarcel Moolenaar sigset_t mask; 13642c42a146SMarcel Moolenaar register int sig, prop; 1365df8bae1dSRodney W. Grimes 1366628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1367df8bae1dSRodney W. Grimes for (;;) { 13682a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 13692a024a2bSSean Eric Fagan 13702c42a146SMarcel Moolenaar mask = p->p_siglist; 13712c42a146SMarcel Moolenaar SIGSETNAND(mask, p->p_sigmask); 1372df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 13732c42a146SMarcel Moolenaar SIG_STOPSIGMASK(mask); 13742c42a146SMarcel Moolenaar if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1375df8bae1dSRodney W. Grimes return (0); 13762c42a146SMarcel Moolenaar sig = sig_ffs(&mask); 13772c42a146SMarcel Moolenaar prop = sigprop(sig); 13782a024a2bSSean Eric Fagan 1379628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 13802a024a2bSSean Eric Fagan 1381df8bae1dSRodney W. Grimes /* 1382df8bae1dSRodney W. Grimes * We should see pending but ignored signals 1383df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 1384df8bae1dSRodney W. Grimes */ 13852c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 13862c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 1387df8bae1dSRodney W. Grimes continue; 1388df8bae1dSRodney W. Grimes } 1389df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1390df8bae1dSRodney W. Grimes /* 1391df8bae1dSRodney W. Grimes * If traced, always stop, and stay 1392df8bae1dSRodney W. Grimes * stopped until released by the parent. 1393df8bae1dSRodney W. Grimes */ 13942c42a146SMarcel Moolenaar p->p_xstat = sig; 1395628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1396df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1397628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1398df8bae1dSRodney W. Grimes do { 1399df8bae1dSRodney W. Grimes stop(p); 14009ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1401628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 14027da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 1403df8bae1dSRodney W. Grimes mi_switch(); 14049ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 140520cdcc5bSJohn Baldwin PICKUP_GIANT(); 1406628d2653SJohn Baldwin PROC_LOCK(p); 14072a024a2bSSean Eric Fagan } while (!trace_req(p) 14082a024a2bSSean Eric Fagan && p->p_flag & P_TRACED); 1409df8bae1dSRodney W. Grimes 1410df8bae1dSRodney W. Grimes /* 1411df8bae1dSRodney W. Grimes * If the traced bit got turned off, go back up 1412df8bae1dSRodney W. Grimes * to the top to rescan signals. This ensures 1413df8bae1dSRodney W. Grimes * that p_sig* and ps_sigact are consistent. 1414df8bae1dSRodney W. Grimes */ 1415df8bae1dSRodney W. Grimes if ((p->p_flag & P_TRACED) == 0) 1416df8bae1dSRodney W. Grimes continue; 1417df8bae1dSRodney W. Grimes 1418df8bae1dSRodney W. Grimes /* 1419df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 1420df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 1421df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 1422df8bae1dSRodney W. Grimes */ 14232c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* clear old signal */ 14242c42a146SMarcel Moolenaar sig = p->p_xstat; 14252c42a146SMarcel Moolenaar if (sig == 0) 1426df8bae1dSRodney W. Grimes continue; 1427df8bae1dSRodney W. Grimes 1428df8bae1dSRodney W. Grimes /* 1429df8bae1dSRodney W. Grimes * Put the new signal into p_siglist. If the 1430df8bae1dSRodney W. Grimes * signal is being masked, look for other signals. 1431df8bae1dSRodney W. Grimes */ 14322c42a146SMarcel Moolenaar SIGADDSET(p->p_siglist, sig); 14332c42a146SMarcel Moolenaar if (SIGISMEMBER(p->p_sigmask, sig)) 1434df8bae1dSRodney W. Grimes continue; 1435df8bae1dSRodney W. Grimes } 1436df8bae1dSRodney W. Grimes 1437df8bae1dSRodney W. Grimes /* 1438df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 1439df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 1440df8bae1dSRodney W. Grimes * to clear it from the pending mask. 1441df8bae1dSRodney W. Grimes */ 14422c42a146SMarcel Moolenaar switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1443df8bae1dSRodney W. Grimes 14440b53fbe8SBruce Evans case (int)SIG_DFL: 1445df8bae1dSRodney W. Grimes /* 1446df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 1447df8bae1dSRodney W. Grimes */ 1448df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 1449df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1450df8bae1dSRodney W. Grimes /* 1451df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 1452df8bae1dSRodney W. Grimes * in init? XXX 1453df8bae1dSRodney W. Grimes */ 1454d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 14552c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 1456df8bae1dSRodney W. Grimes #endif 1457df8bae1dSRodney W. Grimes break; /* == ignore */ 1458df8bae1dSRodney W. Grimes } 1459df8bae1dSRodney W. Grimes /* 1460df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 1461df8bae1dSRodney W. Grimes * with default action, stop here, 1462df8bae1dSRodney W. Grimes * then clear the signal. However, 1463df8bae1dSRodney W. Grimes * if process is member of an orphaned 1464df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 1465df8bae1dSRodney W. Grimes */ 1466df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1467df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 1468df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 1469df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 1470df8bae1dSRodney W. Grimes break; /* == ignore */ 14712c42a146SMarcel Moolenaar p->p_xstat = sig; 1472df8bae1dSRodney W. Grimes stop(p); 1473628d2653SJohn Baldwin if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) { 1474628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 1475df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 1476628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 1477628d2653SJohn Baldwin } 14789ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1479628d2653SJohn Baldwin PROC_UNLOCK_NOSWITCH(p); 14807da6f977SJake Burkholder DROP_GIANT_NOSWITCH(); 1481df8bae1dSRodney W. Grimes mi_switch(); 14829ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 148320cdcc5bSJohn Baldwin PICKUP_GIANT(); 1484628d2653SJohn Baldwin PROC_LOCK(p); 1485df8bae1dSRodney W. Grimes break; 1486df8bae1dSRodney W. Grimes } else if (prop & SA_IGNORE) { 1487df8bae1dSRodney W. Grimes /* 1488df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 1489df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 1490df8bae1dSRodney W. Grimes */ 1491df8bae1dSRodney W. Grimes break; /* == ignore */ 1492df8bae1dSRodney W. Grimes } else 14932c42a146SMarcel Moolenaar return (sig); 1494df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1495df8bae1dSRodney W. Grimes 14960b53fbe8SBruce Evans case (int)SIG_IGN: 1497df8bae1dSRodney W. Grimes /* 1498df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 1499df8bae1dSRodney W. Grimes * to take action on an ignored signal other 1500df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 1501df8bae1dSRodney W. Grimes */ 1502df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 1503df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 1504df8bae1dSRodney W. Grimes printf("issignal\n"); 1505df8bae1dSRodney W. Grimes break; /* == ignore */ 1506df8bae1dSRodney W. Grimes 1507df8bae1dSRodney W. Grimes default: 1508df8bae1dSRodney W. Grimes /* 1509df8bae1dSRodney W. Grimes * This signal has an action, let 1510df8bae1dSRodney W. Grimes * postsig() process it. 1511df8bae1dSRodney W. Grimes */ 15122c42a146SMarcel Moolenaar return (sig); 1513df8bae1dSRodney W. Grimes } 15142c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1515df8bae1dSRodney W. Grimes } 1516df8bae1dSRodney W. Grimes /* NOTREACHED */ 1517df8bae1dSRodney W. Grimes } 1518df8bae1dSRodney W. Grimes 1519df8bae1dSRodney W. Grimes /* 1520df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 1521df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 152298f03f90SJake Burkholder * on the run queue. Must be called with at least a shared hold of the 152398f03f90SJake Burkholder * proctree lock. 1524df8bae1dSRodney W. Grimes */ 152526f9a767SRodney W. Grimes void 1526df8bae1dSRodney W. Grimes stop(p) 1527df8bae1dSRodney W. Grimes register struct proc *p; 1528df8bae1dSRodney W. Grimes { 1529df8bae1dSRodney W. Grimes 1530628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 15319ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1532df8bae1dSRodney W. Grimes p->p_stat = SSTOP; 1533df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 1534df8bae1dSRodney W. Grimes wakeup((caddr_t)p->p_pptr); 15359ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1536df8bae1dSRodney W. Grimes } 1537df8bae1dSRodney W. Grimes 1538df8bae1dSRodney W. Grimes /* 1539df8bae1dSRodney W. Grimes * Take the action for the specified signal 1540df8bae1dSRodney W. Grimes * from the current set of pending signals. 1541df8bae1dSRodney W. Grimes */ 1542df8bae1dSRodney W. Grimes void 15432c42a146SMarcel Moolenaar postsig(sig) 15442c42a146SMarcel Moolenaar register int sig; 1545df8bae1dSRodney W. Grimes { 1546df8bae1dSRodney W. Grimes register struct proc *p = curproc; 1547628d2653SJohn Baldwin struct sigacts *ps; 15482c42a146SMarcel Moolenaar sig_t action; 15492c42a146SMarcel Moolenaar sigset_t returnmask; 15502c42a146SMarcel Moolenaar int code; 1551df8bae1dSRodney W. Grimes 15522c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 15535526d2d9SEivind Eklund 1554628d2653SJohn Baldwin PROC_LOCK(p); 1555628d2653SJohn Baldwin ps = p->p_sigacts; 15562c42a146SMarcel Moolenaar SIGDELSET(p->p_siglist, sig); 15572c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 1558df8bae1dSRodney W. Grimes #ifdef KTRACE 1559df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_PSIG)) 1560645682fdSLuoqi Chen ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 15612c42a146SMarcel Moolenaar &p->p_oldsigmask : &p->p_sigmask, 0); 1562df8bae1dSRodney W. Grimes #endif 1563628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 15642a024a2bSSean Eric Fagan 1565df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 1566df8bae1dSRodney W. Grimes /* 1567df8bae1dSRodney W. Grimes * Default action, where the default is to kill 1568df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 1569df8bae1dSRodney W. Grimes */ 15702c42a146SMarcel Moolenaar sigexit(p, sig); 1571df8bae1dSRodney W. Grimes /* NOTREACHED */ 1572df8bae1dSRodney W. Grimes } else { 1573df8bae1dSRodney W. Grimes /* 1574df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 1575df8bae1dSRodney W. Grimes */ 15762c42a146SMarcel Moolenaar KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 15775526d2d9SEivind Eklund ("postsig action")); 1578df8bae1dSRodney W. Grimes /* 1579df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 1580645682fdSLuoqi Chen * occurrences of this signal. 1581df8bae1dSRodney W. Grimes * 1582645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 1583df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 1584645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 1585df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 1586df8bae1dSRodney W. Grimes */ 1587645682fdSLuoqi Chen if (p->p_flag & P_OLDMASK) { 15886626c604SJulian Elischer returnmask = p->p_oldsigmask; 1589645682fdSLuoqi Chen p->p_flag &= ~P_OLDMASK; 1590df8bae1dSRodney W. Grimes } else 1591df8bae1dSRodney W. Grimes returnmask = p->p_sigmask; 15922c42a146SMarcel Moolenaar 15932c42a146SMarcel Moolenaar SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15942c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15952c42a146SMarcel Moolenaar SIGADDSET(p->p_sigmask, sig); 15962c42a146SMarcel Moolenaar 15972c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1598289ccde0SPeter Wemm /* 15992c42a146SMarcel Moolenaar * See do_sigaction() for origin of this code. 1600289ccde0SPeter Wemm */ 16012c42a146SMarcel Moolenaar SIGDELSET(p->p_sigcatch, sig); 16022c42a146SMarcel Moolenaar if (sig != SIGCONT && 16032c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 16042c42a146SMarcel Moolenaar SIGADDSET(p->p_sigignore, sig); 16052c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1606dedc04feSPeter Wemm } 1607df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 16082c42a146SMarcel Moolenaar if (p->p_sig != sig) { 1609df8bae1dSRodney W. Grimes code = 0; 1610df8bae1dSRodney W. Grimes } else { 16116626c604SJulian Elischer code = p->p_code; 16126626c604SJulian Elischer p->p_code = 0; 16136626c604SJulian Elischer p->p_sig = 0; 1614df8bae1dSRodney W. Grimes } 1615628d2653SJohn Baldwin PROC_UNLOCK(p); 16162c42a146SMarcel Moolenaar (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1617df8bae1dSRodney W. Grimes } 1618df8bae1dSRodney W. Grimes } 1619df8bae1dSRodney W. Grimes 1620df8bae1dSRodney W. Grimes /* 1621df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 1622df8bae1dSRodney W. Grimes */ 162326f9a767SRodney W. Grimes void 1624df8bae1dSRodney W. Grimes killproc(p, why) 1625df8bae1dSRodney W. Grimes struct proc *p; 1626df8bae1dSRodney W. Grimes char *why; 1627df8bae1dSRodney W. Grimes { 16280384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 16290384fff8SJason Evans p, p->p_pid, p->p_comm); 1630729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1631729b1e51SDavid Greenman p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1632628d2653SJohn Baldwin PROC_LOCK(p); 1633df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 1634628d2653SJohn Baldwin PROC_UNLOCK(p); 1635df8bae1dSRodney W. Grimes } 1636df8bae1dSRodney W. Grimes 1637df8bae1dSRodney W. Grimes /* 1638df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 1639df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 1640df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 1641df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 1642df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 1643df8bae1dSRodney W. Grimes * does not return. 1644df8bae1dSRodney W. Grimes */ 164526f9a767SRodney W. Grimes void 16462c42a146SMarcel Moolenaar sigexit(p, sig) 1647df8bae1dSRodney W. Grimes register struct proc *p; 16482c42a146SMarcel Moolenaar int sig; 1649df8bae1dSRodney W. Grimes { 1650df8bae1dSRodney W. Grimes 1651628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1652df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 16532c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 16542c42a146SMarcel Moolenaar p->p_sig = sig; 1655c364e17eSAndrey A. Chernov /* 1656c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 1657c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 1658c364e17eSAndrey A. Chernov * these messages.) 1659c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 1660c364e17eSAndrey A. Chernov */ 1661628d2653SJohn Baldwin PROC_UNLOCK(p); 1662c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 1663c31146a1SJohn Baldwin mtx_lock(&Giant); 1664fca666a1SJulian Elischer if (coredump(p) == 0) 16652c42a146SMarcel Moolenaar sig |= WCOREFLAG; 166657308494SJoerg Wunsch if (kern_logsigexit) 166757308494SJoerg Wunsch log(LOG_INFO, 166857308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 16693d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 16703d1b21c6SAndrey A. Chernov p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 16712c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 16722c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 1673c31146a1SJohn Baldwin } else { 1674628d2653SJohn Baldwin PROC_UNLOCK(p); 1675628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 1676628d2653SJohn Baldwin mtx_lock(&Giant); 1677c31146a1SJohn Baldwin } 16782c42a146SMarcel Moolenaar exit1(p, W_EXITCODE(0, sig)); 1679df8bae1dSRodney W. Grimes /* NOTREACHED */ 1680df8bae1dSRodney W. Grimes } 1681df8bae1dSRodney W. Grimes 1682c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1683c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1684c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 1685c5edb423SSean Eric Fagan 1686c5edb423SSean Eric Fagan /* 1687c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 1688c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 1689c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 1690c5edb423SSean Eric Fagan * %N name of process ("name") 1691c5edb423SSean Eric Fagan * %P process id (pid) 1692c5edb423SSean Eric Fagan * %U user id (uid) 1693c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 1694c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1695c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 1696c5edb423SSean Eric Fagan */ 1697c5edb423SSean Eric Fagan 1698fca666a1SJulian Elischer static char * 1699c5edb423SSean Eric Fagan expand_name(name, uid, pid) 170087f1de5fSBill Fumerola const char *name; uid_t uid; pid_t pid; { 1701c5edb423SSean Eric Fagan char *temp; 1702c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 1703c5edb423SSean Eric Fagan int i, n; 1704c5edb423SSean Eric Fagan char *format = corefilename; 1705ce38ca0fSAlfred Perlstein size_t namelen; 1706c5edb423SSean Eric Fagan 1707ce38ca0fSAlfred Perlstein temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 17080bfe2990SEivind Eklund if (temp == NULL) 17090bfe2990SEivind Eklund return NULL; 1710ce38ca0fSAlfred Perlstein namelen = strlen(name); 1711ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1712c5edb423SSean Eric Fagan int l; 1713c5edb423SSean Eric Fagan switch (format[i]) { 1714c5edb423SSean Eric Fagan case '%': /* Format character */ 1715c5edb423SSean Eric Fagan i++; 1716c5edb423SSean Eric Fagan switch (format[i]) { 1717c5edb423SSean Eric Fagan case '%': 1718c5edb423SSean Eric Fagan temp[n++] = '%'; 1719c5edb423SSean Eric Fagan break; 1720c5edb423SSean Eric Fagan case 'N': /* process name */ 1721ce38ca0fSAlfred Perlstein if ((n + namelen) > MAXPATHLEN) { 172287f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1723c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1724c5edb423SSean Eric Fagan free(temp, M_TEMP); 1725c5edb423SSean Eric Fagan return NULL; 1726c5edb423SSean Eric Fagan } 1727ce38ca0fSAlfred Perlstein memcpy(temp+n, name, namelen); 1728ce38ca0fSAlfred Perlstein n += namelen; 1729c5edb423SSean Eric Fagan break; 1730c5edb423SSean Eric Fagan case 'P': /* process id */ 1731ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", pid); 1732c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 173387f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1734c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1735c5edb423SSean Eric Fagan free(temp, M_TEMP); 1736c5edb423SSean Eric Fagan return NULL; 1737c5edb423SSean Eric Fagan } 1738c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1739c5edb423SSean Eric Fagan n += l; 1740c5edb423SSean Eric Fagan break; 1741c5edb423SSean Eric Fagan case 'U': /* user id */ 1742ce38ca0fSAlfred Perlstein l = sprintf(buf, "%u", uid); 1743c5edb423SSean Eric Fagan if ((n + l) > MAXPATHLEN) { 174487f1de5fSBill Fumerola log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1745c5edb423SSean Eric Fagan pid, name, uid, temp, name); 1746c5edb423SSean Eric Fagan free(temp, M_TEMP); 1747c5edb423SSean Eric Fagan return NULL; 1748c5edb423SSean Eric Fagan } 1749c5edb423SSean Eric Fagan memcpy(temp+n, buf, l); 1750c5edb423SSean Eric Fagan n += l; 1751c5edb423SSean Eric Fagan break; 1752c5edb423SSean Eric Fagan default: 1753c5edb423SSean Eric Fagan log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1754c5edb423SSean Eric Fagan } 1755c5edb423SSean Eric Fagan break; 1756c5edb423SSean Eric Fagan default: 1757c5edb423SSean Eric Fagan temp[n++] = format[i]; 1758c5edb423SSean Eric Fagan } 1759c5edb423SSean Eric Fagan } 1760ce38ca0fSAlfred Perlstein temp[n] = '\0'; 1761c5edb423SSean Eric Fagan return temp; 1762c5edb423SSean Eric Fagan } 1763c5edb423SSean Eric Fagan 1764df8bae1dSRodney W. Grimes /* 1765fca666a1SJulian Elischer * Dump a process' core. The main routine does some 1766fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 1767fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 1768fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 1769fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 1770fca666a1SJulian Elischer */ 1771fca666a1SJulian Elischer 1772fca666a1SJulian Elischer static int 1773fca666a1SJulian Elischer coredump(p) 1774fca666a1SJulian Elischer register struct proc *p; 1775fca666a1SJulian Elischer { 1776fca666a1SJulian Elischer register struct vnode *vp; 1777da654d90SPoul-Henning Kamp register struct ucred *cred = p->p_ucred; 1778fca666a1SJulian Elischer struct nameidata nd; 1779fca666a1SJulian Elischer struct vattr vattr; 1780e6796b67SKirk McKusick int error, error1, flags; 1781f2a2857bSKirk McKusick struct mount *mp; 1782fca666a1SJulian Elischer char *name; /* name of corefile */ 1783fca666a1SJulian Elischer off_t limit; 1784fca666a1SJulian Elischer 1785628d2653SJohn Baldwin PROC_LOCK(p); 1786628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 1787fca666a1SJulian Elischer 1788628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1789628d2653SJohn Baldwin PROC_UNLOCK(p); 1790fca666a1SJulian Elischer return (EFAULT); 1791628d2653SJohn Baldwin } 1792fca666a1SJulian Elischer 1793fca666a1SJulian Elischer /* 179435a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 179535a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 179635a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 179735a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 179835a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 179935a2598fSSean Eric Fagan * if it is larger than the limit. 1800fca666a1SJulian Elischer */ 180135a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1802628d2653SJohn Baldwin if (limit == 0) { 1803628d2653SJohn Baldwin PROC_UNLOCK(p); 180435a2598fSSean Eric Fagan return 0; 1805628d2653SJohn Baldwin } 1806628d2653SJohn Baldwin PROC_UNLOCK(p); 180735a2598fSSean Eric Fagan 1808f2a2857bSKirk McKusick restart: 1809fca666a1SJulian Elischer name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1810fca666a1SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1811e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 1812e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 1813fca666a1SJulian Elischer free(name, M_TEMP); 1814fca666a1SJulian Elischer if (error) 1815fca666a1SJulian Elischer return (error); 1816762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 1817fca666a1SJulian Elischer vp = nd.ni_vp; 1818f2a2857bSKirk McKusick if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1819f2a2857bSKirk McKusick VOP_UNLOCK(vp, 0, p); 1820f2a2857bSKirk McKusick if ((error = vn_close(vp, FWRITE, cred, p)) != 0) 1821f2a2857bSKirk McKusick return (error); 1822f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1823f2a2857bSKirk McKusick return (error); 1824f2a2857bSKirk McKusick goto restart; 1825f2a2857bSKirk McKusick } 1826fca666a1SJulian Elischer 1827fca666a1SJulian Elischer /* Don't dump to non-regular files or files with links. */ 1828fca666a1SJulian Elischer if (vp->v_type != VREG || 1829fca666a1SJulian Elischer VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1830fca666a1SJulian Elischer error = EFAULT; 1831fca666a1SJulian Elischer goto out; 1832fca666a1SJulian Elischer } 1833fca666a1SJulian Elischer VATTR_NULL(&vattr); 1834fca666a1SJulian Elischer vattr.va_size = 0; 1835fca666a1SJulian Elischer VOP_LEASE(vp, p, cred, LEASE_WRITE); 1836fca666a1SJulian Elischer VOP_SETATTR(vp, &vattr, cred, p); 1837628d2653SJohn Baldwin PROC_LOCK(p); 1838fca666a1SJulian Elischer p->p_acflag |= ACORE; 1839628d2653SJohn Baldwin PROC_UNLOCK(p); 1840fca666a1SJulian Elischer 1841fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 1842fca666a1SJulian Elischer p->p_sysent->sv_coredump(p, vp, limit) : 1843fca666a1SJulian Elischer ENOSYS; 1844fca666a1SJulian Elischer 1845fca666a1SJulian Elischer out: 1846fca666a1SJulian Elischer VOP_UNLOCK(vp, 0, p); 1847f2a2857bSKirk McKusick vn_finished_write(mp); 1848fca666a1SJulian Elischer error1 = vn_close(vp, FWRITE, cred, p); 1849fca666a1SJulian Elischer if (error == 0) 1850fca666a1SJulian Elischer error = error1; 1851fca666a1SJulian Elischer return (error); 1852fca666a1SJulian Elischer } 1853fca666a1SJulian Elischer 1854fca666a1SJulian Elischer /* 1855df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 1856df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 1857df8bae1dSRodney W. Grimes */ 1858d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1859df8bae1dSRodney W. Grimes struct nosys_args { 1860df8bae1dSRodney W. Grimes int dummy; 1861df8bae1dSRodney W. Grimes }; 1862d2d3e875SBruce Evans #endif 1863df8bae1dSRodney W. Grimes /* ARGSUSED */ 186426f9a767SRodney W. Grimes int 1865cb226aaaSPoul-Henning Kamp nosys(p, args) 1866df8bae1dSRodney W. Grimes struct proc *p; 1867df8bae1dSRodney W. Grimes struct nosys_args *args; 1868df8bae1dSRodney W. Grimes { 1869df8bae1dSRodney W. Grimes 1870628d2653SJohn Baldwin PROC_LOCK(p); 1871df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 1872628d2653SJohn Baldwin PROC_UNLOCK(p); 1873df8bae1dSRodney W. Grimes return (EINVAL); 1874df8bae1dSRodney W. Grimes } 1875831d27a9SDon Lewis 1876831d27a9SDon Lewis /* 1877831d27a9SDon Lewis * Send a signal to a SIGIO or SIGURG to a process or process group using 1878831d27a9SDon Lewis * stored credentials rather than those of the current process. 1879831d27a9SDon Lewis */ 1880831d27a9SDon Lewis void 18812c42a146SMarcel Moolenaar pgsigio(sigio, sig, checkctty) 1882831d27a9SDon Lewis struct sigio *sigio; 18832c42a146SMarcel Moolenaar int sig, checkctty; 1884831d27a9SDon Lewis { 1885831d27a9SDon Lewis if (sigio == NULL) 1886831d27a9SDon Lewis return; 1887831d27a9SDon Lewis 1888831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 1889628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 1890831d27a9SDon Lewis if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1891831d27a9SDon Lewis sigio->sio_proc)) 18922c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 1893628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 1894831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 1895831d27a9SDon Lewis struct proc *p; 1896831d27a9SDon Lewis 1897628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 1898628d2653SJohn Baldwin PROC_LOCK(p); 1899831d27a9SDon Lewis if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1900831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 19012c42a146SMarcel Moolenaar psignal(p, sig); 1902628d2653SJohn Baldwin PROC_UNLOCK(p); 1903628d2653SJohn Baldwin } 1904831d27a9SDon Lewis } 1905831d27a9SDon Lewis } 1906cb679c38SJonathan Lemon 1907cb679c38SJonathan Lemon static int 1908cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 1909cb679c38SJonathan Lemon { 1910cb679c38SJonathan Lemon struct proc *p = curproc; 1911cb679c38SJonathan Lemon 1912cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 1913cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 1914cb679c38SJonathan Lemon 1915628d2653SJohn Baldwin PROC_LOCK(p); 1916cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 1917628d2653SJohn Baldwin PROC_UNLOCK(p); 1918cb679c38SJonathan Lemon 1919cb679c38SJonathan Lemon return (0); 1920cb679c38SJonathan Lemon } 1921cb679c38SJonathan Lemon 1922cb679c38SJonathan Lemon static void 1923cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 1924cb679c38SJonathan Lemon { 1925cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 1926cb679c38SJonathan Lemon 1927628d2653SJohn Baldwin PROC_LOCK(p); 1928e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 1929628d2653SJohn Baldwin PROC_UNLOCK(p); 1930cb679c38SJonathan Lemon } 1931cb679c38SJonathan Lemon 1932cb679c38SJonathan Lemon /* 1933cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 1934cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 1935cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 1936cb679c38SJonathan Lemon * isn't worth the trouble. 1937cb679c38SJonathan Lemon */ 1938cb679c38SJonathan Lemon static int 1939cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 1940cb679c38SJonathan Lemon { 1941cb679c38SJonathan Lemon 1942cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 1943cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 1944cb679c38SJonathan Lemon 1945cb679c38SJonathan Lemon if (kn->kn_id == hint) 1946cb679c38SJonathan Lemon kn->kn_data++; 1947cb679c38SJonathan Lemon } 1948cb679c38SJonathan Lemon return (kn->kn_data != 0); 1949cb679c38SJonathan Lemon } 1950