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; 8473074d1b4SDavid Xu sigset_t savedmask, sigset; 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 } 8973074d1b4SDavid Xu if (sig) { 8983074d1b4SDavid Xu td->td_sigmask = savedmask; 8993074d1b4SDavid Xu signotify(td); 900a447cd8bSJeff Roberson goto out; 9013074d1b4SDavid Xu } 9023074d1b4SDavid Xu } 9033074d1b4SDavid Xu if (error) 9043074d1b4SDavid Xu goto out; 9053074d1b4SDavid Xu 9063074d1b4SDavid Xu td->td_sigmask = savedmask; 9073074d1b4SDavid Xu signotify(td); 9083074d1b4SDavid Xu sigset = td->td_siglist; 9093074d1b4SDavid Xu SIGSETOR(sigset, p->p_siglist); 9103074d1b4SDavid Xu SIGSETAND(sigset, waitset); 9113074d1b4SDavid Xu if (!SIGISEMPTY(sigset)) 9123074d1b4SDavid Xu goto again; 913a447cd8bSJeff Roberson 914a447cd8bSJeff Roberson /* 915a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 916a447cd8bSJeff Roberson * signals. 917a447cd8bSJeff Roberson */ 918a447cd8bSJeff Roberson if (timeout) { 9191089f031SDavid Xu if (!timevalid) { 920a447cd8bSJeff Roberson error = EINVAL; 921a447cd8bSJeff Roberson goto out; 922a447cd8bSJeff Roberson } 9231089f031SDavid Xu getnanouptime(&rts); 9241089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 9253074d1b4SDavid Xu error = EAGAIN; 9263074d1b4SDavid Xu goto out; 9273074d1b4SDavid Xu } 9281089f031SDavid Xu ts = ets; 9291089f031SDavid Xu timespecsub(&ts, &rts); 9301089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 931a447cd8bSJeff Roberson hz = tvtohz(&tv); 932a447cd8bSJeff Roberson } else 933a447cd8bSJeff Roberson hz = 0; 934a447cd8bSJeff Roberson 9353074d1b4SDavid Xu td->td_waitset = &waitset; 93686b5e563SDag-Erling Smørgrav error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 9373074d1b4SDavid Xu td->td_waitset = NULL; 9381089f031SDavid Xu if (timeout) { 9391089f031SDavid Xu if (error == ERESTART) { 9401089f031SDavid Xu /* timeout can not be restarted. */ 9413074d1b4SDavid Xu error = EINTR; 9421089f031SDavid Xu } else if (error == EAGAIN) { 9431089f031SDavid Xu /* will calculate timeout by ourself. */ 9441089f031SDavid Xu error = 0; 9451089f031SDavid Xu } 9461089f031SDavid Xu } 9473074d1b4SDavid Xu goto again; 9489dde3bc9SDavid Xu 949a447cd8bSJeff Roberson out: 950a447cd8bSJeff Roberson if (sig) { 951a447cd8bSJeff Roberson sig_t action; 952a447cd8bSJeff Roberson 9533074d1b4SDavid Xu error = 0; 9543074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 955a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 95690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 957a447cd8bSJeff Roberson #ifdef KTRACE 958a447cd8bSJeff Roberson if (KTRPOINT(td, KTR_PSIG)) 9593074d1b4SDavid Xu ktrpsig(sig, action, &td->td_sigmask, 0); 960a447cd8bSJeff Roberson #endif 961a447cd8bSJeff Roberson _STOPEVENT(p, S_SIG, sig); 962a447cd8bSJeff Roberson 963a447cd8bSJeff Roberson SIGDELSET(td->td_siglist, sig); 9641089f031SDavid Xu bzero(info, sizeof(*info)); 965a447cd8bSJeff Roberson info->si_signo = sig; 966a447cd8bSJeff Roberson info->si_code = 0; 9673074d1b4SDavid Xu } 968a447cd8bSJeff Roberson PROC_UNLOCK(p); 969a447cd8bSJeff Roberson return (error); 970a447cd8bSJeff Roberson } 971a447cd8bSJeff Roberson 972a447cd8bSJeff Roberson /* 973a447cd8bSJeff Roberson * MPSAFE 974a447cd8bSJeff Roberson */ 975a447cd8bSJeff Roberson int 976b40ce416SJulian Elischer sigpending(td, uap) 977b40ce416SJulian Elischer struct thread *td; 978df8bae1dSRodney W. Grimes struct sigpending_args *uap; 979df8bae1dSRodney W. Grimes { 980b40ce416SJulian Elischer struct proc *p = td->td_proc; 981628d2653SJohn Baldwin sigset_t siglist; 982df8bae1dSRodney W. Grimes 983628d2653SJohn Baldwin PROC_LOCK(p); 9841d9c5696SJuli Mallett siglist = p->p_siglist; 9854093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 986628d2653SJohn Baldwin PROC_UNLOCK(p); 98748e8f774STim J. Robbins return (copyout(&siglist, uap->set, sizeof(sigset_t))); 9882c42a146SMarcel Moolenaar } 9892c42a146SMarcel Moolenaar 99031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9912c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9922c42a146SMarcel Moolenaar struct osigpending_args { 9932c42a146SMarcel Moolenaar int dummy; 9942c42a146SMarcel Moolenaar }; 9952c42a146SMarcel Moolenaar #endif 996fb99ab88SMatthew Dillon /* 997fb99ab88SMatthew Dillon * MPSAFE 998fb99ab88SMatthew Dillon */ 9992c42a146SMarcel Moolenaar int 1000b40ce416SJulian Elischer osigpending(td, uap) 1001b40ce416SJulian Elischer struct thread *td; 10022c42a146SMarcel Moolenaar struct osigpending_args *uap; 10032c42a146SMarcel Moolenaar { 1004b40ce416SJulian Elischer struct proc *p = td->td_proc; 10054093529dSJeff Roberson sigset_t siglist; 1006b40ce416SJulian Elischer 1007628d2653SJohn Baldwin PROC_LOCK(p); 10084093529dSJeff Roberson siglist = p->p_siglist; 10094093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 1010628d2653SJohn Baldwin PROC_UNLOCK(p); 10119d8643ecSJohn Baldwin SIG2OSIG(siglist, td->td_retval[0]); 1012df8bae1dSRodney W. Grimes return (0); 1013df8bae1dSRodney W. Grimes } 101431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1015df8bae1dSRodney W. Grimes 10161930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1017df8bae1dSRodney W. Grimes /* 1018df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1019df8bae1dSRodney W. Grimes */ 1020d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1021df8bae1dSRodney W. Grimes struct osigvec_args { 1022df8bae1dSRodney W. Grimes int signum; 1023df8bae1dSRodney W. Grimes struct sigvec *nsv; 1024df8bae1dSRodney W. Grimes struct sigvec *osv; 1025df8bae1dSRodney W. Grimes }; 1026d2d3e875SBruce Evans #endif 1027fb99ab88SMatthew Dillon /* 1028fb99ab88SMatthew Dillon * MPSAFE 1029fb99ab88SMatthew Dillon */ 1030df8bae1dSRodney W. Grimes /* ARGSUSED */ 103126f9a767SRodney W. Grimes int 1032b40ce416SJulian Elischer osigvec(td, uap) 1033b40ce416SJulian Elischer struct thread *td; 1034df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 1035df8bae1dSRodney W. Grimes { 1036df8bae1dSRodney W. Grimes struct sigvec vec; 10372c42a146SMarcel Moolenaar struct sigaction nsa, osa; 10382c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 10392c42a146SMarcel Moolenaar int error; 1040df8bae1dSRodney W. Grimes 10416f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 10426f841fb7SMarcel Moolenaar return (EINVAL); 10436f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 10446f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 10452c42a146SMarcel Moolenaar if (nsap) { 10466f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 10472c42a146SMarcel Moolenaar if (error) 1048df8bae1dSRodney W. Grimes return (error); 10492c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 10502c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 10512c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 10522c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1053df8bae1dSRodney W. Grimes } 10545edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 10552c42a146SMarcel Moolenaar if (osap && !error) { 10562c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 10572c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 10582c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 10592c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 10602c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 10616f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 10622c42a146SMarcel Moolenaar } 10632c42a146SMarcel Moolenaar return (error); 1064df8bae1dSRodney W. Grimes } 1065df8bae1dSRodney W. Grimes 1066d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1067df8bae1dSRodney W. Grimes struct osigblock_args { 1068df8bae1dSRodney W. Grimes int mask; 1069df8bae1dSRodney W. Grimes }; 1070d2d3e875SBruce Evans #endif 1071fb99ab88SMatthew Dillon /* 1072fb99ab88SMatthew Dillon * MPSAFE 1073fb99ab88SMatthew Dillon */ 107426f9a767SRodney W. Grimes int 1075b40ce416SJulian Elischer osigblock(td, uap) 1076b40ce416SJulian Elischer register struct thread *td; 1077df8bae1dSRodney W. Grimes struct osigblock_args *uap; 1078df8bae1dSRodney W. Grimes { 1079b40ce416SJulian Elischer struct proc *p = td->td_proc; 10802c42a146SMarcel Moolenaar sigset_t set; 1081df8bae1dSRodney W. Grimes 10822c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10832c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1084628d2653SJohn Baldwin PROC_LOCK(p); 10854093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 10864093529dSJeff Roberson SIGSETOR(td->td_sigmask, set); 1087628d2653SJohn Baldwin PROC_UNLOCK(p); 1088df8bae1dSRodney W. Grimes return (0); 1089df8bae1dSRodney W. Grimes } 1090df8bae1dSRodney W. Grimes 1091d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1092df8bae1dSRodney W. Grimes struct osigsetmask_args { 1093df8bae1dSRodney W. Grimes int mask; 1094df8bae1dSRodney W. Grimes }; 1095d2d3e875SBruce Evans #endif 1096fb99ab88SMatthew Dillon /* 1097fb99ab88SMatthew Dillon * MPSAFE 1098fb99ab88SMatthew Dillon */ 109926f9a767SRodney W. Grimes int 1100b40ce416SJulian Elischer osigsetmask(td, uap) 1101b40ce416SJulian Elischer struct thread *td; 1102df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 1103df8bae1dSRodney W. Grimes { 1104b40ce416SJulian Elischer struct proc *p = td->td_proc; 11052c42a146SMarcel Moolenaar sigset_t set; 1106df8bae1dSRodney W. Grimes 11072c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 11082c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1109628d2653SJohn Baldwin PROC_LOCK(p); 11104093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 11114093529dSJeff Roberson SIGSETLO(td->td_sigmask, set); 11124093529dSJeff Roberson signotify(td); 1113628d2653SJohn Baldwin PROC_UNLOCK(p); 1114df8bae1dSRodney W. Grimes return (0); 1115df8bae1dSRodney W. Grimes } 11161930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1117df8bae1dSRodney W. Grimes 1118df8bae1dSRodney W. Grimes /* 1119df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 112097563428SAlexander Kabaev * in the meantime. 1121b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 1122b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 1123b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 1124df8bae1dSRodney W. Grimes */ 1125d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1126df8bae1dSRodney W. Grimes struct sigsuspend_args { 11272c42a146SMarcel Moolenaar const sigset_t *sigmask; 1128df8bae1dSRodney W. Grimes }; 1129d2d3e875SBruce Evans #endif 1130fb99ab88SMatthew Dillon /* 1131fb99ab88SMatthew Dillon * MPSAFE 1132fb99ab88SMatthew Dillon */ 1133df8bae1dSRodney W. Grimes /* ARGSUSED */ 113426f9a767SRodney W. Grimes int 1135b40ce416SJulian Elischer sigsuspend(td, uap) 1136b40ce416SJulian Elischer struct thread *td; 1137df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 1138df8bae1dSRodney W. Grimes { 11392c42a146SMarcel Moolenaar sigset_t mask; 11402c42a146SMarcel Moolenaar int error; 11412c42a146SMarcel Moolenaar 11426f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 11432c42a146SMarcel Moolenaar if (error) 11442c42a146SMarcel Moolenaar return (error); 11458f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 11468f19eb88SIan Dowse } 11478f19eb88SIan Dowse 11488f19eb88SIan Dowse int 11498f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 11508f19eb88SIan Dowse { 11518f19eb88SIan Dowse struct proc *p = td->td_proc; 1152df8bae1dSRodney W. Grimes 1153df8bae1dSRodney W. Grimes /* 1154645682fdSLuoqi Chen * When returning from sigsuspend, we want 1155df8bae1dSRodney W. Grimes * the old mask to be restored after the 1156df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1157df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1158df8bae1dSRodney W. Grimes * to indicate this. 1159df8bae1dSRodney W. Grimes */ 1160628d2653SJohn Baldwin PROC_LOCK(p); 11614093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11625e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1163645682fdSLuoqi Chen SIG_CANTMASK(mask); 11644093529dSJeff Roberson td->td_sigmask = mask; 11654093529dSJeff Roberson signotify(td); 116686b5e563SDag-Erling Smørgrav while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 11672c42a146SMarcel Moolenaar /* void */; 1168628d2653SJohn Baldwin PROC_UNLOCK(p); 11692c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 11702c42a146SMarcel Moolenaar return (EINTR); 11712c42a146SMarcel Moolenaar } 11722c42a146SMarcel Moolenaar 117331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 117497563428SAlexander Kabaev /* 117597563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 117697563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 117797563428SAlexander Kabaev */ 11782c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11792c42a146SMarcel Moolenaar struct osigsuspend_args { 11802c42a146SMarcel Moolenaar osigset_t mask; 11812c42a146SMarcel Moolenaar }; 11822c42a146SMarcel Moolenaar #endif 1183fb99ab88SMatthew Dillon /* 1184fb99ab88SMatthew Dillon * MPSAFE 1185fb99ab88SMatthew Dillon */ 11862c42a146SMarcel Moolenaar /* ARGSUSED */ 11872c42a146SMarcel Moolenaar int 1188b40ce416SJulian Elischer osigsuspend(td, uap) 1189b40ce416SJulian Elischer struct thread *td; 11902c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 11912c42a146SMarcel Moolenaar { 1192b40ce416SJulian Elischer struct proc *p = td->td_proc; 1193645682fdSLuoqi Chen sigset_t mask; 11942c42a146SMarcel Moolenaar 1195628d2653SJohn Baldwin PROC_LOCK(p); 11964093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11975e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1198645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 1199645682fdSLuoqi Chen SIG_CANTMASK(mask); 12004093529dSJeff Roberson SIGSETLO(td->td_sigmask, mask); 12014093529dSJeff Roberson signotify(td); 120286b5e563SDag-Erling Smørgrav while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1203df8bae1dSRodney W. Grimes /* void */; 1204628d2653SJohn Baldwin PROC_UNLOCK(p); 1205df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 1206df8bae1dSRodney W. Grimes return (EINTR); 1207df8bae1dSRodney W. Grimes } 120831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1209df8bae1dSRodney W. Grimes 12101930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1211d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1212df8bae1dSRodney W. Grimes struct osigstack_args { 1213df8bae1dSRodney W. Grimes struct sigstack *nss; 1214df8bae1dSRodney W. Grimes struct sigstack *oss; 1215df8bae1dSRodney W. Grimes }; 1216d2d3e875SBruce Evans #endif 1217fb99ab88SMatthew Dillon /* 1218fb99ab88SMatthew Dillon * MPSAFE 1219fb99ab88SMatthew Dillon */ 1220df8bae1dSRodney W. Grimes /* ARGSUSED */ 122126f9a767SRodney W. Grimes int 1222b40ce416SJulian Elischer osigstack(td, uap) 1223b40ce416SJulian Elischer struct thread *td; 1224df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 1225df8bae1dSRodney W. Grimes { 12265afe0c99SJohn Baldwin struct sigstack nss, oss; 1227fb99ab88SMatthew Dillon int error = 0; 1228fb99ab88SMatthew Dillon 1229d034d459SMarcel Moolenaar if (uap->nss != NULL) { 12305afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 12315afe0c99SJohn Baldwin if (error) 12325afe0c99SJohn Baldwin return (error); 1233df8bae1dSRodney W. Grimes } 1234a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 12355afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 12365afe0c99SJohn Baldwin if (uap->nss != NULL) { 1237a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1238a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1239a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1240a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 12415afe0c99SJohn Baldwin } 12425afe0c99SJohn Baldwin if (uap->oss != NULL) 12435afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 12445afe0c99SJohn Baldwin 1245fb99ab88SMatthew Dillon return (error); 1246df8bae1dSRodney W. Grimes } 12471930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1248df8bae1dSRodney W. Grimes 1249d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1250df8bae1dSRodney W. Grimes struct sigaltstack_args { 12512c42a146SMarcel Moolenaar stack_t *ss; 12522c42a146SMarcel Moolenaar stack_t *oss; 1253df8bae1dSRodney W. Grimes }; 1254d2d3e875SBruce Evans #endif 1255fb99ab88SMatthew Dillon /* 1256fb99ab88SMatthew Dillon * MPSAFE 1257fb99ab88SMatthew Dillon */ 1258df8bae1dSRodney W. Grimes /* ARGSUSED */ 125926f9a767SRodney W. Grimes int 1260b40ce416SJulian Elischer sigaltstack(td, uap) 1261b40ce416SJulian Elischer struct thread *td; 1262df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1263df8bae1dSRodney W. Grimes { 12648f19eb88SIan Dowse stack_t ss, oss; 12658f19eb88SIan Dowse int error; 12668f19eb88SIan Dowse 12678f19eb88SIan Dowse if (uap->ss != NULL) { 12688f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 12698f19eb88SIan Dowse if (error) 12708f19eb88SIan Dowse return (error); 12718f19eb88SIan Dowse } 12728f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 12738f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 12748f19eb88SIan Dowse if (error) 12758f19eb88SIan Dowse return (error); 12768f19eb88SIan Dowse if (uap->oss != NULL) 12778f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 12788f19eb88SIan Dowse return (error); 12798f19eb88SIan Dowse } 12808f19eb88SIan Dowse 12818f19eb88SIan Dowse int 12828f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 12838f19eb88SIan Dowse { 1284b40ce416SJulian Elischer struct proc *p = td->td_proc; 1285fb99ab88SMatthew Dillon int oonstack; 1286fb99ab88SMatthew Dillon 1287b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1288d034d459SMarcel Moolenaar 12898f19eb88SIan Dowse if (oss != NULL) { 1290a30ec4b9SDavid Xu *oss = td->td_sigstk; 1291a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1292d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1293df8bae1dSRodney W. Grimes } 1294d034d459SMarcel Moolenaar 12958f19eb88SIan Dowse if (ss != NULL) { 1296a30ec4b9SDavid Xu if (oonstack) 1297cf60731bSJohn Baldwin return (EPERM); 1298a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1299cf60731bSJohn Baldwin return (EINVAL); 13008f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 13018f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1302cf60731bSJohn Baldwin return (ENOMEM); 1303fb99ab88SMatthew Dillon } 1304a30ec4b9SDavid Xu td->td_sigstk = *ss; 1305a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1306628d2653SJohn Baldwin } else { 1307a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1308628d2653SJohn Baldwin } 1309d034d459SMarcel Moolenaar } 1310cf60731bSJohn Baldwin return (0); 1311df8bae1dSRodney W. Grimes } 1312df8bae1dSRodney W. Grimes 1313d93f860cSPoul-Henning Kamp /* 1314d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1315d93f860cSPoul-Henning Kamp * cp is calling process. 1316d93f860cSPoul-Henning Kamp */ 131737c84183SPoul-Henning Kamp static int 13181a88a252SMaxim Sobolev killpg1(td, sig, pgid, all) 13199c1ab3e0SJohn Baldwin register struct thread *td; 13201a88a252SMaxim Sobolev int sig, pgid, all; 1321d93f860cSPoul-Henning Kamp { 1322d93f860cSPoul-Henning Kamp register struct proc *p; 1323d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1324d93f860cSPoul-Henning Kamp int nfound = 0; 1325d93f860cSPoul-Henning Kamp 1326553629ebSJake Burkholder if (all) { 1327d93f860cSPoul-Henning Kamp /* 1328d93f860cSPoul-Henning Kamp * broadcast 1329d93f860cSPoul-Henning Kamp */ 13301005a129SJohn Baldwin sx_slock(&allproc_lock); 13312e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1332628d2653SJohn Baldwin PROC_LOCK(p); 13339c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 13349c1ab3e0SJohn Baldwin p == td->td_proc) { 1335628d2653SJohn Baldwin PROC_UNLOCK(p); 1336628d2653SJohn Baldwin continue; 1337628d2653SJohn Baldwin } 13381a88a252SMaxim Sobolev if (p_cansignal(td, p, sig) == 0) { 1339d93f860cSPoul-Henning Kamp nfound++; 134033a9ed9dSJohn Baldwin if (sig) 13412c42a146SMarcel Moolenaar psignal(p, sig); 1342628d2653SJohn Baldwin } 134333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1344d93f860cSPoul-Henning Kamp } 13451005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1346553629ebSJake Burkholder } else { 1347ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1348f591779bSSeigo Tanimura if (pgid == 0) { 1349d93f860cSPoul-Henning Kamp /* 1350d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1351d93f860cSPoul-Henning Kamp */ 13529c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1353f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1354f591779bSSeigo Tanimura } else { 1355d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1356f591779bSSeigo Tanimura if (pgrp == NULL) { 1357ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1358d93f860cSPoul-Henning Kamp return (ESRCH); 1359d93f860cSPoul-Henning Kamp } 1360f591779bSSeigo Tanimura } 1361ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 13622e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1363628d2653SJohn Baldwin PROC_LOCK(p); 1364628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1365628d2653SJohn Baldwin PROC_UNLOCK(p); 1366628d2653SJohn Baldwin continue; 1367628d2653SJohn Baldwin } 13681a88a252SMaxim Sobolev if (p_cansignal(td, p, sig) == 0) { 1369d93f860cSPoul-Henning Kamp nfound++; 137033a9ed9dSJohn Baldwin if (sig) 13712c42a146SMarcel Moolenaar psignal(p, sig); 1372628d2653SJohn Baldwin } 137333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1374d93f860cSPoul-Henning Kamp } 1375f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1376d93f860cSPoul-Henning Kamp } 1377d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1378d93f860cSPoul-Henning Kamp } 1379d93f860cSPoul-Henning Kamp 1380d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1381df8bae1dSRodney W. Grimes struct kill_args { 1382df8bae1dSRodney W. Grimes int pid; 1383df8bae1dSRodney W. Grimes int signum; 1384df8bae1dSRodney W. Grimes }; 1385d2d3e875SBruce Evans #endif 1386fb99ab88SMatthew Dillon /* 1387fb99ab88SMatthew Dillon * MPSAFE 1388fb99ab88SMatthew Dillon */ 1389df8bae1dSRodney W. Grimes /* ARGSUSED */ 139026f9a767SRodney W. Grimes int 1391b40ce416SJulian Elischer kill(td, uap) 1392b40ce416SJulian Elischer register struct thread *td; 1393df8bae1dSRodney W. Grimes register struct kill_args *uap; 1394df8bae1dSRodney W. Grimes { 1395df8bae1dSRodney W. Grimes register struct proc *p; 139690af4afaSJohn Baldwin int error; 1397df8bae1dSRodney W. Grimes 13986c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1399df8bae1dSRodney W. Grimes return (EINVAL); 1400fb99ab88SMatthew Dillon 1401df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1402df8bae1dSRodney W. Grimes /* kill single process */ 14030b011ea3SPawel Jakub Dawidek if ((p = pfind(uap->pid)) == NULL) { 140424b2151fSPawel Jakub Dawidek if ((p = zpfind(uap->pid)) == NULL) 140590af4afaSJohn Baldwin return (ESRCH); 14060b011ea3SPawel Jakub Dawidek } 14071a88a252SMaxim Sobolev error = p_cansignal(td, p, uap->signum); 140890af4afaSJohn Baldwin if (error == 0 && uap->signum) 1409df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1410628d2653SJohn Baldwin PROC_UNLOCK(p); 141190af4afaSJohn Baldwin return (error); 1412df8bae1dSRodney W. Grimes } 1413df8bae1dSRodney W. Grimes switch (uap->pid) { 1414df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 14151a88a252SMaxim Sobolev return (killpg1(td, uap->signum, 0, 1)); 1416df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 14171a88a252SMaxim Sobolev return (killpg1(td, uap->signum, 0, 0)); 1418df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 14191a88a252SMaxim Sobolev return (killpg1(td, uap->signum, -uap->pid, 0)); 1420df8bae1dSRodney W. Grimes } 142190af4afaSJohn Baldwin /* NOTREACHED */ 1422df8bae1dSRodney W. Grimes } 1423df8bae1dSRodney W. Grimes 14241930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1425d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1426df8bae1dSRodney W. Grimes struct okillpg_args { 1427df8bae1dSRodney W. Grimes int pgid; 1428df8bae1dSRodney W. Grimes int signum; 1429df8bae1dSRodney W. Grimes }; 1430d2d3e875SBruce Evans #endif 1431fb99ab88SMatthew Dillon /* 1432fb99ab88SMatthew Dillon * MPSAFE 1433fb99ab88SMatthew Dillon */ 1434df8bae1dSRodney W. Grimes /* ARGSUSED */ 143526f9a767SRodney W. Grimes int 1436b40ce416SJulian Elischer okillpg(td, uap) 1437b40ce416SJulian Elischer struct thread *td; 1438df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1439df8bae1dSRodney W. Grimes { 1440df8bae1dSRodney W. Grimes 14416c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1442df8bae1dSRodney W. Grimes return (EINVAL); 14431a88a252SMaxim Sobolev return (killpg1(td, uap->signum, uap->pgid, 0)); 1444df8bae1dSRodney W. Grimes } 14451930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1446df8bae1dSRodney W. Grimes 1447df8bae1dSRodney W. Grimes /* 1448df8bae1dSRodney W. Grimes * Send a signal to a process group. 1449df8bae1dSRodney W. Grimes */ 1450df8bae1dSRodney W. Grimes void 14512c42a146SMarcel Moolenaar gsignal(pgid, sig) 14522c42a146SMarcel Moolenaar int pgid, sig; 1453df8bae1dSRodney W. Grimes { 1454df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1455df8bae1dSRodney W. Grimes 1456f591779bSSeigo Tanimura if (pgid != 0) { 1457ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1458f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1459ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1460f591779bSSeigo Tanimura if (pgrp != NULL) { 14612c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1462f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1463f591779bSSeigo Tanimura } 1464f591779bSSeigo Tanimura } 1465df8bae1dSRodney W. Grimes } 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes /* 1468df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1469df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1470df8bae1dSRodney W. Grimes */ 1471df8bae1dSRodney W. Grimes void 14722c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1473df8bae1dSRodney W. Grimes struct pgrp *pgrp; 14742c42a146SMarcel Moolenaar int sig, checkctty; 1475df8bae1dSRodney W. Grimes { 1476df8bae1dSRodney W. Grimes register struct proc *p; 1477df8bae1dSRodney W. Grimes 1478628d2653SJohn Baldwin if (pgrp) { 1479f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1480628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1481628d2653SJohn Baldwin PROC_LOCK(p); 1482df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 14832c42a146SMarcel Moolenaar psignal(p, sig); 1484628d2653SJohn Baldwin PROC_UNLOCK(p); 1485628d2653SJohn Baldwin } 1486628d2653SJohn Baldwin } 1487df8bae1dSRodney W. Grimes } 1488df8bae1dSRodney W. Grimes 1489df8bae1dSRodney W. Grimes /* 14901bf4700bSJeff Roberson * Send a signal caused by a trap to the current thread. 1491df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1492df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1493356861dbSMatthew Dillon * 1494356861dbSMatthew Dillon * MPSAFE 1495df8bae1dSRodney W. Grimes */ 1496df8bae1dSRodney W. Grimes void 14971bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code) 1498df8bae1dSRodney W. Grimes { 14991bf4700bSJeff Roberson struct sigacts *ps; 15001bf4700bSJeff Roberson struct proc *p; 15019dde3bc9SDavid Xu siginfo_t siginfo; 15029dde3bc9SDavid Xu int error; 15031bf4700bSJeff Roberson 15041bf4700bSJeff Roberson p = td->td_proc; 1505aa0aa7a1STim J. Robbins if (td->td_pflags & TDP_SA) { 15067eeaaf9bSDavid Xu if (td->td_mailbox == NULL) 15075995adc2SJulian Elischer thread_user_enter(td); 1508628d2653SJohn Baldwin PROC_LOCK(p); 15099dde3bc9SDavid Xu SIGDELSET(td->td_sigmask, sig); 15109dde3bc9SDavid Xu mtx_lock_spin(&sched_lock); 15119dde3bc9SDavid Xu /* 15129dde3bc9SDavid Xu * Force scheduling an upcall, so UTS has chance to 15139dde3bc9SDavid Xu * process the signal before thread runs again in 15149dde3bc9SDavid Xu * userland. 15159dde3bc9SDavid Xu */ 15169dde3bc9SDavid Xu if (td->td_upcall) 15179dde3bc9SDavid Xu td->td_upcall->ku_flags |= KUF_DOUPCALL; 15189dde3bc9SDavid Xu mtx_unlock_spin(&sched_lock); 1519432b45deSDavid Xu } else { 15209dde3bc9SDavid Xu PROC_LOCK(p); 15219dde3bc9SDavid Xu } 1522ef3dab76STim J. Robbins ps = p->p_sigacts; 152390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 152490af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 15254093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1526df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1527df8bae1dSRodney W. Grimes #ifdef KTRACE 1528374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1529374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 15304093529dSJeff Roberson &td->td_sigmask, code); 1531df8bae1dSRodney W. Grimes #endif 1532aa0aa7a1STim J. Robbins if (!(td->td_pflags & TDP_SA)) 15339dde3bc9SDavid Xu (*p->p_sysent->sv_sendsig)( 15349dde3bc9SDavid Xu ps->ps_sigact[_SIG_IDX(sig)], sig, 15354093529dSJeff Roberson &td->td_sigmask, code); 1536cbf4e354SDavid Xu else if (td->td_mailbox == NULL) { 1537cbf4e354SDavid Xu mtx_unlock(&ps->ps_mtx); 1538cbf4e354SDavid Xu /* UTS caused a sync signal */ 1539cbf4e354SDavid Xu p->p_code = code; /* XXX for core dump/debugger */ 1540cbf4e354SDavid Xu p->p_sig = sig; /* XXX to verify code */ 1541cbf4e354SDavid Xu sigexit(td, sig); 1542cbf4e354SDavid Xu } else { 15434b7d5d84SDavid Xu cpu_thread_siginfo(sig, code, &siginfo); 15449dde3bc9SDavid Xu mtx_unlock(&ps->ps_mtx); 1545cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 15469dde3bc9SDavid Xu PROC_UNLOCK(p); 15479dde3bc9SDavid Xu error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 15489dde3bc9SDavid Xu sizeof(siginfo)); 15499dde3bc9SDavid Xu PROC_LOCK(p); 1550432b45deSDavid Xu /* UTS memory corrupted */ 15519dde3bc9SDavid Xu if (error) 1552cbf4e354SDavid Xu sigexit(td, SIGSEGV); 15539dde3bc9SDavid Xu mtx_lock(&ps->ps_mtx); 15549dde3bc9SDavid Xu } 15554093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15562c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15574093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 15582c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1559289ccde0SPeter Wemm /* 15608f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1561289ccde0SPeter Wemm */ 156290af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 15632c42a146SMarcel Moolenaar if (sig != SIGCONT && 15642c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 156590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 15662c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1567dedc04feSPeter Wemm } 156890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15696f841fb7SMarcel Moolenaar } else { 157090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15716626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 15722c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 1573c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_TD); 1574df8bae1dSRodney W. Grimes } 1575628d2653SJohn Baldwin PROC_UNLOCK(p); 1576df8bae1dSRodney W. Grimes } 1577df8bae1dSRodney W. Grimes 15784093529dSJeff Roberson static struct thread * 15794093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 15804093529dSJeff Roberson { 15813074d1b4SDavid Xu struct thread *td, *signal_td; 15824093529dSJeff Roberson 15834093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 15844093529dSJeff Roberson 15854093529dSJeff Roberson /* 15863074d1b4SDavid Xu * First find a thread in sigwait state and signal belongs to 15873074d1b4SDavid Xu * its wait set. POSIX's arguments is that speed of delivering signal 15883074d1b4SDavid Xu * to sigwait thread is faster than delivering signal to user stack. 15893074d1b4SDavid Xu * If we can not find sigwait thread, then find the first thread in 15903074d1b4SDavid Xu * the proc that doesn't have this signal masked, an exception is 15913074d1b4SDavid Xu * if current thread is sending signal to its process, and it does not 15923074d1b4SDavid Xu * mask the signal, it should get the signal, this is another fast 15933074d1b4SDavid Xu * way to deliver signal. 15944093529dSJeff Roberson */ 15953074d1b4SDavid Xu signal_td = NULL; 1596a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 15973074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 15983074d1b4SDavid Xu if (td->td_waitset != NULL && 1599a9a48d68SDavid Xu SIGISMEMBER(*(td->td_waitset), sig)) { 1600a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 16014093529dSJeff Roberson return (td); 1602a9a48d68SDavid Xu } 16033074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 16043074d1b4SDavid Xu if (td == curthread) 16053074d1b4SDavid Xu signal_td = curthread; 16063074d1b4SDavid Xu else if (signal_td == NULL) 16073074d1b4SDavid Xu signal_td = td; 16083074d1b4SDavid Xu } 16093074d1b4SDavid Xu } 16103074d1b4SDavid Xu if (signal_td == NULL) 16113074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 1612a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 16133074d1b4SDavid Xu return (signal_td); 16144093529dSJeff Roberson } 16154093529dSJeff Roberson 1616df8bae1dSRodney W. Grimes /* 1617df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1618df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1619df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1620df8bae1dSRodney W. Grimes * 1621df8bae1dSRodney W. Grimes * Exceptions: 1622df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1623df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1624df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1625df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1626df8bae1dSRodney W. Grimes * 1627df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 162890af4afaSJohn Baldwin * 162990af4afaSJohn Baldwin * MPSAFE 1630df8bae1dSRodney W. Grimes */ 1631df8bae1dSRodney W. Grimes void 16324093529dSJeff Roberson psignal(struct proc *p, int sig) 1633df8bae1dSRodney W. Grimes { 1634b40ce416SJulian Elischer struct thread *td; 16354093529dSJeff Roberson int prop; 16364093529dSJeff Roberson 163741b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 163841b3077aSJacques Vidrine panic("psignal(): invalid signal"); 163941b3077aSJacques Vidrine 16404093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 164124b2151fSPawel Jakub Dawidek /* 164224b2151fSPawel Jakub Dawidek * IEEE Std 1003.1-2001: return success when killing a zombie. 164324b2151fSPawel Jakub Dawidek */ 164424b2151fSPawel Jakub Dawidek if (p->p_state == PRS_ZOMBIE) 164524b2151fSPawel Jakub Dawidek return; 16464093529dSJeff Roberson prop = sigprop(sig); 16474093529dSJeff Roberson 16484093529dSJeff Roberson /* 16494093529dSJeff Roberson * Find a thread to deliver the signal to. 16504093529dSJeff Roberson */ 16514093529dSJeff Roberson td = sigtd(p, sig, prop); 16524093529dSJeff Roberson 1653c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_P); 16544093529dSJeff Roberson } 16554093529dSJeff Roberson 165690af4afaSJohn Baldwin /* 165790af4afaSJohn Baldwin * MPSAFE 165890af4afaSJohn Baldwin */ 16594093529dSJeff Roberson void 1660c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target) 16614093529dSJeff Roberson { 16629dde3bc9SDavid Xu sigset_t saved; 16639dde3bc9SDavid Xu struct proc *p = td->td_proc; 16649dde3bc9SDavid Xu 16659dde3bc9SDavid Xu if (p->p_flag & P_SA) 16669dde3bc9SDavid Xu saved = p->p_siglist; 1667c197abc4SMike Makonnen do_tdsignal(td, sig, target); 16689dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 16696dbc0850SJohn Baldwin if (!SIGSETEQ(saved, p->p_siglist)) { 16709dde3bc9SDavid Xu /* pending set changed */ 16719dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 16729dde3bc9SDavid Xu wakeup(&p->p_siglist); 16739dde3bc9SDavid Xu } 16749dde3bc9SDavid Xu } 16759dde3bc9SDavid Xu } 16769dde3bc9SDavid Xu 16779dde3bc9SDavid Xu static void 1678c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target) 16799dde3bc9SDavid Xu { 16804093529dSJeff Roberson struct proc *p; 16814093529dSJeff Roberson register sig_t action; 16824093529dSJeff Roberson sigset_t *siglist; 16834093529dSJeff Roberson struct thread *td0; 1684e602ba25SJulian Elischer register int prop; 168590af4afaSJohn Baldwin struct sigacts *ps; 1686df8bae1dSRodney W. Grimes 168741b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 168841b3077aSJacques Vidrine panic("do_tdsignal(): invalid signal"); 16894093529dSJeff Roberson 16904093529dSJeff Roberson p = td->td_proc; 169190af4afaSJohn Baldwin ps = p->p_sigacts; 16922c42a146SMarcel Moolenaar 1693628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1694ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 1695cb679c38SJonathan Lemon 16962c42a146SMarcel Moolenaar prop = sigprop(sig); 16974093529dSJeff Roberson 16984093529dSJeff Roberson /* 169914b5ae1aSMike Makonnen * If the signal is blocked and not destined for this thread, then 170014b5ae1aSMike Makonnen * assign it to the process so that we can find it later in the first 170114b5ae1aSMike Makonnen * thread that unblocks it. Otherwise, assign it to this thread now. 17024093529dSJeff Roberson */ 17033074d1b4SDavid Xu if (target == SIGTARGET_TD) { 17043074d1b4SDavid Xu siglist = &td->td_siglist; 17053074d1b4SDavid Xu } else { 17063074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) 17073074d1b4SDavid Xu siglist = &td->td_siglist; 17083074d1b4SDavid Xu else if (td->td_waitset != NULL && 17093074d1b4SDavid Xu SIGISMEMBER(*(td->td_waitset), sig)) 17103074d1b4SDavid Xu siglist = &td->td_siglist; 17113074d1b4SDavid Xu else 17123074d1b4SDavid Xu siglist = &p->p_siglist; 17133074d1b4SDavid Xu } 17144093529dSJeff Roberson 1715df8bae1dSRodney W. Grimes /* 17162a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 17172a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 17182a024a2bSSean Eric Fagan * a chance, as well. 1719df8bae1dSRodney W. Grimes */ 1720b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1721df8bae1dSRodney W. Grimes action = SIG_DFL; 1722b40ce416SJulian Elischer } else { 1723df8bae1dSRodney W. Grimes /* 1724df8bae1dSRodney W. Grimes * If the signal is being ignored, 1725df8bae1dSRodney W. Grimes * then we forget about it immediately. 172690af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 1727df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1728df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1729df8bae1dSRodney W. Grimes */ 173090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 173190af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) || 173290af4afaSJohn Baldwin (p->p_flag & P_WEXIT)) { 173390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1734df8bae1dSRodney W. Grimes return; 173590af4afaSJohn Baldwin } 17363074d1b4SDavid Xu if (((td->td_waitset == NULL) && 17373074d1b4SDavid Xu SIGISMEMBER(td->td_sigmask, sig)) || 17383074d1b4SDavid Xu ((td->td_waitset != NULL) && 17393074d1b4SDavid Xu SIGISMEMBER(td->td_sigmask, sig) && 17403074d1b4SDavid Xu !SIGISMEMBER(*(td->td_waitset), sig))) 1741df8bae1dSRodney W. Grimes action = SIG_HOLD; 174290af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1743df8bae1dSRodney W. Grimes action = SIG_CATCH; 1744df8bae1dSRodney W. Grimes else 1745df8bae1dSRodney W. Grimes action = SIG_DFL; 174690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1747df8bae1dSRodney W. Grimes } 1748df8bae1dSRodney W. Grimes 17494093529dSJeff Roberson if (prop & SA_CONT) { 17501d9c5696SJuli Mallett SIG_STOPSIGMASK(p->p_siglist); 17514093529dSJeff Roberson /* 17524093529dSJeff Roberson * XXX Should investigate leaving STOP and CONT sigs only in 17534093529dSJeff Roberson * the proc's siglist. 17544093529dSJeff Roberson */ 1755a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 17564093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17574093529dSJeff Roberson SIG_STOPSIGMASK(td0->td_siglist); 1758a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 17594093529dSJeff Roberson } 1760df8bae1dSRodney W. Grimes 1761df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1762df8bae1dSRodney W. Grimes /* 1763df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1764df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1765df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1766df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1767df8bae1dSRodney W. Grimes */ 1768e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1769e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1770e602ba25SJulian Elischer (action == SIG_DFL)) 1771df8bae1dSRodney W. Grimes return; 17721d9c5696SJuli Mallett SIG_CONTSIGMASK(p->p_siglist); 1773a9a48d68SDavid Xu mtx_lock_spin(&sched_lock); 17744093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17754093529dSJeff Roberson SIG_CONTSIGMASK(td0->td_siglist); 1776a9a48d68SDavid Xu mtx_unlock_spin(&sched_lock); 17776933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1778df8bae1dSRodney W. Grimes } 17793074d1b4SDavid Xu 17804093529dSJeff Roberson SIGADDSET(*siglist, sig); 17814093529dSJeff Roberson signotify(td); /* uses schedlock */ 17823074d1b4SDavid Xu if (siglist == &td->td_siglist && (td->td_waitset != NULL) && 17833074d1b4SDavid Xu action != SIG_HOLD) { 17843074d1b4SDavid Xu td->td_waitset = NULL; 17853074d1b4SDavid Xu } 17863074d1b4SDavid Xu 17875312b1c7SDavid Xu /* 17885312b1c7SDavid Xu * Defer further processing for signals which are held, 17895312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 17905312b1c7SDavid Xu */ 17915312b1c7SDavid Xu if (action == SIG_HOLD && 17925312b1c7SDavid Xu !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 17935312b1c7SDavid Xu return; 1794df8bae1dSRodney W. Grimes /* 179590d75f78SAlfred Perlstein * SIGKILL: Remove procfs STOPEVENTs. 179690d75f78SAlfred Perlstein */ 179790d75f78SAlfred Perlstein if (sig == SIGKILL) { 179890d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCBIC */ 179990d75f78SAlfred Perlstein p->p_stops = 0; 180090d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCCONT */ 180190d75f78SAlfred Perlstein p->p_step = 0; 180290d75f78SAlfred Perlstein wakeup(&p->p_step); 180390d75f78SAlfred Perlstein } 180490d75f78SAlfred Perlstein /* 1805e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1806e602ba25SJulian Elischer * component. Most processing occurs when the process next 1807e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1808e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1809e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1810e602ba25SJulian Elischer * We try do the per-process part here. 1811df8bae1dSRodney W. Grimes */ 1812e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1813e602ba25SJulian Elischer /* 1814e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1815e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1816e602ba25SJulian Elischer */ 1817e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1818e602ba25SJulian Elischer /* 1819e602ba25SJulian Elischer * The traced process is already stopped, 1820e602ba25SJulian Elischer * so no further action is necessary. 1821e602ba25SJulian Elischer * No signal can restart us. 1822e602ba25SJulian Elischer */ 1823e602ba25SJulian Elischer goto out; 18241c32c37cSJohn Baldwin } 1825b40ce416SJulian Elischer 1826e602ba25SJulian Elischer if (sig == SIGKILL) { 1827df8bae1dSRodney W. Grimes /* 1828e602ba25SJulian Elischer * SIGKILL sets process running. 1829e602ba25SJulian Elischer * It will die elsewhere. 1830e602ba25SJulian Elischer * All threads must be restarted. 1831df8bae1dSRodney W. Grimes */ 1832482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 1833e602ba25SJulian Elischer goto runfast; 1834e602ba25SJulian Elischer } 1835e602ba25SJulian Elischer 1836e602ba25SJulian Elischer if (prop & SA_CONT) { 1837e602ba25SJulian Elischer /* 1838e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 18394093529dSJeff Roberson * process but don't leave the signal in siglist as 18401d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 1841e602ba25SJulian Elischer * continue the process and leave the signal in 18424093529dSJeff Roberson * siglist. If the process catches SIGCONT, let it 1843e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1844e602ba25SJulian Elischer * an event, it goes back to run state. 1845e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1846e602ba25SJulian Elischer */ 18471279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 18486933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1849e602ba25SJulian Elischer if (action == SIG_DFL) { 18504093529dSJeff Roberson SIGDELSET(*siglist, sig); 1851e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1852e602ba25SJulian Elischer /* 1853e602ba25SJulian Elischer * The process wants to catch it so it needs 1854e602ba25SJulian Elischer * to run at least one thread, but which one? 1855e602ba25SJulian Elischer * It would seem that the answer would be to 1856e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1857e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1858e602ba25SJulian Elischer * process, we need to make sure that the 1859e602ba25SJulian Elischer * single thread is runnable asap. 1860e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1861e602ba25SJulian Elischer */ 1862e602ba25SJulian Elischer goto runfast; 1863e602ba25SJulian Elischer } 1864e602ba25SJulian Elischer /* 1865e602ba25SJulian Elischer * The signal is not ignored or caught. 1866e602ba25SJulian Elischer */ 1867e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 186804774f23SJulian Elischer thread_unsuspend(p); 1869e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1870e602ba25SJulian Elischer goto out; 1871e602ba25SJulian Elischer } 1872e602ba25SJulian Elischer 1873e602ba25SJulian Elischer if (prop & SA_STOP) { 1874e602ba25SJulian Elischer /* 1875e602ba25SJulian Elischer * Already stopped, don't need to stop again 1876e602ba25SJulian Elischer * (If we did the shell could get confused). 187704774f23SJulian Elischer * Just make sure the signal STOP bit set. 1878e602ba25SJulian Elischer */ 18791279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 18804093529dSJeff Roberson SIGDELSET(*siglist, sig); 1881e602ba25SJulian Elischer goto out; 1882e602ba25SJulian Elischer } 1883e602ba25SJulian Elischer 1884e602ba25SJulian Elischer /* 1885e602ba25SJulian Elischer * All other kinds of signals: 1886e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1887e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1888e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 188904774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1890e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1891e602ba25SJulian Elischer */ 1892e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 189344f3b092SJohn Baldwin if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 189444f3b092SJohn Baldwin sleepq_abort(td); 1895e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1896df8bae1dSRodney W. Grimes goto out; 1897df8bae1dSRodney W. Grimes /* 18989a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 18999a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 1900df8bae1dSRodney W. Grimes */ 1901e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 190235c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 190335c32a76SDavid Xu !(prop & SA_STOP)) { 1904721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 19054093529dSJeff Roberson tdsigwakeup(td, sig, action); 1906721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1907df8bae1dSRodney W. Grimes goto out; 190804774f23SJulian Elischer } 190935c32a76SDavid Xu if (prop & SA_STOP) { 191035c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 191135c32a76SDavid Xu goto out; 1912e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 1913e574e444SDavid Xu p->p_xstat = sig; 1914cbf4e354SDavid Xu p->p_xthread = td; 191535c32a76SDavid Xu mtx_lock_spin(&sched_lock); 19164093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) { 19174093529dSJeff Roberson if (TD_IS_SLEEPING(td0) && 1918062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 1919062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 19204093529dSJeff Roberson thread_suspend_one(td0); 1921062cf543SDavid Xu } else if (td != td0) { 1922062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 1923062cf543SDavid Xu } 192435c32a76SDavid Xu } 1925e574e444SDavid Xu thread_stopped(p); 19264093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 1927e574e444SDavid Xu SIGDELSET(p->p_siglist, p->p_xstat); 19284093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 19294093529dSJeff Roberson SIGDELSET(td0->td_siglist, p->p_xstat); 19304093529dSJeff Roberson } 193135c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 193235c32a76SDavid Xu goto out; 193335c32a76SDavid Xu } 1934721e5910SJulian Elischer else 1935df8bae1dSRodney W. Grimes goto runfast; 1936df8bae1dSRodney W. Grimes /* NOTREACHED */ 1937e602ba25SJulian Elischer } else { 1938e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 19394093529dSJeff Roberson SIGDELSET(*siglist, sig); 1940e602ba25SJulian Elischer goto out; 1941e602ba25SJulian Elischer } 1942e602ba25SJulian Elischer 1943b40ce416SJulian Elischer /* 1944e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1945e602ba25SJulian Elischer * running threads. 1946b40ce416SJulian Elischer */ 1947e602ba25SJulian Elischer 1948e602ba25SJulian Elischer runfast: 1949aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 19504093529dSJeff Roberson tdsigwakeup(td, sig, action); 1951e602ba25SJulian Elischer thread_unsuspend(p); 1952e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1953e602ba25SJulian Elischer out: 1954e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1955e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1956e602ba25SJulian Elischer } 1957e602ba25SJulian Elischer 1958e602ba25SJulian Elischer /* 1959e602ba25SJulian Elischer * The force of a signal has been directed against a single 1960e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1961e602ba25SJulian Elischer * out of any sleep it may be in etc. 1962e602ba25SJulian Elischer */ 1963e602ba25SJulian Elischer static void 19644093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action) 1965e602ba25SJulian Elischer { 1966e602ba25SJulian Elischer struct proc *p = td->td_proc; 1967e602ba25SJulian Elischer register int prop; 1968e602ba25SJulian Elischer 19698b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1970aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1971e602ba25SJulian Elischer prop = sigprop(sig); 1972a4c2da15SBruce Evans 1973e602ba25SJulian Elischer /* 1974aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1975e602ba25SJulian Elischer * killed in this lifetime. 1976e602ba25SJulian Elischer */ 1977a4c2da15SBruce Evans if (action == SIG_DFL && (prop & SA_KILL)) { 19783ef6ac33SJeff Roberson if (p->p_nice > 0) 19793ef6ac33SJeff Roberson sched_nice(td->td_proc, 0); 1980a4c2da15SBruce Evans if (td->td_priority > PUSER) 1981b3a4fb14SDavid Xu sched_prio(td, PUSER); 1982b40ce416SJulian Elischer } 1983a4c2da15SBruce Evans 198480c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 1985e602ba25SJulian Elischer /* 1986e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1987e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1988e602ba25SJulian Elischer * be noticed when the process returns through 1989e602ba25SJulian Elischer * trap() or syscall(). 1990e602ba25SJulian Elischer */ 199144f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 1992aa0fa334SJulian Elischer return; 1993e602ba25SJulian Elischer /* 1994e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1995e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1996e602ba25SJulian Elischer * for its parent. 1997e602ba25SJulian Elischer */ 1998e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1999e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 2000aa0fa334SJulian Elischer } else { 2001df8bae1dSRodney W. Grimes /* 2002e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2003e602ba25SJulian Elischer * asleep, we are finished; the process should not 2004e602ba25SJulian Elischer * be awakened. 2005df8bae1dSRodney W. Grimes */ 2006e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 20071d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 20084093529dSJeff Roberson /* 20094093529dSJeff Roberson * It may be on either list in this state. 20104093529dSJeff Roberson * Remove from both for now. 20114093529dSJeff Roberson */ 20124093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 2013aa0fa334SJulian Elischer return; 2014df8bae1dSRodney W. Grimes } 2015df8bae1dSRodney W. Grimes 2016aa0fa334SJulian Elischer /* 2017a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2018aa0fa334SJulian Elischer */ 201944f3b092SJohn Baldwin if (td->td_priority > PUSER) 2020b3a4fb14SDavid Xu sched_prio(td, PUSER); 2021aa0fa334SJulian Elischer } 202244f3b092SJohn Baldwin sleepq_abort(td); 2023a4c2da15SBruce Evans } else { 2024df8bae1dSRodney W. Grimes /* 2025a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2026df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2027df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2028df8bae1dSRodney W. Grimes */ 2029a4c2da15SBruce Evans #ifdef SMP 203044f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2031e602ba25SJulian Elischer forward_signal(td); 20323163861cSTor Egge #endif 20336caa8a15SJohn Baldwin } 2034a4c2da15SBruce Evans } 2035df8bae1dSRodney W. Grimes 2036cbf4e354SDavid Xu int 20374cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig) 20384cc9f52fSRobert Drehmel { 20394cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 2040cbf4e354SDavid Xu struct thread *td0; 20414cc9f52fSRobert Drehmel 204230a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 20434cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 20444cc9f52fSRobert Drehmel &p->p_mtx.mtx_object, "Stopping for traced signal"); 20454cc9f52fSRobert Drehmel 20464cc9f52fSRobert Drehmel mtx_lock_spin(&sched_lock); 2047cbf4e354SDavid Xu td->td_flags |= TDF_XSIG; 2048cbf4e354SDavid Xu mtx_unlock_spin(&sched_lock); 2049cbf4e354SDavid Xu td->td_xsig = sig; 2050cbf4e354SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2051cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) { 2052cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2053cbf4e354SDavid Xu td->td_flags &= ~TDF_XSIG; 2054cbf4e354SDavid Xu mtx_unlock_spin(&sched_lock); 2055cbf4e354SDavid Xu return (sig); 2056cbf4e354SDavid Xu } 2057cbf4e354SDavid Xu /* 2058cbf4e354SDavid Xu * Just make wait() to work, the last stopped thread 2059cbf4e354SDavid Xu * will win. 2060cbf4e354SDavid Xu */ 2061cbf4e354SDavid Xu p->p_xstat = sig; 2062cbf4e354SDavid Xu p->p_xthread = td; 2063cbf4e354SDavid Xu p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2064cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2065cbf4e354SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2066cbf4e354SDavid Xu if (TD_IS_SLEEPING(td0) && 2067cbf4e354SDavid Xu (td0->td_flags & TDF_SINTR) && 2068cbf4e354SDavid Xu !TD_IS_SUSPENDED(td0)) { 2069cbf4e354SDavid Xu thread_suspend_one(td0); 2070cbf4e354SDavid Xu } else if (td != td0) { 2071cbf4e354SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2072cbf4e354SDavid Xu } 2073cbf4e354SDavid Xu } 2074cbf4e354SDavid Xu stopme: 2075cbf4e354SDavid Xu thread_stopped(p); 20764cc9f52fSRobert Drehmel thread_suspend_one(td); 20774cc9f52fSRobert Drehmel PROC_UNLOCK(p); 20784cc9f52fSRobert Drehmel DROP_GIANT(); 2079cbf4e354SDavid Xu mi_switch(SW_VOL, NULL); 20804cc9f52fSRobert Drehmel mtx_unlock_spin(&sched_lock); 20814cc9f52fSRobert Drehmel PICKUP_GIANT(); 2082cbf4e354SDavid Xu PROC_LOCK(p); 2083cbf4e354SDavid Xu if (!(p->p_flag & P_TRACED)) 2084cbf4e354SDavid Xu break; 2085cbf4e354SDavid Xu if (td->td_flags & TDF_DBSUSPEND) { 2086cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2087cbf4e354SDavid Xu break; 2088cbf4e354SDavid Xu mtx_lock_spin(&sched_lock); 2089cbf4e354SDavid Xu goto stopme; 2090cbf4e354SDavid Xu } 2091cbf4e354SDavid Xu } 2092cbf4e354SDavid Xu return (td->td_xsig); 20934cc9f52fSRobert Drehmel } 20944cc9f52fSRobert Drehmel 2095df8bae1dSRodney W. Grimes /* 2096df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2097df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2098df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2099df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2100df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2101628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2102df8bae1dSRodney W. Grimes * sequence is 2103df8bae1dSRodney W. Grimes * 2104e602ba25SJulian Elischer * while (sig = cursig(curthread)) 21052c42a146SMarcel Moolenaar * postsig(sig); 2106df8bae1dSRodney W. Grimes */ 21076711f10fSJohn Baldwin static int 2108e602ba25SJulian Elischer issignal(td) 2109e602ba25SJulian Elischer struct thread *td; 2110df8bae1dSRodney W. Grimes { 2111e602ba25SJulian Elischer struct proc *p; 211290af4afaSJohn Baldwin struct sigacts *ps; 21134093529dSJeff Roberson sigset_t sigpending; 2114cbf4e354SDavid Xu int sig, prop, newsig; 2115062cf543SDavid Xu struct thread *td0; 2116df8bae1dSRodney W. Grimes 2117e602ba25SJulian Elischer p = td->td_proc; 211890af4afaSJohn Baldwin ps = p->p_sigacts; 211990af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2120628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2121df8bae1dSRodney W. Grimes for (;;) { 21222a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 21232a024a2bSSean Eric Fagan 21244093529dSJeff Roberson sigpending = td->td_siglist; 21254093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 21264093529dSJeff Roberson 2127df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 21284093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 21294093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2130df8bae1dSRodney W. Grimes return (0); 21314093529dSJeff Roberson sig = sig_ffs(&sigpending); 21322a024a2bSSean Eric Fagan 2133047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2134047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2135047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2136047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2137047aa39bSRobert Watson } 21382a024a2bSSean Eric Fagan 2139df8bae1dSRodney W. Grimes /* 2140df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2141df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2142df8bae1dSRodney W. Grimes */ 214390af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 21444093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 2145cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2146cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 2147df8bae1dSRodney W. Grimes continue; 2148df8bae1dSRodney W. Grimes } 2149df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2150df8bae1dSRodney W. Grimes /* 2151d8f4f6a4SJonathan Mini * If traced, always stop. 2152df8bae1dSRodney W. Grimes */ 215390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2154cbf4e354SDavid Xu newsig = ptracestop(td, sig); 215590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2156df8bae1dSRodney W. Grimes 2157df8bae1dSRodney W. Grimes /* 2158df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 2159df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 2160df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 2161df8bae1dSRodney W. Grimes */ 21624093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* clear old signal */ 2163cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2164cbf4e354SDavid Xu SIGADDSET(td->td_sigmask, sig); 2165cbf4e354SDavid Xu if (newsig == 0) 2166df8bae1dSRodney W. Grimes continue; 2167cbf4e354SDavid Xu sig = newsig; 2168df8bae1dSRodney W. Grimes /* 21698d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 21708d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 21718d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 21728d542cb5SDavid E. O'Brien */ 21738d542cb5SDavid E. O'Brien if ((p->p_flag & P_TRACED) == 0) 21748d542cb5SDavid E. O'Brien continue; 21758d542cb5SDavid E. O'Brien 21768d542cb5SDavid E. O'Brien /* 21774093529dSJeff Roberson * Put the new signal into td_siglist. If the 21781d9c5696SJuli Mallett * signal is being masked, look for other signals. 2179df8bae1dSRodney W. Grimes */ 21804093529dSJeff Roberson SIGADDSET(td->td_siglist, sig); 2181cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 2182cbf4e354SDavid Xu SIGDELSET(td->td_sigmask, sig); 21834093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 2184df8bae1dSRodney W. Grimes continue; 21854093529dSJeff Roberson signotify(td); 2186df8bae1dSRodney W. Grimes } 2187df8bae1dSRodney W. Grimes 21888d542cb5SDavid E. O'Brien prop = sigprop(sig); 21898d542cb5SDavid E. O'Brien 2190df8bae1dSRodney W. Grimes /* 2191df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2192df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2193df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2194df8bae1dSRodney W. Grimes */ 2195d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2196df8bae1dSRodney W. Grimes 2197d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2198df8bae1dSRodney W. Grimes /* 2199df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2200df8bae1dSRodney W. Grimes */ 2201df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2202df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2203df8bae1dSRodney W. Grimes /* 2204df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2205df8bae1dSRodney W. Grimes * in init? XXX 2206df8bae1dSRodney W. Grimes */ 2207d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 22082c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2209df8bae1dSRodney W. Grimes #endif 2210df8bae1dSRodney W. Grimes break; /* == ignore */ 2211df8bae1dSRodney W. Grimes } 2212df8bae1dSRodney W. Grimes /* 2213df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 2214df8bae1dSRodney W. Grimes * with default action, stop here, 2215df8bae1dSRodney W. Grimes * then clear the signal. However, 2216df8bae1dSRodney W. Grimes * if process is member of an orphaned 2217df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 2218df8bae1dSRodney W. Grimes */ 2219df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 2220df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 2221df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2222df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 2223df8bae1dSRodney W. Grimes break; /* == ignore */ 222490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 222590af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 222690af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2227e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 22282c42a146SMarcel Moolenaar p->p_xstat = sig; 2229cbf4e354SDavid Xu p->p_xthread = td; 2230721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 2231062cf543SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2232062cf543SDavid Xu if (TD_IS_SLEEPING(td0) && 2233062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 2234062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 2235062cf543SDavid Xu thread_suspend_one(td0); 2236062cf543SDavid Xu } else if (td != td0) { 2237062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2238062cf543SDavid Xu } 2239062cf543SDavid Xu } 2240e574e444SDavid Xu thread_stopped(p); 224171fad9fdSJulian Elischer thread_suspend_one(td); 2242721e5910SJulian Elischer PROC_UNLOCK(p); 2243721e5910SJulian Elischer DROP_GIANT(); 2244bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 2245721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 2246721e5910SJulian Elischer PICKUP_GIANT(); 2247721e5910SJulian Elischer PROC_LOCK(p); 224890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2249df8bae1dSRodney W. Grimes break; 225021b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 2251df8bae1dSRodney W. Grimes /* 2252df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2253df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2254df8bae1dSRodney W. Grimes */ 2255df8bae1dSRodney W. Grimes break; /* == ignore */ 2256df8bae1dSRodney W. Grimes } else 22572c42a146SMarcel Moolenaar return (sig); 2258df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2259df8bae1dSRodney W. Grimes 2260d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2261df8bae1dSRodney W. Grimes /* 2262df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2263df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2264df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2265df8bae1dSRodney W. Grimes */ 2266df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 2267df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2268df8bae1dSRodney W. Grimes printf("issignal\n"); 2269df8bae1dSRodney W. Grimes break; /* == ignore */ 2270df8bae1dSRodney W. Grimes 2271df8bae1dSRodney W. Grimes default: 2272df8bae1dSRodney W. Grimes /* 2273df8bae1dSRodney W. Grimes * This signal has an action, let 2274df8bae1dSRodney W. Grimes * postsig() process it. 2275df8bae1dSRodney W. Grimes */ 22762c42a146SMarcel Moolenaar return (sig); 2277df8bae1dSRodney W. Grimes } 22784093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2279df8bae1dSRodney W. Grimes } 2280df8bae1dSRodney W. Grimes /* NOTREACHED */ 2281df8bae1dSRodney W. Grimes } 2282df8bae1dSRodney W. Grimes 2283df8bae1dSRodney W. Grimes /* 2284df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 2285df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 22864a3b3dcbSColin Percival * on the run queue. Must be called with the proc p locked. 2287df8bae1dSRodney W. Grimes */ 22885b3047d5SJohn Baldwin static void 2289e574e444SDavid Xu stop(struct proc *p) 2290df8bae1dSRodney W. Grimes { 2291df8bae1dSRodney W. Grimes 2292628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 22931279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 2294df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 229501609114SAlfred Perlstein wakeup(p->p_pptr); 2296df8bae1dSRodney W. Grimes } 2297df8bae1dSRodney W. Grimes 229890af4afaSJohn Baldwin /* 229990af4afaSJohn Baldwin * MPSAFE 230090af4afaSJohn Baldwin */ 2301e574e444SDavid Xu void 2302e574e444SDavid Xu thread_stopped(struct proc *p) 2303e574e444SDavid Xu { 2304e574e444SDavid Xu struct proc *p1 = curthread->td_proc; 230590af4afaSJohn Baldwin struct sigacts *ps; 2306e574e444SDavid Xu int n; 2307e574e444SDavid Xu 2308e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 2309e574e444SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 2310e574e444SDavid Xu n = p->p_suspcount; 2311e574e444SDavid Xu if (p == p1) 2312e574e444SDavid Xu n++; 2313e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2314e574e444SDavid Xu mtx_unlock_spin(&sched_lock); 2315e574e444SDavid Xu stop(p); 2316e574e444SDavid Xu PROC_LOCK(p->p_pptr); 231790af4afaSJohn Baldwin ps = p->p_pptr->p_sigacts; 231890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 231990af4afaSJohn Baldwin if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 232090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2321e574e444SDavid Xu psignal(p->p_pptr, SIGCHLD); 232290af4afaSJohn Baldwin } else 232390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2324e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 2325e574e444SDavid Xu mtx_lock_spin(&sched_lock); 2326e574e444SDavid Xu } 2327e574e444SDavid Xu } 2328e574e444SDavid Xu 2329df8bae1dSRodney W. Grimes /* 2330df8bae1dSRodney W. Grimes * Take the action for the specified signal 2331df8bae1dSRodney W. Grimes * from the current set of pending signals. 2332df8bae1dSRodney W. Grimes */ 2333df8bae1dSRodney W. Grimes void 23342c42a146SMarcel Moolenaar postsig(sig) 23352c42a146SMarcel Moolenaar register int sig; 2336df8bae1dSRodney W. Grimes { 2337b40ce416SJulian Elischer struct thread *td = curthread; 2338b40ce416SJulian Elischer register struct proc *p = td->td_proc; 2339628d2653SJohn Baldwin struct sigacts *ps; 23402c42a146SMarcel Moolenaar sig_t action; 23412c42a146SMarcel Moolenaar sigset_t returnmask; 23422c42a146SMarcel Moolenaar int code; 2343df8bae1dSRodney W. Grimes 23442c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 23455526d2d9SEivind Eklund 23462ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2347628d2653SJohn Baldwin ps = p->p_sigacts; 234890af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 23494093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 23502c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2351df8bae1dSRodney W. Grimes #ifdef KTRACE 2352374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 23535e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 23544093529dSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 2355df8bae1dSRodney W. Grimes #endif 2356047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2357047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2358047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2359047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2360047aa39bSRobert Watson } 23612a024a2bSSean Eric Fagan 2362cbf4e354SDavid Xu if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2363df8bae1dSRodney W. Grimes /* 2364df8bae1dSRodney W. Grimes * Default action, where the default is to kill 2365df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 2366df8bae1dSRodney W. Grimes */ 236790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2368b40ce416SJulian Elischer sigexit(td, sig); 2369df8bae1dSRodney W. Grimes /* NOTREACHED */ 2370df8bae1dSRodney W. Grimes } else { 2371cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) { 2372432b45deSDavid Xu if (sig == SIGKILL) { 2373432b45deSDavid Xu mtx_unlock(&ps->ps_mtx); 2374432b45deSDavid Xu sigexit(td, sig); 2375432b45deSDavid Xu } 2376432b45deSDavid Xu } 2377432b45deSDavid Xu 2378df8bae1dSRodney W. Grimes /* 2379df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 2380df8bae1dSRodney W. Grimes */ 23814093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 23825526d2d9SEivind Eklund ("postsig action")); 2383df8bae1dSRodney W. Grimes /* 2384df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 2385645682fdSLuoqi Chen * occurrences of this signal. 2386df8bae1dSRodney W. Grimes * 2387645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 2388df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 2389645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 2390df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 2391df8bae1dSRodney W. Grimes */ 23925e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 23934093529dSJeff Roberson returnmask = td->td_oldsigmask; 23945e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 2395df8bae1dSRodney W. Grimes } else 23964093529dSJeff Roberson returnmask = td->td_sigmask; 23972c42a146SMarcel Moolenaar 23984093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 23992c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 24004093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 24012c42a146SMarcel Moolenaar 24022c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2403289ccde0SPeter Wemm /* 24048f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 2405289ccde0SPeter Wemm */ 240690af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 24072c42a146SMarcel Moolenaar if (sig != SIGCONT && 24082c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 240990af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 24102c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2411dedc04feSPeter Wemm } 2412df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 24132c42a146SMarcel Moolenaar if (p->p_sig != sig) { 2414df8bae1dSRodney W. Grimes code = 0; 2415df8bae1dSRodney W. Grimes } else { 24166626c604SJulian Elischer code = p->p_code; 24176626c604SJulian Elischer p->p_code = 0; 24186626c604SJulian Elischer p->p_sig = 0; 2419df8bae1dSRodney W. Grimes } 2420cbf4e354SDavid Xu if (td->td_pflags & TDP_SA) 242158a3c273SJeff Roberson thread_signal_add(curthread, sig); 242258a3c273SJeff Roberson else 242358a3c273SJeff Roberson (*p->p_sysent->sv_sendsig)(action, sig, 242458a3c273SJeff Roberson &returnmask, code); 2425df8bae1dSRodney W. Grimes } 2426df8bae1dSRodney W. Grimes } 2427df8bae1dSRodney W. Grimes 2428df8bae1dSRodney W. Grimes /* 2429df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2430df8bae1dSRodney W. Grimes */ 243126f9a767SRodney W. Grimes void 2432df8bae1dSRodney W. Grimes killproc(p, why) 2433df8bae1dSRodney W. Grimes struct proc *p; 2434df8bae1dSRodney W. Grimes char *why; 2435df8bae1dSRodney W. Grimes { 24369081e5e8SJohn Baldwin 24379081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 24380384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 24390384fff8SJason Evans p, p->p_pid, p->p_comm); 2440729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2441b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2442df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 2443df8bae1dSRodney W. Grimes } 2444df8bae1dSRodney W. Grimes 2445df8bae1dSRodney W. Grimes /* 2446df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2447df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2448df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2449df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2450df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2451df8bae1dSRodney W. Grimes * does not return. 245290af4afaSJohn Baldwin * 245390af4afaSJohn Baldwin * MPSAFE 2454df8bae1dSRodney W. Grimes */ 245526f9a767SRodney W. Grimes void 2456b40ce416SJulian Elischer sigexit(td, sig) 2457b40ce416SJulian Elischer struct thread *td; 24582c42a146SMarcel Moolenaar int sig; 2459df8bae1dSRodney W. Grimes { 2460b40ce416SJulian Elischer struct proc *p = td->td_proc; 2461df8bae1dSRodney W. Grimes 2462628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2463df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 24642c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 24652c42a146SMarcel Moolenaar p->p_sig = sig; 2466c364e17eSAndrey A. Chernov /* 2467c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2468c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2469c364e17eSAndrey A. Chernov * these messages.) 2470c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 24714ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 2472c364e17eSAndrey A. Chernov */ 2473b40ce416SJulian Elischer if (coredump(td) == 0) 24742c42a146SMarcel Moolenaar sig |= WCOREFLAG; 247557308494SJoerg Wunsch if (kern_logsigexit) 247657308494SJoerg Wunsch log(LOG_INFO, 247757308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 24783d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 24799c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 24802c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 24812c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 24824ae89b95SJohn Baldwin } else 2483628d2653SJohn Baldwin PROC_UNLOCK(p); 2484b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2485df8bae1dSRodney W. Grimes /* NOTREACHED */ 2486df8bae1dSRodney W. Grimes } 2487df8bae1dSRodney W. Grimes 24886d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 2489c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2490c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 2491c5edb423SSean Eric Fagan 2492c5edb423SSean Eric Fagan /* 2493c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 2494c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 2495c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 2496c5edb423SSean Eric Fagan * %N name of process ("name") 2497c5edb423SSean Eric Fagan * %P process id (pid) 2498c5edb423SSean Eric Fagan * %U user id (uid) 2499c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 2500c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2501c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 2502c5edb423SSean Eric Fagan */ 2503c5edb423SSean Eric Fagan 2504fca666a1SJulian Elischer static char * 2505c5edb423SSean Eric Fagan expand_name(name, uid, pid) 25068b43b535SAlfred Perlstein const char *name; 25078b43b535SAlfred Perlstein uid_t uid; 25088b43b535SAlfred Perlstein pid_t pid; 25098b43b535SAlfred Perlstein { 25108b43b535SAlfred Perlstein const char *format, *appendstr; 2511c5edb423SSean Eric Fagan char *temp; 2512c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 25138b43b535SAlfred Perlstein size_t i, l, n; 2514c5edb423SSean Eric Fagan 25158b43b535SAlfred Perlstein format = corefilename; 25168b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 25170bfe2990SEivind Eklund if (temp == NULL) 25188b43b535SAlfred Perlstein return (NULL); 2519ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2520c5edb423SSean Eric Fagan switch (format[i]) { 2521c5edb423SSean Eric Fagan case '%': /* Format character */ 2522c5edb423SSean Eric Fagan i++; 2523c5edb423SSean Eric Fagan switch (format[i]) { 2524c5edb423SSean Eric Fagan case '%': 25258b43b535SAlfred Perlstein appendstr = "%"; 2526c5edb423SSean Eric Fagan break; 2527c5edb423SSean Eric Fagan case 'N': /* process name */ 25288b43b535SAlfred Perlstein appendstr = name; 2529c5edb423SSean Eric Fagan break; 2530c5edb423SSean Eric Fagan case 'P': /* process id */ 25318b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 25328b43b535SAlfred Perlstein appendstr = buf; 2533c5edb423SSean Eric Fagan break; 2534c5edb423SSean Eric Fagan case 'U': /* user id */ 25358b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 25368b43b535SAlfred Perlstein appendstr = buf; 2537c5edb423SSean Eric Fagan break; 2538c5edb423SSean Eric Fagan default: 25398b43b535SAlfred Perlstein appendstr = ""; 25408b43b535SAlfred Perlstein log(LOG_ERR, 25418b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 25428b43b535SAlfred Perlstein format[i], format); 2543c5edb423SSean Eric Fagan } 25448b43b535SAlfred Perlstein l = strlen(appendstr); 25458b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 25468b43b535SAlfred Perlstein goto toolong; 25478b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 25488b43b535SAlfred Perlstein n += l; 2549c5edb423SSean Eric Fagan break; 2550c5edb423SSean Eric Fagan default: 2551c5edb423SSean Eric Fagan temp[n++] = format[i]; 2552c5edb423SSean Eric Fagan } 2553c5edb423SSean Eric Fagan } 25548b43b535SAlfred Perlstein if (format[i] != '\0') 25558b43b535SAlfred Perlstein goto toolong; 25568b43b535SAlfred Perlstein return (temp); 25578b43b535SAlfred Perlstein toolong: 25588b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 25598b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 25608b43b535SAlfred Perlstein free(temp, M_TEMP); 25618b43b535SAlfred Perlstein return (NULL); 2562c5edb423SSean Eric Fagan } 2563c5edb423SSean Eric Fagan 2564df8bae1dSRodney W. Grimes /* 2565fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2566fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2567fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2568fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2569fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2570fca666a1SJulian Elischer */ 2571fca666a1SJulian Elischer 2572fca666a1SJulian Elischer static int 2573b40ce416SJulian Elischer coredump(struct thread *td) 2574fca666a1SJulian Elischer { 2575b40ce416SJulian Elischer struct proc *p = td->td_proc; 2576fca666a1SJulian Elischer register struct vnode *vp; 25779c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 257806ae1e91SMatthew Dillon struct flock lf; 2579fca666a1SJulian Elischer struct nameidata nd; 2580fca666a1SJulian Elischer struct vattr vattr; 2581c447f5b2SRobert Watson int error, error1, flags, locked; 2582f2a2857bSKirk McKusick struct mount *mp; 2583fca666a1SJulian Elischer char *name; /* name of corefile */ 2584fca666a1SJulian Elischer off_t limit; 2585fca666a1SJulian Elischer 25864ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2587628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2588fca666a1SJulian Elischer 2589628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2590628d2653SJohn Baldwin PROC_UNLOCK(p); 2591fca666a1SJulian Elischer return (EFAULT); 2592628d2653SJohn Baldwin } 2593fca666a1SJulian Elischer 2594fca666a1SJulian Elischer /* 259535a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 259635a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 259735a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 259835a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 259935a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 260035a2598fSSean Eric Fagan * if it is larger than the limit. 2601fca666a1SJulian Elischer */ 260291d5354aSJohn Baldwin limit = (off_t)lim_cur(p, RLIMIT_CORE); 2603628d2653SJohn Baldwin PROC_UNLOCK(p); 260491d5354aSJohn Baldwin if (limit == 0) 260591d5354aSJohn Baldwin return (EFBIG); 260635a2598fSSean Eric Fagan 26074ae89b95SJohn Baldwin mtx_lock(&Giant); 2608f2a2857bSKirk McKusick restart: 26099c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 26104ae89b95SJohn Baldwin if (name == NULL) { 26114ae89b95SJohn Baldwin mtx_unlock(&Giant); 2612ccdbd10cSPeter Pentchev return (EINVAL); 26134ae89b95SJohn Baldwin } 2614b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2615e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 26167c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 2617fca666a1SJulian Elischer free(name, M_TEMP); 26184ae89b95SJohn Baldwin if (error) { 26194ae89b95SJohn Baldwin mtx_unlock(&Giant); 2620fca666a1SJulian Elischer return (error); 26214ae89b95SJohn Baldwin } 2622762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2623fca666a1SJulian Elischer vp = nd.ni_vp; 262406ae1e91SMatthew Dillon 2625832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2626832dafadSDon Lewis if (vp->v_type != VREG || 2627832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2628832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2629832dafadSDon Lewis error = EFAULT; 26304ae89b95SJohn Baldwin goto out; 2631832dafadSDon Lewis } 2632832dafadSDon Lewis 2633b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 263406ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 263506ae1e91SMatthew Dillon lf.l_start = 0; 263606ae1e91SMatthew Dillon lf.l_len = 0; 263706ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 2638c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 263906ae1e91SMatthew Dillon 264006ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 264106ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 264236bbf86bSRobert Watson if (locked) 264306ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2644b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2645f2a2857bSKirk McKusick return (error); 2646f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2647f2a2857bSKirk McKusick return (error); 2648f2a2857bSKirk McKusick goto restart; 2649f2a2857bSKirk McKusick } 2650fca666a1SJulian Elischer 2651fca666a1SJulian Elischer VATTR_NULL(&vattr); 2652fca666a1SJulian Elischer vattr.va_size = 0; 26536141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 26546141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 265588b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2656b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2657b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 265888b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2659628d2653SJohn Baldwin PROC_LOCK(p); 2660fca666a1SJulian Elischer p->p_acflag |= ACORE; 2661628d2653SJohn Baldwin PROC_UNLOCK(p); 2662fca666a1SJulian Elischer 2663fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2664b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2665fca666a1SJulian Elischer ENOSYS; 2666fca666a1SJulian Elischer 2667c447f5b2SRobert Watson if (locked) { 266806ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 266906ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2670c447f5b2SRobert Watson } 2671f2a2857bSKirk McKusick vn_finished_write(mp); 26724ae89b95SJohn Baldwin out: 2673b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 26744ae89b95SJohn Baldwin mtx_unlock(&Giant); 2675fca666a1SJulian Elischer if (error == 0) 2676fca666a1SJulian Elischer error = error1; 2677fca666a1SJulian Elischer return (error); 2678fca666a1SJulian Elischer } 2679fca666a1SJulian Elischer 2680fca666a1SJulian Elischer /* 2681df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2682df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2683df8bae1dSRodney W. Grimes */ 2684d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2685df8bae1dSRodney W. Grimes struct nosys_args { 2686df8bae1dSRodney W. Grimes int dummy; 2687df8bae1dSRodney W. Grimes }; 2688d2d3e875SBruce Evans #endif 2689fb99ab88SMatthew Dillon /* 2690fb99ab88SMatthew Dillon * MPSAFE 2691fb99ab88SMatthew Dillon */ 2692df8bae1dSRodney W. Grimes /* ARGSUSED */ 269326f9a767SRodney W. Grimes int 2694b40ce416SJulian Elischer nosys(td, args) 2695b40ce416SJulian Elischer struct thread *td; 2696df8bae1dSRodney W. Grimes struct nosys_args *args; 2697df8bae1dSRodney W. Grimes { 2698b40ce416SJulian Elischer struct proc *p = td->td_proc; 2699b40ce416SJulian Elischer 2700628d2653SJohn Baldwin PROC_LOCK(p); 2701df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2702628d2653SJohn Baldwin PROC_UNLOCK(p); 2703f5216b9aSBruce Evans return (ENOSYS); 2704df8bae1dSRodney W. Grimes } 2705831d27a9SDon Lewis 2706831d27a9SDon Lewis /* 270748f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2708831d27a9SDon Lewis * stored credentials rather than those of the current process. 2709831d27a9SDon Lewis */ 2710831d27a9SDon Lewis void 2711f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2712f1320723SAlfred Perlstein struct sigio **sigiop; 27132c42a146SMarcel Moolenaar int sig, checkctty; 2714831d27a9SDon Lewis { 2715f1320723SAlfred Perlstein struct sigio *sigio; 2716831d27a9SDon Lewis 2717f1320723SAlfred Perlstein SIGIO_LOCK(); 2718f1320723SAlfred Perlstein sigio = *sigiop; 2719f1320723SAlfred Perlstein if (sigio == NULL) { 2720f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2721f1320723SAlfred Perlstein return; 2722f1320723SAlfred Perlstein } 2723831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2724628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 27252b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 27262c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2727628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2728831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2729831d27a9SDon Lewis struct proc *p; 2730831d27a9SDon Lewis 2731f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2732628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2733628d2653SJohn Baldwin PROC_LOCK(p); 27342b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2735831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 27362c42a146SMarcel Moolenaar psignal(p, sig); 2737628d2653SJohn Baldwin PROC_UNLOCK(p); 2738628d2653SJohn Baldwin } 2739f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2740831d27a9SDon Lewis } 2741f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2742831d27a9SDon Lewis } 2743cb679c38SJonathan Lemon 2744cb679c38SJonathan Lemon static int 2745cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2746cb679c38SJonathan Lemon { 2747cb679c38SJonathan Lemon struct proc *p = curproc; 2748cb679c38SJonathan Lemon 2749cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2750cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2751cb679c38SJonathan Lemon 2752ad3b9257SJohn-Mark Gurney knlist_add(&p->p_klist, kn, 0); 2753cb679c38SJonathan Lemon 2754cb679c38SJonathan Lemon return (0); 2755cb679c38SJonathan Lemon } 2756cb679c38SJonathan Lemon 2757cb679c38SJonathan Lemon static void 2758cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2759cb679c38SJonathan Lemon { 2760cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2761cb679c38SJonathan Lemon 2762ad3b9257SJohn-Mark Gurney knlist_remove(&p->p_klist, kn, 0); 2763cb679c38SJonathan Lemon } 2764cb679c38SJonathan Lemon 2765cb679c38SJonathan Lemon /* 2766cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2767cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2768cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2769cb679c38SJonathan Lemon * isn't worth the trouble. 2770cb679c38SJonathan Lemon */ 2771cb679c38SJonathan Lemon static int 2772cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2773cb679c38SJonathan Lemon { 2774cb679c38SJonathan Lemon 2775cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2776cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2777cb679c38SJonathan Lemon 2778cb679c38SJonathan Lemon if (kn->kn_id == hint) 2779cb679c38SJonathan Lemon kn->kn_data++; 2780cb679c38SJonathan Lemon } 2781cb679c38SJonathan Lemon return (kn->kn_data != 0); 2782cb679c38SJonathan Lemon } 278390af4afaSJohn Baldwin 278490af4afaSJohn Baldwin struct sigacts * 278590af4afaSJohn Baldwin sigacts_alloc(void) 278690af4afaSJohn Baldwin { 278790af4afaSJohn Baldwin struct sigacts *ps; 278890af4afaSJohn Baldwin 278990af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 279090af4afaSJohn Baldwin ps->ps_refcnt = 1; 279190af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 279290af4afaSJohn Baldwin return (ps); 279390af4afaSJohn Baldwin } 279490af4afaSJohn Baldwin 279590af4afaSJohn Baldwin void 279690af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 279790af4afaSJohn Baldwin { 279890af4afaSJohn Baldwin 279990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 280090af4afaSJohn Baldwin ps->ps_refcnt--; 280190af4afaSJohn Baldwin if (ps->ps_refcnt == 0) { 280290af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 280390af4afaSJohn Baldwin free(ps, M_SUBPROC); 280490af4afaSJohn Baldwin } else 280590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 280690af4afaSJohn Baldwin } 280790af4afaSJohn Baldwin 280890af4afaSJohn Baldwin struct sigacts * 280990af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 281090af4afaSJohn Baldwin { 281190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 281290af4afaSJohn Baldwin ps->ps_refcnt++; 281390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 281490af4afaSJohn Baldwin return (ps); 281590af4afaSJohn Baldwin } 281690af4afaSJohn Baldwin 281790af4afaSJohn Baldwin void 281890af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 281990af4afaSJohn Baldwin { 282090af4afaSJohn Baldwin 282190af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 282290af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 282390af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 282490af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 282590af4afaSJohn Baldwin } 282690af4afaSJohn Baldwin 282790af4afaSJohn Baldwin int 282890af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 282990af4afaSJohn Baldwin { 283090af4afaSJohn Baldwin int shared; 283190af4afaSJohn Baldwin 283290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 283390af4afaSJohn Baldwin shared = ps->ps_refcnt > 1; 283490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 283590af4afaSJohn Baldwin return (shared); 283690af4afaSJohn Baldwin } 2837