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 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45eb6368d4SRui Paulo #include <sys/ctype.h> 4636240ea5SDoug Rabson #include <sys/systm.h> 47df8bae1dSRodney W. Grimes #include <sys/signalvar.h> 48df8bae1dSRodney W. Grimes #include <sys/vnode.h> 49df8bae1dSRodney W. Grimes #include <sys/acct.h> 504a144410SRobert Watson #include <sys/capsicum.h> 5178f57a9cSMark Johnston #include <sys/compressor.h> 52238510fcSJason Evans #include <sys/condvar.h> 53773e541eSWarner Losh #include <sys/devctl.h> 54854dc8c2SJohn Baldwin #include <sys/event.h> 55854dc8c2SJohn Baldwin #include <sys/fcntl.h> 56e7228204SAlfred Perlstein #include <sys/imgact.h> 57854dc8c2SJohn Baldwin #include <sys/kernel.h> 580384fff8SJason Evans #include <sys/ktr.h> 59df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 6091898857SMark Johnston #include <sys/limits.h> 61854dc8c2SJohn Baldwin #include <sys/lock.h> 62854dc8c2SJohn Baldwin #include <sys/malloc.h> 63854dc8c2SJohn Baldwin #include <sys/mutex.h> 64c959c237SMateusz Guzik #include <sys/refcount.h> 65854dc8c2SJohn Baldwin #include <sys/namei.h> 66854dc8c2SJohn Baldwin #include <sys/proc.h> 67cfb5f768SJonathan Anderson #include <sys/procdesc.h> 68ef401a85SKonstantin Belousov #include <sys/ptrace.h> 696aeb05d7STom Rhodes #include <sys/posix4.h> 70b8fdb0d9SEdward Tomasz Napierala #include <sys/racct.h> 71c31146a1SJohn Baldwin #include <sys/resourcevar.h> 725d217f17SJohn Birrell #include <sys/sdt.h> 7336b208e0SRobert Watson #include <sys/sbuf.h> 7444f3b092SJohn Baldwin #include <sys/sleepqueue.h> 756caa8a15SJohn Baldwin #include <sys/smp.h> 76df8bae1dSRodney W. Grimes #include <sys/stat.h> 771005a129SJohn Baldwin #include <sys/sx.h> 788f19eb88SIan Dowse #include <sys/syscallsubr.h> 79c87e2930SDavid Greenman #include <sys/sysctl.h> 80854dc8c2SJohn Baldwin #include <sys/sysent.h> 81854dc8c2SJohn Baldwin #include <sys/syslog.h> 82854dc8c2SJohn Baldwin #include <sys/sysproto.h> 8356c06c4bSDavid Xu #include <sys/timers.h> 8406ae1e91SMatthew Dillon #include <sys/unistd.h> 85854dc8c2SJohn Baldwin #include <sys/wait.h> 869104847fSDavid Xu #include <vm/vm.h> 879104847fSDavid Xu #include <vm/vm_extern.h> 889104847fSDavid Xu #include <vm/uma.h> 89df8bae1dSRodney W. Grimes 90e7228204SAlfred Perlstein #include <sys/jail.h> 91e7228204SAlfred Perlstein 92df8bae1dSRodney W. Grimes #include <machine/cpu.h> 93df8bae1dSRodney W. Grimes 94bfd7575aSWayne Salamon #include <security/audit/audit.h> 95bfd7575aSWayne Salamon 966f841fb7SMarcel Moolenaar #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 976f841fb7SMarcel Moolenaar 985d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 9936160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__send, 10036160958SMark Johnston "struct thread *", "struct proc *", "int"); 10136160958SMark Johnston SDT_PROBE_DEFINE2(proc, , , signal__clear, 10236160958SMark Johnston "int", "ksiginfo_t *"); 10336160958SMark Johnston SDT_PROBE_DEFINE3(proc, , , signal__discard, 1047b77e1feSMark Johnston "struct thread *", "struct proc *", "int"); 1055d217f17SJohn Birrell 1064d77a549SAlfred Perlstein static int coredump(struct thread *); 107a3de221dSKonstantin Belousov static int killpg1(struct thread *td, int sig, int pgid, int all, 108a3de221dSKonstantin Belousov ksiginfo_t *ksi); 1093cf3b9f0SJohn Baldwin static int issignal(struct thread *td); 1100bc52b0bSKonstantin Belousov static void reschedule_signals(struct proc *p, sigset_t block, int flags); 1114d77a549SAlfred Perlstein static int sigprop(int sig); 11294f0972bSDavid Xu static void tdsigwakeup(struct thread *, int, sig_t, int); 1136f56cb8dSKonstantin Belousov static int sig_suspend_threads(struct thread *, struct proc *, int); 114cb679c38SJonathan Lemon static int filt_sigattach(struct knote *kn); 115cb679c38SJonathan Lemon static void filt_sigdetach(struct knote *kn); 116cb679c38SJonathan Lemon static int filt_signal(struct knote *kn, long hint); 117146fc63fSKonstantin Belousov static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock); 1189104847fSDavid Xu static void sigqueue_start(void); 119cb679c38SJonathan Lemon 1209104847fSDavid Xu static uma_zone_t ksiginfo_zone = NULL; 121e76d823bSRobert Watson struct filterops sig_filtops = { 122e76d823bSRobert Watson .f_isfd = 0, 123e76d823bSRobert Watson .f_attach = filt_sigattach, 124e76d823bSRobert Watson .f_detach = filt_sigdetach, 125e76d823bSRobert Watson .f_event = filt_signal, 126e76d823bSRobert Watson }; 127cb679c38SJonathan Lemon 128fc8cca02SJohn Baldwin static int kern_logsigexit = 1; 1293d177f46SBill Fumerola SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 1303d177f46SBill Fumerola &kern_logsigexit, 0, 1313d177f46SBill Fumerola "Log processes quitting on abnormal signals to syslog(3)"); 13257308494SJoerg Wunsch 133f71a882fSDavid Xu static int kern_forcesigexit = 1; 134f71a882fSDavid Xu SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 135f71a882fSDavid Xu &kern_forcesigexit, 0, "Force trap signal to be handled"); 136f71a882fSDavid Xu 1377029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1386472ac3dSEd Schouten "POSIX real time signal"); 1399104847fSDavid Xu 1409104847fSDavid Xu static int max_pending_per_proc = 128; 1419104847fSDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 1429104847fSDavid Xu &max_pending_per_proc, 0, "Max pending signals per proc"); 1439104847fSDavid Xu 1449104847fSDavid Xu static int preallocate_siginfo = 1024; 145af3b2549SHans Petter Selasky SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN, 1469104847fSDavid Xu &preallocate_siginfo, 0, "Preallocated signal memory size"); 1479104847fSDavid Xu 1489104847fSDavid Xu static int signal_overflow = 0; 149761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 1509104847fSDavid Xu &signal_overflow, 0, "Number of signals overflew"); 1519104847fSDavid Xu 1529104847fSDavid Xu static int signal_alloc_fail = 0; 153761a4d94SDavid Xu SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 1549104847fSDavid Xu &signal_alloc_fail, 0, "signals failed to be allocated"); 1559104847fSDavid Xu 156f5a077c3SKonstantin Belousov static int kern_lognosys = 0; 157f5a077c3SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0, 158f5a077c3SKonstantin Belousov "Log invalid syscalls"); 159f5a077c3SKonstantin Belousov 160a113b17fSKonstantin Belousov __read_frequently bool sigfastblock_fetch_always = false; 161a113b17fSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN, 162a113b17fSKonstantin Belousov &sigfastblock_fetch_always, 0, 163a113b17fSKonstantin Belousov "Fetch sigfastblock word on each syscall entry for proper " 164a113b17fSKonstantin Belousov "blocking semantic"); 165a113b17fSKonstantin Belousov 166bc387624SKonstantin Belousov static bool kern_sig_discard_ign = true; 167bc387624SKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN, 168bc387624SKonstantin Belousov &kern_sig_discard_ign, 0, 169bc387624SKonstantin Belousov "Discard ignored signals on delivery, otherwise queue them to " 170bc387624SKonstantin Belousov "the target queue"); 171bc387624SKonstantin Belousov 1729104847fSDavid Xu SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 1739104847fSDavid Xu 1742b87b6d4SRobert Watson /* 1752b87b6d4SRobert Watson * Policy -- Can ucred cr1 send SIGIO to process cr2? 1762b87b6d4SRobert Watson * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 1772b87b6d4SRobert Watson * in the right situations. 1782b87b6d4SRobert Watson */ 1792b87b6d4SRobert Watson #define CANSIGIO(cr1, cr2) \ 1802b87b6d4SRobert Watson ((cr1)->cr_uid == 0 || \ 1812b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_ruid || \ 1822b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_ruid || \ 1832b87b6d4SRobert Watson (cr1)->cr_ruid == (cr2)->cr_uid || \ 1842b87b6d4SRobert Watson (cr1)->cr_uid == (cr2)->cr_uid) 1852b87b6d4SRobert Watson 186fc8cca02SJohn Baldwin static int sugid_coredump; 187af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN, 1888b5adf9dSJoseph Koshy &sugid_coredump, 0, "Allow setuid and setgid processes to dump core"); 189c87e2930SDavid Greenman 190b0c9d4d7SPawel Jakub Dawidek static int capmode_coredump; 191af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN, 192b0c9d4d7SPawel Jakub Dawidek &capmode_coredump, 0, "Allow processes in capability mode to dump core"); 193b0c9d4d7SPawel Jakub Dawidek 194e5a28db9SPaul Saab static int do_coredump = 1; 195e5a28db9SPaul Saab SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 196e5a28db9SPaul Saab &do_coredump, 0, "Enable/Disable coredumps"); 197e5a28db9SPaul Saab 1986141e04aSJohn-Mark Gurney static int set_core_nodump_flag = 0; 1996141e04aSJohn-Mark Gurney SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 2006141e04aSJohn-Mark Gurney 0, "Enable setting the NODUMP flag on coredump files"); 2016141e04aSJohn-Mark Gurney 2020da9e11bSRui Paulo static int coredump_devctl = 0; 203eb6368d4SRui Paulo SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl, 204eb6368d4SRui Paulo 0, "Generate a devctl notification when processes coredump"); 205eb6368d4SRui Paulo 2062c42a146SMarcel Moolenaar /* 2072c42a146SMarcel Moolenaar * Signal properties and actions. 2082c42a146SMarcel Moolenaar * The array below categorizes the signals and their default actions 2092c42a146SMarcel Moolenaar * according to the following properties: 2102c42a146SMarcel Moolenaar */ 211fd50a707SBrooks Davis #define SIGPROP_KILL 0x01 /* terminates process by default */ 212fd50a707SBrooks Davis #define SIGPROP_CORE 0x02 /* ditto and coredumps */ 213fd50a707SBrooks Davis #define SIGPROP_STOP 0x04 /* suspend process */ 214fd50a707SBrooks Davis #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */ 215fd50a707SBrooks Davis #define SIGPROP_IGNORE 0x10 /* ignore by default */ 216fd50a707SBrooks Davis #define SIGPROP_CONT 0x20 /* continue if suspended */ 217df8bae1dSRodney W. Grimes 2182c42a146SMarcel Moolenaar static int sigproptbl[NSIG] = { 219ed6d876bSBrooks Davis [SIGHUP] = SIGPROP_KILL, 220ed6d876bSBrooks Davis [SIGINT] = SIGPROP_KILL, 221ed6d876bSBrooks Davis [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE, 222ed6d876bSBrooks Davis [SIGILL] = SIGPROP_KILL | SIGPROP_CORE, 223ed6d876bSBrooks Davis [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE, 224ed6d876bSBrooks Davis [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE, 225ed6d876bSBrooks Davis [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE, 226ed6d876bSBrooks Davis [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE, 227ed6d876bSBrooks Davis [SIGKILL] = SIGPROP_KILL, 228ed6d876bSBrooks Davis [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE, 229ed6d876bSBrooks Davis [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE, 230ed6d876bSBrooks Davis [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE, 231ed6d876bSBrooks Davis [SIGPIPE] = SIGPROP_KILL, 232ed6d876bSBrooks Davis [SIGALRM] = SIGPROP_KILL, 233ed6d876bSBrooks Davis [SIGTERM] = SIGPROP_KILL, 234ed6d876bSBrooks Davis [SIGURG] = SIGPROP_IGNORE, 235ed6d876bSBrooks Davis [SIGSTOP] = SIGPROP_STOP, 236ed6d876bSBrooks Davis [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP, 237ed6d876bSBrooks Davis [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT, 238ed6d876bSBrooks Davis [SIGCHLD] = SIGPROP_IGNORE, 239ed6d876bSBrooks Davis [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP, 240ed6d876bSBrooks Davis [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP, 241ed6d876bSBrooks Davis [SIGIO] = SIGPROP_IGNORE, 242ed6d876bSBrooks Davis [SIGXCPU] = SIGPROP_KILL, 243ed6d876bSBrooks Davis [SIGXFSZ] = SIGPROP_KILL, 244ed6d876bSBrooks Davis [SIGVTALRM] = SIGPROP_KILL, 245ed6d876bSBrooks Davis [SIGPROF] = SIGPROP_KILL, 246ed6d876bSBrooks Davis [SIGWINCH] = SIGPROP_IGNORE, 247ed6d876bSBrooks Davis [SIGINFO] = SIGPROP_IGNORE, 248ed6d876bSBrooks Davis [SIGUSR1] = SIGPROP_KILL, 249ed6d876bSBrooks Davis [SIGUSR2] = SIGPROP_KILL, 2502c42a146SMarcel Moolenaar }; 2512c42a146SMarcel Moolenaar 25281f2e906SMark Johnston #define _SIG_FOREACH_ADVANCE(i, set) ({ \ 25381f2e906SMark Johnston int __found; \ 25481f2e906SMark Johnston for (;;) { \ 25581f2e906SMark Johnston if (__bits != 0) { \ 25681f2e906SMark Johnston int __sig = ffs(__bits); \ 25781f2e906SMark Johnston __bits &= ~(1u << (__sig - 1)); \ 25881f2e906SMark Johnston sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \ 25981f2e906SMark Johnston __found = 1; \ 26081f2e906SMark Johnston break; \ 26181f2e906SMark Johnston } \ 26281f2e906SMark Johnston if (++__i == _SIG_WORDS) { \ 26381f2e906SMark Johnston __found = 0; \ 26481f2e906SMark Johnston break; \ 26581f2e906SMark Johnston } \ 26681f2e906SMark Johnston __bits = (set)->__bits[__i]; \ 26781f2e906SMark Johnston } \ 26881f2e906SMark Johnston __found != 0; \ 26981f2e906SMark Johnston }) 27081f2e906SMark Johnston 27181f2e906SMark Johnston #define SIG_FOREACH(i, set) \ 27281f2e906SMark Johnston for (int32_t __i = -1, __bits = 0; \ 27381f2e906SMark Johnston _SIG_FOREACH_ADVANCE(i, set); ) \ 27481f2e906SMark Johnston 275146fc63fSKonstantin Belousov sigset_t fastblock_mask; 2766b286ee8SKonstantin Belousov 2779104847fSDavid Xu static void 2789104847fSDavid Xu sigqueue_start(void) 2799104847fSDavid Xu { 2809104847fSDavid Xu ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 2819104847fSDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2829104847fSDavid Xu uma_prealloc(ksiginfo_zone, preallocate_siginfo); 283b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 284b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 285b51d237aSDavid Xu p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 286146fc63fSKonstantin Belousov SIGFILLSET(fastblock_mask); 287146fc63fSKonstantin Belousov SIG_CANTMASK(fastblock_mask); 2889104847fSDavid Xu } 2899104847fSDavid Xu 2905da49fcbSDavid Xu ksiginfo_t * 291ebceaf6dSDavid Xu ksiginfo_alloc(int wait) 2929104847fSDavid Xu { 293ebceaf6dSDavid Xu int flags; 294ebceaf6dSDavid Xu 295ebceaf6dSDavid Xu flags = M_ZERO; 296ebceaf6dSDavid Xu if (! wait) 297ebceaf6dSDavid Xu flags |= M_NOWAIT; 2989104847fSDavid Xu if (ksiginfo_zone != NULL) 299ebceaf6dSDavid Xu return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 3009104847fSDavid Xu return (NULL); 3019104847fSDavid Xu } 3029104847fSDavid Xu 3035da49fcbSDavid Xu void 3049104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 3059104847fSDavid Xu { 3069104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 3079104847fSDavid Xu } 3089104847fSDavid Xu 3095da49fcbSDavid Xu static __inline int 3105da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 3115da49fcbSDavid Xu { 3125da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT)) { 3135da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 3145da49fcbSDavid Xu return (1); 3155da49fcbSDavid Xu } 3165da49fcbSDavid Xu return (0); 3175da49fcbSDavid Xu } 3185da49fcbSDavid Xu 3199104847fSDavid Xu void 3209104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 3219104847fSDavid Xu { 3229104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 3233dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 32482a4538fSEric Badger SIGEMPTYSET(list->sq_ptrace); 3259104847fSDavid Xu TAILQ_INIT(&list->sq_list); 3269104847fSDavid Xu list->sq_proc = p; 3279104847fSDavid Xu list->sq_flags = SQ_INIT; 3289104847fSDavid Xu } 3299104847fSDavid Xu 3309104847fSDavid Xu /* 3319104847fSDavid Xu * Get a signal's ksiginfo. 3329104847fSDavid Xu * Return: 3339104847fSDavid Xu * 0 - signal not found 3349104847fSDavid Xu * others - signal number 3359104847fSDavid Xu */ 336a5799a4fSKonstantin Belousov static int 3379104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 3389104847fSDavid Xu { 3399104847fSDavid Xu struct proc *p = sq->sq_proc; 3409104847fSDavid Xu struct ksiginfo *ksi, *next; 3419104847fSDavid Xu int count = 0; 3429104847fSDavid Xu 3439104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3449104847fSDavid Xu 3459104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 3469104847fSDavid Xu return (0); 3479104847fSDavid Xu 34882a4538fSEric Badger if (SIGISMEMBER(sq->sq_ptrace, signo)) { 34982a4538fSEric Badger count++; 35082a4538fSEric Badger SIGDELSET(sq->sq_ptrace, signo); 35182a4538fSEric Badger si->ksi_flags |= KSI_PTRACE; 35282a4538fSEric Badger } 3533dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 3543dfcaad6SDavid Xu count++; 35582a4538fSEric Badger if (count == 1) 3563dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 3573dfcaad6SDavid Xu } 3583dfcaad6SDavid Xu 3595c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 3609104847fSDavid Xu if (ksi->ksi_signo == signo) { 3619104847fSDavid Xu if (count == 0) { 3629104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3635da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3649104847fSDavid Xu ksiginfo_copy(ksi, si); 3655da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 3669104847fSDavid Xu p->p_pendingcnt--; 3679104847fSDavid Xu } 368016fa302SDavid Xu if (++count > 1) 369016fa302SDavid Xu break; 3709104847fSDavid Xu } 3719104847fSDavid Xu } 3729104847fSDavid Xu 3739104847fSDavid Xu if (count <= 1) 3749104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 3759104847fSDavid Xu si->ksi_signo = signo; 3769104847fSDavid Xu return (signo); 3779104847fSDavid Xu } 3789104847fSDavid Xu 3795da49fcbSDavid Xu void 3805da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 3815da49fcbSDavid Xu { 3825da49fcbSDavid Xu struct ksiginfo *kp; 3835da49fcbSDavid Xu struct proc *p; 3845da49fcbSDavid Xu sigqueue_t *sq; 3855da49fcbSDavid Xu 386ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 3875da49fcbSDavid Xu return; 3885da49fcbSDavid Xu 3895da49fcbSDavid Xu p = sq->sq_proc; 3905da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3915da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3925da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 3935da49fcbSDavid Xu p->p_pendingcnt--; 3945da49fcbSDavid Xu 3955da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 3965da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 3975da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 3985da49fcbSDavid Xu break; 3995da49fcbSDavid Xu } 40082a4538fSEric Badger if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 40182a4538fSEric Badger !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 4025da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 4035da49fcbSDavid Xu } 4045da49fcbSDavid Xu 405a5799a4fSKonstantin Belousov static int 4069104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 4079104847fSDavid Xu { 4089104847fSDavid Xu struct proc *p = sq->sq_proc; 4099104847fSDavid Xu struct ksiginfo *ksi; 4109104847fSDavid Xu int ret = 0; 4119104847fSDavid Xu 4129104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4139104847fSDavid Xu 41482a4538fSEric Badger /* 41582a4538fSEric Badger * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 41682a4538fSEric Badger * for these signals. 41782a4538fSEric Badger */ 4183dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 4193dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4209104847fSDavid Xu goto out_set_bit; 4213dfcaad6SDavid Xu } 4229104847fSDavid Xu 4235da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 4245da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 4256a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4266a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 4276a671a6bSKonstantin Belousov else 4285da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 4295da49fcbSDavid Xu si->ksi_sigq = sq; 4305da49fcbSDavid Xu goto out_set_bit; 4315da49fcbSDavid Xu } 4325da49fcbSDavid Xu 4333dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 4343dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4359104847fSDavid Xu goto out_set_bit; 4363dfcaad6SDavid Xu } 4379104847fSDavid Xu 438ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 4399104847fSDavid Xu signal_overflow++; 4409104847fSDavid Xu ret = EAGAIN; 441ebceaf6dSDavid Xu } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 4429104847fSDavid Xu signal_alloc_fail++; 4439104847fSDavid Xu ret = EAGAIN; 4449104847fSDavid Xu } else { 4459104847fSDavid Xu if (p != NULL) 4469104847fSDavid Xu p->p_pendingcnt++; 4479104847fSDavid Xu ksiginfo_copy(si, ksi); 4489104847fSDavid Xu ksi->ksi_signo = signo; 4496a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4506a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 4516a671a6bSKonstantin Belousov else 4529104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 4535da49fcbSDavid Xu ksi->ksi_sigq = sq; 4549104847fSDavid Xu } 4559104847fSDavid Xu 45682a4538fSEric Badger if (ret != 0) { 45782a4538fSEric Badger if ((si->ksi_flags & KSI_PTRACE) != 0) { 45882a4538fSEric Badger SIGADDSET(sq->sq_ptrace, signo); 45982a4538fSEric Badger ret = 0; 46082a4538fSEric Badger goto out_set_bit; 46182a4538fSEric Badger } else if ((si->ksi_flags & KSI_TRAP) != 0 || 462a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 4633dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4649104847fSDavid Xu ret = 0; 4659104847fSDavid Xu goto out_set_bit; 4669104847fSDavid Xu } 4679104847fSDavid Xu return (ret); 46882a4538fSEric Badger } 4699104847fSDavid Xu 4709104847fSDavid Xu out_set_bit: 4719104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 4729104847fSDavid Xu return (ret); 4739104847fSDavid Xu } 4749104847fSDavid Xu 4759104847fSDavid Xu void 4769104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 4779104847fSDavid Xu { 4789104847fSDavid Xu struct proc *p = sq->sq_proc; 4799104847fSDavid Xu ksiginfo_t *ksi; 4809104847fSDavid Xu 4819104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4829104847fSDavid Xu 4835da49fcbSDavid Xu if (p != NULL) 4845da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 4855da49fcbSDavid Xu 4869104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 4879104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4885da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4895da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4909104847fSDavid Xu p->p_pendingcnt--; 4919104847fSDavid Xu } 4929104847fSDavid Xu 4939104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 4943dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 49582a4538fSEric Badger SIGEMPTYSET(sq->sq_ptrace); 4969104847fSDavid Xu } 4979104847fSDavid Xu 498a5799a4fSKonstantin Belousov static void 499fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 5009104847fSDavid Xu { 501fc4ecc1dSDavid Xu sigset_t tmp; 5029104847fSDavid Xu struct proc *p1, *p2; 5039104847fSDavid Xu ksiginfo_t *ksi, *next; 5049104847fSDavid Xu 5059104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5069104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 5079104847fSDavid Xu p1 = src->sq_proc; 5089104847fSDavid Xu p2 = dst->sq_proc; 5099104847fSDavid Xu /* Move siginfo to target list */ 5105c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 511fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5129104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 5139104847fSDavid Xu if (p1 != NULL) 5149104847fSDavid Xu p1->p_pendingcnt--; 5159104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 5165da49fcbSDavid Xu ksi->ksi_sigq = dst; 5179104847fSDavid Xu if (p2 != NULL) 5189104847fSDavid Xu p2->p_pendingcnt++; 5199104847fSDavid Xu } 5209104847fSDavid Xu } 5219104847fSDavid Xu 5229104847fSDavid Xu /* Move pending bits to target list */ 5233dfcaad6SDavid Xu tmp = src->sq_kill; 524fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 5253dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 5263dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 5273dfcaad6SDavid Xu 52882a4538fSEric Badger tmp = src->sq_ptrace; 52982a4538fSEric Badger SIGSETAND(tmp, *set); 53082a4538fSEric Badger SIGSETOR(dst->sq_ptrace, tmp); 53182a4538fSEric Badger SIGSETNAND(src->sq_ptrace, tmp); 53282a4538fSEric Badger 5339104847fSDavid Xu tmp = src->sq_signals; 534fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 5359104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 5369104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 5379104847fSDavid Xu } 5389104847fSDavid Xu 539407af02bSDavid Xu #if 0 540a5799a4fSKonstantin Belousov static void 5419104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 5429104847fSDavid Xu { 5439104847fSDavid Xu sigset_t set; 5449104847fSDavid Xu 5459104847fSDavid Xu SIGEMPTYSET(set); 5469104847fSDavid Xu SIGADDSET(set, signo); 5479104847fSDavid Xu sigqueue_move_set(src, dst, &set); 5489104847fSDavid Xu } 549407af02bSDavid Xu #endif 5509104847fSDavid Xu 551a5799a4fSKonstantin Belousov static void 552fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 5539104847fSDavid Xu { 5549104847fSDavid Xu struct proc *p = sq->sq_proc; 5559104847fSDavid Xu ksiginfo_t *ksi, *next; 5569104847fSDavid Xu 5579104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5589104847fSDavid Xu 5599104847fSDavid Xu /* Remove siginfo queue */ 5605c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 5619104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5629104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5635da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5645da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5659104847fSDavid Xu p->p_pendingcnt--; 5669104847fSDavid Xu } 5679104847fSDavid Xu } 5683dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 56982a4538fSEric Badger SIGSETNAND(sq->sq_ptrace, *set); 5709104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 5719104847fSDavid Xu } 5729104847fSDavid Xu 5739104847fSDavid Xu void 5749104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 5759104847fSDavid Xu { 5769104847fSDavid Xu sigset_t set; 5779104847fSDavid Xu 5789104847fSDavid Xu SIGEMPTYSET(set); 5799104847fSDavid Xu SIGADDSET(set, signo); 5809104847fSDavid Xu sigqueue_delete_set(sq, &set); 5819104847fSDavid Xu } 5829104847fSDavid Xu 5839104847fSDavid Xu /* Remove a set of signals for a process */ 584a5799a4fSKonstantin Belousov static void 585fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 5869104847fSDavid Xu { 5879104847fSDavid Xu sigqueue_t worklist; 5889104847fSDavid Xu struct thread *td0; 5899104847fSDavid Xu 5909104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5919104847fSDavid Xu 5929104847fSDavid Xu sigqueue_init(&worklist, NULL); 5939104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 5949104847fSDavid Xu 5959104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 5969104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 5979104847fSDavid Xu 5989104847fSDavid Xu sigqueue_flush(&worklist); 5999104847fSDavid Xu } 6009104847fSDavid Xu 6019104847fSDavid Xu void 6029104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 6039104847fSDavid Xu { 6049104847fSDavid Xu sigset_t set; 6059104847fSDavid Xu 6069104847fSDavid Xu SIGEMPTYSET(set); 6079104847fSDavid Xu SIGADDSET(set, signo); 6089104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 6099104847fSDavid Xu } 6109104847fSDavid Xu 611a5799a4fSKonstantin Belousov static void 6129104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 6139104847fSDavid Xu { 6149104847fSDavid Xu sigset_t set; 6159104847fSDavid Xu 6169104847fSDavid Xu SIGEMPTYSET(set); 6179104847fSDavid Xu SIGADDSET(set, SIGSTOP); 6189104847fSDavid Xu SIGADDSET(set, SIGTSTP); 6199104847fSDavid Xu SIGADDSET(set, SIGTTIN); 6209104847fSDavid Xu SIGADDSET(set, SIGTTOU); 6219104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 6229104847fSDavid Xu } 6239104847fSDavid Xu 624fbbeeb6cSBruce Evans /* 6251968f37bSJohn Baldwin * Determine signal that should be delivered to thread td, the current 6261968f37bSJohn Baldwin * thread, 0 if none. If there is a pending stop signal with default 627fbbeeb6cSBruce Evans * action, the process stops in issignal(). 628fbbeeb6cSBruce Evans */ 629fbbeeb6cSBruce Evans int 6303cf3b9f0SJohn Baldwin cursig(struct thread *td) 631fbbeeb6cSBruce Evans { 632c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 63390af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 634a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 6353cf3b9f0SJohn Baldwin return (SIGPENDING(td) ? issignal(td) : 0); 636fbbeeb6cSBruce Evans } 637fbbeeb6cSBruce Evans 63879065dbaSBruce Evans /* 63979065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 6409104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 6414093529dSJeff Roberson * unmasked in td_sigmask. 64279065dbaSBruce Evans */ 64379065dbaSBruce Evans void 6444093529dSJeff Roberson signotify(struct thread *td) 64579065dbaSBruce Evans { 6464093529dSJeff Roberson 647ddd4d15eSMatt Macy PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 6484093529dSJeff Roberson 6498b94a061SJohn Baldwin if (SIGPENDING(td)) { 650a54e85fdSJeff Roberson thread_lock(td); 6514093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 652a54e85fdSJeff Roberson thread_unlock(td); 65379065dbaSBruce Evans } 6548b94a061SJohn Baldwin } 6558b94a061SJohn Baldwin 656affd9185SKonstantin Belousov /* 657affd9185SKonstantin Belousov * Returns 1 (true) if altstack is configured for the thread, and the 658affd9185SKonstantin Belousov * passed stack bottom address falls into the altstack range. Handles 659affd9185SKonstantin Belousov * the 43 compat special case where the alt stack size is zero. 660affd9185SKonstantin Belousov */ 6618b94a061SJohn Baldwin int 6628b94a061SJohn Baldwin sigonstack(size_t sp) 6638b94a061SJohn Baldwin { 664affd9185SKonstantin Belousov struct thread *td; 6658b94a061SJohn Baldwin 666affd9185SKonstantin Belousov td = curthread; 667affd9185SKonstantin Belousov if ((td->td_pflags & TDP_ALTSTACK) == 0) 668affd9185SKonstantin Belousov return (0); 6691930e303SPoul-Henning Kamp #if defined(COMPAT_43) 6707e097daaSKonstantin Belousov if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0) 671affd9185SKonstantin Belousov return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0); 6728b94a061SJohn Baldwin #endif 673affd9185SKonstantin Belousov return (sp >= (size_t)td->td_sigstk.ss_sp && 674affd9185SKonstantin Belousov sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp); 6758b94a061SJohn Baldwin } 67679065dbaSBruce Evans 6776f841fb7SMarcel Moolenaar static __inline int 6786f841fb7SMarcel Moolenaar sigprop(int sig) 6792c42a146SMarcel Moolenaar { 6806f841fb7SMarcel Moolenaar 681ed6d876bSBrooks Davis if (sig > 0 && sig < nitems(sigproptbl)) 682ed6d876bSBrooks Davis return (sigproptbl[sig]); 6832c42a146SMarcel Moolenaar return (0); 684df8bae1dSRodney W. Grimes } 6852c42a146SMarcel Moolenaar 686350ae563SKonstantin Belousov static bool 687ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag) 688350ae563SKonstantin Belousov { 689350ae563SKonstantin Belousov 690c83655f3SKonstantin Belousov /* 691c83655f3SKonstantin Belousov * SA_SIGINFO is reset when signal disposition is set to 692c83655f3SKonstantin Belousov * ignore or default. Other flags are kept according to user 693c83655f3SKonstantin Belousov * settings. 694c83655f3SKonstantin Belousov */ 695c83655f3SKonstantin Belousov return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 696c83655f3SKonstantin Belousov ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 697c83655f3SKonstantin Belousov (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 698350ae563SKonstantin Belousov } 699350ae563SKonstantin Belousov 7002c42a146SMarcel Moolenaar /* 7018f19eb88SIan Dowse * kern_sigaction 7022c42a146SMarcel Moolenaar * sigaction 70323eeeff7SPeter Wemm * freebsd4_sigaction 7042c42a146SMarcel Moolenaar * osigaction 7052c42a146SMarcel Moolenaar */ 7068f19eb88SIan Dowse int 707ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 708ea566832SEd Schouten struct sigaction *oact, int flags) 709df8bae1dSRodney W. Grimes { 71090af4afaSJohn Baldwin struct sigacts *ps; 7118f19eb88SIan Dowse struct proc *p = td->td_proc; 712df8bae1dSRodney W. Grimes 7132899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 7142c42a146SMarcel Moolenaar return (EINVAL); 715271ab240SKonstantin Belousov if (act != NULL && act->sa_handler != SIG_DFL && 716271ab240SKonstantin Belousov act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 717271ab240SKonstantin Belousov SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 718271ab240SKonstantin Belousov SA_NOCLDWAIT | SA_SIGINFO)) != 0) 7192d864174SKonstantin Belousov return (EINVAL); 7202c42a146SMarcel Moolenaar 721628d2653SJohn Baldwin PROC_LOCK(p); 722628d2653SJohn Baldwin ps = p->p_sigacts; 72390af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 7242c42a146SMarcel Moolenaar if (oact) { 725fb441a88SKonstantin Belousov memset(oact, 0, sizeof(*oact)); 7262c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 7272c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 7282c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 7292c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 7302c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 7312c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 7322c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 7332c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 7342c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 73510c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 7362c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 73710c2b8e1SDavid E. O'Brien oact->sa_sigaction = 73810c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 73910c2b8e1SDavid E. O'Brien } else 74010c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 74190af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 7422c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 74390af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 7442c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 7452c42a146SMarcel Moolenaar } 7462c42a146SMarcel Moolenaar if (act) { 7472c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 748628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 74990af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 750628d2653SJohn Baldwin PROC_UNLOCK(p); 7512c42a146SMarcel Moolenaar return (EINVAL); 752628d2653SJohn Baldwin } 7532c42a146SMarcel Moolenaar 754df8bae1dSRodney W. Grimes /* 755df8bae1dSRodney W. Grimes * Change setting atomically. 756df8bae1dSRodney W. Grimes */ 7572c42a146SMarcel Moolenaar 7582c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 7592c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 760350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_SIGINFO)) { 761aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 762aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 76380f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 76480f42b55SIan Dowse } else { 76580f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 7662c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 7672c42a146SMarcel Moolenaar } 768350ae563SKonstantin Belousov if (!sigact_flag_test(act, SA_RESTART)) 7692c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 770df8bae1dSRodney W. Grimes else 7712c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 772350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_ONSTACK)) 7732c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 774df8bae1dSRodney W. Grimes else 7752c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 776350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_RESETHAND)) 7772c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 7781e41c1b5SSteven Wallace else 7792c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 780350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_NODEFER)) 7812c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 782289ccde0SPeter Wemm else 7832c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 7842c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 7852c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 78690af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 7876626c604SJulian Elischer else 78890af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 789ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 790245f17d4SJoerg Wunsch /* 7912c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 7922c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 7932c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 7942c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 795245f17d4SJoerg Wunsch */ 796245f17d4SJoerg Wunsch if (p->p_pid == 1) 79790af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 7986626c604SJulian Elischer else 79990af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 800245f17d4SJoerg Wunsch } else 80190af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 802ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 80390af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 804ba1551caSIan Dowse else 80590af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 806df8bae1dSRodney W. Grimes } 807df8bae1dSRodney W. Grimes /* 80890af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 8092c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 81090af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 8112c42a146SMarcel Moolenaar * have to restart the process. 812df8bae1dSRodney W. Grimes */ 8132c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 814fd50a707SBrooks Davis (sigprop(sig) & SIGPROP_IGNORE && 8152c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 8162c42a146SMarcel Moolenaar /* never to be seen again */ 8179104847fSDavid Xu sigqueue_delete_proc(p, sig); 8182c42a146SMarcel Moolenaar if (sig != SIGCONT) 8192c42a146SMarcel Moolenaar /* easier in psignal */ 82090af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 82190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 822645682fdSLuoqi Chen } else { 82390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 8242c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 82590af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 8262c42a146SMarcel Moolenaar else 82790af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 8282c42a146SMarcel Moolenaar } 82923eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 83023eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 83123eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 83223eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 83323eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 83423eeeff7SPeter Wemm else 83523eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 83623eeeff7SPeter Wemm #endif 837e8ebc08fSPeter Wemm #ifdef COMPAT_43 838645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 83923eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 84023eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 841645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 842645682fdSLuoqi Chen else 843645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 844e8ebc08fSPeter Wemm #endif 845df8bae1dSRodney W. Grimes } 84690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 847628d2653SJohn Baldwin PROC_UNLOCK(p); 8482c42a146SMarcel Moolenaar return (0); 8492c42a146SMarcel Moolenaar } 8502c42a146SMarcel Moolenaar 8512c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8522c42a146SMarcel Moolenaar struct sigaction_args { 8532c42a146SMarcel Moolenaar int sig; 8542c42a146SMarcel Moolenaar struct sigaction *act; 8552c42a146SMarcel Moolenaar struct sigaction *oact; 8562c42a146SMarcel Moolenaar }; 8572c42a146SMarcel Moolenaar #endif 8582c42a146SMarcel Moolenaar int 859e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap) 8602c42a146SMarcel Moolenaar { 8612c42a146SMarcel Moolenaar struct sigaction act, oact; 862e052a8b9SEd Maste struct sigaction *actp, *oactp; 8632c42a146SMarcel Moolenaar int error; 8642c42a146SMarcel Moolenaar 8656f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 8666f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 8672c42a146SMarcel Moolenaar if (actp) { 8686f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 8692c42a146SMarcel Moolenaar if (error) 87044443757STim J. Robbins return (error); 8712c42a146SMarcel Moolenaar } 8728f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 87325d6dc06SJohn Baldwin if (oactp && !error) 8746f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 8752c42a146SMarcel Moolenaar return (error); 8762c42a146SMarcel Moolenaar } 8772c42a146SMarcel Moolenaar 87823eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 87923eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 88023eeeff7SPeter Wemm struct freebsd4_sigaction_args { 88123eeeff7SPeter Wemm int sig; 88223eeeff7SPeter Wemm struct sigaction *act; 88323eeeff7SPeter Wemm struct sigaction *oact; 88423eeeff7SPeter Wemm }; 88523eeeff7SPeter Wemm #endif 88623eeeff7SPeter Wemm int 887e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 88823eeeff7SPeter Wemm { 88923eeeff7SPeter Wemm struct sigaction act, oact; 890e052a8b9SEd Maste struct sigaction *actp, *oactp; 89123eeeff7SPeter Wemm int error; 89223eeeff7SPeter Wemm 89323eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 89423eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 89523eeeff7SPeter Wemm if (actp) { 89623eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 89723eeeff7SPeter Wemm if (error) 89844443757STim J. Robbins return (error); 89923eeeff7SPeter Wemm } 90023eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 901a14e1189SJohn Baldwin if (oactp && !error) 90223eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 90323eeeff7SPeter Wemm return (error); 90423eeeff7SPeter Wemm } 90523eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 90623eeeff7SPeter Wemm 90731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9082c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9092c42a146SMarcel Moolenaar struct osigaction_args { 9102c42a146SMarcel Moolenaar int signum; 9112c42a146SMarcel Moolenaar struct osigaction *nsa; 9122c42a146SMarcel Moolenaar struct osigaction *osa; 9132c42a146SMarcel Moolenaar }; 9142c42a146SMarcel Moolenaar #endif 9152c42a146SMarcel Moolenaar int 916e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap) 9172c42a146SMarcel Moolenaar { 9182c42a146SMarcel Moolenaar struct osigaction sa; 9192c42a146SMarcel Moolenaar struct sigaction nsa, osa; 920e052a8b9SEd Maste struct sigaction *nsap, *osap; 9212c42a146SMarcel Moolenaar int error; 9222c42a146SMarcel Moolenaar 9236f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 9246f841fb7SMarcel Moolenaar return (EINVAL); 925fb99ab88SMatthew Dillon 9266f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 9276f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 928fb99ab88SMatthew Dillon 9292c42a146SMarcel Moolenaar if (nsap) { 9306f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 9312c42a146SMarcel Moolenaar if (error) 93244443757STim J. Robbins return (error); 9332c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 9342c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 9352c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 9362c42a146SMarcel Moolenaar } 93723eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 9382c42a146SMarcel Moolenaar if (osap && !error) { 9392c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 9402c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 9412c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 9426f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 9432c42a146SMarcel Moolenaar } 9442c42a146SMarcel Moolenaar return (error); 9452c42a146SMarcel Moolenaar } 94623eeeff7SPeter Wemm 94773dbd3daSJohn Baldwin #if !defined(__i386__) 94823eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 94923eeeff7SPeter Wemm int 950e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap) 95123eeeff7SPeter Wemm { 95223eeeff7SPeter Wemm 95323eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 95423eeeff7SPeter Wemm } 95523eeeff7SPeter Wemm #endif 95631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 957df8bae1dSRodney W. Grimes 958df8bae1dSRodney W. Grimes /* 959df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 960df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 961df8bae1dSRodney W. Grimes */ 962df8bae1dSRodney W. Grimes void 963e052a8b9SEd Maste siginit(struct proc *p) 964df8bae1dSRodney W. Grimes { 9653e85b721SEd Maste int i; 96690af4afaSJohn Baldwin struct sigacts *ps; 967df8bae1dSRodney W. Grimes 968628d2653SJohn Baldwin PROC_LOCK(p); 96990af4afaSJohn Baldwin ps = p->p_sigacts; 97090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 971350ae563SKonstantin Belousov for (i = 1; i <= NSIG; i++) { 972fd50a707SBrooks Davis if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 97390af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 974350ae563SKonstantin Belousov } 975350ae563SKonstantin Belousov } 97690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 977628d2653SJohn Baldwin PROC_UNLOCK(p); 978df8bae1dSRodney W. Grimes } 979df8bae1dSRodney W. Grimes 980df8bae1dSRodney W. Grimes /* 981350ae563SKonstantin Belousov * Reset specified signal to the default disposition. 982350ae563SKonstantin Belousov */ 983350ae563SKonstantin Belousov static void 984350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig) 985350ae563SKonstantin Belousov { 986350ae563SKonstantin Belousov 987350ae563SKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 988350ae563SKonstantin Belousov SIGDELSET(ps->ps_sigcatch, sig); 989fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 990350ae563SKonstantin Belousov SIGADDSET(ps->ps_sigignore, sig); 991350ae563SKonstantin Belousov ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 992350ae563SKonstantin Belousov SIGDELSET(ps->ps_siginfo, sig); 993350ae563SKonstantin Belousov } 994350ae563SKonstantin Belousov 995350ae563SKonstantin Belousov /* 996df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 997df8bae1dSRodney W. Grimes */ 998df8bae1dSRodney W. Grimes void 999a30ec4b9SDavid Xu execsigs(struct proc *p) 1000df8bae1dSRodney W. Grimes { 1001a30ec4b9SDavid Xu struct sigacts *ps; 1002a30ec4b9SDavid Xu struct thread *td; 1003df8bae1dSRodney W. Grimes 1004df8bae1dSRodney W. Grimes /* 1005df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 10064093529dSJeff Roberson * through td_sigmask (unless they were caught, 1007df8bae1dSRodney W. Grimes * and are now ignored by default). 1008df8bae1dSRodney W. Grimes */ 10099b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1010628d2653SJohn Baldwin ps = p->p_sigacts; 101190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 1012079c5b9eSKyle Evans sig_drop_caught(p); 1013f3fe76ecSEd Schouten 1014f3fe76ecSEd Schouten /* 1015df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 1016df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 1017df8bae1dSRodney W. Grimes */ 1018469ec1ebSKonstantin Belousov td = curthread; 1019469ec1ebSKonstantin Belousov MPASS(td->td_proc == p); 1020a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 1021a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1022a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 1023a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 102480e907a1SPeter Wemm /* 102580e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 102680e907a1SPeter Wemm */ 102790af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1028c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1029c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 103090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes /* 1034e77daab1SJohn Baldwin * kern_sigprocmask() 10357c8fdcbdSMatthew Dillon * 1036628d2653SJohn Baldwin * Manipulate signal mask. 1037df8bae1dSRodney W. Grimes */ 1038e77daab1SJohn Baldwin int 10396b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 104084440afbSKonstantin Belousov int flags) 10412c42a146SMarcel Moolenaar { 10426b286ee8SKonstantin Belousov sigset_t new_block, oset1; 10436b286ee8SKonstantin Belousov struct proc *p; 10442c42a146SMarcel Moolenaar int error; 10452c42a146SMarcel Moolenaar 10466b286ee8SKonstantin Belousov p = td->td_proc; 104770778bbaSKonstantin Belousov if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 104870778bbaSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 104970778bbaSKonstantin Belousov else 10506b286ee8SKonstantin Belousov PROC_LOCK(p); 105170778bbaSKonstantin Belousov mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 105270778bbaSKonstantin Belousov ? MA_OWNED : MA_NOTOWNED); 10532c42a146SMarcel Moolenaar if (oset != NULL) 10544093529dSJeff Roberson *oset = td->td_sigmask; 10552c42a146SMarcel Moolenaar 10562c42a146SMarcel Moolenaar error = 0; 10572c42a146SMarcel Moolenaar if (set != NULL) { 10582c42a146SMarcel Moolenaar switch (how) { 10592c42a146SMarcel Moolenaar case SIG_BLOCK: 1060645682fdSLuoqi Chen SIG_CANTMASK(*set); 10616b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 10624093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 10636b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10646b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10652c42a146SMarcel Moolenaar break; 10662c42a146SMarcel Moolenaar case SIG_UNBLOCK: 10674093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 10684093529dSJeff Roberson signotify(td); 1069407af02bSDavid Xu goto out; 10702c42a146SMarcel Moolenaar case SIG_SETMASK: 1071645682fdSLuoqi Chen SIG_CANTMASK(*set); 10726b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 107384440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 10744093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1075645682fdSLuoqi Chen else 10764093529dSJeff Roberson td->td_sigmask = *set; 10776b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10786b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10794093529dSJeff Roberson signotify(td); 10802c42a146SMarcel Moolenaar break; 10812c42a146SMarcel Moolenaar default: 10822c42a146SMarcel Moolenaar error = EINVAL; 1083407af02bSDavid Xu goto out; 10842c42a146SMarcel Moolenaar } 10856b286ee8SKonstantin Belousov 10866b286ee8SKonstantin Belousov /* 10877df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 10886b286ee8SKonstantin Belousov * blocked, but are blocked now. 10896b286ee8SKonstantin Belousov * 10906b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 10916b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1092407af02bSDavid Xu * signal delivery to some thread that does not block the 1093407af02bSDavid Xu * signal, possibly waking it up. 10946b286ee8SKonstantin Belousov */ 10956b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 109680a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1097407af02bSDavid Xu } 10986b286ee8SKonstantin Belousov 1099407af02bSDavid Xu out: 110084440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 11016b286ee8SKonstantin Belousov PROC_UNLOCK(p); 11022c42a146SMarcel Moolenaar return (error); 11032c42a146SMarcel Moolenaar } 11042c42a146SMarcel Moolenaar 1105d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1106df8bae1dSRodney W. Grimes struct sigprocmask_args { 1107df8bae1dSRodney W. Grimes int how; 11082c42a146SMarcel Moolenaar const sigset_t *set; 11092c42a146SMarcel Moolenaar sigset_t *oset; 1110df8bae1dSRodney W. Grimes }; 1111d2d3e875SBruce Evans #endif 111226f9a767SRodney W. Grimes int 1113e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1114df8bae1dSRodney W. Grimes { 11152c42a146SMarcel Moolenaar sigset_t set, oset; 11162c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 11172c42a146SMarcel Moolenaar int error; 1118df8bae1dSRodney W. Grimes 11196f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 11206f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 11212c42a146SMarcel Moolenaar if (setp) { 11226f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 11232c42a146SMarcel Moolenaar if (error) 11242c42a146SMarcel Moolenaar return (error); 1125df8bae1dSRodney W. Grimes } 1126e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 11272c42a146SMarcel Moolenaar if (osetp && !error) { 11286f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 11292c42a146SMarcel Moolenaar } 11302c42a146SMarcel Moolenaar return (error); 11312c42a146SMarcel Moolenaar } 11322c42a146SMarcel Moolenaar 113331c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11342c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11352c42a146SMarcel Moolenaar struct osigprocmask_args { 11362c42a146SMarcel Moolenaar int how; 11372c42a146SMarcel Moolenaar osigset_t mask; 11382c42a146SMarcel Moolenaar }; 11392c42a146SMarcel Moolenaar #endif 11402c42a146SMarcel Moolenaar int 1141e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap) 11422c42a146SMarcel Moolenaar { 11432c42a146SMarcel Moolenaar sigset_t set, oset; 11442c42a146SMarcel Moolenaar int error; 11452c42a146SMarcel Moolenaar 11462c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1147e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1148b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1149df8bae1dSRodney W. Grimes return (error); 1150df8bae1dSRodney W. Grimes } 115131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1152df8bae1dSRodney W. Grimes 115326f9a767SRodney W. Grimes int 11548451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1155a447cd8bSJeff Roberson { 11569104847fSDavid Xu ksiginfo_t ksi; 1157a447cd8bSJeff Roberson sigset_t set; 1158a447cd8bSJeff Roberson int error; 1159a447cd8bSJeff Roberson 1160a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 116136939a0aSDavid Xu if (error) { 116236939a0aSDavid Xu td->td_retval[0] = error; 116336939a0aSDavid Xu return (0); 116436939a0aSDavid Xu } 1165a447cd8bSJeff Roberson 11669104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 116736939a0aSDavid Xu if (error) { 1168acced8b0SKonstantin Belousov /* 1169acced8b0SKonstantin Belousov * sigwait() function shall not return EINTR, but 1170acced8b0SKonstantin Belousov * the syscall does. Non-ancient libc provides the 1171acced8b0SKonstantin Belousov * wrapper which hides EINTR. Otherwise, EINTR return 1172acced8b0SKonstantin Belousov * is used by libthr to handle required cancellation 1173acced8b0SKonstantin Belousov * point in the sigwait(). 1174acced8b0SKonstantin Belousov */ 11758e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 1176afb36e28SKonstantin Belousov return (ERESTART); 117736939a0aSDavid Xu td->td_retval[0] = error; 117836939a0aSDavid Xu return (0); 117936939a0aSDavid Xu } 1180a447cd8bSJeff Roberson 11819104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 118236939a0aSDavid Xu td->td_retval[0] = error; 118336939a0aSDavid Xu return (0); 1184a447cd8bSJeff Roberson } 11850c14ff0eSRobert Watson 1186a447cd8bSJeff Roberson int 11878451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1188a447cd8bSJeff Roberson { 1189a447cd8bSJeff Roberson struct timespec ts; 1190a447cd8bSJeff Roberson struct timespec *timeout; 1191a447cd8bSJeff Roberson sigset_t set; 11929104847fSDavid Xu ksiginfo_t ksi; 1193a447cd8bSJeff Roberson int error; 1194a447cd8bSJeff Roberson 1195a447cd8bSJeff Roberson if (uap->timeout) { 1196a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1197a447cd8bSJeff Roberson if (error) 1198a447cd8bSJeff Roberson return (error); 1199a447cd8bSJeff Roberson 1200a447cd8bSJeff Roberson timeout = &ts; 1201a447cd8bSJeff Roberson } else 1202a447cd8bSJeff Roberson timeout = NULL; 1203a447cd8bSJeff Roberson 1204a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1205a447cd8bSJeff Roberson if (error) 1206a447cd8bSJeff Roberson return (error); 1207a447cd8bSJeff Roberson 12089104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1209a447cd8bSJeff Roberson if (error) 1210a447cd8bSJeff Roberson return (error); 12119dde3bc9SDavid Xu 1212418228dfSDavid Xu if (uap->info) 12139104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12149104847fSDavid Xu 12159104847fSDavid Xu if (error == 0) 12169104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1217a447cd8bSJeff Roberson return (error); 1218a447cd8bSJeff Roberson } 1219a447cd8bSJeff Roberson 1220a447cd8bSJeff Roberson int 12218451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1222a447cd8bSJeff Roberson { 12239104847fSDavid Xu ksiginfo_t ksi; 1224a447cd8bSJeff Roberson sigset_t set; 1225a447cd8bSJeff Roberson int error; 1226a447cd8bSJeff Roberson 1227a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1228a447cd8bSJeff Roberson if (error) 1229a447cd8bSJeff Roberson return (error); 1230a447cd8bSJeff Roberson 12319104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1232a447cd8bSJeff Roberson if (error) 1233a447cd8bSJeff Roberson return (error); 1234a447cd8bSJeff Roberson 1235418228dfSDavid Xu if (uap->info) 12369104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12379104847fSDavid Xu 12389104847fSDavid Xu if (error == 0) 12399104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1240a447cd8bSJeff Roberson return (error); 1241a447cd8bSJeff Roberson } 1242a447cd8bSJeff Roberson 124386be94fcSTycho Nightingale static void 124486be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 124586be94fcSTycho Nightingale { 124686be94fcSTycho Nightingale struct thread *thr; 124786be94fcSTycho Nightingale 124886be94fcSTycho Nightingale FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 124986be94fcSTycho Nightingale if (thr == td) 125086be94fcSTycho Nightingale thr->td_si = *si; 125186be94fcSTycho Nightingale else 125286be94fcSTycho Nightingale thr->td_si.si_signo = 0; 125386be94fcSTycho Nightingale } 125486be94fcSTycho Nightingale } 125586be94fcSTycho Nightingale 1256c6511aeaSDavid Xu int 12579104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1258a447cd8bSJeff Roberson struct timespec *timeout) 1259a447cd8bSJeff Roberson { 12603074d1b4SDavid Xu struct sigacts *ps; 1261407af02bSDavid Xu sigset_t saved_mask, new_block; 1262a447cd8bSJeff Roberson struct proc *p; 1263407af02bSDavid Xu int error, sig, timo, timevalid = 0; 12641089f031SDavid Xu struct timespec rts, ets, ts; 12651089f031SDavid Xu struct timeval tv; 1266ef401a85SKonstantin Belousov bool traced; 1267a447cd8bSJeff Roberson 1268a447cd8bSJeff Roberson p = td->td_proc; 1269a447cd8bSJeff Roberson error = 0; 12709b73d239SMatt Jacob ets.tv_sec = 0; 12719b73d239SMatt Jacob ets.tv_nsec = 0; 1272ef401a85SKonstantin Belousov traced = false; 1273a447cd8bSJeff Roberson 1274dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1275dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1276dbec10e0SJonathan T. Looney 1277407af02bSDavid Xu if (timeout != NULL) { 12781089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 12791089f031SDavid Xu timevalid = 1; 12801089f031SDavid Xu getnanouptime(&rts); 12816040822cSAlan Somers timespecadd(&rts, timeout, &ets); 12821089f031SDavid Xu } 12831089f031SDavid Xu } 1284407af02bSDavid Xu ksiginfo_init(ksi); 1285407af02bSDavid Xu /* Some signals can not be waited for. */ 1286407af02bSDavid Xu SIG_CANTMASK(waitset); 1287407af02bSDavid Xu ps = p->p_sigacts; 1288407af02bSDavid Xu PROC_LOCK(p); 1289407af02bSDavid Xu saved_mask = td->td_sigmask; 1290407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1291bc387624SKonstantin Belousov if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 || 1292b599982bSKonstantin Belousov !kern_sig_discard_ign) { 1293b599982bSKonstantin Belousov thread_lock(td); 1294b599982bSKonstantin Belousov td->td_flags |= TDF_SIGWAIT; 1295b599982bSKonstantin Belousov thread_unlock(td); 1296b599982bSKonstantin Belousov } 1297407af02bSDavid Xu for (;;) { 12983074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 12993cf3b9f0SJohn Baldwin sig = cursig(td); 13003074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 130146e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 1302407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1303407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1304407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1305407af02bSDavid Xu error = 0; 1306407af02bSDavid Xu break; 13073074d1b4SDavid Xu } 13087e0221a2SDavid Xu } 13097e0221a2SDavid Xu 1310407af02bSDavid Xu if (error != 0) 1311407af02bSDavid Xu break; 13123074d1b4SDavid Xu 1313a447cd8bSJeff Roberson /* 1314a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1315a447cd8bSJeff Roberson * signals. 1316a447cd8bSJeff Roberson */ 1317407af02bSDavid Xu if (timeout != NULL) { 13181089f031SDavid Xu if (!timevalid) { 1319a447cd8bSJeff Roberson error = EINVAL; 1320407af02bSDavid Xu break; 1321a447cd8bSJeff Roberson } 13221089f031SDavid Xu getnanouptime(&rts); 13231089f031SDavid Xu if (timespeccmp(&rts, &ets, >=)) { 13243074d1b4SDavid Xu error = EAGAIN; 1325407af02bSDavid Xu break; 13263074d1b4SDavid Xu } 13276040822cSAlan Somers timespecsub(&ets, &rts, &ts); 13281089f031SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1329407af02bSDavid Xu timo = tvtohz(&tv); 1330407af02bSDavid Xu } else { 1331407af02bSDavid Xu timo = 0; 1332407af02bSDavid Xu } 1333a447cd8bSJeff Roberson 1334ef401a85SKonstantin Belousov if (traced) { 1335ef401a85SKonstantin Belousov error = EINTR; 1336ef401a85SKonstantin Belousov break; 1337ef401a85SKonstantin Belousov } 1338ef401a85SKonstantin Belousov 1339c4feb1abSMark Johnston error = msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, 1340c4feb1abSMark Johnston "sigwait", timo); 1341407af02bSDavid Xu 1342afb36e28SKonstantin Belousov /* The syscalls can not be restarted. */ 1343afb36e28SKonstantin Belousov if (error == ERESTART) 13443074d1b4SDavid Xu error = EINTR; 1345afb36e28SKonstantin Belousov 1346407af02bSDavid Xu /* We will calculate timeout by ourself. */ 1347afb36e28SKonstantin Belousov if (timeout != NULL && error == EAGAIN) 13481089f031SDavid Xu error = 0; 1349ef401a85SKonstantin Belousov 1350ef401a85SKonstantin Belousov /* 1351ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1352ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1353ef401a85SKonstantin Belousov * EINTR after wait was done. Only do this as last 1354ef401a85SKonstantin Belousov * resort after rechecking for possible queued signals 1355ef401a85SKonstantin Belousov * and expired timeouts. 1356ef401a85SKonstantin Belousov */ 1357ef401a85SKonstantin Belousov if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0) 1358ef401a85SKonstantin Belousov traced = true; 1359407af02bSDavid Xu } 1360b599982bSKonstantin Belousov thread_lock(td); 1361b599982bSKonstantin Belousov td->td_flags &= ~TDF_SIGWAIT; 1362b599982bSKonstantin Belousov thread_unlock(td); 13639dde3bc9SDavid Xu 1364407af02bSDavid Xu new_block = saved_mask; 1365407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1366407af02bSDavid Xu td->td_sigmask = saved_mask; 1367407af02bSDavid Xu /* 1368407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1369407af02bSDavid Xu * notification. 1370407af02bSDavid Xu */ 1371407af02bSDavid Xu if (p->p_numthreads != 1) 1372407af02bSDavid Xu reschedule_signals(p, new_block, 0); 13735d217f17SJohn Birrell 1374407af02bSDavid Xu if (error == 0) { 137536160958SMark Johnston SDT_PROBE2(proc, , , signal__clear, sig, ksi); 13765d217f17SJohn Birrell 137756c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 137856c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 13797e0221a2SDavid Xu 13807e0221a2SDavid Xu #ifdef KTRACE 13817e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 13827e0221a2SDavid Xu sig_t action; 13837e0221a2SDavid Xu 13843074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1385a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 138690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 138761009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 13887e0221a2SDavid Xu } 1389a447cd8bSJeff Roberson #endif 139086be94fcSTycho Nightingale if (sig == SIGKILL) { 139186be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi->ksi_info); 13927e0221a2SDavid Xu sigexit(td, sig); 13933074d1b4SDavid Xu } 139486be94fcSTycho Nightingale } 1395a447cd8bSJeff Roberson PROC_UNLOCK(p); 1396a447cd8bSJeff Roberson return (error); 1397a447cd8bSJeff Roberson } 1398a447cd8bSJeff Roberson 13999104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 14009104847fSDavid Xu struct sigpending_args { 14019104847fSDavid Xu sigset_t *set; 14029104847fSDavid Xu }; 14039104847fSDavid Xu #endif 1404a447cd8bSJeff Roberson int 1405e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap) 1406df8bae1dSRodney W. Grimes { 1407b40ce416SJulian Elischer struct proc *p = td->td_proc; 14089104847fSDavid Xu sigset_t pending; 1409df8bae1dSRodney W. Grimes 1410628d2653SJohn Baldwin PROC_LOCK(p); 14119104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 14129104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1413628d2653SJohn Baldwin PROC_UNLOCK(p); 14149104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 14152c42a146SMarcel Moolenaar } 14162c42a146SMarcel Moolenaar 141731c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 14182c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 14192c42a146SMarcel Moolenaar struct osigpending_args { 14202c42a146SMarcel Moolenaar int dummy; 14212c42a146SMarcel Moolenaar }; 14222c42a146SMarcel Moolenaar #endif 14232c42a146SMarcel Moolenaar int 1424e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap) 14252c42a146SMarcel Moolenaar { 1426b40ce416SJulian Elischer struct proc *p = td->td_proc; 14279104847fSDavid Xu sigset_t pending; 1428b40ce416SJulian Elischer 1429628d2653SJohn Baldwin PROC_LOCK(p); 14309104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 14319104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1432628d2653SJohn Baldwin PROC_UNLOCK(p); 14339104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1434df8bae1dSRodney W. Grimes return (0); 1435df8bae1dSRodney W. Grimes } 143631c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1437df8bae1dSRodney W. Grimes 14381930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1439df8bae1dSRodney W. Grimes /* 1440df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1441df8bae1dSRodney W. Grimes */ 1442d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1443df8bae1dSRodney W. Grimes struct osigvec_args { 1444df8bae1dSRodney W. Grimes int signum; 1445df8bae1dSRodney W. Grimes struct sigvec *nsv; 1446df8bae1dSRodney W. Grimes struct sigvec *osv; 1447df8bae1dSRodney W. Grimes }; 1448d2d3e875SBruce Evans #endif 1449df8bae1dSRodney W. Grimes /* ARGSUSED */ 145026f9a767SRodney W. Grimes int 1451e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap) 1452df8bae1dSRodney W. Grimes { 1453df8bae1dSRodney W. Grimes struct sigvec vec; 14542c42a146SMarcel Moolenaar struct sigaction nsa, osa; 1455e052a8b9SEd Maste struct sigaction *nsap, *osap; 14562c42a146SMarcel Moolenaar int error; 1457df8bae1dSRodney W. Grimes 14586f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 14596f841fb7SMarcel Moolenaar return (EINVAL); 14606f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 14616f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 14622c42a146SMarcel Moolenaar if (nsap) { 14636f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 14642c42a146SMarcel Moolenaar if (error) 1465df8bae1dSRodney W. Grimes return (error); 14662c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 14672c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 14682c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 14692c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1470df8bae1dSRodney W. Grimes } 14715edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 14722c42a146SMarcel Moolenaar if (osap && !error) { 14732c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 14742c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 14752c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 14762c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 14772c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 14786f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 14792c42a146SMarcel Moolenaar } 14802c42a146SMarcel Moolenaar return (error); 1481df8bae1dSRodney W. Grimes } 1482df8bae1dSRodney W. Grimes 1483d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1484df8bae1dSRodney W. Grimes struct osigblock_args { 1485df8bae1dSRodney W. Grimes int mask; 1486df8bae1dSRodney W. Grimes }; 1487d2d3e875SBruce Evans #endif 148826f9a767SRodney W. Grimes int 1489e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap) 1490df8bae1dSRodney W. Grimes { 1491d6e029adSKonstantin Belousov sigset_t set, oset; 1492df8bae1dSRodney W. Grimes 14932c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1494d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1495d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1496df8bae1dSRodney W. Grimes return (0); 1497df8bae1dSRodney W. Grimes } 1498df8bae1dSRodney W. Grimes 1499d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1500df8bae1dSRodney W. Grimes struct osigsetmask_args { 1501df8bae1dSRodney W. Grimes int mask; 1502df8bae1dSRodney W. Grimes }; 1503d2d3e875SBruce Evans #endif 150426f9a767SRodney W. Grimes int 1505e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1506df8bae1dSRodney W. Grimes { 1507d6e029adSKonstantin Belousov sigset_t set, oset; 1508df8bae1dSRodney W. Grimes 15092c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1510d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1511d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1512df8bae1dSRodney W. Grimes return (0); 1513df8bae1dSRodney W. Grimes } 15141930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1515df8bae1dSRodney W. Grimes 1516df8bae1dSRodney W. Grimes /* 1517873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1518873fbcd7SRobert Watson * meantime. 1519df8bae1dSRodney W. Grimes */ 1520d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1521df8bae1dSRodney W. Grimes struct sigsuspend_args { 15222c42a146SMarcel Moolenaar const sigset_t *sigmask; 1523df8bae1dSRodney W. Grimes }; 1524d2d3e875SBruce Evans #endif 1525df8bae1dSRodney W. Grimes /* ARGSUSED */ 152626f9a767SRodney W. Grimes int 1527e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1528df8bae1dSRodney W. Grimes { 15292c42a146SMarcel Moolenaar sigset_t mask; 15302c42a146SMarcel Moolenaar int error; 15312c42a146SMarcel Moolenaar 15326f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 15332c42a146SMarcel Moolenaar if (error) 15342c42a146SMarcel Moolenaar return (error); 15358f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 15368f19eb88SIan Dowse } 15378f19eb88SIan Dowse 15388f19eb88SIan Dowse int 15398f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 15408f19eb88SIan Dowse { 15418f19eb88SIan Dowse struct proc *p = td->td_proc; 154284440afbSKonstantin Belousov int has_sig, sig; 1543df8bae1dSRodney W. Grimes 1544dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1545dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1546dbec10e0SJonathan T. Looney 1547df8bae1dSRodney W. Grimes /* 1548645682fdSLuoqi Chen * When returning from sigsuspend, we want 1549df8bae1dSRodney W. Grimes * the old mask to be restored after the 1550df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1551df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1552df8bae1dSRodney W. Grimes * to indicate this. 1553df8bae1dSRodney W. Grimes */ 1554628d2653SJohn Baldwin PROC_LOCK(p); 155584440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 155684440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 15575e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 155884440afbSKonstantin Belousov 155984440afbSKonstantin Belousov /* 156084440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 156184440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 156284440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 156384440afbSKonstantin Belousov * delivery. 156484440afbSKonstantin Belousov */ 1565afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 156684440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 156784440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 156884440afbSKonstantin Belousov 0) == 0) 15692c42a146SMarcel Moolenaar /* void */; 157084440afbSKonstantin Belousov thread_suspend_check(0); 157184440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 157246e47c4fSKonstantin Belousov while ((sig = cursig(td)) != 0) { 157346e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 157475c586a4SKonstantin Belousov has_sig += postsig(sig); 157546e47c4fSKonstantin Belousov } 157684440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 1577ef401a85SKonstantin Belousov 1578ef401a85SKonstantin Belousov /* 1579ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1580ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1581ef401a85SKonstantin Belousov * EINTR. 1582ef401a85SKonstantin Belousov */ 1583ef401a85SKonstantin Belousov if ((p->p_ptevents & PTRACE_SYSCALL) != 0) 1584ef401a85SKonstantin Belousov has_sig += 1; 158584440afbSKonstantin Belousov } 1586628d2653SJohn Baldwin PROC_UNLOCK(p); 15872dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 15882dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 158975c586a4SKonstantin Belousov return (EJUSTRETURN); 15902c42a146SMarcel Moolenaar } 15912c42a146SMarcel Moolenaar 159231c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 159397563428SAlexander Kabaev /* 159497563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 159597563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 159697563428SAlexander Kabaev */ 15972c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 15982c42a146SMarcel Moolenaar struct osigsuspend_args { 15992c42a146SMarcel Moolenaar osigset_t mask; 16002c42a146SMarcel Moolenaar }; 16012c42a146SMarcel Moolenaar #endif 16022c42a146SMarcel Moolenaar /* ARGSUSED */ 16032c42a146SMarcel Moolenaar int 1604e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap) 16052c42a146SMarcel Moolenaar { 1606645682fdSLuoqi Chen sigset_t mask; 16072c42a146SMarcel Moolenaar 1608645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 160984440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1610df8bae1dSRodney W. Grimes } 161131c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1612df8bae1dSRodney W. Grimes 16131930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1614d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1615df8bae1dSRodney W. Grimes struct osigstack_args { 1616df8bae1dSRodney W. Grimes struct sigstack *nss; 1617df8bae1dSRodney W. Grimes struct sigstack *oss; 1618df8bae1dSRodney W. Grimes }; 1619d2d3e875SBruce Evans #endif 1620df8bae1dSRodney W. Grimes /* ARGSUSED */ 162126f9a767SRodney W. Grimes int 1622e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap) 1623df8bae1dSRodney W. Grimes { 16245afe0c99SJohn Baldwin struct sigstack nss, oss; 1625fb99ab88SMatthew Dillon int error = 0; 1626fb99ab88SMatthew Dillon 1627d034d459SMarcel Moolenaar if (uap->nss != NULL) { 16285afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 16295afe0c99SJohn Baldwin if (error) 16305afe0c99SJohn Baldwin return (error); 1631df8bae1dSRodney W. Grimes } 1632a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 16335afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 16345afe0c99SJohn Baldwin if (uap->nss != NULL) { 1635a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1636a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1637a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1638a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 16395afe0c99SJohn Baldwin } 16405afe0c99SJohn Baldwin if (uap->oss != NULL) 16415afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 16425afe0c99SJohn Baldwin 1643fb99ab88SMatthew Dillon return (error); 1644df8bae1dSRodney W. Grimes } 16451930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1646df8bae1dSRodney W. Grimes 1647d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1648df8bae1dSRodney W. Grimes struct sigaltstack_args { 16492c42a146SMarcel Moolenaar stack_t *ss; 16502c42a146SMarcel Moolenaar stack_t *oss; 1651df8bae1dSRodney W. Grimes }; 1652d2d3e875SBruce Evans #endif 1653df8bae1dSRodney W. Grimes /* ARGSUSED */ 165426f9a767SRodney W. Grimes int 1655e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1656df8bae1dSRodney W. Grimes { 16578f19eb88SIan Dowse stack_t ss, oss; 16588f19eb88SIan Dowse int error; 16598f19eb88SIan Dowse 16608f19eb88SIan Dowse if (uap->ss != NULL) { 16618f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 16628f19eb88SIan Dowse if (error) 16638f19eb88SIan Dowse return (error); 16648f19eb88SIan Dowse } 16658f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 16668f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 16678f19eb88SIan Dowse if (error) 16688f19eb88SIan Dowse return (error); 16698f19eb88SIan Dowse if (uap->oss != NULL) 16708f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 16718f19eb88SIan Dowse return (error); 16728f19eb88SIan Dowse } 16738f19eb88SIan Dowse 16748f19eb88SIan Dowse int 16758f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 16768f19eb88SIan Dowse { 1677b40ce416SJulian Elischer struct proc *p = td->td_proc; 1678fb99ab88SMatthew Dillon int oonstack; 1679fb99ab88SMatthew Dillon 1680b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1681d034d459SMarcel Moolenaar 16828f19eb88SIan Dowse if (oss != NULL) { 1683a30ec4b9SDavid Xu *oss = td->td_sigstk; 1684a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1685d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1686df8bae1dSRodney W. Grimes } 1687d034d459SMarcel Moolenaar 16888f19eb88SIan Dowse if (ss != NULL) { 1689a30ec4b9SDavid Xu if (oonstack) 1690cf60731bSJohn Baldwin return (EPERM); 1691a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1692cf60731bSJohn Baldwin return (EINVAL); 16938f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 16949104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1695cf60731bSJohn Baldwin return (ENOMEM); 16969104847fSDavid Xu 1697a30ec4b9SDavid Xu td->td_sigstk = *ss; 1698a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1699628d2653SJohn Baldwin } else { 1700a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1701628d2653SJohn Baldwin } 1702d034d459SMarcel Moolenaar } 1703cf60731bSJohn Baldwin return (0); 1704df8bae1dSRodney W. Grimes } 1705df8bae1dSRodney W. Grimes 17060cc9fb75SKonstantin Belousov struct killpg1_ctx { 17070cc9fb75SKonstantin Belousov struct thread *td; 17080cc9fb75SKonstantin Belousov ksiginfo_t *ksi; 17090cc9fb75SKonstantin Belousov int sig; 17100cc9fb75SKonstantin Belousov bool sent; 17110cc9fb75SKonstantin Belousov bool found; 17120cc9fb75SKonstantin Belousov int ret; 17130cc9fb75SKonstantin Belousov }; 17140cc9fb75SKonstantin Belousov 17150cc9fb75SKonstantin Belousov static void 17160cc9fb75SKonstantin Belousov killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg) 17170cc9fb75SKonstantin Belousov { 17180cc9fb75SKonstantin Belousov int err; 17190cc9fb75SKonstantin Belousov 17200cc9fb75SKonstantin Belousov if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 17210cc9fb75SKonstantin Belousov (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW) 17220cc9fb75SKonstantin Belousov return; 17230cc9fb75SKonstantin Belousov PROC_LOCK(p); 17240cc9fb75SKonstantin Belousov err = p_cansignal(arg->td, p, arg->sig); 17250cc9fb75SKonstantin Belousov if (err == 0 && arg->sig != 0) 17260cc9fb75SKonstantin Belousov pksignal(p, arg->sig, arg->ksi); 17270cc9fb75SKonstantin Belousov PROC_UNLOCK(p); 17280cc9fb75SKonstantin Belousov if (err != ESRCH) 17290cc9fb75SKonstantin Belousov arg->found = true; 17300cc9fb75SKonstantin Belousov if (err == 0) 17310cc9fb75SKonstantin Belousov arg->sent = true; 17320cc9fb75SKonstantin Belousov else if (arg->ret == 0 && err != ESRCH && err != EPERM) 17330cc9fb75SKonstantin Belousov arg->ret = err; 17340cc9fb75SKonstantin Belousov } 17350cc9fb75SKonstantin Belousov 1736d93f860cSPoul-Henning Kamp /* 1737d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1738d93f860cSPoul-Henning Kamp * cp is calling process. 1739d93f860cSPoul-Henning Kamp */ 174037c84183SPoul-Henning Kamp static int 1741a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1742d93f860cSPoul-Henning Kamp { 1743a3de221dSKonstantin Belousov struct proc *p; 1744d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 17450cc9fb75SKonstantin Belousov struct killpg1_ctx arg; 1746d93f860cSPoul-Henning Kamp 17470cc9fb75SKonstantin Belousov arg.td = td; 17480cc9fb75SKonstantin Belousov arg.ksi = ksi; 17490cc9fb75SKonstantin Belousov arg.sig = sig; 17500cc9fb75SKonstantin Belousov arg.sent = false; 17510cc9fb75SKonstantin Belousov arg.found = false; 17520cc9fb75SKonstantin Belousov arg.ret = 0; 1753553629ebSJake Burkholder if (all) { 1754d93f860cSPoul-Henning Kamp /* 1755d93f860cSPoul-Henning Kamp * broadcast 1756d93f860cSPoul-Henning Kamp */ 17571005a129SJohn Baldwin sx_slock(&allproc_lock); 17584f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 17590cc9fb75SKonstantin Belousov killpg1_sendsig(p, true, &arg); 1760d93f860cSPoul-Henning Kamp } 17611005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1762553629ebSJake Burkholder } else { 1763ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1764f591779bSSeigo Tanimura if (pgid == 0) { 1765d93f860cSPoul-Henning Kamp /* 1766d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1767d93f860cSPoul-Henning Kamp */ 17689c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1769f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1770f591779bSSeigo Tanimura } else { 1771d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1772f591779bSSeigo Tanimura if (pgrp == NULL) { 1773ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1774d93f860cSPoul-Henning Kamp return (ESRCH); 1775d93f860cSPoul-Henning Kamp } 1776f591779bSSeigo Tanimura } 1777ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 17782e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 17790cc9fb75SKonstantin Belousov killpg1_sendsig(p, false, &arg); 1780d93f860cSPoul-Henning Kamp } 1781f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1782d93f860cSPoul-Henning Kamp } 17830cc9fb75SKonstantin Belousov MPASS(arg.ret != 0 || arg.found || !arg.sent); 17840cc9fb75SKonstantin Belousov if (arg.ret == 0 && !arg.sent) 17850cc9fb75SKonstantin Belousov arg.ret = arg.found ? EPERM : ESRCH; 17860cc9fb75SKonstantin Belousov return (arg.ret); 1787d93f860cSPoul-Henning Kamp } 1788d93f860cSPoul-Henning Kamp 1789d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1790df8bae1dSRodney W. Grimes struct kill_args { 1791df8bae1dSRodney W. Grimes int pid; 1792df8bae1dSRodney W. Grimes int signum; 1793df8bae1dSRodney W. Grimes }; 1794d2d3e875SBruce Evans #endif 1795df8bae1dSRodney W. Grimes /* ARGSUSED */ 179626f9a767SRodney W. Grimes int 17978451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1798df8bae1dSRodney W. Grimes { 179934ad5ac2SEdward Tomasz Napierala 180034ad5ac2SEdward Tomasz Napierala return (kern_kill(td, uap->pid, uap->signum)); 180134ad5ac2SEdward Tomasz Napierala } 180234ad5ac2SEdward Tomasz Napierala 180334ad5ac2SEdward Tomasz Napierala int 180434ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum) 180534ad5ac2SEdward Tomasz Napierala { 1806a3de221dSKonstantin Belousov ksiginfo_t ksi; 1807a3de221dSKonstantin Belousov struct proc *p; 180890af4afaSJohn Baldwin int error; 1809df8bae1dSRodney W. Grimes 18108890f5d0SPawel Jakub Dawidek /* 18118890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 18128890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 18138890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 18148890f5d0SPawel Jakub Dawidek */ 181534ad5ac2SEdward Tomasz Napierala if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid) 18168890f5d0SPawel Jakub Dawidek return (ECAPMODE); 18178890f5d0SPawel Jakub Dawidek 181834ad5ac2SEdward Tomasz Napierala AUDIT_ARG_SIGNUM(signum); 181934ad5ac2SEdward Tomasz Napierala AUDIT_ARG_PID(pid); 182034ad5ac2SEdward Tomasz Napierala if ((u_int)signum > _SIG_MAXSIG) 1821df8bae1dSRodney W. Grimes return (EINVAL); 1822fb99ab88SMatthew Dillon 1823a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 182434ad5ac2SEdward Tomasz Napierala ksi.ksi_signo = signum; 1825a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1826a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1827a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1828a3de221dSKonstantin Belousov 182934ad5ac2SEdward Tomasz Napierala if (pid > 0) { 1830df8bae1dSRodney W. Grimes /* kill single process */ 183134ad5ac2SEdward Tomasz Napierala if ((p = pfind_any(pid)) == NULL) 183290af4afaSJohn Baldwin return (ESRCH); 183314961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 183434ad5ac2SEdward Tomasz Napierala error = p_cansignal(td, p, signum); 183534ad5ac2SEdward Tomasz Napierala if (error == 0 && signum) 183634ad5ac2SEdward Tomasz Napierala pksignal(p, signum, &ksi); 1837628d2653SJohn Baldwin PROC_UNLOCK(p); 183890af4afaSJohn Baldwin return (error); 1839df8bae1dSRodney W. Grimes } 184034ad5ac2SEdward Tomasz Napierala switch (pid) { 1841df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 184234ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 1, &ksi)); 1843df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 184434ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 0, &ksi)); 1845df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 184634ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, -pid, 0, &ksi)); 1847df8bae1dSRodney W. Grimes } 184890af4afaSJohn Baldwin /* NOTREACHED */ 1849df8bae1dSRodney W. Grimes } 1850df8bae1dSRodney W. Grimes 1851cfb5f768SJonathan Anderson int 1852e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap) 1853cfb5f768SJonathan Anderson { 1854cfb5f768SJonathan Anderson struct proc *p; 1855cfb5f768SJonathan Anderson int error; 1856cfb5f768SJonathan Anderson 1857cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1858cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1859cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1860cfb5f768SJonathan Anderson return (EINVAL); 1861cfb5f768SJonathan Anderson 1862cbd92ce6SMatt Macy error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p); 1863cfb5f768SJonathan Anderson if (error) 1864cfb5f768SJonathan Anderson return (error); 1865cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1866cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1867cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 18688451d0ddSKip Macy kern_psignal(p, uap->signum); 1869cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1870cfb5f768SJonathan Anderson return (error); 1871cfb5f768SJonathan Anderson } 1872cfb5f768SJonathan Anderson 18731930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1874d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1875df8bae1dSRodney W. Grimes struct okillpg_args { 1876df8bae1dSRodney W. Grimes int pgid; 1877df8bae1dSRodney W. Grimes int signum; 1878df8bae1dSRodney W. Grimes }; 1879d2d3e875SBruce Evans #endif 1880df8bae1dSRodney W. Grimes /* ARGSUSED */ 188126f9a767SRodney W. Grimes int 1882a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1883df8bae1dSRodney W. Grimes { 1884a3de221dSKonstantin Belousov ksiginfo_t ksi; 1885df8bae1dSRodney W. Grimes 188614961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 188714961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 18886c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1889df8bae1dSRodney W. Grimes return (EINVAL); 18909104847fSDavid Xu 1891a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1892a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1893a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1894a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1895a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1896a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1897df8bae1dSRodney W. Grimes } 18981930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1899df8bae1dSRodney W. Grimes 19009104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 19019104847fSDavid Xu struct sigqueue_args { 19029104847fSDavid Xu pid_t pid; 19039104847fSDavid Xu int signum; 19049104847fSDavid Xu /* union sigval */ void *value; 19059104847fSDavid Xu }; 19069104847fSDavid Xu #endif 19079104847fSDavid Xu int 19088451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 19099104847fSDavid Xu { 1910f19351aaSBrooks Davis union sigval sv; 1911f19351aaSBrooks Davis 1912f19351aaSBrooks Davis sv.sival_ptr = uap->value; 1913f19351aaSBrooks Davis 1914f19351aaSBrooks Davis return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 1915f19351aaSBrooks Davis } 1916f19351aaSBrooks Davis 1917f19351aaSBrooks Davis int 1918f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value) 1919f19351aaSBrooks Davis { 19209104847fSDavid Xu ksiginfo_t ksi; 19219104847fSDavid Xu struct proc *p; 19229104847fSDavid Xu int error; 19239104847fSDavid Xu 1924f19351aaSBrooks Davis if ((u_int)signum > _SIG_MAXSIG) 19259104847fSDavid Xu return (EINVAL); 19269104847fSDavid Xu 19279104847fSDavid Xu /* 19289104847fSDavid Xu * Specification says sigqueue can only send signal to 19299104847fSDavid Xu * single process. 19309104847fSDavid Xu */ 1931f19351aaSBrooks Davis if (pid <= 0) 19329104847fSDavid Xu return (EINVAL); 19339104847fSDavid Xu 1934537d0fb1SMateusz Guzik if ((p = pfind_any(pid)) == NULL) 19359104847fSDavid Xu return (ESRCH); 1936f19351aaSBrooks Davis error = p_cansignal(td, p, signum); 1937f19351aaSBrooks Davis if (error == 0 && signum != 0) { 19389104847fSDavid Xu ksiginfo_init(&ksi); 1939a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 1940f19351aaSBrooks Davis ksi.ksi_signo = signum; 19419104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 19429104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 19439104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 1944f19351aaSBrooks Davis ksi.ksi_value = *value; 1945ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 19469104847fSDavid Xu } 19479104847fSDavid Xu PROC_UNLOCK(p); 19489104847fSDavid Xu return (error); 19499104847fSDavid Xu } 19509104847fSDavid Xu 1951df8bae1dSRodney W. Grimes /* 1952df8bae1dSRodney W. Grimes * Send a signal to a process group. 1953df8bae1dSRodney W. Grimes */ 1954df8bae1dSRodney W. Grimes void 1955a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 1956df8bae1dSRodney W. Grimes { 1957df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1958df8bae1dSRodney W. Grimes 1959f591779bSSeigo Tanimura if (pgid != 0) { 1960ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1961f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1962ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1963f591779bSSeigo Tanimura if (pgrp != NULL) { 1964a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 1965f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1966f591779bSSeigo Tanimura } 1967f591779bSSeigo Tanimura } 1968df8bae1dSRodney W. Grimes } 1969df8bae1dSRodney W. Grimes 1970df8bae1dSRodney W. Grimes /* 1971df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1972df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1973df8bae1dSRodney W. Grimes */ 1974df8bae1dSRodney W. Grimes void 1975a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1976df8bae1dSRodney W. Grimes { 1977a3de221dSKonstantin Belousov struct proc *p; 1978df8bae1dSRodney W. Grimes 1979628d2653SJohn Baldwin if (pgrp) { 1980f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1981628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1982628d2653SJohn Baldwin PROC_LOCK(p); 1983e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 1984e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 1985a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 1986628d2653SJohn Baldwin PROC_UNLOCK(p); 1987628d2653SJohn Baldwin } 1988628d2653SJohn Baldwin } 1989df8bae1dSRodney W. Grimes } 1990df8bae1dSRodney W. Grimes 1991e442f29fSKonstantin Belousov /* 1992e442f29fSKonstantin Belousov * Recalculate the signal mask and reset the signal disposition after 1993e442f29fSKonstantin Belousov * usermode frame for delivery is formed. Should be called after 1994e442f29fSKonstantin Belousov * mach-specific routine, because sysent->sv_sendsig() needs correct 1995e442f29fSKonstantin Belousov * ps_siginfo and signal mask. 1996e442f29fSKonstantin Belousov */ 1997e442f29fSKonstantin Belousov static void 1998e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps) 1999e442f29fSKonstantin Belousov { 2000e442f29fSKonstantin Belousov sigset_t mask; 2001e442f29fSKonstantin Belousov 2002e442f29fSKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 2003e442f29fSKonstantin Belousov td->td_ru.ru_nsignals++; 2004e442f29fSKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2005e442f29fSKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2006e442f29fSKonstantin Belousov SIGADDSET(mask, sig); 2007e442f29fSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 2008e442f29fSKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 2009e442f29fSKonstantin Belousov if (SIGISMEMBER(ps->ps_sigreset, sig)) 2010e442f29fSKonstantin Belousov sigdflt(ps, sig); 2011e442f29fSKonstantin Belousov } 2012e442f29fSKonstantin Belousov 2013df8bae1dSRodney W. Grimes /* 20140c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 20150c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 20160c14ff0eSRobert Watson * normally. 2017df8bae1dSRodney W. Grimes */ 2018df8bae1dSRodney W. Grimes void 20199104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 2020df8bae1dSRodney W. Grimes { 20211bf4700bSJeff Roberson struct sigacts *ps; 20221bf4700bSJeff Roberson struct proc *p; 2023146fc63fSKonstantin Belousov sigset_t sigmask; 2024146fc63fSKonstantin Belousov int code, sig; 20251bf4700bSJeff Roberson 20261bf4700bSJeff Roberson p = td->td_proc; 20279104847fSDavid Xu sig = ksi->ksi_signo; 20289104847fSDavid Xu code = ksi->ksi_code; 20299104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 20309104847fSDavid Xu 2031a113b17fSKonstantin Belousov sigfastblock_fetch(td); 2032628d2653SJohn Baldwin PROC_LOCK(p); 2033ef3dab76STim J. Robbins ps = p->p_sigacts; 203490af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2035146fc63fSKonstantin Belousov sigmask = td->td_sigmask; 2036146fc63fSKonstantin Belousov if (td->td_sigblock_val != 0) 2037146fc63fSKonstantin Belousov SIGSETOR(sigmask, fastblock_mask); 203890af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 2039146fc63fSKonstantin Belousov !SIGISMEMBER(sigmask, sig)) { 2040df8bae1dSRodney W. Grimes #ifdef KTRACE 2041374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 2042374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 20434093529dSJeff Roberson &td->td_sigmask, code); 2044df8bae1dSRodney W. Grimes #endif 20459104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 20469104847fSDavid Xu ksi, &td->td_sigmask); 2047e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 204890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20496f841fb7SMarcel Moolenaar } else { 2050f71a882fSDavid Xu /* 2051f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 2052f71a882fSDavid Xu * masking the signal or process is ignoring the 2053f71a882fSDavid Xu * signal. 2054f71a882fSDavid Xu */ 2055146fc63fSKonstantin Belousov if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) || 2056f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 2057f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 2058f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 2059f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 2060f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2061146fc63fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTBLOCK; 2062146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 2063f71a882fSDavid Xu } 206490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20652c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 2066ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 2067df8bae1dSRodney W. Grimes } 2068628d2653SJohn Baldwin PROC_UNLOCK(p); 2069df8bae1dSRodney W. Grimes } 2070df8bae1dSRodney W. Grimes 20714093529dSJeff Roberson static struct thread * 2072146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock) 20734093529dSJeff Roberson { 20743074d1b4SDavid Xu struct thread *td, *signal_td; 20754093529dSJeff Roberson 20764093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2077146fc63fSKonstantin Belousov MPASS(!fast_sigblock || p == curproc); 20784093529dSJeff Roberson 20794093529dSJeff Roberson /* 2080627451c1SDavid Xu * Check if current thread can handle the signal without 208115b7a831SKonstantin Belousov * switching context to another thread. 20824093529dSJeff Roberson */ 2083146fc63fSKonstantin Belousov if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) && 2084146fc63fSKonstantin Belousov (!fast_sigblock || curthread->td_sigblock_val == 0)) 2085627451c1SDavid Xu return (curthread); 20863074d1b4SDavid Xu signal_td = NULL; 20873074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 2088146fc63fSKonstantin Belousov if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock || 2089146fc63fSKonstantin Belousov td != curthread || td->td_sigblock_val == 0)) { 20903074d1b4SDavid Xu signal_td = td; 2091627451c1SDavid Xu break; 20923074d1b4SDavid Xu } 20933074d1b4SDavid Xu } 20943074d1b4SDavid Xu if (signal_td == NULL) 20953074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 20963074d1b4SDavid Xu return (signal_td); 20974093529dSJeff Roberson } 20984093529dSJeff Roberson 2099df8bae1dSRodney W. Grimes /* 2100df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 2101df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 2102df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 2103df8bae1dSRodney W. Grimes * 2104df8bae1dSRodney W. Grimes * Exceptions: 2105df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 2106df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 2107df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 2108df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 2109df8bae1dSRodney W. Grimes * 2110df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 21114dec0e67SRobert Watson * 21124dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 21134dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 21144dec0e67SRobert Watson * side effects of this unwise possibility. 2115df8bae1dSRodney W. Grimes */ 2116df8bae1dSRodney W. Grimes void 21178451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 2118df8bae1dSRodney W. Grimes { 2119a3de221dSKonstantin Belousov ksiginfo_t ksi; 2120a3de221dSKonstantin Belousov 2121a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 2122a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 2123a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 2124ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 2125a3de221dSKonstantin Belousov } 2126a3de221dSKonstantin Belousov 2127ad6eec7bSJohn Baldwin int 2128a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2129a3de221dSKonstantin Belousov { 2130a3de221dSKonstantin Belousov 2131ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 21329104847fSDavid Xu } 21339104847fSDavid Xu 2134cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 21359104847fSDavid Xu int 2136cf7d9a8cSDavid Xu sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd) 21379104847fSDavid Xu { 2138cf7d9a8cSDavid Xu struct thread *td; 213941b3077aSJacques Vidrine 21406d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2141cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 21426d7b314bSDavid Xu if (td == NULL) 21436d7b314bSDavid Xu return (ESRCH); 2144cf7d9a8cSDavid Xu *ttd = td; 2145cf7d9a8cSDavid Xu } else { 2146cf7d9a8cSDavid Xu *ttd = NULL; 2147cf7d9a8cSDavid Xu PROC_LOCK(p); 21486d7b314bSDavid Xu } 2149cf7d9a8cSDavid Xu return (0); 21504093529dSJeff Roberson } 21514093529dSJeff Roberson 2152ad6eec7bSJohn Baldwin void 2153ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2154ad6eec7bSJohn Baldwin { 2155ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2156ad6eec7bSJohn Baldwin 2157ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2158ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2159ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2160ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2161ad6eec7bSJohn Baldwin } 2162ad6eec7bSJohn Baldwin 2163ad6eec7bSJohn Baldwin void 2164ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2165ad6eec7bSJohn Baldwin { 2166ad6eec7bSJohn Baldwin 2167ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2168ad6eec7bSJohn Baldwin } 2169ad6eec7bSJohn Baldwin 21709b86d3e5SKonstantin Belousov static int 21719b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval) 21729b86d3e5SKonstantin Belousov { 21739b86d3e5SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_OWNED); 21749b86d3e5SKonstantin Belousov 21759b86d3e5SKonstantin Belousov if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) { 21769b86d3e5SKonstantin Belousov thread_unlock(td); 21779b86d3e5SKonstantin Belousov return (0); 21789b86d3e5SKonstantin Belousov } 21799b86d3e5SKonstantin Belousov return (sleepq_abort(td, intrval)); 21809b86d3e5SKonstantin Belousov } 21819b86d3e5SKonstantin Belousov 2182cf7d9a8cSDavid Xu int 2183ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 21844093529dSJeff Roberson { 21859104847fSDavid Xu sig_t action; 21869104847fSDavid Xu sigqueue_t *sigqueue; 21879104847fSDavid Xu int prop; 218890af4afaSJohn Baldwin struct sigacts *ps; 218994f0972bSDavid Xu int intrval; 21909104847fSDavid Xu int ret = 0; 2191da7bbd2cSJohn Baldwin int wakeup_swapper; 2192df8bae1dSRodney W. Grimes 2193cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 21946d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 21956d7b314bSDavid Xu 219641b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2197212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 21984093529dSJeff Roberson 2199212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 22006d7b314bSDavid Xu 22016d7b314bSDavid Xu /* 22026d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 22036d7b314bSDavid Xu */ 22046d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 22056d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 22066d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22076d7b314bSDavid Xu return (ret); 22086d7b314bSDavid Xu } 22096d7b314bSDavid Xu 221090af4afaSJohn Baldwin ps = p->p_sigacts; 22119e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 22122c42a146SMarcel Moolenaar prop = sigprop(sig); 22134093529dSJeff Roberson 22146d7b314bSDavid Xu if (td == NULL) { 2215146fc63fSKonstantin Belousov td = sigtd(p, sig, false); 22166d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 2217462314b3SMateusz Guzik } else 22189104847fSDavid Xu sigqueue = &td->td_sigqueue; 22194093529dSJeff Roberson 222036160958SMark Johnston SDT_PROBE3(proc, , , signal__send, td, p, sig); 22215d217f17SJohn Birrell 2222df8bae1dSRodney W. Grimes /* 2223bc387624SKonstantin Belousov * If the signal is being ignored, then we forget about it 2224bc387624SKonstantin Belousov * immediately, except when the target process executes 2225bc387624SKonstantin Belousov * sigwait(). (Note: we don't set SIGCONT in ps_sigignore, 2226bc387624SKonstantin Belousov * and if it is set to SIG_IGN, action will be SIG_DFL here.) 2227df8bae1dSRodney W. Grimes */ 222890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 22290fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2230bc387624SKonstantin Belousov if (kern_sig_discard_ign && 2231bc387624SKonstantin Belousov (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) { 223236160958SMark Johnston SDT_PROBE3(proc, , , signal__discard, td, p, sig); 22335d217f17SJohn Birrell 223490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 22356d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 22366d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22379104847fSDavid Xu return (ret); 2238bc387624SKonstantin Belousov } else { 2239bc387624SKonstantin Belousov action = SIG_CATCH; 2240f17eb93dSKonstantin Belousov intrval = 0; 224190af4afaSJohn Baldwin } 2242f17eb93dSKonstantin Belousov } else { 2243f17eb93dSKonstantin Belousov if (SIGISMEMBER(td->td_sigmask, sig)) 2244df8bae1dSRodney W. Grimes action = SIG_HOLD; 224590af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2246df8bae1dSRodney W. Grimes action = SIG_CATCH; 2247df8bae1dSRodney W. Grimes else 2248df8bae1dSRodney W. Grimes action = SIG_DFL; 224994f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 225094f0972bSDavid Xu intrval = EINTR; 225194f0972bSDavid Xu else 225294f0972bSDavid Xu intrval = ERESTART; 2253f17eb93dSKonstantin Belousov } 225490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2255df8bae1dSRodney W. Grimes 2256fd50a707SBrooks Davis if (prop & SIGPROP_CONT) 22579104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 2258fd50a707SBrooks Davis else if (prop & SIGPROP_STOP) { 2259df8bae1dSRodney W. Grimes /* 2260df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2261df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2262df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2263df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2264df8bae1dSRodney W. Grimes */ 2265993a1699SKonstantin Belousov if ((prop & SIGPROP_TTYSTOP) != 0 && 22665844bd05SKonstantin Belousov (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 && 2267993a1699SKonstantin Belousov action == SIG_DFL) { 22686d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 22696d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22709104847fSDavid Xu return (ret); 22716d7b314bSDavid Xu } 22729104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2273ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 22746933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2275ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2276ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2277ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2278ebceaf6dSDavid Xu } 2279df8bae1dSRodney W. Grimes } 22803074d1b4SDavid Xu 22819104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 22829104847fSDavid Xu if (ret != 0) 22839104847fSDavid Xu return (ret); 22846d7b314bSDavid Xu signotify(td); 22855312b1c7SDavid Xu /* 22865312b1c7SDavid Xu * Defer further processing for signals which are held, 22875312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 22885312b1c7SDavid Xu */ 22895312b1c7SDavid Xu if (action == SIG_HOLD && 2290fd50a707SBrooks Davis !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 22919104847fSDavid Xu return (ret); 2292b4d33259SEric Badger 229361a74c5cSJeff Roberson wakeup_swapper = 0; 229461a74c5cSJeff Roberson 229590d75f78SAlfred Perlstein /* 2296e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2297e602ba25SJulian Elischer * component. Most processing occurs when the process next 2298e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 22991968f37bSJohn Baldwin * times when processing needs to be done immediately, such as 2300e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 23011968f37bSJohn Baldwin * We try to do the per-process part here. 2302df8bae1dSRodney W. Grimes */ 2303e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 23040f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 23050f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2306e602ba25SJulian Elischer if (sig == SIGKILL) { 2307137cf33dSDavid Xu /* 2308137cf33dSDavid Xu * If traced process is already stopped, 2309137cf33dSDavid Xu * then no further action is necessary. 2310137cf33dSDavid Xu */ 231183b718ebSDavid Xu if (p->p_flag & P_TRACED) 231283b718ebSDavid Xu goto out; 2313df8bae1dSRodney W. Grimes /* 2314e602ba25SJulian Elischer * SIGKILL sets process running. 2315e602ba25SJulian Elischer * It will die elsewhere. 2316e602ba25SJulian Elischer * All threads must be restarted. 2317df8bae1dSRodney W. Grimes */ 2318482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2319e602ba25SJulian Elischer goto runfast; 2320e602ba25SJulian Elischer } 2321e602ba25SJulian Elischer 2322fd50a707SBrooks Davis if (prop & SIGPROP_CONT) { 2323137cf33dSDavid Xu /* 2324137cf33dSDavid Xu * If traced process is already stopped, 2325137cf33dSDavid Xu * then no further action is necessary. 2326137cf33dSDavid Xu */ 232783b718ebSDavid Xu if (p->p_flag & P_TRACED) 232883b718ebSDavid Xu goto out; 2329e602ba25SJulian Elischer /* 2330e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 23319104847fSDavid Xu * process but don't leave the signal in sigqueue as 23321d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2333e602ba25SJulian Elischer * continue the process and leave the signal in 23349104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2335e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2336e602ba25SJulian Elischer * an event, it goes back to run state. 2337e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2338e602ba25SJulian Elischer */ 23391279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 23407b4a950aSDavid Xu PROC_SLOCK(p); 2341ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 23427b4a950aSDavid Xu PROC_SUNLOCK(p); 23436933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 2344b4490c6eSKonstantin Belousov p->p_xsig = SIGCONT; 23457f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2346ebceaf6dSDavid Xu childproc_continued(p); 23477f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 23487b4a950aSDavid Xu PROC_SLOCK(p); 2349ebceaf6dSDavid Xu } 2350e602ba25SJulian Elischer if (action == SIG_DFL) { 2351a54e85fdSJeff Roberson thread_unsuspend(p); 23527b4a950aSDavid Xu PROC_SUNLOCK(p); 23539104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2354dc47fdf1SKonstantin Belousov goto out_cont; 2355a54e85fdSJeff Roberson } 2356a54e85fdSJeff Roberson if (action == SIG_CATCH) { 23578460a577SJohn Birrell /* 23588460a577SJohn Birrell * The process wants to catch it so it needs 23598460a577SJohn Birrell * to run at least one thread, but which one? 23608460a577SJohn Birrell */ 23617b4a950aSDavid Xu PROC_SUNLOCK(p); 2362e602ba25SJulian Elischer goto runfast; 2363e602ba25SJulian Elischer } 2364e602ba25SJulian Elischer /* 2365e602ba25SJulian Elischer * The signal is not ignored or caught. 2366e602ba25SJulian Elischer */ 236704774f23SJulian Elischer thread_unsuspend(p); 23687b4a950aSDavid Xu PROC_SUNLOCK(p); 2369dc47fdf1SKonstantin Belousov goto out_cont; 2370e602ba25SJulian Elischer } 2371e602ba25SJulian Elischer 2372fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2373137cf33dSDavid Xu /* 2374137cf33dSDavid Xu * If traced process is already stopped, 2375137cf33dSDavid Xu * then no further action is necessary. 2376137cf33dSDavid Xu */ 237783b718ebSDavid Xu if (p->p_flag & P_TRACED) 237883b718ebSDavid Xu goto out; 2379e602ba25SJulian Elischer /* 2380e602ba25SJulian Elischer * Already stopped, don't need to stop again 2381e602ba25SJulian Elischer * (If we did the shell could get confused). 238204774f23SJulian Elischer * Just make sure the signal STOP bit set. 2383e602ba25SJulian Elischer */ 23841279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 23859104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2386e602ba25SJulian Elischer goto out; 2387e602ba25SJulian Elischer } 2388e602ba25SJulian Elischer 2389e602ba25SJulian Elischer /* 2390e602ba25SJulian Elischer * All other kinds of signals: 2391e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2392e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2393e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 239404774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2395e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2396e602ba25SJulian Elischer */ 23977b4a950aSDavid Xu PROC_SLOCK(p); 2398a54e85fdSJeff Roberson thread_lock(td); 239961a74c5cSJeff Roberson if (TD_CAN_ABORT(td)) 24009b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 240161a74c5cSJeff Roberson else 2402a54e85fdSJeff Roberson thread_unlock(td); 24037b4a950aSDavid Xu PROC_SUNLOCK(p); 2404df8bae1dSRodney W. Grimes goto out; 2405df8bae1dSRodney W. Grimes /* 24069a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 24079a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2408df8bae1dSRodney W. Grimes */ 2409e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2410ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 241194f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2412df8bae1dSRodney W. Grimes goto out; 241304774f23SJulian Elischer } 2414ec8297bdSDavid Xu 2415ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2416ec8297bdSDavid Xu 2417fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 24180f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 241935c32a76SDavid Xu goto out; 2420e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2421b4490c6eSKonstantin Belousov p->p_xsig = sig; 24227b4a950aSDavid Xu PROC_SLOCK(p); 24236f56cb8dSKonstantin Belousov wakeup_swapper = sig_suspend_threads(td, p, 1); 24244093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2425ebceaf6dSDavid Xu /* 2426ebceaf6dSDavid Xu * only thread sending signal to another 2427ebceaf6dSDavid Xu * process can reach here, if thread is sending 2428ebceaf6dSDavid Xu * signal to its process, because thread does 2429ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2430ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2431ebceaf6dSDavid Xu */ 2432ebceaf6dSDavid Xu thread_stopped(p); 24337b4a950aSDavid Xu PROC_SUNLOCK(p); 2434b4490c6eSKonstantin Belousov sigqueue_delete_proc(p, p->p_xsig); 24357b4a950aSDavid Xu } else 24367b4a950aSDavid Xu PROC_SUNLOCK(p); 243735c32a76SDavid Xu goto out; 243835c32a76SDavid Xu } 2439e602ba25SJulian Elischer } else { 2440e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 24419104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2442e602ba25SJulian Elischer goto out; 2443e602ba25SJulian Elischer } 2444e602ba25SJulian Elischer 2445b40ce416SJulian Elischer /* 2446e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2447e602ba25SJulian Elischer * running threads. 2448b40ce416SJulian Elischer */ 2449e602ba25SJulian Elischer runfast: 245094f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 24517b4a950aSDavid Xu PROC_SLOCK(p); 2452e602ba25SJulian Elischer thread_unsuspend(p); 24537b4a950aSDavid Xu PROC_SUNLOCK(p); 2454dc47fdf1SKonstantin Belousov out_cont: 2455dc47fdf1SKonstantin Belousov itimer_proc_continue(p); 24562fd1ffefSKonstantin Belousov kqtimer_proc_continue(p); 2457e602ba25SJulian Elischer out: 24587b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 24597b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 246061a74c5cSJeff Roberson if (wakeup_swapper) 246161a74c5cSJeff Roberson kick_proc0(); 246261a74c5cSJeff Roberson 24639104847fSDavid Xu return (ret); 2464e602ba25SJulian Elischer } 2465e602ba25SJulian Elischer 2466e602ba25SJulian Elischer /* 2467e602ba25SJulian Elischer * The force of a signal has been directed against a single 2468e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2469e602ba25SJulian Elischer * out of any sleep it may be in etc. 2470e602ba25SJulian Elischer */ 2471e602ba25SJulian Elischer static void 247294f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2473e602ba25SJulian Elischer { 2474e602ba25SJulian Elischer struct proc *p = td->td_proc; 247561a74c5cSJeff Roberson int prop, wakeup_swapper; 2476e602ba25SJulian Elischer 24778b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2478e602ba25SJulian Elischer prop = sigprop(sig); 2479a4c2da15SBruce Evans 24807b4a950aSDavid Xu PROC_SLOCK(p); 2481374ae2a3SJeff Roberson thread_lock(td); 2482e602ba25SJulian Elischer /* 2483aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2484aef68c96SKonstantin Belousov * killed in this lifetime. Be careful to avoid bumping the 2485aef68c96SKonstantin Belousov * priority of the idle thread, since we still allow to signal 2486aef68c96SKonstantin Belousov * kernel processes. 2487e602ba25SJulian Elischer */ 2488fd50a707SBrooks Davis if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2489aef68c96SKonstantin Belousov td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2490b3a4fb14SDavid Xu sched_prio(td, PUSER); 249180c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2492e602ba25SJulian Elischer /* 2493e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2494e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2495e602ba25SJulian Elischer * be noticed when the process returns through 2496e602ba25SJulian Elischer * trap() or syscall(). 2497e602ba25SJulian Elischer */ 249844f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2499374ae2a3SJeff Roberson goto out; 2500e602ba25SJulian Elischer /* 2501e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2502e602ba25SJulian Elischer * asleep, we are finished; the process should not 2503e602ba25SJulian Elischer * be awakened. 2504df8bae1dSRodney W. Grimes */ 2505fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2506a54e85fdSJeff Roberson thread_unlock(td); 25077b4a950aSDavid Xu PROC_SUNLOCK(p); 25089104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 25094093529dSJeff Roberson /* 25104093529dSJeff Roberson * It may be on either list in this state. 25114093529dSJeff Roberson * Remove from both for now. 25124093529dSJeff Roberson */ 25139104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2514aa0fa334SJulian Elischer return; 2515df8bae1dSRodney W. Grimes } 2516df8bae1dSRodney W. Grimes 2517aa0fa334SJulian Elischer /* 2518a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2519a120a7a3SJohn Baldwin * STOP signal is deferred. 2520a120a7a3SJohn Baldwin */ 2521fd50a707SBrooks Davis if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 25226f56cb8dSKonstantin Belousov TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2523a120a7a3SJohn Baldwin goto out; 2524a120a7a3SJohn Baldwin 2525a120a7a3SJohn Baldwin /* 2526a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2527aa0fa334SJulian Elischer */ 2528aef68c96SKonstantin Belousov if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2529b3a4fb14SDavid Xu sched_prio(td, PUSER); 2530ec8297bdSDavid Xu 25319b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 253261a74c5cSJeff Roberson PROC_SUNLOCK(p); 253361a74c5cSJeff Roberson if (wakeup_swapper) 253461a74c5cSJeff Roberson kick_proc0(); 253561a74c5cSJeff Roberson return; 253661a74c5cSJeff Roberson } 253761a74c5cSJeff Roberson 2538df8bae1dSRodney W. Grimes /* 2539a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2540df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2541df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2542df8bae1dSRodney W. Grimes */ 2543a4c2da15SBruce Evans #ifdef SMP 254444f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2545e602ba25SJulian Elischer forward_signal(td); 25463163861cSTor Egge #endif 254761a74c5cSJeff Roberson 2548374ae2a3SJeff Roberson out: 25497b4a950aSDavid Xu PROC_SUNLOCK(p); 2550374ae2a3SJeff Roberson thread_unlock(td); 2551a4c2da15SBruce Evans } 2552df8bae1dSRodney W. Grimes 255387a64872SKonstantin Belousov static void 255487a64872SKonstantin Belousov ptrace_coredump(struct thread *td) 255587a64872SKonstantin Belousov { 255687a64872SKonstantin Belousov struct proc *p; 255787a64872SKonstantin Belousov struct thr_coredump_req *tcq; 255887a64872SKonstantin Belousov void *rl_cookie; 255987a64872SKonstantin Belousov 256087a64872SKonstantin Belousov MPASS(td == curthread); 256187a64872SKonstantin Belousov p = td->td_proc; 256287a64872SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 256387a64872SKonstantin Belousov if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0) 256487a64872SKonstantin Belousov return; 256587a64872SKonstantin Belousov KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped")); 256687a64872SKonstantin Belousov 256787a64872SKonstantin Belousov tcq = td->td_coredump; 256887a64872SKonstantin Belousov KASSERT(tcq != NULL, ("td_coredump is NULL")); 256987a64872SKonstantin Belousov 257087a64872SKonstantin Belousov if (p->p_sysent->sv_coredump == NULL) { 257187a64872SKonstantin Belousov tcq->tc_error = ENOSYS; 257287a64872SKonstantin Belousov goto wake; 257387a64872SKonstantin Belousov } 257487a64872SKonstantin Belousov 257587a64872SKonstantin Belousov PROC_UNLOCK(p); 257687a64872SKonstantin Belousov rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX); 257787a64872SKonstantin Belousov 257887a64872SKonstantin Belousov tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp, 257987a64872SKonstantin Belousov tcq->tc_limit, tcq->tc_flags); 258087a64872SKonstantin Belousov 258187a64872SKonstantin Belousov vn_rangelock_unlock(tcq->tc_vp, rl_cookie); 258287a64872SKonstantin Belousov PROC_LOCK(p); 258387a64872SKonstantin Belousov wake: 258487a64872SKonstantin Belousov td->td_dbgflags &= ~TDB_COREDUMPRQ; 258587a64872SKonstantin Belousov td->td_coredump = NULL; 258687a64872SKonstantin Belousov wakeup(p); 258787a64872SKonstantin Belousov } 258887a64872SKonstantin Belousov 25896f56cb8dSKonstantin Belousov static int 2590d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2591d8267df7SDavid Xu { 2592d8267df7SDavid Xu struct thread *td2; 25936f56cb8dSKonstantin Belousov int wakeup_swapper; 2594d8267df7SDavid Xu 2595d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 25967b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 25970c46712cSMark Johnston MPASS(sending || td == curthread); 2598d8267df7SDavid Xu 25996f56cb8dSKonstantin Belousov wakeup_swapper = 0; 2600d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2601a54e85fdSJeff Roberson thread_lock(td2); 2602b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2603d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2604f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2605f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2606a120a7a3SJohn Baldwin /* 2607a120a7a3SJohn Baldwin * Once a thread is asleep with 26086f56cb8dSKonstantin Belousov * TDF_SBDRY and without TDF_SERESTART 26096f56cb8dSKonstantin Belousov * or TDF_SEINTR set, it should never 2610a120a7a3SJohn Baldwin * become suspended due to this check. 2611a120a7a3SJohn Baldwin */ 2612a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2613a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 261461a74c5cSJeff Roberson if (TD_SBDRY_INTR(td2)) { 261546e47c4fSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, 261646e47c4fSKonstantin Belousov TD_SBDRY_ERRNO(td2)); 261761a74c5cSJeff Roberson continue; 2618f33a947bSKonstantin Belousov } 261961a74c5cSJeff Roberson } else if (!TD_IS_SUSPENDED(td2)) 262061a74c5cSJeff Roberson thread_suspend_one(td2); 2621f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2622d8267df7SDavid Xu if (sending || td != td2) 2623d8267df7SDavid Xu td2->td_flags |= TDF_ASTPENDING; 2624d8267df7SDavid Xu #ifdef SMP 2625d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2626d8267df7SDavid Xu forward_signal(td2); 2627d8267df7SDavid Xu #endif 2628d8267df7SDavid Xu } 2629a54e85fdSJeff Roberson thread_unlock(td2); 2630d8267df7SDavid Xu } 26316f56cb8dSKonstantin Belousov return (wakeup_swapper); 2632d8267df7SDavid Xu } 2633d8267df7SDavid Xu 263482a4538fSEric Badger /* 263582a4538fSEric Badger * Stop the process for an event deemed interesting to the debugger. If si is 263682a4538fSEric Badger * non-NULL, this is a signal exchange; the new signal requested by the 263782a4538fSEric Badger * debugger will be returned for handling. If si is NULL, this is some other 263882a4538fSEric Badger * type of interesting event. The debugger may request a signal be delivered in 263982a4538fSEric Badger * that case as well, however it will be deferred until it can be handled. 264082a4538fSEric Badger */ 2641cbf4e354SDavid Xu int 264282a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si) 26434cc9f52fSRobert Drehmel { 26444cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 264582a4538fSEric Badger struct thread *td2; 264682a4538fSEric Badger ksiginfo_t ksi; 26474cc9f52fSRobert Drehmel 264830a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 26490f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 26504cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2651aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 26524cc9f52fSRobert Drehmel 2653cbf4e354SDavid Xu td->td_xsig = sig; 265482a4538fSEric Badger 265582a4538fSEric Badger if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 265682a4538fSEric Badger td->td_dbgflags |= TDB_XSIG; 2657515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2658515b7a0bSJohn Baldwin td->td_tid, p->p_pid, td->td_dbgflags, sig); 26597b4a950aSDavid Xu PROC_SLOCK(p); 2660904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 266182a4538fSEric Badger if (P_KILLED(p)) { 266282a4538fSEric Badger /* 266382a4538fSEric Badger * Ensure that, if we've been PT_KILLed, the 266482a4538fSEric Badger * exit status reflects that. Another thread 266582a4538fSEric Badger * may also be in ptracestop(), having just 266682a4538fSEric Badger * received the SIGKILL, but this thread was 266782a4538fSEric Badger * unsuspended first. 266882a4538fSEric Badger */ 266982a4538fSEric Badger td->td_dbgflags &= ~TDB_XSIG; 267082a4538fSEric Badger td->td_xsig = SIGKILL; 267182a4538fSEric Badger p->p_ptevents = 0; 267282a4538fSEric Badger break; 267382a4538fSEric Badger } 26745fcfab6eSJohn Baldwin if (p->p_flag & P_SINGLE_EXIT && 26755fcfab6eSJohn Baldwin !(td->td_dbgflags & TDB_EXIT)) { 26765fcfab6eSJohn Baldwin /* 26775fcfab6eSJohn Baldwin * Ignore ptrace stops except for thread exit 26785fcfab6eSJohn Baldwin * events when the process exits. 26795fcfab6eSJohn Baldwin */ 2680904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 26817b4a950aSDavid Xu PROC_SUNLOCK(p); 268282a4538fSEric Badger return (0); 2683cbf4e354SDavid Xu } 2684b7a25e63SKonstantin Belousov 2685cbf4e354SDavid Xu /* 2686b7a25e63SKonstantin Belousov * Make wait(2) work. Ensure that right after the 2687b7a25e63SKonstantin Belousov * attach, the thread which was decided to become the 2688b7a25e63SKonstantin Belousov * leader of attach gets reported to the waiter. 2689b7a25e63SKonstantin Belousov * Otherwise, just avoid overwriting another thread's 2690b7a25e63SKonstantin Belousov * assignment to p_xthread. If another thread has 2691b7a25e63SKonstantin Belousov * already set p_xthread, the current thread will get 2692b7a25e63SKonstantin Belousov * a chance to report itself upon the next iteration. 2693cbf4e354SDavid Xu */ 2694b7a25e63SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0 || 26957f649ddaSMark Johnston ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2696b7a25e63SKonstantin Belousov p->p_xthread == NULL)) { 2697b4490c6eSKonstantin Belousov p->p_xsig = sig; 2698cbf4e354SDavid Xu p->p_xthread = td; 2699ab74c843SKonstantin Belousov 2700ab74c843SKonstantin Belousov /* 2701ab74c843SKonstantin Belousov * If we are on sleepqueue already, 2702ab74c843SKonstantin Belousov * let sleepqueue code decide if it 2703ab74c843SKonstantin Belousov * needs to go sleep after attach. 2704ab74c843SKonstantin Belousov */ 2705ab74c843SKonstantin Belousov if (td->td_wchan == NULL) 2706b7a25e63SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 2707ab74c843SKonstantin Belousov 2708b7a25e63SKonstantin Belousov p->p_flag2 &= ~P2_PTRACE_FSTP; 2709b7a25e63SKonstantin Belousov p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2710d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 2711b7a25e63SKonstantin Belousov } 27126fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 27136fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 27146fa39a73SKonstantin Belousov } 2715cbf4e354SDavid Xu stopme: 271668d311b6SKonstantin Belousov td->td_dbgflags |= TDB_SSWITCH; 27176ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 271868d311b6SKonstantin Belousov td->td_dbgflags &= ~TDB_SSWITCH; 271987a64872SKonstantin Belousov if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) { 272087a64872SKonstantin Belousov PROC_SUNLOCK(p); 272187a64872SKonstantin Belousov ptrace_coredump(td); 272287a64872SKonstantin Belousov PROC_SLOCK(p); 272387a64872SKonstantin Belousov goto stopme; 272487a64872SKonstantin Belousov } 27257ce60f60SDavid Xu if (p->p_xthread == td) 27267ce60f60SDavid Xu p->p_xthread = NULL; 27277ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2728cbf4e354SDavid Xu break; 2729904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2730cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2731cbf4e354SDavid Xu break; 2732cbf4e354SDavid Xu goto stopme; 2733cbf4e354SDavid Xu } 2734cbf4e354SDavid Xu } 27357b4a950aSDavid Xu PROC_SUNLOCK(p); 273682a4538fSEric Badger } 273782a4538fSEric Badger 273882a4538fSEric Badger if (si != NULL && sig == td->td_xsig) { 273982a4538fSEric Badger /* Parent wants us to take the original signal unchanged. */ 274082a4538fSEric Badger si->ksi_flags |= KSI_HEAD; 274182a4538fSEric Badger if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 274282a4538fSEric Badger si->ksi_signo = 0; 274382a4538fSEric Badger } else if (td->td_xsig != 0) { 274482a4538fSEric Badger /* 274582a4538fSEric Badger * If parent wants us to take a new signal, then it will leave 274682a4538fSEric Badger * it in td->td_xsig; otherwise we just look for signals again. 274782a4538fSEric Badger */ 274882a4538fSEric Badger ksiginfo_init(&ksi); 274982a4538fSEric Badger ksi.ksi_signo = td->td_xsig; 275082a4538fSEric Badger ksi.ksi_flags |= KSI_PTRACE; 2751146fc63fSKonstantin Belousov td2 = sigtd(p, td->td_xsig, false); 275282a4538fSEric Badger tdsendsignal(p, td2, td->td_xsig, &ksi); 275382a4538fSEric Badger if (td != td2) 275482a4538fSEric Badger return (0); 275582a4538fSEric Badger } 275682a4538fSEric Badger 2757cbf4e354SDavid Xu return (td->td_xsig); 27584cc9f52fSRobert Drehmel } 27594cc9f52fSRobert Drehmel 27600bc52b0bSKonstantin Belousov static void 276180a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 27626b286ee8SKonstantin Belousov { 27636b286ee8SKonstantin Belousov struct sigacts *ps; 27646b286ee8SKonstantin Belousov struct thread *td; 2765407af02bSDavid Xu int sig; 2766146fc63fSKonstantin Belousov bool fastblk, pslocked; 27676b286ee8SKonstantin Belousov 27686b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 276970778bbaSKonstantin Belousov ps = p->p_sigacts; 2770146fc63fSKonstantin Belousov pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0; 2771146fc63fSKonstantin Belousov mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED); 2772407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2773407af02bSDavid Xu return; 2774407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2775146fc63fSKonstantin Belousov fastblk = (flags & SIGPROCMASK_FASTBLK) != 0; 277681f2e906SMark Johnston SIG_FOREACH(sig, &block) { 2777146fc63fSKonstantin Belousov td = sigtd(p, sig, fastblk); 2778146fc63fSKonstantin Belousov 2779146fc63fSKonstantin Belousov /* 2780146fc63fSKonstantin Belousov * If sigtd() selected us despite sigfastblock is 2781146fc63fSKonstantin Belousov * blocking, do not activate AST or wake us, to avoid 2782146fc63fSKonstantin Belousov * loop in AST handler. 2783146fc63fSKonstantin Belousov */ 2784146fc63fSKonstantin Belousov if (fastblk && td == curthread) 2785146fc63fSKonstantin Belousov continue; 2786146fc63fSKonstantin Belousov 27876b286ee8SKonstantin Belousov signotify(td); 2788146fc63fSKonstantin Belousov if (!pslocked) 27896b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2790396a0d44SKonstantin Belousov if (p->p_flag & P_TRACED || 2791396a0d44SKonstantin Belousov (SIGISMEMBER(ps->ps_sigcatch, sig) && 2792146fc63fSKonstantin Belousov !SIGISMEMBER(td->td_sigmask, sig))) { 2793407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2794407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 27956b286ee8SKonstantin Belousov ERESTART)); 2796146fc63fSKonstantin Belousov } 2797146fc63fSKonstantin Belousov if (!pslocked) 27986b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 27996b286ee8SKonstantin Belousov } 28006b286ee8SKonstantin Belousov } 28016b286ee8SKonstantin Belousov 28026b286ee8SKonstantin Belousov void 28036b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 28046b286ee8SKonstantin Belousov { 28056b286ee8SKonstantin Belousov struct proc *p; 28066b286ee8SKonstantin Belousov sigset_t unblocked; 28076b286ee8SKonstantin Belousov 28086b286ee8SKonstantin Belousov p = td->td_proc; 28096b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 28106b286ee8SKonstantin Belousov 28116b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 28126b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 28136b286ee8SKonstantin Belousov return; 28146b286ee8SKonstantin Belousov 28156b286ee8SKonstantin Belousov /* 28166b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 28176b286ee8SKonstantin Belousov * about this by filling the sigmask. 28186b286ee8SKonstantin Belousov * 28196b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 28206b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 28216b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 28226b286ee8SKonstantin Belousov */ 28236b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 28246b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 28256b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 282680a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 28276b286ee8SKonstantin Belousov 28286b286ee8SKonstantin Belousov } 28296b286ee8SKonstantin Belousov 28303a1e5dd8SKonstantin Belousov static int 28313a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags) 2832a120a7a3SJohn Baldwin { 2833a120a7a3SJohn Baldwin 28343a1e5dd8SKonstantin Belousov MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 28353a1e5dd8SKonstantin Belousov (cflags & TDF_SBDRY) != 0); 28363a1e5dd8SKonstantin Belousov return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 2837a120a7a3SJohn Baldwin } 2838a120a7a3SJohn Baldwin 2839a120a7a3SJohn Baldwin /* 28403a1e5dd8SKonstantin Belousov * Defer the delivery of SIGSTOP for the current thread, according to 28413a1e5dd8SKonstantin Belousov * the requested mode. Returns previous flags, which must be restored 28423a1e5dd8SKonstantin Belousov * by sigallowstop(). 28433a1e5dd8SKonstantin Belousov * 28443a1e5dd8SKonstantin Belousov * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 28453a1e5dd8SKonstantin Belousov * cleared by the current thread, which allow the lock-less read-only 28463a1e5dd8SKonstantin Belousov * accesses below. 2847a120a7a3SJohn Baldwin */ 2848e3612a4cSKonstantin Belousov int 284946e47c4fSKonstantin Belousov sigdeferstop_impl(int mode) 2850a120a7a3SJohn Baldwin { 2851593efaf9SJohn Baldwin struct thread *td; 28523a1e5dd8SKonstantin Belousov int cflags, nflags; 2853a120a7a3SJohn Baldwin 2854593efaf9SJohn Baldwin td = curthread; 28553a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 28563a1e5dd8SKonstantin Belousov switch (mode) { 28573a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_NOP: 28583a1e5dd8SKonstantin Belousov nflags = cflags; 28593a1e5dd8SKonstantin Belousov break; 28603a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_OFF: 28613a1e5dd8SKonstantin Belousov nflags = 0; 28623a1e5dd8SKonstantin Belousov break; 28633a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_SILENT: 28643a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 28653a1e5dd8SKonstantin Belousov break; 28663a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_EINTR: 28673a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 28683a1e5dd8SKonstantin Belousov break; 28693a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_ERESTART: 28703a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 28713a1e5dd8SKonstantin Belousov break; 28723a1e5dd8SKonstantin Belousov default: 28733a1e5dd8SKonstantin Belousov panic("sigdeferstop: invalid mode %x", mode); 28743a1e5dd8SKonstantin Belousov break; 28753a1e5dd8SKonstantin Belousov } 287646e47c4fSKonstantin Belousov if (cflags == nflags) 287746e47c4fSKonstantin Belousov return (SIGDEFERSTOP_VAL_NCHG); 2878a120a7a3SJohn Baldwin thread_lock(td); 28793a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | nflags; 2880a120a7a3SJohn Baldwin thread_unlock(td); 28813a1e5dd8SKonstantin Belousov return (cflags); 28823a1e5dd8SKonstantin Belousov } 28833a1e5dd8SKonstantin Belousov 28843a1e5dd8SKonstantin Belousov /* 28853a1e5dd8SKonstantin Belousov * Restores the STOP handling mode, typically permitting the delivery 28863a1e5dd8SKonstantin Belousov * of SIGSTOP for the current thread. This does not immediately 28873a1e5dd8SKonstantin Belousov * suspend if a stop was posted. Instead, the thread will suspend 28883a1e5dd8SKonstantin Belousov * either via ast() or a subsequent interruptible sleep. 28893a1e5dd8SKonstantin Belousov */ 28903a1e5dd8SKonstantin Belousov void 289146e47c4fSKonstantin Belousov sigallowstop_impl(int prev) 28923a1e5dd8SKonstantin Belousov { 28933a1e5dd8SKonstantin Belousov struct thread *td; 28943a1e5dd8SKonstantin Belousov int cflags; 28953a1e5dd8SKonstantin Belousov 289646e47c4fSKonstantin Belousov KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 28973a1e5dd8SKonstantin Belousov KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 28983a1e5dd8SKonstantin Belousov ("sigallowstop: incorrect previous mode %x", prev)); 28993a1e5dd8SKonstantin Belousov td = curthread; 29003a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 29013a1e5dd8SKonstantin Belousov if (cflags != prev) { 29023a1e5dd8SKonstantin Belousov thread_lock(td); 29033a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | prev; 29043a1e5dd8SKonstantin Belousov thread_unlock(td); 29053a1e5dd8SKonstantin Belousov } 2906a120a7a3SJohn Baldwin } 2907a120a7a3SJohn Baldwin 290881f2e906SMark Johnston enum sigstatus { 290981f2e906SMark Johnston SIGSTATUS_HANDLE, 291081f2e906SMark Johnston SIGSTATUS_HANDLED, 291181f2e906SMark Johnston SIGSTATUS_IGNORE, 291281f2e906SMark Johnston SIGSTATUS_SBDRY_STOP, 291381f2e906SMark Johnston }; 291481f2e906SMark Johnston 291581f2e906SMark Johnston /* 291681f2e906SMark Johnston * The thread has signal "sig" pending. Figure out what to do with it: 291781f2e906SMark Johnston * 291881f2e906SMark Johnston * _HANDLE -> the caller should handle the signal 291981f2e906SMark Johnston * _HANDLED -> handled internally, reload pending signal set 292081f2e906SMark Johnston * _IGNORE -> ignored, remove from the set of pending signals and try the 292181f2e906SMark Johnston * next pending signal 292281f2e906SMark Johnston * _SBDRY_STOP -> the signal should stop the thread but this is not 292381f2e906SMark Johnston * permitted in the current context 292481f2e906SMark Johnston */ 292581f2e906SMark Johnston static enum sigstatus 292681f2e906SMark Johnston sigprocess(struct thread *td, int sig) 292781f2e906SMark Johnston { 292881f2e906SMark Johnston struct proc *p; 292981f2e906SMark Johnston struct sigacts *ps; 293081f2e906SMark Johnston struct sigqueue *queue; 293181f2e906SMark Johnston ksiginfo_t ksi; 293281f2e906SMark Johnston int prop; 293381f2e906SMark Johnston 293481f2e906SMark Johnston KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig)); 293581f2e906SMark Johnston 293681f2e906SMark Johnston p = td->td_proc; 293781f2e906SMark Johnston ps = p->p_sigacts; 293881f2e906SMark Johnston mtx_assert(&ps->ps_mtx, MA_OWNED); 293981f2e906SMark Johnston PROC_LOCK_ASSERT(p, MA_OWNED); 294081f2e906SMark Johnston 294181f2e906SMark Johnston /* 294281f2e906SMark Johnston * We should allow pending but ignored signals below 294381f2e906SMark Johnston * only if there is sigwait() active, or P_TRACED was 294481f2e906SMark Johnston * on when they were posted. 294581f2e906SMark Johnston */ 294681f2e906SMark Johnston if (SIGISMEMBER(ps->ps_sigignore, sig) && 294781f2e906SMark Johnston (p->p_flag & P_TRACED) == 0 && 294881f2e906SMark Johnston (td->td_flags & TDF_SIGWAIT) == 0) { 294981f2e906SMark Johnston return (SIGSTATUS_IGNORE); 295081f2e906SMark Johnston } 295181f2e906SMark Johnston 295281f2e906SMark Johnston if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 295381f2e906SMark Johnston /* 295481f2e906SMark Johnston * If traced, always stop. 295581f2e906SMark Johnston * Remove old signal from queue before the stop. 295681f2e906SMark Johnston * XXX shrug off debugger, it causes siginfo to 295781f2e906SMark Johnston * be thrown away. 295881f2e906SMark Johnston */ 295981f2e906SMark Johnston queue = &td->td_sigqueue; 296081f2e906SMark Johnston ksiginfo_init(&ksi); 296181f2e906SMark Johnston if (sigqueue_get(queue, sig, &ksi) == 0) { 296281f2e906SMark Johnston queue = &p->p_sigqueue; 296381f2e906SMark Johnston sigqueue_get(queue, sig, &ksi); 296481f2e906SMark Johnston } 296581f2e906SMark Johnston td->td_si = ksi.ksi_info; 296681f2e906SMark Johnston 296781f2e906SMark Johnston mtx_unlock(&ps->ps_mtx); 296881f2e906SMark Johnston sig = ptracestop(td, sig, &ksi); 296981f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 297081f2e906SMark Johnston 297181f2e906SMark Johnston td->td_si.si_signo = 0; 297281f2e906SMark Johnston 297381f2e906SMark Johnston /* 297481f2e906SMark Johnston * Keep looking if the debugger discarded or 297581f2e906SMark Johnston * replaced the signal. 297681f2e906SMark Johnston */ 297781f2e906SMark Johnston if (sig == 0) 297881f2e906SMark Johnston return (SIGSTATUS_HANDLED); 297981f2e906SMark Johnston 298081f2e906SMark Johnston /* 298181f2e906SMark Johnston * If the signal became masked, re-queue it. 298281f2e906SMark Johnston */ 298381f2e906SMark Johnston if (SIGISMEMBER(td->td_sigmask, sig)) { 298481f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 298581f2e906SMark Johnston sigqueue_add(&p->p_sigqueue, sig, &ksi); 298681f2e906SMark Johnston return (SIGSTATUS_HANDLED); 298781f2e906SMark Johnston } 298881f2e906SMark Johnston 298981f2e906SMark Johnston /* 299081f2e906SMark Johnston * If the traced bit got turned off, requeue the signal and 299181f2e906SMark Johnston * reload the set of pending signals. This ensures that p_sig* 299281f2e906SMark Johnston * and p_sigact are consistent. 299381f2e906SMark Johnston */ 299481f2e906SMark Johnston if ((p->p_flag & P_TRACED) == 0) { 299581f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 299681f2e906SMark Johnston sigqueue_add(queue, sig, &ksi); 299781f2e906SMark Johnston return (SIGSTATUS_HANDLED); 299881f2e906SMark Johnston } 299981f2e906SMark Johnston } 300081f2e906SMark Johnston 300181f2e906SMark Johnston /* 300281f2e906SMark Johnston * Decide whether the signal should be returned. 300381f2e906SMark Johnston * Return the signal's number, or fall through 300481f2e906SMark Johnston * to clear it from the pending mask. 300581f2e906SMark Johnston */ 300681f2e906SMark Johnston switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 300781f2e906SMark Johnston case (intptr_t)SIG_DFL: 300881f2e906SMark Johnston /* 300981f2e906SMark Johnston * Don't take default actions on system processes. 301081f2e906SMark Johnston */ 301181f2e906SMark Johnston if (p->p_pid <= 1) { 301281f2e906SMark Johnston #ifdef DIAGNOSTIC 301381f2e906SMark Johnston /* 301481f2e906SMark Johnston * Are you sure you want to ignore SIGSEGV 301581f2e906SMark Johnston * in init? XXX 301681f2e906SMark Johnston */ 301781f2e906SMark Johnston printf("Process (pid %lu) got signal %d\n", 301881f2e906SMark Johnston (u_long)p->p_pid, sig); 301981f2e906SMark Johnston #endif 302081f2e906SMark Johnston return (SIGSTATUS_IGNORE); 302181f2e906SMark Johnston } 302281f2e906SMark Johnston 302381f2e906SMark Johnston /* 302481f2e906SMark Johnston * If there is a pending stop signal to process with 302581f2e906SMark Johnston * default action, stop here, then clear the signal. 302681f2e906SMark Johnston * Traced or exiting processes should ignore stops. 302781f2e906SMark Johnston * Additionally, a member of an orphaned process group 302881f2e906SMark Johnston * should ignore tty stops. 302981f2e906SMark Johnston */ 303081f2e906SMark Johnston prop = sigprop(sig); 303181f2e906SMark Johnston if (prop & SIGPROP_STOP) { 303281f2e906SMark Johnston mtx_unlock(&ps->ps_mtx); 303381f2e906SMark Johnston if ((p->p_flag & (P_TRACED | P_WEXIT | 303481f2e906SMark Johnston P_SINGLE_EXIT)) != 0 || ((p->p_pgrp-> 303581f2e906SMark Johnston pg_flags & PGRP_ORPHANED) != 0 && 303681f2e906SMark Johnston (prop & SIGPROP_TTYSTOP) != 0)) { 303781f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 303881f2e906SMark Johnston return (SIGSTATUS_IGNORE); 303981f2e906SMark Johnston } 304081f2e906SMark Johnston if (TD_SBDRY_INTR(td)) { 304181f2e906SMark Johnston KASSERT((td->td_flags & TDF_SBDRY) != 0, 304281f2e906SMark Johnston ("lost TDF_SBDRY")); 304381f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 304481f2e906SMark Johnston return (SIGSTATUS_SBDRY_STOP); 304581f2e906SMark Johnston } 304681f2e906SMark Johnston WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 304781f2e906SMark Johnston &p->p_mtx.lock_object, "Catching SIGSTOP"); 304881f2e906SMark Johnston sigqueue_delete(&td->td_sigqueue, sig); 304981f2e906SMark Johnston sigqueue_delete(&p->p_sigqueue, sig); 305081f2e906SMark Johnston p->p_flag |= P_STOPPED_SIG; 305181f2e906SMark Johnston p->p_xsig = sig; 305281f2e906SMark Johnston PROC_SLOCK(p); 305381f2e906SMark Johnston sig_suspend_threads(td, p, 0); 305481f2e906SMark Johnston thread_suspend_switch(td, p); 305581f2e906SMark Johnston PROC_SUNLOCK(p); 305681f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 305781f2e906SMark Johnston return (SIGSTATUS_HANDLED); 305881f2e906SMark Johnston } else if ((prop & SIGPROP_IGNORE) != 0 && 305981f2e906SMark Johnston (td->td_flags & TDF_SIGWAIT) == 0) { 306081f2e906SMark Johnston /* 306181f2e906SMark Johnston * Default action is to ignore; drop it if 306281f2e906SMark Johnston * not in kern_sigtimedwait(). 306381f2e906SMark Johnston */ 306481f2e906SMark Johnston return (SIGSTATUS_IGNORE); 306581f2e906SMark Johnston } else { 306681f2e906SMark Johnston return (SIGSTATUS_HANDLE); 306781f2e906SMark Johnston } 306881f2e906SMark Johnston 306981f2e906SMark Johnston case (intptr_t)SIG_IGN: 307081f2e906SMark Johnston if ((td->td_flags & TDF_SIGWAIT) == 0) 307181f2e906SMark Johnston return (SIGSTATUS_IGNORE); 307281f2e906SMark Johnston else 307381f2e906SMark Johnston return (SIGSTATUS_HANDLE); 307481f2e906SMark Johnston 307581f2e906SMark Johnston default: 307681f2e906SMark Johnston /* 307781f2e906SMark Johnston * This signal has an action, let postsig() process it. 307881f2e906SMark Johnston */ 307981f2e906SMark Johnston return (SIGSTATUS_HANDLE); 308081f2e906SMark Johnston } 308181f2e906SMark Johnston } 308281f2e906SMark Johnston 3083df8bae1dSRodney W. Grimes /* 3084df8bae1dSRodney W. Grimes * If the current process has received a signal (should be caught or cause 3085df8bae1dSRodney W. Grimes * termination, should interrupt current syscall), return the signal number. 3086df8bae1dSRodney W. Grimes * Stop signals with default action are processed immediately, then cleared; 3087df8bae1dSRodney W. Grimes * they aren't returned. This is checked after each entry to the system for 308881f2e906SMark Johnston * a syscall or trap (though this can usually be done without calling 308981f2e906SMark Johnston * issignal by checking the pending signal masks in cursig.) The normal call 3090df8bae1dSRodney W. Grimes * sequence is 3091df8bae1dSRodney W. Grimes * 3092e602ba25SJulian Elischer * while (sig = cursig(curthread)) 30932c42a146SMarcel Moolenaar * postsig(sig); 3094df8bae1dSRodney W. Grimes */ 30956711f10fSJohn Baldwin static int 30963cf3b9f0SJohn Baldwin issignal(struct thread *td) 3097df8bae1dSRodney W. Grimes { 3098e602ba25SJulian Elischer struct proc *p; 30994093529dSJeff Roberson sigset_t sigpending; 310081f2e906SMark Johnston int sig; 3101df8bae1dSRodney W. Grimes 3102e602ba25SJulian Elischer p = td->td_proc; 3103628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 310481f2e906SMark Johnston 3105df8bae1dSRodney W. Grimes for (;;) { 31069104847fSDavid Xu sigpending = td->td_sigqueue.sq_signals; 31076b286ee8SKonstantin Belousov SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 31084093529dSJeff Roberson SIGSETNAND(sigpending, td->td_sigmask); 31094093529dSJeff Roberson 31106f56cb8dSKonstantin Belousov if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 31116f56cb8dSKonstantin Belousov (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 31124093529dSJeff Roberson SIG_STOPSIGMASK(sigpending); 31134093529dSJeff Roberson if (SIGISEMPTY(sigpending)) /* no signal to send */ 3114df8bae1dSRodney W. Grimes return (0); 3115146fc63fSKonstantin Belousov 3116146fc63fSKonstantin Belousov /* 3117146fc63fSKonstantin Belousov * Do fast sigblock if requested by usermode. Since 3118146fc63fSKonstantin Belousov * we do know that there was a signal pending at this 3119146fc63fSKonstantin Belousov * point, set the FAST_SIGBLOCK_PEND as indicator for 3120146fc63fSKonstantin Belousov * usermode to perform a dummy call to 3121146fc63fSKonstantin Belousov * FAST_SIGBLOCK_UNBLOCK, which causes immediate 3122146fc63fSKonstantin Belousov * delivery of postponed pending signal. 3123146fc63fSKonstantin Belousov */ 3124146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 3125146fc63fSKonstantin Belousov if (td->td_sigblock_val != 0) 3126146fc63fSKonstantin Belousov SIGSETNAND(sigpending, fastblock_mask); 3127146fc63fSKonstantin Belousov if (SIGISEMPTY(sigpending)) { 3128146fc63fSKonstantin Belousov td->td_pflags |= TDP_SIGFASTPENDING; 3129146fc63fSKonstantin Belousov return (0); 3130146fc63fSKonstantin Belousov } 3131146fc63fSKonstantin Belousov } 3132146fc63fSKonstantin Belousov 3133b7a25e63SKonstantin Belousov if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 3134b7a25e63SKonstantin Belousov (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 3135b7a25e63SKonstantin Belousov SIGISMEMBER(sigpending, SIGSTOP)) { 3136b7a25e63SKonstantin Belousov /* 3137b7a25e63SKonstantin Belousov * If debugger just attached, always consume 3138b7a25e63SKonstantin Belousov * SIGSTOP from ptrace(PT_ATTACH) first, to 3139b7a25e63SKonstantin Belousov * execute the debugger attach ritual in 3140b7a25e63SKonstantin Belousov * order. 3141b7a25e63SKonstantin Belousov */ 3142b7a25e63SKonstantin Belousov td->td_dbgflags |= TDB_FSTP; 314381f2e906SMark Johnston SIGEMPTYSET(sigpending); 314481f2e906SMark Johnston SIGADDSET(sigpending, SIGSTOP); 3145b7a25e63SKonstantin Belousov } 31462a024a2bSSean Eric Fagan 314781f2e906SMark Johnston SIG_FOREACH(sig, &sigpending) { 314881f2e906SMark Johnston switch (sigprocess(td, sig)) { 314981f2e906SMark Johnston case SIGSTATUS_HANDLE: 315081f2e906SMark Johnston return (sig); 315181f2e906SMark Johnston case SIGSTATUS_HANDLED: 315281f2e906SMark Johnston goto next; 315381f2e906SMark Johnston case SIGSTATUS_IGNORE: 31549104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 31556b286ee8SKonstantin Belousov sigqueue_delete(&p->p_sigqueue, sig); 315681f2e906SMark Johnston break; 315781f2e906SMark Johnston case SIGSTATUS_SBDRY_STOP: 315846e47c4fSKonstantin Belousov return (-1); 315946e47c4fSKonstantin Belousov } 3160df8bae1dSRodney W. Grimes } 3161b7a25e63SKonstantin Belousov next:; 3162df8bae1dSRodney W. Grimes } 3163df8bae1dSRodney W. Grimes } 3164df8bae1dSRodney W. Grimes 3165e574e444SDavid Xu void 3166e574e444SDavid Xu thread_stopped(struct proc *p) 3167e574e444SDavid Xu { 3168e574e444SDavid Xu int n; 3169e574e444SDavid Xu 3170e574e444SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 31717b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 3172e574e444SDavid Xu n = p->p_suspcount; 31737c9a98f1SDavid Xu if (p == curproc) 3174e574e444SDavid Xu n++; 3175e574e444SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 31767b4a950aSDavid Xu PROC_SUNLOCK(p); 3177407948a5SDavid Xu p->p_flag &= ~P_WAITED; 3178e574e444SDavid Xu PROC_LOCK(p->p_pptr); 31797f96995eSDavid Xu childproc_stopped(p, (p->p_flag & P_TRACED) ? 3180ebceaf6dSDavid Xu CLD_TRAPPED : CLD_STOPPED); 3181e574e444SDavid Xu PROC_UNLOCK(p->p_pptr); 31827b4a950aSDavid Xu PROC_SLOCK(p); 3183e574e444SDavid Xu } 3184e574e444SDavid Xu } 3185e574e444SDavid Xu 3186df8bae1dSRodney W. Grimes /* 3187df8bae1dSRodney W. Grimes * Take the action for the specified signal 3188df8bae1dSRodney W. Grimes * from the current set of pending signals. 3189df8bae1dSRodney W. Grimes */ 319075c586a4SKonstantin Belousov int 31910167b33bSKonstantin Belousov postsig(int sig) 3192df8bae1dSRodney W. Grimes { 31930167b33bSKonstantin Belousov struct thread *td; 31940167b33bSKonstantin Belousov struct proc *p; 3195628d2653SJohn Baldwin struct sigacts *ps; 31962c42a146SMarcel Moolenaar sig_t action; 31979104847fSDavid Xu ksiginfo_t ksi; 3198e442f29fSKonstantin Belousov sigset_t returnmask; 3199df8bae1dSRodney W. Grimes 32002c42a146SMarcel Moolenaar KASSERT(sig != 0, ("postsig")); 32015526d2d9SEivind Eklund 32020167b33bSKonstantin Belousov td = curthread; 32030167b33bSKonstantin Belousov p = td->td_proc; 32042ad7d304SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3205628d2653SJohn Baldwin ps = p->p_sigacts; 320690af4afaSJohn Baldwin mtx_assert(&ps->ps_mtx, MA_OWNED); 32075da49fcbSDavid Xu ksiginfo_init(&ksi); 32086b286ee8SKonstantin Belousov if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 32096b286ee8SKonstantin Belousov sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 321075c586a4SKonstantin Belousov return (0); 32119104847fSDavid Xu ksi.ksi_signo = sig; 321256c06c4bSDavid Xu if (ksi.ksi_code == SI_TIMER) 321356c06c4bSDavid Xu itimer_accept(p, ksi.ksi_timerid, &ksi); 32142c42a146SMarcel Moolenaar action = ps->ps_sigact[_SIG_IDX(sig)]; 3215df8bae1dSRodney W. Grimes #ifdef KTRACE 3216374a15aaSJohn Baldwin if (KTRPOINT(td, KTR_PSIG)) 32175e26dcb5SJohn Baldwin ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 321861009552SJilles Tjoelker &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 3219df8bae1dSRodney W. Grimes #endif 32202a024a2bSSean Eric Fagan 32218460a577SJohn Birrell if (action == SIG_DFL) { 3222df8bae1dSRodney W. Grimes /* 3223df8bae1dSRodney W. Grimes * Default action, where the default is to kill 3224df8bae1dSRodney W. Grimes * the process. (Other cases were ignored above.) 3225df8bae1dSRodney W. Grimes */ 322690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 322786be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi.ksi_info); 3228b40ce416SJulian Elischer sigexit(td, sig); 3229df8bae1dSRodney W. Grimes /* NOTREACHED */ 3230df8bae1dSRodney W. Grimes } else { 3231df8bae1dSRodney W. Grimes /* 3232df8bae1dSRodney W. Grimes * If we get here, the signal must be caught. 3233df8bae1dSRodney W. Grimes */ 3234cd735d8fSKonstantin Belousov KASSERT(action != SIG_IGN, ("postsig action %p", action)); 3235cd735d8fSKonstantin Belousov KASSERT(!SIGISMEMBER(td->td_sigmask, sig), 3236cd735d8fSKonstantin Belousov ("postsig action: blocked sig %d", sig)); 3237cd735d8fSKonstantin Belousov 3238df8bae1dSRodney W. Grimes /* 3239df8bae1dSRodney W. Grimes * Set the new mask value and also defer further 3240645682fdSLuoqi Chen * occurrences of this signal. 3241df8bae1dSRodney W. Grimes * 3242645682fdSLuoqi Chen * Special case: user has done a sigsuspend. Here the 3243df8bae1dSRodney W. Grimes * current mask is not of interest, but rather the 3244645682fdSLuoqi Chen * mask from before the sigsuspend is what we want 3245df8bae1dSRodney W. Grimes * restored after the signal processing is completed. 3246df8bae1dSRodney W. Grimes */ 32475e26dcb5SJohn Baldwin if (td->td_pflags & TDP_OLDMASK) { 32484093529dSJeff Roberson returnmask = td->td_oldsigmask; 32495e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_OLDMASK; 3250df8bae1dSRodney W. Grimes } else 32514093529dSJeff Roberson returnmask = td->td_sigmask; 32522c42a146SMarcel Moolenaar 3253c90c9021SEd Schouten if (p->p_sig == sig) { 32546626c604SJulian Elischer p->p_sig = 0; 3255df8bae1dSRodney W. Grimes } 32569104847fSDavid Xu (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3257e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 3258df8bae1dSRodney W. Grimes } 325975c586a4SKonstantin Belousov return (1); 3260df8bae1dSRodney W. Grimes } 3261df8bae1dSRodney W. Grimes 32620c82fb26SKonstantin Belousov int 32630c82fb26SKonstantin Belousov sig_ast_checksusp(struct thread *td) 32640c82fb26SKonstantin Belousov { 32653d277851SKonstantin Belousov struct proc *p __diagused; 32660c82fb26SKonstantin Belousov int ret; 32670c82fb26SKonstantin Belousov 32680c82fb26SKonstantin Belousov p = td->td_proc; 32690c82fb26SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 32700c82fb26SKonstantin Belousov 32710c82fb26SKonstantin Belousov if ((td->td_flags & TDF_NEEDSUSPCHK) == 0) 32720c82fb26SKonstantin Belousov return (0); 32730c82fb26SKonstantin Belousov 32740c82fb26SKonstantin Belousov ret = thread_suspend_check(1); 32750c82fb26SKonstantin Belousov MPASS(ret == 0 || ret == EINTR || ret == ERESTART); 32760c82fb26SKonstantin Belousov return (ret); 32770c82fb26SKonstantin Belousov } 32780c82fb26SKonstantin Belousov 32790c82fb26SKonstantin Belousov int 32800c82fb26SKonstantin Belousov sig_ast_needsigchk(struct thread *td) 32810c82fb26SKonstantin Belousov { 32820c82fb26SKonstantin Belousov struct proc *p; 32830c82fb26SKonstantin Belousov struct sigacts *ps; 32840c82fb26SKonstantin Belousov int ret, sig; 32850c82fb26SKonstantin Belousov 32860c82fb26SKonstantin Belousov p = td->td_proc; 32870c82fb26SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 32880c82fb26SKonstantin Belousov 32890c82fb26SKonstantin Belousov if ((td->td_flags & TDF_NEEDSIGCHK) == 0) 32900c82fb26SKonstantin Belousov return (0); 32910c82fb26SKonstantin Belousov 32920c82fb26SKonstantin Belousov ps = p->p_sigacts; 32930c82fb26SKonstantin Belousov mtx_lock(&ps->ps_mtx); 32940c82fb26SKonstantin Belousov sig = cursig(td); 32950c82fb26SKonstantin Belousov if (sig == -1) { 32960c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 32970c82fb26SKonstantin Belousov KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY")); 32980c82fb26SKonstantin Belousov KASSERT(TD_SBDRY_INTR(td), 32990c82fb26SKonstantin Belousov ("lost TDF_SERESTART of TDF_SEINTR")); 33000c82fb26SKonstantin Belousov KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != 33010c82fb26SKonstantin Belousov (TDF_SEINTR | TDF_SERESTART), 33020c82fb26SKonstantin Belousov ("both TDF_SEINTR and TDF_SERESTART")); 33030c82fb26SKonstantin Belousov ret = TD_SBDRY_ERRNO(td); 33040c82fb26SKonstantin Belousov } else if (sig != 0) { 33050c82fb26SKonstantin Belousov ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART; 33060c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 33070c82fb26SKonstantin Belousov } else { 33080c82fb26SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 33090c82fb26SKonstantin Belousov ret = 0; 33100c82fb26SKonstantin Belousov } 33110c82fb26SKonstantin Belousov 33120c82fb26SKonstantin Belousov /* 33130c82fb26SKonstantin Belousov * Do not go into sleep if this thread was the ptrace(2) 33140c82fb26SKonstantin Belousov * attach leader. cursig() consumed SIGSTOP from PT_ATTACH, 33150c82fb26SKonstantin Belousov * but we usually act on the signal by interrupting sleep, and 33160c82fb26SKonstantin Belousov * should do that here as well. 33170c82fb26SKonstantin Belousov */ 33180c82fb26SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0) { 33190c82fb26SKonstantin Belousov if (ret == 0) 33200c82fb26SKonstantin Belousov ret = EINTR; 33210c82fb26SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 33220c82fb26SKonstantin Belousov } 33230c82fb26SKonstantin Belousov 33240c82fb26SKonstantin Belousov return (ret); 33250c82fb26SKonstantin Belousov } 33260c82fb26SKonstantin Belousov 33270400be45SKonstantin Belousov int 33280400be45SKonstantin Belousov sig_intr(void) 33290400be45SKonstantin Belousov { 33300400be45SKonstantin Belousov struct thread *td; 33310400be45SKonstantin Belousov struct proc *p; 33320400be45SKonstantin Belousov int ret; 33330400be45SKonstantin Belousov 33340400be45SKonstantin Belousov td = curthread; 3335203dda8aSKonstantin Belousov if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0) 3336203dda8aSKonstantin Belousov return (0); 3337203dda8aSKonstantin Belousov 33380400be45SKonstantin Belousov p = td->td_proc; 33390400be45SKonstantin Belousov 33400400be45SKonstantin Belousov PROC_LOCK(p); 33410400be45SKonstantin Belousov ret = sig_ast_checksusp(td); 33420400be45SKonstantin Belousov if (ret == 0) 33430400be45SKonstantin Belousov ret = sig_ast_needsigchk(td); 33440400be45SKonstantin Belousov PROC_UNLOCK(p); 33450400be45SKonstantin Belousov return (ret); 33460400be45SKonstantin Belousov } 33470400be45SKonstantin Belousov 3348244ab566SKonstantin Belousov bool 3349244ab566SKonstantin Belousov curproc_sigkilled(void) 3350244ab566SKonstantin Belousov { 3351244ab566SKonstantin Belousov struct thread *td; 3352244ab566SKonstantin Belousov struct proc *p; 3353244ab566SKonstantin Belousov struct sigacts *ps; 3354244ab566SKonstantin Belousov bool res; 3355244ab566SKonstantin Belousov 3356244ab566SKonstantin Belousov td = curthread; 3357244ab566SKonstantin Belousov if ((td->td_flags & TDF_NEEDSIGCHK) == 0) 3358244ab566SKonstantin Belousov return (false); 3359244ab566SKonstantin Belousov 3360244ab566SKonstantin Belousov p = td->td_proc; 3361244ab566SKonstantin Belousov PROC_LOCK(p); 3362244ab566SKonstantin Belousov ps = p->p_sigacts; 3363244ab566SKonstantin Belousov mtx_lock(&ps->ps_mtx); 3364244ab566SKonstantin Belousov res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) || 3365244ab566SKonstantin Belousov SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL); 3366244ab566SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 3367244ab566SKonstantin Belousov PROC_UNLOCK(p); 3368244ab566SKonstantin Belousov return (res); 3369244ab566SKonstantin Belousov } 3370244ab566SKonstantin Belousov 3371a70e9a13SKonstantin Belousov void 3372a70e9a13SKonstantin Belousov proc_wkilled(struct proc *p) 3373a70e9a13SKonstantin Belousov { 3374a70e9a13SKonstantin Belousov 3375a70e9a13SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 3376a70e9a13SKonstantin Belousov if ((p->p_flag & P_WKILLED) == 0) { 3377a70e9a13SKonstantin Belousov p->p_flag |= P_WKILLED; 3378a70e9a13SKonstantin Belousov /* 3379a70e9a13SKonstantin Belousov * Notify swapper that there is a process to swap in. 3380a70e9a13SKonstantin Belousov * The notification is racy, at worst it would take 10 3381a70e9a13SKonstantin Belousov * seconds for the swapper process to notice. 3382a70e9a13SKonstantin Belousov */ 3383a70e9a13SKonstantin Belousov if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0) 3384a70e9a13SKonstantin Belousov wakeup(&proc0); 3385a70e9a13SKonstantin Belousov } 3386a70e9a13SKonstantin Belousov } 3387a70e9a13SKonstantin Belousov 3388df8bae1dSRodney W. Grimes /* 3389df8bae1dSRodney W. Grimes * Kill the current process for stated reason. 3390df8bae1dSRodney W. Grimes */ 339126f9a767SRodney W. Grimes void 3392fe20aaecSRyan Libby killproc(struct proc *p, const char *why) 3393df8bae1dSRodney W. Grimes { 33949081e5e8SJohn Baldwin 33959081e5e8SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3396c3209846SPawel Jakub Dawidek CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3397c3209846SPawel Jakub Dawidek p->p_comm); 3398af8beccaSKristof Provost log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n", 33994ae4822dSKristof Provost p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id, 3400de2d0d29SKristof Provost p->p_ucred->cr_uid, why); 3401a70e9a13SKonstantin Belousov proc_wkilled(p); 34028451d0ddSKip Macy kern_psignal(p, SIGKILL); 3403df8bae1dSRodney W. Grimes } 3404df8bae1dSRodney W. Grimes 3405df8bae1dSRodney W. Grimes /* 3406df8bae1dSRodney W. Grimes * Force the current process to exit with the specified signal, dumping core 3407df8bae1dSRodney W. Grimes * if appropriate. We bypass the normal tests for masked and caught signals, 3408df8bae1dSRodney W. Grimes * allowing unrecoverable failures to terminate the process without changing 3409df8bae1dSRodney W. Grimes * signal state. Mark the accounting record with the signal termination. 3410df8bae1dSRodney W. Grimes * If dumping core, save the signal number for the debugger. Calls exit and 3411df8bae1dSRodney W. Grimes * does not return. 3412df8bae1dSRodney W. Grimes */ 341326f9a767SRodney W. Grimes void 3414e052a8b9SEd Maste sigexit(struct thread *td, int sig) 3415df8bae1dSRodney W. Grimes { 3416b40ce416SJulian Elischer struct proc *p = td->td_proc; 3417df8bae1dSRodney W. Grimes 3418628d2653SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3419df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 3420f97c3df1SDavid Schultz /* 3421f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 3422f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 3423f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 3424f97c3df1SDavid Schultz * change out from under it. 3425f97c3df1SDavid Schultz * 3426f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 3427f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 3428f97c3df1SDavid Schultz */ 3429fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_CORE) && 3430fd50a707SBrooks Davis thread_single(p, SINGLE_NO_EXIT) == 0) { 34312c42a146SMarcel Moolenaar p->p_sig = sig; 3432c364e17eSAndrey A. Chernov /* 3433c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 3434c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 3435c364e17eSAndrey A. Chernov * these messages.) 3436c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 34374ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 3438c364e17eSAndrey A. Chernov */ 3439b40ce416SJulian Elischer if (coredump(td) == 0) 34402c42a146SMarcel Moolenaar sig |= WCOREFLAG; 344157308494SJoerg Wunsch if (kern_logsigexit) 344257308494SJoerg Wunsch log(LOG_INFO, 3443af8beccaSKristof Provost "pid %d (%s), jid %d, uid %d: exited on " 34444ae4822dSKristof Provost "signal %d%s\n", p->p_pid, p->p_comm, 34454ae4822dSKristof Provost p->p_ucred->cr_prison->pr_id, 3446de2d0d29SKristof Provost td->td_ucred->cr_uid, 34472c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 34482c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 34494ae89b95SJohn Baldwin } else 3450628d2653SJohn Baldwin PROC_UNLOCK(p); 3451b4490c6eSKonstantin Belousov exit1(td, 0, sig); 3452df8bae1dSRodney W. Grimes /* NOTREACHED */ 3453df8bae1dSRodney W. Grimes } 3454df8bae1dSRodney W. Grimes 3455ebceaf6dSDavid Xu /* 34567f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 34577f96995eSDavid Xu * is changed. 3458ebceaf6dSDavid Xu */ 34597f96995eSDavid Xu static void 34607f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 3461ebceaf6dSDavid Xu { 3462ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 3463ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3464ebceaf6dSDavid Xu 3465ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 3466ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 3467ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 34687f96995eSDavid Xu p->p_ksi->ksi_status = status; 3469ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 3470ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3471ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 3472ebceaf6dSDavid Xu return; 3473ebceaf6dSDavid Xu } 3474ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3475ebceaf6dSDavid Xu } 3476ebceaf6dSDavid Xu 34777f96995eSDavid Xu static void 3478b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig) 34797f96995eSDavid Xu { 34807f96995eSDavid Xu struct sigacts *ps; 34817f96995eSDavid Xu 34827f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 34837f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 34847f96995eSDavid Xu 34857f96995eSDavid Xu /* 34867f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 34877f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 34887f96995eSDavid Xu * that parent will awake, because parent may masked 34897f96995eSDavid Xu * the signal. 34907f96995eSDavid Xu */ 34917f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 34927f96995eSDavid Xu wakeup(p->p_pptr); 34937f96995eSDavid Xu 34947f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 34957f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 34967f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 34977f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 3498b20a9aa9SJilles Tjoelker sigparent(p, reason, sig); 34997f96995eSDavid Xu } else 35007f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 35017f96995eSDavid Xu } 35027f96995eSDavid Xu 35037f96995eSDavid Xu void 35047f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 35057f96995eSDavid Xu { 3506b4490c6eSKonstantin Belousov 3507b4490c6eSKonstantin Belousov childproc_jobstate(p, reason, p->p_xsig); 35087f96995eSDavid Xu } 35097f96995eSDavid Xu 3510ebceaf6dSDavid Xu void 3511ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3512ebceaf6dSDavid Xu { 35137f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3514ebceaf6dSDavid Xu } 3515ebceaf6dSDavid Xu 3516ebceaf6dSDavid Xu void 3517ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3518ebceaf6dSDavid Xu { 3519b4490c6eSKonstantin Belousov int reason, status; 3520ebceaf6dSDavid Xu 3521b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 3522b4490c6eSKonstantin Belousov reason = CLD_DUMPED; 3523b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3524b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 3525b4490c6eSKonstantin Belousov reason = CLD_KILLED; 3526b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3527b4490c6eSKonstantin Belousov } else { 3528b4490c6eSKonstantin Belousov reason = CLD_EXITED; 3529b4490c6eSKonstantin Belousov status = p->p_xexit; 3530b4490c6eSKonstantin Belousov } 35317f96995eSDavid Xu /* 35327f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 35337f96995eSDavid Xu * done in exit1(). 35347f96995eSDavid Xu */ 35357f96995eSDavid Xu sigparent(p, reason, status); 3536ebceaf6dSDavid Xu } 3537ebceaf6dSDavid Xu 3538f1fe1e02SMariusz Zaborski #define MAX_NUM_CORE_FILES 100000 3539cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES 3540cc37baeaSStephen J. Kiernan #define NUM_CORE_FILES 5 3541cc37baeaSStephen J. Kiernan #endif 3542cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3543cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES; 3544e7228204SAlfred Perlstein 3545e7228204SAlfred Perlstein static int 3546e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3547e7228204SAlfred Perlstein { 3548e7228204SAlfred Perlstein int error; 3549e7228204SAlfred Perlstein int new_val; 3550e7228204SAlfred Perlstein 3551c5105012SKonstantin Belousov new_val = num_cores; 3552e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3553e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3554e7228204SAlfred Perlstein return (error); 3555cc37baeaSStephen J. Kiernan if (new_val > MAX_NUM_CORE_FILES) 3556cc37baeaSStephen J. Kiernan new_val = MAX_NUM_CORE_FILES; 3557e7228204SAlfred Perlstein if (new_val < 0) 3558e7228204SAlfred Perlstein new_val = 0; 3559e7228204SAlfred Perlstein num_cores = new_val; 3560e7228204SAlfred Perlstein return (0); 3561e7228204SAlfred Perlstein } 35627029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores, 3563*fe27f1dbSAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int), 35647029da5cSPawel Biernacki sysctl_debug_num_cores_check, "I", 35656cad1a5dSMariusz Zaborski "Maximum number of generated process corefiles while using index format"); 3566e7228204SAlfred Perlstein 35676026dcd7SMark Johnston #define GZIP_SUFFIX ".gz" 35686026dcd7SMark Johnston #define ZSTD_SUFFIX ".zst" 3569aa14e9b7SMark Johnston 357078f57a9cSMark Johnston int compress_user_cores = 0; 3571e7228204SAlfred Perlstein 357278f57a9cSMark Johnston static int 357378f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS) 357478f57a9cSMark Johnston { 357578f57a9cSMark Johnston int error, val; 357678f57a9cSMark Johnston 357778f57a9cSMark Johnston val = compress_user_cores; 357878f57a9cSMark Johnston error = sysctl_handle_int(oidp, &val, 0, req); 357978f57a9cSMark Johnston if (error != 0 || req->newptr == NULL) 358078f57a9cSMark Johnston return (error); 358178f57a9cSMark Johnston if (val != 0 && !compressor_avail(val)) 358278f57a9cSMark Johnston return (EINVAL); 358378f57a9cSMark Johnston compress_user_cores = val; 358478f57a9cSMark Johnston return (error); 358578f57a9cSMark Johnston } 35867029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores, 35877029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int), 35887029da5cSPawel Biernacki sysctl_compress_user_cores, "I", 35896026dcd7SMark Johnston "Enable compression of user corefiles (" 35906026dcd7SMark Johnston __XSTRING(COMPRESS_GZIP) " = gzip, " 35916026dcd7SMark Johnston __XSTRING(COMPRESS_ZSTD) " = zstd)"); 359278f57a9cSMark Johnston 359378f57a9cSMark Johnston int compress_user_cores_level = 6; 359478f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN, 359578f57a9cSMark Johnston &compress_user_cores_level, 0, 359678f57a9cSMark Johnston "Corefile compression level"); 3597e7228204SAlfred Perlstein 35985bc0ff88SMateusz Guzik /* 35995bc0ff88SMateusz Guzik * Protect the access to corefilename[] by allproc_lock. 36005bc0ff88SMateusz Guzik */ 36015bc0ff88SMateusz Guzik #define corefilename_lock allproc_lock 36025bc0ff88SMateusz Guzik 36036d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3604fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 36055bc0ff88SMateusz Guzik 36065bc0ff88SMateusz Guzik static int 36075bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 36085bc0ff88SMateusz Guzik { 36095bc0ff88SMateusz Guzik int error; 36105bc0ff88SMateusz Guzik 36115bc0ff88SMateusz Guzik sx_xlock(&corefilename_lock); 36125bc0ff88SMateusz Guzik error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 36135bc0ff88SMateusz Guzik req); 36145bc0ff88SMateusz Guzik sx_xunlock(&corefilename_lock); 36155bc0ff88SMateusz Guzik 36165bc0ff88SMateusz Guzik return (error); 36175bc0ff88SMateusz Guzik } 3618fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 36195bc0ff88SMateusz Guzik CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 36205bc0ff88SMateusz Guzik "Process corefile name format string"); 3621c5edb423SSean Eric Fagan 36220dea6e3cSMariusz Zaborski static void 36230dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp) 36240dea6e3cSMariusz Zaborski { 36250dea6e3cSMariusz Zaborski 3626b249ce48SMateusz Guzik VOP_UNLOCK(vp); 36270dea6e3cSMariusz Zaborski vn_close(vp, FWRITE, td->td_ucred, td); 36280dea6e3cSMariusz Zaborski } 36290dea6e3cSMariusz Zaborski 36300dea6e3cSMariusz Zaborski /* 36310dea6e3cSMariusz Zaborski * If the core format has a %I in it, then we need to check 36320dea6e3cSMariusz Zaborski * for existing corefiles before defining a name. 3633f1fe1e02SMariusz Zaborski * To do this we iterate over 0..ncores to find a 36340dea6e3cSMariusz Zaborski * non-existing core file name to use. If all core files are 36350dea6e3cSMariusz Zaborski * already used we choose the oldest one. 36360dea6e3cSMariusz Zaborski */ 36370dea6e3cSMariusz Zaborski static int 36380dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos, 3639f1fe1e02SMariusz Zaborski int indexlen, int ncores, struct vnode **vpp) 36400dea6e3cSMariusz Zaborski { 36410dea6e3cSMariusz Zaborski struct vnode *oldvp, *nextvp, *vp; 36420dea6e3cSMariusz Zaborski struct vattr vattr; 36430dea6e3cSMariusz Zaborski struct nameidata nd; 36440dea6e3cSMariusz Zaborski int error, i, flags, oflags, cmode; 3645f1fe1e02SMariusz Zaborski char ch; 36460dea6e3cSMariusz Zaborski struct timespec lasttime; 36470dea6e3cSMariusz Zaborski 36480dea6e3cSMariusz Zaborski nextvp = oldvp = NULL; 36490dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 36500dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 36510dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 36520dea6e3cSMariusz Zaborski 3653f1fe1e02SMariusz Zaborski for (i = 0; i < ncores; i++) { 36540dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 3655f1fe1e02SMariusz Zaborski 3656f1fe1e02SMariusz Zaborski ch = name[indexpos + indexlen]; 3657f1fe1e02SMariusz Zaborski (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen, 3658f1fe1e02SMariusz Zaborski i); 3659f1fe1e02SMariusz Zaborski name[indexpos + indexlen] = ch; 36600dea6e3cSMariusz Zaborski 36617e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 36620dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 36630dea6e3cSMariusz Zaborski NULL); 36640dea6e3cSMariusz Zaborski if (error != 0) 36650dea6e3cSMariusz Zaborski break; 36660dea6e3cSMariusz Zaborski 36670dea6e3cSMariusz Zaborski vp = nd.ni_vp; 36680dea6e3cSMariusz Zaborski NDFREE(&nd, NDF_ONLY_PNBUF); 36690dea6e3cSMariusz Zaborski if ((flags & O_CREAT) == O_CREAT) { 36700dea6e3cSMariusz Zaborski nextvp = vp; 36710dea6e3cSMariusz Zaborski break; 36720dea6e3cSMariusz Zaborski } 36730dea6e3cSMariusz Zaborski 36740dea6e3cSMariusz Zaborski error = VOP_GETATTR(vp, &vattr, td->td_ucred); 36750dea6e3cSMariusz Zaborski if (error != 0) { 36760dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 36770dea6e3cSMariusz Zaborski break; 36780dea6e3cSMariusz Zaborski } 36790dea6e3cSMariusz Zaborski 36800dea6e3cSMariusz Zaborski if (oldvp == NULL || 36810dea6e3cSMariusz Zaborski lasttime.tv_sec > vattr.va_mtime.tv_sec || 36820dea6e3cSMariusz Zaborski (lasttime.tv_sec == vattr.va_mtime.tv_sec && 36830dea6e3cSMariusz Zaborski lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) { 36840dea6e3cSMariusz Zaborski if (oldvp != NULL) 3685e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 36860dea6e3cSMariusz Zaborski oldvp = vp; 3687e298466eSAndriy Gapon VOP_UNLOCK(oldvp); 36880dea6e3cSMariusz Zaborski lasttime = vattr.va_mtime; 36890dea6e3cSMariusz Zaborski } else { 36900dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 36910dea6e3cSMariusz Zaborski } 36920dea6e3cSMariusz Zaborski } 36930dea6e3cSMariusz Zaborski 36940dea6e3cSMariusz Zaborski if (oldvp != NULL) { 369589f2ab06SKonstantin Belousov if (nextvp == NULL) { 369689f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) { 369789f2ab06SKonstantin Belousov error = EFAULT; 3698e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 369989f2ab06SKonstantin Belousov } else { 370089f2ab06SKonstantin Belousov nextvp = oldvp; 3701e298466eSAndriy Gapon error = vn_lock(nextvp, LK_EXCLUSIVE); 3702e298466eSAndriy Gapon if (error != 0) { 3703e298466eSAndriy Gapon vn_close(nextvp, FWRITE, td->td_ucred, 3704e298466eSAndriy Gapon td); 3705e298466eSAndriy Gapon nextvp = NULL; 3706e298466eSAndriy Gapon } 370789f2ab06SKonstantin Belousov } 370889f2ab06SKonstantin Belousov } else { 3709e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 371089f2ab06SKonstantin Belousov } 37110dea6e3cSMariusz Zaborski } 37120dea6e3cSMariusz Zaborski if (error != 0) { 37130dea6e3cSMariusz Zaborski if (nextvp != NULL) 37140dea6e3cSMariusz Zaborski vnode_close_locked(td, oldvp); 37150dea6e3cSMariusz Zaborski } else { 37160dea6e3cSMariusz Zaborski *vpp = nextvp; 37170dea6e3cSMariusz Zaborski } 37180dea6e3cSMariusz Zaborski 37190dea6e3cSMariusz Zaborski return (error); 37200dea6e3cSMariusz Zaborski } 37210dea6e3cSMariusz Zaborski 3722c5edb423SSean Eric Fagan /* 3723c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3724c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3725c345faeaSPawel Jakub Dawidek * and open/create core file. 3726c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3727c5edb423SSean Eric Fagan * %N name of process ("name") 3728c5edb423SSean Eric Fagan * %P process id (pid) 3729c5edb423SSean Eric Fagan * %U user id (uid) 3730c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3731c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3732c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3733c5edb423SSean Eric Fagan */ 3734c345faeaSPawel Jakub Dawidek static int 3735c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 37369d3ecb7eSEric van Gyzen int compress, int signum, struct vnode **vpp, char **namep) 37378b43b535SAlfred Perlstein { 373836b208e0SRobert Watson struct sbuf sb; 37390dea6e3cSMariusz Zaborski struct nameidata nd; 374036b208e0SRobert Watson const char *format; 3741c345faeaSPawel Jakub Dawidek char *hostname, *name; 3742f1fe1e02SMariusz Zaborski int cmode, error, flags, i, indexpos, indexlen, oflags, ncores; 3743c5edb423SSean Eric Fagan 3744b7402d82SAlfred Perlstein hostname = NULL; 37458b43b535SAlfred Perlstein format = corefilename; 3746086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3747f1fe1e02SMariusz Zaborski indexlen = 0; 3748e7228204SAlfred Perlstein indexpos = -1; 3749f1fe1e02SMariusz Zaborski ncores = num_cores; 3750c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 37515bc0ff88SMateusz Guzik sx_slock(&corefilename_lock); 375229146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3753c5edb423SSean Eric Fagan switch (format[i]) { 3754c5edb423SSean Eric Fagan case '%': /* Format character */ 3755c5edb423SSean Eric Fagan i++; 3756c5edb423SSean Eric Fagan switch (format[i]) { 3757c5edb423SSean Eric Fagan case '%': 375836b208e0SRobert Watson sbuf_putc(&sb, '%'); 3759c5edb423SSean Eric Fagan break; 3760e7228204SAlfred Perlstein case 'H': /* hostname */ 3761b7402d82SAlfred Perlstein if (hostname == NULL) { 3762b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3763086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3764b7402d82SAlfred Perlstein } 3765e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3766b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3767e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3768e7228204SAlfred Perlstein break; 3769e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3770f1fe1e02SMariusz Zaborski if (indexpos != -1) { 3771f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%%I"); 3772f1fe1e02SMariusz Zaborski break; 3773f1fe1e02SMariusz Zaborski } 3774f1fe1e02SMariusz Zaborski 3775f1fe1e02SMariusz Zaborski indexpos = sbuf_len(&sb); 3776f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%u", ncores - 1); 3777f1fe1e02SMariusz Zaborski indexlen = sbuf_len(&sb) - indexpos; 3778e7228204SAlfred Perlstein break; 3779c5edb423SSean Eric Fagan case 'N': /* process name */ 3780c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3781c5edb423SSean Eric Fagan break; 3782c5edb423SSean Eric Fagan case 'P': /* process id */ 378336b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3784c5edb423SSean Eric Fagan break; 37859d3ecb7eSEric van Gyzen case 'S': /* signal number */ 37869d3ecb7eSEric van Gyzen sbuf_printf(&sb, "%i", signum); 37879d3ecb7eSEric van Gyzen break; 3788c5edb423SSean Eric Fagan case 'U': /* user id */ 378936b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3790c5edb423SSean Eric Fagan break; 3791c5edb423SSean Eric Fagan default: 37928b43b535SAlfred Perlstein log(LOG_ERR, 379336b208e0SRobert Watson "Unknown format character %c in " 379436b208e0SRobert Watson "corename `%s'\n", format[i], format); 379529146f1aSPawel Jakub Dawidek break; 3796c5edb423SSean Eric Fagan } 3797c5edb423SSean Eric Fagan break; 3798c5edb423SSean Eric Fagan default: 379936b208e0SRobert Watson sbuf_putc(&sb, format[i]); 38006c08be2bSPawel Jakub Dawidek break; 3801c5edb423SSean Eric Fagan } 3802c5edb423SSean Eric Fagan } 38035bc0ff88SMateusz Guzik sx_sunlock(&corefilename_lock); 3804b7402d82SAlfred Perlstein free(hostname, M_TEMP); 380578f57a9cSMark Johnston if (compress == COMPRESS_GZIP) 38066026dcd7SMark Johnston sbuf_printf(&sb, GZIP_SUFFIX); 38076026dcd7SMark Johnston else if (compress == COMPRESS_ZSTD) 38086026dcd7SMark Johnston sbuf_printf(&sb, ZSTD_SUFFIX); 38094d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 381036b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3811c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 3812b7402d82SAlfred Perlstein sbuf_delete(&sb); 3813c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3814c345faeaSPawel Jakub Dawidek return (ENOMEM); 3815c5edb423SSean Eric Fagan } 381636b208e0SRobert Watson sbuf_finish(&sb); 381736b208e0SRobert Watson sbuf_delete(&sb); 3818e7228204SAlfred Perlstein 3819e7228204SAlfred Perlstein if (indexpos != -1) { 3820f1fe1e02SMariusz Zaborski error = corefile_open_last(td, name, indexpos, indexlen, ncores, 3821f1fe1e02SMariusz Zaborski vpp); 38220dea6e3cSMariusz Zaborski if (error != 0) { 3823e7228204SAlfred Perlstein log(LOG_ERR, 3824e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 3825e7228204SAlfred Perlstein "on initial open test, error = %d\n", 3826c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 3827c345faeaSPawel Jakub Dawidek } 38280dea6e3cSMariusz Zaborski } else { 38290dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 38300dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 38310dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 38320dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 383389f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) 383489f2ab06SKonstantin Belousov flags |= O_EXCL; 38350dea6e3cSMariusz Zaborski 38367e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 38370dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 38380dea6e3cSMariusz Zaborski NULL); 38390dea6e3cSMariusz Zaborski if (error == 0) { 38400dea6e3cSMariusz Zaborski *vpp = nd.ni_vp; 38410dea6e3cSMariusz Zaborski NDFREE(&nd, NDF_ONLY_PNBUF); 3842c345faeaSPawel Jakub Dawidek } 3843c345faeaSPawel Jakub Dawidek } 3844c345faeaSPawel Jakub Dawidek 38450dea6e3cSMariusz Zaborski if (error != 0) { 3846c345faeaSPawel Jakub Dawidek #ifdef AUDIT 3847c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 3848c345faeaSPawel Jakub Dawidek #endif 3849c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3850c345faeaSPawel Jakub Dawidek return (error); 3851e7228204SAlfred Perlstein } 3852c345faeaSPawel Jakub Dawidek *namep = name; 3853c345faeaSPawel Jakub Dawidek return (0); 385436b208e0SRobert Watson } 3855c5edb423SSean Eric Fagan 3856df8bae1dSRodney W. Grimes /* 3857fca666a1SJulian Elischer * Dump a process' core. The main routine does some 3858fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 3859fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 3860fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 3861fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 3862fca666a1SJulian Elischer */ 3863fca666a1SJulian Elischer 3864fca666a1SJulian Elischer static int 3865b40ce416SJulian Elischer coredump(struct thread *td) 3866fca666a1SJulian Elischer { 3867b40ce416SJulian Elischer struct proc *p = td->td_proc; 3868f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 3869f06f465dSPawel Jakub Dawidek struct vnode *vp; 387006ae1e91SMatthew Dillon struct flock lf; 3871fca666a1SJulian Elischer struct vattr vattr; 38727739d927SMateusz Guzik size_t fullpathsize; 3873c345faeaSPawel Jakub Dawidek int error, error1, locked; 3874fca666a1SJulian Elischer char *name; /* name of corefile */ 3875539c9eefSKonstantin Belousov void *rl_cookie; 3876fca666a1SJulian Elischer off_t limit; 3877842ab62bSRui Paulo char *fullpath, *freepath = NULL; 3878349fcda4SWarner Losh struct sbuf *sb; 3879fca666a1SJulian Elischer 38804ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3881f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3882fca666a1SJulian Elischer 3883677258f7SKonstantin Belousov if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 3884677258f7SKonstantin Belousov (p->p_flag2 & P2_NOTRACE) != 0) { 3885628d2653SJohn Baldwin PROC_UNLOCK(p); 3886fca666a1SJulian Elischer return (EFAULT); 3887628d2653SJohn Baldwin } 3888fca666a1SJulian Elischer 3889fca666a1SJulian Elischer /* 389035a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 389135a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 389235a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 389335a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 389435a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 389535a2598fSSean Eric Fagan * if it is larger than the limit. 3896fca666a1SJulian Elischer */ 3897f6f6d240SMateusz Guzik limit = (off_t)lim_cur(td, RLIMIT_CORE); 3898b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3899628d2653SJohn Baldwin PROC_UNLOCK(p); 390091d5354aSJohn Baldwin return (EFBIG); 390157274c51SChristian S.J. Peron } 3902b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 390335a2598fSSean Eric Fagan 3904aa14e9b7SMark Johnston error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 39059d3ecb7eSEric van Gyzen compress_user_cores, p->p_sig, &vp, &name); 3906c345faeaSPawel Jakub Dawidek if (error != 0) 3907fca666a1SJulian Elischer return (error); 390806ae1e91SMatthew Dillon 3909539c9eefSKonstantin Belousov /* 3910539c9eefSKonstantin Belousov * Don't dump to non-regular files or files with links. 391189f2ab06SKonstantin Belousov * Do not dump into system files. Effective user must own the corefile. 3912539c9eefSKonstantin Belousov */ 3913f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 39147a29e0bfSKonstantin Belousov vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 || 391589f2ab06SKonstantin Belousov vattr.va_uid != cred->cr_uid) { 3916b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3917832dafadSDon Lewis error = EFAULT; 3918dacbc9dbSKonstantin Belousov goto out; 3919832dafadSDon Lewis } 3920832dafadSDon Lewis 3921b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3922539c9eefSKonstantin Belousov 3923539c9eefSKonstantin Belousov /* Postpone other writers, including core dumps of other processes. */ 3924539c9eefSKonstantin Belousov rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3925539c9eefSKonstantin Belousov 392606ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 392706ae1e91SMatthew Dillon lf.l_start = 0; 392806ae1e91SMatthew Dillon lf.l_len = 0; 392906ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 3930c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 393106ae1e91SMatthew Dillon 3932fca666a1SJulian Elischer VATTR_NULL(&vattr); 3933fca666a1SJulian Elischer vattr.va_size = 0; 39346141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 39356141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 3936cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 39370359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 3938b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3939628d2653SJohn Baldwin PROC_LOCK(p); 3940fca666a1SJulian Elischer p->p_acflag |= ACORE; 3941628d2653SJohn Baldwin PROC_UNLOCK(p); 3942fca666a1SJulian Elischer 394323c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 394478f57a9cSMark Johnston error = p->p_sysent->sv_coredump(td, vp, limit, 0); 394523c6445aSPawel Jakub Dawidek } else { 394623c6445aSPawel Jakub Dawidek error = ENOSYS; 394723c6445aSPawel Jakub Dawidek } 3948fca666a1SJulian Elischer 3949c447f5b2SRobert Watson if (locked) { 395006ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 395106ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3952c447f5b2SRobert Watson } 3953539c9eefSKonstantin Belousov vn_rangelock_unlock(vp, rl_cookie); 3954dacbc9dbSKonstantin Belousov 3955842ab62bSRui Paulo /* 3956842ab62bSRui Paulo * Notify the userland helper that a process triggered a core dump. 3957842ab62bSRui Paulo * This allows the helper to run an automated debugging session. 3958842ab62bSRui Paulo */ 3959dacbc9dbSKonstantin Belousov if (error != 0 || coredump_devctl == 0) 3960842ab62bSRui Paulo goto out; 3961349fcda4SWarner Losh sb = sbuf_new_auto(); 3962feabaaf9SMateusz Guzik if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0) 3963349fcda4SWarner Losh goto out2; 3964349fcda4SWarner Losh sbuf_printf(sb, "comm=\""); 3965349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 3966842ab62bSRui Paulo free(freepath, M_TEMP); 3967349fcda4SWarner Losh sbuf_printf(sb, "\" core=\""); 3968349fcda4SWarner Losh 3969349fcda4SWarner Losh /* 3970349fcda4SWarner Losh * We can't lookup core file vp directly. When we're replacing a core, and 3971349fcda4SWarner Losh * other random times, we flush the name cache, so it will fail. Instead, 3972349fcda4SWarner Losh * if the path of the core is relative, add the current dir in front if it. 3973349fcda4SWarner Losh */ 3974349fcda4SWarner Losh if (name[0] != '/') { 39757739d927SMateusz Guzik fullpathsize = MAXPATHLEN; 39767739d927SMateusz Guzik freepath = malloc(fullpathsize, M_TEMP, M_WAITOK); 3977feabaaf9SMateusz Guzik if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) { 39787739d927SMateusz Guzik free(freepath, M_TEMP); 3979349fcda4SWarner Losh goto out2; 3980349fcda4SWarner Losh } 3981349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 39827739d927SMateusz Guzik free(freepath, M_TEMP); 3983349fcda4SWarner Losh sbuf_putc(sb, '/'); 3984349fcda4SWarner Losh } 3985349fcda4SWarner Losh devctl_safe_quote_sb(sb, name); 3986349fcda4SWarner Losh sbuf_printf(sb, "\""); 3987349fcda4SWarner Losh if (sbuf_finish(sb) == 0) 3988349fcda4SWarner Losh devctl_notify("kernel", "signal", "coredump", sbuf_data(sb)); 3989349fcda4SWarner Losh out2: 3990349fcda4SWarner Losh sbuf_delete(sb); 3991842ab62bSRui Paulo out: 3992dacbc9dbSKonstantin Belousov error1 = vn_close(vp, FWRITE, cred, td); 3993dacbc9dbSKonstantin Belousov if (error == 0) 3994dacbc9dbSKonstantin Belousov error = error1; 399557274c51SChristian S.J. Peron #ifdef AUDIT 399657274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 399757274c51SChristian S.J. Peron #endif 399857274c51SChristian S.J. Peron free(name, M_TEMP); 3999fca666a1SJulian Elischer return (error); 4000fca666a1SJulian Elischer } 4001fca666a1SJulian Elischer 4002fca666a1SJulian Elischer /* 40030c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 40040c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 4005df8bae1dSRodney W. Grimes */ 4006d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 4007df8bae1dSRodney W. Grimes struct nosys_args { 4008df8bae1dSRodney W. Grimes int dummy; 4009df8bae1dSRodney W. Grimes }; 4010d2d3e875SBruce Evans #endif 4011df8bae1dSRodney W. Grimes /* ARGSUSED */ 401226f9a767SRodney W. Grimes int 4013e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args) 4014df8bae1dSRodney W. Grimes { 4015f5a077c3SKonstantin Belousov struct proc *p; 4016f5a077c3SKonstantin Belousov 4017f5a077c3SKonstantin Belousov p = td->td_proc; 4018b40ce416SJulian Elischer 4019628d2653SJohn Baldwin PROC_LOCK(p); 4020888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 4021628d2653SJohn Baldwin PROC_UNLOCK(p); 40227ceeb35bSKonstantin Belousov if (kern_lognosys == 1 || kern_lognosys == 3) { 4023f5a077c3SKonstantin Belousov uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 4024f5a077c3SKonstantin Belousov td->td_sa.code); 40257ceeb35bSKonstantin Belousov } 402618f917a9SBrooks Davis if (kern_lognosys == 2 || kern_lognosys == 3 || 402718f917a9SBrooks Davis (p->p_pid == 1 && (kern_lognosys & 3) == 0)) { 40287ceeb35bSKonstantin Belousov printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 40297ceeb35bSKonstantin Belousov td->td_sa.code); 40307ceeb35bSKonstantin Belousov } 4031f5216b9aSBruce Evans return (ENOSYS); 4032df8bae1dSRodney W. Grimes } 4033831d27a9SDon Lewis 4034831d27a9SDon Lewis /* 40350c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 40360c14ff0eSRobert Watson * credentials rather than those of the current process. 4037831d27a9SDon Lewis */ 4038831d27a9SDon Lewis void 4039e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty) 4040831d27a9SDon Lewis { 4041a3de221dSKonstantin Belousov ksiginfo_t ksi; 4042f1320723SAlfred Perlstein struct sigio *sigio; 4043831d27a9SDon Lewis 4044a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 4045a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 4046a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 4047a3de221dSKonstantin Belousov 4048f1320723SAlfred Perlstein SIGIO_LOCK(); 4049f1320723SAlfred Perlstein sigio = *sigiop; 4050f1320723SAlfred Perlstein if (sigio == NULL) { 4051f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4052f1320723SAlfred Perlstein return; 4053f1320723SAlfred Perlstein } 4054831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 4055628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 40562b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 40578451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 4058628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 4059831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 4060831d27a9SDon Lewis struct proc *p; 4061831d27a9SDon Lewis 4062f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 4063628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 4064628d2653SJohn Baldwin PROC_LOCK(p); 4065e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 4066e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 4067831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 40688451d0ddSKip Macy kern_psignal(p, sig); 4069628d2653SJohn Baldwin PROC_UNLOCK(p); 4070628d2653SJohn Baldwin } 4071f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 4072831d27a9SDon Lewis } 4073f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4074831d27a9SDon Lewis } 4075cb679c38SJonathan Lemon 4076cb679c38SJonathan Lemon static int 4077cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 4078cb679c38SJonathan Lemon { 4079cb679c38SJonathan Lemon struct proc *p = curproc; 4080cb679c38SJonathan Lemon 4081cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 4082cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 4083cb679c38SJonathan Lemon 40849e590ff0SKonstantin Belousov knlist_add(p->p_klist, kn, 0); 4085cb679c38SJonathan Lemon 4086cb679c38SJonathan Lemon return (0); 4087cb679c38SJonathan Lemon } 4088cb679c38SJonathan Lemon 4089cb679c38SJonathan Lemon static void 4090cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 4091cb679c38SJonathan Lemon { 4092cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 4093cb679c38SJonathan Lemon 40949e590ff0SKonstantin Belousov knlist_remove(p->p_klist, kn, 0); 4095cb679c38SJonathan Lemon } 4096cb679c38SJonathan Lemon 4097cb679c38SJonathan Lemon /* 4098cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 4099cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 4100cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 4101cb679c38SJonathan Lemon * isn't worth the trouble. 4102cb679c38SJonathan Lemon */ 4103cb679c38SJonathan Lemon static int 4104cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 4105cb679c38SJonathan Lemon { 4106cb679c38SJonathan Lemon 4107cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 4108cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 4109cb679c38SJonathan Lemon 4110cb679c38SJonathan Lemon if (kn->kn_id == hint) 4111cb679c38SJonathan Lemon kn->kn_data++; 4112cb679c38SJonathan Lemon } 4113cb679c38SJonathan Lemon return (kn->kn_data != 0); 4114cb679c38SJonathan Lemon } 411590af4afaSJohn Baldwin 411690af4afaSJohn Baldwin struct sigacts * 411790af4afaSJohn Baldwin sigacts_alloc(void) 411890af4afaSJohn Baldwin { 411990af4afaSJohn Baldwin struct sigacts *ps; 412090af4afaSJohn Baldwin 412190af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 4122ce8daaadSMateusz Guzik refcount_init(&ps->ps_refcnt, 1); 412390af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 412490af4afaSJohn Baldwin return (ps); 412590af4afaSJohn Baldwin } 412690af4afaSJohn Baldwin 412790af4afaSJohn Baldwin void 412890af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 412990af4afaSJohn Baldwin { 413090af4afaSJohn Baldwin 4131c959c237SMateusz Guzik if (refcount_release(&ps->ps_refcnt) == 0) 4132c959c237SMateusz Guzik return; 413390af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 413490af4afaSJohn Baldwin free(ps, M_SUBPROC); 413590af4afaSJohn Baldwin } 413690af4afaSJohn Baldwin 413790af4afaSJohn Baldwin struct sigacts * 413890af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 413990af4afaSJohn Baldwin { 4140c959c237SMateusz Guzik 4141c959c237SMateusz Guzik refcount_acquire(&ps->ps_refcnt); 414290af4afaSJohn Baldwin return (ps); 414390af4afaSJohn Baldwin } 414490af4afaSJohn Baldwin 414590af4afaSJohn Baldwin void 414690af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 414790af4afaSJohn Baldwin { 414890af4afaSJohn Baldwin 414990af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 415090af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 415190af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 415290af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 415390af4afaSJohn Baldwin } 415490af4afaSJohn Baldwin 415590af4afaSJohn Baldwin int 415690af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 415790af4afaSJohn Baldwin { 415890af4afaSJohn Baldwin 4159d00c8ea4SMateusz Guzik return (ps->ps_refcnt > 1); 416090af4afaSJohn Baldwin } 4161079c5b9eSKyle Evans 4162079c5b9eSKyle Evans void 4163079c5b9eSKyle Evans sig_drop_caught(struct proc *p) 4164079c5b9eSKyle Evans { 4165079c5b9eSKyle Evans int sig; 4166079c5b9eSKyle Evans struct sigacts *ps; 4167079c5b9eSKyle Evans 4168079c5b9eSKyle Evans ps = p->p_sigacts; 4169079c5b9eSKyle Evans PROC_LOCK_ASSERT(p, MA_OWNED); 4170079c5b9eSKyle Evans mtx_assert(&ps->ps_mtx, MA_OWNED); 417181f2e906SMark Johnston SIG_FOREACH(sig, &ps->ps_sigcatch) { 4172079c5b9eSKyle Evans sigdflt(ps, sig); 4173079c5b9eSKyle Evans if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 4174079c5b9eSKyle Evans sigqueue_delete_proc(p, sig); 4175079c5b9eSKyle Evans } 4176079c5b9eSKyle Evans } 4177146fc63fSKonstantin Belousov 4178a113b17fSKonstantin Belousov static void 4179a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write) 4180a113b17fSKonstantin Belousov { 4181a113b17fSKonstantin Belousov ksiginfo_t ksi; 4182a113b17fSKonstantin Belousov 4183a113b17fSKonstantin Belousov /* 4184a113b17fSKonstantin Belousov * Prevent further fetches and SIGSEGVs, allowing thread to 4185a113b17fSKonstantin Belousov * issue syscalls despite corruption. 4186a113b17fSKonstantin Belousov */ 4187a113b17fSKonstantin Belousov sigfastblock_clear(td); 4188a113b17fSKonstantin Belousov 4189a113b17fSKonstantin Belousov if (!sendsig) 4190a113b17fSKonstantin Belousov return; 4191a113b17fSKonstantin Belousov ksiginfo_init_trap(&ksi); 4192a113b17fSKonstantin Belousov ksi.ksi_signo = SIGSEGV; 4193a113b17fSKonstantin Belousov ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR; 4194a113b17fSKonstantin Belousov ksi.ksi_addr = td->td_sigblock_ptr; 4195a113b17fSKonstantin Belousov trapsignal(td, &ksi); 4196a113b17fSKonstantin Belousov } 4197a113b17fSKonstantin Belousov 4198a113b17fSKonstantin Belousov static bool 4199a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp) 4200a113b17fSKonstantin Belousov { 4201a113b17fSKonstantin Belousov uint32_t res; 4202a113b17fSKonstantin Belousov 4203a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4204a113b17fSKonstantin Belousov return (true); 4205a113b17fSKonstantin Belousov if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) { 4206a113b17fSKonstantin Belousov sigfastblock_failed(td, sendsig, false); 4207a113b17fSKonstantin Belousov return (false); 4208a113b17fSKonstantin Belousov } 4209a113b17fSKonstantin Belousov *valp = res; 4210a113b17fSKonstantin Belousov td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS; 4211a113b17fSKonstantin Belousov return (true); 4212a113b17fSKonstantin Belousov } 4213a113b17fSKonstantin Belousov 4214fb3c434bSKonstantin Belousov static void 4215fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched) 4216fb3c434bSKonstantin Belousov { 4217fb3c434bSKonstantin Belousov struct proc *p; 4218fb3c434bSKonstantin Belousov 4219fb3c434bSKonstantin Belousov if (resched) { 4220fb3c434bSKonstantin Belousov p = td->td_proc; 4221fb3c434bSKonstantin Belousov PROC_LOCK(p); 4222fb3c434bSKonstantin Belousov reschedule_signals(p, td->td_sigmask, 0); 4223fb3c434bSKonstantin Belousov PROC_UNLOCK(p); 4224fb3c434bSKonstantin Belousov } 4225fb3c434bSKonstantin Belousov thread_lock(td); 4226fb3c434bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING | TDF_NEEDSIGCHK; 4227fb3c434bSKonstantin Belousov thread_unlock(td); 4228fb3c434bSKonstantin Belousov } 4229fb3c434bSKonstantin Belousov 4230146fc63fSKonstantin Belousov int 4231146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap) 4232146fc63fSKonstantin Belousov { 4233146fc63fSKonstantin Belousov struct proc *p; 4234146fc63fSKonstantin Belousov int error, res; 4235146fc63fSKonstantin Belousov uint32_t oldval; 4236146fc63fSKonstantin Belousov 4237146fc63fSKonstantin Belousov error = 0; 4238a113b17fSKonstantin Belousov p = td->td_proc; 4239146fc63fSKonstantin Belousov switch (uap->cmd) { 4240146fc63fSKonstantin Belousov case SIGFASTBLOCK_SETPTR: 4241146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 4242146fc63fSKonstantin Belousov error = EBUSY; 4243146fc63fSKonstantin Belousov break; 4244146fc63fSKonstantin Belousov } 4245146fc63fSKonstantin Belousov if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) { 4246146fc63fSKonstantin Belousov error = EINVAL; 4247146fc63fSKonstantin Belousov break; 4248146fc63fSKonstantin Belousov } 4249146fc63fSKonstantin Belousov td->td_pflags |= TDP_SIGFASTBLOCK; 4250146fc63fSKonstantin Belousov td->td_sigblock_ptr = uap->ptr; 4251146fc63fSKonstantin Belousov break; 4252146fc63fSKonstantin Belousov 4253146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNBLOCK: 4254a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4255146fc63fSKonstantin Belousov error = EINVAL; 4256146fc63fSKonstantin Belousov break; 4257146fc63fSKonstantin Belousov } 4258a113b17fSKonstantin Belousov 4259a113b17fSKonstantin Belousov for (;;) { 4260a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, 4261a113b17fSKonstantin Belousov SIGFASTBLOCK_PEND, &oldval, 0); 4262146fc63fSKonstantin Belousov if (res == -1) { 4263146fc63fSKonstantin Belousov error = EFAULT; 4264a113b17fSKonstantin Belousov sigfastblock_failed(td, false, true); 4265146fc63fSKonstantin Belousov break; 4266146fc63fSKonstantin Belousov } 4267a113b17fSKonstantin Belousov if (res == 0) 4268a113b17fSKonstantin Belousov break; 4269a113b17fSKonstantin Belousov MPASS(res == 1); 4270146fc63fSKonstantin Belousov if (oldval != SIGFASTBLOCK_PEND) { 4271146fc63fSKonstantin Belousov error = EBUSY; 4272146fc63fSKonstantin Belousov break; 4273146fc63fSKonstantin Belousov } 4274146fc63fSKonstantin Belousov error = thread_check_susp(td, false); 4275146fc63fSKonstantin Belousov if (error != 0) 4276146fc63fSKonstantin Belousov break; 4277146fc63fSKonstantin Belousov } 4278a113b17fSKonstantin Belousov if (error != 0) 4279a113b17fSKonstantin Belousov break; 4280a113b17fSKonstantin Belousov 4281a113b17fSKonstantin Belousov /* 4282a113b17fSKonstantin Belousov * td_sigblock_val is cleared there, but not on a 4283a113b17fSKonstantin Belousov * syscall exit. The end effect is that a single 4284a113b17fSKonstantin Belousov * interruptible sleep, while user sigblock word is 4285a113b17fSKonstantin Belousov * set, might return EINTR or ERESTART to usermode 4286a113b17fSKonstantin Belousov * without delivering signal. All further sleeps, 4287a113b17fSKonstantin Belousov * until userspace clears the word and does 4288a113b17fSKonstantin Belousov * sigfastblock(UNBLOCK), observe current word and no 4289a113b17fSKonstantin Belousov * longer get interrupted. It is slight 4290a113b17fSKonstantin Belousov * non-conformance, with alternative to have read the 4291a113b17fSKonstantin Belousov * sigblock word on each syscall entry. 4292a113b17fSKonstantin Belousov */ 4293146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 4294146fc63fSKonstantin Belousov 4295146fc63fSKonstantin Belousov /* 4296146fc63fSKonstantin Belousov * Rely on normal ast mechanism to deliver pending 4297146fc63fSKonstantin Belousov * signals to current thread. But notify others about 4298146fc63fSKonstantin Belousov * fake unblock. 4299146fc63fSKonstantin Belousov */ 4300fb3c434bSKonstantin Belousov sigfastblock_resched(td, error == 0 && p->p_numthreads != 1); 4301fb3c434bSKonstantin Belousov 4302146fc63fSKonstantin Belousov break; 4303146fc63fSKonstantin Belousov 4304146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNSETPTR: 4305146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4306146fc63fSKonstantin Belousov error = EINVAL; 4307146fc63fSKonstantin Belousov break; 4308146fc63fSKonstantin Belousov } 4309a113b17fSKonstantin Belousov if (!sigfastblock_fetch_sig(td, false, &oldval)) { 4310146fc63fSKonstantin Belousov error = EFAULT; 4311146fc63fSKonstantin Belousov break; 4312146fc63fSKonstantin Belousov } 4313146fc63fSKonstantin Belousov if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) { 4314146fc63fSKonstantin Belousov error = EBUSY; 4315146fc63fSKonstantin Belousov break; 4316146fc63fSKonstantin Belousov } 4317a113b17fSKonstantin Belousov sigfastblock_clear(td); 4318146fc63fSKonstantin Belousov break; 4319146fc63fSKonstantin Belousov 4320146fc63fSKonstantin Belousov default: 4321146fc63fSKonstantin Belousov error = EINVAL; 4322146fc63fSKonstantin Belousov break; 4323146fc63fSKonstantin Belousov } 4324146fc63fSKonstantin Belousov return (error); 4325146fc63fSKonstantin Belousov } 4326146fc63fSKonstantin Belousov 4327146fc63fSKonstantin Belousov void 4328a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td) 4329146fc63fSKonstantin Belousov { 4330a113b17fSKonstantin Belousov bool resched; 4331146fc63fSKonstantin Belousov 4332146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4333146fc63fSKonstantin Belousov return; 4334a113b17fSKonstantin Belousov td->td_sigblock_val = 0; 433500ebd809SKonstantin Belousov resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 || 433600ebd809SKonstantin Belousov SIGPENDING(td); 4337a113b17fSKonstantin Belousov td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING); 4338fb3c434bSKonstantin Belousov sigfastblock_resched(td, resched); 4339146fc63fSKonstantin Belousov } 4340146fc63fSKonstantin Belousov 4341146fc63fSKonstantin Belousov void 4342a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td) 4343146fc63fSKonstantin Belousov { 4344a113b17fSKonstantin Belousov uint32_t val; 4345146fc63fSKonstantin Belousov 4346a113b17fSKonstantin Belousov (void)sigfastblock_fetch_sig(td, true, &val); 4347a113b17fSKonstantin Belousov } 4348146fc63fSKonstantin Belousov 43490bc52b0bSKonstantin Belousov static void 43500bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td) 4351a113b17fSKonstantin Belousov { 4352a113b17fSKonstantin Belousov int res; 4353a113b17fSKonstantin Belousov uint32_t oldval; 4354a113b17fSKonstantin Belousov 4355513320c0SKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTPENDING) == 0) 4356a113b17fSKonstantin Belousov return; 4357a113b17fSKonstantin Belousov res = fueword32((void *)td->td_sigblock_ptr, &oldval); 4358a113b17fSKonstantin Belousov if (res == -1) { 4359a113b17fSKonstantin Belousov sigfastblock_failed(td, true, false); 4360a113b17fSKonstantin Belousov return; 4361a113b17fSKonstantin Belousov } 4362a113b17fSKonstantin Belousov for (;;) { 4363a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, oldval, &oldval, 4364a113b17fSKonstantin Belousov oldval | SIGFASTBLOCK_PEND); 4365a113b17fSKonstantin Belousov if (res == -1) { 4366a113b17fSKonstantin Belousov sigfastblock_failed(td, true, true); 4367a113b17fSKonstantin Belousov return; 4368a113b17fSKonstantin Belousov } 4369a113b17fSKonstantin Belousov if (res == 0) { 4370a113b17fSKonstantin Belousov td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS; 4371a113b17fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTPENDING; 4372a113b17fSKonstantin Belousov break; 4373a113b17fSKonstantin Belousov } 4374a113b17fSKonstantin Belousov MPASS(res == 1); 4375a113b17fSKonstantin Belousov if (thread_check_susp(td, false) != 0) 4376a113b17fSKonstantin Belousov break; 4377a113b17fSKonstantin Belousov } 4378146fc63fSKonstantin Belousov } 43790bc52b0bSKonstantin Belousov 43800bc52b0bSKonstantin Belousov void 43810bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched) 43820bc52b0bSKonstantin Belousov { 43830bc52b0bSKonstantin Belousov struct proc *p; 43840bc52b0bSKonstantin Belousov 43850bc52b0bSKonstantin Belousov sigfastblock_setpend1(td); 43860bc52b0bSKonstantin Belousov if (resched) { 43870bc52b0bSKonstantin Belousov p = td->td_proc; 43880bc52b0bSKonstantin Belousov PROC_LOCK(p); 43890bc52b0bSKonstantin Belousov reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK); 43900bc52b0bSKonstantin Belousov PROC_UNLOCK(p); 43910bc52b0bSKonstantin Belousov } 43920bc52b0bSKonstantin Belousov } 4393