1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41677b542eSDavid E. O'Brien #include <sys/cdefs.h> 42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 43677b542eSDavid E. O'Brien 445591b823SEivind Eklund #include "opt_compat.h" 45db6a20e2SGarrett Wollman #include "opt_ktrace.h" 46db6a20e2SGarrett Wollman 47df8bae1dSRodney W. Grimes #include <sys/param.h> 4836240ea5SDoug Rabson #include <sys/systm.h> 49df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/vnode.h> 51df8bae1dSRodney W. Grimes #include <sys/acct.h> 52238510fcSJason Evans #include <sys/condvar.h> 53854dc8c2SJohn Baldwin #include <sys/event.h> 54854dc8c2SJohn Baldwin #include <sys/fcntl.h> 55854dc8c2SJohn Baldwin #include <sys/kernel.h> 569dde3bc9SDavid Xu #include <sys/kse.h> 570384fff8SJason Evans #include <sys/ktr.h> 58df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 59854dc8c2SJohn Baldwin #include <sys/lock.h> 60854dc8c2SJohn Baldwin #include <sys/malloc.h> 61854dc8c2SJohn Baldwin #include <sys/mutex.h> 62854dc8c2SJohn Baldwin #include <sys/namei.h> 63854dc8c2SJohn Baldwin #include <sys/proc.h> 64854dc8c2SJohn Baldwin #include <sys/pioctl.h> 65c31146a1SJohn Baldwin #include <sys/resourcevar.h> 666caa8a15SJohn Baldwin #include <sys/smp.h> 67df8bae1dSRodney W. Grimes #include <sys/stat.h> 681005a129SJohn Baldwin #include <sys/sx.h> 698f19eb88SIan Dowse #include <sys/syscallsubr.h> 70c87e2930SDavid Greenman #include <sys/sysctl.h> 71854dc8c2SJohn Baldwin #include <sys/sysent.h> 72854dc8c2SJohn Baldwin #include <sys/syslog.h> 73854dc8c2SJohn Baldwin #include <sys/sysproto.h> 7406ae1e91SMatthew Dillon #include <sys/unistd.h> 75854dc8c2SJohn Baldwin #include <sys/wait.h> 76df8bae1dSRodney W. Grimes 77df8bae1dSRodney W. Grimes #include <machine/cpu.h> 78df8bae1dSRodney W. Grimes 7923eeeff7SPeter Wemm #if defined (__alpha__) && !defined(COMPAT_43) 8023eeeff7SPeter Wemm #error "You *really* need COMPAT_43 on the alpha for longjmp(3)" 8123eeeff7SPeter Wemm #endif 8223eeeff7SPeter Wemm 836f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 846f841fb7SMarcel Moolenaar 854d77a549SAlfred Perlstein static int coredump(struct thread *); 864d77a549SAlfred Perlstein static char *expand_name(const char *, uid_t, pid_t); 879c1ab3e0SJohn Baldwin static int killpg1(struct thread *td, int sig, int pgid, int all); 886711f10fSJohn Baldwin static int issignal(struct thread *p); 894d77a549SAlfred Perlstein static int sigprop(int sig); 904d77a549SAlfred Perlstein static void stop(struct proc *); 914093529dSJeff Roberson static void tdsigwakeup(struct thread *td, int sig, sig_t action); 92cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 93cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 94cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 954093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop); 96a447cd8bSJeff Roberson static int kern_sigtimedwait(struct thread *td, sigset_t set, 97a447cd8bSJeff Roberson siginfo_t *info, struct timespec *timeout); 98c197abc4SMike Makonnen static void do_tdsignal(struct thread *td, int sig, sigtarget_t target); 99cb679c38SJonathan Lemon 100cb679c38SJonathan Lemon struct filterops sig_filtops = 101cb679c38SJonathan Lemon { 0, filt_sigattach, filt_sigdetach, filt_signal }; 102cb679c38SJonathan Lemon 10357308494SJoerg Wunsch static int kern_logsigexit = 1; 1043d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1053d177f46SBill Fumerola &kern_logsigexit, 0, 1063d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 10757308494SJoerg Wunsch 1082b87b6d4SRobert Watson /* 1092b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1102b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1112b87b6d4SRobert Watson * in the right situations. 1122b87b6d4SRobert Watson */ 1132b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1142b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1152b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1162b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1172b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1182b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1192b87b6d4SRobert Watson 12022d4b0fbSJohn Polstra int sugid_coredump; 1213d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1223d177f46SBill Fumerola &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 123c87e2930SDavid Greenman 124e5a28db9SPaul Saab static int do_coredump = 1; 125e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 126e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 127e5a28db9SPaul Saab 1282c42a146SMarcel Moolenaar /* 1292c42a146SMarcel Moolenaar * Signal properties and actions. 1302c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1312c42a146SMarcel Moolenaar * according to the following properties: 1322c42a146SMarcel Moolenaar */ 1332c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1342c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1352c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1362c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1372c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 1382c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 1392c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 140da33176fSJeff Roberson #define SA_PROC 0x80 /* deliverable to any thread */ 141df8bae1dSRodney W. Grimes 1422c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 143da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGHUP */ 144da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGINT */ 145da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */ 1462c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 1472c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 1482c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 149da33176fSJeff Roberson SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */ 1502c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 151da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGKILL */ 1522c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 1532c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 1542c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 155da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPIPE */ 156da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGALRM */ 157da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGTERM */ 158da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGURG */ 159da33176fSJeff Roberson SA_STOP|SA_PROC, /* SIGSTOP */ 160da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */ 161da33176fSJeff Roberson SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */ 162da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGCHLD */ 163da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */ 164da33176fSJeff Roberson SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */ 165da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGIO */ 1662c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 1672c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 168da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGVTALRM */ 169da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGPROF */ 170da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGWINCH */ 171da33176fSJeff Roberson SA_IGNORE|SA_PROC, /* SIGINFO */ 172da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR1 */ 173da33176fSJeff Roberson SA_KILL|SA_PROC, /* SIGUSR2 */ 1742c42a146SMarcel Moolenaar }; 1752c42a146SMarcel Moolenaar 176fbbeeb6cSBruce Evans /* 177fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 178fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 179fbbeeb6cSBruce Evans * action, the process stops in issignal(). 180e602ba25SJulian Elischer * XXXKSE the check for a pending stop is not done under KSE 181fbbeeb6cSBruce Evans * 18233510ef1SBruce Evans * MP SAFE. 183fbbeeb6cSBruce Evans */ 184fbbeeb6cSBruce Evans int 185e602ba25SJulian Elischer cursig(struct thread *td) 186fbbeeb6cSBruce Evans { 187c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 18890af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 189179235b3SBruce Evans mtx_assert(&sched_lock, MA_NOTOWNED); 1904093529dSJeff Roberson return (SIGPENDING(td) ? issignal(td) : 0); 191fbbeeb6cSBruce Evans } 192fbbeeb6cSBruce Evans 19379065dbaSBruce Evans /* 19479065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 1954093529dSJeff Roberson * mode. This must be called whenever a signal is added to td_siglist or 1964093529dSJeff Roberson * unmasked in td_sigmask. 19779065dbaSBruce Evans */ 19879065dbaSBruce Evans void 1994093529dSJeff Roberson signotify(struct thread *td) 20079065dbaSBruce Evans { 2014093529dSJeff Roberson struct proc *p; 2029dde3bc9SDavid Xu sigset_t set, saved; 2034093529dSJeff Roberson 2044093529dSJeff Roberson p = td->td_proc; 20579065dbaSBruce Evans 20679065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 2074093529dSJeff Roberson 2084093529dSJeff Roberson /* 2094093529dSJeff Roberson * If our mask changed we may have to move signal that were 2104093529dSJeff Roberson * previously masked by all threads to our siglist. 2114093529dSJeff Roberson */ 2124093529dSJeff Roberson set = p->p_siglist; 2139dde3bc9SDavid Xu if (p->p_flag & P_SA) 2149dde3bc9SDavid Xu saved = p->p_siglist; 2154093529dSJeff Roberson SIGSETNAND(set, td->td_sigmask); 2164093529dSJeff Roberson SIGSETNAND(p->p_siglist, set); 2174093529dSJeff Roberson SIGSETOR(td->td_siglist, set); 2184093529dSJeff Roberson 2198b94a061SJohn Baldwin if (SIGPENDING(td)) { 22079065dbaSBruce Evans mtx_lock_spin(&sched_lock); 2214093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 22279065dbaSBruce Evans mtx_unlock_spin(&sched_lock); 22379065dbaSBruce Evans } 2249dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 2259dde3bc9SDavid Xu if (SIGSETEQ(saved, p->p_siglist)) 2269dde3bc9SDavid Xu return; 2279dde3bc9SDavid Xu else { 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 { 2388b94a061SJohn Baldwin struct proc *p = curthread->td_proc; 2398b94a061SJohn Baldwin 2408b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2418b94a061SJohn Baldwin return ((p->p_flag & P_ALTSTACK) ? 2428b94a061SJohn Baldwin #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2438b94a061SJohn Baldwin ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) : 2448b94a061SJohn Baldwin ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)) 2458b94a061SJohn Baldwin #else 2468b94a061SJohn Baldwin ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_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); 353df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 3542c42a146SMarcel Moolenaar if (act->sa_flags & SA_USERTRAMP) 3552c42a146SMarcel Moolenaar SIGADDSET(ps->ps_usertramp, sig); 356df8bae1dSRodney W. Grimes else 3578c3d74f4SBruce Evans SIGDELSET(ps->ps_usertramp, sig); 358df8bae1dSRodney W. Grimes #endif 3592c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 3602c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 36190af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 3626626c604SJulian Elischer else 36390af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 364ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 365245f17d4SJoerg Wunsch /* 3662c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 3672c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 3682c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 3692c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 370245f17d4SJoerg Wunsch */ 371245f17d4SJoerg Wunsch if (p->p_pid == 1) 37290af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 3736626c604SJulian Elischer else 37490af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 375245f17d4SJoerg Wunsch } else 37690af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 377ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 37890af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 379ba1551caSIan Dowse else 38090af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes /* 38390af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 3842c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 38590af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 3862c42a146SMarcel Moolenaar * have to restart the process. 387df8bae1dSRodney W. Grimes */ 3882c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 3892c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 3902c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 3919dde3bc9SDavid Xu if ((p->p_flag & P_SA) && 3929dde3bc9SDavid Xu SIGISMEMBER(p->p_siglist, sig)) { 3939dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 3949dde3bc9SDavid Xu wakeup(&p->p_siglist); 3959dde3bc9SDavid Xu } 3962c42a146SMarcel Moolenaar /* never to be seen again */ 3971d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 3984093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 3994093529dSJeff Roberson SIGDELSET(td0->td_siglist, sig); 4002c42a146SMarcel Moolenaar if (sig != SIGCONT) 4012c42a146SMarcel Moolenaar /* easier in psignal */ 40290af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 40390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 404645682fdSLuoqi Chen } else { 40590af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 4062c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 40790af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 4082c42a146SMarcel Moolenaar else 40990af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 4102c42a146SMarcel Moolenaar } 41123eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 41223eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 41323eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 41423eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 41523eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 41623eeeff7SPeter Wemm else 41723eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 41823eeeff7SPeter Wemm #endif 419e8ebc08fSPeter Wemm #ifdef COMPAT_43 420645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 42123eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 42223eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 423645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 424645682fdSLuoqi Chen else 425645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 426e8ebc08fSPeter Wemm #endif 427df8bae1dSRodney W. Grimes } 42890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 429628d2653SJohn Baldwin PROC_UNLOCK(p); 4302c42a146SMarcel Moolenaar return (0); 4312c42a146SMarcel Moolenaar } 4322c42a146SMarcel Moolenaar 4332c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 4342c42a146SMarcel Moolenaar struct sigaction_args { 4352c42a146SMarcel Moolenaar int sig; 4362c42a146SMarcel Moolenaar struct sigaction *act; 4372c42a146SMarcel Moolenaar struct sigaction *oact; 4382c42a146SMarcel Moolenaar }; 4392c42a146SMarcel Moolenaar #endif 440fb99ab88SMatthew Dillon /* 441fb99ab88SMatthew Dillon * MPSAFE 442fb99ab88SMatthew Dillon */ 4432c42a146SMarcel Moolenaar int 444b40ce416SJulian Elischer sigaction(td, uap) 445b40ce416SJulian Elischer struct thread *td; 4462c42a146SMarcel Moolenaar register struct sigaction_args *uap; 4472c42a146SMarcel Moolenaar { 4482c42a146SMarcel Moolenaar struct sigaction act, oact; 4492c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 4502c42a146SMarcel Moolenaar int error; 4512c42a146SMarcel Moolenaar 4526f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 4536f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 4542c42a146SMarcel Moolenaar if (actp) { 4556f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 4562c42a146SMarcel Moolenaar if (error) 45744443757STim J. Robbins return (error); 4582c42a146SMarcel Moolenaar } 4598f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 46025d6dc06SJohn Baldwin if (oactp && !error) 4616f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 4622c42a146SMarcel Moolenaar return (error); 4632c42a146SMarcel Moolenaar } 4642c42a146SMarcel Moolenaar 46523eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 46623eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 46723eeeff7SPeter Wemm struct freebsd4_sigaction_args { 46823eeeff7SPeter Wemm int sig; 46923eeeff7SPeter Wemm struct sigaction *act; 47023eeeff7SPeter Wemm struct sigaction *oact; 47123eeeff7SPeter Wemm }; 47223eeeff7SPeter Wemm #endif 47323eeeff7SPeter Wemm /* 47423eeeff7SPeter Wemm * MPSAFE 47523eeeff7SPeter Wemm */ 47623eeeff7SPeter Wemm int 47723eeeff7SPeter Wemm freebsd4_sigaction(td, uap) 47823eeeff7SPeter Wemm struct thread *td; 47923eeeff7SPeter Wemm register struct freebsd4_sigaction_args *uap; 48023eeeff7SPeter Wemm { 48123eeeff7SPeter Wemm struct sigaction act, oact; 48223eeeff7SPeter Wemm register struct sigaction *actp, *oactp; 48323eeeff7SPeter Wemm int error; 48423eeeff7SPeter Wemm 48523eeeff7SPeter Wemm 48623eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 48723eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 48823eeeff7SPeter Wemm if (actp) { 48923eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 49023eeeff7SPeter Wemm if (error) 49144443757STim J. Robbins return (error); 49223eeeff7SPeter Wemm } 49323eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 494a14e1189SJohn Baldwin if (oactp && !error) 49523eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 49623eeeff7SPeter Wemm return (error); 49723eeeff7SPeter Wemm } 49823eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 49923eeeff7SPeter Wemm 50031c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 5012c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 5022c42a146SMarcel Moolenaar struct osigaction_args { 5032c42a146SMarcel Moolenaar int signum; 5042c42a146SMarcel Moolenaar struct osigaction *nsa; 5052c42a146SMarcel Moolenaar struct osigaction *osa; 5062c42a146SMarcel Moolenaar }; 5072c42a146SMarcel Moolenaar #endif 508fb99ab88SMatthew Dillon /* 509fb99ab88SMatthew Dillon * MPSAFE 510fb99ab88SMatthew Dillon */ 5112c42a146SMarcel Moolenaar int 512b40ce416SJulian Elischer osigaction(td, uap) 513b40ce416SJulian Elischer struct thread *td; 5142c42a146SMarcel Moolenaar register struct osigaction_args *uap; 5152c42a146SMarcel Moolenaar { 5162c42a146SMarcel Moolenaar struct osigaction sa; 5172c42a146SMarcel Moolenaar struct sigaction nsa, osa; 5182c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 5192c42a146SMarcel Moolenaar int error; 5202c42a146SMarcel Moolenaar 5216f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 5226f841fb7SMarcel Moolenaar return (EINVAL); 523fb99ab88SMatthew Dillon 5246f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 5256f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 526fb99ab88SMatthew Dillon 5272c42a146SMarcel Moolenaar if (nsap) { 5286f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 5292c42a146SMarcel Moolenaar if (error) 53044443757STim J. Robbins return (error); 5312c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 5322c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 5332c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 5342c42a146SMarcel Moolenaar } 53523eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 5362c42a146SMarcel Moolenaar if (osap && !error) { 5372c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 5382c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 5392c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 5406f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 5412c42a146SMarcel Moolenaar } 5422c42a146SMarcel Moolenaar return (error); 5432c42a146SMarcel Moolenaar } 54423eeeff7SPeter Wemm 54523eeeff7SPeter Wemm #if !defined(__i386__) && !defined(__alpha__) 54623eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 54723eeeff7SPeter Wemm int 54823eeeff7SPeter Wemm osigreturn(td, uap) 54923eeeff7SPeter Wemm struct thread *td; 55023eeeff7SPeter Wemm struct osigreturn_args *uap; 55123eeeff7SPeter Wemm { 55223eeeff7SPeter Wemm 55323eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 55423eeeff7SPeter Wemm } 55523eeeff7SPeter Wemm #endif 55631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 557df8bae1dSRodney W. Grimes 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 560df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes void 563df8bae1dSRodney W. Grimes siginit(p) 564df8bae1dSRodney W. Grimes struct proc *p; 565df8bae1dSRodney W. Grimes { 566df8bae1dSRodney W. Grimes register int i; 56790af4afaSJohn Baldwin struct sigacts *ps; 568df8bae1dSRodney W. Grimes 569628d2653SJohn Baldwin PROC_LOCK(p); 57090af4afaSJohn Baldwin ps = p->p_sigacts; 57190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 5722c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 5732c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 57490af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 57590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 576628d2653SJohn Baldwin PROC_UNLOCK(p); 577df8bae1dSRodney W. Grimes } 578df8bae1dSRodney W. Grimes 579df8bae1dSRodney W. Grimes /* 580df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 581df8bae1dSRodney W. Grimes */ 582df8bae1dSRodney W. Grimes void 583df8bae1dSRodney W. Grimes execsigs(p) 584df8bae1dSRodney W. Grimes register struct proc *p; 585df8bae1dSRodney W. Grimes { 586628d2653SJohn Baldwin register struct sigacts *ps; 5872c42a146SMarcel Moolenaar register int sig; 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes /* 590df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 5914093529dSJeff Roberson * through td_sigmask (unless they were caught, 592df8bae1dSRodney W. Grimes * and are now ignored by default). 593df8bae1dSRodney W. Grimes */ 5949b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 595628d2653SJohn Baldwin ps = p->p_sigacts; 59690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 59790af4afaSJohn Baldwin while (SIGNOTEMPTY(ps->ps_sigcatch)) { 59890af4afaSJohn Baldwin sig = sig_ffs(&ps->ps_sigcatch); 59990af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 6002c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 6012c42a146SMarcel Moolenaar if (sig != SIGCONT) 60290af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 6031d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 6044093529dSJeff Roberson /* 6054093529dSJeff Roberson * There is only one thread at this point. 6064093529dSJeff Roberson */ 6074093529dSJeff Roberson SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig); 608df8bae1dSRodney W. Grimes } 6092c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes /* 6124093529dSJeff Roberson * Clear out the td's sigmask. Normal processes use the proc sigmask. 6134093529dSJeff Roberson */ 6144093529dSJeff Roberson SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask); 6154093529dSJeff Roberson /* 616df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 617df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 618df8bae1dSRodney W. Grimes */ 619645682fdSLuoqi Chen p->p_sigstk.ss_flags = SS_DISABLE; 620645682fdSLuoqi Chen p->p_sigstk.ss_size = 0; 621645682fdSLuoqi Chen p->p_sigstk.ss_sp = 0; 6223b26be6aSAkinori MUSHA p->p_flag &= ~P_ALTSTACK; 62380e907a1SPeter Wemm /* 62480e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 62580e907a1SPeter Wemm */ 62690af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 627c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 628c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 62990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes /* 633e77daab1SJohn Baldwin * kern_sigprocmask() 6347c8fdcbdSMatthew Dillon * 635628d2653SJohn Baldwin * Manipulate signal mask. 636df8bae1dSRodney W. Grimes */ 637e77daab1SJohn Baldwin int 638e77daab1SJohn Baldwin kern_sigprocmask(td, how, set, oset, old) 6394093529dSJeff Roberson struct thread *td; 6402c42a146SMarcel Moolenaar int how; 6412c42a146SMarcel Moolenaar sigset_t *set, *oset; 642645682fdSLuoqi Chen int old; 6432c42a146SMarcel Moolenaar { 6442c42a146SMarcel Moolenaar int error; 6452c42a146SMarcel Moolenaar 6464093529dSJeff Roberson PROC_LOCK(td->td_proc); 6472c42a146SMarcel Moolenaar if (oset != NULL) 6484093529dSJeff Roberson *oset = td->td_sigmask; 6492c42a146SMarcel Moolenaar 6502c42a146SMarcel Moolenaar error = 0; 6512c42a146SMarcel Moolenaar if (set != NULL) { 6522c42a146SMarcel Moolenaar switch (how) { 6532c42a146SMarcel Moolenaar case SIG_BLOCK: 654645682fdSLuoqi Chen SIG_CANTMASK(*set); 6554093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 6562c42a146SMarcel Moolenaar break; 6572c42a146SMarcel Moolenaar case SIG_UNBLOCK: 6584093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 6594093529dSJeff Roberson signotify(td); 6602c42a146SMarcel Moolenaar break; 6612c42a146SMarcel Moolenaar case SIG_SETMASK: 662645682fdSLuoqi Chen SIG_CANTMASK(*set); 663645682fdSLuoqi Chen if (old) 6644093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 665645682fdSLuoqi Chen else 6664093529dSJeff Roberson td->td_sigmask = *set; 6674093529dSJeff Roberson signotify(td); 6682c42a146SMarcel Moolenaar break; 6692c42a146SMarcel Moolenaar default: 6702c42a146SMarcel Moolenaar error = EINVAL; 6712c42a146SMarcel Moolenaar break; 6722c42a146SMarcel Moolenaar } 6732c42a146SMarcel Moolenaar } 6744093529dSJeff Roberson PROC_UNLOCK(td->td_proc); 6752c42a146SMarcel Moolenaar return (error); 6762c42a146SMarcel Moolenaar } 6772c42a146SMarcel Moolenaar 6787c8fdcbdSMatthew Dillon /* 679e77daab1SJohn Baldwin * sigprocmask() - MP SAFE 6807c8fdcbdSMatthew Dillon */ 6817c8fdcbdSMatthew Dillon 682d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 683df8bae1dSRodney W. Grimes struct sigprocmask_args { 684df8bae1dSRodney W. Grimes int how; 6852c42a146SMarcel Moolenaar const sigset_t *set; 6862c42a146SMarcel Moolenaar sigset_t *oset; 687df8bae1dSRodney W. Grimes }; 688d2d3e875SBruce Evans #endif 68926f9a767SRodney W. Grimes int 690b40ce416SJulian Elischer sigprocmask(td, uap) 691b40ce416SJulian Elischer register struct thread *td; 692df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 693df8bae1dSRodney W. Grimes { 6942c42a146SMarcel Moolenaar sigset_t set, oset; 6952c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 6962c42a146SMarcel Moolenaar int error; 697df8bae1dSRodney W. Grimes 6986f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 6996f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 7002c42a146SMarcel Moolenaar if (setp) { 7016f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 7022c42a146SMarcel Moolenaar if (error) 7032c42a146SMarcel Moolenaar return (error); 704df8bae1dSRodney W. Grimes } 705e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 7062c42a146SMarcel Moolenaar if (osetp && !error) { 7076f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 7082c42a146SMarcel Moolenaar } 7092c42a146SMarcel Moolenaar return (error); 7102c42a146SMarcel Moolenaar } 7112c42a146SMarcel Moolenaar 71231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 7137c8fdcbdSMatthew Dillon /* 7147c8fdcbdSMatthew Dillon * osigprocmask() - MP SAFE 7157c8fdcbdSMatthew Dillon */ 7162c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7172c42a146SMarcel Moolenaar struct osigprocmask_args { 7182c42a146SMarcel Moolenaar int how; 7192c42a146SMarcel Moolenaar osigset_t mask; 7202c42a146SMarcel Moolenaar }; 7212c42a146SMarcel Moolenaar #endif 7222c42a146SMarcel Moolenaar int 723b40ce416SJulian Elischer osigprocmask(td, uap) 724b40ce416SJulian Elischer register struct thread *td; 7252c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 7262c42a146SMarcel Moolenaar { 7272c42a146SMarcel Moolenaar sigset_t set, oset; 7282c42a146SMarcel Moolenaar int error; 7292c42a146SMarcel Moolenaar 7302c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 731e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 732b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 733df8bae1dSRodney W. Grimes return (error); 734df8bae1dSRodney W. Grimes } 73531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 736df8bae1dSRodney W. Grimes 737d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 738df8bae1dSRodney W. Grimes struct sigpending_args { 7392c42a146SMarcel Moolenaar sigset_t *set; 740df8bae1dSRodney W. Grimes }; 741d2d3e875SBruce Evans #endif 742fb99ab88SMatthew Dillon /* 743fb99ab88SMatthew Dillon * MPSAFE 744fb99ab88SMatthew Dillon */ 74526f9a767SRodney W. Grimes int 746a447cd8bSJeff Roberson sigwait(struct thread *td, struct sigwait_args *uap) 747a447cd8bSJeff Roberson { 748a447cd8bSJeff Roberson siginfo_t info; 749a447cd8bSJeff Roberson sigset_t set; 750a447cd8bSJeff Roberson int error; 751a447cd8bSJeff Roberson 752a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 753a447cd8bSJeff Roberson if (error) 754a447cd8bSJeff Roberson return (error); 755a447cd8bSJeff Roberson 756a447cd8bSJeff Roberson error = kern_sigtimedwait(td, set, &info, NULL); 757a447cd8bSJeff Roberson if (error) 758a447cd8bSJeff Roberson return (error); 759a447cd8bSJeff Roberson 760a447cd8bSJeff Roberson error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo)); 761a447cd8bSJeff Roberson /* Repost if we got an error. */ 76218440c7fSJohn Baldwin if (error && info.si_signo) { 76318440c7fSJohn Baldwin PROC_LOCK(td->td_proc); 764c197abc4SMike Makonnen tdsignal(td, info.si_signo, SIGTARGET_TD); 76518440c7fSJohn Baldwin PROC_UNLOCK(td->td_proc); 76618440c7fSJohn Baldwin } 767a447cd8bSJeff Roberson return (error); 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; 849a447cd8bSJeff Roberson int error; 850a447cd8bSJeff Roberson int sig; 851a447cd8bSJeff Roberson int hz; 8523074d1b4SDavid Xu int i; 853a447cd8bSJeff Roberson 854a447cd8bSJeff Roberson p = td->td_proc; 855a447cd8bSJeff Roberson error = 0; 856a447cd8bSJeff Roberson sig = 0; 8573074d1b4SDavid Xu SIG_CANTMASK(waitset); 858a447cd8bSJeff Roberson 859a447cd8bSJeff Roberson PROC_LOCK(p); 860a447cd8bSJeff Roberson ps = p->p_sigacts; 8613074d1b4SDavid Xu savedmask = td->td_sigmask; 8623074d1b4SDavid Xu 8633074d1b4SDavid Xu again: 8643074d1b4SDavid Xu for (i = 1; i <= _SIG_MAXSIG; ++i) { 8653074d1b4SDavid Xu if (!SIGISMEMBER(waitset, i)) 8663074d1b4SDavid Xu continue; 8673074d1b4SDavid Xu if (SIGISMEMBER(td->td_siglist, i)) { 868418228dfSDavid Xu SIGFILLSET(td->td_sigmask); 869418228dfSDavid Xu SIG_CANTMASK(td->td_sigmask); 8703074d1b4SDavid Xu SIGDELSET(td->td_sigmask, i); 87190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 872a447cd8bSJeff Roberson sig = cursig(td); 8733074d1b4SDavid Xu i = 0; 8743074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 8753074d1b4SDavid Xu } else if (SIGISMEMBER(p->p_siglist, i)) { 8763074d1b4SDavid Xu if (p->p_flag & P_SA) { 8773074d1b4SDavid Xu p->p_flag |= P_SIGEVENT; 8783074d1b4SDavid Xu wakeup(&p->p_siglist); 8793074d1b4SDavid Xu } 8803074d1b4SDavid Xu SIGDELSET(p->p_siglist, i); 8813074d1b4SDavid Xu SIGADDSET(td->td_siglist, i); 8823074d1b4SDavid Xu SIGFILLSET(td->td_sigmask); 8833074d1b4SDavid Xu SIG_CANTMASK(td->td_sigmask); 8843074d1b4SDavid Xu SIGDELSET(td->td_sigmask, i); 8853074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 8863074d1b4SDavid Xu sig = cursig(td); 8873074d1b4SDavid Xu i = 0; 8883074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 8893074d1b4SDavid Xu } 8903074d1b4SDavid Xu if (sig) { 8913074d1b4SDavid Xu td->td_sigmask = savedmask; 8923074d1b4SDavid Xu signotify(td); 893a447cd8bSJeff Roberson goto out; 8943074d1b4SDavid Xu } 8953074d1b4SDavid Xu } 8963074d1b4SDavid Xu if (error) 8973074d1b4SDavid Xu goto out; 8983074d1b4SDavid Xu 8993074d1b4SDavid Xu td->td_sigmask = savedmask; 9003074d1b4SDavid Xu signotify(td); 9013074d1b4SDavid Xu sigset = td->td_siglist; 9023074d1b4SDavid Xu SIGSETOR(sigset, p->p_siglist); 9033074d1b4SDavid Xu SIGSETAND(sigset, waitset); 9043074d1b4SDavid Xu if (!SIGISEMPTY(sigset)) 9053074d1b4SDavid Xu goto again; 906a447cd8bSJeff Roberson 907a447cd8bSJeff Roberson /* 908a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 909a447cd8bSJeff Roberson * signals. 910a447cd8bSJeff Roberson */ 911a447cd8bSJeff Roberson if (timeout) { 912a447cd8bSJeff Roberson struct timeval tv; 913a447cd8bSJeff Roberson 914a447cd8bSJeff Roberson if (timeout->tv_nsec > 1000000000) { 915a447cd8bSJeff Roberson error = EINVAL; 916a447cd8bSJeff Roberson goto out; 917a447cd8bSJeff Roberson } 9183074d1b4SDavid Xu if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) { 9193074d1b4SDavid Xu error = EAGAIN; 9203074d1b4SDavid Xu goto out; 9213074d1b4SDavid Xu } 922a447cd8bSJeff Roberson TIMESPEC_TO_TIMEVAL(&tv, timeout); 923a447cd8bSJeff Roberson hz = tvtohz(&tv); 924a447cd8bSJeff Roberson } else 925a447cd8bSJeff Roberson hz = 0; 926a447cd8bSJeff Roberson 9273074d1b4SDavid Xu td->td_waitset = &waitset; 9283074d1b4SDavid Xu error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 9293074d1b4SDavid Xu td->td_waitset = NULL; 9303074d1b4SDavid Xu if (error == 0) /* surplus wakeup ? */ 9313074d1b4SDavid Xu error = EINTR; 9323074d1b4SDavid Xu goto again; 9339dde3bc9SDavid Xu 934a447cd8bSJeff Roberson out: 935a447cd8bSJeff Roberson if (sig) { 936a447cd8bSJeff Roberson sig_t action; 937a447cd8bSJeff Roberson 9383074d1b4SDavid Xu error = 0; 9393074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 940a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 94190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 942a447cd8bSJeff Roberson #ifdef KTRACE 943a447cd8bSJeff Roberson if (KTRPOINT(td, KTR_PSIG)) 9443074d1b4SDavid Xu ktrpsig(sig, action, &td->td_sigmask, 0); 945a447cd8bSJeff Roberson #endif 946a447cd8bSJeff Roberson _STOPEVENT(p, S_SIG, sig); 947a447cd8bSJeff Roberson 948a447cd8bSJeff Roberson SIGDELSET(td->td_siglist, sig); 949a447cd8bSJeff Roberson info->si_signo = sig; 950a447cd8bSJeff Roberson info->si_code = 0; 9513074d1b4SDavid Xu } 952a447cd8bSJeff Roberson PROC_UNLOCK(p); 953a447cd8bSJeff Roberson return (error); 954a447cd8bSJeff Roberson } 955a447cd8bSJeff Roberson 956a447cd8bSJeff Roberson /* 957a447cd8bSJeff Roberson * MPSAFE 958a447cd8bSJeff Roberson */ 959a447cd8bSJeff Roberson int 960b40ce416SJulian Elischer sigpending(td, uap) 961b40ce416SJulian Elischer struct thread *td; 962df8bae1dSRodney W. Grimes struct sigpending_args *uap; 963df8bae1dSRodney W. Grimes { 964b40ce416SJulian Elischer struct proc *p = td->td_proc; 965628d2653SJohn Baldwin sigset_t siglist; 966df8bae1dSRodney W. Grimes 967628d2653SJohn Baldwin PROC_LOCK(p); 9681d9c5696SJuli Mallett siglist = p->p_siglist; 9694093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 970628d2653SJohn Baldwin PROC_UNLOCK(p); 97148e8f774STim J. Robbins return (copyout(&siglist, uap->set, sizeof(sigset_t))); 9722c42a146SMarcel Moolenaar } 9732c42a146SMarcel Moolenaar 97431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9752c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9762c42a146SMarcel Moolenaar struct osigpending_args { 9772c42a146SMarcel Moolenaar int dummy; 9782c42a146SMarcel Moolenaar }; 9792c42a146SMarcel Moolenaar #endif 980fb99ab88SMatthew Dillon /* 981fb99ab88SMatthew Dillon * MPSAFE 982fb99ab88SMatthew Dillon */ 9832c42a146SMarcel Moolenaar int 984b40ce416SJulian Elischer osigpending(td, uap) 985b40ce416SJulian Elischer struct thread *td; 9862c42a146SMarcel Moolenaar struct osigpending_args *uap; 9872c42a146SMarcel Moolenaar { 988b40ce416SJulian Elischer struct proc *p = td->td_proc; 9894093529dSJeff Roberson sigset_t siglist; 990b40ce416SJulian Elischer 991628d2653SJohn Baldwin PROC_LOCK(p); 9924093529dSJeff Roberson siglist = p->p_siglist; 9934093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 994628d2653SJohn Baldwin PROC_UNLOCK(p); 9959d8643ecSJohn Baldwin SIG2OSIG(siglist, td->td_retval[0]); 996df8bae1dSRodney W. Grimes return (0); 997df8bae1dSRodney W. Grimes } 99831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 999df8bae1dSRodney W. Grimes 1000df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1001df8bae1dSRodney W. Grimes /* 1002df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1003df8bae1dSRodney W. Grimes */ 1004d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1005df8bae1dSRodney W. Grimes struct osigvec_args { 1006df8bae1dSRodney W. Grimes int signum; 1007df8bae1dSRodney W. Grimes struct sigvec *nsv; 1008df8bae1dSRodney W. Grimes struct sigvec *osv; 1009df8bae1dSRodney W. Grimes }; 1010d2d3e875SBruce Evans #endif 1011fb99ab88SMatthew Dillon /* 1012fb99ab88SMatthew Dillon * MPSAFE 1013fb99ab88SMatthew Dillon */ 1014df8bae1dSRodney W. Grimes /* ARGSUSED */ 101526f9a767SRodney W. Grimes int 1016b40ce416SJulian Elischer osigvec(td, uap) 1017b40ce416SJulian Elischer struct thread *td; 1018df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 1019df8bae1dSRodney W. Grimes { 1020df8bae1dSRodney W. Grimes struct sigvec vec; 10212c42a146SMarcel Moolenaar struct sigaction nsa, osa; 10222c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 10232c42a146SMarcel Moolenaar int error; 1024df8bae1dSRodney W. Grimes 10256f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 10266f841fb7SMarcel Moolenaar return (EINVAL); 10276f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 10286f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 10292c42a146SMarcel Moolenaar if (nsap) { 10306f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 10312c42a146SMarcel Moolenaar if (error) 1032df8bae1dSRodney W. Grimes return (error); 10332c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 10342c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 10352c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 10362c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1037df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 10382c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 1039df8bae1dSRodney W. Grimes #endif 1040df8bae1dSRodney W. Grimes } 10415edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 10422c42a146SMarcel Moolenaar if (osap && !error) { 10432c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 10442c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 10452c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 10462c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 10472c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 10482c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 10492c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 10502c42a146SMarcel Moolenaar #endif 10516f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 10522c42a146SMarcel Moolenaar } 10532c42a146SMarcel Moolenaar return (error); 1054df8bae1dSRodney W. Grimes } 1055df8bae1dSRodney W. Grimes 1056d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1057df8bae1dSRodney W. Grimes struct osigblock_args { 1058df8bae1dSRodney W. Grimes int mask; 1059df8bae1dSRodney W. Grimes }; 1060d2d3e875SBruce Evans #endif 1061fb99ab88SMatthew Dillon /* 1062fb99ab88SMatthew Dillon * MPSAFE 1063fb99ab88SMatthew Dillon */ 106426f9a767SRodney W. Grimes int 1065b40ce416SJulian Elischer osigblock(td, uap) 1066b40ce416SJulian Elischer register struct thread *td; 1067df8bae1dSRodney W. Grimes struct osigblock_args *uap; 1068df8bae1dSRodney W. Grimes { 1069b40ce416SJulian Elischer struct proc *p = td->td_proc; 10702c42a146SMarcel Moolenaar sigset_t set; 1071df8bae1dSRodney W. Grimes 10722c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10732c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1074628d2653SJohn Baldwin PROC_LOCK(p); 10754093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 10764093529dSJeff Roberson SIGSETOR(td->td_sigmask, set); 1077628d2653SJohn Baldwin PROC_UNLOCK(p); 1078df8bae1dSRodney W. Grimes return (0); 1079df8bae1dSRodney W. Grimes } 1080df8bae1dSRodney W. Grimes 1081d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1082df8bae1dSRodney W. Grimes struct osigsetmask_args { 1083df8bae1dSRodney W. Grimes int mask; 1084df8bae1dSRodney W. Grimes }; 1085d2d3e875SBruce Evans #endif 1086fb99ab88SMatthew Dillon /* 1087fb99ab88SMatthew Dillon * MPSAFE 1088fb99ab88SMatthew Dillon */ 108926f9a767SRodney W. Grimes int 1090b40ce416SJulian Elischer osigsetmask(td, uap) 1091b40ce416SJulian Elischer struct thread *td; 1092df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 1093df8bae1dSRodney W. Grimes { 1094b40ce416SJulian Elischer struct proc *p = td->td_proc; 10952c42a146SMarcel Moolenaar sigset_t set; 1096df8bae1dSRodney W. Grimes 10972c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10982c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1099628d2653SJohn Baldwin PROC_LOCK(p); 11004093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 11014093529dSJeff Roberson SIGSETLO(td->td_sigmask, set); 11024093529dSJeff Roberson signotify(td); 1103628d2653SJohn Baldwin PROC_UNLOCK(p); 1104df8bae1dSRodney W. Grimes return (0); 1105df8bae1dSRodney W. Grimes } 1106df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1107df8bae1dSRodney W. Grimes 1108df8bae1dSRodney W. Grimes /* 1109df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 1110df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 1111df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 1112b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 1113b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 1114b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 1115df8bae1dSRodney W. Grimes */ 1116d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1117df8bae1dSRodney W. Grimes struct sigsuspend_args { 11182c42a146SMarcel Moolenaar const sigset_t *sigmask; 1119df8bae1dSRodney W. Grimes }; 1120d2d3e875SBruce Evans #endif 1121fb99ab88SMatthew Dillon /* 1122fb99ab88SMatthew Dillon * MPSAFE 1123fb99ab88SMatthew Dillon */ 1124df8bae1dSRodney W. Grimes /* ARGSUSED */ 112526f9a767SRodney W. Grimes int 1126b40ce416SJulian Elischer sigsuspend(td, uap) 1127b40ce416SJulian Elischer struct thread *td; 1128df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 1129df8bae1dSRodney W. Grimes { 11302c42a146SMarcel Moolenaar sigset_t mask; 11312c42a146SMarcel Moolenaar int error; 11322c42a146SMarcel Moolenaar 11336f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 11342c42a146SMarcel Moolenaar if (error) 11352c42a146SMarcel Moolenaar return (error); 11368f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 11378f19eb88SIan Dowse } 11388f19eb88SIan Dowse 11398f19eb88SIan Dowse int 11408f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 11418f19eb88SIan Dowse { 11428f19eb88SIan Dowse struct proc *p = td->td_proc; 1143df8bae1dSRodney W. Grimes 1144df8bae1dSRodney W. Grimes /* 1145645682fdSLuoqi Chen * When returning from sigsuspend, we want 1146df8bae1dSRodney W. Grimes * the old mask to be restored after the 1147df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1148df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1149df8bae1dSRodney W. Grimes * to indicate this. 1150df8bae1dSRodney W. Grimes */ 1151628d2653SJohn Baldwin PROC_LOCK(p); 11524093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11535e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1154645682fdSLuoqi Chen SIG_CANTMASK(mask); 11554093529dSJeff Roberson td->td_sigmask = mask; 11564093529dSJeff Roberson signotify(td); 1157b1bf1c3aSJohn Baldwin while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 11582c42a146SMarcel Moolenaar /* void */; 1159628d2653SJohn Baldwin PROC_UNLOCK(p); 11602c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 11612c42a146SMarcel Moolenaar return (EINTR); 11622c42a146SMarcel Moolenaar } 11632c42a146SMarcel Moolenaar 116431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11652c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11662c42a146SMarcel Moolenaar struct osigsuspend_args { 11672c42a146SMarcel Moolenaar osigset_t mask; 11682c42a146SMarcel Moolenaar }; 11692c42a146SMarcel Moolenaar #endif 1170fb99ab88SMatthew Dillon /* 1171fb99ab88SMatthew Dillon * MPSAFE 1172fb99ab88SMatthew Dillon */ 11732c42a146SMarcel Moolenaar /* ARGSUSED */ 11742c42a146SMarcel Moolenaar int 1175b40ce416SJulian Elischer osigsuspend(td, uap) 1176b40ce416SJulian Elischer struct thread *td; 11772c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 11782c42a146SMarcel Moolenaar { 1179b40ce416SJulian Elischer struct proc *p = td->td_proc; 1180645682fdSLuoqi Chen sigset_t mask; 11812c42a146SMarcel Moolenaar 1182628d2653SJohn Baldwin PROC_LOCK(p); 11834093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11845e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1185645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 1186645682fdSLuoqi Chen SIG_CANTMASK(mask); 11874093529dSJeff Roberson SIGSETLO(td->td_sigmask, mask); 11884093529dSJeff Roberson signotify(td); 1189b1bf1c3aSJohn Baldwin while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1190df8bae1dSRodney W. Grimes /* void */; 1191628d2653SJohn Baldwin PROC_UNLOCK(p); 1192df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 1193df8bae1dSRodney W. Grimes return (EINTR); 1194df8bae1dSRodney W. Grimes } 119531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1198d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1199df8bae1dSRodney W. Grimes struct osigstack_args { 1200df8bae1dSRodney W. Grimes struct sigstack *nss; 1201df8bae1dSRodney W. Grimes struct sigstack *oss; 1202df8bae1dSRodney W. Grimes }; 1203d2d3e875SBruce Evans #endif 1204fb99ab88SMatthew Dillon /* 1205fb99ab88SMatthew Dillon * MPSAFE 1206fb99ab88SMatthew Dillon */ 1207df8bae1dSRodney W. Grimes /* ARGSUSED */ 120826f9a767SRodney W. Grimes int 1209b40ce416SJulian Elischer osigstack(td, uap) 1210b40ce416SJulian Elischer struct thread *td; 1211df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 1212df8bae1dSRodney W. Grimes { 1213b40ce416SJulian Elischer struct proc *p = td->td_proc; 12145afe0c99SJohn Baldwin struct sigstack nss, oss; 1215fb99ab88SMatthew Dillon int error = 0; 1216fb99ab88SMatthew Dillon 1217d034d459SMarcel Moolenaar if (uap->nss != NULL) { 12185afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 12195afe0c99SJohn Baldwin if (error) 12205afe0c99SJohn Baldwin return (error); 1221df8bae1dSRodney W. Grimes } 12225afe0c99SJohn Baldwin PROC_LOCK(p); 12235afe0c99SJohn Baldwin oss.ss_sp = p->p_sigstk.ss_sp; 12245afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 12255afe0c99SJohn Baldwin if (uap->nss != NULL) { 12265afe0c99SJohn Baldwin p->p_sigstk.ss_sp = nss.ss_sp; 12275afe0c99SJohn Baldwin p->p_sigstk.ss_size = 0; 12285afe0c99SJohn Baldwin p->p_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 12295afe0c99SJohn Baldwin p->p_flag |= P_ALTSTACK; 12305afe0c99SJohn Baldwin } 12315afe0c99SJohn Baldwin PROC_UNLOCK(p); 12325afe0c99SJohn Baldwin if (uap->oss != NULL) 12335afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 12345afe0c99SJohn Baldwin 1235fb99ab88SMatthew Dillon return (error); 1236df8bae1dSRodney W. Grimes } 1237df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1238df8bae1dSRodney W. Grimes 1239d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1240df8bae1dSRodney W. Grimes struct sigaltstack_args { 12412c42a146SMarcel Moolenaar stack_t *ss; 12422c42a146SMarcel Moolenaar stack_t *oss; 1243df8bae1dSRodney W. Grimes }; 1244d2d3e875SBruce Evans #endif 1245fb99ab88SMatthew Dillon /* 1246fb99ab88SMatthew Dillon * MPSAFE 1247fb99ab88SMatthew Dillon */ 1248df8bae1dSRodney W. Grimes /* ARGSUSED */ 124926f9a767SRodney W. Grimes int 1250b40ce416SJulian Elischer sigaltstack(td, uap) 1251b40ce416SJulian Elischer struct thread *td; 1252df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1253df8bae1dSRodney W. Grimes { 12548f19eb88SIan Dowse stack_t ss, oss; 12558f19eb88SIan Dowse int error; 12568f19eb88SIan Dowse 12578f19eb88SIan Dowse if (uap->ss != NULL) { 12588f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 12598f19eb88SIan Dowse if (error) 12608f19eb88SIan Dowse return (error); 12618f19eb88SIan Dowse } 12628f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 12638f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 12648f19eb88SIan Dowse if (error) 12658f19eb88SIan Dowse return (error); 12668f19eb88SIan Dowse if (uap->oss != NULL) 12678f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 12688f19eb88SIan Dowse return (error); 12698f19eb88SIan Dowse } 12708f19eb88SIan Dowse 12718f19eb88SIan Dowse int 12728f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 12738f19eb88SIan Dowse { 1274b40ce416SJulian Elischer struct proc *p = td->td_proc; 1275fb99ab88SMatthew Dillon int oonstack; 1276fb99ab88SMatthew Dillon 127706ce69a7SDavid Xu PROC_LOCK(p); 1278b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1279d034d459SMarcel Moolenaar 12808f19eb88SIan Dowse if (oss != NULL) { 12818f19eb88SIan Dowse *oss = p->p_sigstk; 12828f19eb88SIan Dowse oss->ss_flags = (p->p_flag & P_ALTSTACK) 1283d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1284df8bae1dSRodney W. Grimes } 1285d034d459SMarcel Moolenaar 12868f19eb88SIan Dowse if (ss != NULL) { 1287fb99ab88SMatthew Dillon if (oonstack) { 1288cf60731bSJohn Baldwin PROC_UNLOCK(p); 1289cf60731bSJohn Baldwin return (EPERM); 1290fb99ab88SMatthew Dillon } 12918f19eb88SIan Dowse if ((ss->ss_flags & ~SS_DISABLE) != 0) { 1292cf60731bSJohn Baldwin PROC_UNLOCK(p); 1293cf60731bSJohn Baldwin return (EINVAL); 1294fb99ab88SMatthew Dillon } 12958f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 12968f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1297cf60731bSJohn Baldwin PROC_UNLOCK(p); 1298cf60731bSJohn Baldwin return (ENOMEM); 1299fb99ab88SMatthew Dillon } 13008f19eb88SIan Dowse p->p_sigstk = *ss; 1301d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 1302628d2653SJohn Baldwin } else { 1303d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 1304628d2653SJohn Baldwin } 1305d034d459SMarcel Moolenaar } 130606ce69a7SDavid Xu PROC_UNLOCK(p); 1307cf60731bSJohn Baldwin return (0); 1308df8bae1dSRodney W. Grimes } 1309df8bae1dSRodney W. Grimes 1310d93f860cSPoul-Henning Kamp /* 1311d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1312d93f860cSPoul-Henning Kamp * cp is calling process. 1313d93f860cSPoul-Henning Kamp */ 131437c84183SPoul-Henning Kamp static int 13159c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all) 13169c1ab3e0SJohn Baldwin register struct thread *td; 13172c42a146SMarcel Moolenaar int sig, pgid, all; 1318d93f860cSPoul-Henning Kamp { 1319d93f860cSPoul-Henning Kamp register struct proc *p; 1320d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1321d93f860cSPoul-Henning Kamp int nfound = 0; 1322d93f860cSPoul-Henning Kamp 1323553629ebSJake Burkholder if (all) { 1324d93f860cSPoul-Henning Kamp /* 1325d93f860cSPoul-Henning Kamp * broadcast 1326d93f860cSPoul-Henning Kamp */ 13271005a129SJohn Baldwin sx_slock(&allproc_lock); 13282e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1329628d2653SJohn Baldwin PROC_LOCK(p); 13309c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 13319c1ab3e0SJohn Baldwin p == td->td_proc) { 1332628d2653SJohn Baldwin PROC_UNLOCK(p); 1333628d2653SJohn Baldwin continue; 1334628d2653SJohn Baldwin } 1335f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1336d93f860cSPoul-Henning Kamp nfound++; 133733a9ed9dSJohn Baldwin if (sig) 13382c42a146SMarcel Moolenaar psignal(p, sig); 1339628d2653SJohn Baldwin } 134033a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1341d93f860cSPoul-Henning Kamp } 13421005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1343553629ebSJake Burkholder } else { 1344ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1345f591779bSSeigo Tanimura if (pgid == 0) { 1346d93f860cSPoul-Henning Kamp /* 1347d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1348d93f860cSPoul-Henning Kamp */ 13499c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1350f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1351f591779bSSeigo Tanimura } else { 1352d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1353f591779bSSeigo Tanimura if (pgrp == NULL) { 1354ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1355d93f860cSPoul-Henning Kamp return (ESRCH); 1356d93f860cSPoul-Henning Kamp } 1357f591779bSSeigo Tanimura } 1358ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 13592e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1360628d2653SJohn Baldwin PROC_LOCK(p); 1361628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1362628d2653SJohn Baldwin PROC_UNLOCK(p); 1363628d2653SJohn Baldwin continue; 1364628d2653SJohn Baldwin } 1365e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 136633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1367628d2653SJohn Baldwin continue; 1368628d2653SJohn Baldwin } 1369f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1370d93f860cSPoul-Henning Kamp nfound++; 137133a9ed9dSJohn Baldwin if (sig) 13722c42a146SMarcel Moolenaar psignal(p, sig); 1373628d2653SJohn Baldwin } 137433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1375d93f860cSPoul-Henning Kamp } 1376f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1377d93f860cSPoul-Henning Kamp } 1378d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1379d93f860cSPoul-Henning Kamp } 1380d93f860cSPoul-Henning Kamp 1381d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1382df8bae1dSRodney W. Grimes struct kill_args { 1383df8bae1dSRodney W. Grimes int pid; 1384df8bae1dSRodney W. Grimes int signum; 1385df8bae1dSRodney W. Grimes }; 1386d2d3e875SBruce Evans #endif 1387fb99ab88SMatthew Dillon /* 1388fb99ab88SMatthew Dillon * MPSAFE 1389fb99ab88SMatthew Dillon */ 1390df8bae1dSRodney W. Grimes /* ARGSUSED */ 139126f9a767SRodney W. Grimes int 1392b40ce416SJulian Elischer kill(td, uap) 1393b40ce416SJulian Elischer register struct thread *td; 1394df8bae1dSRodney W. Grimes register struct kill_args *uap; 1395df8bae1dSRodney W. Grimes { 1396df8bae1dSRodney W. Grimes register struct proc *p; 139790af4afaSJohn Baldwin int error; 1398df8bae1dSRodney W. Grimes 13996c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1400df8bae1dSRodney W. Grimes return (EINVAL); 1401fb99ab88SMatthew Dillon 1402df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1403df8bae1dSRodney W. Grimes /* kill single process */ 140490af4afaSJohn Baldwin if ((p = pfind(uap->pid)) == NULL) 140590af4afaSJohn Baldwin return (ESRCH); 140690af4afaSJohn Baldwin error = p_cansignal(td, p, uap->signum); 140790af4afaSJohn Baldwin if (error == 0 && uap->signum) 1408df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1409628d2653SJohn Baldwin PROC_UNLOCK(p); 141090af4afaSJohn Baldwin return (error); 1411df8bae1dSRodney W. Grimes } 1412df8bae1dSRodney W. Grimes switch (uap->pid) { 1413df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 141490af4afaSJohn Baldwin return (killpg1(td, uap->signum, 0, 1)); 1415df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 141690af4afaSJohn Baldwin return (killpg1(td, uap->signum, 0, 0)); 1417df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 141890af4afaSJohn Baldwin return (killpg1(td, uap->signum, -uap->pid, 0)); 1419df8bae1dSRodney W. Grimes } 142090af4afaSJohn Baldwin /* NOTREACHED */ 1421df8bae1dSRodney W. Grimes } 1422df8bae1dSRodney W. Grimes 1423df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1424d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1425df8bae1dSRodney W. Grimes struct okillpg_args { 1426df8bae1dSRodney W. Grimes int pgid; 1427df8bae1dSRodney W. Grimes int signum; 1428df8bae1dSRodney W. Grimes }; 1429d2d3e875SBruce Evans #endif 1430fb99ab88SMatthew Dillon /* 1431fb99ab88SMatthew Dillon * MPSAFE 1432fb99ab88SMatthew Dillon */ 1433df8bae1dSRodney W. Grimes /* ARGSUSED */ 143426f9a767SRodney W. Grimes int 1435b40ce416SJulian Elischer okillpg(td, uap) 1436b40ce416SJulian Elischer struct thread *td; 1437df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1438df8bae1dSRodney W. Grimes { 1439df8bae1dSRodney W. Grimes 14406c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1441df8bae1dSRodney W. Grimes return (EINVAL); 144290af4afaSJohn Baldwin return (killpg1(td, uap->signum, uap->pgid, 0)); 1443df8bae1dSRodney W. Grimes } 1444df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1445df8bae1dSRodney W. Grimes 1446df8bae1dSRodney W. Grimes /* 1447df8bae1dSRodney W. Grimes * Send a signal to a process group. 1448df8bae1dSRodney W. Grimes */ 1449df8bae1dSRodney W. Grimes void 14502c42a146SMarcel Moolenaar gsignal(pgid, sig) 14512c42a146SMarcel Moolenaar int pgid, sig; 1452df8bae1dSRodney W. Grimes { 1453df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1454df8bae1dSRodney W. Grimes 1455f591779bSSeigo Tanimura if (pgid != 0) { 1456ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1457f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1458ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1459f591779bSSeigo Tanimura if (pgrp != NULL) { 14602c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1461f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1462f591779bSSeigo Tanimura } 1463f591779bSSeigo Tanimura } 1464df8bae1dSRodney W. Grimes } 1465df8bae1dSRodney W. Grimes 1466df8bae1dSRodney W. Grimes /* 1467df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1468df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1469df8bae1dSRodney W. Grimes */ 1470df8bae1dSRodney W. Grimes void 14712c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1472df8bae1dSRodney W. Grimes struct pgrp *pgrp; 14732c42a146SMarcel Moolenaar int sig, checkctty; 1474df8bae1dSRodney W. Grimes { 1475df8bae1dSRodney W. Grimes register struct proc *p; 1476df8bae1dSRodney W. Grimes 1477628d2653SJohn Baldwin if (pgrp) { 1478f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1479628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1480628d2653SJohn Baldwin PROC_LOCK(p); 1481df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 14822c42a146SMarcel Moolenaar psignal(p, sig); 1483628d2653SJohn Baldwin PROC_UNLOCK(p); 1484628d2653SJohn Baldwin } 1485628d2653SJohn Baldwin } 1486df8bae1dSRodney W. Grimes } 1487df8bae1dSRodney W. Grimes 1488df8bae1dSRodney W. Grimes /* 14891bf4700bSJeff Roberson * Send a signal caused by a trap to the current thread. 1490df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1491df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1492356861dbSMatthew Dillon * 1493356861dbSMatthew Dillon * MPSAFE 1494df8bae1dSRodney W. Grimes */ 1495df8bae1dSRodney W. Grimes void 14961bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code) 1497df8bae1dSRodney W. Grimes { 14981bf4700bSJeff Roberson struct sigacts *ps; 14991bf4700bSJeff Roberson struct proc *p; 15009dde3bc9SDavid Xu siginfo_t siginfo; 15019dde3bc9SDavid Xu int error; 15021bf4700bSJeff Roberson 15031bf4700bSJeff Roberson p = td->td_proc; 15049dde3bc9SDavid Xu if (td->td_flags & TDF_SA) { 15059dde3bc9SDavid Xu thread_user_enter(p, td); 1506628d2653SJohn Baldwin PROC_LOCK(p); 15079dde3bc9SDavid Xu if (td->td_mailbox) { 15089dde3bc9SDavid Xu SIGDELSET(td->td_sigmask, sig); 15099dde3bc9SDavid Xu mtx_lock_spin(&sched_lock); 15109dde3bc9SDavid Xu /* 15119dde3bc9SDavid Xu * Force scheduling an upcall, so UTS has chance to 15129dde3bc9SDavid Xu * process the signal before thread runs again in 15139dde3bc9SDavid Xu * userland. 15149dde3bc9SDavid Xu */ 15159dde3bc9SDavid Xu if (td->td_upcall) 15169dde3bc9SDavid Xu td->td_upcall->ku_flags |= KUF_DOUPCALL; 15179dde3bc9SDavid Xu mtx_unlock_spin(&sched_lock); 15189dde3bc9SDavid Xu } 15199dde3bc9SDavid 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 15329dde3bc9SDavid Xu if (!(td->td_flags & TDF_SA)) 15339dde3bc9SDavid Xu (*p->p_sysent->sv_sendsig)( 15349dde3bc9SDavid Xu ps->ps_sigact[_SIG_IDX(sig)], sig, 15354093529dSJeff Roberson &td->td_sigmask, code); 15369dde3bc9SDavid Xu else { 15374b7d5d84SDavid Xu cpu_thread_siginfo(sig, code, &siginfo); 15389dde3bc9SDavid Xu mtx_unlock(&ps->ps_mtx); 15399dde3bc9SDavid Xu PROC_UNLOCK(p); 15409dde3bc9SDavid Xu error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 15419dde3bc9SDavid Xu sizeof(siginfo)); 15429dde3bc9SDavid Xu PROC_LOCK(p); 15439dde3bc9SDavid Xu if (error) 15449dde3bc9SDavid Xu sigexit(td, SIGILL); 15459dde3bc9SDavid Xu SIGADDSET(td->td_sigmask, sig); 15469dde3bc9SDavid Xu mtx_lock(&ps->ps_mtx); 15479dde3bc9SDavid Xu } 15484093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15492c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15504093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 15512c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1552289ccde0SPeter Wemm /* 15538f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1554289ccde0SPeter Wemm */ 155590af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 15562c42a146SMarcel Moolenaar if (sig != SIGCONT && 15572c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 155890af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 15592c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1560dedc04feSPeter Wemm } 156190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15626f841fb7SMarcel Moolenaar } else { 156390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15646626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 15652c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 1566c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_TD); 1567df8bae1dSRodney W. Grimes } 1568628d2653SJohn Baldwin PROC_UNLOCK(p); 1569df8bae1dSRodney W. Grimes } 1570df8bae1dSRodney W. Grimes 15714093529dSJeff Roberson static struct thread * 15724093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 15734093529dSJeff Roberson { 15743074d1b4SDavid Xu struct thread *td, *signal_td; 15754093529dSJeff Roberson 15764093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 15774093529dSJeff Roberson 15784093529dSJeff Roberson /* 15793074d1b4SDavid Xu * First find a thread in sigwait state and signal belongs to 15803074d1b4SDavid Xu * its wait set. POSIX's arguments is that speed of delivering signal 15813074d1b4SDavid Xu * to sigwait thread is faster than delivering signal to user stack. 15823074d1b4SDavid Xu * If we can not find sigwait thread, then find the first thread in 15833074d1b4SDavid Xu * the proc that doesn't have this signal masked, an exception is 15843074d1b4SDavid Xu * if current thread is sending signal to its process, and it does not 15853074d1b4SDavid Xu * mask the signal, it should get the signal, this is another fast 15863074d1b4SDavid Xu * way to deliver signal. 15874093529dSJeff Roberson */ 15883074d1b4SDavid Xu signal_td = NULL; 15893074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 15903074d1b4SDavid Xu if (td->td_waitset != NULL && 15913074d1b4SDavid Xu SIGISMEMBER(*(td->td_waitset), sig)) 15924093529dSJeff Roberson return (td); 15933074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 15943074d1b4SDavid Xu if (td == curthread) 15953074d1b4SDavid Xu signal_td = curthread; 15963074d1b4SDavid Xu else if (signal_td == NULL) 15973074d1b4SDavid Xu signal_td = td; 15983074d1b4SDavid Xu } 15993074d1b4SDavid Xu } 16003074d1b4SDavid Xu if (signal_td == NULL) 16013074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 16023074d1b4SDavid Xu return (signal_td); 16034093529dSJeff Roberson } 16044093529dSJeff Roberson 1605df8bae1dSRodney W. Grimes /* 1606df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1607df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1608df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1609df8bae1dSRodney W. Grimes * 1610df8bae1dSRodney W. Grimes * Exceptions: 1611df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1612df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1613df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1614df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1615df8bae1dSRodney W. Grimes * 1616df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 161790af4afaSJohn Baldwin * 161890af4afaSJohn Baldwin * MPSAFE 1619df8bae1dSRodney W. Grimes */ 1620df8bae1dSRodney W. Grimes void 16214093529dSJeff Roberson psignal(struct proc *p, int sig) 1622df8bae1dSRodney W. Grimes { 1623b40ce416SJulian Elischer struct thread *td; 16244093529dSJeff Roberson int prop; 16254093529dSJeff Roberson 16264093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 16274093529dSJeff Roberson prop = sigprop(sig); 16284093529dSJeff Roberson 16294093529dSJeff Roberson /* 16304093529dSJeff Roberson * Find a thread to deliver the signal to. 16314093529dSJeff Roberson */ 16324093529dSJeff Roberson td = sigtd(p, sig, prop); 16334093529dSJeff Roberson 1634c197abc4SMike Makonnen tdsignal(td, sig, SIGTARGET_P); 16354093529dSJeff Roberson } 16364093529dSJeff Roberson 163790af4afaSJohn Baldwin /* 163890af4afaSJohn Baldwin * MPSAFE 163990af4afaSJohn Baldwin */ 16404093529dSJeff Roberson void 1641c197abc4SMike Makonnen tdsignal(struct thread *td, int sig, sigtarget_t target) 16424093529dSJeff Roberson { 16439dde3bc9SDavid Xu sigset_t saved; 16449dde3bc9SDavid Xu struct proc *p = td->td_proc; 16459dde3bc9SDavid Xu 16469dde3bc9SDavid Xu if (p->p_flag & P_SA) 16479dde3bc9SDavid Xu saved = p->p_siglist; 1648c197abc4SMike Makonnen do_tdsignal(td, sig, target); 16499dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 16509dde3bc9SDavid Xu if (SIGSETEQ(saved, p->p_siglist)) 16519dde3bc9SDavid Xu return; 16529dde3bc9SDavid Xu else { 16539dde3bc9SDavid Xu /* pending set changed */ 16549dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 16559dde3bc9SDavid Xu wakeup(&p->p_siglist); 16569dde3bc9SDavid Xu } 16579dde3bc9SDavid Xu } 16589dde3bc9SDavid Xu } 16599dde3bc9SDavid Xu 16609dde3bc9SDavid Xu static void 1661c197abc4SMike Makonnen do_tdsignal(struct thread *td, int sig, sigtarget_t target) 16629dde3bc9SDavid Xu { 16634093529dSJeff Roberson struct proc *p; 16644093529dSJeff Roberson register sig_t action; 16654093529dSJeff Roberson sigset_t *siglist; 16664093529dSJeff Roberson struct thread *td0; 1667e602ba25SJulian Elischer register int prop; 166890af4afaSJohn Baldwin struct sigacts *ps; 1669df8bae1dSRodney W. Grimes 16702899d606SDag-Erling Smørgrav KASSERT(_SIG_VALID(sig), 16714093529dSJeff Roberson ("tdsignal(): invalid signal %d\n", sig)); 16724093529dSJeff Roberson 16734093529dSJeff Roberson p = td->td_proc; 167490af4afaSJohn Baldwin ps = p->p_sigacts; 16752c42a146SMarcel Moolenaar 1676628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1677cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1678cb679c38SJonathan Lemon 16792c42a146SMarcel Moolenaar prop = sigprop(sig); 16804093529dSJeff Roberson 16814093529dSJeff Roberson /* 168214b5ae1aSMike Makonnen * If the signal is blocked and not destined for this thread, then 168314b5ae1aSMike Makonnen * assign it to the process so that we can find it later in the first 168414b5ae1aSMike Makonnen * thread that unblocks it. Otherwise, assign it to this thread now. 16854093529dSJeff Roberson */ 16863074d1b4SDavid Xu if (target == SIGTARGET_TD) { 16873074d1b4SDavid Xu siglist = &td->td_siglist; 16883074d1b4SDavid Xu } else { 16893074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) 16903074d1b4SDavid Xu siglist = &td->td_siglist; 16913074d1b4SDavid Xu else if (td->td_waitset != NULL && 16923074d1b4SDavid Xu SIGISMEMBER(*(td->td_waitset), sig)) 16933074d1b4SDavid Xu siglist = &td->td_siglist; 16943074d1b4SDavid Xu else 16953074d1b4SDavid Xu siglist = &p->p_siglist; 16963074d1b4SDavid Xu } 16974093529dSJeff Roberson 1698df8bae1dSRodney W. Grimes /* 16992a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 17002a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 17012a024a2bSSean Eric Fagan * a chance, as well. 1702df8bae1dSRodney W. Grimes */ 1703b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1704df8bae1dSRodney W. Grimes action = SIG_DFL; 1705b40ce416SJulian Elischer } else { 1706df8bae1dSRodney W. Grimes /* 1707df8bae1dSRodney W. Grimes * If the signal is being ignored, 1708df8bae1dSRodney W. Grimes * then we forget about it immediately. 170990af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 1710df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1711df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1712df8bae1dSRodney W. Grimes */ 171390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 171490af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) || 171590af4afaSJohn Baldwin (p->p_flag & P_WEXIT)) { 171690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1717df8bae1dSRodney W. Grimes return; 171890af4afaSJohn Baldwin } 17193074d1b4SDavid Xu if (((td->td_waitset == NULL) && 17203074d1b4SDavid Xu SIGISMEMBER(td->td_sigmask, sig)) || 17213074d1b4SDavid Xu ((td->td_waitset != NULL) && 17223074d1b4SDavid Xu SIGISMEMBER(td->td_sigmask, sig) && 17233074d1b4SDavid Xu !SIGISMEMBER(*(td->td_waitset), sig))) 1724df8bae1dSRodney W. Grimes action = SIG_HOLD; 172590af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1726df8bae1dSRodney W. Grimes action = SIG_CATCH; 1727df8bae1dSRodney W. Grimes else 1728df8bae1dSRodney W. Grimes action = SIG_DFL; 172990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1730df8bae1dSRodney W. Grimes } 1731df8bae1dSRodney W. Grimes 17324093529dSJeff Roberson if (prop & SA_CONT) { 17331d9c5696SJuli Mallett SIG_STOPSIGMASK(p->p_siglist); 17344093529dSJeff Roberson /* 17354093529dSJeff Roberson * XXX Should investigate leaving STOP and CONT sigs only in 17364093529dSJeff Roberson * the proc's siglist. 17374093529dSJeff Roberson */ 17384093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17394093529dSJeff Roberson SIG_STOPSIGMASK(td0->td_siglist); 17404093529dSJeff Roberson } 1741df8bae1dSRodney W. Grimes 1742df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1743df8bae1dSRodney W. Grimes /* 1744df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1745df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1746df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1747df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1748df8bae1dSRodney W. Grimes */ 1749e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1750e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1751e602ba25SJulian Elischer (action == SIG_DFL)) 1752df8bae1dSRodney W. Grimes return; 17531d9c5696SJuli Mallett SIG_CONTSIGMASK(p->p_siglist); 17544093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17554093529dSJeff Roberson SIG_CONTSIGMASK(td0->td_siglist); 17566933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1757df8bae1dSRodney W. Grimes } 17583074d1b4SDavid Xu 17594093529dSJeff Roberson SIGADDSET(*siglist, sig); 17604093529dSJeff Roberson signotify(td); /* uses schedlock */ 17613074d1b4SDavid Xu if (siglist == &td->td_siglist && (td->td_waitset != NULL) && 17623074d1b4SDavid Xu action != SIG_HOLD) { 17633074d1b4SDavid Xu td->td_waitset = NULL; 17643074d1b4SDavid Xu } 17653074d1b4SDavid Xu 17665312b1c7SDavid Xu /* 17675312b1c7SDavid Xu * Defer further processing for signals which are held, 17685312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 17695312b1c7SDavid Xu */ 17705312b1c7SDavid Xu if (action == SIG_HOLD && 17715312b1c7SDavid Xu !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 17725312b1c7SDavid Xu return; 1773df8bae1dSRodney W. Grimes /* 1774e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1775e602ba25SJulian Elischer * component. Most processing occurs when the process next 1776e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1777e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1778e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1779e602ba25SJulian Elischer * We try do the per-process part here. 1780df8bae1dSRodney W. Grimes */ 1781e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1782e602ba25SJulian Elischer /* 1783e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1784e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1785e602ba25SJulian Elischer */ 1786e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1787e602ba25SJulian Elischer /* 1788e602ba25SJulian Elischer * The traced process is already stopped, 1789e602ba25SJulian Elischer * so no further action is necessary. 1790e602ba25SJulian Elischer * No signal can restart us. 1791e602ba25SJulian Elischer */ 1792e602ba25SJulian Elischer goto out; 17931c32c37cSJohn Baldwin } 1794b40ce416SJulian Elischer 1795e602ba25SJulian Elischer if (sig == SIGKILL) { 1796df8bae1dSRodney W. Grimes /* 1797e602ba25SJulian Elischer * SIGKILL sets process running. 1798e602ba25SJulian Elischer * It will die elsewhere. 1799e602ba25SJulian Elischer * All threads must be restarted. 1800df8bae1dSRodney W. Grimes */ 1801e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED; 1802e602ba25SJulian Elischer goto runfast; 1803e602ba25SJulian Elischer } 1804e602ba25SJulian Elischer 1805e602ba25SJulian Elischer if (prop & SA_CONT) { 1806e602ba25SJulian Elischer /* 1807e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 18084093529dSJeff Roberson * process but don't leave the signal in siglist as 18091d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 1810e602ba25SJulian Elischer * continue the process and leave the signal in 18114093529dSJeff Roberson * siglist. If the process catches SIGCONT, let it 1812e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1813e602ba25SJulian Elischer * an event, it goes back to run state. 1814e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1815e602ba25SJulian Elischer */ 18161279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 18176933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1818e602ba25SJulian Elischer if (action == SIG_DFL) { 18194093529dSJeff Roberson SIGDELSET(*siglist, sig); 1820e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1821e602ba25SJulian Elischer /* 1822e602ba25SJulian Elischer * The process wants to catch it so it needs 1823e602ba25SJulian Elischer * to run at least one thread, but which one? 1824e602ba25SJulian Elischer * It would seem that the answer would be to 1825e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1826e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1827e602ba25SJulian Elischer * process, we need to make sure that the 1828e602ba25SJulian Elischer * single thread is runnable asap. 1829e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1830e602ba25SJulian Elischer */ 1831e602ba25SJulian Elischer goto runfast; 1832e602ba25SJulian Elischer } 1833e602ba25SJulian Elischer /* 1834e602ba25SJulian Elischer * The signal is not ignored or caught. 1835e602ba25SJulian Elischer */ 1836e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 183704774f23SJulian Elischer thread_unsuspend(p); 1838e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1839e602ba25SJulian Elischer goto out; 1840e602ba25SJulian Elischer } 1841e602ba25SJulian Elischer 1842e602ba25SJulian Elischer if (prop & SA_STOP) { 1843e602ba25SJulian Elischer /* 1844e602ba25SJulian Elischer * Already stopped, don't need to stop again 1845e602ba25SJulian Elischer * (If we did the shell could get confused). 184604774f23SJulian Elischer * Just make sure the signal STOP bit set. 1847e602ba25SJulian Elischer */ 18481279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 18494093529dSJeff Roberson SIGDELSET(*siglist, sig); 1850e602ba25SJulian Elischer goto out; 1851e602ba25SJulian Elischer } 1852e602ba25SJulian Elischer 1853e602ba25SJulian Elischer /* 1854e602ba25SJulian Elischer * All other kinds of signals: 1855e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1856e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1857e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 185804774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1859e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1860e602ba25SJulian Elischer */ 1861e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 186271fad9fdSJulian Elischer if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) { 1863e602ba25SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 186471fad9fdSJulian Elischer cv_abort(td); 1865e602ba25SJulian Elischer else 186671fad9fdSJulian Elischer abortsleep(td); 1867e602ba25SJulian Elischer } 1868e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1869df8bae1dSRodney W. Grimes goto out; 1870df8bae1dSRodney W. Grimes /* 1871e602ba25SJulian Elischer * XXXKSE What about threads that are waiting on mutexes? 1872e602ba25SJulian Elischer * Shouldn't they abort too? 187304774f23SJulian Elischer * No, hopefully mutexes are short lived.. They'll 187404774f23SJulian Elischer * eventually hit thread_suspend_check(). 1875df8bae1dSRodney W. Grimes */ 1876e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 187735c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 187835c32a76SDavid Xu !(prop & SA_STOP)) { 1879721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 18804093529dSJeff Roberson tdsigwakeup(td, sig, action); 1881721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1882df8bae1dSRodney W. Grimes goto out; 188304774f23SJulian Elischer } 188435c32a76SDavid Xu if (prop & SA_STOP) { 188535c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 188635c32a76SDavid Xu goto out; 1887e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 1888e574e444SDavid Xu p->p_xstat = sig; 188935c32a76SDavid Xu mtx_lock_spin(&sched_lock); 18904093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) { 18914093529dSJeff Roberson if (TD_IS_SLEEPING(td0) && 1892062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 1893062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 18944093529dSJeff Roberson thread_suspend_one(td0); 1895062cf543SDavid Xu } else if (td != td0) { 1896062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 1897062cf543SDavid Xu } 189835c32a76SDavid Xu } 1899e574e444SDavid Xu thread_stopped(p); 19004093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 1901e574e444SDavid Xu SIGDELSET(p->p_siglist, p->p_xstat); 19024093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 19034093529dSJeff Roberson SIGDELSET(td0->td_siglist, p->p_xstat); 19044093529dSJeff Roberson } 190535c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 190635c32a76SDavid Xu goto out; 190735c32a76SDavid Xu } 1908721e5910SJulian Elischer else 1909df8bae1dSRodney W. Grimes goto runfast; 1910df8bae1dSRodney W. Grimes /* NOTREACHED */ 1911e602ba25SJulian Elischer } else { 1912e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 19134093529dSJeff Roberson SIGDELSET(*siglist, sig); 1914e602ba25SJulian Elischer goto out; 1915e602ba25SJulian Elischer } 1916e602ba25SJulian Elischer 1917b40ce416SJulian Elischer /* 1918e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1919e602ba25SJulian Elischer * running threads. 1920b40ce416SJulian Elischer */ 1921e602ba25SJulian Elischer 1922e602ba25SJulian Elischer runfast: 1923aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 19244093529dSJeff Roberson tdsigwakeup(td, sig, action); 1925e602ba25SJulian Elischer thread_unsuspend(p); 1926e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1927e602ba25SJulian Elischer out: 1928e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1929e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1930e602ba25SJulian Elischer } 1931e602ba25SJulian Elischer 1932e602ba25SJulian Elischer /* 1933e602ba25SJulian Elischer * The force of a signal has been directed against a single 1934e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1935e602ba25SJulian Elischer * out of any sleep it may be in etc. 1936e602ba25SJulian Elischer */ 1937e602ba25SJulian Elischer static void 19384093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action) 1939e602ba25SJulian Elischer { 1940e602ba25SJulian Elischer struct proc *p = td->td_proc; 1941e602ba25SJulian Elischer register int prop; 1942e602ba25SJulian Elischer 19438b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1944aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1945e602ba25SJulian Elischer prop = sigprop(sig); 1946e602ba25SJulian Elischer /* 1947aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1948e602ba25SJulian Elischer * killed in this lifetime. 1949e602ba25SJulian Elischer */ 1950e602ba25SJulian Elischer if ((action == SIG_DFL) && (prop & SA_KILL)) { 1951e602ba25SJulian Elischer if (td->td_priority > PUSER) { 1952e602ba25SJulian Elischer td->td_priority = PUSER; 1953b40ce416SJulian Elischer } 1954b40ce416SJulian Elischer } 195571fad9fdSJulian Elischer if (TD_IS_SLEEPING(td)) { 1956e602ba25SJulian Elischer /* 1957e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1958e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1959e602ba25SJulian Elischer * be noticed when the process returns through 1960e602ba25SJulian Elischer * trap() or syscall(). 1961e602ba25SJulian Elischer */ 1962e602ba25SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 1963aa0fa334SJulian Elischer return; 1964e602ba25SJulian Elischer } 1965e602ba25SJulian Elischer /* 1966e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1967e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1968e602ba25SJulian Elischer * for its parent. 1969e602ba25SJulian Elischer */ 1970e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1971e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1972aa0fa334SJulian Elischer } else { 1973aa0fa334SJulian Elischer 1974df8bae1dSRodney W. Grimes /* 1975e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1976e602ba25SJulian Elischer * asleep, we are finished; the process should not 1977e602ba25SJulian Elischer * be awakened. 1978df8bae1dSRodney W. Grimes */ 1979e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 19801d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 19814093529dSJeff Roberson /* 19824093529dSJeff Roberson * It may be on either list in this state. 19834093529dSJeff Roberson * Remove from both for now. 19844093529dSJeff Roberson */ 19854093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 1986aa0fa334SJulian Elischer return; 1987df8bae1dSRodney W. Grimes } 1988df8bae1dSRodney W. Grimes 1989aa0fa334SJulian Elischer /* 1990aa0fa334SJulian Elischer * Raise priority to at least PUSER. 1991aa0fa334SJulian Elischer */ 1992aa0fa334SJulian Elischer if (td->td_priority > PUSER) { 1993aa0fa334SJulian Elischer td->td_priority = PUSER; 1994aa0fa334SJulian Elischer } 1995aa0fa334SJulian Elischer } 199671fad9fdSJulian Elischer if (td->td_flags & TDF_CVWAITQ) 199771fad9fdSJulian Elischer cv_abort(td); 199871fad9fdSJulian Elischer else 199971fad9fdSJulian Elischer abortsleep(td); 2000aa0fa334SJulian Elischer } 2001aa0fa334SJulian Elischer #ifdef SMP 2002aa0fa334SJulian Elischer else { 2003df8bae1dSRodney W. Grimes /* 2004e602ba25SJulian Elischer * Other states do nothing with the signal immediatly, 2005df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2006df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2007df8bae1dSRodney W. Grimes */ 200871fad9fdSJulian Elischer if (TD_IS_RUNNING(td) && td != curthread) { 2009e602ba25SJulian Elischer forward_signal(td); 2010aa0fa334SJulian Elischer } 20110ac3b636SAndrew Gallatin } 20123163861cSTor Egge #endif 20136caa8a15SJohn Baldwin } 2014df8bae1dSRodney W. Grimes 2015df8bae1dSRodney W. Grimes /* 2016df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2017df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2018df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2019df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2020df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2021628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2022df8bae1dSRodney W. Grimes * sequence is 2023df8bae1dSRodney W. Grimes * 2024e602ba25SJulian Elischer * while (sig = cursig(curthread)) 20252c42a146SMarcel Moolenaar * postsig(sig); 2026df8bae1dSRodney W. Grimes */ 20276711f10fSJohn Baldwin static int 2028e602ba25SJulian Elischer issignal(td) 2029e602ba25SJulian Elischer struct thread *td; 2030df8bae1dSRodney W. Grimes { 2031e602ba25SJulian Elischer struct proc *p; 203290af4afaSJohn Baldwin struct sigacts *ps; 20334093529dSJeff Roberson sigset_t sigpending; 2034062cf543SDavid Xu int sig, prop; 2035062cf543SDavid Xu struct thread *td0; 2036df8bae1dSRodney W. Grimes 2037e602ba25SJulian Elischer p = td->td_proc; 203890af4afaSJohn Baldwin ps = p->p_sigacts; 203990af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2040628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2041df8bae1dSRodney W. Grimes for (;;) { 20422a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 20432a024a2bSSean Eric Fagan 20444093529dSJeff Roberson sigpending = td->td_siglist; 20454093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 20464093529dSJeff Roberson 2047df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 20484093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 20494093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2050df8bae1dSRodney W. Grimes return (0); 20514093529dSJeff Roberson sig = sig_ffs(&sigpending); 20522a024a2bSSean Eric Fagan 2053628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 20542a024a2bSSean Eric Fagan 2055df8bae1dSRodney W. Grimes /* 2056df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2057df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2058df8bae1dSRodney W. Grimes */ 205990af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 20604093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 2061df8bae1dSRodney W. Grimes continue; 2062df8bae1dSRodney W. Grimes } 2063df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2064df8bae1dSRodney W. Grimes /* 2065d8f4f6a4SJonathan Mini * If traced, always stop. 2066df8bae1dSRodney W. Grimes */ 206790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 206890af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 206990af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Stopping for traced signal"); 20702c42a146SMarcel Moolenaar p->p_xstat = sig; 2071628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 2072df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 2073628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 20749ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 20754d492b43SJulian Elischer stop(p); /* uses schedlock too eventually */ 207671fad9fdSJulian Elischer thread_suspend_one(td); 2077c86b6ff5SJohn Baldwin PROC_UNLOCK(p); 2078c86b6ff5SJohn Baldwin DROP_GIANT(); 20792ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 2080df8bae1dSRodney W. Grimes mi_switch(); 20819ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 208220cdcc5bSJohn Baldwin PICKUP_GIANT(); 2083628d2653SJohn Baldwin PROC_LOCK(p); 208490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2085df8bae1dSRodney W. Grimes 2086df8bae1dSRodney W. Grimes /* 2087df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 2088df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 2089df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 2090df8bae1dSRodney W. Grimes */ 20914093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* clear old signal */ 20922c42a146SMarcel Moolenaar sig = p->p_xstat; 20932c42a146SMarcel Moolenaar if (sig == 0) 2094df8bae1dSRodney W. Grimes continue; 2095df8bae1dSRodney W. Grimes 2096df8bae1dSRodney W. Grimes /* 20978d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 20988d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 20998d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 21008d542cb5SDavid E. O'Brien */ 21018d542cb5SDavid E. O'Brien if ((p->p_flag & P_TRACED) == 0) 21028d542cb5SDavid E. O'Brien continue; 21038d542cb5SDavid E. O'Brien 21048d542cb5SDavid E. O'Brien /* 21054093529dSJeff Roberson * Put the new signal into td_siglist. If the 21061d9c5696SJuli Mallett * signal is being masked, look for other signals. 2107df8bae1dSRodney W. Grimes */ 21084093529dSJeff Roberson SIGADDSET(td->td_siglist, sig); 21094093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 2110df8bae1dSRodney W. Grimes continue; 21114093529dSJeff Roberson signotify(td); 2112df8bae1dSRodney W. Grimes } 2113df8bae1dSRodney W. Grimes 21148d542cb5SDavid E. O'Brien prop = sigprop(sig); 21158d542cb5SDavid E. O'Brien 2116df8bae1dSRodney W. Grimes /* 2117df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2118df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2119df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2120df8bae1dSRodney W. Grimes */ 2121d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2122df8bae1dSRodney W. Grimes 2123d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2124df8bae1dSRodney W. Grimes /* 2125df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2126df8bae1dSRodney W. Grimes */ 2127df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2128df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2129df8bae1dSRodney W. Grimes /* 2130df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2131df8bae1dSRodney W. Grimes * in init? XXX 2132df8bae1dSRodney W. Grimes */ 2133d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 21342c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2135df8bae1dSRodney W. Grimes #endif 2136df8bae1dSRodney W. Grimes break; /* == ignore */ 2137df8bae1dSRodney W. Grimes } 2138df8bae1dSRodney W. Grimes /* 2139df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 2140df8bae1dSRodney W. Grimes * with default action, stop here, 2141df8bae1dSRodney W. Grimes * then clear the signal. However, 2142df8bae1dSRodney W. Grimes * if process is member of an orphaned 2143df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 2144df8bae1dSRodney W. Grimes */ 2145df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 2146df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 2147df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2148df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 2149df8bae1dSRodney W. Grimes break; /* == ignore */ 215090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 215190af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 215290af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2153e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 21542c42a146SMarcel Moolenaar p->p_xstat = sig; 2155721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 2156062cf543SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2157062cf543SDavid Xu if (TD_IS_SLEEPING(td0) && 2158062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 2159062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 2160062cf543SDavid Xu thread_suspend_one(td0); 2161062cf543SDavid Xu } else if (td != td0) { 2162062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2163062cf543SDavid Xu } 2164062cf543SDavid Xu } 2165e574e444SDavid Xu thread_stopped(p); 216671fad9fdSJulian Elischer thread_suspend_one(td); 2167721e5910SJulian Elischer PROC_UNLOCK(p); 2168721e5910SJulian Elischer DROP_GIANT(); 2169721e5910SJulian Elischer p->p_stats->p_ru.ru_nivcsw++; 2170721e5910SJulian Elischer mi_switch(); 2171721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 2172721e5910SJulian Elischer PICKUP_GIANT(); 2173721e5910SJulian Elischer PROC_LOCK(p); 217490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2175df8bae1dSRodney W. Grimes break; 217621b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 2177df8bae1dSRodney W. Grimes /* 2178df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2179df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2180df8bae1dSRodney W. Grimes */ 2181df8bae1dSRodney W. Grimes break; /* == ignore */ 2182df8bae1dSRodney W. Grimes } else 21832c42a146SMarcel Moolenaar return (sig); 2184df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2185df8bae1dSRodney W. Grimes 2186d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2187df8bae1dSRodney W. Grimes /* 2188df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2189df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2190df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2191df8bae1dSRodney W. Grimes */ 2192df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 2193df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2194df8bae1dSRodney W. Grimes printf("issignal\n"); 2195df8bae1dSRodney W. Grimes break; /* == ignore */ 2196df8bae1dSRodney W. Grimes 2197df8bae1dSRodney W. Grimes default: 2198df8bae1dSRodney W. Grimes /* 2199df8bae1dSRodney W. Grimes * This signal has an action, let 2200df8bae1dSRodney W. Grimes * postsig() process it. 2201df8bae1dSRodney W. Grimes */ 22022c42a146SMarcel Moolenaar return (sig); 2203df8bae1dSRodney W. Grimes } 22044093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2205df8bae1dSRodney W. Grimes } 2206df8bae1dSRodney W. Grimes /* NOTREACHED */ 2207df8bae1dSRodney W. Grimes } 2208df8bae1dSRodney W. Grimes 2209df8bae1dSRodney W. Grimes /* 2210df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 2211df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 22125b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 22135b3047d5SJohn Baldwin * lock held. 2214df8bae1dSRodney W. Grimes */ 22155b3047d5SJohn Baldwin static void 2216e574e444SDavid Xu stop(struct proc *p) 2217df8bae1dSRodney W. Grimes { 2218df8bae1dSRodney W. Grimes 2219628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 22201279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 2221df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 222201609114SAlfred Perlstein wakeup(p->p_pptr); 2223df8bae1dSRodney W. Grimes } 2224df8bae1dSRodney W. Grimes 222590af4afaSJohn Baldwin /* 222690af4afaSJohn Baldwin * MPSAFE 222790af4afaSJohn Baldwin */ 2228e574e444SDavid Xu void 2229e574e444SDavid Xu thread_stopped(struct proc *p) 2230e574e444SDavid Xu { 2231e574e444SDavid Xu struct proc *p1 = curthread->td_proc; 223290af4afaSJohn Baldwin struct sigacts *ps; 2233e574e444SDavid Xu int n; 2234e574e444SDavid Xu 2235e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 2236e574e444SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 2237e574e444SDavid Xu n = p->p_suspcount; 2238e574e444SDavid Xu if (p == p1) 2239e574e444SDavid Xu n++; 2240e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2241e574e444SDavid Xu mtx_unlock_spin(&sched_lock); 2242e574e444SDavid Xu stop(p); 2243e574e444SDavid Xu PROC_LOCK(p->p_pptr); 224490af4afaSJohn Baldwin ps = p->p_pptr->p_sigacts; 224590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 224690af4afaSJohn Baldwin if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 224790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2248e574e444SDavid Xu psignal(p->p_pptr, SIGCHLD); 224990af4afaSJohn Baldwin } else 225090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2251e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 2252e574e444SDavid Xu mtx_lock_spin(&sched_lock); 2253e574e444SDavid Xu } 2254e574e444SDavid Xu } 2255e574e444SDavid Xu 2256df8bae1dSRodney W. Grimes /* 2257df8bae1dSRodney W. Grimes * Take the action for the specified signal 2258df8bae1dSRodney W. Grimes * from the current set of pending signals. 2259df8bae1dSRodney W. Grimes */ 2260df8bae1dSRodney W. Grimes void 22612c42a146SMarcel Moolenaar postsig(sig) 22622c42a146SMarcel Moolenaar register int sig; 2263df8bae1dSRodney W. Grimes { 2264b40ce416SJulian Elischer struct thread *td = curthread; 2265b40ce416SJulian Elischer register struct proc *p = td->td_proc; 2266628d2653SJohn Baldwin struct sigacts *ps; 22672c42a146SMarcel Moolenaar sig_t action; 22682c42a146SMarcel Moolenaar sigset_t returnmask; 22692c42a146SMarcel Moolenaar int code; 2270df8bae1dSRodney W. Grimes 22712c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 22725526d2d9SEivind Eklund 22732ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2274628d2653SJohn Baldwin ps = p->p_sigacts; 227590af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 22764093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 22772c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2278df8bae1dSRodney W. Grimes #ifdef KTRACE 2279374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 22805e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 22814093529dSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 2282df8bae1dSRodney W. Grimes #endif 2283628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 22842a024a2bSSean Eric Fagan 2285df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 2286df8bae1dSRodney W. Grimes /* 2287df8bae1dSRodney W. Grimes * Default action, where the default is to kill 2288df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 2289df8bae1dSRodney W. Grimes */ 229090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2291b40ce416SJulian Elischer sigexit(td, sig); 2292df8bae1dSRodney W. Grimes /* NOTREACHED */ 2293df8bae1dSRodney W. Grimes } else { 2294df8bae1dSRodney W. Grimes /* 2295df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 2296df8bae1dSRodney W. Grimes */ 22974093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 22985526d2d9SEivind Eklund ("postsig action")); 2299df8bae1dSRodney W. Grimes /* 2300df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 2301645682fdSLuoqi Chen * occurrences of this signal. 2302df8bae1dSRodney W. Grimes * 2303645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 2304df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 2305645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 2306df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 2307df8bae1dSRodney W. Grimes */ 23085e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 23094093529dSJeff Roberson returnmask = td->td_oldsigmask; 23105e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 2311df8bae1dSRodney W. Grimes } else 23124093529dSJeff Roberson returnmask = td->td_sigmask; 23132c42a146SMarcel Moolenaar 23144093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 23152c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 23164093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 23172c42a146SMarcel Moolenaar 23182c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2319289ccde0SPeter Wemm /* 23208f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 2321289ccde0SPeter Wemm */ 232290af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 23232c42a146SMarcel Moolenaar if (sig != SIGCONT && 23242c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 232590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 23262c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2327dedc04feSPeter Wemm } 2328df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 23292c42a146SMarcel Moolenaar if (p->p_sig != sig) { 2330df8bae1dSRodney W. Grimes code = 0; 2331df8bae1dSRodney W. Grimes } else { 23326626c604SJulian Elischer code = p->p_code; 23336626c604SJulian Elischer p->p_code = 0; 23346626c604SJulian Elischer p->p_sig = 0; 2335df8bae1dSRodney W. Grimes } 2336cd4f6ebbSDavid Xu if (td->td_flags & TDF_SA) 233758a3c273SJeff Roberson thread_signal_add(curthread, sig); 233858a3c273SJeff Roberson else 233958a3c273SJeff Roberson (*p->p_sysent->sv_sendsig)(action, sig, 234058a3c273SJeff Roberson &returnmask, code); 2341df8bae1dSRodney W. Grimes } 2342df8bae1dSRodney W. Grimes } 2343df8bae1dSRodney W. Grimes 2344df8bae1dSRodney W. Grimes /* 2345df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2346df8bae1dSRodney W. Grimes */ 234726f9a767SRodney W. Grimes void 2348df8bae1dSRodney W. Grimes killproc(p, why) 2349df8bae1dSRodney W. Grimes struct proc *p; 2350df8bae1dSRodney W. Grimes char *why; 2351df8bae1dSRodney W. Grimes { 23529081e5e8SJohn Baldwin 23539081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 23540384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 23550384fff8SJason Evans p, p->p_pid, p->p_comm); 2356729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2357b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2358df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 2359df8bae1dSRodney W. Grimes } 2360df8bae1dSRodney W. Grimes 2361df8bae1dSRodney W. Grimes /* 2362df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2363df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2364df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2365df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2366df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2367df8bae1dSRodney W. Grimes * does not return. 236890af4afaSJohn Baldwin * 236990af4afaSJohn Baldwin * MPSAFE 2370df8bae1dSRodney W. Grimes */ 237126f9a767SRodney W. Grimes void 2372b40ce416SJulian Elischer sigexit(td, sig) 2373b40ce416SJulian Elischer struct thread *td; 23742c42a146SMarcel Moolenaar int sig; 2375df8bae1dSRodney W. Grimes { 2376b40ce416SJulian Elischer struct proc *p = td->td_proc; 2377df8bae1dSRodney W. Grimes 2378628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2379df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 23802c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 23812c42a146SMarcel Moolenaar p->p_sig = sig; 2382c364e17eSAndrey A. Chernov /* 2383c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2384c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2385c364e17eSAndrey A. Chernov * these messages.) 2386c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 2387c364e17eSAndrey A. Chernov */ 2388628d2653SJohn Baldwin PROC_UNLOCK(p); 2389c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 2390c31146a1SJohn Baldwin mtx_lock(&Giant); 2391b40ce416SJulian Elischer if (coredump(td) == 0) 23922c42a146SMarcel Moolenaar sig |= WCOREFLAG; 239357308494SJoerg Wunsch if (kern_logsigexit) 239457308494SJoerg Wunsch log(LOG_INFO, 239557308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 23963d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 23979c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 23982c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 23992c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 2400c31146a1SJohn Baldwin } else { 2401628d2653SJohn Baldwin PROC_UNLOCK(p); 2402628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 2403628d2653SJohn Baldwin mtx_lock(&Giant); 2404c31146a1SJohn Baldwin } 2405b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2406df8bae1dSRodney W. Grimes /* NOTREACHED */ 2407df8bae1dSRodney W. Grimes } 2408df8bae1dSRodney W. Grimes 2409c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 2410c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2411c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 2412c5edb423SSean Eric Fagan 2413c5edb423SSean Eric Fagan /* 2414c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 2415c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 2416c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 2417c5edb423SSean Eric Fagan * %N name of process ("name") 2418c5edb423SSean Eric Fagan * %P process id (pid) 2419c5edb423SSean Eric Fagan * %U user id (uid) 2420c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 2421c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2422c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 2423c5edb423SSean Eric Fagan */ 2424c5edb423SSean Eric Fagan 2425fca666a1SJulian Elischer static char * 2426c5edb423SSean Eric Fagan expand_name(name, uid, pid) 24278b43b535SAlfred Perlstein const char *name; 24288b43b535SAlfred Perlstein uid_t uid; 24298b43b535SAlfred Perlstein pid_t pid; 24308b43b535SAlfred Perlstein { 24318b43b535SAlfred Perlstein const char *format, *appendstr; 2432c5edb423SSean Eric Fagan char *temp; 2433c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 24348b43b535SAlfred Perlstein size_t i, l, n; 2435c5edb423SSean Eric Fagan 24368b43b535SAlfred Perlstein format = corefilename; 24378b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 24380bfe2990SEivind Eklund if (temp == NULL) 24398b43b535SAlfred Perlstein return (NULL); 2440ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2441c5edb423SSean Eric Fagan switch (format[i]) { 2442c5edb423SSean Eric Fagan case '%': /* Format character */ 2443c5edb423SSean Eric Fagan i++; 2444c5edb423SSean Eric Fagan switch (format[i]) { 2445c5edb423SSean Eric Fagan case '%': 24468b43b535SAlfred Perlstein appendstr = "%"; 2447c5edb423SSean Eric Fagan break; 2448c5edb423SSean Eric Fagan case 'N': /* process name */ 24498b43b535SAlfred Perlstein appendstr = name; 2450c5edb423SSean Eric Fagan break; 2451c5edb423SSean Eric Fagan case 'P': /* process id */ 24528b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 24538b43b535SAlfred Perlstein appendstr = buf; 2454c5edb423SSean Eric Fagan break; 2455c5edb423SSean Eric Fagan case 'U': /* user id */ 24568b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 24578b43b535SAlfred Perlstein appendstr = buf; 2458c5edb423SSean Eric Fagan break; 2459c5edb423SSean Eric Fagan default: 24608b43b535SAlfred Perlstein appendstr = ""; 24618b43b535SAlfred Perlstein log(LOG_ERR, 24628b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 24638b43b535SAlfred Perlstein format[i], format); 2464c5edb423SSean Eric Fagan } 24658b43b535SAlfred Perlstein l = strlen(appendstr); 24668b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 24678b43b535SAlfred Perlstein goto toolong; 24688b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 24698b43b535SAlfred Perlstein n += l; 2470c5edb423SSean Eric Fagan break; 2471c5edb423SSean Eric Fagan default: 2472c5edb423SSean Eric Fagan temp[n++] = format[i]; 2473c5edb423SSean Eric Fagan } 2474c5edb423SSean Eric Fagan } 24758b43b535SAlfred Perlstein if (format[i] != '\0') 24768b43b535SAlfred Perlstein goto toolong; 24778b43b535SAlfred Perlstein return (temp); 24788b43b535SAlfred Perlstein toolong: 24798b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 24808b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 24818b43b535SAlfred Perlstein free(temp, M_TEMP); 24828b43b535SAlfred Perlstein return (NULL); 2483c5edb423SSean Eric Fagan } 2484c5edb423SSean Eric Fagan 2485df8bae1dSRodney W. Grimes /* 2486fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2487fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2488fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2489fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2490fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2491fca666a1SJulian Elischer */ 2492fca666a1SJulian Elischer 2493fca666a1SJulian Elischer static int 2494b40ce416SJulian Elischer coredump(struct thread *td) 2495fca666a1SJulian Elischer { 2496b40ce416SJulian Elischer struct proc *p = td->td_proc; 2497fca666a1SJulian Elischer register struct vnode *vp; 24989c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 249906ae1e91SMatthew Dillon struct flock lf; 2500fca666a1SJulian Elischer struct nameidata nd; 2501fca666a1SJulian Elischer struct vattr vattr; 2502e6796b67SKirk McKusick int error, error1, flags; 2503f2a2857bSKirk McKusick struct mount *mp; 2504fca666a1SJulian Elischer char *name; /* name of corefile */ 2505fca666a1SJulian Elischer off_t limit; 2506fca666a1SJulian Elischer 2507628d2653SJohn Baldwin PROC_LOCK(p); 2508628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2509fca666a1SJulian Elischer 2510628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2511628d2653SJohn Baldwin PROC_UNLOCK(p); 2512fca666a1SJulian Elischer return (EFAULT); 2513628d2653SJohn Baldwin } 2514fca666a1SJulian Elischer 2515fca666a1SJulian Elischer /* 251635a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 251735a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 251835a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 251935a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 252035a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 252135a2598fSSean Eric Fagan * if it is larger than the limit. 2522fca666a1SJulian Elischer */ 252335a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2524628d2653SJohn Baldwin if (limit == 0) { 2525628d2653SJohn Baldwin PROC_UNLOCK(p); 252635a2598fSSean Eric Fagan return 0; 2527628d2653SJohn Baldwin } 2528628d2653SJohn Baldwin PROC_UNLOCK(p); 252935a2598fSSean Eric Fagan 2530f2a2857bSKirk McKusick restart: 25319c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2532ccdbd10cSPeter Pentchev if (name == NULL) 2533ccdbd10cSPeter Pentchev return (EINVAL); 2534b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2535e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 2536e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2537fca666a1SJulian Elischer free(name, M_TEMP); 2538fca666a1SJulian Elischer if (error) 2539fca666a1SJulian Elischer return (error); 2540762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2541fca666a1SJulian Elischer vp = nd.ni_vp; 254206ae1e91SMatthew Dillon 2543832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2544832dafadSDon Lewis if (vp->v_type != VREG || 2545832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2546832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2547832dafadSDon Lewis error = EFAULT; 2548832dafadSDon Lewis goto out2; 2549832dafadSDon Lewis } 2550832dafadSDon Lewis 2551b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 255206ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 255306ae1e91SMatthew Dillon lf.l_start = 0; 255406ae1e91SMatthew Dillon lf.l_len = 0; 255506ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 255606ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 255706ae1e91SMatthew Dillon if (error) 255806ae1e91SMatthew Dillon goto out2; 255906ae1e91SMatthew Dillon 256006ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 256106ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 256206ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2563b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2564f2a2857bSKirk McKusick return (error); 2565f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2566f2a2857bSKirk McKusick return (error); 2567f2a2857bSKirk McKusick goto restart; 2568f2a2857bSKirk McKusick } 2569fca666a1SJulian Elischer 2570fca666a1SJulian Elischer VATTR_NULL(&vattr); 2571fca666a1SJulian Elischer vattr.va_size = 0; 257288b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2573b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2574b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 257588b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2576628d2653SJohn Baldwin PROC_LOCK(p); 2577fca666a1SJulian Elischer p->p_acflag |= ACORE; 2578628d2653SJohn Baldwin PROC_UNLOCK(p); 2579fca666a1SJulian Elischer 2580fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2581b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2582fca666a1SJulian Elischer ENOSYS; 2583fca666a1SJulian Elischer 258406ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 258506ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2586f2a2857bSKirk McKusick vn_finished_write(mp); 258706ae1e91SMatthew Dillon out2: 2588b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2589fca666a1SJulian Elischer if (error == 0) 2590fca666a1SJulian Elischer error = error1; 2591fca666a1SJulian Elischer return (error); 2592fca666a1SJulian Elischer } 2593fca666a1SJulian Elischer 2594fca666a1SJulian Elischer /* 2595df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2596df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2597df8bae1dSRodney W. Grimes */ 2598d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2599df8bae1dSRodney W. Grimes struct nosys_args { 2600df8bae1dSRodney W. Grimes int dummy; 2601df8bae1dSRodney W. Grimes }; 2602d2d3e875SBruce Evans #endif 2603fb99ab88SMatthew Dillon /* 2604fb99ab88SMatthew Dillon * MPSAFE 2605fb99ab88SMatthew Dillon */ 2606df8bae1dSRodney W. Grimes /* ARGSUSED */ 260726f9a767SRodney W. Grimes int 2608b40ce416SJulian Elischer nosys(td, args) 2609b40ce416SJulian Elischer struct thread *td; 2610df8bae1dSRodney W. Grimes struct nosys_args *args; 2611df8bae1dSRodney W. Grimes { 2612b40ce416SJulian Elischer struct proc *p = td->td_proc; 2613b40ce416SJulian Elischer 2614628d2653SJohn Baldwin PROC_LOCK(p); 2615df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2616628d2653SJohn Baldwin PROC_UNLOCK(p); 2617f5216b9aSBruce Evans return (ENOSYS); 2618df8bae1dSRodney W. Grimes } 2619831d27a9SDon Lewis 2620831d27a9SDon Lewis /* 262148f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2622831d27a9SDon Lewis * stored credentials rather than those of the current process. 2623831d27a9SDon Lewis */ 2624831d27a9SDon Lewis void 2625f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2626f1320723SAlfred Perlstein struct sigio **sigiop; 26272c42a146SMarcel Moolenaar int sig, checkctty; 2628831d27a9SDon Lewis { 2629f1320723SAlfred Perlstein struct sigio *sigio; 2630831d27a9SDon Lewis 2631f1320723SAlfred Perlstein SIGIO_LOCK(); 2632f1320723SAlfred Perlstein sigio = *sigiop; 2633f1320723SAlfred Perlstein if (sigio == NULL) { 2634f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2635f1320723SAlfred Perlstein return; 2636f1320723SAlfred Perlstein } 2637831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2638628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 26392b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 26402c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2641628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2642831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2643831d27a9SDon Lewis struct proc *p; 2644831d27a9SDon Lewis 2645f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2646628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2647628d2653SJohn Baldwin PROC_LOCK(p); 26482b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2649831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 26502c42a146SMarcel Moolenaar psignal(p, sig); 2651628d2653SJohn Baldwin PROC_UNLOCK(p); 2652628d2653SJohn Baldwin } 2653f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2654831d27a9SDon Lewis } 2655f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2656831d27a9SDon Lewis } 2657cb679c38SJonathan Lemon 2658cb679c38SJonathan Lemon static int 2659cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2660cb679c38SJonathan Lemon { 2661cb679c38SJonathan Lemon struct proc *p = curproc; 2662cb679c38SJonathan Lemon 2663cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2664cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2665cb679c38SJonathan Lemon 2666628d2653SJohn Baldwin PROC_LOCK(p); 2667cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2668628d2653SJohn Baldwin PROC_UNLOCK(p); 2669cb679c38SJonathan Lemon 2670cb679c38SJonathan Lemon return (0); 2671cb679c38SJonathan Lemon } 2672cb679c38SJonathan Lemon 2673cb679c38SJonathan Lemon static void 2674cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2675cb679c38SJonathan Lemon { 2676cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2677cb679c38SJonathan Lemon 2678628d2653SJohn Baldwin PROC_LOCK(p); 2679e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2680628d2653SJohn Baldwin PROC_UNLOCK(p); 2681cb679c38SJonathan Lemon } 2682cb679c38SJonathan Lemon 2683cb679c38SJonathan Lemon /* 2684cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2685cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2686cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2687cb679c38SJonathan Lemon * isn't worth the trouble. 2688cb679c38SJonathan Lemon */ 2689cb679c38SJonathan Lemon static int 2690cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2691cb679c38SJonathan Lemon { 2692cb679c38SJonathan Lemon 2693cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2694cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2695cb679c38SJonathan Lemon 2696cb679c38SJonathan Lemon if (kn->kn_id == hint) 2697cb679c38SJonathan Lemon kn->kn_data++; 2698cb679c38SJonathan Lemon } 2699cb679c38SJonathan Lemon return (kn->kn_data != 0); 2700cb679c38SJonathan Lemon } 270190af4afaSJohn Baldwin 270290af4afaSJohn Baldwin struct sigacts * 270390af4afaSJohn Baldwin sigacts_alloc(void) 270490af4afaSJohn Baldwin { 270590af4afaSJohn Baldwin struct sigacts *ps; 270690af4afaSJohn Baldwin 270790af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 270890af4afaSJohn Baldwin ps->ps_refcnt = 1; 270990af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 271090af4afaSJohn Baldwin return (ps); 271190af4afaSJohn Baldwin } 271290af4afaSJohn Baldwin 271390af4afaSJohn Baldwin void 271490af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 271590af4afaSJohn Baldwin { 271690af4afaSJohn Baldwin 271790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 271890af4afaSJohn Baldwin ps->ps_refcnt--; 271990af4afaSJohn Baldwin if (ps->ps_refcnt == 0) { 272090af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 272190af4afaSJohn Baldwin free(ps, M_SUBPROC); 272290af4afaSJohn Baldwin } else 272390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 272490af4afaSJohn Baldwin } 272590af4afaSJohn Baldwin 272690af4afaSJohn Baldwin struct sigacts * 272790af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 272890af4afaSJohn Baldwin { 272990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 273090af4afaSJohn Baldwin ps->ps_refcnt++; 273190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 273290af4afaSJohn Baldwin return (ps); 273390af4afaSJohn Baldwin } 273490af4afaSJohn Baldwin 273590af4afaSJohn Baldwin void 273690af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 273790af4afaSJohn Baldwin { 273890af4afaSJohn Baldwin 273990af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 274090af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 274190af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 274290af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 274390af4afaSJohn Baldwin } 274490af4afaSJohn Baldwin 274590af4afaSJohn Baldwin int 274690af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 274790af4afaSJohn Baldwin { 274890af4afaSJohn Baldwin int shared; 274990af4afaSJohn Baldwin 275090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 275190af4afaSJohn Baldwin shared = ps->ps_refcnt > 1; 275290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 275390af4afaSJohn Baldwin return (shared); 275490af4afaSJohn Baldwin } 2755