19454b2d8SWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405591b823SEivind Eklund #include "opt_compat.h" 41db6a20e2SGarrett Wollman #include "opt_ktrace.h" 42db6a20e2SGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 4436240ea5SDoug Rabson #include <sys/systm.h> 45df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 46df8bae1dSRodney W. Grimes #include <sys/vnode.h> 47df8bae1dSRodney W. Grimes #include <sys/acct.h> 48238510fcSJason Evans #include <sys/condvar.h> 49854dc8c2SJohn Baldwin #include <sys/event.h> 50854dc8c2SJohn Baldwin #include <sys/fcntl.h> 51854dc8c2SJohn Baldwin #include <sys/kernel.h> 529dde3bc9SDavid Xu #include <sys/kse.h> 530384fff8SJason Evans #include <sys/ktr.h> 54df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 55854dc8c2SJohn Baldwin #include <sys/lock.h> 56854dc8c2SJohn Baldwin #include <sys/malloc.h> 57854dc8c2SJohn Baldwin #include <sys/mutex.h> 58854dc8c2SJohn Baldwin #include <sys/namei.h> 59854dc8c2SJohn Baldwin #include <sys/proc.h> 60854dc8c2SJohn Baldwin #include <sys/pioctl.h> 61c31146a1SJohn Baldwin #include <sys/resourcevar.h> 62b3a4fb14SDavid Xu #include <sys/sched.h> 6344f3b092SJohn Baldwin #include <sys/sleepqueue.h> 646caa8a15SJohn Baldwin #include <sys/smp.h> 65df8bae1dSRodney W. Grimes #include <sys/stat.h> 661005a129SJohn Baldwin #include <sys/sx.h> 678f19eb88SIan Dowse #include <sys/syscallsubr.h> 68c87e2930SDavid Greenman #include <sys/sysctl.h> 69854dc8c2SJohn Baldwin #include <sys/sysent.h> 70854dc8c2SJohn Baldwin #include <sys/syslog.h> 71854dc8c2SJohn Baldwin #include <sys/sysproto.h> 7206ae1e91SMatthew Dillon #include <sys/unistd.h> 73854dc8c2SJohn Baldwin #include <sys/wait.h> 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes #include <machine/cpu.h> 76df8bae1dSRodney W. Grimes 7723eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43) 7823eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)" 7923eeeff7SPeter Wemm #endif 8023eeeff7SPeter Wemm 816f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 826f841fb7SMarcel Moolenaar 834d77a549SAlfred Perlstein static int coredump(struct thread *); 844d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t); 851a88a252SMaxim Sobolev static int killpg1(struct thread *td, int sig, int pgid, int all); 866711f10fSJohn Baldwin static int issignal(struct thread *p); 874d77a549SAlfred Perlstein static int sigprop(int sig); 884d77a549SAlfred Perlstein static void stop(struct proc *); 894093529dSJeff Roberson static void tdsigwakeup(struct thread *td, int sig, sig_t action); 90cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 91cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 92cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 934093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop); 94a447cd8bSJeff Roberson static int kern_sigtimedwait(struct thread *td, sigset_t set, 95a447cd8bSJeff Roberson siginfo_t *info, struct timespec *timeout); 96c197abc4SMike Makonnen static void do_tdsignal(struct thread *td, int sig, sigtarget_t target); 97cb679c38SJonathan Lemon 98cb679c38SJonathan Lemon struct filterops sig_filtops = 99cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 100cb679c38SJonathan Lemon 10157308494SJoerg Wunsch static int kern_logsigexit = 1; 1023d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1033d177f46SBill Fumerola &kern_logsigexit, 0, 1043d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 10557308494SJoerg Wunsch 1062b87b6d4SRobert Watson /* 1072b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1082b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1092b87b6d4SRobert Watson * in the right situations. 1102b87b6d4SRobert Watson */ 1112b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1122b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1132b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1142b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1152b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1162b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1172b87b6d4SRobert Watson 11822d4b0fbSJohn Polstra int sugid_coredump; 1193d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1203d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 121c87e2930SDavid Greenman 122e5a28db9SPaul Saab static int do_coredump = 1; 123e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 124e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 125e5a28db9SPaul Saab 1266141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 1276141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 1286141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 1296141e04aSJohn-Mark Gurney 1302c42a146SMarcel Moolenaar /* 1312c42a146SMarcel Moolenaar * Signal properties and actions. 1322c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1332c42a146SMarcel Moolenaar * according to the following properties: 1342c42a146SMarcel Moolenaar */ 1352c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1362c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1372c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1382c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1392c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1402c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1412c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 142da33176fSJeff Roberson #define SA_PROC 0x80 /* deliverable to any thread */ 143df8bae1dSRodney W. Grimes 1442c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 145da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGHUP */ 146da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGINT */ 147da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */ 1482c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1492c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1502c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 151da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */ 1522c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 153da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGKILL */ 1542c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1552c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1562c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 157da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPIPE */ 158da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGALRM */ 159da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGTERM */ 160da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGURG */ 161da33176fSJeff Roberson SA_STOP|SA_PROC, /* SIGSTOP */ 162da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */ 163da33176fSJeff Roberson SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */ 164da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGCHLD */ 165da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */ 166da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */ 167da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGIO */ 1682c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1692c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 170da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGVTALRM */ 171da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPROF */ 172da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGWINCH */ 173da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGINFO */ 174da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR1 */ 175da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR2 */ 1762c42a146SMarcel Moolenaar }; 1772c42a146SMarcel Moolenaar 178fbbeeb6cSBruce Evans /* 179fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 180fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 181fbbeeb6cSBruce Evans * action, the process stops in issignal(). 182e602ba25SJulian Elischer * XXXKSE the check for a pending stop is not done under KSE 183fbbeeb6cSBruce Evans * 18433510ef1SBruce Evans * MP SAFE. 185fbbeeb6cSBruce Evans */ 186fbbeeb6cSBruce Evans int 187e602ba25SJulian Elischer cursig(struct thread *td) 188fbbeeb6cSBruce Evans { 189c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 19090af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 191179235b3SBruce Evans mtx_assert(&sched_lock, MA_NOTOWNED); 1924093529dSJeff Roberson return (SIGPENDING(td) ? issignal(td) : 0); 193fbbeeb6cSBruce Evans } 194fbbeeb6cSBruce Evans 19579065dbaSBruce Evans /* 19679065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 1974093529dSJeff Roberson * mode. This must be called whenever a signal is added to td_siglist or 1984093529dSJeff Roberson * unmasked in td_sigmask. 19979065dbaSBruce Evans */ 20079065dbaSBruce Evans void 2014093529dSJeff Roberson signotify(struct thread *td) 20279065dbaSBruce Evans { 2034093529dSJeff Roberson struct proc *p; 2049dde3bc9SDavid Xu sigset_t set, saved; 2054093529dSJeff Roberson 2064093529dSJeff Roberson p = td->td_proc; 20779065dbaSBruce Evans 20879065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 2094093529dSJeff Roberson 2104093529dSJeff Roberson /* 2114093529dSJeff Roberson * If our mask changed we may have to move signal that were 2124093529dSJeff Roberson * previously masked by all threads to our siglist. 2134093529dSJeff Roberson */ 2144093529dSJeff Roberson set = p->p_siglist; 2159dde3bc9SDavid Xu if (p->p_flag & P_SA) 2169dde3bc9SDavid Xu saved = p->p_siglist; 2174093529dSJeff Roberson SIGSETNAND(set, td->td_sigmask); 2184093529dSJeff Roberson SIGSETNAND(p->p_siglist, set); 2194093529dSJeff Roberson SIGSETOR(td->td_siglist, set); 2204093529dSJeff Roberson 2218b94a061SJohn Baldwin if (SIGPENDING(td)) { 22279065dbaSBruce Evans mtx_lock_spin(&sched_lock); 2234093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 22479065dbaSBruce Evans mtx_unlock_spin(&sched_lock); 22579065dbaSBruce Evans } 2269dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 2276dbc0850SJohn Baldwin if (!SIGSETEQ(saved, p->p_siglist)) { 2289dde3bc9SDavid Xu /* pending set changed */ 2299dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 2309dde3bc9SDavid Xu wakeup(&p->p_siglist); 2319dde3bc9SDavid Xu } 2329dde3bc9SDavid Xu } 2338b94a061SJohn Baldwin } 2348b94a061SJohn Baldwin 2358b94a061SJohn Baldwin int 2368b94a061SJohn Baldwin sigonstack(size_t sp) 2378b94a061SJohn Baldwin { 238a30ec4b9SDavid Xu struct thread *td = curthread; 2398b94a061SJohn Baldwin 240a30ec4b9SDavid Xu return ((td->td_pflags & TDP_ALTSTACK) ? 2411930e303SPoul-Henning Kamp #if defined(COMPAT_43) 242a30ec4b9SDavid Xu ((td->td_sigstk.ss_size == 0) ? 243a30ec4b9SDavid Xu (td->td_sigstk.ss_flags & SS_ONSTACK) : 244a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 2458b94a061SJohn Baldwin #else 246a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 2478b94a061SJohn Baldwin #endif 2488b94a061SJohn Baldwin : 0); 2498b94a061SJohn Baldwin } 25079065dbaSBruce Evans 2516f841fb7SMarcel Moolenaar static __inline int 2526f841fb7SMarcel Moolenaar sigprop(int sig) 2532c42a146SMarcel Moolenaar { 2546f841fb7SMarcel Moolenaar 2552c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 2562c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 2572c42a146SMarcel Moolenaar return (0); 258df8bae1dSRodney W. Grimes } 2592c42a146SMarcel Moolenaar 2604093529dSJeff Roberson int 2616f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 2622c42a146SMarcel Moolenaar { 2632c42a146SMarcel Moolenaar int i; 2642c42a146SMarcel Moolenaar 2656f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 2662c42a146SMarcel Moolenaar if (set->__bits[i]) 2672c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 268df8bae1dSRodney W. Grimes return (0); 269df8bae1dSRodney W. Grimes } 270df8bae1dSRodney W. Grimes 2712c42a146SMarcel Moolenaar /* 2728f19eb88SIan Dowse * kern_sigaction 2732c42a146SMarcel Moolenaar * sigaction 27423eeeff7SPeter Wemm * freebsd4_sigaction 2752c42a146SMarcel Moolenaar * osigaction 27625d6dc06SJohn Baldwin * 27725d6dc06SJohn Baldwin * MPSAFE 2782c42a146SMarcel Moolenaar */ 2798f19eb88SIan Dowse int 28023eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags) 2818f19eb88SIan Dowse struct thread *td; 2822c42a146SMarcel Moolenaar register int sig; 2832c42a146SMarcel Moolenaar struct sigaction *act, *oact; 28423eeeff7SPeter Wemm int flags; 285df8bae1dSRodney W. Grimes { 28690af4afaSJohn Baldwin struct sigacts *ps; 2874093529dSJeff Roberson struct thread *td0; 2888f19eb88SIan Dowse struct proc *p = td->td_proc; 289df8bae1dSRodney W. Grimes 2902899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 2912c42a146SMarcel Moolenaar return (EINVAL); 2922c42a146SMarcel Moolenaar 293628d2653SJohn Baldwin PROC_LOCK(p); 294628d2653SJohn Baldwin ps = p->p_sigacts; 29590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2962c42a146SMarcel Moolenaar if (oact) { 2972c42a146SMarcel Moolenaar oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 2982c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2992c42a146SMarcel Moolenaar oact->sa_flags = 0; 3002c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 3012c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 3022c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 3032c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 3042c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 3052c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 3062c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 3072c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 3082c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_siginfo, sig)) 3092c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 31090af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 3112c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 31290af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 3132c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 3142c42a146SMarcel Moolenaar } 3152c42a146SMarcel Moolenaar if (act) { 3162c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 317628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 31890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 319628d2653SJohn Baldwin PROC_UNLOCK(p); 3202c42a146SMarcel Moolenaar return (EINVAL); 321628d2653SJohn Baldwin } 3222c42a146SMarcel Moolenaar 323df8bae1dSRodney W. Grimes /* 324df8bae1dSRodney W. Grimes * Change setting atomically. 325df8bae1dSRodney W. Grimes */ 3262c42a146SMarcel Moolenaar 3272c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 3282c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 3292c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 330aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 331aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 33280f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 33380f42b55SIan Dowse } else { 33480f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 3352c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 3362c42a146SMarcel Moolenaar } 3372c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 3382c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 339df8bae1dSRodney W. Grimes else 3402c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 3412c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 3422c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 343df8bae1dSRodney W. Grimes else 3442c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 3452c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 3462c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 3471e41c1b5SSteven Wallace else 3482c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 3492c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 3502c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 351289ccde0SPeter Wemm else 3522c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 3532c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3542c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 35590af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 3566626c604SJulian Elischer else 35790af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 358ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 359245f17d4SJoerg Wunsch /* 3602c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3612c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3622c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3632c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 364245f17d4SJoerg Wunsch */ 365245f17d4SJoerg Wunsch if (p->p_pid == 1) 36690af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 3676626c604SJulian Elischer else 36890af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 369245f17d4SJoerg Wunsch } else 37090af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 371ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 37290af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 373ba1551caSIan Dowse else 37490af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes /* 37790af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 3782c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 37990af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 3802c42a146SMarcel Moolenaar * have to restart the process. 381df8bae1dSRodney W. Grimes */ 3822c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3832c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3842c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3859dde3bc9SDavid Xu if ((p->p_flag & P_SA) && 3869dde3bc9SDavid Xu SIGISMEMBER(p->p_siglist, sig)) { 3879dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 3889dde3bc9SDavid Xu wakeup(&p->p_siglist); 3899dde3bc9SDavid Xu } 3902c42a146SMarcel Moolenaar /* never to be seen again */ 3911d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 392a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 3934093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 3944093529dSJeff Roberson SIGDELSET(td0->td_siglist, sig); 395a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 3962c42a146SMarcel Moolenaar if (sig != SIGCONT) 3972c42a146SMarcel Moolenaar /* easier in psignal */ 39890af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 39990af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 400645682fdSLuoqi Chen } else { 40190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 4022c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 40390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 4042c42a146SMarcel Moolenaar else 40590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 4062c42a146SMarcel Moolenaar } 40723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 40823eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 40923eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 41023eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 41123eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 41223eeeff7SPeter Wemm else 41323eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 41423eeeff7SPeter Wemm #endif 415e8ebc08fSPeter Wemm #ifdef COMPAT_43 416645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 41723eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 41823eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 419645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 420645682fdSLuoqi Chen else 421645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 422e8ebc08fSPeter Wemm #endif 423df8bae1dSRodney W. Grimes } 42490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 425628d2653SJohn Baldwin PROC_UNLOCK(p); 4262c42a146SMarcel Moolenaar return (0); 4272c42a146SMarcel Moolenaar } 4282c42a146SMarcel Moolenaar 4292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4302c42a146SMarcel Moolenaar struct sigaction_args { 4312c42a146SMarcel Moolenaar int sig; 4322c42a146SMarcel Moolenaar struct sigaction *act; 4332c42a146SMarcel Moolenaar struct sigaction *oact; 4342c42a146SMarcel Moolenaar }; 4352c42a146SMarcel Moolenaar #endif 436fb99ab88SMatthew Dillon /* 437fb99ab88SMatthew Dillon * MPSAFE 438fb99ab88SMatthew Dillon */ 4392c42a146SMarcel Moolenaar int 440b40ce416SJulian Elischer sigaction(td, uap) 441b40ce416SJulian Elischer struct thread *td; 4422c42a146SMarcel Moolenaar register struct sigaction_args *uap; 4432c42a146SMarcel Moolenaar { 4442c42a146SMarcel Moolenaar struct sigaction act, oact; 4452c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 4462c42a146SMarcel Moolenaar int error; 4472c42a146SMarcel Moolenaar 4486f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 4496f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 4502c42a146SMarcel Moolenaar if (actp) { 4516f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 4522c42a146SMarcel Moolenaar if (error) 45344443757STim J. Robbins return (error); 4542c42a146SMarcel Moolenaar } 4558f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 45625d6dc06SJohn Baldwin if (oactp && !error) 4576f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 4582c42a146SMarcel Moolenaar return (error); 4592c42a146SMarcel Moolenaar } 4602c42a146SMarcel Moolenaar 46123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 46223eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 46323eeeff7SPeter Wemm struct freebsd4_sigaction_args { 46423eeeff7SPeter Wemm int sig; 46523eeeff7SPeter Wemm struct sigaction *act; 46623eeeff7SPeter Wemm struct sigaction *oact; 46723eeeff7SPeter Wemm }; 46823eeeff7SPeter Wemm #endif 46923eeeff7SPeter Wemm /* 47023eeeff7SPeter Wemm * MPSAFE 47123eeeff7SPeter Wemm */ 47223eeeff7SPeter Wemm int 47323eeeff7SPeter Wemm freebsd4_sigaction(td, uap) 47423eeeff7SPeter Wemm struct thread *td; 47523eeeff7SPeter Wemm register struct freebsd4_sigaction_args *uap; 47623eeeff7SPeter Wemm { 47723eeeff7SPeter Wemm struct sigaction act, oact; 47823eeeff7SPeter Wemm register struct sigaction *actp, *oactp; 47923eeeff7SPeter Wemm int error; 48023eeeff7SPeter Wemm 48123eeeff7SPeter Wemm 48223eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 48323eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 48423eeeff7SPeter Wemm if (actp) { 48523eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 48623eeeff7SPeter Wemm if (error) 48744443757STim J. Robbins return (error); 48823eeeff7SPeter Wemm } 48923eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 490a14e1189SJohn Baldwin if (oactp && !error) 49123eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 49223eeeff7SPeter Wemm return (error); 49323eeeff7SPeter Wemm } 49423eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 49523eeeff7SPeter Wemm 49631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 4972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4982c42a146SMarcel Moolenaar struct osigaction_args { 4992c42a146SMarcel Moolenaar int signum; 5002c42a146SMarcel Moolenaar struct osigaction *nsa; 5012c42a146SMarcel Moolenaar struct osigaction *osa; 5022c42a146SMarcel Moolenaar }; 5032c42a146SMarcel Moolenaar #endif 504fb99ab88SMatthew Dillon /* 505fb99ab88SMatthew Dillon * MPSAFE 506fb99ab88SMatthew Dillon */ 5072c42a146SMarcel Moolenaar int 508b40ce416SJulian Elischer osigaction(td, uap) 509b40ce416SJulian Elischer struct thread *td; 5102c42a146SMarcel Moolenaar register struct osigaction_args *uap; 5112c42a146SMarcel Moolenaar { 5122c42a146SMarcel Moolenaar struct osigaction sa; 5132c42a146SMarcel Moolenaar struct sigaction nsa, osa; 5142c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 5152c42a146SMarcel Moolenaar int error; 5162c42a146SMarcel Moolenaar 5176f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 5186f841fb7SMarcel Moolenaar return (EINVAL); 519fb99ab88SMatthew Dillon 5206f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 5216f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 522fb99ab88SMatthew Dillon 5232c42a146SMarcel Moolenaar if (nsap) { 5246f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 5252c42a146SMarcel Moolenaar if (error) 52644443757STim J. Robbins return (error); 5272c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 5282c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 5292c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 5302c42a146SMarcel Moolenaar } 53123eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 5322c42a146SMarcel Moolenaar if (osap && !error) { 5332c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 5342c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 5352c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 5366f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 5372c42a146SMarcel Moolenaar } 5382c42a146SMarcel Moolenaar return (error); 5392c42a146SMarcel Moolenaar } 54023eeeff7SPeter Wemm 54123eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__) 54223eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 54323eeeff7SPeter Wemm int 54423eeeff7SPeter Wemm osigreturn(td, uap) 54523eeeff7SPeter Wemm struct thread *td; 54623eeeff7SPeter Wemm struct osigreturn_args *uap; 54723eeeff7SPeter Wemm { 54823eeeff7SPeter Wemm 54923eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 55023eeeff7SPeter Wemm } 55123eeeff7SPeter Wemm #endif 55231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 553df8bae1dSRodney W. Grimes 554df8bae1dSRodney W. Grimes /* 555df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 556df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 557df8bae1dSRodney W. Grimes */ 558df8bae1dSRodney W. Grimes void 559df8bae1dSRodney W. Grimes siginit(p) 560df8bae1dSRodney W. Grimes struct proc *p; 561df8bae1dSRodney W. Grimes { 562df8bae1dSRodney W. Grimes register int i; 56390af4afaSJohn Baldwin struct sigacts *ps; 564df8bae1dSRodney W. Grimes 565628d2653SJohn Baldwin PROC_LOCK(p); 56690af4afaSJohn Baldwin ps = p->p_sigacts; 56790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 5682c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 5692c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 57090af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 57190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 572628d2653SJohn Baldwin PROC_UNLOCK(p); 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes 575df8bae1dSRodney W. Grimes /* 576df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 577df8bae1dSRodney W. Grimes */ 578df8bae1dSRodney W. Grimes void 579a30ec4b9SDavid Xu execsigs(struct proc *p) 580df8bae1dSRodney W. Grimes { 581a30ec4b9SDavid Xu struct sigacts *ps; 582a30ec4b9SDavid Xu int sig; 583a30ec4b9SDavid Xu struct thread *td; 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes /* 586df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 5874093529dSJeff Roberson * through td_sigmask (unless they were caught, 588df8bae1dSRodney W. Grimes * and are now ignored by default). 589df8bae1dSRodney W. Grimes */ 5909b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 591a30ec4b9SDavid Xu td = FIRST_THREAD_IN_PROC(p); 592628d2653SJohn Baldwin ps = p->p_sigacts; 59390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 59490af4afaSJohn Baldwin while (SIGNOTEMPTY(ps->ps_sigcatch)) { 59590af4afaSJohn Baldwin sig = sig_ffs(&ps->ps_sigcatch); 59690af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 5972c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 5982c42a146SMarcel Moolenaar if (sig != SIGCONT) 59990af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 6001d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 6014093529dSJeff Roberson /* 6024093529dSJeff Roberson * There is only one thread at this point. 6034093529dSJeff Roberson */ 604a30ec4b9SDavid Xu SIGDELSET(td->td_siglist, sig); 605df8bae1dSRodney W. Grimes } 6062c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes /* 609df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 610df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 611df8bae1dSRodney W. Grimes */ 612a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 613a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 614a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 615a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 61680e907a1SPeter Wemm /* 61780e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 61880e907a1SPeter Wemm */ 61990af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 620c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 621c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 62290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes /* 626e77daab1SJohn Baldwin * kern_sigprocmask() 6277c8fdcbdSMatthew Dillon * 628628d2653SJohn Baldwin * Manipulate signal mask. 629df8bae1dSRodney W. Grimes */ 630e77daab1SJohn Baldwin int 631e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old) 6324093529dSJeff Roberson struct thread *td; 6332c42a146SMarcel Moolenaar int how; 6342c42a146SMarcel Moolenaar sigset_t *set, *oset; 635645682fdSLuoqi Chen int old; 6362c42a146SMarcel Moolenaar { 6372c42a146SMarcel Moolenaar int error; 6382c42a146SMarcel Moolenaar 6394093529dSJeff Roberson PROC_LOCK(td->td_proc); 6402c42a146SMarcel Moolenaar if (oset != NULL) 6414093529dSJeff Roberson *oset = td->td_sigmask; 6422c42a146SMarcel Moolenaar 6432c42a146SMarcel Moolenaar error = 0; 6442c42a146SMarcel Moolenaar if (set != NULL) { 6452c42a146SMarcel Moolenaar switch (how) { 6462c42a146SMarcel Moolenaar case SIG_BLOCK: 647645682fdSLuoqi Chen SIG_CANTMASK(*set); 6484093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 6492c42a146SMarcel Moolenaar break; 6502c42a146SMarcel Moolenaar case SIG_UNBLOCK: 6514093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 6524093529dSJeff Roberson signotify(td); 6532c42a146SMarcel Moolenaar break; 6542c42a146SMarcel Moolenaar case SIG_SETMASK: 655645682fdSLuoqi Chen SIG_CANTMASK(*set); 656645682fdSLuoqi Chen if (old) 6574093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 658645682fdSLuoqi Chen else 6594093529dSJeff Roberson td->td_sigmask = *set; 6604093529dSJeff Roberson signotify(td); 6612c42a146SMarcel Moolenaar break; 6622c42a146SMarcel Moolenaar default: 6632c42a146SMarcel Moolenaar error = EINVAL; 6642c42a146SMarcel Moolenaar break; 6652c42a146SMarcel Moolenaar } 6662c42a146SMarcel Moolenaar } 6674093529dSJeff Roberson PROC_UNLOCK(td->td_proc); 6682c42a146SMarcel Moolenaar return (error); 6692c42a146SMarcel Moolenaar } 6702c42a146SMarcel Moolenaar 6717c8fdcbdSMatthew Dillon /* 672e77daab1SJohn Baldwin * sigprocmask() - MP SAFE 6737c8fdcbdSMatthew Dillon */ 6747c8fdcbdSMatthew Dillon 675d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 676df8bae1dSRodney W. Grimes struct sigprocmask_args { 677df8bae1dSRodney W. Grimes int how; 6782c42a146SMarcel Moolenaar const sigset_t *set; 6792c42a146SMarcel Moolenaar sigset_t *oset; 680df8bae1dSRodney W. Grimes }; 681d2d3e875SBruce Evans #endif 68226f9a767SRodney W. Grimes int 683b40ce416SJulian Elischer sigprocmask(td, uap) 684b40ce416SJulian Elischer register struct thread *td; 685df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 686df8bae1dSRodney W. Grimes { 6872c42a146SMarcel Moolenaar sigset_t set, oset; 6882c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 6892c42a146SMarcel Moolenaar int error; 690df8bae1dSRodney W. Grimes 6916f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 6926f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 6932c42a146SMarcel Moolenaar if (setp) { 6946f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 6952c42a146SMarcel Moolenaar if (error) 6962c42a146SMarcel Moolenaar return (error); 697df8bae1dSRodney W. Grimes } 698e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 6992c42a146SMarcel Moolenaar if (osetp && !error) { 7006f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 7012c42a146SMarcel Moolenaar } 7022c42a146SMarcel Moolenaar return (error); 7032c42a146SMarcel Moolenaar } 7042c42a146SMarcel Moolenaar 70531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 7067c8fdcbdSMatthew Dillon /* 7077c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 7087c8fdcbdSMatthew Dillon */ 7092c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7102c42a146SMarcel Moolenaar struct osigprocmask_args { 7112c42a146SMarcel Moolenaar int how; 7122c42a146SMarcel Moolenaar osigset_t mask; 7132c42a146SMarcel Moolenaar }; 7142c42a146SMarcel Moolenaar #endif 7152c42a146SMarcel Moolenaar int 716b40ce416SJulian Elischer osigprocmask(td, uap) 717b40ce416SJulian Elischer register struct thread *td; 7182c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 7192c42a146SMarcel Moolenaar { 7202c42a146SMarcel Moolenaar sigset_t set, oset; 7212c42a146SMarcel Moolenaar int error; 7222c42a146SMarcel Moolenaar 7232c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 724e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 725b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 726df8bae1dSRodney W. Grimes return (error); 727df8bae1dSRodney W. Grimes } 72831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 729df8bae1dSRodney W. Grimes 730d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 731df8bae1dSRodney W. Grimes struct sigpending_args { 7322c42a146SMarcel Moolenaar sigset_t *set; 733df8bae1dSRodney W. Grimes }; 734d2d3e875SBruce Evans #endif 735fb99ab88SMatthew Dillon /* 736fb99ab88SMatthew Dillon * MPSAFE 737fb99ab88SMatthew Dillon */ 73826f9a767SRodney W. Grimes int 739a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap) 740a447cd8bSJeff Roberson { 741a447cd8bSJeff Roberson siginfo_t info; 742a447cd8bSJeff Roberson sigset_t set; 743a447cd8bSJeff Roberson int error; 744a447cd8bSJeff Roberson 745a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 74636939a0aSDavid Xu if (error) { 74736939a0aSDavid Xu td->td_retval[0] = error; 74836939a0aSDavid Xu return (0); 74936939a0aSDavid Xu } 750a447cd8bSJeff Roberson 751a447cd8bSJeff Roberson error = kern_sigtimedwait(td, set, &info, NULL); 75236939a0aSDavid Xu if (error) { 75336939a0aSDavid Xu if (error == ERESTART) 754a447cd8bSJeff Roberson return (error); 75536939a0aSDavid Xu td->td_retval[0] = error; 75636939a0aSDavid Xu return (0); 75736939a0aSDavid Xu } 758a447cd8bSJeff Roberson 759a447cd8bSJeff Roberson error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo)); 760a447cd8bSJeff Roberson /* Repost if we got an error. */ 76118440c7fSJohn Baldwin if (error && info.si_signo) { 76218440c7fSJohn Baldwin PROC_LOCK(td->td_proc); 763c197abc4SMike Makonnen tdsignal(td, info.si_signo, SIGTARGET_TD); 76418440c7fSJohn Baldwin PROC_UNLOCK(td->td_proc); 76518440c7fSJohn Baldwin } 76636939a0aSDavid Xu td->td_retval[0] = error; 76736939a0aSDavid Xu return (0); 768a447cd8bSJeff Roberson } 769a447cd8bSJeff Roberson /* 770a447cd8bSJeff Roberson * MPSAFE 771a447cd8bSJeff Roberson */ 772a447cd8bSJeff Roberson int 773a447cd8bSJeff Roberson sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 774a447cd8bSJeff Roberson { 775a447cd8bSJeff Roberson struct timespec ts; 776a447cd8bSJeff Roberson struct timespec *timeout; 777a447cd8bSJeff Roberson sigset_t set; 778a447cd8bSJeff Roberson siginfo_t info; 779a447cd8bSJeff Roberson int error; 780a447cd8bSJeff Roberson 781a447cd8bSJeff Roberson if (uap->timeout) { 782a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 783a447cd8bSJeff Roberson if (error) 784a447cd8bSJeff Roberson return (error); 785a447cd8bSJeff Roberson 786a447cd8bSJeff Roberson timeout = &ts; 787a447cd8bSJeff Roberson } else 788a447cd8bSJeff Roberson timeout = NULL; 789a447cd8bSJeff Roberson 790a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 791a447cd8bSJeff Roberson if (error) 792a447cd8bSJeff Roberson return (error); 793a447cd8bSJeff Roberson 794a447cd8bSJeff Roberson error = kern_sigtimedwait(td, set, &info, timeout); 795a447cd8bSJeff Roberson if (error) 796a447cd8bSJeff Roberson return (error); 7979dde3bc9SDavid Xu 798418228dfSDavid Xu if (uap->info) 799a447cd8bSJeff Roberson error = copyout(&info, uap->info, sizeof(info)); 800a447cd8bSJeff Roberson /* Repost if we got an error. */ 80118440c7fSJohn Baldwin if (error && info.si_signo) { 80218440c7fSJohn Baldwin PROC_LOCK(td->td_proc); 803c197abc4SMike Makonnen tdsignal(td, info.si_signo, SIGTARGET_TD); 80418440c7fSJohn Baldwin PROC_UNLOCK(td->td_proc); 805418228dfSDavid Xu } else { 806418228dfSDavid Xu td->td_retval[0] = info.si_signo; 80718440c7fSJohn Baldwin } 808a447cd8bSJeff Roberson return (error); 809a447cd8bSJeff Roberson } 810a447cd8bSJeff Roberson 811a447cd8bSJeff Roberson /* 812a447cd8bSJeff Roberson * MPSAFE 813a447cd8bSJeff Roberson */ 814a447cd8bSJeff Roberson int 815a447cd8bSJeff Roberson sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 816a447cd8bSJeff Roberson { 817a447cd8bSJeff Roberson siginfo_t info; 818a447cd8bSJeff Roberson sigset_t set; 819a447cd8bSJeff Roberson int error; 820a447cd8bSJeff Roberson 821a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 822a447cd8bSJeff Roberson if (error) 823a447cd8bSJeff Roberson return (error); 824a447cd8bSJeff Roberson 825a447cd8bSJeff Roberson error = kern_sigtimedwait(td, set, &info, NULL); 826a447cd8bSJeff Roberson if (error) 827a447cd8bSJeff Roberson return (error); 828a447cd8bSJeff Roberson 829418228dfSDavid Xu if (uap->info) 830a447cd8bSJeff Roberson error = copyout(&info, uap->info, sizeof(info)); 831a447cd8bSJeff Roberson /* Repost if we got an error. */ 83218440c7fSJohn Baldwin if (error && info.si_signo) { 83318440c7fSJohn Baldwin PROC_LOCK(td->td_proc); 834c197abc4SMike Makonnen tdsignal(td, info.si_signo, SIGTARGET_TD); 83518440c7fSJohn Baldwin PROC_UNLOCK(td->td_proc); 836418228dfSDavid Xu } else { 837418228dfSDavid Xu td->td_retval[0] = info.si_signo; 83818440c7fSJohn Baldwin } 839a447cd8bSJeff Roberson return (error); 840a447cd8bSJeff Roberson } 841a447cd8bSJeff Roberson 842a447cd8bSJeff Roberson static int 8433074d1b4SDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info, 844a447cd8bSJeff Roberson struct timespec *timeout) 845a447cd8bSJeff Roberson { 8463074d1b4SDavid Xu struct sigacts *ps; 8476675b36eSDavid Xu sigset_t savedmask; 848a447cd8bSJeff Roberson struct proc *p; 8491089f031SDavid Xu int error, sig, hz, i, timevalid = 0; 8501089f031SDavid Xu struct timespec rts, ets, ts; 8511089f031SDavid Xu struct timeval tv; 852a447cd8bSJeff Roberson 853a447cd8bSJeff Roberson p = td->td_proc; 854a447cd8bSJeff Roberson error = 0; 855a447cd8bSJeff Roberson sig = 0; 8563074d1b4SDavid Xu SIG_CANTMASK(waitset); 857a447cd8bSJeff Roberson 858a447cd8bSJeff Roberson PROC_LOCK(p); 859a447cd8bSJeff Roberson ps = p->p_sigacts; 8603074d1b4SDavid Xu savedmask = td->td_sigmask; 8611089f031SDavid Xu if (timeout) { 8621089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 8631089f031SDavid Xu timevalid = 1; 8641089f031SDavid Xu getnanouptime(&rts); 8651089f031SDavid Xu ets = rts; 8661089f031SDavid Xu timespecadd(&ets, timeout); 8671089f031SDavid Xu } 8681089f031SDavid Xu } 8693074d1b4SDavid Xu 8703074d1b4SDavid Xu again: 8713074d1b4SDavid Xu for (i = 1; i <= _SIG_MAXSIG; ++i) { 8723074d1b4SDavid Xu if (!SIGISMEMBER(waitset, i)) 8733074d1b4SDavid Xu continue; 8743074d1b4SDavid Xu if (SIGISMEMBER(td->td_siglist, i)) { 875418228dfSDavid Xu SIGFILLSET(td->td_sigmask); 876418228dfSDavid Xu SIG_CANTMASK(td->td_sigmask); 8773074d1b4SDavid Xu SIGDELSET(td->td_sigmask, i); 87890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 879a447cd8bSJeff Roberson sig = cursig(td); 8803074d1b4SDavid Xu i = 0; 8813074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 8823074d1b4SDavid Xu } else if (SIGISMEMBER(p->p_siglist, i)) { 8833074d1b4SDavid Xu if (p->p_flag & P_SA) { 8843074d1b4SDavid Xu p->p_flag |= P_SIGEVENT; 8853074d1b4SDavid Xu wakeup(&p->p_siglist); 8863074d1b4SDavid Xu } 8873074d1b4SDavid Xu SIGDELSET(p->p_siglist, i); 8883074d1b4SDavid Xu SIGADDSET(td->td_siglist, i); 8893074d1b4SDavid Xu SIGFILLSET(td->td_sigmask); 8903074d1b4SDavid Xu SIG_CANTMASK(td->td_sigmask); 8913074d1b4SDavid Xu SIGDELSET(td->td_sigmask, i); 8923074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 8933074d1b4SDavid Xu sig = cursig(td); 8943074d1b4SDavid Xu i = 0; 8953074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 8963074d1b4SDavid Xu } 8976675b36eSDavid Xu if (sig) 898a447cd8bSJeff Roberson goto out; 8993074d1b4SDavid Xu } 9003074d1b4SDavid Xu if (error) 9013074d1b4SDavid Xu goto out; 9023074d1b4SDavid Xu 903a447cd8bSJeff Roberson /* 904a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 905a447cd8bSJeff Roberson * signals. 906a447cd8bSJeff Roberson */ 907a447cd8bSJeff Roberson if (timeout) { 9081089f031SDavid Xu if (!timevalid) { 909a447cd8bSJeff Roberson error = EINVAL; 910a447cd8bSJeff Roberson goto out; 911a447cd8bSJeff Roberson } 9121089f031SDavid Xu getnanouptime(&rts); 9131089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 9143074d1b4SDavid Xu error = EAGAIN; 9153074d1b4SDavid Xu goto out; 9163074d1b4SDavid Xu } 9171089f031SDavid Xu ts = ets; 9181089f031SDavid Xu timespecsub(&ts, &rts); 9191089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 920a447cd8bSJeff Roberson hz = tvtohz(&tv); 921a447cd8bSJeff Roberson } else 922a447cd8bSJeff Roberson hz = 0; 923a447cd8bSJeff Roberson 9246675b36eSDavid Xu td->td_sigmask = savedmask; 9256675b36eSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 9266675b36eSDavid Xu signotify(td); 92786b5e563SDag-Erling Smørgrav error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 9281089f031SDavid Xu if (timeout) { 9291089f031SDavid Xu if (error == ERESTART) { 9301089f031SDavid Xu /* timeout can not be restarted. */ 9313074d1b4SDavid Xu error = EINTR; 9321089f031SDavid Xu } else if (error == EAGAIN) { 9331089f031SDavid Xu /* will calculate timeout by ourself. */ 9341089f031SDavid Xu error = 0; 9351089f031SDavid Xu } 9361089f031SDavid Xu } 9373074d1b4SDavid Xu goto again; 9389dde3bc9SDavid Xu 939a447cd8bSJeff Roberson out: 9406675b36eSDavid Xu td->td_sigmask = savedmask; 9416675b36eSDavid Xu signotify(td); 942a447cd8bSJeff Roberson if (sig) { 943a447cd8bSJeff Roberson sig_t action; 944a447cd8bSJeff Roberson 9453074d1b4SDavid Xu error = 0; 9463074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 947a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 94890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 949a447cd8bSJeff Roberson #ifdef KTRACE 950a447cd8bSJeff Roberson if (KTRPOINT(td, KTR_PSIG)) 9513074d1b4SDavid Xu ktrpsig(sig, action, &td->td_sigmask, 0); 952a447cd8bSJeff Roberson #endif 953a447cd8bSJeff Roberson _STOPEVENT(p, S_SIG, sig); 954a447cd8bSJeff Roberson 955a447cd8bSJeff Roberson SIGDELSET(td->td_siglist, sig); 9561089f031SDavid Xu bzero(info, sizeof(*info)); 957a447cd8bSJeff Roberson info->si_signo = sig; 958a447cd8bSJeff Roberson info->si_code = 0; 9593074d1b4SDavid Xu } 960a447cd8bSJeff Roberson PROC_UNLOCK(p); 961a447cd8bSJeff Roberson return (error); 962a447cd8bSJeff Roberson } 963a447cd8bSJeff Roberson 964a447cd8bSJeff Roberson /* 965a447cd8bSJeff Roberson * MPSAFE 966a447cd8bSJeff Roberson */ 967a447cd8bSJeff Roberson int 968b40ce416SJulian Elischer sigpending(td, uap) 969b40ce416SJulian Elischer struct thread *td; 970df8bae1dSRodney W. Grimes struct sigpending_args *uap; 971df8bae1dSRodney W. Grimes { 972b40ce416SJulian Elischer struct proc *p = td->td_proc; 973628d2653SJohn Baldwin sigset_t siglist; 974df8bae1dSRodney W. Grimes 975628d2653SJohn Baldwin PROC_LOCK(p); 9761d9c5696SJuli Mallett siglist = p->p_siglist; 9774093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 978628d2653SJohn Baldwin PROC_UNLOCK(p); 97948e8f774STim J. Robbins return (copyout(&siglist, uap->set, sizeof(sigset_t))); 9802c42a146SMarcel Moolenaar } 9812c42a146SMarcel Moolenaar 98231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9842c42a146SMarcel Moolenaar struct osigpending_args { 9852c42a146SMarcel Moolenaar int dummy; 9862c42a146SMarcel Moolenaar }; 9872c42a146SMarcel Moolenaar #endif 988fb99ab88SMatthew Dillon /* 989fb99ab88SMatthew Dillon * MPSAFE 990fb99ab88SMatthew Dillon */ 9912c42a146SMarcel Moolenaar int 992b40ce416SJulian Elischer osigpending(td, uap) 993b40ce416SJulian Elischer struct thread *td; 9942c42a146SMarcel Moolenaar struct osigpending_args *uap; 9952c42a146SMarcel Moolenaar { 996b40ce416SJulian Elischer struct proc *p = td->td_proc; 9974093529dSJeff Roberson sigset_t siglist; 998b40ce416SJulian Elischer 999628d2653SJohn Baldwin PROC_LOCK(p); 10004093529dSJeff Roberson siglist = p->p_siglist; 10014093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 1002628d2653SJohn Baldwin PROC_UNLOCK(p); 10039d8643ecSJohn Baldwin SIG2OSIG(siglist, td->td_retval[0]); 1004df8bae1dSRodney W. Grimes return (0); 1005df8bae1dSRodney W. Grimes } 100631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1007df8bae1dSRodney W. Grimes 10081930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1009df8bae1dSRodney W. Grimes /* 1010df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1011df8bae1dSRodney W. Grimes */ 1012d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1013df8bae1dSRodney W. Grimes struct osigvec_args { 1014df8bae1dSRodney W. Grimes int signum; 1015df8bae1dSRodney W. Grimes struct sigvec *nsv; 1016df8bae1dSRodney W. Grimes struct sigvec *osv; 1017df8bae1dSRodney W. Grimes }; 1018d2d3e875SBruce Evans #endif 1019fb99ab88SMatthew Dillon /* 1020fb99ab88SMatthew Dillon * MPSAFE 1021fb99ab88SMatthew Dillon */ 1022df8bae1dSRodney W. Grimes /* ARGSUSED */ 102326f9a767SRodney W. Grimes int 1024b40ce416SJulian Elischer osigvec(td, uap) 1025b40ce416SJulian Elischer struct thread *td; 1026df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 1027df8bae1dSRodney W. Grimes { 1028df8bae1dSRodney W. Grimes struct sigvec vec; 10292c42a146SMarcel Moolenaar struct sigaction nsa, osa; 10302c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 10312c42a146SMarcel Moolenaar int error; 1032df8bae1dSRodney W. Grimes 10336f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 10346f841fb7SMarcel Moolenaar return (EINVAL); 10356f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 10366f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 10372c42a146SMarcel Moolenaar if (nsap) { 10386f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 10392c42a146SMarcel Moolenaar if (error) 1040df8bae1dSRodney W. Grimes return (error); 10412c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 10422c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 10432c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 10442c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1045df8bae1dSRodney W. Grimes } 10465edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 10472c42a146SMarcel Moolenaar if (osap && !error) { 10482c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 10492c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 10502c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 10512c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 10522c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 10536f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 10542c42a146SMarcel Moolenaar } 10552c42a146SMarcel Moolenaar return (error); 1056df8bae1dSRodney W. Grimes } 1057df8bae1dSRodney W. Grimes 1058d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1059df8bae1dSRodney W. Grimes struct osigblock_args { 1060df8bae1dSRodney W. Grimes int mask; 1061df8bae1dSRodney W. Grimes }; 1062d2d3e875SBruce Evans #endif 1063fb99ab88SMatthew Dillon /* 1064fb99ab88SMatthew Dillon * MPSAFE 1065fb99ab88SMatthew Dillon */ 106626f9a767SRodney W. Grimes int 1067b40ce416SJulian Elischer osigblock(td, uap) 1068b40ce416SJulian Elischer register struct thread *td; 1069df8bae1dSRodney W. Grimes struct osigblock_args *uap; 1070df8bae1dSRodney W. Grimes { 1071b40ce416SJulian Elischer struct proc *p = td->td_proc; 10722c42a146SMarcel Moolenaar sigset_t set; 1073df8bae1dSRodney W. Grimes 10742c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10752c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1076628d2653SJohn Baldwin PROC_LOCK(p); 10774093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 10784093529dSJeff Roberson SIGSETOR(td->td_sigmask, set); 1079628d2653SJohn Baldwin PROC_UNLOCK(p); 1080df8bae1dSRodney W. Grimes return (0); 1081df8bae1dSRodney W. Grimes } 1082df8bae1dSRodney W. Grimes 1083d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1084df8bae1dSRodney W. Grimes struct osigsetmask_args { 1085df8bae1dSRodney W. Grimes int mask; 1086df8bae1dSRodney W. Grimes }; 1087d2d3e875SBruce Evans #endif 1088fb99ab88SMatthew Dillon /* 1089fb99ab88SMatthew Dillon * MPSAFE 1090fb99ab88SMatthew Dillon */ 109126f9a767SRodney W. Grimes int 1092b40ce416SJulian Elischer osigsetmask(td, uap) 1093b40ce416SJulian Elischer struct thread *td; 1094df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 1095df8bae1dSRodney W. Grimes { 1096b40ce416SJulian Elischer struct proc *p = td->td_proc; 10972c42a146SMarcel Moolenaar sigset_t set; 1098df8bae1dSRodney W. Grimes 10992c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 11002c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1101628d2653SJohn Baldwin PROC_LOCK(p); 11024093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 11034093529dSJeff Roberson SIGSETLO(td->td_sigmask, set); 11044093529dSJeff Roberson signotify(td); 1105628d2653SJohn Baldwin PROC_UNLOCK(p); 1106df8bae1dSRodney W. Grimes return (0); 1107df8bae1dSRodney W. Grimes } 11081930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1109df8bae1dSRodney W. Grimes 1110df8bae1dSRodney W. Grimes /* 1111df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 111297563428SAlexander Kabaev * in the meantime. 1113b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 1114b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 1115b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 1116df8bae1dSRodney W. Grimes */ 1117d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1118df8bae1dSRodney W. Grimes struct sigsuspend_args { 11192c42a146SMarcel Moolenaar const sigset_t *sigmask; 1120df8bae1dSRodney W. Grimes }; 1121d2d3e875SBruce Evans #endif 1122fb99ab88SMatthew Dillon /* 1123fb99ab88SMatthew Dillon * MPSAFE 1124fb99ab88SMatthew Dillon */ 1125df8bae1dSRodney W. Grimes /* ARGSUSED */ 112626f9a767SRodney W. Grimes int 1127b40ce416SJulian Elischer sigsuspend(td, uap) 1128b40ce416SJulian Elischer struct thread *td; 1129df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 1130df8bae1dSRodney W. Grimes { 11312c42a146SMarcel Moolenaar sigset_t mask; 11322c42a146SMarcel Moolenaar int error; 11332c42a146SMarcel Moolenaar 11346f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 11352c42a146SMarcel Moolenaar if (error) 11362c42a146SMarcel Moolenaar return (error); 11378f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 11388f19eb88SIan Dowse } 11398f19eb88SIan Dowse 11408f19eb88SIan Dowse int 11418f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 11428f19eb88SIan Dowse { 11438f19eb88SIan Dowse struct proc *p = td->td_proc; 1144df8bae1dSRodney W. Grimes 1145df8bae1dSRodney W. Grimes /* 1146645682fdSLuoqi Chen * When returning from sigsuspend, we want 1147df8bae1dSRodney W. Grimes * the old mask to be restored after the 1148df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1149df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1150df8bae1dSRodney W. Grimes * to indicate this. 1151df8bae1dSRodney W. Grimes */ 1152628d2653SJohn Baldwin PROC_LOCK(p); 11534093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11545e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1155645682fdSLuoqi Chen SIG_CANTMASK(mask); 11564093529dSJeff Roberson td->td_sigmask = mask; 11574093529dSJeff Roberson signotify(td); 115886b5e563SDag-Erling Smørgrav while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 11592c42a146SMarcel Moolenaar /* void */; 1160628d2653SJohn Baldwin PROC_UNLOCK(p); 11612c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 11622c42a146SMarcel Moolenaar return (EINTR); 11632c42a146SMarcel Moolenaar } 11642c42a146SMarcel Moolenaar 116531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 116697563428SAlexander Kabaev /* 116797563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 116897563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 116997563428SAlexander Kabaev */ 11702c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11712c42a146SMarcel Moolenaar struct osigsuspend_args { 11722c42a146SMarcel Moolenaar osigset_t mask; 11732c42a146SMarcel Moolenaar }; 11742c42a146SMarcel Moolenaar #endif 1175fb99ab88SMatthew Dillon /* 1176fb99ab88SMatthew Dillon * MPSAFE 1177fb99ab88SMatthew Dillon */ 11782c42a146SMarcel Moolenaar /* ARGSUSED */ 11792c42a146SMarcel Moolenaar int 1180b40ce416SJulian Elischer osigsuspend(td, uap) 1181b40ce416SJulian Elischer struct thread *td; 11822c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 11832c42a146SMarcel Moolenaar { 1184b40ce416SJulian Elischer struct proc *p = td->td_proc; 1185645682fdSLuoqi Chen sigset_t mask; 11862c42a146SMarcel Moolenaar 1187628d2653SJohn Baldwin PROC_LOCK(p); 11884093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11895e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1190645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 1191645682fdSLuoqi Chen SIG_CANTMASK(mask); 11924093529dSJeff Roberson SIGSETLO(td->td_sigmask, mask); 11934093529dSJeff Roberson signotify(td); 119486b5e563SDag-Erling Smørgrav while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1195df8bae1dSRodney W. Grimes /* void */; 1196628d2653SJohn Baldwin PROC_UNLOCK(p); 1197df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 1198df8bae1dSRodney W. Grimes return (EINTR); 1199df8bae1dSRodney W. Grimes } 120031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1201df8bae1dSRodney W. Grimes 12021930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1203d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1204df8bae1dSRodney W. Grimes struct osigstack_args { 1205df8bae1dSRodney W. Grimes struct sigstack *nss; 1206df8bae1dSRodney W. Grimes struct sigstack *oss; 1207df8bae1dSRodney W. Grimes }; 1208d2d3e875SBruce Evans #endif 1209fb99ab88SMatthew Dillon /* 1210fb99ab88SMatthew Dillon * MPSAFE 1211fb99ab88SMatthew Dillon */ 1212df8bae1dSRodney W. Grimes /* ARGSUSED */ 121326f9a767SRodney W. Grimes int 1214b40ce416SJulian Elischer osigstack(td, uap) 1215b40ce416SJulian Elischer struct thread *td; 1216df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 1217df8bae1dSRodney W. Grimes { 12185afe0c99SJohn Baldwin struct sigstack nss, oss; 1219fb99ab88SMatthew Dillon int error = 0; 1220fb99ab88SMatthew Dillon 1221d034d459SMarcel Moolenaar if (uap->nss != NULL) { 12225afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 12235afe0c99SJohn Baldwin if (error) 12245afe0c99SJohn Baldwin return (error); 1225df8bae1dSRodney W. Grimes } 1226a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 12275afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 12285afe0c99SJohn Baldwin if (uap->nss != NULL) { 1229a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1230a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1231a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1232a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 12335afe0c99SJohn Baldwin } 12345afe0c99SJohn Baldwin if (uap->oss != NULL) 12355afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 12365afe0c99SJohn Baldwin 1237fb99ab88SMatthew Dillon return (error); 1238df8bae1dSRodney W. Grimes } 12391930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1240df8bae1dSRodney W. Grimes 1241d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1242df8bae1dSRodney W. Grimes struct sigaltstack_args { 12432c42a146SMarcel Moolenaar stack_t *ss; 12442c42a146SMarcel Moolenaar stack_t *oss; 1245df8bae1dSRodney W. Grimes }; 1246d2d3e875SBruce Evans #endif 1247fb99ab88SMatthew Dillon /* 1248fb99ab88SMatthew Dillon * MPSAFE 1249fb99ab88SMatthew Dillon */ 1250df8bae1dSRodney W. Grimes /* ARGSUSED */ 125126f9a767SRodney W. Grimes int 1252b40ce416SJulian Elischer sigaltstack(td, uap) 1253b40ce416SJulian Elischer struct thread *td; 1254df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1255df8bae1dSRodney W. Grimes { 12568f19eb88SIan Dowse stack_t ss, oss; 12578f19eb88SIan Dowse int error; 12588f19eb88SIan Dowse 12598f19eb88SIan Dowse if (uap->ss != NULL) { 12608f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 12618f19eb88SIan Dowse if (error) 12628f19eb88SIan Dowse return (error); 12638f19eb88SIan Dowse } 12648f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 12658f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 12668f19eb88SIan Dowse if (error) 12678f19eb88SIan Dowse return (error); 12688f19eb88SIan Dowse if (uap->oss != NULL) 12698f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 12708f19eb88SIan Dowse return (error); 12718f19eb88SIan Dowse } 12728f19eb88SIan Dowse 12738f19eb88SIan Dowse int 12748f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 12758f19eb88SIan Dowse { 1276b40ce416SJulian Elischer struct proc *p = td->td_proc; 1277fb99ab88SMatthew Dillon int oonstack; 1278fb99ab88SMatthew Dillon 1279b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1280d034d459SMarcel Moolenaar 12818f19eb88SIan Dowse if (oss != NULL) { 1282a30ec4b9SDavid Xu *oss = td->td_sigstk; 1283a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1284d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1285df8bae1dSRodney W. Grimes } 1286d034d459SMarcel Moolenaar 12878f19eb88SIan Dowse if (ss != NULL) { 1288a30ec4b9SDavid Xu if (oonstack) 1289cf60731bSJohn Baldwin return (EPERM); 1290a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1291cf60731bSJohn Baldwin return (EINVAL); 12928f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 12938f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1294cf60731bSJohn Baldwin return (ENOMEM); 1295fb99ab88SMatthew Dillon } 1296a30ec4b9SDavid Xu td->td_sigstk = *ss; 1297a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1298628d2653SJohn Baldwin } else { 1299a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1300628d2653SJohn Baldwin } 1301d034d459SMarcel Moolenaar } 1302cf60731bSJohn Baldwin return (0); 1303df8bae1dSRodney W. Grimes } 1304df8bae1dSRodney W. Grimes 1305d93f860cSPoul-Henning Kamp /* 1306d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1307d93f860cSPoul-Henning Kamp * cp is calling process. 1308d93f860cSPoul-Henning Kamp */ 130937c84183SPoul-Henning Kamp static int 13101a88a252SMaxim Sobolev killpg1(td, sig, pgid, all) 13119c1ab3e0SJohn Baldwin register struct thread *td; 13121a88a252SMaxim Sobolev int sig, pgid, all; 1313d93f860cSPoul-Henning Kamp { 1314d93f860cSPoul-Henning Kamp register struct proc *p; 1315d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1316d93f860cSPoul-Henning Kamp int nfound = 0; 1317d93f860cSPoul-Henning Kamp 1318553629ebSJake Burkholder if (all) { 1319d93f860cSPoul-Henning Kamp /* 1320d93f860cSPoul-Henning Kamp * broadcast 1321d93f860cSPoul-Henning Kamp */ 13221005a129SJohn Baldwin sx_slock(&allproc_lock); 13232e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1324628d2653SJohn Baldwin PROC_LOCK(p); 13259c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 13269c1ab3e0SJohn Baldwin p == td->td_proc) { 1327628d2653SJohn Baldwin PROC_UNLOCK(p); 1328628d2653SJohn Baldwin continue; 1329628d2653SJohn Baldwin } 13301a88a252SMaxim Sobolev if (p_cansignal(td, p, sig) == 0) { 1331d93f860cSPoul-Henning Kamp nfound++; 133233a9ed9dSJohn Baldwin if (sig) 13332c42a146SMarcel Moolenaar psignal(p, sig); 1334628d2653SJohn Baldwin } 133533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1336d93f860cSPoul-Henning Kamp } 13371005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1338553629ebSJake Burkholder } else { 1339ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1340f591779bSSeigo Tanimura if (pgid == 0) { 1341d93f860cSPoul-Henning Kamp /* 1342d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1343d93f860cSPoul-Henning Kamp */ 13449c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1345f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1346f591779bSSeigo Tanimura } else { 1347d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1348f591779bSSeigo Tanimura if (pgrp == NULL) { 1349ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1350d93f860cSPoul-Henning Kamp return (ESRCH); 1351d93f860cSPoul-Henning Kamp } 1352f591779bSSeigo Tanimura } 1353ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 13542e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1355628d2653SJohn Baldwin PROC_LOCK(p); 1356628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1357628d2653SJohn Baldwin PROC_UNLOCK(p); 1358628d2653SJohn Baldwin continue; 1359628d2653SJohn Baldwin } 13601a88a252SMaxim Sobolev if (p_cansignal(td, p, sig) == 0) { 1361d93f860cSPoul-Henning Kamp nfound++; 136233a9ed9dSJohn Baldwin if (sig) 13632c42a146SMarcel Moolenaar psignal(p, sig); 1364628d2653SJohn Baldwin } 136533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1366d93f860cSPoul-Henning Kamp } 1367f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1368d93f860cSPoul-Henning Kamp } 1369d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1370d93f860cSPoul-Henning Kamp } 1371d93f860cSPoul-Henning Kamp 1372d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1373df8bae1dSRodney W. Grimes struct kill_args { 1374df8bae1dSRodney W. Grimes int pid; 1375df8bae1dSRodney W. Grimes int signum; 1376df8bae1dSRodney W. Grimes }; 1377d2d3e875SBruce Evans #endif 1378fb99ab88SMatthew Dillon /* 1379fb99ab88SMatthew Dillon * MPSAFE 1380fb99ab88SMatthew Dillon */ 1381df8bae1dSRodney W. Grimes /* ARGSUSED */ 138226f9a767SRodney W. Grimes int 1383b40ce416SJulian Elischer kill(td, uap) 1384b40ce416SJulian Elischer register struct thread *td; 1385df8bae1dSRodney W. Grimes register struct kill_args *uap; 1386df8bae1dSRodney W. Grimes { 1387df8bae1dSRodney W. Grimes register struct proc *p; 138890af4afaSJohn Baldwin int error; 1389df8bae1dSRodney W. Grimes 13906c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1391df8bae1dSRodney W. Grimes return (EINVAL); 1392fb99ab88SMatthew Dillon 1393df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1394df8bae1dSRodney W. Grimes /* kill single process */ 13950b011ea3SPawel Jakub Dawidek if ((p = pfind(uap->pid)) == NULL) { 139624b2151fSPawel Jakub Dawidek if ((p = zpfind(uap->pid)) == NULL) 139790af4afaSJohn Baldwin return (ESRCH); 13980b011ea3SPawel Jakub Dawidek } 13991a88a252SMaxim Sobolev error = p_cansignal(td, p, uap->signum); 140090af4afaSJohn Baldwin if (error == 0 && uap->signum) 1401df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1402628d2653SJohn Baldwin PROC_UNLOCK(p); 140390af4afaSJohn Baldwin return (error); 1404df8bae1dSRodney W. Grimes } 1405df8bae1dSRodney W. Grimes switch (uap->pid) { 1406df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 14071a88a252SMaxim Sobolev return (killpg1(td, uap->signum, 0, 1)); 1408df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 14091a88a252SMaxim Sobolev return (killpg1(td, uap->signum, 0, 0)); 1410df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 14111a88a252SMaxim Sobolev return (killpg1(td, uap->signum, -uap->pid, 0)); 1412df8bae1dSRodney W. Grimes } 141390af4afaSJohn Baldwin /* NOTREACHED */ 1414df8bae1dSRodney W. Grimes } 1415df8bae1dSRodney W. Grimes 14161930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1417d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1418df8bae1dSRodney W. Grimes struct okillpg_args { 1419df8bae1dSRodney W. Grimes int pgid; 1420df8bae1dSRodney W. Grimes int signum; 1421df8bae1dSRodney W. Grimes }; 1422d2d3e875SBruce Evans #endif 1423fb99ab88SMatthew Dillon /* 1424fb99ab88SMatthew Dillon * MPSAFE 1425fb99ab88SMatthew Dillon */ 1426df8bae1dSRodney W. Grimes /* ARGSUSED */ 142726f9a767SRodney W. Grimes int 1428b40ce416SJulian Elischer okillpg(td, uap) 1429b40ce416SJulian Elischer struct thread *td; 1430df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1431df8bae1dSRodney W. Grimes { 1432df8bae1dSRodney W. Grimes 14336c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1434df8bae1dSRodney W. Grimes return (EINVAL); 14351a88a252SMaxim Sobolev return (killpg1(td, uap->signum, uap->pgid, 0)); 1436df8bae1dSRodney W. Grimes } 14371930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1438df8bae1dSRodney W. Grimes 1439df8bae1dSRodney W. Grimes /* 1440df8bae1dSRodney W. Grimes * Send a signal to a process group. 1441df8bae1dSRodney W. Grimes */ 1442df8bae1dSRodney W. Grimes void 14432c42a146SMarcel Moolenaar gsignal(pgid, sig) 14442c42a146SMarcel Moolenaar int pgid, sig; 1445df8bae1dSRodney W. Grimes { 1446df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1447df8bae1dSRodney W. Grimes 1448f591779bSSeigo Tanimura if (pgid != 0) { 1449ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1450f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1451ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1452f591779bSSeigo Tanimura if (pgrp != NULL) { 14532c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1454f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1455f591779bSSeigo Tanimura } 1456f591779bSSeigo Tanimura } 1457df8bae1dSRodney W. Grimes } 1458df8bae1dSRodney W. Grimes 1459df8bae1dSRodney W. Grimes /* 1460df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1461df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1462df8bae1dSRodney W. Grimes */ 1463df8bae1dSRodney W. Grimes void 14642c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1465df8bae1dSRodney W. Grimes struct pgrp *pgrp; 14662c42a146SMarcel Moolenaar int sig, checkctty; 1467df8bae1dSRodney W. Grimes { 1468df8bae1dSRodney W. Grimes register struct proc *p; 1469df8bae1dSRodney W. Grimes 1470628d2653SJohn Baldwin if (pgrp) { 1471f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1472628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1473628d2653SJohn Baldwin PROC_LOCK(p); 1474df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 14752c42a146SMarcel Moolenaar psignal(p, sig); 1476628d2653SJohn Baldwin PROC_UNLOCK(p); 1477628d2653SJohn Baldwin } 1478628d2653SJohn Baldwin } 1479df8bae1dSRodney W. Grimes } 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes /* 14821bf4700bSJeff Roberson * Send a signal caused by a trap to the current thread. 1483df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1484df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1485356861dbSMatthew Dillon * 1486356861dbSMatthew Dillon * MPSAFE 1487df8bae1dSRodney W. Grimes */ 1488df8bae1dSRodney W. Grimes void 14891bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code) 1490df8bae1dSRodney W. Grimes { 14911bf4700bSJeff Roberson struct sigacts *ps; 14921bf4700bSJeff Roberson struct proc *p; 14939dde3bc9SDavid Xu siginfo_t siginfo; 14949dde3bc9SDavid Xu int error; 14951bf4700bSJeff Roberson 14961bf4700bSJeff Roberson p = td->td_proc; 1497aa0aa7a1STim J. Robbins if (td->td_pflags & TDP_SA) { 14987eeaaf9bSDavid Xu if (td->td_mailbox == NULL) 14995995adc2SJulian Elischer thread_user_enter(td); 1500628d2653SJohn Baldwin PROC_LOCK(p); 15019dde3bc9SDavid Xu SIGDELSET(td->td_sigmask, sig); 15029dde3bc9SDavid Xu mtx_lock_spin(&sched_lock); 15039dde3bc9SDavid Xu /* 15049dde3bc9SDavid Xu * Force scheduling an upcall, so UTS has chance to 15059dde3bc9SDavid Xu * process the signal before thread runs again in 15069dde3bc9SDavid Xu * userland. 15079dde3bc9SDavid Xu */ 15089dde3bc9SDavid Xu if (td->td_upcall) 15099dde3bc9SDavid Xu td->td_upcall->ku_flags |= KUF_DOUPCALL; 15109dde3bc9SDavid Xu mtx_unlock_spin(&sched_lock); 1511432b45deSDavid Xu } else { 15129dde3bc9SDavid Xu PROC_LOCK(p); 15139dde3bc9SDavid Xu } 1514ef3dab76STim J. Robbins ps = p->p_sigacts; 151590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 151690af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 15174093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1518df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1519df8bae1dSRodney W. Grimes #ifdef KTRACE 1520374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1521374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 15224093529dSJeff Roberson &td->td_sigmask, code); 1523df8bae1dSRodney W. Grimes #endif 1524aa0aa7a1STim J. Robbins if (!(td->td_pflags & TDP_SA)) 15259dde3bc9SDavid Xu (*p->p_sysent->sv_sendsig)( 15269dde3bc9SDavid Xu ps->ps_sigact[_SIG_IDX(sig)], sig, 15274093529dSJeff Roberson &td->td_sigmask, code); 1528cbf4e354SDavid Xu else if (td->td_mailbox == NULL) { 1529cbf4e354SDavid Xu mtx_unlock(&ps->ps_mtx); 1530cbf4e354SDavid Xu /* UTS caused a sync signal */ 1531cbf4e354SDavid Xu p->p_code = code; /* XXX for core dump/debugger */ 1532cbf4e354SDavid Xu p->p_sig = sig; /* XXX to verify code */ 1533cbf4e354SDavid Xu sigexit(td, sig); 1534cbf4e354SDavid Xu } else { 15354b7d5d84SDavid Xu cpu_thread_siginfo(sig, code, &siginfo); 15369dde3bc9SDavid Xu mtx_unlock(&ps->ps_mtx); 1537cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 15389dde3bc9SDavid Xu PROC_UNLOCK(p); 15399dde3bc9SDavid Xu error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 15409dde3bc9SDavid Xu sizeof(siginfo)); 15419dde3bc9SDavid Xu PROC_LOCK(p); 1542432b45deSDavid Xu /* UTS memory corrupted */ 15439dde3bc9SDavid Xu if (error) 1544cbf4e354SDavid Xu sigexit(td, SIGSEGV); 15459dde3bc9SDavid Xu mtx_lock(&ps->ps_mtx); 15469dde3bc9SDavid Xu } 15474093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15482c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15494093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 15502c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1551289ccde0SPeter Wemm /* 15528f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1553289ccde0SPeter Wemm */ 155490af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 15552c42a146SMarcel Moolenaar if (sig != SIGCONT && 15562c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 155790af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 15582c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1559dedc04feSPeter Wemm } 156090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15616f841fb7SMarcel Moolenaar } else { 156290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15636626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 15642c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 1565c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_TD); 1566df8bae1dSRodney W. Grimes } 1567628d2653SJohn Baldwin PROC_UNLOCK(p); 1568df8bae1dSRodney W. Grimes } 1569df8bae1dSRodney W. Grimes 15704093529dSJeff Roberson static struct thread * 15714093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 15724093529dSJeff Roberson { 15733074d1b4SDavid Xu struct thread *td, *signal_td; 15744093529dSJeff Roberson 15754093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 15764093529dSJeff Roberson 15774093529dSJeff Roberson /* 1578627451c1SDavid Xu * Check if current thread can handle the signal without 1579627451c1SDavid Xu * switching conetxt to another thread. 15804093529dSJeff Roberson */ 1581627451c1SDavid Xu if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 1582627451c1SDavid Xu return (curthread); 15833074d1b4SDavid Xu signal_td = NULL; 1584a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 15853074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 15863074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 15873074d1b4SDavid Xu signal_td = td; 1588627451c1SDavid Xu break; 15893074d1b4SDavid Xu } 15903074d1b4SDavid Xu } 15913074d1b4SDavid Xu if (signal_td == NULL) 15923074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 1593a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 15943074d1b4SDavid Xu return (signal_td); 15954093529dSJeff Roberson } 15964093529dSJeff Roberson 1597df8bae1dSRodney W. Grimes /* 1598df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1599df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1600df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1601df8bae1dSRodney W. Grimes * 1602df8bae1dSRodney W. Grimes * Exceptions: 1603df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1604df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1605df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1606df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1607df8bae1dSRodney W. Grimes * 1608df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 160990af4afaSJohn Baldwin * 161090af4afaSJohn Baldwin * MPSAFE 1611df8bae1dSRodney W. Grimes */ 1612df8bae1dSRodney W. Grimes void 16134093529dSJeff Roberson psignal(struct proc *p, int sig) 1614df8bae1dSRodney W. Grimes { 1615b40ce416SJulian Elischer struct thread *td; 16164093529dSJeff Roberson int prop; 16174093529dSJeff Roberson 161841b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 161941b3077aSJacques Vidrine panic("psignal(): invalid signal"); 162041b3077aSJacques Vidrine 16214093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 162224b2151fSPawel Jakub Dawidek /* 162324b2151fSPawel Jakub Dawidek * IEEE Std 1003.1-2001: return success when killing a zombie. 162424b2151fSPawel Jakub Dawidek */ 162524b2151fSPawel Jakub Dawidek if (p->p_state == PRS_ZOMBIE) 162624b2151fSPawel Jakub Dawidek return; 16274093529dSJeff Roberson prop = sigprop(sig); 16284093529dSJeff Roberson 16294093529dSJeff Roberson /* 16304093529dSJeff Roberson * Find a thread to deliver the signal to. 16314093529dSJeff Roberson */ 16324093529dSJeff Roberson td = sigtd(p, sig, prop); 16334093529dSJeff Roberson 1634c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_P); 16354093529dSJeff Roberson } 16364093529dSJeff Roberson 163790af4afaSJohn Baldwin /* 163890af4afaSJohn Baldwin * MPSAFE 163990af4afaSJohn Baldwin */ 16404093529dSJeff Roberson void 1641c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target) 16424093529dSJeff Roberson { 16439dde3bc9SDavid Xu sigset_t saved; 16449dde3bc9SDavid Xu struct proc *p = td->td_proc; 16459dde3bc9SDavid Xu 16469dde3bc9SDavid Xu if (p->p_flag & P_SA) 16479dde3bc9SDavid Xu saved = p->p_siglist; 1648c197abc4SMike Makonnen do_tdsignal(td, sig, target); 16499dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 16506dbc0850SJohn Baldwin if (!SIGSETEQ(saved, p->p_siglist)) { 16519dde3bc9SDavid Xu /* pending set changed */ 16529dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 16539dde3bc9SDavid Xu wakeup(&p->p_siglist); 16549dde3bc9SDavid Xu } 16559dde3bc9SDavid Xu } 16569dde3bc9SDavid Xu } 16579dde3bc9SDavid Xu 16589dde3bc9SDavid Xu static void 1659c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target) 16609dde3bc9SDavid Xu { 16614093529dSJeff Roberson struct proc *p; 16624093529dSJeff Roberson register sig_t action; 16634093529dSJeff Roberson sigset_t *siglist; 16644093529dSJeff Roberson struct thread *td0; 1665e602ba25SJulian Elischer register int prop; 166690af4afaSJohn Baldwin struct sigacts *ps; 1667df8bae1dSRodney W. Grimes 166841b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 166941b3077aSJacques Vidrine panic("do_tdsignal(): invalid signal"); 16704093529dSJeff Roberson 16714093529dSJeff Roberson p = td->td_proc; 167290af4afaSJohn Baldwin ps = p->p_sigacts; 16732c42a146SMarcel Moolenaar 1674628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1675ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 1676cb679c38SJonathan Lemon 16772c42a146SMarcel Moolenaar prop = sigprop(sig); 16784093529dSJeff Roberson 16794093529dSJeff Roberson /* 168014b5ae1aSMike Makonnen * If the signal is blocked and not destined for this thread, then 168114b5ae1aSMike Makonnen * assign it to the process so that we can find it later in the first 168214b5ae1aSMike Makonnen * thread that unblocks it. Otherwise, assign it to this thread now. 16834093529dSJeff Roberson */ 16843074d1b4SDavid Xu if (target == SIGTARGET_TD) { 16853074d1b4SDavid Xu siglist = &td->td_siglist; 16863074d1b4SDavid Xu } else { 16873074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) 16883074d1b4SDavid Xu siglist = &td->td_siglist; 16893074d1b4SDavid Xu else 16903074d1b4SDavid Xu siglist = &p->p_siglist; 16913074d1b4SDavid Xu } 16924093529dSJeff Roberson 1693df8bae1dSRodney W. Grimes /* 16942a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 16952a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 16962a024a2bSSean Eric Fagan * a chance, as well. 1697df8bae1dSRodney W. Grimes */ 1698b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1699df8bae1dSRodney W. Grimes action = SIG_DFL; 1700b40ce416SJulian Elischer } else { 1701df8bae1dSRodney W. Grimes /* 1702df8bae1dSRodney W. Grimes * If the signal is being ignored, 1703df8bae1dSRodney W. Grimes * then we forget about it immediately. 170490af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 1705df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1706df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1707df8bae1dSRodney W. Grimes */ 170890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 170990af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) || 171090af4afaSJohn Baldwin (p->p_flag & P_WEXIT)) { 171190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1712df8bae1dSRodney W. Grimes return; 171390af4afaSJohn Baldwin } 17146675b36eSDavid Xu if (SIGISMEMBER(td->td_sigmask, sig)) 1715df8bae1dSRodney W. Grimes action = SIG_HOLD; 171690af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1717df8bae1dSRodney W. Grimes action = SIG_CATCH; 1718df8bae1dSRodney W. Grimes else 1719df8bae1dSRodney W. Grimes action = SIG_DFL; 172090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1721df8bae1dSRodney W. Grimes } 1722df8bae1dSRodney W. Grimes 17234093529dSJeff Roberson if (prop & SA_CONT) { 17241d9c5696SJuli Mallett SIG_STOPSIGMASK(p->p_siglist); 17254093529dSJeff Roberson /* 17264093529dSJeff Roberson * XXX Should investigate leaving STOP and CONT sigs only in 17274093529dSJeff Roberson * the proc's siglist. 17284093529dSJeff Roberson */ 1729a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 17304093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17314093529dSJeff Roberson SIG_STOPSIGMASK(td0->td_siglist); 1732a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 17334093529dSJeff Roberson } 1734df8bae1dSRodney W. Grimes 1735df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1736df8bae1dSRodney W. Grimes /* 1737df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1738df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1739df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1740df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1741df8bae1dSRodney W. Grimes */ 1742e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1743e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1744e602ba25SJulian Elischer (action == SIG_DFL)) 1745df8bae1dSRodney W. Grimes return; 17461d9c5696SJuli Mallett SIG_CONTSIGMASK(p->p_siglist); 1747a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 17484093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17494093529dSJeff Roberson SIG_CONTSIGMASK(td0->td_siglist); 1750a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 17516933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1752df8bae1dSRodney W. Grimes } 17533074d1b4SDavid Xu 17544093529dSJeff Roberson SIGADDSET(*siglist, sig); 17554093529dSJeff Roberson signotify(td); /* uses schedlock */ 17565312b1c7SDavid Xu /* 17575312b1c7SDavid Xu * Defer further processing for signals which are held, 17585312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 17595312b1c7SDavid Xu */ 17605312b1c7SDavid Xu if (action == SIG_HOLD && 17615312b1c7SDavid Xu !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 17625312b1c7SDavid Xu return; 1763df8bae1dSRodney W. Grimes /* 176490d75f78SAlfred Perlstein * SIGKILL: Remove procfs STOPEVENTs. 176590d75f78SAlfred Perlstein */ 176690d75f78SAlfred Perlstein if (sig == SIGKILL) { 176790d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCBIC */ 176890d75f78SAlfred Perlstein p->p_stops = 0; 176990d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCCONT */ 177090d75f78SAlfred Perlstein p->p_step = 0; 177190d75f78SAlfred Perlstein wakeup(&p->p_step); 177290d75f78SAlfred Perlstein } 177390d75f78SAlfred Perlstein /* 1774e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1775e602ba25SJulian Elischer * component. Most processing occurs when the process next 1776e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1777e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1778e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1779e602ba25SJulian Elischer * We try do the per-process part here. 1780df8bae1dSRodney W. Grimes */ 1781e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1782e602ba25SJulian Elischer /* 1783e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1784e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1785e602ba25SJulian Elischer */ 1786e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1787e602ba25SJulian Elischer /* 1788e602ba25SJulian Elischer * The traced process is already stopped, 1789e602ba25SJulian Elischer * so no further action is necessary. 1790e602ba25SJulian Elischer * No signal can restart us. 1791e602ba25SJulian Elischer */ 1792e602ba25SJulian Elischer goto out; 17931c32c37cSJohn Baldwin } 1794b40ce416SJulian Elischer 1795e602ba25SJulian Elischer if (sig == SIGKILL) { 1796df8bae1dSRodney W. Grimes /* 1797e602ba25SJulian Elischer * SIGKILL sets process running. 1798e602ba25SJulian Elischer * It will die elsewhere. 1799e602ba25SJulian Elischer * All threads must be restarted. 1800df8bae1dSRodney W. Grimes */ 1801482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 1802e602ba25SJulian Elischer goto runfast; 1803e602ba25SJulian Elischer } 1804e602ba25SJulian Elischer 1805e602ba25SJulian Elischer if (prop & SA_CONT) { 1806e602ba25SJulian Elischer /* 1807e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 18084093529dSJeff Roberson * process but don't leave the signal in siglist as 18091d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 1810e602ba25SJulian Elischer * continue the process and leave the signal in 18114093529dSJeff Roberson * siglist. If the process catches SIGCONT, let it 1812e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1813e602ba25SJulian Elischer * an event, it goes back to run state. 1814e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1815e602ba25SJulian Elischer */ 18161279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 18176933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1818e602ba25SJulian Elischer if (action == SIG_DFL) { 18194093529dSJeff Roberson SIGDELSET(*siglist, sig); 1820e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1821e602ba25SJulian Elischer /* 1822e602ba25SJulian Elischer * The process wants to catch it so it needs 1823e602ba25SJulian Elischer * to run at least one thread, but which one? 1824e602ba25SJulian Elischer * It would seem that the answer would be to 1825e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1826e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1827e602ba25SJulian Elischer * process, we need to make sure that the 1828e602ba25SJulian Elischer * single thread is runnable asap. 1829e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1830e602ba25SJulian Elischer */ 1831e602ba25SJulian Elischer goto runfast; 1832e602ba25SJulian Elischer } 1833e602ba25SJulian Elischer /* 1834e602ba25SJulian Elischer * The signal is not ignored or caught. 1835e602ba25SJulian Elischer */ 1836e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 183704774f23SJulian Elischer thread_unsuspend(p); 1838e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1839e602ba25SJulian Elischer goto out; 1840e602ba25SJulian Elischer } 1841e602ba25SJulian Elischer 1842e602ba25SJulian Elischer if (prop & SA_STOP) { 1843e602ba25SJulian Elischer /* 1844e602ba25SJulian Elischer * Already stopped, don't need to stop again 1845e602ba25SJulian Elischer * (If we did the shell could get confused). 184604774f23SJulian Elischer * Just make sure the signal STOP bit set. 1847e602ba25SJulian Elischer */ 18481279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 18494093529dSJeff Roberson SIGDELSET(*siglist, sig); 1850e602ba25SJulian Elischer goto out; 1851e602ba25SJulian Elischer } 1852e602ba25SJulian Elischer 1853e602ba25SJulian Elischer /* 1854e602ba25SJulian Elischer * All other kinds of signals: 1855e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1856e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1857e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 185804774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1859e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1860e602ba25SJulian Elischer */ 1861e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 186244f3b092SJohn Baldwin if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 186344f3b092SJohn Baldwin sleepq_abort(td); 1864e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1865df8bae1dSRodney W. Grimes goto out; 1866df8bae1dSRodney W. Grimes /* 18679a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 18689a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 1869df8bae1dSRodney W. Grimes */ 1870e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 187135c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 187235c32a76SDavid Xu !(prop & SA_STOP)) { 1873721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 18744093529dSJeff Roberson tdsigwakeup(td, sig, action); 1875721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1876df8bae1dSRodney W. Grimes goto out; 187704774f23SJulian Elischer } 187835c32a76SDavid Xu if (prop & SA_STOP) { 187935c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 188035c32a76SDavid Xu goto out; 1881e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 1882e574e444SDavid Xu p->p_xstat = sig; 1883cbf4e354SDavid Xu p->p_xthread = td; 188435c32a76SDavid Xu mtx_lock_spin(&sched_lock); 18854093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) { 18864093529dSJeff Roberson if (TD_IS_SLEEPING(td0) && 1887062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 1888062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 18894093529dSJeff Roberson thread_suspend_one(td0); 1890062cf543SDavid Xu } else if (td != td0) { 1891062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 1892062cf543SDavid Xu } 189335c32a76SDavid Xu } 1894e574e444SDavid Xu thread_stopped(p); 18954093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 1896e574e444SDavid Xu SIGDELSET(p->p_siglist, p->p_xstat); 18974093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 18984093529dSJeff Roberson SIGDELSET(td0->td_siglist, p->p_xstat); 18994093529dSJeff Roberson } 190035c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 190135c32a76SDavid Xu goto out; 190235c32a76SDavid Xu } 1903721e5910SJulian Elischer else 1904df8bae1dSRodney W. Grimes goto runfast; 1905df8bae1dSRodney W. Grimes /* NOTREACHED */ 1906e602ba25SJulian Elischer } else { 1907e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 19084093529dSJeff Roberson SIGDELSET(*siglist, sig); 1909e602ba25SJulian Elischer goto out; 1910e602ba25SJulian Elischer } 1911e602ba25SJulian Elischer 1912b40ce416SJulian Elischer /* 1913e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1914e602ba25SJulian Elischer * running threads. 1915b40ce416SJulian Elischer */ 1916e602ba25SJulian Elischer 1917e602ba25SJulian Elischer runfast: 1918aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 19194093529dSJeff Roberson tdsigwakeup(td, sig, action); 1920e602ba25SJulian Elischer thread_unsuspend(p); 1921e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1922e602ba25SJulian Elischer out: 1923e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1924e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1925e602ba25SJulian Elischer } 1926e602ba25SJulian Elischer 1927e602ba25SJulian Elischer /* 1928e602ba25SJulian Elischer * The force of a signal has been directed against a single 1929e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1930e602ba25SJulian Elischer * out of any sleep it may be in etc. 1931e602ba25SJulian Elischer */ 1932e602ba25SJulian Elischer static void 19334093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action) 1934e602ba25SJulian Elischer { 1935e602ba25SJulian Elischer struct proc *p = td->td_proc; 1936e602ba25SJulian Elischer register int prop; 1937e602ba25SJulian Elischer 19388b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1939aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1940e602ba25SJulian Elischer prop = sigprop(sig); 1941a4c2da15SBruce Evans 1942e602ba25SJulian Elischer /* 1943aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1944e602ba25SJulian Elischer * killed in this lifetime. 1945e602ba25SJulian Elischer */ 1946a4c2da15SBruce Evans if (action == SIG_DFL && (prop & SA_KILL)) { 19473ef6ac33SJeff Roberson if (p->p_nice > 0) 19483ef6ac33SJeff Roberson sched_nice(td->td_proc, 0); 1949a4c2da15SBruce Evans if (td->td_priority > PUSER) 1950b3a4fb14SDavid Xu sched_prio(td, PUSER); 1951b40ce416SJulian Elischer } 1952a4c2da15SBruce Evans 195380c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 1954e602ba25SJulian Elischer /* 1955e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1956e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1957e602ba25SJulian Elischer * be noticed when the process returns through 1958e602ba25SJulian Elischer * trap() or syscall(). 1959e602ba25SJulian Elischer */ 196044f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 1961aa0fa334SJulian Elischer return; 1962e602ba25SJulian Elischer /* 1963e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1964e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1965e602ba25SJulian Elischer * for its parent. 1966e602ba25SJulian Elischer */ 1967e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1968e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1969aa0fa334SJulian Elischer } else { 1970df8bae1dSRodney W. Grimes /* 1971e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1972e602ba25SJulian Elischer * asleep, we are finished; the process should not 1973e602ba25SJulian Elischer * be awakened. 1974df8bae1dSRodney W. Grimes */ 1975e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 19761d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 19774093529dSJeff Roberson /* 19784093529dSJeff Roberson * It may be on either list in this state. 19794093529dSJeff Roberson * Remove from both for now. 19804093529dSJeff Roberson */ 19814093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 1982aa0fa334SJulian Elischer return; 1983df8bae1dSRodney W. Grimes } 1984df8bae1dSRodney W. Grimes 1985aa0fa334SJulian Elischer /* 1986a4c2da15SBruce Evans * Give low priority threads a better chance to run. 1987aa0fa334SJulian Elischer */ 198844f3b092SJohn Baldwin if (td->td_priority > PUSER) 1989b3a4fb14SDavid Xu sched_prio(td, PUSER); 1990aa0fa334SJulian Elischer } 199144f3b092SJohn Baldwin sleepq_abort(td); 1992a4c2da15SBruce Evans } else { 1993df8bae1dSRodney W. Grimes /* 1994a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 1995df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1996df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1997df8bae1dSRodney W. Grimes */ 1998a4c2da15SBruce Evans #ifdef SMP 199944f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2000e602ba25SJulian Elischer forward_signal(td); 20013163861cSTor Egge #endif 20026caa8a15SJohn Baldwin } 2003a4c2da15SBruce Evans } 2004df8bae1dSRodney W. Grimes 2005cbf4e354SDavid Xu int 20064cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig) 20074cc9f52fSRobert Drehmel { 20084cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 2009cbf4e354SDavid Xu struct thread *td0; 20104cc9f52fSRobert Drehmel 201130a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 20124cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 20134cc9f52fSRobert Drehmel &p->p_mtx.mtx_object, "Stopping for traced signal"); 20144cc9f52fSRobert Drehmel 20154cc9f52fSRobert Drehmel mtx_lock_spin(&sched_lock); 2016cbf4e354SDavid Xu td->td_flags |= TDF_XSIG; 2017cbf4e354SDavid Xu mtx_unlock_spin(&sched_lock); 2018cbf4e354SDavid Xu td->td_xsig = sig; 2019cbf4e354SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2020cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) { 2021cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2022cbf4e354SDavid Xu td->td_flags &= ~TDF_XSIG; 2023cbf4e354SDavid Xu mtx_unlock_spin(&sched_lock); 2024cbf4e354SDavid Xu return (sig); 2025cbf4e354SDavid Xu } 2026cbf4e354SDavid Xu /* 2027cbf4e354SDavid Xu * Just make wait() to work, the last stopped thread 2028cbf4e354SDavid Xu * will win. 2029cbf4e354SDavid Xu */ 2030cbf4e354SDavid Xu p->p_xstat = sig; 2031cbf4e354SDavid Xu p->p_xthread = td; 2032cbf4e354SDavid Xu p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2033cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2034cbf4e354SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2035cbf4e354SDavid Xu if (TD_IS_SLEEPING(td0) && 2036cbf4e354SDavid Xu (td0->td_flags & TDF_SINTR) && 2037cbf4e354SDavid Xu !TD_IS_SUSPENDED(td0)) { 2038cbf4e354SDavid Xu thread_suspend_one(td0); 2039cbf4e354SDavid Xu } else if (td != td0) { 2040cbf4e354SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2041cbf4e354SDavid Xu } 2042cbf4e354SDavid Xu } 2043cbf4e354SDavid Xu stopme: 2044cbf4e354SDavid Xu thread_stopped(p); 20454cc9f52fSRobert Drehmel thread_suspend_one(td); 20464cc9f52fSRobert Drehmel PROC_UNLOCK(p); 20474cc9f52fSRobert Drehmel DROP_GIANT(); 2048cbf4e354SDavid Xu mi_switch(SW_VOL, NULL); 20494cc9f52fSRobert Drehmel mtx_unlock_spin(&sched_lock); 20504cc9f52fSRobert Drehmel PICKUP_GIANT(); 2051cbf4e354SDavid Xu PROC_LOCK(p); 2052cbf4e354SDavid Xu if (!(p->p_flag & P_TRACED)) 2053cbf4e354SDavid Xu break; 2054cbf4e354SDavid Xu if (td->td_flags & TDF_DBSUSPEND) { 2055cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2056cbf4e354SDavid Xu break; 2057cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2058cbf4e354SDavid Xu goto stopme; 2059cbf4e354SDavid Xu } 2060cbf4e354SDavid Xu } 2061cbf4e354SDavid Xu return (td->td_xsig); 20624cc9f52fSRobert Drehmel } 20634cc9f52fSRobert Drehmel 2064df8bae1dSRodney W. Grimes /* 2065df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2066df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2067df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2068df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2069df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2070628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2071df8bae1dSRodney W. Grimes * sequence is 2072df8bae1dSRodney W. Grimes * 2073e602ba25SJulian Elischer * while (sig = cursig(curthread)) 20742c42a146SMarcel Moolenaar * postsig(sig); 2075df8bae1dSRodney W. Grimes */ 20766711f10fSJohn Baldwin static int 2077e602ba25SJulian Elischer issignal(td) 2078e602ba25SJulian Elischer struct thread *td; 2079df8bae1dSRodney W. Grimes { 2080e602ba25SJulian Elischer struct proc *p; 208190af4afaSJohn Baldwin struct sigacts *ps; 20824093529dSJeff Roberson sigset_t sigpending; 2083cbf4e354SDavid Xu int sig, prop, newsig; 2084062cf543SDavid Xu struct thread *td0; 2085df8bae1dSRodney W. Grimes 2086e602ba25SJulian Elischer p = td->td_proc; 208790af4afaSJohn Baldwin ps = p->p_sigacts; 208890af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2089628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2090df8bae1dSRodney W. Grimes for (;;) { 20912a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 20922a024a2bSSean Eric Fagan 20934093529dSJeff Roberson sigpending = td->td_siglist; 20944093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 20954093529dSJeff Roberson 2096df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 20974093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 20984093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2099df8bae1dSRodney W. Grimes return (0); 21004093529dSJeff Roberson sig = sig_ffs(&sigpending); 21012a024a2bSSean Eric Fagan 2102047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2103047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2104047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2105047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2106047aa39bSRobert Watson } 21072a024a2bSSean Eric Fagan 2108df8bae1dSRodney W. Grimes /* 2109df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2110df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2111df8bae1dSRodney W. Grimes */ 211290af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 21134093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 2114cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2115cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 2116df8bae1dSRodney W. Grimes continue; 2117df8bae1dSRodney W. Grimes } 2118df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2119df8bae1dSRodney W. Grimes /* 2120d8f4f6a4SJonathan Mini * If traced, always stop. 2121df8bae1dSRodney W. Grimes */ 212290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2123cbf4e354SDavid Xu newsig = ptracestop(td, sig); 212490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2125df8bae1dSRodney W. Grimes 2126df8bae1dSRodney W. Grimes /* 2127df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 2128df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 2129df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 2130df8bae1dSRodney W. Grimes */ 21314093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* clear old signal */ 2132cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2133cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 2134cbf4e354SDavid Xu if (newsig == 0) 2135df8bae1dSRodney W. Grimes continue; 2136cbf4e354SDavid Xu sig = newsig; 2137df8bae1dSRodney W. Grimes /* 21388d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 21398d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 21408d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 21418d542cb5SDavid E. O'Brien */ 21428d542cb5SDavid E. O'Brien if ((p->p_flag & P_TRACED) == 0) 21438d542cb5SDavid E. O'Brien continue; 21448d542cb5SDavid E. O'Brien 21458d542cb5SDavid E. O'Brien /* 21464093529dSJeff Roberson * Put the new signal into td_siglist. If the 21471d9c5696SJuli Mallett * signal is being masked, look for other signals. 2148df8bae1dSRodney W. Grimes */ 21494093529dSJeff Roberson SIGADDSET(td->td_siglist, sig); 2150cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2151cbf4e354SDavid Xu SIGDELSET(td->td_sigmask, sig); 21524093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 2153df8bae1dSRodney W. Grimes continue; 21544093529dSJeff Roberson signotify(td); 2155df8bae1dSRodney W. Grimes } 2156df8bae1dSRodney W. Grimes 21578d542cb5SDavid E. O'Brien prop = sigprop(sig); 21588d542cb5SDavid E. O'Brien 2159df8bae1dSRodney W. Grimes /* 2160df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2161df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2162df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2163df8bae1dSRodney W. Grimes */ 2164d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2165df8bae1dSRodney W. Grimes 2166d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2167df8bae1dSRodney W. Grimes /* 2168df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2169df8bae1dSRodney W. Grimes */ 2170df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2171df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2172df8bae1dSRodney W. Grimes /* 2173df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2174df8bae1dSRodney W. Grimes * in init? XXX 2175df8bae1dSRodney W. Grimes */ 2176d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 21772c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2178df8bae1dSRodney W. Grimes #endif 2179df8bae1dSRodney W. Grimes break; /* == ignore */ 2180df8bae1dSRodney W. Grimes } 2181df8bae1dSRodney W. Grimes /* 2182df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 2183df8bae1dSRodney W. Grimes * with default action, stop here, 2184df8bae1dSRodney W. Grimes * then clear the signal. However, 2185df8bae1dSRodney W. Grimes * if process is member of an orphaned 2186df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 2187df8bae1dSRodney W. Grimes */ 2188df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 2189df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 2190df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2191df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 2192df8bae1dSRodney W. Grimes break; /* == ignore */ 219390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 219490af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 219590af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2196e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 21972c42a146SMarcel Moolenaar p->p_xstat = sig; 2198cbf4e354SDavid Xu p->p_xthread = td; 2199721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 2200062cf543SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2201062cf543SDavid Xu if (TD_IS_SLEEPING(td0) && 2202062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 2203062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 2204062cf543SDavid Xu thread_suspend_one(td0); 2205062cf543SDavid Xu } else if (td != td0) { 2206062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2207062cf543SDavid Xu } 2208062cf543SDavid Xu } 2209e574e444SDavid Xu thread_stopped(p); 221071fad9fdSJulian Elischer thread_suspend_one(td); 2211721e5910SJulian Elischer PROC_UNLOCK(p); 2212721e5910SJulian Elischer DROP_GIANT(); 2213bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 2214721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 2215721e5910SJulian Elischer PICKUP_GIANT(); 2216721e5910SJulian Elischer PROC_LOCK(p); 221790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2218df8bae1dSRodney W. Grimes break; 221921b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 2220df8bae1dSRodney W. Grimes /* 2221df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2222df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2223df8bae1dSRodney W. Grimes */ 2224df8bae1dSRodney W. Grimes break; /* == ignore */ 2225df8bae1dSRodney W. Grimes } else 22262c42a146SMarcel Moolenaar return (sig); 2227df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2228df8bae1dSRodney W. Grimes 2229d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2230df8bae1dSRodney W. Grimes /* 2231df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2232df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2233df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2234df8bae1dSRodney W. Grimes */ 2235df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 2236df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2237df8bae1dSRodney W. Grimes printf("issignal\n"); 2238df8bae1dSRodney W. Grimes break; /* == ignore */ 2239df8bae1dSRodney W. Grimes 2240df8bae1dSRodney W. Grimes default: 2241df8bae1dSRodney W. Grimes /* 2242df8bae1dSRodney W. Grimes * This signal has an action, let 2243df8bae1dSRodney W. Grimes * postsig() process it. 2244df8bae1dSRodney W. Grimes */ 22452c42a146SMarcel Moolenaar return (sig); 2246df8bae1dSRodney W. Grimes } 22474093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2248df8bae1dSRodney W. Grimes } 2249df8bae1dSRodney W. Grimes /* NOTREACHED */ 2250df8bae1dSRodney W. Grimes } 2251df8bae1dSRodney W. Grimes 2252df8bae1dSRodney W. Grimes /* 2253df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 2254df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 22554a3b3dcbSColin Percival * on the run queue. Must be called with the proc p locked. 2256df8bae1dSRodney W. Grimes */ 22575b3047d5SJohn Baldwin static void 2258e574e444SDavid Xu stop(struct proc *p) 2259df8bae1dSRodney W. Grimes { 2260df8bae1dSRodney W. Grimes 2261628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 22621279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 2263df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 226401609114SAlfred Perlstein wakeup(p->p_pptr); 2265df8bae1dSRodney W. Grimes } 2266df8bae1dSRodney W. Grimes 226790af4afaSJohn Baldwin /* 226890af4afaSJohn Baldwin * MPSAFE 226990af4afaSJohn Baldwin */ 2270e574e444SDavid Xu void 2271e574e444SDavid Xu thread_stopped(struct proc *p) 2272e574e444SDavid Xu { 2273e574e444SDavid Xu struct proc *p1 = curthread->td_proc; 227490af4afaSJohn Baldwin struct sigacts *ps; 2275e574e444SDavid Xu int n; 2276e574e444SDavid Xu 2277e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 2278e574e444SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 2279e574e444SDavid Xu n = p->p_suspcount; 2280e574e444SDavid Xu if (p == p1) 2281e574e444SDavid Xu n++; 2282e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2283e574e444SDavid Xu mtx_unlock_spin(&sched_lock); 2284e574e444SDavid Xu stop(p); 2285e574e444SDavid Xu PROC_LOCK(p->p_pptr); 228690af4afaSJohn Baldwin ps = p->p_pptr->p_sigacts; 228790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 228890af4afaSJohn Baldwin if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 228990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2290e574e444SDavid Xu psignal(p->p_pptr, SIGCHLD); 229190af4afaSJohn Baldwin } else 229290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2293e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 2294e574e444SDavid Xu mtx_lock_spin(&sched_lock); 2295e574e444SDavid Xu } 2296e574e444SDavid Xu } 2297e574e444SDavid Xu 2298df8bae1dSRodney W. Grimes /* 2299df8bae1dSRodney W. Grimes * Take the action for the specified signal 2300df8bae1dSRodney W. Grimes * from the current set of pending signals. 2301df8bae1dSRodney W. Grimes */ 2302df8bae1dSRodney W. Grimes void 23032c42a146SMarcel Moolenaar postsig(sig) 23042c42a146SMarcel Moolenaar register int sig; 2305df8bae1dSRodney W. Grimes { 2306b40ce416SJulian Elischer struct thread *td = curthread; 2307b40ce416SJulian Elischer register struct proc *p = td->td_proc; 2308628d2653SJohn Baldwin struct sigacts *ps; 23092c42a146SMarcel Moolenaar sig_t action; 23102c42a146SMarcel Moolenaar sigset_t returnmask; 23112c42a146SMarcel Moolenaar int code; 2312df8bae1dSRodney W. Grimes 23132c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 23145526d2d9SEivind Eklund 23152ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2316628d2653SJohn Baldwin ps = p->p_sigacts; 231790af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 23184093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 23192c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2320df8bae1dSRodney W. Grimes #ifdef KTRACE 2321374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 23225e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 23234093529dSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 2324df8bae1dSRodney W. Grimes #endif 2325047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2326047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2327047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2328047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2329047aa39bSRobert Watson } 23302a024a2bSSean Eric Fagan 2331cbf4e354SDavid Xu if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2332df8bae1dSRodney W. Grimes /* 2333df8bae1dSRodney W. Grimes * Default action, where the default is to kill 2334df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 2335df8bae1dSRodney W. Grimes */ 233690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2337b40ce416SJulian Elischer sigexit(td, sig); 2338df8bae1dSRodney W. Grimes /* NOTREACHED */ 2339df8bae1dSRodney W. Grimes } else { 2340cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) { 2341432b45deSDavid Xu if (sig == SIGKILL) { 2342432b45deSDavid Xu mtx_unlock(&ps->ps_mtx); 2343432b45deSDavid Xu sigexit(td, sig); 2344432b45deSDavid Xu } 2345432b45deSDavid Xu } 2346432b45deSDavid Xu 2347df8bae1dSRodney W. Grimes /* 2348df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 2349df8bae1dSRodney W. Grimes */ 23504093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 23515526d2d9SEivind Eklund ("postsig action")); 2352df8bae1dSRodney W. Grimes /* 2353df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 2354645682fdSLuoqi Chen * occurrences of this signal. 2355df8bae1dSRodney W. Grimes * 2356645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 2357df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 2358645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 2359df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 2360df8bae1dSRodney W. Grimes */ 23615e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 23624093529dSJeff Roberson returnmask = td->td_oldsigmask; 23635e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 2364df8bae1dSRodney W. Grimes } else 23654093529dSJeff Roberson returnmask = td->td_sigmask; 23662c42a146SMarcel Moolenaar 23674093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 23682c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 23694093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 23702c42a146SMarcel Moolenaar 23712c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2372289ccde0SPeter Wemm /* 23738f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 2374289ccde0SPeter Wemm */ 237590af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 23762c42a146SMarcel Moolenaar if (sig != SIGCONT && 23772c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 237890af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 23792c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2380dedc04feSPeter Wemm } 2381df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 23822c42a146SMarcel Moolenaar if (p->p_sig != sig) { 2383df8bae1dSRodney W. Grimes code = 0; 2384df8bae1dSRodney W. Grimes } else { 23856626c604SJulian Elischer code = p->p_code; 23866626c604SJulian Elischer p->p_code = 0; 23876626c604SJulian Elischer p->p_sig = 0; 2388df8bae1dSRodney W. Grimes } 2389cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 239058a3c273SJeff Roberson thread_signal_add(curthread, sig); 239158a3c273SJeff Roberson else 239258a3c273SJeff Roberson (*p->p_sysent->sv_sendsig)(action, sig, 239358a3c273SJeff Roberson &returnmask, code); 2394df8bae1dSRodney W. Grimes } 2395df8bae1dSRodney W. Grimes } 2396df8bae1dSRodney W. Grimes 2397df8bae1dSRodney W. Grimes /* 2398df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2399df8bae1dSRodney W. Grimes */ 240026f9a767SRodney W. Grimes void 2401df8bae1dSRodney W. Grimes killproc(p, why) 2402df8bae1dSRodney W. Grimes struct proc *p; 2403df8bae1dSRodney W. Grimes char *why; 2404df8bae1dSRodney W. Grimes { 24059081e5e8SJohn Baldwin 24069081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 24070384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 24080384fff8SJason Evans p, p->p_pid, p->p_comm); 2409729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2410b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2411df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 2412df8bae1dSRodney W. Grimes } 2413df8bae1dSRodney W. Grimes 2414df8bae1dSRodney W. Grimes /* 2415df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2416df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2417df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2418df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2419df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2420df8bae1dSRodney W. Grimes * does not return. 242190af4afaSJohn Baldwin * 242290af4afaSJohn Baldwin * MPSAFE 2423df8bae1dSRodney W. Grimes */ 242426f9a767SRodney W. Grimes void 2425b40ce416SJulian Elischer sigexit(td, sig) 2426b40ce416SJulian Elischer struct thread *td; 24272c42a146SMarcel Moolenaar int sig; 2428df8bae1dSRodney W. Grimes { 2429b40ce416SJulian Elischer struct proc *p = td->td_proc; 2430df8bae1dSRodney W. Grimes 2431628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2432df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 2433f97c3df1SDavid Schultz /* 2434f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 2435f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 2436f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 2437f97c3df1SDavid Schultz * change out from under it. 2438f97c3df1SDavid Schultz * 2439f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 2440f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 2441f97c3df1SDavid Schultz */ 2442f97c3df1SDavid Schultz if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) { 24432c42a146SMarcel Moolenaar p->p_sig = sig; 2444c364e17eSAndrey A. Chernov /* 2445c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2446c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2447c364e17eSAndrey A. Chernov * these messages.) 2448c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 24494ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 2450c364e17eSAndrey A. Chernov */ 2451b40ce416SJulian Elischer if (coredump(td) == 0) 24522c42a146SMarcel Moolenaar sig |= WCOREFLAG; 245357308494SJoerg Wunsch if (kern_logsigexit) 245457308494SJoerg Wunsch log(LOG_INFO, 245557308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 24563d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 24579c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 24582c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 24592c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 24604ae89b95SJohn Baldwin } else 2461628d2653SJohn Baldwin PROC_UNLOCK(p); 2462b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2463df8bae1dSRodney W. Grimes /* NOTREACHED */ 2464df8bae1dSRodney W. Grimes } 2465df8bae1dSRodney W. Grimes 24666d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 2467c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2468c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 2469c5edb423SSean Eric Fagan 2470c5edb423SSean Eric Fagan /* 2471c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 2472c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 2473c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 2474c5edb423SSean Eric Fagan * %N name of process ("name") 2475c5edb423SSean Eric Fagan * %P process id (pid) 2476c5edb423SSean Eric Fagan * %U user id (uid) 2477c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 2478c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2479c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 2480c5edb423SSean Eric Fagan */ 2481c5edb423SSean Eric Fagan 2482fca666a1SJulian Elischer static char * 2483c5edb423SSean Eric Fagan expand_name(name, uid, pid) 24848b43b535SAlfred Perlstein const char *name; 24858b43b535SAlfred Perlstein uid_t uid; 24868b43b535SAlfred Perlstein pid_t pid; 24878b43b535SAlfred Perlstein { 24888b43b535SAlfred Perlstein const char *format, *appendstr; 2489c5edb423SSean Eric Fagan char *temp; 2490c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 24918b43b535SAlfred Perlstein size_t i, l, n; 2492c5edb423SSean Eric Fagan 24938b43b535SAlfred Perlstein format = corefilename; 24948b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 24950bfe2990SEivind Eklund if (temp == NULL) 24968b43b535SAlfred Perlstein return (NULL); 2497ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2498c5edb423SSean Eric Fagan switch (format[i]) { 2499c5edb423SSean Eric Fagan case '%': /* Format character */ 2500c5edb423SSean Eric Fagan i++; 2501c5edb423SSean Eric Fagan switch (format[i]) { 2502c5edb423SSean Eric Fagan case '%': 25038b43b535SAlfred Perlstein appendstr = "%"; 2504c5edb423SSean Eric Fagan break; 2505c5edb423SSean Eric Fagan case 'N': /* process name */ 25068b43b535SAlfred Perlstein appendstr = name; 2507c5edb423SSean Eric Fagan break; 2508c5edb423SSean Eric Fagan case 'P': /* process id */ 25098b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 25108b43b535SAlfred Perlstein appendstr = buf; 2511c5edb423SSean Eric Fagan break; 2512c5edb423SSean Eric Fagan case 'U': /* user id */ 25138b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 25148b43b535SAlfred Perlstein appendstr = buf; 2515c5edb423SSean Eric Fagan break; 2516c5edb423SSean Eric Fagan default: 25178b43b535SAlfred Perlstein appendstr = ""; 25188b43b535SAlfred Perlstein log(LOG_ERR, 25198b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 25208b43b535SAlfred Perlstein format[i], format); 2521c5edb423SSean Eric Fagan } 25228b43b535SAlfred Perlstein l = strlen(appendstr); 25238b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 25248b43b535SAlfred Perlstein goto toolong; 25258b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 25268b43b535SAlfred Perlstein n += l; 2527c5edb423SSean Eric Fagan break; 2528c5edb423SSean Eric Fagan default: 2529c5edb423SSean Eric Fagan temp[n++] = format[i]; 2530c5edb423SSean Eric Fagan } 2531c5edb423SSean Eric Fagan } 25328b43b535SAlfred Perlstein if (format[i] != '\0') 25338b43b535SAlfred Perlstein goto toolong; 25348b43b535SAlfred Perlstein return (temp); 25358b43b535SAlfred Perlstein toolong: 25368b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 25378b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 25388b43b535SAlfred Perlstein free(temp, M_TEMP); 25398b43b535SAlfred Perlstein return (NULL); 2540c5edb423SSean Eric Fagan } 2541c5edb423SSean Eric Fagan 2542df8bae1dSRodney W. Grimes /* 2543fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2544fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2545fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2546fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2547fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2548fca666a1SJulian Elischer */ 2549fca666a1SJulian Elischer 2550fca666a1SJulian Elischer static int 2551b40ce416SJulian Elischer coredump(struct thread *td) 2552fca666a1SJulian Elischer { 2553b40ce416SJulian Elischer struct proc *p = td->td_proc; 2554fca666a1SJulian Elischer register struct vnode *vp; 25559c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 255606ae1e91SMatthew Dillon struct flock lf; 2557fca666a1SJulian Elischer struct nameidata nd; 2558fca666a1SJulian Elischer struct vattr vattr; 2559c447f5b2SRobert Watson int error, error1, flags, locked; 2560f2a2857bSKirk McKusick struct mount *mp; 2561fca666a1SJulian Elischer char *name; /* name of corefile */ 2562fca666a1SJulian Elischer off_t limit; 2563fca666a1SJulian Elischer 25644ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2565f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 2566628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2567fca666a1SJulian Elischer 2568628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2569628d2653SJohn Baldwin PROC_UNLOCK(p); 2570fca666a1SJulian Elischer return (EFAULT); 2571628d2653SJohn Baldwin } 2572fca666a1SJulian Elischer 2573fca666a1SJulian Elischer /* 257435a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 257535a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 257635a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 257735a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 257835a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 257935a2598fSSean Eric Fagan * if it is larger than the limit. 2580fca666a1SJulian Elischer */ 258191d5354aSJohn Baldwin limit = (off_t)lim_cur(p, RLIMIT_CORE); 2582628d2653SJohn Baldwin PROC_UNLOCK(p); 258391d5354aSJohn Baldwin if (limit == 0) 258491d5354aSJohn Baldwin return (EFBIG); 258535a2598fSSean Eric Fagan 25864ae89b95SJohn Baldwin mtx_lock(&Giant); 2587f2a2857bSKirk McKusick restart: 25889c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 25894ae89b95SJohn Baldwin if (name == NULL) { 25904ae89b95SJohn Baldwin mtx_unlock(&Giant); 2591ccdbd10cSPeter Pentchev return (EINVAL); 25924ae89b95SJohn Baldwin } 2593b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2594e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 25957c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 2596fca666a1SJulian Elischer free(name, M_TEMP); 25974ae89b95SJohn Baldwin if (error) { 25984ae89b95SJohn Baldwin mtx_unlock(&Giant); 2599fca666a1SJulian Elischer return (error); 26004ae89b95SJohn Baldwin } 2601762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2602fca666a1SJulian Elischer vp = nd.ni_vp; 260306ae1e91SMatthew Dillon 2604832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2605832dafadSDon Lewis if (vp->v_type != VREG || 2606832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2607832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2608832dafadSDon Lewis error = EFAULT; 26094ae89b95SJohn Baldwin goto out; 2610832dafadSDon Lewis } 2611832dafadSDon Lewis 2612b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 261306ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 261406ae1e91SMatthew Dillon lf.l_start = 0; 261506ae1e91SMatthew Dillon lf.l_len = 0; 261606ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 2617c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 261806ae1e91SMatthew Dillon 261906ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 262006ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 262136bbf86bSRobert Watson if (locked) 262206ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2623b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2624f2a2857bSKirk McKusick return (error); 2625f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2626f2a2857bSKirk McKusick return (error); 2627f2a2857bSKirk McKusick goto restart; 2628f2a2857bSKirk McKusick } 2629fca666a1SJulian Elischer 2630fca666a1SJulian Elischer VATTR_NULL(&vattr); 2631fca666a1SJulian Elischer vattr.va_size = 0; 26326141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 26336141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 263488b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2635b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2636b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 263788b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2638628d2653SJohn Baldwin PROC_LOCK(p); 2639fca666a1SJulian Elischer p->p_acflag |= ACORE; 2640628d2653SJohn Baldwin PROC_UNLOCK(p); 2641fca666a1SJulian Elischer 2642fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2643b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2644fca666a1SJulian Elischer ENOSYS; 2645fca666a1SJulian Elischer 2646c447f5b2SRobert Watson if (locked) { 264706ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 264806ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2649c447f5b2SRobert Watson } 2650f2a2857bSKirk McKusick vn_finished_write(mp); 26514ae89b95SJohn Baldwin out: 2652b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 26534ae89b95SJohn Baldwin mtx_unlock(&Giant); 2654fca666a1SJulian Elischer if (error == 0) 2655fca666a1SJulian Elischer error = error1; 2656fca666a1SJulian Elischer return (error); 2657fca666a1SJulian Elischer } 2658fca666a1SJulian Elischer 2659fca666a1SJulian Elischer /* 2660df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2661df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2662df8bae1dSRodney W. Grimes */ 2663d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2664df8bae1dSRodney W. Grimes struct nosys_args { 2665df8bae1dSRodney W. Grimes int dummy; 2666df8bae1dSRodney W. Grimes }; 2667d2d3e875SBruce Evans #endif 2668fb99ab88SMatthew Dillon /* 2669fb99ab88SMatthew Dillon * MPSAFE 2670fb99ab88SMatthew Dillon */ 2671df8bae1dSRodney W. Grimes /* ARGSUSED */ 267226f9a767SRodney W. Grimes int 2673b40ce416SJulian Elischer nosys(td, args) 2674b40ce416SJulian Elischer struct thread *td; 2675df8bae1dSRodney W. Grimes struct nosys_args *args; 2676df8bae1dSRodney W. Grimes { 2677b40ce416SJulian Elischer struct proc *p = td->td_proc; 2678b40ce416SJulian Elischer 2679628d2653SJohn Baldwin PROC_LOCK(p); 2680df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2681628d2653SJohn Baldwin PROC_UNLOCK(p); 2682f5216b9aSBruce Evans return (ENOSYS); 2683df8bae1dSRodney W. Grimes } 2684831d27a9SDon Lewis 2685831d27a9SDon Lewis /* 268648f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2687831d27a9SDon Lewis * stored credentials rather than those of the current process. 2688831d27a9SDon Lewis */ 2689831d27a9SDon Lewis void 2690f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2691f1320723SAlfred Perlstein struct sigio **sigiop; 26922c42a146SMarcel Moolenaar int sig, checkctty; 2693831d27a9SDon Lewis { 2694f1320723SAlfred Perlstein struct sigio *sigio; 2695831d27a9SDon Lewis 2696f1320723SAlfred Perlstein SIGIO_LOCK(); 2697f1320723SAlfred Perlstein sigio = *sigiop; 2698f1320723SAlfred Perlstein if (sigio == NULL) { 2699f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2700f1320723SAlfred Perlstein return; 2701f1320723SAlfred Perlstein } 2702831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2703628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 27042b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 27052c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2706628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2707831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2708831d27a9SDon Lewis struct proc *p; 2709831d27a9SDon Lewis 2710f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2711628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2712628d2653SJohn Baldwin PROC_LOCK(p); 27132b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2714831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 27152c42a146SMarcel Moolenaar psignal(p, sig); 2716628d2653SJohn Baldwin PROC_UNLOCK(p); 2717628d2653SJohn Baldwin } 2718f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2719831d27a9SDon Lewis } 2720f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2721831d27a9SDon Lewis } 2722cb679c38SJonathan Lemon 2723cb679c38SJonathan Lemon static int 2724cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2725cb679c38SJonathan Lemon { 2726cb679c38SJonathan Lemon struct proc *p = curproc; 2727cb679c38SJonathan Lemon 2728cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2729cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2730cb679c38SJonathan Lemon 2731ad3b9257SJohn-Mark Gurney knlist_add(&p->p_klist, kn, 0); 2732cb679c38SJonathan Lemon 2733cb679c38SJonathan Lemon return (0); 2734cb679c38SJonathan Lemon } 2735cb679c38SJonathan Lemon 2736cb679c38SJonathan Lemon static void 2737cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2738cb679c38SJonathan Lemon { 2739cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2740cb679c38SJonathan Lemon 2741ad3b9257SJohn-Mark Gurney knlist_remove(&p->p_klist, kn, 0); 2742cb679c38SJonathan Lemon } 2743cb679c38SJonathan Lemon 2744cb679c38SJonathan Lemon /* 2745cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2746cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2747cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2748cb679c38SJonathan Lemon * isn't worth the trouble. 2749cb679c38SJonathan Lemon */ 2750cb679c38SJonathan Lemon static int 2751cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2752cb679c38SJonathan Lemon { 2753cb679c38SJonathan Lemon 2754cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2755cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2756cb679c38SJonathan Lemon 2757cb679c38SJonathan Lemon if (kn->kn_id == hint) 2758cb679c38SJonathan Lemon kn->kn_data++; 2759cb679c38SJonathan Lemon } 2760cb679c38SJonathan Lemon return (kn->kn_data != 0); 2761cb679c38SJonathan Lemon } 276290af4afaSJohn Baldwin 276390af4afaSJohn Baldwin struct sigacts * 276490af4afaSJohn Baldwin sigacts_alloc(void) 276590af4afaSJohn Baldwin { 276690af4afaSJohn Baldwin struct sigacts *ps; 276790af4afaSJohn Baldwin 276890af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 276990af4afaSJohn Baldwin ps->ps_refcnt = 1; 277090af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 277190af4afaSJohn Baldwin return (ps); 277290af4afaSJohn Baldwin } 277390af4afaSJohn Baldwin 277490af4afaSJohn Baldwin void 277590af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 277690af4afaSJohn Baldwin { 277790af4afaSJohn Baldwin 277890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 277990af4afaSJohn Baldwin ps->ps_refcnt--; 278090af4afaSJohn Baldwin if (ps->ps_refcnt == 0) { 278190af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 278290af4afaSJohn Baldwin free(ps, M_SUBPROC); 278390af4afaSJohn Baldwin } else 278490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 278590af4afaSJohn Baldwin } 278690af4afaSJohn Baldwin 278790af4afaSJohn Baldwin struct sigacts * 278890af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 278990af4afaSJohn Baldwin { 279090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 279190af4afaSJohn Baldwin ps->ps_refcnt++; 279290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 279390af4afaSJohn Baldwin return (ps); 279490af4afaSJohn Baldwin } 279590af4afaSJohn Baldwin 279690af4afaSJohn Baldwin void 279790af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 279890af4afaSJohn Baldwin { 279990af4afaSJohn Baldwin 280090af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 280190af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 280290af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 280390af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 280490af4afaSJohn Baldwin } 280590af4afaSJohn Baldwin 280690af4afaSJohn Baldwin int 280790af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 280890af4afaSJohn Baldwin { 280990af4afaSJohn Baldwin int shared; 281090af4afaSJohn Baldwin 281190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 281290af4afaSJohn Baldwin shared = ps->ps_refcnt > 1; 281390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 281490af4afaSJohn Baldwin return (shared); 281590af4afaSJohn Baldwin } 2816