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. 1869a28758SEd Maste * 3. 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" 41aa14e9b7SMark Johnston #include "opt_gzio.h" 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45eb6368d4SRui Paulo #include <sys/ctype.h> 4636240ea5SDoug Rabson #include <sys/systm.h> 47df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 48df8bae1dSRodney W. Grimes #include <sys/vnode.h> 49df8bae1dSRodney W. Grimes #include <sys/acct.h> 50842ab62bSRui Paulo #include <sys/bus.h> 514a144410SRobert Watson #include <sys/capsicum.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> 62c959c237SMateusz Guzik #include <sys/refcount.h> 63854dc8c2SJohn Baldwin #include <sys/namei.h> 64854dc8c2SJohn Baldwin #include <sys/proc.h> 65cfb5f768SJonathan Anderson #include <sys/procdesc.h> 666aeb05d7STom Rhodes #include <sys/posix4.h> 67854dc8c2SJohn Baldwin #include <sys/pioctl.h> 68b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h> 69c31146a1SJohn Baldwin #include <sys/resourcevar.h> 705d217f17SJohn Birrell #include <sys/sdt.h> 7136b208e0SRobert Watson #include <sys/sbuf.h> 7244f3b092SJohn Baldwin #include <sys/sleepqueue.h> 736caa8a15SJohn Baldwin #include <sys/smp.h> 74df8bae1dSRodney W. Grimes #include <sys/stat.h> 751005a129SJohn Baldwin #include <sys/sx.h> 768f19eb88SIan Dowse #include <sys/syscallsubr.h> 77c87e2930SDavid Greenman #include <sys/sysctl.h> 78854dc8c2SJohn Baldwin #include <sys/sysent.h> 79854dc8c2SJohn Baldwin #include <sys/syslog.h> 80854dc8c2SJohn Baldwin #include <sys/sysproto.h> 8156c06c4bSDavid Xu #include <sys/timers.h> 8206ae1e91SMatthew Dillon #include <sys/unistd.h> 83854dc8c2SJohn Baldwin #include <sys/wait.h> 849104847fSDavid Xu #include <vm/vm.h> 859104847fSDavid Xu #include <vm/vm_extern.h> 869104847fSDavid Xu #include <vm/uma.h> 87df8bae1dSRodney W. Grimes 88e7228204SAlfred Perlstein #include <sys/jail.h> 89e7228204SAlfred Perlstein 90df8bae1dSRodney W. Grimes #include <machine/cpu.h> 91df8bae1dSRodney W. Grimes 92bfd7575aSWayne Salamon #include <security/audit/audit.h> 93bfd7575aSWayne Salamon 946f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 956f841fb7SMarcel Moolenaar 965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 9736160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send, 9836160958SMark Johnston "struct thread *", "struct proc *", "int"); 9936160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear, 10036160958SMark Johnston "int", "ksiginfo_t *"); 10136160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard, 1027b77e1feSMark Johnston "struct thread *", "struct proc *", "int"); 1035d217f17SJohn Birrell 1044d77a549SAlfred Perlstein static int coredump(struct thread *); 105a3de221dSKonstantin Belousov static int killpg1(struct thread *td, int sig, int pgid, int all, 106a3de221dSKonstantin Belousov ksiginfo_t *ksi); 1073cf3b9f0SJohn Baldwin static int issignal(struct thread *td); 1084d77a549SAlfred Perlstein static int sigprop(int sig); 10994f0972bSDavid Xu static void tdsigwakeup(struct thread *, int, sig_t, int); 1106f56cb8dSKonstantin Belousov static int sig_suspend_threads(struct thread *, struct proc *, int); 111cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 112cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 113cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 1144093529dSJeff Roberson static struct thread *sigtd(struct proc *p, int sig, int prop); 1159104847fSDavid Xu static void sigqueue_start(void); 116cb679c38SJonathan Lemon 1179104847fSDavid Xu static uma_zone_t ksiginfo_zone = NULL; 118e76d823bSRobert Watson struct filterops sig_filtops = { 119e76d823bSRobert Watson .f_isfd = 0, 120e76d823bSRobert Watson .f_attach = filt_sigattach, 121e76d823bSRobert Watson .f_detach = filt_sigdetach, 122e76d823bSRobert Watson .f_event = filt_signal, 123e76d823bSRobert Watson }; 124cb679c38SJonathan Lemon 125fc8cca02SJohn Baldwin static int kern_logsigexit = 1; 1263d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1273d177f46SBill Fumerola &kern_logsigexit, 0, 1283d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 12957308494SJoerg Wunsch 130f71a882fSDavid Xu static int kern_forcesigexit = 1; 131f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 132f71a882fSDavid Xu &kern_forcesigexit, 0, "Force trap signal to be handled"); 133f71a882fSDavid Xu 1346472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, 1356472ac3dSEd Schouten "POSIX real time signal"); 1369104847fSDavid Xu 1379104847fSDavid Xu static int max_pending_per_proc = 128; 1389104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 1399104847fSDavid Xu &max_pending_per_proc, 0, "Max pending signals per proc"); 1409104847fSDavid Xu 1419104847fSDavid Xu static int preallocate_siginfo = 1024; 142af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN, 1439104847fSDavid Xu &preallocate_siginfo, 0, "Preallocated signal memory size"); 1449104847fSDavid Xu 1459104847fSDavid Xu static int signal_overflow = 0; 146761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 1479104847fSDavid Xu &signal_overflow, 0, "Number of signals overflew"); 1489104847fSDavid Xu 1499104847fSDavid Xu static int signal_alloc_fail = 0; 150761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 1519104847fSDavid Xu &signal_alloc_fail, 0, "signals failed to be allocated"); 1529104847fSDavid Xu 1539104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 1549104847fSDavid Xu 1552b87b6d4SRobert Watson /* 1562b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1572b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1582b87b6d4SRobert Watson * in the right situations. 1592b87b6d4SRobert Watson */ 1602b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1612b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1622b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1632b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1642b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1652b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1662b87b6d4SRobert Watson 167fc8cca02SJohn Baldwin static int sugid_coredump; 168af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN, 1698b5adf9dSJoseph Koshy &sugid_coredump, 0, "Allow setuid and setgid processes to dump core"); 170c87e2930SDavid Greenman 171b0c9d4d7SPawel Jakub Dawidek static int capmode_coredump; 172af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN, 173b0c9d4d7SPawel Jakub Dawidek &capmode_coredump, 0, "Allow processes in capability mode to dump core"); 174b0c9d4d7SPawel Jakub Dawidek 175e5a28db9SPaul Saab static int do_coredump = 1; 176e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 177e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 178e5a28db9SPaul Saab 1796141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 1806141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 1816141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 1826141e04aSJohn-Mark Gurney 1830da9e11bSRui Paulo static int coredump_devctl = 0; 184eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl, 185eb6368d4SRui Paulo 0, "Generate a devctl notification when processes coredump"); 186eb6368d4SRui Paulo 1872c42a146SMarcel Moolenaar /* 1882c42a146SMarcel Moolenaar * Signal properties and actions. 1892c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1902c42a146SMarcel Moolenaar * according to the following properties: 1912c42a146SMarcel Moolenaar */ 192fd50a707SBrooks Davis #define SIGPROP_KILL 0x01 /* terminates process by default */ 193fd50a707SBrooks Davis #define SIGPROP_CORE 0x02 /* ditto and coredumps */ 194fd50a707SBrooks Davis #define SIGPROP_STOP 0x04 /* suspend process */ 195fd50a707SBrooks Davis #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */ 196fd50a707SBrooks Davis #define SIGPROP_IGNORE 0x10 /* ignore by default */ 197fd50a707SBrooks Davis #define SIGPROP_CONT 0x20 /* continue if suspended */ 198fd50a707SBrooks Davis #define SIGPROP_CANTMASK 0x40 /* non-maskable, catchable */ 199df8bae1dSRodney W. Grimes 2002c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 201ed6d876bSBrooks Davis [SIGHUP] = SIGPROP_KILL, 202ed6d876bSBrooks Davis [SIGINT] = SIGPROP_KILL, 203ed6d876bSBrooks Davis [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE, 204ed6d876bSBrooks Davis [SIGILL] = SIGPROP_KILL | SIGPROP_CORE, 205ed6d876bSBrooks Davis [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE, 206ed6d876bSBrooks Davis [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE, 207ed6d876bSBrooks Davis [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE, 208ed6d876bSBrooks Davis [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE, 209ed6d876bSBrooks Davis [SIGKILL] = SIGPROP_KILL, 210ed6d876bSBrooks Davis [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE, 211ed6d876bSBrooks Davis [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE, 212ed6d876bSBrooks Davis [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE, 213ed6d876bSBrooks Davis [SIGPIPE] = SIGPROP_KILL, 214ed6d876bSBrooks Davis [SIGALRM] = SIGPROP_KILL, 215ed6d876bSBrooks Davis [SIGTERM] = SIGPROP_KILL, 216ed6d876bSBrooks Davis [SIGURG] = SIGPROP_IGNORE, 217ed6d876bSBrooks Davis [SIGSTOP] = SIGPROP_STOP, 218ed6d876bSBrooks Davis [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP, 219ed6d876bSBrooks Davis [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT, 220ed6d876bSBrooks Davis [SIGCHLD] = SIGPROP_IGNORE, 221ed6d876bSBrooks Davis [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP, 222ed6d876bSBrooks Davis [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP, 223ed6d876bSBrooks Davis [SIGIO] = SIGPROP_IGNORE, 224ed6d876bSBrooks Davis [SIGXCPU] = SIGPROP_KILL, 225ed6d876bSBrooks Davis [SIGXFSZ] = SIGPROP_KILL, 226ed6d876bSBrooks Davis [SIGVTALRM] = SIGPROP_KILL, 227ed6d876bSBrooks Davis [SIGPROF] = SIGPROP_KILL, 228ed6d876bSBrooks Davis [SIGWINCH] = SIGPROP_IGNORE, 229ed6d876bSBrooks Davis [SIGINFO] = SIGPROP_IGNORE, 230ed6d876bSBrooks Davis [SIGUSR1] = SIGPROP_KILL, 231ed6d876bSBrooks Davis [SIGUSR2] = SIGPROP_KILL, 2322c42a146SMarcel Moolenaar }; 2332c42a146SMarcel Moolenaar 23480a8b0f3SKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags); 2356b286ee8SKonstantin Belousov 2369104847fSDavid Xu static void 2379104847fSDavid Xu sigqueue_start(void) 2389104847fSDavid Xu { 2399104847fSDavid Xu ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 2409104847fSDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2419104847fSDavid Xu uma_prealloc(ksiginfo_zone, preallocate_siginfo); 242b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 243b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 244b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 2459104847fSDavid Xu } 2469104847fSDavid Xu 2475da49fcbSDavid Xu ksiginfo_t * 248ebceaf6dSDavid Xu ksiginfo_alloc(int wait) 2499104847fSDavid Xu { 250ebceaf6dSDavid Xu int flags; 251ebceaf6dSDavid Xu 252ebceaf6dSDavid Xu flags = M_ZERO; 253ebceaf6dSDavid Xu if (! wait) 254ebceaf6dSDavid Xu flags |= M_NOWAIT; 2559104847fSDavid Xu if (ksiginfo_zone != NULL) 256ebceaf6dSDavid Xu return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 2579104847fSDavid Xu return (NULL); 2589104847fSDavid Xu } 2599104847fSDavid Xu 2605da49fcbSDavid Xu void 2619104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 2629104847fSDavid Xu { 2639104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2649104847fSDavid Xu } 2659104847fSDavid Xu 2665da49fcbSDavid Xu static __inline int 2675da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 2685da49fcbSDavid Xu { 2695da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT)) { 2705da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2715da49fcbSDavid Xu return (1); 2725da49fcbSDavid Xu } 2735da49fcbSDavid Xu return (0); 2745da49fcbSDavid Xu } 2755da49fcbSDavid Xu 2769104847fSDavid Xu void 2779104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 2789104847fSDavid Xu { 2799104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 2803dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 28182a4538fSEric Badger SIGEMPTYSET(list->sq_ptrace); 2829104847fSDavid Xu TAILQ_INIT(&list->sq_list); 2839104847fSDavid Xu list->sq_proc = p; 2849104847fSDavid Xu list->sq_flags = SQ_INIT; 2859104847fSDavid Xu } 2869104847fSDavid Xu 2879104847fSDavid Xu /* 2889104847fSDavid Xu * Get a signal's ksiginfo. 2899104847fSDavid Xu * Return: 2909104847fSDavid Xu * 0 - signal not found 2919104847fSDavid Xu * others - signal number 2929104847fSDavid Xu */ 293a5799a4fSKonstantin Belousov static int 2949104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 2959104847fSDavid Xu { 2969104847fSDavid Xu struct proc *p = sq->sq_proc; 2979104847fSDavid Xu struct ksiginfo *ksi, *next; 2989104847fSDavid Xu int count = 0; 2999104847fSDavid Xu 3009104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3019104847fSDavid Xu 3029104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 3039104847fSDavid Xu return (0); 3049104847fSDavid Xu 30582a4538fSEric Badger if (SIGISMEMBER(sq->sq_ptrace, signo)) { 30682a4538fSEric Badger count++; 30782a4538fSEric Badger SIGDELSET(sq->sq_ptrace, signo); 30882a4538fSEric Badger si->ksi_flags |= KSI_PTRACE; 30982a4538fSEric Badger } 3103dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 3113dfcaad6SDavid Xu count++; 31282a4538fSEric Badger if (count == 1) 3133dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 3143dfcaad6SDavid Xu } 3153dfcaad6SDavid Xu 3165c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 3179104847fSDavid Xu if (ksi->ksi_signo == signo) { 3189104847fSDavid Xu if (count == 0) { 3199104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3205da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3219104847fSDavid Xu ksiginfo_copy(ksi, si); 3225da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 3239104847fSDavid Xu p->p_pendingcnt--; 3249104847fSDavid Xu } 325016fa302SDavid Xu if (++count > 1) 326016fa302SDavid Xu break; 3279104847fSDavid Xu } 3289104847fSDavid Xu } 3299104847fSDavid Xu 3309104847fSDavid Xu if (count <= 1) 3319104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 3329104847fSDavid Xu si->ksi_signo = signo; 3339104847fSDavid Xu return (signo); 3349104847fSDavid Xu } 3359104847fSDavid Xu 3365da49fcbSDavid Xu void 3375da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 3385da49fcbSDavid Xu { 3395da49fcbSDavid Xu struct ksiginfo *kp; 3405da49fcbSDavid Xu struct proc *p; 3415da49fcbSDavid Xu sigqueue_t *sq; 3425da49fcbSDavid Xu 343ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 3445da49fcbSDavid Xu return; 3455da49fcbSDavid Xu 3465da49fcbSDavid Xu p = sq->sq_proc; 3475da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3485da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3495da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 3505da49fcbSDavid Xu p->p_pendingcnt--; 3515da49fcbSDavid Xu 3525da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 3535da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 3545da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 3555da49fcbSDavid Xu break; 3565da49fcbSDavid Xu } 35782a4538fSEric Badger if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 35882a4538fSEric Badger !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 3595da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 3605da49fcbSDavid Xu } 3615da49fcbSDavid Xu 362a5799a4fSKonstantin Belousov static int 3639104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 3649104847fSDavid Xu { 3659104847fSDavid Xu struct proc *p = sq->sq_proc; 3669104847fSDavid Xu struct ksiginfo *ksi; 3679104847fSDavid Xu int ret = 0; 3689104847fSDavid Xu 3699104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3709104847fSDavid Xu 37182a4538fSEric Badger /* 37282a4538fSEric Badger * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 37382a4538fSEric Badger * for these signals. 37482a4538fSEric Badger */ 3753dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 3763dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3779104847fSDavid Xu goto out_set_bit; 3783dfcaad6SDavid Xu } 3799104847fSDavid Xu 3805da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 3815da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 3826a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 3836a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 3846a671a6bSKonstantin Belousov else 3855da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 3865da49fcbSDavid Xu si->ksi_sigq = sq; 3875da49fcbSDavid Xu goto out_set_bit; 3885da49fcbSDavid Xu } 3895da49fcbSDavid Xu 3903dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 3913dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3929104847fSDavid Xu goto out_set_bit; 3933dfcaad6SDavid Xu } 3949104847fSDavid Xu 395ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 3969104847fSDavid Xu signal_overflow++; 3979104847fSDavid Xu ret = EAGAIN; 398ebceaf6dSDavid Xu } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 3999104847fSDavid Xu signal_alloc_fail++; 4009104847fSDavid Xu ret = EAGAIN; 4019104847fSDavid Xu } else { 4029104847fSDavid Xu if (p != NULL) 4039104847fSDavid Xu p->p_pendingcnt++; 4049104847fSDavid Xu ksiginfo_copy(si, ksi); 4059104847fSDavid Xu ksi->ksi_signo = signo; 4066a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4076a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 4086a671a6bSKonstantin Belousov else 4099104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 4105da49fcbSDavid Xu ksi->ksi_sigq = sq; 4119104847fSDavid Xu } 4129104847fSDavid Xu 41382a4538fSEric Badger if (ret != 0) { 41482a4538fSEric Badger if ((si->ksi_flags & KSI_PTRACE) != 0) { 41582a4538fSEric Badger SIGADDSET(sq->sq_ptrace, signo); 41682a4538fSEric Badger ret = 0; 41782a4538fSEric Badger goto out_set_bit; 41882a4538fSEric Badger } else if ((si->ksi_flags & KSI_TRAP) != 0 || 419a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 4203dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4219104847fSDavid Xu ret = 0; 4229104847fSDavid Xu goto out_set_bit; 4239104847fSDavid Xu } 4249104847fSDavid Xu return (ret); 42582a4538fSEric Badger } 4269104847fSDavid Xu 4279104847fSDavid Xu out_set_bit: 4289104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 4299104847fSDavid Xu return (ret); 4309104847fSDavid Xu } 4319104847fSDavid Xu 4329104847fSDavid Xu void 4339104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 4349104847fSDavid Xu { 4359104847fSDavid Xu struct proc *p = sq->sq_proc; 4369104847fSDavid Xu ksiginfo_t *ksi; 4379104847fSDavid Xu 4389104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4399104847fSDavid Xu 4405da49fcbSDavid Xu if (p != NULL) 4415da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 4425da49fcbSDavid Xu 4439104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 4449104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4455da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4465da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4479104847fSDavid Xu p->p_pendingcnt--; 4489104847fSDavid Xu } 4499104847fSDavid Xu 4509104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 4513dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 45282a4538fSEric Badger SIGEMPTYSET(sq->sq_ptrace); 4539104847fSDavid Xu } 4549104847fSDavid Xu 455a5799a4fSKonstantin Belousov static void 456fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 4579104847fSDavid Xu { 458fc4ecc1dSDavid Xu sigset_t tmp; 4599104847fSDavid Xu struct proc *p1, *p2; 4609104847fSDavid Xu ksiginfo_t *ksi, *next; 4619104847fSDavid Xu 4629104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 4639104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 4649104847fSDavid Xu p1 = src->sq_proc; 4659104847fSDavid Xu p2 = dst->sq_proc; 4669104847fSDavid Xu /* Move siginfo to target list */ 4675c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 468fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 4699104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 4709104847fSDavid Xu if (p1 != NULL) 4719104847fSDavid Xu p1->p_pendingcnt--; 4729104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 4735da49fcbSDavid Xu ksi->ksi_sigq = dst; 4749104847fSDavid Xu if (p2 != NULL) 4759104847fSDavid Xu p2->p_pendingcnt++; 4769104847fSDavid Xu } 4779104847fSDavid Xu } 4789104847fSDavid Xu 4799104847fSDavid Xu /* Move pending bits to target list */ 4803dfcaad6SDavid Xu tmp = src->sq_kill; 481fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4823dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 4833dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 4843dfcaad6SDavid Xu 48582a4538fSEric Badger tmp = src->sq_ptrace; 48682a4538fSEric Badger SIGSETAND(tmp, *set); 48782a4538fSEric Badger SIGSETOR(dst->sq_ptrace, tmp); 48882a4538fSEric Badger SIGSETNAND(src->sq_ptrace, tmp); 48982a4538fSEric Badger 4909104847fSDavid Xu tmp = src->sq_signals; 491fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4929104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 4939104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 4949104847fSDavid Xu } 4959104847fSDavid Xu 496407af02bSDavid Xu #if 0 497a5799a4fSKonstantin Belousov static void 4989104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 4999104847fSDavid Xu { 5009104847fSDavid Xu sigset_t set; 5019104847fSDavid Xu 5029104847fSDavid Xu SIGEMPTYSET(set); 5039104847fSDavid Xu SIGADDSET(set, signo); 5049104847fSDavid Xu sigqueue_move_set(src, dst, &set); 5059104847fSDavid Xu } 506407af02bSDavid Xu #endif 5079104847fSDavid Xu 508a5799a4fSKonstantin Belousov static void 509fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 5109104847fSDavid Xu { 5119104847fSDavid Xu struct proc *p = sq->sq_proc; 5129104847fSDavid Xu ksiginfo_t *ksi, *next; 5139104847fSDavid Xu 5149104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5159104847fSDavid Xu 5169104847fSDavid Xu /* Remove siginfo queue */ 5175c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 5189104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5199104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5205da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5215da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5229104847fSDavid Xu p->p_pendingcnt--; 5239104847fSDavid Xu } 5249104847fSDavid Xu } 5253dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 52682a4538fSEric Badger SIGSETNAND(sq->sq_ptrace, *set); 5279104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 5289104847fSDavid Xu } 5299104847fSDavid Xu 5309104847fSDavid Xu void 5319104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 5329104847fSDavid Xu { 5339104847fSDavid Xu sigset_t set; 5349104847fSDavid Xu 5359104847fSDavid Xu SIGEMPTYSET(set); 5369104847fSDavid Xu SIGADDSET(set, signo); 5379104847fSDavid Xu sigqueue_delete_set(sq, &set); 5389104847fSDavid Xu } 5399104847fSDavid Xu 5409104847fSDavid Xu /* Remove a set of signals for a process */ 541a5799a4fSKonstantin Belousov static void 542fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 5439104847fSDavid Xu { 5449104847fSDavid Xu sigqueue_t worklist; 5459104847fSDavid Xu struct thread *td0; 5469104847fSDavid Xu 5479104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5489104847fSDavid Xu 5499104847fSDavid Xu sigqueue_init(&worklist, NULL); 5509104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 5519104847fSDavid Xu 5529104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 5539104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 5549104847fSDavid Xu 5559104847fSDavid Xu sigqueue_flush(&worklist); 5569104847fSDavid Xu } 5579104847fSDavid Xu 5589104847fSDavid Xu void 5599104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 5609104847fSDavid Xu { 5619104847fSDavid Xu sigset_t set; 5629104847fSDavid Xu 5639104847fSDavid Xu SIGEMPTYSET(set); 5649104847fSDavid Xu SIGADDSET(set, signo); 5659104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5669104847fSDavid Xu } 5679104847fSDavid Xu 568a5799a4fSKonstantin Belousov static void 5699104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 5709104847fSDavid Xu { 5719104847fSDavid Xu sigset_t set; 5729104847fSDavid Xu 5739104847fSDavid Xu SIGEMPTYSET(set); 5749104847fSDavid Xu SIGADDSET(set, SIGSTOP); 5759104847fSDavid Xu SIGADDSET(set, SIGTSTP); 5769104847fSDavid Xu SIGADDSET(set, SIGTTIN); 5779104847fSDavid Xu SIGADDSET(set, SIGTTOU); 5789104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5799104847fSDavid Xu } 5809104847fSDavid Xu 581fbbeeb6cSBruce Evans /* 5821968f37bSJohn Baldwin * Determine signal that should be delivered to thread td, the current 5831968f37bSJohn Baldwin * thread, 0 if none. If there is a pending stop signal with default 584fbbeeb6cSBruce Evans * action, the process stops in issignal(). 585fbbeeb6cSBruce Evans */ 586fbbeeb6cSBruce Evans int 5873cf3b9f0SJohn Baldwin cursig(struct thread *td) 588fbbeeb6cSBruce Evans { 589c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 59090af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 591a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 5923cf3b9f0SJohn Baldwin return (SIGPENDING(td) ? issignal(td) : 0); 593fbbeeb6cSBruce Evans } 594fbbeeb6cSBruce Evans 59579065dbaSBruce Evans /* 59679065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 5979104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 5984093529dSJeff Roberson * unmasked in td_sigmask. 59979065dbaSBruce Evans */ 60079065dbaSBruce Evans void 6014093529dSJeff Roberson signotify(struct thread *td) 60279065dbaSBruce Evans { 6034093529dSJeff Roberson struct proc *p; 6044093529dSJeff Roberson 6054093529dSJeff Roberson p = td->td_proc; 60679065dbaSBruce Evans 60779065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 6084093529dSJeff Roberson 6098b94a061SJohn Baldwin if (SIGPENDING(td)) { 610a54e85fdSJeff Roberson thread_lock(td); 6114093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 612a54e85fdSJeff Roberson thread_unlock(td); 61379065dbaSBruce Evans } 6148b94a061SJohn Baldwin } 6158b94a061SJohn Baldwin 6168b94a061SJohn Baldwin int 6178b94a061SJohn Baldwin sigonstack(size_t sp) 6188b94a061SJohn Baldwin { 619a30ec4b9SDavid Xu struct thread *td = curthread; 6208b94a061SJohn Baldwin 621a30ec4b9SDavid Xu return ((td->td_pflags & TDP_ALTSTACK) ? 6221930e303SPoul-Henning Kamp #if defined(COMPAT_43) 623a30ec4b9SDavid Xu ((td->td_sigstk.ss_size == 0) ? 624a30ec4b9SDavid Xu (td->td_sigstk.ss_flags & SS_ONSTACK) : 625a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 6268b94a061SJohn Baldwin #else 627a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 6288b94a061SJohn Baldwin #endif 6298b94a061SJohn Baldwin : 0); 6308b94a061SJohn Baldwin } 63179065dbaSBruce Evans 6326f841fb7SMarcel Moolenaar static __inline int 6336f841fb7SMarcel Moolenaar sigprop(int sig) 6342c42a146SMarcel Moolenaar { 6356f841fb7SMarcel Moolenaar 636ed6d876bSBrooks Davis if (sig > 0 && sig < nitems(sigproptbl)) 637ed6d876bSBrooks Davis return (sigproptbl[sig]); 6382c42a146SMarcel Moolenaar return (0); 639df8bae1dSRodney W. Grimes } 6402c42a146SMarcel Moolenaar 6414093529dSJeff Roberson int 6426f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 6432c42a146SMarcel Moolenaar { 6442c42a146SMarcel Moolenaar int i; 6452c42a146SMarcel Moolenaar 6466f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 6472c42a146SMarcel Moolenaar if (set->__bits[i]) 6482c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 649df8bae1dSRodney W. Grimes return (0); 650df8bae1dSRodney W. Grimes } 651df8bae1dSRodney W. Grimes 652350ae563SKonstantin Belousov static bool 653ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag) 654350ae563SKonstantin Belousov { 655350ae563SKonstantin Belousov 656c83655f3SKonstantin Belousov /* 657c83655f3SKonstantin Belousov * SA_SIGINFO is reset when signal disposition is set to 658c83655f3SKonstantin Belousov * ignore or default. Other flags are kept according to user 659c83655f3SKonstantin Belousov * settings. 660c83655f3SKonstantin Belousov */ 661c83655f3SKonstantin Belousov return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 662c83655f3SKonstantin Belousov ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 663c83655f3SKonstantin Belousov (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 664350ae563SKonstantin Belousov } 665350ae563SKonstantin Belousov 6662c42a146SMarcel Moolenaar /* 6678f19eb88SIan Dowse * kern_sigaction 6682c42a146SMarcel Moolenaar * sigaction 66923eeeff7SPeter Wemm * freebsd4_sigaction 6702c42a146SMarcel Moolenaar * osigaction 6712c42a146SMarcel Moolenaar */ 6728f19eb88SIan Dowse int 673ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 674ea566832SEd Schouten struct sigaction *oact, int flags) 675df8bae1dSRodney W. Grimes { 67690af4afaSJohn Baldwin struct sigacts *ps; 6778f19eb88SIan Dowse struct proc *p = td->td_proc; 678df8bae1dSRodney W. Grimes 6792899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 6802c42a146SMarcel Moolenaar return (EINVAL); 681271ab240SKonstantin Belousov if (act != NULL && act->sa_handler != SIG_DFL && 682271ab240SKonstantin Belousov act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 683271ab240SKonstantin Belousov SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 684271ab240SKonstantin Belousov SA_NOCLDWAIT | SA_SIGINFO)) != 0) 6852d864174SKonstantin Belousov return (EINVAL); 6862c42a146SMarcel Moolenaar 687628d2653SJohn Baldwin PROC_LOCK(p); 688628d2653SJohn Baldwin ps = p->p_sigacts; 68990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 6902c42a146SMarcel Moolenaar if (oact) { 6912c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 6922c42a146SMarcel Moolenaar oact->sa_flags = 0; 6932c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 6942c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 6952c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 6962c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 6972c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 6982c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 6992c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 7002c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 70110c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 7022c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 70310c2b8e1SDavid E. O'Brien oact->sa_sigaction = 70410c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 70510c2b8e1SDavid E. O'Brien } else 70610c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 70790af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 7082c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 70990af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 7102c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 7112c42a146SMarcel Moolenaar } 7122c42a146SMarcel Moolenaar if (act) { 7132c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 714628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 71590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 716628d2653SJohn Baldwin PROC_UNLOCK(p); 7172c42a146SMarcel Moolenaar return (EINVAL); 718628d2653SJohn Baldwin } 7192c42a146SMarcel Moolenaar 720df8bae1dSRodney W. Grimes /* 721df8bae1dSRodney W. Grimes * Change setting atomically. 722df8bae1dSRodney W. Grimes */ 7232c42a146SMarcel Moolenaar 7242c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 7252c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 726350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_SIGINFO)) { 727aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 728aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 72980f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 73080f42b55SIan Dowse } else { 73180f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 7322c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 7332c42a146SMarcel Moolenaar } 734350ae563SKonstantin Belousov if (!sigact_flag_test(act, SA_RESTART)) 7352c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 736df8bae1dSRodney W. Grimes else 7372c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 738350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_ONSTACK)) 7392c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 740df8bae1dSRodney W. Grimes else 7412c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 742350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_RESETHAND)) 7432c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 7441e41c1b5SSteven Wallace else 7452c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 746350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_NODEFER)) 7472c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 748289ccde0SPeter Wemm else 7492c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 7502c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 7512c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 75290af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 7536626c604SJulian Elischer else 75490af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 755ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 756245f17d4SJoerg Wunsch /* 7572c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 7582c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 7592c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 7602c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 761245f17d4SJoerg Wunsch */ 762245f17d4SJoerg Wunsch if (p->p_pid == 1) 76390af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 7646626c604SJulian Elischer else 76590af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 766245f17d4SJoerg Wunsch } else 76790af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 768ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 76990af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 770ba1551caSIan Dowse else 77190af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 772df8bae1dSRodney W. Grimes } 773df8bae1dSRodney W. Grimes /* 77490af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 7752c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 77690af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 7772c42a146SMarcel Moolenaar * have to restart the process. 778df8bae1dSRodney W. Grimes */ 7792c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 780fd50a707SBrooks Davis (sigprop(sig) & SIGPROP_IGNORE && 7812c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 7822c42a146SMarcel Moolenaar /* never to be seen again */ 7839104847fSDavid Xu sigqueue_delete_proc(p, sig); 7842c42a146SMarcel Moolenaar if (sig != SIGCONT) 7852c42a146SMarcel Moolenaar /* easier in psignal */ 78690af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 78790af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 788645682fdSLuoqi Chen } else { 78990af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 7902c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 79190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 7922c42a146SMarcel Moolenaar else 79390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 7942c42a146SMarcel Moolenaar } 79523eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 79623eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 79723eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 79823eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 79923eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 80023eeeff7SPeter Wemm else 80123eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 80223eeeff7SPeter Wemm #endif 803e8ebc08fSPeter Wemm #ifdef COMPAT_43 804645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 80523eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 80623eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 807645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 808645682fdSLuoqi Chen else 809645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 810e8ebc08fSPeter Wemm #endif 811df8bae1dSRodney W. Grimes } 81290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 813628d2653SJohn Baldwin PROC_UNLOCK(p); 8142c42a146SMarcel Moolenaar return (0); 8152c42a146SMarcel Moolenaar } 8162c42a146SMarcel Moolenaar 8172c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8182c42a146SMarcel Moolenaar struct sigaction_args { 8192c42a146SMarcel Moolenaar int sig; 8202c42a146SMarcel Moolenaar struct sigaction *act; 8212c42a146SMarcel Moolenaar struct sigaction *oact; 8222c42a146SMarcel Moolenaar }; 8232c42a146SMarcel Moolenaar #endif 8242c42a146SMarcel Moolenaar int 825e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap) 8262c42a146SMarcel Moolenaar { 8272c42a146SMarcel Moolenaar struct sigaction act, oact; 828e052a8b9SEd Maste struct sigaction *actp, *oactp; 8292c42a146SMarcel Moolenaar int error; 8302c42a146SMarcel Moolenaar 8316f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 8326f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 8332c42a146SMarcel Moolenaar if (actp) { 8346f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 8352c42a146SMarcel Moolenaar if (error) 83644443757STim J. Robbins return (error); 8372c42a146SMarcel Moolenaar } 8388f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 83925d6dc06SJohn Baldwin if (oactp && !error) 8406f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 8412c42a146SMarcel Moolenaar return (error); 8422c42a146SMarcel Moolenaar } 8432c42a146SMarcel Moolenaar 84423eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 84523eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 84623eeeff7SPeter Wemm struct freebsd4_sigaction_args { 84723eeeff7SPeter Wemm int sig; 84823eeeff7SPeter Wemm struct sigaction *act; 84923eeeff7SPeter Wemm struct sigaction *oact; 85023eeeff7SPeter Wemm }; 85123eeeff7SPeter Wemm #endif 85223eeeff7SPeter Wemm int 853e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 85423eeeff7SPeter Wemm { 85523eeeff7SPeter Wemm struct sigaction act, oact; 856e052a8b9SEd Maste struct sigaction *actp, *oactp; 85723eeeff7SPeter Wemm int error; 85823eeeff7SPeter Wemm 85923eeeff7SPeter Wemm 86023eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 86123eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 86223eeeff7SPeter Wemm if (actp) { 86323eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 86423eeeff7SPeter Wemm if (error) 86544443757STim J. Robbins return (error); 86623eeeff7SPeter Wemm } 86723eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 868a14e1189SJohn Baldwin if (oactp && !error) 86923eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 87023eeeff7SPeter Wemm return (error); 87123eeeff7SPeter Wemm } 87223eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 87323eeeff7SPeter Wemm 87431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8752c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8762c42a146SMarcel Moolenaar struct osigaction_args { 8772c42a146SMarcel Moolenaar int signum; 8782c42a146SMarcel Moolenaar struct osigaction *nsa; 8792c42a146SMarcel Moolenaar struct osigaction *osa; 8802c42a146SMarcel Moolenaar }; 8812c42a146SMarcel Moolenaar #endif 8822c42a146SMarcel Moolenaar int 883e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap) 8842c42a146SMarcel Moolenaar { 8852c42a146SMarcel Moolenaar struct osigaction sa; 8862c42a146SMarcel Moolenaar struct sigaction nsa, osa; 887e052a8b9SEd Maste struct sigaction *nsap, *osap; 8882c42a146SMarcel Moolenaar int error; 8892c42a146SMarcel Moolenaar 8906f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 8916f841fb7SMarcel Moolenaar return (EINVAL); 892fb99ab88SMatthew Dillon 8936f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 8946f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 895fb99ab88SMatthew Dillon 8962c42a146SMarcel Moolenaar if (nsap) { 8976f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 8982c42a146SMarcel Moolenaar if (error) 89944443757STim J. Robbins return (error); 9002c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 9012c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 9022c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 9032c42a146SMarcel Moolenaar } 90423eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 9052c42a146SMarcel Moolenaar if (osap && !error) { 9062c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 9072c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 9082c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 9096f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 9102c42a146SMarcel Moolenaar } 9112c42a146SMarcel Moolenaar return (error); 9122c42a146SMarcel Moolenaar } 91323eeeff7SPeter Wemm 91473dbd3daSJohn Baldwin #if !defined(__i386__) 91523eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 91623eeeff7SPeter Wemm int 917e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap) 91823eeeff7SPeter Wemm { 91923eeeff7SPeter Wemm 92023eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 92123eeeff7SPeter Wemm } 92223eeeff7SPeter Wemm #endif 92331c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 924df8bae1dSRodney W. Grimes 925df8bae1dSRodney W. Grimes /* 926df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 927df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 928df8bae1dSRodney W. Grimes */ 929df8bae1dSRodney W. Grimes void 930e052a8b9SEd Maste siginit(struct proc *p) 931df8bae1dSRodney W. Grimes { 932df8bae1dSRodney W. Grimes register int i; 93390af4afaSJohn Baldwin struct sigacts *ps; 934df8bae1dSRodney W. Grimes 935628d2653SJohn Baldwin PROC_LOCK(p); 93690af4afaSJohn Baldwin ps = p->p_sigacts; 93790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 938350ae563SKonstantin Belousov for (i = 1; i <= NSIG; i++) { 939fd50a707SBrooks Davis if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 94090af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 941350ae563SKonstantin Belousov } 942350ae563SKonstantin Belousov } 94390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 944628d2653SJohn Baldwin PROC_UNLOCK(p); 945df8bae1dSRodney W. Grimes } 946df8bae1dSRodney W. Grimes 947df8bae1dSRodney W. Grimes /* 948350ae563SKonstantin Belousov * Reset specified signal to the default disposition. 949350ae563SKonstantin Belousov */ 950350ae563SKonstantin Belousov static void 951350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig) 952350ae563SKonstantin Belousov { 953350ae563SKonstantin Belousov 954350ae563SKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 955350ae563SKonstantin Belousov SIGDELSET(ps->ps_sigcatch, sig); 956fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 957350ae563SKonstantin Belousov SIGADDSET(ps->ps_sigignore, sig); 958350ae563SKonstantin Belousov ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 959350ae563SKonstantin Belousov SIGDELSET(ps->ps_siginfo, sig); 960350ae563SKonstantin Belousov } 961350ae563SKonstantin Belousov 962350ae563SKonstantin Belousov /* 963df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 964df8bae1dSRodney W. Grimes */ 965df8bae1dSRodney W. Grimes void 966a30ec4b9SDavid Xu execsigs(struct proc *p) 967df8bae1dSRodney W. Grimes { 968f3fe76ecSEd Schouten sigset_t osigignore; 969a30ec4b9SDavid Xu struct sigacts *ps; 970a30ec4b9SDavid Xu int sig; 971a30ec4b9SDavid Xu struct thread *td; 972df8bae1dSRodney W. Grimes 973df8bae1dSRodney W. Grimes /* 974df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 9754093529dSJeff Roberson * through td_sigmask (unless they were caught, 976df8bae1dSRodney W. Grimes * and are now ignored by default). 977df8bae1dSRodney W. Grimes */ 9789b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 979628d2653SJohn Baldwin ps = p->p_sigacts; 98090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 98190af4afaSJohn Baldwin while (SIGNOTEMPTY(ps->ps_sigcatch)) { 98290af4afaSJohn Baldwin sig = sig_ffs(&ps->ps_sigcatch); 983350ae563SKonstantin Belousov sigdflt(ps, sig); 984fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 9859104847fSDavid Xu sigqueue_delete_proc(p, sig); 986df8bae1dSRodney W. Grimes } 987f3fe76ecSEd Schouten 988f3fe76ecSEd Schouten /* 989f3fe76ecSEd Schouten * As CloudABI processes cannot modify signal handlers, fully 990f3fe76ecSEd Schouten * reset all signals to their default behavior. Do ignore 991f3fe76ecSEd Schouten * SIGPIPE, as it would otherwise be impossible to recover from 992f3fe76ecSEd Schouten * writes to broken pipes and sockets. 993f3fe76ecSEd Schouten */ 994f3fe76ecSEd Schouten if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) { 995f3fe76ecSEd Schouten osigignore = ps->ps_sigignore; 996f3fe76ecSEd Schouten while (SIGNOTEMPTY(osigignore)) { 997f3fe76ecSEd Schouten sig = sig_ffs(&osigignore); 998f3fe76ecSEd Schouten SIGDELSET(osigignore, sig); 999f3fe76ecSEd Schouten if (sig != SIGPIPE) 1000f3fe76ecSEd Schouten sigdflt(ps, sig); 1001f3fe76ecSEd Schouten } 1002f3fe76ecSEd Schouten SIGADDSET(ps->ps_sigignore, SIGPIPE); 1003f3fe76ecSEd Schouten } 1004f3fe76ecSEd Schouten 1005df8bae1dSRodney W. Grimes /* 1006df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 1007df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 1008df8bae1dSRodney W. Grimes */ 1009469ec1ebSKonstantin Belousov td = curthread; 1010469ec1ebSKonstantin Belousov MPASS(td->td_proc == p); 1011a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 1012a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1013a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 1014a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 101580e907a1SPeter Wemm /* 101680e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 101780e907a1SPeter Wemm */ 101890af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1019c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1020c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 102190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1022df8bae1dSRodney W. Grimes } 1023df8bae1dSRodney W. Grimes 1024df8bae1dSRodney W. Grimes /* 1025e77daab1SJohn Baldwin * kern_sigprocmask() 10267c8fdcbdSMatthew Dillon * 1027628d2653SJohn Baldwin * Manipulate signal mask. 1028df8bae1dSRodney W. Grimes */ 1029e77daab1SJohn Baldwin int 10306b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 103184440afbSKonstantin Belousov int flags) 10322c42a146SMarcel Moolenaar { 10336b286ee8SKonstantin Belousov sigset_t new_block, oset1; 10346b286ee8SKonstantin Belousov struct proc *p; 10352c42a146SMarcel Moolenaar int error; 10362c42a146SMarcel Moolenaar 10376b286ee8SKonstantin Belousov p = td->td_proc; 103870778bbaSKonstantin Belousov if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 103970778bbaSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 104070778bbaSKonstantin Belousov else 10416b286ee8SKonstantin Belousov PROC_LOCK(p); 104270778bbaSKonstantin Belousov mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 104370778bbaSKonstantin Belousov ? MA_OWNED : MA_NOTOWNED); 10442c42a146SMarcel Moolenaar if (oset != NULL) 10454093529dSJeff Roberson *oset = td->td_sigmask; 10462c42a146SMarcel Moolenaar 10472c42a146SMarcel Moolenaar error = 0; 10482c42a146SMarcel Moolenaar if (set != NULL) { 10492c42a146SMarcel Moolenaar switch (how) { 10502c42a146SMarcel Moolenaar case SIG_BLOCK: 1051645682fdSLuoqi Chen SIG_CANTMASK(*set); 10526b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 10534093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 10546b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10556b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10562c42a146SMarcel Moolenaar break; 10572c42a146SMarcel Moolenaar case SIG_UNBLOCK: 10584093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 10594093529dSJeff Roberson signotify(td); 1060407af02bSDavid Xu goto out; 10612c42a146SMarcel Moolenaar case SIG_SETMASK: 1062645682fdSLuoqi Chen SIG_CANTMASK(*set); 10636b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 106484440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 10654093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1066645682fdSLuoqi Chen else 10674093529dSJeff Roberson td->td_sigmask = *set; 10686b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10696b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10704093529dSJeff Roberson signotify(td); 10712c42a146SMarcel Moolenaar break; 10722c42a146SMarcel Moolenaar default: 10732c42a146SMarcel Moolenaar error = EINVAL; 1074407af02bSDavid Xu goto out; 10752c42a146SMarcel Moolenaar } 10766b286ee8SKonstantin Belousov 10776b286ee8SKonstantin Belousov /* 10787df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 10796b286ee8SKonstantin Belousov * blocked, but are blocked now. 10806b286ee8SKonstantin Belousov * 10816b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 10826b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1083407af02bSDavid Xu * signal delivery to some thread that does not block the 1084407af02bSDavid Xu * signal, possibly waking it up. 10856b286ee8SKonstantin Belousov */ 10866b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 108780a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1088407af02bSDavid Xu } 10896b286ee8SKonstantin Belousov 1090407af02bSDavid Xu out: 109184440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 10926b286ee8SKonstantin Belousov PROC_UNLOCK(p); 10932c42a146SMarcel Moolenaar return (error); 10942c42a146SMarcel Moolenaar } 10952c42a146SMarcel Moolenaar 1096d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1097df8bae1dSRodney W. Grimes struct sigprocmask_args { 1098df8bae1dSRodney W. Grimes int how; 10992c42a146SMarcel Moolenaar const sigset_t *set; 11002c42a146SMarcel Moolenaar sigset_t *oset; 1101df8bae1dSRodney W. Grimes }; 1102d2d3e875SBruce Evans #endif 110326f9a767SRodney W. Grimes int 1104e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1105df8bae1dSRodney W. Grimes { 11062c42a146SMarcel Moolenaar sigset_t set, oset; 11072c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 11082c42a146SMarcel Moolenaar int error; 1109df8bae1dSRodney W. Grimes 11106f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 11116f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 11122c42a146SMarcel Moolenaar if (setp) { 11136f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 11142c42a146SMarcel Moolenaar if (error) 11152c42a146SMarcel Moolenaar return (error); 1116df8bae1dSRodney W. Grimes } 1117e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 11182c42a146SMarcel Moolenaar if (osetp && !error) { 11196f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 11202c42a146SMarcel Moolenaar } 11212c42a146SMarcel Moolenaar return (error); 11222c42a146SMarcel Moolenaar } 11232c42a146SMarcel Moolenaar 112431c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11252c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11262c42a146SMarcel Moolenaar struct osigprocmask_args { 11272c42a146SMarcel Moolenaar int how; 11282c42a146SMarcel Moolenaar osigset_t mask; 11292c42a146SMarcel Moolenaar }; 11302c42a146SMarcel Moolenaar #endif 11312c42a146SMarcel Moolenaar int 1132e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap) 11332c42a146SMarcel Moolenaar { 11342c42a146SMarcel Moolenaar sigset_t set, oset; 11352c42a146SMarcel Moolenaar int error; 11362c42a146SMarcel Moolenaar 11372c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1138e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1139b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1140df8bae1dSRodney W. Grimes return (error); 1141df8bae1dSRodney W. Grimes } 114231c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1143df8bae1dSRodney W. Grimes 114426f9a767SRodney W. Grimes int 11458451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1146a447cd8bSJeff Roberson { 11479104847fSDavid Xu ksiginfo_t ksi; 1148a447cd8bSJeff Roberson sigset_t set; 1149a447cd8bSJeff Roberson int error; 1150a447cd8bSJeff Roberson 1151a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 115236939a0aSDavid Xu if (error) { 115336939a0aSDavid Xu td->td_retval[0] = error; 115436939a0aSDavid Xu return (0); 115536939a0aSDavid Xu } 1156a447cd8bSJeff Roberson 11579104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 115836939a0aSDavid Xu if (error) { 11598e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 11608e9a54eeSKonstantin Belousov error = ERESTART; 116136939a0aSDavid Xu if (error == ERESTART) 1162a447cd8bSJeff Roberson return (error); 116336939a0aSDavid Xu td->td_retval[0] = error; 116436939a0aSDavid Xu return (0); 116536939a0aSDavid Xu } 1166a447cd8bSJeff Roberson 11679104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 116836939a0aSDavid Xu td->td_retval[0] = error; 116936939a0aSDavid Xu return (0); 1170a447cd8bSJeff Roberson } 11710c14ff0eSRobert Watson 1172a447cd8bSJeff Roberson int 11738451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1174a447cd8bSJeff Roberson { 1175a447cd8bSJeff Roberson struct timespec ts; 1176a447cd8bSJeff Roberson struct timespec *timeout; 1177a447cd8bSJeff Roberson sigset_t set; 11789104847fSDavid Xu ksiginfo_t ksi; 1179a447cd8bSJeff Roberson int error; 1180a447cd8bSJeff Roberson 1181a447cd8bSJeff Roberson if (uap->timeout) { 1182a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1183a447cd8bSJeff Roberson if (error) 1184a447cd8bSJeff Roberson return (error); 1185a447cd8bSJeff Roberson 1186a447cd8bSJeff Roberson timeout = &ts; 1187a447cd8bSJeff Roberson } else 1188a447cd8bSJeff Roberson timeout = NULL; 1189a447cd8bSJeff Roberson 1190a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1191a447cd8bSJeff Roberson if (error) 1192a447cd8bSJeff Roberson return (error); 1193a447cd8bSJeff Roberson 11949104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1195a447cd8bSJeff Roberson if (error) 1196a447cd8bSJeff Roberson return (error); 11979dde3bc9SDavid Xu 1198418228dfSDavid Xu if (uap->info) 11999104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12009104847fSDavid Xu 12019104847fSDavid Xu if (error == 0) 12029104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1203a447cd8bSJeff Roberson return (error); 1204a447cd8bSJeff Roberson } 1205a447cd8bSJeff Roberson 1206a447cd8bSJeff Roberson int 12078451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1208a447cd8bSJeff Roberson { 12099104847fSDavid Xu ksiginfo_t ksi; 1210a447cd8bSJeff Roberson sigset_t set; 1211a447cd8bSJeff Roberson int error; 1212a447cd8bSJeff Roberson 1213a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1214a447cd8bSJeff Roberson if (error) 1215a447cd8bSJeff Roberson return (error); 1216a447cd8bSJeff Roberson 12179104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1218a447cd8bSJeff Roberson if (error) 1219a447cd8bSJeff Roberson return (error); 1220a447cd8bSJeff Roberson 1221418228dfSDavid Xu if (uap->info) 12229104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12239104847fSDavid Xu 12249104847fSDavid Xu if (error == 0) 12259104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1226a447cd8bSJeff Roberson return (error); 1227a447cd8bSJeff Roberson } 1228a447cd8bSJeff Roberson 1229*86be94fcSTycho Nightingale static void 1230*86be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 1231*86be94fcSTycho Nightingale { 1232*86be94fcSTycho Nightingale struct thread *thr; 1233*86be94fcSTycho Nightingale 1234*86be94fcSTycho Nightingale FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 1235*86be94fcSTycho Nightingale if (thr == td) 1236*86be94fcSTycho Nightingale thr->td_si = *si; 1237*86be94fcSTycho Nightingale else 1238*86be94fcSTycho Nightingale thr->td_si.si_signo = 0; 1239*86be94fcSTycho Nightingale } 1240*86be94fcSTycho Nightingale } 1241*86be94fcSTycho Nightingale 1242c6511aeaSDavid Xu int 12439104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1244a447cd8bSJeff Roberson struct timespec *timeout) 1245a447cd8bSJeff Roberson { 12463074d1b4SDavid Xu struct sigacts *ps; 1247407af02bSDavid Xu sigset_t saved_mask, new_block; 1248a447cd8bSJeff Roberson struct proc *p; 1249407af02bSDavid Xu int error, sig, timo, timevalid = 0; 12501089f031SDavid Xu struct timespec rts, ets, ts; 12511089f031SDavid Xu struct timeval tv; 1252a447cd8bSJeff Roberson 1253a447cd8bSJeff Roberson p = td->td_proc; 1254a447cd8bSJeff Roberson error = 0; 12559b73d239SMatt Jacob ets.tv_sec = 0; 12569b73d239SMatt Jacob ets.tv_nsec = 0; 1257a447cd8bSJeff Roberson 1258407af02bSDavid Xu if (timeout != NULL) { 12591089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 12601089f031SDavid Xu timevalid = 1; 12611089f031SDavid Xu getnanouptime(&rts); 12621089f031SDavid Xu ets = rts; 12631089f031SDavid Xu timespecadd(&ets, timeout); 12641089f031SDavid Xu } 12651089f031SDavid Xu } 1266407af02bSDavid Xu ksiginfo_init(ksi); 1267407af02bSDavid Xu /* Some signals can not be waited for. */ 1268407af02bSDavid Xu SIG_CANTMASK(waitset); 1269407af02bSDavid Xu ps = p->p_sigacts; 1270407af02bSDavid Xu PROC_LOCK(p); 1271407af02bSDavid Xu saved_mask = td->td_sigmask; 1272407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1273407af02bSDavid Xu for (;;) { 12743074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 12753cf3b9f0SJohn Baldwin sig = cursig(td); 12763074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 127746e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 1278407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1279407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1280407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1281407af02bSDavid Xu error = 0; 1282407af02bSDavid Xu break; 12833074d1b4SDavid Xu } 12847e0221a2SDavid Xu } 12857e0221a2SDavid Xu 1286407af02bSDavid Xu if (error != 0) 1287407af02bSDavid Xu break; 12883074d1b4SDavid Xu 1289a447cd8bSJeff Roberson /* 1290a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1291a447cd8bSJeff Roberson * signals. 1292a447cd8bSJeff Roberson */ 1293407af02bSDavid Xu if (timeout != NULL) { 12941089f031SDavid Xu if (!timevalid) { 1295a447cd8bSJeff Roberson error = EINVAL; 1296407af02bSDavid Xu break; 1297a447cd8bSJeff Roberson } 12981089f031SDavid Xu getnanouptime(&rts); 12991089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 13003074d1b4SDavid Xu error = EAGAIN; 1301407af02bSDavid Xu break; 13023074d1b4SDavid Xu } 13031089f031SDavid Xu ts = ets; 13041089f031SDavid Xu timespecsub(&ts, &rts); 13051089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1306407af02bSDavid Xu timo = tvtohz(&tv); 1307407af02bSDavid Xu } else { 1308407af02bSDavid Xu timo = 0; 1309407af02bSDavid Xu } 1310a447cd8bSJeff Roberson 1311407af02bSDavid Xu error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo); 1312407af02bSDavid Xu 1313407af02bSDavid Xu if (timeout != NULL) { 13141089f031SDavid Xu if (error == ERESTART) { 1315407af02bSDavid Xu /* Timeout can not be restarted. */ 13163074d1b4SDavid Xu error = EINTR; 13171089f031SDavid Xu } else if (error == EAGAIN) { 1318407af02bSDavid Xu /* We will calculate timeout by ourself. */ 13191089f031SDavid Xu error = 0; 13201089f031SDavid Xu } 13211089f031SDavid Xu } 1322407af02bSDavid Xu } 13239dde3bc9SDavid Xu 1324407af02bSDavid Xu new_block = saved_mask; 1325407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1326407af02bSDavid Xu td->td_sigmask = saved_mask; 1327407af02bSDavid Xu /* 1328407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1329407af02bSDavid Xu * notification. 1330407af02bSDavid Xu */ 1331407af02bSDavid Xu if (p->p_numthreads != 1) 1332407af02bSDavid Xu reschedule_signals(p, new_block, 0); 13335d217f17SJohn Birrell 1334407af02bSDavid Xu if (error == 0) { 133536160958SMark Johnston SDT_PROBE2(proc, , , signal__clear, sig, ksi); 13365d217f17SJohn Birrell 133756c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 133856c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 13397e0221a2SDavid Xu 13407e0221a2SDavid Xu #ifdef KTRACE 13417e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 13427e0221a2SDavid Xu sig_t action; 13437e0221a2SDavid Xu 13443074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1345a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 134690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 134761009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 13487e0221a2SDavid Xu } 1349a447cd8bSJeff Roberson #endif 1350*86be94fcSTycho Nightingale if (sig == SIGKILL) { 1351*86be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi->ksi_info); 13527e0221a2SDavid Xu sigexit(td, sig); 13533074d1b4SDavid Xu } 1354*86be94fcSTycho Nightingale } 1355a447cd8bSJeff Roberson PROC_UNLOCK(p); 1356a447cd8bSJeff Roberson return (error); 1357a447cd8bSJeff Roberson } 1358a447cd8bSJeff Roberson 13599104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 13609104847fSDavid Xu struct sigpending_args { 13619104847fSDavid Xu sigset_t *set; 13629104847fSDavid Xu }; 13639104847fSDavid Xu #endif 1364a447cd8bSJeff Roberson int 1365e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap) 1366df8bae1dSRodney W. Grimes { 1367b40ce416SJulian Elischer struct proc *p = td->td_proc; 13689104847fSDavid Xu sigset_t pending; 1369df8bae1dSRodney W. Grimes 1370628d2653SJohn Baldwin PROC_LOCK(p); 13719104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13729104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1373628d2653SJohn Baldwin PROC_UNLOCK(p); 13749104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 13752c42a146SMarcel Moolenaar } 13762c42a146SMarcel Moolenaar 137731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 13782c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 13792c42a146SMarcel Moolenaar struct osigpending_args { 13802c42a146SMarcel Moolenaar int dummy; 13812c42a146SMarcel Moolenaar }; 13822c42a146SMarcel Moolenaar #endif 13832c42a146SMarcel Moolenaar int 1384e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap) 13852c42a146SMarcel Moolenaar { 1386b40ce416SJulian Elischer struct proc *p = td->td_proc; 13879104847fSDavid Xu sigset_t pending; 1388b40ce416SJulian Elischer 1389628d2653SJohn Baldwin PROC_LOCK(p); 13909104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13919104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1392628d2653SJohn Baldwin PROC_UNLOCK(p); 13939104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1394df8bae1dSRodney W. Grimes return (0); 1395df8bae1dSRodney W. Grimes } 139631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1397df8bae1dSRodney W. Grimes 13981930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1399df8bae1dSRodney W. Grimes /* 1400df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1401df8bae1dSRodney W. Grimes */ 1402d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1403df8bae1dSRodney W. Grimes struct osigvec_args { 1404df8bae1dSRodney W. Grimes int signum; 1405df8bae1dSRodney W. Grimes struct sigvec *nsv; 1406df8bae1dSRodney W. Grimes struct sigvec *osv; 1407df8bae1dSRodney W. Grimes }; 1408d2d3e875SBruce Evans #endif 1409df8bae1dSRodney W. Grimes /* ARGSUSED */ 141026f9a767SRodney W. Grimes int 1411e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap) 1412df8bae1dSRodney W. Grimes { 1413df8bae1dSRodney W. Grimes struct sigvec vec; 14142c42a146SMarcel Moolenaar struct sigaction nsa, osa; 1415e052a8b9SEd Maste struct sigaction *nsap, *osap; 14162c42a146SMarcel Moolenaar int error; 1417df8bae1dSRodney W. Grimes 14186f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 14196f841fb7SMarcel Moolenaar return (EINVAL); 14206f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 14216f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 14222c42a146SMarcel Moolenaar if (nsap) { 14236f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 14242c42a146SMarcel Moolenaar if (error) 1425df8bae1dSRodney W. Grimes return (error); 14262c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 14272c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 14282c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 14292c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1430df8bae1dSRodney W. Grimes } 14315edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 14322c42a146SMarcel Moolenaar if (osap && !error) { 14332c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 14342c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 14352c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 14362c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 14372c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 14386f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 14392c42a146SMarcel Moolenaar } 14402c42a146SMarcel Moolenaar return (error); 1441df8bae1dSRodney W. Grimes } 1442df8bae1dSRodney W. Grimes 1443d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1444df8bae1dSRodney W. Grimes struct osigblock_args { 1445df8bae1dSRodney W. Grimes int mask; 1446df8bae1dSRodney W. Grimes }; 1447d2d3e875SBruce Evans #endif 144826f9a767SRodney W. Grimes int 1449e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap) 1450df8bae1dSRodney W. Grimes { 1451d6e029adSKonstantin Belousov sigset_t set, oset; 1452df8bae1dSRodney W. Grimes 14532c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1454d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1455d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1456df8bae1dSRodney W. Grimes return (0); 1457df8bae1dSRodney W. Grimes } 1458df8bae1dSRodney W. Grimes 1459d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1460df8bae1dSRodney W. Grimes struct osigsetmask_args { 1461df8bae1dSRodney W. Grimes int mask; 1462df8bae1dSRodney W. Grimes }; 1463d2d3e875SBruce Evans #endif 146426f9a767SRodney W. Grimes int 1465e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1466df8bae1dSRodney W. Grimes { 1467d6e029adSKonstantin Belousov sigset_t set, oset; 1468df8bae1dSRodney W. Grimes 14692c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1470d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1471d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1472df8bae1dSRodney W. Grimes return (0); 1473df8bae1dSRodney W. Grimes } 14741930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1475df8bae1dSRodney W. Grimes 1476df8bae1dSRodney W. Grimes /* 1477873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1478873fbcd7SRobert Watson * meantime. 1479df8bae1dSRodney W. Grimes */ 1480d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1481df8bae1dSRodney W. Grimes struct sigsuspend_args { 14822c42a146SMarcel Moolenaar const sigset_t *sigmask; 1483df8bae1dSRodney W. Grimes }; 1484d2d3e875SBruce Evans #endif 1485df8bae1dSRodney W. Grimes /* ARGSUSED */ 148626f9a767SRodney W. Grimes int 1487e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1488df8bae1dSRodney W. Grimes { 14892c42a146SMarcel Moolenaar sigset_t mask; 14902c42a146SMarcel Moolenaar int error; 14912c42a146SMarcel Moolenaar 14926f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 14932c42a146SMarcel Moolenaar if (error) 14942c42a146SMarcel Moolenaar return (error); 14958f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 14968f19eb88SIan Dowse } 14978f19eb88SIan Dowse 14988f19eb88SIan Dowse int 14998f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 15008f19eb88SIan Dowse { 15018f19eb88SIan Dowse struct proc *p = td->td_proc; 150284440afbSKonstantin Belousov int has_sig, sig; 1503df8bae1dSRodney W. Grimes 1504df8bae1dSRodney W. Grimes /* 1505645682fdSLuoqi Chen * When returning from sigsuspend, we want 1506df8bae1dSRodney W. Grimes * the old mask to be restored after the 1507df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1508df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1509df8bae1dSRodney W. Grimes * to indicate this. 1510df8bae1dSRodney W. Grimes */ 1511628d2653SJohn Baldwin PROC_LOCK(p); 151284440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 151384440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 15145e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 151584440afbSKonstantin Belousov 151684440afbSKonstantin Belousov /* 151784440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 151884440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 151984440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 152084440afbSKonstantin Belousov * delivery. 152184440afbSKonstantin Belousov */ 1522afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 152384440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 152484440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 152584440afbSKonstantin Belousov 0) == 0) 15262c42a146SMarcel Moolenaar /* void */; 152784440afbSKonstantin Belousov thread_suspend_check(0); 152884440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 152946e47c4fSKonstantin Belousov while ((sig = cursig(td)) != 0) { 153046e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 153175c586a4SKonstantin Belousov has_sig += postsig(sig); 153246e47c4fSKonstantin Belousov } 153384440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 153484440afbSKonstantin Belousov } 1535628d2653SJohn Baldwin PROC_UNLOCK(p); 15362dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 15372dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 153875c586a4SKonstantin Belousov return (EJUSTRETURN); 15392c42a146SMarcel Moolenaar } 15402c42a146SMarcel Moolenaar 154131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 154297563428SAlexander Kabaev /* 154397563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 154497563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 154597563428SAlexander Kabaev */ 15462c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 15472c42a146SMarcel Moolenaar struct osigsuspend_args { 15482c42a146SMarcel Moolenaar osigset_t mask; 15492c42a146SMarcel Moolenaar }; 15502c42a146SMarcel Moolenaar #endif 15512c42a146SMarcel Moolenaar /* ARGSUSED */ 15522c42a146SMarcel Moolenaar int 1553e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap) 15542c42a146SMarcel Moolenaar { 1555645682fdSLuoqi Chen sigset_t mask; 15562c42a146SMarcel Moolenaar 1557645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 155884440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1559df8bae1dSRodney W. Grimes } 156031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1561df8bae1dSRodney W. Grimes 15621930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1563d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1564df8bae1dSRodney W. Grimes struct osigstack_args { 1565df8bae1dSRodney W. Grimes struct sigstack *nss; 1566df8bae1dSRodney W. Grimes struct sigstack *oss; 1567df8bae1dSRodney W. Grimes }; 1568d2d3e875SBruce Evans #endif 1569df8bae1dSRodney W. Grimes /* ARGSUSED */ 157026f9a767SRodney W. Grimes int 1571e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap) 1572df8bae1dSRodney W. Grimes { 15735afe0c99SJohn Baldwin struct sigstack nss, oss; 1574fb99ab88SMatthew Dillon int error = 0; 1575fb99ab88SMatthew Dillon 1576d034d459SMarcel Moolenaar if (uap->nss != NULL) { 15775afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 15785afe0c99SJohn Baldwin if (error) 15795afe0c99SJohn Baldwin return (error); 1580df8bae1dSRodney W. Grimes } 1581a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 15825afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 15835afe0c99SJohn Baldwin if (uap->nss != NULL) { 1584a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1585a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1586a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1587a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 15885afe0c99SJohn Baldwin } 15895afe0c99SJohn Baldwin if (uap->oss != NULL) 15905afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 15915afe0c99SJohn Baldwin 1592fb99ab88SMatthew Dillon return (error); 1593df8bae1dSRodney W. Grimes } 15941930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1595df8bae1dSRodney W. Grimes 1596d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1597df8bae1dSRodney W. Grimes struct sigaltstack_args { 15982c42a146SMarcel Moolenaar stack_t *ss; 15992c42a146SMarcel Moolenaar stack_t *oss; 1600df8bae1dSRodney W. Grimes }; 1601d2d3e875SBruce Evans #endif 1602df8bae1dSRodney W. Grimes /* ARGSUSED */ 160326f9a767SRodney W. Grimes int 1604e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1605df8bae1dSRodney W. Grimes { 16068f19eb88SIan Dowse stack_t ss, oss; 16078f19eb88SIan Dowse int error; 16088f19eb88SIan Dowse 16098f19eb88SIan Dowse if (uap->ss != NULL) { 16108f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 16118f19eb88SIan Dowse if (error) 16128f19eb88SIan Dowse return (error); 16138f19eb88SIan Dowse } 16148f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 16158f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 16168f19eb88SIan Dowse if (error) 16178f19eb88SIan Dowse return (error); 16188f19eb88SIan Dowse if (uap->oss != NULL) 16198f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 16208f19eb88SIan Dowse return (error); 16218f19eb88SIan Dowse } 16228f19eb88SIan Dowse 16238f19eb88SIan Dowse int 16248f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 16258f19eb88SIan Dowse { 1626b40ce416SJulian Elischer struct proc *p = td->td_proc; 1627fb99ab88SMatthew Dillon int oonstack; 1628fb99ab88SMatthew Dillon 1629b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1630d034d459SMarcel Moolenaar 16318f19eb88SIan Dowse if (oss != NULL) { 1632a30ec4b9SDavid Xu *oss = td->td_sigstk; 1633a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1634d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1635df8bae1dSRodney W. Grimes } 1636d034d459SMarcel Moolenaar 16378f19eb88SIan Dowse if (ss != NULL) { 1638a30ec4b9SDavid Xu if (oonstack) 1639cf60731bSJohn Baldwin return (EPERM); 1640a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1641cf60731bSJohn Baldwin return (EINVAL); 16428f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 16439104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1644cf60731bSJohn Baldwin return (ENOMEM); 16459104847fSDavid Xu 1646a30ec4b9SDavid Xu td->td_sigstk = *ss; 1647a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1648628d2653SJohn Baldwin } else { 1649a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1650628d2653SJohn Baldwin } 1651d034d459SMarcel Moolenaar } 1652cf60731bSJohn Baldwin return (0); 1653df8bae1dSRodney W. Grimes } 1654df8bae1dSRodney W. Grimes 1655d93f860cSPoul-Henning Kamp /* 1656d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1657d93f860cSPoul-Henning Kamp * cp is calling process. 1658d93f860cSPoul-Henning Kamp */ 165937c84183SPoul-Henning Kamp static int 1660a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1661d93f860cSPoul-Henning Kamp { 1662a3de221dSKonstantin Belousov struct proc *p; 1663d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 16643d74f47bSEitan Adler int err; 16653d74f47bSEitan Adler int ret; 1666d93f860cSPoul-Henning Kamp 16673d74f47bSEitan Adler ret = ESRCH; 1668553629ebSJake Burkholder if (all) { 1669d93f860cSPoul-Henning Kamp /* 1670d93f860cSPoul-Henning Kamp * broadcast 1671d93f860cSPoul-Henning Kamp */ 16721005a129SJohn Baldwin sx_slock(&allproc_lock); 16734f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1674628d2653SJohn Baldwin PROC_LOCK(p); 16759c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 167695f16c1eSPaul Saab p == td->td_proc || p->p_state == PRS_NEW) { 1677628d2653SJohn Baldwin PROC_UNLOCK(p); 1678628d2653SJohn Baldwin continue; 1679628d2653SJohn Baldwin } 16803d74f47bSEitan Adler err = p_cansignal(td, p, sig); 16813d74f47bSEitan Adler if (err == 0) { 168233a9ed9dSJohn Baldwin if (sig) 1683a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 16843d74f47bSEitan Adler ret = err; 1685628d2653SJohn Baldwin } 16863d74f47bSEitan Adler else if (ret == ESRCH) 16873d74f47bSEitan Adler ret = err; 168833a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1689d93f860cSPoul-Henning Kamp } 16901005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1691553629ebSJake Burkholder } else { 1692ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1693f591779bSSeigo Tanimura if (pgid == 0) { 1694d93f860cSPoul-Henning Kamp /* 1695d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1696d93f860cSPoul-Henning Kamp */ 16979c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1698f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1699f591779bSSeigo Tanimura } else { 1700d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1701f591779bSSeigo Tanimura if (pgrp == NULL) { 1702ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1703d93f860cSPoul-Henning Kamp return (ESRCH); 1704d93f860cSPoul-Henning Kamp } 1705f591779bSSeigo Tanimura } 1706ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 17072e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1708628d2653SJohn Baldwin PROC_LOCK(p); 170995f16c1eSPaul Saab if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 171095f16c1eSPaul Saab p->p_state == PRS_NEW) { 1711628d2653SJohn Baldwin PROC_UNLOCK(p); 1712628d2653SJohn Baldwin continue; 1713628d2653SJohn Baldwin } 17143d74f47bSEitan Adler err = p_cansignal(td, p, sig); 17153d74f47bSEitan Adler if (err == 0) { 171633a9ed9dSJohn Baldwin if (sig) 1717a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 17183d74f47bSEitan Adler ret = err; 1719628d2653SJohn Baldwin } 17203d74f47bSEitan Adler else if (ret == ESRCH) 17213d74f47bSEitan Adler ret = err; 172233a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1723d93f860cSPoul-Henning Kamp } 1724f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1725d93f860cSPoul-Henning Kamp } 17263d74f47bSEitan Adler return (ret); 1727d93f860cSPoul-Henning Kamp } 1728d93f860cSPoul-Henning Kamp 1729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1730df8bae1dSRodney W. Grimes struct kill_args { 1731df8bae1dSRodney W. Grimes int pid; 1732df8bae1dSRodney W. Grimes int signum; 1733df8bae1dSRodney W. Grimes }; 1734d2d3e875SBruce Evans #endif 1735df8bae1dSRodney W. Grimes /* ARGSUSED */ 173626f9a767SRodney W. Grimes int 17378451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1738df8bae1dSRodney W. Grimes { 1739a3de221dSKonstantin Belousov ksiginfo_t ksi; 1740a3de221dSKonstantin Belousov struct proc *p; 174190af4afaSJohn Baldwin int error; 1742df8bae1dSRodney W. Grimes 17438890f5d0SPawel Jakub Dawidek /* 17448890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 17458890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 17468890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 17478890f5d0SPawel Jakub Dawidek */ 17488890f5d0SPawel Jakub Dawidek if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid) 17498890f5d0SPawel Jakub Dawidek return (ECAPMODE); 17508890f5d0SPawel Jakub Dawidek 175114961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 175214961ba7SRobert Watson AUDIT_ARG_PID(uap->pid); 17536c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1754df8bae1dSRodney W. Grimes return (EINVAL); 1755fb99ab88SMatthew Dillon 1756a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1757a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1758a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1759a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1760a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1761a3de221dSKonstantin Belousov 1762df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1763df8bae1dSRodney W. Grimes /* kill single process */ 17640b011ea3SPawel Jakub Dawidek if ((p = pfind(uap->pid)) == NULL) { 176524b2151fSPawel Jakub Dawidek if ((p = zpfind(uap->pid)) == NULL) 176690af4afaSJohn Baldwin return (ESRCH); 17670b011ea3SPawel Jakub Dawidek } 176814961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 17691a88a252SMaxim Sobolev error = p_cansignal(td, p, uap->signum); 177090af4afaSJohn Baldwin if (error == 0 && uap->signum) 1771a3de221dSKonstantin Belousov pksignal(p, uap->signum, &ksi); 1772628d2653SJohn Baldwin PROC_UNLOCK(p); 177390af4afaSJohn Baldwin return (error); 1774df8bae1dSRodney W. Grimes } 1775df8bae1dSRodney W. Grimes switch (uap->pid) { 1776df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 1777a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 1, &ksi)); 1778df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 1779a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 0, &ksi)); 1780df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 1781a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, -uap->pid, 0, &ksi)); 1782df8bae1dSRodney W. Grimes } 178390af4afaSJohn Baldwin /* NOTREACHED */ 1784df8bae1dSRodney W. Grimes } 1785df8bae1dSRodney W. Grimes 1786cfb5f768SJonathan Anderson int 1787e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap) 1788cfb5f768SJonathan Anderson { 1789cfb5f768SJonathan Anderson struct proc *p; 17907008be5bSPawel Jakub Dawidek cap_rights_t rights; 1791cfb5f768SJonathan Anderson int error; 1792cfb5f768SJonathan Anderson 1793cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1794cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1795cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1796cfb5f768SJonathan Anderson return (EINVAL); 1797cfb5f768SJonathan Anderson 17987008be5bSPawel Jakub Dawidek error = procdesc_find(td, uap->fd, 17997008be5bSPawel Jakub Dawidek cap_rights_init(&rights, CAP_PDKILL), &p); 1800cfb5f768SJonathan Anderson if (error) 1801cfb5f768SJonathan Anderson return (error); 1802cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1803cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1804cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 18058451d0ddSKip Macy kern_psignal(p, uap->signum); 1806cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1807cfb5f768SJonathan Anderson return (error); 1808cfb5f768SJonathan Anderson } 1809cfb5f768SJonathan Anderson 18101930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1811d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1812df8bae1dSRodney W. Grimes struct okillpg_args { 1813df8bae1dSRodney W. Grimes int pgid; 1814df8bae1dSRodney W. Grimes int signum; 1815df8bae1dSRodney W. Grimes }; 1816d2d3e875SBruce Evans #endif 1817df8bae1dSRodney W. Grimes /* ARGSUSED */ 181826f9a767SRodney W. Grimes int 1819a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1820df8bae1dSRodney W. Grimes { 1821a3de221dSKonstantin Belousov ksiginfo_t ksi; 1822df8bae1dSRodney W. Grimes 182314961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 182414961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 18256c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1826df8bae1dSRodney W. Grimes return (EINVAL); 18279104847fSDavid Xu 1828a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1829a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1830a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1831a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1832a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1833a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1834df8bae1dSRodney W. Grimes } 18351930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1836df8bae1dSRodney W. Grimes 18379104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 18389104847fSDavid Xu struct sigqueue_args { 18399104847fSDavid Xu pid_t pid; 18409104847fSDavid Xu int signum; 18419104847fSDavid Xu /* union sigval */ void *value; 18429104847fSDavid Xu }; 18439104847fSDavid Xu #endif 18449104847fSDavid Xu int 18458451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 18469104847fSDavid Xu { 18479104847fSDavid Xu ksiginfo_t ksi; 18489104847fSDavid Xu struct proc *p; 18499104847fSDavid Xu int error; 18509104847fSDavid Xu 18519104847fSDavid Xu if ((u_int)uap->signum > _SIG_MAXSIG) 18529104847fSDavid Xu return (EINVAL); 18539104847fSDavid Xu 18549104847fSDavid Xu /* 18559104847fSDavid Xu * Specification says sigqueue can only send signal to 18569104847fSDavid Xu * single process. 18579104847fSDavid Xu */ 18589104847fSDavid Xu if (uap->pid <= 0) 18599104847fSDavid Xu return (EINVAL); 18609104847fSDavid Xu 18619104847fSDavid Xu if ((p = pfind(uap->pid)) == NULL) { 18629104847fSDavid Xu if ((p = zpfind(uap->pid)) == NULL) 18639104847fSDavid Xu return (ESRCH); 18649104847fSDavid Xu } 18659104847fSDavid Xu error = p_cansignal(td, p, uap->signum); 18669104847fSDavid Xu if (error == 0 && uap->signum != 0) { 18679104847fSDavid Xu ksiginfo_init(&ksi); 1868a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 18699104847fSDavid Xu ksi.ksi_signo = uap->signum; 18709104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 18719104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 18729104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 18738f0371f1SDavid Xu ksi.ksi_value.sival_ptr = uap->value; 1874ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 18759104847fSDavid Xu } 18769104847fSDavid Xu PROC_UNLOCK(p); 18779104847fSDavid Xu return (error); 18789104847fSDavid Xu } 18799104847fSDavid Xu 1880df8bae1dSRodney W. Grimes /* 1881df8bae1dSRodney W. Grimes * Send a signal to a process group. 1882df8bae1dSRodney W. Grimes */ 1883df8bae1dSRodney W. Grimes void 1884a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 1885df8bae1dSRodney W. Grimes { 1886df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1887df8bae1dSRodney W. Grimes 1888f591779bSSeigo Tanimura if (pgid != 0) { 1889ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1890f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1891ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1892f591779bSSeigo Tanimura if (pgrp != NULL) { 1893a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 1894f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1895f591779bSSeigo Tanimura } 1896f591779bSSeigo Tanimura } 1897df8bae1dSRodney W. Grimes } 1898df8bae1dSRodney W. Grimes 1899df8bae1dSRodney W. Grimes /* 1900df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1901df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1902df8bae1dSRodney W. Grimes */ 1903df8bae1dSRodney W. Grimes void 1904a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1905df8bae1dSRodney W. Grimes { 1906a3de221dSKonstantin Belousov struct proc *p; 1907df8bae1dSRodney W. Grimes 1908628d2653SJohn Baldwin if (pgrp) { 1909f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1910628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1911628d2653SJohn Baldwin PROC_LOCK(p); 1912e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 1913e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 1914a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 1915628d2653SJohn Baldwin PROC_UNLOCK(p); 1916628d2653SJohn Baldwin } 1917628d2653SJohn Baldwin } 1918df8bae1dSRodney W. Grimes } 1919df8bae1dSRodney W. Grimes 1920e442f29fSKonstantin Belousov 1921e442f29fSKonstantin Belousov /* 1922e442f29fSKonstantin Belousov * Recalculate the signal mask and reset the signal disposition after 1923e442f29fSKonstantin Belousov * usermode frame for delivery is formed. Should be called after 1924e442f29fSKonstantin Belousov * mach-specific routine, because sysent->sv_sendsig() needs correct 1925e442f29fSKonstantin Belousov * ps_siginfo and signal mask. 1926e442f29fSKonstantin Belousov */ 1927e442f29fSKonstantin Belousov static void 1928e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps) 1929e442f29fSKonstantin Belousov { 1930e442f29fSKonstantin Belousov sigset_t mask; 1931e442f29fSKonstantin Belousov 1932e442f29fSKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 1933e442f29fSKonstantin Belousov td->td_ru.ru_nsignals++; 1934e442f29fSKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 1935e442f29fSKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1936e442f29fSKonstantin Belousov SIGADDSET(mask, sig); 1937e442f29fSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 1938e442f29fSKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 1939e442f29fSKonstantin Belousov if (SIGISMEMBER(ps->ps_sigreset, sig)) 1940e442f29fSKonstantin Belousov sigdflt(ps, sig); 1941e442f29fSKonstantin Belousov } 1942e442f29fSKonstantin Belousov 1943e442f29fSKonstantin Belousov 1944df8bae1dSRodney W. Grimes /* 19450c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 19460c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 19470c14ff0eSRobert Watson * normally. 1948df8bae1dSRodney W. Grimes */ 1949df8bae1dSRodney W. Grimes void 19509104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 1951df8bae1dSRodney W. Grimes { 19521bf4700bSJeff Roberson struct sigacts *ps; 19531bf4700bSJeff Roberson struct proc *p; 19549104847fSDavid Xu int sig; 19559104847fSDavid Xu int code; 19561bf4700bSJeff Roberson 19571bf4700bSJeff Roberson p = td->td_proc; 19589104847fSDavid Xu sig = ksi->ksi_signo; 19599104847fSDavid Xu code = ksi->ksi_code; 19609104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 19619104847fSDavid Xu 1962628d2653SJohn Baldwin PROC_LOCK(p); 1963ef3dab76STim J. Robbins ps = p->p_sigacts; 196490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 196590af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 19664093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1967df8bae1dSRodney W. Grimes #ifdef KTRACE 1968374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1969374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 19704093529dSJeff Roberson &td->td_sigmask, code); 1971df8bae1dSRodney W. Grimes #endif 19729104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 19739104847fSDavid Xu ksi, &td->td_sigmask); 1974e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 197590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 19766f841fb7SMarcel Moolenaar } else { 1977f71a882fSDavid Xu /* 1978f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 1979f71a882fSDavid Xu * masking the signal or process is ignoring the 1980f71a882fSDavid Xu * signal. 1981f71a882fSDavid Xu */ 1982f71a882fSDavid Xu if (kern_forcesigexit && 1983f71a882fSDavid Xu (SIGISMEMBER(td->td_sigmask, sig) || 1984f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 1985f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 1986f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 1987f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 1988f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1989f71a882fSDavid Xu } 199090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 19916626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 19922c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 1993ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 1994df8bae1dSRodney W. Grimes } 1995628d2653SJohn Baldwin PROC_UNLOCK(p); 1996df8bae1dSRodney W. Grimes } 1997df8bae1dSRodney W. Grimes 19984093529dSJeff Roberson static struct thread * 19994093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 20004093529dSJeff Roberson { 20013074d1b4SDavid Xu struct thread *td, *signal_td; 20024093529dSJeff Roberson 20034093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 20044093529dSJeff Roberson 20054093529dSJeff Roberson /* 2006627451c1SDavid Xu * Check if current thread can handle the signal without 200715b7a831SKonstantin Belousov * switching context to another thread. 20084093529dSJeff Roberson */ 2009627451c1SDavid Xu if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 2010627451c1SDavid Xu return (curthread); 20113074d1b4SDavid Xu signal_td = NULL; 20123074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 20133074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 20143074d1b4SDavid Xu signal_td = td; 2015627451c1SDavid Xu break; 20163074d1b4SDavid Xu } 20173074d1b4SDavid Xu } 20183074d1b4SDavid Xu if (signal_td == NULL) 20193074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 20203074d1b4SDavid Xu return (signal_td); 20214093529dSJeff Roberson } 20224093529dSJeff Roberson 2023df8bae1dSRodney W. Grimes /* 2024df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 2025df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 2026df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 2027df8bae1dSRodney W. Grimes * 2028df8bae1dSRodney W. Grimes * Exceptions: 2029df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 2030df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 2031df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 2032df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 2033df8bae1dSRodney W. Grimes * 2034df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 20354dec0e67SRobert Watson * 20364dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 20374dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 20384dec0e67SRobert Watson * side effects of this unwise possibility. 2039df8bae1dSRodney W. Grimes */ 2040df8bae1dSRodney W. Grimes void 20418451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 2042df8bae1dSRodney W. Grimes { 2043a3de221dSKonstantin Belousov ksiginfo_t ksi; 2044a3de221dSKonstantin Belousov 2045a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 2046a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 2047a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 2048ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 2049a3de221dSKonstantin Belousov } 2050a3de221dSKonstantin Belousov 2051ad6eec7bSJohn Baldwin int 2052a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2053a3de221dSKonstantin Belousov { 2054a3de221dSKonstantin Belousov 2055ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 20569104847fSDavid Xu } 20579104847fSDavid Xu 2058cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 20599104847fSDavid Xu int 2060cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd) 20619104847fSDavid Xu { 2062cf7d9a8cSDavid Xu struct thread *td; 206341b3077aSJacques Vidrine 20646d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2065cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 20666d7b314bSDavid Xu if (td == NULL) 20676d7b314bSDavid Xu return (ESRCH); 2068cf7d9a8cSDavid Xu *ttd = td; 2069cf7d9a8cSDavid Xu } else { 2070cf7d9a8cSDavid Xu *ttd = NULL; 2071cf7d9a8cSDavid Xu PROC_LOCK(p); 20726d7b314bSDavid Xu } 2073cf7d9a8cSDavid Xu return (0); 20744093529dSJeff Roberson } 20754093529dSJeff Roberson 2076ad6eec7bSJohn Baldwin void 2077ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2078ad6eec7bSJohn Baldwin { 2079ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2080ad6eec7bSJohn Baldwin 2081ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2082ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2083ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2084ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2085ad6eec7bSJohn Baldwin } 2086ad6eec7bSJohn Baldwin 2087ad6eec7bSJohn Baldwin void 2088ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2089ad6eec7bSJohn Baldwin { 2090ad6eec7bSJohn Baldwin 2091ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2092ad6eec7bSJohn Baldwin } 2093ad6eec7bSJohn Baldwin 2094cf7d9a8cSDavid Xu int 2095ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 20964093529dSJeff Roberson { 20979104847fSDavid Xu sig_t action; 20989104847fSDavid Xu sigqueue_t *sigqueue; 20999104847fSDavid Xu int prop; 210090af4afaSJohn Baldwin struct sigacts *ps; 210194f0972bSDavid Xu int intrval; 21029104847fSDavid Xu int ret = 0; 2103da7bbd2cSJohn Baldwin int wakeup_swapper; 2104df8bae1dSRodney W. Grimes 2105cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 21066d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 21076d7b314bSDavid Xu 210841b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2109212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 21104093529dSJeff Roberson 2111212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 21126d7b314bSDavid Xu 21136d7b314bSDavid Xu /* 21146d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 21156d7b314bSDavid Xu */ 21166d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 21176d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21186d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21196d7b314bSDavid Xu return (ret); 21206d7b314bSDavid Xu } 21216d7b314bSDavid Xu 212290af4afaSJohn Baldwin ps = p->p_sigacts; 21239e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 21242c42a146SMarcel Moolenaar prop = sigprop(sig); 21254093529dSJeff Roberson 21266d7b314bSDavid Xu if (td == NULL) { 21276d7b314bSDavid Xu td = sigtd(p, sig, prop); 21286d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 2129462314b3SMateusz Guzik } else 21309104847fSDavid Xu sigqueue = &td->td_sigqueue; 21314093529dSJeff Roberson 213236160958SMark Johnston SDT_PROBE3(proc, , , signal__send, td, p, sig); 21335d217f17SJohn Birrell 2134df8bae1dSRodney W. Grimes /* 2135df8bae1dSRodney W. Grimes * If the signal is being ignored, 2136df8bae1dSRodney W. Grimes * then we forget about it immediately. 213790af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 2138df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 2139df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 2140df8bae1dSRodney W. Grimes */ 214190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 21420fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 214336160958SMark Johnston SDT_PROBE3(proc, , , signal__discard, td, p, sig); 21445d217f17SJohn Birrell 214590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 21466d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21476d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21489104847fSDavid Xu return (ret); 214990af4afaSJohn Baldwin } 21506675b36eSDavid Xu if (SIGISMEMBER(td->td_sigmask, sig)) 2151df8bae1dSRodney W. Grimes action = SIG_HOLD; 215290af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2153df8bae1dSRodney W. Grimes action = SIG_CATCH; 2154df8bae1dSRodney W. Grimes else 2155df8bae1dSRodney W. Grimes action = SIG_DFL; 215694f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 215794f0972bSDavid Xu intrval = EINTR; 215894f0972bSDavid Xu else 215994f0972bSDavid Xu intrval = ERESTART; 216090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2161df8bae1dSRodney W. Grimes 2162fd50a707SBrooks Davis if (prop & SIGPROP_CONT) 21639104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 2164fd50a707SBrooks Davis else if (prop & SIGPROP_STOP) { 2165df8bae1dSRodney W. Grimes /* 2166df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2167df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2168df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2169df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2170df8bae1dSRodney W. Grimes */ 2171fd50a707SBrooks Davis if ((prop & SIGPROP_TTYSTOP) && 2172e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 21736d7b314bSDavid Xu (action == SIG_DFL)) { 21746d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21756d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21769104847fSDavid Xu return (ret); 21776d7b314bSDavid Xu } 21789104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2179ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 21806933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2181ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2182ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2183ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2184ebceaf6dSDavid Xu } 2185df8bae1dSRodney W. Grimes } 21863074d1b4SDavid Xu 21879104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 21889104847fSDavid Xu if (ret != 0) 21899104847fSDavid Xu return (ret); 21906d7b314bSDavid Xu signotify(td); 21915312b1c7SDavid Xu /* 21925312b1c7SDavid Xu * Defer further processing for signals which are held, 21935312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 21945312b1c7SDavid Xu */ 21955312b1c7SDavid Xu if (action == SIG_HOLD && 2196fd50a707SBrooks Davis !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 21979104847fSDavid Xu return (ret); 2198b4d33259SEric Badger 2199b4d33259SEric Badger /* SIGKILL: Remove procfs STOPEVENTs. */ 220090d75f78SAlfred Perlstein if (sig == SIGKILL) { 220190d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCBIC */ 220290d75f78SAlfred Perlstein p->p_stops = 0; 220390d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCCONT */ 220490d75f78SAlfred Perlstein p->p_step = 0; 220590d75f78SAlfred Perlstein wakeup(&p->p_step); 220690d75f78SAlfred Perlstein } 220790d75f78SAlfred Perlstein /* 2208e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2209e602ba25SJulian Elischer * component. Most processing occurs when the process next 2210e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 22111968f37bSJohn Baldwin * times when processing needs to be done immediately, such as 2212e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 22131968f37bSJohn Baldwin * We try to do the per-process part here. 2214df8bae1dSRodney W. Grimes */ 2215e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 22160f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 22170f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2218e602ba25SJulian Elischer if (sig == SIGKILL) { 2219137cf33dSDavid Xu /* 2220137cf33dSDavid Xu * If traced process is already stopped, 2221137cf33dSDavid Xu * then no further action is necessary. 2222137cf33dSDavid Xu */ 222383b718ebSDavid Xu if (p->p_flag & P_TRACED) 222483b718ebSDavid Xu goto out; 2225df8bae1dSRodney W. Grimes /* 2226e602ba25SJulian Elischer * SIGKILL sets process running. 2227e602ba25SJulian Elischer * It will die elsewhere. 2228e602ba25SJulian Elischer * All threads must be restarted. 2229df8bae1dSRodney W. Grimes */ 2230482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2231e602ba25SJulian Elischer goto runfast; 2232e602ba25SJulian Elischer } 2233e602ba25SJulian Elischer 2234fd50a707SBrooks Davis if (prop & SIGPROP_CONT) { 2235137cf33dSDavid Xu /* 2236137cf33dSDavid Xu * If traced process is already stopped, 2237137cf33dSDavid Xu * then no further action is necessary. 2238137cf33dSDavid Xu */ 223983b718ebSDavid Xu if (p->p_flag & P_TRACED) 224083b718ebSDavid Xu goto out; 2241e602ba25SJulian Elischer /* 2242e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 22439104847fSDavid Xu * process but don't leave the signal in sigqueue as 22441d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2245e602ba25SJulian Elischer * continue the process and leave the signal in 22469104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2247e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2248e602ba25SJulian Elischer * an event, it goes back to run state. 2249e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2250e602ba25SJulian Elischer */ 22511279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 22527b4a950aSDavid Xu PROC_SLOCK(p); 2253ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 22547b4a950aSDavid Xu PROC_SUNLOCK(p); 22556933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 2256b4490c6eSKonstantin Belousov p->p_xsig = SIGCONT; 22577f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2258ebceaf6dSDavid Xu childproc_continued(p); 22597f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 22607b4a950aSDavid Xu PROC_SLOCK(p); 2261ebceaf6dSDavid Xu } 2262e602ba25SJulian Elischer if (action == SIG_DFL) { 2263a54e85fdSJeff Roberson thread_unsuspend(p); 22647b4a950aSDavid Xu PROC_SUNLOCK(p); 22659104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2266a54e85fdSJeff Roberson goto out; 2267a54e85fdSJeff Roberson } 2268a54e85fdSJeff Roberson if (action == SIG_CATCH) { 22698460a577SJohn Birrell /* 22708460a577SJohn Birrell * The process wants to catch it so it needs 22718460a577SJohn Birrell * to run at least one thread, but which one? 22728460a577SJohn Birrell */ 22737b4a950aSDavid Xu PROC_SUNLOCK(p); 2274e602ba25SJulian Elischer goto runfast; 2275e602ba25SJulian Elischer } 2276e602ba25SJulian Elischer /* 2277e602ba25SJulian Elischer * The signal is not ignored or caught. 2278e602ba25SJulian Elischer */ 227904774f23SJulian Elischer thread_unsuspend(p); 22807b4a950aSDavid Xu PROC_SUNLOCK(p); 2281e602ba25SJulian Elischer goto out; 2282e602ba25SJulian Elischer } 2283e602ba25SJulian Elischer 2284fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2285137cf33dSDavid Xu /* 2286137cf33dSDavid Xu * If traced process is already stopped, 2287137cf33dSDavid Xu * then no further action is necessary. 2288137cf33dSDavid Xu */ 228983b718ebSDavid Xu if (p->p_flag & P_TRACED) 229083b718ebSDavid Xu goto out; 2291e602ba25SJulian Elischer /* 2292e602ba25SJulian Elischer * Already stopped, don't need to stop again 2293e602ba25SJulian Elischer * (If we did the shell could get confused). 229404774f23SJulian Elischer * Just make sure the signal STOP bit set. 2295e602ba25SJulian Elischer */ 22961279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 22979104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2298e602ba25SJulian Elischer goto out; 2299e602ba25SJulian Elischer } 2300e602ba25SJulian Elischer 2301e602ba25SJulian Elischer /* 2302e602ba25SJulian Elischer * All other kinds of signals: 2303e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2304e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2305e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 230604774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2307e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2308e602ba25SJulian Elischer */ 2309da7bbd2cSJohn Baldwin wakeup_swapper = 0; 23107b4a950aSDavid Xu PROC_SLOCK(p); 2311a54e85fdSJeff Roberson thread_lock(td); 231244f3b092SJohn Baldwin if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2313da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2314a54e85fdSJeff Roberson thread_unlock(td); 23157b4a950aSDavid Xu PROC_SUNLOCK(p); 2316da7bbd2cSJohn Baldwin if (wakeup_swapper) 2317da7bbd2cSJohn Baldwin kick_proc0(); 2318df8bae1dSRodney W. Grimes goto out; 2319df8bae1dSRodney W. Grimes /* 23209a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 23219a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2322df8bae1dSRodney W. Grimes */ 2323e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2324ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 232594f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2326df8bae1dSRodney W. Grimes goto out; 232704774f23SJulian Elischer } 2328ec8297bdSDavid Xu 2329ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2330ec8297bdSDavid Xu 2331fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 23320f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 233335c32a76SDavid Xu goto out; 2334e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2335b4490c6eSKonstantin Belousov p->p_xsig = sig; 23367b4a950aSDavid Xu PROC_SLOCK(p); 23376f56cb8dSKonstantin Belousov wakeup_swapper = sig_suspend_threads(td, p, 1); 23384093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2339ebceaf6dSDavid Xu /* 2340ebceaf6dSDavid Xu * only thread sending signal to another 2341ebceaf6dSDavid Xu * process can reach here, if thread is sending 2342ebceaf6dSDavid Xu * signal to its process, because thread does 2343ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2344ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2345ebceaf6dSDavid Xu */ 2346ebceaf6dSDavid Xu thread_stopped(p); 23477b4a950aSDavid Xu PROC_SUNLOCK(p); 2348b4490c6eSKonstantin Belousov sigqueue_delete_proc(p, p->p_xsig); 23497b4a950aSDavid Xu } else 23507b4a950aSDavid Xu PROC_SUNLOCK(p); 23516f56cb8dSKonstantin Belousov if (wakeup_swapper) 23526f56cb8dSKonstantin Belousov kick_proc0(); 235335c32a76SDavid Xu goto out; 235435c32a76SDavid Xu } 2355e602ba25SJulian Elischer } else { 2356e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 23579104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2358e602ba25SJulian Elischer goto out; 2359e602ba25SJulian Elischer } 2360e602ba25SJulian Elischer 2361b40ce416SJulian Elischer /* 2362e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2363e602ba25SJulian Elischer * running threads. 2364b40ce416SJulian Elischer */ 2365e602ba25SJulian Elischer runfast: 236694f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 23677b4a950aSDavid Xu PROC_SLOCK(p); 2368e602ba25SJulian Elischer thread_unsuspend(p); 23697b4a950aSDavid Xu PROC_SUNLOCK(p); 2370e602ba25SJulian Elischer out: 23717b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 23727b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 23739104847fSDavid Xu return (ret); 2374e602ba25SJulian Elischer } 2375e602ba25SJulian Elischer 2376e602ba25SJulian Elischer /* 2377e602ba25SJulian Elischer * The force of a signal has been directed against a single 2378e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2379e602ba25SJulian Elischer * out of any sleep it may be in etc. 2380e602ba25SJulian Elischer */ 2381e602ba25SJulian Elischer static void 238294f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2383e602ba25SJulian Elischer { 2384e602ba25SJulian Elischer struct proc *p = td->td_proc; 2385e602ba25SJulian Elischer register int prop; 2386da7bbd2cSJohn Baldwin int wakeup_swapper; 2387e602ba25SJulian Elischer 2388da7bbd2cSJohn Baldwin wakeup_swapper = 0; 23898b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2390e602ba25SJulian Elischer prop = sigprop(sig); 2391a4c2da15SBruce Evans 23927b4a950aSDavid Xu PROC_SLOCK(p); 2393374ae2a3SJeff Roberson thread_lock(td); 2394e602ba25SJulian Elischer /* 2395aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2396aef68c96SKonstantin Belousov * killed in this lifetime. Be careful to avoid bumping the 2397aef68c96SKonstantin Belousov * priority of the idle thread, since we still allow to signal 2398aef68c96SKonstantin Belousov * kernel processes. 2399e602ba25SJulian Elischer */ 2400fd50a707SBrooks Davis if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2401aef68c96SKonstantin Belousov td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2402b3a4fb14SDavid Xu sched_prio(td, PUSER); 240380c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2404e602ba25SJulian Elischer /* 2405e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2406e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2407e602ba25SJulian Elischer * be noticed when the process returns through 2408e602ba25SJulian Elischer * trap() or syscall(). 2409e602ba25SJulian Elischer */ 241044f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2411374ae2a3SJeff Roberson goto out; 2412e602ba25SJulian Elischer /* 2413e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2414e602ba25SJulian Elischer * asleep, we are finished; the process should not 2415e602ba25SJulian Elischer * be awakened. 2416df8bae1dSRodney W. Grimes */ 2417fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2418a54e85fdSJeff Roberson thread_unlock(td); 24197b4a950aSDavid Xu PROC_SUNLOCK(p); 24209104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 24214093529dSJeff Roberson /* 24224093529dSJeff Roberson * It may be on either list in this state. 24234093529dSJeff Roberson * Remove from both for now. 24244093529dSJeff Roberson */ 24259104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2426aa0fa334SJulian Elischer return; 2427df8bae1dSRodney W. Grimes } 2428df8bae1dSRodney W. Grimes 2429aa0fa334SJulian Elischer /* 2430a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2431a120a7a3SJohn Baldwin * STOP signal is deferred. 2432a120a7a3SJohn Baldwin */ 2433fd50a707SBrooks Davis if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 24346f56cb8dSKonstantin Belousov TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2435a120a7a3SJohn Baldwin goto out; 2436a120a7a3SJohn Baldwin 2437a120a7a3SJohn Baldwin /* 2438a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2439aa0fa334SJulian Elischer */ 2440aef68c96SKonstantin Belousov if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2441b3a4fb14SDavid Xu sched_prio(td, PUSER); 2442ec8297bdSDavid Xu 2443da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2444a4c2da15SBruce Evans } else { 2445df8bae1dSRodney W. Grimes /* 2446a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2447df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2448df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2449df8bae1dSRodney W. Grimes */ 2450a4c2da15SBruce Evans #ifdef SMP 245144f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2452e602ba25SJulian Elischer forward_signal(td); 24533163861cSTor Egge #endif 24546caa8a15SJohn Baldwin } 2455374ae2a3SJeff Roberson out: 24567b4a950aSDavid Xu PROC_SUNLOCK(p); 2457374ae2a3SJeff Roberson thread_unlock(td); 2458da7bbd2cSJohn Baldwin if (wakeup_swapper) 2459da7bbd2cSJohn Baldwin kick_proc0(); 2460a4c2da15SBruce Evans } 2461df8bae1dSRodney W. Grimes 24626f56cb8dSKonstantin Belousov static int 2463d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2464d8267df7SDavid Xu { 2465d8267df7SDavid Xu struct thread *td2; 24666f56cb8dSKonstantin Belousov int wakeup_swapper; 2467d8267df7SDavid Xu 2468d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 24697b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2470d8267df7SDavid Xu 24716f56cb8dSKonstantin Belousov wakeup_swapper = 0; 2472d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2473a54e85fdSJeff Roberson thread_lock(td2); 2474b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2475d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2476f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2477f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2478a120a7a3SJohn Baldwin /* 2479a120a7a3SJohn Baldwin * Once a thread is asleep with 24806f56cb8dSKonstantin Belousov * TDF_SBDRY and without TDF_SERESTART 24816f56cb8dSKonstantin Belousov * or TDF_SEINTR set, it should never 2482a120a7a3SJohn Baldwin * become suspended due to this check. 2483a120a7a3SJohn Baldwin */ 2484a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2485a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 248646e47c4fSKonstantin Belousov if (TD_SBDRY_INTR(td2) && sending) { 248746e47c4fSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, 248846e47c4fSKonstantin Belousov TD_SBDRY_ERRNO(td2)); 24896f56cb8dSKonstantin Belousov } 2490f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2491d8267df7SDavid Xu thread_suspend_one(td2); 2492f33a947bSKonstantin Belousov } 2493f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2494d8267df7SDavid Xu if (sending || td != td2) 2495d8267df7SDavid Xu td2->td_flags |= TDF_ASTPENDING; 2496d8267df7SDavid Xu #ifdef SMP 2497d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2498d8267df7SDavid Xu forward_signal(td2); 2499d8267df7SDavid Xu #endif 2500d8267df7SDavid Xu } 2501a54e85fdSJeff Roberson thread_unlock(td2); 2502d8267df7SDavid Xu } 25036f56cb8dSKonstantin Belousov return (wakeup_swapper); 2504d8267df7SDavid Xu } 2505d8267df7SDavid Xu 250682a4538fSEric Badger /* 250782a4538fSEric Badger * Stop the process for an event deemed interesting to the debugger. If si is 250882a4538fSEric Badger * non-NULL, this is a signal exchange; the new signal requested by the 250982a4538fSEric Badger * debugger will be returned for handling. If si is NULL, this is some other 251082a4538fSEric Badger * type of interesting event. The debugger may request a signal be delivered in 251182a4538fSEric Badger * that case as well, however it will be deferred until it can be handled. 251282a4538fSEric Badger */ 2513cbf4e354SDavid Xu int 251482a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si) 25154cc9f52fSRobert Drehmel { 25164cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 251782a4538fSEric Badger struct thread *td2; 251882a4538fSEric Badger ksiginfo_t ksi; 251982a4538fSEric Badger int prop; 25204cc9f52fSRobert Drehmel 252130a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 25220f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 25234cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2524aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 25254cc9f52fSRobert Drehmel 2526cbf4e354SDavid Xu td->td_xsig = sig; 252782a4538fSEric Badger 252882a4538fSEric Badger if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 252982a4538fSEric Badger td->td_dbgflags |= TDB_XSIG; 2530515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2531515b7a0bSJohn Baldwin td->td_tid, p->p_pid, td->td_dbgflags, sig); 25327b4a950aSDavid Xu PROC_SLOCK(p); 2533904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 253482a4538fSEric Badger if (P_KILLED(p)) { 253582a4538fSEric Badger /* 253682a4538fSEric Badger * Ensure that, if we've been PT_KILLed, the 253782a4538fSEric Badger * exit status reflects that. Another thread 253882a4538fSEric Badger * may also be in ptracestop(), having just 253982a4538fSEric Badger * received the SIGKILL, but this thread was 254082a4538fSEric Badger * unsuspended first. 254182a4538fSEric Badger */ 254282a4538fSEric Badger td->td_dbgflags &= ~TDB_XSIG; 254382a4538fSEric Badger td->td_xsig = SIGKILL; 254482a4538fSEric Badger p->p_ptevents = 0; 254582a4538fSEric Badger break; 254682a4538fSEric Badger } 25475fcfab6eSJohn Baldwin if (p->p_flag & P_SINGLE_EXIT && 25485fcfab6eSJohn Baldwin !(td->td_dbgflags & TDB_EXIT)) { 25495fcfab6eSJohn Baldwin /* 25505fcfab6eSJohn Baldwin * Ignore ptrace stops except for thread exit 25515fcfab6eSJohn Baldwin * events when the process exits. 25525fcfab6eSJohn Baldwin */ 2553904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 25547b4a950aSDavid Xu PROC_SUNLOCK(p); 255582a4538fSEric Badger return (0); 2556cbf4e354SDavid Xu } 2557b7a25e63SKonstantin Belousov 2558cbf4e354SDavid Xu /* 2559b7a25e63SKonstantin Belousov * Make wait(2) work. Ensure that right after the 2560b7a25e63SKonstantin Belousov * attach, the thread which was decided to become the 2561b7a25e63SKonstantin Belousov * leader of attach gets reported to the waiter. 2562b7a25e63SKonstantin Belousov * Otherwise, just avoid overwriting another thread's 2563b7a25e63SKonstantin Belousov * assignment to p_xthread. If another thread has 2564b7a25e63SKonstantin Belousov * already set p_xthread, the current thread will get 2565b7a25e63SKonstantin Belousov * a chance to report itself upon the next iteration. 2566cbf4e354SDavid Xu */ 2567b7a25e63SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0 || 25687f649ddaSMark Johnston ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2569b7a25e63SKonstantin Belousov p->p_xthread == NULL)) { 2570b4490c6eSKonstantin Belousov p->p_xsig = sig; 2571cbf4e354SDavid Xu p->p_xthread = td; 2572b7a25e63SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 2573b7a25e63SKonstantin Belousov p->p_flag2 &= ~P2_PTRACE_FSTP; 2574b7a25e63SKonstantin Belousov p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2575d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 2576b7a25e63SKonstantin Belousov } 25776fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 25786fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 25796fa39a73SKonstantin Belousov cv_broadcast(&p->p_dbgwait); 25806fa39a73SKonstantin Belousov } 2581cbf4e354SDavid Xu stopme: 25826ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 25837ce60f60SDavid Xu if (p->p_xthread == td) 25847ce60f60SDavid Xu p->p_xthread = NULL; 25857ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2586cbf4e354SDavid Xu break; 2587904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2588cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2589cbf4e354SDavid Xu break; 2590cbf4e354SDavid Xu goto stopme; 2591cbf4e354SDavid Xu } 2592cbf4e354SDavid Xu } 25937b4a950aSDavid Xu PROC_SUNLOCK(p); 259482a4538fSEric Badger } 259582a4538fSEric Badger 259682a4538fSEric Badger if (si != NULL && sig == td->td_xsig) { 259782a4538fSEric Badger /* Parent wants us to take the original signal unchanged. */ 259882a4538fSEric Badger si->ksi_flags |= KSI_HEAD; 259982a4538fSEric Badger if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 260082a4538fSEric Badger si->ksi_signo = 0; 260182a4538fSEric Badger } else if (td->td_xsig != 0) { 260282a4538fSEric Badger /* 260382a4538fSEric Badger * If parent wants us to take a new signal, then it will leave 260482a4538fSEric Badger * it in td->td_xsig; otherwise we just look for signals again. 260582a4538fSEric Badger */ 260682a4538fSEric Badger ksiginfo_init(&ksi); 260782a4538fSEric Badger ksi.ksi_signo = td->td_xsig; 260882a4538fSEric Badger ksi.ksi_flags |= KSI_PTRACE; 260982a4538fSEric Badger prop = sigprop(td->td_xsig); 261082a4538fSEric Badger td2 = sigtd(p, td->td_xsig, prop); 261182a4538fSEric Badger tdsendsignal(p, td2, td->td_xsig, &ksi); 261282a4538fSEric Badger if (td != td2) 261382a4538fSEric Badger return (0); 261482a4538fSEric Badger } 261582a4538fSEric Badger 2616cbf4e354SDavid Xu return (td->td_xsig); 26174cc9f52fSRobert Drehmel } 26184cc9f52fSRobert Drehmel 26196b286ee8SKonstantin Belousov static void 262080a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 26216b286ee8SKonstantin Belousov { 26226b286ee8SKonstantin Belousov struct sigacts *ps; 26236b286ee8SKonstantin Belousov struct thread *td; 2624407af02bSDavid Xu int sig; 26256b286ee8SKonstantin Belousov 26266b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 262770778bbaSKonstantin Belousov ps = p->p_sigacts; 262870778bbaSKonstantin Belousov mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ? 262970778bbaSKonstantin Belousov MA_OWNED : MA_NOTOWNED); 2630407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2631407af02bSDavid Xu return; 2632407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2633407af02bSDavid Xu while ((sig = sig_ffs(&block)) != 0) { 2634407af02bSDavid Xu SIGDELSET(block, sig); 2635407af02bSDavid Xu td = sigtd(p, sig, 0); 26366b286ee8SKonstantin Belousov signotify(td); 263780a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 26386b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2639407af02bSDavid Xu if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig)) 2640407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2641407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 26426b286ee8SKonstantin Belousov ERESTART)); 264380a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 26446b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 26456b286ee8SKonstantin Belousov } 26466b286ee8SKonstantin Belousov } 26476b286ee8SKonstantin Belousov 26486b286ee8SKonstantin Belousov void 26496b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 26506b286ee8SKonstantin Belousov { 26516b286ee8SKonstantin Belousov struct proc *p; 26526b286ee8SKonstantin Belousov sigset_t unblocked; 26536b286ee8SKonstantin Belousov 26546b286ee8SKonstantin Belousov p = td->td_proc; 26556b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 26566b286ee8SKonstantin Belousov 26576b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 26586b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 26596b286ee8SKonstantin Belousov return; 26606b286ee8SKonstantin Belousov 26616b286ee8SKonstantin Belousov /* 26626b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 26636b286ee8SKonstantin Belousov * about this by filling the sigmask. 26646b286ee8SKonstantin Belousov * 26656b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 26666b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 26676b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 26686b286ee8SKonstantin Belousov */ 26696b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 26706b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 26716b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 267280a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 26736b286ee8SKonstantin Belousov 26746b286ee8SKonstantin Belousov } 26756b286ee8SKonstantin Belousov 26763a1e5dd8SKonstantin Belousov static int 26773a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags) 2678a120a7a3SJohn Baldwin { 2679a120a7a3SJohn Baldwin 26803a1e5dd8SKonstantin Belousov MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 26813a1e5dd8SKonstantin Belousov (cflags & TDF_SBDRY) != 0); 26823a1e5dd8SKonstantin Belousov return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 2683a120a7a3SJohn Baldwin } 2684a120a7a3SJohn Baldwin 2685a120a7a3SJohn Baldwin /* 26863a1e5dd8SKonstantin Belousov * Defer the delivery of SIGSTOP for the current thread, according to 26873a1e5dd8SKonstantin Belousov * the requested mode. Returns previous flags, which must be restored 26883a1e5dd8SKonstantin Belousov * by sigallowstop(). 26893a1e5dd8SKonstantin Belousov * 26903a1e5dd8SKonstantin Belousov * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 26913a1e5dd8SKonstantin Belousov * cleared by the current thread, which allow the lock-less read-only 26923a1e5dd8SKonstantin Belousov * accesses below. 2693a120a7a3SJohn Baldwin */ 2694e3612a4cSKonstantin Belousov int 269546e47c4fSKonstantin Belousov sigdeferstop_impl(int mode) 2696a120a7a3SJohn Baldwin { 2697593efaf9SJohn Baldwin struct thread *td; 26983a1e5dd8SKonstantin Belousov int cflags, nflags; 2699a120a7a3SJohn Baldwin 2700593efaf9SJohn Baldwin td = curthread; 27013a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 27023a1e5dd8SKonstantin Belousov switch (mode) { 27033a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_NOP: 27043a1e5dd8SKonstantin Belousov nflags = cflags; 27053a1e5dd8SKonstantin Belousov break; 27063a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_OFF: 27073a1e5dd8SKonstantin Belousov nflags = 0; 27083a1e5dd8SKonstantin Belousov break; 27093a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_SILENT: 27103a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 27113a1e5dd8SKonstantin Belousov break; 27123a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_EINTR: 27133a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 27143a1e5dd8SKonstantin Belousov break; 27153a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_ERESTART: 27163a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 27173a1e5dd8SKonstantin Belousov break; 27183a1e5dd8SKonstantin Belousov default: 27193a1e5dd8SKonstantin Belousov panic("sigdeferstop: invalid mode %x", mode); 27203a1e5dd8SKonstantin Belousov break; 27213a1e5dd8SKonstantin Belousov } 272246e47c4fSKonstantin Belousov if (cflags == nflags) 272346e47c4fSKonstantin Belousov return (SIGDEFERSTOP_VAL_NCHG); 2724a120a7a3SJohn Baldwin thread_lock(td); 27253a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | nflags; 2726a120a7a3SJohn Baldwin thread_unlock(td); 27273a1e5dd8SKonstantin Belousov return (cflags); 27283a1e5dd8SKonstantin Belousov } 27293a1e5dd8SKonstantin Belousov 27303a1e5dd8SKonstantin Belousov /* 27313a1e5dd8SKonstantin Belousov * Restores the STOP handling mode, typically permitting the delivery 27323a1e5dd8SKonstantin Belousov * of SIGSTOP for the current thread. This does not immediately 27333a1e5dd8SKonstantin Belousov * suspend if a stop was posted. Instead, the thread will suspend 27343a1e5dd8SKonstantin Belousov * either via ast() or a subsequent interruptible sleep. 27353a1e5dd8SKonstantin Belousov */ 27363a1e5dd8SKonstantin Belousov void 273746e47c4fSKonstantin Belousov sigallowstop_impl(int prev) 27383a1e5dd8SKonstantin Belousov { 27393a1e5dd8SKonstantin Belousov struct thread *td; 27403a1e5dd8SKonstantin Belousov int cflags; 27413a1e5dd8SKonstantin Belousov 274246e47c4fSKonstantin Belousov KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 27433a1e5dd8SKonstantin Belousov KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 27443a1e5dd8SKonstantin Belousov ("sigallowstop: incorrect previous mode %x", prev)); 27453a1e5dd8SKonstantin Belousov td = curthread; 27463a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 27473a1e5dd8SKonstantin Belousov if (cflags != prev) { 27483a1e5dd8SKonstantin Belousov thread_lock(td); 27493a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | prev; 27503a1e5dd8SKonstantin Belousov thread_unlock(td); 27513a1e5dd8SKonstantin Belousov } 2752a120a7a3SJohn Baldwin } 2753a120a7a3SJohn Baldwin 2754df8bae1dSRodney W. Grimes /* 2755df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2756df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2757df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2758df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2759df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2760628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2761df8bae1dSRodney W. Grimes * sequence is 2762df8bae1dSRodney W. Grimes * 2763e602ba25SJulian Elischer * while (sig = cursig(curthread)) 27642c42a146SMarcel Moolenaar * postsig(sig); 2765df8bae1dSRodney W. Grimes */ 27666711f10fSJohn Baldwin static int 27673cf3b9f0SJohn Baldwin issignal(struct thread *td) 2768df8bae1dSRodney W. Grimes { 2769e602ba25SJulian Elischer struct proc *p; 277090af4afaSJohn Baldwin struct sigacts *ps; 27716b286ee8SKonstantin Belousov struct sigqueue *queue; 27724093529dSJeff Roberson sigset_t sigpending; 277382a4538fSEric Badger int sig, prop; 2774*86be94fcSTycho Nightingale ksiginfo_t ksi; 2775df8bae1dSRodney W. Grimes 2776e602ba25SJulian Elischer p = td->td_proc; 277790af4afaSJohn Baldwin ps = p->p_sigacts; 277890af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2779628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2780df8bae1dSRodney W. Grimes for (;;) { 27812a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 27822a024a2bSSean Eric Fagan 27839104847fSDavid Xu sigpending = td->td_sigqueue.sq_signals; 27846b286ee8SKonstantin Belousov SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 27854093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 27864093529dSJeff Roberson 27876f56cb8dSKonstantin Belousov if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 27886f56cb8dSKonstantin Belousov (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 27894093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 27904093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2791df8bae1dSRodney W. Grimes return (0); 2792b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 2793b7a25e63SKonstantin Belousov (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 2794b7a25e63SKonstantin Belousov SIGISMEMBER(sigpending, SIGSTOP)) { 2795b7a25e63SKonstantin Belousov /* 2796b7a25e63SKonstantin Belousov * If debugger just attached, always consume 2797b7a25e63SKonstantin Belousov * SIGSTOP from ptrace(PT_ATTACH) first, to 2798b7a25e63SKonstantin Belousov * execute the debugger attach ritual in 2799b7a25e63SKonstantin Belousov * order. 2800b7a25e63SKonstantin Belousov */ 2801b7a25e63SKonstantin Belousov sig = SIGSTOP; 2802b7a25e63SKonstantin Belousov td->td_dbgflags |= TDB_FSTP; 2803b7a25e63SKonstantin Belousov } else { 28044093529dSJeff Roberson sig = sig_ffs(&sigpending); 2805b7a25e63SKonstantin Belousov } 28062a024a2bSSean Eric Fagan 2807047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2808047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2809047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2810047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2811047aa39bSRobert Watson } 28122a024a2bSSean Eric Fagan 2813df8bae1dSRodney W. Grimes /* 2814df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2815df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2816df8bae1dSRodney W. Grimes */ 281790af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 28189104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 28196b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2820df8bae1dSRodney W. Grimes continue; 2821df8bae1dSRodney W. Grimes } 2822b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 2823df8bae1dSRodney W. Grimes /* 2824d8f4f6a4SJonathan Mini * If traced, always stop. 28256a671a6bSKonstantin Belousov * Remove old signal from queue before the stop. 28266a671a6bSKonstantin Belousov * XXX shrug off debugger, it causes siginfo to 28276a671a6bSKonstantin Belousov * be thrown away. 2828df8bae1dSRodney W. Grimes */ 28296a671a6bSKonstantin Belousov queue = &td->td_sigqueue; 2830*86be94fcSTycho Nightingale ksiginfo_init(&ksi); 2831*86be94fcSTycho Nightingale if (sigqueue_get(queue, sig, &ksi) == 0) { 28326a671a6bSKonstantin Belousov queue = &p->p_sigqueue; 2833*86be94fcSTycho Nightingale sigqueue_get(queue, sig, &ksi); 28346a671a6bSKonstantin Belousov } 2835*86be94fcSTycho Nightingale td->td_si = ksi.ksi_info; 28366a671a6bSKonstantin Belousov 283790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2838*86be94fcSTycho Nightingale sig = ptracestop(td, sig, &ksi); 283990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2840df8bae1dSRodney W. Grimes 2841df8bae1dSRodney W. Grimes /* 284282a4538fSEric Badger * Keep looking if the debugger discarded the signal 284382a4538fSEric Badger * or replaced it with a masked signal. 284482a4538fSEric Badger * 28458d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 28468d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 28478d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 28488d542cb5SDavid E. O'Brien */ 284982a4538fSEric Badger if (sig == 0 || (p->p_flag & P_TRACED) == 0) 28508d542cb5SDavid E. O'Brien continue; 2851df8bae1dSRodney W. Grimes } 2852df8bae1dSRodney W. Grimes 28538d542cb5SDavid E. O'Brien prop = sigprop(sig); 28548d542cb5SDavid E. O'Brien 2855df8bae1dSRodney W. Grimes /* 2856df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2857df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2858df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2859df8bae1dSRodney W. Grimes */ 2860d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2861df8bae1dSRodney W. Grimes 2862d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2863df8bae1dSRodney W. Grimes /* 2864df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2865df8bae1dSRodney W. Grimes */ 2866df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2867df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2868df8bae1dSRodney W. Grimes /* 2869df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2870df8bae1dSRodney W. Grimes * in init? XXX 2871df8bae1dSRodney W. Grimes */ 2872d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 28732c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2874df8bae1dSRodney W. Grimes #endif 2875df8bae1dSRodney W. Grimes break; /* == ignore */ 2876df8bae1dSRodney W. Grimes } 2877df8bae1dSRodney W. Grimes /* 2878b38bd91fSEric Badger * If there is a pending stop signal to process with 2879b38bd91fSEric Badger * default action, stop here, then clear the signal. 2880b38bd91fSEric Badger * Traced or exiting processes should ignore stops. 2881b38bd91fSEric Badger * Additionally, a member of an orphaned process group 2882b38bd91fSEric Badger * should ignore tty stops. 2883df8bae1dSRodney W. Grimes */ 2884fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2885b38bd91fSEric Badger if (p->p_flag & 2886b38bd91fSEric Badger (P_TRACED | P_WEXIT | P_SINGLE_EXIT) || 2887df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2888fd50a707SBrooks Davis prop & SIGPROP_TTYSTOP)) 2889df8bae1dSRodney W. Grimes break; /* == ignore */ 289046e47c4fSKonstantin Belousov if (TD_SBDRY_INTR(td)) { 289146e47c4fSKonstantin Belousov KASSERT((td->td_flags & TDF_SBDRY) != 0, 289246e47c4fSKonstantin Belousov ("lost TDF_SBDRY")); 289346e47c4fSKonstantin Belousov return (-1); 289446e47c4fSKonstantin Belousov } 289590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 289690af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2897aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Catching SIGSTOP"); 2898b7a25e63SKonstantin Belousov sigqueue_delete(&td->td_sigqueue, sig); 2899b7a25e63SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2900e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2901b4490c6eSKonstantin Belousov p->p_xsig = sig; 29027b4a950aSDavid Xu PROC_SLOCK(p); 2903d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 29046ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 29057b4a950aSDavid Xu PROC_SUNLOCK(p); 290690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2907b7a25e63SKonstantin Belousov goto next; 2908fd50a707SBrooks Davis } else if (prop & SIGPROP_IGNORE) { 2909df8bae1dSRodney W. Grimes /* 2910df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2911df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2912df8bae1dSRodney W. Grimes */ 2913df8bae1dSRodney W. Grimes break; /* == ignore */ 2914df8bae1dSRodney W. Grimes } else 29152c42a146SMarcel Moolenaar return (sig); 2916df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2917df8bae1dSRodney W. Grimes 2918d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2919df8bae1dSRodney W. Grimes /* 2920df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2921df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2922df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2923df8bae1dSRodney W. Grimes */ 2924fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) == 0 && 2925df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2926df8bae1dSRodney W. Grimes printf("issignal\n"); 2927df8bae1dSRodney W. Grimes break; /* == ignore */ 2928df8bae1dSRodney W. Grimes 2929df8bae1dSRodney W. Grimes default: 2930df8bae1dSRodney W. Grimes /* 2931df8bae1dSRodney W. Grimes * This signal has an action, let 2932df8bae1dSRodney W. Grimes * postsig() process it. 2933df8bae1dSRodney W. Grimes */ 29342c42a146SMarcel Moolenaar return (sig); 2935df8bae1dSRodney W. Grimes } 29369104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 29376b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2938b7a25e63SKonstantin Belousov next:; 2939df8bae1dSRodney W. Grimes } 2940df8bae1dSRodney W. Grimes /* NOTREACHED */ 2941df8bae1dSRodney W. Grimes } 2942df8bae1dSRodney W. Grimes 2943e574e444SDavid Xu void 2944e574e444SDavid Xu thread_stopped(struct proc *p) 2945e574e444SDavid Xu { 2946e574e444SDavid Xu int n; 2947e574e444SDavid Xu 2948e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 29497b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2950e574e444SDavid Xu n = p->p_suspcount; 29517c9a98f1SDavid Xu if (p == curproc) 2952e574e444SDavid Xu n++; 2953e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 29547b4a950aSDavid Xu PROC_SUNLOCK(p); 2955407948a5SDavid Xu p->p_flag &= ~P_WAITED; 2956e574e444SDavid Xu PROC_LOCK(p->p_pptr); 29577f96995eSDavid Xu childproc_stopped(p, (p->p_flag & P_TRACED) ? 2958ebceaf6dSDavid Xu CLD_TRAPPED : CLD_STOPPED); 2959e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 29607b4a950aSDavid Xu PROC_SLOCK(p); 2961e574e444SDavid Xu } 2962e574e444SDavid Xu } 2963e574e444SDavid Xu 2964df8bae1dSRodney W. Grimes /* 2965df8bae1dSRodney W. Grimes * Take the action for the specified signal 2966df8bae1dSRodney W. Grimes * from the current set of pending signals. 2967df8bae1dSRodney W. Grimes */ 296875c586a4SKonstantin Belousov int 29692c42a146SMarcel Moolenaar postsig(sig) 29702c42a146SMarcel Moolenaar register int sig; 2971df8bae1dSRodney W. Grimes { 2972b40ce416SJulian Elischer struct thread *td = curthread; 2973e052a8b9SEd Maste struct proc *p = td->td_proc; 2974628d2653SJohn Baldwin struct sigacts *ps; 29752c42a146SMarcel Moolenaar sig_t action; 29769104847fSDavid Xu ksiginfo_t ksi; 2977e442f29fSKonstantin Belousov sigset_t returnmask; 2978df8bae1dSRodney W. Grimes 29792c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 29805526d2d9SEivind Eklund 29812ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2982628d2653SJohn Baldwin ps = p->p_sigacts; 298390af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 29845da49fcbSDavid Xu ksiginfo_init(&ksi); 29856b286ee8SKonstantin Belousov if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 29866b286ee8SKonstantin Belousov sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 298775c586a4SKonstantin Belousov return (0); 29889104847fSDavid Xu ksi.ksi_signo = sig; 298956c06c4bSDavid Xu if (ksi.ksi_code == SI_TIMER) 299056c06c4bSDavid Xu itimer_accept(p, ksi.ksi_timerid, &ksi); 29912c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 2992df8bae1dSRodney W. Grimes #ifdef KTRACE 2993374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 29945e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 299561009552SJilles Tjoelker &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 2996df8bae1dSRodney W. Grimes #endif 2997047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2998047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2999047aa39bSRobert Watson stopevent(p, S_SIG, sig); 3000047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 3001047aa39bSRobert Watson } 30022a024a2bSSean Eric Fagan 30038460a577SJohn Birrell if (action == SIG_DFL) { 3004df8bae1dSRodney W. Grimes /* 3005df8bae1dSRodney W. Grimes * Default action, where the default is to kill 3006df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 3007df8bae1dSRodney W. Grimes */ 300890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 3009*86be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi.ksi_info); 3010b40ce416SJulian Elischer sigexit(td, sig); 3011df8bae1dSRodney W. Grimes /* NOTREACHED */ 3012df8bae1dSRodney W. Grimes } else { 3013df8bae1dSRodney W. Grimes /* 3014df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 3015df8bae1dSRodney W. Grimes */ 30164093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 30175526d2d9SEivind Eklund ("postsig action")); 3018df8bae1dSRodney W. Grimes /* 3019df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 3020645682fdSLuoqi Chen * occurrences of this signal. 3021df8bae1dSRodney W. Grimes * 3022645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 3023df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 3024645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 3025df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 3026df8bae1dSRodney W. Grimes */ 30275e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 30284093529dSJeff Roberson returnmask = td->td_oldsigmask; 30295e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 3030df8bae1dSRodney W. Grimes } else 30314093529dSJeff Roberson returnmask = td->td_sigmask; 30322c42a146SMarcel Moolenaar 3033c90c9021SEd Schouten if (p->p_sig == sig) { 30346626c604SJulian Elischer p->p_code = 0; 30356626c604SJulian Elischer p->p_sig = 0; 3036df8bae1dSRodney W. Grimes } 30379104847fSDavid Xu (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3038e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 3039df8bae1dSRodney W. Grimes } 304075c586a4SKonstantin Belousov return (1); 3041df8bae1dSRodney W. Grimes } 3042df8bae1dSRodney W. Grimes 3043df8bae1dSRodney W. Grimes /* 3044df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 3045df8bae1dSRodney W. Grimes */ 304626f9a767SRodney W. Grimes void 3047e052a8b9SEd Maste killproc(struct proc *p, char *why) 3048df8bae1dSRodney W. Grimes { 30499081e5e8SJohn Baldwin 30509081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3051c3209846SPawel Jakub Dawidek CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3052c3209846SPawel Jakub Dawidek p->p_comm); 3053c3209846SPawel Jakub Dawidek log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, 3054c3209846SPawel Jakub Dawidek p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why); 30553f1c4c4fSKonstantin Belousov p->p_flag |= P_WKILLED; 30568451d0ddSKip Macy kern_psignal(p, SIGKILL); 3057df8bae1dSRodney W. Grimes } 3058df8bae1dSRodney W. Grimes 3059df8bae1dSRodney W. Grimes /* 3060df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 3061df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 3062df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 3063df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 3064df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 3065df8bae1dSRodney W. Grimes * does not return. 3066df8bae1dSRodney W. Grimes */ 306726f9a767SRodney W. Grimes void 3068e052a8b9SEd Maste sigexit(struct thread *td, int sig) 3069df8bae1dSRodney W. Grimes { 3070b40ce416SJulian Elischer struct proc *p = td->td_proc; 3071df8bae1dSRodney W. Grimes 3072628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3073df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 3074f97c3df1SDavid Schultz /* 3075f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 3076f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 3077f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 3078f97c3df1SDavid Schultz * change out from under it. 3079f97c3df1SDavid Schultz * 3080f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 3081f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 3082f97c3df1SDavid Schultz */ 3083fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_CORE) && 3084fd50a707SBrooks Davis thread_single(p, SINGLE_NO_EXIT) == 0) { 30852c42a146SMarcel Moolenaar p->p_sig = sig; 3086c364e17eSAndrey A. Chernov /* 3087c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 3088c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 3089c364e17eSAndrey A. Chernov * these messages.) 3090c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 30914ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 3092c364e17eSAndrey A. Chernov */ 3093b40ce416SJulian Elischer if (coredump(td) == 0) 30942c42a146SMarcel Moolenaar sig |= WCOREFLAG; 309557308494SJoerg Wunsch if (kern_logsigexit) 309657308494SJoerg Wunsch log(LOG_INFO, 309757308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 30983d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 30999c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 31002c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 31012c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 31024ae89b95SJohn Baldwin } else 3103628d2653SJohn Baldwin PROC_UNLOCK(p); 3104b4490c6eSKonstantin Belousov exit1(td, 0, sig); 3105df8bae1dSRodney W. Grimes /* NOTREACHED */ 3106df8bae1dSRodney W. Grimes } 3107df8bae1dSRodney W. Grimes 3108ebceaf6dSDavid Xu /* 31097f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 31107f96995eSDavid Xu * is changed. 3111ebceaf6dSDavid Xu */ 31127f96995eSDavid Xu static void 31137f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 3114ebceaf6dSDavid Xu { 3115ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 3116ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3117ebceaf6dSDavid Xu 3118ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 3119ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 3120ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 31217f96995eSDavid Xu p->p_ksi->ksi_status = status; 3122ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 3123ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3124ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 3125ebceaf6dSDavid Xu return; 3126ebceaf6dSDavid Xu } 3127ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3128ebceaf6dSDavid Xu } 3129ebceaf6dSDavid Xu 31307f96995eSDavid Xu static void 3131b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig) 31327f96995eSDavid Xu { 31337f96995eSDavid Xu struct sigacts *ps; 31347f96995eSDavid Xu 31357f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 31367f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 31377f96995eSDavid Xu 31387f96995eSDavid Xu /* 31397f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 31407f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 31417f96995eSDavid Xu * that parent will awake, because parent may masked 31427f96995eSDavid Xu * the signal. 31437f96995eSDavid Xu */ 31447f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 31457f96995eSDavid Xu wakeup(p->p_pptr); 31467f96995eSDavid Xu 31477f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 31487f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 31497f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 31507f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 3151b20a9aa9SJilles Tjoelker sigparent(p, reason, sig); 31527f96995eSDavid Xu } else 31537f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 31547f96995eSDavid Xu } 31557f96995eSDavid Xu 31567f96995eSDavid Xu void 31577f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 31587f96995eSDavid Xu { 3159b4490c6eSKonstantin Belousov 3160b4490c6eSKonstantin Belousov childproc_jobstate(p, reason, p->p_xsig); 31617f96995eSDavid Xu } 31627f96995eSDavid Xu 3163ebceaf6dSDavid Xu void 3164ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3165ebceaf6dSDavid Xu { 31667f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3167ebceaf6dSDavid Xu } 3168ebceaf6dSDavid Xu 3169ebceaf6dSDavid Xu void 3170ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3171ebceaf6dSDavid Xu { 3172b4490c6eSKonstantin Belousov int reason, status; 3173ebceaf6dSDavid Xu 3174b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 3175b4490c6eSKonstantin Belousov reason = CLD_DUMPED; 3176b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3177b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 3178b4490c6eSKonstantin Belousov reason = CLD_KILLED; 3179b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3180b4490c6eSKonstantin Belousov } else { 3181b4490c6eSKonstantin Belousov reason = CLD_EXITED; 3182b4490c6eSKonstantin Belousov status = p->p_xexit; 3183b4490c6eSKonstantin Belousov } 31847f96995eSDavid Xu /* 31857f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 31867f96995eSDavid Xu * done in exit1(). 31877f96995eSDavid Xu */ 31887f96995eSDavid Xu sigparent(p, reason, status); 3189ebceaf6dSDavid Xu } 3190ebceaf6dSDavid Xu 3191e7228204SAlfred Perlstein /* 3192e7228204SAlfred Perlstein * We only have 1 character for the core count in the format 3193e7228204SAlfred Perlstein * string, so the range will be 0-9 3194e7228204SAlfred Perlstein */ 3195cc37baeaSStephen J. Kiernan #define MAX_NUM_CORE_FILES 10 3196cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES 3197cc37baeaSStephen J. Kiernan #define NUM_CORE_FILES 5 3198cc37baeaSStephen J. Kiernan #endif 3199cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3200cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES; 3201e7228204SAlfred Perlstein 3202e7228204SAlfred Perlstein static int 3203e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3204e7228204SAlfred Perlstein { 3205e7228204SAlfred Perlstein int error; 3206e7228204SAlfred Perlstein int new_val; 3207e7228204SAlfred Perlstein 3208c5105012SKonstantin Belousov new_val = num_cores; 3209e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3210e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3211e7228204SAlfred Perlstein return (error); 3212cc37baeaSStephen J. Kiernan if (new_val > MAX_NUM_CORE_FILES) 3213cc37baeaSStephen J. Kiernan new_val = MAX_NUM_CORE_FILES; 3214e7228204SAlfred Perlstein if (new_val < 0) 3215e7228204SAlfred Perlstein new_val = 0; 3216e7228204SAlfred Perlstein num_cores = new_val; 3217e7228204SAlfred Perlstein return (0); 3218e7228204SAlfred Perlstein } 3219e7228204SAlfred Perlstein SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW, 3220e7228204SAlfred Perlstein 0, sizeof(int), sysctl_debug_num_cores_check, "I", ""); 3221e7228204SAlfred Perlstein 3222aa14e9b7SMark Johnston #define GZ_SUFFIX ".gz" 3223aa14e9b7SMark Johnston 3224aa14e9b7SMark Johnston #ifdef GZIO 3225aa14e9b7SMark Johnston static int compress_user_cores = 1; 3226aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RWTUN, 322729146f1aSPawel Jakub Dawidek &compress_user_cores, 0, "Compression of user corefiles"); 3228e7228204SAlfred Perlstein 3229aa14e9b7SMark Johnston int compress_user_cores_gzlevel = 6; 3230aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN, 3231aa14e9b7SMark Johnston &compress_user_cores_gzlevel, 0, "Corefile gzip compression level"); 3232aa14e9b7SMark Johnston #else 3233aa14e9b7SMark Johnston static int compress_user_cores = 0; 3234e7228204SAlfred Perlstein #endif 3235e7228204SAlfred Perlstein 32365bc0ff88SMateusz Guzik /* 32375bc0ff88SMateusz Guzik * Protect the access to corefilename[] by allproc_lock. 32385bc0ff88SMateusz Guzik */ 32395bc0ff88SMateusz Guzik #define corefilename_lock allproc_lock 32405bc0ff88SMateusz Guzik 32416d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3242fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 32435bc0ff88SMateusz Guzik 32445bc0ff88SMateusz Guzik static int 32455bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 32465bc0ff88SMateusz Guzik { 32475bc0ff88SMateusz Guzik int error; 32485bc0ff88SMateusz Guzik 32495bc0ff88SMateusz Guzik sx_xlock(&corefilename_lock); 32505bc0ff88SMateusz Guzik error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 32515bc0ff88SMateusz Guzik req); 32525bc0ff88SMateusz Guzik sx_xunlock(&corefilename_lock); 32535bc0ff88SMateusz Guzik 32545bc0ff88SMateusz Guzik return (error); 32555bc0ff88SMateusz Guzik } 3256fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 32575bc0ff88SMateusz Guzik CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 32585bc0ff88SMateusz Guzik "Process corefile name format string"); 3259c5edb423SSean Eric Fagan 3260c5edb423SSean Eric Fagan /* 3261c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3262c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3263c345faeaSPawel Jakub Dawidek * and open/create core file. 3264c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3265c5edb423SSean Eric Fagan * %N name of process ("name") 3266c5edb423SSean Eric Fagan * %P process id (pid) 3267c5edb423SSean Eric Fagan * %U user id (uid) 3268c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3269c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3270c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3271c5edb423SSean Eric Fagan */ 3272c345faeaSPawel Jakub Dawidek static int 3273c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 3274c345faeaSPawel Jakub Dawidek int compress, struct vnode **vpp, char **namep) 32758b43b535SAlfred Perlstein { 3276c345faeaSPawel Jakub Dawidek struct nameidata nd; 327736b208e0SRobert Watson struct sbuf sb; 327836b208e0SRobert Watson const char *format; 3279c345faeaSPawel Jakub Dawidek char *hostname, *name; 3280c345faeaSPawel Jakub Dawidek int indexpos, i, error, cmode, flags, oflags; 3281c5edb423SSean Eric Fagan 3282b7402d82SAlfred Perlstein hostname = NULL; 32838b43b535SAlfred Perlstein format = corefilename; 3284086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3285e7228204SAlfred Perlstein indexpos = -1; 3286c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 32875bc0ff88SMateusz Guzik sx_slock(&corefilename_lock); 328829146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3289c5edb423SSean Eric Fagan switch (format[i]) { 3290c5edb423SSean Eric Fagan case '%': /* Format character */ 3291c5edb423SSean Eric Fagan i++; 3292c5edb423SSean Eric Fagan switch (format[i]) { 3293c5edb423SSean Eric Fagan case '%': 329436b208e0SRobert Watson sbuf_putc(&sb, '%'); 3295c5edb423SSean Eric Fagan break; 3296e7228204SAlfred Perlstein case 'H': /* hostname */ 3297b7402d82SAlfred Perlstein if (hostname == NULL) { 3298b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3299086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3300b7402d82SAlfred Perlstein } 3301e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3302b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3303e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3304e7228204SAlfred Perlstein break; 3305e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3306e7228204SAlfred Perlstein sbuf_printf(&sb, "0"); 3307e7228204SAlfred Perlstein indexpos = sbuf_len(&sb) - 1; 3308e7228204SAlfred Perlstein break; 3309c5edb423SSean Eric Fagan case 'N': /* process name */ 3310c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3311c5edb423SSean Eric Fagan break; 3312c5edb423SSean Eric Fagan case 'P': /* process id */ 331336b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3314c5edb423SSean Eric Fagan break; 3315c5edb423SSean Eric Fagan case 'U': /* user id */ 331636b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3317c5edb423SSean Eric Fagan break; 3318c5edb423SSean Eric Fagan default: 33198b43b535SAlfred Perlstein log(LOG_ERR, 332036b208e0SRobert Watson "Unknown format character %c in " 332136b208e0SRobert Watson "corename `%s'\n", format[i], format); 332229146f1aSPawel Jakub Dawidek break; 3323c5edb423SSean Eric Fagan } 3324c5edb423SSean Eric Fagan break; 3325c5edb423SSean Eric Fagan default: 332636b208e0SRobert Watson sbuf_putc(&sb, format[i]); 33276c08be2bSPawel Jakub Dawidek break; 3328c5edb423SSean Eric Fagan } 3329c5edb423SSean Eric Fagan } 33305bc0ff88SMateusz Guzik sx_sunlock(&corefilename_lock); 3331b7402d82SAlfred Perlstein free(hostname, M_TEMP); 3332f06f465dSPawel Jakub Dawidek if (compress) 3333e7228204SAlfred Perlstein sbuf_printf(&sb, GZ_SUFFIX); 33344d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 333536b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3336c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 3337b7402d82SAlfred Perlstein sbuf_delete(&sb); 3338c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3339c345faeaSPawel Jakub Dawidek return (ENOMEM); 3340c5edb423SSean Eric Fagan } 334136b208e0SRobert Watson sbuf_finish(&sb); 334236b208e0SRobert Watson sbuf_delete(&sb); 3343e7228204SAlfred Perlstein 3344c345faeaSPawel Jakub Dawidek cmode = S_IRUSR | S_IWUSR; 33458ee9765aSKonstantin Belousov oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 33468ee9765aSKonstantin Belousov (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 3347c345faeaSPawel Jakub Dawidek 3348e7228204SAlfred Perlstein /* 3349e7228204SAlfred Perlstein * If the core format has a %I in it, then we need to check 3350e7228204SAlfred Perlstein * for existing corefiles before returning a name. 3351e7228204SAlfred Perlstein * To do this we iterate over 0..num_cores to find a 3352e7228204SAlfred Perlstein * non-existing core file name to use. 3353e7228204SAlfred Perlstein */ 3354e7228204SAlfred Perlstein if (indexpos != -1) { 335529146f1aSPawel Jakub Dawidek for (i = 0; i < num_cores; i++) { 335607a8e078SPawel Jakub Dawidek flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW; 335729146f1aSPawel Jakub Dawidek name[indexpos] = '0' + i; 3358c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3359b0c9d4d7SPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, 3360b0c9d4d7SPawel Jakub Dawidek td->td_ucred, NULL); 3361e7228204SAlfred Perlstein if (error) { 336223c6445aSPawel Jakub Dawidek if (error == EEXIST) 3363e7228204SAlfred Perlstein continue; 3364e7228204SAlfred Perlstein log(LOG_ERR, 3365e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 3366e7228204SAlfred Perlstein "on initial open test, error = %d\n", 3367c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 3368c345faeaSPawel Jakub Dawidek } 3369c345faeaSPawel Jakub Dawidek goto out; 3370c345faeaSPawel Jakub Dawidek } 3371c345faeaSPawel Jakub Dawidek } 3372c345faeaSPawel Jakub Dawidek 3373c345faeaSPawel Jakub Dawidek flags = O_CREAT | FWRITE | O_NOFOLLOW; 3374c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3375c345faeaSPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL); 3376c345faeaSPawel Jakub Dawidek out: 3377c345faeaSPawel Jakub Dawidek if (error) { 3378c345faeaSPawel Jakub Dawidek #ifdef AUDIT 3379c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 3380c345faeaSPawel Jakub Dawidek #endif 3381c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3382c345faeaSPawel Jakub Dawidek return (error); 3383e7228204SAlfred Perlstein } 3384e7228204SAlfred Perlstein NDFREE(&nd, NDF_ONLY_PNBUF); 3385c345faeaSPawel Jakub Dawidek *vpp = nd.ni_vp; 3386c345faeaSPawel Jakub Dawidek *namep = name; 3387c345faeaSPawel Jakub Dawidek return (0); 338836b208e0SRobert Watson } 3389c5edb423SSean Eric Fagan 3390eb6368d4SRui Paulo static int 3391eb6368d4SRui Paulo coredump_sanitise_path(const char *path) 3392eb6368d4SRui Paulo { 3393624157bbSRui Paulo size_t i; 3394eb6368d4SRui Paulo 3395eb6368d4SRui Paulo /* 3396eb6368d4SRui Paulo * Only send a subset of ASCII to devd(8) because it 3397eb6368d4SRui Paulo * might pass these strings to sh -c. 3398eb6368d4SRui Paulo */ 3399624157bbSRui Paulo for (i = 0; path[i]; i++) 3400eb6368d4SRui Paulo if (!(isalpha(path[i]) || isdigit(path[i])) && 3401eb6368d4SRui Paulo path[i] != '/' && path[i] != '.' && 3402eb6368d4SRui Paulo path[i] != '-') 3403eb6368d4SRui Paulo return (0); 3404eb6368d4SRui Paulo 3405eb6368d4SRui Paulo return (1); 3406eb6368d4SRui Paulo } 3407eb6368d4SRui Paulo 3408df8bae1dSRodney W. Grimes /* 3409fca666a1SJulian Elischer * Dump a process' core. The main routine does some 3410fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 3411fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 3412fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 3413fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 3414fca666a1SJulian Elischer */ 3415fca666a1SJulian Elischer 3416fca666a1SJulian Elischer static int 3417b40ce416SJulian Elischer coredump(struct thread *td) 3418fca666a1SJulian Elischer { 3419b40ce416SJulian Elischer struct proc *p = td->td_proc; 3420f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 3421f06f465dSPawel Jakub Dawidek struct vnode *vp; 342206ae1e91SMatthew Dillon struct flock lf; 3423fca666a1SJulian Elischer struct vattr vattr; 3424c345faeaSPawel Jakub Dawidek int error, error1, locked; 3425fca666a1SJulian Elischer char *name; /* name of corefile */ 3426539c9eefSKonstantin Belousov void *rl_cookie; 3427fca666a1SJulian Elischer off_t limit; 34286fbc0f7dSRui Paulo char *data = NULL; 3429842ab62bSRui Paulo char *fullpath, *freepath = NULL; 3430eb6368d4SRui Paulo size_t len; 34316fbc0f7dSRui Paulo static const char comm_name[] = "comm="; 34326fbc0f7dSRui Paulo static const char core_name[] = "core="; 3433fca666a1SJulian Elischer 34344ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3435f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3436628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 3437fca666a1SJulian Elischer 3438677258f7SKonstantin Belousov if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 3439677258f7SKonstantin Belousov (p->p_flag2 & P2_NOTRACE) != 0) { 3440628d2653SJohn Baldwin PROC_UNLOCK(p); 3441fca666a1SJulian Elischer return (EFAULT); 3442628d2653SJohn Baldwin } 3443fca666a1SJulian Elischer 3444fca666a1SJulian Elischer /* 344535a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 344635a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 344735a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 344835a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 344935a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 345035a2598fSSean Eric Fagan * if it is larger than the limit. 3451fca666a1SJulian Elischer */ 3452f6f6d240SMateusz Guzik limit = (off_t)lim_cur(td, RLIMIT_CORE); 3453b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3454628d2653SJohn Baldwin PROC_UNLOCK(p); 345591d5354aSJohn Baldwin return (EFBIG); 345657274c51SChristian S.J. Peron } 3457b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 345835a2598fSSean Eric Fagan 3459aa14e9b7SMark Johnston error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 3460aa14e9b7SMark Johnston compress_user_cores, &vp, &name); 3461c345faeaSPawel Jakub Dawidek if (error != 0) 3462fca666a1SJulian Elischer return (error); 346306ae1e91SMatthew Dillon 3464539c9eefSKonstantin Belousov /* 3465539c9eefSKonstantin Belousov * Don't dump to non-regular files or files with links. 3466539c9eefSKonstantin Belousov * Do not dump into system files. 3467539c9eefSKonstantin Belousov */ 3468f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 3469539c9eefSKonstantin Belousov vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0) { 347022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3471832dafadSDon Lewis error = EFAULT; 3472dacbc9dbSKonstantin Belousov goto out; 3473832dafadSDon Lewis } 3474832dafadSDon Lewis 347522db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3476539c9eefSKonstantin Belousov 3477539c9eefSKonstantin Belousov /* Postpone other writers, including core dumps of other processes. */ 3478539c9eefSKonstantin Belousov rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3479539c9eefSKonstantin Belousov 348006ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 348106ae1e91SMatthew Dillon lf.l_start = 0; 348206ae1e91SMatthew Dillon lf.l_len = 0; 348306ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 3484c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 348506ae1e91SMatthew Dillon 3486fca666a1SJulian Elischer VATTR_NULL(&vattr); 3487fca666a1SJulian Elischer vattr.va_size = 0; 34886141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 34896141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 3490cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 34910359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 349222db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3493628d2653SJohn Baldwin PROC_LOCK(p); 3494fca666a1SJulian Elischer p->p_acflag |= ACORE; 3495628d2653SJohn Baldwin PROC_UNLOCK(p); 3496fca666a1SJulian Elischer 349723c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 349823c6445aSPawel Jakub Dawidek error = p->p_sysent->sv_coredump(td, vp, limit, 3499aa14e9b7SMark Johnston compress_user_cores ? IMGACT_CORE_COMPRESS : 0); 350023c6445aSPawel Jakub Dawidek } else { 350123c6445aSPawel Jakub Dawidek error = ENOSYS; 350223c6445aSPawel Jakub Dawidek } 3503fca666a1SJulian Elischer 3504c447f5b2SRobert Watson if (locked) { 350506ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 350606ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3507c447f5b2SRobert Watson } 3508539c9eefSKonstantin Belousov vn_rangelock_unlock(vp, rl_cookie); 3509dacbc9dbSKonstantin Belousov 3510842ab62bSRui Paulo /* 3511842ab62bSRui Paulo * Notify the userland helper that a process triggered a core dump. 3512842ab62bSRui Paulo * This allows the helper to run an automated debugging session. 3513842ab62bSRui Paulo */ 3514dacbc9dbSKonstantin Belousov if (error != 0 || coredump_devctl == 0) 3515842ab62bSRui Paulo goto out; 35166fbc0f7dSRui Paulo len = MAXPATHLEN * 2 + sizeof(comm_name) - 1 + 35176fbc0f7dSRui Paulo sizeof(' ') + sizeof(core_name) - 1; 35186fbc0f7dSRui Paulo data = malloc(len, M_TEMP, M_WAITOK); 3519842ab62bSRui Paulo if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0) 3520842ab62bSRui Paulo goto out; 3521eb6368d4SRui Paulo if (!coredump_sanitise_path(fullpath)) 3522eb6368d4SRui Paulo goto out; 35236fbc0f7dSRui Paulo snprintf(data, len, "%s%s ", comm_name, fullpath); 3524842ab62bSRui Paulo free(freepath, M_TEMP); 3525842ab62bSRui Paulo freepath = NULL; 3526624157bbSRui Paulo if (vn_fullpath_global(td, vp, &fullpath, &freepath) != 0) 3527842ab62bSRui Paulo goto out; 3528eb6368d4SRui Paulo if (!coredump_sanitise_path(fullpath)) 3529eb6368d4SRui Paulo goto out; 35306fbc0f7dSRui Paulo strlcat(data, core_name, len); 35316fbc0f7dSRui Paulo strlcat(data, fullpath, len); 3532842ab62bSRui Paulo devctl_notify("kernel", "signal", "coredump", data); 3533842ab62bSRui Paulo out: 3534dacbc9dbSKonstantin Belousov error1 = vn_close(vp, FWRITE, cred, td); 3535dacbc9dbSKonstantin Belousov if (error == 0) 3536dacbc9dbSKonstantin Belousov error = error1; 353757274c51SChristian S.J. Peron #ifdef AUDIT 353857274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 353957274c51SChristian S.J. Peron #endif 3540842ab62bSRui Paulo free(freepath, M_TEMP); 35416fbc0f7dSRui Paulo free(data, M_TEMP); 354257274c51SChristian S.J. Peron free(name, M_TEMP); 3543fca666a1SJulian Elischer return (error); 3544fca666a1SJulian Elischer } 3545fca666a1SJulian Elischer 3546fca666a1SJulian Elischer /* 35470c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 35480c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 3549df8bae1dSRodney W. Grimes */ 3550d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 3551df8bae1dSRodney W. Grimes struct nosys_args { 3552df8bae1dSRodney W. Grimes int dummy; 3553df8bae1dSRodney W. Grimes }; 3554d2d3e875SBruce Evans #endif 3555df8bae1dSRodney W. Grimes /* ARGSUSED */ 355626f9a767SRodney W. Grimes int 3557e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args) 3558df8bae1dSRodney W. Grimes { 3559b40ce416SJulian Elischer struct proc *p = td->td_proc; 3560b40ce416SJulian Elischer 3561628d2653SJohn Baldwin PROC_LOCK(p); 3562888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 3563628d2653SJohn Baldwin PROC_UNLOCK(p); 3564f5216b9aSBruce Evans return (ENOSYS); 3565df8bae1dSRodney W. Grimes } 3566831d27a9SDon Lewis 3567831d27a9SDon Lewis /* 35680c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 35690c14ff0eSRobert Watson * credentials rather than those of the current process. 3570831d27a9SDon Lewis */ 3571831d27a9SDon Lewis void 3572e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty) 3573831d27a9SDon Lewis { 3574a3de221dSKonstantin Belousov ksiginfo_t ksi; 3575f1320723SAlfred Perlstein struct sigio *sigio; 3576831d27a9SDon Lewis 3577a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 3578a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 3579a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 3580a3de221dSKonstantin Belousov 3581f1320723SAlfred Perlstein SIGIO_LOCK(); 3582f1320723SAlfred Perlstein sigio = *sigiop; 3583f1320723SAlfred Perlstein if (sigio == NULL) { 3584f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3585f1320723SAlfred Perlstein return; 3586f1320723SAlfred Perlstein } 3587831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 3588628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 35892b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 35908451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 3591628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 3592831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 3593831d27a9SDon Lewis struct proc *p; 3594831d27a9SDon Lewis 3595f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 3596628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3597628d2653SJohn Baldwin PROC_LOCK(p); 3598e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 3599e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3600831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 36018451d0ddSKip Macy kern_psignal(p, sig); 3602628d2653SJohn Baldwin PROC_UNLOCK(p); 3603628d2653SJohn Baldwin } 3604f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 3605831d27a9SDon Lewis } 3606f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3607831d27a9SDon Lewis } 3608cb679c38SJonathan Lemon 3609cb679c38SJonathan Lemon static int 3610cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 3611cb679c38SJonathan Lemon { 3612cb679c38SJonathan Lemon struct proc *p = curproc; 3613cb679c38SJonathan Lemon 3614cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 3615cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 3616cb679c38SJonathan Lemon 36179e590ff0SKonstantin Belousov knlist_add(p->p_klist, kn, 0); 3618cb679c38SJonathan Lemon 3619cb679c38SJonathan Lemon return (0); 3620cb679c38SJonathan Lemon } 3621cb679c38SJonathan Lemon 3622cb679c38SJonathan Lemon static void 3623cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 3624cb679c38SJonathan Lemon { 3625cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 3626cb679c38SJonathan Lemon 36279e590ff0SKonstantin Belousov knlist_remove(p->p_klist, kn, 0); 3628cb679c38SJonathan Lemon } 3629cb679c38SJonathan Lemon 3630cb679c38SJonathan Lemon /* 3631cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 3632cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 3633cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 3634cb679c38SJonathan Lemon * isn't worth the trouble. 3635cb679c38SJonathan Lemon */ 3636cb679c38SJonathan Lemon static int 3637cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 3638cb679c38SJonathan Lemon { 3639cb679c38SJonathan Lemon 3640cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 3641cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 3642cb679c38SJonathan Lemon 3643cb679c38SJonathan Lemon if (kn->kn_id == hint) 3644cb679c38SJonathan Lemon kn->kn_data++; 3645cb679c38SJonathan Lemon } 3646cb679c38SJonathan Lemon return (kn->kn_data != 0); 3647cb679c38SJonathan Lemon } 364890af4afaSJohn Baldwin 364990af4afaSJohn Baldwin struct sigacts * 365090af4afaSJohn Baldwin sigacts_alloc(void) 365190af4afaSJohn Baldwin { 365290af4afaSJohn Baldwin struct sigacts *ps; 365390af4afaSJohn Baldwin 365490af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3655ce8daaadSMateusz Guzik refcount_init(&ps->ps_refcnt, 1); 365690af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 365790af4afaSJohn Baldwin return (ps); 365890af4afaSJohn Baldwin } 365990af4afaSJohn Baldwin 366090af4afaSJohn Baldwin void 366190af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 366290af4afaSJohn Baldwin { 366390af4afaSJohn Baldwin 3664c959c237SMateusz Guzik if (refcount_release(&ps->ps_refcnt) == 0) 3665c959c237SMateusz Guzik return; 366690af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 366790af4afaSJohn Baldwin free(ps, M_SUBPROC); 366890af4afaSJohn Baldwin } 366990af4afaSJohn Baldwin 367090af4afaSJohn Baldwin struct sigacts * 367190af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 367290af4afaSJohn Baldwin { 3673c959c237SMateusz Guzik 3674c959c237SMateusz Guzik refcount_acquire(&ps->ps_refcnt); 367590af4afaSJohn Baldwin return (ps); 367690af4afaSJohn Baldwin } 367790af4afaSJohn Baldwin 367890af4afaSJohn Baldwin void 367990af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 368090af4afaSJohn Baldwin { 368190af4afaSJohn Baldwin 368290af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 368390af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 368490af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 368590af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 368690af4afaSJohn Baldwin } 368790af4afaSJohn Baldwin 368890af4afaSJohn Baldwin int 368990af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 369090af4afaSJohn Baldwin { 369190af4afaSJohn Baldwin 3692d00c8ea4SMateusz Guzik return (ps->ps_refcnt > 1); 369390af4afaSJohn Baldwin } 3694