19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405591b823SEivind Eklund #include "opt_compat.h" 415d217f17SJohn Birrell #include "opt_kdtrace.h" 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43e7228204SAlfred Perlstein #include "opt_core.h" 44cfb5f768SJonathan Anderson #include "opt_procdesc.h" 45db6a20e2SGarrett Wollman 46df8bae1dSRodney W. Grimes #include <sys/param.h> 4736240ea5SDoug Rabson #include <sys/systm.h> 48df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/vnode.h> 50df8bae1dSRodney W. Grimes #include <sys/acct.h> 51cfb5f768SJonathan Anderson #include <sys/capability.h> 52238510fcSJason Evans #include <sys/condvar.h> 53854dc8c2SJohn Baldwin #include <sys/event.h> 54854dc8c2SJohn Baldwin #include <sys/fcntl.h> 55e7228204SAlfred Perlstein #include <sys/imgact.h> 56854dc8c2SJohn Baldwin #include <sys/kernel.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> 64cfb5f768SJonathan Anderson #include <sys/procdesc.h> 656aeb05d7STom Rhodes #include <sys/posix4.h> 66854dc8c2SJohn Baldwin #include <sys/pioctl.h> 67b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h> 68c31146a1SJohn Baldwin #include <sys/resourcevar.h> 695d217f17SJohn Birrell #include <sys/sdt.h> 7036b208e0SRobert Watson #include <sys/sbuf.h> 7144f3b092SJohn Baldwin #include <sys/sleepqueue.h> 726caa8a15SJohn Baldwin #include <sys/smp.h> 73df8bae1dSRodney W. Grimes #include <sys/stat.h> 741005a129SJohn Baldwin #include <sys/sx.h> 758f19eb88SIan Dowse #include <sys/syscallsubr.h> 76c87e2930SDavid Greenman #include <sys/sysctl.h> 77854dc8c2SJohn Baldwin #include <sys/sysent.h> 78854dc8c2SJohn Baldwin #include <sys/syslog.h> 79854dc8c2SJohn Baldwin #include <sys/sysproto.h> 8056c06c4bSDavid Xu #include <sys/timers.h> 8106ae1e91SMatthew Dillon #include <sys/unistd.h> 82854dc8c2SJohn Baldwin #include <sys/wait.h> 839104847fSDavid Xu #include <vm/vm.h> 849104847fSDavid Xu #include <vm/vm_extern.h> 859104847fSDavid Xu #include <vm/uma.h> 86df8bae1dSRodney W. Grimes 87e7228204SAlfred Perlstein #include <sys/jail.h> 88e7228204SAlfred Perlstein 89df8bae1dSRodney W. Grimes #include <machine/cpu.h> 90df8bae1dSRodney W. Grimes 91bfd7575aSWayne Salamon #include <security/audit/audit.h> 92bfd7575aSWayne Salamon 936f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 946f841fb7SMarcel Moolenaar 955d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 9679856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send); 975d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *"); 985d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *"); 995d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int"); 10079856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear); 1015d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int"); 1025d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *"); 10379856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard); 1045d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *"); 1055d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *"); 1065d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int"); 1075d217f17SJohn Birrell 1084d77a549SAlfred Perlstein static int coredump(struct thread *); 109a3de221dSKonstantin Belousov static int killpg1(struct thread *td, int sig, int pgid, int all, 110a3de221dSKonstantin Belousov ksiginfo_t *ksi); 111f33a947bSKonstantin Belousov static int issignal(struct thread *td, int stop_allowed); 1124d77a549SAlfred Perlstein static int sigprop(int sig); 11394f0972bSDavid Xu static void tdsigwakeup(struct thread *, int, sig_t, int); 114d8267df7SDavid Xu static void sig_suspend_threads(struct thread *, struct proc *, int); 115cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 116cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 117cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 1184093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop); 1199104847fSDavid Xu static void sigqueue_start(void); 120cb679c38SJonathan Lemon 1219104847fSDavid Xu static uma_zone_t ksiginfo_zone = NULL; 122e76d823bSRobert Watson struct filterops sig_filtops = { 123e76d823bSRobert Watson .f_isfd = 0, 124e76d823bSRobert Watson .f_attach = filt_sigattach, 125e76d823bSRobert Watson .f_detach = filt_sigdetach, 126e76d823bSRobert Watson .f_event = filt_signal, 127e76d823bSRobert Watson }; 128cb679c38SJonathan Lemon 129fc8cca02SJohn Baldwin static int kern_logsigexit = 1; 1303d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1313d177f46SBill Fumerola &kern_logsigexit, 0, 1323d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 13357308494SJoerg Wunsch 134f71a882fSDavid Xu static int kern_forcesigexit = 1; 135f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 136f71a882fSDavid Xu &kern_forcesigexit, 0, "Force trap signal to be handled"); 137f71a882fSDavid Xu 1386472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, 1396472ac3dSEd Schouten "POSIX real time signal"); 1409104847fSDavid Xu 1419104847fSDavid Xu static int max_pending_per_proc = 128; 1429104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 1439104847fSDavid Xu &max_pending_per_proc, 0, "Max pending signals per proc"); 1449104847fSDavid Xu 1459104847fSDavid Xu static int preallocate_siginfo = 1024; 1469104847fSDavid Xu TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo); 1479104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD, 1489104847fSDavid Xu &preallocate_siginfo, 0, "Preallocated signal memory size"); 1499104847fSDavid Xu 1509104847fSDavid Xu static int signal_overflow = 0; 151761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 1529104847fSDavid Xu &signal_overflow, 0, "Number of signals overflew"); 1539104847fSDavid Xu 1549104847fSDavid Xu static int signal_alloc_fail = 0; 155761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 1569104847fSDavid Xu &signal_alloc_fail, 0, "signals failed to be allocated"); 1579104847fSDavid Xu 1589104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 1599104847fSDavid Xu 1602b87b6d4SRobert Watson /* 1612b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1622b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1632b87b6d4SRobert Watson * in the right situations. 1642b87b6d4SRobert Watson */ 1652b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1662b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1672b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1682b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1692b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1702b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1712b87b6d4SRobert Watson 172fc8cca02SJohn Baldwin static int sugid_coredump; 173b62d05fcSPawel Jakub Dawidek TUNABLE_INT("kern.sugid_coredump", &sugid_coredump); 1743d177f46SBill Fumerola SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 1758b5adf9dSJoseph Koshy &sugid_coredump, 0, "Allow setuid and setgid processes to dump core"); 176c87e2930SDavid Greenman 177b0c9d4d7SPawel Jakub Dawidek static int capmode_coredump; 178b0c9d4d7SPawel Jakub Dawidek TUNABLE_INT("kern.capmode_coredump", &capmode_coredump); 179b0c9d4d7SPawel Jakub Dawidek SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RW, 180b0c9d4d7SPawel Jakub Dawidek &capmode_coredump, 0, "Allow processes in capability mode to dump core"); 181b0c9d4d7SPawel Jakub Dawidek 182e5a28db9SPaul Saab static int do_coredump = 1; 183e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 184e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 185e5a28db9SPaul Saab 1866141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 1876141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 1886141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 1896141e04aSJohn-Mark Gurney 1902c42a146SMarcel Moolenaar /* 1912c42a146SMarcel Moolenaar * Signal properties and actions. 1922c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1932c42a146SMarcel Moolenaar * according to the following properties: 1942c42a146SMarcel Moolenaar */ 1952c42a146SMarcel Moolenaar #define SA_KILL 0x01 /* terminates process by default */ 1962c42a146SMarcel Moolenaar #define SA_CORE 0x02 /* ditto and coredumps */ 1972c42a146SMarcel Moolenaar #define SA_STOP 0x04 /* suspend process */ 1982c42a146SMarcel Moolenaar #define SA_TTYSTOP 0x08 /* ditto, from tty */ 1992c42a146SMarcel Moolenaar #define SA_IGNORE 0x10 /* ignore by default */ 2002c42a146SMarcel Moolenaar #define SA_CONT 0x20 /* continue if suspended */ 2012c42a146SMarcel Moolenaar #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 202df8bae1dSRodney W. Grimes 2032c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 2048a8be776SJilles Tjoelker SA_KILL, /* SIGHUP */ 2058a8be776SJilles Tjoelker SA_KILL, /* SIGINT */ 2068a8be776SJilles Tjoelker SA_KILL|SA_CORE, /* SIGQUIT */ 2072c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGILL */ 2082c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGTRAP */ 2092c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGABRT */ 2108a8be776SJilles Tjoelker SA_KILL|SA_CORE, /* SIGEMT */ 2112c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGFPE */ 2128a8be776SJilles Tjoelker SA_KILL, /* SIGKILL */ 2132c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGBUS */ 2142c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSEGV */ 2152c42a146SMarcel Moolenaar SA_KILL|SA_CORE, /* SIGSYS */ 2168a8be776SJilles Tjoelker SA_KILL, /* SIGPIPE */ 2178a8be776SJilles Tjoelker SA_KILL, /* SIGALRM */ 2188a8be776SJilles Tjoelker SA_KILL, /* SIGTERM */ 2198a8be776SJilles Tjoelker SA_IGNORE, /* SIGURG */ 2208a8be776SJilles Tjoelker SA_STOP, /* SIGSTOP */ 2218a8be776SJilles Tjoelker SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 2228a8be776SJilles Tjoelker SA_IGNORE|SA_CONT, /* SIGCONT */ 2238a8be776SJilles Tjoelker SA_IGNORE, /* SIGCHLD */ 2248a8be776SJilles Tjoelker SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 2258a8be776SJilles Tjoelker SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 2268a8be776SJilles Tjoelker SA_IGNORE, /* SIGIO */ 2272c42a146SMarcel Moolenaar SA_KILL, /* SIGXCPU */ 2282c42a146SMarcel Moolenaar SA_KILL, /* SIGXFSZ */ 2298a8be776SJilles Tjoelker SA_KILL, /* SIGVTALRM */ 2308a8be776SJilles Tjoelker SA_KILL, /* SIGPROF */ 2318a8be776SJilles Tjoelker SA_IGNORE, /* SIGWINCH */ 2328a8be776SJilles Tjoelker SA_IGNORE, /* SIGINFO */ 2338a8be776SJilles Tjoelker SA_KILL, /* SIGUSR1 */ 2348a8be776SJilles Tjoelker SA_KILL, /* SIGUSR2 */ 2352c42a146SMarcel Moolenaar }; 2362c42a146SMarcel Moolenaar 23780a8b0f3SKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags); 2386b286ee8SKonstantin Belousov 2399104847fSDavid Xu static void 2409104847fSDavid Xu sigqueue_start(void) 2419104847fSDavid Xu { 2429104847fSDavid Xu ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 2439104847fSDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2449104847fSDavid Xu uma_prealloc(ksiginfo_zone, preallocate_siginfo); 245b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 246b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 247b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 2489104847fSDavid Xu } 2499104847fSDavid Xu 2505da49fcbSDavid Xu ksiginfo_t * 251ebceaf6dSDavid Xu ksiginfo_alloc(int wait) 2529104847fSDavid Xu { 253ebceaf6dSDavid Xu int flags; 254ebceaf6dSDavid Xu 255ebceaf6dSDavid Xu flags = M_ZERO; 256ebceaf6dSDavid Xu if (! wait) 257ebceaf6dSDavid Xu flags |= M_NOWAIT; 2589104847fSDavid Xu if (ksiginfo_zone != NULL) 259ebceaf6dSDavid Xu return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 2609104847fSDavid Xu return (NULL); 2619104847fSDavid Xu } 2629104847fSDavid Xu 2635da49fcbSDavid Xu void 2649104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 2659104847fSDavid Xu { 2669104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2679104847fSDavid Xu } 2689104847fSDavid Xu 2695da49fcbSDavid Xu static __inline int 2705da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 2715da49fcbSDavid Xu { 2725da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT)) { 2735da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2745da49fcbSDavid Xu return (1); 2755da49fcbSDavid Xu } 2765da49fcbSDavid Xu return (0); 2775da49fcbSDavid Xu } 2785da49fcbSDavid Xu 2799104847fSDavid Xu void 2809104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 2819104847fSDavid Xu { 2829104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 2833dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 2849104847fSDavid Xu TAILQ_INIT(&list->sq_list); 2859104847fSDavid Xu list->sq_proc = p; 2869104847fSDavid Xu list->sq_flags = SQ_INIT; 2879104847fSDavid Xu } 2889104847fSDavid Xu 2899104847fSDavid Xu /* 2909104847fSDavid Xu * Get a signal's ksiginfo. 2919104847fSDavid Xu * Return: 2929104847fSDavid Xu * 0 - signal not found 2939104847fSDavid Xu * others - signal number 2949104847fSDavid Xu */ 295a5799a4fSKonstantin Belousov static int 2969104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 2979104847fSDavid Xu { 2989104847fSDavid Xu struct proc *p = sq->sq_proc; 2999104847fSDavid Xu struct ksiginfo *ksi, *next; 3009104847fSDavid Xu int count = 0; 3019104847fSDavid Xu 3029104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3039104847fSDavid Xu 3049104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 3059104847fSDavid Xu return (0); 3069104847fSDavid Xu 3073dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 3083dfcaad6SDavid Xu count++; 3093dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 3103dfcaad6SDavid Xu } 3113dfcaad6SDavid Xu 3125c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 3139104847fSDavid Xu if (ksi->ksi_signo == signo) { 3149104847fSDavid Xu if (count == 0) { 3159104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3165da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3179104847fSDavid Xu ksiginfo_copy(ksi, si); 3185da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 3199104847fSDavid Xu p->p_pendingcnt--; 3209104847fSDavid Xu } 321016fa302SDavid Xu if (++count > 1) 322016fa302SDavid Xu break; 3239104847fSDavid Xu } 3249104847fSDavid Xu } 3259104847fSDavid Xu 3269104847fSDavid Xu if (count <= 1) 3279104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 3289104847fSDavid Xu si->ksi_signo = signo; 3299104847fSDavid Xu return (signo); 3309104847fSDavid Xu } 3319104847fSDavid Xu 3325da49fcbSDavid Xu void 3335da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 3345da49fcbSDavid Xu { 3355da49fcbSDavid Xu struct ksiginfo *kp; 3365da49fcbSDavid Xu struct proc *p; 3375da49fcbSDavid Xu sigqueue_t *sq; 3385da49fcbSDavid Xu 339ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 3405da49fcbSDavid Xu return; 3415da49fcbSDavid Xu 3425da49fcbSDavid Xu p = sq->sq_proc; 3435da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3445da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3455da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 3465da49fcbSDavid Xu p->p_pendingcnt--; 3475da49fcbSDavid Xu 3485da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 3495da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 3505da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 3515da49fcbSDavid Xu break; 3525da49fcbSDavid Xu } 3533dfcaad6SDavid Xu if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo)) 3545da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 3555da49fcbSDavid Xu } 3565da49fcbSDavid Xu 357a5799a4fSKonstantin Belousov static int 3589104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 3599104847fSDavid Xu { 3609104847fSDavid Xu struct proc *p = sq->sq_proc; 3619104847fSDavid Xu struct ksiginfo *ksi; 3629104847fSDavid Xu int ret = 0; 3639104847fSDavid Xu 3649104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3659104847fSDavid Xu 3663dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 3673dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3689104847fSDavid Xu goto out_set_bit; 3693dfcaad6SDavid Xu } 3709104847fSDavid Xu 3715da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 3725da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 3736a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 3746a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 3756a671a6bSKonstantin Belousov else 3765da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 3775da49fcbSDavid Xu si->ksi_sigq = sq; 3785da49fcbSDavid Xu goto out_set_bit; 3795da49fcbSDavid Xu } 3805da49fcbSDavid Xu 3813dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 3823dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3839104847fSDavid Xu goto out_set_bit; 3843dfcaad6SDavid Xu } 3859104847fSDavid Xu 386ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 3879104847fSDavid Xu signal_overflow++; 3889104847fSDavid Xu ret = EAGAIN; 389ebceaf6dSDavid Xu } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 3909104847fSDavid Xu signal_alloc_fail++; 3919104847fSDavid Xu ret = EAGAIN; 3929104847fSDavid Xu } else { 3939104847fSDavid Xu if (p != NULL) 3949104847fSDavid Xu p->p_pendingcnt++; 3959104847fSDavid Xu ksiginfo_copy(si, ksi); 3969104847fSDavid Xu ksi->ksi_signo = signo; 3976a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 3986a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 3996a671a6bSKonstantin Belousov else 4009104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 4015da49fcbSDavid Xu ksi->ksi_sigq = sq; 4029104847fSDavid Xu } 4039104847fSDavid Xu 404a3de221dSKonstantin Belousov if ((si->ksi_flags & KSI_TRAP) != 0 || 405a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 4063dfcaad6SDavid Xu if (ret != 0) 4073dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4089104847fSDavid Xu ret = 0; 4099104847fSDavid Xu goto out_set_bit; 4109104847fSDavid Xu } 4119104847fSDavid Xu 4129104847fSDavid Xu if (ret != 0) 4139104847fSDavid Xu return (ret); 4149104847fSDavid Xu 4159104847fSDavid Xu out_set_bit: 4169104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 4179104847fSDavid Xu return (ret); 4189104847fSDavid Xu } 4199104847fSDavid Xu 4209104847fSDavid Xu void 4219104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 4229104847fSDavid Xu { 4239104847fSDavid Xu struct proc *p = sq->sq_proc; 4249104847fSDavid Xu ksiginfo_t *ksi; 4259104847fSDavid Xu 4269104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4279104847fSDavid Xu 4285da49fcbSDavid Xu if (p != NULL) 4295da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 4305da49fcbSDavid Xu 4319104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 4329104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4335da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4345da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4359104847fSDavid Xu p->p_pendingcnt--; 4369104847fSDavid Xu } 4379104847fSDavid Xu 4389104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 4393dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 4409104847fSDavid Xu } 4419104847fSDavid Xu 442a5799a4fSKonstantin Belousov static void 443fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 4449104847fSDavid Xu { 445fc4ecc1dSDavid Xu sigset_t tmp; 4469104847fSDavid Xu struct proc *p1, *p2; 4479104847fSDavid Xu ksiginfo_t *ksi, *next; 4489104847fSDavid Xu 4499104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 4509104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 4519104847fSDavid Xu p1 = src->sq_proc; 4529104847fSDavid Xu p2 = dst->sq_proc; 4539104847fSDavid Xu /* Move siginfo to target list */ 4545c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 455fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 4569104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 4579104847fSDavid Xu if (p1 != NULL) 4589104847fSDavid Xu p1->p_pendingcnt--; 4599104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 4605da49fcbSDavid Xu ksi->ksi_sigq = dst; 4619104847fSDavid Xu if (p2 != NULL) 4629104847fSDavid Xu p2->p_pendingcnt++; 4639104847fSDavid Xu } 4649104847fSDavid Xu } 4659104847fSDavid Xu 4669104847fSDavid Xu /* Move pending bits to target list */ 4673dfcaad6SDavid Xu tmp = src->sq_kill; 468fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4693dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 4703dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 4713dfcaad6SDavid Xu 4729104847fSDavid Xu tmp = src->sq_signals; 473fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4749104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 4759104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 4769104847fSDavid Xu } 4779104847fSDavid Xu 478407af02bSDavid Xu #if 0 479a5799a4fSKonstantin Belousov static void 4809104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 4819104847fSDavid Xu { 4829104847fSDavid Xu sigset_t set; 4839104847fSDavid Xu 4849104847fSDavid Xu SIGEMPTYSET(set); 4859104847fSDavid Xu SIGADDSET(set, signo); 4869104847fSDavid Xu sigqueue_move_set(src, dst, &set); 4879104847fSDavid Xu } 488407af02bSDavid Xu #endif 4899104847fSDavid Xu 490a5799a4fSKonstantin Belousov static void 491fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 4929104847fSDavid Xu { 4939104847fSDavid Xu struct proc *p = sq->sq_proc; 4949104847fSDavid Xu ksiginfo_t *ksi, *next; 4959104847fSDavid Xu 4969104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 4979104847fSDavid Xu 4989104847fSDavid Xu /* Remove siginfo queue */ 4995c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 5009104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5019104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5025da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5035da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5049104847fSDavid Xu p->p_pendingcnt--; 5059104847fSDavid Xu } 5069104847fSDavid Xu } 5073dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 5089104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 5099104847fSDavid Xu } 5109104847fSDavid Xu 5119104847fSDavid Xu void 5129104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 5139104847fSDavid Xu { 5149104847fSDavid Xu sigset_t set; 5159104847fSDavid Xu 5169104847fSDavid Xu SIGEMPTYSET(set); 5179104847fSDavid Xu SIGADDSET(set, signo); 5189104847fSDavid Xu sigqueue_delete_set(sq, &set); 5199104847fSDavid Xu } 5209104847fSDavid Xu 5219104847fSDavid Xu /* Remove a set of signals for a process */ 522a5799a4fSKonstantin Belousov static void 523fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 5249104847fSDavid Xu { 5259104847fSDavid Xu sigqueue_t worklist; 5269104847fSDavid Xu struct thread *td0; 5279104847fSDavid Xu 5289104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5299104847fSDavid Xu 5309104847fSDavid Xu sigqueue_init(&worklist, NULL); 5319104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 5329104847fSDavid Xu 5339104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 5349104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 5359104847fSDavid Xu 5369104847fSDavid Xu sigqueue_flush(&worklist); 5379104847fSDavid Xu } 5389104847fSDavid Xu 5399104847fSDavid Xu void 5409104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 5419104847fSDavid Xu { 5429104847fSDavid Xu sigset_t set; 5439104847fSDavid Xu 5449104847fSDavid Xu SIGEMPTYSET(set); 5459104847fSDavid Xu SIGADDSET(set, signo); 5469104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5479104847fSDavid Xu } 5489104847fSDavid Xu 549a5799a4fSKonstantin Belousov static void 5509104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 5519104847fSDavid Xu { 5529104847fSDavid Xu sigset_t set; 5539104847fSDavid Xu 5549104847fSDavid Xu SIGEMPTYSET(set); 5559104847fSDavid Xu SIGADDSET(set, SIGSTOP); 5569104847fSDavid Xu SIGADDSET(set, SIGTSTP); 5579104847fSDavid Xu SIGADDSET(set, SIGTTIN); 5589104847fSDavid Xu SIGADDSET(set, SIGTTOU); 5599104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5609104847fSDavid Xu } 5619104847fSDavid Xu 562fbbeeb6cSBruce Evans /* 563fbbeeb6cSBruce Evans * Determine signal that should be delivered to process p, the current 564fbbeeb6cSBruce Evans * process, 0 if none. If there is a pending stop signal with default 565fbbeeb6cSBruce Evans * action, the process stops in issignal(). 566fbbeeb6cSBruce Evans */ 567fbbeeb6cSBruce Evans int 568f33a947bSKonstantin Belousov cursig(struct thread *td, int stop_allowed) 569fbbeeb6cSBruce Evans { 570c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 571f33a947bSKonstantin Belousov KASSERT(stop_allowed == SIG_STOP_ALLOWED || 572f33a947bSKonstantin Belousov stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed")); 57390af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 574a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 575f33a947bSKonstantin Belousov return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0); 576fbbeeb6cSBruce Evans } 577fbbeeb6cSBruce Evans 57879065dbaSBruce Evans /* 57979065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 5809104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 5814093529dSJeff Roberson * unmasked in td_sigmask. 58279065dbaSBruce Evans */ 58379065dbaSBruce Evans void 5844093529dSJeff Roberson signotify(struct thread *td) 58579065dbaSBruce Evans { 5864093529dSJeff Roberson struct proc *p; 5874093529dSJeff Roberson 5884093529dSJeff Roberson p = td->td_proc; 58979065dbaSBruce Evans 59079065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 5914093529dSJeff Roberson 5928b94a061SJohn Baldwin if (SIGPENDING(td)) { 593a54e85fdSJeff Roberson thread_lock(td); 5944093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 595a54e85fdSJeff Roberson thread_unlock(td); 59679065dbaSBruce Evans } 5978b94a061SJohn Baldwin } 5988b94a061SJohn Baldwin 5998b94a061SJohn Baldwin int 6008b94a061SJohn Baldwin sigonstack(size_t sp) 6018b94a061SJohn Baldwin { 602a30ec4b9SDavid Xu struct thread *td = curthread; 6038b94a061SJohn Baldwin 604a30ec4b9SDavid Xu return ((td->td_pflags & TDP_ALTSTACK) ? 6051930e303SPoul-Henning Kamp #if defined(COMPAT_43) 606a30ec4b9SDavid Xu ((td->td_sigstk.ss_size == 0) ? 607a30ec4b9SDavid Xu (td->td_sigstk.ss_flags & SS_ONSTACK) : 608a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 6098b94a061SJohn Baldwin #else 610a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 6118b94a061SJohn Baldwin #endif 6128b94a061SJohn Baldwin : 0); 6138b94a061SJohn Baldwin } 61479065dbaSBruce Evans 6156f841fb7SMarcel Moolenaar static __inline int 6166f841fb7SMarcel Moolenaar sigprop(int sig) 6172c42a146SMarcel Moolenaar { 6186f841fb7SMarcel Moolenaar 6192c42a146SMarcel Moolenaar if (sig > 0 && sig < NSIG) 6202c42a146SMarcel Moolenaar return (sigproptbl[_SIG_IDX(sig)]); 6212c42a146SMarcel Moolenaar return (0); 622df8bae1dSRodney W. Grimes } 6232c42a146SMarcel Moolenaar 6244093529dSJeff Roberson int 6256f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 6262c42a146SMarcel Moolenaar { 6272c42a146SMarcel Moolenaar int i; 6282c42a146SMarcel Moolenaar 6296f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 6302c42a146SMarcel Moolenaar if (set->__bits[i]) 6312c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 632df8bae1dSRodney W. Grimes return (0); 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes 6352c42a146SMarcel Moolenaar /* 6368f19eb88SIan Dowse * kern_sigaction 6372c42a146SMarcel Moolenaar * sigaction 63823eeeff7SPeter Wemm * freebsd4_sigaction 6392c42a146SMarcel Moolenaar * osigaction 6402c42a146SMarcel Moolenaar */ 6418f19eb88SIan Dowse int 64223eeeff7SPeter Wemm kern_sigaction(td, sig, act, oact, flags) 6438f19eb88SIan Dowse struct thread *td; 6442c42a146SMarcel Moolenaar register int sig; 6452c42a146SMarcel Moolenaar struct sigaction *act, *oact; 64623eeeff7SPeter Wemm int flags; 647df8bae1dSRodney W. Grimes { 64890af4afaSJohn Baldwin struct sigacts *ps; 6498f19eb88SIan Dowse struct proc *p = td->td_proc; 650df8bae1dSRodney W. Grimes 6512899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 6522c42a146SMarcel Moolenaar return (EINVAL); 6532c42a146SMarcel Moolenaar 654628d2653SJohn Baldwin PROC_LOCK(p); 655628d2653SJohn Baldwin ps = p->p_sigacts; 65690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 6572c42a146SMarcel Moolenaar if (oact) { 6582c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 6592c42a146SMarcel Moolenaar oact->sa_flags = 0; 6602c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 6612c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 6622c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 6632c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 6642c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 6652c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 6662c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 6672c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 66810c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 6692c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 67010c2b8e1SDavid E. O'Brien oact->sa_sigaction = 67110c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 67210c2b8e1SDavid E. O'Brien } else 67310c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 67490af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 6752c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 67690af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 6772c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 6782c42a146SMarcel Moolenaar } 6792c42a146SMarcel Moolenaar if (act) { 6802c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 681628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 68290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 683628d2653SJohn Baldwin PROC_UNLOCK(p); 6842c42a146SMarcel Moolenaar return (EINVAL); 685628d2653SJohn Baldwin } 6862c42a146SMarcel Moolenaar 687df8bae1dSRodney W. Grimes /* 688df8bae1dSRodney W. Grimes * Change setting atomically. 689df8bae1dSRodney W. Grimes */ 6902c42a146SMarcel Moolenaar 6912c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 6922c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 6932c42a146SMarcel Moolenaar if (act->sa_flags & SA_SIGINFO) { 694aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 695aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 69680f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 69780f42b55SIan Dowse } else { 69880f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 6992c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 7002c42a146SMarcel Moolenaar } 7012c42a146SMarcel Moolenaar if (!(act->sa_flags & SA_RESTART)) 7022c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 703df8bae1dSRodney W. Grimes else 7042c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 7052c42a146SMarcel Moolenaar if (act->sa_flags & SA_ONSTACK) 7062c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 707df8bae1dSRodney W. Grimes else 7082c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 7092c42a146SMarcel Moolenaar if (act->sa_flags & SA_RESETHAND) 7102c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 7111e41c1b5SSteven Wallace else 7122c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 7132c42a146SMarcel Moolenaar if (act->sa_flags & SA_NODEFER) 7142c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 715289ccde0SPeter Wemm else 7162c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 7172c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 7182c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 71990af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 7206626c604SJulian Elischer else 72190af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 722ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 723245f17d4SJoerg Wunsch /* 7242c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 7252c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 7262c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 7272c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 728245f17d4SJoerg Wunsch */ 729245f17d4SJoerg Wunsch if (p->p_pid == 1) 73090af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 7316626c604SJulian Elischer else 73290af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 733245f17d4SJoerg Wunsch } else 73490af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 735ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 73690af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 737ba1551caSIan Dowse else 73890af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 739df8bae1dSRodney W. Grimes } 740df8bae1dSRodney W. Grimes /* 74190af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 7422c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 74390af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 7442c42a146SMarcel Moolenaar * have to restart the process. 745df8bae1dSRodney W. Grimes */ 7462c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 7472c42a146SMarcel Moolenaar (sigprop(sig) & SA_IGNORE && 7482c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 7492c42a146SMarcel Moolenaar /* never to be seen again */ 7509104847fSDavid Xu sigqueue_delete_proc(p, sig); 7512c42a146SMarcel Moolenaar if (sig != SIGCONT) 7522c42a146SMarcel Moolenaar /* easier in psignal */ 75390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 75490af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 755645682fdSLuoqi Chen } else { 75690af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 7572c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 75890af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 7592c42a146SMarcel Moolenaar else 76090af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 7612c42a146SMarcel Moolenaar } 76223eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 76323eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 76423eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 76523eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 76623eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 76723eeeff7SPeter Wemm else 76823eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 76923eeeff7SPeter Wemm #endif 770e8ebc08fSPeter Wemm #ifdef COMPAT_43 771645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 77223eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 77323eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 774645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 775645682fdSLuoqi Chen else 776645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 777e8ebc08fSPeter Wemm #endif 778df8bae1dSRodney W. Grimes } 77990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 780628d2653SJohn Baldwin PROC_UNLOCK(p); 7812c42a146SMarcel Moolenaar return (0); 7822c42a146SMarcel Moolenaar } 7832c42a146SMarcel Moolenaar 7842c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 7852c42a146SMarcel Moolenaar struct sigaction_args { 7862c42a146SMarcel Moolenaar int sig; 7872c42a146SMarcel Moolenaar struct sigaction *act; 7882c42a146SMarcel Moolenaar struct sigaction *oact; 7892c42a146SMarcel Moolenaar }; 7902c42a146SMarcel Moolenaar #endif 7912c42a146SMarcel Moolenaar int 7928451d0ddSKip Macy sys_sigaction(td, uap) 793b40ce416SJulian Elischer struct thread *td; 7942c42a146SMarcel Moolenaar register struct sigaction_args *uap; 7952c42a146SMarcel Moolenaar { 7962c42a146SMarcel Moolenaar struct sigaction act, oact; 7972c42a146SMarcel Moolenaar register struct sigaction *actp, *oactp; 7982c42a146SMarcel Moolenaar int error; 7992c42a146SMarcel Moolenaar 8006f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 8016f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 8022c42a146SMarcel Moolenaar if (actp) { 8036f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 8042c42a146SMarcel Moolenaar if (error) 80544443757STim J. Robbins return (error); 8062c42a146SMarcel Moolenaar } 8078f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 80825d6dc06SJohn Baldwin if (oactp && !error) 8096f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 8102c42a146SMarcel Moolenaar return (error); 8112c42a146SMarcel Moolenaar } 8122c42a146SMarcel Moolenaar 81323eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 81423eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 81523eeeff7SPeter Wemm struct freebsd4_sigaction_args { 81623eeeff7SPeter Wemm int sig; 81723eeeff7SPeter Wemm struct sigaction *act; 81823eeeff7SPeter Wemm struct sigaction *oact; 81923eeeff7SPeter Wemm }; 82023eeeff7SPeter Wemm #endif 82123eeeff7SPeter Wemm int 82223eeeff7SPeter Wemm freebsd4_sigaction(td, uap) 82323eeeff7SPeter Wemm struct thread *td; 82423eeeff7SPeter Wemm register struct freebsd4_sigaction_args *uap; 82523eeeff7SPeter Wemm { 82623eeeff7SPeter Wemm struct sigaction act, oact; 82723eeeff7SPeter Wemm register struct sigaction *actp, *oactp; 82823eeeff7SPeter Wemm int error; 82923eeeff7SPeter Wemm 83023eeeff7SPeter Wemm 83123eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 83223eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 83323eeeff7SPeter Wemm if (actp) { 83423eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 83523eeeff7SPeter Wemm if (error) 83644443757STim J. Robbins return (error); 83723eeeff7SPeter Wemm } 83823eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 839a14e1189SJohn Baldwin if (oactp && !error) 84023eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 84123eeeff7SPeter Wemm return (error); 84223eeeff7SPeter Wemm } 84323eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 84423eeeff7SPeter Wemm 84531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8462c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8472c42a146SMarcel Moolenaar struct osigaction_args { 8482c42a146SMarcel Moolenaar int signum; 8492c42a146SMarcel Moolenaar struct osigaction *nsa; 8502c42a146SMarcel Moolenaar struct osigaction *osa; 8512c42a146SMarcel Moolenaar }; 8522c42a146SMarcel Moolenaar #endif 8532c42a146SMarcel Moolenaar int 854b40ce416SJulian Elischer osigaction(td, uap) 855b40ce416SJulian Elischer struct thread *td; 8562c42a146SMarcel Moolenaar register struct osigaction_args *uap; 8572c42a146SMarcel Moolenaar { 8582c42a146SMarcel Moolenaar struct osigaction sa; 8592c42a146SMarcel Moolenaar struct sigaction nsa, osa; 8602c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 8612c42a146SMarcel Moolenaar int error; 8622c42a146SMarcel Moolenaar 8636f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 8646f841fb7SMarcel Moolenaar return (EINVAL); 865fb99ab88SMatthew Dillon 8666f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 8676f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 868fb99ab88SMatthew Dillon 8692c42a146SMarcel Moolenaar if (nsap) { 8706f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 8712c42a146SMarcel Moolenaar if (error) 87244443757STim J. Robbins return (error); 8732c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 8742c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 8752c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 8762c42a146SMarcel Moolenaar } 87723eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 8782c42a146SMarcel Moolenaar if (osap && !error) { 8792c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 8802c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 8812c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 8826f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 8832c42a146SMarcel Moolenaar } 8842c42a146SMarcel Moolenaar return (error); 8852c42a146SMarcel Moolenaar } 88623eeeff7SPeter Wemm 88773dbd3daSJohn Baldwin #if !defined(__i386__) 88823eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 88923eeeff7SPeter Wemm int 89023eeeff7SPeter Wemm osigreturn(td, uap) 89123eeeff7SPeter Wemm struct thread *td; 89223eeeff7SPeter Wemm struct osigreturn_args *uap; 89323eeeff7SPeter Wemm { 89423eeeff7SPeter Wemm 89523eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 89623eeeff7SPeter Wemm } 89723eeeff7SPeter Wemm #endif 89831c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 899df8bae1dSRodney W. Grimes 900df8bae1dSRodney W. Grimes /* 901df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 902df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 903df8bae1dSRodney W. Grimes */ 904df8bae1dSRodney W. Grimes void 905df8bae1dSRodney W. Grimes siginit(p) 906df8bae1dSRodney W. Grimes struct proc *p; 907df8bae1dSRodney W. Grimes { 908df8bae1dSRodney W. Grimes register int i; 90990af4afaSJohn Baldwin struct sigacts *ps; 910df8bae1dSRodney W. Grimes 911628d2653SJohn Baldwin PROC_LOCK(p); 91290af4afaSJohn Baldwin ps = p->p_sigacts; 91390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 9142c42a146SMarcel Moolenaar for (i = 1; i <= NSIG; i++) 9152c42a146SMarcel Moolenaar if (sigprop(i) & SA_IGNORE && i != SIGCONT) 91690af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 91790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 918628d2653SJohn Baldwin PROC_UNLOCK(p); 919df8bae1dSRodney W. Grimes } 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes /* 922df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 923df8bae1dSRodney W. Grimes */ 924df8bae1dSRodney W. Grimes void 925a30ec4b9SDavid Xu execsigs(struct proc *p) 926df8bae1dSRodney W. Grimes { 927a30ec4b9SDavid Xu struct sigacts *ps; 928a30ec4b9SDavid Xu int sig; 929a30ec4b9SDavid Xu struct thread *td; 930df8bae1dSRodney W. Grimes 931df8bae1dSRodney W. Grimes /* 932df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 9334093529dSJeff Roberson * through td_sigmask (unless they were caught, 934df8bae1dSRodney W. Grimes * and are now ignored by default). 935df8bae1dSRodney W. Grimes */ 9369b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 937a30ec4b9SDavid Xu td = FIRST_THREAD_IN_PROC(p); 938628d2653SJohn Baldwin ps = p->p_sigacts; 93990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 94090af4afaSJohn Baldwin while (SIGNOTEMPTY(ps->ps_sigcatch)) { 94190af4afaSJohn Baldwin sig = sig_ffs(&ps->ps_sigcatch); 94290af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 9432c42a146SMarcel Moolenaar if (sigprop(sig) & SA_IGNORE) { 9442c42a146SMarcel Moolenaar if (sig != SIGCONT) 94590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 9469104847fSDavid Xu sigqueue_delete_proc(p, sig); 947df8bae1dSRodney W. Grimes } 9482c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 949df8bae1dSRodney W. Grimes } 950df8bae1dSRodney W. Grimes /* 951df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 952df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 953df8bae1dSRodney W. Grimes */ 954a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 955a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 956a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 957a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 95880e907a1SPeter Wemm /* 95980e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 96080e907a1SPeter Wemm */ 96190af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 962c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 963c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 96490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes 967df8bae1dSRodney W. Grimes /* 968e77daab1SJohn Baldwin * kern_sigprocmask() 9697c8fdcbdSMatthew Dillon * 970628d2653SJohn Baldwin * Manipulate signal mask. 971df8bae1dSRodney W. Grimes */ 972e77daab1SJohn Baldwin int 9736b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 97484440afbSKonstantin Belousov int flags) 9752c42a146SMarcel Moolenaar { 9766b286ee8SKonstantin Belousov sigset_t new_block, oset1; 9776b286ee8SKonstantin Belousov struct proc *p; 9782c42a146SMarcel Moolenaar int error; 9792c42a146SMarcel Moolenaar 9806b286ee8SKonstantin Belousov p = td->td_proc; 98184440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 9826b286ee8SKonstantin Belousov PROC_LOCK(p); 9832c42a146SMarcel Moolenaar if (oset != NULL) 9844093529dSJeff Roberson *oset = td->td_sigmask; 9852c42a146SMarcel Moolenaar 9862c42a146SMarcel Moolenaar error = 0; 9872c42a146SMarcel Moolenaar if (set != NULL) { 9882c42a146SMarcel Moolenaar switch (how) { 9892c42a146SMarcel Moolenaar case SIG_BLOCK: 990645682fdSLuoqi Chen SIG_CANTMASK(*set); 9916b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 9924093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 9936b286ee8SKonstantin Belousov new_block = td->td_sigmask; 9946b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 9952c42a146SMarcel Moolenaar break; 9962c42a146SMarcel Moolenaar case SIG_UNBLOCK: 9974093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 9984093529dSJeff Roberson signotify(td); 999407af02bSDavid Xu goto out; 10002c42a146SMarcel Moolenaar case SIG_SETMASK: 1001645682fdSLuoqi Chen SIG_CANTMASK(*set); 10026b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 100384440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 10044093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1005645682fdSLuoqi Chen else 10064093529dSJeff Roberson td->td_sigmask = *set; 10076b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10086b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10094093529dSJeff Roberson signotify(td); 10102c42a146SMarcel Moolenaar break; 10112c42a146SMarcel Moolenaar default: 10122c42a146SMarcel Moolenaar error = EINVAL; 1013407af02bSDavid Xu goto out; 10142c42a146SMarcel Moolenaar } 10156b286ee8SKonstantin Belousov 10166b286ee8SKonstantin Belousov /* 10177df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 10186b286ee8SKonstantin Belousov * blocked, but are blocked now. 10196b286ee8SKonstantin Belousov * 10206b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 10216b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1022407af02bSDavid Xu * signal delivery to some thread that does not block the 1023407af02bSDavid Xu * signal, possibly waking it up. 10246b286ee8SKonstantin Belousov */ 10256b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 102680a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1027407af02bSDavid Xu } 10286b286ee8SKonstantin Belousov 1029407af02bSDavid Xu out: 103084440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 10316b286ee8SKonstantin Belousov PROC_UNLOCK(p); 10322c42a146SMarcel Moolenaar return (error); 10332c42a146SMarcel Moolenaar } 10342c42a146SMarcel Moolenaar 1035d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1036df8bae1dSRodney W. Grimes struct sigprocmask_args { 1037df8bae1dSRodney W. Grimes int how; 10382c42a146SMarcel Moolenaar const sigset_t *set; 10392c42a146SMarcel Moolenaar sigset_t *oset; 1040df8bae1dSRodney W. Grimes }; 1041d2d3e875SBruce Evans #endif 104226f9a767SRodney W. Grimes int 10438451d0ddSKip Macy sys_sigprocmask(td, uap) 1044b40ce416SJulian Elischer register struct thread *td; 1045df8bae1dSRodney W. Grimes struct sigprocmask_args *uap; 1046df8bae1dSRodney W. Grimes { 10472c42a146SMarcel Moolenaar sigset_t set, oset; 10482c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 10492c42a146SMarcel Moolenaar int error; 1050df8bae1dSRodney W. Grimes 10516f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 10526f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 10532c42a146SMarcel Moolenaar if (setp) { 10546f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 10552c42a146SMarcel Moolenaar if (error) 10562c42a146SMarcel Moolenaar return (error); 1057df8bae1dSRodney W. Grimes } 1058e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 10592c42a146SMarcel Moolenaar if (osetp && !error) { 10606f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 10612c42a146SMarcel Moolenaar } 10622c42a146SMarcel Moolenaar return (error); 10632c42a146SMarcel Moolenaar } 10642c42a146SMarcel Moolenaar 106531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 10662c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 10672c42a146SMarcel Moolenaar struct osigprocmask_args { 10682c42a146SMarcel Moolenaar int how; 10692c42a146SMarcel Moolenaar osigset_t mask; 10702c42a146SMarcel Moolenaar }; 10712c42a146SMarcel Moolenaar #endif 10722c42a146SMarcel Moolenaar int 1073b40ce416SJulian Elischer osigprocmask(td, uap) 1074b40ce416SJulian Elischer register struct thread *td; 10752c42a146SMarcel Moolenaar struct osigprocmask_args *uap; 10762c42a146SMarcel Moolenaar { 10772c42a146SMarcel Moolenaar sigset_t set, oset; 10782c42a146SMarcel Moolenaar int error; 10792c42a146SMarcel Moolenaar 10802c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1081e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1082b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1083df8bae1dSRodney W. Grimes return (error); 1084df8bae1dSRodney W. Grimes } 108531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1086df8bae1dSRodney W. Grimes 108726f9a767SRodney W. Grimes int 10888451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1089a447cd8bSJeff Roberson { 10909104847fSDavid Xu ksiginfo_t ksi; 1091a447cd8bSJeff Roberson sigset_t set; 1092a447cd8bSJeff Roberson int error; 1093a447cd8bSJeff Roberson 1094a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 109536939a0aSDavid Xu if (error) { 109636939a0aSDavid Xu td->td_retval[0] = error; 109736939a0aSDavid Xu return (0); 109836939a0aSDavid Xu } 1099a447cd8bSJeff Roberson 11009104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 110136939a0aSDavid Xu if (error) { 11028e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 11038e9a54eeSKonstantin Belousov error = ERESTART; 110436939a0aSDavid Xu if (error == ERESTART) 1105a447cd8bSJeff Roberson return (error); 110636939a0aSDavid Xu td->td_retval[0] = error; 110736939a0aSDavid Xu return (0); 110836939a0aSDavid Xu } 1109a447cd8bSJeff Roberson 11109104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 111136939a0aSDavid Xu td->td_retval[0] = error; 111236939a0aSDavid Xu return (0); 1113a447cd8bSJeff Roberson } 11140c14ff0eSRobert Watson 1115a447cd8bSJeff Roberson int 11168451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1117a447cd8bSJeff Roberson { 1118a447cd8bSJeff Roberson struct timespec ts; 1119a447cd8bSJeff Roberson struct timespec *timeout; 1120a447cd8bSJeff Roberson sigset_t set; 11219104847fSDavid Xu ksiginfo_t ksi; 1122a447cd8bSJeff Roberson int error; 1123a447cd8bSJeff Roberson 1124a447cd8bSJeff Roberson if (uap->timeout) { 1125a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1126a447cd8bSJeff Roberson if (error) 1127a447cd8bSJeff Roberson return (error); 1128a447cd8bSJeff Roberson 1129a447cd8bSJeff Roberson timeout = &ts; 1130a447cd8bSJeff Roberson } else 1131a447cd8bSJeff Roberson timeout = NULL; 1132a447cd8bSJeff Roberson 1133a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1134a447cd8bSJeff Roberson if (error) 1135a447cd8bSJeff Roberson return (error); 1136a447cd8bSJeff Roberson 11379104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1138a447cd8bSJeff Roberson if (error) 1139a447cd8bSJeff Roberson return (error); 11409dde3bc9SDavid Xu 1141418228dfSDavid Xu if (uap->info) 11429104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 11439104847fSDavid Xu 11449104847fSDavid Xu if (error == 0) 11459104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1146a447cd8bSJeff Roberson return (error); 1147a447cd8bSJeff Roberson } 1148a447cd8bSJeff Roberson 1149a447cd8bSJeff Roberson int 11508451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1151a447cd8bSJeff Roberson { 11529104847fSDavid Xu ksiginfo_t ksi; 1153a447cd8bSJeff Roberson sigset_t set; 1154a447cd8bSJeff Roberson int error; 1155a447cd8bSJeff Roberson 1156a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1157a447cd8bSJeff Roberson if (error) 1158a447cd8bSJeff Roberson return (error); 1159a447cd8bSJeff Roberson 11609104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1161a447cd8bSJeff Roberson if (error) 1162a447cd8bSJeff Roberson return (error); 1163a447cd8bSJeff Roberson 1164418228dfSDavid Xu if (uap->info) 11659104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 11669104847fSDavid Xu 11679104847fSDavid Xu if (error == 0) 11689104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1169a447cd8bSJeff Roberson return (error); 1170a447cd8bSJeff Roberson } 1171a447cd8bSJeff Roberson 1172c6511aeaSDavid Xu int 11739104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1174a447cd8bSJeff Roberson struct timespec *timeout) 1175a447cd8bSJeff Roberson { 11763074d1b4SDavid Xu struct sigacts *ps; 1177407af02bSDavid Xu sigset_t saved_mask, new_block; 1178a447cd8bSJeff Roberson struct proc *p; 1179407af02bSDavid Xu int error, sig, timo, timevalid = 0; 11801089f031SDavid Xu struct timespec rts, ets, ts; 11811089f031SDavid Xu struct timeval tv; 1182a447cd8bSJeff Roberson 1183a447cd8bSJeff Roberson p = td->td_proc; 1184a447cd8bSJeff Roberson error = 0; 11859b73d239SMatt Jacob ets.tv_sec = 0; 11869b73d239SMatt Jacob ets.tv_nsec = 0; 1187a447cd8bSJeff Roberson 1188407af02bSDavid Xu if (timeout != NULL) { 11891089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 11901089f031SDavid Xu timevalid = 1; 11911089f031SDavid Xu getnanouptime(&rts); 11921089f031SDavid Xu ets = rts; 11931089f031SDavid Xu timespecadd(&ets, timeout); 11941089f031SDavid Xu } 11951089f031SDavid Xu } 1196407af02bSDavid Xu ksiginfo_init(ksi); 1197407af02bSDavid Xu /* Some signals can not be waited for. */ 1198407af02bSDavid Xu SIG_CANTMASK(waitset); 1199407af02bSDavid Xu ps = p->p_sigacts; 1200407af02bSDavid Xu PROC_LOCK(p); 1201407af02bSDavid Xu saved_mask = td->td_sigmask; 1202407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1203407af02bSDavid Xu for (;;) { 12043074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1205f33a947bSKonstantin Belousov sig = cursig(td, SIG_STOP_ALLOWED); 12063074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 1207407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1208407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1209407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1210407af02bSDavid Xu error = 0; 1211407af02bSDavid Xu break; 12123074d1b4SDavid Xu } 12137e0221a2SDavid Xu } 12147e0221a2SDavid Xu 1215407af02bSDavid Xu if (error != 0) 1216407af02bSDavid Xu break; 12173074d1b4SDavid Xu 1218a447cd8bSJeff Roberson /* 1219a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1220a447cd8bSJeff Roberson * signals. 1221a447cd8bSJeff Roberson */ 1222407af02bSDavid Xu if (timeout != NULL) { 12231089f031SDavid Xu if (!timevalid) { 1224a447cd8bSJeff Roberson error = EINVAL; 1225407af02bSDavid Xu break; 1226a447cd8bSJeff Roberson } 12271089f031SDavid Xu getnanouptime(&rts); 12281089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 12293074d1b4SDavid Xu error = EAGAIN; 1230407af02bSDavid Xu break; 12313074d1b4SDavid Xu } 12321089f031SDavid Xu ts = ets; 12331089f031SDavid Xu timespecsub(&ts, &rts); 12341089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1235407af02bSDavid Xu timo = tvtohz(&tv); 1236407af02bSDavid Xu } else { 1237407af02bSDavid Xu timo = 0; 1238407af02bSDavid Xu } 1239a447cd8bSJeff Roberson 1240407af02bSDavid Xu error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo); 1241407af02bSDavid Xu 1242407af02bSDavid Xu if (timeout != NULL) { 12431089f031SDavid Xu if (error == ERESTART) { 1244407af02bSDavid Xu /* Timeout can not be restarted. */ 12453074d1b4SDavid Xu error = EINTR; 12461089f031SDavid Xu } else if (error == EAGAIN) { 1247407af02bSDavid Xu /* We will calculate timeout by ourself. */ 12481089f031SDavid Xu error = 0; 12491089f031SDavid Xu } 12501089f031SDavid Xu } 1251407af02bSDavid Xu } 12529dde3bc9SDavid Xu 1253407af02bSDavid Xu new_block = saved_mask; 1254407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1255407af02bSDavid Xu td->td_sigmask = saved_mask; 1256407af02bSDavid Xu /* 1257407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1258407af02bSDavid Xu * notification. 1259407af02bSDavid Xu */ 1260407af02bSDavid Xu if (p->p_numthreads != 1) 1261407af02bSDavid Xu reschedule_signals(p, new_block, 0); 12625d217f17SJohn Birrell 1263407af02bSDavid Xu if (error == 0) { 12645d217f17SJohn Birrell SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0); 12655d217f17SJohn Birrell 126656c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 126756c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 12687e0221a2SDavid Xu 12697e0221a2SDavid Xu #ifdef KTRACE 12707e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 12717e0221a2SDavid Xu sig_t action; 12727e0221a2SDavid Xu 12733074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1274a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 127590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 127661009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 12777e0221a2SDavid Xu } 1278a447cd8bSJeff Roberson #endif 12797b8d5e48SDavid Xu if (sig == SIGKILL) 12807e0221a2SDavid Xu sigexit(td, sig); 12813074d1b4SDavid Xu } 1282a447cd8bSJeff Roberson PROC_UNLOCK(p); 1283a447cd8bSJeff Roberson return (error); 1284a447cd8bSJeff Roberson } 1285a447cd8bSJeff Roberson 12869104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 12879104847fSDavid Xu struct sigpending_args { 12889104847fSDavid Xu sigset_t *set; 12899104847fSDavid Xu }; 12909104847fSDavid Xu #endif 1291a447cd8bSJeff Roberson int 12928451d0ddSKip Macy sys_sigpending(td, uap) 1293b40ce416SJulian Elischer struct thread *td; 1294df8bae1dSRodney W. Grimes struct sigpending_args *uap; 1295df8bae1dSRodney W. Grimes { 1296b40ce416SJulian Elischer struct proc *p = td->td_proc; 12979104847fSDavid Xu sigset_t pending; 1298df8bae1dSRodney W. Grimes 1299628d2653SJohn Baldwin PROC_LOCK(p); 13009104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13019104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1302628d2653SJohn Baldwin PROC_UNLOCK(p); 13039104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 13042c42a146SMarcel Moolenaar } 13052c42a146SMarcel Moolenaar 130631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 13072c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 13082c42a146SMarcel Moolenaar struct osigpending_args { 13092c42a146SMarcel Moolenaar int dummy; 13102c42a146SMarcel Moolenaar }; 13112c42a146SMarcel Moolenaar #endif 13122c42a146SMarcel Moolenaar int 1313b40ce416SJulian Elischer osigpending(td, uap) 1314b40ce416SJulian Elischer struct thread *td; 13152c42a146SMarcel Moolenaar struct osigpending_args *uap; 13162c42a146SMarcel Moolenaar { 1317b40ce416SJulian Elischer struct proc *p = td->td_proc; 13189104847fSDavid Xu sigset_t pending; 1319b40ce416SJulian Elischer 1320628d2653SJohn Baldwin PROC_LOCK(p); 13219104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13229104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1323628d2653SJohn Baldwin PROC_UNLOCK(p); 13249104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1325df8bae1dSRodney W. Grimes return (0); 1326df8bae1dSRodney W. Grimes } 132731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1328df8bae1dSRodney W. Grimes 13291930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1330df8bae1dSRodney W. Grimes /* 1331df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1332df8bae1dSRodney W. Grimes */ 1333d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1334df8bae1dSRodney W. Grimes struct osigvec_args { 1335df8bae1dSRodney W. Grimes int signum; 1336df8bae1dSRodney W. Grimes struct sigvec *nsv; 1337df8bae1dSRodney W. Grimes struct sigvec *osv; 1338df8bae1dSRodney W. Grimes }; 1339d2d3e875SBruce Evans #endif 1340df8bae1dSRodney W. Grimes /* ARGSUSED */ 134126f9a767SRodney W. Grimes int 1342b40ce416SJulian Elischer osigvec(td, uap) 1343b40ce416SJulian Elischer struct thread *td; 1344df8bae1dSRodney W. Grimes register struct osigvec_args *uap; 1345df8bae1dSRodney W. Grimes { 1346df8bae1dSRodney W. Grimes struct sigvec vec; 13472c42a146SMarcel Moolenaar struct sigaction nsa, osa; 13482c42a146SMarcel Moolenaar register struct sigaction *nsap, *osap; 13492c42a146SMarcel Moolenaar int error; 1350df8bae1dSRodney W. Grimes 13516f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 13526f841fb7SMarcel Moolenaar return (EINVAL); 13536f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 13546f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 13552c42a146SMarcel Moolenaar if (nsap) { 13566f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 13572c42a146SMarcel Moolenaar if (error) 1358df8bae1dSRodney W. Grimes return (error); 13592c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 13602c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 13612c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 13622c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1363df8bae1dSRodney W. Grimes } 13645edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 13652c42a146SMarcel Moolenaar if (osap && !error) { 13662c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 13672c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 13682c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 13692c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 13702c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 13716f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 13722c42a146SMarcel Moolenaar } 13732c42a146SMarcel Moolenaar return (error); 1374df8bae1dSRodney W. Grimes } 1375df8bae1dSRodney W. Grimes 1376d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1377df8bae1dSRodney W. Grimes struct osigblock_args { 1378df8bae1dSRodney W. Grimes int mask; 1379df8bae1dSRodney W. Grimes }; 1380d2d3e875SBruce Evans #endif 138126f9a767SRodney W. Grimes int 1382b40ce416SJulian Elischer osigblock(td, uap) 1383b40ce416SJulian Elischer register struct thread *td; 1384df8bae1dSRodney W. Grimes struct osigblock_args *uap; 1385df8bae1dSRodney W. Grimes { 1386d6e029adSKonstantin Belousov sigset_t set, oset; 1387df8bae1dSRodney W. Grimes 13882c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1389d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1390d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1391df8bae1dSRodney W. Grimes return (0); 1392df8bae1dSRodney W. Grimes } 1393df8bae1dSRodney W. Grimes 1394d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1395df8bae1dSRodney W. Grimes struct osigsetmask_args { 1396df8bae1dSRodney W. Grimes int mask; 1397df8bae1dSRodney W. Grimes }; 1398d2d3e875SBruce Evans #endif 139926f9a767SRodney W. Grimes int 1400b40ce416SJulian Elischer osigsetmask(td, uap) 1401b40ce416SJulian Elischer struct thread *td; 1402df8bae1dSRodney W. Grimes struct osigsetmask_args *uap; 1403df8bae1dSRodney W. Grimes { 1404d6e029adSKonstantin Belousov sigset_t set, oset; 1405df8bae1dSRodney W. Grimes 14062c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1407d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1408d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1409df8bae1dSRodney W. Grimes return (0); 1410df8bae1dSRodney W. Grimes } 14111930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1412df8bae1dSRodney W. Grimes 1413df8bae1dSRodney W. Grimes /* 1414873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1415873fbcd7SRobert Watson * meantime. 1416df8bae1dSRodney W. Grimes */ 1417d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1418df8bae1dSRodney W. Grimes struct sigsuspend_args { 14192c42a146SMarcel Moolenaar const sigset_t *sigmask; 1420df8bae1dSRodney W. Grimes }; 1421d2d3e875SBruce Evans #endif 1422df8bae1dSRodney W. Grimes /* ARGSUSED */ 142326f9a767SRodney W. Grimes int 14248451d0ddSKip Macy sys_sigsuspend(td, uap) 1425b40ce416SJulian Elischer struct thread *td; 1426df8bae1dSRodney W. Grimes struct sigsuspend_args *uap; 1427df8bae1dSRodney W. Grimes { 14282c42a146SMarcel Moolenaar sigset_t mask; 14292c42a146SMarcel Moolenaar int error; 14302c42a146SMarcel Moolenaar 14316f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 14322c42a146SMarcel Moolenaar if (error) 14332c42a146SMarcel Moolenaar return (error); 14348f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 14358f19eb88SIan Dowse } 14368f19eb88SIan Dowse 14378f19eb88SIan Dowse int 14388f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 14398f19eb88SIan Dowse { 14408f19eb88SIan Dowse struct proc *p = td->td_proc; 144184440afbSKonstantin Belousov int has_sig, sig; 1442df8bae1dSRodney W. Grimes 1443df8bae1dSRodney W. Grimes /* 1444645682fdSLuoqi Chen * When returning from sigsuspend, we want 1445df8bae1dSRodney W. Grimes * the old mask to be restored after the 1446df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1447df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1448df8bae1dSRodney W. Grimes * to indicate this. 1449df8bae1dSRodney W. Grimes */ 1450628d2653SJohn Baldwin PROC_LOCK(p); 145184440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 145284440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 14535e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 145484440afbSKonstantin Belousov 145584440afbSKonstantin Belousov /* 145684440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 145784440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 145884440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 145984440afbSKonstantin Belousov * delivery. 146084440afbSKonstantin Belousov */ 1461afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 146284440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 146384440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 146484440afbSKonstantin Belousov 0) == 0) 14652c42a146SMarcel Moolenaar /* void */; 146684440afbSKonstantin Belousov thread_suspend_check(0); 146784440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 146875c586a4SKonstantin Belousov while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0) 146975c586a4SKonstantin Belousov has_sig += postsig(sig); 147084440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 147184440afbSKonstantin Belousov } 1472628d2653SJohn Baldwin PROC_UNLOCK(p); 14732dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 14742dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 147575c586a4SKonstantin Belousov return (EJUSTRETURN); 14762c42a146SMarcel Moolenaar } 14772c42a146SMarcel Moolenaar 147831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 147997563428SAlexander Kabaev /* 148097563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 148197563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 148297563428SAlexander Kabaev */ 14832c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 14842c42a146SMarcel Moolenaar struct osigsuspend_args { 14852c42a146SMarcel Moolenaar osigset_t mask; 14862c42a146SMarcel Moolenaar }; 14872c42a146SMarcel Moolenaar #endif 14882c42a146SMarcel Moolenaar /* ARGSUSED */ 14892c42a146SMarcel Moolenaar int 1490b40ce416SJulian Elischer osigsuspend(td, uap) 1491b40ce416SJulian Elischer struct thread *td; 14922c42a146SMarcel Moolenaar struct osigsuspend_args *uap; 14932c42a146SMarcel Moolenaar { 1494645682fdSLuoqi Chen sigset_t mask; 14952c42a146SMarcel Moolenaar 1496645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 149784440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1498df8bae1dSRodney W. Grimes } 149931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1500df8bae1dSRodney W. Grimes 15011930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1502d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1503df8bae1dSRodney W. Grimes struct osigstack_args { 1504df8bae1dSRodney W. Grimes struct sigstack *nss; 1505df8bae1dSRodney W. Grimes struct sigstack *oss; 1506df8bae1dSRodney W. Grimes }; 1507d2d3e875SBruce Evans #endif 1508df8bae1dSRodney W. Grimes /* ARGSUSED */ 150926f9a767SRodney W. Grimes int 1510b40ce416SJulian Elischer osigstack(td, uap) 1511b40ce416SJulian Elischer struct thread *td; 1512df8bae1dSRodney W. Grimes register struct osigstack_args *uap; 1513df8bae1dSRodney W. Grimes { 15145afe0c99SJohn Baldwin struct sigstack nss, oss; 1515fb99ab88SMatthew Dillon int error = 0; 1516fb99ab88SMatthew Dillon 1517d034d459SMarcel Moolenaar if (uap->nss != NULL) { 15185afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 15195afe0c99SJohn Baldwin if (error) 15205afe0c99SJohn Baldwin return (error); 1521df8bae1dSRodney W. Grimes } 1522a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 15235afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 15245afe0c99SJohn Baldwin if (uap->nss != NULL) { 1525a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1526a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1527a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1528a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 15295afe0c99SJohn Baldwin } 15305afe0c99SJohn Baldwin if (uap->oss != NULL) 15315afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 15325afe0c99SJohn Baldwin 1533fb99ab88SMatthew Dillon return (error); 1534df8bae1dSRodney W. Grimes } 15351930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1536df8bae1dSRodney W. Grimes 1537d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1538df8bae1dSRodney W. Grimes struct sigaltstack_args { 15392c42a146SMarcel Moolenaar stack_t *ss; 15402c42a146SMarcel Moolenaar stack_t *oss; 1541df8bae1dSRodney W. Grimes }; 1542d2d3e875SBruce Evans #endif 1543df8bae1dSRodney W. Grimes /* ARGSUSED */ 154426f9a767SRodney W. Grimes int 15458451d0ddSKip Macy sys_sigaltstack(td, uap) 1546b40ce416SJulian Elischer struct thread *td; 1547df8bae1dSRodney W. Grimes register struct sigaltstack_args *uap; 1548df8bae1dSRodney W. Grimes { 15498f19eb88SIan Dowse stack_t ss, oss; 15508f19eb88SIan Dowse int error; 15518f19eb88SIan Dowse 15528f19eb88SIan Dowse if (uap->ss != NULL) { 15538f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 15548f19eb88SIan Dowse if (error) 15558f19eb88SIan Dowse return (error); 15568f19eb88SIan Dowse } 15578f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 15588f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 15598f19eb88SIan Dowse if (error) 15608f19eb88SIan Dowse return (error); 15618f19eb88SIan Dowse if (uap->oss != NULL) 15628f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 15638f19eb88SIan Dowse return (error); 15648f19eb88SIan Dowse } 15658f19eb88SIan Dowse 15668f19eb88SIan Dowse int 15678f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 15688f19eb88SIan Dowse { 1569b40ce416SJulian Elischer struct proc *p = td->td_proc; 1570fb99ab88SMatthew Dillon int oonstack; 1571fb99ab88SMatthew Dillon 1572b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1573d034d459SMarcel Moolenaar 15748f19eb88SIan Dowse if (oss != NULL) { 1575a30ec4b9SDavid Xu *oss = td->td_sigstk; 1576a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1577d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1578df8bae1dSRodney W. Grimes } 1579d034d459SMarcel Moolenaar 15808f19eb88SIan Dowse if (ss != NULL) { 1581a30ec4b9SDavid Xu if (oonstack) 1582cf60731bSJohn Baldwin return (EPERM); 1583a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1584cf60731bSJohn Baldwin return (EINVAL); 15858f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 15869104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1587cf60731bSJohn Baldwin return (ENOMEM); 15889104847fSDavid Xu 1589a30ec4b9SDavid Xu td->td_sigstk = *ss; 1590a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1591628d2653SJohn Baldwin } else { 1592a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1593628d2653SJohn Baldwin } 1594d034d459SMarcel Moolenaar } 1595cf60731bSJohn Baldwin return (0); 1596df8bae1dSRodney W. Grimes } 1597df8bae1dSRodney W. Grimes 1598d93f860cSPoul-Henning Kamp /* 1599d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1600d93f860cSPoul-Henning Kamp * cp is calling process. 1601d93f860cSPoul-Henning Kamp */ 160237c84183SPoul-Henning Kamp static int 1603a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1604d93f860cSPoul-Henning Kamp { 1605a3de221dSKonstantin Belousov struct proc *p; 1606d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 16073d74f47bSEitan Adler int err; 16083d74f47bSEitan Adler int ret; 1609d93f860cSPoul-Henning Kamp 16103d74f47bSEitan Adler ret = ESRCH; 1611553629ebSJake Burkholder if (all) { 1612d93f860cSPoul-Henning Kamp /* 1613d93f860cSPoul-Henning Kamp * broadcast 1614d93f860cSPoul-Henning Kamp */ 16151005a129SJohn Baldwin sx_slock(&allproc_lock); 16164f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1617628d2653SJohn Baldwin PROC_LOCK(p); 16189c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 161995f16c1eSPaul Saab p == td->td_proc || p->p_state == PRS_NEW) { 1620628d2653SJohn Baldwin PROC_UNLOCK(p); 1621628d2653SJohn Baldwin continue; 1622628d2653SJohn Baldwin } 16233d74f47bSEitan Adler err = p_cansignal(td, p, sig); 16243d74f47bSEitan Adler if (err == 0) { 162533a9ed9dSJohn Baldwin if (sig) 1626a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 16273d74f47bSEitan Adler ret = err; 1628628d2653SJohn Baldwin } 16293d74f47bSEitan Adler else if (ret == ESRCH) 16303d74f47bSEitan Adler ret = err; 163133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1632d93f860cSPoul-Henning Kamp } 16331005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1634553629ebSJake Burkholder } else { 1635ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1636f591779bSSeigo Tanimura if (pgid == 0) { 1637d93f860cSPoul-Henning Kamp /* 1638d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1639d93f860cSPoul-Henning Kamp */ 16409c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1641f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1642f591779bSSeigo Tanimura } else { 1643d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1644f591779bSSeigo Tanimura if (pgrp == NULL) { 1645ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1646d93f860cSPoul-Henning Kamp return (ESRCH); 1647d93f860cSPoul-Henning Kamp } 1648f591779bSSeigo Tanimura } 1649ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 16502e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1651628d2653SJohn Baldwin PROC_LOCK(p); 165295f16c1eSPaul Saab if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 165395f16c1eSPaul Saab p->p_state == PRS_NEW) { 1654628d2653SJohn Baldwin PROC_UNLOCK(p); 1655628d2653SJohn Baldwin continue; 1656628d2653SJohn Baldwin } 16573d74f47bSEitan Adler err = p_cansignal(td, p, sig); 16583d74f47bSEitan Adler if (err == 0) { 165933a9ed9dSJohn Baldwin if (sig) 1660a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 16613d74f47bSEitan Adler ret = err; 1662628d2653SJohn Baldwin } 16633d74f47bSEitan Adler else if (ret == ESRCH) 16643d74f47bSEitan Adler ret = err; 166533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1666d93f860cSPoul-Henning Kamp } 1667f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1668d93f860cSPoul-Henning Kamp } 16693d74f47bSEitan Adler return (ret); 1670d93f860cSPoul-Henning Kamp } 1671d93f860cSPoul-Henning Kamp 1672d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1673df8bae1dSRodney W. Grimes struct kill_args { 1674df8bae1dSRodney W. Grimes int pid; 1675df8bae1dSRodney W. Grimes int signum; 1676df8bae1dSRodney W. Grimes }; 1677d2d3e875SBruce Evans #endif 1678df8bae1dSRodney W. Grimes /* ARGSUSED */ 167926f9a767SRodney W. Grimes int 16808451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1681df8bae1dSRodney W. Grimes { 1682a3de221dSKonstantin Belousov ksiginfo_t ksi; 1683a3de221dSKonstantin Belousov struct proc *p; 168490af4afaSJohn Baldwin int error; 1685df8bae1dSRodney W. Grimes 16868890f5d0SPawel Jakub Dawidek /* 16878890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 16888890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 16898890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 16908890f5d0SPawel Jakub Dawidek */ 16918890f5d0SPawel Jakub Dawidek if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid) 16928890f5d0SPawel Jakub Dawidek return (ECAPMODE); 16938890f5d0SPawel Jakub Dawidek 169414961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 169514961ba7SRobert Watson AUDIT_ARG_PID(uap->pid); 16966c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1697df8bae1dSRodney W. Grimes return (EINVAL); 1698fb99ab88SMatthew Dillon 1699a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1700a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1701a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1702a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1703a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1704a3de221dSKonstantin Belousov 1705df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1706df8bae1dSRodney W. Grimes /* kill single process */ 17070b011ea3SPawel Jakub Dawidek if ((p = pfind(uap->pid)) == NULL) { 170824b2151fSPawel Jakub Dawidek if ((p = zpfind(uap->pid)) == NULL) 170990af4afaSJohn Baldwin return (ESRCH); 17100b011ea3SPawel Jakub Dawidek } 171114961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 17121a88a252SMaxim Sobolev error = p_cansignal(td, p, uap->signum); 171390af4afaSJohn Baldwin if (error == 0 && uap->signum) 1714a3de221dSKonstantin Belousov pksignal(p, uap->signum, &ksi); 1715628d2653SJohn Baldwin PROC_UNLOCK(p); 171690af4afaSJohn Baldwin return (error); 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes switch (uap->pid) { 1719df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 1720a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 1, &ksi)); 1721df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 1722a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 0, &ksi)); 1723df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 1724a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, -uap->pid, 0, &ksi)); 1725df8bae1dSRodney W. Grimes } 172690af4afaSJohn Baldwin /* NOTREACHED */ 1727df8bae1dSRodney W. Grimes } 1728df8bae1dSRodney W. Grimes 1729cfb5f768SJonathan Anderson int 17308451d0ddSKip Macy sys_pdkill(td, uap) 1731cfb5f768SJonathan Anderson struct thread *td; 1732cfb5f768SJonathan Anderson struct pdkill_args *uap; 1733cfb5f768SJonathan Anderson { 1734cfb5f768SJonathan Anderson #ifdef PROCDESC 1735cfb5f768SJonathan Anderson struct proc *p; 1736cfb5f768SJonathan Anderson int error; 1737cfb5f768SJonathan Anderson 1738cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1739cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1740cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1741cfb5f768SJonathan Anderson return (EINVAL); 1742cfb5f768SJonathan Anderson 1743cfb5f768SJonathan Anderson error = procdesc_find(td, uap->fd, CAP_PDKILL, &p); 1744cfb5f768SJonathan Anderson if (error) 1745cfb5f768SJonathan Anderson return (error); 1746cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1747cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1748cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 17498451d0ddSKip Macy kern_psignal(p, uap->signum); 1750cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1751cfb5f768SJonathan Anderson return (error); 1752cfb5f768SJonathan Anderson #else 1753cfb5f768SJonathan Anderson return (ENOSYS); 1754cfb5f768SJonathan Anderson #endif 1755cfb5f768SJonathan Anderson } 1756cfb5f768SJonathan Anderson 17571930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1758d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1759df8bae1dSRodney W. Grimes struct okillpg_args { 1760df8bae1dSRodney W. Grimes int pgid; 1761df8bae1dSRodney W. Grimes int signum; 1762df8bae1dSRodney W. Grimes }; 1763d2d3e875SBruce Evans #endif 1764df8bae1dSRodney W. Grimes /* ARGSUSED */ 176526f9a767SRodney W. Grimes int 1766a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1767df8bae1dSRodney W. Grimes { 1768a3de221dSKonstantin Belousov ksiginfo_t ksi; 1769df8bae1dSRodney W. Grimes 177014961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 177114961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 17726c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1773df8bae1dSRodney W. Grimes return (EINVAL); 17749104847fSDavid Xu 1775a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1776a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1777a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1778a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1779a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1780a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1781df8bae1dSRodney W. Grimes } 17821930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1783df8bae1dSRodney W. Grimes 17849104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 17859104847fSDavid Xu struct sigqueue_args { 17869104847fSDavid Xu pid_t pid; 17879104847fSDavid Xu int signum; 17889104847fSDavid Xu /* union sigval */ void *value; 17899104847fSDavid Xu }; 17909104847fSDavid Xu #endif 17919104847fSDavid Xu int 17928451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 17939104847fSDavid Xu { 17949104847fSDavid Xu ksiginfo_t ksi; 17959104847fSDavid Xu struct proc *p; 17969104847fSDavid Xu int error; 17979104847fSDavid Xu 17989104847fSDavid Xu if ((u_int)uap->signum > _SIG_MAXSIG) 17999104847fSDavid Xu return (EINVAL); 18009104847fSDavid Xu 18019104847fSDavid Xu /* 18029104847fSDavid Xu * Specification says sigqueue can only send signal to 18039104847fSDavid Xu * single process. 18049104847fSDavid Xu */ 18059104847fSDavid Xu if (uap->pid <= 0) 18069104847fSDavid Xu return (EINVAL); 18079104847fSDavid Xu 18089104847fSDavid Xu if ((p = pfind(uap->pid)) == NULL) { 18099104847fSDavid Xu if ((p = zpfind(uap->pid)) == NULL) 18109104847fSDavid Xu return (ESRCH); 18119104847fSDavid Xu } 18129104847fSDavid Xu error = p_cansignal(td, p, uap->signum); 18139104847fSDavid Xu if (error == 0 && uap->signum != 0) { 18149104847fSDavid Xu ksiginfo_init(&ksi); 1815a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 18169104847fSDavid Xu ksi.ksi_signo = uap->signum; 18179104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 18189104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 18199104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 18208f0371f1SDavid Xu ksi.ksi_value.sival_ptr = uap->value; 1821ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 18229104847fSDavid Xu } 18239104847fSDavid Xu PROC_UNLOCK(p); 18249104847fSDavid Xu return (error); 18259104847fSDavid Xu } 18269104847fSDavid Xu 1827df8bae1dSRodney W. Grimes /* 1828df8bae1dSRodney W. Grimes * Send a signal to a process group. 1829df8bae1dSRodney W. Grimes */ 1830df8bae1dSRodney W. Grimes void 1831a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 1832df8bae1dSRodney W. Grimes { 1833df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1834df8bae1dSRodney W. Grimes 1835f591779bSSeigo Tanimura if (pgid != 0) { 1836ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1837f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1838ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1839f591779bSSeigo Tanimura if (pgrp != NULL) { 1840a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 1841f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1842f591779bSSeigo Tanimura } 1843f591779bSSeigo Tanimura } 1844df8bae1dSRodney W. Grimes } 1845df8bae1dSRodney W. Grimes 1846df8bae1dSRodney W. Grimes /* 1847df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1848df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1849df8bae1dSRodney W. Grimes */ 1850df8bae1dSRodney W. Grimes void 1851a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1852df8bae1dSRodney W. Grimes { 1853a3de221dSKonstantin Belousov struct proc *p; 1854df8bae1dSRodney W. Grimes 1855628d2653SJohn Baldwin if (pgrp) { 1856f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1857628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1858628d2653SJohn Baldwin PROC_LOCK(p); 1859e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 1860e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 1861a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 1862628d2653SJohn Baldwin PROC_UNLOCK(p); 1863628d2653SJohn Baldwin } 1864628d2653SJohn Baldwin } 1865df8bae1dSRodney W. Grimes } 1866df8bae1dSRodney W. Grimes 1867df8bae1dSRodney W. Grimes /* 18680c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 18690c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 18700c14ff0eSRobert Watson * normally. 1871df8bae1dSRodney W. Grimes */ 1872df8bae1dSRodney W. Grimes void 18739104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 1874df8bae1dSRodney W. Grimes { 18751bf4700bSJeff Roberson struct sigacts *ps; 1876d6e029adSKonstantin Belousov sigset_t mask; 18771bf4700bSJeff Roberson struct proc *p; 18789104847fSDavid Xu int sig; 18799104847fSDavid Xu int code; 18801bf4700bSJeff Roberson 18811bf4700bSJeff Roberson p = td->td_proc; 18829104847fSDavid Xu sig = ksi->ksi_signo; 18839104847fSDavid Xu code = ksi->ksi_code; 18849104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 18859104847fSDavid Xu 1886628d2653SJohn Baldwin PROC_LOCK(p); 1887ef3dab76STim J. Robbins ps = p->p_sigacts; 188890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 188990af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 18904093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 18911c4bcd05SJeff Roberson td->td_ru.ru_nsignals++; 1892df8bae1dSRodney W. Grimes #ifdef KTRACE 1893374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1894374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 18954093529dSJeff Roberson &td->td_sigmask, code); 1896df8bae1dSRodney W. Grimes #endif 18979104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 18989104847fSDavid Xu ksi, &td->td_sigmask); 189980a8b0f3SKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 190080a8b0f3SKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1901d6e029adSKonstantin Belousov SIGADDSET(mask, sig); 190280845402SKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 190380a8b0f3SKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 19042c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1905289ccde0SPeter Wemm /* 19068f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 1907289ccde0SPeter Wemm */ 190890af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 19092c42a146SMarcel Moolenaar if (sig != SIGCONT && 19102c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 191190af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 19122c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1913dedc04feSPeter Wemm } 191490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 19156f841fb7SMarcel Moolenaar } else { 1916f71a882fSDavid Xu /* 1917f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 1918f71a882fSDavid Xu * masking the signal or process is ignoring the 1919f71a882fSDavid Xu * signal. 1920f71a882fSDavid Xu */ 1921f71a882fSDavid Xu if (kern_forcesigexit && 1922f71a882fSDavid Xu (SIGISMEMBER(td->td_sigmask, sig) || 1923f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 1924f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 1925f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 1926f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 1927f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1928f71a882fSDavid Xu } 192990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 19306626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 19312c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 1932ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 1933df8bae1dSRodney W. Grimes } 1934628d2653SJohn Baldwin PROC_UNLOCK(p); 1935df8bae1dSRodney W. Grimes } 1936df8bae1dSRodney W. Grimes 19374093529dSJeff Roberson static struct thread * 19384093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 19394093529dSJeff Roberson { 19403074d1b4SDavid Xu struct thread *td, *signal_td; 19414093529dSJeff Roberson 19424093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 19434093529dSJeff Roberson 19444093529dSJeff Roberson /* 1945627451c1SDavid Xu * Check if current thread can handle the signal without 194615b7a831SKonstantin Belousov * switching context to another thread. 19474093529dSJeff Roberson */ 1948627451c1SDavid Xu if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 1949627451c1SDavid Xu return (curthread); 19503074d1b4SDavid Xu signal_td = NULL; 19513074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 19523074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 19533074d1b4SDavid Xu signal_td = td; 1954627451c1SDavid Xu break; 19553074d1b4SDavid Xu } 19563074d1b4SDavid Xu } 19573074d1b4SDavid Xu if (signal_td == NULL) 19583074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 19593074d1b4SDavid Xu return (signal_td); 19604093529dSJeff Roberson } 19614093529dSJeff Roberson 1962df8bae1dSRodney W. Grimes /* 1963df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 1964df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 1965df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 1966df8bae1dSRodney W. Grimes * 1967df8bae1dSRodney W. Grimes * Exceptions: 1968df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 1969df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 1970df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 1971df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 1972df8bae1dSRodney W. Grimes * 1973df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 19744dec0e67SRobert Watson * 19754dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 19764dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 19774dec0e67SRobert Watson * side effects of this unwise possibility. 1978df8bae1dSRodney W. Grimes */ 1979df8bae1dSRodney W. Grimes void 19808451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 1981df8bae1dSRodney W. Grimes { 1982a3de221dSKonstantin Belousov ksiginfo_t ksi; 1983a3de221dSKonstantin Belousov 1984a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1985a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 1986a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 1987ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 1988a3de221dSKonstantin Belousov } 1989a3de221dSKonstantin Belousov 1990ad6eec7bSJohn Baldwin int 1991a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 1992a3de221dSKonstantin Belousov { 1993a3de221dSKonstantin Belousov 1994ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 19959104847fSDavid Xu } 19969104847fSDavid Xu 1997cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 19989104847fSDavid Xu int 1999cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd) 20009104847fSDavid Xu { 2001cf7d9a8cSDavid Xu struct thread *td; 200241b3077aSJacques Vidrine 20036d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2004cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 20056d7b314bSDavid Xu if (td == NULL) 20066d7b314bSDavid Xu return (ESRCH); 2007cf7d9a8cSDavid Xu *ttd = td; 2008cf7d9a8cSDavid Xu } else { 2009cf7d9a8cSDavid Xu *ttd = NULL; 2010cf7d9a8cSDavid Xu PROC_LOCK(p); 20116d7b314bSDavid Xu } 2012cf7d9a8cSDavid Xu return (0); 20134093529dSJeff Roberson } 20144093529dSJeff Roberson 2015ad6eec7bSJohn Baldwin void 2016ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2017ad6eec7bSJohn Baldwin { 2018ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2019ad6eec7bSJohn Baldwin 2020ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2021ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2022ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2023ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2024ad6eec7bSJohn Baldwin } 2025ad6eec7bSJohn Baldwin 2026ad6eec7bSJohn Baldwin void 2027ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2028ad6eec7bSJohn Baldwin { 2029ad6eec7bSJohn Baldwin 2030ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2031ad6eec7bSJohn Baldwin } 2032ad6eec7bSJohn Baldwin 2033cf7d9a8cSDavid Xu int 2034ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 20354093529dSJeff Roberson { 20369104847fSDavid Xu sig_t action; 20379104847fSDavid Xu sigqueue_t *sigqueue; 20389104847fSDavid Xu int prop; 203990af4afaSJohn Baldwin struct sigacts *ps; 204094f0972bSDavid Xu int intrval; 20419104847fSDavid Xu int ret = 0; 2042da7bbd2cSJohn Baldwin int wakeup_swapper; 2043df8bae1dSRodney W. Grimes 2044cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 20456d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 20466d7b314bSDavid Xu 204741b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2048212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 20494093529dSJeff Roberson 2050212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 20516d7b314bSDavid Xu 20526d7b314bSDavid Xu /* 20536d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 20546d7b314bSDavid Xu */ 20556d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 20566d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 20576d7b314bSDavid Xu ksiginfo_tryfree(ksi); 20586d7b314bSDavid Xu return (ret); 20596d7b314bSDavid Xu } 20606d7b314bSDavid Xu 206190af4afaSJohn Baldwin ps = p->p_sigacts; 2062ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 20632c42a146SMarcel Moolenaar prop = sigprop(sig); 20644093529dSJeff Roberson 20656d7b314bSDavid Xu if (td == NULL) { 20666d7b314bSDavid Xu td = sigtd(p, sig, prop); 20676d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 20683074d1b4SDavid Xu } else { 20696d7b314bSDavid Xu KASSERT(td->td_proc == p, ("invalid thread")); 20709104847fSDavid Xu sigqueue = &td->td_sigqueue; 20713074d1b4SDavid Xu } 20724093529dSJeff Roberson 20735d217f17SJohn Birrell SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 ); 20745d217f17SJohn Birrell 2075df8bae1dSRodney W. Grimes /* 2076df8bae1dSRodney W. Grimes * If the signal is being ignored, 2077df8bae1dSRodney W. Grimes * then we forget about it immediately. 207890af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 2079df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 2080df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 2081df8bae1dSRodney W. Grimes */ 208290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 20830fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2084c241c5e4SSergey Kandaurov SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 ); 20855d217f17SJohn Birrell 208690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20876d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 20886d7b314bSDavid Xu ksiginfo_tryfree(ksi); 20899104847fSDavid Xu return (ret); 209090af4afaSJohn Baldwin } 20916675b36eSDavid Xu if (SIGISMEMBER(td->td_sigmask, sig)) 2092df8bae1dSRodney W. Grimes action = SIG_HOLD; 209390af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2094df8bae1dSRodney W. Grimes action = SIG_CATCH; 2095df8bae1dSRodney W. Grimes else 2096df8bae1dSRodney W. Grimes action = SIG_DFL; 209794f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 209894f0972bSDavid Xu intrval = EINTR; 209994f0972bSDavid Xu else 210094f0972bSDavid Xu intrval = ERESTART; 210190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2102df8bae1dSRodney W. Grimes 21039104847fSDavid Xu if (prop & SA_CONT) 21049104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 21059104847fSDavid Xu else if (prop & SA_STOP) { 2106df8bae1dSRodney W. Grimes /* 2107df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2108df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2109df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2110df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2111df8bae1dSRodney W. Grimes */ 2112e602ba25SJulian Elischer if ((prop & SA_TTYSTOP) && 2113e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 21146d7b314bSDavid Xu (action == SIG_DFL)) { 21156d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21166d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21179104847fSDavid Xu return (ret); 21186d7b314bSDavid Xu } 21199104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2120ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 21216933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2122ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2123ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2124ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2125ebceaf6dSDavid Xu } 2126df8bae1dSRodney W. Grimes } 21273074d1b4SDavid Xu 21289104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 21299104847fSDavid Xu if (ret != 0) 21309104847fSDavid Xu return (ret); 21316d7b314bSDavid Xu signotify(td); 21325312b1c7SDavid Xu /* 21335312b1c7SDavid Xu * Defer further processing for signals which are held, 21345312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 21355312b1c7SDavid Xu */ 21365312b1c7SDavid Xu if (action == SIG_HOLD && 21375312b1c7SDavid Xu !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 21389104847fSDavid Xu return (ret); 2139df8bae1dSRodney W. Grimes /* 214090d75f78SAlfred Perlstein * SIGKILL: Remove procfs STOPEVENTs. 214190d75f78SAlfred Perlstein */ 214290d75f78SAlfred Perlstein if (sig == SIGKILL) { 214390d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCBIC */ 214490d75f78SAlfred Perlstein p->p_stops = 0; 214590d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCCONT */ 214690d75f78SAlfred Perlstein p->p_step = 0; 214790d75f78SAlfred Perlstein wakeup(&p->p_step); 214890d75f78SAlfred Perlstein } 214990d75f78SAlfred Perlstein /* 2150e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2151e602ba25SJulian Elischer * component. Most processing occurs when the process next 2152e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 2153e602ba25SJulian Elischer * times when processing needs to be done immediatly, such as 2154e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 2155e602ba25SJulian Elischer * We try do the per-process part here. 2156df8bae1dSRodney W. Grimes */ 2157e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 21580f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 21590f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2160e602ba25SJulian Elischer if (sig == SIGKILL) { 2161137cf33dSDavid Xu /* 2162137cf33dSDavid Xu * If traced process is already stopped, 2163137cf33dSDavid Xu * then no further action is necessary. 2164137cf33dSDavid Xu */ 216583b718ebSDavid Xu if (p->p_flag & P_TRACED) 216683b718ebSDavid Xu goto out; 2167df8bae1dSRodney W. Grimes /* 2168e602ba25SJulian Elischer * SIGKILL sets process running. 2169e602ba25SJulian Elischer * It will die elsewhere. 2170e602ba25SJulian Elischer * All threads must be restarted. 2171df8bae1dSRodney W. Grimes */ 2172482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2173e602ba25SJulian Elischer goto runfast; 2174e602ba25SJulian Elischer } 2175e602ba25SJulian Elischer 2176e602ba25SJulian Elischer if (prop & SA_CONT) { 2177137cf33dSDavid Xu /* 2178137cf33dSDavid Xu * If traced process is already stopped, 2179137cf33dSDavid Xu * then no further action is necessary. 2180137cf33dSDavid Xu */ 218183b718ebSDavid Xu if (p->p_flag & P_TRACED) 218283b718ebSDavid Xu goto out; 2183e602ba25SJulian Elischer /* 2184e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 21859104847fSDavid Xu * process but don't leave the signal in sigqueue as 21861d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2187e602ba25SJulian Elischer * continue the process and leave the signal in 21889104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2189e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2190e602ba25SJulian Elischer * an event, it goes back to run state. 2191e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2192e602ba25SJulian Elischer */ 21931279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 21947b4a950aSDavid Xu PROC_SLOCK(p); 2195ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 21967b4a950aSDavid Xu PROC_SUNLOCK(p); 21976933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 21987f96995eSDavid Xu p->p_xstat = SIGCONT; 21997f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2200ebceaf6dSDavid Xu childproc_continued(p); 22017f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 22027b4a950aSDavid Xu PROC_SLOCK(p); 2203ebceaf6dSDavid Xu } 2204e602ba25SJulian Elischer if (action == SIG_DFL) { 2205a54e85fdSJeff Roberson thread_unsuspend(p); 22067b4a950aSDavid Xu PROC_SUNLOCK(p); 22079104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2208a54e85fdSJeff Roberson goto out; 2209a54e85fdSJeff Roberson } 2210a54e85fdSJeff Roberson if (action == SIG_CATCH) { 22118460a577SJohn Birrell /* 22128460a577SJohn Birrell * The process wants to catch it so it needs 22138460a577SJohn Birrell * to run at least one thread, but which one? 22148460a577SJohn Birrell */ 22157b4a950aSDavid Xu PROC_SUNLOCK(p); 2216e602ba25SJulian Elischer goto runfast; 2217e602ba25SJulian Elischer } 2218e602ba25SJulian Elischer /* 2219e602ba25SJulian Elischer * The signal is not ignored or caught. 2220e602ba25SJulian Elischer */ 222104774f23SJulian Elischer thread_unsuspend(p); 22227b4a950aSDavid Xu PROC_SUNLOCK(p); 2223e602ba25SJulian Elischer goto out; 2224e602ba25SJulian Elischer } 2225e602ba25SJulian Elischer 2226e602ba25SJulian Elischer if (prop & SA_STOP) { 2227137cf33dSDavid Xu /* 2228137cf33dSDavid Xu * If traced process is already stopped, 2229137cf33dSDavid Xu * then no further action is necessary. 2230137cf33dSDavid Xu */ 223183b718ebSDavid Xu if (p->p_flag & P_TRACED) 223283b718ebSDavid Xu goto out; 2233e602ba25SJulian Elischer /* 2234e602ba25SJulian Elischer * Already stopped, don't need to stop again 2235e602ba25SJulian Elischer * (If we did the shell could get confused). 223604774f23SJulian Elischer * Just make sure the signal STOP bit set. 2237e602ba25SJulian Elischer */ 22381279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 22399104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2240e602ba25SJulian Elischer goto out; 2241e602ba25SJulian Elischer } 2242e602ba25SJulian Elischer 2243e602ba25SJulian Elischer /* 2244e602ba25SJulian Elischer * All other kinds of signals: 2245e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2246e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2247e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 224804774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2249e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2250e602ba25SJulian Elischer */ 2251da7bbd2cSJohn Baldwin wakeup_swapper = 0; 22527b4a950aSDavid Xu PROC_SLOCK(p); 2253a54e85fdSJeff Roberson thread_lock(td); 225444f3b092SJohn Baldwin if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2255da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2256a54e85fdSJeff Roberson thread_unlock(td); 22577b4a950aSDavid Xu PROC_SUNLOCK(p); 2258da7bbd2cSJohn Baldwin if (wakeup_swapper) 2259da7bbd2cSJohn Baldwin kick_proc0(); 2260df8bae1dSRodney W. Grimes goto out; 2261df8bae1dSRodney W. Grimes /* 22629a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 22639a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2264df8bae1dSRodney W. Grimes */ 2265e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2266ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 226794f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2268df8bae1dSRodney W. Grimes goto out; 226904774f23SJulian Elischer } 2270ec8297bdSDavid Xu 2271ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2272ec8297bdSDavid Xu 227335c32a76SDavid Xu if (prop & SA_STOP) { 22740f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 227535c32a76SDavid Xu goto out; 2276e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2277e574e444SDavid Xu p->p_xstat = sig; 22787b4a950aSDavid Xu PROC_SLOCK(p); 2279d8267df7SDavid Xu sig_suspend_threads(td, p, 1); 22804093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2281ebceaf6dSDavid Xu /* 2282ebceaf6dSDavid Xu * only thread sending signal to another 2283ebceaf6dSDavid Xu * process can reach here, if thread is sending 2284ebceaf6dSDavid Xu * signal to its process, because thread does 2285ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2286ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2287ebceaf6dSDavid Xu */ 2288ebceaf6dSDavid Xu thread_stopped(p); 22897b4a950aSDavid Xu PROC_SUNLOCK(p); 22909104847fSDavid Xu sigqueue_delete_proc(p, p->p_xstat); 22917b4a950aSDavid Xu } else 22927b4a950aSDavid Xu PROC_SUNLOCK(p); 229335c32a76SDavid Xu goto out; 229435c32a76SDavid Xu } 2295e602ba25SJulian Elischer } else { 2296e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 22979104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2298e602ba25SJulian Elischer goto out; 2299e602ba25SJulian Elischer } 2300e602ba25SJulian Elischer 2301b40ce416SJulian Elischer /* 2302e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2303e602ba25SJulian Elischer * running threads. 2304b40ce416SJulian Elischer */ 2305e602ba25SJulian Elischer runfast: 230694f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 23077b4a950aSDavid Xu PROC_SLOCK(p); 2308e602ba25SJulian Elischer thread_unsuspend(p); 23097b4a950aSDavid Xu PROC_SUNLOCK(p); 2310e602ba25SJulian Elischer out: 23117b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 23127b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 23139104847fSDavid Xu return (ret); 2314e602ba25SJulian Elischer } 2315e602ba25SJulian Elischer 2316e602ba25SJulian Elischer /* 2317e602ba25SJulian Elischer * The force of a signal has been directed against a single 2318e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2319e602ba25SJulian Elischer * out of any sleep it may be in etc. 2320e602ba25SJulian Elischer */ 2321e602ba25SJulian Elischer static void 232294f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2323e602ba25SJulian Elischer { 2324e602ba25SJulian Elischer struct proc *p = td->td_proc; 2325e602ba25SJulian Elischer register int prop; 2326da7bbd2cSJohn Baldwin int wakeup_swapper; 2327e602ba25SJulian Elischer 2328da7bbd2cSJohn Baldwin wakeup_swapper = 0; 23298b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2330e602ba25SJulian Elischer prop = sigprop(sig); 2331a4c2da15SBruce Evans 23327b4a950aSDavid Xu PROC_SLOCK(p); 2333374ae2a3SJeff Roberson thread_lock(td); 2334e602ba25SJulian Elischer /* 2335aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2336e602ba25SJulian Elischer * killed in this lifetime. 2337e602ba25SJulian Elischer */ 23386eeb364bSJeff Roberson if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER) 2339b3a4fb14SDavid Xu sched_prio(td, PUSER); 234080c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2341e602ba25SJulian Elischer /* 2342e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2343e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2344e602ba25SJulian Elischer * be noticed when the process returns through 2345e602ba25SJulian Elischer * trap() or syscall(). 2346e602ba25SJulian Elischer */ 234744f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2348374ae2a3SJeff Roberson goto out; 2349e602ba25SJulian Elischer /* 2350e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2351e602ba25SJulian Elischer * asleep, we are finished; the process should not 2352e602ba25SJulian Elischer * be awakened. 2353df8bae1dSRodney W. Grimes */ 2354e602ba25SJulian Elischer if ((prop & SA_CONT) && action == SIG_DFL) { 2355a54e85fdSJeff Roberson thread_unlock(td); 23567b4a950aSDavid Xu PROC_SUNLOCK(p); 23579104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 23584093529dSJeff Roberson /* 23594093529dSJeff Roberson * It may be on either list in this state. 23604093529dSJeff Roberson * Remove from both for now. 23614093529dSJeff Roberson */ 23629104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2363aa0fa334SJulian Elischer return; 2364df8bae1dSRodney W. Grimes } 2365df8bae1dSRodney W. Grimes 2366aa0fa334SJulian Elischer /* 2367a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2368a120a7a3SJohn Baldwin * STOP signal is deferred. 2369a120a7a3SJohn Baldwin */ 2370a120a7a3SJohn Baldwin if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY)) 2371a120a7a3SJohn Baldwin goto out; 2372a120a7a3SJohn Baldwin 2373a120a7a3SJohn Baldwin /* 2374a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2375aa0fa334SJulian Elischer */ 237644f3b092SJohn Baldwin if (td->td_priority > PUSER) 2377b3a4fb14SDavid Xu sched_prio(td, PUSER); 2378ec8297bdSDavid Xu 2379da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2380a4c2da15SBruce Evans } else { 2381df8bae1dSRodney W. Grimes /* 2382a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2383df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2384df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2385df8bae1dSRodney W. Grimes */ 2386a4c2da15SBruce Evans #ifdef SMP 238744f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2388e602ba25SJulian Elischer forward_signal(td); 23893163861cSTor Egge #endif 23906caa8a15SJohn Baldwin } 2391374ae2a3SJeff Roberson out: 23927b4a950aSDavid Xu PROC_SUNLOCK(p); 2393374ae2a3SJeff Roberson thread_unlock(td); 2394da7bbd2cSJohn Baldwin if (wakeup_swapper) 2395da7bbd2cSJohn Baldwin kick_proc0(); 2396a4c2da15SBruce Evans } 2397df8bae1dSRodney W. Grimes 2398d8267df7SDavid Xu static void 2399d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2400d8267df7SDavid Xu { 2401d8267df7SDavid Xu struct thread *td2; 2402f33a947bSKonstantin Belousov int wakeup_swapper; 2403d8267df7SDavid Xu 2404d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 24057b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2406d8267df7SDavid Xu 2407f33a947bSKonstantin Belousov wakeup_swapper = 0; 2408d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2409a54e85fdSJeff Roberson thread_lock(td2); 2410b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2411d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2412f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2413f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2414a120a7a3SJohn Baldwin /* 2415a120a7a3SJohn Baldwin * Once a thread is asleep with 2416a120a7a3SJohn Baldwin * TDF_SBDRY set, it should never 2417a120a7a3SJohn Baldwin * become suspended due to this check. 2418a120a7a3SJohn Baldwin */ 2419a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2420a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 2421f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2422d8267df7SDavid Xu thread_suspend_one(td2); 2423f33a947bSKonstantin Belousov } 2424f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2425d8267df7SDavid Xu if (sending || td != td2) 2426d8267df7SDavid Xu td2->td_flags |= TDF_ASTPENDING; 2427d8267df7SDavid Xu #ifdef SMP 2428d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2429d8267df7SDavid Xu forward_signal(td2); 2430d8267df7SDavid Xu #endif 2431d8267df7SDavid Xu } 2432a54e85fdSJeff Roberson thread_unlock(td2); 2433d8267df7SDavid Xu } 2434f33a947bSKonstantin Belousov if (wakeup_swapper) 2435f33a947bSKonstantin Belousov kick_proc0(); 2436d8267df7SDavid Xu } 2437d8267df7SDavid Xu 2438cbf4e354SDavid Xu int 24394cc9f52fSRobert Drehmel ptracestop(struct thread *td, int sig) 24404cc9f52fSRobert Drehmel { 24414cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 24424cc9f52fSRobert Drehmel 244330a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 24440f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 24454cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2446aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 24474cc9f52fSRobert Drehmel 2448904c5ec4SDavid Xu td->td_dbgflags |= TDB_XSIG; 2449cbf4e354SDavid Xu td->td_xsig = sig; 24507b4a950aSDavid Xu PROC_SLOCK(p); 2451904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 2452cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) { 2453904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 24547b4a950aSDavid Xu PROC_SUNLOCK(p); 2455cbf4e354SDavid Xu return (sig); 2456cbf4e354SDavid Xu } 2457cbf4e354SDavid Xu /* 2458cbf4e354SDavid Xu * Just make wait() to work, the last stopped thread 2459cbf4e354SDavid Xu * will win. 2460cbf4e354SDavid Xu */ 2461cbf4e354SDavid Xu p->p_xstat = sig; 2462cbf4e354SDavid Xu p->p_xthread = td; 2463cbf4e354SDavid Xu p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2464d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 24656fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 24666fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 24676fa39a73SKonstantin Belousov cv_broadcast(&p->p_dbgwait); 24686fa39a73SKonstantin Belousov } 2469cbf4e354SDavid Xu stopme: 2470a54e85fdSJeff Roberson thread_suspend_switch(td); 24717ce60f60SDavid Xu if (p->p_xthread == td) 24727ce60f60SDavid Xu p->p_xthread = NULL; 24737ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2474cbf4e354SDavid Xu break; 2475904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2476cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2477cbf4e354SDavid Xu break; 2478cbf4e354SDavid Xu goto stopme; 2479cbf4e354SDavid Xu } 2480cbf4e354SDavid Xu } 24817b4a950aSDavid Xu PROC_SUNLOCK(p); 2482cbf4e354SDavid Xu return (td->td_xsig); 24834cc9f52fSRobert Drehmel } 24844cc9f52fSRobert Drehmel 24856b286ee8SKonstantin Belousov static void 248680a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 24876b286ee8SKonstantin Belousov { 24886b286ee8SKonstantin Belousov struct sigacts *ps; 24896b286ee8SKonstantin Belousov struct thread *td; 2490407af02bSDavid Xu int sig; 24916b286ee8SKonstantin Belousov 24926b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 2493407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2494407af02bSDavid Xu return; 24956b286ee8SKonstantin Belousov ps = p->p_sigacts; 2496407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2497407af02bSDavid Xu while ((sig = sig_ffs(&block)) != 0) { 2498407af02bSDavid Xu SIGDELSET(block, sig); 2499407af02bSDavid Xu td = sigtd(p, sig, 0); 25006b286ee8SKonstantin Belousov signotify(td); 250180a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 25026b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2503407af02bSDavid Xu if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig)) 2504407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2505407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 25066b286ee8SKonstantin Belousov ERESTART)); 250780a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 25086b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 25096b286ee8SKonstantin Belousov } 25106b286ee8SKonstantin Belousov } 25116b286ee8SKonstantin Belousov 25126b286ee8SKonstantin Belousov void 25136b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 25146b286ee8SKonstantin Belousov { 25156b286ee8SKonstantin Belousov struct proc *p; 25166b286ee8SKonstantin Belousov sigset_t unblocked; 25176b286ee8SKonstantin Belousov 25186b286ee8SKonstantin Belousov p = td->td_proc; 25196b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 25206b286ee8SKonstantin Belousov 25216b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 25226b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 25236b286ee8SKonstantin Belousov return; 25246b286ee8SKonstantin Belousov 25256b286ee8SKonstantin Belousov /* 25266b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 25276b286ee8SKonstantin Belousov * about this by filling the sigmask. 25286b286ee8SKonstantin Belousov * 25296b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 25306b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 25316b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 25326b286ee8SKonstantin Belousov */ 25336b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 25346b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 25356b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 253680a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 25376b286ee8SKonstantin Belousov 25386b286ee8SKonstantin Belousov } 25396b286ee8SKonstantin Belousov 2540a120a7a3SJohn Baldwin /* Defer the delivery of SIGSTOP for the current thread. */ 2541a120a7a3SJohn Baldwin void 2542a120a7a3SJohn Baldwin sigdeferstop(struct thread *td) 2543a120a7a3SJohn Baldwin { 2544a120a7a3SJohn Baldwin 2545a120a7a3SJohn Baldwin KASSERT(!(td->td_flags & TDF_SBDRY), 2546a120a7a3SJohn Baldwin ("attempt to set TDF_SBDRY recursively")); 2547a120a7a3SJohn Baldwin thread_lock(td); 2548a120a7a3SJohn Baldwin td->td_flags |= TDF_SBDRY; 2549a120a7a3SJohn Baldwin thread_unlock(td); 2550a120a7a3SJohn Baldwin } 2551a120a7a3SJohn Baldwin 2552a120a7a3SJohn Baldwin /* 2553a120a7a3SJohn Baldwin * Permit the delivery of SIGSTOP for the current thread. This does 2554a120a7a3SJohn Baldwin * not immediately suspend if a stop was posted. Instead, the thread 2555a120a7a3SJohn Baldwin * will suspend either via ast() or a subsequent interruptible sleep. 2556a120a7a3SJohn Baldwin */ 2557a120a7a3SJohn Baldwin void 2558a120a7a3SJohn Baldwin sigallowstop(struct thread *td) 2559a120a7a3SJohn Baldwin { 2560a120a7a3SJohn Baldwin 2561a120a7a3SJohn Baldwin KASSERT(td->td_flags & TDF_SBDRY, 2562a120a7a3SJohn Baldwin ("attempt to clear already-cleared TDF_SBDRY")); 2563a120a7a3SJohn Baldwin thread_lock(td); 2564a120a7a3SJohn Baldwin td->td_flags &= ~TDF_SBDRY; 2565a120a7a3SJohn Baldwin thread_unlock(td); 2566a120a7a3SJohn Baldwin } 2567a120a7a3SJohn Baldwin 2568df8bae1dSRodney W. Grimes /* 2569df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2570df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2571df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2572df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2573df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2574628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2575df8bae1dSRodney W. Grimes * sequence is 2576df8bae1dSRodney W. Grimes * 2577e602ba25SJulian Elischer * while (sig = cursig(curthread)) 25782c42a146SMarcel Moolenaar * postsig(sig); 2579df8bae1dSRodney W. Grimes */ 25806711f10fSJohn Baldwin static int 2581f33a947bSKonstantin Belousov issignal(struct thread *td, int stop_allowed) 2582df8bae1dSRodney W. Grimes { 2583e602ba25SJulian Elischer struct proc *p; 258490af4afaSJohn Baldwin struct sigacts *ps; 25856b286ee8SKonstantin Belousov struct sigqueue *queue; 25864093529dSJeff Roberson sigset_t sigpending; 25874f17d481SKonstantin Belousov int sig, prop, newsig; 2588df8bae1dSRodney W. Grimes 2589e602ba25SJulian Elischer p = td->td_proc; 259090af4afaSJohn Baldwin ps = p->p_sigacts; 259190af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2592628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2593df8bae1dSRodney W. Grimes for (;;) { 25942a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 25952a024a2bSSean Eric Fagan 25969104847fSDavid Xu sigpending = td->td_sigqueue.sq_signals; 25976b286ee8SKonstantin Belousov SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 25984093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 25994093529dSJeff Roberson 2600a120a7a3SJohn Baldwin if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY) 26014093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 26024093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2603df8bae1dSRodney W. Grimes return (0); 26044093529dSJeff Roberson sig = sig_ffs(&sigpending); 26052a024a2bSSean Eric Fagan 2606047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2607047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2608047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2609047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2610047aa39bSRobert Watson } 26112a024a2bSSean Eric Fagan 2612df8bae1dSRodney W. Grimes /* 2613df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2614df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2615df8bae1dSRodney W. Grimes */ 261690af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 26179104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 26186b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2619df8bae1dSRodney W. Grimes continue; 2620df8bae1dSRodney W. Grimes } 2621888d4d4fSKonstantin Belousov if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) { 2622df8bae1dSRodney W. Grimes /* 2623d8f4f6a4SJonathan Mini * If traced, always stop. 26246a671a6bSKonstantin Belousov * Remove old signal from queue before the stop. 26256a671a6bSKonstantin Belousov * XXX shrug off debugger, it causes siginfo to 26266a671a6bSKonstantin Belousov * be thrown away. 2627df8bae1dSRodney W. Grimes */ 26286a671a6bSKonstantin Belousov queue = &td->td_sigqueue; 26298a260079SKonstantin Belousov td->td_dbgksi.ksi_signo = 0; 26308a260079SKonstantin Belousov if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) { 26316a671a6bSKonstantin Belousov queue = &p->p_sigqueue; 26328a260079SKonstantin Belousov sigqueue_get(queue, sig, &td->td_dbgksi); 26336a671a6bSKonstantin Belousov } 26346a671a6bSKonstantin Belousov 263590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2636cbf4e354SDavid Xu newsig = ptracestop(td, sig); 263790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2638df8bae1dSRodney W. Grimes 26399104847fSDavid Xu if (sig != newsig) { 26409104847fSDavid Xu 2641df8bae1dSRodney W. Grimes /* 2642df8bae1dSRodney W. Grimes * If parent wants us to take the signal, 2643df8bae1dSRodney W. Grimes * then it will leave it in p->p_xstat; 2644df8bae1dSRodney W. Grimes * otherwise we just look for signals again. 2645df8bae1dSRodney W. Grimes */ 2646cbf4e354SDavid Xu if (newsig == 0) 2647df8bae1dSRodney W. Grimes continue; 2648cbf4e354SDavid Xu sig = newsig; 26499104847fSDavid Xu 26509104847fSDavid Xu /* 26519104847fSDavid Xu * Put the new signal into td_sigqueue. If the 265223c6445aSPawel Jakub Dawidek * signal is being masked, look for other 265323c6445aSPawel Jakub Dawidek * signals. 26549104847fSDavid Xu */ 26556a671a6bSKonstantin Belousov sigqueue_add(queue, sig, NULL); 26569104847fSDavid Xu if (SIGISMEMBER(td->td_sigmask, sig)) 26579104847fSDavid Xu continue; 26589104847fSDavid Xu signotify(td); 26596a671a6bSKonstantin Belousov } else { 26608a260079SKonstantin Belousov if (td->td_dbgksi.ksi_signo != 0) { 26618a260079SKonstantin Belousov td->td_dbgksi.ksi_flags |= KSI_HEAD; 26626a671a6bSKonstantin Belousov if (sigqueue_add(&td->td_sigqueue, sig, 26638a260079SKonstantin Belousov &td->td_dbgksi) != 0) 26648a260079SKonstantin Belousov td->td_dbgksi.ksi_signo = 0; 26656a671a6bSKonstantin Belousov } 26668a260079SKonstantin Belousov if (td->td_dbgksi.ksi_signo == 0) 26676a671a6bSKonstantin Belousov sigqueue_add(&td->td_sigqueue, sig, 26686a671a6bSKonstantin Belousov NULL); 26699104847fSDavid Xu } 26709104847fSDavid Xu 2671df8bae1dSRodney W. Grimes /* 26728d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 26738d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 26748d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 26758d542cb5SDavid E. O'Brien */ 26768d542cb5SDavid E. O'Brien if ((p->p_flag & P_TRACED) == 0) 26778d542cb5SDavid E. O'Brien continue; 2678df8bae1dSRodney W. Grimes } 2679df8bae1dSRodney W. Grimes 26808d542cb5SDavid E. O'Brien prop = sigprop(sig); 26818d542cb5SDavid E. O'Brien 2682df8bae1dSRodney W. Grimes /* 2683df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2684df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2685df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2686df8bae1dSRodney W. Grimes */ 2687d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2688df8bae1dSRodney W. Grimes 2689d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2690df8bae1dSRodney W. Grimes /* 2691df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2692df8bae1dSRodney W. Grimes */ 2693df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2694df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2695df8bae1dSRodney W. Grimes /* 2696df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2697df8bae1dSRodney W. Grimes * in init? XXX 2698df8bae1dSRodney W. Grimes */ 2699d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 27002c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2701df8bae1dSRodney W. Grimes #endif 2702df8bae1dSRodney W. Grimes break; /* == ignore */ 2703df8bae1dSRodney W. Grimes } 2704df8bae1dSRodney W. Grimes /* 2705df8bae1dSRodney W. Grimes * If there is a pending stop signal to process 2706df8bae1dSRodney W. Grimes * with default action, stop here, 2707df8bae1dSRodney W. Grimes * then clear the signal. However, 2708df8bae1dSRodney W. Grimes * if process is member of an orphaned 2709df8bae1dSRodney W. Grimes * process group, ignore tty stop signals. 2710df8bae1dSRodney W. Grimes */ 2711df8bae1dSRodney W. Grimes if (prop & SA_STOP) { 27120f14f15bSJohn Baldwin if (p->p_flag & (P_TRACED|P_WEXIT) || 2713df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2714df8bae1dSRodney W. Grimes prop & SA_TTYSTOP)) 2715df8bae1dSRodney W. Grimes break; /* == ignore */ 271690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 271790af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2718aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Catching SIGSTOP"); 2719e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 27202c42a146SMarcel Moolenaar p->p_xstat = sig; 27217b4a950aSDavid Xu PROC_SLOCK(p); 2722d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 2723a54e85fdSJeff Roberson thread_suspend_switch(td); 27247b4a950aSDavid Xu PROC_SUNLOCK(p); 272590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2726df8bae1dSRodney W. Grimes break; 272721b68415SDavid E. O'Brien } else if (prop & SA_IGNORE) { 2728df8bae1dSRodney W. Grimes /* 2729df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2730df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2731df8bae1dSRodney W. Grimes */ 2732df8bae1dSRodney W. Grimes break; /* == ignore */ 2733df8bae1dSRodney W. Grimes } else 27342c42a146SMarcel Moolenaar return (sig); 2735df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2736df8bae1dSRodney W. Grimes 2737d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2738df8bae1dSRodney W. Grimes /* 2739df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2740df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2741df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2742df8bae1dSRodney W. Grimes */ 2743df8bae1dSRodney W. Grimes if ((prop & SA_CONT) == 0 && 2744df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2745df8bae1dSRodney W. Grimes printf("issignal\n"); 2746df8bae1dSRodney W. Grimes break; /* == ignore */ 2747df8bae1dSRodney W. Grimes 2748df8bae1dSRodney W. Grimes default: 2749df8bae1dSRodney W. Grimes /* 2750df8bae1dSRodney W. Grimes * This signal has an action, let 2751df8bae1dSRodney W. Grimes * postsig() process it. 2752df8bae1dSRodney W. Grimes */ 27532c42a146SMarcel Moolenaar return (sig); 2754df8bae1dSRodney W. Grimes } 27559104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 27566b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2757df8bae1dSRodney W. Grimes } 2758df8bae1dSRodney W. Grimes /* NOTREACHED */ 2759df8bae1dSRodney W. Grimes } 2760df8bae1dSRodney W. Grimes 2761e574e444SDavid Xu void 2762e574e444SDavid Xu thread_stopped(struct proc *p) 2763e574e444SDavid Xu { 2764e574e444SDavid Xu int n; 2765e574e444SDavid Xu 2766e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 27677b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2768e574e444SDavid Xu n = p->p_suspcount; 27697c9a98f1SDavid Xu if (p == curproc) 2770e574e444SDavid Xu n++; 2771e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 27727b4a950aSDavid Xu PROC_SUNLOCK(p); 2773407948a5SDavid Xu p->p_flag &= ~P_WAITED; 2774e574e444SDavid Xu PROC_LOCK(p->p_pptr); 27757f96995eSDavid Xu childproc_stopped(p, (p->p_flag & P_TRACED) ? 2776ebceaf6dSDavid Xu CLD_TRAPPED : CLD_STOPPED); 2777e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 27787b4a950aSDavid Xu PROC_SLOCK(p); 2779e574e444SDavid Xu } 2780e574e444SDavid Xu } 2781e574e444SDavid Xu 2782df8bae1dSRodney W. Grimes /* 2783df8bae1dSRodney W. Grimes * Take the action for the specified signal 2784df8bae1dSRodney W. Grimes * from the current set of pending signals. 2785df8bae1dSRodney W. Grimes */ 278675c586a4SKonstantin Belousov int 27872c42a146SMarcel Moolenaar postsig(sig) 27882c42a146SMarcel Moolenaar register int sig; 2789df8bae1dSRodney W. Grimes { 2790b40ce416SJulian Elischer struct thread *td = curthread; 2791b40ce416SJulian Elischer register struct proc *p = td->td_proc; 2792628d2653SJohn Baldwin struct sigacts *ps; 27932c42a146SMarcel Moolenaar sig_t action; 27949104847fSDavid Xu ksiginfo_t ksi; 2795d6e029adSKonstantin Belousov sigset_t returnmask, mask; 2796df8bae1dSRodney W. Grimes 27972c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 27985526d2d9SEivind Eklund 27992ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2800628d2653SJohn Baldwin ps = p->p_sigacts; 280190af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 28025da49fcbSDavid Xu ksiginfo_init(&ksi); 28036b286ee8SKonstantin Belousov if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 28046b286ee8SKonstantin Belousov sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 280575c586a4SKonstantin Belousov return (0); 28069104847fSDavid Xu ksi.ksi_signo = sig; 280756c06c4bSDavid Xu if (ksi.ksi_code == SI_TIMER) 280856c06c4bSDavid Xu itimer_accept(p, ksi.ksi_timerid, &ksi); 28092c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2810df8bae1dSRodney W. Grimes #ifdef KTRACE 2811374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 28125e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 281361009552SJilles Tjoelker &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 2814df8bae1dSRodney W. Grimes #endif 2815047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2816047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2817047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2818047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2819047aa39bSRobert Watson } 28202a024a2bSSean Eric Fagan 28218460a577SJohn Birrell if (action == SIG_DFL) { 2822df8bae1dSRodney W. Grimes /* 2823df8bae1dSRodney W. Grimes * Default action, where the default is to kill 2824df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 2825df8bae1dSRodney W. Grimes */ 282690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2827b40ce416SJulian Elischer sigexit(td, sig); 2828df8bae1dSRodney W. Grimes /* NOTREACHED */ 2829df8bae1dSRodney W. Grimes } else { 2830df8bae1dSRodney W. Grimes /* 2831df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 2832df8bae1dSRodney W. Grimes */ 28334093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 28345526d2d9SEivind Eklund ("postsig action")); 2835df8bae1dSRodney W. Grimes /* 2836df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 2837645682fdSLuoqi Chen * occurrences of this signal. 2838df8bae1dSRodney W. Grimes * 2839645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 2840df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 2841645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 2842df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 2843df8bae1dSRodney W. Grimes */ 28445e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 28454093529dSJeff Roberson returnmask = td->td_oldsigmask; 28465e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 2847df8bae1dSRodney W. Grimes } else 28484093529dSJeff Roberson returnmask = td->td_sigmask; 28492c42a146SMarcel Moolenaar 285080a8b0f3SKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 285180a8b0f3SKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2852d6e029adSKonstantin Belousov SIGADDSET(mask, sig); 2853d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 285480a8b0f3SKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 28552c42a146SMarcel Moolenaar 28562c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2857289ccde0SPeter Wemm /* 28588f19eb88SIan Dowse * See kern_sigaction() for origin of this code. 2859289ccde0SPeter Wemm */ 286090af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 28612c42a146SMarcel Moolenaar if (sig != SIGCONT && 28622c42a146SMarcel Moolenaar sigprop(sig) & SA_IGNORE) 286390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 28642c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2865dedc04feSPeter Wemm } 28661c4bcd05SJeff Roberson td->td_ru.ru_nsignals++; 2867c90c9021SEd Schouten if (p->p_sig == sig) { 28686626c604SJulian Elischer p->p_code = 0; 28696626c604SJulian Elischer p->p_sig = 0; 2870df8bae1dSRodney W. Grimes } 28719104847fSDavid Xu (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 2872df8bae1dSRodney W. Grimes } 287375c586a4SKonstantin Belousov return (1); 2874df8bae1dSRodney W. Grimes } 2875df8bae1dSRodney W. Grimes 2876df8bae1dSRodney W. Grimes /* 2877df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 2878df8bae1dSRodney W. Grimes */ 287926f9a767SRodney W. Grimes void 2880df8bae1dSRodney W. Grimes killproc(p, why) 2881df8bae1dSRodney W. Grimes struct proc *p; 2882df8bae1dSRodney W. Grimes char *why; 2883df8bae1dSRodney W. Grimes { 28849081e5e8SJohn Baldwin 28859081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2886c3209846SPawel Jakub Dawidek CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 2887c3209846SPawel Jakub Dawidek p->p_comm); 2888c3209846SPawel Jakub Dawidek log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, 2889c3209846SPawel Jakub Dawidek p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why); 28903f1c4c4fSKonstantin Belousov p->p_flag |= P_WKILLED; 28918451d0ddSKip Macy kern_psignal(p, SIGKILL); 2892df8bae1dSRodney W. Grimes } 2893df8bae1dSRodney W. Grimes 2894df8bae1dSRodney W. Grimes /* 2895df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 2896df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 2897df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 2898df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 2899df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 2900df8bae1dSRodney W. Grimes * does not return. 2901df8bae1dSRodney W. Grimes */ 290226f9a767SRodney W. Grimes void 2903b40ce416SJulian Elischer sigexit(td, sig) 2904b40ce416SJulian Elischer struct thread *td; 29052c42a146SMarcel Moolenaar int sig; 2906df8bae1dSRodney W. Grimes { 2907b40ce416SJulian Elischer struct proc *p = td->td_proc; 2908df8bae1dSRodney W. Grimes 2909628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2910df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 2911f97c3df1SDavid Schultz /* 2912f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 2913f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 2914f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 2915f97c3df1SDavid Schultz * change out from under it. 2916f97c3df1SDavid Schultz * 2917f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 2918f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 2919f97c3df1SDavid Schultz */ 2920f97c3df1SDavid Schultz if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) { 29212c42a146SMarcel Moolenaar p->p_sig = sig; 2922c364e17eSAndrey A. Chernov /* 2923c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 2924c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 2925c364e17eSAndrey A. Chernov * these messages.) 2926c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 29274ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 2928c364e17eSAndrey A. Chernov */ 2929b40ce416SJulian Elischer if (coredump(td) == 0) 29302c42a146SMarcel Moolenaar sig |= WCOREFLAG; 293157308494SJoerg Wunsch if (kern_logsigexit) 293257308494SJoerg Wunsch log(LOG_INFO, 293357308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 29343d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 29359c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 29362c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 29372c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 29384ae89b95SJohn Baldwin } else 2939628d2653SJohn Baldwin PROC_UNLOCK(p); 2940b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, sig)); 2941df8bae1dSRodney W. Grimes /* NOTREACHED */ 2942df8bae1dSRodney W. Grimes } 2943df8bae1dSRodney W. Grimes 2944ebceaf6dSDavid Xu /* 29457f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 29467f96995eSDavid Xu * is changed. 2947ebceaf6dSDavid Xu */ 29487f96995eSDavid Xu static void 29497f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 2950ebceaf6dSDavid Xu { 2951ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 2952ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 2953ebceaf6dSDavid Xu 2954ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2955ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 2956ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 29577f96995eSDavid Xu p->p_ksi->ksi_status = status; 2958ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 2959ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 2960ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 2961ebceaf6dSDavid Xu return; 2962ebceaf6dSDavid Xu } 2963ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 2964ebceaf6dSDavid Xu } 2965ebceaf6dSDavid Xu 29667f96995eSDavid Xu static void 29677f96995eSDavid Xu childproc_jobstate(struct proc *p, int reason, int status) 29687f96995eSDavid Xu { 29697f96995eSDavid Xu struct sigacts *ps; 29707f96995eSDavid Xu 29717f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 29727f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 29737f96995eSDavid Xu 29747f96995eSDavid Xu /* 29757f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 29767f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 29777f96995eSDavid Xu * that parent will awake, because parent may masked 29787f96995eSDavid Xu * the signal. 29797f96995eSDavid Xu */ 29807f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 29817f96995eSDavid Xu wakeup(p->p_pptr); 29827f96995eSDavid Xu 29837f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 29847f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 29857f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 29867f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 29877f96995eSDavid Xu sigparent(p, reason, status); 29887f96995eSDavid Xu } else 29897f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 29907f96995eSDavid Xu } 29917f96995eSDavid Xu 29927f96995eSDavid Xu void 29937f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 29947f96995eSDavid Xu { 29957f96995eSDavid Xu childproc_jobstate(p, reason, p->p_xstat); 29967f96995eSDavid Xu } 29977f96995eSDavid Xu 2998ebceaf6dSDavid Xu void 2999ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3000ebceaf6dSDavid Xu { 30017f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3002ebceaf6dSDavid Xu } 3003ebceaf6dSDavid Xu 3004ebceaf6dSDavid Xu void 3005ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3006ebceaf6dSDavid Xu { 3007ebceaf6dSDavid Xu int reason; 3008f4d85223SDavid Xu int status = p->p_xstat; /* convert to int */ 3009ebceaf6dSDavid Xu 3010ebceaf6dSDavid Xu reason = CLD_EXITED; 3011f4d85223SDavid Xu if (WCOREDUMP(status)) 3012ebceaf6dSDavid Xu reason = CLD_DUMPED; 3013f4d85223SDavid Xu else if (WIFSIGNALED(status)) 3014ebceaf6dSDavid Xu reason = CLD_KILLED; 30157f96995eSDavid Xu /* 30167f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 30177f96995eSDavid Xu * done in exit1(). 30187f96995eSDavid Xu */ 30197f96995eSDavid Xu sigparent(p, reason, status); 3020ebceaf6dSDavid Xu } 3021ebceaf6dSDavid Xu 3022e7228204SAlfred Perlstein /* 3023e7228204SAlfred Perlstein * We only have 1 character for the core count in the format 3024e7228204SAlfred Perlstein * string, so the range will be 0-9 3025e7228204SAlfred Perlstein */ 3026e7228204SAlfred Perlstein #define MAX_NUM_CORES 10 3027e7228204SAlfred Perlstein static int num_cores = 5; 3028e7228204SAlfred Perlstein 3029e7228204SAlfred Perlstein static int 3030e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3031e7228204SAlfred Perlstein { 3032e7228204SAlfred Perlstein int error; 3033e7228204SAlfred Perlstein int new_val; 3034e7228204SAlfred Perlstein 3035c5105012SKonstantin Belousov new_val = num_cores; 3036e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3037e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3038e7228204SAlfred Perlstein return (error); 3039e7228204SAlfred Perlstein if (new_val > MAX_NUM_CORES) 3040e7228204SAlfred Perlstein new_val = MAX_NUM_CORES; 3041e7228204SAlfred Perlstein if (new_val < 0) 3042e7228204SAlfred Perlstein new_val = 0; 3043e7228204SAlfred Perlstein num_cores = new_val; 3044e7228204SAlfred Perlstein return (0); 3045e7228204SAlfred Perlstein } 3046e7228204SAlfred Perlstein SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW, 3047e7228204SAlfred Perlstein 0, sizeof(int), sysctl_debug_num_cores_check, "I", ""); 3048e7228204SAlfred Perlstein 3049e7228204SAlfred Perlstein #if defined(COMPRESS_USER_CORES) 3050e7228204SAlfred Perlstein int compress_user_cores = 1; 3051e7228204SAlfred Perlstein SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW, 305229146f1aSPawel Jakub Dawidek &compress_user_cores, 0, "Compression of user corefiles"); 3053e7228204SAlfred Perlstein 3054e7228204SAlfred Perlstein int compress_user_cores_gzlevel = -1; /* default level */ 3055e7228204SAlfred Perlstein SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW, 305629146f1aSPawel Jakub Dawidek &compress_user_cores_gzlevel, -1, "Corefile gzip compression level"); 3057e7228204SAlfred Perlstein 3058e7228204SAlfred Perlstein #define GZ_SUFFIX ".gz" 3059e7228204SAlfred Perlstein #define GZ_SUFFIX_LEN 3 3060e7228204SAlfred Perlstein #endif 3061e7228204SAlfred Perlstein 30626d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3063b62d05fcSPawel Jakub Dawidek TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 3064c5edb423SSean Eric Fagan SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 306529146f1aSPawel Jakub Dawidek sizeof(corefilename), "Process corefile name format string"); 3066c5edb423SSean Eric Fagan 3067c5edb423SSean Eric Fagan /* 3068c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3069c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3070c345faeaSPawel Jakub Dawidek * and open/create core file. 3071c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3072c5edb423SSean Eric Fagan * %N name of process ("name") 3073c5edb423SSean Eric Fagan * %P process id (pid) 3074c5edb423SSean Eric Fagan * %U user id (uid) 3075c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3076c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3077c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3078c5edb423SSean Eric Fagan */ 3079c345faeaSPawel Jakub Dawidek static int 3080c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 3081c345faeaSPawel Jakub Dawidek int compress, struct vnode **vpp, char **namep) 30828b43b535SAlfred Perlstein { 3083c345faeaSPawel Jakub Dawidek struct nameidata nd; 308436b208e0SRobert Watson struct sbuf sb; 308536b208e0SRobert Watson const char *format; 3086c345faeaSPawel Jakub Dawidek char *hostname, *name; 3087c345faeaSPawel Jakub Dawidek int indexpos, i, error, cmode, flags, oflags; 3088c5edb423SSean Eric Fagan 3089b7402d82SAlfred Perlstein hostname = NULL; 30908b43b535SAlfred Perlstein format = corefilename; 3091086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3092e7228204SAlfred Perlstein indexpos = -1; 3093c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 309429146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3095c5edb423SSean Eric Fagan switch (format[i]) { 3096c5edb423SSean Eric Fagan case '%': /* Format character */ 3097c5edb423SSean Eric Fagan i++; 3098c5edb423SSean Eric Fagan switch (format[i]) { 3099c5edb423SSean Eric Fagan case '%': 310036b208e0SRobert Watson sbuf_putc(&sb, '%'); 3101c5edb423SSean Eric Fagan break; 3102e7228204SAlfred Perlstein case 'H': /* hostname */ 3103b7402d82SAlfred Perlstein if (hostname == NULL) { 3104b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3105086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3106b7402d82SAlfred Perlstein } 3107e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3108b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3109e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3110e7228204SAlfred Perlstein break; 3111e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3112e7228204SAlfred Perlstein sbuf_printf(&sb, "0"); 3113e7228204SAlfred Perlstein indexpos = sbuf_len(&sb) - 1; 3114e7228204SAlfred Perlstein break; 3115c5edb423SSean Eric Fagan case 'N': /* process name */ 3116c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3117c5edb423SSean Eric Fagan break; 3118c5edb423SSean Eric Fagan case 'P': /* process id */ 311936b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3120c5edb423SSean Eric Fagan break; 3121c5edb423SSean Eric Fagan case 'U': /* user id */ 312236b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3123c5edb423SSean Eric Fagan break; 3124c5edb423SSean Eric Fagan default: 31258b43b535SAlfred Perlstein log(LOG_ERR, 312636b208e0SRobert Watson "Unknown format character %c in " 312736b208e0SRobert Watson "corename `%s'\n", format[i], format); 312829146f1aSPawel Jakub Dawidek break; 3129c5edb423SSean Eric Fagan } 3130c5edb423SSean Eric Fagan break; 3131c5edb423SSean Eric Fagan default: 313236b208e0SRobert Watson sbuf_putc(&sb, format[i]); 3133*6c08be2bSPawel Jakub Dawidek break; 3134c5edb423SSean Eric Fagan } 3135c5edb423SSean Eric Fagan } 3136b7402d82SAlfred Perlstein free(hostname, M_TEMP); 3137e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 3138f06f465dSPawel Jakub Dawidek if (compress) 3139e7228204SAlfred Perlstein sbuf_printf(&sb, GZ_SUFFIX); 3140e7228204SAlfred Perlstein #endif 31414d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 314236b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3143c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 3144b7402d82SAlfred Perlstein sbuf_delete(&sb); 3145c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3146c345faeaSPawel Jakub Dawidek return (ENOMEM); 3147c5edb423SSean Eric Fagan } 314836b208e0SRobert Watson sbuf_finish(&sb); 314936b208e0SRobert Watson sbuf_delete(&sb); 3150e7228204SAlfred Perlstein 3151c345faeaSPawel Jakub Dawidek cmode = S_IRUSR | S_IWUSR; 3152c345faeaSPawel Jakub Dawidek oflags = VN_OPEN_NOAUDIT | (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 3153c345faeaSPawel Jakub Dawidek 3154e7228204SAlfred Perlstein /* 3155e7228204SAlfred Perlstein * If the core format has a %I in it, then we need to check 3156e7228204SAlfred Perlstein * for existing corefiles before returning a name. 3157e7228204SAlfred Perlstein * To do this we iterate over 0..num_cores to find a 3158e7228204SAlfred Perlstein * non-existing core file name to use. 3159e7228204SAlfred Perlstein */ 3160e7228204SAlfred Perlstein if (indexpos != -1) { 316129146f1aSPawel Jakub Dawidek for (i = 0; i < num_cores; i++) { 316207a8e078SPawel Jakub Dawidek flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW; 316329146f1aSPawel Jakub Dawidek name[indexpos] = '0' + i; 3164c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3165b0c9d4d7SPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, 3166b0c9d4d7SPawel Jakub Dawidek td->td_ucred, NULL); 3167e7228204SAlfred Perlstein if (error) { 316823c6445aSPawel Jakub Dawidek if (error == EEXIST) 3169e7228204SAlfred Perlstein continue; 3170e7228204SAlfred Perlstein log(LOG_ERR, 3171e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 3172e7228204SAlfred Perlstein "on initial open test, error = %d\n", 3173c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 3174c345faeaSPawel Jakub Dawidek } 3175c345faeaSPawel Jakub Dawidek goto out; 3176c345faeaSPawel Jakub Dawidek } 3177c345faeaSPawel Jakub Dawidek } 3178c345faeaSPawel Jakub Dawidek 3179c345faeaSPawel Jakub Dawidek flags = O_CREAT | FWRITE | O_NOFOLLOW; 3180c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3181c345faeaSPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL); 3182c345faeaSPawel Jakub Dawidek out: 3183c345faeaSPawel Jakub Dawidek if (error) { 3184c345faeaSPawel Jakub Dawidek #ifdef AUDIT 3185c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 3186c345faeaSPawel Jakub Dawidek #endif 3187c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3188c345faeaSPawel Jakub Dawidek return (error); 3189e7228204SAlfred Perlstein } 3190e7228204SAlfred Perlstein NDFREE(&nd, NDF_ONLY_PNBUF); 3191c345faeaSPawel Jakub Dawidek *vpp = nd.ni_vp; 3192c345faeaSPawel Jakub Dawidek *namep = name; 3193c345faeaSPawel Jakub Dawidek return (0); 319436b208e0SRobert Watson } 3195c5edb423SSean Eric Fagan 3196df8bae1dSRodney W. Grimes /* 3197fca666a1SJulian Elischer * Dump a process' core. The main routine does some 3198fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 3199fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 3200fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 3201fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 3202fca666a1SJulian Elischer */ 3203fca666a1SJulian Elischer 3204fca666a1SJulian Elischer static int 3205b40ce416SJulian Elischer coredump(struct thread *td) 3206fca666a1SJulian Elischer { 3207b40ce416SJulian Elischer struct proc *p = td->td_proc; 3208f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 3209f06f465dSPawel Jakub Dawidek struct vnode *vp; 321006ae1e91SMatthew Dillon struct flock lf; 3211fca666a1SJulian Elischer struct vattr vattr; 3212c345faeaSPawel Jakub Dawidek int error, error1, locked; 3213f2a2857bSKirk McKusick struct mount *mp; 3214fca666a1SJulian Elischer char *name; /* name of corefile */ 3215fca666a1SJulian Elischer off_t limit; 3216e7228204SAlfred Perlstein int compress; 3217fca666a1SJulian Elischer 3218e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 3219e7228204SAlfred Perlstein compress = compress_user_cores; 3220e7228204SAlfred Perlstein #else 3221e7228204SAlfred Perlstein compress = 0; 3222e7228204SAlfred Perlstein #endif 32234ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3224f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3225628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 3226fca666a1SJulian Elischer 32277e73ee85SPawel Jakub Dawidek if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0)) { 3228628d2653SJohn Baldwin PROC_UNLOCK(p); 3229fca666a1SJulian Elischer return (EFAULT); 3230628d2653SJohn Baldwin } 3231fca666a1SJulian Elischer 3232fca666a1SJulian Elischer /* 323335a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 323435a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 323535a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 323635a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 323735a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 323835a2598fSSean Eric Fagan * if it is larger than the limit. 3239fca666a1SJulian Elischer */ 324091d5354aSJohn Baldwin limit = (off_t)lim_cur(p, RLIMIT_CORE); 3241b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3242628d2653SJohn Baldwin PROC_UNLOCK(p); 324391d5354aSJohn Baldwin return (EFBIG); 324457274c51SChristian S.J. Peron } 3245b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 324635a2598fSSean Eric Fagan 3247f2a2857bSKirk McKusick restart: 3248c345faeaSPawel Jakub Dawidek error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, compress, 3249c345faeaSPawel Jakub Dawidek &vp, &name); 3250c345faeaSPawel Jakub Dawidek if (error != 0) 3251fca666a1SJulian Elischer return (error); 325206ae1e91SMatthew Dillon 3253832dafadSDon Lewis /* Don't dump to non-regular files or files with links. */ 3254f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 3255f06f465dSPawel Jakub Dawidek vattr.va_nlink != 1) { 325622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3257832dafadSDon Lewis error = EFAULT; 325833f19beeSJohn Baldwin goto close; 3259832dafadSDon Lewis } 3260832dafadSDon Lewis 326122db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 326206ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 326306ae1e91SMatthew Dillon lf.l_start = 0; 326406ae1e91SMatthew Dillon lf.l_len = 0; 326506ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 3266c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 326706ae1e91SMatthew Dillon 326806ae1e91SMatthew Dillon if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 326906ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 327036bbf86bSRobert Watson if (locked) 327106ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3272b40ce416SJulian Elischer if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 327333f19beeSJohn Baldwin goto out; 3274f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 327533f19beeSJohn Baldwin goto out; 3276f2a2857bSKirk McKusick goto restart; 3277f2a2857bSKirk McKusick } 3278fca666a1SJulian Elischer 3279fca666a1SJulian Elischer VATTR_NULL(&vattr); 3280fca666a1SJulian Elischer vattr.va_size = 0; 32816141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 32826141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 3283cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 32840359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 328522db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 328611991ab4STor Egge vn_finished_write(mp); 3287628d2653SJohn Baldwin PROC_LOCK(p); 3288fca666a1SJulian Elischer p->p_acflag |= ACORE; 3289628d2653SJohn Baldwin PROC_UNLOCK(p); 3290fca666a1SJulian Elischer 329123c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 329223c6445aSPawel Jakub Dawidek error = p->p_sysent->sv_coredump(td, vp, limit, 329323c6445aSPawel Jakub Dawidek compress ? IMGACT_CORE_COMPRESS : 0); 329423c6445aSPawel Jakub Dawidek } else { 329523c6445aSPawel Jakub Dawidek error = ENOSYS; 329623c6445aSPawel Jakub Dawidek } 3297fca666a1SJulian Elischer 3298c447f5b2SRobert Watson if (locked) { 329906ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 330006ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3301c447f5b2SRobert Watson } 330233f19beeSJohn Baldwin close: 3303b40ce416SJulian Elischer error1 = vn_close(vp, FWRITE, cred, td); 3304fca666a1SJulian Elischer if (error == 0) 3305fca666a1SJulian Elischer error = error1; 330633f19beeSJohn Baldwin out: 330757274c51SChristian S.J. Peron #ifdef AUDIT 330857274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 330957274c51SChristian S.J. Peron #endif 331057274c51SChristian S.J. Peron free(name, M_TEMP); 3311fca666a1SJulian Elischer return (error); 3312fca666a1SJulian Elischer } 3313fca666a1SJulian Elischer 3314fca666a1SJulian Elischer /* 33150c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 33160c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 3317df8bae1dSRodney W. Grimes */ 3318d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 3319df8bae1dSRodney W. Grimes struct nosys_args { 3320df8bae1dSRodney W. Grimes int dummy; 3321df8bae1dSRodney W. Grimes }; 3322d2d3e875SBruce Evans #endif 3323df8bae1dSRodney W. Grimes /* ARGSUSED */ 332426f9a767SRodney W. Grimes int 3325b40ce416SJulian Elischer nosys(td, args) 3326b40ce416SJulian Elischer struct thread *td; 3327df8bae1dSRodney W. Grimes struct nosys_args *args; 3328df8bae1dSRodney W. Grimes { 3329b40ce416SJulian Elischer struct proc *p = td->td_proc; 3330b40ce416SJulian Elischer 3331628d2653SJohn Baldwin PROC_LOCK(p); 3332888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 3333628d2653SJohn Baldwin PROC_UNLOCK(p); 3334f5216b9aSBruce Evans return (ENOSYS); 3335df8bae1dSRodney W. Grimes } 3336831d27a9SDon Lewis 3337831d27a9SDon Lewis /* 33380c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 33390c14ff0eSRobert Watson * credentials rather than those of the current process. 3340831d27a9SDon Lewis */ 3341831d27a9SDon Lewis void 3342f1320723SAlfred Perlstein pgsigio(sigiop, sig, checkctty) 3343f1320723SAlfred Perlstein struct sigio **sigiop; 33442c42a146SMarcel Moolenaar int sig, checkctty; 3345831d27a9SDon Lewis { 3346a3de221dSKonstantin Belousov ksiginfo_t ksi; 3347f1320723SAlfred Perlstein struct sigio *sigio; 3348831d27a9SDon Lewis 3349a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 3350a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 3351a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 3352a3de221dSKonstantin Belousov 3353f1320723SAlfred Perlstein SIGIO_LOCK(); 3354f1320723SAlfred Perlstein sigio = *sigiop; 3355f1320723SAlfred Perlstein if (sigio == NULL) { 3356f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3357f1320723SAlfred Perlstein return; 3358f1320723SAlfred Perlstein } 3359831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 3360628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 33612b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 33628451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 3363628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 3364831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 3365831d27a9SDon Lewis struct proc *p; 3366831d27a9SDon Lewis 3367f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 3368628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3369628d2653SJohn Baldwin PROC_LOCK(p); 3370e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 3371e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3372831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 33738451d0ddSKip Macy kern_psignal(p, sig); 3374628d2653SJohn Baldwin PROC_UNLOCK(p); 3375628d2653SJohn Baldwin } 3376f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 3377831d27a9SDon Lewis } 3378f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3379831d27a9SDon Lewis } 3380cb679c38SJonathan Lemon 3381cb679c38SJonathan Lemon static int 3382cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 3383cb679c38SJonathan Lemon { 3384cb679c38SJonathan Lemon struct proc *p = curproc; 3385cb679c38SJonathan Lemon 3386cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 3387cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 3388cb679c38SJonathan Lemon 3389ad3b9257SJohn-Mark Gurney knlist_add(&p->p_klist, kn, 0); 3390cb679c38SJonathan Lemon 3391cb679c38SJonathan Lemon return (0); 3392cb679c38SJonathan Lemon } 3393cb679c38SJonathan Lemon 3394cb679c38SJonathan Lemon static void 3395cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 3396cb679c38SJonathan Lemon { 3397cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 3398cb679c38SJonathan Lemon 3399ad3b9257SJohn-Mark Gurney knlist_remove(&p->p_klist, kn, 0); 3400cb679c38SJonathan Lemon } 3401cb679c38SJonathan Lemon 3402cb679c38SJonathan Lemon /* 3403cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 3404cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 3405cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 3406cb679c38SJonathan Lemon * isn't worth the trouble. 3407cb679c38SJonathan Lemon */ 3408cb679c38SJonathan Lemon static int 3409cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 3410cb679c38SJonathan Lemon { 3411cb679c38SJonathan Lemon 3412cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 3413cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 3414cb679c38SJonathan Lemon 3415cb679c38SJonathan Lemon if (kn->kn_id == hint) 3416cb679c38SJonathan Lemon kn->kn_data++; 3417cb679c38SJonathan Lemon } 3418cb679c38SJonathan Lemon return (kn->kn_data != 0); 3419cb679c38SJonathan Lemon } 342090af4afaSJohn Baldwin 342190af4afaSJohn Baldwin struct sigacts * 342290af4afaSJohn Baldwin sigacts_alloc(void) 342390af4afaSJohn Baldwin { 342490af4afaSJohn Baldwin struct sigacts *ps; 342590af4afaSJohn Baldwin 342690af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 342790af4afaSJohn Baldwin ps->ps_refcnt = 1; 342890af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 342990af4afaSJohn Baldwin return (ps); 343090af4afaSJohn Baldwin } 343190af4afaSJohn Baldwin 343290af4afaSJohn Baldwin void 343390af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 343490af4afaSJohn Baldwin { 343590af4afaSJohn Baldwin 343690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 343790af4afaSJohn Baldwin ps->ps_refcnt--; 343890af4afaSJohn Baldwin if (ps->ps_refcnt == 0) { 343990af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 344090af4afaSJohn Baldwin free(ps, M_SUBPROC); 344190af4afaSJohn Baldwin } else 344290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 344390af4afaSJohn Baldwin } 344490af4afaSJohn Baldwin 344590af4afaSJohn Baldwin struct sigacts * 344690af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 344790af4afaSJohn Baldwin { 344890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 344990af4afaSJohn Baldwin ps->ps_refcnt++; 345090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 345190af4afaSJohn Baldwin return (ps); 345290af4afaSJohn Baldwin } 345390af4afaSJohn Baldwin 345490af4afaSJohn Baldwin void 345590af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 345690af4afaSJohn Baldwin { 345790af4afaSJohn Baldwin 345890af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 345990af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 346090af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 346190af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 346290af4afaSJohn Baldwin } 346390af4afaSJohn Baldwin 346490af4afaSJohn Baldwin int 346590af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 346690af4afaSJohn Baldwin { 346790af4afaSJohn Baldwin int shared; 346890af4afaSJohn Baldwin 346990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 347090af4afaSJohn Baldwin shared = ps->ps_refcnt > 1; 347190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 347290af4afaSJohn Baldwin return (shared); 347390af4afaSJohn Baldwin } 3474