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); 989dde3bc9SDavid Xu static void do_tdsignal(struct thread *td, int sig); 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); 764a447cd8bSJeff Roberson tdsignal(td, info.si_signo); 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); 803a447cd8bSJeff Roberson tdsignal(td, info.si_signo); 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); 834a447cd8bSJeff Roberson tdsignal(td, info.si_signo); 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 843a447cd8bSJeff Roberson kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info, 844a447cd8bSJeff Roberson struct timespec *timeout) 845a447cd8bSJeff Roberson { 846a447cd8bSJeff Roberson register struct sigacts *ps; 847a447cd8bSJeff Roberson sigset_t oldmask; 848a447cd8bSJeff Roberson struct proc *p; 849a447cd8bSJeff Roberson int error; 850a447cd8bSJeff Roberson int sig; 851a447cd8bSJeff Roberson int hz; 852a447cd8bSJeff Roberson 853a447cd8bSJeff Roberson p = td->td_proc; 854a447cd8bSJeff Roberson error = 0; 855a447cd8bSJeff Roberson sig = 0; 856a447cd8bSJeff Roberson SIG_CANTMASK(set); 857a447cd8bSJeff Roberson 858a447cd8bSJeff Roberson PROC_LOCK(p); 859a447cd8bSJeff Roberson ps = p->p_sigacts; 860a447cd8bSJeff Roberson oldmask = td->td_sigmask; 861418228dfSDavid Xu SIGFILLSET(td->td_sigmask); 862418228dfSDavid Xu SIG_CANTMASK(td->td_sigmask); 863418228dfSDavid Xu SIGSETNAND(td->td_sigmask, set); 864a447cd8bSJeff Roberson signotify(td); 865a447cd8bSJeff Roberson 86690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 867a447cd8bSJeff Roberson sig = cursig(td); 868a447cd8bSJeff Roberson if (sig) 869a447cd8bSJeff Roberson goto out; 870a447cd8bSJeff Roberson 871a447cd8bSJeff Roberson /* 872a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 873a447cd8bSJeff Roberson * signals. 874a447cd8bSJeff Roberson */ 875a447cd8bSJeff Roberson if (timeout) { 876a447cd8bSJeff Roberson struct timeval tv; 877a447cd8bSJeff Roberson 878a447cd8bSJeff Roberson if (timeout->tv_nsec > 1000000000) { 879a447cd8bSJeff Roberson error = EINVAL; 880a447cd8bSJeff Roberson goto out; 881a447cd8bSJeff Roberson } 8829dde3bc9SDavid Xu if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) 8839dde3bc9SDavid Xu goto nosleep; 884a447cd8bSJeff Roberson TIMESPEC_TO_TIMEVAL(&tv, timeout); 885a447cd8bSJeff Roberson hz = tvtohz(&tv); 886a447cd8bSJeff Roberson } else 887a447cd8bSJeff Roberson hz = 0; 888a447cd8bSJeff Roberson 88990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 890a447cd8bSJeff Roberson error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz); 89190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 892a447cd8bSJeff Roberson if (error == EINTR) 893a447cd8bSJeff Roberson error = 0; 894a447cd8bSJeff Roberson else if (error) 895a447cd8bSJeff Roberson goto out; 8969dde3bc9SDavid Xu nosleep: 897a447cd8bSJeff Roberson sig = cursig(td); 8989dde3bc9SDavid Xu 899a447cd8bSJeff Roberson out: 900a447cd8bSJeff Roberson td->td_sigmask = oldmask; 901a447cd8bSJeff Roberson if (sig) { 902a447cd8bSJeff Roberson sig_t action; 903a447cd8bSJeff Roberson 904a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 90590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 906a447cd8bSJeff Roberson #ifdef KTRACE 907a447cd8bSJeff Roberson if (KTRPOINT(td, KTR_PSIG)) 9085e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 909a447cd8bSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 910a447cd8bSJeff Roberson #endif 911a447cd8bSJeff Roberson _STOPEVENT(p, S_SIG, sig); 912a447cd8bSJeff Roberson 913a447cd8bSJeff Roberson if (action == SIG_DFL) 914a447cd8bSJeff Roberson sigexit(td, sig); 915a447cd8bSJeff Roberson /* NOTREACHED */ 916a447cd8bSJeff Roberson 917a447cd8bSJeff Roberson SIGDELSET(td->td_siglist, sig); 918a447cd8bSJeff Roberson info->si_signo = sig; 919a447cd8bSJeff Roberson info->si_code = 0; 92090af4afaSJohn Baldwin } else 92190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 922a447cd8bSJeff Roberson PROC_UNLOCK(p); 923a447cd8bSJeff Roberson return (error); 924a447cd8bSJeff Roberson } 925a447cd8bSJeff Roberson 926a447cd8bSJeff Roberson /* 927a447cd8bSJeff Roberson * MPSAFE 928a447cd8bSJeff Roberson */ 929a447cd8bSJeff Roberson int 930b40ce416SJulian Elischer sigpending(td, uap) 931b40ce416SJulian Elischer struct thread *td; 932df8bae1dSRodney W. Grimes struct sigpending_args *uap; 933df8bae1dSRodney W. Grimes { 934b40ce416SJulian Elischer struct proc *p = td->td_proc; 935628d2653SJohn Baldwin sigset_t siglist; 936df8bae1dSRodney W. Grimes 937628d2653SJohn Baldwin PROC_LOCK(p); 9381d9c5696SJuli Mallett siglist = p->p_siglist; 9394093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 940628d2653SJohn Baldwin PROC_UNLOCK(p); 94148e8f774STim J. Robbins return (copyout(&siglist, uap->set, sizeof(sigset_t))); 9422c42a146SMarcel Moolenaar } 9432c42a146SMarcel Moolenaar 94431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9452c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9462c42a146SMarcel Moolenaar struct osigpending_args { 9472c42a146SMarcel Moolenaar int dummy; 9482c42a146SMarcel Moolenaar }; 9492c42a146SMarcel Moolenaar #endif 950fb99ab88SMatthew Dillon /* 951fb99ab88SMatthew Dillon * MPSAFE 952fb99ab88SMatthew Dillon */ 9532c42a146SMarcel Moolenaar int 954b40ce416SJulian Elischer osigpending(td, uap) 955b40ce416SJulian Elischer struct thread *td; 9562c42a146SMarcel Moolenaar struct osigpending_args *uap; 9572c42a146SMarcel Moolenaar { 958b40ce416SJulian Elischer struct proc *p = td->td_proc; 9594093529dSJeff Roberson sigset_t siglist; 960b40ce416SJulian Elischer 961628d2653SJohn Baldwin PROC_LOCK(p); 9624093529dSJeff Roberson siglist = p->p_siglist; 9634093529dSJeff Roberson SIGSETOR(siglist, td->td_siglist); 964628d2653SJohn Baldwin PROC_UNLOCK(p); 9659d8643ecSJohn Baldwin SIG2OSIG(siglist, td->td_retval[0]); 966df8bae1dSRodney W. Grimes return (0); 967df8bae1dSRodney W. Grimes } 96831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 969df8bae1dSRodney W. Grimes 970df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 971df8bae1dSRodney W. Grimes /* 972df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 973df8bae1dSRodney W. Grimes */ 974d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 975df8bae1dSRodney W. Grimes struct osigvec_args { 976df8bae1dSRodney W. Grimes int signum; 977df8bae1dSRodney W. Grimes struct sigvec *nsv; 978df8bae1dSRodney W. Grimes struct sigvec *osv; 979df8bae1dSRodney W. Grimes }; 980d2d3e875SBruce Evans #endif 981fb99ab88SMatthew Dillon /* 982fb99ab88SMatthew Dillon * MPSAFE 983fb99ab88SMatthew Dillon */ 984df8bae1dSRodney W. Grimes /* ARGSUSED */ 98526f9a767SRodney W. Grimes int 986b40ce416SJulian Elischer osigvec(td, uap) 987b40ce416SJulian Elischer struct thread *td; 988df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 989df8bae1dSRodney W. Grimes { 990df8bae1dSRodney W. Grimes struct sigvec vec; 9912c42a146SMarcel Moolenaar struct sigaction nsa, osa; 9922c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 9932c42a146SMarcel Moolenaar int error; 994df8bae1dSRodney W. Grimes 9956f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 9966f841fb7SMarcel Moolenaar return (EINVAL); 9976f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 9986f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 9992c42a146SMarcel Moolenaar if (nsap) { 10006f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 10012c42a146SMarcel Moolenaar if (error) 1002df8bae1dSRodney W. Grimes return (error); 10032c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 10042c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 10052c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 10062c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1007df8bae1dSRodney W. Grimes #ifdef COMPAT_SUNOS 10082c42a146SMarcel Moolenaar nsap->sa_flags |= SA_USERTRAMP; 1009df8bae1dSRodney W. Grimes #endif 1010df8bae1dSRodney W. Grimes } 10115edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 10122c42a146SMarcel Moolenaar if (osap && !error) { 10132c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 10142c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 10152c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 10162c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 10172c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 10182c42a146SMarcel Moolenaar #ifdef COMPAT_SUNOS 10192c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDSTOP; 10202c42a146SMarcel Moolenaar #endif 10216f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 10222c42a146SMarcel Moolenaar } 10232c42a146SMarcel Moolenaar return (error); 1024df8bae1dSRodney W. Grimes } 1025df8bae1dSRodney W. Grimes 1026d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1027df8bae1dSRodney W. Grimes struct osigblock_args { 1028df8bae1dSRodney W. Grimes int mask; 1029df8bae1dSRodney W. Grimes }; 1030d2d3e875SBruce Evans #endif 1031fb99ab88SMatthew Dillon /* 1032fb99ab88SMatthew Dillon * MPSAFE 1033fb99ab88SMatthew Dillon */ 103426f9a767SRodney W. Grimes int 1035b40ce416SJulian Elischer osigblock(td, uap) 1036b40ce416SJulian Elischer register struct thread *td; 1037df8bae1dSRodney W. Grimes struct osigblock_args *uap; 1038df8bae1dSRodney W. Grimes { 1039b40ce416SJulian Elischer struct proc *p = td->td_proc; 10402c42a146SMarcel Moolenaar sigset_t set; 1041df8bae1dSRodney W. Grimes 10422c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10432c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1044628d2653SJohn Baldwin PROC_LOCK(p); 10454093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 10464093529dSJeff Roberson SIGSETOR(td->td_sigmask, set); 1047628d2653SJohn Baldwin PROC_UNLOCK(p); 1048df8bae1dSRodney W. Grimes return (0); 1049df8bae1dSRodney W. Grimes } 1050df8bae1dSRodney W. Grimes 1051d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1052df8bae1dSRodney W. Grimes struct osigsetmask_args { 1053df8bae1dSRodney W. Grimes int mask; 1054df8bae1dSRodney W. Grimes }; 1055d2d3e875SBruce Evans #endif 1056fb99ab88SMatthew Dillon /* 1057fb99ab88SMatthew Dillon * MPSAFE 1058fb99ab88SMatthew Dillon */ 105926f9a767SRodney W. Grimes int 1060b40ce416SJulian Elischer osigsetmask(td, uap) 1061b40ce416SJulian Elischer struct thread *td; 1062df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 1063df8bae1dSRodney W. Grimes { 1064b40ce416SJulian Elischer struct proc *p = td->td_proc; 10652c42a146SMarcel Moolenaar sigset_t set; 1066df8bae1dSRodney W. Grimes 10672c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 10682c42a146SMarcel Moolenaar SIG_CANTMASK(set); 1069628d2653SJohn Baldwin PROC_LOCK(p); 10704093529dSJeff Roberson SIG2OSIG(td->td_sigmask, td->td_retval[0]); 10714093529dSJeff Roberson SIGSETLO(td->td_sigmask, set); 10724093529dSJeff Roberson signotify(td); 1073628d2653SJohn Baldwin PROC_UNLOCK(p); 1074df8bae1dSRodney W. Grimes return (0); 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1077df8bae1dSRodney W. Grimes 1078df8bae1dSRodney W. Grimes /* 1079df8bae1dSRodney W. Grimes * Suspend process until signal, providing mask to be set 1080df8bae1dSRodney W. Grimes * in the meantime. Note nonstandard calling convention: 1081df8bae1dSRodney W. Grimes * libc stub passes mask, not pointer, to save a copyin. 1082b40ce416SJulian Elischer ***** XXXKSE this doesn't make sense under KSE. 1083b40ce416SJulian Elischer ***** Do we suspend the thread or all threads in the process? 1084b40ce416SJulian Elischer ***** How do we suspend threads running NOW on another processor? 1085df8bae1dSRodney W. Grimes */ 1086d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1087df8bae1dSRodney W. Grimes struct sigsuspend_args { 10882c42a146SMarcel Moolenaar const sigset_t *sigmask; 1089df8bae1dSRodney W. Grimes }; 1090d2d3e875SBruce Evans #endif 1091fb99ab88SMatthew Dillon /* 1092fb99ab88SMatthew Dillon * MPSAFE 1093fb99ab88SMatthew Dillon */ 1094df8bae1dSRodney W. Grimes /* ARGSUSED */ 109526f9a767SRodney W. Grimes int 1096b40ce416SJulian Elischer sigsuspend(td, uap) 1097b40ce416SJulian Elischer struct thread *td; 1098df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 1099df8bae1dSRodney W. Grimes { 11002c42a146SMarcel Moolenaar sigset_t mask; 11012c42a146SMarcel Moolenaar int error; 11022c42a146SMarcel Moolenaar 11036f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 11042c42a146SMarcel Moolenaar if (error) 11052c42a146SMarcel Moolenaar return (error); 11068f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 11078f19eb88SIan Dowse } 11088f19eb88SIan Dowse 11098f19eb88SIan Dowse int 11108f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 11118f19eb88SIan Dowse { 11128f19eb88SIan Dowse struct proc *p = td->td_proc; 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes /* 1115645682fdSLuoqi Chen * When returning from sigsuspend, we want 1116df8bae1dSRodney W. Grimes * the old mask to be restored after the 1117df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1118df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1119df8bae1dSRodney W. Grimes * to indicate this. 1120df8bae1dSRodney W. Grimes */ 1121628d2653SJohn Baldwin PROC_LOCK(p); 11224093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11235e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1124645682fdSLuoqi Chen SIG_CANTMASK(mask); 11254093529dSJeff Roberson td->td_sigmask = mask; 11264093529dSJeff Roberson signotify(td); 1127b1bf1c3aSJohn Baldwin while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 11282c42a146SMarcel Moolenaar /* void */; 1129628d2653SJohn Baldwin PROC_UNLOCK(p); 11302c42a146SMarcel Moolenaar /* always return EINTR rather than ERESTART... */ 11312c42a146SMarcel Moolenaar return (EINTR); 11322c42a146SMarcel Moolenaar } 11332c42a146SMarcel Moolenaar 113431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11352c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11362c42a146SMarcel Moolenaar struct osigsuspend_args { 11372c42a146SMarcel Moolenaar osigset_t mask; 11382c42a146SMarcel Moolenaar }; 11392c42a146SMarcel Moolenaar #endif 1140fb99ab88SMatthew Dillon /* 1141fb99ab88SMatthew Dillon * MPSAFE 1142fb99ab88SMatthew Dillon */ 11432c42a146SMarcel Moolenaar /* ARGSUSED */ 11442c42a146SMarcel Moolenaar int 1145b40ce416SJulian Elischer osigsuspend(td, uap) 1146b40ce416SJulian Elischer struct thread *td; 11472c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 11482c42a146SMarcel Moolenaar { 1149b40ce416SJulian Elischer struct proc *p = td->td_proc; 1150645682fdSLuoqi Chen sigset_t mask; 11512c42a146SMarcel Moolenaar 1152628d2653SJohn Baldwin PROC_LOCK(p); 11534093529dSJeff Roberson td->td_oldsigmask = td->td_sigmask; 11545e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1155645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 1156645682fdSLuoqi Chen SIG_CANTMASK(mask); 11574093529dSJeff Roberson SIGSETLO(td->td_sigmask, mask); 11584093529dSJeff Roberson signotify(td); 1159b1bf1c3aSJohn Baldwin while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1160df8bae1dSRodney W. Grimes /* void */; 1161628d2653SJohn Baldwin PROC_UNLOCK(p); 1162df8bae1dSRodney W. Grimes /* always return EINTR rather than ERESTART... */ 1163df8bae1dSRodney W. Grimes return (EINTR); 1164df8bae1dSRodney W. Grimes } 116531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1166df8bae1dSRodney W. Grimes 1167df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1168d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1169df8bae1dSRodney W. Grimes struct osigstack_args { 1170df8bae1dSRodney W. Grimes struct sigstack *nss; 1171df8bae1dSRodney W. Grimes struct sigstack *oss; 1172df8bae1dSRodney W. Grimes }; 1173d2d3e875SBruce Evans #endif 1174fb99ab88SMatthew Dillon /* 1175fb99ab88SMatthew Dillon * MPSAFE 1176fb99ab88SMatthew Dillon */ 1177df8bae1dSRodney W. Grimes /* ARGSUSED */ 117826f9a767SRodney W. Grimes int 1179b40ce416SJulian Elischer osigstack(td, uap) 1180b40ce416SJulian Elischer struct thread *td; 1181df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 1182df8bae1dSRodney W. Grimes { 1183b40ce416SJulian Elischer struct proc *p = td->td_proc; 11845afe0c99SJohn Baldwin struct sigstack nss, oss; 1185fb99ab88SMatthew Dillon int error = 0; 1186fb99ab88SMatthew Dillon 1187d034d459SMarcel Moolenaar if (uap->nss != NULL) { 11885afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 11895afe0c99SJohn Baldwin if (error) 11905afe0c99SJohn Baldwin return (error); 1191df8bae1dSRodney W. Grimes } 11925afe0c99SJohn Baldwin PROC_LOCK(p); 11935afe0c99SJohn Baldwin oss.ss_sp = p->p_sigstk.ss_sp; 11945afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 11955afe0c99SJohn Baldwin if (uap->nss != NULL) { 11965afe0c99SJohn Baldwin p->p_sigstk.ss_sp = nss.ss_sp; 11975afe0c99SJohn Baldwin p->p_sigstk.ss_size = 0; 11985afe0c99SJohn Baldwin p->p_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 11995afe0c99SJohn Baldwin p->p_flag |= P_ALTSTACK; 12005afe0c99SJohn Baldwin } 12015afe0c99SJohn Baldwin PROC_UNLOCK(p); 12025afe0c99SJohn Baldwin if (uap->oss != NULL) 12035afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 12045afe0c99SJohn Baldwin 1205fb99ab88SMatthew Dillon return (error); 1206df8bae1dSRodney W. Grimes } 1207df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1208df8bae1dSRodney W. Grimes 1209d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1210df8bae1dSRodney W. Grimes struct sigaltstack_args { 12112c42a146SMarcel Moolenaar stack_t *ss; 12122c42a146SMarcel Moolenaar stack_t *oss; 1213df8bae1dSRodney W. Grimes }; 1214d2d3e875SBruce Evans #endif 1215fb99ab88SMatthew Dillon /* 1216fb99ab88SMatthew Dillon * MPSAFE 1217fb99ab88SMatthew Dillon */ 1218df8bae1dSRodney W. Grimes /* ARGSUSED */ 121926f9a767SRodney W. Grimes int 1220b40ce416SJulian Elischer sigaltstack(td, uap) 1221b40ce416SJulian Elischer struct thread *td; 1222df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1223df8bae1dSRodney W. Grimes { 12248f19eb88SIan Dowse stack_t ss, oss; 12258f19eb88SIan Dowse int error; 12268f19eb88SIan Dowse 12278f19eb88SIan Dowse if (uap->ss != NULL) { 12288f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 12298f19eb88SIan Dowse if (error) 12308f19eb88SIan Dowse return (error); 12318f19eb88SIan Dowse } 12328f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 12338f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 12348f19eb88SIan Dowse if (error) 12358f19eb88SIan Dowse return (error); 12368f19eb88SIan Dowse if (uap->oss != NULL) 12378f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 12388f19eb88SIan Dowse return (error); 12398f19eb88SIan Dowse } 12408f19eb88SIan Dowse 12418f19eb88SIan Dowse int 12428f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 12438f19eb88SIan Dowse { 1244b40ce416SJulian Elischer struct proc *p = td->td_proc; 1245fb99ab88SMatthew Dillon int oonstack; 1246fb99ab88SMatthew Dillon 124706ce69a7SDavid Xu PROC_LOCK(p); 1248b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1249d034d459SMarcel Moolenaar 12508f19eb88SIan Dowse if (oss != NULL) { 12518f19eb88SIan Dowse *oss = p->p_sigstk; 12528f19eb88SIan Dowse oss->ss_flags = (p->p_flag & P_ALTSTACK) 1253d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1254df8bae1dSRodney W. Grimes } 1255d034d459SMarcel Moolenaar 12568f19eb88SIan Dowse if (ss != NULL) { 1257fb99ab88SMatthew Dillon if (oonstack) { 1258cf60731bSJohn Baldwin PROC_UNLOCK(p); 1259cf60731bSJohn Baldwin return (EPERM); 1260fb99ab88SMatthew Dillon } 12618f19eb88SIan Dowse if ((ss->ss_flags & ~SS_DISABLE) != 0) { 1262cf60731bSJohn Baldwin PROC_UNLOCK(p); 1263cf60731bSJohn Baldwin return (EINVAL); 1264fb99ab88SMatthew Dillon } 12658f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 12668f19eb88SIan Dowse if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1267cf60731bSJohn Baldwin PROC_UNLOCK(p); 1268cf60731bSJohn Baldwin return (ENOMEM); 1269fb99ab88SMatthew Dillon } 12708f19eb88SIan Dowse p->p_sigstk = *ss; 1271d034d459SMarcel Moolenaar p->p_flag |= P_ALTSTACK; 1272628d2653SJohn Baldwin } else { 1273d034d459SMarcel Moolenaar p->p_flag &= ~P_ALTSTACK; 1274628d2653SJohn Baldwin } 1275d034d459SMarcel Moolenaar } 127606ce69a7SDavid Xu PROC_UNLOCK(p); 1277cf60731bSJohn Baldwin return (0); 1278df8bae1dSRodney W. Grimes } 1279df8bae1dSRodney W. Grimes 1280d93f860cSPoul-Henning Kamp /* 1281d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1282d93f860cSPoul-Henning Kamp * cp is calling process. 1283d93f860cSPoul-Henning Kamp */ 128437c84183SPoul-Henning Kamp static int 12859c1ab3e0SJohn Baldwin killpg1(td, sig, pgid, all) 12869c1ab3e0SJohn Baldwin register struct thread *td; 12872c42a146SMarcel Moolenaar int sig, pgid, all; 1288d93f860cSPoul-Henning Kamp { 1289d93f860cSPoul-Henning Kamp register struct proc *p; 1290d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 1291d93f860cSPoul-Henning Kamp int nfound = 0; 1292d93f860cSPoul-Henning Kamp 1293553629ebSJake Burkholder if (all) { 1294d93f860cSPoul-Henning Kamp /* 1295d93f860cSPoul-Henning Kamp * broadcast 1296d93f860cSPoul-Henning Kamp */ 12971005a129SJohn Baldwin sx_slock(&allproc_lock); 12982e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1299628d2653SJohn Baldwin PROC_LOCK(p); 13009c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 13019c1ab3e0SJohn Baldwin p == td->td_proc) { 1302628d2653SJohn Baldwin PROC_UNLOCK(p); 1303628d2653SJohn Baldwin continue; 1304628d2653SJohn Baldwin } 1305f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1306d93f860cSPoul-Henning Kamp nfound++; 130733a9ed9dSJohn Baldwin if (sig) 13082c42a146SMarcel Moolenaar psignal(p, sig); 1309628d2653SJohn Baldwin } 131033a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1311d93f860cSPoul-Henning Kamp } 13121005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1313553629ebSJake Burkholder } else { 1314ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1315f591779bSSeigo Tanimura if (pgid == 0) { 1316d93f860cSPoul-Henning Kamp /* 1317d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1318d93f860cSPoul-Henning Kamp */ 13199c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1320f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1321f591779bSSeigo Tanimura } else { 1322d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1323f591779bSSeigo Tanimura if (pgrp == NULL) { 1324ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1325d93f860cSPoul-Henning Kamp return (ESRCH); 1326d93f860cSPoul-Henning Kamp } 1327f591779bSSeigo Tanimura } 1328ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 13292e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1330628d2653SJohn Baldwin PROC_LOCK(p); 1331628d2653SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1332628d2653SJohn Baldwin PROC_UNLOCK(p); 1333628d2653SJohn Baldwin continue; 1334628d2653SJohn Baldwin } 1335e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 133633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1337628d2653SJohn Baldwin continue; 1338628d2653SJohn Baldwin } 1339f44d9e24SJohn Baldwin if (p_cansignal(td, p, sig) == 0) { 1340d93f860cSPoul-Henning Kamp nfound++; 134133a9ed9dSJohn Baldwin if (sig) 13422c42a146SMarcel Moolenaar psignal(p, sig); 1343628d2653SJohn Baldwin } 134433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1345d93f860cSPoul-Henning Kamp } 1346f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1347d93f860cSPoul-Henning Kamp } 1348d93f860cSPoul-Henning Kamp return (nfound ? 0 : ESRCH); 1349d93f860cSPoul-Henning Kamp } 1350d93f860cSPoul-Henning Kamp 1351d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1352df8bae1dSRodney W. Grimes struct kill_args { 1353df8bae1dSRodney W. Grimes int pid; 1354df8bae1dSRodney W. Grimes int signum; 1355df8bae1dSRodney W. Grimes }; 1356d2d3e875SBruce Evans #endif 1357fb99ab88SMatthew Dillon /* 1358fb99ab88SMatthew Dillon * MPSAFE 1359fb99ab88SMatthew Dillon */ 1360df8bae1dSRodney W. Grimes /* ARGSUSED */ 136126f9a767SRodney W. Grimes int 1362b40ce416SJulian Elischer kill(td, uap) 1363b40ce416SJulian Elischer register struct thread *td; 1364df8bae1dSRodney W. Grimes register struct kill_args *uap; 1365df8bae1dSRodney W. Grimes { 1366df8bae1dSRodney W. Grimes register struct proc *p; 136790af4afaSJohn Baldwin int error; 1368df8bae1dSRodney W. Grimes 13696c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1370df8bae1dSRodney W. Grimes return (EINVAL); 1371fb99ab88SMatthew Dillon 1372df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1373df8bae1dSRodney W. Grimes /* kill single process */ 137490af4afaSJohn Baldwin if ((p = pfind(uap->pid)) == NULL) 137590af4afaSJohn Baldwin return (ESRCH); 137690af4afaSJohn Baldwin error = p_cansignal(td, p, uap->signum); 137790af4afaSJohn Baldwin if (error == 0 && uap->signum) 1378df8bae1dSRodney W. Grimes psignal(p, uap->signum); 1379628d2653SJohn Baldwin PROC_UNLOCK(p); 138090af4afaSJohn Baldwin return (error); 1381df8bae1dSRodney W. Grimes } 1382df8bae1dSRodney W. Grimes switch (uap->pid) { 1383df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 138490af4afaSJohn Baldwin return (killpg1(td, uap->signum, 0, 1)); 1385df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 138690af4afaSJohn Baldwin return (killpg1(td, uap->signum, 0, 0)); 1387df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 138890af4afaSJohn Baldwin return (killpg1(td, uap->signum, -uap->pid, 0)); 1389df8bae1dSRodney W. Grimes } 139090af4afaSJohn Baldwin /* NOTREACHED */ 1391df8bae1dSRodney W. Grimes } 1392df8bae1dSRodney W. Grimes 1393df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1394d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1395df8bae1dSRodney W. Grimes struct okillpg_args { 1396df8bae1dSRodney W. Grimes int pgid; 1397df8bae1dSRodney W. Grimes int signum; 1398df8bae1dSRodney W. Grimes }; 1399d2d3e875SBruce Evans #endif 1400fb99ab88SMatthew Dillon /* 1401fb99ab88SMatthew Dillon * MPSAFE 1402fb99ab88SMatthew Dillon */ 1403df8bae1dSRodney W. Grimes /* ARGSUSED */ 140426f9a767SRodney W. Grimes int 1405b40ce416SJulian Elischer okillpg(td, uap) 1406b40ce416SJulian Elischer struct thread *td; 1407df8bae1dSRodney W. Grimes register struct okillpg_args *uap; 1408df8bae1dSRodney W. Grimes { 1409df8bae1dSRodney W. Grimes 14106c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1411df8bae1dSRodney W. Grimes return (EINVAL); 141290af4afaSJohn Baldwin return (killpg1(td, uap->signum, uap->pgid, 0)); 1413df8bae1dSRodney W. Grimes } 1414df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */ 1415df8bae1dSRodney W. Grimes 1416df8bae1dSRodney W. Grimes /* 1417df8bae1dSRodney W. Grimes * Send a signal to a process group. 1418df8bae1dSRodney W. Grimes */ 1419df8bae1dSRodney W. Grimes void 14202c42a146SMarcel Moolenaar gsignal(pgid, sig) 14212c42a146SMarcel Moolenaar int pgid, sig; 1422df8bae1dSRodney W. Grimes { 1423df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1424df8bae1dSRodney W. Grimes 1425f591779bSSeigo Tanimura if (pgid != 0) { 1426ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1427f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1428ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1429f591779bSSeigo Tanimura if (pgrp != NULL) { 14302c42a146SMarcel Moolenaar pgsignal(pgrp, sig, 0); 1431f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1432f591779bSSeigo Tanimura } 1433f591779bSSeigo Tanimura } 1434df8bae1dSRodney W. Grimes } 1435df8bae1dSRodney W. Grimes 1436df8bae1dSRodney W. Grimes /* 1437df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1438df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1439df8bae1dSRodney W. Grimes */ 1440df8bae1dSRodney W. Grimes void 14412c42a146SMarcel Moolenaar pgsignal(pgrp, sig, checkctty) 1442df8bae1dSRodney W. Grimes struct pgrp *pgrp; 14432c42a146SMarcel Moolenaar int sig, checkctty; 1444df8bae1dSRodney W. Grimes { 1445df8bae1dSRodney W. Grimes register struct proc *p; 1446df8bae1dSRodney W. Grimes 1447628d2653SJohn Baldwin if (pgrp) { 1448f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1449628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1450628d2653SJohn Baldwin PROC_LOCK(p); 1451df8bae1dSRodney W. Grimes if (checkctty == 0 || p->p_flag & P_CONTROLT) 14522c42a146SMarcel Moolenaar psignal(p, sig); 1453628d2653SJohn Baldwin PROC_UNLOCK(p); 1454628d2653SJohn Baldwin } 1455628d2653SJohn Baldwin } 1456df8bae1dSRodney W. Grimes } 1457df8bae1dSRodney W. Grimes 1458df8bae1dSRodney W. Grimes /* 14591bf4700bSJeff Roberson * Send a signal caused by a trap to the current thread. 1460df8bae1dSRodney W. Grimes * If it will be caught immediately, deliver it with correct code. 1461df8bae1dSRodney W. Grimes * Otherwise, post it normally. 1462356861dbSMatthew Dillon * 1463356861dbSMatthew Dillon * MPSAFE 1464df8bae1dSRodney W. Grimes */ 1465df8bae1dSRodney W. Grimes void 14661bf4700bSJeff Roberson trapsignal(struct thread *td, int sig, u_long code) 1467df8bae1dSRodney W. Grimes { 14681bf4700bSJeff Roberson struct sigacts *ps; 14691bf4700bSJeff Roberson struct proc *p; 14709dde3bc9SDavid Xu siginfo_t siginfo; 14719dde3bc9SDavid Xu int error; 14721bf4700bSJeff Roberson 14731bf4700bSJeff Roberson p = td->td_proc; 14749dde3bc9SDavid Xu if (td->td_flags & TDF_SA) { 14759dde3bc9SDavid Xu thread_user_enter(p, td); 1476628d2653SJohn Baldwin PROC_LOCK(p); 14779dde3bc9SDavid Xu if (td->td_mailbox) { 14789dde3bc9SDavid Xu SIGDELSET(td->td_sigmask, sig); 14799dde3bc9SDavid Xu mtx_lock_spin(&sched_lock); 14809dde3bc9SDavid Xu /* 14819dde3bc9SDavid Xu * don't psignal to the thread, it only can accept 14829dde3bc9SDavid Xu * its own sync signal. 14839dde3bc9SDavid Xu */ 14849dde3bc9SDavid Xu td->td_flags |= TDF_NOSIGPOST; 14859dde3bc9SDavid Xu /* 14869dde3bc9SDavid Xu * Force scheduling an upcall, so UTS has chance to 14879dde3bc9SDavid Xu * process the signal before thread runs again in 14889dde3bc9SDavid Xu * userland. 14899dde3bc9SDavid Xu */ 14909dde3bc9SDavid Xu if (td->td_upcall) 14919dde3bc9SDavid Xu td->td_upcall->ku_flags |= KUF_DOUPCALL; 14929dde3bc9SDavid Xu mtx_unlock_spin(&sched_lock); 14939dde3bc9SDavid Xu } 14949dde3bc9SDavid Xu } else { 14959dde3bc9SDavid Xu PROC_LOCK(p); 14969dde3bc9SDavid Xu } 1497ef3dab76STim J. Robbins ps = p->p_sigacts; 149890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 149990af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 15004093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1501df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 1502df8bae1dSRodney W. Grimes #ifdef KTRACE 1503374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1504374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 15054093529dSJeff Roberson &td->td_sigmask, code); 1506df8bae1dSRodney W. Grimes #endif 15079dde3bc9SDavid Xu if (!(td->td_flags & TDF_SA)) 15089dde3bc9SDavid Xu (*p->p_sysent->sv_sendsig)( 15099dde3bc9SDavid Xu ps->ps_sigact[_SIG_IDX(sig)], sig, 15104093529dSJeff Roberson &td->td_sigmask, code); 15119dde3bc9SDavid Xu else { 15129dde3bc9SDavid Xu thread_siginfo(sig, code, &siginfo); 15139dde3bc9SDavid Xu mtx_unlock(&ps->ps_mtx); 15149dde3bc9SDavid Xu PROC_UNLOCK(p); 15159dde3bc9SDavid Xu error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 15169dde3bc9SDavid Xu sizeof(siginfo)); 15179dde3bc9SDavid Xu PROC_LOCK(p); 15189dde3bc9SDavid Xu if (error) 15199dde3bc9SDavid Xu sigexit(td, SIGILL); 15209dde3bc9SDavid Xu SIGADDSET(td->td_sigmask, sig); 15219dde3bc9SDavid Xu mtx_lock(&ps->ps_mtx); 15229dde3bc9SDavid Xu } 15234093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 15242c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 15254093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 15262c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1527289ccde0SPeter Wemm /* 15288f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1529289ccde0SPeter Wemm */ 153090af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 15312c42a146SMarcel Moolenaar if (sig != SIGCONT && 15322c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 153390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 15342c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1535dedc04feSPeter Wemm } 153690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15376f841fb7SMarcel Moolenaar } else { 153890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 15396626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 15402c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 15414093529dSJeff Roberson tdsignal(td, sig); 1542df8bae1dSRodney W. Grimes } 1543628d2653SJohn Baldwin PROC_UNLOCK(p); 1544df8bae1dSRodney W. Grimes } 1545df8bae1dSRodney W. Grimes 15464093529dSJeff Roberson static struct thread * 15474093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 15484093529dSJeff Roberson { 15494093529dSJeff Roberson struct thread *td; 15504093529dSJeff Roberson 15514093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 15524093529dSJeff Roberson 15534093529dSJeff Roberson /* 15544093529dSJeff Roberson * If we know the signal is bound for a specific thread then we 15554093529dSJeff Roberson * assume that we are in that threads context. This is the case 15564093529dSJeff Roberson * for SIGXCPU, SIGILL, etc. Otherwise someone did a kill() from 15574093529dSJeff Roberson * userland and the real thread doesn't actually matter. 15584093529dSJeff Roberson */ 15594093529dSJeff Roberson if ((prop & SA_PROC) != 0 && curthread->td_proc == p) 15604093529dSJeff Roberson return (curthread); 15614093529dSJeff Roberson 15624093529dSJeff Roberson /* 15634093529dSJeff Roberson * We should search for the first thread that is blocked in 15644093529dSJeff Roberson * sigsuspend with this signal unmasked. 15654093529dSJeff Roberson */ 15664093529dSJeff Roberson 15674093529dSJeff Roberson /* XXX */ 15684093529dSJeff Roberson 15694093529dSJeff Roberson /* 15704093529dSJeff Roberson * Find the first thread in the proc that doesn't have this signal 15714093529dSJeff Roberson * masked. 15724093529dSJeff Roberson */ 15734093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) 15749dde3bc9SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig) && 15759dde3bc9SDavid Xu !(td->td_flags & TDF_NOSIGPOST)) 15764093529dSJeff Roberson return (td); 15774093529dSJeff Roberson 15784093529dSJeff Roberson return (FIRST_THREAD_IN_PROC(p)); 15794093529dSJeff Roberson } 15804093529dSJeff Roberson 1581df8bae1dSRodney W. Grimes /* 1582df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1583df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1584df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1585df8bae1dSRodney W. Grimes * 1586df8bae1dSRodney W. Grimes * Exceptions: 1587df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1588df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1589df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1590df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1591df8bae1dSRodney W. Grimes * 1592df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 159390af4afaSJohn Baldwin * 159490af4afaSJohn Baldwin * MPSAFE 1595df8bae1dSRodney W. Grimes */ 1596df8bae1dSRodney W. Grimes void 15974093529dSJeff Roberson psignal(struct proc *p, int sig) 1598df8bae1dSRodney W. Grimes { 1599b40ce416SJulian Elischer struct thread *td; 16004093529dSJeff Roberson int prop; 16014093529dSJeff Roberson 16024093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 16034093529dSJeff Roberson prop = sigprop(sig); 16044093529dSJeff Roberson 16054093529dSJeff Roberson /* 16064093529dSJeff Roberson * Find a thread to deliver the signal to. 16074093529dSJeff Roberson */ 16084093529dSJeff Roberson td = sigtd(p, sig, prop); 16094093529dSJeff Roberson 16104093529dSJeff Roberson tdsignal(td, sig); 16114093529dSJeff Roberson } 16124093529dSJeff Roberson 161390af4afaSJohn Baldwin /* 161490af4afaSJohn Baldwin * MPSAFE 161590af4afaSJohn Baldwin */ 16164093529dSJeff Roberson void 16174093529dSJeff Roberson tdsignal(struct thread *td, int sig) 16184093529dSJeff Roberson { 16199dde3bc9SDavid Xu sigset_t saved; 16209dde3bc9SDavid Xu struct proc *p = td->td_proc; 16219dde3bc9SDavid Xu 16229dde3bc9SDavid Xu if (p->p_flag & P_SA) 16239dde3bc9SDavid Xu saved = p->p_siglist; 16249dde3bc9SDavid Xu do_tdsignal(td, sig); 16259dde3bc9SDavid Xu if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 16269dde3bc9SDavid Xu if (SIGSETEQ(saved, p->p_siglist)) 16279dde3bc9SDavid Xu return; 16289dde3bc9SDavid Xu else { 16299dde3bc9SDavid Xu /* pending set changed */ 16309dde3bc9SDavid Xu p->p_flag |= P_SIGEVENT; 16319dde3bc9SDavid Xu wakeup(&p->p_siglist); 16329dde3bc9SDavid Xu } 16339dde3bc9SDavid Xu } 16349dde3bc9SDavid Xu } 16359dde3bc9SDavid Xu 16369dde3bc9SDavid Xu static void 16379dde3bc9SDavid Xu do_tdsignal(struct thread *td, int sig) 16389dde3bc9SDavid Xu { 16394093529dSJeff Roberson struct proc *p; 16404093529dSJeff Roberson register sig_t action; 16414093529dSJeff Roberson sigset_t *siglist; 16424093529dSJeff Roberson struct thread *td0; 1643e602ba25SJulian Elischer register int prop; 164490af4afaSJohn Baldwin struct sigacts *ps; 1645df8bae1dSRodney W. Grimes 16462899d606SDag-Erling Smørgrav KASSERT(_SIG_VALID(sig), 16474093529dSJeff Roberson ("tdsignal(): invalid signal %d\n", sig)); 16484093529dSJeff Roberson 16494093529dSJeff Roberson p = td->td_proc; 165090af4afaSJohn Baldwin ps = p->p_sigacts; 16512c42a146SMarcel Moolenaar 1652628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1653cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1654cb679c38SJonathan Lemon 16552c42a146SMarcel Moolenaar prop = sigprop(sig); 16564093529dSJeff Roberson 16574093529dSJeff Roberson /* 16584093529dSJeff Roberson * If this thread is blocking this signal then we'll leave it in the 16594093529dSJeff Roberson * proc so that we can find it in the first thread that unblocks it. 16604093529dSJeff Roberson */ 16614093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 16624093529dSJeff Roberson siglist = &p->p_siglist; 16634093529dSJeff Roberson else 16644093529dSJeff Roberson siglist = &td->td_siglist; 16654093529dSJeff Roberson 1666df8bae1dSRodney W. Grimes /* 16672a024a2bSSean Eric Fagan * If proc is traced, always give parent a chance; 16682a024a2bSSean Eric Fagan * if signal event is tracked by procfs, give *that* 16692a024a2bSSean Eric Fagan * a chance, as well. 1670df8bae1dSRodney W. Grimes */ 1671b40ce416SJulian Elischer if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1672df8bae1dSRodney W. Grimes action = SIG_DFL; 1673b40ce416SJulian Elischer } else { 1674df8bae1dSRodney W. Grimes /* 1675df8bae1dSRodney W. Grimes * If the signal is being ignored, 1676df8bae1dSRodney W. Grimes * then we forget about it immediately. 167790af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 1678df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 1679df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 1680df8bae1dSRodney W. Grimes */ 168190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 168290af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) || 168390af4afaSJohn Baldwin (p->p_flag & P_WEXIT)) { 168490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1685df8bae1dSRodney W. Grimes return; 168690af4afaSJohn Baldwin } 16874093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 1688df8bae1dSRodney W. Grimes action = SIG_HOLD; 168990af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1690df8bae1dSRodney W. Grimes action = SIG_CATCH; 1691df8bae1dSRodney W. Grimes else 1692df8bae1dSRodney W. Grimes action = SIG_DFL; 169390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1694df8bae1dSRodney W. Grimes } 1695df8bae1dSRodney W. Grimes 16964093529dSJeff Roberson if (prop & SA_CONT) { 16971d9c5696SJuli Mallett SIG_STOPSIGMASK(p->p_siglist); 16984093529dSJeff Roberson /* 16994093529dSJeff Roberson * XXX Should investigate leaving STOP and CONT sigs only in 17004093529dSJeff Roberson * the proc's siglist. 17014093529dSJeff Roberson */ 17024093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17034093529dSJeff Roberson SIG_STOPSIGMASK(td0->td_siglist); 17044093529dSJeff Roberson } 1705df8bae1dSRodney W. Grimes 1706df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 1707df8bae1dSRodney W. Grimes /* 1708df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 1709df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 1710df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 1711df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 1712df8bae1dSRodney W. Grimes */ 1713e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 1714e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 1715e602ba25SJulian Elischer (action == SIG_DFL)) 1716df8bae1dSRodney W. Grimes return; 17171d9c5696SJuli Mallett SIG_CONTSIGMASK(p->p_siglist); 17184093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 17194093529dSJeff Roberson SIG_CONTSIGMASK(td0->td_siglist); 17206933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 1721df8bae1dSRodney W. Grimes } 17224093529dSJeff Roberson SIGADDSET(*siglist, sig); 17234093529dSJeff Roberson signotify(td); /* uses schedlock */ 17245312b1c7SDavid Xu /* 17255312b1c7SDavid Xu * Defer further processing for signals which are held, 17265312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 17275312b1c7SDavid Xu */ 17285312b1c7SDavid Xu if (action == SIG_HOLD && 17295312b1c7SDavid Xu !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 17305312b1c7SDavid Xu return; 1731df8bae1dSRodney W. Grimes /* 1732e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 1733e602ba25SJulian Elischer * component. Most processing occurs when the process next 1734e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 1735e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 1736e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 1737e602ba25SJulian Elischer * We try do the per-process part here. 1738df8bae1dSRodney W. Grimes */ 1739e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 1740e602ba25SJulian Elischer /* 1741e602ba25SJulian Elischer * The process is in stopped mode. All the threads should be 1742e602ba25SJulian Elischer * either winding down or already on the suspended queue. 1743e602ba25SJulian Elischer */ 1744e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1745e602ba25SJulian Elischer /* 1746e602ba25SJulian Elischer * The traced process is already stopped, 1747e602ba25SJulian Elischer * so no further action is necessary. 1748e602ba25SJulian Elischer * No signal can restart us. 1749e602ba25SJulian Elischer */ 1750e602ba25SJulian Elischer goto out; 17511c32c37cSJohn Baldwin } 1752b40ce416SJulian Elischer 1753e602ba25SJulian Elischer if (sig == SIGKILL) { 1754df8bae1dSRodney W. Grimes /* 1755e602ba25SJulian Elischer * SIGKILL sets process running. 1756e602ba25SJulian Elischer * It will die elsewhere. 1757e602ba25SJulian Elischer * All threads must be restarted. 1758df8bae1dSRodney W. Grimes */ 1759e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED; 1760e602ba25SJulian Elischer goto runfast; 1761e602ba25SJulian Elischer } 1762e602ba25SJulian Elischer 1763e602ba25SJulian Elischer if (prop & SA_CONT) { 1764e602ba25SJulian Elischer /* 1765e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 17664093529dSJeff Roberson * process but don't leave the signal in siglist as 17671d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 1768e602ba25SJulian Elischer * continue the process and leave the signal in 17694093529dSJeff Roberson * siglist. If the process catches SIGCONT, let it 1770e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 1771e602ba25SJulian Elischer * an event, it goes back to run state. 1772e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 1773e602ba25SJulian Elischer */ 17741279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 17756933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 1776e602ba25SJulian Elischer if (action == SIG_DFL) { 17774093529dSJeff Roberson SIGDELSET(*siglist, sig); 1778e602ba25SJulian Elischer } else if (action == SIG_CATCH) { 1779e602ba25SJulian Elischer /* 1780e602ba25SJulian Elischer * The process wants to catch it so it needs 1781e602ba25SJulian Elischer * to run at least one thread, but which one? 1782e602ba25SJulian Elischer * It would seem that the answer would be to 1783e602ba25SJulian Elischer * run an upcall in the next KSE to run, and 1784e602ba25SJulian Elischer * deliver the signal that way. In a NON KSE 1785e602ba25SJulian Elischer * process, we need to make sure that the 1786e602ba25SJulian Elischer * single thread is runnable asap. 1787e602ba25SJulian Elischer * XXXKSE for now however, make them all run. 1788e602ba25SJulian Elischer */ 1789e602ba25SJulian Elischer goto runfast; 1790e602ba25SJulian Elischer } 1791e602ba25SJulian Elischer /* 1792e602ba25SJulian Elischer * The signal is not ignored or caught. 1793e602ba25SJulian Elischer */ 1794e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 179504774f23SJulian Elischer thread_unsuspend(p); 1796e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1797e602ba25SJulian Elischer goto out; 1798e602ba25SJulian Elischer } 1799e602ba25SJulian Elischer 1800e602ba25SJulian Elischer if (prop & SA_STOP) { 1801e602ba25SJulian Elischer /* 1802e602ba25SJulian Elischer * Already stopped, don't need to stop again 1803e602ba25SJulian Elischer * (If we did the shell could get confused). 180404774f23SJulian Elischer * Just make sure the signal STOP bit set. 1805e602ba25SJulian Elischer */ 18061279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 18074093529dSJeff Roberson SIGDELSET(*siglist, sig); 1808e602ba25SJulian Elischer goto out; 1809e602ba25SJulian Elischer } 1810e602ba25SJulian Elischer 1811e602ba25SJulian Elischer /* 1812e602ba25SJulian Elischer * All other kinds of signals: 1813e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 1814e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 1815e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 181604774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 1817e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 1818e602ba25SJulian Elischer */ 1819e602ba25SJulian Elischer mtx_lock_spin(&sched_lock); 182071fad9fdSJulian Elischer if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) { 1821e602ba25SJulian Elischer if (td->td_flags & TDF_CVWAITQ) 182271fad9fdSJulian Elischer cv_abort(td); 1823e602ba25SJulian Elischer else 182471fad9fdSJulian Elischer abortsleep(td); 1825e602ba25SJulian Elischer } 1826e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1827df8bae1dSRodney W. Grimes goto out; 1828df8bae1dSRodney W. Grimes /* 1829e602ba25SJulian Elischer * XXXKSE What about threads that are waiting on mutexes? 1830e602ba25SJulian Elischer * Shouldn't they abort too? 183104774f23SJulian Elischer * No, hopefully mutexes are short lived.. They'll 183204774f23SJulian Elischer * eventually hit thread_suspend_check(). 1833df8bae1dSRodney W. Grimes */ 1834e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 183535c32a76SDavid Xu if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 183635c32a76SDavid Xu !(prop & SA_STOP)) { 1837721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 18384093529dSJeff Roberson tdsigwakeup(td, sig, action); 1839721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 1840df8bae1dSRodney W. Grimes goto out; 184104774f23SJulian Elischer } 184235c32a76SDavid Xu if (prop & SA_STOP) { 184335c32a76SDavid Xu if (p->p_flag & P_PPWAIT) 184435c32a76SDavid Xu goto out; 1845e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 1846e574e444SDavid Xu p->p_xstat = sig; 184735c32a76SDavid Xu mtx_lock_spin(&sched_lock); 18484093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) { 18494093529dSJeff Roberson if (TD_IS_SLEEPING(td0) && 1850062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 1851062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 18524093529dSJeff Roberson thread_suspend_one(td0); 1853062cf543SDavid Xu } else if (td != td0) { 1854062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 1855062cf543SDavid Xu } 185635c32a76SDavid Xu } 1857e574e444SDavid Xu thread_stopped(p); 18584093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 1859e574e444SDavid Xu SIGDELSET(p->p_siglist, p->p_xstat); 18604093529dSJeff Roberson FOREACH_THREAD_IN_PROC(p, td0) 18614093529dSJeff Roberson SIGDELSET(td0->td_siglist, p->p_xstat); 18624093529dSJeff Roberson } 186335c32a76SDavid Xu mtx_unlock_spin(&sched_lock); 186435c32a76SDavid Xu goto out; 186535c32a76SDavid Xu } 1866721e5910SJulian Elischer else 1867df8bae1dSRodney W. Grimes goto runfast; 1868df8bae1dSRodney W. Grimes /* NOTREACHED */ 1869e602ba25SJulian Elischer } else { 1870e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 18714093529dSJeff Roberson SIGDELSET(*siglist, sig); 1872e602ba25SJulian Elischer goto out; 1873e602ba25SJulian Elischer } 1874e602ba25SJulian Elischer 1875b40ce416SJulian Elischer /* 1876e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 1877e602ba25SJulian Elischer * running threads. 1878b40ce416SJulian Elischer */ 1879e602ba25SJulian Elischer 1880e602ba25SJulian Elischer runfast: 1881aa0fa334SJulian Elischer mtx_lock_spin(&sched_lock); 18824093529dSJeff Roberson tdsigwakeup(td, sig, action); 1883e602ba25SJulian Elischer thread_unsuspend(p); 1884e602ba25SJulian Elischer mtx_unlock_spin(&sched_lock); 1885e602ba25SJulian Elischer out: 1886e602ba25SJulian Elischer /* If we jump here, sched_lock should not be owned. */ 1887e602ba25SJulian Elischer mtx_assert(&sched_lock, MA_NOTOWNED); 1888e602ba25SJulian Elischer } 1889e602ba25SJulian Elischer 1890e602ba25SJulian Elischer /* 1891e602ba25SJulian Elischer * The force of a signal has been directed against a single 1892e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 1893e602ba25SJulian Elischer * out of any sleep it may be in etc. 1894e602ba25SJulian Elischer */ 1895e602ba25SJulian Elischer static void 18964093529dSJeff Roberson tdsigwakeup(struct thread *td, int sig, sig_t action) 1897e602ba25SJulian Elischer { 1898e602ba25SJulian Elischer struct proc *p = td->td_proc; 1899e602ba25SJulian Elischer register int prop; 1900e602ba25SJulian Elischer 19018b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1902aa0fa334SJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 1903e602ba25SJulian Elischer prop = sigprop(sig); 1904e602ba25SJulian Elischer /* 1905aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 1906e602ba25SJulian Elischer * killed in this lifetime. 1907e602ba25SJulian Elischer */ 1908e602ba25SJulian Elischer if ((action == SIG_DFL) && (prop & SA_KILL)) { 1909e602ba25SJulian Elischer if (td->td_priority > PUSER) { 1910e602ba25SJulian Elischer td->td_priority = PUSER; 1911b40ce416SJulian Elischer } 1912b40ce416SJulian Elischer } 191371fad9fdSJulian Elischer if (TD_IS_SLEEPING(td)) { 1914e602ba25SJulian Elischer /* 1915e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 1916e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 1917e602ba25SJulian Elischer * be noticed when the process returns through 1918e602ba25SJulian Elischer * trap() or syscall(). 1919e602ba25SJulian Elischer */ 1920e602ba25SJulian Elischer if ((td->td_flags & TDF_SINTR) == 0) { 1921aa0fa334SJulian Elischer return; 1922e602ba25SJulian Elischer } 1923e602ba25SJulian Elischer /* 1924e602ba25SJulian Elischer * Process is sleeping and traced. Make it runnable 1925e602ba25SJulian Elischer * so it can discover the signal in issignal() and stop 1926e602ba25SJulian Elischer * for its parent. 1927e602ba25SJulian Elischer */ 1928e602ba25SJulian Elischer if (p->p_flag & P_TRACED) { 1929e602ba25SJulian Elischer p->p_flag &= ~P_STOPPED_TRACE; 1930aa0fa334SJulian Elischer } else { 1931aa0fa334SJulian Elischer 1932df8bae1dSRodney W. Grimes /* 1933e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 1934e602ba25SJulian Elischer * asleep, we are finished; the process should not 1935e602ba25SJulian Elischer * be awakened. 1936df8bae1dSRodney W. Grimes */ 1937e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 19381d9c5696SJuli Mallett SIGDELSET(p->p_siglist, sig); 19394093529dSJeff Roberson /* 19404093529dSJeff Roberson * It may be on either list in this state. 19414093529dSJeff Roberson * Remove from both for now. 19424093529dSJeff Roberson */ 19434093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 1944aa0fa334SJulian Elischer return; 1945df8bae1dSRodney W. Grimes } 1946df8bae1dSRodney W. Grimes 1947aa0fa334SJulian Elischer /* 1948aa0fa334SJulian Elischer * Raise priority to at least PUSER. 1949aa0fa334SJulian Elischer */ 1950aa0fa334SJulian Elischer if (td->td_priority > PUSER) { 1951aa0fa334SJulian Elischer td->td_priority = PUSER; 1952aa0fa334SJulian Elischer } 1953aa0fa334SJulian Elischer } 195471fad9fdSJulian Elischer if (td->td_flags & TDF_CVWAITQ) 195571fad9fdSJulian Elischer cv_abort(td); 195671fad9fdSJulian Elischer else 195771fad9fdSJulian Elischer abortsleep(td); 1958aa0fa334SJulian Elischer } 1959aa0fa334SJulian Elischer #ifdef SMP 1960aa0fa334SJulian Elischer else { 1961df8bae1dSRodney W. Grimes /* 1962e602ba25SJulian Elischer * Other states do nothing with the signal immediatly, 1963df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 1964df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 1965df8bae1dSRodney W. Grimes */ 196671fad9fdSJulian Elischer if (TD_IS_RUNNING(td) && td != curthread) { 1967e602ba25SJulian Elischer forward_signal(td); 1968aa0fa334SJulian Elischer } 19690ac3b636SAndrew Gallatin } 19703163861cSTor Egge #endif 19716caa8a15SJohn Baldwin } 1972df8bae1dSRodney W. Grimes 1973df8bae1dSRodney W. Grimes /* 1974df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 1975df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 1976df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 1977df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 1978df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 1979628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 1980df8bae1dSRodney W. Grimes * sequence is 1981df8bae1dSRodney W. Grimes * 1982e602ba25SJulian Elischer * while (sig = cursig(curthread)) 19832c42a146SMarcel Moolenaar * postsig(sig); 1984df8bae1dSRodney W. Grimes */ 19856711f10fSJohn Baldwin static int 1986e602ba25SJulian Elischer issignal(td) 1987e602ba25SJulian Elischer struct thread *td; 1988df8bae1dSRodney W. Grimes { 1989e602ba25SJulian Elischer struct proc *p; 199090af4afaSJohn Baldwin struct sigacts *ps; 19914093529dSJeff Roberson sigset_t sigpending; 1992062cf543SDavid Xu int sig, prop; 1993062cf543SDavid Xu struct thread *td0; 1994df8bae1dSRodney W. Grimes 1995e602ba25SJulian Elischer p = td->td_proc; 199690af4afaSJohn Baldwin ps = p->p_sigacts; 199790af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 1998628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1999df8bae1dSRodney W. Grimes for (;;) { 20002a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 20012a024a2bSSean Eric Fagan 20024093529dSJeff Roberson sigpending = td->td_siglist; 20034093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 20044093529dSJeff Roberson 2005df8bae1dSRodney W. Grimes if (p->p_flag & P_PPWAIT) 20064093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 20074093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2008df8bae1dSRodney W. Grimes return (0); 20094093529dSJeff Roberson sig = sig_ffs(&sigpending); 20102a024a2bSSean Eric Fagan 2011628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 20122a024a2bSSean Eric Fagan 2013df8bae1dSRodney W. Grimes /* 2014df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2015df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2016df8bae1dSRodney W. Grimes */ 201790af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 20184093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 2019df8bae1dSRodney W. Grimes continue; 2020df8bae1dSRodney W. Grimes } 2021df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2022df8bae1dSRodney W. Grimes /* 2023d8f4f6a4SJonathan Mini * If traced, always stop. 2024df8bae1dSRodney W. Grimes */ 202590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 202690af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 202790af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Stopping for traced signal"); 20282c42a146SMarcel Moolenaar p->p_xstat = sig; 2029628d2653SJohn Baldwin PROC_LOCK(p->p_pptr); 2030df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 2031628d2653SJohn Baldwin PROC_UNLOCK(p->p_pptr); 20329ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 20334d492b43SJulian Elischer stop(p); /* uses schedlock too eventually */ 203471fad9fdSJulian Elischer thread_suspend_one(td); 2035c86b6ff5SJohn Baldwin PROC_UNLOCK(p); 2036c86b6ff5SJohn Baldwin DROP_GIANT(); 20372ad7d304SJohn Baldwin p->p_stats->p_ru.ru_nivcsw++; 2038df8bae1dSRodney W. Grimes mi_switch(); 20399ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 204020cdcc5bSJohn Baldwin PICKUP_GIANT(); 2041628d2653SJohn Baldwin PROC_LOCK(p); 204290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2043df8bae1dSRodney W. Grimes 2044df8bae1dSRodney W. Grimes /* 2045df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 2046df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 2047df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 2048df8bae1dSRodney W. Grimes */ 20494093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* clear old signal */ 20502c42a146SMarcel Moolenaar sig = p->p_xstat; 20512c42a146SMarcel Moolenaar if (sig == 0) 2052df8bae1dSRodney W. Grimes continue; 2053df8bae1dSRodney W. Grimes 2054df8bae1dSRodney W. Grimes /* 20558d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 20568d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 20578d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 20588d542cb5SDavid E. O'Brien */ 20598d542cb5SDavid E. O'Brien if ((p->p_flag & P_TRACED) == 0) 20608d542cb5SDavid E. O'Brien continue; 20618d542cb5SDavid E. O'Brien 20628d542cb5SDavid E. O'Brien /* 20634093529dSJeff Roberson * Put the new signal into td_siglist. If the 20641d9c5696SJuli Mallett * signal is being masked, look for other signals. 2065df8bae1dSRodney W. Grimes */ 20664093529dSJeff Roberson SIGADDSET(td->td_siglist, sig); 20674093529dSJeff Roberson if (SIGISMEMBER(td->td_sigmask, sig)) 2068df8bae1dSRodney W. Grimes continue; 20694093529dSJeff Roberson signotify(td); 2070df8bae1dSRodney W. Grimes } 2071df8bae1dSRodney W. Grimes 20728d542cb5SDavid E. O'Brien prop = sigprop(sig); 20738d542cb5SDavid E. O'Brien 2074df8bae1dSRodney W. Grimes /* 2075df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2076df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2077df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2078df8bae1dSRodney W. Grimes */ 2079d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2080df8bae1dSRodney W. Grimes 2081d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2082df8bae1dSRodney W. Grimes /* 2083df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2084df8bae1dSRodney W. Grimes */ 2085df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2086df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2087df8bae1dSRodney W. Grimes /* 2088df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2089df8bae1dSRodney W. Grimes * in init? XXX 2090df8bae1dSRodney W. Grimes */ 2091d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 20922c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2093df8bae1dSRodney W. Grimes #endif 2094df8bae1dSRodney W. Grimes break; /* == ignore */ 2095df8bae1dSRodney W. Grimes } 2096df8bae1dSRodney W. Grimes /* 2097df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 2098df8bae1dSRodney W. Grimes * with default action, stop here, 2099df8bae1dSRodney W. Grimes * then clear the signal. However, 2100df8bae1dSRodney W. Grimes * if process is member of an orphaned 2101df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 2102df8bae1dSRodney W. Grimes */ 2103df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 2104df8bae1dSRodney W. Grimes if (p->p_flag & P_TRACED || 2105df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2106df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 2107df8bae1dSRodney W. Grimes break; /* == ignore */ 210890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 210990af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 211090af4afaSJohn Baldwin &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2111e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 21122c42a146SMarcel Moolenaar p->p_xstat = sig; 2113721e5910SJulian Elischer mtx_lock_spin(&sched_lock); 2114062cf543SDavid Xu FOREACH_THREAD_IN_PROC(p, td0) { 2115062cf543SDavid Xu if (TD_IS_SLEEPING(td0) && 2116062cf543SDavid Xu (td0->td_flags & TDF_SINTR) && 2117062cf543SDavid Xu !TD_IS_SUSPENDED(td0)) { 2118062cf543SDavid Xu thread_suspend_one(td0); 2119062cf543SDavid Xu } else if (td != td0) { 2120062cf543SDavid Xu td0->td_flags |= TDF_ASTPENDING; 2121062cf543SDavid Xu } 2122062cf543SDavid Xu } 2123e574e444SDavid Xu thread_stopped(p); 212471fad9fdSJulian Elischer thread_suspend_one(td); 2125721e5910SJulian Elischer PROC_UNLOCK(p); 2126721e5910SJulian Elischer DROP_GIANT(); 2127721e5910SJulian Elischer p->p_stats->p_ru.ru_nivcsw++; 2128721e5910SJulian Elischer mi_switch(); 2129721e5910SJulian Elischer mtx_unlock_spin(&sched_lock); 2130721e5910SJulian Elischer PICKUP_GIANT(); 2131721e5910SJulian Elischer PROC_LOCK(p); 213290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2133df8bae1dSRodney W. Grimes break; 213421b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 2135df8bae1dSRodney W. Grimes /* 2136df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2137df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2138df8bae1dSRodney W. Grimes */ 2139df8bae1dSRodney W. Grimes break; /* == ignore */ 2140df8bae1dSRodney W. Grimes } else 21412c42a146SMarcel Moolenaar return (sig); 2142df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2143df8bae1dSRodney W. Grimes 2144d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2145df8bae1dSRodney W. Grimes /* 2146df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2147df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2148df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2149df8bae1dSRodney W. Grimes */ 2150df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 2151df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2152df8bae1dSRodney W. Grimes printf("issignal\n"); 2153df8bae1dSRodney W. Grimes break; /* == ignore */ 2154df8bae1dSRodney W. Grimes 2155df8bae1dSRodney W. Grimes default: 2156df8bae1dSRodney W. Grimes /* 2157df8bae1dSRodney W. Grimes * This signal has an action, let 2158df8bae1dSRodney W. Grimes * postsig() process it. 2159df8bae1dSRodney W. Grimes */ 21602c42a146SMarcel Moolenaar return (sig); 2161df8bae1dSRodney W. Grimes } 21624093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2163df8bae1dSRodney W. Grimes } 2164df8bae1dSRodney W. Grimes /* NOTREACHED */ 2165df8bae1dSRodney W. Grimes } 2166df8bae1dSRodney W. Grimes 2167df8bae1dSRodney W. Grimes /* 2168df8bae1dSRodney W. Grimes * Put the argument process into the stopped state and notify the parent 2169df8bae1dSRodney W. Grimes * via wakeup. Signals are handled elsewhere. The process must not be 21705b3047d5SJohn Baldwin * on the run queue. Must be called with the proc p locked and the scheduler 21715b3047d5SJohn Baldwin * lock held. 2172df8bae1dSRodney W. Grimes */ 21735b3047d5SJohn Baldwin static void 2174e574e444SDavid Xu stop(struct proc *p) 2175df8bae1dSRodney W. Grimes { 2176df8bae1dSRodney W. Grimes 2177628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 21781279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 2179df8bae1dSRodney W. Grimes p->p_flag &= ~P_WAITED; 218001609114SAlfred Perlstein wakeup(p->p_pptr); 2181df8bae1dSRodney W. Grimes } 2182df8bae1dSRodney W. Grimes 218390af4afaSJohn Baldwin /* 218490af4afaSJohn Baldwin * MPSAFE 218590af4afaSJohn Baldwin */ 2186e574e444SDavid Xu void 2187e574e444SDavid Xu thread_stopped(struct proc *p) 2188e574e444SDavid Xu { 2189e574e444SDavid Xu struct proc *p1 = curthread->td_proc; 219090af4afaSJohn Baldwin struct sigacts *ps; 2191e574e444SDavid Xu int n; 2192e574e444SDavid Xu 2193e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 2194e574e444SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 2195e574e444SDavid Xu n = p->p_suspcount; 2196e574e444SDavid Xu if (p == p1) 2197e574e444SDavid Xu n++; 2198e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2199e574e444SDavid Xu mtx_unlock_spin(&sched_lock); 2200e574e444SDavid Xu stop(p); 2201e574e444SDavid Xu PROC_LOCK(p->p_pptr); 220290af4afaSJohn Baldwin ps = p->p_pptr->p_sigacts; 220390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 220490af4afaSJohn Baldwin if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 220590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2206e574e444SDavid Xu psignal(p->p_pptr, SIGCHLD); 220790af4afaSJohn Baldwin } else 220890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2209e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 2210e574e444SDavid Xu mtx_lock_spin(&sched_lock); 2211e574e444SDavid Xu } 2212e574e444SDavid Xu } 2213e574e444SDavid Xu 2214df8bae1dSRodney W. Grimes /* 2215df8bae1dSRodney W. Grimes * Take the action for the specified signal 2216df8bae1dSRodney W. Grimes * from the current set of pending signals. 2217df8bae1dSRodney W. Grimes */ 2218df8bae1dSRodney W. Grimes void 22192c42a146SMarcel Moolenaar postsig(sig) 22202c42a146SMarcel Moolenaar register int sig; 2221df8bae1dSRodney W. Grimes { 2222b40ce416SJulian Elischer struct thread *td = curthread; 2223b40ce416SJulian Elischer register struct proc *p = td->td_proc; 2224628d2653SJohn Baldwin struct sigacts *ps; 22252c42a146SMarcel Moolenaar sig_t action; 22262c42a146SMarcel Moolenaar sigset_t returnmask; 22272c42a146SMarcel Moolenaar int code; 2228df8bae1dSRodney W. Grimes 22292c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 22305526d2d9SEivind Eklund 22312ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2232628d2653SJohn Baldwin ps = p->p_sigacts; 223390af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 22344093529dSJeff Roberson SIGDELSET(td->td_siglist, sig); 22352c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2236df8bae1dSRodney W. Grimes #ifdef KTRACE 2237374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 22385e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 22394093529dSJeff Roberson &td->td_oldsigmask : &td->td_sigmask, 0); 2240df8bae1dSRodney W. Grimes #endif 2241628d2653SJohn Baldwin _STOPEVENT(p, S_SIG, sig); 22422a024a2bSSean Eric Fagan 2243df8bae1dSRodney W. Grimes if (action == SIG_DFL) { 2244df8bae1dSRodney W. Grimes /* 2245df8bae1dSRodney W. Grimes * Default action, where the default is to kill 2246df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 2247df8bae1dSRodney W. Grimes */ 224890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2249b40ce416SJulian Elischer sigexit(td, sig); 2250df8bae1dSRodney W. Grimes /* NOTREACHED */ 2251df8bae1dSRodney W. Grimes } else { 2252df8bae1dSRodney W. Grimes /* 2253df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 2254df8bae1dSRodney W. Grimes */ 22554093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 22565526d2d9SEivind Eklund ("postsig action")); 2257df8bae1dSRodney W. Grimes /* 2258df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 2259645682fdSLuoqi Chen * occurrences of this signal. 2260df8bae1dSRodney W. Grimes * 2261645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 2262df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 2263645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 2264df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 2265df8bae1dSRodney W. Grimes */ 22665e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 22674093529dSJeff Roberson returnmask = td->td_oldsigmask; 22685e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 2269df8bae1dSRodney W. Grimes } else 22704093529dSJeff Roberson returnmask = td->td_sigmask; 22712c42a146SMarcel Moolenaar 22724093529dSJeff Roberson SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 22732c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_signodefer, sig)) 22744093529dSJeff Roberson SIGADDSET(td->td_sigmask, sig); 22752c42a146SMarcel Moolenaar 22762c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2277289ccde0SPeter Wemm /* 22788f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 2279289ccde0SPeter Wemm */ 228090af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 22812c42a146SMarcel Moolenaar if (sig != SIGCONT && 22822c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 228390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 22842c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2285dedc04feSPeter Wemm } 2286df8bae1dSRodney W. Grimes p->p_stats->p_ru.ru_nsignals++; 22872c42a146SMarcel Moolenaar if (p->p_sig != sig) { 2288df8bae1dSRodney W. Grimes code = 0; 2289df8bae1dSRodney W. Grimes } else { 22906626c604SJulian Elischer code = p->p_code; 22916626c604SJulian Elischer p->p_code = 0; 22926626c604SJulian Elischer p->p_sig = 0; 2293df8bae1dSRodney W. Grimes } 2294cd4f6ebbSDavid Xu if (td->td_flags & TDF_SA) 229558a3c273SJeff Roberson thread_signal_add(curthread, sig); 229658a3c273SJeff Roberson else 229758a3c273SJeff Roberson (*p->p_sysent->sv_sendsig)(action, sig, 229858a3c273SJeff Roberson &returnmask, code); 2299df8bae1dSRodney W. Grimes } 2300df8bae1dSRodney W. Grimes } 2301df8bae1dSRodney W. Grimes 2302df8bae1dSRodney W. Grimes /* 2303df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2304df8bae1dSRodney W. Grimes */ 230526f9a767SRodney W. Grimes void 2306df8bae1dSRodney W. Grimes killproc(p, why) 2307df8bae1dSRodney W. Grimes struct proc *p; 2308df8bae1dSRodney W. Grimes char *why; 2309df8bae1dSRodney W. Grimes { 23109081e5e8SJohn Baldwin 23119081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 23120384fff8SJason Evans CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 23130384fff8SJason Evans p, p->p_pid, p->p_comm); 2314729b1e51SDavid Greenman log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2315b1fc0ec1SRobert Watson p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2316df8bae1dSRodney W. Grimes psignal(p, SIGKILL); 2317df8bae1dSRodney W. Grimes } 2318df8bae1dSRodney W. Grimes 2319df8bae1dSRodney W. Grimes /* 2320df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2321df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2322df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2323df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2324df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2325df8bae1dSRodney W. Grimes * does not return. 232690af4afaSJohn Baldwin * 232790af4afaSJohn Baldwin * MPSAFE 2328df8bae1dSRodney W. Grimes */ 232926f9a767SRodney W. Grimes void 2330b40ce416SJulian Elischer sigexit(td, sig) 2331b40ce416SJulian Elischer struct thread *td; 23322c42a146SMarcel Moolenaar int sig; 2333df8bae1dSRodney W. Grimes { 2334b40ce416SJulian Elischer struct proc *p = td->td_proc; 2335df8bae1dSRodney W. Grimes 2336628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2337df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 23382c42a146SMarcel Moolenaar if (sigprop(sig) & SA_CORE) { 23392c42a146SMarcel Moolenaar p->p_sig = sig; 2340c364e17eSAndrey A. Chernov /* 2341c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2342c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2343c364e17eSAndrey A. Chernov * these messages.) 2344c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 2345c364e17eSAndrey A. Chernov */ 2346628d2653SJohn Baldwin PROC_UNLOCK(p); 2347c31146a1SJohn Baldwin if (!mtx_owned(&Giant)) 2348c31146a1SJohn Baldwin mtx_lock(&Giant); 2349b40ce416SJulian Elischer if (coredump(td) == 0) 23502c42a146SMarcel Moolenaar sig |= WCOREFLAG; 235157308494SJoerg Wunsch if (kern_logsigexit) 235257308494SJoerg Wunsch log(LOG_INFO, 235357308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 23543d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 23559c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 23562c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 23572c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 2358c31146a1SJohn Baldwin } else { 2359628d2653SJohn Baldwin PROC_UNLOCK(p); 2360628d2653SJohn Baldwin if (!mtx_owned(&Giant)) 2361628d2653SJohn Baldwin mtx_lock(&Giant); 2362c31146a1SJohn Baldwin } 2363b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2364df8bae1dSRodney W. Grimes /* NOTREACHED */ 2365df8bae1dSRodney W. Grimes } 2366df8bae1dSRodney W. Grimes 2367c5edb423SSean Eric Fagan static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 2368c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2369c5edb423SSean Eric Fagan sizeof(corefilename), "process corefile name format string"); 2370c5edb423SSean Eric Fagan 2371c5edb423SSean Eric Fagan /* 2372c5edb423SSean Eric Fagan * expand_name(name, uid, pid) 2373c5edb423SSean Eric Fagan * Expand the name described in corefilename, using name, uid, and pid. 2374c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 2375c5edb423SSean Eric Fagan * %N name of process ("name") 2376c5edb423SSean Eric Fagan * %P process id (pid) 2377c5edb423SSean Eric Fagan * %U user id (uid) 2378c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 2379c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2380c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 2381c5edb423SSean Eric Fagan */ 2382c5edb423SSean Eric Fagan 2383fca666a1SJulian Elischer static char * 2384c5edb423SSean Eric Fagan expand_name(name, uid, pid) 23858b43b535SAlfred Perlstein const char *name; 23868b43b535SAlfred Perlstein uid_t uid; 23878b43b535SAlfred Perlstein pid_t pid; 23888b43b535SAlfred Perlstein { 23898b43b535SAlfred Perlstein const char *format, *appendstr; 2390c5edb423SSean Eric Fagan char *temp; 2391c5edb423SSean Eric Fagan char buf[11]; /* Buffer for pid/uid -- max 4B */ 23928b43b535SAlfred Perlstein size_t i, l, n; 2393c5edb423SSean Eric Fagan 23948b43b535SAlfred Perlstein format = corefilename; 23958b43b535SAlfred Perlstein temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 23960bfe2990SEivind Eklund if (temp == NULL) 23978b43b535SAlfred Perlstein return (NULL); 2398ce38ca0fSAlfred Perlstein for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2399c5edb423SSean Eric Fagan switch (format[i]) { 2400c5edb423SSean Eric Fagan case '%': /* Format character */ 2401c5edb423SSean Eric Fagan i++; 2402c5edb423SSean Eric Fagan switch (format[i]) { 2403c5edb423SSean Eric Fagan case '%': 24048b43b535SAlfred Perlstein appendstr = "%"; 2405c5edb423SSean Eric Fagan break; 2406c5edb423SSean Eric Fagan case 'N': /* process name */ 24078b43b535SAlfred Perlstein appendstr = name; 2408c5edb423SSean Eric Fagan break; 2409c5edb423SSean Eric Fagan case 'P': /* process id */ 24108b43b535SAlfred Perlstein sprintf(buf, "%u", pid); 24118b43b535SAlfred Perlstein appendstr = buf; 2412c5edb423SSean Eric Fagan break; 2413c5edb423SSean Eric Fagan case 'U': /* user id */ 24148b43b535SAlfred Perlstein sprintf(buf, "%u", uid); 24158b43b535SAlfred Perlstein appendstr = buf; 2416c5edb423SSean Eric Fagan break; 2417c5edb423SSean Eric Fagan default: 24188b43b535SAlfred Perlstein appendstr = ""; 24198b43b535SAlfred Perlstein log(LOG_ERR, 24208b43b535SAlfred Perlstein "Unknown format character %c in `%s'\n", 24218b43b535SAlfred Perlstein format[i], format); 2422c5edb423SSean Eric Fagan } 24238b43b535SAlfred Perlstein l = strlen(appendstr); 24248b43b535SAlfred Perlstein if ((n + l) >= MAXPATHLEN) 24258b43b535SAlfred Perlstein goto toolong; 24268b43b535SAlfred Perlstein memcpy(temp + n, appendstr, l); 24278b43b535SAlfred Perlstein n += l; 2428c5edb423SSean Eric Fagan break; 2429c5edb423SSean Eric Fagan default: 2430c5edb423SSean Eric Fagan temp[n++] = format[i]; 2431c5edb423SSean Eric Fagan } 2432c5edb423SSean Eric Fagan } 24338b43b535SAlfred Perlstein if (format[i] != '\0') 24348b43b535SAlfred Perlstein goto toolong; 24358b43b535SAlfred Perlstein return (temp); 24368b43b535SAlfred Perlstein toolong: 24378b43b535SAlfred Perlstein log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 24388b43b535SAlfred Perlstein (long)pid, name, (u_long)uid); 24398b43b535SAlfred Perlstein free(temp, M_TEMP); 24408b43b535SAlfred Perlstein return (NULL); 2441c5edb423SSean Eric Fagan } 2442c5edb423SSean Eric Fagan 2443df8bae1dSRodney W. Grimes /* 2444fca666a1SJulian Elischer * Dump a process' core. The main routine does some 2445fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 2446fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 2447fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 2448fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 2449fca666a1SJulian Elischer */ 2450fca666a1SJulian Elischer 2451fca666a1SJulian Elischer static int 2452b40ce416SJulian Elischer coredump(struct thread *td) 2453fca666a1SJulian Elischer { 2454b40ce416SJulian Elischer struct proc *p = td->td_proc; 2455fca666a1SJulian Elischer register struct vnode *vp; 24569c1ab3e0SJohn Baldwin register struct ucred *cred = td->td_ucred; 245706ae1e91SMatthew Dillon struct flock lf; 2458fca666a1SJulian Elischer struct nameidata nd; 2459fca666a1SJulian Elischer struct vattr vattr; 2460e6796b67SKirk McKusick int error, error1, flags; 2461f2a2857bSKirk McKusick struct mount *mp; 2462fca666a1SJulian Elischer char *name; /* name of corefile */ 2463fca666a1SJulian Elischer off_t limit; 2464fca666a1SJulian Elischer 2465628d2653SJohn Baldwin PROC_LOCK(p); 2466628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 2467fca666a1SJulian Elischer 2468628d2653SJohn Baldwin if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2469628d2653SJohn Baldwin PROC_UNLOCK(p); 2470fca666a1SJulian Elischer return (EFAULT); 2471628d2653SJohn Baldwin } 2472fca666a1SJulian Elischer 2473fca666a1SJulian Elischer /* 247435a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 247535a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 247635a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 247735a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 247835a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 247935a2598fSSean Eric Fagan * if it is larger than the limit. 2480fca666a1SJulian Elischer */ 248135a2598fSSean Eric Fagan limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2482628d2653SJohn Baldwin if (limit == 0) { 2483628d2653SJohn Baldwin PROC_UNLOCK(p); 248435a2598fSSean Eric Fagan return 0; 2485628d2653SJohn Baldwin } 2486628d2653SJohn Baldwin PROC_UNLOCK(p); 248735a2598fSSean Eric Fagan 2488f2a2857bSKirk McKusick restart: 24899c1ab3e0SJohn Baldwin name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2490ccdbd10cSPeter Pentchev if (name == NULL) 2491ccdbd10cSPeter Pentchev return (EINVAL); 2492b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2493e6796b67SKirk McKusick flags = O_CREAT | FWRITE | O_NOFOLLOW; 2494e6796b67SKirk McKusick error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2495fca666a1SJulian Elischer free(name, M_TEMP); 2496fca666a1SJulian Elischer if (error) 2497fca666a1SJulian Elischer return (error); 2498762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 2499fca666a1SJulian Elischer vp = nd.ni_vp; 250006ae1e91SMatthew Dillon 2501832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 2502832dafadSDon Lewis if (vp->v_type != VREG || 2503832dafadSDon Lewis VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2504832dafadSDon Lewis VOP_UNLOCK(vp, 0, td); 2505832dafadSDon Lewis error = EFAULT; 2506832dafadSDon Lewis goto out2; 2507832dafadSDon Lewis } 2508832dafadSDon Lewis 2509b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 251006ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 251106ae1e91SMatthew Dillon lf.l_start = 0; 251206ae1e91SMatthew Dillon lf.l_len = 0; 251306ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 251406ae1e91SMatthew Dillon error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 251506ae1e91SMatthew Dillon if (error) 251606ae1e91SMatthew Dillon goto out2; 251706ae1e91SMatthew Dillon 251806ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 251906ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 252006ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2521b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2522f2a2857bSKirk McKusick return (error); 2523f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2524f2a2857bSKirk McKusick return (error); 2525f2a2857bSKirk McKusick goto restart; 2526f2a2857bSKirk McKusick } 2527fca666a1SJulian Elischer 2528fca666a1SJulian Elischer VATTR_NULL(&vattr); 2529fca666a1SJulian Elischer vattr.va_size = 0; 253088b1d98fSPaul Saab vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2531b40ce416SJulian Elischer VOP_LEASE(vp, td, cred, LEASE_WRITE); 2532b40ce416SJulian Elischer VOP_SETATTR(vp, &vattr, cred, td); 253388b1d98fSPaul Saab VOP_UNLOCK(vp, 0, td); 2534628d2653SJohn Baldwin PROC_LOCK(p); 2535fca666a1SJulian Elischer p->p_acflag |= ACORE; 2536628d2653SJohn Baldwin PROC_UNLOCK(p); 2537fca666a1SJulian Elischer 2538fca666a1SJulian Elischer error = p->p_sysent->sv_coredump ? 2539b40ce416SJulian Elischer p->p_sysent->sv_coredump(td, vp, limit) : 2540fca666a1SJulian Elischer ENOSYS; 2541fca666a1SJulian Elischer 254206ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 254306ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2544f2a2857bSKirk McKusick vn_finished_write(mp); 254506ae1e91SMatthew Dillon out2: 2546b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 2547fca666a1SJulian Elischer if (error == 0) 2548fca666a1SJulian Elischer error = error1; 2549fca666a1SJulian Elischer return (error); 2550fca666a1SJulian Elischer } 2551fca666a1SJulian Elischer 2552fca666a1SJulian Elischer /* 2553df8bae1dSRodney W. Grimes * Nonexistent system call-- signal process (may want to handle it). 2554df8bae1dSRodney W. Grimes * Flag error in case process won't see signal immediately (blocked or ignored). 2555df8bae1dSRodney W. Grimes */ 2556d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 2557df8bae1dSRodney W. Grimes struct nosys_args { 2558df8bae1dSRodney W. Grimes int dummy; 2559df8bae1dSRodney W. Grimes }; 2560d2d3e875SBruce Evans #endif 2561fb99ab88SMatthew Dillon /* 2562fb99ab88SMatthew Dillon * MPSAFE 2563fb99ab88SMatthew Dillon */ 2564df8bae1dSRodney W. Grimes /* ARGSUSED */ 256526f9a767SRodney W. Grimes int 2566b40ce416SJulian Elischer nosys(td, args) 2567b40ce416SJulian Elischer struct thread *td; 2568df8bae1dSRodney W. Grimes struct nosys_args *args; 2569df8bae1dSRodney W. Grimes { 2570b40ce416SJulian Elischer struct proc *p = td->td_proc; 2571b40ce416SJulian Elischer 2572628d2653SJohn Baldwin PROC_LOCK(p); 2573df8bae1dSRodney W. Grimes psignal(p, SIGSYS); 2574628d2653SJohn Baldwin PROC_UNLOCK(p); 2575f5216b9aSBruce Evans return (ENOSYS); 2576df8bae1dSRodney W. Grimes } 2577831d27a9SDon Lewis 2578831d27a9SDon Lewis /* 257948f1ba5bSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using 2580831d27a9SDon Lewis * stored credentials rather than those of the current process. 2581831d27a9SDon Lewis */ 2582831d27a9SDon Lewis void 2583f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 2584f1320723SAlfred Perlstein struct sigio **sigiop; 25852c42a146SMarcel Moolenaar int sig, checkctty; 2586831d27a9SDon Lewis { 2587f1320723SAlfred Perlstein struct sigio *sigio; 2588831d27a9SDon Lewis 2589f1320723SAlfred Perlstein SIGIO_LOCK(); 2590f1320723SAlfred Perlstein sigio = *sigiop; 2591f1320723SAlfred Perlstein if (sigio == NULL) { 2592f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2593f1320723SAlfred Perlstein return; 2594f1320723SAlfred Perlstein } 2595831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 2596628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 25972b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 25982c42a146SMarcel Moolenaar psignal(sigio->sio_proc, sig); 2599628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 2600831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 2601831d27a9SDon Lewis struct proc *p; 2602831d27a9SDon Lewis 2603f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 2604628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2605628d2653SJohn Baldwin PROC_LOCK(p); 26062b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2607831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 26082c42a146SMarcel Moolenaar psignal(p, sig); 2609628d2653SJohn Baldwin PROC_UNLOCK(p); 2610628d2653SJohn Baldwin } 2611f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 2612831d27a9SDon Lewis } 2613f1320723SAlfred Perlstein SIGIO_UNLOCK(); 2614831d27a9SDon Lewis } 2615cb679c38SJonathan Lemon 2616cb679c38SJonathan Lemon static int 2617cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 2618cb679c38SJonathan Lemon { 2619cb679c38SJonathan Lemon struct proc *p = curproc; 2620cb679c38SJonathan Lemon 2621cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 2622cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 2623cb679c38SJonathan Lemon 2624628d2653SJohn Baldwin PROC_LOCK(p); 2625cb679c38SJonathan Lemon SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2626628d2653SJohn Baldwin PROC_UNLOCK(p); 2627cb679c38SJonathan Lemon 2628cb679c38SJonathan Lemon return (0); 2629cb679c38SJonathan Lemon } 2630cb679c38SJonathan Lemon 2631cb679c38SJonathan Lemon static void 2632cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 2633cb679c38SJonathan Lemon { 2634cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 2635cb679c38SJonathan Lemon 2636628d2653SJohn Baldwin PROC_LOCK(p); 2637e3975643SJake Burkholder SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2638628d2653SJohn Baldwin PROC_UNLOCK(p); 2639cb679c38SJonathan Lemon } 2640cb679c38SJonathan Lemon 2641cb679c38SJonathan Lemon /* 2642cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 2643cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 2644cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 2645cb679c38SJonathan Lemon * isn't worth the trouble. 2646cb679c38SJonathan Lemon */ 2647cb679c38SJonathan Lemon static int 2648cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 2649cb679c38SJonathan Lemon { 2650cb679c38SJonathan Lemon 2651cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 2652cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 2653cb679c38SJonathan Lemon 2654cb679c38SJonathan Lemon if (kn->kn_id == hint) 2655cb679c38SJonathan Lemon kn->kn_data++; 2656cb679c38SJonathan Lemon } 2657cb679c38SJonathan Lemon return (kn->kn_data != 0); 2658cb679c38SJonathan Lemon } 265990af4afaSJohn Baldwin 266090af4afaSJohn Baldwin struct sigacts * 266190af4afaSJohn Baldwin sigacts_alloc(void) 266290af4afaSJohn Baldwin { 266390af4afaSJohn Baldwin struct sigacts *ps; 266490af4afaSJohn Baldwin 266590af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 266690af4afaSJohn Baldwin ps->ps_refcnt = 1; 266790af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 266890af4afaSJohn Baldwin return (ps); 266990af4afaSJohn Baldwin } 267090af4afaSJohn Baldwin 267190af4afaSJohn Baldwin void 267290af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 267390af4afaSJohn Baldwin { 267490af4afaSJohn Baldwin 267590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 267690af4afaSJohn Baldwin ps->ps_refcnt--; 267790af4afaSJohn Baldwin if (ps->ps_refcnt == 0) { 267890af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 267990af4afaSJohn Baldwin free(ps, M_SUBPROC); 268090af4afaSJohn Baldwin } else 268190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 268290af4afaSJohn Baldwin } 268390af4afaSJohn Baldwin 268490af4afaSJohn Baldwin struct sigacts * 268590af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 268690af4afaSJohn Baldwin { 268790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 268890af4afaSJohn Baldwin ps->ps_refcnt++; 268990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 269090af4afaSJohn Baldwin return (ps); 269190af4afaSJohn Baldwin } 269290af4afaSJohn Baldwin 269390af4afaSJohn Baldwin void 269490af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 269590af4afaSJohn Baldwin { 269690af4afaSJohn Baldwin 269790af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 269890af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 269990af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 270090af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 270190af4afaSJohn Baldwin } 270290af4afaSJohn Baldwin 270390af4afaSJohn Baldwin int 270490af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 270590af4afaSJohn Baldwin { 270690af4afaSJohn Baldwin int shared; 270790af4afaSJohn Baldwin 270890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 270990af4afaSJohn Baldwin shared = ps->ps_refcnt > 1; 271090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 271190af4afaSJohn Baldwin return (shared); 271290af4afaSJohn Baldwin } 2713