19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 7df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 8df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 9df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 2069a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39677b542eSDavid E. O'Brien #include <sys/cdefs.h> 40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 41677b542eSDavid E. O'Brien 42140ceb5dSKonstantin Belousov #include "opt_capsicum.h" 43db6a20e2SGarrett Wollman #include "opt_ktrace.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46140ceb5dSKonstantin Belousov #include <sys/capsicum.h> 47eb6368d4SRui Paulo #include <sys/ctype.h> 4836240ea5SDoug Rabson #include <sys/systm.h> 49df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/vnode.h> 51df8bae1dSRodney W. Grimes #include <sys/acct.h> 524a144410SRobert Watson #include <sys/capsicum.h> 5378f57a9cSMark Johnston #include <sys/compressor.h> 54238510fcSJason Evans #include <sys/condvar.h> 55773e541eSWarner Losh #include <sys/devctl.h> 56854dc8c2SJohn Baldwin #include <sys/event.h> 57854dc8c2SJohn Baldwin #include <sys/fcntl.h> 58e7228204SAlfred Perlstein #include <sys/imgact.h> 59854dc8c2SJohn Baldwin #include <sys/kernel.h> 600384fff8SJason Evans #include <sys/ktr.h> 61df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 6291898857SMark Johnston #include <sys/limits.h> 63854dc8c2SJohn Baldwin #include <sys/lock.h> 64854dc8c2SJohn Baldwin #include <sys/malloc.h> 65854dc8c2SJohn Baldwin #include <sys/mutex.h> 66c959c237SMateusz Guzik #include <sys/refcount.h> 67854dc8c2SJohn Baldwin #include <sys/namei.h> 68854dc8c2SJohn Baldwin #include <sys/proc.h> 69cfb5f768SJonathan Anderson #include <sys/procdesc.h> 70ef401a85SKonstantin Belousov #include <sys/ptrace.h> 716aeb05d7STom Rhodes #include <sys/posix4.h> 72b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h> 73c31146a1SJohn Baldwin #include <sys/resourcevar.h> 745d217f17SJohn Birrell #include <sys/sdt.h> 7536b208e0SRobert Watson #include <sys/sbuf.h> 7644f3b092SJohn Baldwin #include <sys/sleepqueue.h> 776caa8a15SJohn Baldwin #include <sys/smp.h> 78df8bae1dSRodney W. Grimes #include <sys/stat.h> 791005a129SJohn Baldwin #include <sys/sx.h> 80140ceb5dSKonstantin Belousov #include <sys/syscall.h> 818f19eb88SIan Dowse #include <sys/syscallsubr.h> 82c87e2930SDavid Greenman #include <sys/sysctl.h> 83854dc8c2SJohn Baldwin #include <sys/sysent.h> 84854dc8c2SJohn Baldwin #include <sys/syslog.h> 85854dc8c2SJohn Baldwin #include <sys/sysproto.h> 8656c06c4bSDavid Xu #include <sys/timers.h> 8706ae1e91SMatthew Dillon #include <sys/unistd.h> 88140ceb5dSKonstantin Belousov #include <sys/vmmeter.h> 89854dc8c2SJohn Baldwin #include <sys/wait.h> 909104847fSDavid Xu #include <vm/vm.h> 919104847fSDavid Xu #include <vm/vm_extern.h> 929104847fSDavid Xu #include <vm/uma.h> 93df8bae1dSRodney W. Grimes 94e7228204SAlfred Perlstein #include <sys/jail.h> 95e7228204SAlfred Perlstein 96df8bae1dSRodney W. Grimes #include <machine/cpu.h> 97df8bae1dSRodney W. Grimes 98bfd7575aSWayne Salamon #include <security/audit/audit.h> 99bfd7575aSWayne Salamon 1006f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 1016f841fb7SMarcel Moolenaar 1025d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 10336160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send, 10436160958SMark Johnston "struct thread *", "struct proc *", "int"); 10536160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear, 10636160958SMark Johnston "int", "ksiginfo_t *"); 10736160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard, 1087b77e1feSMark Johnston "struct thread *", "struct proc *", "int"); 1095d217f17SJohn Birrell 1104d77a549SAlfred Perlstein static int coredump(struct thread *); 111a3de221dSKonstantin Belousov static int killpg1(struct thread *td, int sig, int pgid, int all, 112a3de221dSKonstantin Belousov ksiginfo_t *ksi); 1133cf3b9f0SJohn Baldwin static int issignal(struct thread *td); 1140bc52b0bSKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags); 1154d77a549SAlfred Perlstein static int sigprop(int sig); 11694f0972bSDavid Xu static void tdsigwakeup(struct thread *, int, sig_t, int); 117c53fec76SKonstantin Belousov static int sig_suspend_threads(struct thread *, struct proc *); 118cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 119cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 120cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 121146fc63fSKonstantin Belousov static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock); 1229104847fSDavid Xu static void sigqueue_start(void); 1234fced864SKonstantin Belousov static void sigfastblock_setpend(struct thread *td, bool resched); 124cb679c38SJonathan Lemon 1259104847fSDavid Xu static uma_zone_t ksiginfo_zone = NULL; 126e76d823bSRobert Watson struct filterops sig_filtops = { 127e76d823bSRobert Watson .f_isfd = 0, 128e76d823bSRobert Watson .f_attach = filt_sigattach, 129e76d823bSRobert Watson .f_detach = filt_sigdetach, 130e76d823bSRobert Watson .f_event = filt_signal, 131e76d823bSRobert Watson }; 132cb679c38SJonathan Lemon 133fc8cca02SJohn Baldwin static int kern_logsigexit = 1; 1343d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1353d177f46SBill Fumerola &kern_logsigexit, 0, 1363d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 13757308494SJoerg Wunsch 138f71a882fSDavid Xu static int kern_forcesigexit = 1; 139f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 140f71a882fSDavid Xu &kern_forcesigexit, 0, "Force trap signal to be handled"); 141f71a882fSDavid Xu 1427029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1436472ac3dSEd Schouten "POSIX real time signal"); 1449104847fSDavid Xu 1459104847fSDavid Xu static int max_pending_per_proc = 128; 1469104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 1479104847fSDavid Xu &max_pending_per_proc, 0, "Max pending signals per proc"); 1489104847fSDavid Xu 1499104847fSDavid Xu static int preallocate_siginfo = 1024; 150af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN, 1519104847fSDavid Xu &preallocate_siginfo, 0, "Preallocated signal memory size"); 1529104847fSDavid Xu 1539104847fSDavid Xu static int signal_overflow = 0; 154761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 1559104847fSDavid Xu &signal_overflow, 0, "Number of signals overflew"); 1569104847fSDavid Xu 1579104847fSDavid Xu static int signal_alloc_fail = 0; 158761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 1599104847fSDavid Xu &signal_alloc_fail, 0, "signals failed to be allocated"); 1609104847fSDavid Xu 161f5a077c3SKonstantin Belousov static int kern_lognosys = 0; 162f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0, 163f5a077c3SKonstantin Belousov "Log invalid syscalls"); 164f5a077c3SKonstantin Belousov 165a113b17fSKonstantin Belousov __read_frequently bool sigfastblock_fetch_always = false; 166a113b17fSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN, 167a113b17fSKonstantin Belousov &sigfastblock_fetch_always, 0, 168a113b17fSKonstantin Belousov "Fetch sigfastblock word on each syscall entry for proper " 169a113b17fSKonstantin Belousov "blocking semantic"); 170a113b17fSKonstantin Belousov 171bc387624SKonstantin Belousov static bool kern_sig_discard_ign = true; 172bc387624SKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN, 173bc387624SKonstantin Belousov &kern_sig_discard_ign, 0, 174bc387624SKonstantin Belousov "Discard ignored signals on delivery, otherwise queue them to " 175bc387624SKonstantin Belousov "the target queue"); 176bc387624SKonstantin Belousov 1779104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 1789104847fSDavid Xu 1792b87b6d4SRobert Watson /* 1802b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1812b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1822b87b6d4SRobert Watson * in the right situations. 1832b87b6d4SRobert Watson */ 1842b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1852b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1862b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1872b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1882b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1892b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1902b87b6d4SRobert Watson 191fc8cca02SJohn Baldwin static int sugid_coredump; 192af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN, 1938b5adf9dSJoseph Koshy &sugid_coredump, 0, "Allow setuid and setgid processes to dump core"); 194c87e2930SDavid Greenman 195b0c9d4d7SPawel Jakub Dawidek static int capmode_coredump; 196af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN, 197b0c9d4d7SPawel Jakub Dawidek &capmode_coredump, 0, "Allow processes in capability mode to dump core"); 198b0c9d4d7SPawel Jakub Dawidek 199e5a28db9SPaul Saab static int do_coredump = 1; 200e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 201e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 202e5a28db9SPaul Saab 2036141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 2046141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 2056141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 2066141e04aSJohn-Mark Gurney 2070da9e11bSRui Paulo static int coredump_devctl = 0; 208eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl, 209eb6368d4SRui Paulo 0, "Generate a devctl notification when processes coredump"); 210eb6368d4SRui Paulo 2112c42a146SMarcel Moolenaar /* 2122c42a146SMarcel Moolenaar * Signal properties and actions. 2132c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 2142c42a146SMarcel Moolenaar * according to the following properties: 2152c42a146SMarcel Moolenaar */ 216fd50a707SBrooks Davis #define SIGPROP_KILL 0x01 /* terminates process by default */ 217fd50a707SBrooks Davis #define SIGPROP_CORE 0x02 /* ditto and coredumps */ 218fd50a707SBrooks Davis #define SIGPROP_STOP 0x04 /* suspend process */ 219fd50a707SBrooks Davis #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */ 220fd50a707SBrooks Davis #define SIGPROP_IGNORE 0x10 /* ignore by default */ 221fd50a707SBrooks Davis #define SIGPROP_CONT 0x20 /* continue if suspended */ 222df8bae1dSRodney W. Grimes 2232c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 224ed6d876bSBrooks Davis [SIGHUP] = SIGPROP_KILL, 225ed6d876bSBrooks Davis [SIGINT] = SIGPROP_KILL, 226ed6d876bSBrooks Davis [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE, 227ed6d876bSBrooks Davis [SIGILL] = SIGPROP_KILL | SIGPROP_CORE, 228ed6d876bSBrooks Davis [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE, 229ed6d876bSBrooks Davis [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE, 230ed6d876bSBrooks Davis [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE, 231ed6d876bSBrooks Davis [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE, 232ed6d876bSBrooks Davis [SIGKILL] = SIGPROP_KILL, 233ed6d876bSBrooks Davis [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE, 234ed6d876bSBrooks Davis [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE, 235ed6d876bSBrooks Davis [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE, 236ed6d876bSBrooks Davis [SIGPIPE] = SIGPROP_KILL, 237ed6d876bSBrooks Davis [SIGALRM] = SIGPROP_KILL, 238ed6d876bSBrooks Davis [SIGTERM] = SIGPROP_KILL, 239ed6d876bSBrooks Davis [SIGURG] = SIGPROP_IGNORE, 240ed6d876bSBrooks Davis [SIGSTOP] = SIGPROP_STOP, 241ed6d876bSBrooks Davis [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP, 242ed6d876bSBrooks Davis [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT, 243ed6d876bSBrooks Davis [SIGCHLD] = SIGPROP_IGNORE, 244ed6d876bSBrooks Davis [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP, 245ed6d876bSBrooks Davis [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP, 246ed6d876bSBrooks Davis [SIGIO] = SIGPROP_IGNORE, 247ed6d876bSBrooks Davis [SIGXCPU] = SIGPROP_KILL, 248ed6d876bSBrooks Davis [SIGXFSZ] = SIGPROP_KILL, 249ed6d876bSBrooks Davis [SIGVTALRM] = SIGPROP_KILL, 250ed6d876bSBrooks Davis [SIGPROF] = SIGPROP_KILL, 251ed6d876bSBrooks Davis [SIGWINCH] = SIGPROP_IGNORE, 252ed6d876bSBrooks Davis [SIGINFO] = SIGPROP_IGNORE, 253ed6d876bSBrooks Davis [SIGUSR1] = SIGPROP_KILL, 254ed6d876bSBrooks Davis [SIGUSR2] = SIGPROP_KILL, 2552c42a146SMarcel Moolenaar }; 2562c42a146SMarcel Moolenaar 25781f2e906SMark Johnston #define _SIG_FOREACH_ADVANCE(i, set) ({ \ 25881f2e906SMark Johnston int __found; \ 25981f2e906SMark Johnston for (;;) { \ 26081f2e906SMark Johnston if (__bits != 0) { \ 26181f2e906SMark Johnston int __sig = ffs(__bits); \ 26281f2e906SMark Johnston __bits &= ~(1u << (__sig - 1)); \ 26381f2e906SMark Johnston sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \ 26481f2e906SMark Johnston __found = 1; \ 26581f2e906SMark Johnston break; \ 26681f2e906SMark Johnston } \ 26781f2e906SMark Johnston if (++__i == _SIG_WORDS) { \ 26881f2e906SMark Johnston __found = 0; \ 26981f2e906SMark Johnston break; \ 27081f2e906SMark Johnston } \ 27181f2e906SMark Johnston __bits = (set)->__bits[__i]; \ 27281f2e906SMark Johnston } \ 27381f2e906SMark Johnston __found != 0; \ 27481f2e906SMark Johnston }) 27581f2e906SMark Johnston 27681f2e906SMark Johnston #define SIG_FOREACH(i, set) \ 27781f2e906SMark Johnston for (int32_t __i = -1, __bits = 0; \ 27881f2e906SMark Johnston _SIG_FOREACH_ADVANCE(i, set); ) \ 27981f2e906SMark Johnston 2804fced864SKonstantin Belousov static sigset_t fastblock_mask; 2816b286ee8SKonstantin Belousov 2829104847fSDavid Xu static void 283c6d31b83SKonstantin Belousov ast_sig(struct thread *td, int tda) 284c6d31b83SKonstantin Belousov { 285c6d31b83SKonstantin Belousov struct proc *p; 286f0592b3cSKonstantin Belousov int old_boundary, sig; 287c6d31b83SKonstantin Belousov bool resched_sigs; 288c6d31b83SKonstantin Belousov 289c6d31b83SKonstantin Belousov p = td->td_proc; 290c6d31b83SKonstantin Belousov 291c6d31b83SKonstantin Belousov #ifdef DIAGNOSTIC 292c6d31b83SKonstantin Belousov if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) | 293c6d31b83SKonstantin Belousov TDAI(TDA_AST))) == 0) { 294c6d31b83SKonstantin Belousov PROC_LOCK(p); 295c6d31b83SKonstantin Belousov thread_lock(td); 296c6d31b83SKonstantin Belousov /* 297c6d31b83SKonstantin Belousov * Note that TDA_SIG should be re-read from 298c6d31b83SKonstantin Belousov * td_ast, since signal might have been delivered 299c6d31b83SKonstantin Belousov * after we cleared td_flags above. This is one of 300c6d31b83SKonstantin Belousov * the reason for looping check for AST condition. 301c6d31b83SKonstantin Belousov * See comment in userret() about P_PPWAIT. 302c6d31b83SKonstantin Belousov */ 303c6d31b83SKonstantin Belousov if ((p->p_flag & P_PPWAIT) == 0 && 304c6d31b83SKonstantin Belousov (td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 305c6d31b83SKonstantin Belousov if (SIGPENDING(td) && ((tda | td->td_ast) & 306c6d31b83SKonstantin Belousov (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) { 307c6d31b83SKonstantin Belousov thread_unlock(td); /* fix dumps */ 308c6d31b83SKonstantin Belousov panic( 309c6d31b83SKonstantin Belousov "failed2 to set signal flags for ast p %p " 310c6d31b83SKonstantin Belousov "td %p tda %#x td_ast %#x fl %#x", 311c6d31b83SKonstantin Belousov p, td, tda, td->td_ast, td->td_flags); 312c6d31b83SKonstantin Belousov } 313c6d31b83SKonstantin Belousov } 314c6d31b83SKonstantin Belousov thread_unlock(td); 315c6d31b83SKonstantin Belousov PROC_UNLOCK(p); 316c6d31b83SKonstantin Belousov } 317c6d31b83SKonstantin Belousov #endif 318c6d31b83SKonstantin Belousov 319c6d31b83SKonstantin Belousov /* 320c6d31b83SKonstantin Belousov * Check for signals. Unlocked reads of p_pendingcnt or 321c6d31b83SKonstantin Belousov * p_siglist might cause process-directed signal to be handled 322c6d31b83SKonstantin Belousov * later. 323c6d31b83SKonstantin Belousov */ 324f2fd7d8bSKonstantin Belousov if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 || 325c6d31b83SKonstantin Belousov !SIGISEMPTY(p->p_siglist)) { 326c6d31b83SKonstantin Belousov sigfastblock_fetch(td); 327c6d31b83SKonstantin Belousov PROC_LOCK(p); 328f0592b3cSKonstantin Belousov old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY); 329f0592b3cSKonstantin Belousov td->td_dbgflags |= TDB_BOUNDARY; 330c6d31b83SKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 331c6d31b83SKonstantin Belousov while ((sig = cursig(td)) != 0) { 332c6d31b83SKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 333c6d31b83SKonstantin Belousov postsig(sig); 334c6d31b83SKonstantin Belousov } 335c6d31b83SKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 336f0592b3cSKonstantin Belousov td->td_dbgflags &= old_boundary; 337c6d31b83SKonstantin Belousov PROC_UNLOCK(p); 338c6d31b83SKonstantin Belousov resched_sigs = true; 339c6d31b83SKonstantin Belousov } else { 340c6d31b83SKonstantin Belousov resched_sigs = false; 341c6d31b83SKonstantin Belousov } 342c6d31b83SKonstantin Belousov 343c6d31b83SKonstantin Belousov /* 344c6d31b83SKonstantin Belousov * Handle deferred update of the fast sigblock value, after 345c6d31b83SKonstantin Belousov * the postsig() loop was performed. 346c6d31b83SKonstantin Belousov */ 347c6d31b83SKonstantin Belousov sigfastblock_setpend(td, resched_sigs); 348c6d31b83SKonstantin Belousov } 349c6d31b83SKonstantin Belousov 350c6d31b83SKonstantin Belousov static void 351c6d31b83SKonstantin Belousov ast_sigsuspend(struct thread *td, int tda __unused) 352c6d31b83SKonstantin Belousov { 353c6d31b83SKonstantin Belousov MPASS((td->td_pflags & TDP_OLDMASK) != 0); 354c6d31b83SKonstantin Belousov td->td_pflags &= ~TDP_OLDMASK; 355c6d31b83SKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0); 356c6d31b83SKonstantin Belousov } 357c6d31b83SKonstantin Belousov 358c6d31b83SKonstantin Belousov static void 3599104847fSDavid Xu sigqueue_start(void) 3609104847fSDavid Xu { 3619104847fSDavid Xu ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 3629104847fSDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 3639104847fSDavid Xu uma_prealloc(ksiginfo_zone, preallocate_siginfo); 364b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 365b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 366b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 367146fc63fSKonstantin Belousov SIGFILLSET(fastblock_mask); 368146fc63fSKonstantin Belousov SIG_CANTMASK(fastblock_mask); 369c6d31b83SKonstantin Belousov ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig); 370c6d31b83SKonstantin Belousov ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP, 371c6d31b83SKonstantin Belousov TDP_OLDMASK, ast_sigsuspend); 3729104847fSDavid Xu } 3739104847fSDavid Xu 3745da49fcbSDavid Xu ksiginfo_t * 375cc29f221SKonstantin Belousov ksiginfo_alloc(int mwait) 3769104847fSDavid Xu { 377cc29f221SKonstantin Belousov MPASS(mwait == M_WAITOK || mwait == M_NOWAIT); 378ebceaf6dSDavid Xu 379cc29f221SKonstantin Belousov if (ksiginfo_zone == NULL) 3809104847fSDavid Xu return (NULL); 381cc29f221SKonstantin Belousov return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO)); 3829104847fSDavid Xu } 3839104847fSDavid Xu 3845da49fcbSDavid Xu void 3859104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 3869104847fSDavid Xu { 3879104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 3889104847fSDavid Xu } 3899104847fSDavid Xu 390cdb58f9dSKonstantin Belousov static __inline bool 3915da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 3925da49fcbSDavid Xu { 393cdb58f9dSKonstantin Belousov if ((ksi->ksi_flags & KSI_EXT) == 0) { 3945da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 395cdb58f9dSKonstantin Belousov return (true); 3965da49fcbSDavid Xu } 397cdb58f9dSKonstantin Belousov return (false); 3985da49fcbSDavid Xu } 3995da49fcbSDavid Xu 4009104847fSDavid Xu void 4019104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 4029104847fSDavid Xu { 4039104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 4043dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 40582a4538fSEric Badger SIGEMPTYSET(list->sq_ptrace); 4069104847fSDavid Xu TAILQ_INIT(&list->sq_list); 4079104847fSDavid Xu list->sq_proc = p; 4089104847fSDavid Xu list->sq_flags = SQ_INIT; 4099104847fSDavid Xu } 4109104847fSDavid Xu 4119104847fSDavid Xu /* 4129104847fSDavid Xu * Get a signal's ksiginfo. 4139104847fSDavid Xu * Return: 4149104847fSDavid Xu * 0 - signal not found 4159104847fSDavid Xu * others - signal number 4169104847fSDavid Xu */ 417a5799a4fSKonstantin Belousov static int 4189104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 4199104847fSDavid Xu { 4209104847fSDavid Xu struct proc *p = sq->sq_proc; 4219104847fSDavid Xu struct ksiginfo *ksi, *next; 4229104847fSDavid Xu int count = 0; 4239104847fSDavid Xu 4249104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4259104847fSDavid Xu 4269104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 4279104847fSDavid Xu return (0); 4289104847fSDavid Xu 42982a4538fSEric Badger if (SIGISMEMBER(sq->sq_ptrace, signo)) { 43082a4538fSEric Badger count++; 43182a4538fSEric Badger SIGDELSET(sq->sq_ptrace, signo); 43282a4538fSEric Badger si->ksi_flags |= KSI_PTRACE; 43382a4538fSEric Badger } 4343dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 4353dfcaad6SDavid Xu count++; 43682a4538fSEric Badger if (count == 1) 4373dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 4383dfcaad6SDavid Xu } 4393dfcaad6SDavid Xu 4405c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 4419104847fSDavid Xu if (ksi->ksi_signo == signo) { 4429104847fSDavid Xu if (count == 0) { 4439104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4445da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4459104847fSDavid Xu ksiginfo_copy(ksi, si); 4465da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4479104847fSDavid Xu p->p_pendingcnt--; 4489104847fSDavid Xu } 449016fa302SDavid Xu if (++count > 1) 450016fa302SDavid Xu break; 4519104847fSDavid Xu } 4529104847fSDavid Xu } 4539104847fSDavid Xu 4549104847fSDavid Xu if (count <= 1) 4559104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 4569104847fSDavid Xu si->ksi_signo = signo; 4579104847fSDavid Xu return (signo); 4589104847fSDavid Xu } 4599104847fSDavid Xu 4605da49fcbSDavid Xu void 4615da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 4625da49fcbSDavid Xu { 4635da49fcbSDavid Xu struct ksiginfo *kp; 4645da49fcbSDavid Xu struct proc *p; 4655da49fcbSDavid Xu sigqueue_t *sq; 4665da49fcbSDavid Xu 467ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 4685da49fcbSDavid Xu return; 4695da49fcbSDavid Xu 4705da49fcbSDavid Xu p = sq->sq_proc; 4715da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4725da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4735da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 4745da49fcbSDavid Xu p->p_pendingcnt--; 4755da49fcbSDavid Xu 4765da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 4775da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 4785da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 4795da49fcbSDavid Xu break; 4805da49fcbSDavid Xu } 48182a4538fSEric Badger if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 48282a4538fSEric Badger !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 4835da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 4845da49fcbSDavid Xu } 4855da49fcbSDavid Xu 486a5799a4fSKonstantin Belousov static int 4879104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 4889104847fSDavid Xu { 4899104847fSDavid Xu struct proc *p = sq->sq_proc; 4909104847fSDavid Xu struct ksiginfo *ksi; 4919104847fSDavid Xu int ret = 0; 4929104847fSDavid Xu 4939104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4949104847fSDavid Xu 49582a4538fSEric Badger /* 49682a4538fSEric Badger * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 49782a4538fSEric Badger * for these signals. 49882a4538fSEric Badger */ 4993dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 5003dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 5019104847fSDavid Xu goto out_set_bit; 5023dfcaad6SDavid Xu } 5039104847fSDavid Xu 5045da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 5055da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 5066a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 5076a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 5086a671a6bSKonstantin Belousov else 5095da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 5105da49fcbSDavid Xu si->ksi_sigq = sq; 5115da49fcbSDavid Xu goto out_set_bit; 5125da49fcbSDavid Xu } 5135da49fcbSDavid Xu 5143dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 5153dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 5169104847fSDavid Xu goto out_set_bit; 5173dfcaad6SDavid Xu } 5189104847fSDavid Xu 519ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 5209104847fSDavid Xu signal_overflow++; 5219104847fSDavid Xu ret = EAGAIN; 522cc29f221SKonstantin Belousov } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) { 5239104847fSDavid Xu signal_alloc_fail++; 5249104847fSDavid Xu ret = EAGAIN; 5259104847fSDavid Xu } else { 5269104847fSDavid Xu if (p != NULL) 5279104847fSDavid Xu p->p_pendingcnt++; 5289104847fSDavid Xu ksiginfo_copy(si, ksi); 5299104847fSDavid Xu ksi->ksi_signo = signo; 5306a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 5316a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 5326a671a6bSKonstantin Belousov else 5339104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 5345da49fcbSDavid Xu ksi->ksi_sigq = sq; 5359104847fSDavid Xu } 5369104847fSDavid Xu 53782a4538fSEric Badger if (ret != 0) { 53882a4538fSEric Badger if ((si->ksi_flags & KSI_PTRACE) != 0) { 53982a4538fSEric Badger SIGADDSET(sq->sq_ptrace, signo); 54082a4538fSEric Badger ret = 0; 54182a4538fSEric Badger goto out_set_bit; 54282a4538fSEric Badger } else if ((si->ksi_flags & KSI_TRAP) != 0 || 543a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 5443dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 5459104847fSDavid Xu ret = 0; 5469104847fSDavid Xu goto out_set_bit; 5479104847fSDavid Xu } 5489104847fSDavid Xu return (ret); 54982a4538fSEric Badger } 5509104847fSDavid Xu 5519104847fSDavid Xu out_set_bit: 5529104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 5539104847fSDavid Xu return (ret); 5549104847fSDavid Xu } 5559104847fSDavid Xu 5569104847fSDavid Xu void 5579104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 5589104847fSDavid Xu { 5599104847fSDavid Xu struct proc *p = sq->sq_proc; 5609104847fSDavid Xu ksiginfo_t *ksi; 5619104847fSDavid Xu 5629104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 5639104847fSDavid Xu 5645da49fcbSDavid Xu if (p != NULL) 5655da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5665da49fcbSDavid Xu 5679104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 5689104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5695da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5705da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5719104847fSDavid Xu p->p_pendingcnt--; 5729104847fSDavid Xu } 5739104847fSDavid Xu 5749104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 5753dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 57682a4538fSEric Badger SIGEMPTYSET(sq->sq_ptrace); 5779104847fSDavid Xu } 5789104847fSDavid Xu 579a5799a4fSKonstantin Belousov static void 580fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 5819104847fSDavid Xu { 582fc4ecc1dSDavid Xu sigset_t tmp; 5839104847fSDavid Xu struct proc *p1, *p2; 5849104847fSDavid Xu ksiginfo_t *ksi, *next; 5859104847fSDavid Xu 5869104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5879104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 5889104847fSDavid Xu p1 = src->sq_proc; 5899104847fSDavid Xu p2 = dst->sq_proc; 5909104847fSDavid Xu /* Move siginfo to target list */ 5915c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 592fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5939104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 5949104847fSDavid Xu if (p1 != NULL) 5959104847fSDavid Xu p1->p_pendingcnt--; 5969104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 5975da49fcbSDavid Xu ksi->ksi_sigq = dst; 5989104847fSDavid Xu if (p2 != NULL) 5999104847fSDavid Xu p2->p_pendingcnt++; 6009104847fSDavid Xu } 6019104847fSDavid Xu } 6029104847fSDavid Xu 6039104847fSDavid Xu /* Move pending bits to target list */ 6043dfcaad6SDavid Xu tmp = src->sq_kill; 605fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 6063dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 6073dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 6083dfcaad6SDavid Xu 60982a4538fSEric Badger tmp = src->sq_ptrace; 61082a4538fSEric Badger SIGSETAND(tmp, *set); 61182a4538fSEric Badger SIGSETOR(dst->sq_ptrace, tmp); 61282a4538fSEric Badger SIGSETNAND(src->sq_ptrace, tmp); 61382a4538fSEric Badger 6149104847fSDavid Xu tmp = src->sq_signals; 615fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 6169104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 6179104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 6189104847fSDavid Xu } 6199104847fSDavid Xu 620407af02bSDavid Xu #if 0 621a5799a4fSKonstantin Belousov static void 6229104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 6239104847fSDavid Xu { 6249104847fSDavid Xu sigset_t set; 6259104847fSDavid Xu 6269104847fSDavid Xu SIGEMPTYSET(set); 6279104847fSDavid Xu SIGADDSET(set, signo); 6289104847fSDavid Xu sigqueue_move_set(src, dst, &set); 6299104847fSDavid Xu } 630407af02bSDavid Xu #endif 6319104847fSDavid Xu 632a5799a4fSKonstantin Belousov static void 633fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 6349104847fSDavid Xu { 6359104847fSDavid Xu struct proc *p = sq->sq_proc; 6369104847fSDavid Xu ksiginfo_t *ksi, *next; 6379104847fSDavid Xu 6389104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 6399104847fSDavid Xu 6409104847fSDavid Xu /* Remove siginfo queue */ 6415c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 6429104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 6439104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 6445da49fcbSDavid Xu ksi->ksi_sigq = NULL; 6455da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 6469104847fSDavid Xu p->p_pendingcnt--; 6479104847fSDavid Xu } 6489104847fSDavid Xu } 6493dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 65082a4538fSEric Badger SIGSETNAND(sq->sq_ptrace, *set); 6519104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 6529104847fSDavid Xu } 6539104847fSDavid Xu 6549104847fSDavid Xu void 6559104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 6569104847fSDavid Xu { 6579104847fSDavid Xu sigset_t set; 6589104847fSDavid Xu 6599104847fSDavid Xu SIGEMPTYSET(set); 6609104847fSDavid Xu SIGADDSET(set, signo); 6619104847fSDavid Xu sigqueue_delete_set(sq, &set); 6629104847fSDavid Xu } 6639104847fSDavid Xu 6649104847fSDavid Xu /* Remove a set of signals for a process */ 665a5799a4fSKonstantin Belousov static void 666fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 6679104847fSDavid Xu { 6689104847fSDavid Xu sigqueue_t worklist; 6699104847fSDavid Xu struct thread *td0; 6709104847fSDavid Xu 6719104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 6729104847fSDavid Xu 6739104847fSDavid Xu sigqueue_init(&worklist, NULL); 6749104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 6759104847fSDavid Xu 6769104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 6779104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 6789104847fSDavid Xu 6799104847fSDavid Xu sigqueue_flush(&worklist); 6809104847fSDavid Xu } 6819104847fSDavid Xu 6829104847fSDavid Xu void 6839104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 6849104847fSDavid Xu { 6859104847fSDavid Xu sigset_t set; 6869104847fSDavid Xu 6879104847fSDavid Xu SIGEMPTYSET(set); 6889104847fSDavid Xu SIGADDSET(set, signo); 6899104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 6909104847fSDavid Xu } 6919104847fSDavid Xu 692a5799a4fSKonstantin Belousov static void 6939104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 6949104847fSDavid Xu { 6959104847fSDavid Xu sigset_t set; 6969104847fSDavid Xu 6979104847fSDavid Xu SIGEMPTYSET(set); 6989104847fSDavid Xu SIGADDSET(set, SIGSTOP); 6999104847fSDavid Xu SIGADDSET(set, SIGTSTP); 7009104847fSDavid Xu SIGADDSET(set, SIGTTIN); 7019104847fSDavid Xu SIGADDSET(set, SIGTTOU); 7029104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 7039104847fSDavid Xu } 7049104847fSDavid Xu 705fbbeeb6cSBruce Evans /* 7061968f37bSJohn Baldwin * Determine signal that should be delivered to thread td, the current 7071968f37bSJohn Baldwin * thread, 0 if none. If there is a pending stop signal with default 708fbbeeb6cSBruce Evans * action, the process stops in issignal(). 709fbbeeb6cSBruce Evans */ 710fbbeeb6cSBruce Evans int 7113cf3b9f0SJohn Baldwin cursig(struct thread *td) 712fbbeeb6cSBruce Evans { 713c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 71490af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 715a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 7163cf3b9f0SJohn Baldwin return (SIGPENDING(td) ? issignal(td) : 0); 717fbbeeb6cSBruce Evans } 718fbbeeb6cSBruce Evans 71979065dbaSBruce Evans /* 72079065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 7219104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 7224093529dSJeff Roberson * unmasked in td_sigmask. 72379065dbaSBruce Evans */ 72479065dbaSBruce Evans void 7254093529dSJeff Roberson signotify(struct thread *td) 72679065dbaSBruce Evans { 7274093529dSJeff Roberson 728ddd4d15eSMatt Macy PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 7294093529dSJeff Roberson 730c6d31b83SKonstantin Belousov if (SIGPENDING(td)) 731c6d31b83SKonstantin Belousov ast_sched(td, TDA_SIG); 7328b94a061SJohn Baldwin } 7338b94a061SJohn Baldwin 734affd9185SKonstantin Belousov /* 735affd9185SKonstantin Belousov * Returns 1 (true) if altstack is configured for the thread, and the 736affd9185SKonstantin Belousov * passed stack bottom address falls into the altstack range. Handles 737affd9185SKonstantin Belousov * the 43 compat special case where the alt stack size is zero. 738affd9185SKonstantin Belousov */ 7398b94a061SJohn Baldwin int 7408b94a061SJohn Baldwin sigonstack(size_t sp) 7418b94a061SJohn Baldwin { 742affd9185SKonstantin Belousov struct thread *td; 7438b94a061SJohn Baldwin 744affd9185SKonstantin Belousov td = curthread; 745affd9185SKonstantin Belousov if ((td->td_pflags & TDP_ALTSTACK) == 0) 746affd9185SKonstantin Belousov return (0); 7471930e303SPoul-Henning Kamp #if defined(COMPAT_43) 7487e097daaSKonstantin Belousov if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0) 749affd9185SKonstantin Belousov return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0); 7508b94a061SJohn Baldwin #endif 751affd9185SKonstantin Belousov return (sp >= (size_t)td->td_sigstk.ss_sp && 752affd9185SKonstantin Belousov sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp); 7538b94a061SJohn Baldwin } 75479065dbaSBruce Evans 7556f841fb7SMarcel Moolenaar static __inline int 7566f841fb7SMarcel Moolenaar sigprop(int sig) 7572c42a146SMarcel Moolenaar { 7586f841fb7SMarcel Moolenaar 759ed6d876bSBrooks Davis if (sig > 0 && sig < nitems(sigproptbl)) 760ed6d876bSBrooks Davis return (sigproptbl[sig]); 7612c42a146SMarcel Moolenaar return (0); 762df8bae1dSRodney W. Grimes } 7632c42a146SMarcel Moolenaar 764350ae563SKonstantin Belousov static bool 765ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag) 766350ae563SKonstantin Belousov { 767350ae563SKonstantin Belousov 768c83655f3SKonstantin Belousov /* 769c83655f3SKonstantin Belousov * SA_SIGINFO is reset when signal disposition is set to 770c83655f3SKonstantin Belousov * ignore or default. Other flags are kept according to user 771c83655f3SKonstantin Belousov * settings. 772c83655f3SKonstantin Belousov */ 773c83655f3SKonstantin Belousov return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 774c83655f3SKonstantin Belousov ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 775c83655f3SKonstantin Belousov (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 776350ae563SKonstantin Belousov } 777350ae563SKonstantin Belousov 7782c42a146SMarcel Moolenaar /* 7798f19eb88SIan Dowse * kern_sigaction 7802c42a146SMarcel Moolenaar * sigaction 78123eeeff7SPeter Wemm * freebsd4_sigaction 7822c42a146SMarcel Moolenaar * osigaction 7832c42a146SMarcel Moolenaar */ 7848f19eb88SIan Dowse int 785ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 786ea566832SEd Schouten struct sigaction *oact, int flags) 787df8bae1dSRodney W. Grimes { 78890af4afaSJohn Baldwin struct sigacts *ps; 7898f19eb88SIan Dowse struct proc *p = td->td_proc; 790df8bae1dSRodney W. Grimes 7912899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 7922c42a146SMarcel Moolenaar return (EINVAL); 793271ab240SKonstantin Belousov if (act != NULL && act->sa_handler != SIG_DFL && 794271ab240SKonstantin Belousov act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 795271ab240SKonstantin Belousov SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 796271ab240SKonstantin Belousov SA_NOCLDWAIT | SA_SIGINFO)) != 0) 7972d864174SKonstantin Belousov return (EINVAL); 7982c42a146SMarcel Moolenaar 799628d2653SJohn Baldwin PROC_LOCK(p); 800628d2653SJohn Baldwin ps = p->p_sigacts; 80190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 8022c42a146SMarcel Moolenaar if (oact) { 803fb441a88SKonstantin Belousov memset(oact, 0, sizeof(*oact)); 8042c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 8052c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 8062c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 8072c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 8082c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 8092c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 8102c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 8112c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 8122c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 81310c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 8142c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 81510c2b8e1SDavid E. O'Brien oact->sa_sigaction = 81610c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 81710c2b8e1SDavid E. O'Brien } else 81810c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 81990af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 8202c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 82190af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 8222c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 8232c42a146SMarcel Moolenaar } 8242c42a146SMarcel Moolenaar if (act) { 8252c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 826628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 82790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 828628d2653SJohn Baldwin PROC_UNLOCK(p); 8292c42a146SMarcel Moolenaar return (EINVAL); 830628d2653SJohn Baldwin } 8312c42a146SMarcel Moolenaar 832df8bae1dSRodney W. Grimes /* 833df8bae1dSRodney W. Grimes * Change setting atomically. 834df8bae1dSRodney W. Grimes */ 8352c42a146SMarcel Moolenaar 8362c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 8372c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 838350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_SIGINFO)) { 839aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 840aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 84180f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 84280f42b55SIan Dowse } else { 84380f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 8442c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 8452c42a146SMarcel Moolenaar } 846350ae563SKonstantin Belousov if (!sigact_flag_test(act, SA_RESTART)) 8472c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 848df8bae1dSRodney W. Grimes else 8492c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 850350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_ONSTACK)) 8512c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 852df8bae1dSRodney W. Grimes else 8532c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 854350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_RESETHAND)) 8552c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 8561e41c1b5SSteven Wallace else 8572c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 858350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_NODEFER)) 8592c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 860289ccde0SPeter Wemm else 8612c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 8622c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 8632c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 86490af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 8656626c604SJulian Elischer else 86690af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 867ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 868245f17d4SJoerg Wunsch /* 8692c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 8702c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 8712c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 8722c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 873245f17d4SJoerg Wunsch */ 874245f17d4SJoerg Wunsch if (p->p_pid == 1) 87590af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 8766626c604SJulian Elischer else 87790af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 878245f17d4SJoerg Wunsch } else 87990af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 880ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 88190af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 882ba1551caSIan Dowse else 88390af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes /* 88690af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 8872c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 88890af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 8892c42a146SMarcel Moolenaar * have to restart the process. 890df8bae1dSRodney W. Grimes */ 8912c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 892fd50a707SBrooks Davis (sigprop(sig) & SIGPROP_IGNORE && 8932c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 8942c42a146SMarcel Moolenaar /* never to be seen again */ 8959104847fSDavid Xu sigqueue_delete_proc(p, sig); 8962c42a146SMarcel Moolenaar if (sig != SIGCONT) 8972c42a146SMarcel Moolenaar /* easier in psignal */ 89890af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 89990af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 900645682fdSLuoqi Chen } else { 90190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 9022c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 90390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 9042c42a146SMarcel Moolenaar else 90590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 9062c42a146SMarcel Moolenaar } 90723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 90823eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 90923eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 91023eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 91123eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 91223eeeff7SPeter Wemm else 91323eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 91423eeeff7SPeter Wemm #endif 915e8ebc08fSPeter Wemm #ifdef COMPAT_43 916645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 91723eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 91823eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 919645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 920645682fdSLuoqi Chen else 921645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 922e8ebc08fSPeter Wemm #endif 923df8bae1dSRodney W. Grimes } 92490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 925628d2653SJohn Baldwin PROC_UNLOCK(p); 9262c42a146SMarcel Moolenaar return (0); 9272c42a146SMarcel Moolenaar } 9282c42a146SMarcel Moolenaar 9292c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9302c42a146SMarcel Moolenaar struct sigaction_args { 9312c42a146SMarcel Moolenaar int sig; 9322c42a146SMarcel Moolenaar struct sigaction *act; 9332c42a146SMarcel Moolenaar struct sigaction *oact; 9342c42a146SMarcel Moolenaar }; 9352c42a146SMarcel Moolenaar #endif 9362c42a146SMarcel Moolenaar int 937e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap) 9382c42a146SMarcel Moolenaar { 9392c42a146SMarcel Moolenaar struct sigaction act, oact; 940e052a8b9SEd Maste struct sigaction *actp, *oactp; 9412c42a146SMarcel Moolenaar int error; 9422c42a146SMarcel Moolenaar 9436f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 9446f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 9452c42a146SMarcel Moolenaar if (actp) { 9466f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 9472c42a146SMarcel Moolenaar if (error) 94844443757STim J. Robbins return (error); 9492c42a146SMarcel Moolenaar } 9508f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 95125d6dc06SJohn Baldwin if (oactp && !error) 9526f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 9532c42a146SMarcel Moolenaar return (error); 9542c42a146SMarcel Moolenaar } 9552c42a146SMarcel Moolenaar 95623eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 95723eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 95823eeeff7SPeter Wemm struct freebsd4_sigaction_args { 95923eeeff7SPeter Wemm int sig; 96023eeeff7SPeter Wemm struct sigaction *act; 96123eeeff7SPeter Wemm struct sigaction *oact; 96223eeeff7SPeter Wemm }; 96323eeeff7SPeter Wemm #endif 96423eeeff7SPeter Wemm int 965e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 96623eeeff7SPeter Wemm { 96723eeeff7SPeter Wemm struct sigaction act, oact; 968e052a8b9SEd Maste struct sigaction *actp, *oactp; 96923eeeff7SPeter Wemm int error; 97023eeeff7SPeter Wemm 97123eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 97223eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 97323eeeff7SPeter Wemm if (actp) { 97423eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 97523eeeff7SPeter Wemm if (error) 97644443757STim J. Robbins return (error); 97723eeeff7SPeter Wemm } 97823eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 979a14e1189SJohn Baldwin if (oactp && !error) 98023eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 98123eeeff7SPeter Wemm return (error); 98223eeeff7SPeter Wemm } 98323eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 98423eeeff7SPeter Wemm 98531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9862c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9872c42a146SMarcel Moolenaar struct osigaction_args { 9882c42a146SMarcel Moolenaar int signum; 9892c42a146SMarcel Moolenaar struct osigaction *nsa; 9902c42a146SMarcel Moolenaar struct osigaction *osa; 9912c42a146SMarcel Moolenaar }; 9922c42a146SMarcel Moolenaar #endif 9932c42a146SMarcel Moolenaar int 994e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap) 9952c42a146SMarcel Moolenaar { 9962c42a146SMarcel Moolenaar struct osigaction sa; 9972c42a146SMarcel Moolenaar struct sigaction nsa, osa; 998e052a8b9SEd Maste struct sigaction *nsap, *osap; 9992c42a146SMarcel Moolenaar int error; 10002c42a146SMarcel Moolenaar 10016f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 10026f841fb7SMarcel Moolenaar return (EINVAL); 1003fb99ab88SMatthew Dillon 10046f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 10056f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 1006fb99ab88SMatthew Dillon 10072c42a146SMarcel Moolenaar if (nsap) { 10086f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 10092c42a146SMarcel Moolenaar if (error) 101044443757STim J. Robbins return (error); 10112c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 10122c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 10132c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 10142c42a146SMarcel Moolenaar } 101523eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 10162c42a146SMarcel Moolenaar if (osap && !error) { 10172c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 10182c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 10192c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 10206f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 10212c42a146SMarcel Moolenaar } 10222c42a146SMarcel Moolenaar return (error); 10232c42a146SMarcel Moolenaar } 102423eeeff7SPeter Wemm 102573dbd3daSJohn Baldwin #if !defined(__i386__) 102623eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 102723eeeff7SPeter Wemm int 1028e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap) 102923eeeff7SPeter Wemm { 103023eeeff7SPeter Wemm 103123eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 103223eeeff7SPeter Wemm } 103323eeeff7SPeter Wemm #endif 103431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1035df8bae1dSRodney W. Grimes 1036df8bae1dSRodney W. Grimes /* 1037df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 1038df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 1039df8bae1dSRodney W. Grimes */ 1040df8bae1dSRodney W. Grimes void 1041e052a8b9SEd Maste siginit(struct proc *p) 1042df8bae1dSRodney W. Grimes { 10433e85b721SEd Maste int i; 104490af4afaSJohn Baldwin struct sigacts *ps; 1045df8bae1dSRodney W. Grimes 1046628d2653SJohn Baldwin PROC_LOCK(p); 104790af4afaSJohn Baldwin ps = p->p_sigacts; 104890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 1049350ae563SKonstantin Belousov for (i = 1; i <= NSIG; i++) { 1050fd50a707SBrooks Davis if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 105190af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 1052350ae563SKonstantin Belousov } 1053350ae563SKonstantin Belousov } 105490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1055628d2653SJohn Baldwin PROC_UNLOCK(p); 1056df8bae1dSRodney W. Grimes } 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes /* 1059350ae563SKonstantin Belousov * Reset specified signal to the default disposition. 1060350ae563SKonstantin Belousov */ 1061350ae563SKonstantin Belousov static void 1062350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig) 1063350ae563SKonstantin Belousov { 1064350ae563SKonstantin Belousov 1065350ae563SKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 1066350ae563SKonstantin Belousov SIGDELSET(ps->ps_sigcatch, sig); 1067fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 1068350ae563SKonstantin Belousov SIGADDSET(ps->ps_sigignore, sig); 1069350ae563SKonstantin Belousov ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1070350ae563SKonstantin Belousov SIGDELSET(ps->ps_siginfo, sig); 1071350ae563SKonstantin Belousov } 1072350ae563SKonstantin Belousov 1073350ae563SKonstantin Belousov /* 1074df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 1075df8bae1dSRodney W. Grimes */ 1076df8bae1dSRodney W. Grimes void 1077a30ec4b9SDavid Xu execsigs(struct proc *p) 1078df8bae1dSRodney W. Grimes { 1079a30ec4b9SDavid Xu struct sigacts *ps; 1080a30ec4b9SDavid Xu struct thread *td; 1081df8bae1dSRodney W. Grimes 1082df8bae1dSRodney W. Grimes /* 1083df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 10844093529dSJeff Roberson * through td_sigmask (unless they were caught, 1085df8bae1dSRodney W. Grimes * and are now ignored by default). 1086df8bae1dSRodney W. Grimes */ 10879b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1088628d2653SJohn Baldwin ps = p->p_sigacts; 108990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 1090079c5b9eSKyle Evans sig_drop_caught(p); 1091f3fe76ecSEd Schouten 1092f3fe76ecSEd Schouten /* 1093df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 1094df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 1095df8bae1dSRodney W. Grimes */ 1096469ec1ebSKonstantin Belousov td = curthread; 1097469ec1ebSKonstantin Belousov MPASS(td->td_proc == p); 1098a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 1099a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1100a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 1101a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 110280e907a1SPeter Wemm /* 110380e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 110480e907a1SPeter Wemm */ 110590af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1106c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1107c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 110890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes 1111df8bae1dSRodney W. Grimes /* 1112e77daab1SJohn Baldwin * kern_sigprocmask() 11137c8fdcbdSMatthew Dillon * 1114628d2653SJohn Baldwin * Manipulate signal mask. 1115df8bae1dSRodney W. Grimes */ 1116e77daab1SJohn Baldwin int 11176b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 111884440afbSKonstantin Belousov int flags) 11192c42a146SMarcel Moolenaar { 11206b286ee8SKonstantin Belousov sigset_t new_block, oset1; 11216b286ee8SKonstantin Belousov struct proc *p; 11222c42a146SMarcel Moolenaar int error; 11232c42a146SMarcel Moolenaar 11246b286ee8SKonstantin Belousov p = td->td_proc; 112570778bbaSKonstantin Belousov if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 112670778bbaSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 112770778bbaSKonstantin Belousov else 11286b286ee8SKonstantin Belousov PROC_LOCK(p); 112970778bbaSKonstantin Belousov mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 113070778bbaSKonstantin Belousov ? MA_OWNED : MA_NOTOWNED); 11312c42a146SMarcel Moolenaar if (oset != NULL) 11324093529dSJeff Roberson *oset = td->td_sigmask; 11332c42a146SMarcel Moolenaar 11342c42a146SMarcel Moolenaar error = 0; 11352c42a146SMarcel Moolenaar if (set != NULL) { 11362c42a146SMarcel Moolenaar switch (how) { 11372c42a146SMarcel Moolenaar case SIG_BLOCK: 1138645682fdSLuoqi Chen SIG_CANTMASK(*set); 11396b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 11404093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 11416b286ee8SKonstantin Belousov new_block = td->td_sigmask; 11426b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 11432c42a146SMarcel Moolenaar break; 11442c42a146SMarcel Moolenaar case SIG_UNBLOCK: 11454093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 11464093529dSJeff Roberson signotify(td); 1147407af02bSDavid Xu goto out; 11482c42a146SMarcel Moolenaar case SIG_SETMASK: 1149645682fdSLuoqi Chen SIG_CANTMASK(*set); 11506b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 115184440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 11524093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1153645682fdSLuoqi Chen else 11544093529dSJeff Roberson td->td_sigmask = *set; 11556b286ee8SKonstantin Belousov new_block = td->td_sigmask; 11566b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 11574093529dSJeff Roberson signotify(td); 11582c42a146SMarcel Moolenaar break; 11592c42a146SMarcel Moolenaar default: 11602c42a146SMarcel Moolenaar error = EINVAL; 1161407af02bSDavid Xu goto out; 11622c42a146SMarcel Moolenaar } 11636b286ee8SKonstantin Belousov 11646b286ee8SKonstantin Belousov /* 11657df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 11666b286ee8SKonstantin Belousov * blocked, but are blocked now. 11676b286ee8SKonstantin Belousov * 11686b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 11696b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1170407af02bSDavid Xu * signal delivery to some thread that does not block the 1171407af02bSDavid Xu * signal, possibly waking it up. 11726b286ee8SKonstantin Belousov */ 11736b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 117480a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1175407af02bSDavid Xu } 11766b286ee8SKonstantin Belousov 1177407af02bSDavid Xu out: 117884440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 11796b286ee8SKonstantin Belousov PROC_UNLOCK(p); 11802c42a146SMarcel Moolenaar return (error); 11812c42a146SMarcel Moolenaar } 11822c42a146SMarcel Moolenaar 1183d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1184df8bae1dSRodney W. Grimes struct sigprocmask_args { 1185df8bae1dSRodney W. Grimes int how; 11862c42a146SMarcel Moolenaar const sigset_t *set; 11872c42a146SMarcel Moolenaar sigset_t *oset; 1188df8bae1dSRodney W. Grimes }; 1189d2d3e875SBruce Evans #endif 119026f9a767SRodney W. Grimes int 1191e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1192df8bae1dSRodney W. Grimes { 11932c42a146SMarcel Moolenaar sigset_t set, oset; 11942c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 11952c42a146SMarcel Moolenaar int error; 1196df8bae1dSRodney W. Grimes 11976f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 11986f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 11992c42a146SMarcel Moolenaar if (setp) { 12006f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 12012c42a146SMarcel Moolenaar if (error) 12022c42a146SMarcel Moolenaar return (error); 1203df8bae1dSRodney W. Grimes } 1204e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 12052c42a146SMarcel Moolenaar if (osetp && !error) { 12066f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 12072c42a146SMarcel Moolenaar } 12082c42a146SMarcel Moolenaar return (error); 12092c42a146SMarcel Moolenaar } 12102c42a146SMarcel Moolenaar 121131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 12122c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 12132c42a146SMarcel Moolenaar struct osigprocmask_args { 12142c42a146SMarcel Moolenaar int how; 12152c42a146SMarcel Moolenaar osigset_t mask; 12162c42a146SMarcel Moolenaar }; 12172c42a146SMarcel Moolenaar #endif 12182c42a146SMarcel Moolenaar int 1219e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap) 12202c42a146SMarcel Moolenaar { 12212c42a146SMarcel Moolenaar sigset_t set, oset; 12222c42a146SMarcel Moolenaar int error; 12232c42a146SMarcel Moolenaar 12242c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1225e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1226b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1227df8bae1dSRodney W. Grimes return (error); 1228df8bae1dSRodney W. Grimes } 122931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1230df8bae1dSRodney W. Grimes 123126f9a767SRodney W. Grimes int 12328451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1233a447cd8bSJeff Roberson { 12349104847fSDavid Xu ksiginfo_t ksi; 1235a447cd8bSJeff Roberson sigset_t set; 1236a447cd8bSJeff Roberson int error; 1237a447cd8bSJeff Roberson 1238a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 123936939a0aSDavid Xu if (error) { 124036939a0aSDavid Xu td->td_retval[0] = error; 124136939a0aSDavid Xu return (0); 124236939a0aSDavid Xu } 1243a447cd8bSJeff Roberson 12449104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 124536939a0aSDavid Xu if (error) { 1246acced8b0SKonstantin Belousov /* 1247acced8b0SKonstantin Belousov * sigwait() function shall not return EINTR, but 1248acced8b0SKonstantin Belousov * the syscall does. Non-ancient libc provides the 1249acced8b0SKonstantin Belousov * wrapper which hides EINTR. Otherwise, EINTR return 1250acced8b0SKonstantin Belousov * is used by libthr to handle required cancellation 1251acced8b0SKonstantin Belousov * point in the sigwait(). 1252acced8b0SKonstantin Belousov */ 12538e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 1254afb36e28SKonstantin Belousov return (ERESTART); 125536939a0aSDavid Xu td->td_retval[0] = error; 125636939a0aSDavid Xu return (0); 125736939a0aSDavid Xu } 1258a447cd8bSJeff Roberson 12599104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 126036939a0aSDavid Xu td->td_retval[0] = error; 126136939a0aSDavid Xu return (0); 1262a447cd8bSJeff Roberson } 12630c14ff0eSRobert Watson 1264a447cd8bSJeff Roberson int 12658451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1266a447cd8bSJeff Roberson { 1267a447cd8bSJeff Roberson struct timespec ts; 1268a447cd8bSJeff Roberson struct timespec *timeout; 1269a447cd8bSJeff Roberson sigset_t set; 12709104847fSDavid Xu ksiginfo_t ksi; 1271a447cd8bSJeff Roberson int error; 1272a447cd8bSJeff Roberson 1273a447cd8bSJeff Roberson if (uap->timeout) { 1274a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1275a447cd8bSJeff Roberson if (error) 1276a447cd8bSJeff Roberson return (error); 1277a447cd8bSJeff Roberson 1278a447cd8bSJeff Roberson timeout = &ts; 1279a447cd8bSJeff Roberson } else 1280a447cd8bSJeff Roberson timeout = NULL; 1281a447cd8bSJeff Roberson 1282a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1283a447cd8bSJeff Roberson if (error) 1284a447cd8bSJeff Roberson return (error); 1285a447cd8bSJeff Roberson 12869104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1287a447cd8bSJeff Roberson if (error) 1288a447cd8bSJeff Roberson return (error); 12899dde3bc9SDavid Xu 1290418228dfSDavid Xu if (uap->info) 12919104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12929104847fSDavid Xu 12939104847fSDavid Xu if (error == 0) 12949104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1295a447cd8bSJeff Roberson return (error); 1296a447cd8bSJeff Roberson } 1297a447cd8bSJeff Roberson 1298a447cd8bSJeff Roberson int 12998451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1300a447cd8bSJeff Roberson { 13019104847fSDavid Xu ksiginfo_t ksi; 1302a447cd8bSJeff Roberson sigset_t set; 1303a447cd8bSJeff Roberson int error; 1304a447cd8bSJeff Roberson 1305a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1306a447cd8bSJeff Roberson if (error) 1307a447cd8bSJeff Roberson return (error); 1308a447cd8bSJeff Roberson 13099104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1310a447cd8bSJeff Roberson if (error) 1311a447cd8bSJeff Roberson return (error); 1312a447cd8bSJeff Roberson 1313418228dfSDavid Xu if (uap->info) 13149104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 13159104847fSDavid Xu 13169104847fSDavid Xu if (error == 0) 13179104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1318a447cd8bSJeff Roberson return (error); 1319a447cd8bSJeff Roberson } 1320a447cd8bSJeff Roberson 132186be94fcSTycho Nightingale static void 132286be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 132386be94fcSTycho Nightingale { 132486be94fcSTycho Nightingale struct thread *thr; 132586be94fcSTycho Nightingale 132686be94fcSTycho Nightingale FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 132786be94fcSTycho Nightingale if (thr == td) 132886be94fcSTycho Nightingale thr->td_si = *si; 132986be94fcSTycho Nightingale else 133086be94fcSTycho Nightingale thr->td_si.si_signo = 0; 133186be94fcSTycho Nightingale } 133286be94fcSTycho Nightingale } 133386be94fcSTycho Nightingale 1334c6511aeaSDavid Xu int 13359104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1336a447cd8bSJeff Roberson struct timespec *timeout) 1337a447cd8bSJeff Roberson { 13383074d1b4SDavid Xu struct sigacts *ps; 1339407af02bSDavid Xu sigset_t saved_mask, new_block; 1340a447cd8bSJeff Roberson struct proc *p; 13414a700f3cSDmitry Chagin int error, sig, timevalid = 0; 13424a700f3cSDmitry Chagin sbintime_t sbt, precision, tsbt; 13434a700f3cSDmitry Chagin struct timespec ts; 1344ef401a85SKonstantin Belousov bool traced; 1345a447cd8bSJeff Roberson 1346a447cd8bSJeff Roberson p = td->td_proc; 1347a447cd8bSJeff Roberson error = 0; 1348ef401a85SKonstantin Belousov traced = false; 1349a447cd8bSJeff Roberson 1350dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1351dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1352dbec10e0SJonathan T. Looney 1353407af02bSDavid Xu if (timeout != NULL) { 13541089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 13551089f031SDavid Xu timevalid = 1; 13564a700f3cSDmitry Chagin ts = *timeout; 13574a700f3cSDmitry Chagin if (ts.tv_sec < INT32_MAX / 2) { 13584a700f3cSDmitry Chagin tsbt = tstosbt(ts); 13594a700f3cSDmitry Chagin precision = tsbt; 13604a700f3cSDmitry Chagin precision >>= tc_precexp; 13614a700f3cSDmitry Chagin if (TIMESEL(&sbt, tsbt)) 13624a700f3cSDmitry Chagin sbt += tc_tick_sbt; 13634a700f3cSDmitry Chagin sbt += tsbt; 13644a700f3cSDmitry Chagin } else 13654a700f3cSDmitry Chagin precision = sbt = 0; 13661089f031SDavid Xu } 13674a700f3cSDmitry Chagin } else 13684a700f3cSDmitry Chagin precision = sbt = 0; 1369407af02bSDavid Xu ksiginfo_init(ksi); 1370407af02bSDavid Xu /* Some signals can not be waited for. */ 1371407af02bSDavid Xu SIG_CANTMASK(waitset); 1372407af02bSDavid Xu ps = p->p_sigacts; 1373407af02bSDavid Xu PROC_LOCK(p); 1374407af02bSDavid Xu saved_mask = td->td_sigmask; 1375407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1376bc387624SKonstantin Belousov if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 || 1377b599982bSKonstantin Belousov !kern_sig_discard_ign) { 1378b599982bSKonstantin Belousov thread_lock(td); 1379b599982bSKonstantin Belousov td->td_flags |= TDF_SIGWAIT; 1380b599982bSKonstantin Belousov thread_unlock(td); 1381b599982bSKonstantin Belousov } 1382407af02bSDavid Xu for (;;) { 13833074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 13843cf3b9f0SJohn Baldwin sig = cursig(td); 13853074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 138646e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 1387407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1388407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1389407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1390407af02bSDavid Xu error = 0; 1391407af02bSDavid Xu break; 13923074d1b4SDavid Xu } 13937e0221a2SDavid Xu } 13947e0221a2SDavid Xu 1395407af02bSDavid Xu if (error != 0) 1396407af02bSDavid Xu break; 13973074d1b4SDavid Xu 1398a447cd8bSJeff Roberson /* 1399a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1400a447cd8bSJeff Roberson * signals. 1401a447cd8bSJeff Roberson */ 14024a700f3cSDmitry Chagin if (timeout != NULL && !timevalid) { 1403a447cd8bSJeff Roberson error = EINVAL; 1404407af02bSDavid Xu break; 1405a447cd8bSJeff Roberson } 1406a447cd8bSJeff Roberson 1407ef401a85SKonstantin Belousov if (traced) { 1408ef401a85SKonstantin Belousov error = EINTR; 1409ef401a85SKonstantin Belousov break; 1410ef401a85SKonstantin Belousov } 1411ef401a85SKonstantin Belousov 14124a700f3cSDmitry Chagin error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, 14134a700f3cSDmitry Chagin "sigwait", sbt, precision, C_ABSOLUTE); 1414407af02bSDavid Xu 1415afb36e28SKonstantin Belousov /* The syscalls can not be restarted. */ 1416afb36e28SKonstantin Belousov if (error == ERESTART) 14173074d1b4SDavid Xu error = EINTR; 1418afb36e28SKonstantin Belousov 1419ef401a85SKonstantin Belousov /* 1420ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1421ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1422ef401a85SKonstantin Belousov * EINTR after wait was done. Only do this as last 1423ef401a85SKonstantin Belousov * resort after rechecking for possible queued signals 1424ef401a85SKonstantin Belousov * and expired timeouts. 1425ef401a85SKonstantin Belousov */ 1426ef401a85SKonstantin Belousov if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0) 1427ef401a85SKonstantin Belousov traced = true; 1428407af02bSDavid Xu } 1429b599982bSKonstantin Belousov thread_lock(td); 1430b599982bSKonstantin Belousov td->td_flags &= ~TDF_SIGWAIT; 1431b599982bSKonstantin Belousov thread_unlock(td); 14329dde3bc9SDavid Xu 1433407af02bSDavid Xu new_block = saved_mask; 1434407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1435407af02bSDavid Xu td->td_sigmask = saved_mask; 1436407af02bSDavid Xu /* 1437407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1438407af02bSDavid Xu * notification. 1439407af02bSDavid Xu */ 1440407af02bSDavid Xu if (p->p_numthreads != 1) 1441407af02bSDavid Xu reschedule_signals(p, new_block, 0); 14425d217f17SJohn Birrell 1443407af02bSDavid Xu if (error == 0) { 144436160958SMark Johnston SDT_PROBE2(proc, , , signal__clear, sig, ksi); 14455d217f17SJohn Birrell 144656c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 144756c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 14487e0221a2SDavid Xu 14497e0221a2SDavid Xu #ifdef KTRACE 14507e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 14517e0221a2SDavid Xu sig_t action; 14527e0221a2SDavid Xu 14533074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1454a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 145590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 145661009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 14577e0221a2SDavid Xu } 1458a447cd8bSJeff Roberson #endif 145986be94fcSTycho Nightingale if (sig == SIGKILL) { 146086be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi->ksi_info); 14617e0221a2SDavid Xu sigexit(td, sig); 14623074d1b4SDavid Xu } 146386be94fcSTycho Nightingale } 1464a447cd8bSJeff Roberson PROC_UNLOCK(p); 1465a447cd8bSJeff Roberson return (error); 1466a447cd8bSJeff Roberson } 1467a447cd8bSJeff Roberson 14689104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 14699104847fSDavid Xu struct sigpending_args { 14709104847fSDavid Xu sigset_t *set; 14719104847fSDavid Xu }; 14729104847fSDavid Xu #endif 1473a447cd8bSJeff Roberson int 1474e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap) 1475df8bae1dSRodney W. Grimes { 1476b40ce416SJulian Elischer struct proc *p = td->td_proc; 14779104847fSDavid Xu sigset_t pending; 1478df8bae1dSRodney W. Grimes 1479628d2653SJohn Baldwin PROC_LOCK(p); 14809104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 14819104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1482628d2653SJohn Baldwin PROC_UNLOCK(p); 14839104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 14842c42a146SMarcel Moolenaar } 14852c42a146SMarcel Moolenaar 148631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 14872c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 14882c42a146SMarcel Moolenaar struct osigpending_args { 14892c42a146SMarcel Moolenaar int dummy; 14902c42a146SMarcel Moolenaar }; 14912c42a146SMarcel Moolenaar #endif 14922c42a146SMarcel Moolenaar int 1493e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap) 14942c42a146SMarcel Moolenaar { 1495b40ce416SJulian Elischer struct proc *p = td->td_proc; 14969104847fSDavid Xu sigset_t pending; 1497b40ce416SJulian Elischer 1498628d2653SJohn Baldwin PROC_LOCK(p); 14999104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 15009104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1501628d2653SJohn Baldwin PROC_UNLOCK(p); 15029104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1503df8bae1dSRodney W. Grimes return (0); 1504df8bae1dSRodney W. Grimes } 150531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1506df8bae1dSRodney W. Grimes 15071930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1508df8bae1dSRodney W. Grimes /* 1509df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1510df8bae1dSRodney W. Grimes */ 1511d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1512df8bae1dSRodney W. Grimes struct osigvec_args { 1513df8bae1dSRodney W. Grimes int signum; 1514df8bae1dSRodney W. Grimes struct sigvec *nsv; 1515df8bae1dSRodney W. Grimes struct sigvec *osv; 1516df8bae1dSRodney W. Grimes }; 1517d2d3e875SBruce Evans #endif 1518df8bae1dSRodney W. Grimes /* ARGSUSED */ 151926f9a767SRodney W. Grimes int 1520e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap) 1521df8bae1dSRodney W. Grimes { 1522df8bae1dSRodney W. Grimes struct sigvec vec; 15232c42a146SMarcel Moolenaar struct sigaction nsa, osa; 1524e052a8b9SEd Maste struct sigaction *nsap, *osap; 15252c42a146SMarcel Moolenaar int error; 1526df8bae1dSRodney W. Grimes 15276f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 15286f841fb7SMarcel Moolenaar return (EINVAL); 15296f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 15306f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 15312c42a146SMarcel Moolenaar if (nsap) { 15326f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 15332c42a146SMarcel Moolenaar if (error) 1534df8bae1dSRodney W. Grimes return (error); 15352c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 15362c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 15372c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 15382c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1539df8bae1dSRodney W. Grimes } 15405edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 15412c42a146SMarcel Moolenaar if (osap && !error) { 15422c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 15432c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 15442c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 15452c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 15462c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 15476f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 15482c42a146SMarcel Moolenaar } 15492c42a146SMarcel Moolenaar return (error); 1550df8bae1dSRodney W. Grimes } 1551df8bae1dSRodney W. Grimes 1552d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1553df8bae1dSRodney W. Grimes struct osigblock_args { 1554df8bae1dSRodney W. Grimes int mask; 1555df8bae1dSRodney W. Grimes }; 1556d2d3e875SBruce Evans #endif 155726f9a767SRodney W. Grimes int 1558e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap) 1559df8bae1dSRodney W. Grimes { 1560d6e029adSKonstantin Belousov sigset_t set, oset; 1561df8bae1dSRodney W. Grimes 15622c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1563d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1564d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1565df8bae1dSRodney W. Grimes return (0); 1566df8bae1dSRodney W. Grimes } 1567df8bae1dSRodney W. Grimes 1568d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1569df8bae1dSRodney W. Grimes struct osigsetmask_args { 1570df8bae1dSRodney W. Grimes int mask; 1571df8bae1dSRodney W. Grimes }; 1572d2d3e875SBruce Evans #endif 157326f9a767SRodney W. Grimes int 1574e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1575df8bae1dSRodney W. Grimes { 1576d6e029adSKonstantin Belousov sigset_t set, oset; 1577df8bae1dSRodney W. Grimes 15782c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1579d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1580d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1581df8bae1dSRodney W. Grimes return (0); 1582df8bae1dSRodney W. Grimes } 15831930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1584df8bae1dSRodney W. Grimes 1585df8bae1dSRodney W. Grimes /* 1586873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1587873fbcd7SRobert Watson * meantime. 1588df8bae1dSRodney W. Grimes */ 1589d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1590df8bae1dSRodney W. Grimes struct sigsuspend_args { 15912c42a146SMarcel Moolenaar const sigset_t *sigmask; 1592df8bae1dSRodney W. Grimes }; 1593d2d3e875SBruce Evans #endif 1594df8bae1dSRodney W. Grimes /* ARGSUSED */ 159526f9a767SRodney W. Grimes int 1596e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1597df8bae1dSRodney W. Grimes { 15982c42a146SMarcel Moolenaar sigset_t mask; 15992c42a146SMarcel Moolenaar int error; 16002c42a146SMarcel Moolenaar 16016f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 16022c42a146SMarcel Moolenaar if (error) 16032c42a146SMarcel Moolenaar return (error); 16048f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 16058f19eb88SIan Dowse } 16068f19eb88SIan Dowse 16078f19eb88SIan Dowse int 16088f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 16098f19eb88SIan Dowse { 16108f19eb88SIan Dowse struct proc *p = td->td_proc; 161184440afbSKonstantin Belousov int has_sig, sig; 1612df8bae1dSRodney W. Grimes 1613dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1614dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1615dbec10e0SJonathan T. Looney 1616df8bae1dSRodney W. Grimes /* 1617645682fdSLuoqi Chen * When returning from sigsuspend, we want 1618df8bae1dSRodney W. Grimes * the old mask to be restored after the 1619df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1620df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1621df8bae1dSRodney W. Grimes * to indicate this. 1622df8bae1dSRodney W. Grimes */ 1623628d2653SJohn Baldwin PROC_LOCK(p); 162484440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 162584440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 16265e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 1627c6d31b83SKonstantin Belousov ast_sched(td, TDA_SIGSUSPEND); 162884440afbSKonstantin Belousov 162984440afbSKonstantin Belousov /* 163084440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 163184440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 163284440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 163384440afbSKonstantin Belousov * delivery. 163484440afbSKonstantin Belousov */ 1635afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 163684440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 163784440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 163884440afbSKonstantin Belousov 0) == 0) 16392c42a146SMarcel Moolenaar /* void */; 164084440afbSKonstantin Belousov thread_suspend_check(0); 164184440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 164246e47c4fSKonstantin Belousov while ((sig = cursig(td)) != 0) { 164346e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 164475c586a4SKonstantin Belousov has_sig += postsig(sig); 164546e47c4fSKonstantin Belousov } 164684440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 1647ef401a85SKonstantin Belousov 1648ef401a85SKonstantin Belousov /* 1649ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1650ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1651ef401a85SKonstantin Belousov * EINTR. 1652ef401a85SKonstantin Belousov */ 1653ef401a85SKonstantin Belousov if ((p->p_ptevents & PTRACE_SYSCALL) != 0) 1654ef401a85SKonstantin Belousov has_sig += 1; 165584440afbSKonstantin Belousov } 1656628d2653SJohn Baldwin PROC_UNLOCK(p); 16572dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 16582dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 165975c586a4SKonstantin Belousov return (EJUSTRETURN); 16602c42a146SMarcel Moolenaar } 16612c42a146SMarcel Moolenaar 166231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 166397563428SAlexander Kabaev /* 166497563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 166597563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 166697563428SAlexander Kabaev */ 16672c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 16682c42a146SMarcel Moolenaar struct osigsuspend_args { 16692c42a146SMarcel Moolenaar osigset_t mask; 16702c42a146SMarcel Moolenaar }; 16712c42a146SMarcel Moolenaar #endif 16722c42a146SMarcel Moolenaar /* ARGSUSED */ 16732c42a146SMarcel Moolenaar int 1674e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap) 16752c42a146SMarcel Moolenaar { 1676645682fdSLuoqi Chen sigset_t mask; 16772c42a146SMarcel Moolenaar 1678645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 167984440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1680df8bae1dSRodney W. Grimes } 168131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1682df8bae1dSRodney W. Grimes 16831930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1684d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1685df8bae1dSRodney W. Grimes struct osigstack_args { 1686df8bae1dSRodney W. Grimes struct sigstack *nss; 1687df8bae1dSRodney W. Grimes struct sigstack *oss; 1688df8bae1dSRodney W. Grimes }; 1689d2d3e875SBruce Evans #endif 1690df8bae1dSRodney W. Grimes /* ARGSUSED */ 169126f9a767SRodney W. Grimes int 1692e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap) 1693df8bae1dSRodney W. Grimes { 16945afe0c99SJohn Baldwin struct sigstack nss, oss; 1695fb99ab88SMatthew Dillon int error = 0; 1696fb99ab88SMatthew Dillon 1697d034d459SMarcel Moolenaar if (uap->nss != NULL) { 16985afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 16995afe0c99SJohn Baldwin if (error) 17005afe0c99SJohn Baldwin return (error); 1701df8bae1dSRodney W. Grimes } 1702a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 17035afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 17045afe0c99SJohn Baldwin if (uap->nss != NULL) { 1705a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1706a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1707a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1708a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 17095afe0c99SJohn Baldwin } 17105afe0c99SJohn Baldwin if (uap->oss != NULL) 17115afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 17125afe0c99SJohn Baldwin 1713fb99ab88SMatthew Dillon return (error); 1714df8bae1dSRodney W. Grimes } 17151930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1716df8bae1dSRodney W. Grimes 1717d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1718df8bae1dSRodney W. Grimes struct sigaltstack_args { 17192c42a146SMarcel Moolenaar stack_t *ss; 17202c42a146SMarcel Moolenaar stack_t *oss; 1721df8bae1dSRodney W. Grimes }; 1722d2d3e875SBruce Evans #endif 1723df8bae1dSRodney W. Grimes /* ARGSUSED */ 172426f9a767SRodney W. Grimes int 1725e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1726df8bae1dSRodney W. Grimes { 17278f19eb88SIan Dowse stack_t ss, oss; 17288f19eb88SIan Dowse int error; 17298f19eb88SIan Dowse 17308f19eb88SIan Dowse if (uap->ss != NULL) { 17318f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 17328f19eb88SIan Dowse if (error) 17338f19eb88SIan Dowse return (error); 17348f19eb88SIan Dowse } 17358f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 17368f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 17378f19eb88SIan Dowse if (error) 17388f19eb88SIan Dowse return (error); 17398f19eb88SIan Dowse if (uap->oss != NULL) 17408f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 17418f19eb88SIan Dowse return (error); 17428f19eb88SIan Dowse } 17438f19eb88SIan Dowse 17448f19eb88SIan Dowse int 17458f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 17468f19eb88SIan Dowse { 1747b40ce416SJulian Elischer struct proc *p = td->td_proc; 1748fb99ab88SMatthew Dillon int oonstack; 1749fb99ab88SMatthew Dillon 1750b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1751d034d459SMarcel Moolenaar 17528f19eb88SIan Dowse if (oss != NULL) { 1753a30ec4b9SDavid Xu *oss = td->td_sigstk; 1754a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1755d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1756df8bae1dSRodney W. Grimes } 1757d034d459SMarcel Moolenaar 17588f19eb88SIan Dowse if (ss != NULL) { 1759a30ec4b9SDavid Xu if (oonstack) 1760cf60731bSJohn Baldwin return (EPERM); 1761a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1762cf60731bSJohn Baldwin return (EINVAL); 17638f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 17649104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1765cf60731bSJohn Baldwin return (ENOMEM); 17669104847fSDavid Xu 1767a30ec4b9SDavid Xu td->td_sigstk = *ss; 1768a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1769628d2653SJohn Baldwin } else { 1770a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1771628d2653SJohn Baldwin } 1772d034d459SMarcel Moolenaar } 1773cf60731bSJohn Baldwin return (0); 1774df8bae1dSRodney W. Grimes } 1775df8bae1dSRodney W. Grimes 17760cc9fb75SKonstantin Belousov struct killpg1_ctx { 17770cc9fb75SKonstantin Belousov struct thread *td; 17780cc9fb75SKonstantin Belousov ksiginfo_t *ksi; 17790cc9fb75SKonstantin Belousov int sig; 17800cc9fb75SKonstantin Belousov bool sent; 17810cc9fb75SKonstantin Belousov bool found; 17820cc9fb75SKonstantin Belousov int ret; 17830cc9fb75SKonstantin Belousov }; 17840cc9fb75SKonstantin Belousov 17850cc9fb75SKonstantin Belousov static void 178669413598SMateusz Guzik killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg) 17870cc9fb75SKonstantin Belousov { 17880cc9fb75SKonstantin Belousov int err; 17890cc9fb75SKonstantin Belousov 17900cc9fb75SKonstantin Belousov err = p_cansignal(arg->td, p, arg->sig); 17910cc9fb75SKonstantin Belousov if (err == 0 && arg->sig != 0) 17920cc9fb75SKonstantin Belousov pksignal(p, arg->sig, arg->ksi); 17930cc9fb75SKonstantin Belousov if (err != ESRCH) 17940cc9fb75SKonstantin Belousov arg->found = true; 17950cc9fb75SKonstantin Belousov if (err == 0) 17960cc9fb75SKonstantin Belousov arg->sent = true; 17970cc9fb75SKonstantin Belousov else if (arg->ret == 0 && err != ESRCH && err != EPERM) 17980cc9fb75SKonstantin Belousov arg->ret = err; 17990cc9fb75SKonstantin Belousov } 18000cc9fb75SKonstantin Belousov 180169413598SMateusz Guzik static void 180269413598SMateusz Guzik killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg) 180369413598SMateusz Guzik { 180469413598SMateusz Guzik 180569413598SMateusz Guzik if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 180669413598SMateusz Guzik (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW) 180769413598SMateusz Guzik return; 180869413598SMateusz Guzik 180969413598SMateusz Guzik PROC_LOCK(p); 181069413598SMateusz Guzik killpg1_sendsig_locked(p, arg); 181169413598SMateusz Guzik PROC_UNLOCK(p); 181269413598SMateusz Guzik } 181369413598SMateusz Guzik 181469413598SMateusz Guzik static void 181569413598SMateusz Guzik kill_processes_prison_cb(struct proc *p, void *arg) 181669413598SMateusz Guzik { 181769413598SMateusz Guzik struct killpg1_ctx *ctx = arg; 181869413598SMateusz Guzik 181969413598SMateusz Guzik if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 182069413598SMateusz Guzik (p == ctx->td->td_proc) || p->p_state == PRS_NEW) 182169413598SMateusz Guzik return; 182269413598SMateusz Guzik 182369413598SMateusz Guzik killpg1_sendsig_locked(p, ctx); 182469413598SMateusz Guzik } 182569413598SMateusz Guzik 1826d93f860cSPoul-Henning Kamp /* 1827d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1828d93f860cSPoul-Henning Kamp * cp is calling process. 1829d93f860cSPoul-Henning Kamp */ 183037c84183SPoul-Henning Kamp static int 1831a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1832d93f860cSPoul-Henning Kamp { 1833a3de221dSKonstantin Belousov struct proc *p; 1834d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 18350cc9fb75SKonstantin Belousov struct killpg1_ctx arg; 1836d93f860cSPoul-Henning Kamp 18370cc9fb75SKonstantin Belousov arg.td = td; 18380cc9fb75SKonstantin Belousov arg.ksi = ksi; 18390cc9fb75SKonstantin Belousov arg.sig = sig; 18400cc9fb75SKonstantin Belousov arg.sent = false; 18410cc9fb75SKonstantin Belousov arg.found = false; 18420cc9fb75SKonstantin Belousov arg.ret = 0; 1843553629ebSJake Burkholder if (all) { 1844d93f860cSPoul-Henning Kamp /* 1845d93f860cSPoul-Henning Kamp * broadcast 1846d93f860cSPoul-Henning Kamp */ 184769413598SMateusz Guzik prison_proc_iterate(td->td_ucred->cr_prison, 184869413598SMateusz Guzik kill_processes_prison_cb, &arg); 1849553629ebSJake Burkholder } else { 1850ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1851f591779bSSeigo Tanimura if (pgid == 0) { 1852d93f860cSPoul-Henning Kamp /* 1853d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1854d93f860cSPoul-Henning Kamp */ 18559c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1856f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1857f591779bSSeigo Tanimura } else { 1858d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1859f591779bSSeigo Tanimura if (pgrp == NULL) { 1860ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1861d93f860cSPoul-Henning Kamp return (ESRCH); 1862d93f860cSPoul-Henning Kamp } 1863f591779bSSeigo Tanimura } 1864ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 18652e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 18660cc9fb75SKonstantin Belousov killpg1_sendsig(p, false, &arg); 1867d93f860cSPoul-Henning Kamp } 1868f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1869d93f860cSPoul-Henning Kamp } 18700cc9fb75SKonstantin Belousov MPASS(arg.ret != 0 || arg.found || !arg.sent); 18710cc9fb75SKonstantin Belousov if (arg.ret == 0 && !arg.sent) 18720cc9fb75SKonstantin Belousov arg.ret = arg.found ? EPERM : ESRCH; 18730cc9fb75SKonstantin Belousov return (arg.ret); 1874d93f860cSPoul-Henning Kamp } 1875d93f860cSPoul-Henning Kamp 1876d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1877df8bae1dSRodney W. Grimes struct kill_args { 1878df8bae1dSRodney W. Grimes int pid; 1879df8bae1dSRodney W. Grimes int signum; 1880df8bae1dSRodney W. Grimes }; 1881d2d3e875SBruce Evans #endif 1882df8bae1dSRodney W. Grimes /* ARGSUSED */ 188326f9a767SRodney W. Grimes int 18848451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1885df8bae1dSRodney W. Grimes { 188634ad5ac2SEdward Tomasz Napierala 188734ad5ac2SEdward Tomasz Napierala return (kern_kill(td, uap->pid, uap->signum)); 188834ad5ac2SEdward Tomasz Napierala } 188934ad5ac2SEdward Tomasz Napierala 189034ad5ac2SEdward Tomasz Napierala int 189134ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum) 189234ad5ac2SEdward Tomasz Napierala { 1893a3de221dSKonstantin Belousov ksiginfo_t ksi; 1894a3de221dSKonstantin Belousov struct proc *p; 189590af4afaSJohn Baldwin int error; 1896df8bae1dSRodney W. Grimes 18978890f5d0SPawel Jakub Dawidek /* 18988890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 18998890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 19008890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 19018890f5d0SPawel Jakub Dawidek */ 190234ad5ac2SEdward Tomasz Napierala if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid) 19038890f5d0SPawel Jakub Dawidek return (ECAPMODE); 19048890f5d0SPawel Jakub Dawidek 190534ad5ac2SEdward Tomasz Napierala AUDIT_ARG_SIGNUM(signum); 190634ad5ac2SEdward Tomasz Napierala AUDIT_ARG_PID(pid); 190734ad5ac2SEdward Tomasz Napierala if ((u_int)signum > _SIG_MAXSIG) 1908df8bae1dSRodney W. Grimes return (EINVAL); 1909fb99ab88SMatthew Dillon 1910a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 191134ad5ac2SEdward Tomasz Napierala ksi.ksi_signo = signum; 1912a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1913a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1914a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1915a3de221dSKonstantin Belousov 191634ad5ac2SEdward Tomasz Napierala if (pid > 0) { 1917df8bae1dSRodney W. Grimes /* kill single process */ 191834ad5ac2SEdward Tomasz Napierala if ((p = pfind_any(pid)) == NULL) 191990af4afaSJohn Baldwin return (ESRCH); 192014961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 192134ad5ac2SEdward Tomasz Napierala error = p_cansignal(td, p, signum); 192234ad5ac2SEdward Tomasz Napierala if (error == 0 && signum) 192334ad5ac2SEdward Tomasz Napierala pksignal(p, signum, &ksi); 1924628d2653SJohn Baldwin PROC_UNLOCK(p); 192590af4afaSJohn Baldwin return (error); 1926df8bae1dSRodney W. Grimes } 192734ad5ac2SEdward Tomasz Napierala switch (pid) { 1928df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 192934ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 1, &ksi)); 1930df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 193134ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 0, &ksi)); 1932df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 193334ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, -pid, 0, &ksi)); 1934df8bae1dSRodney W. Grimes } 193590af4afaSJohn Baldwin /* NOTREACHED */ 1936df8bae1dSRodney W. Grimes } 1937df8bae1dSRodney W. Grimes 1938cfb5f768SJonathan Anderson int 1939e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap) 1940cfb5f768SJonathan Anderson { 1941cfb5f768SJonathan Anderson struct proc *p; 1942cfb5f768SJonathan Anderson int error; 1943cfb5f768SJonathan Anderson 1944cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1945cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1946cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1947cfb5f768SJonathan Anderson return (EINVAL); 1948cfb5f768SJonathan Anderson 1949cbd92ce6SMatt Macy error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p); 1950cfb5f768SJonathan Anderson if (error) 1951cfb5f768SJonathan Anderson return (error); 1952cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1953cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1954cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 19558451d0ddSKip Macy kern_psignal(p, uap->signum); 1956cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1957cfb5f768SJonathan Anderson return (error); 1958cfb5f768SJonathan Anderson } 1959cfb5f768SJonathan Anderson 19601930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1961d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1962df8bae1dSRodney W. Grimes struct okillpg_args { 1963df8bae1dSRodney W. Grimes int pgid; 1964df8bae1dSRodney W. Grimes int signum; 1965df8bae1dSRodney W. Grimes }; 1966d2d3e875SBruce Evans #endif 1967df8bae1dSRodney W. Grimes /* ARGSUSED */ 196826f9a767SRodney W. Grimes int 1969a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1970df8bae1dSRodney W. Grimes { 1971a3de221dSKonstantin Belousov ksiginfo_t ksi; 1972df8bae1dSRodney W. Grimes 197314961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 197414961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 19756c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1976df8bae1dSRodney W. Grimes return (EINVAL); 19779104847fSDavid Xu 1978a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1979a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1980a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1981a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1982a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1983a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1984df8bae1dSRodney W. Grimes } 19851930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1986df8bae1dSRodney W. Grimes 19879104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 19889104847fSDavid Xu struct sigqueue_args { 19899104847fSDavid Xu pid_t pid; 19909104847fSDavid Xu int signum; 19919104847fSDavid Xu /* union sigval */ void *value; 19929104847fSDavid Xu }; 19939104847fSDavid Xu #endif 19949104847fSDavid Xu int 19958451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 19969104847fSDavid Xu { 1997f19351aaSBrooks Davis union sigval sv; 1998f19351aaSBrooks Davis 1999f19351aaSBrooks Davis sv.sival_ptr = uap->value; 2000f19351aaSBrooks Davis 2001f19351aaSBrooks Davis return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 2002f19351aaSBrooks Davis } 2003f19351aaSBrooks Davis 2004f19351aaSBrooks Davis int 2005f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value) 2006f19351aaSBrooks Davis { 20079104847fSDavid Xu ksiginfo_t ksi; 20089104847fSDavid Xu struct proc *p; 20099104847fSDavid Xu int error; 20109104847fSDavid Xu 2011f19351aaSBrooks Davis if ((u_int)signum > _SIG_MAXSIG) 20129104847fSDavid Xu return (EINVAL); 20139104847fSDavid Xu 20149104847fSDavid Xu /* 20159104847fSDavid Xu * Specification says sigqueue can only send signal to 20169104847fSDavid Xu * single process. 20179104847fSDavid Xu */ 2018f19351aaSBrooks Davis if (pid <= 0) 20199104847fSDavid Xu return (EINVAL); 20209104847fSDavid Xu 2021537d0fb1SMateusz Guzik if ((p = pfind_any(pid)) == NULL) 20229104847fSDavid Xu return (ESRCH); 2023f19351aaSBrooks Davis error = p_cansignal(td, p, signum); 2024f19351aaSBrooks Davis if (error == 0 && signum != 0) { 20259104847fSDavid Xu ksiginfo_init(&ksi); 2026a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 2027f19351aaSBrooks Davis ksi.ksi_signo = signum; 20289104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 20299104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 20309104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 2031f19351aaSBrooks Davis ksi.ksi_value = *value; 2032ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 20339104847fSDavid Xu } 20349104847fSDavid Xu PROC_UNLOCK(p); 20359104847fSDavid Xu return (error); 20369104847fSDavid Xu } 20379104847fSDavid Xu 2038df8bae1dSRodney W. Grimes /* 2039df8bae1dSRodney W. Grimes * Send a signal to a process group. 2040df8bae1dSRodney W. Grimes */ 2041df8bae1dSRodney W. Grimes void 2042a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 2043df8bae1dSRodney W. Grimes { 2044df8bae1dSRodney W. Grimes struct pgrp *pgrp; 2045df8bae1dSRodney W. Grimes 2046f591779bSSeigo Tanimura if (pgid != 0) { 2047ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 2048f591779bSSeigo Tanimura pgrp = pgfind(pgid); 2049ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 2050f591779bSSeigo Tanimura if (pgrp != NULL) { 2051a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 2052f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 2053f591779bSSeigo Tanimura } 2054f591779bSSeigo Tanimura } 2055df8bae1dSRodney W. Grimes } 2056df8bae1dSRodney W. Grimes 2057df8bae1dSRodney W. Grimes /* 2058df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 2059df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 2060df8bae1dSRodney W. Grimes */ 2061df8bae1dSRodney W. Grimes void 2062a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 2063df8bae1dSRodney W. Grimes { 2064a3de221dSKonstantin Belousov struct proc *p; 2065df8bae1dSRodney W. Grimes 2066628d2653SJohn Baldwin if (pgrp) { 2067f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 2068628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 2069628d2653SJohn Baldwin PROC_LOCK(p); 2070e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 2071e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 2072a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 2073628d2653SJohn Baldwin PROC_UNLOCK(p); 2074628d2653SJohn Baldwin } 2075628d2653SJohn Baldwin } 2076df8bae1dSRodney W. Grimes } 2077df8bae1dSRodney W. Grimes 2078e442f29fSKonstantin Belousov /* 2079e442f29fSKonstantin Belousov * Recalculate the signal mask and reset the signal disposition after 2080e442f29fSKonstantin Belousov * usermode frame for delivery is formed. Should be called after 2081e442f29fSKonstantin Belousov * mach-specific routine, because sysent->sv_sendsig() needs correct 2082e442f29fSKonstantin Belousov * ps_siginfo and signal mask. 2083e442f29fSKonstantin Belousov */ 2084e442f29fSKonstantin Belousov static void 2085e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps) 2086e442f29fSKonstantin Belousov { 2087e442f29fSKonstantin Belousov sigset_t mask; 2088e442f29fSKonstantin Belousov 2089e442f29fSKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 2090e442f29fSKonstantin Belousov td->td_ru.ru_nsignals++; 2091e442f29fSKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2092e442f29fSKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2093e442f29fSKonstantin Belousov SIGADDSET(mask, sig); 2094e442f29fSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 2095e442f29fSKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 2096e442f29fSKonstantin Belousov if (SIGISMEMBER(ps->ps_sigreset, sig)) 2097e442f29fSKonstantin Belousov sigdflt(ps, sig); 2098e442f29fSKonstantin Belousov } 2099e442f29fSKonstantin Belousov 2100df8bae1dSRodney W. Grimes /* 21010c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 21020c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 21030c14ff0eSRobert Watson * normally. 2104df8bae1dSRodney W. Grimes */ 2105df8bae1dSRodney W. Grimes void 21069104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 2107df8bae1dSRodney W. Grimes { 21081bf4700bSJeff Roberson struct sigacts *ps; 21091bf4700bSJeff Roberson struct proc *p; 2110146fc63fSKonstantin Belousov sigset_t sigmask; 2111c5c981d4SMateusz Guzik int sig; 21121bf4700bSJeff Roberson 21131bf4700bSJeff Roberson p = td->td_proc; 21149104847fSDavid Xu sig = ksi->ksi_signo; 21159104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 21169104847fSDavid Xu 2117a113b17fSKonstantin Belousov sigfastblock_fetch(td); 2118628d2653SJohn Baldwin PROC_LOCK(p); 2119ef3dab76STim J. Robbins ps = p->p_sigacts; 212090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2121146fc63fSKonstantin Belousov sigmask = td->td_sigmask; 2122146fc63fSKonstantin Belousov if (td->td_sigblock_val != 0) 2123146fc63fSKonstantin Belousov SIGSETOR(sigmask, fastblock_mask); 212490af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 2125146fc63fSKonstantin Belousov !SIGISMEMBER(sigmask, sig)) { 2126df8bae1dSRodney W. Grimes #ifdef KTRACE 2127374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 2128374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 2129c5c981d4SMateusz Guzik &td->td_sigmask, ksi->ksi_code); 2130df8bae1dSRodney W. Grimes #endif 21319104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 21329104847fSDavid Xu ksi, &td->td_sigmask); 2133e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 213490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 21356f841fb7SMarcel Moolenaar } else { 2136f71a882fSDavid Xu /* 2137f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 2138f71a882fSDavid Xu * masking the signal or process is ignoring the 2139f71a882fSDavid Xu * signal. 2140f71a882fSDavid Xu */ 2141146fc63fSKonstantin Belousov if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) || 2142f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 2143f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 2144f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 2145f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 2146f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2147146fc63fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTBLOCK; 2148146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 2149f71a882fSDavid Xu } 215090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 21512c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 2152ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 2153df8bae1dSRodney W. Grimes } 2154628d2653SJohn Baldwin PROC_UNLOCK(p); 2155df8bae1dSRodney W. Grimes } 2156df8bae1dSRodney W. Grimes 21574093529dSJeff Roberson static struct thread * 2158146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock) 21594093529dSJeff Roberson { 21603074d1b4SDavid Xu struct thread *td, *signal_td; 21614093529dSJeff Roberson 21624093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2163146fc63fSKonstantin Belousov MPASS(!fast_sigblock || p == curproc); 21644093529dSJeff Roberson 21654093529dSJeff Roberson /* 2166627451c1SDavid Xu * Check if current thread can handle the signal without 216715b7a831SKonstantin Belousov * switching context to another thread. 21684093529dSJeff Roberson */ 2169146fc63fSKonstantin Belousov if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) && 2170146fc63fSKonstantin Belousov (!fast_sigblock || curthread->td_sigblock_val == 0)) 2171627451c1SDavid Xu return (curthread); 21723074d1b4SDavid Xu signal_td = NULL; 21733074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 2174146fc63fSKonstantin Belousov if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock || 2175146fc63fSKonstantin Belousov td != curthread || td->td_sigblock_val == 0)) { 21763074d1b4SDavid Xu signal_td = td; 2177627451c1SDavid Xu break; 21783074d1b4SDavid Xu } 21793074d1b4SDavid Xu } 21803074d1b4SDavid Xu if (signal_td == NULL) 21813074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 21823074d1b4SDavid Xu return (signal_td); 21834093529dSJeff Roberson } 21844093529dSJeff Roberson 2185df8bae1dSRodney W. Grimes /* 2186df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 2187df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 2188df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 2189df8bae1dSRodney W. Grimes * 2190df8bae1dSRodney W. Grimes * Exceptions: 2191df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 2192df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 2193df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 2194df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 2195df8bae1dSRodney W. Grimes * 2196df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 21974dec0e67SRobert Watson * 21984dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 21994dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 22004dec0e67SRobert Watson * side effects of this unwise possibility. 2201df8bae1dSRodney W. Grimes */ 2202df8bae1dSRodney W. Grimes void 22038451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 2204df8bae1dSRodney W. Grimes { 2205a3de221dSKonstantin Belousov ksiginfo_t ksi; 2206a3de221dSKonstantin Belousov 2207a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 2208a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 2209a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 2210ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 2211a3de221dSKonstantin Belousov } 2212a3de221dSKonstantin Belousov 2213ad6eec7bSJohn Baldwin int 2214a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2215a3de221dSKonstantin Belousov { 2216a3de221dSKonstantin Belousov 2217ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 22189104847fSDavid Xu } 22199104847fSDavid Xu 2220cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 22219104847fSDavid Xu int 2222cf7d9a8cSDavid Xu sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd) 22239104847fSDavid Xu { 2224cf7d9a8cSDavid Xu struct thread *td; 222541b3077aSJacques Vidrine 22266d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2227cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 22286d7b314bSDavid Xu if (td == NULL) 22296d7b314bSDavid Xu return (ESRCH); 2230cf7d9a8cSDavid Xu *ttd = td; 2231cf7d9a8cSDavid Xu } else { 2232cf7d9a8cSDavid Xu *ttd = NULL; 2233cf7d9a8cSDavid Xu PROC_LOCK(p); 22346d7b314bSDavid Xu } 2235cf7d9a8cSDavid Xu return (0); 22364093529dSJeff Roberson } 22374093529dSJeff Roberson 2238ad6eec7bSJohn Baldwin void 2239ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2240ad6eec7bSJohn Baldwin { 2241ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2242ad6eec7bSJohn Baldwin 2243ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2244ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2245ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2246ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2247ad6eec7bSJohn Baldwin } 2248ad6eec7bSJohn Baldwin 2249ad6eec7bSJohn Baldwin void 2250ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2251ad6eec7bSJohn Baldwin { 2252ad6eec7bSJohn Baldwin 2253ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2254ad6eec7bSJohn Baldwin } 2255ad6eec7bSJohn Baldwin 22569b86d3e5SKonstantin Belousov static int 22579b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval) 22589b86d3e5SKonstantin Belousov { 22599b86d3e5SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_OWNED); 22609b86d3e5SKonstantin Belousov 22619b86d3e5SKonstantin Belousov if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) { 22629b86d3e5SKonstantin Belousov thread_unlock(td); 22639b86d3e5SKonstantin Belousov return (0); 22649b86d3e5SKonstantin Belousov } 22659b86d3e5SKonstantin Belousov return (sleepq_abort(td, intrval)); 22669b86d3e5SKonstantin Belousov } 22679b86d3e5SKonstantin Belousov 2268cf7d9a8cSDavid Xu int 2269ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 22704093529dSJeff Roberson { 22719104847fSDavid Xu sig_t action; 22729104847fSDavid Xu sigqueue_t *sigqueue; 22739104847fSDavid Xu int prop; 227490af4afaSJohn Baldwin struct sigacts *ps; 227594f0972bSDavid Xu int intrval; 22769104847fSDavid Xu int ret = 0; 2277da7bbd2cSJohn Baldwin int wakeup_swapper; 2278df8bae1dSRodney W. Grimes 2279cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 22806d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 22816d7b314bSDavid Xu 228241b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2283212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 22844093529dSJeff Roberson 2285212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 22866d7b314bSDavid Xu 22876d7b314bSDavid Xu /* 22886d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 22896d7b314bSDavid Xu */ 22906d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 22910a4f2ac3SKonstantin Belousov if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 22926d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22936d7b314bSDavid Xu return (ret); 22946d7b314bSDavid Xu } 22956d7b314bSDavid Xu 229690af4afaSJohn Baldwin ps = p->p_sigacts; 22979e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 22982c42a146SMarcel Moolenaar prop = sigprop(sig); 22994093529dSJeff Roberson 23006d7b314bSDavid Xu if (td == NULL) { 2301146fc63fSKonstantin Belousov td = sigtd(p, sig, false); 23026d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 2303462314b3SMateusz Guzik } else 23049104847fSDavid Xu sigqueue = &td->td_sigqueue; 23054093529dSJeff Roberson 230636160958SMark Johnston SDT_PROBE3(proc, , , signal__send, td, p, sig); 23075d217f17SJohn Birrell 2308df8bae1dSRodney W. Grimes /* 2309bc387624SKonstantin Belousov * If the signal is being ignored, then we forget about it 2310bc387624SKonstantin Belousov * immediately, except when the target process executes 2311bc387624SKonstantin Belousov * sigwait(). (Note: we don't set SIGCONT in ps_sigignore, 2312bc387624SKonstantin Belousov * and if it is set to SIG_IGN, action will be SIG_DFL here.) 2313df8bae1dSRodney W. Grimes */ 231490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 23150fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2316bc387624SKonstantin Belousov if (kern_sig_discard_ign && 2317bc387624SKonstantin Belousov (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) { 231836160958SMark Johnston SDT_PROBE3(proc, , , signal__discard, td, p, sig); 23195d217f17SJohn Birrell 232090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 23210a4f2ac3SKonstantin Belousov if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 23226d7b314bSDavid Xu ksiginfo_tryfree(ksi); 23239104847fSDavid Xu return (ret); 2324bc387624SKonstantin Belousov } else { 2325bc387624SKonstantin Belousov action = SIG_CATCH; 2326f17eb93dSKonstantin Belousov intrval = 0; 232790af4afaSJohn Baldwin } 2328f17eb93dSKonstantin Belousov } else { 2329f17eb93dSKonstantin Belousov if (SIGISMEMBER(td->td_sigmask, sig)) 2330df8bae1dSRodney W. Grimes action = SIG_HOLD; 233190af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2332df8bae1dSRodney W. Grimes action = SIG_CATCH; 2333df8bae1dSRodney W. Grimes else 2334df8bae1dSRodney W. Grimes action = SIG_DFL; 233594f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 233694f0972bSDavid Xu intrval = EINTR; 233794f0972bSDavid Xu else 233894f0972bSDavid Xu intrval = ERESTART; 2339f17eb93dSKonstantin Belousov } 234090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2341df8bae1dSRodney W. Grimes 2342fd50a707SBrooks Davis if (prop & SIGPROP_CONT) 23439104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 2344fd50a707SBrooks Davis else if (prop & SIGPROP_STOP) { 2345df8bae1dSRodney W. Grimes /* 2346df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2347df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2348df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2349df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2350df8bae1dSRodney W. Grimes */ 2351993a1699SKonstantin Belousov if ((prop & SIGPROP_TTYSTOP) != 0 && 23525844bd05SKonstantin Belousov (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 && 2353993a1699SKonstantin Belousov action == SIG_DFL) { 23540a4f2ac3SKonstantin Belousov if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 23556d7b314bSDavid Xu ksiginfo_tryfree(ksi); 23569104847fSDavid Xu return (ret); 23576d7b314bSDavid Xu } 23589104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2359ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 23606933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2361ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2362ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2363ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2364ebceaf6dSDavid Xu } 2365df8bae1dSRodney W. Grimes } 23663074d1b4SDavid Xu 23679104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 23689104847fSDavid Xu if (ret != 0) 23699104847fSDavid Xu return (ret); 23706d7b314bSDavid Xu signotify(td); 23715312b1c7SDavid Xu /* 23725312b1c7SDavid Xu * Defer further processing for signals which are held, 23735312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 23745312b1c7SDavid Xu */ 23755312b1c7SDavid Xu if (action == SIG_HOLD && 2376fd50a707SBrooks Davis !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 23779104847fSDavid Xu return (ret); 2378b4d33259SEric Badger 237961a74c5cSJeff Roberson wakeup_swapper = 0; 238061a74c5cSJeff Roberson 238190d75f78SAlfred Perlstein /* 2382e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2383e602ba25SJulian Elischer * component. Most processing occurs when the process next 2384e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 23851968f37bSJohn Baldwin * times when processing needs to be done immediately, such as 2386e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 23871968f37bSJohn Baldwin * We try to do the per-process part here. 2388df8bae1dSRodney W. Grimes */ 2389e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 23900f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 23910f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2392e602ba25SJulian Elischer if (sig == SIGKILL) { 2393137cf33dSDavid Xu /* 2394137cf33dSDavid Xu * If traced process is already stopped, 2395137cf33dSDavid Xu * then no further action is necessary. 2396137cf33dSDavid Xu */ 239783b718ebSDavid Xu if (p->p_flag & P_TRACED) 239883b718ebSDavid Xu goto out; 2399df8bae1dSRodney W. Grimes /* 2400e602ba25SJulian Elischer * SIGKILL sets process running. 2401e602ba25SJulian Elischer * It will die elsewhere. 2402e602ba25SJulian Elischer * All threads must be restarted. 2403df8bae1dSRodney W. Grimes */ 2404482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2405e602ba25SJulian Elischer goto runfast; 2406e602ba25SJulian Elischer } 2407e602ba25SJulian Elischer 2408fd50a707SBrooks Davis if (prop & SIGPROP_CONT) { 2409137cf33dSDavid Xu /* 2410137cf33dSDavid Xu * If traced process is already stopped, 2411137cf33dSDavid Xu * then no further action is necessary. 2412137cf33dSDavid Xu */ 241383b718ebSDavid Xu if (p->p_flag & P_TRACED) 241483b718ebSDavid Xu goto out; 2415e602ba25SJulian Elischer /* 2416e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 24179104847fSDavid Xu * process but don't leave the signal in sigqueue as 24181d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2419e602ba25SJulian Elischer * continue the process and leave the signal in 24209104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2421e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2422e602ba25SJulian Elischer * an event, it goes back to run state. 2423e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2424e602ba25SJulian Elischer */ 24251279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 24267b4a950aSDavid Xu PROC_SLOCK(p); 2427ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 24287b4a950aSDavid Xu PROC_SUNLOCK(p); 24296933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 2430b4490c6eSKonstantin Belousov p->p_xsig = SIGCONT; 24317f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2432ebceaf6dSDavid Xu childproc_continued(p); 24337f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 24347b4a950aSDavid Xu PROC_SLOCK(p); 2435ebceaf6dSDavid Xu } 2436e602ba25SJulian Elischer if (action == SIG_DFL) { 2437a54e85fdSJeff Roberson thread_unsuspend(p); 24387b4a950aSDavid Xu PROC_SUNLOCK(p); 24399104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2440dc47fdf1SKonstantin Belousov goto out_cont; 2441a54e85fdSJeff Roberson } 2442a54e85fdSJeff Roberson if (action == SIG_CATCH) { 24438460a577SJohn Birrell /* 24448460a577SJohn Birrell * The process wants to catch it so it needs 24458460a577SJohn Birrell * to run at least one thread, but which one? 24468460a577SJohn Birrell */ 24477b4a950aSDavid Xu PROC_SUNLOCK(p); 2448e602ba25SJulian Elischer goto runfast; 2449e602ba25SJulian Elischer } 2450e602ba25SJulian Elischer /* 2451e602ba25SJulian Elischer * The signal is not ignored or caught. 2452e602ba25SJulian Elischer */ 245304774f23SJulian Elischer thread_unsuspend(p); 24547b4a950aSDavid Xu PROC_SUNLOCK(p); 2455dc47fdf1SKonstantin Belousov goto out_cont; 2456e602ba25SJulian Elischer } 2457e602ba25SJulian Elischer 2458fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2459137cf33dSDavid Xu /* 2460137cf33dSDavid Xu * If traced process is already stopped, 2461137cf33dSDavid Xu * then no further action is necessary. 2462137cf33dSDavid Xu */ 246383b718ebSDavid Xu if (p->p_flag & P_TRACED) 246483b718ebSDavid Xu goto out; 2465e602ba25SJulian Elischer /* 2466e602ba25SJulian Elischer * Already stopped, don't need to stop again 2467e602ba25SJulian Elischer * (If we did the shell could get confused). 246804774f23SJulian Elischer * Just make sure the signal STOP bit set. 2469e602ba25SJulian Elischer */ 24701279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 24719104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2472e602ba25SJulian Elischer goto out; 2473e602ba25SJulian Elischer } 2474e602ba25SJulian Elischer 2475e602ba25SJulian Elischer /* 2476e602ba25SJulian Elischer * All other kinds of signals: 2477e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2478e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2479e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 248004774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2481e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2482e602ba25SJulian Elischer */ 24837b4a950aSDavid Xu PROC_SLOCK(p); 2484a54e85fdSJeff Roberson thread_lock(td); 248561a74c5cSJeff Roberson if (TD_CAN_ABORT(td)) 24869b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 248761a74c5cSJeff Roberson else 2488a54e85fdSJeff Roberson thread_unlock(td); 24897b4a950aSDavid Xu PROC_SUNLOCK(p); 2490df8bae1dSRodney W. Grimes goto out; 2491df8bae1dSRodney W. Grimes /* 24929a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 24939a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2494df8bae1dSRodney W. Grimes */ 2495e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2496ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 249794f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2498df8bae1dSRodney W. Grimes goto out; 249904774f23SJulian Elischer } 2500ec8297bdSDavid Xu 2501ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2502ec8297bdSDavid Xu 2503fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 25040f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 250535c32a76SDavid Xu goto out; 2506e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2507b4490c6eSKonstantin Belousov p->p_xsig = sig; 25087b4a950aSDavid Xu PROC_SLOCK(p); 2509c53fec76SKonstantin Belousov wakeup_swapper = sig_suspend_threads(td, p); 25104093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2511ebceaf6dSDavid Xu /* 2512ebceaf6dSDavid Xu * only thread sending signal to another 2513ebceaf6dSDavid Xu * process can reach here, if thread is sending 2514ebceaf6dSDavid Xu * signal to its process, because thread does 2515ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2516ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2517ebceaf6dSDavid Xu */ 2518ebceaf6dSDavid Xu thread_stopped(p); 25197b4a950aSDavid Xu PROC_SUNLOCK(p); 2520b4490c6eSKonstantin Belousov sigqueue_delete_proc(p, p->p_xsig); 25217b4a950aSDavid Xu } else 25227b4a950aSDavid Xu PROC_SUNLOCK(p); 252335c32a76SDavid Xu goto out; 252435c32a76SDavid Xu } 2525e602ba25SJulian Elischer } else { 2526e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 25279104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2528e602ba25SJulian Elischer goto out; 2529e602ba25SJulian Elischer } 2530e602ba25SJulian Elischer 2531b40ce416SJulian Elischer /* 2532e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2533e602ba25SJulian Elischer * running threads. 2534b40ce416SJulian Elischer */ 2535e602ba25SJulian Elischer runfast: 253694f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 25377b4a950aSDavid Xu PROC_SLOCK(p); 2538e602ba25SJulian Elischer thread_unsuspend(p); 25397b4a950aSDavid Xu PROC_SUNLOCK(p); 2540dc47fdf1SKonstantin Belousov out_cont: 2541dc47fdf1SKonstantin Belousov itimer_proc_continue(p); 25422fd1ffefSKonstantin Belousov kqtimer_proc_continue(p); 2543e602ba25SJulian Elischer out: 25447b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 25457b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 254661a74c5cSJeff Roberson if (wakeup_swapper) 254761a74c5cSJeff Roberson kick_proc0(); 254861a74c5cSJeff Roberson 25499104847fSDavid Xu return (ret); 2550e602ba25SJulian Elischer } 2551e602ba25SJulian Elischer 2552e602ba25SJulian Elischer /* 2553e602ba25SJulian Elischer * The force of a signal has been directed against a single 2554e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2555e602ba25SJulian Elischer * out of any sleep it may be in etc. 2556e602ba25SJulian Elischer */ 2557e602ba25SJulian Elischer static void 255894f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2559e602ba25SJulian Elischer { 2560e602ba25SJulian Elischer struct proc *p = td->td_proc; 256161a74c5cSJeff Roberson int prop, wakeup_swapper; 2562e602ba25SJulian Elischer 25638b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2564e602ba25SJulian Elischer prop = sigprop(sig); 2565a4c2da15SBruce Evans 25667b4a950aSDavid Xu PROC_SLOCK(p); 2567374ae2a3SJeff Roberson thread_lock(td); 2568e602ba25SJulian Elischer /* 2569aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2570aef68c96SKonstantin Belousov * killed in this lifetime. Be careful to avoid bumping the 2571aef68c96SKonstantin Belousov * priority of the idle thread, since we still allow to signal 2572aef68c96SKonstantin Belousov * kernel processes. 2573e602ba25SJulian Elischer */ 2574fd50a707SBrooks Davis if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2575aef68c96SKonstantin Belousov td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2576b3a4fb14SDavid Xu sched_prio(td, PUSER); 257780c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2578e602ba25SJulian Elischer /* 2579e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2580e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2581e602ba25SJulian Elischer * be noticed when the process returns through 2582e602ba25SJulian Elischer * trap() or syscall(). 2583e602ba25SJulian Elischer */ 258444f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2585374ae2a3SJeff Roberson goto out; 2586e602ba25SJulian Elischer /* 2587e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2588e602ba25SJulian Elischer * asleep, we are finished; the process should not 2589e602ba25SJulian Elischer * be awakened. 2590df8bae1dSRodney W. Grimes */ 2591fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2592a54e85fdSJeff Roberson thread_unlock(td); 25937b4a950aSDavid Xu PROC_SUNLOCK(p); 25949104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 25954093529dSJeff Roberson /* 25964093529dSJeff Roberson * It may be on either list in this state. 25974093529dSJeff Roberson * Remove from both for now. 25984093529dSJeff Roberson */ 25999104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2600aa0fa334SJulian Elischer return; 2601df8bae1dSRodney W. Grimes } 2602df8bae1dSRodney W. Grimes 2603aa0fa334SJulian Elischer /* 2604a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2605a120a7a3SJohn Baldwin * STOP signal is deferred. 2606a120a7a3SJohn Baldwin */ 2607fd50a707SBrooks Davis if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 26086f56cb8dSKonstantin Belousov TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2609a120a7a3SJohn Baldwin goto out; 2610a120a7a3SJohn Baldwin 2611a120a7a3SJohn Baldwin /* 2612a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2613aa0fa334SJulian Elischer */ 2614aef68c96SKonstantin Belousov if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2615b3a4fb14SDavid Xu sched_prio(td, PUSER); 2616ec8297bdSDavid Xu 26179b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 261861a74c5cSJeff Roberson PROC_SUNLOCK(p); 261961a74c5cSJeff Roberson if (wakeup_swapper) 262061a74c5cSJeff Roberson kick_proc0(); 262161a74c5cSJeff Roberson return; 262261a74c5cSJeff Roberson } 262361a74c5cSJeff Roberson 2624df8bae1dSRodney W. Grimes /* 2625a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2626df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2627df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2628df8bae1dSRodney W. Grimes */ 2629a4c2da15SBruce Evans #ifdef SMP 263044f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2631e602ba25SJulian Elischer forward_signal(td); 26323163861cSTor Egge #endif 263361a74c5cSJeff Roberson 2634374ae2a3SJeff Roberson out: 26357b4a950aSDavid Xu PROC_SUNLOCK(p); 2636374ae2a3SJeff Roberson thread_unlock(td); 2637a4c2da15SBruce Evans } 2638df8bae1dSRodney W. Grimes 263987a64872SKonstantin Belousov static void 2640140ceb5dSKonstantin Belousov ptrace_coredumpreq(struct thread *td, struct proc *p, 2641140ceb5dSKonstantin Belousov struct thr_coredump_req *tcq) 2642140ceb5dSKonstantin Belousov { 2643140ceb5dSKonstantin Belousov void *rl_cookie; 2644140ceb5dSKonstantin Belousov 2645140ceb5dSKonstantin Belousov if (p->p_sysent->sv_coredump == NULL) { 2646140ceb5dSKonstantin Belousov tcq->tc_error = ENOSYS; 2647140ceb5dSKonstantin Belousov return; 2648140ceb5dSKonstantin Belousov } 2649140ceb5dSKonstantin Belousov 2650140ceb5dSKonstantin Belousov rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX); 2651140ceb5dSKonstantin Belousov tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp, 2652140ceb5dSKonstantin Belousov tcq->tc_limit, tcq->tc_flags); 2653140ceb5dSKonstantin Belousov vn_rangelock_unlock(tcq->tc_vp, rl_cookie); 2654140ceb5dSKonstantin Belousov } 2655140ceb5dSKonstantin Belousov 2656140ceb5dSKonstantin Belousov static void 2657140ceb5dSKonstantin Belousov ptrace_syscallreq(struct thread *td, struct proc *p, 2658140ceb5dSKonstantin Belousov struct thr_syscall_req *tsr) 2659140ceb5dSKonstantin Belousov { 2660140ceb5dSKonstantin Belousov struct sysentvec *sv; 2661140ceb5dSKonstantin Belousov struct sysent *se; 2662140ceb5dSKonstantin Belousov register_t rv_saved[2]; 2663140ceb5dSKonstantin Belousov int error, nerror; 2664140ceb5dSKonstantin Belousov int sc; 2665140ceb5dSKonstantin Belousov bool audited, sy_thr_static; 2666140ceb5dSKonstantin Belousov 2667140ceb5dSKonstantin Belousov sv = p->p_sysent; 2668140ceb5dSKonstantin Belousov if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) { 2669140ceb5dSKonstantin Belousov tsr->ts_ret.sr_error = ENOSYS; 2670140ceb5dSKonstantin Belousov return; 2671140ceb5dSKonstantin Belousov } 2672140ceb5dSKonstantin Belousov 2673140ceb5dSKonstantin Belousov sc = tsr->ts_sa.code; 2674140ceb5dSKonstantin Belousov if (sc == SYS_syscall || sc == SYS___syscall) { 2675*974be51bSKonstantin Belousov sc = tsr->ts_sa.args[0]; 2676140ceb5dSKonstantin Belousov memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1], 2677140ceb5dSKonstantin Belousov sizeof(register_t) * (tsr->ts_nargs - 1)); 2678140ceb5dSKonstantin Belousov } 2679140ceb5dSKonstantin Belousov 2680140ceb5dSKonstantin Belousov tsr->ts_sa.callp = se = &sv->sv_table[sc]; 2681140ceb5dSKonstantin Belousov 2682140ceb5dSKonstantin Belousov VM_CNT_INC(v_syscall); 2683140ceb5dSKonstantin Belousov td->td_pticks = 0; 2684140ceb5dSKonstantin Belousov if (__predict_false(td->td_cowgen != atomic_load_int( 2685140ceb5dSKonstantin Belousov &td->td_proc->p_cowgen))) 2686140ceb5dSKonstantin Belousov thread_cow_update(td); 2687140ceb5dSKonstantin Belousov 2688140ceb5dSKonstantin Belousov #ifdef CAPABILITY_MODE 2689140ceb5dSKonstantin Belousov if (IN_CAPABILITY_MODE(td) && (se->sy_flags & SYF_CAPENABLED) == 0) { 2690140ceb5dSKonstantin Belousov tsr->ts_ret.sr_error = ECAPMODE; 2691140ceb5dSKonstantin Belousov return; 2692140ceb5dSKonstantin Belousov } 2693140ceb5dSKonstantin Belousov #endif 2694140ceb5dSKonstantin Belousov 2695140ceb5dSKonstantin Belousov sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0; 2696*974be51bSKonstantin Belousov audited = AUDIT_SYSCALL_ENTER(sc, td) != 0; 2697140ceb5dSKonstantin Belousov 2698140ceb5dSKonstantin Belousov if (!sy_thr_static) { 2699140ceb5dSKonstantin Belousov error = syscall_thread_enter(td, se); 2700140ceb5dSKonstantin Belousov if (error != 0) { 2701140ceb5dSKonstantin Belousov tsr->ts_ret.sr_error = error; 2702140ceb5dSKonstantin Belousov return; 2703140ceb5dSKonstantin Belousov } 2704140ceb5dSKonstantin Belousov } 2705140ceb5dSKonstantin Belousov 2706140ceb5dSKonstantin Belousov rv_saved[0] = td->td_retval[0]; 2707140ceb5dSKonstantin Belousov rv_saved[1] = td->td_retval[1]; 2708140ceb5dSKonstantin Belousov nerror = td->td_errno; 2709140ceb5dSKonstantin Belousov td->td_retval[0] = 0; 2710140ceb5dSKonstantin Belousov td->td_retval[1] = 0; 2711140ceb5dSKonstantin Belousov 2712140ceb5dSKonstantin Belousov #ifdef KDTRACE_HOOKS 2713140ceb5dSKonstantin Belousov if (se->sy_entry != 0) 2714140ceb5dSKonstantin Belousov (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0); 2715140ceb5dSKonstantin Belousov #endif 2716140ceb5dSKonstantin Belousov tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args); 2717140ceb5dSKonstantin Belousov #ifdef KDTRACE_HOOKS 2718140ceb5dSKonstantin Belousov if (se->sy_return != 0) 2719140ceb5dSKonstantin Belousov (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN, 2720*974be51bSKonstantin Belousov tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]); 2721140ceb5dSKonstantin Belousov #endif 2722140ceb5dSKonstantin Belousov 2723140ceb5dSKonstantin Belousov tsr->ts_ret.sr_retval[0] = td->td_retval[0]; 2724140ceb5dSKonstantin Belousov tsr->ts_ret.sr_retval[1] = td->td_retval[1]; 2725140ceb5dSKonstantin Belousov td->td_retval[0] = rv_saved[0]; 2726140ceb5dSKonstantin Belousov td->td_retval[1] = rv_saved[1]; 2727140ceb5dSKonstantin Belousov td->td_errno = nerror; 2728140ceb5dSKonstantin Belousov 2729140ceb5dSKonstantin Belousov if (audited) 2730140ceb5dSKonstantin Belousov AUDIT_SYSCALL_EXIT(error, td); 2731140ceb5dSKonstantin Belousov if (!sy_thr_static) 2732140ceb5dSKonstantin Belousov syscall_thread_exit(td, se); 2733140ceb5dSKonstantin Belousov } 2734140ceb5dSKonstantin Belousov 2735140ceb5dSKonstantin Belousov static void 2736140ceb5dSKonstantin Belousov ptrace_remotereq(struct thread *td, int flag) 273787a64872SKonstantin Belousov { 273887a64872SKonstantin Belousov struct proc *p; 273987a64872SKonstantin Belousov 274087a64872SKonstantin Belousov MPASS(td == curthread); 274187a64872SKonstantin Belousov p = td->td_proc; 274287a64872SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 2743140ceb5dSKonstantin Belousov if ((td->td_dbgflags & flag) == 0) 274487a64872SKonstantin Belousov return; 274587a64872SKonstantin Belousov KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped")); 2746140ceb5dSKonstantin Belousov KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL")); 274787a64872SKonstantin Belousov 274887a64872SKonstantin Belousov PROC_UNLOCK(p); 2749140ceb5dSKonstantin Belousov switch (flag) { 2750140ceb5dSKonstantin Belousov case TDB_COREDUMPREQ: 2751140ceb5dSKonstantin Belousov ptrace_coredumpreq(td, p, td->td_remotereq); 2752140ceb5dSKonstantin Belousov break; 2753140ceb5dSKonstantin Belousov case TDB_SCREMOTEREQ: 2754140ceb5dSKonstantin Belousov ptrace_syscallreq(td, p, td->td_remotereq); 2755140ceb5dSKonstantin Belousov break; 2756140ceb5dSKonstantin Belousov default: 2757140ceb5dSKonstantin Belousov __unreachable(); 2758140ceb5dSKonstantin Belousov } 275987a64872SKonstantin Belousov PROC_LOCK(p); 2760140ceb5dSKonstantin Belousov 2761140ceb5dSKonstantin Belousov MPASS((td->td_dbgflags & flag) != 0); 2762140ceb5dSKonstantin Belousov td->td_dbgflags &= ~flag; 2763e6feeae2SKonstantin Belousov td->td_remotereq = NULL; 276487a64872SKonstantin Belousov wakeup(p); 276587a64872SKonstantin Belousov } 276687a64872SKonstantin Belousov 27676f56cb8dSKonstantin Belousov static int 2768c53fec76SKonstantin Belousov sig_suspend_threads(struct thread *td, struct proc *p) 2769d8267df7SDavid Xu { 2770d8267df7SDavid Xu struct thread *td2; 27716f56cb8dSKonstantin Belousov int wakeup_swapper; 2772d8267df7SDavid Xu 2773d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 27747b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 2775d8267df7SDavid Xu 27766f56cb8dSKonstantin Belousov wakeup_swapper = 0; 2777d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2778a54e85fdSJeff Roberson thread_lock(td2); 2779c6d31b83SKonstantin Belousov ast_sched_locked(td2, TDA_SUSPEND); 2780d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2781f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2782f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2783a120a7a3SJohn Baldwin /* 2784a120a7a3SJohn Baldwin * Once a thread is asleep with 27856f56cb8dSKonstantin Belousov * TDF_SBDRY and without TDF_SERESTART 27866f56cb8dSKonstantin Belousov * or TDF_SEINTR set, it should never 2787a120a7a3SJohn Baldwin * become suspended due to this check. 2788a120a7a3SJohn Baldwin */ 2789a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2790a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 279161a74c5cSJeff Roberson if (TD_SBDRY_INTR(td2)) { 279246e47c4fSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, 279346e47c4fSKonstantin Belousov TD_SBDRY_ERRNO(td2)); 279461a74c5cSJeff Roberson continue; 2795f33a947bSKonstantin Belousov } 279661a74c5cSJeff Roberson } else if (!TD_IS_SUSPENDED(td2)) 279761a74c5cSJeff Roberson thread_suspend_one(td2); 2798f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2799d8267df7SDavid Xu #ifdef SMP 2800d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2801d8267df7SDavid Xu forward_signal(td2); 2802d8267df7SDavid Xu #endif 2803d8267df7SDavid Xu } 2804a54e85fdSJeff Roberson thread_unlock(td2); 2805d8267df7SDavid Xu } 28066f56cb8dSKonstantin Belousov return (wakeup_swapper); 2807d8267df7SDavid Xu } 2808d8267df7SDavid Xu 280982a4538fSEric Badger /* 281082a4538fSEric Badger * Stop the process for an event deemed interesting to the debugger. If si is 281182a4538fSEric Badger * non-NULL, this is a signal exchange; the new signal requested by the 281282a4538fSEric Badger * debugger will be returned for handling. If si is NULL, this is some other 281382a4538fSEric Badger * type of interesting event. The debugger may request a signal be delivered in 281482a4538fSEric Badger * that case as well, however it will be deferred until it can be handled. 281582a4538fSEric Badger */ 2816cbf4e354SDavid Xu int 281782a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si) 28184cc9f52fSRobert Drehmel { 28194cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 282082a4538fSEric Badger struct thread *td2; 282182a4538fSEric Badger ksiginfo_t ksi; 28224cc9f52fSRobert Drehmel 282330a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 28240f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 28254cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2826aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 28274cc9f52fSRobert Drehmel 2828cbf4e354SDavid Xu td->td_xsig = sig; 282982a4538fSEric Badger 283082a4538fSEric Badger if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 283182a4538fSEric Badger td->td_dbgflags |= TDB_XSIG; 2832515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2833515b7a0bSJohn Baldwin td->td_tid, p->p_pid, td->td_dbgflags, sig); 28347b4a950aSDavid Xu PROC_SLOCK(p); 2835904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 283682a4538fSEric Badger if (P_KILLED(p)) { 283782a4538fSEric Badger /* 283882a4538fSEric Badger * Ensure that, if we've been PT_KILLed, the 283982a4538fSEric Badger * exit status reflects that. Another thread 284082a4538fSEric Badger * may also be in ptracestop(), having just 284182a4538fSEric Badger * received the SIGKILL, but this thread was 284282a4538fSEric Badger * unsuspended first. 284382a4538fSEric Badger */ 284482a4538fSEric Badger td->td_dbgflags &= ~TDB_XSIG; 284582a4538fSEric Badger td->td_xsig = SIGKILL; 284682a4538fSEric Badger p->p_ptevents = 0; 284782a4538fSEric Badger break; 284882a4538fSEric Badger } 28495fcfab6eSJohn Baldwin if (p->p_flag & P_SINGLE_EXIT && 28505fcfab6eSJohn Baldwin !(td->td_dbgflags & TDB_EXIT)) { 28515fcfab6eSJohn Baldwin /* 28525fcfab6eSJohn Baldwin * Ignore ptrace stops except for thread exit 28535fcfab6eSJohn Baldwin * events when the process exits. 28545fcfab6eSJohn Baldwin */ 2855904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 28567b4a950aSDavid Xu PROC_SUNLOCK(p); 285782a4538fSEric Badger return (0); 2858cbf4e354SDavid Xu } 2859b7a25e63SKonstantin Belousov 2860cbf4e354SDavid Xu /* 2861b7a25e63SKonstantin Belousov * Make wait(2) work. Ensure that right after the 2862b7a25e63SKonstantin Belousov * attach, the thread which was decided to become the 2863b7a25e63SKonstantin Belousov * leader of attach gets reported to the waiter. 2864b7a25e63SKonstantin Belousov * Otherwise, just avoid overwriting another thread's 2865b7a25e63SKonstantin Belousov * assignment to p_xthread. If another thread has 2866b7a25e63SKonstantin Belousov * already set p_xthread, the current thread will get 2867b7a25e63SKonstantin Belousov * a chance to report itself upon the next iteration. 2868cbf4e354SDavid Xu */ 2869b7a25e63SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0 || 28707f649ddaSMark Johnston ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2871b7a25e63SKonstantin Belousov p->p_xthread == NULL)) { 2872b4490c6eSKonstantin Belousov p->p_xsig = sig; 2873cbf4e354SDavid Xu p->p_xthread = td; 2874ab74c843SKonstantin Belousov 2875ab74c843SKonstantin Belousov /* 2876ab74c843SKonstantin Belousov * If we are on sleepqueue already, 2877ab74c843SKonstantin Belousov * let sleepqueue code decide if it 2878ab74c843SKonstantin Belousov * needs to go sleep after attach. 2879ab74c843SKonstantin Belousov */ 2880ab74c843SKonstantin Belousov if (td->td_wchan == NULL) 2881b7a25e63SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 2882ab74c843SKonstantin Belousov 2883b7a25e63SKonstantin Belousov p->p_flag2 &= ~P2_PTRACE_FSTP; 2884b7a25e63SKonstantin Belousov p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2885c53fec76SKonstantin Belousov sig_suspend_threads(td, p); 2886b7a25e63SKonstantin Belousov } 28876fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 28886fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 28896fa39a73SKonstantin Belousov } 2890cbf4e354SDavid Xu stopme: 289168d311b6SKonstantin Belousov td->td_dbgflags |= TDB_SSWITCH; 28926ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 289368d311b6SKonstantin Belousov td->td_dbgflags &= ~TDB_SSWITCH; 2894140ceb5dSKonstantin Belousov if ((td->td_dbgflags & (TDB_COREDUMPREQ | 2895140ceb5dSKonstantin Belousov TDB_SCREMOTEREQ)) != 0) { 2896140ceb5dSKonstantin Belousov MPASS((td->td_dbgflags & (TDB_COREDUMPREQ | 2897140ceb5dSKonstantin Belousov TDB_SCREMOTEREQ)) != 2898140ceb5dSKonstantin Belousov (TDB_COREDUMPREQ | TDB_SCREMOTEREQ)); 289987a64872SKonstantin Belousov PROC_SUNLOCK(p); 2900140ceb5dSKonstantin Belousov ptrace_remotereq(td, td->td_dbgflags & 2901140ceb5dSKonstantin Belousov (TDB_COREDUMPREQ | TDB_SCREMOTEREQ)); 290287a64872SKonstantin Belousov PROC_SLOCK(p); 290387a64872SKonstantin Belousov goto stopme; 290487a64872SKonstantin Belousov } 29057ce60f60SDavid Xu if (p->p_xthread == td) 29067ce60f60SDavid Xu p->p_xthread = NULL; 29077ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2908cbf4e354SDavid Xu break; 2909904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2910cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2911cbf4e354SDavid Xu break; 2912cbf4e354SDavid Xu goto stopme; 2913cbf4e354SDavid Xu } 2914cbf4e354SDavid Xu } 29157b4a950aSDavid Xu PROC_SUNLOCK(p); 291682a4538fSEric Badger } 291782a4538fSEric Badger 291882a4538fSEric Badger if (si != NULL && sig == td->td_xsig) { 291982a4538fSEric Badger /* Parent wants us to take the original signal unchanged. */ 292082a4538fSEric Badger si->ksi_flags |= KSI_HEAD; 292182a4538fSEric Badger if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 292282a4538fSEric Badger si->ksi_signo = 0; 292382a4538fSEric Badger } else if (td->td_xsig != 0) { 292482a4538fSEric Badger /* 292582a4538fSEric Badger * If parent wants us to take a new signal, then it will leave 292682a4538fSEric Badger * it in td->td_xsig; otherwise we just look for signals again. 292782a4538fSEric Badger */ 292882a4538fSEric Badger ksiginfo_init(&ksi); 292982a4538fSEric Badger ksi.ksi_signo = td->td_xsig; 293082a4538fSEric Badger ksi.ksi_flags |= KSI_PTRACE; 2931146fc63fSKonstantin Belousov td2 = sigtd(p, td->td_xsig, false); 293282a4538fSEric Badger tdsendsignal(p, td2, td->td_xsig, &ksi); 293382a4538fSEric Badger if (td != td2) 293482a4538fSEric Badger return (0); 293582a4538fSEric Badger } 293682a4538fSEric Badger 2937cbf4e354SDavid Xu return (td->td_xsig); 29384cc9f52fSRobert Drehmel } 29394cc9f52fSRobert Drehmel 29400bc52b0bSKonstantin Belousov static void 294180a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 29426b286ee8SKonstantin Belousov { 29436b286ee8SKonstantin Belousov struct sigacts *ps; 29446b286ee8SKonstantin Belousov struct thread *td; 2945407af02bSDavid Xu int sig; 2946146fc63fSKonstantin Belousov bool fastblk, pslocked; 29476b286ee8SKonstantin Belousov 29486b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 294970778bbaSKonstantin Belousov ps = p->p_sigacts; 2950146fc63fSKonstantin Belousov pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0; 2951146fc63fSKonstantin Belousov mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED); 2952407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2953407af02bSDavid Xu return; 2954407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2955146fc63fSKonstantin Belousov fastblk = (flags & SIGPROCMASK_FASTBLK) != 0; 295681f2e906SMark Johnston SIG_FOREACH(sig, &block) { 2957146fc63fSKonstantin Belousov td = sigtd(p, sig, fastblk); 2958146fc63fSKonstantin Belousov 2959146fc63fSKonstantin Belousov /* 2960146fc63fSKonstantin Belousov * If sigtd() selected us despite sigfastblock is 2961146fc63fSKonstantin Belousov * blocking, do not activate AST or wake us, to avoid 2962146fc63fSKonstantin Belousov * loop in AST handler. 2963146fc63fSKonstantin Belousov */ 2964146fc63fSKonstantin Belousov if (fastblk && td == curthread) 2965146fc63fSKonstantin Belousov continue; 2966146fc63fSKonstantin Belousov 29676b286ee8SKonstantin Belousov signotify(td); 2968146fc63fSKonstantin Belousov if (!pslocked) 29696b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2970396a0d44SKonstantin Belousov if (p->p_flag & P_TRACED || 2971396a0d44SKonstantin Belousov (SIGISMEMBER(ps->ps_sigcatch, sig) && 2972146fc63fSKonstantin Belousov !SIGISMEMBER(td->td_sigmask, sig))) { 2973407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2974407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 29756b286ee8SKonstantin Belousov ERESTART)); 2976146fc63fSKonstantin Belousov } 2977146fc63fSKonstantin Belousov if (!pslocked) 29786b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 29796b286ee8SKonstantin Belousov } 29806b286ee8SKonstantin Belousov } 29816b286ee8SKonstantin Belousov 29826b286ee8SKonstantin Belousov void 29836b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 29846b286ee8SKonstantin Belousov { 29856b286ee8SKonstantin Belousov struct proc *p; 29866b286ee8SKonstantin Belousov sigset_t unblocked; 29876b286ee8SKonstantin Belousov 29886b286ee8SKonstantin Belousov p = td->td_proc; 29896b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 29906b286ee8SKonstantin Belousov 29916b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 29926b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 29936b286ee8SKonstantin Belousov return; 29946b286ee8SKonstantin Belousov 29956b286ee8SKonstantin Belousov /* 29966b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 29976b286ee8SKonstantin Belousov * about this by filling the sigmask. 29986b286ee8SKonstantin Belousov * 29996b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 30006b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 30016b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 30026b286ee8SKonstantin Belousov */ 30036b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 30046b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 30056b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 300680a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 30076b286ee8SKonstantin Belousov 30086b286ee8SKonstantin Belousov } 30096b286ee8SKonstantin Belousov 30103a1e5dd8SKonstantin Belousov static int 30113a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags) 3012a120a7a3SJohn Baldwin { 3013a120a7a3SJohn Baldwin 30143a1e5dd8SKonstantin Belousov MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 30153a1e5dd8SKonstantin Belousov (cflags & TDF_SBDRY) != 0); 30163a1e5dd8SKonstantin Belousov return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 3017a120a7a3SJohn Baldwin } 3018a120a7a3SJohn Baldwin 3019a120a7a3SJohn Baldwin /* 30203a1e5dd8SKonstantin Belousov * Defer the delivery of SIGSTOP for the current thread, according to 30213a1e5dd8SKonstantin Belousov * the requested mode. Returns previous flags, which must be restored 30223a1e5dd8SKonstantin Belousov * by sigallowstop(). 30233a1e5dd8SKonstantin Belousov * 30243a1e5dd8SKonstantin Belousov * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 30253a1e5dd8SKonstantin Belousov * cleared by the current thread, which allow the lock-less read-only 30263a1e5dd8SKonstantin Belousov * accesses below. 3027a120a7a3SJohn Baldwin */ 3028e3612a4cSKonstantin Belousov int 302946e47c4fSKonstantin Belousov sigdeferstop_impl(int mode) 3030a120a7a3SJohn Baldwin { 3031593efaf9SJohn Baldwin struct thread *td; 30323a1e5dd8SKonstantin Belousov int cflags, nflags; 3033a120a7a3SJohn Baldwin 3034593efaf9SJohn Baldwin td = curthread; 30353a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 30363a1e5dd8SKonstantin Belousov switch (mode) { 30373a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_NOP: 30383a1e5dd8SKonstantin Belousov nflags = cflags; 30393a1e5dd8SKonstantin Belousov break; 30403a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_OFF: 30413a1e5dd8SKonstantin Belousov nflags = 0; 30423a1e5dd8SKonstantin Belousov break; 30433a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_SILENT: 30443a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 30453a1e5dd8SKonstantin Belousov break; 30463a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_EINTR: 30473a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 30483a1e5dd8SKonstantin Belousov break; 30493a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_ERESTART: 30503a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 30513a1e5dd8SKonstantin Belousov break; 30523a1e5dd8SKonstantin Belousov default: 30533a1e5dd8SKonstantin Belousov panic("sigdeferstop: invalid mode %x", mode); 30543a1e5dd8SKonstantin Belousov break; 30553a1e5dd8SKonstantin Belousov } 305646e47c4fSKonstantin Belousov if (cflags == nflags) 305746e47c4fSKonstantin Belousov return (SIGDEFERSTOP_VAL_NCHG); 3058a120a7a3SJohn Baldwin thread_lock(td); 30593a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | nflags; 3060a120a7a3SJohn Baldwin thread_unlock(td); 30613a1e5dd8SKonstantin Belousov return (cflags); 30623a1e5dd8SKonstantin Belousov } 30633a1e5dd8SKonstantin Belousov 30643a1e5dd8SKonstantin Belousov /* 30653a1e5dd8SKonstantin Belousov * Restores the STOP handling mode, typically permitting the delivery 30663a1e5dd8SKonstantin Belousov * of SIGSTOP for the current thread. This does not immediately 30673a1e5dd8SKonstantin Belousov * suspend if a stop was posted. Instead, the thread will suspend 30683a1e5dd8SKonstantin Belousov * either via ast() or a subsequent interruptible sleep. 30693a1e5dd8SKonstantin Belousov */ 30703a1e5dd8SKonstantin Belousov void 307146e47c4fSKonstantin Belousov sigallowstop_impl(int prev) 30723a1e5dd8SKonstantin Belousov { 30733a1e5dd8SKonstantin Belousov struct thread *td; 30743a1e5dd8SKonstantin Belousov int cflags; 30753a1e5dd8SKonstantin Belousov 307646e47c4fSKonstantin Belousov KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 30773a1e5dd8SKonstantin Belousov KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 30783a1e5dd8SKonstantin Belousov ("sigallowstop: incorrect previous mode %x", prev)); 30793a1e5dd8SKonstantin Belousov td = curthread; 30803a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 30813a1e5dd8SKonstantin Belousov if (cflags != prev) { 30823a1e5dd8SKonstantin Belousov thread_lock(td); 30833a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | prev; 30843a1e5dd8SKonstantin Belousov thread_unlock(td); 30853a1e5dd8SKonstantin Belousov } 3086a120a7a3SJohn Baldwin } 3087a120a7a3SJohn Baldwin 308881f2e906SMark Johnston enum sigstatus { 308981f2e906SMark Johnston SIGSTATUS_HANDLE, 309081f2e906SMark Johnston SIGSTATUS_HANDLED, 309181f2e906SMark Johnston SIGSTATUS_IGNORE, 309281f2e906SMark Johnston SIGSTATUS_SBDRY_STOP, 309381f2e906SMark Johnston }; 309481f2e906SMark Johnston 309581f2e906SMark Johnston /* 309681f2e906SMark Johnston * The thread has signal "sig" pending. Figure out what to do with it: 309781f2e906SMark Johnston * 309881f2e906SMark Johnston * _HANDLE -> the caller should handle the signal 309981f2e906SMark Johnston * _HANDLED -> handled internally, reload pending signal set 310081f2e906SMark Johnston * _IGNORE -> ignored, remove from the set of pending signals and try the 310181f2e906SMark Johnston * next pending signal 310281f2e906SMark Johnston * _SBDRY_STOP -> the signal should stop the thread but this is not 310381f2e906SMark Johnston * permitted in the current context 310481f2e906SMark Johnston */ 310581f2e906SMark Johnston static enum sigstatus 310681f2e906SMark Johnston sigprocess(struct thread *td, int sig) 310781f2e906SMark Johnston { 310881f2e906SMark Johnston struct proc *p; 310981f2e906SMark Johnston struct sigacts *ps; 311081f2e906SMark Johnston struct sigqueue *queue; 311181f2e906SMark Johnston ksiginfo_t ksi; 311281f2e906SMark Johnston int prop; 311381f2e906SMark Johnston 311481f2e906SMark Johnston KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig)); 311581f2e906SMark Johnston 311681f2e906SMark Johnston p = td->td_proc; 311781f2e906SMark Johnston ps = p->p_sigacts; 311881f2e906SMark Johnston mtx_assert(&ps->ps_mtx, MA_OWNED); 311981f2e906SMark Johnston PROC_LOCK_ASSERT(p, MA_OWNED); 312081f2e906SMark Johnston 312181f2e906SMark Johnston /* 312281f2e906SMark Johnston * We should allow pending but ignored signals below 312302a2aacbSKonstantin Belousov * if there is sigwait() active, or P_TRACED was 312481f2e906SMark Johnston * on when they were posted. 312581f2e906SMark Johnston */ 312681f2e906SMark Johnston if (SIGISMEMBER(ps->ps_sigignore, sig) && 312781f2e906SMark Johnston (p->p_flag & P_TRACED) == 0 && 312881f2e906SMark Johnston (td->td_flags & TDF_SIGWAIT) == 0) { 312981f2e906SMark Johnston return (SIGSTATUS_IGNORE); 313081f2e906SMark Johnston } 313181f2e906SMark Johnston 313202a2aacbSKonstantin Belousov /* 313302a2aacbSKonstantin Belousov * If the process is going to single-thread mode to prepare 313402a2aacbSKonstantin Belousov * for exit, there is no sense in delivering any signal 313502a2aacbSKonstantin Belousov * to usermode. Another important consequence is that 313602a2aacbSKonstantin Belousov * msleep(..., PCATCH, ...) now is only interruptible by a 313702a2aacbSKonstantin Belousov * suspend request. 313802a2aacbSKonstantin Belousov */ 313902a2aacbSKonstantin Belousov if ((p->p_flag2 & P2_WEXIT) != 0) 314002a2aacbSKonstantin Belousov return (SIGSTATUS_IGNORE); 314102a2aacbSKonstantin Belousov 314281f2e906SMark Johnston if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 314381f2e906SMark Johnston /* 314481f2e906SMark Johnston * If traced, always stop. 314581f2e906SMark Johnston * Remove old signal from queue before the stop. 314681f2e906SMark Johnston * XXX shrug off debugger, it causes siginfo to 314781f2e906SMark Johnston * be thrown away. 314881f2e906SMark Johnston */ 314981f2e906SMark Johnston queue = &td->td_sigqueue; 315081f2e906SMark Johnston ksiginfo_init(&ksi); 315181f2e906SMark Johnston if (sigqueue_get(queue, sig, &ksi) == 0) { 315281f2e906SMark Johnston queue = &p->p_sigqueue; 315381f2e906SMark Johnston sigqueue_get(queue, sig, &ksi); 315481f2e906SMark Johnston } 315581f2e906SMark Johnston td->td_si = ksi.ksi_info; 315681f2e906SMark Johnston 315781f2e906SMark Johnston mtx_unlock(&ps->ps_mtx); 315881f2e906SMark Johnston sig = ptracestop(td, sig, &ksi); 315981f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 316081f2e906SMark Johnston 316181f2e906SMark Johnston td->td_si.si_signo = 0; 316281f2e906SMark Johnston 316381f2e906SMark Johnston /* 316481f2e906SMark Johnston * Keep looking if the debugger discarded or 316581f2e906SMark Johnston * replaced the signal. 316681f2e906SMark Johnston */ 316781f2e906SMark Johnston if (sig == 0) 316881f2e906SMark Johnston return (SIGSTATUS_HANDLED); 316981f2e906SMark Johnston 317081f2e906SMark Johnston /* 317181f2e906SMark Johnston * If the signal became masked, re-queue it. 317281f2e906SMark Johnston */ 317381f2e906SMark Johnston if (SIGISMEMBER(td->td_sigmask, sig)) { 317481f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 317581f2e906SMark Johnston sigqueue_add(&p->p_sigqueue, sig, &ksi); 317681f2e906SMark Johnston return (SIGSTATUS_HANDLED); 317781f2e906SMark Johnston } 317881f2e906SMark Johnston 317981f2e906SMark Johnston /* 318081f2e906SMark Johnston * If the traced bit got turned off, requeue the signal and 318181f2e906SMark Johnston * reload the set of pending signals. This ensures that p_sig* 318281f2e906SMark Johnston * and p_sigact are consistent. 318381f2e906SMark Johnston */ 318481f2e906SMark Johnston if ((p->p_flag & P_TRACED) == 0) { 3185a24afbb4SKonstantin Belousov if ((ksi.ksi_flags & KSI_PTRACE) == 0) { 318681f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 318781f2e906SMark Johnston sigqueue_add(queue, sig, &ksi); 3188a24afbb4SKonstantin Belousov } 318981f2e906SMark Johnston return (SIGSTATUS_HANDLED); 319081f2e906SMark Johnston } 319181f2e906SMark Johnston } 319281f2e906SMark Johnston 319381f2e906SMark Johnston /* 319481f2e906SMark Johnston * Decide whether the signal should be returned. 319581f2e906SMark Johnston * Return the signal's number, or fall through 319681f2e906SMark Johnston * to clear it from the pending mask. 319781f2e906SMark Johnston */ 319881f2e906SMark Johnston switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 319981f2e906SMark Johnston case (intptr_t)SIG_DFL: 320081f2e906SMark Johnston /* 320181f2e906SMark Johnston * Don't take default actions on system processes. 320281f2e906SMark Johnston */ 320381f2e906SMark Johnston if (p->p_pid <= 1) { 320481f2e906SMark Johnston #ifdef DIAGNOSTIC 320581f2e906SMark Johnston /* 320681f2e906SMark Johnston * Are you sure you want to ignore SIGSEGV 320781f2e906SMark Johnston * in init? XXX 320881f2e906SMark Johnston */ 320981f2e906SMark Johnston printf("Process (pid %lu) got signal %d\n", 321081f2e906SMark Johnston (u_long)p->p_pid, sig); 321181f2e906SMark Johnston #endif 321281f2e906SMark Johnston return (SIGSTATUS_IGNORE); 321381f2e906SMark Johnston } 321481f2e906SMark Johnston 321581f2e906SMark Johnston /* 321681f2e906SMark Johnston * If there is a pending stop signal to process with 321781f2e906SMark Johnston * default action, stop here, then clear the signal. 321881f2e906SMark Johnston * Traced or exiting processes should ignore stops. 321981f2e906SMark Johnston * Additionally, a member of an orphaned process group 322081f2e906SMark Johnston * should ignore tty stops. 322181f2e906SMark Johnston */ 322281f2e906SMark Johnston prop = sigprop(sig); 322381f2e906SMark Johnston if (prop & SIGPROP_STOP) { 322481f2e906SMark Johnston mtx_unlock(&ps->ps_mtx); 322581f2e906SMark Johnston if ((p->p_flag & (P_TRACED | P_WEXIT | 322681f2e906SMark Johnston P_SINGLE_EXIT)) != 0 || ((p->p_pgrp-> 322781f2e906SMark Johnston pg_flags & PGRP_ORPHANED) != 0 && 322881f2e906SMark Johnston (prop & SIGPROP_TTYSTOP) != 0)) { 322981f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 323081f2e906SMark Johnston return (SIGSTATUS_IGNORE); 323181f2e906SMark Johnston } 323281f2e906SMark Johnston if (TD_SBDRY_INTR(td)) { 323381f2e906SMark Johnston KASSERT((td->td_flags & TDF_SBDRY) != 0, 323481f2e906SMark Johnston ("lost TDF_SBDRY")); 323581f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 323681f2e906SMark Johnston return (SIGSTATUS_SBDRY_STOP); 323781f2e906SMark Johnston } 323881f2e906SMark Johnston WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 323981f2e906SMark Johnston &p->p_mtx.lock_object, "Catching SIGSTOP"); 324081f2e906SMark Johnston sigqueue_delete(&td->td_sigqueue, sig); 324181f2e906SMark Johnston sigqueue_delete(&p->p_sigqueue, sig); 324281f2e906SMark Johnston p->p_flag |= P_STOPPED_SIG; 324381f2e906SMark Johnston p->p_xsig = sig; 324481f2e906SMark Johnston PROC_SLOCK(p); 3245c53fec76SKonstantin Belousov sig_suspend_threads(td, p); 324681f2e906SMark Johnston thread_suspend_switch(td, p); 324781f2e906SMark Johnston PROC_SUNLOCK(p); 324881f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 324981f2e906SMark Johnston return (SIGSTATUS_HANDLED); 325081f2e906SMark Johnston } else if ((prop & SIGPROP_IGNORE) != 0 && 325181f2e906SMark Johnston (td->td_flags & TDF_SIGWAIT) == 0) { 325281f2e906SMark Johnston /* 325381f2e906SMark Johnston * Default action is to ignore; drop it if 325481f2e906SMark Johnston * not in kern_sigtimedwait(). 325581f2e906SMark Johnston */ 325681f2e906SMark Johnston return (SIGSTATUS_IGNORE); 325781f2e906SMark Johnston } else { 325881f2e906SMark Johnston return (SIGSTATUS_HANDLE); 325981f2e906SMark Johnston } 326081f2e906SMark Johnston 326181f2e906SMark Johnston case (intptr_t)SIG_IGN: 326281f2e906SMark Johnston if ((td->td_flags & TDF_SIGWAIT) == 0) 326381f2e906SMark Johnston return (SIGSTATUS_IGNORE); 326481f2e906SMark Johnston else 326581f2e906SMark Johnston return (SIGSTATUS_HANDLE); 326681f2e906SMark Johnston 326781f2e906SMark Johnston default: 326881f2e906SMark Johnston /* 326981f2e906SMark Johnston * This signal has an action, let postsig() process it. 327081f2e906SMark Johnston */ 327181f2e906SMark Johnston return (SIGSTATUS_HANDLE); 327281f2e906SMark Johnston } 327381f2e906SMark Johnston } 327481f2e906SMark Johnston 3275df8bae1dSRodney W. Grimes /* 3276df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 3277df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 3278df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 3279df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 328081f2e906SMark Johnston * a syscall or trap (though this can usually be done without calling 328181f2e906SMark Johnston * issignal by checking the pending signal masks in cursig.) The normal call 3282df8bae1dSRodney W. Grimes * sequence is 3283df8bae1dSRodney W. Grimes * 3284e602ba25SJulian Elischer * while (sig = cursig(curthread)) 32852c42a146SMarcel Moolenaar * postsig(sig); 3286df8bae1dSRodney W. Grimes */ 32876711f10fSJohn Baldwin static int 32883cf3b9f0SJohn Baldwin issignal(struct thread *td) 3289df8bae1dSRodney W. Grimes { 3290e602ba25SJulian Elischer struct proc *p; 32914093529dSJeff Roberson sigset_t sigpending; 329281f2e906SMark Johnston int sig; 3293df8bae1dSRodney W. Grimes 3294e602ba25SJulian Elischer p = td->td_proc; 3295628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 329681f2e906SMark Johnston 3297df8bae1dSRodney W. Grimes for (;;) { 32989104847fSDavid Xu sigpending = td->td_sigqueue.sq_signals; 32996b286ee8SKonstantin Belousov SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 33004093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 33014093529dSJeff Roberson 33026f56cb8dSKonstantin Belousov if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 33036f56cb8dSKonstantin Belousov (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 33044093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 33054093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 3306df8bae1dSRodney W. Grimes return (0); 3307146fc63fSKonstantin Belousov 3308146fc63fSKonstantin Belousov /* 3309146fc63fSKonstantin Belousov * Do fast sigblock if requested by usermode. Since 3310146fc63fSKonstantin Belousov * we do know that there was a signal pending at this 3311146fc63fSKonstantin Belousov * point, set the FAST_SIGBLOCK_PEND as indicator for 3312146fc63fSKonstantin Belousov * usermode to perform a dummy call to 3313146fc63fSKonstantin Belousov * FAST_SIGBLOCK_UNBLOCK, which causes immediate 3314146fc63fSKonstantin Belousov * delivery of postponed pending signal. 3315146fc63fSKonstantin Belousov */ 3316146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 3317146fc63fSKonstantin Belousov if (td->td_sigblock_val != 0) 3318146fc63fSKonstantin Belousov SIGSETNAND(sigpending, fastblock_mask); 3319146fc63fSKonstantin Belousov if (SIGISEMPTY(sigpending)) { 3320146fc63fSKonstantin Belousov td->td_pflags |= TDP_SIGFASTPENDING; 3321146fc63fSKonstantin Belousov return (0); 3322146fc63fSKonstantin Belousov } 3323146fc63fSKonstantin Belousov } 3324146fc63fSKonstantin Belousov 3325b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 3326b7a25e63SKonstantin Belousov (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 3327b7a25e63SKonstantin Belousov SIGISMEMBER(sigpending, SIGSTOP)) { 3328b7a25e63SKonstantin Belousov /* 3329b7a25e63SKonstantin Belousov * If debugger just attached, always consume 3330b7a25e63SKonstantin Belousov * SIGSTOP from ptrace(PT_ATTACH) first, to 3331b7a25e63SKonstantin Belousov * execute the debugger attach ritual in 3332b7a25e63SKonstantin Belousov * order. 3333b7a25e63SKonstantin Belousov */ 3334b7a25e63SKonstantin Belousov td->td_dbgflags |= TDB_FSTP; 333581f2e906SMark Johnston SIGEMPTYSET(sigpending); 333681f2e906SMark Johnston SIGADDSET(sigpending, SIGSTOP); 3337b7a25e63SKonstantin Belousov } 33382a024a2bSSean Eric Fagan 333981f2e906SMark Johnston SIG_FOREACH(sig, &sigpending) { 334081f2e906SMark Johnston switch (sigprocess(td, sig)) { 334181f2e906SMark Johnston case SIGSTATUS_HANDLE: 334281f2e906SMark Johnston return (sig); 334381f2e906SMark Johnston case SIGSTATUS_HANDLED: 334481f2e906SMark Johnston goto next; 334581f2e906SMark Johnston case SIGSTATUS_IGNORE: 33469104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 33476b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 334881f2e906SMark Johnston break; 334981f2e906SMark Johnston case SIGSTATUS_SBDRY_STOP: 335046e47c4fSKonstantin Belousov return (-1); 335146e47c4fSKonstantin Belousov } 3352df8bae1dSRodney W. Grimes } 3353b7a25e63SKonstantin Belousov next:; 3354df8bae1dSRodney W. Grimes } 3355df8bae1dSRodney W. Grimes } 3356df8bae1dSRodney W. Grimes 3357e574e444SDavid Xu void 3358e574e444SDavid Xu thread_stopped(struct proc *p) 3359e574e444SDavid Xu { 3360e574e444SDavid Xu int n; 3361e574e444SDavid Xu 3362e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 33637b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 3364e574e444SDavid Xu n = p->p_suspcount; 33657c9a98f1SDavid Xu if (p == curproc) 3366e574e444SDavid Xu n++; 3367e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 33687b4a950aSDavid Xu PROC_SUNLOCK(p); 3369407948a5SDavid Xu p->p_flag &= ~P_WAITED; 3370e574e444SDavid Xu PROC_LOCK(p->p_pptr); 33717f96995eSDavid Xu childproc_stopped(p, (p->p_flag & P_TRACED) ? 3372ebceaf6dSDavid Xu CLD_TRAPPED : CLD_STOPPED); 3373e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 33747b4a950aSDavid Xu PROC_SLOCK(p); 3375e574e444SDavid Xu } 3376e574e444SDavid Xu } 3377e574e444SDavid Xu 3378df8bae1dSRodney W. Grimes /* 3379df8bae1dSRodney W. Grimes * Take the action for the specified signal 3380df8bae1dSRodney W. Grimes * from the current set of pending signals. 3381df8bae1dSRodney W. Grimes */ 338275c586a4SKonstantin Belousov int 33830167b33bSKonstantin Belousov postsig(int sig) 3384df8bae1dSRodney W. Grimes { 33850167b33bSKonstantin Belousov struct thread *td; 33860167b33bSKonstantin Belousov struct proc *p; 3387628d2653SJohn Baldwin struct sigacts *ps; 33882c42a146SMarcel Moolenaar sig_t action; 33899104847fSDavid Xu ksiginfo_t ksi; 3390e442f29fSKonstantin Belousov sigset_t returnmask; 3391df8bae1dSRodney W. Grimes 33922c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 33935526d2d9SEivind Eklund 33940167b33bSKonstantin Belousov td = curthread; 33950167b33bSKonstantin Belousov p = td->td_proc; 33962ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3397628d2653SJohn Baldwin ps = p->p_sigacts; 339890af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 33995da49fcbSDavid Xu ksiginfo_init(&ksi); 34006b286ee8SKonstantin Belousov if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 34016b286ee8SKonstantin Belousov sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 340275c586a4SKonstantin Belousov return (0); 34039104847fSDavid Xu ksi.ksi_signo = sig; 340456c06c4bSDavid Xu if (ksi.ksi_code == SI_TIMER) 340556c06c4bSDavid Xu itimer_accept(p, ksi.ksi_timerid, &ksi); 34062c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 3407df8bae1dSRodney W. Grimes #ifdef KTRACE 3408374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 34095e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 341061009552SJilles Tjoelker &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 3411df8bae1dSRodney W. Grimes #endif 34122a024a2bSSean Eric Fagan 34138460a577SJohn Birrell if (action == SIG_DFL) { 3414df8bae1dSRodney W. Grimes /* 3415df8bae1dSRodney W. Grimes * Default action, where the default is to kill 3416df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 3417df8bae1dSRodney W. Grimes */ 341890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 341986be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi.ksi_info); 3420b40ce416SJulian Elischer sigexit(td, sig); 3421df8bae1dSRodney W. Grimes /* NOTREACHED */ 3422df8bae1dSRodney W. Grimes } else { 3423df8bae1dSRodney W. Grimes /* 3424df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 3425df8bae1dSRodney W. Grimes */ 3426cd735d8fSKonstantin Belousov KASSERT(action != SIG_IGN, ("postsig action %p", action)); 3427cd735d8fSKonstantin Belousov KASSERT(!SIGISMEMBER(td->td_sigmask, sig), 3428cd735d8fSKonstantin Belousov ("postsig action: blocked sig %d", sig)); 3429cd735d8fSKonstantin Belousov 3430df8bae1dSRodney W. Grimes /* 3431df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 3432645682fdSLuoqi Chen * occurrences of this signal. 3433df8bae1dSRodney W. Grimes * 3434645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 3435df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 3436645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 3437df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 3438df8bae1dSRodney W. Grimes */ 34395e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 34404093529dSJeff Roberson returnmask = td->td_oldsigmask; 34415e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 3442df8bae1dSRodney W. Grimes } else 34434093529dSJeff Roberson returnmask = td->td_sigmask; 34442c42a146SMarcel Moolenaar 3445c90c9021SEd Schouten if (p->p_sig == sig) { 34466626c604SJulian Elischer p->p_sig = 0; 3447df8bae1dSRodney W. Grimes } 34489104847fSDavid Xu (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3449e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 3450df8bae1dSRodney W. Grimes } 345175c586a4SKonstantin Belousov return (1); 3452df8bae1dSRodney W. Grimes } 3453df8bae1dSRodney W. Grimes 34540c82fb26SKonstantin Belousov int 34550c82fb26SKonstantin Belousov sig_ast_checksusp(struct thread *td) 34560c82fb26SKonstantin Belousov { 34573d277851SKonstantin Belousov struct proc *p __diagused; 34580c82fb26SKonstantin Belousov int ret; 34590c82fb26SKonstantin Belousov 34600c82fb26SKonstantin Belousov p = td->td_proc; 34610c82fb26SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 34620c82fb26SKonstantin Belousov 3463c6d31b83SKonstantin Belousov if (!td_ast_pending(td, TDA_SUSPEND)) 34640c82fb26SKonstantin Belousov return (0); 34650c82fb26SKonstantin Belousov 34660c82fb26SKonstantin Belousov ret = thread_suspend_check(1); 34670c82fb26SKonstantin Belousov MPASS(ret == 0 || ret == EINTR || ret == ERESTART); 34680c82fb26SKonstantin Belousov return (ret); 34690c82fb26SKonstantin Belousov } 34700c82fb26SKonstantin Belousov 34710c82fb26SKonstantin Belousov int 34720c82fb26SKonstantin Belousov sig_ast_needsigchk(struct thread *td) 34730c82fb26SKonstantin Belousov { 34740c82fb26SKonstantin Belousov struct proc *p; 34750c82fb26SKonstantin Belousov struct sigacts *ps; 34760c82fb26SKonstantin Belousov int ret, sig; 34770c82fb26SKonstantin Belousov 34780c82fb26SKonstantin Belousov p = td->td_proc; 34790c82fb26SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 34800c82fb26SKonstantin Belousov 3481c6d31b83SKonstantin Belousov if (!td_ast_pending(td, TDA_SIG)) 34820c82fb26SKonstantin Belousov return (0); 34830c82fb26SKonstantin Belousov 34840c82fb26SKonstantin Belousov ps = p->p_sigacts; 34850c82fb26SKonstantin Belousov mtx_lock(&ps->ps_mtx); 34860c82fb26SKonstantin Belousov sig = cursig(td); 34870c82fb26SKonstantin Belousov if (sig == -1) { 34880c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 34890c82fb26SKonstantin Belousov KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY")); 34900c82fb26SKonstantin Belousov KASSERT(TD_SBDRY_INTR(td), 34910c82fb26SKonstantin Belousov ("lost TDF_SERESTART of TDF_SEINTR")); 34920c82fb26SKonstantin Belousov KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != 34930c82fb26SKonstantin Belousov (TDF_SEINTR | TDF_SERESTART), 34940c82fb26SKonstantin Belousov ("both TDF_SEINTR and TDF_SERESTART")); 34950c82fb26SKonstantin Belousov ret = TD_SBDRY_ERRNO(td); 34960c82fb26SKonstantin Belousov } else if (sig != 0) { 34970c82fb26SKonstantin Belousov ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART; 34980c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 34990c82fb26SKonstantin Belousov } else { 35000c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 35010c82fb26SKonstantin Belousov ret = 0; 35020c82fb26SKonstantin Belousov } 35030c82fb26SKonstantin Belousov 35040c82fb26SKonstantin Belousov /* 35050c82fb26SKonstantin Belousov * Do not go into sleep if this thread was the ptrace(2) 35060c82fb26SKonstantin Belousov * attach leader. cursig() consumed SIGSTOP from PT_ATTACH, 35070c82fb26SKonstantin Belousov * but we usually act on the signal by interrupting sleep, and 35080c82fb26SKonstantin Belousov * should do that here as well. 35090c82fb26SKonstantin Belousov */ 35100c82fb26SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0) { 35110c82fb26SKonstantin Belousov if (ret == 0) 35120c82fb26SKonstantin Belousov ret = EINTR; 35130c82fb26SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 35140c82fb26SKonstantin Belousov } 35150c82fb26SKonstantin Belousov 35160c82fb26SKonstantin Belousov return (ret); 35170c82fb26SKonstantin Belousov } 35180c82fb26SKonstantin Belousov 35190400be45SKonstantin Belousov int 35200400be45SKonstantin Belousov sig_intr(void) 35210400be45SKonstantin Belousov { 35220400be45SKonstantin Belousov struct thread *td; 35230400be45SKonstantin Belousov struct proc *p; 35240400be45SKonstantin Belousov int ret; 35250400be45SKonstantin Belousov 35260400be45SKonstantin Belousov td = curthread; 3527c6d31b83SKonstantin Belousov if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND)) 3528203dda8aSKonstantin Belousov return (0); 3529203dda8aSKonstantin Belousov 35300400be45SKonstantin Belousov p = td->td_proc; 35310400be45SKonstantin Belousov 35320400be45SKonstantin Belousov PROC_LOCK(p); 35330400be45SKonstantin Belousov ret = sig_ast_checksusp(td); 35340400be45SKonstantin Belousov if (ret == 0) 35350400be45SKonstantin Belousov ret = sig_ast_needsigchk(td); 35360400be45SKonstantin Belousov PROC_UNLOCK(p); 35370400be45SKonstantin Belousov return (ret); 35380400be45SKonstantin Belousov } 35390400be45SKonstantin Belousov 3540244ab566SKonstantin Belousov bool 3541244ab566SKonstantin Belousov curproc_sigkilled(void) 3542244ab566SKonstantin Belousov { 3543244ab566SKonstantin Belousov struct thread *td; 3544244ab566SKonstantin Belousov struct proc *p; 3545244ab566SKonstantin Belousov struct sigacts *ps; 3546244ab566SKonstantin Belousov bool res; 3547244ab566SKonstantin Belousov 3548244ab566SKonstantin Belousov td = curthread; 3549c6d31b83SKonstantin Belousov if (!td_ast_pending(td, TDA_SIG)) 3550244ab566SKonstantin Belousov return (false); 3551244ab566SKonstantin Belousov 3552244ab566SKonstantin Belousov p = td->td_proc; 3553244ab566SKonstantin Belousov PROC_LOCK(p); 3554244ab566SKonstantin Belousov ps = p->p_sigacts; 3555244ab566SKonstantin Belousov mtx_lock(&ps->ps_mtx); 3556244ab566SKonstantin Belousov res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) || 3557244ab566SKonstantin Belousov SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL); 3558244ab566SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 3559244ab566SKonstantin Belousov PROC_UNLOCK(p); 3560244ab566SKonstantin Belousov return (res); 3561244ab566SKonstantin Belousov } 3562244ab566SKonstantin Belousov 3563a70e9a13SKonstantin Belousov void 3564a70e9a13SKonstantin Belousov proc_wkilled(struct proc *p) 3565a70e9a13SKonstantin Belousov { 3566a70e9a13SKonstantin Belousov 3567a70e9a13SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 3568a70e9a13SKonstantin Belousov if ((p->p_flag & P_WKILLED) == 0) { 3569a70e9a13SKonstantin Belousov p->p_flag |= P_WKILLED; 3570a70e9a13SKonstantin Belousov /* 3571a70e9a13SKonstantin Belousov * Notify swapper that there is a process to swap in. 3572a70e9a13SKonstantin Belousov * The notification is racy, at worst it would take 10 3573a70e9a13SKonstantin Belousov * seconds for the swapper process to notice. 3574a70e9a13SKonstantin Belousov */ 3575a70e9a13SKonstantin Belousov if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0) 3576a70e9a13SKonstantin Belousov wakeup(&proc0); 3577a70e9a13SKonstantin Belousov } 3578a70e9a13SKonstantin Belousov } 3579a70e9a13SKonstantin Belousov 3580df8bae1dSRodney W. Grimes /* 3581df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 3582df8bae1dSRodney W. Grimes */ 358326f9a767SRodney W. Grimes void 3584fe20aaecSRyan Libby killproc(struct proc *p, const char *why) 3585df8bae1dSRodney W. Grimes { 35869081e5e8SJohn Baldwin 35879081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3588c3209846SPawel Jakub Dawidek CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3589c3209846SPawel Jakub Dawidek p->p_comm); 3590af8beccaSKristof Provost log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n", 35914ae4822dSKristof Provost p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id, 3592de2d0d29SKristof Provost p->p_ucred->cr_uid, why); 3593a70e9a13SKonstantin Belousov proc_wkilled(p); 35948451d0ddSKip Macy kern_psignal(p, SIGKILL); 3595df8bae1dSRodney W. Grimes } 3596df8bae1dSRodney W. Grimes 3597df8bae1dSRodney W. Grimes /* 3598df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 3599df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 3600df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 3601df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 3602df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 3603df8bae1dSRodney W. Grimes * does not return. 3604df8bae1dSRodney W. Grimes */ 360526f9a767SRodney W. Grimes void 3606e052a8b9SEd Maste sigexit(struct thread *td, int sig) 3607df8bae1dSRodney W. Grimes { 3608b40ce416SJulian Elischer struct proc *p = td->td_proc; 3609df8bae1dSRodney W. Grimes 3610628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 36114493a13eSKonstantin Belousov proc_set_p2_wexit(p); 36124493a13eSKonstantin Belousov 3613df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 3614f97c3df1SDavid Schultz /* 3615f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 3616f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 3617f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 3618f97c3df1SDavid Schultz * change out from under it. 3619f97c3df1SDavid Schultz * 3620f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 3621f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 3622f97c3df1SDavid Schultz */ 3623fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_CORE) && 3624fd50a707SBrooks Davis thread_single(p, SINGLE_NO_EXIT) == 0) { 36252c42a146SMarcel Moolenaar p->p_sig = sig; 3626c364e17eSAndrey A. Chernov /* 3627c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 3628c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 3629c364e17eSAndrey A. Chernov * these messages.) 3630c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 36314ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 3632c364e17eSAndrey A. Chernov */ 3633b40ce416SJulian Elischer if (coredump(td) == 0) 36342c42a146SMarcel Moolenaar sig |= WCOREFLAG; 363557308494SJoerg Wunsch if (kern_logsigexit) 363657308494SJoerg Wunsch log(LOG_INFO, 3637af8beccaSKristof Provost "pid %d (%s), jid %d, uid %d: exited on " 36384ae4822dSKristof Provost "signal %d%s\n", p->p_pid, p->p_comm, 36394ae4822dSKristof Provost p->p_ucred->cr_prison->pr_id, 3640de2d0d29SKristof Provost td->td_ucred->cr_uid, 36412c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 36422c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 36434ae89b95SJohn Baldwin } else 3644628d2653SJohn Baldwin PROC_UNLOCK(p); 3645b4490c6eSKonstantin Belousov exit1(td, 0, sig); 3646df8bae1dSRodney W. Grimes /* NOTREACHED */ 3647df8bae1dSRodney W. Grimes } 3648df8bae1dSRodney W. Grimes 3649ebceaf6dSDavid Xu /* 36507f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 36517f96995eSDavid Xu * is changed. 3652ebceaf6dSDavid Xu */ 36537f96995eSDavid Xu static void 36547f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 3655ebceaf6dSDavid Xu { 3656ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 3657ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3658ebceaf6dSDavid Xu 3659ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 3660ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 3661ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 36627f96995eSDavid Xu p->p_ksi->ksi_status = status; 3663ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 3664ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3665ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 3666ebceaf6dSDavid Xu return; 3667ebceaf6dSDavid Xu } 3668ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3669ebceaf6dSDavid Xu } 3670ebceaf6dSDavid Xu 36717f96995eSDavid Xu static void 3672b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig) 36737f96995eSDavid Xu { 36747f96995eSDavid Xu struct sigacts *ps; 36757f96995eSDavid Xu 36767f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 36777f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 36787f96995eSDavid Xu 36797f96995eSDavid Xu /* 36807f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 36817f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 36827f96995eSDavid Xu * that parent will awake, because parent may masked 36837f96995eSDavid Xu * the signal. 36847f96995eSDavid Xu */ 36857f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 36867f96995eSDavid Xu wakeup(p->p_pptr); 36877f96995eSDavid Xu 36887f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 36897f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 36907f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 36917f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 3692b20a9aa9SJilles Tjoelker sigparent(p, reason, sig); 36937f96995eSDavid Xu } else 36947f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 36957f96995eSDavid Xu } 36967f96995eSDavid Xu 36977f96995eSDavid Xu void 36987f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 36997f96995eSDavid Xu { 3700b4490c6eSKonstantin Belousov 3701b4490c6eSKonstantin Belousov childproc_jobstate(p, reason, p->p_xsig); 37027f96995eSDavid Xu } 37037f96995eSDavid Xu 3704ebceaf6dSDavid Xu void 3705ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3706ebceaf6dSDavid Xu { 37077f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3708ebceaf6dSDavid Xu } 3709ebceaf6dSDavid Xu 3710ebceaf6dSDavid Xu void 3711ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3712ebceaf6dSDavid Xu { 3713b4490c6eSKonstantin Belousov int reason, status; 3714ebceaf6dSDavid Xu 3715b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 3716b4490c6eSKonstantin Belousov reason = CLD_DUMPED; 3717b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3718b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 3719b4490c6eSKonstantin Belousov reason = CLD_KILLED; 3720b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3721b4490c6eSKonstantin Belousov } else { 3722b4490c6eSKonstantin Belousov reason = CLD_EXITED; 3723b4490c6eSKonstantin Belousov status = p->p_xexit; 3724b4490c6eSKonstantin Belousov } 37257f96995eSDavid Xu /* 37267f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 37277f96995eSDavid Xu * done in exit1(). 37287f96995eSDavid Xu */ 37297f96995eSDavid Xu sigparent(p, reason, status); 3730ebceaf6dSDavid Xu } 3731ebceaf6dSDavid Xu 3732f1fe1e02SMariusz Zaborski #define MAX_NUM_CORE_FILES 100000 3733cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES 3734cc37baeaSStephen J. Kiernan #define NUM_CORE_FILES 5 3735cc37baeaSStephen J. Kiernan #endif 3736cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3737cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES; 3738e7228204SAlfred Perlstein 3739e7228204SAlfred Perlstein static int 3740e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3741e7228204SAlfred Perlstein { 3742e7228204SAlfred Perlstein int error; 3743e7228204SAlfred Perlstein int new_val; 3744e7228204SAlfred Perlstein 3745c5105012SKonstantin Belousov new_val = num_cores; 3746e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3747e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3748e7228204SAlfred Perlstein return (error); 3749cc37baeaSStephen J. Kiernan if (new_val > MAX_NUM_CORE_FILES) 3750cc37baeaSStephen J. Kiernan new_val = MAX_NUM_CORE_FILES; 3751e7228204SAlfred Perlstein if (new_val < 0) 3752e7228204SAlfred Perlstein new_val = 0; 3753e7228204SAlfred Perlstein num_cores = new_val; 3754e7228204SAlfred Perlstein return (0); 3755e7228204SAlfred Perlstein } 37567029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores, 3757fe27f1dbSAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int), 37587029da5cSPawel Biernacki sysctl_debug_num_cores_check, "I", 37596cad1a5dSMariusz Zaborski "Maximum number of generated process corefiles while using index format"); 3760e7228204SAlfred Perlstein 37616026dcd7SMark Johnston #define GZIP_SUFFIX ".gz" 37626026dcd7SMark Johnston #define ZSTD_SUFFIX ".zst" 3763aa14e9b7SMark Johnston 376478f57a9cSMark Johnston int compress_user_cores = 0; 3765e7228204SAlfred Perlstein 376678f57a9cSMark Johnston static int 376778f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS) 376878f57a9cSMark Johnston { 376978f57a9cSMark Johnston int error, val; 377078f57a9cSMark Johnston 377178f57a9cSMark Johnston val = compress_user_cores; 377278f57a9cSMark Johnston error = sysctl_handle_int(oidp, &val, 0, req); 377378f57a9cSMark Johnston if (error != 0 || req->newptr == NULL) 377478f57a9cSMark Johnston return (error); 377578f57a9cSMark Johnston if (val != 0 && !compressor_avail(val)) 377678f57a9cSMark Johnston return (EINVAL); 377778f57a9cSMark Johnston compress_user_cores = val; 377878f57a9cSMark Johnston return (error); 377978f57a9cSMark Johnston } 37807029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores, 37817029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int), 37827029da5cSPawel Biernacki sysctl_compress_user_cores, "I", 37836026dcd7SMark Johnston "Enable compression of user corefiles (" 37846026dcd7SMark Johnston __XSTRING(COMPRESS_GZIP) " = gzip, " 37856026dcd7SMark Johnston __XSTRING(COMPRESS_ZSTD) " = zstd)"); 378678f57a9cSMark Johnston 378778f57a9cSMark Johnston int compress_user_cores_level = 6; 378878f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN, 378978f57a9cSMark Johnston &compress_user_cores_level, 0, 379078f57a9cSMark Johnston "Corefile compression level"); 3791e7228204SAlfred Perlstein 37925bc0ff88SMateusz Guzik /* 37935bc0ff88SMateusz Guzik * Protect the access to corefilename[] by allproc_lock. 37945bc0ff88SMateusz Guzik */ 37955bc0ff88SMateusz Guzik #define corefilename_lock allproc_lock 37965bc0ff88SMateusz Guzik 37976d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3798fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 37995bc0ff88SMateusz Guzik 38005bc0ff88SMateusz Guzik static int 38015bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 38025bc0ff88SMateusz Guzik { 38035bc0ff88SMateusz Guzik int error; 38045bc0ff88SMateusz Guzik 38055bc0ff88SMateusz Guzik sx_xlock(&corefilename_lock); 38065bc0ff88SMateusz Guzik error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 38075bc0ff88SMateusz Guzik req); 38085bc0ff88SMateusz Guzik sx_xunlock(&corefilename_lock); 38095bc0ff88SMateusz Guzik 38105bc0ff88SMateusz Guzik return (error); 38115bc0ff88SMateusz Guzik } 3812fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 38135bc0ff88SMateusz Guzik CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 38145bc0ff88SMateusz Guzik "Process corefile name format string"); 3815c5edb423SSean Eric Fagan 38160dea6e3cSMariusz Zaborski static void 38170dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp) 38180dea6e3cSMariusz Zaborski { 38190dea6e3cSMariusz Zaborski 3820b249ce48SMateusz Guzik VOP_UNLOCK(vp); 38210dea6e3cSMariusz Zaborski vn_close(vp, FWRITE, td->td_ucred, td); 38220dea6e3cSMariusz Zaborski } 38230dea6e3cSMariusz Zaborski 38240dea6e3cSMariusz Zaborski /* 38250dea6e3cSMariusz Zaborski * If the core format has a %I in it, then we need to check 38260dea6e3cSMariusz Zaborski * for existing corefiles before defining a name. 3827f1fe1e02SMariusz Zaborski * To do this we iterate over 0..ncores to find a 38280dea6e3cSMariusz Zaborski * non-existing core file name to use. If all core files are 38290dea6e3cSMariusz Zaborski * already used we choose the oldest one. 38300dea6e3cSMariusz Zaborski */ 38310dea6e3cSMariusz Zaborski static int 38320dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos, 3833f1fe1e02SMariusz Zaborski int indexlen, int ncores, struct vnode **vpp) 38340dea6e3cSMariusz Zaborski { 38350dea6e3cSMariusz Zaborski struct vnode *oldvp, *nextvp, *vp; 38360dea6e3cSMariusz Zaborski struct vattr vattr; 38370dea6e3cSMariusz Zaborski struct nameidata nd; 38380dea6e3cSMariusz Zaborski int error, i, flags, oflags, cmode; 3839f1fe1e02SMariusz Zaborski char ch; 38400dea6e3cSMariusz Zaborski struct timespec lasttime; 38410dea6e3cSMariusz Zaborski 38420dea6e3cSMariusz Zaborski nextvp = oldvp = NULL; 38430dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 38440dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 38450dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 38460dea6e3cSMariusz Zaborski 3847f1fe1e02SMariusz Zaborski for (i = 0; i < ncores; i++) { 38480dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 3849f1fe1e02SMariusz Zaborski 3850f1fe1e02SMariusz Zaborski ch = name[indexpos + indexlen]; 3851f1fe1e02SMariusz Zaborski (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen, 3852f1fe1e02SMariusz Zaborski i); 3853f1fe1e02SMariusz Zaborski name[indexpos + indexlen] = ch; 38540dea6e3cSMariusz Zaborski 38557e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 38560dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 38570dea6e3cSMariusz Zaborski NULL); 38580dea6e3cSMariusz Zaborski if (error != 0) 38590dea6e3cSMariusz Zaborski break; 38600dea6e3cSMariusz Zaborski 38610dea6e3cSMariusz Zaborski vp = nd.ni_vp; 3862bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 38630dea6e3cSMariusz Zaborski if ((flags & O_CREAT) == O_CREAT) { 38640dea6e3cSMariusz Zaborski nextvp = vp; 38650dea6e3cSMariusz Zaborski break; 38660dea6e3cSMariusz Zaborski } 38670dea6e3cSMariusz Zaborski 38680dea6e3cSMariusz Zaborski error = VOP_GETATTR(vp, &vattr, td->td_ucred); 38690dea6e3cSMariusz Zaborski if (error != 0) { 38700dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 38710dea6e3cSMariusz Zaborski break; 38720dea6e3cSMariusz Zaborski } 38730dea6e3cSMariusz Zaborski 38740dea6e3cSMariusz Zaborski if (oldvp == NULL || 38750dea6e3cSMariusz Zaborski lasttime.tv_sec > vattr.va_mtime.tv_sec || 38760dea6e3cSMariusz Zaborski (lasttime.tv_sec == vattr.va_mtime.tv_sec && 38770dea6e3cSMariusz Zaborski lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) { 38780dea6e3cSMariusz Zaborski if (oldvp != NULL) 3879e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 38800dea6e3cSMariusz Zaborski oldvp = vp; 3881e298466eSAndriy Gapon VOP_UNLOCK(oldvp); 38820dea6e3cSMariusz Zaborski lasttime = vattr.va_mtime; 38830dea6e3cSMariusz Zaborski } else { 38840dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 38850dea6e3cSMariusz Zaborski } 38860dea6e3cSMariusz Zaborski } 38870dea6e3cSMariusz Zaborski 38880dea6e3cSMariusz Zaborski if (oldvp != NULL) { 388989f2ab06SKonstantin Belousov if (nextvp == NULL) { 389089f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) { 389189f2ab06SKonstantin Belousov error = EFAULT; 3892e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 389389f2ab06SKonstantin Belousov } else { 389489f2ab06SKonstantin Belousov nextvp = oldvp; 3895e298466eSAndriy Gapon error = vn_lock(nextvp, LK_EXCLUSIVE); 3896e298466eSAndriy Gapon if (error != 0) { 3897e298466eSAndriy Gapon vn_close(nextvp, FWRITE, td->td_ucred, 3898e298466eSAndriy Gapon td); 3899e298466eSAndriy Gapon nextvp = NULL; 3900e298466eSAndriy Gapon } 390189f2ab06SKonstantin Belousov } 390289f2ab06SKonstantin Belousov } else { 3903e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 390489f2ab06SKonstantin Belousov } 39050dea6e3cSMariusz Zaborski } 39060dea6e3cSMariusz Zaborski if (error != 0) { 39070dea6e3cSMariusz Zaborski if (nextvp != NULL) 39080dea6e3cSMariusz Zaborski vnode_close_locked(td, oldvp); 39090dea6e3cSMariusz Zaborski } else { 39100dea6e3cSMariusz Zaborski *vpp = nextvp; 39110dea6e3cSMariusz Zaborski } 39120dea6e3cSMariusz Zaborski 39130dea6e3cSMariusz Zaborski return (error); 39140dea6e3cSMariusz Zaborski } 39150dea6e3cSMariusz Zaborski 3916c5edb423SSean Eric Fagan /* 3917c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3918c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3919c345faeaSPawel Jakub Dawidek * and open/create core file. 3920c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3921c5edb423SSean Eric Fagan * %N name of process ("name") 3922c5edb423SSean Eric Fagan * %P process id (pid) 3923c5edb423SSean Eric Fagan * %U user id (uid) 3924c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3925c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3926c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3927c5edb423SSean Eric Fagan */ 3928c345faeaSPawel Jakub Dawidek static int 3929c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 39309d3ecb7eSEric van Gyzen int compress, int signum, struct vnode **vpp, char **namep) 39318b43b535SAlfred Perlstein { 393236b208e0SRobert Watson struct sbuf sb; 39330dea6e3cSMariusz Zaborski struct nameidata nd; 393436b208e0SRobert Watson const char *format; 3935c345faeaSPawel Jakub Dawidek char *hostname, *name; 3936f1fe1e02SMariusz Zaborski int cmode, error, flags, i, indexpos, indexlen, oflags, ncores; 3937c5edb423SSean Eric Fagan 3938b7402d82SAlfred Perlstein hostname = NULL; 39398b43b535SAlfred Perlstein format = corefilename; 3940086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3941f1fe1e02SMariusz Zaborski indexlen = 0; 3942e7228204SAlfred Perlstein indexpos = -1; 3943f1fe1e02SMariusz Zaborski ncores = num_cores; 3944c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 39455bc0ff88SMateusz Guzik sx_slock(&corefilename_lock); 394629146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3947c5edb423SSean Eric Fagan switch (format[i]) { 3948c5edb423SSean Eric Fagan case '%': /* Format character */ 3949c5edb423SSean Eric Fagan i++; 3950c5edb423SSean Eric Fagan switch (format[i]) { 3951c5edb423SSean Eric Fagan case '%': 395236b208e0SRobert Watson sbuf_putc(&sb, '%'); 3953c5edb423SSean Eric Fagan break; 3954e7228204SAlfred Perlstein case 'H': /* hostname */ 3955b7402d82SAlfred Perlstein if (hostname == NULL) { 3956b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3957086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3958b7402d82SAlfred Perlstein } 3959e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3960b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3961e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3962e7228204SAlfred Perlstein break; 3963e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3964f1fe1e02SMariusz Zaborski if (indexpos != -1) { 3965f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%%I"); 3966f1fe1e02SMariusz Zaborski break; 3967f1fe1e02SMariusz Zaborski } 3968f1fe1e02SMariusz Zaborski 3969f1fe1e02SMariusz Zaborski indexpos = sbuf_len(&sb); 3970f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%u", ncores - 1); 3971f1fe1e02SMariusz Zaborski indexlen = sbuf_len(&sb) - indexpos; 3972e7228204SAlfred Perlstein break; 3973c5edb423SSean Eric Fagan case 'N': /* process name */ 3974c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3975c5edb423SSean Eric Fagan break; 3976c5edb423SSean Eric Fagan case 'P': /* process id */ 397736b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3978c5edb423SSean Eric Fagan break; 39799d3ecb7eSEric van Gyzen case 'S': /* signal number */ 39809d3ecb7eSEric van Gyzen sbuf_printf(&sb, "%i", signum); 39819d3ecb7eSEric van Gyzen break; 3982c5edb423SSean Eric Fagan case 'U': /* user id */ 398336b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3984c5edb423SSean Eric Fagan break; 3985c5edb423SSean Eric Fagan default: 39868b43b535SAlfred Perlstein log(LOG_ERR, 398736b208e0SRobert Watson "Unknown format character %c in " 398836b208e0SRobert Watson "corename `%s'\n", format[i], format); 398929146f1aSPawel Jakub Dawidek break; 3990c5edb423SSean Eric Fagan } 3991c5edb423SSean Eric Fagan break; 3992c5edb423SSean Eric Fagan default: 399336b208e0SRobert Watson sbuf_putc(&sb, format[i]); 39946c08be2bSPawel Jakub Dawidek break; 3995c5edb423SSean Eric Fagan } 3996c5edb423SSean Eric Fagan } 39975bc0ff88SMateusz Guzik sx_sunlock(&corefilename_lock); 3998b7402d82SAlfred Perlstein free(hostname, M_TEMP); 399978f57a9cSMark Johnston if (compress == COMPRESS_GZIP) 40006026dcd7SMark Johnston sbuf_printf(&sb, GZIP_SUFFIX); 40016026dcd7SMark Johnston else if (compress == COMPRESS_ZSTD) 40026026dcd7SMark Johnston sbuf_printf(&sb, ZSTD_SUFFIX); 40034d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 400436b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 4005c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 4006b7402d82SAlfred Perlstein sbuf_delete(&sb); 4007c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 4008c345faeaSPawel Jakub Dawidek return (ENOMEM); 4009c5edb423SSean Eric Fagan } 401036b208e0SRobert Watson sbuf_finish(&sb); 401136b208e0SRobert Watson sbuf_delete(&sb); 4012e7228204SAlfred Perlstein 4013e7228204SAlfred Perlstein if (indexpos != -1) { 4014f1fe1e02SMariusz Zaborski error = corefile_open_last(td, name, indexpos, indexlen, ncores, 4015f1fe1e02SMariusz Zaborski vpp); 40160dea6e3cSMariusz Zaborski if (error != 0) { 4017e7228204SAlfred Perlstein log(LOG_ERR, 4018e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 4019e7228204SAlfred Perlstein "on initial open test, error = %d\n", 4020c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 4021c345faeaSPawel Jakub Dawidek } 40220dea6e3cSMariusz Zaborski } else { 40230dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 40240dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 40250dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 40260dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 402789f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) 402889f2ab06SKonstantin Belousov flags |= O_EXCL; 40290dea6e3cSMariusz Zaborski 40307e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 40310dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 40320dea6e3cSMariusz Zaborski NULL); 40330dea6e3cSMariusz Zaborski if (error == 0) { 40340dea6e3cSMariusz Zaborski *vpp = nd.ni_vp; 4035bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 4036c345faeaSPawel Jakub Dawidek } 4037c345faeaSPawel Jakub Dawidek } 4038c345faeaSPawel Jakub Dawidek 40390dea6e3cSMariusz Zaborski if (error != 0) { 4040c345faeaSPawel Jakub Dawidek #ifdef AUDIT 4041c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 4042c345faeaSPawel Jakub Dawidek #endif 4043c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 4044c345faeaSPawel Jakub Dawidek return (error); 4045e7228204SAlfred Perlstein } 4046c345faeaSPawel Jakub Dawidek *namep = name; 4047c345faeaSPawel Jakub Dawidek return (0); 404836b208e0SRobert Watson } 4049c5edb423SSean Eric Fagan 4050df8bae1dSRodney W. Grimes /* 4051fca666a1SJulian Elischer * Dump a process' core. The main routine does some 4052fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 4053fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 4054fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 4055fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 4056fca666a1SJulian Elischer */ 4057fca666a1SJulian Elischer 4058fca666a1SJulian Elischer static int 4059b40ce416SJulian Elischer coredump(struct thread *td) 4060fca666a1SJulian Elischer { 4061b40ce416SJulian Elischer struct proc *p = td->td_proc; 4062f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 4063f06f465dSPawel Jakub Dawidek struct vnode *vp; 406406ae1e91SMatthew Dillon struct flock lf; 4065fca666a1SJulian Elischer struct vattr vattr; 40667739d927SMateusz Guzik size_t fullpathsize; 4067c345faeaSPawel Jakub Dawidek int error, error1, locked; 4068fca666a1SJulian Elischer char *name; /* name of corefile */ 4069539c9eefSKonstantin Belousov void *rl_cookie; 4070fca666a1SJulian Elischer off_t limit; 4071842ab62bSRui Paulo char *fullpath, *freepath = NULL; 4072349fcda4SWarner Losh struct sbuf *sb; 4073fca666a1SJulian Elischer 40744ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 4075f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 4076fca666a1SJulian Elischer 4077677258f7SKonstantin Belousov if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 4078677258f7SKonstantin Belousov (p->p_flag2 & P2_NOTRACE) != 0) { 4079628d2653SJohn Baldwin PROC_UNLOCK(p); 4080fca666a1SJulian Elischer return (EFAULT); 4081628d2653SJohn Baldwin } 4082fca666a1SJulian Elischer 4083fca666a1SJulian Elischer /* 408435a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 408535a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 408635a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 408735a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 408835a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 408935a2598fSSean Eric Fagan * if it is larger than the limit. 4090fca666a1SJulian Elischer */ 4091f6f6d240SMateusz Guzik limit = (off_t)lim_cur(td, RLIMIT_CORE); 4092b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 4093628d2653SJohn Baldwin PROC_UNLOCK(p); 409491d5354aSJohn Baldwin return (EFBIG); 409557274c51SChristian S.J. Peron } 4096b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 409735a2598fSSean Eric Fagan 4098aa14e9b7SMark Johnston error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 40999d3ecb7eSEric van Gyzen compress_user_cores, p->p_sig, &vp, &name); 4100c345faeaSPawel Jakub Dawidek if (error != 0) 4101fca666a1SJulian Elischer return (error); 410206ae1e91SMatthew Dillon 4103539c9eefSKonstantin Belousov /* 4104539c9eefSKonstantin Belousov * Don't dump to non-regular files or files with links. 410589f2ab06SKonstantin Belousov * Do not dump into system files. Effective user must own the corefile. 4106539c9eefSKonstantin Belousov */ 4107f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 41087a29e0bfSKonstantin Belousov vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 || 410989f2ab06SKonstantin Belousov vattr.va_uid != cred->cr_uid) { 4110b249ce48SMateusz Guzik VOP_UNLOCK(vp); 4111832dafadSDon Lewis error = EFAULT; 4112dacbc9dbSKonstantin Belousov goto out; 4113832dafadSDon Lewis } 4114832dafadSDon Lewis 4115b249ce48SMateusz Guzik VOP_UNLOCK(vp); 4116539c9eefSKonstantin Belousov 4117539c9eefSKonstantin Belousov /* Postpone other writers, including core dumps of other processes. */ 4118539c9eefSKonstantin Belousov rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 4119539c9eefSKonstantin Belousov 412006ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 412106ae1e91SMatthew Dillon lf.l_start = 0; 412206ae1e91SMatthew Dillon lf.l_len = 0; 412306ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 4124c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 412506ae1e91SMatthew Dillon 4126fca666a1SJulian Elischer VATTR_NULL(&vattr); 4127fca666a1SJulian Elischer vattr.va_size = 0; 41286141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 41296141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 4130cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 41310359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 4132b249ce48SMateusz Guzik VOP_UNLOCK(vp); 4133628d2653SJohn Baldwin PROC_LOCK(p); 4134fca666a1SJulian Elischer p->p_acflag |= ACORE; 4135628d2653SJohn Baldwin PROC_UNLOCK(p); 4136fca666a1SJulian Elischer 413723c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 413878f57a9cSMark Johnston error = p->p_sysent->sv_coredump(td, vp, limit, 0); 413923c6445aSPawel Jakub Dawidek } else { 414023c6445aSPawel Jakub Dawidek error = ENOSYS; 414123c6445aSPawel Jakub Dawidek } 4142fca666a1SJulian Elischer 4143c447f5b2SRobert Watson if (locked) { 414406ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 414506ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 4146c447f5b2SRobert Watson } 4147539c9eefSKonstantin Belousov vn_rangelock_unlock(vp, rl_cookie); 4148dacbc9dbSKonstantin Belousov 4149842ab62bSRui Paulo /* 4150842ab62bSRui Paulo * Notify the userland helper that a process triggered a core dump. 4151842ab62bSRui Paulo * This allows the helper to run an automated debugging session. 4152842ab62bSRui Paulo */ 4153dacbc9dbSKonstantin Belousov if (error != 0 || coredump_devctl == 0) 4154842ab62bSRui Paulo goto out; 4155349fcda4SWarner Losh sb = sbuf_new_auto(); 4156feabaaf9SMateusz Guzik if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0) 4157349fcda4SWarner Losh goto out2; 4158349fcda4SWarner Losh sbuf_printf(sb, "comm=\""); 4159349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 4160842ab62bSRui Paulo free(freepath, M_TEMP); 4161349fcda4SWarner Losh sbuf_printf(sb, "\" core=\""); 4162349fcda4SWarner Losh 4163349fcda4SWarner Losh /* 4164349fcda4SWarner Losh * We can't lookup core file vp directly. When we're replacing a core, and 4165349fcda4SWarner Losh * other random times, we flush the name cache, so it will fail. Instead, 4166349fcda4SWarner Losh * if the path of the core is relative, add the current dir in front if it. 4167349fcda4SWarner Losh */ 4168349fcda4SWarner Losh if (name[0] != '/') { 41697739d927SMateusz Guzik fullpathsize = MAXPATHLEN; 41707739d927SMateusz Guzik freepath = malloc(fullpathsize, M_TEMP, M_WAITOK); 4171feabaaf9SMateusz Guzik if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) { 41727739d927SMateusz Guzik free(freepath, M_TEMP); 4173349fcda4SWarner Losh goto out2; 4174349fcda4SWarner Losh } 4175349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 41767739d927SMateusz Guzik free(freepath, M_TEMP); 4177349fcda4SWarner Losh sbuf_putc(sb, '/'); 4178349fcda4SWarner Losh } 4179349fcda4SWarner Losh devctl_safe_quote_sb(sb, name); 4180349fcda4SWarner Losh sbuf_printf(sb, "\""); 4181349fcda4SWarner Losh if (sbuf_finish(sb) == 0) 4182349fcda4SWarner Losh devctl_notify("kernel", "signal", "coredump", sbuf_data(sb)); 4183349fcda4SWarner Losh out2: 4184349fcda4SWarner Losh sbuf_delete(sb); 4185842ab62bSRui Paulo out: 4186dacbc9dbSKonstantin Belousov error1 = vn_close(vp, FWRITE, cred, td); 4187dacbc9dbSKonstantin Belousov if (error == 0) 4188dacbc9dbSKonstantin Belousov error = error1; 418957274c51SChristian S.J. Peron #ifdef AUDIT 419057274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 419157274c51SChristian S.J. Peron #endif 419257274c51SChristian S.J. Peron free(name, M_TEMP); 4193fca666a1SJulian Elischer return (error); 4194fca666a1SJulian Elischer } 4195fca666a1SJulian Elischer 4196fca666a1SJulian Elischer /* 41970c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 41980c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 4199df8bae1dSRodney W. Grimes */ 4200d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 4201df8bae1dSRodney W. Grimes struct nosys_args { 4202df8bae1dSRodney W. Grimes int dummy; 4203df8bae1dSRodney W. Grimes }; 4204d2d3e875SBruce Evans #endif 4205df8bae1dSRodney W. Grimes /* ARGSUSED */ 420626f9a767SRodney W. Grimes int 4207e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args) 4208df8bae1dSRodney W. Grimes { 4209f5a077c3SKonstantin Belousov struct proc *p; 4210f5a077c3SKonstantin Belousov 4211f5a077c3SKonstantin Belousov p = td->td_proc; 4212b40ce416SJulian Elischer 4213628d2653SJohn Baldwin PROC_LOCK(p); 4214888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 4215628d2653SJohn Baldwin PROC_UNLOCK(p); 42167ceeb35bSKonstantin Belousov if (kern_lognosys == 1 || kern_lognosys == 3) { 4217f5a077c3SKonstantin Belousov uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 4218f5a077c3SKonstantin Belousov td->td_sa.code); 42197ceeb35bSKonstantin Belousov } 422018f917a9SBrooks Davis if (kern_lognosys == 2 || kern_lognosys == 3 || 422118f917a9SBrooks Davis (p->p_pid == 1 && (kern_lognosys & 3) == 0)) { 42227ceeb35bSKonstantin Belousov printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 42237ceeb35bSKonstantin Belousov td->td_sa.code); 42247ceeb35bSKonstantin Belousov } 4225f5216b9aSBruce Evans return (ENOSYS); 4226df8bae1dSRodney W. Grimes } 4227831d27a9SDon Lewis 4228831d27a9SDon Lewis /* 42290c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 42300c14ff0eSRobert Watson * credentials rather than those of the current process. 4231831d27a9SDon Lewis */ 4232831d27a9SDon Lewis void 4233e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty) 4234831d27a9SDon Lewis { 4235a3de221dSKonstantin Belousov ksiginfo_t ksi; 4236f1320723SAlfred Perlstein struct sigio *sigio; 4237831d27a9SDon Lewis 4238a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 4239a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 4240a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 4241a3de221dSKonstantin Belousov 4242f1320723SAlfred Perlstein SIGIO_LOCK(); 4243f1320723SAlfred Perlstein sigio = *sigiop; 4244f1320723SAlfred Perlstein if (sigio == NULL) { 4245f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4246f1320723SAlfred Perlstein return; 4247f1320723SAlfred Perlstein } 4248831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 4249628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 42502b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 42518451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 4252628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 4253831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 4254831d27a9SDon Lewis struct proc *p; 4255831d27a9SDon Lewis 4256f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 4257628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 4258628d2653SJohn Baldwin PROC_LOCK(p); 4259e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 4260e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 4261831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 42628451d0ddSKip Macy kern_psignal(p, sig); 4263628d2653SJohn Baldwin PROC_UNLOCK(p); 4264628d2653SJohn Baldwin } 4265f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 4266831d27a9SDon Lewis } 4267f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4268831d27a9SDon Lewis } 4269cb679c38SJonathan Lemon 4270cb679c38SJonathan Lemon static int 4271cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 4272cb679c38SJonathan Lemon { 4273cb679c38SJonathan Lemon struct proc *p = curproc; 4274cb679c38SJonathan Lemon 4275cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 4276cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 4277cb679c38SJonathan Lemon 42789e590ff0SKonstantin Belousov knlist_add(p->p_klist, kn, 0); 4279cb679c38SJonathan Lemon 4280cb679c38SJonathan Lemon return (0); 4281cb679c38SJonathan Lemon } 4282cb679c38SJonathan Lemon 4283cb679c38SJonathan Lemon static void 4284cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 4285cb679c38SJonathan Lemon { 4286cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 4287cb679c38SJonathan Lemon 42889e590ff0SKonstantin Belousov knlist_remove(p->p_klist, kn, 0); 4289cb679c38SJonathan Lemon } 4290cb679c38SJonathan Lemon 4291cb679c38SJonathan Lemon /* 4292cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 4293cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 4294cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 4295cb679c38SJonathan Lemon * isn't worth the trouble. 4296cb679c38SJonathan Lemon */ 4297cb679c38SJonathan Lemon static int 4298cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 4299cb679c38SJonathan Lemon { 4300cb679c38SJonathan Lemon 4301cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 4302cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 4303cb679c38SJonathan Lemon 4304cb679c38SJonathan Lemon if (kn->kn_id == hint) 4305cb679c38SJonathan Lemon kn->kn_data++; 4306cb679c38SJonathan Lemon } 4307cb679c38SJonathan Lemon return (kn->kn_data != 0); 4308cb679c38SJonathan Lemon } 430990af4afaSJohn Baldwin 431090af4afaSJohn Baldwin struct sigacts * 431190af4afaSJohn Baldwin sigacts_alloc(void) 431290af4afaSJohn Baldwin { 431390af4afaSJohn Baldwin struct sigacts *ps; 431490af4afaSJohn Baldwin 431590af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 4316ce8daaadSMateusz Guzik refcount_init(&ps->ps_refcnt, 1); 431790af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 431890af4afaSJohn Baldwin return (ps); 431990af4afaSJohn Baldwin } 432090af4afaSJohn Baldwin 432190af4afaSJohn Baldwin void 432290af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 432390af4afaSJohn Baldwin { 432490af4afaSJohn Baldwin 4325c959c237SMateusz Guzik if (refcount_release(&ps->ps_refcnt) == 0) 4326c959c237SMateusz Guzik return; 432790af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 432890af4afaSJohn Baldwin free(ps, M_SUBPROC); 432990af4afaSJohn Baldwin } 433090af4afaSJohn Baldwin 433190af4afaSJohn Baldwin struct sigacts * 433290af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 433390af4afaSJohn Baldwin { 4334c959c237SMateusz Guzik 4335c959c237SMateusz Guzik refcount_acquire(&ps->ps_refcnt); 433690af4afaSJohn Baldwin return (ps); 433790af4afaSJohn Baldwin } 433890af4afaSJohn Baldwin 433990af4afaSJohn Baldwin void 434090af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 434190af4afaSJohn Baldwin { 434290af4afaSJohn Baldwin 434390af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 434490af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 434590af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 434690af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 434790af4afaSJohn Baldwin } 434890af4afaSJohn Baldwin 434990af4afaSJohn Baldwin int 435090af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 435190af4afaSJohn Baldwin { 435290af4afaSJohn Baldwin 4353d00c8ea4SMateusz Guzik return (ps->ps_refcnt > 1); 435490af4afaSJohn Baldwin } 4355079c5b9eSKyle Evans 4356079c5b9eSKyle Evans void 4357079c5b9eSKyle Evans sig_drop_caught(struct proc *p) 4358079c5b9eSKyle Evans { 4359079c5b9eSKyle Evans int sig; 4360079c5b9eSKyle Evans struct sigacts *ps; 4361079c5b9eSKyle Evans 4362079c5b9eSKyle Evans ps = p->p_sigacts; 4363079c5b9eSKyle Evans PROC_LOCK_ASSERT(p, MA_OWNED); 4364079c5b9eSKyle Evans mtx_assert(&ps->ps_mtx, MA_OWNED); 436581f2e906SMark Johnston SIG_FOREACH(sig, &ps->ps_sigcatch) { 4366079c5b9eSKyle Evans sigdflt(ps, sig); 4367079c5b9eSKyle Evans if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 4368079c5b9eSKyle Evans sigqueue_delete_proc(p, sig); 4369079c5b9eSKyle Evans } 4370079c5b9eSKyle Evans } 4371146fc63fSKonstantin Belousov 4372a113b17fSKonstantin Belousov static void 4373a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write) 4374a113b17fSKonstantin Belousov { 4375a113b17fSKonstantin Belousov ksiginfo_t ksi; 4376a113b17fSKonstantin Belousov 4377a113b17fSKonstantin Belousov /* 4378a113b17fSKonstantin Belousov * Prevent further fetches and SIGSEGVs, allowing thread to 4379a113b17fSKonstantin Belousov * issue syscalls despite corruption. 4380a113b17fSKonstantin Belousov */ 4381a113b17fSKonstantin Belousov sigfastblock_clear(td); 4382a113b17fSKonstantin Belousov 4383a113b17fSKonstantin Belousov if (!sendsig) 4384a113b17fSKonstantin Belousov return; 4385a113b17fSKonstantin Belousov ksiginfo_init_trap(&ksi); 4386a113b17fSKonstantin Belousov ksi.ksi_signo = SIGSEGV; 4387a113b17fSKonstantin Belousov ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR; 4388a113b17fSKonstantin Belousov ksi.ksi_addr = td->td_sigblock_ptr; 4389a113b17fSKonstantin Belousov trapsignal(td, &ksi); 4390a113b17fSKonstantin Belousov } 4391a113b17fSKonstantin Belousov 4392a113b17fSKonstantin Belousov static bool 4393a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp) 4394a113b17fSKonstantin Belousov { 4395a113b17fSKonstantin Belousov uint32_t res; 4396a113b17fSKonstantin Belousov 4397a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4398a113b17fSKonstantin Belousov return (true); 4399a113b17fSKonstantin Belousov if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) { 4400a113b17fSKonstantin Belousov sigfastblock_failed(td, sendsig, false); 4401a113b17fSKonstantin Belousov return (false); 4402a113b17fSKonstantin Belousov } 4403a113b17fSKonstantin Belousov *valp = res; 4404a113b17fSKonstantin Belousov td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS; 4405a113b17fSKonstantin Belousov return (true); 4406a113b17fSKonstantin Belousov } 4407a113b17fSKonstantin Belousov 4408fb3c434bSKonstantin Belousov static void 4409fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched) 4410fb3c434bSKonstantin Belousov { 4411fb3c434bSKonstantin Belousov struct proc *p; 4412fb3c434bSKonstantin Belousov 4413fb3c434bSKonstantin Belousov if (resched) { 4414fb3c434bSKonstantin Belousov p = td->td_proc; 4415fb3c434bSKonstantin Belousov PROC_LOCK(p); 4416fb3c434bSKonstantin Belousov reschedule_signals(p, td->td_sigmask, 0); 4417fb3c434bSKonstantin Belousov PROC_UNLOCK(p); 4418fb3c434bSKonstantin Belousov } 4419c6d31b83SKonstantin Belousov ast_sched(td, TDA_SIG); 4420fb3c434bSKonstantin Belousov } 4421fb3c434bSKonstantin Belousov 4422146fc63fSKonstantin Belousov int 4423146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap) 4424146fc63fSKonstantin Belousov { 4425146fc63fSKonstantin Belousov struct proc *p; 4426146fc63fSKonstantin Belousov int error, res; 4427146fc63fSKonstantin Belousov uint32_t oldval; 4428146fc63fSKonstantin Belousov 4429146fc63fSKonstantin Belousov error = 0; 4430a113b17fSKonstantin Belousov p = td->td_proc; 4431146fc63fSKonstantin Belousov switch (uap->cmd) { 4432146fc63fSKonstantin Belousov case SIGFASTBLOCK_SETPTR: 4433146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 4434146fc63fSKonstantin Belousov error = EBUSY; 4435146fc63fSKonstantin Belousov break; 4436146fc63fSKonstantin Belousov } 4437146fc63fSKonstantin Belousov if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) { 4438146fc63fSKonstantin Belousov error = EINVAL; 4439146fc63fSKonstantin Belousov break; 4440146fc63fSKonstantin Belousov } 4441146fc63fSKonstantin Belousov td->td_pflags |= TDP_SIGFASTBLOCK; 4442146fc63fSKonstantin Belousov td->td_sigblock_ptr = uap->ptr; 4443146fc63fSKonstantin Belousov break; 4444146fc63fSKonstantin Belousov 4445146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNBLOCK: 4446a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4447146fc63fSKonstantin Belousov error = EINVAL; 4448146fc63fSKonstantin Belousov break; 4449146fc63fSKonstantin Belousov } 4450a113b17fSKonstantin Belousov 4451a113b17fSKonstantin Belousov for (;;) { 4452a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, 4453a113b17fSKonstantin Belousov SIGFASTBLOCK_PEND, &oldval, 0); 4454146fc63fSKonstantin Belousov if (res == -1) { 4455146fc63fSKonstantin Belousov error = EFAULT; 4456a113b17fSKonstantin Belousov sigfastblock_failed(td, false, true); 4457146fc63fSKonstantin Belousov break; 4458146fc63fSKonstantin Belousov } 4459a113b17fSKonstantin Belousov if (res == 0) 4460a113b17fSKonstantin Belousov break; 4461a113b17fSKonstantin Belousov MPASS(res == 1); 4462146fc63fSKonstantin Belousov if (oldval != SIGFASTBLOCK_PEND) { 4463146fc63fSKonstantin Belousov error = EBUSY; 4464146fc63fSKonstantin Belousov break; 4465146fc63fSKonstantin Belousov } 4466146fc63fSKonstantin Belousov error = thread_check_susp(td, false); 4467146fc63fSKonstantin Belousov if (error != 0) 4468146fc63fSKonstantin Belousov break; 4469146fc63fSKonstantin Belousov } 4470a113b17fSKonstantin Belousov if (error != 0) 4471a113b17fSKonstantin Belousov break; 4472a113b17fSKonstantin Belousov 4473a113b17fSKonstantin Belousov /* 4474a113b17fSKonstantin Belousov * td_sigblock_val is cleared there, but not on a 4475a113b17fSKonstantin Belousov * syscall exit. The end effect is that a single 4476a113b17fSKonstantin Belousov * interruptible sleep, while user sigblock word is 4477a113b17fSKonstantin Belousov * set, might return EINTR or ERESTART to usermode 4478a113b17fSKonstantin Belousov * without delivering signal. All further sleeps, 4479a113b17fSKonstantin Belousov * until userspace clears the word and does 4480a113b17fSKonstantin Belousov * sigfastblock(UNBLOCK), observe current word and no 4481a113b17fSKonstantin Belousov * longer get interrupted. It is slight 4482a113b17fSKonstantin Belousov * non-conformance, with alternative to have read the 4483a113b17fSKonstantin Belousov * sigblock word on each syscall entry. 4484a113b17fSKonstantin Belousov */ 4485146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 4486146fc63fSKonstantin Belousov 4487146fc63fSKonstantin Belousov /* 4488146fc63fSKonstantin Belousov * Rely on normal ast mechanism to deliver pending 4489146fc63fSKonstantin Belousov * signals to current thread. But notify others about 4490146fc63fSKonstantin Belousov * fake unblock. 4491146fc63fSKonstantin Belousov */ 4492fb3c434bSKonstantin Belousov sigfastblock_resched(td, error == 0 && p->p_numthreads != 1); 4493fb3c434bSKonstantin Belousov 4494146fc63fSKonstantin Belousov break; 4495146fc63fSKonstantin Belousov 4496146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNSETPTR: 4497146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4498146fc63fSKonstantin Belousov error = EINVAL; 4499146fc63fSKonstantin Belousov break; 4500146fc63fSKonstantin Belousov } 4501a113b17fSKonstantin Belousov if (!sigfastblock_fetch_sig(td, false, &oldval)) { 4502146fc63fSKonstantin Belousov error = EFAULT; 4503146fc63fSKonstantin Belousov break; 4504146fc63fSKonstantin Belousov } 4505146fc63fSKonstantin Belousov if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) { 4506146fc63fSKonstantin Belousov error = EBUSY; 4507146fc63fSKonstantin Belousov break; 4508146fc63fSKonstantin Belousov } 4509a113b17fSKonstantin Belousov sigfastblock_clear(td); 4510146fc63fSKonstantin Belousov break; 4511146fc63fSKonstantin Belousov 4512146fc63fSKonstantin Belousov default: 4513146fc63fSKonstantin Belousov error = EINVAL; 4514146fc63fSKonstantin Belousov break; 4515146fc63fSKonstantin Belousov } 4516146fc63fSKonstantin Belousov return (error); 4517146fc63fSKonstantin Belousov } 4518146fc63fSKonstantin Belousov 4519146fc63fSKonstantin Belousov void 4520a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td) 4521146fc63fSKonstantin Belousov { 4522a113b17fSKonstantin Belousov bool resched; 4523146fc63fSKonstantin Belousov 4524146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4525146fc63fSKonstantin Belousov return; 4526a113b17fSKonstantin Belousov td->td_sigblock_val = 0; 452700ebd809SKonstantin Belousov resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 || 452800ebd809SKonstantin Belousov SIGPENDING(td); 4529a113b17fSKonstantin Belousov td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING); 4530fb3c434bSKonstantin Belousov sigfastblock_resched(td, resched); 4531146fc63fSKonstantin Belousov } 4532146fc63fSKonstantin Belousov 4533146fc63fSKonstantin Belousov void 4534a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td) 4535146fc63fSKonstantin Belousov { 4536a113b17fSKonstantin Belousov uint32_t val; 4537146fc63fSKonstantin Belousov 4538a113b17fSKonstantin Belousov (void)sigfastblock_fetch_sig(td, true, &val); 4539a113b17fSKonstantin Belousov } 4540146fc63fSKonstantin Belousov 45410bc52b0bSKonstantin Belousov static void 45420bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td) 4543a113b17fSKonstantin Belousov { 4544a113b17fSKonstantin Belousov int res; 4545a113b17fSKonstantin Belousov uint32_t oldval; 4546a113b17fSKonstantin Belousov 4547513320c0SKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTPENDING) == 0) 4548a113b17fSKonstantin Belousov return; 4549a113b17fSKonstantin Belousov res = fueword32((void *)td->td_sigblock_ptr, &oldval); 4550a113b17fSKonstantin Belousov if (res == -1) { 4551a113b17fSKonstantin Belousov sigfastblock_failed(td, true, false); 4552a113b17fSKonstantin Belousov return; 4553a113b17fSKonstantin Belousov } 4554a113b17fSKonstantin Belousov for (;;) { 4555a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, oldval, &oldval, 4556a113b17fSKonstantin Belousov oldval | SIGFASTBLOCK_PEND); 4557a113b17fSKonstantin Belousov if (res == -1) { 4558a113b17fSKonstantin Belousov sigfastblock_failed(td, true, true); 4559a113b17fSKonstantin Belousov return; 4560a113b17fSKonstantin Belousov } 4561a113b17fSKonstantin Belousov if (res == 0) { 4562a113b17fSKonstantin Belousov td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS; 4563a113b17fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTPENDING; 4564a113b17fSKonstantin Belousov break; 4565a113b17fSKonstantin Belousov } 4566a113b17fSKonstantin Belousov MPASS(res == 1); 4567a113b17fSKonstantin Belousov if (thread_check_susp(td, false) != 0) 4568a113b17fSKonstantin Belousov break; 4569a113b17fSKonstantin Belousov } 4570146fc63fSKonstantin Belousov } 45710bc52b0bSKonstantin Belousov 45724fced864SKonstantin Belousov static void 45730bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched) 45740bc52b0bSKonstantin Belousov { 45750bc52b0bSKonstantin Belousov struct proc *p; 45760bc52b0bSKonstantin Belousov 45770bc52b0bSKonstantin Belousov sigfastblock_setpend1(td); 45780bc52b0bSKonstantin Belousov if (resched) { 45790bc52b0bSKonstantin Belousov p = td->td_proc; 45800bc52b0bSKonstantin Belousov PROC_LOCK(p); 45810bc52b0bSKonstantin Belousov reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK); 45820bc52b0bSKonstantin Belousov PROC_UNLOCK(p); 45830bc52b0bSKonstantin Belousov } 45840bc52b0bSKonstantin Belousov } 4585