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 153*f5a077c3SKonstantin Belousov static int kern_lognosys = 0; 154*f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0, 155*f5a077c3SKonstantin Belousov "Log invalid syscalls"); 156*f5a077c3SKonstantin Belousov 1579104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 1589104847fSDavid Xu 1592b87b6d4SRobert Watson /* 1602b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1612b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1622b87b6d4SRobert Watson * in the right situations. 1632b87b6d4SRobert Watson */ 1642b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1652b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1662b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1672b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1682b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1692b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1702b87b6d4SRobert Watson 171fc8cca02SJohn Baldwin static int sugid_coredump; 172af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN, 1738b5adf9dSJoseph Koshy &sugid_coredump, 0, "Allow setuid and setgid processes to dump core"); 174c87e2930SDavid Greenman 175b0c9d4d7SPawel Jakub Dawidek static int capmode_coredump; 176af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN, 177b0c9d4d7SPawel Jakub Dawidek &capmode_coredump, 0, "Allow processes in capability mode to dump core"); 178b0c9d4d7SPawel Jakub Dawidek 179e5a28db9SPaul Saab static int do_coredump = 1; 180e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 181e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 182e5a28db9SPaul Saab 1836141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 1846141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 1856141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 1866141e04aSJohn-Mark Gurney 1870da9e11bSRui Paulo static int coredump_devctl = 0; 188eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl, 189eb6368d4SRui Paulo 0, "Generate a devctl notification when processes coredump"); 190eb6368d4SRui Paulo 1912c42a146SMarcel Moolenaar /* 1922c42a146SMarcel Moolenaar * Signal properties and actions. 1932c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 1942c42a146SMarcel Moolenaar * according to the following properties: 1952c42a146SMarcel Moolenaar */ 196fd50a707SBrooks Davis #define SIGPROP_KILL 0x01 /* terminates process by default */ 197fd50a707SBrooks Davis #define SIGPROP_CORE 0x02 /* ditto and coredumps */ 198fd50a707SBrooks Davis #define SIGPROP_STOP 0x04 /* suspend process */ 199fd50a707SBrooks Davis #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */ 200fd50a707SBrooks Davis #define SIGPROP_IGNORE 0x10 /* ignore by default */ 201fd50a707SBrooks Davis #define SIGPROP_CONT 0x20 /* continue if suspended */ 202fd50a707SBrooks Davis #define SIGPROP_CANTMASK 0x40 /* non-maskable, catchable */ 203df8bae1dSRodney W. Grimes 2042c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 205ed6d876bSBrooks Davis [SIGHUP] = SIGPROP_KILL, 206ed6d876bSBrooks Davis [SIGINT] = SIGPROP_KILL, 207ed6d876bSBrooks Davis [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE, 208ed6d876bSBrooks Davis [SIGILL] = SIGPROP_KILL | SIGPROP_CORE, 209ed6d876bSBrooks Davis [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE, 210ed6d876bSBrooks Davis [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE, 211ed6d876bSBrooks Davis [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE, 212ed6d876bSBrooks Davis [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE, 213ed6d876bSBrooks Davis [SIGKILL] = SIGPROP_KILL, 214ed6d876bSBrooks Davis [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE, 215ed6d876bSBrooks Davis [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE, 216ed6d876bSBrooks Davis [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE, 217ed6d876bSBrooks Davis [SIGPIPE] = SIGPROP_KILL, 218ed6d876bSBrooks Davis [SIGALRM] = SIGPROP_KILL, 219ed6d876bSBrooks Davis [SIGTERM] = SIGPROP_KILL, 220ed6d876bSBrooks Davis [SIGURG] = SIGPROP_IGNORE, 221ed6d876bSBrooks Davis [SIGSTOP] = SIGPROP_STOP, 222ed6d876bSBrooks Davis [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP, 223ed6d876bSBrooks Davis [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT, 224ed6d876bSBrooks Davis [SIGCHLD] = SIGPROP_IGNORE, 225ed6d876bSBrooks Davis [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP, 226ed6d876bSBrooks Davis [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP, 227ed6d876bSBrooks Davis [SIGIO] = SIGPROP_IGNORE, 228ed6d876bSBrooks Davis [SIGXCPU] = SIGPROP_KILL, 229ed6d876bSBrooks Davis [SIGXFSZ] = SIGPROP_KILL, 230ed6d876bSBrooks Davis [SIGVTALRM] = SIGPROP_KILL, 231ed6d876bSBrooks Davis [SIGPROF] = SIGPROP_KILL, 232ed6d876bSBrooks Davis [SIGWINCH] = SIGPROP_IGNORE, 233ed6d876bSBrooks Davis [SIGINFO] = SIGPROP_IGNORE, 234ed6d876bSBrooks Davis [SIGUSR1] = SIGPROP_KILL, 235ed6d876bSBrooks Davis [SIGUSR2] = SIGPROP_KILL, 2362c42a146SMarcel Moolenaar }; 2372c42a146SMarcel Moolenaar 23880a8b0f3SKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags); 2396b286ee8SKonstantin Belousov 2409104847fSDavid Xu static void 2419104847fSDavid Xu sigqueue_start(void) 2429104847fSDavid Xu { 2439104847fSDavid Xu ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 2449104847fSDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2459104847fSDavid Xu uma_prealloc(ksiginfo_zone, preallocate_siginfo); 246b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 247b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 248b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 2499104847fSDavid Xu } 2509104847fSDavid Xu 2515da49fcbSDavid Xu ksiginfo_t * 252ebceaf6dSDavid Xu ksiginfo_alloc(int wait) 2539104847fSDavid Xu { 254ebceaf6dSDavid Xu int flags; 255ebceaf6dSDavid Xu 256ebceaf6dSDavid Xu flags = M_ZERO; 257ebceaf6dSDavid Xu if (! wait) 258ebceaf6dSDavid Xu flags |= M_NOWAIT; 2599104847fSDavid Xu if (ksiginfo_zone != NULL) 260ebceaf6dSDavid Xu return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 2619104847fSDavid Xu return (NULL); 2629104847fSDavid Xu } 2639104847fSDavid Xu 2645da49fcbSDavid Xu void 2659104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 2669104847fSDavid Xu { 2679104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2689104847fSDavid Xu } 2699104847fSDavid Xu 2705da49fcbSDavid Xu static __inline int 2715da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 2725da49fcbSDavid Xu { 2735da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT)) { 2745da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 2755da49fcbSDavid Xu return (1); 2765da49fcbSDavid Xu } 2775da49fcbSDavid Xu return (0); 2785da49fcbSDavid Xu } 2795da49fcbSDavid Xu 2809104847fSDavid Xu void 2819104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 2829104847fSDavid Xu { 2839104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 2843dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 28582a4538fSEric Badger SIGEMPTYSET(list->sq_ptrace); 2869104847fSDavid Xu TAILQ_INIT(&list->sq_list); 2879104847fSDavid Xu list->sq_proc = p; 2889104847fSDavid Xu list->sq_flags = SQ_INIT; 2899104847fSDavid Xu } 2909104847fSDavid Xu 2919104847fSDavid Xu /* 2929104847fSDavid Xu * Get a signal's ksiginfo. 2939104847fSDavid Xu * Return: 2949104847fSDavid Xu * 0 - signal not found 2959104847fSDavid Xu * others - signal number 2969104847fSDavid Xu */ 297a5799a4fSKonstantin Belousov static int 2989104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 2999104847fSDavid Xu { 3009104847fSDavid Xu struct proc *p = sq->sq_proc; 3019104847fSDavid Xu struct ksiginfo *ksi, *next; 3029104847fSDavid Xu int count = 0; 3039104847fSDavid Xu 3049104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3059104847fSDavid Xu 3069104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 3079104847fSDavid Xu return (0); 3089104847fSDavid Xu 30982a4538fSEric Badger if (SIGISMEMBER(sq->sq_ptrace, signo)) { 31082a4538fSEric Badger count++; 31182a4538fSEric Badger SIGDELSET(sq->sq_ptrace, signo); 31282a4538fSEric Badger si->ksi_flags |= KSI_PTRACE; 31382a4538fSEric Badger } 3143dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 3153dfcaad6SDavid Xu count++; 31682a4538fSEric Badger if (count == 1) 3173dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 3183dfcaad6SDavid Xu } 3193dfcaad6SDavid Xu 3205c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 3219104847fSDavid Xu if (ksi->ksi_signo == signo) { 3229104847fSDavid Xu if (count == 0) { 3239104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3245da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3259104847fSDavid Xu ksiginfo_copy(ksi, si); 3265da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 3279104847fSDavid Xu p->p_pendingcnt--; 3289104847fSDavid Xu } 329016fa302SDavid Xu if (++count > 1) 330016fa302SDavid Xu break; 3319104847fSDavid Xu } 3329104847fSDavid Xu } 3339104847fSDavid Xu 3349104847fSDavid Xu if (count <= 1) 3359104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 3369104847fSDavid Xu si->ksi_signo = signo; 3379104847fSDavid Xu return (signo); 3389104847fSDavid Xu } 3399104847fSDavid Xu 3405da49fcbSDavid Xu void 3415da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 3425da49fcbSDavid Xu { 3435da49fcbSDavid Xu struct ksiginfo *kp; 3445da49fcbSDavid Xu struct proc *p; 3455da49fcbSDavid Xu sigqueue_t *sq; 3465da49fcbSDavid Xu 347ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 3485da49fcbSDavid Xu return; 3495da49fcbSDavid Xu 3505da49fcbSDavid Xu p = sq->sq_proc; 3515da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3525da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3535da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 3545da49fcbSDavid Xu p->p_pendingcnt--; 3555da49fcbSDavid Xu 3565da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 3575da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 3585da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 3595da49fcbSDavid Xu break; 3605da49fcbSDavid Xu } 36182a4538fSEric Badger if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 36282a4538fSEric Badger !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 3635da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 3645da49fcbSDavid Xu } 3655da49fcbSDavid Xu 366a5799a4fSKonstantin Belousov static int 3679104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 3689104847fSDavid Xu { 3699104847fSDavid Xu struct proc *p = sq->sq_proc; 3709104847fSDavid Xu struct ksiginfo *ksi; 3719104847fSDavid Xu int ret = 0; 3729104847fSDavid Xu 3739104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3749104847fSDavid Xu 37582a4538fSEric Badger /* 37682a4538fSEric Badger * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 37782a4538fSEric Badger * for these signals. 37882a4538fSEric Badger */ 3793dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 3803dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3819104847fSDavid Xu goto out_set_bit; 3823dfcaad6SDavid Xu } 3839104847fSDavid Xu 3845da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 3855da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 3866a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 3876a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 3886a671a6bSKonstantin Belousov else 3895da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 3905da49fcbSDavid Xu si->ksi_sigq = sq; 3915da49fcbSDavid Xu goto out_set_bit; 3925da49fcbSDavid Xu } 3935da49fcbSDavid Xu 3943dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 3953dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 3969104847fSDavid Xu goto out_set_bit; 3973dfcaad6SDavid Xu } 3989104847fSDavid Xu 399ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 4009104847fSDavid Xu signal_overflow++; 4019104847fSDavid Xu ret = EAGAIN; 402ebceaf6dSDavid Xu } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 4039104847fSDavid Xu signal_alloc_fail++; 4049104847fSDavid Xu ret = EAGAIN; 4059104847fSDavid Xu } else { 4069104847fSDavid Xu if (p != NULL) 4079104847fSDavid Xu p->p_pendingcnt++; 4089104847fSDavid Xu ksiginfo_copy(si, ksi); 4099104847fSDavid Xu ksi->ksi_signo = signo; 4106a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4116a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 4126a671a6bSKonstantin Belousov else 4139104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 4145da49fcbSDavid Xu ksi->ksi_sigq = sq; 4159104847fSDavid Xu } 4169104847fSDavid Xu 41782a4538fSEric Badger if (ret != 0) { 41882a4538fSEric Badger if ((si->ksi_flags & KSI_PTRACE) != 0) { 41982a4538fSEric Badger SIGADDSET(sq->sq_ptrace, signo); 42082a4538fSEric Badger ret = 0; 42182a4538fSEric Badger goto out_set_bit; 42282a4538fSEric Badger } else if ((si->ksi_flags & KSI_TRAP) != 0 || 423a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 4243dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4259104847fSDavid Xu ret = 0; 4269104847fSDavid Xu goto out_set_bit; 4279104847fSDavid Xu } 4289104847fSDavid Xu return (ret); 42982a4538fSEric Badger } 4309104847fSDavid Xu 4319104847fSDavid Xu out_set_bit: 4329104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 4339104847fSDavid Xu return (ret); 4349104847fSDavid Xu } 4359104847fSDavid Xu 4369104847fSDavid Xu void 4379104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 4389104847fSDavid Xu { 4399104847fSDavid Xu struct proc *p = sq->sq_proc; 4409104847fSDavid Xu ksiginfo_t *ksi; 4419104847fSDavid Xu 4429104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4439104847fSDavid Xu 4445da49fcbSDavid Xu if (p != NULL) 4455da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 4465da49fcbSDavid Xu 4479104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 4489104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4495da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4505da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4519104847fSDavid Xu p->p_pendingcnt--; 4529104847fSDavid Xu } 4539104847fSDavid Xu 4549104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 4553dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 45682a4538fSEric Badger SIGEMPTYSET(sq->sq_ptrace); 4579104847fSDavid Xu } 4589104847fSDavid Xu 459a5799a4fSKonstantin Belousov static void 460fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 4619104847fSDavid Xu { 462fc4ecc1dSDavid Xu sigset_t tmp; 4639104847fSDavid Xu struct proc *p1, *p2; 4649104847fSDavid Xu ksiginfo_t *ksi, *next; 4659104847fSDavid Xu 4669104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 4679104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 4689104847fSDavid Xu p1 = src->sq_proc; 4699104847fSDavid Xu p2 = dst->sq_proc; 4709104847fSDavid Xu /* Move siginfo to target list */ 4715c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 472fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 4739104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 4749104847fSDavid Xu if (p1 != NULL) 4759104847fSDavid Xu p1->p_pendingcnt--; 4769104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 4775da49fcbSDavid Xu ksi->ksi_sigq = dst; 4789104847fSDavid Xu if (p2 != NULL) 4799104847fSDavid Xu p2->p_pendingcnt++; 4809104847fSDavid Xu } 4819104847fSDavid Xu } 4829104847fSDavid Xu 4839104847fSDavid Xu /* Move pending bits to target list */ 4843dfcaad6SDavid Xu tmp = src->sq_kill; 485fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4863dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 4873dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 4883dfcaad6SDavid Xu 48982a4538fSEric Badger tmp = src->sq_ptrace; 49082a4538fSEric Badger SIGSETAND(tmp, *set); 49182a4538fSEric Badger SIGSETOR(dst->sq_ptrace, tmp); 49282a4538fSEric Badger SIGSETNAND(src->sq_ptrace, tmp); 49382a4538fSEric Badger 4949104847fSDavid Xu tmp = src->sq_signals; 495fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 4969104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 4979104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 4989104847fSDavid Xu } 4999104847fSDavid Xu 500407af02bSDavid Xu #if 0 501a5799a4fSKonstantin Belousov static void 5029104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 5039104847fSDavid Xu { 5049104847fSDavid Xu sigset_t set; 5059104847fSDavid Xu 5069104847fSDavid Xu SIGEMPTYSET(set); 5079104847fSDavid Xu SIGADDSET(set, signo); 5089104847fSDavid Xu sigqueue_move_set(src, dst, &set); 5099104847fSDavid Xu } 510407af02bSDavid Xu #endif 5119104847fSDavid Xu 512a5799a4fSKonstantin Belousov static void 513fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 5149104847fSDavid Xu { 5159104847fSDavid Xu struct proc *p = sq->sq_proc; 5169104847fSDavid Xu ksiginfo_t *ksi, *next; 5179104847fSDavid Xu 5189104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5199104847fSDavid Xu 5209104847fSDavid Xu /* Remove siginfo queue */ 5215c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 5229104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5239104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5245da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5255da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5269104847fSDavid Xu p->p_pendingcnt--; 5279104847fSDavid Xu } 5289104847fSDavid Xu } 5293dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 53082a4538fSEric Badger SIGSETNAND(sq->sq_ptrace, *set); 5319104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 5329104847fSDavid Xu } 5339104847fSDavid Xu 5349104847fSDavid Xu void 5359104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 5369104847fSDavid Xu { 5379104847fSDavid Xu sigset_t set; 5389104847fSDavid Xu 5399104847fSDavid Xu SIGEMPTYSET(set); 5409104847fSDavid Xu SIGADDSET(set, signo); 5419104847fSDavid Xu sigqueue_delete_set(sq, &set); 5429104847fSDavid Xu } 5439104847fSDavid Xu 5449104847fSDavid Xu /* Remove a set of signals for a process */ 545a5799a4fSKonstantin Belousov static void 546fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 5479104847fSDavid Xu { 5489104847fSDavid Xu sigqueue_t worklist; 5499104847fSDavid Xu struct thread *td0; 5509104847fSDavid Xu 5519104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5529104847fSDavid Xu 5539104847fSDavid Xu sigqueue_init(&worklist, NULL); 5549104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 5559104847fSDavid Xu 5569104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 5579104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 5589104847fSDavid Xu 5599104847fSDavid Xu sigqueue_flush(&worklist); 5609104847fSDavid Xu } 5619104847fSDavid Xu 5629104847fSDavid Xu void 5639104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 5649104847fSDavid Xu { 5659104847fSDavid Xu sigset_t set; 5669104847fSDavid Xu 5679104847fSDavid Xu SIGEMPTYSET(set); 5689104847fSDavid Xu SIGADDSET(set, signo); 5699104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5709104847fSDavid Xu } 5719104847fSDavid Xu 572a5799a4fSKonstantin Belousov static void 5739104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 5749104847fSDavid Xu { 5759104847fSDavid Xu sigset_t set; 5769104847fSDavid Xu 5779104847fSDavid Xu SIGEMPTYSET(set); 5789104847fSDavid Xu SIGADDSET(set, SIGSTOP); 5799104847fSDavid Xu SIGADDSET(set, SIGTSTP); 5809104847fSDavid Xu SIGADDSET(set, SIGTTIN); 5819104847fSDavid Xu SIGADDSET(set, SIGTTOU); 5829104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 5839104847fSDavid Xu } 5849104847fSDavid Xu 585fbbeeb6cSBruce Evans /* 5861968f37bSJohn Baldwin * Determine signal that should be delivered to thread td, the current 5871968f37bSJohn Baldwin * thread, 0 if none. If there is a pending stop signal with default 588fbbeeb6cSBruce Evans * action, the process stops in issignal(). 589fbbeeb6cSBruce Evans */ 590fbbeeb6cSBruce Evans int 5913cf3b9f0SJohn Baldwin cursig(struct thread *td) 592fbbeeb6cSBruce Evans { 593c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 59490af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 595a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 5963cf3b9f0SJohn Baldwin return (SIGPENDING(td) ? issignal(td) : 0); 597fbbeeb6cSBruce Evans } 598fbbeeb6cSBruce Evans 59979065dbaSBruce Evans /* 60079065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 6019104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 6024093529dSJeff Roberson * unmasked in td_sigmask. 60379065dbaSBruce Evans */ 60479065dbaSBruce Evans void 6054093529dSJeff Roberson signotify(struct thread *td) 60679065dbaSBruce Evans { 6074093529dSJeff Roberson struct proc *p; 6084093529dSJeff Roberson 6094093529dSJeff Roberson p = td->td_proc; 61079065dbaSBruce Evans 61179065dbaSBruce Evans PROC_LOCK_ASSERT(p, MA_OWNED); 6124093529dSJeff Roberson 6138b94a061SJohn Baldwin if (SIGPENDING(td)) { 614a54e85fdSJeff Roberson thread_lock(td); 6154093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 616a54e85fdSJeff Roberson thread_unlock(td); 61779065dbaSBruce Evans } 6188b94a061SJohn Baldwin } 6198b94a061SJohn Baldwin 6208b94a061SJohn Baldwin int 6218b94a061SJohn Baldwin sigonstack(size_t sp) 6228b94a061SJohn Baldwin { 623a30ec4b9SDavid Xu struct thread *td = curthread; 6248b94a061SJohn Baldwin 625a30ec4b9SDavid Xu return ((td->td_pflags & TDP_ALTSTACK) ? 6261930e303SPoul-Henning Kamp #if defined(COMPAT_43) 627a30ec4b9SDavid Xu ((td->td_sigstk.ss_size == 0) ? 628a30ec4b9SDavid Xu (td->td_sigstk.ss_flags & SS_ONSTACK) : 629a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 6308b94a061SJohn Baldwin #else 631a30ec4b9SDavid Xu ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 6328b94a061SJohn Baldwin #endif 6338b94a061SJohn Baldwin : 0); 6348b94a061SJohn Baldwin } 63579065dbaSBruce Evans 6366f841fb7SMarcel Moolenaar static __inline int 6376f841fb7SMarcel Moolenaar sigprop(int sig) 6382c42a146SMarcel Moolenaar { 6396f841fb7SMarcel Moolenaar 640ed6d876bSBrooks Davis if (sig > 0 && sig < nitems(sigproptbl)) 641ed6d876bSBrooks Davis return (sigproptbl[sig]); 6422c42a146SMarcel Moolenaar return (0); 643df8bae1dSRodney W. Grimes } 6442c42a146SMarcel Moolenaar 6454093529dSJeff Roberson int 6466f841fb7SMarcel Moolenaar sig_ffs(sigset_t *set) 6472c42a146SMarcel Moolenaar { 6482c42a146SMarcel Moolenaar int i; 6492c42a146SMarcel Moolenaar 6506f841fb7SMarcel Moolenaar for (i = 0; i < _SIG_WORDS; i++) 6512c42a146SMarcel Moolenaar if (set->__bits[i]) 6522c42a146SMarcel Moolenaar return (ffs(set->__bits[i]) + (i * 32)); 653df8bae1dSRodney W. Grimes return (0); 654df8bae1dSRodney W. Grimes } 655df8bae1dSRodney W. Grimes 656350ae563SKonstantin Belousov static bool 657ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag) 658350ae563SKonstantin Belousov { 659350ae563SKonstantin Belousov 660c83655f3SKonstantin Belousov /* 661c83655f3SKonstantin Belousov * SA_SIGINFO is reset when signal disposition is set to 662c83655f3SKonstantin Belousov * ignore or default. Other flags are kept according to user 663c83655f3SKonstantin Belousov * settings. 664c83655f3SKonstantin Belousov */ 665c83655f3SKonstantin Belousov return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 666c83655f3SKonstantin Belousov ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 667c83655f3SKonstantin Belousov (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 668350ae563SKonstantin Belousov } 669350ae563SKonstantin Belousov 6702c42a146SMarcel Moolenaar /* 6718f19eb88SIan Dowse * kern_sigaction 6722c42a146SMarcel Moolenaar * sigaction 67323eeeff7SPeter Wemm * freebsd4_sigaction 6742c42a146SMarcel Moolenaar * osigaction 6752c42a146SMarcel Moolenaar */ 6768f19eb88SIan Dowse int 677ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 678ea566832SEd Schouten struct sigaction *oact, int flags) 679df8bae1dSRodney W. Grimes { 68090af4afaSJohn Baldwin struct sigacts *ps; 6818f19eb88SIan Dowse struct proc *p = td->td_proc; 682df8bae1dSRodney W. Grimes 6832899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 6842c42a146SMarcel Moolenaar return (EINVAL); 685271ab240SKonstantin Belousov if (act != NULL && act->sa_handler != SIG_DFL && 686271ab240SKonstantin Belousov act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 687271ab240SKonstantin Belousov SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 688271ab240SKonstantin Belousov SA_NOCLDWAIT | SA_SIGINFO)) != 0) 6892d864174SKonstantin Belousov return (EINVAL); 6902c42a146SMarcel Moolenaar 691628d2653SJohn Baldwin PROC_LOCK(p); 692628d2653SJohn Baldwin ps = p->p_sigacts; 69390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 6942c42a146SMarcel Moolenaar if (oact) { 6952c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 6962c42a146SMarcel Moolenaar oact->sa_flags = 0; 6972c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 6982c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 6992c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 7002c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 7012c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 7022c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 7032c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 7042c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 70510c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 7062c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 70710c2b8e1SDavid E. O'Brien oact->sa_sigaction = 70810c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 70910c2b8e1SDavid E. O'Brien } else 71010c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 71190af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 7122c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 71390af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 7142c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 7152c42a146SMarcel Moolenaar } 7162c42a146SMarcel Moolenaar if (act) { 7172c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 718628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 71990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 720628d2653SJohn Baldwin PROC_UNLOCK(p); 7212c42a146SMarcel Moolenaar return (EINVAL); 722628d2653SJohn Baldwin } 7232c42a146SMarcel Moolenaar 724df8bae1dSRodney W. Grimes /* 725df8bae1dSRodney W. Grimes * Change setting atomically. 726df8bae1dSRodney W. Grimes */ 7272c42a146SMarcel Moolenaar 7282c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 7292c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 730350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_SIGINFO)) { 731aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 732aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 73380f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 73480f42b55SIan Dowse } else { 73580f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 7362c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 7372c42a146SMarcel Moolenaar } 738350ae563SKonstantin Belousov if (!sigact_flag_test(act, SA_RESTART)) 7392c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 740df8bae1dSRodney W. Grimes else 7412c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 742350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_ONSTACK)) 7432c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 744df8bae1dSRodney W. Grimes else 7452c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 746350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_RESETHAND)) 7472c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 7481e41c1b5SSteven Wallace else 7492c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 750350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_NODEFER)) 7512c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 752289ccde0SPeter Wemm else 7532c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 7542c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 7552c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 75690af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 7576626c604SJulian Elischer else 75890af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 759ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 760245f17d4SJoerg Wunsch /* 7612c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 7622c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 7632c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 7642c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 765245f17d4SJoerg Wunsch */ 766245f17d4SJoerg Wunsch if (p->p_pid == 1) 76790af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 7686626c604SJulian Elischer else 76990af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 770245f17d4SJoerg Wunsch } else 77190af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 772ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 77390af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 774ba1551caSIan Dowse else 77590af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 776df8bae1dSRodney W. Grimes } 777df8bae1dSRodney W. Grimes /* 77890af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 7792c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 78090af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 7812c42a146SMarcel Moolenaar * have to restart the process. 782df8bae1dSRodney W. Grimes */ 7832c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 784fd50a707SBrooks Davis (sigprop(sig) & SIGPROP_IGNORE && 7852c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 7862c42a146SMarcel Moolenaar /* never to be seen again */ 7879104847fSDavid Xu sigqueue_delete_proc(p, sig); 7882c42a146SMarcel Moolenaar if (sig != SIGCONT) 7892c42a146SMarcel Moolenaar /* easier in psignal */ 79090af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 79190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 792645682fdSLuoqi Chen } else { 79390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 7942c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 79590af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 7962c42a146SMarcel Moolenaar else 79790af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 7982c42a146SMarcel Moolenaar } 79923eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 80023eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 80123eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 80223eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 80323eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 80423eeeff7SPeter Wemm else 80523eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 80623eeeff7SPeter Wemm #endif 807e8ebc08fSPeter Wemm #ifdef COMPAT_43 808645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 80923eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 81023eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 811645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 812645682fdSLuoqi Chen else 813645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 814e8ebc08fSPeter Wemm #endif 815df8bae1dSRodney W. Grimes } 81690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 817628d2653SJohn Baldwin PROC_UNLOCK(p); 8182c42a146SMarcel Moolenaar return (0); 8192c42a146SMarcel Moolenaar } 8202c42a146SMarcel Moolenaar 8212c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8222c42a146SMarcel Moolenaar struct sigaction_args { 8232c42a146SMarcel Moolenaar int sig; 8242c42a146SMarcel Moolenaar struct sigaction *act; 8252c42a146SMarcel Moolenaar struct sigaction *oact; 8262c42a146SMarcel Moolenaar }; 8272c42a146SMarcel Moolenaar #endif 8282c42a146SMarcel Moolenaar int 829e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap) 8302c42a146SMarcel Moolenaar { 8312c42a146SMarcel Moolenaar struct sigaction act, oact; 832e052a8b9SEd Maste struct sigaction *actp, *oactp; 8332c42a146SMarcel Moolenaar int error; 8342c42a146SMarcel Moolenaar 8356f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 8366f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 8372c42a146SMarcel Moolenaar if (actp) { 8386f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 8392c42a146SMarcel Moolenaar if (error) 84044443757STim J. Robbins return (error); 8412c42a146SMarcel Moolenaar } 8428f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 84325d6dc06SJohn Baldwin if (oactp && !error) 8446f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 8452c42a146SMarcel Moolenaar return (error); 8462c42a146SMarcel Moolenaar } 8472c42a146SMarcel Moolenaar 84823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 84923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 85023eeeff7SPeter Wemm struct freebsd4_sigaction_args { 85123eeeff7SPeter Wemm int sig; 85223eeeff7SPeter Wemm struct sigaction *act; 85323eeeff7SPeter Wemm struct sigaction *oact; 85423eeeff7SPeter Wemm }; 85523eeeff7SPeter Wemm #endif 85623eeeff7SPeter Wemm int 857e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 85823eeeff7SPeter Wemm { 85923eeeff7SPeter Wemm struct sigaction act, oact; 860e052a8b9SEd Maste struct sigaction *actp, *oactp; 86123eeeff7SPeter Wemm int error; 86223eeeff7SPeter Wemm 86323eeeff7SPeter Wemm 86423eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 86523eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 86623eeeff7SPeter Wemm if (actp) { 86723eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 86823eeeff7SPeter Wemm if (error) 86944443757STim J. Robbins return (error); 87023eeeff7SPeter Wemm } 87123eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 872a14e1189SJohn Baldwin if (oactp && !error) 87323eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 87423eeeff7SPeter Wemm return (error); 87523eeeff7SPeter Wemm } 87623eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 87723eeeff7SPeter Wemm 87831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 8792c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8802c42a146SMarcel Moolenaar struct osigaction_args { 8812c42a146SMarcel Moolenaar int signum; 8822c42a146SMarcel Moolenaar struct osigaction *nsa; 8832c42a146SMarcel Moolenaar struct osigaction *osa; 8842c42a146SMarcel Moolenaar }; 8852c42a146SMarcel Moolenaar #endif 8862c42a146SMarcel Moolenaar int 887e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap) 8882c42a146SMarcel Moolenaar { 8892c42a146SMarcel Moolenaar struct osigaction sa; 8902c42a146SMarcel Moolenaar struct sigaction nsa, osa; 891e052a8b9SEd Maste struct sigaction *nsap, *osap; 8922c42a146SMarcel Moolenaar int error; 8932c42a146SMarcel Moolenaar 8946f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 8956f841fb7SMarcel Moolenaar return (EINVAL); 896fb99ab88SMatthew Dillon 8976f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 8986f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 899fb99ab88SMatthew Dillon 9002c42a146SMarcel Moolenaar if (nsap) { 9016f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 9022c42a146SMarcel Moolenaar if (error) 90344443757STim J. Robbins return (error); 9042c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 9052c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 9062c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 9072c42a146SMarcel Moolenaar } 90823eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 9092c42a146SMarcel Moolenaar if (osap && !error) { 9102c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 9112c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 9122c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 9136f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 9142c42a146SMarcel Moolenaar } 9152c42a146SMarcel Moolenaar return (error); 9162c42a146SMarcel Moolenaar } 91723eeeff7SPeter Wemm 91873dbd3daSJohn Baldwin #if !defined(__i386__) 91923eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 92023eeeff7SPeter Wemm int 921e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap) 92223eeeff7SPeter Wemm { 92323eeeff7SPeter Wemm 92423eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 92523eeeff7SPeter Wemm } 92623eeeff7SPeter Wemm #endif 92731c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 928df8bae1dSRodney W. Grimes 929df8bae1dSRodney W. Grimes /* 930df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 931df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 932df8bae1dSRodney W. Grimes */ 933df8bae1dSRodney W. Grimes void 934e052a8b9SEd Maste siginit(struct proc *p) 935df8bae1dSRodney W. Grimes { 9363e85b721SEd Maste int i; 93790af4afaSJohn Baldwin struct sigacts *ps; 938df8bae1dSRodney W. Grimes 939628d2653SJohn Baldwin PROC_LOCK(p); 94090af4afaSJohn Baldwin ps = p->p_sigacts; 94190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 942350ae563SKonstantin Belousov for (i = 1; i <= NSIG; i++) { 943fd50a707SBrooks Davis if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 94490af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 945350ae563SKonstantin Belousov } 946350ae563SKonstantin Belousov } 94790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 948628d2653SJohn Baldwin PROC_UNLOCK(p); 949df8bae1dSRodney W. Grimes } 950df8bae1dSRodney W. Grimes 951df8bae1dSRodney W. Grimes /* 952350ae563SKonstantin Belousov * Reset specified signal to the default disposition. 953350ae563SKonstantin Belousov */ 954350ae563SKonstantin Belousov static void 955350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig) 956350ae563SKonstantin Belousov { 957350ae563SKonstantin Belousov 958350ae563SKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 959350ae563SKonstantin Belousov SIGDELSET(ps->ps_sigcatch, sig); 960fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 961350ae563SKonstantin Belousov SIGADDSET(ps->ps_sigignore, sig); 962350ae563SKonstantin Belousov ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 963350ae563SKonstantin Belousov SIGDELSET(ps->ps_siginfo, sig); 964350ae563SKonstantin Belousov } 965350ae563SKonstantin Belousov 966350ae563SKonstantin Belousov /* 967df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 968df8bae1dSRodney W. Grimes */ 969df8bae1dSRodney W. Grimes void 970a30ec4b9SDavid Xu execsigs(struct proc *p) 971df8bae1dSRodney W. Grimes { 972f3fe76ecSEd Schouten sigset_t osigignore; 973a30ec4b9SDavid Xu struct sigacts *ps; 974a30ec4b9SDavid Xu int sig; 975a30ec4b9SDavid Xu struct thread *td; 976df8bae1dSRodney W. Grimes 977df8bae1dSRodney W. Grimes /* 978df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 9794093529dSJeff Roberson * through td_sigmask (unless they were caught, 980df8bae1dSRodney W. Grimes * and are now ignored by default). 981df8bae1dSRodney W. Grimes */ 9829b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 983628d2653SJohn Baldwin ps = p->p_sigacts; 98490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 98590af4afaSJohn Baldwin while (SIGNOTEMPTY(ps->ps_sigcatch)) { 98690af4afaSJohn Baldwin sig = sig_ffs(&ps->ps_sigcatch); 987350ae563SKonstantin Belousov sigdflt(ps, sig); 988fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 9899104847fSDavid Xu sigqueue_delete_proc(p, sig); 990df8bae1dSRodney W. Grimes } 991f3fe76ecSEd Schouten 992f3fe76ecSEd Schouten /* 993f3fe76ecSEd Schouten * As CloudABI processes cannot modify signal handlers, fully 994f3fe76ecSEd Schouten * reset all signals to their default behavior. Do ignore 995f3fe76ecSEd Schouten * SIGPIPE, as it would otherwise be impossible to recover from 996f3fe76ecSEd Schouten * writes to broken pipes and sockets. 997f3fe76ecSEd Schouten */ 998f3fe76ecSEd Schouten if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) { 999f3fe76ecSEd Schouten osigignore = ps->ps_sigignore; 1000f3fe76ecSEd Schouten while (SIGNOTEMPTY(osigignore)) { 1001f3fe76ecSEd Schouten sig = sig_ffs(&osigignore); 1002f3fe76ecSEd Schouten SIGDELSET(osigignore, sig); 1003f3fe76ecSEd Schouten if (sig != SIGPIPE) 1004f3fe76ecSEd Schouten sigdflt(ps, sig); 1005f3fe76ecSEd Schouten } 1006f3fe76ecSEd Schouten SIGADDSET(ps->ps_sigignore, SIGPIPE); 1007f3fe76ecSEd Schouten } 1008f3fe76ecSEd Schouten 1009df8bae1dSRodney W. Grimes /* 1010df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 1011df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 1012df8bae1dSRodney W. Grimes */ 1013469ec1ebSKonstantin Belousov td = curthread; 1014469ec1ebSKonstantin Belousov MPASS(td->td_proc == p); 1015a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 1016a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1017a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 1018a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 101980e907a1SPeter Wemm /* 102080e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 102180e907a1SPeter Wemm */ 102290af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1023c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1024c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 102590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1026df8bae1dSRodney W. Grimes } 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes /* 1029e77daab1SJohn Baldwin * kern_sigprocmask() 10307c8fdcbdSMatthew Dillon * 1031628d2653SJohn Baldwin * Manipulate signal mask. 1032df8bae1dSRodney W. Grimes */ 1033e77daab1SJohn Baldwin int 10346b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 103584440afbSKonstantin Belousov int flags) 10362c42a146SMarcel Moolenaar { 10376b286ee8SKonstantin Belousov sigset_t new_block, oset1; 10386b286ee8SKonstantin Belousov struct proc *p; 10392c42a146SMarcel Moolenaar int error; 10402c42a146SMarcel Moolenaar 10416b286ee8SKonstantin Belousov p = td->td_proc; 104270778bbaSKonstantin Belousov if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 104370778bbaSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 104470778bbaSKonstantin Belousov else 10456b286ee8SKonstantin Belousov PROC_LOCK(p); 104670778bbaSKonstantin Belousov mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 104770778bbaSKonstantin Belousov ? MA_OWNED : MA_NOTOWNED); 10482c42a146SMarcel Moolenaar if (oset != NULL) 10494093529dSJeff Roberson *oset = td->td_sigmask; 10502c42a146SMarcel Moolenaar 10512c42a146SMarcel Moolenaar error = 0; 10522c42a146SMarcel Moolenaar if (set != NULL) { 10532c42a146SMarcel Moolenaar switch (how) { 10542c42a146SMarcel Moolenaar case SIG_BLOCK: 1055645682fdSLuoqi Chen SIG_CANTMASK(*set); 10566b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 10574093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 10586b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10596b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10602c42a146SMarcel Moolenaar break; 10612c42a146SMarcel Moolenaar case SIG_UNBLOCK: 10624093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 10634093529dSJeff Roberson signotify(td); 1064407af02bSDavid Xu goto out; 10652c42a146SMarcel Moolenaar case SIG_SETMASK: 1066645682fdSLuoqi Chen SIG_CANTMASK(*set); 10676b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 106884440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 10694093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1070645682fdSLuoqi Chen else 10714093529dSJeff Roberson td->td_sigmask = *set; 10726b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10736b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10744093529dSJeff Roberson signotify(td); 10752c42a146SMarcel Moolenaar break; 10762c42a146SMarcel Moolenaar default: 10772c42a146SMarcel Moolenaar error = EINVAL; 1078407af02bSDavid Xu goto out; 10792c42a146SMarcel Moolenaar } 10806b286ee8SKonstantin Belousov 10816b286ee8SKonstantin Belousov /* 10827df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 10836b286ee8SKonstantin Belousov * blocked, but are blocked now. 10846b286ee8SKonstantin Belousov * 10856b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 10866b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1087407af02bSDavid Xu * signal delivery to some thread that does not block the 1088407af02bSDavid Xu * signal, possibly waking it up. 10896b286ee8SKonstantin Belousov */ 10906b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 109180a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1092407af02bSDavid Xu } 10936b286ee8SKonstantin Belousov 1094407af02bSDavid Xu out: 109584440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 10966b286ee8SKonstantin Belousov PROC_UNLOCK(p); 10972c42a146SMarcel Moolenaar return (error); 10982c42a146SMarcel Moolenaar } 10992c42a146SMarcel Moolenaar 1100d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1101df8bae1dSRodney W. Grimes struct sigprocmask_args { 1102df8bae1dSRodney W. Grimes int how; 11032c42a146SMarcel Moolenaar const sigset_t *set; 11042c42a146SMarcel Moolenaar sigset_t *oset; 1105df8bae1dSRodney W. Grimes }; 1106d2d3e875SBruce Evans #endif 110726f9a767SRodney W. Grimes int 1108e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1109df8bae1dSRodney W. Grimes { 11102c42a146SMarcel Moolenaar sigset_t set, oset; 11112c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 11122c42a146SMarcel Moolenaar int error; 1113df8bae1dSRodney W. Grimes 11146f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 11156f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 11162c42a146SMarcel Moolenaar if (setp) { 11176f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 11182c42a146SMarcel Moolenaar if (error) 11192c42a146SMarcel Moolenaar return (error); 1120df8bae1dSRodney W. Grimes } 1121e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 11222c42a146SMarcel Moolenaar if (osetp && !error) { 11236f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 11242c42a146SMarcel Moolenaar } 11252c42a146SMarcel Moolenaar return (error); 11262c42a146SMarcel Moolenaar } 11272c42a146SMarcel Moolenaar 112831c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11302c42a146SMarcel Moolenaar struct osigprocmask_args { 11312c42a146SMarcel Moolenaar int how; 11322c42a146SMarcel Moolenaar osigset_t mask; 11332c42a146SMarcel Moolenaar }; 11342c42a146SMarcel Moolenaar #endif 11352c42a146SMarcel Moolenaar int 1136e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap) 11372c42a146SMarcel Moolenaar { 11382c42a146SMarcel Moolenaar sigset_t set, oset; 11392c42a146SMarcel Moolenaar int error; 11402c42a146SMarcel Moolenaar 11412c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1142e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1143b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1144df8bae1dSRodney W. Grimes return (error); 1145df8bae1dSRodney W. Grimes } 114631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1147df8bae1dSRodney W. Grimes 114826f9a767SRodney W. Grimes int 11498451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1150a447cd8bSJeff Roberson { 11519104847fSDavid Xu ksiginfo_t ksi; 1152a447cd8bSJeff Roberson sigset_t set; 1153a447cd8bSJeff Roberson int error; 1154a447cd8bSJeff Roberson 1155a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 115636939a0aSDavid Xu if (error) { 115736939a0aSDavid Xu td->td_retval[0] = error; 115836939a0aSDavid Xu return (0); 115936939a0aSDavid Xu } 1160a447cd8bSJeff Roberson 11619104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 116236939a0aSDavid Xu if (error) { 11638e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 11648e9a54eeSKonstantin Belousov error = ERESTART; 116536939a0aSDavid Xu if (error == ERESTART) 1166a447cd8bSJeff Roberson return (error); 116736939a0aSDavid Xu td->td_retval[0] = error; 116836939a0aSDavid Xu return (0); 116936939a0aSDavid Xu } 1170a447cd8bSJeff Roberson 11719104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 117236939a0aSDavid Xu td->td_retval[0] = error; 117336939a0aSDavid Xu return (0); 1174a447cd8bSJeff Roberson } 11750c14ff0eSRobert Watson 1176a447cd8bSJeff Roberson int 11778451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1178a447cd8bSJeff Roberson { 1179a447cd8bSJeff Roberson struct timespec ts; 1180a447cd8bSJeff Roberson struct timespec *timeout; 1181a447cd8bSJeff Roberson sigset_t set; 11829104847fSDavid Xu ksiginfo_t ksi; 1183a447cd8bSJeff Roberson int error; 1184a447cd8bSJeff Roberson 1185a447cd8bSJeff Roberson if (uap->timeout) { 1186a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1187a447cd8bSJeff Roberson if (error) 1188a447cd8bSJeff Roberson return (error); 1189a447cd8bSJeff Roberson 1190a447cd8bSJeff Roberson timeout = &ts; 1191a447cd8bSJeff Roberson } else 1192a447cd8bSJeff Roberson timeout = NULL; 1193a447cd8bSJeff Roberson 1194a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1195a447cd8bSJeff Roberson if (error) 1196a447cd8bSJeff Roberson return (error); 1197a447cd8bSJeff Roberson 11989104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1199a447cd8bSJeff Roberson if (error) 1200a447cd8bSJeff Roberson return (error); 12019dde3bc9SDavid Xu 1202418228dfSDavid Xu if (uap->info) 12039104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12049104847fSDavid Xu 12059104847fSDavid Xu if (error == 0) 12069104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1207a447cd8bSJeff Roberson return (error); 1208a447cd8bSJeff Roberson } 1209a447cd8bSJeff Roberson 1210a447cd8bSJeff Roberson int 12118451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1212a447cd8bSJeff Roberson { 12139104847fSDavid Xu ksiginfo_t ksi; 1214a447cd8bSJeff Roberson sigset_t set; 1215a447cd8bSJeff Roberson int error; 1216a447cd8bSJeff Roberson 1217a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1218a447cd8bSJeff Roberson if (error) 1219a447cd8bSJeff Roberson return (error); 1220a447cd8bSJeff Roberson 12219104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1222a447cd8bSJeff Roberson if (error) 1223a447cd8bSJeff Roberson return (error); 1224a447cd8bSJeff Roberson 1225418228dfSDavid Xu if (uap->info) 12269104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12279104847fSDavid Xu 12289104847fSDavid Xu if (error == 0) 12299104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1230a447cd8bSJeff Roberson return (error); 1231a447cd8bSJeff Roberson } 1232a447cd8bSJeff Roberson 123386be94fcSTycho Nightingale static void 123486be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 123586be94fcSTycho Nightingale { 123686be94fcSTycho Nightingale struct thread *thr; 123786be94fcSTycho Nightingale 123886be94fcSTycho Nightingale FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 123986be94fcSTycho Nightingale if (thr == td) 124086be94fcSTycho Nightingale thr->td_si = *si; 124186be94fcSTycho Nightingale else 124286be94fcSTycho Nightingale thr->td_si.si_signo = 0; 124386be94fcSTycho Nightingale } 124486be94fcSTycho Nightingale } 124586be94fcSTycho Nightingale 1246c6511aeaSDavid Xu int 12479104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1248a447cd8bSJeff Roberson struct timespec *timeout) 1249a447cd8bSJeff Roberson { 12503074d1b4SDavid Xu struct sigacts *ps; 1251407af02bSDavid Xu sigset_t saved_mask, new_block; 1252a447cd8bSJeff Roberson struct proc *p; 1253407af02bSDavid Xu int error, sig, timo, timevalid = 0; 12541089f031SDavid Xu struct timespec rts, ets, ts; 12551089f031SDavid Xu struct timeval tv; 1256a447cd8bSJeff Roberson 1257a447cd8bSJeff Roberson p = td->td_proc; 1258a447cd8bSJeff Roberson error = 0; 12599b73d239SMatt Jacob ets.tv_sec = 0; 12609b73d239SMatt Jacob ets.tv_nsec = 0; 1261a447cd8bSJeff Roberson 1262407af02bSDavid Xu if (timeout != NULL) { 12631089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 12641089f031SDavid Xu timevalid = 1; 12651089f031SDavid Xu getnanouptime(&rts); 12661089f031SDavid Xu ets = rts; 12671089f031SDavid Xu timespecadd(&ets, timeout); 12681089f031SDavid Xu } 12691089f031SDavid Xu } 1270407af02bSDavid Xu ksiginfo_init(ksi); 1271407af02bSDavid Xu /* Some signals can not be waited for. */ 1272407af02bSDavid Xu SIG_CANTMASK(waitset); 1273407af02bSDavid Xu ps = p->p_sigacts; 1274407af02bSDavid Xu PROC_LOCK(p); 1275407af02bSDavid Xu saved_mask = td->td_sigmask; 1276407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1277407af02bSDavid Xu for (;;) { 12783074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 12793cf3b9f0SJohn Baldwin sig = cursig(td); 12803074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 128146e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 1282407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1283407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1284407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1285407af02bSDavid Xu error = 0; 1286407af02bSDavid Xu break; 12873074d1b4SDavid Xu } 12887e0221a2SDavid Xu } 12897e0221a2SDavid Xu 1290407af02bSDavid Xu if (error != 0) 1291407af02bSDavid Xu break; 12923074d1b4SDavid Xu 1293a447cd8bSJeff Roberson /* 1294a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1295a447cd8bSJeff Roberson * signals. 1296a447cd8bSJeff Roberson */ 1297407af02bSDavid Xu if (timeout != NULL) { 12981089f031SDavid Xu if (!timevalid) { 1299a447cd8bSJeff Roberson error = EINVAL; 1300407af02bSDavid Xu break; 1301a447cd8bSJeff Roberson } 13021089f031SDavid Xu getnanouptime(&rts); 13031089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 13043074d1b4SDavid Xu error = EAGAIN; 1305407af02bSDavid Xu break; 13063074d1b4SDavid Xu } 13071089f031SDavid Xu ts = ets; 13081089f031SDavid Xu timespecsub(&ts, &rts); 13091089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1310407af02bSDavid Xu timo = tvtohz(&tv); 1311407af02bSDavid Xu } else { 1312407af02bSDavid Xu timo = 0; 1313407af02bSDavid Xu } 1314a447cd8bSJeff Roberson 1315407af02bSDavid Xu error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo); 1316407af02bSDavid Xu 1317407af02bSDavid Xu if (timeout != NULL) { 13181089f031SDavid Xu if (error == ERESTART) { 1319407af02bSDavid Xu /* Timeout can not be restarted. */ 13203074d1b4SDavid Xu error = EINTR; 13211089f031SDavid Xu } else if (error == EAGAIN) { 1322407af02bSDavid Xu /* We will calculate timeout by ourself. */ 13231089f031SDavid Xu error = 0; 13241089f031SDavid Xu } 13251089f031SDavid Xu } 1326407af02bSDavid Xu } 13279dde3bc9SDavid Xu 1328407af02bSDavid Xu new_block = saved_mask; 1329407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1330407af02bSDavid Xu td->td_sigmask = saved_mask; 1331407af02bSDavid Xu /* 1332407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1333407af02bSDavid Xu * notification. 1334407af02bSDavid Xu */ 1335407af02bSDavid Xu if (p->p_numthreads != 1) 1336407af02bSDavid Xu reschedule_signals(p, new_block, 0); 13375d217f17SJohn Birrell 1338407af02bSDavid Xu if (error == 0) { 133936160958SMark Johnston SDT_PROBE2(proc, , , signal__clear, sig, ksi); 13405d217f17SJohn Birrell 134156c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 134256c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 13437e0221a2SDavid Xu 13447e0221a2SDavid Xu #ifdef KTRACE 13457e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 13467e0221a2SDavid Xu sig_t action; 13477e0221a2SDavid Xu 13483074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1349a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 135090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 135161009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 13527e0221a2SDavid Xu } 1353a447cd8bSJeff Roberson #endif 135486be94fcSTycho Nightingale if (sig == SIGKILL) { 135586be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi->ksi_info); 13567e0221a2SDavid Xu sigexit(td, sig); 13573074d1b4SDavid Xu } 135886be94fcSTycho Nightingale } 1359a447cd8bSJeff Roberson PROC_UNLOCK(p); 1360a447cd8bSJeff Roberson return (error); 1361a447cd8bSJeff Roberson } 1362a447cd8bSJeff Roberson 13639104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 13649104847fSDavid Xu struct sigpending_args { 13659104847fSDavid Xu sigset_t *set; 13669104847fSDavid Xu }; 13679104847fSDavid Xu #endif 1368a447cd8bSJeff Roberson int 1369e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap) 1370df8bae1dSRodney W. Grimes { 1371b40ce416SJulian Elischer struct proc *p = td->td_proc; 13729104847fSDavid Xu sigset_t pending; 1373df8bae1dSRodney W. Grimes 1374628d2653SJohn Baldwin PROC_LOCK(p); 13759104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13769104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1377628d2653SJohn Baldwin PROC_UNLOCK(p); 13789104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 13792c42a146SMarcel Moolenaar } 13802c42a146SMarcel Moolenaar 138131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 13822c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 13832c42a146SMarcel Moolenaar struct osigpending_args { 13842c42a146SMarcel Moolenaar int dummy; 13852c42a146SMarcel Moolenaar }; 13862c42a146SMarcel Moolenaar #endif 13872c42a146SMarcel Moolenaar int 1388e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap) 13892c42a146SMarcel Moolenaar { 1390b40ce416SJulian Elischer struct proc *p = td->td_proc; 13919104847fSDavid Xu sigset_t pending; 1392b40ce416SJulian Elischer 1393628d2653SJohn Baldwin PROC_LOCK(p); 13949104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 13959104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1396628d2653SJohn Baldwin PROC_UNLOCK(p); 13979104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1398df8bae1dSRodney W. Grimes return (0); 1399df8bae1dSRodney W. Grimes } 140031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1401df8bae1dSRodney W. Grimes 14021930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1403df8bae1dSRodney W. Grimes /* 1404df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1405df8bae1dSRodney W. Grimes */ 1406d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1407df8bae1dSRodney W. Grimes struct osigvec_args { 1408df8bae1dSRodney W. Grimes int signum; 1409df8bae1dSRodney W. Grimes struct sigvec *nsv; 1410df8bae1dSRodney W. Grimes struct sigvec *osv; 1411df8bae1dSRodney W. Grimes }; 1412d2d3e875SBruce Evans #endif 1413df8bae1dSRodney W. Grimes /* ARGSUSED */ 141426f9a767SRodney W. Grimes int 1415e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap) 1416df8bae1dSRodney W. Grimes { 1417df8bae1dSRodney W. Grimes struct sigvec vec; 14182c42a146SMarcel Moolenaar struct sigaction nsa, osa; 1419e052a8b9SEd Maste struct sigaction *nsap, *osap; 14202c42a146SMarcel Moolenaar int error; 1421df8bae1dSRodney W. Grimes 14226f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 14236f841fb7SMarcel Moolenaar return (EINVAL); 14246f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 14256f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 14262c42a146SMarcel Moolenaar if (nsap) { 14276f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 14282c42a146SMarcel Moolenaar if (error) 1429df8bae1dSRodney W. Grimes return (error); 14302c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 14312c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 14322c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 14332c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1434df8bae1dSRodney W. Grimes } 14355edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 14362c42a146SMarcel Moolenaar if (osap && !error) { 14372c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 14382c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 14392c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 14402c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 14412c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 14426f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 14432c42a146SMarcel Moolenaar } 14442c42a146SMarcel Moolenaar return (error); 1445df8bae1dSRodney W. Grimes } 1446df8bae1dSRodney W. Grimes 1447d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1448df8bae1dSRodney W. Grimes struct osigblock_args { 1449df8bae1dSRodney W. Grimes int mask; 1450df8bae1dSRodney W. Grimes }; 1451d2d3e875SBruce Evans #endif 145226f9a767SRodney W. Grimes int 1453e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap) 1454df8bae1dSRodney W. Grimes { 1455d6e029adSKonstantin Belousov sigset_t set, oset; 1456df8bae1dSRodney W. Grimes 14572c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1458d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1459d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1460df8bae1dSRodney W. Grimes return (0); 1461df8bae1dSRodney W. Grimes } 1462df8bae1dSRodney W. Grimes 1463d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1464df8bae1dSRodney W. Grimes struct osigsetmask_args { 1465df8bae1dSRodney W. Grimes int mask; 1466df8bae1dSRodney W. Grimes }; 1467d2d3e875SBruce Evans #endif 146826f9a767SRodney W. Grimes int 1469e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1470df8bae1dSRodney W. Grimes { 1471d6e029adSKonstantin Belousov sigset_t set, oset; 1472df8bae1dSRodney W. Grimes 14732c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1474d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1475d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1476df8bae1dSRodney W. Grimes return (0); 1477df8bae1dSRodney W. Grimes } 14781930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1479df8bae1dSRodney W. Grimes 1480df8bae1dSRodney W. Grimes /* 1481873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1482873fbcd7SRobert Watson * meantime. 1483df8bae1dSRodney W. Grimes */ 1484d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1485df8bae1dSRodney W. Grimes struct sigsuspend_args { 14862c42a146SMarcel Moolenaar const sigset_t *sigmask; 1487df8bae1dSRodney W. Grimes }; 1488d2d3e875SBruce Evans #endif 1489df8bae1dSRodney W. Grimes /* ARGSUSED */ 149026f9a767SRodney W. Grimes int 1491e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1492df8bae1dSRodney W. Grimes { 14932c42a146SMarcel Moolenaar sigset_t mask; 14942c42a146SMarcel Moolenaar int error; 14952c42a146SMarcel Moolenaar 14966f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 14972c42a146SMarcel Moolenaar if (error) 14982c42a146SMarcel Moolenaar return (error); 14998f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 15008f19eb88SIan Dowse } 15018f19eb88SIan Dowse 15028f19eb88SIan Dowse int 15038f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 15048f19eb88SIan Dowse { 15058f19eb88SIan Dowse struct proc *p = td->td_proc; 150684440afbSKonstantin Belousov int has_sig, sig; 1507df8bae1dSRodney W. Grimes 1508df8bae1dSRodney W. Grimes /* 1509645682fdSLuoqi Chen * When returning from sigsuspend, we want 1510df8bae1dSRodney W. Grimes * the old mask to be restored after the 1511df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1512df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1513df8bae1dSRodney W. Grimes * to indicate this. 1514df8bae1dSRodney W. Grimes */ 1515628d2653SJohn Baldwin PROC_LOCK(p); 151684440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 151784440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 15185e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 151984440afbSKonstantin Belousov 152084440afbSKonstantin Belousov /* 152184440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 152284440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 152384440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 152484440afbSKonstantin Belousov * delivery. 152584440afbSKonstantin Belousov */ 1526afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 152784440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 152884440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 152984440afbSKonstantin Belousov 0) == 0) 15302c42a146SMarcel Moolenaar /* void */; 153184440afbSKonstantin Belousov thread_suspend_check(0); 153284440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 153346e47c4fSKonstantin Belousov while ((sig = cursig(td)) != 0) { 153446e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 153575c586a4SKonstantin Belousov has_sig += postsig(sig); 153646e47c4fSKonstantin Belousov } 153784440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 153884440afbSKonstantin Belousov } 1539628d2653SJohn Baldwin PROC_UNLOCK(p); 15402dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 15412dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 154275c586a4SKonstantin Belousov return (EJUSTRETURN); 15432c42a146SMarcel Moolenaar } 15442c42a146SMarcel Moolenaar 154531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 154697563428SAlexander Kabaev /* 154797563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 154897563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 154997563428SAlexander Kabaev */ 15502c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 15512c42a146SMarcel Moolenaar struct osigsuspend_args { 15522c42a146SMarcel Moolenaar osigset_t mask; 15532c42a146SMarcel Moolenaar }; 15542c42a146SMarcel Moolenaar #endif 15552c42a146SMarcel Moolenaar /* ARGSUSED */ 15562c42a146SMarcel Moolenaar int 1557e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap) 15582c42a146SMarcel Moolenaar { 1559645682fdSLuoqi Chen sigset_t mask; 15602c42a146SMarcel Moolenaar 1561645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 156284440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1563df8bae1dSRodney W. Grimes } 156431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1565df8bae1dSRodney W. Grimes 15661930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1567d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1568df8bae1dSRodney W. Grimes struct osigstack_args { 1569df8bae1dSRodney W. Grimes struct sigstack *nss; 1570df8bae1dSRodney W. Grimes struct sigstack *oss; 1571df8bae1dSRodney W. Grimes }; 1572d2d3e875SBruce Evans #endif 1573df8bae1dSRodney W. Grimes /* ARGSUSED */ 157426f9a767SRodney W. Grimes int 1575e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap) 1576df8bae1dSRodney W. Grimes { 15775afe0c99SJohn Baldwin struct sigstack nss, oss; 1578fb99ab88SMatthew Dillon int error = 0; 1579fb99ab88SMatthew Dillon 1580d034d459SMarcel Moolenaar if (uap->nss != NULL) { 15815afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 15825afe0c99SJohn Baldwin if (error) 15835afe0c99SJohn Baldwin return (error); 1584df8bae1dSRodney W. Grimes } 1585a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 15865afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 15875afe0c99SJohn Baldwin if (uap->nss != NULL) { 1588a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1589a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1590a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1591a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 15925afe0c99SJohn Baldwin } 15935afe0c99SJohn Baldwin if (uap->oss != NULL) 15945afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 15955afe0c99SJohn Baldwin 1596fb99ab88SMatthew Dillon return (error); 1597df8bae1dSRodney W. Grimes } 15981930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1599df8bae1dSRodney W. Grimes 1600d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1601df8bae1dSRodney W. Grimes struct sigaltstack_args { 16022c42a146SMarcel Moolenaar stack_t *ss; 16032c42a146SMarcel Moolenaar stack_t *oss; 1604df8bae1dSRodney W. Grimes }; 1605d2d3e875SBruce Evans #endif 1606df8bae1dSRodney W. Grimes /* ARGSUSED */ 160726f9a767SRodney W. Grimes int 1608e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1609df8bae1dSRodney W. Grimes { 16108f19eb88SIan Dowse stack_t ss, oss; 16118f19eb88SIan Dowse int error; 16128f19eb88SIan Dowse 16138f19eb88SIan Dowse if (uap->ss != NULL) { 16148f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 16158f19eb88SIan Dowse if (error) 16168f19eb88SIan Dowse return (error); 16178f19eb88SIan Dowse } 16188f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 16198f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 16208f19eb88SIan Dowse if (error) 16218f19eb88SIan Dowse return (error); 16228f19eb88SIan Dowse if (uap->oss != NULL) 16238f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 16248f19eb88SIan Dowse return (error); 16258f19eb88SIan Dowse } 16268f19eb88SIan Dowse 16278f19eb88SIan Dowse int 16288f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 16298f19eb88SIan Dowse { 1630b40ce416SJulian Elischer struct proc *p = td->td_proc; 1631fb99ab88SMatthew Dillon int oonstack; 1632fb99ab88SMatthew Dillon 1633b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1634d034d459SMarcel Moolenaar 16358f19eb88SIan Dowse if (oss != NULL) { 1636a30ec4b9SDavid Xu *oss = td->td_sigstk; 1637a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1638d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1639df8bae1dSRodney W. Grimes } 1640d034d459SMarcel Moolenaar 16418f19eb88SIan Dowse if (ss != NULL) { 1642a30ec4b9SDavid Xu if (oonstack) 1643cf60731bSJohn Baldwin return (EPERM); 1644a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1645cf60731bSJohn Baldwin return (EINVAL); 16468f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 16479104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1648cf60731bSJohn Baldwin return (ENOMEM); 16499104847fSDavid Xu 1650a30ec4b9SDavid Xu td->td_sigstk = *ss; 1651a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1652628d2653SJohn Baldwin } else { 1653a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1654628d2653SJohn Baldwin } 1655d034d459SMarcel Moolenaar } 1656cf60731bSJohn Baldwin return (0); 1657df8bae1dSRodney W. Grimes } 1658df8bae1dSRodney W. Grimes 1659d93f860cSPoul-Henning Kamp /* 1660d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1661d93f860cSPoul-Henning Kamp * cp is calling process. 1662d93f860cSPoul-Henning Kamp */ 166337c84183SPoul-Henning Kamp static int 1664a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1665d93f860cSPoul-Henning Kamp { 1666a3de221dSKonstantin Belousov struct proc *p; 1667d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 16683d74f47bSEitan Adler int err; 16693d74f47bSEitan Adler int ret; 1670d93f860cSPoul-Henning Kamp 16713d74f47bSEitan Adler ret = ESRCH; 1672553629ebSJake Burkholder if (all) { 1673d93f860cSPoul-Henning Kamp /* 1674d93f860cSPoul-Henning Kamp * broadcast 1675d93f860cSPoul-Henning Kamp */ 16761005a129SJohn Baldwin sx_slock(&allproc_lock); 16774f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1678628d2653SJohn Baldwin PROC_LOCK(p); 16799c1ab3e0SJohn Baldwin if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 168095f16c1eSPaul Saab p == td->td_proc || p->p_state == PRS_NEW) { 1681628d2653SJohn Baldwin PROC_UNLOCK(p); 1682628d2653SJohn Baldwin continue; 1683628d2653SJohn Baldwin } 16843d74f47bSEitan Adler err = p_cansignal(td, p, sig); 16853d74f47bSEitan Adler if (err == 0) { 168633a9ed9dSJohn Baldwin if (sig) 1687a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 16883d74f47bSEitan Adler ret = err; 1689628d2653SJohn Baldwin } 16903d74f47bSEitan Adler else if (ret == ESRCH) 16913d74f47bSEitan Adler ret = err; 169233a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1693d93f860cSPoul-Henning Kamp } 16941005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1695553629ebSJake Burkholder } else { 1696ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1697f591779bSSeigo Tanimura if (pgid == 0) { 1698d93f860cSPoul-Henning Kamp /* 1699d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1700d93f860cSPoul-Henning Kamp */ 17019c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1702f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1703f591779bSSeigo Tanimura } else { 1704d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1705f591779bSSeigo Tanimura if (pgrp == NULL) { 1706ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1707d93f860cSPoul-Henning Kamp return (ESRCH); 1708d93f860cSPoul-Henning Kamp } 1709f591779bSSeigo Tanimura } 1710ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 17112e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1712628d2653SJohn Baldwin PROC_LOCK(p); 171395f16c1eSPaul Saab if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 171495f16c1eSPaul Saab p->p_state == PRS_NEW) { 1715628d2653SJohn Baldwin PROC_UNLOCK(p); 1716628d2653SJohn Baldwin continue; 1717628d2653SJohn Baldwin } 17183d74f47bSEitan Adler err = p_cansignal(td, p, sig); 17193d74f47bSEitan Adler if (err == 0) { 172033a9ed9dSJohn Baldwin if (sig) 1721a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 17223d74f47bSEitan Adler ret = err; 1723628d2653SJohn Baldwin } 17243d74f47bSEitan Adler else if (ret == ESRCH) 17253d74f47bSEitan Adler ret = err; 172633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1727d93f860cSPoul-Henning Kamp } 1728f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1729d93f860cSPoul-Henning Kamp } 17303d74f47bSEitan Adler return (ret); 1731d93f860cSPoul-Henning Kamp } 1732d93f860cSPoul-Henning Kamp 1733d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1734df8bae1dSRodney W. Grimes struct kill_args { 1735df8bae1dSRodney W. Grimes int pid; 1736df8bae1dSRodney W. Grimes int signum; 1737df8bae1dSRodney W. Grimes }; 1738d2d3e875SBruce Evans #endif 1739df8bae1dSRodney W. Grimes /* ARGSUSED */ 174026f9a767SRodney W. Grimes int 17418451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1742df8bae1dSRodney W. Grimes { 1743a3de221dSKonstantin Belousov ksiginfo_t ksi; 1744a3de221dSKonstantin Belousov struct proc *p; 174590af4afaSJohn Baldwin int error; 1746df8bae1dSRodney W. Grimes 17478890f5d0SPawel Jakub Dawidek /* 17488890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 17498890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 17508890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 17518890f5d0SPawel Jakub Dawidek */ 17528890f5d0SPawel Jakub Dawidek if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid) 17538890f5d0SPawel Jakub Dawidek return (ECAPMODE); 17548890f5d0SPawel Jakub Dawidek 175514961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 175614961ba7SRobert Watson AUDIT_ARG_PID(uap->pid); 17576c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1758df8bae1dSRodney W. Grimes return (EINVAL); 1759fb99ab88SMatthew Dillon 1760a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1761a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1762a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1763a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1764a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1765a3de221dSKonstantin Belousov 1766df8bae1dSRodney W. Grimes if (uap->pid > 0) { 1767df8bae1dSRodney W. Grimes /* kill single process */ 17680b011ea3SPawel Jakub Dawidek if ((p = pfind(uap->pid)) == NULL) { 176924b2151fSPawel Jakub Dawidek if ((p = zpfind(uap->pid)) == NULL) 177090af4afaSJohn Baldwin return (ESRCH); 17710b011ea3SPawel Jakub Dawidek } 177214961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 17731a88a252SMaxim Sobolev error = p_cansignal(td, p, uap->signum); 177490af4afaSJohn Baldwin if (error == 0 && uap->signum) 1775a3de221dSKonstantin Belousov pksignal(p, uap->signum, &ksi); 1776628d2653SJohn Baldwin PROC_UNLOCK(p); 177790af4afaSJohn Baldwin return (error); 1778df8bae1dSRodney W. Grimes } 1779df8bae1dSRodney W. Grimes switch (uap->pid) { 1780df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 1781a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 1, &ksi)); 1782df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 1783a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, 0, 0, &ksi)); 1784df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 1785a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, -uap->pid, 0, &ksi)); 1786df8bae1dSRodney W. Grimes } 178790af4afaSJohn Baldwin /* NOTREACHED */ 1788df8bae1dSRodney W. Grimes } 1789df8bae1dSRodney W. Grimes 1790cfb5f768SJonathan Anderson int 1791e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap) 1792cfb5f768SJonathan Anderson { 1793cfb5f768SJonathan Anderson struct proc *p; 17947008be5bSPawel Jakub Dawidek cap_rights_t rights; 1795cfb5f768SJonathan Anderson int error; 1796cfb5f768SJonathan Anderson 1797cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1798cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1799cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1800cfb5f768SJonathan Anderson return (EINVAL); 1801cfb5f768SJonathan Anderson 18027008be5bSPawel Jakub Dawidek error = procdesc_find(td, uap->fd, 18037008be5bSPawel Jakub Dawidek cap_rights_init(&rights, CAP_PDKILL), &p); 1804cfb5f768SJonathan Anderson if (error) 1805cfb5f768SJonathan Anderson return (error); 1806cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1807cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1808cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 18098451d0ddSKip Macy kern_psignal(p, uap->signum); 1810cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1811cfb5f768SJonathan Anderson return (error); 1812cfb5f768SJonathan Anderson } 1813cfb5f768SJonathan Anderson 18141930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1815d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1816df8bae1dSRodney W. Grimes struct okillpg_args { 1817df8bae1dSRodney W. Grimes int pgid; 1818df8bae1dSRodney W. Grimes int signum; 1819df8bae1dSRodney W. Grimes }; 1820d2d3e875SBruce Evans #endif 1821df8bae1dSRodney W. Grimes /* ARGSUSED */ 182226f9a767SRodney W. Grimes int 1823a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1824df8bae1dSRodney W. Grimes { 1825a3de221dSKonstantin Belousov ksiginfo_t ksi; 1826df8bae1dSRodney W. Grimes 182714961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 182814961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 18296c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1830df8bae1dSRodney W. Grimes return (EINVAL); 18319104847fSDavid Xu 1832a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1833a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1834a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1835a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1836a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1837a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1838df8bae1dSRodney W. Grimes } 18391930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1840df8bae1dSRodney W. Grimes 18419104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 18429104847fSDavid Xu struct sigqueue_args { 18439104847fSDavid Xu pid_t pid; 18449104847fSDavid Xu int signum; 18459104847fSDavid Xu /* union sigval */ void *value; 18469104847fSDavid Xu }; 18479104847fSDavid Xu #endif 18489104847fSDavid Xu int 18498451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 18509104847fSDavid Xu { 1851f19351aaSBrooks Davis union sigval sv; 1852f19351aaSBrooks Davis 1853f19351aaSBrooks Davis sv.sival_ptr = uap->value; 1854f19351aaSBrooks Davis 1855f19351aaSBrooks Davis return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 1856f19351aaSBrooks Davis } 1857f19351aaSBrooks Davis 1858f19351aaSBrooks Davis int 1859f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value) 1860f19351aaSBrooks Davis { 18619104847fSDavid Xu ksiginfo_t ksi; 18629104847fSDavid Xu struct proc *p; 18639104847fSDavid Xu int error; 18649104847fSDavid Xu 1865f19351aaSBrooks Davis if ((u_int)signum > _SIG_MAXSIG) 18669104847fSDavid Xu return (EINVAL); 18679104847fSDavid Xu 18689104847fSDavid Xu /* 18699104847fSDavid Xu * Specification says sigqueue can only send signal to 18709104847fSDavid Xu * single process. 18719104847fSDavid Xu */ 1872f19351aaSBrooks Davis if (pid <= 0) 18739104847fSDavid Xu return (EINVAL); 18749104847fSDavid Xu 1875f19351aaSBrooks Davis if ((p = pfind(pid)) == NULL) { 1876f19351aaSBrooks Davis if ((p = zpfind(pid)) == NULL) 18779104847fSDavid Xu return (ESRCH); 18789104847fSDavid Xu } 1879f19351aaSBrooks Davis error = p_cansignal(td, p, signum); 1880f19351aaSBrooks Davis if (error == 0 && signum != 0) { 18819104847fSDavid Xu ksiginfo_init(&ksi); 1882a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 1883f19351aaSBrooks Davis ksi.ksi_signo = signum; 18849104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 18859104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 18869104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 1887f19351aaSBrooks Davis ksi.ksi_value = *value; 1888ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 18899104847fSDavid Xu } 18909104847fSDavid Xu PROC_UNLOCK(p); 18919104847fSDavid Xu return (error); 18929104847fSDavid Xu } 18939104847fSDavid Xu 1894df8bae1dSRodney W. Grimes /* 1895df8bae1dSRodney W. Grimes * Send a signal to a process group. 1896df8bae1dSRodney W. Grimes */ 1897df8bae1dSRodney W. Grimes void 1898a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 1899df8bae1dSRodney W. Grimes { 1900df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1901df8bae1dSRodney W. Grimes 1902f591779bSSeigo Tanimura if (pgid != 0) { 1903ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1904f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1905ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1906f591779bSSeigo Tanimura if (pgrp != NULL) { 1907a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 1908f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1909f591779bSSeigo Tanimura } 1910f591779bSSeigo Tanimura } 1911df8bae1dSRodney W. Grimes } 1912df8bae1dSRodney W. Grimes 1913df8bae1dSRodney W. Grimes /* 1914df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1915df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1916df8bae1dSRodney W. Grimes */ 1917df8bae1dSRodney W. Grimes void 1918a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1919df8bae1dSRodney W. Grimes { 1920a3de221dSKonstantin Belousov struct proc *p; 1921df8bae1dSRodney W. Grimes 1922628d2653SJohn Baldwin if (pgrp) { 1923f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1924628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1925628d2653SJohn Baldwin PROC_LOCK(p); 1926e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 1927e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 1928a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 1929628d2653SJohn Baldwin PROC_UNLOCK(p); 1930628d2653SJohn Baldwin } 1931628d2653SJohn Baldwin } 1932df8bae1dSRodney W. Grimes } 1933df8bae1dSRodney W. Grimes 1934e442f29fSKonstantin Belousov 1935e442f29fSKonstantin Belousov /* 1936e442f29fSKonstantin Belousov * Recalculate the signal mask and reset the signal disposition after 1937e442f29fSKonstantin Belousov * usermode frame for delivery is formed. Should be called after 1938e442f29fSKonstantin Belousov * mach-specific routine, because sysent->sv_sendsig() needs correct 1939e442f29fSKonstantin Belousov * ps_siginfo and signal mask. 1940e442f29fSKonstantin Belousov */ 1941e442f29fSKonstantin Belousov static void 1942e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps) 1943e442f29fSKonstantin Belousov { 1944e442f29fSKonstantin Belousov sigset_t mask; 1945e442f29fSKonstantin Belousov 1946e442f29fSKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 1947e442f29fSKonstantin Belousov td->td_ru.ru_nsignals++; 1948e442f29fSKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 1949e442f29fSKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1950e442f29fSKonstantin Belousov SIGADDSET(mask, sig); 1951e442f29fSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 1952e442f29fSKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 1953e442f29fSKonstantin Belousov if (SIGISMEMBER(ps->ps_sigreset, sig)) 1954e442f29fSKonstantin Belousov sigdflt(ps, sig); 1955e442f29fSKonstantin Belousov } 1956e442f29fSKonstantin Belousov 1957e442f29fSKonstantin Belousov 1958df8bae1dSRodney W. Grimes /* 19590c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 19600c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 19610c14ff0eSRobert Watson * normally. 1962df8bae1dSRodney W. Grimes */ 1963df8bae1dSRodney W. Grimes void 19649104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 1965df8bae1dSRodney W. Grimes { 19661bf4700bSJeff Roberson struct sigacts *ps; 19671bf4700bSJeff Roberson struct proc *p; 19689104847fSDavid Xu int sig; 19699104847fSDavid Xu int code; 19701bf4700bSJeff Roberson 19711bf4700bSJeff Roberson p = td->td_proc; 19729104847fSDavid Xu sig = ksi->ksi_signo; 19739104847fSDavid Xu code = ksi->ksi_code; 19749104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 19759104847fSDavid Xu 1976628d2653SJohn Baldwin PROC_LOCK(p); 1977ef3dab76STim J. Robbins ps = p->p_sigacts; 197890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 197990af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 19804093529dSJeff Roberson !SIGISMEMBER(td->td_sigmask, sig)) { 1981df8bae1dSRodney W. Grimes #ifdef KTRACE 1982374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 1983374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 19844093529dSJeff Roberson &td->td_sigmask, code); 1985df8bae1dSRodney W. Grimes #endif 19869104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 19879104847fSDavid Xu ksi, &td->td_sigmask); 1988e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 198990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 19906f841fb7SMarcel Moolenaar } else { 1991f71a882fSDavid Xu /* 1992f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 1993f71a882fSDavid Xu * masking the signal or process is ignoring the 1994f71a882fSDavid Xu * signal. 1995f71a882fSDavid Xu */ 1996f71a882fSDavid Xu if (kern_forcesigexit && 1997f71a882fSDavid Xu (SIGISMEMBER(td->td_sigmask, sig) || 1998f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 1999f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 2000f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 2001f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 2002f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2003f71a882fSDavid Xu } 200490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20056626c604SJulian Elischer p->p_code = code; /* XXX for core dump/debugger */ 20062c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 2007ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 2008df8bae1dSRodney W. Grimes } 2009628d2653SJohn Baldwin PROC_UNLOCK(p); 2010df8bae1dSRodney W. Grimes } 2011df8bae1dSRodney W. Grimes 20124093529dSJeff Roberson static struct thread * 20134093529dSJeff Roberson sigtd(struct proc *p, int sig, int prop) 20144093529dSJeff Roberson { 20153074d1b4SDavid Xu struct thread *td, *signal_td; 20164093529dSJeff Roberson 20174093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 20184093529dSJeff Roberson 20194093529dSJeff Roberson /* 2020627451c1SDavid Xu * Check if current thread can handle the signal without 202115b7a831SKonstantin Belousov * switching context to another thread. 20224093529dSJeff Roberson */ 2023627451c1SDavid Xu if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 2024627451c1SDavid Xu return (curthread); 20253074d1b4SDavid Xu signal_td = NULL; 20263074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 20273074d1b4SDavid Xu if (!SIGISMEMBER(td->td_sigmask, sig)) { 20283074d1b4SDavid Xu signal_td = td; 2029627451c1SDavid Xu break; 20303074d1b4SDavid Xu } 20313074d1b4SDavid Xu } 20323074d1b4SDavid Xu if (signal_td == NULL) 20333074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 20343074d1b4SDavid Xu return (signal_td); 20354093529dSJeff Roberson } 20364093529dSJeff Roberson 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 2039df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 2040df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 2041df8bae1dSRodney W. Grimes * 2042df8bae1dSRodney W. Grimes * Exceptions: 2043df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 2044df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 2045df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 2046df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 2047df8bae1dSRodney W. Grimes * 2048df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 20494dec0e67SRobert Watson * 20504dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 20514dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 20524dec0e67SRobert Watson * side effects of this unwise possibility. 2053df8bae1dSRodney W. Grimes */ 2054df8bae1dSRodney W. Grimes void 20558451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 2056df8bae1dSRodney W. Grimes { 2057a3de221dSKonstantin Belousov ksiginfo_t ksi; 2058a3de221dSKonstantin Belousov 2059a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 2060a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 2061a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 2062ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 2063a3de221dSKonstantin Belousov } 2064a3de221dSKonstantin Belousov 2065ad6eec7bSJohn Baldwin int 2066a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2067a3de221dSKonstantin Belousov { 2068a3de221dSKonstantin Belousov 2069ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 20709104847fSDavid Xu } 20719104847fSDavid Xu 2072cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 20739104847fSDavid Xu int 2074cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd) 20759104847fSDavid Xu { 2076cf7d9a8cSDavid Xu struct thread *td; 207741b3077aSJacques Vidrine 20786d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2079cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 20806d7b314bSDavid Xu if (td == NULL) 20816d7b314bSDavid Xu return (ESRCH); 2082cf7d9a8cSDavid Xu *ttd = td; 2083cf7d9a8cSDavid Xu } else { 2084cf7d9a8cSDavid Xu *ttd = NULL; 2085cf7d9a8cSDavid Xu PROC_LOCK(p); 20866d7b314bSDavid Xu } 2087cf7d9a8cSDavid Xu return (0); 20884093529dSJeff Roberson } 20894093529dSJeff Roberson 2090ad6eec7bSJohn Baldwin void 2091ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2092ad6eec7bSJohn Baldwin { 2093ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2094ad6eec7bSJohn Baldwin 2095ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2096ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2097ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2098ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2099ad6eec7bSJohn Baldwin } 2100ad6eec7bSJohn Baldwin 2101ad6eec7bSJohn Baldwin void 2102ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2103ad6eec7bSJohn Baldwin { 2104ad6eec7bSJohn Baldwin 2105ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2106ad6eec7bSJohn Baldwin } 2107ad6eec7bSJohn Baldwin 2108cf7d9a8cSDavid Xu int 2109ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 21104093529dSJeff Roberson { 21119104847fSDavid Xu sig_t action; 21129104847fSDavid Xu sigqueue_t *sigqueue; 21139104847fSDavid Xu int prop; 211490af4afaSJohn Baldwin struct sigacts *ps; 211594f0972bSDavid Xu int intrval; 21169104847fSDavid Xu int ret = 0; 2117da7bbd2cSJohn Baldwin int wakeup_swapper; 2118df8bae1dSRodney W. Grimes 2119cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 21206d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 21216d7b314bSDavid Xu 212241b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2123212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 21244093529dSJeff Roberson 2125212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 21266d7b314bSDavid Xu 21276d7b314bSDavid Xu /* 21286d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 21296d7b314bSDavid Xu */ 21306d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 21316d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21326d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21336d7b314bSDavid Xu return (ret); 21346d7b314bSDavid Xu } 21356d7b314bSDavid Xu 213690af4afaSJohn Baldwin ps = p->p_sigacts; 21379e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 21382c42a146SMarcel Moolenaar prop = sigprop(sig); 21394093529dSJeff Roberson 21406d7b314bSDavid Xu if (td == NULL) { 21416d7b314bSDavid Xu td = sigtd(p, sig, prop); 21426d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 2143462314b3SMateusz Guzik } else 21449104847fSDavid Xu sigqueue = &td->td_sigqueue; 21454093529dSJeff Roberson 214636160958SMark Johnston SDT_PROBE3(proc, , , signal__send, td, p, sig); 21475d217f17SJohn Birrell 2148df8bae1dSRodney W. Grimes /* 2149df8bae1dSRodney W. Grimes * If the signal is being ignored, 2150df8bae1dSRodney W. Grimes * then we forget about it immediately. 215190af4afaSJohn Baldwin * (Note: we don't set SIGCONT in ps_sigignore, 2152df8bae1dSRodney W. Grimes * and if it is set to SIG_IGN, 2153df8bae1dSRodney W. Grimes * action will be SIG_DFL here.) 2154df8bae1dSRodney W. Grimes */ 215590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 21560fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 215736160958SMark Johnston SDT_PROBE3(proc, , , signal__discard, td, p, sig); 21585d217f17SJohn Birrell 215990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 21606d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21616d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21629104847fSDavid Xu return (ret); 216390af4afaSJohn Baldwin } 21646675b36eSDavid Xu if (SIGISMEMBER(td->td_sigmask, sig)) 2165df8bae1dSRodney W. Grimes action = SIG_HOLD; 216690af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2167df8bae1dSRodney W. Grimes action = SIG_CATCH; 2168df8bae1dSRodney W. Grimes else 2169df8bae1dSRodney W. Grimes action = SIG_DFL; 217094f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 217194f0972bSDavid Xu intrval = EINTR; 217294f0972bSDavid Xu else 217394f0972bSDavid Xu intrval = ERESTART; 217490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2175df8bae1dSRodney W. Grimes 2176fd50a707SBrooks Davis if (prop & SIGPROP_CONT) 21779104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 2178fd50a707SBrooks Davis else if (prop & SIGPROP_STOP) { 2179df8bae1dSRodney W. Grimes /* 2180df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2181df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2182df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2183df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2184df8bae1dSRodney W. Grimes */ 2185fd50a707SBrooks Davis if ((prop & SIGPROP_TTYSTOP) && 2186e602ba25SJulian Elischer (p->p_pgrp->pg_jobc == 0) && 21876d7b314bSDavid Xu (action == SIG_DFL)) { 21886d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21896d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21909104847fSDavid Xu return (ret); 21916d7b314bSDavid Xu } 21929104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2193ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 21946933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2195ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2196ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2197ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2198ebceaf6dSDavid Xu } 2199df8bae1dSRodney W. Grimes } 22003074d1b4SDavid Xu 22019104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 22029104847fSDavid Xu if (ret != 0) 22039104847fSDavid Xu return (ret); 22046d7b314bSDavid Xu signotify(td); 22055312b1c7SDavid Xu /* 22065312b1c7SDavid Xu * Defer further processing for signals which are held, 22075312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 22085312b1c7SDavid Xu */ 22095312b1c7SDavid Xu if (action == SIG_HOLD && 2210fd50a707SBrooks Davis !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 22119104847fSDavid Xu return (ret); 2212b4d33259SEric Badger 2213b4d33259SEric Badger /* SIGKILL: Remove procfs STOPEVENTs. */ 221490d75f78SAlfred Perlstein if (sig == SIGKILL) { 221590d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCBIC */ 221690d75f78SAlfred Perlstein p->p_stops = 0; 221790d75f78SAlfred Perlstein /* from procfs_ioctl.c: PIOCCONT */ 221890d75f78SAlfred Perlstein p->p_step = 0; 221990d75f78SAlfred Perlstein wakeup(&p->p_step); 222090d75f78SAlfred Perlstein } 222190d75f78SAlfred Perlstein /* 2222e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2223e602ba25SJulian Elischer * component. Most processing occurs when the process next 2224e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 22251968f37bSJohn Baldwin * times when processing needs to be done immediately, such as 2226e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 22271968f37bSJohn Baldwin * We try to do the per-process part here. 2228df8bae1dSRodney W. Grimes */ 2229e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 22300f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 22310f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2232e602ba25SJulian Elischer if (sig == SIGKILL) { 2233137cf33dSDavid Xu /* 2234137cf33dSDavid Xu * If traced process is already stopped, 2235137cf33dSDavid Xu * then no further action is necessary. 2236137cf33dSDavid Xu */ 223783b718ebSDavid Xu if (p->p_flag & P_TRACED) 223883b718ebSDavid Xu goto out; 2239df8bae1dSRodney W. Grimes /* 2240e602ba25SJulian Elischer * SIGKILL sets process running. 2241e602ba25SJulian Elischer * It will die elsewhere. 2242e602ba25SJulian Elischer * All threads must be restarted. 2243df8bae1dSRodney W. Grimes */ 2244482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2245e602ba25SJulian Elischer goto runfast; 2246e602ba25SJulian Elischer } 2247e602ba25SJulian Elischer 2248fd50a707SBrooks Davis if (prop & SIGPROP_CONT) { 2249137cf33dSDavid Xu /* 2250137cf33dSDavid Xu * If traced process is already stopped, 2251137cf33dSDavid Xu * then no further action is necessary. 2252137cf33dSDavid Xu */ 225383b718ebSDavid Xu if (p->p_flag & P_TRACED) 225483b718ebSDavid Xu goto out; 2255e602ba25SJulian Elischer /* 2256e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 22579104847fSDavid Xu * process but don't leave the signal in sigqueue as 22581d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2259e602ba25SJulian Elischer * continue the process and leave the signal in 22609104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2261e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2262e602ba25SJulian Elischer * an event, it goes back to run state. 2263e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2264e602ba25SJulian Elischer */ 22651279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 22667b4a950aSDavid Xu PROC_SLOCK(p); 2267ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 22687b4a950aSDavid Xu PROC_SUNLOCK(p); 22696933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 2270b4490c6eSKonstantin Belousov p->p_xsig = SIGCONT; 22717f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2272ebceaf6dSDavid Xu childproc_continued(p); 22737f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 22747b4a950aSDavid Xu PROC_SLOCK(p); 2275ebceaf6dSDavid Xu } 2276e602ba25SJulian Elischer if (action == SIG_DFL) { 2277a54e85fdSJeff Roberson thread_unsuspend(p); 22787b4a950aSDavid Xu PROC_SUNLOCK(p); 22799104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2280a54e85fdSJeff Roberson goto out; 2281a54e85fdSJeff Roberson } 2282a54e85fdSJeff Roberson if (action == SIG_CATCH) { 22838460a577SJohn Birrell /* 22848460a577SJohn Birrell * The process wants to catch it so it needs 22858460a577SJohn Birrell * to run at least one thread, but which one? 22868460a577SJohn Birrell */ 22877b4a950aSDavid Xu PROC_SUNLOCK(p); 2288e602ba25SJulian Elischer goto runfast; 2289e602ba25SJulian Elischer } 2290e602ba25SJulian Elischer /* 2291e602ba25SJulian Elischer * The signal is not ignored or caught. 2292e602ba25SJulian Elischer */ 229304774f23SJulian Elischer thread_unsuspend(p); 22947b4a950aSDavid Xu PROC_SUNLOCK(p); 2295e602ba25SJulian Elischer goto out; 2296e602ba25SJulian Elischer } 2297e602ba25SJulian Elischer 2298fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2299137cf33dSDavid Xu /* 2300137cf33dSDavid Xu * If traced process is already stopped, 2301137cf33dSDavid Xu * then no further action is necessary. 2302137cf33dSDavid Xu */ 230383b718ebSDavid Xu if (p->p_flag & P_TRACED) 230483b718ebSDavid Xu goto out; 2305e602ba25SJulian Elischer /* 2306e602ba25SJulian Elischer * Already stopped, don't need to stop again 2307e602ba25SJulian Elischer * (If we did the shell could get confused). 230804774f23SJulian Elischer * Just make sure the signal STOP bit set. 2309e602ba25SJulian Elischer */ 23101279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 23119104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2312e602ba25SJulian Elischer goto out; 2313e602ba25SJulian Elischer } 2314e602ba25SJulian Elischer 2315e602ba25SJulian Elischer /* 2316e602ba25SJulian Elischer * All other kinds of signals: 2317e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2318e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2319e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 232004774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2321e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2322e602ba25SJulian Elischer */ 2323da7bbd2cSJohn Baldwin wakeup_swapper = 0; 23247b4a950aSDavid Xu PROC_SLOCK(p); 2325a54e85fdSJeff Roberson thread_lock(td); 232644f3b092SJohn Baldwin if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2327da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2328a54e85fdSJeff Roberson thread_unlock(td); 23297b4a950aSDavid Xu PROC_SUNLOCK(p); 2330da7bbd2cSJohn Baldwin if (wakeup_swapper) 2331da7bbd2cSJohn Baldwin kick_proc0(); 2332df8bae1dSRodney W. Grimes goto out; 2333df8bae1dSRodney W. Grimes /* 23349a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 23359a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2336df8bae1dSRodney W. Grimes */ 2337e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2338ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 233994f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2340df8bae1dSRodney W. Grimes goto out; 234104774f23SJulian Elischer } 2342ec8297bdSDavid Xu 2343ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2344ec8297bdSDavid Xu 2345fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 23460f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 234735c32a76SDavid Xu goto out; 2348e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2349b4490c6eSKonstantin Belousov p->p_xsig = sig; 23507b4a950aSDavid Xu PROC_SLOCK(p); 23516f56cb8dSKonstantin Belousov wakeup_swapper = sig_suspend_threads(td, p, 1); 23524093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2353ebceaf6dSDavid Xu /* 2354ebceaf6dSDavid Xu * only thread sending signal to another 2355ebceaf6dSDavid Xu * process can reach here, if thread is sending 2356ebceaf6dSDavid Xu * signal to its process, because thread does 2357ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2358ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2359ebceaf6dSDavid Xu */ 2360ebceaf6dSDavid Xu thread_stopped(p); 23617b4a950aSDavid Xu PROC_SUNLOCK(p); 2362b4490c6eSKonstantin Belousov sigqueue_delete_proc(p, p->p_xsig); 23637b4a950aSDavid Xu } else 23647b4a950aSDavid Xu PROC_SUNLOCK(p); 23656f56cb8dSKonstantin Belousov if (wakeup_swapper) 23666f56cb8dSKonstantin Belousov kick_proc0(); 236735c32a76SDavid Xu goto out; 236835c32a76SDavid Xu } 2369e602ba25SJulian Elischer } else { 2370e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 23719104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2372e602ba25SJulian Elischer goto out; 2373e602ba25SJulian Elischer } 2374e602ba25SJulian Elischer 2375b40ce416SJulian Elischer /* 2376e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2377e602ba25SJulian Elischer * running threads. 2378b40ce416SJulian Elischer */ 2379e602ba25SJulian Elischer runfast: 238094f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 23817b4a950aSDavid Xu PROC_SLOCK(p); 2382e602ba25SJulian Elischer thread_unsuspend(p); 23837b4a950aSDavid Xu PROC_SUNLOCK(p); 2384e602ba25SJulian Elischer out: 23857b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 23867b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 23879104847fSDavid Xu return (ret); 2388e602ba25SJulian Elischer } 2389e602ba25SJulian Elischer 2390e602ba25SJulian Elischer /* 2391e602ba25SJulian Elischer * The force of a signal has been directed against a single 2392e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2393e602ba25SJulian Elischer * out of any sleep it may be in etc. 2394e602ba25SJulian Elischer */ 2395e602ba25SJulian Elischer static void 239694f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2397e602ba25SJulian Elischer { 2398e602ba25SJulian Elischer struct proc *p = td->td_proc; 23993e85b721SEd Maste int prop; 2400da7bbd2cSJohn Baldwin int wakeup_swapper; 2401e602ba25SJulian Elischer 2402da7bbd2cSJohn Baldwin wakeup_swapper = 0; 24038b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2404e602ba25SJulian Elischer prop = sigprop(sig); 2405a4c2da15SBruce Evans 24067b4a950aSDavid Xu PROC_SLOCK(p); 2407374ae2a3SJeff Roberson thread_lock(td); 2408e602ba25SJulian Elischer /* 2409aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2410aef68c96SKonstantin Belousov * killed in this lifetime. Be careful to avoid bumping the 2411aef68c96SKonstantin Belousov * priority of the idle thread, since we still allow to signal 2412aef68c96SKonstantin Belousov * kernel processes. 2413e602ba25SJulian Elischer */ 2414fd50a707SBrooks Davis if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2415aef68c96SKonstantin Belousov td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2416b3a4fb14SDavid Xu sched_prio(td, PUSER); 241780c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2418e602ba25SJulian Elischer /* 2419e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2420e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2421e602ba25SJulian Elischer * be noticed when the process returns through 2422e602ba25SJulian Elischer * trap() or syscall(). 2423e602ba25SJulian Elischer */ 242444f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2425374ae2a3SJeff Roberson goto out; 2426e602ba25SJulian Elischer /* 2427e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2428e602ba25SJulian Elischer * asleep, we are finished; the process should not 2429e602ba25SJulian Elischer * be awakened. 2430df8bae1dSRodney W. Grimes */ 2431fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2432a54e85fdSJeff Roberson thread_unlock(td); 24337b4a950aSDavid Xu PROC_SUNLOCK(p); 24349104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 24354093529dSJeff Roberson /* 24364093529dSJeff Roberson * It may be on either list in this state. 24374093529dSJeff Roberson * Remove from both for now. 24384093529dSJeff Roberson */ 24399104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2440aa0fa334SJulian Elischer return; 2441df8bae1dSRodney W. Grimes } 2442df8bae1dSRodney W. Grimes 2443aa0fa334SJulian Elischer /* 2444a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2445a120a7a3SJohn Baldwin * STOP signal is deferred. 2446a120a7a3SJohn Baldwin */ 2447fd50a707SBrooks Davis if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 24486f56cb8dSKonstantin Belousov TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2449a120a7a3SJohn Baldwin goto out; 2450a120a7a3SJohn Baldwin 2451a120a7a3SJohn Baldwin /* 2452a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2453aa0fa334SJulian Elischer */ 2454aef68c96SKonstantin Belousov if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2455b3a4fb14SDavid Xu sched_prio(td, PUSER); 2456ec8297bdSDavid Xu 2457da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_abort(td, intrval); 2458a4c2da15SBruce Evans } else { 2459df8bae1dSRodney W. Grimes /* 2460a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2461df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2462df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2463df8bae1dSRodney W. Grimes */ 2464a4c2da15SBruce Evans #ifdef SMP 246544f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2466e602ba25SJulian Elischer forward_signal(td); 24673163861cSTor Egge #endif 24686caa8a15SJohn Baldwin } 2469374ae2a3SJeff Roberson out: 24707b4a950aSDavid Xu PROC_SUNLOCK(p); 2471374ae2a3SJeff Roberson thread_unlock(td); 2472da7bbd2cSJohn Baldwin if (wakeup_swapper) 2473da7bbd2cSJohn Baldwin kick_proc0(); 2474a4c2da15SBruce Evans } 2475df8bae1dSRodney W. Grimes 24766f56cb8dSKonstantin Belousov static int 2477d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2478d8267df7SDavid Xu { 2479d8267df7SDavid Xu struct thread *td2; 24806f56cb8dSKonstantin Belousov int wakeup_swapper; 2481d8267df7SDavid Xu 2482d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 24837b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 24840c46712cSMark Johnston MPASS(sending || td == curthread); 2485d8267df7SDavid Xu 24866f56cb8dSKonstantin Belousov wakeup_swapper = 0; 2487d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2488a54e85fdSJeff Roberson thread_lock(td2); 2489b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2490d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2491f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2492f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2493a120a7a3SJohn Baldwin /* 2494a120a7a3SJohn Baldwin * Once a thread is asleep with 24956f56cb8dSKonstantin Belousov * TDF_SBDRY and without TDF_SERESTART 24966f56cb8dSKonstantin Belousov * or TDF_SEINTR set, it should never 2497a120a7a3SJohn Baldwin * become suspended due to this check. 2498a120a7a3SJohn Baldwin */ 2499a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2500a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 25010c46712cSMark Johnston if (TD_SBDRY_INTR(td2)) 250246e47c4fSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, 250346e47c4fSKonstantin Belousov TD_SBDRY_ERRNO(td2)); 2504f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2505d8267df7SDavid Xu thread_suspend_one(td2); 2506f33a947bSKonstantin Belousov } 2507f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2508d8267df7SDavid Xu if (sending || td != td2) 2509d8267df7SDavid Xu td2->td_flags |= TDF_ASTPENDING; 2510d8267df7SDavid Xu #ifdef SMP 2511d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2512d8267df7SDavid Xu forward_signal(td2); 2513d8267df7SDavid Xu #endif 2514d8267df7SDavid Xu } 2515a54e85fdSJeff Roberson thread_unlock(td2); 2516d8267df7SDavid Xu } 25176f56cb8dSKonstantin Belousov return (wakeup_swapper); 2518d8267df7SDavid Xu } 2519d8267df7SDavid Xu 252082a4538fSEric Badger /* 252182a4538fSEric Badger * Stop the process for an event deemed interesting to the debugger. If si is 252282a4538fSEric Badger * non-NULL, this is a signal exchange; the new signal requested by the 252382a4538fSEric Badger * debugger will be returned for handling. If si is NULL, this is some other 252482a4538fSEric Badger * type of interesting event. The debugger may request a signal be delivered in 252582a4538fSEric Badger * that case as well, however it will be deferred until it can be handled. 252682a4538fSEric Badger */ 2527cbf4e354SDavid Xu int 252882a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si) 25294cc9f52fSRobert Drehmel { 25304cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 253182a4538fSEric Badger struct thread *td2; 253282a4538fSEric Badger ksiginfo_t ksi; 253382a4538fSEric Badger int prop; 25344cc9f52fSRobert Drehmel 253530a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 25360f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 25374cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2538aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 25394cc9f52fSRobert Drehmel 2540cbf4e354SDavid Xu td->td_xsig = sig; 254182a4538fSEric Badger 254282a4538fSEric Badger if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 254382a4538fSEric Badger td->td_dbgflags |= TDB_XSIG; 2544515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2545515b7a0bSJohn Baldwin td->td_tid, p->p_pid, td->td_dbgflags, sig); 25467b4a950aSDavid Xu PROC_SLOCK(p); 2547904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 254882a4538fSEric Badger if (P_KILLED(p)) { 254982a4538fSEric Badger /* 255082a4538fSEric Badger * Ensure that, if we've been PT_KILLed, the 255182a4538fSEric Badger * exit status reflects that. Another thread 255282a4538fSEric Badger * may also be in ptracestop(), having just 255382a4538fSEric Badger * received the SIGKILL, but this thread was 255482a4538fSEric Badger * unsuspended first. 255582a4538fSEric Badger */ 255682a4538fSEric Badger td->td_dbgflags &= ~TDB_XSIG; 255782a4538fSEric Badger td->td_xsig = SIGKILL; 255882a4538fSEric Badger p->p_ptevents = 0; 255982a4538fSEric Badger break; 256082a4538fSEric Badger } 25615fcfab6eSJohn Baldwin if (p->p_flag & P_SINGLE_EXIT && 25625fcfab6eSJohn Baldwin !(td->td_dbgflags & TDB_EXIT)) { 25635fcfab6eSJohn Baldwin /* 25645fcfab6eSJohn Baldwin * Ignore ptrace stops except for thread exit 25655fcfab6eSJohn Baldwin * events when the process exits. 25665fcfab6eSJohn Baldwin */ 2567904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 25687b4a950aSDavid Xu PROC_SUNLOCK(p); 256982a4538fSEric Badger return (0); 2570cbf4e354SDavid Xu } 2571b7a25e63SKonstantin Belousov 2572cbf4e354SDavid Xu /* 2573b7a25e63SKonstantin Belousov * Make wait(2) work. Ensure that right after the 2574b7a25e63SKonstantin Belousov * attach, the thread which was decided to become the 2575b7a25e63SKonstantin Belousov * leader of attach gets reported to the waiter. 2576b7a25e63SKonstantin Belousov * Otherwise, just avoid overwriting another thread's 2577b7a25e63SKonstantin Belousov * assignment to p_xthread. If another thread has 2578b7a25e63SKonstantin Belousov * already set p_xthread, the current thread will get 2579b7a25e63SKonstantin Belousov * a chance to report itself upon the next iteration. 2580cbf4e354SDavid Xu */ 2581b7a25e63SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0 || 25827f649ddaSMark Johnston ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2583b7a25e63SKonstantin Belousov p->p_xthread == NULL)) { 2584b4490c6eSKonstantin Belousov p->p_xsig = sig; 2585cbf4e354SDavid Xu p->p_xthread = td; 2586b7a25e63SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 2587b7a25e63SKonstantin Belousov p->p_flag2 &= ~P2_PTRACE_FSTP; 2588b7a25e63SKonstantin Belousov p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2589d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 2590b7a25e63SKonstantin Belousov } 25916fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 25926fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 25936fa39a73SKonstantin Belousov cv_broadcast(&p->p_dbgwait); 25946fa39a73SKonstantin Belousov } 2595cbf4e354SDavid Xu stopme: 25966ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 25977ce60f60SDavid Xu if (p->p_xthread == td) 25987ce60f60SDavid Xu p->p_xthread = NULL; 25997ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2600cbf4e354SDavid Xu break; 2601904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2602cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2603cbf4e354SDavid Xu break; 2604cbf4e354SDavid Xu goto stopme; 2605cbf4e354SDavid Xu } 2606cbf4e354SDavid Xu } 26077b4a950aSDavid Xu PROC_SUNLOCK(p); 260882a4538fSEric Badger } 260982a4538fSEric Badger 261082a4538fSEric Badger if (si != NULL && sig == td->td_xsig) { 261182a4538fSEric Badger /* Parent wants us to take the original signal unchanged. */ 261282a4538fSEric Badger si->ksi_flags |= KSI_HEAD; 261382a4538fSEric Badger if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 261482a4538fSEric Badger si->ksi_signo = 0; 261582a4538fSEric Badger } else if (td->td_xsig != 0) { 261682a4538fSEric Badger /* 261782a4538fSEric Badger * If parent wants us to take a new signal, then it will leave 261882a4538fSEric Badger * it in td->td_xsig; otherwise we just look for signals again. 261982a4538fSEric Badger */ 262082a4538fSEric Badger ksiginfo_init(&ksi); 262182a4538fSEric Badger ksi.ksi_signo = td->td_xsig; 262282a4538fSEric Badger ksi.ksi_flags |= KSI_PTRACE; 262382a4538fSEric Badger prop = sigprop(td->td_xsig); 262482a4538fSEric Badger td2 = sigtd(p, td->td_xsig, prop); 262582a4538fSEric Badger tdsendsignal(p, td2, td->td_xsig, &ksi); 262682a4538fSEric Badger if (td != td2) 262782a4538fSEric Badger return (0); 262882a4538fSEric Badger } 262982a4538fSEric Badger 2630cbf4e354SDavid Xu return (td->td_xsig); 26314cc9f52fSRobert Drehmel } 26324cc9f52fSRobert Drehmel 26336b286ee8SKonstantin Belousov static void 263480a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 26356b286ee8SKonstantin Belousov { 26366b286ee8SKonstantin Belousov struct sigacts *ps; 26376b286ee8SKonstantin Belousov struct thread *td; 2638407af02bSDavid Xu int sig; 26396b286ee8SKonstantin Belousov 26406b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 264170778bbaSKonstantin Belousov ps = p->p_sigacts; 264270778bbaSKonstantin Belousov mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ? 264370778bbaSKonstantin Belousov MA_OWNED : MA_NOTOWNED); 2644407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2645407af02bSDavid Xu return; 2646407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2647407af02bSDavid Xu while ((sig = sig_ffs(&block)) != 0) { 2648407af02bSDavid Xu SIGDELSET(block, sig); 2649407af02bSDavid Xu td = sigtd(p, sig, 0); 26506b286ee8SKonstantin Belousov signotify(td); 265180a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 26526b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2653396a0d44SKonstantin Belousov if (p->p_flag & P_TRACED || 2654396a0d44SKonstantin Belousov (SIGISMEMBER(ps->ps_sigcatch, sig) && 2655396a0d44SKonstantin Belousov !SIGISMEMBER(td->td_sigmask, sig))) 2656407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2657407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 26586b286ee8SKonstantin Belousov ERESTART)); 265980a8b0f3SKonstantin Belousov if (!(flags & SIGPROCMASK_PS_LOCKED)) 26606b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 26616b286ee8SKonstantin Belousov } 26626b286ee8SKonstantin Belousov } 26636b286ee8SKonstantin Belousov 26646b286ee8SKonstantin Belousov void 26656b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 26666b286ee8SKonstantin Belousov { 26676b286ee8SKonstantin Belousov struct proc *p; 26686b286ee8SKonstantin Belousov sigset_t unblocked; 26696b286ee8SKonstantin Belousov 26706b286ee8SKonstantin Belousov p = td->td_proc; 26716b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 26726b286ee8SKonstantin Belousov 26736b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 26746b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 26756b286ee8SKonstantin Belousov return; 26766b286ee8SKonstantin Belousov 26776b286ee8SKonstantin Belousov /* 26786b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 26796b286ee8SKonstantin Belousov * about this by filling the sigmask. 26806b286ee8SKonstantin Belousov * 26816b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 26826b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 26836b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 26846b286ee8SKonstantin Belousov */ 26856b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 26866b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 26876b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 268880a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 26896b286ee8SKonstantin Belousov 26906b286ee8SKonstantin Belousov } 26916b286ee8SKonstantin Belousov 26923a1e5dd8SKonstantin Belousov static int 26933a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags) 2694a120a7a3SJohn Baldwin { 2695a120a7a3SJohn Baldwin 26963a1e5dd8SKonstantin Belousov MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 26973a1e5dd8SKonstantin Belousov (cflags & TDF_SBDRY) != 0); 26983a1e5dd8SKonstantin Belousov return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 2699a120a7a3SJohn Baldwin } 2700a120a7a3SJohn Baldwin 2701a120a7a3SJohn Baldwin /* 27023a1e5dd8SKonstantin Belousov * Defer the delivery of SIGSTOP for the current thread, according to 27033a1e5dd8SKonstantin Belousov * the requested mode. Returns previous flags, which must be restored 27043a1e5dd8SKonstantin Belousov * by sigallowstop(). 27053a1e5dd8SKonstantin Belousov * 27063a1e5dd8SKonstantin Belousov * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 27073a1e5dd8SKonstantin Belousov * cleared by the current thread, which allow the lock-less read-only 27083a1e5dd8SKonstantin Belousov * accesses below. 2709a120a7a3SJohn Baldwin */ 2710e3612a4cSKonstantin Belousov int 271146e47c4fSKonstantin Belousov sigdeferstop_impl(int mode) 2712a120a7a3SJohn Baldwin { 2713593efaf9SJohn Baldwin struct thread *td; 27143a1e5dd8SKonstantin Belousov int cflags, nflags; 2715a120a7a3SJohn Baldwin 2716593efaf9SJohn Baldwin td = curthread; 27173a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 27183a1e5dd8SKonstantin Belousov switch (mode) { 27193a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_NOP: 27203a1e5dd8SKonstantin Belousov nflags = cflags; 27213a1e5dd8SKonstantin Belousov break; 27223a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_OFF: 27233a1e5dd8SKonstantin Belousov nflags = 0; 27243a1e5dd8SKonstantin Belousov break; 27253a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_SILENT: 27263a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 27273a1e5dd8SKonstantin Belousov break; 27283a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_EINTR: 27293a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 27303a1e5dd8SKonstantin Belousov break; 27313a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_ERESTART: 27323a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 27333a1e5dd8SKonstantin Belousov break; 27343a1e5dd8SKonstantin Belousov default: 27353a1e5dd8SKonstantin Belousov panic("sigdeferstop: invalid mode %x", mode); 27363a1e5dd8SKonstantin Belousov break; 27373a1e5dd8SKonstantin Belousov } 273846e47c4fSKonstantin Belousov if (cflags == nflags) 273946e47c4fSKonstantin Belousov return (SIGDEFERSTOP_VAL_NCHG); 2740a120a7a3SJohn Baldwin thread_lock(td); 27413a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | nflags; 2742a120a7a3SJohn Baldwin thread_unlock(td); 27433a1e5dd8SKonstantin Belousov return (cflags); 27443a1e5dd8SKonstantin Belousov } 27453a1e5dd8SKonstantin Belousov 27463a1e5dd8SKonstantin Belousov /* 27473a1e5dd8SKonstantin Belousov * Restores the STOP handling mode, typically permitting the delivery 27483a1e5dd8SKonstantin Belousov * of SIGSTOP for the current thread. This does not immediately 27493a1e5dd8SKonstantin Belousov * suspend if a stop was posted. Instead, the thread will suspend 27503a1e5dd8SKonstantin Belousov * either via ast() or a subsequent interruptible sleep. 27513a1e5dd8SKonstantin Belousov */ 27523a1e5dd8SKonstantin Belousov void 275346e47c4fSKonstantin Belousov sigallowstop_impl(int prev) 27543a1e5dd8SKonstantin Belousov { 27553a1e5dd8SKonstantin Belousov struct thread *td; 27563a1e5dd8SKonstantin Belousov int cflags; 27573a1e5dd8SKonstantin Belousov 275846e47c4fSKonstantin Belousov KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 27593a1e5dd8SKonstantin Belousov KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 27603a1e5dd8SKonstantin Belousov ("sigallowstop: incorrect previous mode %x", prev)); 27613a1e5dd8SKonstantin Belousov td = curthread; 27623a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 27633a1e5dd8SKonstantin Belousov if (cflags != prev) { 27643a1e5dd8SKonstantin Belousov thread_lock(td); 27653a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | prev; 27663a1e5dd8SKonstantin Belousov thread_unlock(td); 27673a1e5dd8SKonstantin Belousov } 2768a120a7a3SJohn Baldwin } 2769a120a7a3SJohn Baldwin 2770df8bae1dSRodney W. Grimes /* 2771df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 2772df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 2773df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 2774df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 2775df8bae1dSRodney W. Grimes * a syscall or trap (though this can usually be done without calling issignal 2776628855e7SJulian Elischer * by checking the pending signal masks in cursig.) The normal call 2777df8bae1dSRodney W. Grimes * sequence is 2778df8bae1dSRodney W. Grimes * 2779e602ba25SJulian Elischer * while (sig = cursig(curthread)) 27802c42a146SMarcel Moolenaar * postsig(sig); 2781df8bae1dSRodney W. Grimes */ 27826711f10fSJohn Baldwin static int 27833cf3b9f0SJohn Baldwin issignal(struct thread *td) 2784df8bae1dSRodney W. Grimes { 2785e602ba25SJulian Elischer struct proc *p; 278690af4afaSJohn Baldwin struct sigacts *ps; 27876b286ee8SKonstantin Belousov struct sigqueue *queue; 27884093529dSJeff Roberson sigset_t sigpending; 278982a4538fSEric Badger int sig, prop; 279086be94fcSTycho Nightingale ksiginfo_t ksi; 2791df8bae1dSRodney W. Grimes 2792e602ba25SJulian Elischer p = td->td_proc; 279390af4afaSJohn Baldwin ps = p->p_sigacts; 279490af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 2795628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2796df8bae1dSRodney W. Grimes for (;;) { 27972a024a2bSSean Eric Fagan int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 27982a024a2bSSean Eric Fagan 27999104847fSDavid Xu sigpending = td->td_sigqueue.sq_signals; 28006b286ee8SKonstantin Belousov SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 28014093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 28024093529dSJeff Roberson 28036f56cb8dSKonstantin Belousov if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 28046f56cb8dSKonstantin Belousov (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 28054093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 28064093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 2807df8bae1dSRodney W. Grimes return (0); 2808b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 2809b7a25e63SKonstantin Belousov (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 2810b7a25e63SKonstantin Belousov SIGISMEMBER(sigpending, SIGSTOP)) { 2811b7a25e63SKonstantin Belousov /* 2812b7a25e63SKonstantin Belousov * If debugger just attached, always consume 2813b7a25e63SKonstantin Belousov * SIGSTOP from ptrace(PT_ATTACH) first, to 2814b7a25e63SKonstantin Belousov * execute the debugger attach ritual in 2815b7a25e63SKonstantin Belousov * order. 2816b7a25e63SKonstantin Belousov */ 2817b7a25e63SKonstantin Belousov sig = SIGSTOP; 2818b7a25e63SKonstantin Belousov td->td_dbgflags |= TDB_FSTP; 2819b7a25e63SKonstantin Belousov } else { 28204093529dSJeff Roberson sig = sig_ffs(&sigpending); 2821b7a25e63SKonstantin Belousov } 28222a024a2bSSean Eric Fagan 2823047aa39bSRobert Watson if (p->p_stops & S_SIG) { 2824047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 2825047aa39bSRobert Watson stopevent(p, S_SIG, sig); 2826047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 2827047aa39bSRobert Watson } 28282a024a2bSSean Eric Fagan 2829df8bae1dSRodney W. Grimes /* 2830df8bae1dSRodney W. Grimes * We should see pending but ignored signals 2831df8bae1dSRodney W. Grimes * only if P_TRACED was on when they were posted. 2832df8bae1dSRodney W. Grimes */ 283390af4afaSJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 28349104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 28356b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2836df8bae1dSRodney W. Grimes continue; 2837df8bae1dSRodney W. Grimes } 2838b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 2839df8bae1dSRodney W. Grimes /* 2840d8f4f6a4SJonathan Mini * If traced, always stop. 28416a671a6bSKonstantin Belousov * Remove old signal from queue before the stop. 28426a671a6bSKonstantin Belousov * XXX shrug off debugger, it causes siginfo to 28436a671a6bSKonstantin Belousov * be thrown away. 2844df8bae1dSRodney W. Grimes */ 28456a671a6bSKonstantin Belousov queue = &td->td_sigqueue; 284686be94fcSTycho Nightingale ksiginfo_init(&ksi); 284786be94fcSTycho Nightingale if (sigqueue_get(queue, sig, &ksi) == 0) { 28486a671a6bSKonstantin Belousov queue = &p->p_sigqueue; 284986be94fcSTycho Nightingale sigqueue_get(queue, sig, &ksi); 28506a671a6bSKonstantin Belousov } 285186be94fcSTycho Nightingale td->td_si = ksi.ksi_info; 28526a671a6bSKonstantin Belousov 285390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 285486be94fcSTycho Nightingale sig = ptracestop(td, sig, &ksi); 285590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2856df8bae1dSRodney W. Grimes 2857df8bae1dSRodney W. Grimes /* 285882a4538fSEric Badger * Keep looking if the debugger discarded the signal 285982a4538fSEric Badger * or replaced it with a masked signal. 286082a4538fSEric Badger * 28618d542cb5SDavid E. O'Brien * If the traced bit got turned off, go back up 28628d542cb5SDavid E. O'Brien * to the top to rescan signals. This ensures 28638d542cb5SDavid E. O'Brien * that p_sig* and p_sigact are consistent. 28648d542cb5SDavid E. O'Brien */ 286582a4538fSEric Badger if (sig == 0 || (p->p_flag & P_TRACED) == 0) 28668d542cb5SDavid E. O'Brien continue; 2867df8bae1dSRodney W. Grimes } 2868df8bae1dSRodney W. Grimes 28698d542cb5SDavid E. O'Brien prop = sigprop(sig); 28708d542cb5SDavid E. O'Brien 2871df8bae1dSRodney W. Grimes /* 2872df8bae1dSRodney W. Grimes * Decide whether the signal should be returned. 2873df8bae1dSRodney W. Grimes * Return the signal's number, or fall through 2874df8bae1dSRodney W. Grimes * to clear it from the pending mask. 2875df8bae1dSRodney W. Grimes */ 2876d321df47SPoul-Henning Kamp switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2877df8bae1dSRodney W. Grimes 2878d321df47SPoul-Henning Kamp case (intptr_t)SIG_DFL: 2879df8bae1dSRodney W. Grimes /* 2880df8bae1dSRodney W. Grimes * Don't take default actions on system processes. 2881df8bae1dSRodney W. Grimes */ 2882df8bae1dSRodney W. Grimes if (p->p_pid <= 1) { 2883df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 2884df8bae1dSRodney W. Grimes /* 2885df8bae1dSRodney W. Grimes * Are you sure you want to ignore SIGSEGV 2886df8bae1dSRodney W. Grimes * in init? XXX 2887df8bae1dSRodney W. Grimes */ 2888d93f860cSPoul-Henning Kamp printf("Process (pid %lu) got signal %d\n", 28892c42a146SMarcel Moolenaar (u_long)p->p_pid, sig); 2890df8bae1dSRodney W. Grimes #endif 2891df8bae1dSRodney W. Grimes break; /* == ignore */ 2892df8bae1dSRodney W. Grimes } 2893df8bae1dSRodney W. Grimes /* 2894b38bd91fSEric Badger * If there is a pending stop signal to process with 2895b38bd91fSEric Badger * default action, stop here, then clear the signal. 2896b38bd91fSEric Badger * Traced or exiting processes should ignore stops. 2897b38bd91fSEric Badger * Additionally, a member of an orphaned process group 2898b38bd91fSEric Badger * should ignore tty stops. 2899df8bae1dSRodney W. Grimes */ 2900fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2901b38bd91fSEric Badger if (p->p_flag & 2902b38bd91fSEric Badger (P_TRACED | P_WEXIT | P_SINGLE_EXIT) || 2903df8bae1dSRodney W. Grimes (p->p_pgrp->pg_jobc == 0 && 2904fd50a707SBrooks Davis prop & SIGPROP_TTYSTOP)) 2905df8bae1dSRodney W. Grimes break; /* == ignore */ 290646e47c4fSKonstantin Belousov if (TD_SBDRY_INTR(td)) { 290746e47c4fSKonstantin Belousov KASSERT((td->td_flags & TDF_SBDRY) != 0, 290846e47c4fSKonstantin Belousov ("lost TDF_SBDRY")); 290946e47c4fSKonstantin Belousov return (-1); 291046e47c4fSKonstantin Belousov } 291190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 291290af4afaSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2913aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Catching SIGSTOP"); 2914b7a25e63SKonstantin Belousov sigqueue_delete(&td->td_sigqueue, sig); 2915b7a25e63SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2916e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2917b4490c6eSKonstantin Belousov p->p_xsig = sig; 29187b4a950aSDavid Xu PROC_SLOCK(p); 2919d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 29206ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 29217b4a950aSDavid Xu PROC_SUNLOCK(p); 292290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2923b7a25e63SKonstantin Belousov goto next; 2924fd50a707SBrooks Davis } else if (prop & SIGPROP_IGNORE) { 2925df8bae1dSRodney W. Grimes /* 2926df8bae1dSRodney W. Grimes * Except for SIGCONT, shouldn't get here. 2927df8bae1dSRodney W. Grimes * Default action is to ignore; drop it. 2928df8bae1dSRodney W. Grimes */ 2929df8bae1dSRodney W. Grimes break; /* == ignore */ 2930df8bae1dSRodney W. Grimes } else 29312c42a146SMarcel Moolenaar return (sig); 2932df8bae1dSRodney W. Grimes /*NOTREACHED*/ 2933df8bae1dSRodney W. Grimes 2934d321df47SPoul-Henning Kamp case (intptr_t)SIG_IGN: 2935df8bae1dSRodney W. Grimes /* 2936df8bae1dSRodney W. Grimes * Masking above should prevent us ever trying 2937df8bae1dSRodney W. Grimes * to take action on an ignored signal other 2938df8bae1dSRodney W. Grimes * than SIGCONT, unless process is traced. 2939df8bae1dSRodney W. Grimes */ 2940fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) == 0 && 2941df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED) == 0) 2942df8bae1dSRodney W. Grimes printf("issignal\n"); 2943df8bae1dSRodney W. Grimes break; /* == ignore */ 2944df8bae1dSRodney W. Grimes 2945df8bae1dSRodney W. Grimes default: 2946df8bae1dSRodney W. Grimes /* 2947df8bae1dSRodney W. Grimes * This signal has an action, let 2948df8bae1dSRodney W. Grimes * postsig() process it. 2949df8bae1dSRodney W. Grimes */ 29502c42a146SMarcel Moolenaar return (sig); 2951df8bae1dSRodney W. Grimes } 29529104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 29536b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 2954b7a25e63SKonstantin Belousov next:; 2955df8bae1dSRodney W. Grimes } 2956df8bae1dSRodney W. Grimes /* NOTREACHED */ 2957df8bae1dSRodney W. Grimes } 2958df8bae1dSRodney W. Grimes 2959e574e444SDavid Xu void 2960e574e444SDavid Xu thread_stopped(struct proc *p) 2961e574e444SDavid Xu { 2962e574e444SDavid Xu int n; 2963e574e444SDavid Xu 2964e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 29657b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2966e574e444SDavid Xu n = p->p_suspcount; 29677c9a98f1SDavid Xu if (p == curproc) 2968e574e444SDavid Xu n++; 2969e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 29707b4a950aSDavid Xu PROC_SUNLOCK(p); 2971407948a5SDavid Xu p->p_flag &= ~P_WAITED; 2972e574e444SDavid Xu PROC_LOCK(p->p_pptr); 29737f96995eSDavid Xu childproc_stopped(p, (p->p_flag & P_TRACED) ? 2974ebceaf6dSDavid Xu CLD_TRAPPED : CLD_STOPPED); 2975e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 29767b4a950aSDavid Xu PROC_SLOCK(p); 2977e574e444SDavid Xu } 2978e574e444SDavid Xu } 2979e574e444SDavid Xu 2980df8bae1dSRodney W. Grimes /* 2981df8bae1dSRodney W. Grimes * Take the action for the specified signal 2982df8bae1dSRodney W. Grimes * from the current set of pending signals. 2983df8bae1dSRodney W. Grimes */ 298475c586a4SKonstantin Belousov int 29852c42a146SMarcel Moolenaar postsig(sig) 29863e85b721SEd Maste int sig; 2987df8bae1dSRodney W. Grimes { 2988b40ce416SJulian Elischer struct thread *td = curthread; 2989e052a8b9SEd Maste struct proc *p = td->td_proc; 2990628d2653SJohn Baldwin struct sigacts *ps; 29912c42a146SMarcel Moolenaar sig_t action; 29929104847fSDavid Xu ksiginfo_t ksi; 2993e442f29fSKonstantin Belousov sigset_t returnmask; 2994df8bae1dSRodney W. Grimes 29952c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 29965526d2d9SEivind Eklund 29972ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2998628d2653SJohn Baldwin ps = p->p_sigacts; 299990af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 30005da49fcbSDavid Xu ksiginfo_init(&ksi); 30016b286ee8SKonstantin Belousov if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 30026b286ee8SKonstantin Belousov sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 300375c586a4SKonstantin Belousov return (0); 30049104847fSDavid Xu ksi.ksi_signo = sig; 300556c06c4bSDavid Xu if (ksi.ksi_code == SI_TIMER) 300656c06c4bSDavid Xu itimer_accept(p, ksi.ksi_timerid, &ksi); 30072c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 3008df8bae1dSRodney W. Grimes #ifdef KTRACE 3009374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 30105e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 301161009552SJilles Tjoelker &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 3012df8bae1dSRodney W. Grimes #endif 3013047aa39bSRobert Watson if (p->p_stops & S_SIG) { 3014047aa39bSRobert Watson mtx_unlock(&ps->ps_mtx); 3015047aa39bSRobert Watson stopevent(p, S_SIG, sig); 3016047aa39bSRobert Watson mtx_lock(&ps->ps_mtx); 3017047aa39bSRobert Watson } 30182a024a2bSSean Eric Fagan 30198460a577SJohn Birrell if (action == SIG_DFL) { 3020df8bae1dSRodney W. Grimes /* 3021df8bae1dSRodney W. Grimes * Default action, where the default is to kill 3022df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 3023df8bae1dSRodney W. Grimes */ 302490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 302586be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi.ksi_info); 3026b40ce416SJulian Elischer sigexit(td, sig); 3027df8bae1dSRodney W. Grimes /* NOTREACHED */ 3028df8bae1dSRodney W. Grimes } else { 3029df8bae1dSRodney W. Grimes /* 3030df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 3031df8bae1dSRodney W. Grimes */ 30324093529dSJeff Roberson KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 30335526d2d9SEivind Eklund ("postsig action")); 3034df8bae1dSRodney W. Grimes /* 3035df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 3036645682fdSLuoqi Chen * occurrences of this signal. 3037df8bae1dSRodney W. Grimes * 3038645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 3039df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 3040645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 3041df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 3042df8bae1dSRodney W. Grimes */ 30435e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 30444093529dSJeff Roberson returnmask = td->td_oldsigmask; 30455e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 3046df8bae1dSRodney W. Grimes } else 30474093529dSJeff Roberson returnmask = td->td_sigmask; 30482c42a146SMarcel Moolenaar 3049c90c9021SEd Schouten if (p->p_sig == sig) { 30506626c604SJulian Elischer p->p_code = 0; 30516626c604SJulian Elischer p->p_sig = 0; 3052df8bae1dSRodney W. Grimes } 30539104847fSDavid Xu (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3054e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 3055df8bae1dSRodney W. Grimes } 305675c586a4SKonstantin Belousov return (1); 3057df8bae1dSRodney W. Grimes } 3058df8bae1dSRodney W. Grimes 3059df8bae1dSRodney W. Grimes /* 3060df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 3061df8bae1dSRodney W. Grimes */ 306226f9a767SRodney W. Grimes void 3063e052a8b9SEd Maste killproc(struct proc *p, char *why) 3064df8bae1dSRodney W. Grimes { 30659081e5e8SJohn Baldwin 30669081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3067c3209846SPawel Jakub Dawidek CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3068c3209846SPawel Jakub Dawidek p->p_comm); 3069c3209846SPawel Jakub Dawidek log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, 3070c3209846SPawel Jakub Dawidek p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why); 30713f1c4c4fSKonstantin Belousov p->p_flag |= P_WKILLED; 30728451d0ddSKip Macy kern_psignal(p, SIGKILL); 3073df8bae1dSRodney W. Grimes } 3074df8bae1dSRodney W. Grimes 3075df8bae1dSRodney W. Grimes /* 3076df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 3077df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 3078df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 3079df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 3080df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 3081df8bae1dSRodney W. Grimes * does not return. 3082df8bae1dSRodney W. Grimes */ 308326f9a767SRodney W. Grimes void 3084e052a8b9SEd Maste sigexit(struct thread *td, int sig) 3085df8bae1dSRodney W. Grimes { 3086b40ce416SJulian Elischer struct proc *p = td->td_proc; 3087df8bae1dSRodney W. Grimes 3088628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3089df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 3090f97c3df1SDavid Schultz /* 3091f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 3092f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 3093f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 3094f97c3df1SDavid Schultz * change out from under it. 3095f97c3df1SDavid Schultz * 3096f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 3097f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 3098f97c3df1SDavid Schultz */ 3099fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_CORE) && 3100fd50a707SBrooks Davis thread_single(p, SINGLE_NO_EXIT) == 0) { 31012c42a146SMarcel Moolenaar p->p_sig = sig; 3102c364e17eSAndrey A. Chernov /* 3103c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 3104c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 3105c364e17eSAndrey A. Chernov * these messages.) 3106c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 31074ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 3108c364e17eSAndrey A. Chernov */ 3109b40ce416SJulian Elischer if (coredump(td) == 0) 31102c42a146SMarcel Moolenaar sig |= WCOREFLAG; 311157308494SJoerg Wunsch if (kern_logsigexit) 311257308494SJoerg Wunsch log(LOG_INFO, 311357308494SJoerg Wunsch "pid %d (%s), uid %d: exited on signal %d%s\n", 31143d1b21c6SAndrey A. Chernov p->p_pid, p->p_comm, 31159c1ab3e0SJohn Baldwin td->td_ucred ? td->td_ucred->cr_uid : -1, 31162c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 31172c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 31184ae89b95SJohn Baldwin } else 3119628d2653SJohn Baldwin PROC_UNLOCK(p); 3120b4490c6eSKonstantin Belousov exit1(td, 0, sig); 3121df8bae1dSRodney W. Grimes /* NOTREACHED */ 3122df8bae1dSRodney W. Grimes } 3123df8bae1dSRodney W. Grimes 3124ebceaf6dSDavid Xu /* 31257f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 31267f96995eSDavid Xu * is changed. 3127ebceaf6dSDavid Xu */ 31287f96995eSDavid Xu static void 31297f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 3130ebceaf6dSDavid Xu { 3131ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 3132ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3133ebceaf6dSDavid Xu 3134ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 3135ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 3136ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 31377f96995eSDavid Xu p->p_ksi->ksi_status = status; 3138ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 3139ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3140ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 3141ebceaf6dSDavid Xu return; 3142ebceaf6dSDavid Xu } 3143ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3144ebceaf6dSDavid Xu } 3145ebceaf6dSDavid Xu 31467f96995eSDavid Xu static void 3147b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig) 31487f96995eSDavid Xu { 31497f96995eSDavid Xu struct sigacts *ps; 31507f96995eSDavid Xu 31517f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 31527f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 31537f96995eSDavid Xu 31547f96995eSDavid Xu /* 31557f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 31567f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 31577f96995eSDavid Xu * that parent will awake, because parent may masked 31587f96995eSDavid Xu * the signal. 31597f96995eSDavid Xu */ 31607f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 31617f96995eSDavid Xu wakeup(p->p_pptr); 31627f96995eSDavid Xu 31637f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 31647f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 31657f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 31667f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 3167b20a9aa9SJilles Tjoelker sigparent(p, reason, sig); 31687f96995eSDavid Xu } else 31697f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 31707f96995eSDavid Xu } 31717f96995eSDavid Xu 31727f96995eSDavid Xu void 31737f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 31747f96995eSDavid Xu { 3175b4490c6eSKonstantin Belousov 3176b4490c6eSKonstantin Belousov childproc_jobstate(p, reason, p->p_xsig); 31777f96995eSDavid Xu } 31787f96995eSDavid Xu 3179ebceaf6dSDavid Xu void 3180ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3181ebceaf6dSDavid Xu { 31827f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3183ebceaf6dSDavid Xu } 3184ebceaf6dSDavid Xu 3185ebceaf6dSDavid Xu void 3186ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3187ebceaf6dSDavid Xu { 3188b4490c6eSKonstantin Belousov int reason, status; 3189ebceaf6dSDavid Xu 3190b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 3191b4490c6eSKonstantin Belousov reason = CLD_DUMPED; 3192b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3193b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 3194b4490c6eSKonstantin Belousov reason = CLD_KILLED; 3195b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3196b4490c6eSKonstantin Belousov } else { 3197b4490c6eSKonstantin Belousov reason = CLD_EXITED; 3198b4490c6eSKonstantin Belousov status = p->p_xexit; 3199b4490c6eSKonstantin Belousov } 32007f96995eSDavid Xu /* 32017f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 32027f96995eSDavid Xu * done in exit1(). 32037f96995eSDavid Xu */ 32047f96995eSDavid Xu sigparent(p, reason, status); 3205ebceaf6dSDavid Xu } 3206ebceaf6dSDavid Xu 3207e7228204SAlfred Perlstein /* 3208e7228204SAlfred Perlstein * We only have 1 character for the core count in the format 3209e7228204SAlfred Perlstein * string, so the range will be 0-9 3210e7228204SAlfred Perlstein */ 3211cc37baeaSStephen J. Kiernan #define MAX_NUM_CORE_FILES 10 3212cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES 3213cc37baeaSStephen J. Kiernan #define NUM_CORE_FILES 5 3214cc37baeaSStephen J. Kiernan #endif 3215cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3216cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES; 3217e7228204SAlfred Perlstein 3218e7228204SAlfred Perlstein static int 3219e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3220e7228204SAlfred Perlstein { 3221e7228204SAlfred Perlstein int error; 3222e7228204SAlfred Perlstein int new_val; 3223e7228204SAlfred Perlstein 3224c5105012SKonstantin Belousov new_val = num_cores; 3225e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3226e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3227e7228204SAlfred Perlstein return (error); 3228cc37baeaSStephen J. Kiernan if (new_val > MAX_NUM_CORE_FILES) 3229cc37baeaSStephen J. Kiernan new_val = MAX_NUM_CORE_FILES; 3230e7228204SAlfred Perlstein if (new_val < 0) 3231e7228204SAlfred Perlstein new_val = 0; 3232e7228204SAlfred Perlstein num_cores = new_val; 3233e7228204SAlfred Perlstein return (0); 3234e7228204SAlfred Perlstein } 3235e7228204SAlfred Perlstein SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW, 3236e7228204SAlfred Perlstein 0, sizeof(int), sysctl_debug_num_cores_check, "I", ""); 3237e7228204SAlfred Perlstein 3238aa14e9b7SMark Johnston #define GZ_SUFFIX ".gz" 3239aa14e9b7SMark Johnston 3240aa14e9b7SMark Johnston #ifdef GZIO 3241aa14e9b7SMark Johnston static int compress_user_cores = 1; 3242aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RWTUN, 324329146f1aSPawel Jakub Dawidek &compress_user_cores, 0, "Compression of user corefiles"); 3244e7228204SAlfred Perlstein 3245aa14e9b7SMark Johnston int compress_user_cores_gzlevel = 6; 3246aa14e9b7SMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN, 3247aa14e9b7SMark Johnston &compress_user_cores_gzlevel, 0, "Corefile gzip compression level"); 3248aa14e9b7SMark Johnston #else 3249aa14e9b7SMark Johnston static int compress_user_cores = 0; 3250e7228204SAlfred Perlstein #endif 3251e7228204SAlfred Perlstein 32525bc0ff88SMateusz Guzik /* 32535bc0ff88SMateusz Guzik * Protect the access to corefilename[] by allproc_lock. 32545bc0ff88SMateusz Guzik */ 32555bc0ff88SMateusz Guzik #define corefilename_lock allproc_lock 32565bc0ff88SMateusz Guzik 32576d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3258fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 32595bc0ff88SMateusz Guzik 32605bc0ff88SMateusz Guzik static int 32615bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 32625bc0ff88SMateusz Guzik { 32635bc0ff88SMateusz Guzik int error; 32645bc0ff88SMateusz Guzik 32655bc0ff88SMateusz Guzik sx_xlock(&corefilename_lock); 32665bc0ff88SMateusz Guzik error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 32675bc0ff88SMateusz Guzik req); 32685bc0ff88SMateusz Guzik sx_xunlock(&corefilename_lock); 32695bc0ff88SMateusz Guzik 32705bc0ff88SMateusz Guzik return (error); 32715bc0ff88SMateusz Guzik } 3272fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 32735bc0ff88SMateusz Guzik CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 32745bc0ff88SMateusz Guzik "Process corefile name format string"); 3275c5edb423SSean Eric Fagan 3276c5edb423SSean Eric Fagan /* 3277c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3278c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3279c345faeaSPawel Jakub Dawidek * and open/create core file. 3280c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3281c5edb423SSean Eric Fagan * %N name of process ("name") 3282c5edb423SSean Eric Fagan * %P process id (pid) 3283c5edb423SSean Eric Fagan * %U user id (uid) 3284c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3285c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3286c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3287c5edb423SSean Eric Fagan */ 3288c345faeaSPawel Jakub Dawidek static int 3289c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 3290c345faeaSPawel Jakub Dawidek int compress, struct vnode **vpp, char **namep) 32918b43b535SAlfred Perlstein { 3292c345faeaSPawel Jakub Dawidek struct nameidata nd; 329336b208e0SRobert Watson struct sbuf sb; 329436b208e0SRobert Watson const char *format; 3295c345faeaSPawel Jakub Dawidek char *hostname, *name; 3296c345faeaSPawel Jakub Dawidek int indexpos, i, error, cmode, flags, oflags; 3297c5edb423SSean Eric Fagan 3298b7402d82SAlfred Perlstein hostname = NULL; 32998b43b535SAlfred Perlstein format = corefilename; 3300086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3301e7228204SAlfred Perlstein indexpos = -1; 3302c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 33035bc0ff88SMateusz Guzik sx_slock(&corefilename_lock); 330429146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3305c5edb423SSean Eric Fagan switch (format[i]) { 3306c5edb423SSean Eric Fagan case '%': /* Format character */ 3307c5edb423SSean Eric Fagan i++; 3308c5edb423SSean Eric Fagan switch (format[i]) { 3309c5edb423SSean Eric Fagan case '%': 331036b208e0SRobert Watson sbuf_putc(&sb, '%'); 3311c5edb423SSean Eric Fagan break; 3312e7228204SAlfred Perlstein case 'H': /* hostname */ 3313b7402d82SAlfred Perlstein if (hostname == NULL) { 3314b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3315086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3316b7402d82SAlfred Perlstein } 3317e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3318b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3319e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3320e7228204SAlfred Perlstein break; 3321e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3322e7228204SAlfred Perlstein sbuf_printf(&sb, "0"); 3323e7228204SAlfred Perlstein indexpos = sbuf_len(&sb) - 1; 3324e7228204SAlfred Perlstein break; 3325c5edb423SSean Eric Fagan case 'N': /* process name */ 3326c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3327c5edb423SSean Eric Fagan break; 3328c5edb423SSean Eric Fagan case 'P': /* process id */ 332936b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3330c5edb423SSean Eric Fagan break; 3331c5edb423SSean Eric Fagan case 'U': /* user id */ 333236b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3333c5edb423SSean Eric Fagan break; 3334c5edb423SSean Eric Fagan default: 33358b43b535SAlfred Perlstein log(LOG_ERR, 333636b208e0SRobert Watson "Unknown format character %c in " 333736b208e0SRobert Watson "corename `%s'\n", format[i], format); 333829146f1aSPawel Jakub Dawidek break; 3339c5edb423SSean Eric Fagan } 3340c5edb423SSean Eric Fagan break; 3341c5edb423SSean Eric Fagan default: 334236b208e0SRobert Watson sbuf_putc(&sb, format[i]); 33436c08be2bSPawel Jakub Dawidek break; 3344c5edb423SSean Eric Fagan } 3345c5edb423SSean Eric Fagan } 33465bc0ff88SMateusz Guzik sx_sunlock(&corefilename_lock); 3347b7402d82SAlfred Perlstein free(hostname, M_TEMP); 3348f06f465dSPawel Jakub Dawidek if (compress) 3349e7228204SAlfred Perlstein sbuf_printf(&sb, GZ_SUFFIX); 33504d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 335136b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3352c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 3353b7402d82SAlfred Perlstein sbuf_delete(&sb); 3354c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3355c345faeaSPawel Jakub Dawidek return (ENOMEM); 3356c5edb423SSean Eric Fagan } 335736b208e0SRobert Watson sbuf_finish(&sb); 335836b208e0SRobert Watson sbuf_delete(&sb); 3359e7228204SAlfred Perlstein 3360c345faeaSPawel Jakub Dawidek cmode = S_IRUSR | S_IWUSR; 33618ee9765aSKonstantin Belousov oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 33628ee9765aSKonstantin Belousov (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 3363c345faeaSPawel Jakub Dawidek 3364e7228204SAlfred Perlstein /* 3365e7228204SAlfred Perlstein * If the core format has a %I in it, then we need to check 3366e7228204SAlfred Perlstein * for existing corefiles before returning a name. 3367e7228204SAlfred Perlstein * To do this we iterate over 0..num_cores to find a 3368e7228204SAlfred Perlstein * non-existing core file name to use. 3369e7228204SAlfred Perlstein */ 3370e7228204SAlfred Perlstein if (indexpos != -1) { 337129146f1aSPawel Jakub Dawidek for (i = 0; i < num_cores; i++) { 337207a8e078SPawel Jakub Dawidek flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW; 337329146f1aSPawel Jakub Dawidek name[indexpos] = '0' + i; 3374c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3375b0c9d4d7SPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, 3376b0c9d4d7SPawel Jakub Dawidek td->td_ucred, NULL); 3377e7228204SAlfred Perlstein if (error) { 337823c6445aSPawel Jakub Dawidek if (error == EEXIST) 3379e7228204SAlfred Perlstein continue; 3380e7228204SAlfred Perlstein log(LOG_ERR, 3381e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 3382e7228204SAlfred Perlstein "on initial open test, error = %d\n", 3383c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 3384c345faeaSPawel Jakub Dawidek } 3385c345faeaSPawel Jakub Dawidek goto out; 3386c345faeaSPawel Jakub Dawidek } 3387c345faeaSPawel Jakub Dawidek } 3388c345faeaSPawel Jakub Dawidek 3389c345faeaSPawel Jakub Dawidek flags = O_CREAT | FWRITE | O_NOFOLLOW; 3390c345faeaSPawel Jakub Dawidek NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3391c345faeaSPawel Jakub Dawidek error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL); 3392c345faeaSPawel Jakub Dawidek out: 3393c345faeaSPawel Jakub Dawidek if (error) { 3394c345faeaSPawel Jakub Dawidek #ifdef AUDIT 3395c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 3396c345faeaSPawel Jakub Dawidek #endif 3397c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3398c345faeaSPawel Jakub Dawidek return (error); 3399e7228204SAlfred Perlstein } 3400e7228204SAlfred Perlstein NDFREE(&nd, NDF_ONLY_PNBUF); 3401c345faeaSPawel Jakub Dawidek *vpp = nd.ni_vp; 3402c345faeaSPawel Jakub Dawidek *namep = name; 3403c345faeaSPawel Jakub Dawidek return (0); 340436b208e0SRobert Watson } 3405c5edb423SSean Eric Fagan 3406eb6368d4SRui Paulo static int 3407eb6368d4SRui Paulo coredump_sanitise_path(const char *path) 3408eb6368d4SRui Paulo { 3409624157bbSRui Paulo size_t i; 3410eb6368d4SRui Paulo 3411eb6368d4SRui Paulo /* 3412eb6368d4SRui Paulo * Only send a subset of ASCII to devd(8) because it 3413eb6368d4SRui Paulo * might pass these strings to sh -c. 3414eb6368d4SRui Paulo */ 3415624157bbSRui Paulo for (i = 0; path[i]; i++) 3416eb6368d4SRui Paulo if (!(isalpha(path[i]) || isdigit(path[i])) && 3417eb6368d4SRui Paulo path[i] != '/' && path[i] != '.' && 3418eb6368d4SRui Paulo path[i] != '-') 3419eb6368d4SRui Paulo return (0); 3420eb6368d4SRui Paulo 3421eb6368d4SRui Paulo return (1); 3422eb6368d4SRui Paulo } 3423eb6368d4SRui Paulo 3424df8bae1dSRodney W. Grimes /* 3425fca666a1SJulian Elischer * Dump a process' core. The main routine does some 3426fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 3427fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 3428fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 3429fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 3430fca666a1SJulian Elischer */ 3431fca666a1SJulian Elischer 3432fca666a1SJulian Elischer static int 3433b40ce416SJulian Elischer coredump(struct thread *td) 3434fca666a1SJulian Elischer { 3435b40ce416SJulian Elischer struct proc *p = td->td_proc; 3436f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 3437f06f465dSPawel Jakub Dawidek struct vnode *vp; 343806ae1e91SMatthew Dillon struct flock lf; 3439fca666a1SJulian Elischer struct vattr vattr; 3440c345faeaSPawel Jakub Dawidek int error, error1, locked; 3441fca666a1SJulian Elischer char *name; /* name of corefile */ 3442539c9eefSKonstantin Belousov void *rl_cookie; 3443fca666a1SJulian Elischer off_t limit; 34446fbc0f7dSRui Paulo char *data = NULL; 3445842ab62bSRui Paulo char *fullpath, *freepath = NULL; 3446eb6368d4SRui Paulo size_t len; 34476fbc0f7dSRui Paulo static const char comm_name[] = "comm="; 34486fbc0f7dSRui Paulo static const char core_name[] = "core="; 3449fca666a1SJulian Elischer 34504ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3451f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3452628d2653SJohn Baldwin _STOPEVENT(p, S_CORE, 0); 3453fca666a1SJulian Elischer 3454677258f7SKonstantin Belousov if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 3455677258f7SKonstantin Belousov (p->p_flag2 & P2_NOTRACE) != 0) { 3456628d2653SJohn Baldwin PROC_UNLOCK(p); 3457fca666a1SJulian Elischer return (EFAULT); 3458628d2653SJohn Baldwin } 3459fca666a1SJulian Elischer 3460fca666a1SJulian Elischer /* 346135a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 346235a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 346335a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 346435a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 346535a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 346635a2598fSSean Eric Fagan * if it is larger than the limit. 3467fca666a1SJulian Elischer */ 3468f6f6d240SMateusz Guzik limit = (off_t)lim_cur(td, RLIMIT_CORE); 3469b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3470628d2653SJohn Baldwin PROC_UNLOCK(p); 347191d5354aSJohn Baldwin return (EFBIG); 347257274c51SChristian S.J. Peron } 3473b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 347435a2598fSSean Eric Fagan 3475aa14e9b7SMark Johnston error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 3476aa14e9b7SMark Johnston compress_user_cores, &vp, &name); 3477c345faeaSPawel Jakub Dawidek if (error != 0) 3478fca666a1SJulian Elischer return (error); 347906ae1e91SMatthew Dillon 3480539c9eefSKonstantin Belousov /* 3481539c9eefSKonstantin Belousov * Don't dump to non-regular files or files with links. 3482539c9eefSKonstantin Belousov * Do not dump into system files. 3483539c9eefSKonstantin Belousov */ 3484f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 3485539c9eefSKonstantin Belousov vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0) { 348622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3487832dafadSDon Lewis error = EFAULT; 3488dacbc9dbSKonstantin Belousov goto out; 3489832dafadSDon Lewis } 3490832dafadSDon Lewis 349122db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3492539c9eefSKonstantin Belousov 3493539c9eefSKonstantin Belousov /* Postpone other writers, including core dumps of other processes. */ 3494539c9eefSKonstantin Belousov rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3495539c9eefSKonstantin Belousov 349606ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 349706ae1e91SMatthew Dillon lf.l_start = 0; 349806ae1e91SMatthew Dillon lf.l_len = 0; 349906ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 3500c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 350106ae1e91SMatthew Dillon 3502fca666a1SJulian Elischer VATTR_NULL(&vattr); 3503fca666a1SJulian Elischer vattr.va_size = 0; 35046141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 35056141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 3506cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 35070359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 350822db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 3509628d2653SJohn Baldwin PROC_LOCK(p); 3510fca666a1SJulian Elischer p->p_acflag |= ACORE; 3511628d2653SJohn Baldwin PROC_UNLOCK(p); 3512fca666a1SJulian Elischer 351323c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 351423c6445aSPawel Jakub Dawidek error = p->p_sysent->sv_coredump(td, vp, limit, 3515aa14e9b7SMark Johnston compress_user_cores ? IMGACT_CORE_COMPRESS : 0); 351623c6445aSPawel Jakub Dawidek } else { 351723c6445aSPawel Jakub Dawidek error = ENOSYS; 351823c6445aSPawel Jakub Dawidek } 3519fca666a1SJulian Elischer 3520c447f5b2SRobert Watson if (locked) { 352106ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 352206ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3523c447f5b2SRobert Watson } 3524539c9eefSKonstantin Belousov vn_rangelock_unlock(vp, rl_cookie); 3525dacbc9dbSKonstantin Belousov 3526842ab62bSRui Paulo /* 3527842ab62bSRui Paulo * Notify the userland helper that a process triggered a core dump. 3528842ab62bSRui Paulo * This allows the helper to run an automated debugging session. 3529842ab62bSRui Paulo */ 3530dacbc9dbSKonstantin Belousov if (error != 0 || coredump_devctl == 0) 3531842ab62bSRui Paulo goto out; 35326fbc0f7dSRui Paulo len = MAXPATHLEN * 2 + sizeof(comm_name) - 1 + 35336fbc0f7dSRui Paulo sizeof(' ') + sizeof(core_name) - 1; 35346fbc0f7dSRui Paulo data = malloc(len, M_TEMP, M_WAITOK); 3535842ab62bSRui Paulo if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0) 3536842ab62bSRui Paulo goto out; 3537eb6368d4SRui Paulo if (!coredump_sanitise_path(fullpath)) 3538eb6368d4SRui Paulo goto out; 35396fbc0f7dSRui Paulo snprintf(data, len, "%s%s ", comm_name, fullpath); 3540842ab62bSRui Paulo free(freepath, M_TEMP); 3541842ab62bSRui Paulo freepath = NULL; 3542624157bbSRui Paulo if (vn_fullpath_global(td, vp, &fullpath, &freepath) != 0) 3543842ab62bSRui Paulo goto out; 3544eb6368d4SRui Paulo if (!coredump_sanitise_path(fullpath)) 3545eb6368d4SRui Paulo goto out; 35466fbc0f7dSRui Paulo strlcat(data, core_name, len); 35476fbc0f7dSRui Paulo strlcat(data, fullpath, len); 3548842ab62bSRui Paulo devctl_notify("kernel", "signal", "coredump", data); 3549842ab62bSRui Paulo out: 3550dacbc9dbSKonstantin Belousov error1 = vn_close(vp, FWRITE, cred, td); 3551dacbc9dbSKonstantin Belousov if (error == 0) 3552dacbc9dbSKonstantin Belousov error = error1; 355357274c51SChristian S.J. Peron #ifdef AUDIT 355457274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 355557274c51SChristian S.J. Peron #endif 3556842ab62bSRui Paulo free(freepath, M_TEMP); 35576fbc0f7dSRui Paulo free(data, M_TEMP); 355857274c51SChristian S.J. Peron free(name, M_TEMP); 3559fca666a1SJulian Elischer return (error); 3560fca666a1SJulian Elischer } 3561fca666a1SJulian Elischer 3562fca666a1SJulian Elischer /* 35630c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 35640c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 3565df8bae1dSRodney W. Grimes */ 3566d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 3567df8bae1dSRodney W. Grimes struct nosys_args { 3568df8bae1dSRodney W. Grimes int dummy; 3569df8bae1dSRodney W. Grimes }; 3570d2d3e875SBruce Evans #endif 3571df8bae1dSRodney W. Grimes /* ARGSUSED */ 357226f9a767SRodney W. Grimes int 3573e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args) 3574df8bae1dSRodney W. Grimes { 3575*f5a077c3SKonstantin Belousov struct proc *p; 3576*f5a077c3SKonstantin Belousov 3577*f5a077c3SKonstantin Belousov p = td->td_proc; 3578b40ce416SJulian Elischer 3579628d2653SJohn Baldwin PROC_LOCK(p); 3580888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 3581628d2653SJohn Baldwin PROC_UNLOCK(p); 3582*f5a077c3SKonstantin Belousov if (kern_lognosys) 3583*f5a077c3SKonstantin Belousov uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 3584*f5a077c3SKonstantin Belousov td->td_sa.code); 3585f5216b9aSBruce Evans return (ENOSYS); 3586df8bae1dSRodney W. Grimes } 3587831d27a9SDon Lewis 3588831d27a9SDon Lewis /* 35890c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 35900c14ff0eSRobert Watson * credentials rather than those of the current process. 3591831d27a9SDon Lewis */ 3592831d27a9SDon Lewis void 3593e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty) 3594831d27a9SDon Lewis { 3595a3de221dSKonstantin Belousov ksiginfo_t ksi; 3596f1320723SAlfred Perlstein struct sigio *sigio; 3597831d27a9SDon Lewis 3598a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 3599a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 3600a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 3601a3de221dSKonstantin Belousov 3602f1320723SAlfred Perlstein SIGIO_LOCK(); 3603f1320723SAlfred Perlstein sigio = *sigiop; 3604f1320723SAlfred Perlstein if (sigio == NULL) { 3605f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3606f1320723SAlfred Perlstein return; 3607f1320723SAlfred Perlstein } 3608831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 3609628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 36102b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 36118451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 3612628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 3613831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 3614831d27a9SDon Lewis struct proc *p; 3615831d27a9SDon Lewis 3616f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 3617628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3618628d2653SJohn Baldwin PROC_LOCK(p); 3619e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 3620e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3621831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 36228451d0ddSKip Macy kern_psignal(p, sig); 3623628d2653SJohn Baldwin PROC_UNLOCK(p); 3624628d2653SJohn Baldwin } 3625f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 3626831d27a9SDon Lewis } 3627f1320723SAlfred Perlstein SIGIO_UNLOCK(); 3628831d27a9SDon Lewis } 3629cb679c38SJonathan Lemon 3630cb679c38SJonathan Lemon static int 3631cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 3632cb679c38SJonathan Lemon { 3633cb679c38SJonathan Lemon struct proc *p = curproc; 3634cb679c38SJonathan Lemon 3635cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 3636cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 3637cb679c38SJonathan Lemon 36389e590ff0SKonstantin Belousov knlist_add(p->p_klist, kn, 0); 3639cb679c38SJonathan Lemon 3640cb679c38SJonathan Lemon return (0); 3641cb679c38SJonathan Lemon } 3642cb679c38SJonathan Lemon 3643cb679c38SJonathan Lemon static void 3644cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 3645cb679c38SJonathan Lemon { 3646cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 3647cb679c38SJonathan Lemon 36489e590ff0SKonstantin Belousov knlist_remove(p->p_klist, kn, 0); 3649cb679c38SJonathan Lemon } 3650cb679c38SJonathan Lemon 3651cb679c38SJonathan Lemon /* 3652cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 3653cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 3654cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 3655cb679c38SJonathan Lemon * isn't worth the trouble. 3656cb679c38SJonathan Lemon */ 3657cb679c38SJonathan Lemon static int 3658cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 3659cb679c38SJonathan Lemon { 3660cb679c38SJonathan Lemon 3661cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 3662cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 3663cb679c38SJonathan Lemon 3664cb679c38SJonathan Lemon if (kn->kn_id == hint) 3665cb679c38SJonathan Lemon kn->kn_data++; 3666cb679c38SJonathan Lemon } 3667cb679c38SJonathan Lemon return (kn->kn_data != 0); 3668cb679c38SJonathan Lemon } 366990af4afaSJohn Baldwin 367090af4afaSJohn Baldwin struct sigacts * 367190af4afaSJohn Baldwin sigacts_alloc(void) 367290af4afaSJohn Baldwin { 367390af4afaSJohn Baldwin struct sigacts *ps; 367490af4afaSJohn Baldwin 367590af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3676ce8daaadSMateusz Guzik refcount_init(&ps->ps_refcnt, 1); 367790af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 367890af4afaSJohn Baldwin return (ps); 367990af4afaSJohn Baldwin } 368090af4afaSJohn Baldwin 368190af4afaSJohn Baldwin void 368290af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 368390af4afaSJohn Baldwin { 368490af4afaSJohn Baldwin 3685c959c237SMateusz Guzik if (refcount_release(&ps->ps_refcnt) == 0) 3686c959c237SMateusz Guzik return; 368790af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 368890af4afaSJohn Baldwin free(ps, M_SUBPROC); 368990af4afaSJohn Baldwin } 369090af4afaSJohn Baldwin 369190af4afaSJohn Baldwin struct sigacts * 369290af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 369390af4afaSJohn Baldwin { 3694c959c237SMateusz Guzik 3695c959c237SMateusz Guzik refcount_acquire(&ps->ps_refcnt); 369690af4afaSJohn Baldwin return (ps); 369790af4afaSJohn Baldwin } 369890af4afaSJohn Baldwin 369990af4afaSJohn Baldwin void 370090af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 370190af4afaSJohn Baldwin { 370290af4afaSJohn Baldwin 370390af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 370490af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 370590af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 370690af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 370790af4afaSJohn Baldwin } 370890af4afaSJohn Baldwin 370990af4afaSJohn Baldwin int 371090af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 371190af4afaSJohn Baldwin { 371290af4afaSJohn Baldwin 3713d00c8ea4SMateusz Guzik return (ps->ps_refcnt > 1); 371490af4afaSJohn Baldwin } 3715