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 295863070bbSEric van Gyzen flags = M_ZERO | (wait ? M_WAITOK : M_NOWAIT); 2969104847fSDavid Xu if (ksiginfo_zone != NULL) 297ebceaf6dSDavid Xu return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 2989104847fSDavid Xu return (NULL); 2999104847fSDavid Xu } 3009104847fSDavid Xu 3015da49fcbSDavid Xu void 3029104847fSDavid Xu ksiginfo_free(ksiginfo_t *ksi) 3039104847fSDavid Xu { 3049104847fSDavid Xu uma_zfree(ksiginfo_zone, ksi); 3059104847fSDavid Xu } 3069104847fSDavid Xu 3075da49fcbSDavid Xu static __inline int 3085da49fcbSDavid Xu ksiginfo_tryfree(ksiginfo_t *ksi) 3095da49fcbSDavid Xu { 3105da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT)) { 3115da49fcbSDavid Xu uma_zfree(ksiginfo_zone, ksi); 3125da49fcbSDavid Xu return (1); 3135da49fcbSDavid Xu } 3145da49fcbSDavid Xu return (0); 3155da49fcbSDavid Xu } 3165da49fcbSDavid Xu 3179104847fSDavid Xu void 3189104847fSDavid Xu sigqueue_init(sigqueue_t *list, struct proc *p) 3199104847fSDavid Xu { 3209104847fSDavid Xu SIGEMPTYSET(list->sq_signals); 3213dfcaad6SDavid Xu SIGEMPTYSET(list->sq_kill); 32282a4538fSEric Badger SIGEMPTYSET(list->sq_ptrace); 3239104847fSDavid Xu TAILQ_INIT(&list->sq_list); 3249104847fSDavid Xu list->sq_proc = p; 3259104847fSDavid Xu list->sq_flags = SQ_INIT; 3269104847fSDavid Xu } 3279104847fSDavid Xu 3289104847fSDavid Xu /* 3299104847fSDavid Xu * Get a signal's ksiginfo. 3309104847fSDavid Xu * Return: 3319104847fSDavid Xu * 0 - signal not found 3329104847fSDavid Xu * others - signal number 3339104847fSDavid Xu */ 334a5799a4fSKonstantin Belousov static int 3359104847fSDavid Xu sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 3369104847fSDavid Xu { 3379104847fSDavid Xu struct proc *p = sq->sq_proc; 3389104847fSDavid Xu struct ksiginfo *ksi, *next; 3399104847fSDavid Xu int count = 0; 3409104847fSDavid Xu 3419104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 3429104847fSDavid Xu 3439104847fSDavid Xu if (!SIGISMEMBER(sq->sq_signals, signo)) 3449104847fSDavid Xu return (0); 3459104847fSDavid Xu 34682a4538fSEric Badger if (SIGISMEMBER(sq->sq_ptrace, signo)) { 34782a4538fSEric Badger count++; 34882a4538fSEric Badger SIGDELSET(sq->sq_ptrace, signo); 34982a4538fSEric Badger si->ksi_flags |= KSI_PTRACE; 35082a4538fSEric Badger } 3513dfcaad6SDavid Xu if (SIGISMEMBER(sq->sq_kill, signo)) { 3523dfcaad6SDavid Xu count++; 35382a4538fSEric Badger if (count == 1) 3543dfcaad6SDavid Xu SIGDELSET(sq->sq_kill, signo); 3553dfcaad6SDavid Xu } 3563dfcaad6SDavid Xu 3575c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 3589104847fSDavid Xu if (ksi->ksi_signo == signo) { 3599104847fSDavid Xu if (count == 0) { 3609104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3615da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3629104847fSDavid Xu ksiginfo_copy(ksi, si); 3635da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 3649104847fSDavid Xu p->p_pendingcnt--; 3659104847fSDavid Xu } 366016fa302SDavid Xu if (++count > 1) 367016fa302SDavid Xu break; 3689104847fSDavid Xu } 3699104847fSDavid Xu } 3709104847fSDavid Xu 3719104847fSDavid Xu if (count <= 1) 3729104847fSDavid Xu SIGDELSET(sq->sq_signals, signo); 3739104847fSDavid Xu si->ksi_signo = signo; 3749104847fSDavid Xu return (signo); 3759104847fSDavid Xu } 3769104847fSDavid Xu 3775da49fcbSDavid Xu void 3785da49fcbSDavid Xu sigqueue_take(ksiginfo_t *ksi) 3795da49fcbSDavid Xu { 3805da49fcbSDavid Xu struct ksiginfo *kp; 3815da49fcbSDavid Xu struct proc *p; 3825da49fcbSDavid Xu sigqueue_t *sq; 3835da49fcbSDavid Xu 384ebceaf6dSDavid Xu if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 3855da49fcbSDavid Xu return; 3865da49fcbSDavid Xu 3875da49fcbSDavid Xu p = sq->sq_proc; 3885da49fcbSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 3895da49fcbSDavid Xu ksi->ksi_sigq = NULL; 3905da49fcbSDavid Xu if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 3915da49fcbSDavid Xu p->p_pendingcnt--; 3925da49fcbSDavid Xu 3935da49fcbSDavid Xu for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 3945da49fcbSDavid Xu kp = TAILQ_NEXT(kp, ksi_link)) { 3955da49fcbSDavid Xu if (kp->ksi_signo == ksi->ksi_signo) 3965da49fcbSDavid Xu break; 3975da49fcbSDavid Xu } 39882a4538fSEric Badger if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 39982a4538fSEric Badger !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 4005da49fcbSDavid Xu SIGDELSET(sq->sq_signals, ksi->ksi_signo); 4015da49fcbSDavid Xu } 4025da49fcbSDavid Xu 403a5799a4fSKonstantin Belousov static int 4049104847fSDavid Xu sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 4059104847fSDavid Xu { 4069104847fSDavid Xu struct proc *p = sq->sq_proc; 4079104847fSDavid Xu struct ksiginfo *ksi; 4089104847fSDavid Xu int ret = 0; 4099104847fSDavid Xu 4109104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4119104847fSDavid Xu 41282a4538fSEric Badger /* 41382a4538fSEric Badger * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 41482a4538fSEric Badger * for these signals. 41582a4538fSEric Badger */ 4163dfcaad6SDavid Xu if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 4173dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4189104847fSDavid Xu goto out_set_bit; 4193dfcaad6SDavid Xu } 4209104847fSDavid Xu 4215da49fcbSDavid Xu /* directly insert the ksi, don't copy it */ 4225da49fcbSDavid Xu if (si->ksi_flags & KSI_INS) { 4236a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4246a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 4256a671a6bSKonstantin Belousov else 4265da49fcbSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 4275da49fcbSDavid Xu si->ksi_sigq = sq; 4285da49fcbSDavid Xu goto out_set_bit; 4295da49fcbSDavid Xu } 4305da49fcbSDavid Xu 4313dfcaad6SDavid Xu if (__predict_false(ksiginfo_zone == NULL)) { 4323dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4339104847fSDavid Xu goto out_set_bit; 4343dfcaad6SDavid Xu } 4359104847fSDavid Xu 436ebceaf6dSDavid Xu if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 4379104847fSDavid Xu signal_overflow++; 4389104847fSDavid Xu ret = EAGAIN; 439ebceaf6dSDavid Xu } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 4409104847fSDavid Xu signal_alloc_fail++; 4419104847fSDavid Xu ret = EAGAIN; 4429104847fSDavid Xu } else { 4439104847fSDavid Xu if (p != NULL) 4449104847fSDavid Xu p->p_pendingcnt++; 4459104847fSDavid Xu ksiginfo_copy(si, ksi); 4469104847fSDavid Xu ksi->ksi_signo = signo; 4476a671a6bSKonstantin Belousov if (si->ksi_flags & KSI_HEAD) 4486a671a6bSKonstantin Belousov TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 4496a671a6bSKonstantin Belousov else 4509104847fSDavid Xu TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 4515da49fcbSDavid Xu ksi->ksi_sigq = sq; 4529104847fSDavid Xu } 4539104847fSDavid Xu 45482a4538fSEric Badger if (ret != 0) { 45582a4538fSEric Badger if ((si->ksi_flags & KSI_PTRACE) != 0) { 45682a4538fSEric Badger SIGADDSET(sq->sq_ptrace, signo); 45782a4538fSEric Badger ret = 0; 45882a4538fSEric Badger goto out_set_bit; 45982a4538fSEric Badger } else if ((si->ksi_flags & KSI_TRAP) != 0 || 460a3de221dSKonstantin Belousov (si->ksi_flags & KSI_SIGQ) == 0) { 4613dfcaad6SDavid Xu SIGADDSET(sq->sq_kill, signo); 4629104847fSDavid Xu ret = 0; 4639104847fSDavid Xu goto out_set_bit; 4649104847fSDavid Xu } 4659104847fSDavid Xu return (ret); 46682a4538fSEric Badger } 4679104847fSDavid Xu 4689104847fSDavid Xu out_set_bit: 4699104847fSDavid Xu SIGADDSET(sq->sq_signals, signo); 4709104847fSDavid Xu return (ret); 4719104847fSDavid Xu } 4729104847fSDavid Xu 4739104847fSDavid Xu void 4749104847fSDavid Xu sigqueue_flush(sigqueue_t *sq) 4759104847fSDavid Xu { 4769104847fSDavid Xu struct proc *p = sq->sq_proc; 4779104847fSDavid Xu ksiginfo_t *ksi; 4789104847fSDavid Xu 4799104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 4809104847fSDavid Xu 4815da49fcbSDavid Xu if (p != NULL) 4825da49fcbSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 4835da49fcbSDavid Xu 4849104847fSDavid Xu while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 4859104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 4865da49fcbSDavid Xu ksi->ksi_sigq = NULL; 4875da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 4889104847fSDavid Xu p->p_pendingcnt--; 4899104847fSDavid Xu } 4909104847fSDavid Xu 4919104847fSDavid Xu SIGEMPTYSET(sq->sq_signals); 4923dfcaad6SDavid Xu SIGEMPTYSET(sq->sq_kill); 49382a4538fSEric Badger SIGEMPTYSET(sq->sq_ptrace); 4949104847fSDavid Xu } 4959104847fSDavid Xu 496a5799a4fSKonstantin Belousov static void 497fc4ecc1dSDavid Xu sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 4989104847fSDavid Xu { 499fc4ecc1dSDavid Xu sigset_t tmp; 5009104847fSDavid Xu struct proc *p1, *p2; 5019104847fSDavid Xu ksiginfo_t *ksi, *next; 5029104847fSDavid Xu 5039104847fSDavid Xu KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5049104847fSDavid Xu KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 5059104847fSDavid Xu p1 = src->sq_proc; 5069104847fSDavid Xu p2 = dst->sq_proc; 5079104847fSDavid Xu /* Move siginfo to target list */ 5085c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 509fc4ecc1dSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5109104847fSDavid Xu TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 5119104847fSDavid Xu if (p1 != NULL) 5129104847fSDavid Xu p1->p_pendingcnt--; 5139104847fSDavid Xu TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 5145da49fcbSDavid Xu ksi->ksi_sigq = dst; 5159104847fSDavid Xu if (p2 != NULL) 5169104847fSDavid Xu p2->p_pendingcnt++; 5179104847fSDavid Xu } 5189104847fSDavid Xu } 5199104847fSDavid Xu 5209104847fSDavid Xu /* Move pending bits to target list */ 5213dfcaad6SDavid Xu tmp = src->sq_kill; 522fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 5233dfcaad6SDavid Xu SIGSETOR(dst->sq_kill, tmp); 5243dfcaad6SDavid Xu SIGSETNAND(src->sq_kill, tmp); 5253dfcaad6SDavid Xu 52682a4538fSEric Badger tmp = src->sq_ptrace; 52782a4538fSEric Badger SIGSETAND(tmp, *set); 52882a4538fSEric Badger SIGSETOR(dst->sq_ptrace, tmp); 52982a4538fSEric Badger SIGSETNAND(src->sq_ptrace, tmp); 53082a4538fSEric Badger 5319104847fSDavid Xu tmp = src->sq_signals; 532fc4ecc1dSDavid Xu SIGSETAND(tmp, *set); 5339104847fSDavid Xu SIGSETOR(dst->sq_signals, tmp); 5349104847fSDavid Xu SIGSETNAND(src->sq_signals, tmp); 5359104847fSDavid Xu } 5369104847fSDavid Xu 537407af02bSDavid Xu #if 0 538a5799a4fSKonstantin Belousov static void 5399104847fSDavid Xu sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 5409104847fSDavid Xu { 5419104847fSDavid Xu sigset_t set; 5429104847fSDavid Xu 5439104847fSDavid Xu SIGEMPTYSET(set); 5449104847fSDavid Xu SIGADDSET(set, signo); 5459104847fSDavid Xu sigqueue_move_set(src, dst, &set); 5469104847fSDavid Xu } 547407af02bSDavid Xu #endif 5489104847fSDavid Xu 549a5799a4fSKonstantin Belousov static void 550fc4ecc1dSDavid Xu sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 5519104847fSDavid Xu { 5529104847fSDavid Xu struct proc *p = sq->sq_proc; 5539104847fSDavid Xu ksiginfo_t *ksi, *next; 5549104847fSDavid Xu 5559104847fSDavid Xu KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 5569104847fSDavid Xu 5579104847fSDavid Xu /* Remove siginfo queue */ 5585c28a8d4SDavid Xu TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 5599104847fSDavid Xu if (SIGISMEMBER(*set, ksi->ksi_signo)) { 5609104847fSDavid Xu TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 5615da49fcbSDavid Xu ksi->ksi_sigq = NULL; 5625da49fcbSDavid Xu if (ksiginfo_tryfree(ksi) && p != NULL) 5639104847fSDavid Xu p->p_pendingcnt--; 5649104847fSDavid Xu } 5659104847fSDavid Xu } 5663dfcaad6SDavid Xu SIGSETNAND(sq->sq_kill, *set); 56782a4538fSEric Badger SIGSETNAND(sq->sq_ptrace, *set); 5689104847fSDavid Xu SIGSETNAND(sq->sq_signals, *set); 5699104847fSDavid Xu } 5709104847fSDavid Xu 5719104847fSDavid Xu void 5729104847fSDavid Xu sigqueue_delete(sigqueue_t *sq, int signo) 5739104847fSDavid Xu { 5749104847fSDavid Xu sigset_t set; 5759104847fSDavid Xu 5769104847fSDavid Xu SIGEMPTYSET(set); 5779104847fSDavid Xu SIGADDSET(set, signo); 5789104847fSDavid Xu sigqueue_delete_set(sq, &set); 5799104847fSDavid Xu } 5809104847fSDavid Xu 5819104847fSDavid Xu /* Remove a set of signals for a process */ 582a5799a4fSKonstantin Belousov static void 583fc4ecc1dSDavid Xu sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 5849104847fSDavid Xu { 5859104847fSDavid Xu sigqueue_t worklist; 5869104847fSDavid Xu struct thread *td0; 5879104847fSDavid Xu 5889104847fSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 5899104847fSDavid Xu 5909104847fSDavid Xu sigqueue_init(&worklist, NULL); 5919104847fSDavid Xu sigqueue_move_set(&p->p_sigqueue, &worklist, set); 5929104847fSDavid Xu 5939104847fSDavid Xu FOREACH_THREAD_IN_PROC(p, td0) 5949104847fSDavid Xu sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 5959104847fSDavid Xu 5969104847fSDavid Xu sigqueue_flush(&worklist); 5979104847fSDavid Xu } 5989104847fSDavid Xu 5999104847fSDavid Xu void 6009104847fSDavid Xu sigqueue_delete_proc(struct proc *p, int signo) 6019104847fSDavid Xu { 6029104847fSDavid Xu sigset_t set; 6039104847fSDavid Xu 6049104847fSDavid Xu SIGEMPTYSET(set); 6059104847fSDavid Xu SIGADDSET(set, signo); 6069104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 6079104847fSDavid Xu } 6089104847fSDavid Xu 609a5799a4fSKonstantin Belousov static void 6109104847fSDavid Xu sigqueue_delete_stopmask_proc(struct proc *p) 6119104847fSDavid Xu { 6129104847fSDavid Xu sigset_t set; 6139104847fSDavid Xu 6149104847fSDavid Xu SIGEMPTYSET(set); 6159104847fSDavid Xu SIGADDSET(set, SIGSTOP); 6169104847fSDavid Xu SIGADDSET(set, SIGTSTP); 6179104847fSDavid Xu SIGADDSET(set, SIGTTIN); 6189104847fSDavid Xu SIGADDSET(set, SIGTTOU); 6199104847fSDavid Xu sigqueue_delete_set_proc(p, &set); 6209104847fSDavid Xu } 6219104847fSDavid Xu 622fbbeeb6cSBruce Evans /* 6231968f37bSJohn Baldwin * Determine signal that should be delivered to thread td, the current 6241968f37bSJohn Baldwin * thread, 0 if none. If there is a pending stop signal with default 625fbbeeb6cSBruce Evans * action, the process stops in issignal(). 626fbbeeb6cSBruce Evans */ 627fbbeeb6cSBruce Evans int 6283cf3b9f0SJohn Baldwin cursig(struct thread *td) 629fbbeeb6cSBruce Evans { 630c9dfa2e0SJeff Roberson PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 63190af4afaSJohn Baldwin mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 632a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 6333cf3b9f0SJohn Baldwin return (SIGPENDING(td) ? issignal(td) : 0); 634fbbeeb6cSBruce Evans } 635fbbeeb6cSBruce Evans 63679065dbaSBruce Evans /* 63779065dbaSBruce Evans * Arrange for ast() to handle unmasked pending signals on return to user 6389104847fSDavid Xu * mode. This must be called whenever a signal is added to td_sigqueue or 6394093529dSJeff Roberson * unmasked in td_sigmask. 64079065dbaSBruce Evans */ 64179065dbaSBruce Evans void 6424093529dSJeff Roberson signotify(struct thread *td) 64379065dbaSBruce Evans { 6444093529dSJeff Roberson 645ddd4d15eSMatt Macy PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 6464093529dSJeff Roberson 6478b94a061SJohn Baldwin if (SIGPENDING(td)) { 648a54e85fdSJeff Roberson thread_lock(td); 6494093529dSJeff Roberson td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 650a54e85fdSJeff Roberson thread_unlock(td); 65179065dbaSBruce Evans } 6528b94a061SJohn Baldwin } 6538b94a061SJohn Baldwin 654affd9185SKonstantin Belousov /* 655affd9185SKonstantin Belousov * Returns 1 (true) if altstack is configured for the thread, and the 656affd9185SKonstantin Belousov * passed stack bottom address falls into the altstack range. Handles 657affd9185SKonstantin Belousov * the 43 compat special case where the alt stack size is zero. 658affd9185SKonstantin Belousov */ 6598b94a061SJohn Baldwin int 6608b94a061SJohn Baldwin sigonstack(size_t sp) 6618b94a061SJohn Baldwin { 662affd9185SKonstantin Belousov struct thread *td; 6638b94a061SJohn Baldwin 664affd9185SKonstantin Belousov td = curthread; 665affd9185SKonstantin Belousov if ((td->td_pflags & TDP_ALTSTACK) == 0) 666affd9185SKonstantin Belousov return (0); 6671930e303SPoul-Henning Kamp #if defined(COMPAT_43) 6687e097daaSKonstantin Belousov if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0) 669affd9185SKonstantin Belousov return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0); 6708b94a061SJohn Baldwin #endif 671affd9185SKonstantin Belousov return (sp >= (size_t)td->td_sigstk.ss_sp && 672affd9185SKonstantin Belousov sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp); 6738b94a061SJohn Baldwin } 67479065dbaSBruce Evans 6756f841fb7SMarcel Moolenaar static __inline int 6766f841fb7SMarcel Moolenaar sigprop(int sig) 6772c42a146SMarcel Moolenaar { 6786f841fb7SMarcel Moolenaar 679ed6d876bSBrooks Davis if (sig > 0 && sig < nitems(sigproptbl)) 680ed6d876bSBrooks Davis return (sigproptbl[sig]); 6812c42a146SMarcel Moolenaar return (0); 682df8bae1dSRodney W. Grimes } 6832c42a146SMarcel Moolenaar 684350ae563SKonstantin Belousov static bool 685ea566832SEd Schouten sigact_flag_test(const struct sigaction *act, int flag) 686350ae563SKonstantin Belousov { 687350ae563SKonstantin Belousov 688c83655f3SKonstantin Belousov /* 689c83655f3SKonstantin Belousov * SA_SIGINFO is reset when signal disposition is set to 690c83655f3SKonstantin Belousov * ignore or default. Other flags are kept according to user 691c83655f3SKonstantin Belousov * settings. 692c83655f3SKonstantin Belousov */ 693c83655f3SKonstantin Belousov return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 694c83655f3SKonstantin Belousov ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 695c83655f3SKonstantin Belousov (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 696350ae563SKonstantin Belousov } 697350ae563SKonstantin Belousov 6982c42a146SMarcel Moolenaar /* 6998f19eb88SIan Dowse * kern_sigaction 7002c42a146SMarcel Moolenaar * sigaction 70123eeeff7SPeter Wemm * freebsd4_sigaction 7022c42a146SMarcel Moolenaar * osigaction 7032c42a146SMarcel Moolenaar */ 7048f19eb88SIan Dowse int 705ea566832SEd Schouten kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 706ea566832SEd Schouten struct sigaction *oact, int flags) 707df8bae1dSRodney W. Grimes { 70890af4afaSJohn Baldwin struct sigacts *ps; 7098f19eb88SIan Dowse struct proc *p = td->td_proc; 710df8bae1dSRodney W. Grimes 7112899d606SDag-Erling Smørgrav if (!_SIG_VALID(sig)) 7122c42a146SMarcel Moolenaar return (EINVAL); 713271ab240SKonstantin Belousov if (act != NULL && act->sa_handler != SIG_DFL && 714271ab240SKonstantin Belousov act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 715271ab240SKonstantin Belousov SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 716271ab240SKonstantin Belousov SA_NOCLDWAIT | SA_SIGINFO)) != 0) 7172d864174SKonstantin Belousov return (EINVAL); 7182c42a146SMarcel Moolenaar 719628d2653SJohn Baldwin PROC_LOCK(p); 720628d2653SJohn Baldwin ps = p->p_sigacts; 72190af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 7222c42a146SMarcel Moolenaar if (oact) { 723fb441a88SKonstantin Belousov memset(oact, 0, sizeof(*oact)); 7242c42a146SMarcel Moolenaar oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 7252c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigonstack, sig)) 7262c42a146SMarcel Moolenaar oact->sa_flags |= SA_ONSTACK; 7272c42a146SMarcel Moolenaar if (!SIGISMEMBER(ps->ps_sigintr, sig)) 7282c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESTART; 7292c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_sigreset, sig)) 7302c42a146SMarcel Moolenaar oact->sa_flags |= SA_RESETHAND; 7312c42a146SMarcel Moolenaar if (SIGISMEMBER(ps->ps_signodefer, sig)) 7322c42a146SMarcel Moolenaar oact->sa_flags |= SA_NODEFER; 73310c2b8e1SDavid E. O'Brien if (SIGISMEMBER(ps->ps_siginfo, sig)) { 7342c42a146SMarcel Moolenaar oact->sa_flags |= SA_SIGINFO; 73510c2b8e1SDavid E. O'Brien oact->sa_sigaction = 73610c2b8e1SDavid E. O'Brien (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 73710c2b8e1SDavid E. O'Brien } else 73810c2b8e1SDavid E. O'Brien oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 73990af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 7402c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDSTOP; 74190af4afaSJohn Baldwin if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 7422c42a146SMarcel Moolenaar oact->sa_flags |= SA_NOCLDWAIT; 7432c42a146SMarcel Moolenaar } 7442c42a146SMarcel Moolenaar if (act) { 7452c42a146SMarcel Moolenaar if ((sig == SIGKILL || sig == SIGSTOP) && 746628d2653SJohn Baldwin act->sa_handler != SIG_DFL) { 74790af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 748628d2653SJohn Baldwin PROC_UNLOCK(p); 7492c42a146SMarcel Moolenaar return (EINVAL); 750628d2653SJohn Baldwin } 7512c42a146SMarcel Moolenaar 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * Change setting atomically. 754df8bae1dSRodney W. Grimes */ 7552c42a146SMarcel Moolenaar 7562c42a146SMarcel Moolenaar ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 7572c42a146SMarcel Moolenaar SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 758350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_SIGINFO)) { 759aa7a4daeSPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] = 760aa7a4daeSPeter Wemm (__sighandler_t *)act->sa_sigaction; 76180f42b55SIan Dowse SIGADDSET(ps->ps_siginfo, sig); 76280f42b55SIan Dowse } else { 76380f42b55SIan Dowse ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 7642c42a146SMarcel Moolenaar SIGDELSET(ps->ps_siginfo, sig); 7652c42a146SMarcel Moolenaar } 766350ae563SKonstantin Belousov if (!sigact_flag_test(act, SA_RESTART)) 7672c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigintr, sig); 768df8bae1dSRodney W. Grimes else 7692c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigintr, sig); 770350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_ONSTACK)) 7712c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigonstack, sig); 772df8bae1dSRodney W. Grimes else 7732c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigonstack, sig); 774350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_RESETHAND)) 7752c42a146SMarcel Moolenaar SIGADDSET(ps->ps_sigreset, sig); 7761e41c1b5SSteven Wallace else 7772c42a146SMarcel Moolenaar SIGDELSET(ps->ps_sigreset, sig); 778350ae563SKonstantin Belousov if (sigact_flag_test(act, SA_NODEFER)) 7792c42a146SMarcel Moolenaar SIGADDSET(ps->ps_signodefer, sig); 780289ccde0SPeter Wemm else 7812c42a146SMarcel Moolenaar SIGDELSET(ps->ps_signodefer, sig); 7822c42a146SMarcel Moolenaar if (sig == SIGCHLD) { 7832c42a146SMarcel Moolenaar if (act->sa_flags & SA_NOCLDSTOP) 78490af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDSTOP; 7856626c604SJulian Elischer else 78690af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDSTOP; 787ba1551caSIan Dowse if (act->sa_flags & SA_NOCLDWAIT) { 788245f17d4SJoerg Wunsch /* 7892c42a146SMarcel Moolenaar * Paranoia: since SA_NOCLDWAIT is implemented 7902c42a146SMarcel Moolenaar * by reparenting the dying child to PID 1 (and 7912c42a146SMarcel Moolenaar * trust it to reap the zombie), PID 1 itself 7922c42a146SMarcel Moolenaar * is forbidden to set SA_NOCLDWAIT. 793245f17d4SJoerg Wunsch */ 794245f17d4SJoerg Wunsch if (p->p_pid == 1) 79590af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 7966626c604SJulian Elischer else 79790af4afaSJohn Baldwin ps->ps_flag |= PS_NOCLDWAIT; 798245f17d4SJoerg Wunsch } else 79990af4afaSJohn Baldwin ps->ps_flag &= ~PS_NOCLDWAIT; 800ba1551caSIan Dowse if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 80190af4afaSJohn Baldwin ps->ps_flag |= PS_CLDSIGIGN; 802ba1551caSIan Dowse else 80390af4afaSJohn Baldwin ps->ps_flag &= ~PS_CLDSIGIGN; 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes /* 80690af4afaSJohn Baldwin * Set bit in ps_sigignore for signals that are set to SIG_IGN, 8072c42a146SMarcel Moolenaar * and for signals set to SIG_DFL where the default is to 80890af4afaSJohn Baldwin * ignore. However, don't put SIGCONT in ps_sigignore, as we 8092c42a146SMarcel Moolenaar * have to restart the process. 810df8bae1dSRodney W. Grimes */ 8112c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 812fd50a707SBrooks Davis (sigprop(sig) & SIGPROP_IGNORE && 8132c42a146SMarcel Moolenaar ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 8142c42a146SMarcel Moolenaar /* never to be seen again */ 8159104847fSDavid Xu sigqueue_delete_proc(p, sig); 8162c42a146SMarcel Moolenaar if (sig != SIGCONT) 8172c42a146SMarcel Moolenaar /* easier in psignal */ 81890af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, sig); 81990af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 820645682fdSLuoqi Chen } else { 82190af4afaSJohn Baldwin SIGDELSET(ps->ps_sigignore, sig); 8222c42a146SMarcel Moolenaar if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 82390af4afaSJohn Baldwin SIGDELSET(ps->ps_sigcatch, sig); 8242c42a146SMarcel Moolenaar else 82590af4afaSJohn Baldwin SIGADDSET(ps->ps_sigcatch, sig); 8262c42a146SMarcel Moolenaar } 82723eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 82823eeeff7SPeter Wemm if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 82923eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 83023eeeff7SPeter Wemm (flags & KSA_FREEBSD4) == 0) 83123eeeff7SPeter Wemm SIGDELSET(ps->ps_freebsd4, sig); 83223eeeff7SPeter Wemm else 83323eeeff7SPeter Wemm SIGADDSET(ps->ps_freebsd4, sig); 83423eeeff7SPeter Wemm #endif 835e8ebc08fSPeter Wemm #ifdef COMPAT_43 836645682fdSLuoqi Chen if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 83723eeeff7SPeter Wemm ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 83823eeeff7SPeter Wemm (flags & KSA_OSIGSET) == 0) 839645682fdSLuoqi Chen SIGDELSET(ps->ps_osigset, sig); 840645682fdSLuoqi Chen else 841645682fdSLuoqi Chen SIGADDSET(ps->ps_osigset, sig); 842e8ebc08fSPeter Wemm #endif 843df8bae1dSRodney W. Grimes } 84490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 845628d2653SJohn Baldwin PROC_UNLOCK(p); 8462c42a146SMarcel Moolenaar return (0); 8472c42a146SMarcel Moolenaar } 8482c42a146SMarcel Moolenaar 8492c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 8502c42a146SMarcel Moolenaar struct sigaction_args { 8512c42a146SMarcel Moolenaar int sig; 8522c42a146SMarcel Moolenaar struct sigaction *act; 8532c42a146SMarcel Moolenaar struct sigaction *oact; 8542c42a146SMarcel Moolenaar }; 8552c42a146SMarcel Moolenaar #endif 8562c42a146SMarcel Moolenaar int 857e052a8b9SEd Maste sys_sigaction(struct thread *td, struct sigaction_args *uap) 8582c42a146SMarcel Moolenaar { 8592c42a146SMarcel Moolenaar struct sigaction act, oact; 860e052a8b9SEd Maste struct sigaction *actp, *oactp; 8612c42a146SMarcel Moolenaar int error; 8622c42a146SMarcel Moolenaar 8636f841fb7SMarcel Moolenaar actp = (uap->act != NULL) ? &act : NULL; 8646f841fb7SMarcel Moolenaar oactp = (uap->oact != NULL) ? &oact : NULL; 8652c42a146SMarcel Moolenaar if (actp) { 8666f841fb7SMarcel Moolenaar error = copyin(uap->act, actp, sizeof(act)); 8672c42a146SMarcel Moolenaar if (error) 86844443757STim J. Robbins return (error); 8692c42a146SMarcel Moolenaar } 8708f19eb88SIan Dowse error = kern_sigaction(td, uap->sig, actp, oactp, 0); 87125d6dc06SJohn Baldwin if (oactp && !error) 8726f841fb7SMarcel Moolenaar error = copyout(oactp, uap->oact, sizeof(oact)); 8732c42a146SMarcel Moolenaar return (error); 8742c42a146SMarcel Moolenaar } 8752c42a146SMarcel Moolenaar 87623eeeff7SPeter Wemm #ifdef COMPAT_FREEBSD4 87723eeeff7SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 87823eeeff7SPeter Wemm struct freebsd4_sigaction_args { 87923eeeff7SPeter Wemm int sig; 88023eeeff7SPeter Wemm struct sigaction *act; 88123eeeff7SPeter Wemm struct sigaction *oact; 88223eeeff7SPeter Wemm }; 88323eeeff7SPeter Wemm #endif 88423eeeff7SPeter Wemm int 885e052a8b9SEd Maste freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 88623eeeff7SPeter Wemm { 88723eeeff7SPeter Wemm struct sigaction act, oact; 888e052a8b9SEd Maste struct sigaction *actp, *oactp; 88923eeeff7SPeter Wemm int error; 89023eeeff7SPeter Wemm 89123eeeff7SPeter Wemm actp = (uap->act != NULL) ? &act : NULL; 89223eeeff7SPeter Wemm oactp = (uap->oact != NULL) ? &oact : NULL; 89323eeeff7SPeter Wemm if (actp) { 89423eeeff7SPeter Wemm error = copyin(uap->act, actp, sizeof(act)); 89523eeeff7SPeter Wemm if (error) 89644443757STim J. Robbins return (error); 89723eeeff7SPeter Wemm } 89823eeeff7SPeter Wemm error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 899a14e1189SJohn Baldwin if (oactp && !error) 90023eeeff7SPeter Wemm error = copyout(oactp, uap->oact, sizeof(oact)); 90123eeeff7SPeter Wemm return (error); 90223eeeff7SPeter Wemm } 90323eeeff7SPeter Wemm #endif /* COMAPT_FREEBSD4 */ 90423eeeff7SPeter Wemm 90531c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 9062c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 9072c42a146SMarcel Moolenaar struct osigaction_args { 9082c42a146SMarcel Moolenaar int signum; 9092c42a146SMarcel Moolenaar struct osigaction *nsa; 9102c42a146SMarcel Moolenaar struct osigaction *osa; 9112c42a146SMarcel Moolenaar }; 9122c42a146SMarcel Moolenaar #endif 9132c42a146SMarcel Moolenaar int 914e052a8b9SEd Maste osigaction(struct thread *td, struct osigaction_args *uap) 9152c42a146SMarcel Moolenaar { 9162c42a146SMarcel Moolenaar struct osigaction sa; 9172c42a146SMarcel Moolenaar struct sigaction nsa, osa; 918e052a8b9SEd Maste struct sigaction *nsap, *osap; 9192c42a146SMarcel Moolenaar int error; 9202c42a146SMarcel Moolenaar 9216f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 9226f841fb7SMarcel Moolenaar return (EINVAL); 923fb99ab88SMatthew Dillon 9246f841fb7SMarcel Moolenaar nsap = (uap->nsa != NULL) ? &nsa : NULL; 9256f841fb7SMarcel Moolenaar osap = (uap->osa != NULL) ? &osa : NULL; 926fb99ab88SMatthew Dillon 9272c42a146SMarcel Moolenaar if (nsap) { 9286f841fb7SMarcel Moolenaar error = copyin(uap->nsa, &sa, sizeof(sa)); 9292c42a146SMarcel Moolenaar if (error) 93044443757STim J. Robbins return (error); 9312c42a146SMarcel Moolenaar nsap->sa_handler = sa.sa_handler; 9322c42a146SMarcel Moolenaar nsap->sa_flags = sa.sa_flags; 9332c42a146SMarcel Moolenaar OSIG2SIG(sa.sa_mask, nsap->sa_mask); 9342c42a146SMarcel Moolenaar } 93523eeeff7SPeter Wemm error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 9362c42a146SMarcel Moolenaar if (osap && !error) { 9372c42a146SMarcel Moolenaar sa.sa_handler = osap->sa_handler; 9382c42a146SMarcel Moolenaar sa.sa_flags = osap->sa_flags; 9392c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, sa.sa_mask); 9406f841fb7SMarcel Moolenaar error = copyout(&sa, uap->osa, sizeof(sa)); 9412c42a146SMarcel Moolenaar } 9422c42a146SMarcel Moolenaar return (error); 9432c42a146SMarcel Moolenaar } 94423eeeff7SPeter Wemm 94573dbd3daSJohn Baldwin #if !defined(__i386__) 94623eeeff7SPeter Wemm /* Avoid replicating the same stub everywhere */ 94723eeeff7SPeter Wemm int 948e052a8b9SEd Maste osigreturn(struct thread *td, struct osigreturn_args *uap) 94923eeeff7SPeter Wemm { 95023eeeff7SPeter Wemm 95123eeeff7SPeter Wemm return (nosys(td, (struct nosys_args *)uap)); 95223eeeff7SPeter Wemm } 95323eeeff7SPeter Wemm #endif 95431c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 955df8bae1dSRodney W. Grimes 956df8bae1dSRodney W. Grimes /* 957df8bae1dSRodney W. Grimes * Initialize signal state for process 0; 958df8bae1dSRodney W. Grimes * set to ignore signals that are ignored by default. 959df8bae1dSRodney W. Grimes */ 960df8bae1dSRodney W. Grimes void 961e052a8b9SEd Maste siginit(struct proc *p) 962df8bae1dSRodney W. Grimes { 9633e85b721SEd Maste int i; 96490af4afaSJohn Baldwin struct sigacts *ps; 965df8bae1dSRodney W. Grimes 966628d2653SJohn Baldwin PROC_LOCK(p); 96790af4afaSJohn Baldwin ps = p->p_sigacts; 96890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 969350ae563SKonstantin Belousov for (i = 1; i <= NSIG; i++) { 970fd50a707SBrooks Davis if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 97190af4afaSJohn Baldwin SIGADDSET(ps->ps_sigignore, i); 972350ae563SKonstantin Belousov } 973350ae563SKonstantin Belousov } 97490af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 975628d2653SJohn Baldwin PROC_UNLOCK(p); 976df8bae1dSRodney W. Grimes } 977df8bae1dSRodney W. Grimes 978df8bae1dSRodney W. Grimes /* 979350ae563SKonstantin Belousov * Reset specified signal to the default disposition. 980350ae563SKonstantin Belousov */ 981350ae563SKonstantin Belousov static void 982350ae563SKonstantin Belousov sigdflt(struct sigacts *ps, int sig) 983350ae563SKonstantin Belousov { 984350ae563SKonstantin Belousov 985350ae563SKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 986350ae563SKonstantin Belousov SIGDELSET(ps->ps_sigcatch, sig); 987fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 988350ae563SKonstantin Belousov SIGADDSET(ps->ps_sigignore, sig); 989350ae563SKonstantin Belousov ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 990350ae563SKonstantin Belousov SIGDELSET(ps->ps_siginfo, sig); 991350ae563SKonstantin Belousov } 992350ae563SKonstantin Belousov 993350ae563SKonstantin Belousov /* 994df8bae1dSRodney W. Grimes * Reset signals for an exec of the specified process. 995df8bae1dSRodney W. Grimes */ 996df8bae1dSRodney W. Grimes void 997a30ec4b9SDavid Xu execsigs(struct proc *p) 998df8bae1dSRodney W. Grimes { 999a30ec4b9SDavid Xu struct sigacts *ps; 1000a30ec4b9SDavid Xu struct thread *td; 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes /* 1003df8bae1dSRodney W. Grimes * Reset caught signals. Held signals remain held 10044093529dSJeff Roberson * through td_sigmask (unless they were caught, 1005df8bae1dSRodney W. Grimes * and are now ignored by default). 1006df8bae1dSRodney W. Grimes */ 10079b3b1c5fSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1008628d2653SJohn Baldwin ps = p->p_sigacts; 100990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 1010079c5b9eSKyle Evans sig_drop_caught(p); 1011f3fe76ecSEd Schouten 1012f3fe76ecSEd Schouten /* 1013df8bae1dSRodney W. Grimes * Reset stack state to the user stack. 1014df8bae1dSRodney W. Grimes * Clear set of signals caught on the signal stack. 1015df8bae1dSRodney W. Grimes */ 1016469ec1ebSKonstantin Belousov td = curthread; 1017469ec1ebSKonstantin Belousov MPASS(td->td_proc == p); 1018a30ec4b9SDavid Xu td->td_sigstk.ss_flags = SS_DISABLE; 1019a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1020a30ec4b9SDavid Xu td->td_sigstk.ss_sp = 0; 1021a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 102280e907a1SPeter Wemm /* 102380e907a1SPeter Wemm * Reset no zombies if child dies flag as Solaris does. 102480e907a1SPeter Wemm */ 102590af4afaSJohn Baldwin ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1026c7fd62daSDavid Malone if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1027c7fd62daSDavid Malone ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 102890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 1029df8bae1dSRodney W. Grimes } 1030df8bae1dSRodney W. Grimes 1031df8bae1dSRodney W. Grimes /* 1032e77daab1SJohn Baldwin * kern_sigprocmask() 10337c8fdcbdSMatthew Dillon * 1034628d2653SJohn Baldwin * Manipulate signal mask. 1035df8bae1dSRodney W. Grimes */ 1036e77daab1SJohn Baldwin int 10376b286ee8SKonstantin Belousov kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 103884440afbSKonstantin Belousov int flags) 10392c42a146SMarcel Moolenaar { 10406b286ee8SKonstantin Belousov sigset_t new_block, oset1; 10416b286ee8SKonstantin Belousov struct proc *p; 10422c42a146SMarcel Moolenaar int error; 10432c42a146SMarcel Moolenaar 10446b286ee8SKonstantin Belousov p = td->td_proc; 104570778bbaSKonstantin Belousov if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 104670778bbaSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 104770778bbaSKonstantin Belousov else 10486b286ee8SKonstantin Belousov PROC_LOCK(p); 104970778bbaSKonstantin Belousov mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 105070778bbaSKonstantin Belousov ? MA_OWNED : MA_NOTOWNED); 10512c42a146SMarcel Moolenaar if (oset != NULL) 10524093529dSJeff Roberson *oset = td->td_sigmask; 10532c42a146SMarcel Moolenaar 10542c42a146SMarcel Moolenaar error = 0; 10552c42a146SMarcel Moolenaar if (set != NULL) { 10562c42a146SMarcel Moolenaar switch (how) { 10572c42a146SMarcel Moolenaar case SIG_BLOCK: 1058645682fdSLuoqi Chen SIG_CANTMASK(*set); 10596b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 10604093529dSJeff Roberson SIGSETOR(td->td_sigmask, *set); 10616b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10626b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10632c42a146SMarcel Moolenaar break; 10642c42a146SMarcel Moolenaar case SIG_UNBLOCK: 10654093529dSJeff Roberson SIGSETNAND(td->td_sigmask, *set); 10664093529dSJeff Roberson signotify(td); 1067407af02bSDavid Xu goto out; 10682c42a146SMarcel Moolenaar case SIG_SETMASK: 1069645682fdSLuoqi Chen SIG_CANTMASK(*set); 10706b286ee8SKonstantin Belousov oset1 = td->td_sigmask; 107184440afbSKonstantin Belousov if (flags & SIGPROCMASK_OLD) 10724093529dSJeff Roberson SIGSETLO(td->td_sigmask, *set); 1073645682fdSLuoqi Chen else 10744093529dSJeff Roberson td->td_sigmask = *set; 10756b286ee8SKonstantin Belousov new_block = td->td_sigmask; 10766b286ee8SKonstantin Belousov SIGSETNAND(new_block, oset1); 10774093529dSJeff Roberson signotify(td); 10782c42a146SMarcel Moolenaar break; 10792c42a146SMarcel Moolenaar default: 10802c42a146SMarcel Moolenaar error = EINVAL; 1081407af02bSDavid Xu goto out; 10822c42a146SMarcel Moolenaar } 10836b286ee8SKonstantin Belousov 10846b286ee8SKonstantin Belousov /* 10857df8f6abSKonstantin Belousov * The new_block set contains signals that were not previously 10866b286ee8SKonstantin Belousov * blocked, but are blocked now. 10876b286ee8SKonstantin Belousov * 10886b286ee8SKonstantin Belousov * In case we block any signal that was not previously blocked 10896b286ee8SKonstantin Belousov * for td, and process has the signal pending, try to schedule 1090407af02bSDavid Xu * signal delivery to some thread that does not block the 1091407af02bSDavid Xu * signal, possibly waking it up. 10926b286ee8SKonstantin Belousov */ 10936b286ee8SKonstantin Belousov if (p->p_numthreads != 1) 109480a8b0f3SKonstantin Belousov reschedule_signals(p, new_block, flags); 1095407af02bSDavid Xu } 10966b286ee8SKonstantin Belousov 1097407af02bSDavid Xu out: 109884440afbSKonstantin Belousov if (!(flags & SIGPROCMASK_PROC_LOCKED)) 10996b286ee8SKonstantin Belousov PROC_UNLOCK(p); 11002c42a146SMarcel Moolenaar return (error); 11012c42a146SMarcel Moolenaar } 11022c42a146SMarcel Moolenaar 1103d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1104df8bae1dSRodney W. Grimes struct sigprocmask_args { 1105df8bae1dSRodney W. Grimes int how; 11062c42a146SMarcel Moolenaar const sigset_t *set; 11072c42a146SMarcel Moolenaar sigset_t *oset; 1108df8bae1dSRodney W. Grimes }; 1109d2d3e875SBruce Evans #endif 111026f9a767SRodney W. Grimes int 1111e052a8b9SEd Maste sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1112df8bae1dSRodney W. Grimes { 11132c42a146SMarcel Moolenaar sigset_t set, oset; 11142c42a146SMarcel Moolenaar sigset_t *setp, *osetp; 11152c42a146SMarcel Moolenaar int error; 1116df8bae1dSRodney W. Grimes 11176f841fb7SMarcel Moolenaar setp = (uap->set != NULL) ? &set : NULL; 11186f841fb7SMarcel Moolenaar osetp = (uap->oset != NULL) ? &oset : NULL; 11192c42a146SMarcel Moolenaar if (setp) { 11206f841fb7SMarcel Moolenaar error = copyin(uap->set, setp, sizeof(set)); 11212c42a146SMarcel Moolenaar if (error) 11222c42a146SMarcel Moolenaar return (error); 1123df8bae1dSRodney W. Grimes } 1124e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 11252c42a146SMarcel Moolenaar if (osetp && !error) { 11266f841fb7SMarcel Moolenaar error = copyout(osetp, uap->oset, sizeof(oset)); 11272c42a146SMarcel Moolenaar } 11282c42a146SMarcel Moolenaar return (error); 11292c42a146SMarcel Moolenaar } 11302c42a146SMarcel Moolenaar 113131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 11322c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 11332c42a146SMarcel Moolenaar struct osigprocmask_args { 11342c42a146SMarcel Moolenaar int how; 11352c42a146SMarcel Moolenaar osigset_t mask; 11362c42a146SMarcel Moolenaar }; 11372c42a146SMarcel Moolenaar #endif 11382c42a146SMarcel Moolenaar int 1139e052a8b9SEd Maste osigprocmask(struct thread *td, struct osigprocmask_args *uap) 11402c42a146SMarcel Moolenaar { 11412c42a146SMarcel Moolenaar sigset_t set, oset; 11422c42a146SMarcel Moolenaar int error; 11432c42a146SMarcel Moolenaar 11442c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1145e77daab1SJohn Baldwin error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1146b40ce416SJulian Elischer SIG2OSIG(oset, td->td_retval[0]); 1147df8bae1dSRodney W. Grimes return (error); 1148df8bae1dSRodney W. Grimes } 114931c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1150df8bae1dSRodney W. Grimes 115126f9a767SRodney W. Grimes int 11528451d0ddSKip Macy sys_sigwait(struct thread *td, struct sigwait_args *uap) 1153a447cd8bSJeff Roberson { 11549104847fSDavid Xu ksiginfo_t ksi; 1155a447cd8bSJeff Roberson sigset_t set; 1156a447cd8bSJeff Roberson int error; 1157a447cd8bSJeff Roberson 1158a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 115936939a0aSDavid Xu if (error) { 116036939a0aSDavid Xu td->td_retval[0] = error; 116136939a0aSDavid Xu return (0); 116236939a0aSDavid Xu } 1163a447cd8bSJeff Roberson 11649104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 116536939a0aSDavid Xu if (error) { 1166acced8b0SKonstantin Belousov /* 1167acced8b0SKonstantin Belousov * sigwait() function shall not return EINTR, but 1168acced8b0SKonstantin Belousov * the syscall does. Non-ancient libc provides the 1169acced8b0SKonstantin Belousov * wrapper which hides EINTR. Otherwise, EINTR return 1170acced8b0SKonstantin Belousov * is used by libthr to handle required cancellation 1171acced8b0SKonstantin Belousov * point in the sigwait(). 1172acced8b0SKonstantin Belousov */ 11738e9a54eeSKonstantin Belousov if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 1174afb36e28SKonstantin Belousov return (ERESTART); 117536939a0aSDavid Xu td->td_retval[0] = error; 117636939a0aSDavid Xu return (0); 117736939a0aSDavid Xu } 1178a447cd8bSJeff Roberson 11799104847fSDavid Xu error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 118036939a0aSDavid Xu td->td_retval[0] = error; 118136939a0aSDavid Xu return (0); 1182a447cd8bSJeff Roberson } 11830c14ff0eSRobert Watson 1184a447cd8bSJeff Roberson int 11858451d0ddSKip Macy sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1186a447cd8bSJeff Roberson { 1187a447cd8bSJeff Roberson struct timespec ts; 1188a447cd8bSJeff Roberson struct timespec *timeout; 1189a447cd8bSJeff Roberson sigset_t set; 11909104847fSDavid Xu ksiginfo_t ksi; 1191a447cd8bSJeff Roberson int error; 1192a447cd8bSJeff Roberson 1193a447cd8bSJeff Roberson if (uap->timeout) { 1194a447cd8bSJeff Roberson error = copyin(uap->timeout, &ts, sizeof(ts)); 1195a447cd8bSJeff Roberson if (error) 1196a447cd8bSJeff Roberson return (error); 1197a447cd8bSJeff Roberson 1198a447cd8bSJeff Roberson timeout = &ts; 1199a447cd8bSJeff Roberson } else 1200a447cd8bSJeff Roberson timeout = NULL; 1201a447cd8bSJeff Roberson 1202a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1203a447cd8bSJeff Roberson if (error) 1204a447cd8bSJeff Roberson return (error); 1205a447cd8bSJeff Roberson 12069104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, timeout); 1207a447cd8bSJeff Roberson if (error) 1208a447cd8bSJeff Roberson return (error); 12099dde3bc9SDavid Xu 1210418228dfSDavid Xu if (uap->info) 12119104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12129104847fSDavid Xu 12139104847fSDavid Xu if (error == 0) 12149104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1215a447cd8bSJeff Roberson return (error); 1216a447cd8bSJeff Roberson } 1217a447cd8bSJeff Roberson 1218a447cd8bSJeff Roberson int 12198451d0ddSKip Macy sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1220a447cd8bSJeff Roberson { 12219104847fSDavid Xu ksiginfo_t ksi; 1222a447cd8bSJeff Roberson sigset_t set; 1223a447cd8bSJeff Roberson int error; 1224a447cd8bSJeff Roberson 1225a447cd8bSJeff Roberson error = copyin(uap->set, &set, sizeof(set)); 1226a447cd8bSJeff Roberson if (error) 1227a447cd8bSJeff Roberson return (error); 1228a447cd8bSJeff Roberson 12299104847fSDavid Xu error = kern_sigtimedwait(td, set, &ksi, NULL); 1230a447cd8bSJeff Roberson if (error) 1231a447cd8bSJeff Roberson return (error); 1232a447cd8bSJeff Roberson 1233418228dfSDavid Xu if (uap->info) 12349104847fSDavid Xu error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 12359104847fSDavid Xu 12369104847fSDavid Xu if (error == 0) 12379104847fSDavid Xu td->td_retval[0] = ksi.ksi_signo; 1238a447cd8bSJeff Roberson return (error); 1239a447cd8bSJeff Roberson } 1240a447cd8bSJeff Roberson 124186be94fcSTycho Nightingale static void 124286be94fcSTycho Nightingale proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 124386be94fcSTycho Nightingale { 124486be94fcSTycho Nightingale struct thread *thr; 124586be94fcSTycho Nightingale 124686be94fcSTycho Nightingale FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 124786be94fcSTycho Nightingale if (thr == td) 124886be94fcSTycho Nightingale thr->td_si = *si; 124986be94fcSTycho Nightingale else 125086be94fcSTycho Nightingale thr->td_si.si_signo = 0; 125186be94fcSTycho Nightingale } 125286be94fcSTycho Nightingale } 125386be94fcSTycho Nightingale 1254c6511aeaSDavid Xu int 12559104847fSDavid Xu kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1256a447cd8bSJeff Roberson struct timespec *timeout) 1257a447cd8bSJeff Roberson { 12583074d1b4SDavid Xu struct sigacts *ps; 1259407af02bSDavid Xu sigset_t saved_mask, new_block; 1260a447cd8bSJeff Roberson struct proc *p; 12614a700f3cSDmitry Chagin int error, sig, timevalid = 0; 12624a700f3cSDmitry Chagin sbintime_t sbt, precision, tsbt; 12634a700f3cSDmitry Chagin struct timespec ts; 1264ef401a85SKonstantin Belousov bool traced; 1265a447cd8bSJeff Roberson 1266a447cd8bSJeff Roberson p = td->td_proc; 1267a447cd8bSJeff Roberson error = 0; 1268ef401a85SKonstantin Belousov traced = false; 1269a447cd8bSJeff Roberson 1270dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1271dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1272dbec10e0SJonathan T. Looney 1273407af02bSDavid Xu if (timeout != NULL) { 12741089f031SDavid Xu if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 12751089f031SDavid Xu timevalid = 1; 12764a700f3cSDmitry Chagin ts = *timeout; 12774a700f3cSDmitry Chagin if (ts.tv_sec < INT32_MAX / 2) { 12784a700f3cSDmitry Chagin tsbt = tstosbt(ts); 12794a700f3cSDmitry Chagin precision = tsbt; 12804a700f3cSDmitry Chagin precision >>= tc_precexp; 12814a700f3cSDmitry Chagin if (TIMESEL(&sbt, tsbt)) 12824a700f3cSDmitry Chagin sbt += tc_tick_sbt; 12834a700f3cSDmitry Chagin sbt += tsbt; 12844a700f3cSDmitry Chagin } else 12854a700f3cSDmitry Chagin precision = sbt = 0; 12861089f031SDavid Xu } 12874a700f3cSDmitry Chagin } else 12884a700f3cSDmitry Chagin precision = sbt = 0; 1289407af02bSDavid Xu ksiginfo_init(ksi); 1290407af02bSDavid Xu /* Some signals can not be waited for. */ 1291407af02bSDavid Xu SIG_CANTMASK(waitset); 1292407af02bSDavid Xu ps = p->p_sigacts; 1293407af02bSDavid Xu PROC_LOCK(p); 1294407af02bSDavid Xu saved_mask = td->td_sigmask; 1295407af02bSDavid Xu SIGSETNAND(td->td_sigmask, waitset); 1296bc387624SKonstantin Belousov if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 || 1297b599982bSKonstantin Belousov !kern_sig_discard_ign) { 1298b599982bSKonstantin Belousov thread_lock(td); 1299b599982bSKonstantin Belousov td->td_flags |= TDF_SIGWAIT; 1300b599982bSKonstantin Belousov thread_unlock(td); 1301b599982bSKonstantin Belousov } 1302407af02bSDavid Xu for (;;) { 13033074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 13043cf3b9f0SJohn Baldwin sig = cursig(td); 13053074d1b4SDavid Xu mtx_unlock(&ps->ps_mtx); 130646e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 1307407af02bSDavid Xu if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1308407af02bSDavid Xu if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1309407af02bSDavid Xu sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1310407af02bSDavid Xu error = 0; 1311407af02bSDavid Xu break; 13123074d1b4SDavid Xu } 13137e0221a2SDavid Xu } 13147e0221a2SDavid Xu 1315407af02bSDavid Xu if (error != 0) 1316407af02bSDavid Xu break; 13173074d1b4SDavid Xu 1318a447cd8bSJeff Roberson /* 1319a447cd8bSJeff Roberson * POSIX says this must be checked after looking for pending 1320a447cd8bSJeff Roberson * signals. 1321a447cd8bSJeff Roberson */ 13224a700f3cSDmitry Chagin if (timeout != NULL && !timevalid) { 1323a447cd8bSJeff Roberson error = EINVAL; 1324407af02bSDavid Xu break; 1325a447cd8bSJeff Roberson } 1326a447cd8bSJeff Roberson 1327ef401a85SKonstantin Belousov if (traced) { 1328ef401a85SKonstantin Belousov error = EINTR; 1329ef401a85SKonstantin Belousov break; 1330ef401a85SKonstantin Belousov } 1331ef401a85SKonstantin Belousov 13324a700f3cSDmitry Chagin error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, 13334a700f3cSDmitry Chagin "sigwait", sbt, precision, C_ABSOLUTE); 1334407af02bSDavid Xu 1335afb36e28SKonstantin Belousov /* The syscalls can not be restarted. */ 1336afb36e28SKonstantin Belousov if (error == ERESTART) 13373074d1b4SDavid Xu error = EINTR; 1338afb36e28SKonstantin Belousov 1339ef401a85SKonstantin Belousov /* 1340ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1341ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1342ef401a85SKonstantin Belousov * EINTR after wait was done. Only do this as last 1343ef401a85SKonstantin Belousov * resort after rechecking for possible queued signals 1344ef401a85SKonstantin Belousov * and expired timeouts. 1345ef401a85SKonstantin Belousov */ 1346ef401a85SKonstantin Belousov if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0) 1347ef401a85SKonstantin Belousov traced = true; 1348407af02bSDavid Xu } 1349b599982bSKonstantin Belousov thread_lock(td); 1350b599982bSKonstantin Belousov td->td_flags &= ~TDF_SIGWAIT; 1351b599982bSKonstantin Belousov thread_unlock(td); 13529dde3bc9SDavid Xu 1353407af02bSDavid Xu new_block = saved_mask; 1354407af02bSDavid Xu SIGSETNAND(new_block, td->td_sigmask); 1355407af02bSDavid Xu td->td_sigmask = saved_mask; 1356407af02bSDavid Xu /* 1357407af02bSDavid Xu * Fewer signals can be delivered to us, reschedule signal 1358407af02bSDavid Xu * notification. 1359407af02bSDavid Xu */ 1360407af02bSDavid Xu if (p->p_numthreads != 1) 1361407af02bSDavid Xu reschedule_signals(p, new_block, 0); 13625d217f17SJohn Birrell 1363407af02bSDavid Xu if (error == 0) { 136436160958SMark Johnston SDT_PROBE2(proc, , , signal__clear, sig, ksi); 13655d217f17SJohn Birrell 136656c06c4bSDavid Xu if (ksi->ksi_code == SI_TIMER) 136756c06c4bSDavid Xu itimer_accept(p, ksi->ksi_timerid, ksi); 13687e0221a2SDavid Xu 13697e0221a2SDavid Xu #ifdef KTRACE 13707e0221a2SDavid Xu if (KTRPOINT(td, KTR_PSIG)) { 13717e0221a2SDavid Xu sig_t action; 13727e0221a2SDavid Xu 13733074d1b4SDavid Xu mtx_lock(&ps->ps_mtx); 1374a447cd8bSJeff Roberson action = ps->ps_sigact[_SIG_IDX(sig)]; 137590af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 137661009552SJilles Tjoelker ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 13777e0221a2SDavid Xu } 1378a447cd8bSJeff Roberson #endif 137986be94fcSTycho Nightingale if (sig == SIGKILL) { 138086be94fcSTycho Nightingale proc_td_siginfo_capture(td, &ksi->ksi_info); 13817e0221a2SDavid Xu sigexit(td, sig); 13823074d1b4SDavid Xu } 138386be94fcSTycho Nightingale } 1384a447cd8bSJeff Roberson PROC_UNLOCK(p); 1385a447cd8bSJeff Roberson return (error); 1386a447cd8bSJeff Roberson } 1387a447cd8bSJeff Roberson 13889104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 13899104847fSDavid Xu struct sigpending_args { 13909104847fSDavid Xu sigset_t *set; 13919104847fSDavid Xu }; 13929104847fSDavid Xu #endif 1393a447cd8bSJeff Roberson int 1394e052a8b9SEd Maste sys_sigpending(struct thread *td, struct sigpending_args *uap) 1395df8bae1dSRodney W. Grimes { 1396b40ce416SJulian Elischer struct proc *p = td->td_proc; 13979104847fSDavid Xu sigset_t pending; 1398df8bae1dSRodney W. Grimes 1399628d2653SJohn Baldwin PROC_LOCK(p); 14009104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 14019104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1402628d2653SJohn Baldwin PROC_UNLOCK(p); 14039104847fSDavid Xu return (copyout(&pending, uap->set, sizeof(sigset_t))); 14042c42a146SMarcel Moolenaar } 14052c42a146SMarcel Moolenaar 140631c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 14072c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 14082c42a146SMarcel Moolenaar struct osigpending_args { 14092c42a146SMarcel Moolenaar int dummy; 14102c42a146SMarcel Moolenaar }; 14112c42a146SMarcel Moolenaar #endif 14122c42a146SMarcel Moolenaar int 1413e052a8b9SEd Maste osigpending(struct thread *td, struct osigpending_args *uap) 14142c42a146SMarcel Moolenaar { 1415b40ce416SJulian Elischer struct proc *p = td->td_proc; 14169104847fSDavid Xu sigset_t pending; 1417b40ce416SJulian Elischer 1418628d2653SJohn Baldwin PROC_LOCK(p); 14199104847fSDavid Xu pending = p->p_sigqueue.sq_signals; 14209104847fSDavid Xu SIGSETOR(pending, td->td_sigqueue.sq_signals); 1421628d2653SJohn Baldwin PROC_UNLOCK(p); 14229104847fSDavid Xu SIG2OSIG(pending, td->td_retval[0]); 1423df8bae1dSRodney W. Grimes return (0); 1424df8bae1dSRodney W. Grimes } 142531c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1426df8bae1dSRodney W. Grimes 14271930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1428df8bae1dSRodney W. Grimes /* 1429df8bae1dSRodney W. Grimes * Generalized interface signal handler, 4.3-compatible. 1430df8bae1dSRodney W. Grimes */ 1431d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1432df8bae1dSRodney W. Grimes struct osigvec_args { 1433df8bae1dSRodney W. Grimes int signum; 1434df8bae1dSRodney W. Grimes struct sigvec *nsv; 1435df8bae1dSRodney W. Grimes struct sigvec *osv; 1436df8bae1dSRodney W. Grimes }; 1437d2d3e875SBruce Evans #endif 1438df8bae1dSRodney W. Grimes /* ARGSUSED */ 143926f9a767SRodney W. Grimes int 1440e052a8b9SEd Maste osigvec(struct thread *td, struct osigvec_args *uap) 1441df8bae1dSRodney W. Grimes { 1442df8bae1dSRodney W. Grimes struct sigvec vec; 14432c42a146SMarcel Moolenaar struct sigaction nsa, osa; 1444e052a8b9SEd Maste struct sigaction *nsap, *osap; 14452c42a146SMarcel Moolenaar int error; 1446df8bae1dSRodney W. Grimes 14476f841fb7SMarcel Moolenaar if (uap->signum <= 0 || uap->signum >= ONSIG) 14486f841fb7SMarcel Moolenaar return (EINVAL); 14496f841fb7SMarcel Moolenaar nsap = (uap->nsv != NULL) ? &nsa : NULL; 14506f841fb7SMarcel Moolenaar osap = (uap->osv != NULL) ? &osa : NULL; 14512c42a146SMarcel Moolenaar if (nsap) { 14526f841fb7SMarcel Moolenaar error = copyin(uap->nsv, &vec, sizeof(vec)); 14532c42a146SMarcel Moolenaar if (error) 1454df8bae1dSRodney W. Grimes return (error); 14552c42a146SMarcel Moolenaar nsap->sa_handler = vec.sv_handler; 14562c42a146SMarcel Moolenaar OSIG2SIG(vec.sv_mask, nsap->sa_mask); 14572c42a146SMarcel Moolenaar nsap->sa_flags = vec.sv_flags; 14582c42a146SMarcel Moolenaar nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1459df8bae1dSRodney W. Grimes } 14605edadff9SJohn Baldwin error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 14612c42a146SMarcel Moolenaar if (osap && !error) { 14622c42a146SMarcel Moolenaar vec.sv_handler = osap->sa_handler; 14632c42a146SMarcel Moolenaar SIG2OSIG(osap->sa_mask, vec.sv_mask); 14642c42a146SMarcel Moolenaar vec.sv_flags = osap->sa_flags; 14652c42a146SMarcel Moolenaar vec.sv_flags &= ~SA_NOCLDWAIT; 14662c42a146SMarcel Moolenaar vec.sv_flags ^= SA_RESTART; 14676f841fb7SMarcel Moolenaar error = copyout(&vec, uap->osv, sizeof(vec)); 14682c42a146SMarcel Moolenaar } 14692c42a146SMarcel Moolenaar return (error); 1470df8bae1dSRodney W. Grimes } 1471df8bae1dSRodney W. Grimes 1472d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1473df8bae1dSRodney W. Grimes struct osigblock_args { 1474df8bae1dSRodney W. Grimes int mask; 1475df8bae1dSRodney W. Grimes }; 1476d2d3e875SBruce Evans #endif 147726f9a767SRodney W. Grimes int 1478e052a8b9SEd Maste osigblock(struct thread *td, struct osigblock_args *uap) 1479df8bae1dSRodney W. Grimes { 1480d6e029adSKonstantin Belousov sigset_t set, oset; 1481df8bae1dSRodney W. Grimes 14822c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1483d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1484d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1485df8bae1dSRodney W. Grimes return (0); 1486df8bae1dSRodney W. Grimes } 1487df8bae1dSRodney W. Grimes 1488d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1489df8bae1dSRodney W. Grimes struct osigsetmask_args { 1490df8bae1dSRodney W. Grimes int mask; 1491df8bae1dSRodney W. Grimes }; 1492d2d3e875SBruce Evans #endif 149326f9a767SRodney W. Grimes int 1494e052a8b9SEd Maste osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1495df8bae1dSRodney W. Grimes { 1496d6e029adSKonstantin Belousov sigset_t set, oset; 1497df8bae1dSRodney W. Grimes 14982c42a146SMarcel Moolenaar OSIG2SIG(uap->mask, set); 1499d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1500d6e029adSKonstantin Belousov SIG2OSIG(oset, td->td_retval[0]); 1501df8bae1dSRodney W. Grimes return (0); 1502df8bae1dSRodney W. Grimes } 15031930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1504df8bae1dSRodney W. Grimes 1505df8bae1dSRodney W. Grimes /* 1506873fbcd7SRobert Watson * Suspend calling thread until signal, providing mask to be set in the 1507873fbcd7SRobert Watson * meantime. 1508df8bae1dSRodney W. Grimes */ 1509d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1510df8bae1dSRodney W. Grimes struct sigsuspend_args { 15112c42a146SMarcel Moolenaar const sigset_t *sigmask; 1512df8bae1dSRodney W. Grimes }; 1513d2d3e875SBruce Evans #endif 1514df8bae1dSRodney W. Grimes /* ARGSUSED */ 151526f9a767SRodney W. Grimes int 1516e052a8b9SEd Maste sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1517df8bae1dSRodney W. Grimes { 15182c42a146SMarcel Moolenaar sigset_t mask; 15192c42a146SMarcel Moolenaar int error; 15202c42a146SMarcel Moolenaar 15216f841fb7SMarcel Moolenaar error = copyin(uap->sigmask, &mask, sizeof(mask)); 15222c42a146SMarcel Moolenaar if (error) 15232c42a146SMarcel Moolenaar return (error); 15248f19eb88SIan Dowse return (kern_sigsuspend(td, mask)); 15258f19eb88SIan Dowse } 15268f19eb88SIan Dowse 15278f19eb88SIan Dowse int 15288f19eb88SIan Dowse kern_sigsuspend(struct thread *td, sigset_t mask) 15298f19eb88SIan Dowse { 15308f19eb88SIan Dowse struct proc *p = td->td_proc; 153184440afbSKonstantin Belousov int has_sig, sig; 1532df8bae1dSRodney W. Grimes 1533dbec10e0SJonathan T. Looney /* Ensure the sigfastblock value is up to date. */ 1534dbec10e0SJonathan T. Looney sigfastblock_fetch(td); 1535dbec10e0SJonathan T. Looney 1536df8bae1dSRodney W. Grimes /* 1537645682fdSLuoqi Chen * When returning from sigsuspend, we want 1538df8bae1dSRodney W. Grimes * the old mask to be restored after the 1539df8bae1dSRodney W. Grimes * signal handler has finished. Thus, we 1540df8bae1dSRodney W. Grimes * save it here and mark the sigacts structure 1541df8bae1dSRodney W. Grimes * to indicate this. 1542df8bae1dSRodney W. Grimes */ 1543628d2653SJohn Baldwin PROC_LOCK(p); 154484440afbSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 154584440afbSKonstantin Belousov SIGPROCMASK_PROC_LOCKED); 15465e26dcb5SJohn Baldwin td->td_pflags |= TDP_OLDMASK; 154784440afbSKonstantin Belousov 154884440afbSKonstantin Belousov /* 154984440afbSKonstantin Belousov * Process signals now. Otherwise, we can get spurious wakeup 155084440afbSKonstantin Belousov * due to signal entered process queue, but delivered to other 155184440afbSKonstantin Belousov * thread. But sigsuspend should return only on signal 155284440afbSKonstantin Belousov * delivery. 155384440afbSKonstantin Belousov */ 1554afe1a688SKonstantin Belousov (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 155584440afbSKonstantin Belousov for (has_sig = 0; !has_sig;) { 155684440afbSKonstantin Belousov while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 155784440afbSKonstantin Belousov 0) == 0) 15582c42a146SMarcel Moolenaar /* void */; 155984440afbSKonstantin Belousov thread_suspend_check(0); 156084440afbSKonstantin Belousov mtx_lock(&p->p_sigacts->ps_mtx); 156146e47c4fSKonstantin Belousov while ((sig = cursig(td)) != 0) { 156246e47c4fSKonstantin Belousov KASSERT(sig >= 0, ("sig %d", sig)); 156375c586a4SKonstantin Belousov has_sig += postsig(sig); 156446e47c4fSKonstantin Belousov } 156584440afbSKonstantin Belousov mtx_unlock(&p->p_sigacts->ps_mtx); 1566ef401a85SKonstantin Belousov 1567ef401a85SKonstantin Belousov /* 1568ef401a85SKonstantin Belousov * If PTRACE_SCE or PTRACE_SCX were set after 1569ef401a85SKonstantin Belousov * userspace entered the syscall, return spurious 1570ef401a85SKonstantin Belousov * EINTR. 1571ef401a85SKonstantin Belousov */ 1572ef401a85SKonstantin Belousov if ((p->p_ptevents & PTRACE_SYSCALL) != 0) 1573ef401a85SKonstantin Belousov has_sig += 1; 157484440afbSKonstantin Belousov } 1575628d2653SJohn Baldwin PROC_UNLOCK(p); 15762dd9ea6fSKonstantin Belousov td->td_errno = EINTR; 15772dd9ea6fSKonstantin Belousov td->td_pflags |= TDP_NERRNO; 157875c586a4SKonstantin Belousov return (EJUSTRETURN); 15792c42a146SMarcel Moolenaar } 15802c42a146SMarcel Moolenaar 158131c8f3f0SMarcel Moolenaar #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 158297563428SAlexander Kabaev /* 158397563428SAlexander Kabaev * Compatibility sigsuspend call for old binaries. Note nonstandard calling 158497563428SAlexander Kabaev * convention: libc stub passes mask, not pointer, to save a copyin. 158597563428SAlexander Kabaev */ 15862c42a146SMarcel Moolenaar #ifndef _SYS_SYSPROTO_H_ 15872c42a146SMarcel Moolenaar struct osigsuspend_args { 15882c42a146SMarcel Moolenaar osigset_t mask; 15892c42a146SMarcel Moolenaar }; 15902c42a146SMarcel Moolenaar #endif 15912c42a146SMarcel Moolenaar /* ARGSUSED */ 15922c42a146SMarcel Moolenaar int 1593e052a8b9SEd Maste osigsuspend(struct thread *td, struct osigsuspend_args *uap) 15942c42a146SMarcel Moolenaar { 1595645682fdSLuoqi Chen sigset_t mask; 15962c42a146SMarcel Moolenaar 1597645682fdSLuoqi Chen OSIG2SIG(uap->mask, mask); 159884440afbSKonstantin Belousov return (kern_sigsuspend(td, mask)); 1599df8bae1dSRodney W. Grimes } 160031c8f3f0SMarcel Moolenaar #endif /* COMPAT_43 */ 1601df8bae1dSRodney W. Grimes 16021930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1603d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1604df8bae1dSRodney W. Grimes struct osigstack_args { 1605df8bae1dSRodney W. Grimes struct sigstack *nss; 1606df8bae1dSRodney W. Grimes struct sigstack *oss; 1607df8bae1dSRodney W. Grimes }; 1608d2d3e875SBruce Evans #endif 1609df8bae1dSRodney W. Grimes /* ARGSUSED */ 161026f9a767SRodney W. Grimes int 1611e052a8b9SEd Maste osigstack(struct thread *td, struct osigstack_args *uap) 1612df8bae1dSRodney W. Grimes { 16135afe0c99SJohn Baldwin struct sigstack nss, oss; 1614fb99ab88SMatthew Dillon int error = 0; 1615fb99ab88SMatthew Dillon 1616d034d459SMarcel Moolenaar if (uap->nss != NULL) { 16175afe0c99SJohn Baldwin error = copyin(uap->nss, &nss, sizeof(nss)); 16185afe0c99SJohn Baldwin if (error) 16195afe0c99SJohn Baldwin return (error); 1620df8bae1dSRodney W. Grimes } 1621a30ec4b9SDavid Xu oss.ss_sp = td->td_sigstk.ss_sp; 16225afe0c99SJohn Baldwin oss.ss_onstack = sigonstack(cpu_getstack(td)); 16235afe0c99SJohn Baldwin if (uap->nss != NULL) { 1624a30ec4b9SDavid Xu td->td_sigstk.ss_sp = nss.ss_sp; 1625a30ec4b9SDavid Xu td->td_sigstk.ss_size = 0; 1626a30ec4b9SDavid Xu td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1627a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 16285afe0c99SJohn Baldwin } 16295afe0c99SJohn Baldwin if (uap->oss != NULL) 16305afe0c99SJohn Baldwin error = copyout(&oss, uap->oss, sizeof(oss)); 16315afe0c99SJohn Baldwin 1632fb99ab88SMatthew Dillon return (error); 1633df8bae1dSRodney W. Grimes } 16341930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1635df8bae1dSRodney W. Grimes 1636d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1637df8bae1dSRodney W. Grimes struct sigaltstack_args { 16382c42a146SMarcel Moolenaar stack_t *ss; 16392c42a146SMarcel Moolenaar stack_t *oss; 1640df8bae1dSRodney W. Grimes }; 1641d2d3e875SBruce Evans #endif 1642df8bae1dSRodney W. Grimes /* ARGSUSED */ 164326f9a767SRodney W. Grimes int 1644e052a8b9SEd Maste sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1645df8bae1dSRodney W. Grimes { 16468f19eb88SIan Dowse stack_t ss, oss; 16478f19eb88SIan Dowse int error; 16488f19eb88SIan Dowse 16498f19eb88SIan Dowse if (uap->ss != NULL) { 16508f19eb88SIan Dowse error = copyin(uap->ss, &ss, sizeof(ss)); 16518f19eb88SIan Dowse if (error) 16528f19eb88SIan Dowse return (error); 16538f19eb88SIan Dowse } 16548f19eb88SIan Dowse error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 16558f19eb88SIan Dowse (uap->oss != NULL) ? &oss : NULL); 16568f19eb88SIan Dowse if (error) 16578f19eb88SIan Dowse return (error); 16588f19eb88SIan Dowse if (uap->oss != NULL) 16598f19eb88SIan Dowse error = copyout(&oss, uap->oss, sizeof(stack_t)); 16608f19eb88SIan Dowse return (error); 16618f19eb88SIan Dowse } 16628f19eb88SIan Dowse 16638f19eb88SIan Dowse int 16648f19eb88SIan Dowse kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 16658f19eb88SIan Dowse { 1666b40ce416SJulian Elischer struct proc *p = td->td_proc; 1667fb99ab88SMatthew Dillon int oonstack; 1668fb99ab88SMatthew Dillon 1669b40ce416SJulian Elischer oonstack = sigonstack(cpu_getstack(td)); 1670d034d459SMarcel Moolenaar 16718f19eb88SIan Dowse if (oss != NULL) { 1672a30ec4b9SDavid Xu *oss = td->td_sigstk; 1673a30ec4b9SDavid Xu oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1674d034d459SMarcel Moolenaar ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1675df8bae1dSRodney W. Grimes } 1676d034d459SMarcel Moolenaar 16778f19eb88SIan Dowse if (ss != NULL) { 1678a30ec4b9SDavid Xu if (oonstack) 1679cf60731bSJohn Baldwin return (EPERM); 1680a30ec4b9SDavid Xu if ((ss->ss_flags & ~SS_DISABLE) != 0) 1681cf60731bSJohn Baldwin return (EINVAL); 16828f19eb88SIan Dowse if (!(ss->ss_flags & SS_DISABLE)) { 16839104847fSDavid Xu if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1684cf60731bSJohn Baldwin return (ENOMEM); 16859104847fSDavid Xu 1686a30ec4b9SDavid Xu td->td_sigstk = *ss; 1687a30ec4b9SDavid Xu td->td_pflags |= TDP_ALTSTACK; 1688628d2653SJohn Baldwin } else { 1689a30ec4b9SDavid Xu td->td_pflags &= ~TDP_ALTSTACK; 1690628d2653SJohn Baldwin } 1691d034d459SMarcel Moolenaar } 1692cf60731bSJohn Baldwin return (0); 1693df8bae1dSRodney W. Grimes } 1694df8bae1dSRodney W. Grimes 16950cc9fb75SKonstantin Belousov struct killpg1_ctx { 16960cc9fb75SKonstantin Belousov struct thread *td; 16970cc9fb75SKonstantin Belousov ksiginfo_t *ksi; 16980cc9fb75SKonstantin Belousov int sig; 16990cc9fb75SKonstantin Belousov bool sent; 17000cc9fb75SKonstantin Belousov bool found; 17010cc9fb75SKonstantin Belousov int ret; 17020cc9fb75SKonstantin Belousov }; 17030cc9fb75SKonstantin Belousov 17040cc9fb75SKonstantin Belousov static void 17050cc9fb75SKonstantin Belousov killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg) 17060cc9fb75SKonstantin Belousov { 17070cc9fb75SKonstantin Belousov int err; 17080cc9fb75SKonstantin Belousov 17090cc9fb75SKonstantin Belousov if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 17100cc9fb75SKonstantin Belousov (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW) 17110cc9fb75SKonstantin Belousov return; 17120cc9fb75SKonstantin Belousov PROC_LOCK(p); 17130cc9fb75SKonstantin Belousov err = p_cansignal(arg->td, p, arg->sig); 17140cc9fb75SKonstantin Belousov if (err == 0 && arg->sig != 0) 17150cc9fb75SKonstantin Belousov pksignal(p, arg->sig, arg->ksi); 17160cc9fb75SKonstantin Belousov PROC_UNLOCK(p); 17170cc9fb75SKonstantin Belousov if (err != ESRCH) 17180cc9fb75SKonstantin Belousov arg->found = true; 17190cc9fb75SKonstantin Belousov if (err == 0) 17200cc9fb75SKonstantin Belousov arg->sent = true; 17210cc9fb75SKonstantin Belousov else if (arg->ret == 0 && err != ESRCH && err != EPERM) 17220cc9fb75SKonstantin Belousov arg->ret = err; 17230cc9fb75SKonstantin Belousov } 17240cc9fb75SKonstantin Belousov 1725d93f860cSPoul-Henning Kamp /* 1726d93f860cSPoul-Henning Kamp * Common code for kill process group/broadcast kill. 1727d93f860cSPoul-Henning Kamp * cp is calling process. 1728d93f860cSPoul-Henning Kamp */ 172937c84183SPoul-Henning Kamp static int 1730a3de221dSKonstantin Belousov killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1731d93f860cSPoul-Henning Kamp { 1732a3de221dSKonstantin Belousov struct proc *p; 1733d93f860cSPoul-Henning Kamp struct pgrp *pgrp; 17340cc9fb75SKonstantin Belousov struct killpg1_ctx arg; 1735d93f860cSPoul-Henning Kamp 17360cc9fb75SKonstantin Belousov arg.td = td; 17370cc9fb75SKonstantin Belousov arg.ksi = ksi; 17380cc9fb75SKonstantin Belousov arg.sig = sig; 17390cc9fb75SKonstantin Belousov arg.sent = false; 17400cc9fb75SKonstantin Belousov arg.found = false; 17410cc9fb75SKonstantin Belousov arg.ret = 0; 1742553629ebSJake Burkholder if (all) { 1743d93f860cSPoul-Henning Kamp /* 1744d93f860cSPoul-Henning Kamp * broadcast 1745d93f860cSPoul-Henning Kamp */ 17461005a129SJohn Baldwin sx_slock(&allproc_lock); 17474f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 17480cc9fb75SKonstantin Belousov killpg1_sendsig(p, true, &arg); 1749d93f860cSPoul-Henning Kamp } 17501005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1751553629ebSJake Burkholder } else { 1752ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1753f591779bSSeigo Tanimura if (pgid == 0) { 1754d93f860cSPoul-Henning Kamp /* 1755d93f860cSPoul-Henning Kamp * zero pgid means send to my process group. 1756d93f860cSPoul-Henning Kamp */ 17579c1ab3e0SJohn Baldwin pgrp = td->td_proc->p_pgrp; 1758f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 1759f591779bSSeigo Tanimura } else { 1760d93f860cSPoul-Henning Kamp pgrp = pgfind(pgid); 1761f591779bSSeigo Tanimura if (pgrp == NULL) { 1762ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1763d93f860cSPoul-Henning Kamp return (ESRCH); 1764d93f860cSPoul-Henning Kamp } 1765f591779bSSeigo Tanimura } 1766ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 17672e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 17680cc9fb75SKonstantin Belousov killpg1_sendsig(p, false, &arg); 1769d93f860cSPoul-Henning Kamp } 1770f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1771d93f860cSPoul-Henning Kamp } 17720cc9fb75SKonstantin Belousov MPASS(arg.ret != 0 || arg.found || !arg.sent); 17730cc9fb75SKonstantin Belousov if (arg.ret == 0 && !arg.sent) 17740cc9fb75SKonstantin Belousov arg.ret = arg.found ? EPERM : ESRCH; 17750cc9fb75SKonstantin Belousov return (arg.ret); 1776d93f860cSPoul-Henning Kamp } 1777d93f860cSPoul-Henning Kamp 1778d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1779df8bae1dSRodney W. Grimes struct kill_args { 1780df8bae1dSRodney W. Grimes int pid; 1781df8bae1dSRodney W. Grimes int signum; 1782df8bae1dSRodney W. Grimes }; 1783d2d3e875SBruce Evans #endif 1784df8bae1dSRodney W. Grimes /* ARGSUSED */ 178526f9a767SRodney W. Grimes int 17868451d0ddSKip Macy sys_kill(struct thread *td, struct kill_args *uap) 1787df8bae1dSRodney W. Grimes { 178834ad5ac2SEdward Tomasz Napierala 178934ad5ac2SEdward Tomasz Napierala return (kern_kill(td, uap->pid, uap->signum)); 179034ad5ac2SEdward Tomasz Napierala } 179134ad5ac2SEdward Tomasz Napierala 179234ad5ac2SEdward Tomasz Napierala int 179334ad5ac2SEdward Tomasz Napierala kern_kill(struct thread *td, pid_t pid, int signum) 179434ad5ac2SEdward Tomasz Napierala { 1795a3de221dSKonstantin Belousov ksiginfo_t ksi; 1796a3de221dSKonstantin Belousov struct proc *p; 179790af4afaSJohn Baldwin int error; 1798df8bae1dSRodney W. Grimes 17998890f5d0SPawel Jakub Dawidek /* 18008890f5d0SPawel Jakub Dawidek * A process in capability mode can send signals only to himself. 18018890f5d0SPawel Jakub Dawidek * The main rationale behind this is that abort(3) is implemented as 18028890f5d0SPawel Jakub Dawidek * kill(getpid(), SIGABRT). 18038890f5d0SPawel Jakub Dawidek */ 180434ad5ac2SEdward Tomasz Napierala if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid) 18058890f5d0SPawel Jakub Dawidek return (ECAPMODE); 18068890f5d0SPawel Jakub Dawidek 180734ad5ac2SEdward Tomasz Napierala AUDIT_ARG_SIGNUM(signum); 180834ad5ac2SEdward Tomasz Napierala AUDIT_ARG_PID(pid); 180934ad5ac2SEdward Tomasz Napierala if ((u_int)signum > _SIG_MAXSIG) 1810df8bae1dSRodney W. Grimes return (EINVAL); 1811fb99ab88SMatthew Dillon 1812a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 181334ad5ac2SEdward Tomasz Napierala ksi.ksi_signo = signum; 1814a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1815a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1816a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1817a3de221dSKonstantin Belousov 181834ad5ac2SEdward Tomasz Napierala if (pid > 0) { 1819df8bae1dSRodney W. Grimes /* kill single process */ 182034ad5ac2SEdward Tomasz Napierala if ((p = pfind_any(pid)) == NULL) 182190af4afaSJohn Baldwin return (ESRCH); 182214961ba7SRobert Watson AUDIT_ARG_PROCESS(p); 182334ad5ac2SEdward Tomasz Napierala error = p_cansignal(td, p, signum); 182434ad5ac2SEdward Tomasz Napierala if (error == 0 && signum) 182534ad5ac2SEdward Tomasz Napierala pksignal(p, signum, &ksi); 1826628d2653SJohn Baldwin PROC_UNLOCK(p); 182790af4afaSJohn Baldwin return (error); 1828df8bae1dSRodney W. Grimes } 182934ad5ac2SEdward Tomasz Napierala switch (pid) { 1830df8bae1dSRodney W. Grimes case -1: /* broadcast signal */ 183134ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 1, &ksi)); 1832df8bae1dSRodney W. Grimes case 0: /* signal own process group */ 183334ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, 0, 0, &ksi)); 1834df8bae1dSRodney W. Grimes default: /* negative explicit process group */ 183534ad5ac2SEdward Tomasz Napierala return (killpg1(td, signum, -pid, 0, &ksi)); 1836df8bae1dSRodney W. Grimes } 183790af4afaSJohn Baldwin /* NOTREACHED */ 1838df8bae1dSRodney W. Grimes } 1839df8bae1dSRodney W. Grimes 1840cfb5f768SJonathan Anderson int 1841e052a8b9SEd Maste sys_pdkill(struct thread *td, struct pdkill_args *uap) 1842cfb5f768SJonathan Anderson { 1843cfb5f768SJonathan Anderson struct proc *p; 1844cfb5f768SJonathan Anderson int error; 1845cfb5f768SJonathan Anderson 1846cfb5f768SJonathan Anderson AUDIT_ARG_SIGNUM(uap->signum); 1847cfb5f768SJonathan Anderson AUDIT_ARG_FD(uap->fd); 1848cfb5f768SJonathan Anderson if ((u_int)uap->signum > _SIG_MAXSIG) 1849cfb5f768SJonathan Anderson return (EINVAL); 1850cfb5f768SJonathan Anderson 1851cbd92ce6SMatt Macy error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p); 1852cfb5f768SJonathan Anderson if (error) 1853cfb5f768SJonathan Anderson return (error); 1854cfb5f768SJonathan Anderson AUDIT_ARG_PROCESS(p); 1855cfb5f768SJonathan Anderson error = p_cansignal(td, p, uap->signum); 1856cfb5f768SJonathan Anderson if (error == 0 && uap->signum) 18578451d0ddSKip Macy kern_psignal(p, uap->signum); 1858cfb5f768SJonathan Anderson PROC_UNLOCK(p); 1859cfb5f768SJonathan Anderson return (error); 1860cfb5f768SJonathan Anderson } 1861cfb5f768SJonathan Anderson 18621930e303SPoul-Henning Kamp #if defined(COMPAT_43) 1863d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 1864df8bae1dSRodney W. Grimes struct okillpg_args { 1865df8bae1dSRodney W. Grimes int pgid; 1866df8bae1dSRodney W. Grimes int signum; 1867df8bae1dSRodney W. Grimes }; 1868d2d3e875SBruce Evans #endif 1869df8bae1dSRodney W. Grimes /* ARGSUSED */ 187026f9a767SRodney W. Grimes int 1871a3de221dSKonstantin Belousov okillpg(struct thread *td, struct okillpg_args *uap) 1872df8bae1dSRodney W. Grimes { 1873a3de221dSKonstantin Belousov ksiginfo_t ksi; 1874df8bae1dSRodney W. Grimes 187514961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->signum); 187614961ba7SRobert Watson AUDIT_ARG_PID(uap->pgid); 18776c1534a7SPeter Wemm if ((u_int)uap->signum > _SIG_MAXSIG) 1878df8bae1dSRodney W. Grimes return (EINVAL); 18799104847fSDavid Xu 1880a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 1881a3de221dSKonstantin Belousov ksi.ksi_signo = uap->signum; 1882a3de221dSKonstantin Belousov ksi.ksi_code = SI_USER; 1883a3de221dSKonstantin Belousov ksi.ksi_pid = td->td_proc->p_pid; 1884a3de221dSKonstantin Belousov ksi.ksi_uid = td->td_ucred->cr_ruid; 1885a3de221dSKonstantin Belousov return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1886df8bae1dSRodney W. Grimes } 18871930e303SPoul-Henning Kamp #endif /* COMPAT_43 */ 1888df8bae1dSRodney W. Grimes 18899104847fSDavid Xu #ifndef _SYS_SYSPROTO_H_ 18909104847fSDavid Xu struct sigqueue_args { 18919104847fSDavid Xu pid_t pid; 18929104847fSDavid Xu int signum; 18939104847fSDavid Xu /* union sigval */ void *value; 18949104847fSDavid Xu }; 18959104847fSDavid Xu #endif 18969104847fSDavid Xu int 18978451d0ddSKip Macy sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 18989104847fSDavid Xu { 1899f19351aaSBrooks Davis union sigval sv; 1900f19351aaSBrooks Davis 1901f19351aaSBrooks Davis sv.sival_ptr = uap->value; 1902f19351aaSBrooks Davis 1903f19351aaSBrooks Davis return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 1904f19351aaSBrooks Davis } 1905f19351aaSBrooks Davis 1906f19351aaSBrooks Davis int 1907f19351aaSBrooks Davis kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value) 1908f19351aaSBrooks Davis { 19099104847fSDavid Xu ksiginfo_t ksi; 19109104847fSDavid Xu struct proc *p; 19119104847fSDavid Xu int error; 19129104847fSDavid Xu 1913f19351aaSBrooks Davis if ((u_int)signum > _SIG_MAXSIG) 19149104847fSDavid Xu return (EINVAL); 19159104847fSDavid Xu 19169104847fSDavid Xu /* 19179104847fSDavid Xu * Specification says sigqueue can only send signal to 19189104847fSDavid Xu * single process. 19199104847fSDavid Xu */ 1920f19351aaSBrooks Davis if (pid <= 0) 19219104847fSDavid Xu return (EINVAL); 19229104847fSDavid Xu 1923537d0fb1SMateusz Guzik if ((p = pfind_any(pid)) == NULL) 19249104847fSDavid Xu return (ESRCH); 1925f19351aaSBrooks Davis error = p_cansignal(td, p, signum); 1926f19351aaSBrooks Davis if (error == 0 && signum != 0) { 19279104847fSDavid Xu ksiginfo_init(&ksi); 1928a3de221dSKonstantin Belousov ksi.ksi_flags = KSI_SIGQ; 1929f19351aaSBrooks Davis ksi.ksi_signo = signum; 19309104847fSDavid Xu ksi.ksi_code = SI_QUEUE; 19319104847fSDavid Xu ksi.ksi_pid = td->td_proc->p_pid; 19329104847fSDavid Xu ksi.ksi_uid = td->td_ucred->cr_ruid; 1933f19351aaSBrooks Davis ksi.ksi_value = *value; 1934ad6eec7bSJohn Baldwin error = pksignal(p, ksi.ksi_signo, &ksi); 19359104847fSDavid Xu } 19369104847fSDavid Xu PROC_UNLOCK(p); 19379104847fSDavid Xu return (error); 19389104847fSDavid Xu } 19399104847fSDavid Xu 1940df8bae1dSRodney W. Grimes /* 1941df8bae1dSRodney W. Grimes * Send a signal to a process group. 1942df8bae1dSRodney W. Grimes */ 1943df8bae1dSRodney W. Grimes void 1944a3de221dSKonstantin Belousov gsignal(int pgid, int sig, ksiginfo_t *ksi) 1945df8bae1dSRodney W. Grimes { 1946df8bae1dSRodney W. Grimes struct pgrp *pgrp; 1947df8bae1dSRodney W. Grimes 1948f591779bSSeigo Tanimura if (pgid != 0) { 1949ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 1950f591779bSSeigo Tanimura pgrp = pgfind(pgid); 1951ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1952f591779bSSeigo Tanimura if (pgrp != NULL) { 1953a3de221dSKonstantin Belousov pgsignal(pgrp, sig, 0, ksi); 1954f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 1955f591779bSSeigo Tanimura } 1956f591779bSSeigo Tanimura } 1957df8bae1dSRodney W. Grimes } 1958df8bae1dSRodney W. Grimes 1959df8bae1dSRodney W. Grimes /* 1960df8bae1dSRodney W. Grimes * Send a signal to a process group. If checktty is 1, 1961df8bae1dSRodney W. Grimes * limit to members which have a controlling terminal. 1962df8bae1dSRodney W. Grimes */ 1963df8bae1dSRodney W. Grimes void 1964a3de221dSKonstantin Belousov pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1965df8bae1dSRodney W. Grimes { 1966a3de221dSKonstantin Belousov struct proc *p; 1967df8bae1dSRodney W. Grimes 1968628d2653SJohn Baldwin if (pgrp) { 1969f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1970628d2653SJohn Baldwin LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1971628d2653SJohn Baldwin PROC_LOCK(p); 1972e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 1973e806d352SJohn Baldwin (checkctty == 0 || p->p_flag & P_CONTROLT)) 1974a3de221dSKonstantin Belousov pksignal(p, sig, ksi); 1975628d2653SJohn Baldwin PROC_UNLOCK(p); 1976628d2653SJohn Baldwin } 1977628d2653SJohn Baldwin } 1978df8bae1dSRodney W. Grimes } 1979df8bae1dSRodney W. Grimes 1980e442f29fSKonstantin Belousov /* 1981e442f29fSKonstantin Belousov * Recalculate the signal mask and reset the signal disposition after 1982e442f29fSKonstantin Belousov * usermode frame for delivery is formed. Should be called after 1983e442f29fSKonstantin Belousov * mach-specific routine, because sysent->sv_sendsig() needs correct 1984e442f29fSKonstantin Belousov * ps_siginfo and signal mask. 1985e442f29fSKonstantin Belousov */ 1986e442f29fSKonstantin Belousov static void 1987e442f29fSKonstantin Belousov postsig_done(int sig, struct thread *td, struct sigacts *ps) 1988e442f29fSKonstantin Belousov { 1989e442f29fSKonstantin Belousov sigset_t mask; 1990e442f29fSKonstantin Belousov 1991e442f29fSKonstantin Belousov mtx_assert(&ps->ps_mtx, MA_OWNED); 1992e442f29fSKonstantin Belousov td->td_ru.ru_nsignals++; 1993e442f29fSKonstantin Belousov mask = ps->ps_catchmask[_SIG_IDX(sig)]; 1994e442f29fSKonstantin Belousov if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1995e442f29fSKonstantin Belousov SIGADDSET(mask, sig); 1996e442f29fSKonstantin Belousov kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 1997e442f29fSKonstantin Belousov SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 1998e442f29fSKonstantin Belousov if (SIGISMEMBER(ps->ps_sigreset, sig)) 1999e442f29fSKonstantin Belousov sigdflt(ps, sig); 2000e442f29fSKonstantin Belousov } 2001e442f29fSKonstantin Belousov 2002df8bae1dSRodney W. Grimes /* 20030c14ff0eSRobert Watson * Send a signal caused by a trap to the current thread. If it will be 20040c14ff0eSRobert Watson * caught immediately, deliver it with correct code. Otherwise, post it 20050c14ff0eSRobert Watson * normally. 2006df8bae1dSRodney W. Grimes */ 2007df8bae1dSRodney W. Grimes void 20089104847fSDavid Xu trapsignal(struct thread *td, ksiginfo_t *ksi) 2009df8bae1dSRodney W. Grimes { 20101bf4700bSJeff Roberson struct sigacts *ps; 20111bf4700bSJeff Roberson struct proc *p; 2012146fc63fSKonstantin Belousov sigset_t sigmask; 2013c5c981d4SMateusz Guzik int sig; 20141bf4700bSJeff Roberson 20151bf4700bSJeff Roberson p = td->td_proc; 20169104847fSDavid Xu sig = ksi->ksi_signo; 20179104847fSDavid Xu KASSERT(_SIG_VALID(sig), ("invalid signal")); 20189104847fSDavid Xu 2019a113b17fSKonstantin Belousov sigfastblock_fetch(td); 2020628d2653SJohn Baldwin PROC_LOCK(p); 2021ef3dab76STim J. Robbins ps = p->p_sigacts; 202290af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 2023146fc63fSKonstantin Belousov sigmask = td->td_sigmask; 2024146fc63fSKonstantin Belousov if (td->td_sigblock_val != 0) 2025146fc63fSKonstantin Belousov SIGSETOR(sigmask, fastblock_mask); 202690af4afaSJohn Baldwin if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 2027146fc63fSKonstantin Belousov !SIGISMEMBER(sigmask, sig)) { 2028df8bae1dSRodney W. Grimes #ifdef KTRACE 2029374a15aaSJohn Baldwin if (KTRPOINT(curthread, KTR_PSIG)) 2030374a15aaSJohn Baldwin ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 2031c5c981d4SMateusz Guzik &td->td_sigmask, ksi->ksi_code); 2032df8bae1dSRodney W. Grimes #endif 20339104847fSDavid Xu (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 20349104847fSDavid Xu ksi, &td->td_sigmask); 2035e442f29fSKonstantin Belousov postsig_done(sig, td, ps); 203690af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20376f841fb7SMarcel Moolenaar } else { 2038f71a882fSDavid Xu /* 2039f71a882fSDavid Xu * Avoid a possible infinite loop if the thread 2040f71a882fSDavid Xu * masking the signal or process is ignoring the 2041f71a882fSDavid Xu * signal. 2042f71a882fSDavid Xu */ 2043146fc63fSKonstantin Belousov if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) || 2044f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 2045f71a882fSDavid Xu SIGDELSET(td->td_sigmask, sig); 2046f71a882fSDavid Xu SIGDELSET(ps->ps_sigcatch, sig); 2047f71a882fSDavid Xu SIGDELSET(ps->ps_sigignore, sig); 2048f71a882fSDavid Xu ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2049146fc63fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTBLOCK; 2050146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 2051f71a882fSDavid Xu } 205290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 20532c42a146SMarcel Moolenaar p->p_sig = sig; /* XXX to verify code */ 2054ad6eec7bSJohn Baldwin tdsendsignal(p, td, sig, ksi); 2055df8bae1dSRodney W. Grimes } 2056628d2653SJohn Baldwin PROC_UNLOCK(p); 2057df8bae1dSRodney W. Grimes } 2058df8bae1dSRodney W. Grimes 20594093529dSJeff Roberson static struct thread * 2060146fc63fSKonstantin Belousov sigtd(struct proc *p, int sig, bool fast_sigblock) 20614093529dSJeff Roberson { 20623074d1b4SDavid Xu struct thread *td, *signal_td; 20634093529dSJeff Roberson 20644093529dSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2065146fc63fSKonstantin Belousov MPASS(!fast_sigblock || p == curproc); 20664093529dSJeff Roberson 20674093529dSJeff Roberson /* 2068627451c1SDavid Xu * Check if current thread can handle the signal without 206915b7a831SKonstantin Belousov * switching context to another thread. 20704093529dSJeff Roberson */ 2071146fc63fSKonstantin Belousov if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) && 2072146fc63fSKonstantin Belousov (!fast_sigblock || curthread->td_sigblock_val == 0)) 2073627451c1SDavid Xu return (curthread); 20743074d1b4SDavid Xu signal_td = NULL; 20753074d1b4SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 2076146fc63fSKonstantin Belousov if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock || 2077146fc63fSKonstantin Belousov td != curthread || td->td_sigblock_val == 0)) { 20783074d1b4SDavid Xu signal_td = td; 2079627451c1SDavid Xu break; 20803074d1b4SDavid Xu } 20813074d1b4SDavid Xu } 20823074d1b4SDavid Xu if (signal_td == NULL) 20833074d1b4SDavid Xu signal_td = FIRST_THREAD_IN_PROC(p); 20843074d1b4SDavid Xu return (signal_td); 20854093529dSJeff Roberson } 20864093529dSJeff Roberson 2087df8bae1dSRodney W. Grimes /* 2088df8bae1dSRodney W. Grimes * Send the signal to the process. If the signal has an action, the action 2089df8bae1dSRodney W. Grimes * is usually performed by the target process rather than the caller; we add 2090df8bae1dSRodney W. Grimes * the signal to the set of pending signals for the process. 2091df8bae1dSRodney W. Grimes * 2092df8bae1dSRodney W. Grimes * Exceptions: 2093df8bae1dSRodney W. Grimes * o When a stop signal is sent to a sleeping process that takes the 2094df8bae1dSRodney W. Grimes * default action, the process is stopped without awakening it. 2095df8bae1dSRodney W. Grimes * o SIGCONT restarts stopped processes (or puts them back to sleep) 2096df8bae1dSRodney W. Grimes * regardless of the signal action (eg, blocked or ignored). 2097df8bae1dSRodney W. Grimes * 2098df8bae1dSRodney W. Grimes * Other ignored signals are discarded immediately. 20994dec0e67SRobert Watson * 21004dec0e67SRobert Watson * NB: This function may be entered from the debugger via the "kill" DDB 21014dec0e67SRobert Watson * command. There is little that can be done to mitigate the possibly messy 21024dec0e67SRobert Watson * side effects of this unwise possibility. 2103df8bae1dSRodney W. Grimes */ 2104df8bae1dSRodney W. Grimes void 21058451d0ddSKip Macy kern_psignal(struct proc *p, int sig) 2106df8bae1dSRodney W. Grimes { 2107a3de221dSKonstantin Belousov ksiginfo_t ksi; 2108a3de221dSKonstantin Belousov 2109a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 2110a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 2111a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 2112ad6eec7bSJohn Baldwin (void) tdsendsignal(p, NULL, sig, &ksi); 2113a3de221dSKonstantin Belousov } 2114a3de221dSKonstantin Belousov 2115ad6eec7bSJohn Baldwin int 2116a3de221dSKonstantin Belousov pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2117a3de221dSKonstantin Belousov { 2118a3de221dSKonstantin Belousov 2119ad6eec7bSJohn Baldwin return (tdsendsignal(p, NULL, sig, ksi)); 21209104847fSDavid Xu } 21219104847fSDavid Xu 2122cf7d9a8cSDavid Xu /* Utility function for finding a thread to send signal event to. */ 21239104847fSDavid Xu int 2124cf7d9a8cSDavid Xu sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd) 21259104847fSDavid Xu { 2126cf7d9a8cSDavid Xu struct thread *td; 212741b3077aSJacques Vidrine 21286d7b314bSDavid Xu if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2129cf7d9a8cSDavid Xu td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 21306d7b314bSDavid Xu if (td == NULL) 21316d7b314bSDavid Xu return (ESRCH); 2132cf7d9a8cSDavid Xu *ttd = td; 2133cf7d9a8cSDavid Xu } else { 2134cf7d9a8cSDavid Xu *ttd = NULL; 2135cf7d9a8cSDavid Xu PROC_LOCK(p); 21366d7b314bSDavid Xu } 2137cf7d9a8cSDavid Xu return (0); 21384093529dSJeff Roberson } 21394093529dSJeff Roberson 2140ad6eec7bSJohn Baldwin void 2141ad6eec7bSJohn Baldwin tdsignal(struct thread *td, int sig) 2142ad6eec7bSJohn Baldwin { 2143ad6eec7bSJohn Baldwin ksiginfo_t ksi; 2144ad6eec7bSJohn Baldwin 2145ad6eec7bSJohn Baldwin ksiginfo_init(&ksi); 2146ad6eec7bSJohn Baldwin ksi.ksi_signo = sig; 2147ad6eec7bSJohn Baldwin ksi.ksi_code = SI_KERNEL; 2148ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2149ad6eec7bSJohn Baldwin } 2150ad6eec7bSJohn Baldwin 2151ad6eec7bSJohn Baldwin void 2152ad6eec7bSJohn Baldwin tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2153ad6eec7bSJohn Baldwin { 2154ad6eec7bSJohn Baldwin 2155ad6eec7bSJohn Baldwin (void) tdsendsignal(td->td_proc, td, sig, ksi); 2156ad6eec7bSJohn Baldwin } 2157ad6eec7bSJohn Baldwin 21589b86d3e5SKonstantin Belousov static int 21599b86d3e5SKonstantin Belousov sig_sleepq_abort(struct thread *td, int intrval) 21609b86d3e5SKonstantin Belousov { 21619b86d3e5SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_OWNED); 21629b86d3e5SKonstantin Belousov 21639b86d3e5SKonstantin Belousov if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) { 21649b86d3e5SKonstantin Belousov thread_unlock(td); 21659b86d3e5SKonstantin Belousov return (0); 21669b86d3e5SKonstantin Belousov } 21679b86d3e5SKonstantin Belousov return (sleepq_abort(td, intrval)); 21689b86d3e5SKonstantin Belousov } 21699b86d3e5SKonstantin Belousov 2170cf7d9a8cSDavid Xu int 2171ad6eec7bSJohn Baldwin tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 21724093529dSJeff Roberson { 21739104847fSDavid Xu sig_t action; 21749104847fSDavid Xu sigqueue_t *sigqueue; 21759104847fSDavid Xu int prop; 217690af4afaSJohn Baldwin struct sigacts *ps; 217794f0972bSDavid Xu int intrval; 21789104847fSDavid Xu int ret = 0; 2179da7bbd2cSJohn Baldwin int wakeup_swapper; 2180df8bae1dSRodney W. Grimes 2181cf7d9a8cSDavid Xu MPASS(td == NULL || p == td->td_proc); 21826d7b314bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 21836d7b314bSDavid Xu 218441b3077aSJacques Vidrine if (!_SIG_VALID(sig)) 2185212bc4b3SDavid Xu panic("%s(): invalid signal %d", __func__, sig); 21864093529dSJeff Roberson 2187212bc4b3SDavid Xu KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 21886d7b314bSDavid Xu 21896d7b314bSDavid Xu /* 21906d7b314bSDavid Xu * IEEE Std 1003.1-2001: return success when killing a zombie. 21916d7b314bSDavid Xu */ 21926d7b314bSDavid Xu if (p->p_state == PRS_ZOMBIE) { 21936d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 21946d7b314bSDavid Xu ksiginfo_tryfree(ksi); 21956d7b314bSDavid Xu return (ret); 21966d7b314bSDavid Xu } 21976d7b314bSDavid Xu 219890af4afaSJohn Baldwin ps = p->p_sigacts; 21999e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 22002c42a146SMarcel Moolenaar prop = sigprop(sig); 22014093529dSJeff Roberson 22026d7b314bSDavid Xu if (td == NULL) { 2203146fc63fSKonstantin Belousov td = sigtd(p, sig, false); 22046d7b314bSDavid Xu sigqueue = &p->p_sigqueue; 2205462314b3SMateusz Guzik } else 22069104847fSDavid Xu sigqueue = &td->td_sigqueue; 22074093529dSJeff Roberson 220836160958SMark Johnston SDT_PROBE3(proc, , , signal__send, td, p, sig); 22095d217f17SJohn Birrell 2210df8bae1dSRodney W. Grimes /* 2211bc387624SKonstantin Belousov * If the signal is being ignored, then we forget about it 2212bc387624SKonstantin Belousov * immediately, except when the target process executes 2213bc387624SKonstantin Belousov * sigwait(). (Note: we don't set SIGCONT in ps_sigignore, 2214bc387624SKonstantin Belousov * and if it is set to SIG_IGN, action will be SIG_DFL here.) 2215df8bae1dSRodney W. Grimes */ 221690af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 22170fc32899SJohn Baldwin if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2218bc387624SKonstantin Belousov if (kern_sig_discard_ign && 2219bc387624SKonstantin Belousov (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) { 222036160958SMark Johnston SDT_PROBE3(proc, , , signal__discard, td, p, sig); 22215d217f17SJohn Birrell 222290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 22236d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 22246d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22259104847fSDavid Xu return (ret); 2226bc387624SKonstantin Belousov } else { 2227bc387624SKonstantin Belousov action = SIG_CATCH; 2228f17eb93dSKonstantin Belousov intrval = 0; 222990af4afaSJohn Baldwin } 2230f17eb93dSKonstantin Belousov } else { 2231f17eb93dSKonstantin Belousov if (SIGISMEMBER(td->td_sigmask, sig)) 2232df8bae1dSRodney W. Grimes action = SIG_HOLD; 223390af4afaSJohn Baldwin else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2234df8bae1dSRodney W. Grimes action = SIG_CATCH; 2235df8bae1dSRodney W. Grimes else 2236df8bae1dSRodney W. Grimes action = SIG_DFL; 223794f0972bSDavid Xu if (SIGISMEMBER(ps->ps_sigintr, sig)) 223894f0972bSDavid Xu intrval = EINTR; 223994f0972bSDavid Xu else 224094f0972bSDavid Xu intrval = ERESTART; 2241f17eb93dSKonstantin Belousov } 224290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 2243df8bae1dSRodney W. Grimes 2244fd50a707SBrooks Davis if (prop & SIGPROP_CONT) 22459104847fSDavid Xu sigqueue_delete_stopmask_proc(p); 2246fd50a707SBrooks Davis else if (prop & SIGPROP_STOP) { 2247df8bae1dSRodney W. Grimes /* 2248df8bae1dSRodney W. Grimes * If sending a tty stop signal to a member of an orphaned 2249df8bae1dSRodney W. Grimes * process group, discard the signal here if the action 2250df8bae1dSRodney W. Grimes * is default; don't stop the process below if sleeping, 2251df8bae1dSRodney W. Grimes * and don't clear any pending SIGCONT. 2252df8bae1dSRodney W. Grimes */ 2253993a1699SKonstantin Belousov if ((prop & SIGPROP_TTYSTOP) != 0 && 22545844bd05SKonstantin Belousov (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 && 2255993a1699SKonstantin Belousov action == SIG_DFL) { 22566d7b314bSDavid Xu if (ksi && (ksi->ksi_flags & KSI_INS)) 22576d7b314bSDavid Xu ksiginfo_tryfree(ksi); 22589104847fSDavid Xu return (ret); 22596d7b314bSDavid Xu } 22609104847fSDavid Xu sigqueue_delete_proc(p, SIGCONT); 2261ebceaf6dSDavid Xu if (p->p_flag & P_CONTINUED) { 22626933e3c1SJulian Elischer p->p_flag &= ~P_CONTINUED; 2263ebceaf6dSDavid Xu PROC_LOCK(p->p_pptr); 2264ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 2265ebceaf6dSDavid Xu PROC_UNLOCK(p->p_pptr); 2266ebceaf6dSDavid Xu } 2267df8bae1dSRodney W. Grimes } 22683074d1b4SDavid Xu 22699104847fSDavid Xu ret = sigqueue_add(sigqueue, sig, ksi); 22709104847fSDavid Xu if (ret != 0) 22719104847fSDavid Xu return (ret); 22726d7b314bSDavid Xu signotify(td); 22735312b1c7SDavid Xu /* 22745312b1c7SDavid Xu * Defer further processing for signals which are held, 22755312b1c7SDavid Xu * except that stopped processes must be continued by SIGCONT. 22765312b1c7SDavid Xu */ 22775312b1c7SDavid Xu if (action == SIG_HOLD && 2278fd50a707SBrooks Davis !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 22799104847fSDavid Xu return (ret); 2280b4d33259SEric Badger 228161a74c5cSJeff Roberson wakeup_swapper = 0; 228261a74c5cSJeff Roberson 228390d75f78SAlfred Perlstein /* 2284e602ba25SJulian Elischer * Some signals have a process-wide effect and a per-thread 2285e602ba25SJulian Elischer * component. Most processing occurs when the process next 2286e602ba25SJulian Elischer * tries to cross the user boundary, however there are some 22871968f37bSJohn Baldwin * times when processing needs to be done immediately, such as 2288e602ba25SJulian Elischer * waking up threads so that they can cross the user boundary. 22891968f37bSJohn Baldwin * We try to do the per-process part here. 2290df8bae1dSRodney W. Grimes */ 2291e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 22920f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), 22930f14f15bSJohn Baldwin ("signal to stopped but exiting process")); 2294e602ba25SJulian Elischer if (sig == SIGKILL) { 2295137cf33dSDavid Xu /* 2296137cf33dSDavid Xu * If traced process is already stopped, 2297137cf33dSDavid Xu * then no further action is necessary. 2298137cf33dSDavid Xu */ 229983b718ebSDavid Xu if (p->p_flag & P_TRACED) 230083b718ebSDavid Xu goto out; 2301df8bae1dSRodney W. Grimes /* 2302e602ba25SJulian Elischer * SIGKILL sets process running. 2303e602ba25SJulian Elischer * It will die elsewhere. 2304e602ba25SJulian Elischer * All threads must be restarted. 2305df8bae1dSRodney W. Grimes */ 2306482d099cSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 2307e602ba25SJulian Elischer goto runfast; 2308e602ba25SJulian Elischer } 2309e602ba25SJulian Elischer 2310fd50a707SBrooks Davis if (prop & SIGPROP_CONT) { 2311137cf33dSDavid Xu /* 2312137cf33dSDavid Xu * If traced process is already stopped, 2313137cf33dSDavid Xu * then no further action is necessary. 2314137cf33dSDavid Xu */ 231583b718ebSDavid Xu if (p->p_flag & P_TRACED) 231683b718ebSDavid Xu goto out; 2317e602ba25SJulian Elischer /* 2318e602ba25SJulian Elischer * If SIGCONT is default (or ignored), we continue the 23199104847fSDavid Xu * process but don't leave the signal in sigqueue as 23201d9c5696SJuli Mallett * it has no further action. If SIGCONT is held, we 2321e602ba25SJulian Elischer * continue the process and leave the signal in 23229104847fSDavid Xu * sigqueue. If the process catches SIGCONT, let it 2323e602ba25SJulian Elischer * handle the signal itself. If it isn't waiting on 2324e602ba25SJulian Elischer * an event, it goes back to run state. 2325e602ba25SJulian Elischer * Otherwise, process goes back to sleep state. 2326e602ba25SJulian Elischer */ 23271279572aSDavid Xu p->p_flag &= ~P_STOPPED_SIG; 23287b4a950aSDavid Xu PROC_SLOCK(p); 2329ebceaf6dSDavid Xu if (p->p_numthreads == p->p_suspcount) { 23307b4a950aSDavid Xu PROC_SUNLOCK(p); 23316933e3c1SJulian Elischer p->p_flag |= P_CONTINUED; 2332b4490c6eSKonstantin Belousov p->p_xsig = SIGCONT; 23337f96995eSDavid Xu PROC_LOCK(p->p_pptr); 2334ebceaf6dSDavid Xu childproc_continued(p); 23357f96995eSDavid Xu PROC_UNLOCK(p->p_pptr); 23367b4a950aSDavid Xu PROC_SLOCK(p); 2337ebceaf6dSDavid Xu } 2338e602ba25SJulian Elischer if (action == SIG_DFL) { 2339a54e85fdSJeff Roberson thread_unsuspend(p); 23407b4a950aSDavid Xu PROC_SUNLOCK(p); 23419104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2342dc47fdf1SKonstantin Belousov goto out_cont; 2343a54e85fdSJeff Roberson } 2344a54e85fdSJeff Roberson if (action == SIG_CATCH) { 23458460a577SJohn Birrell /* 23468460a577SJohn Birrell * The process wants to catch it so it needs 23478460a577SJohn Birrell * to run at least one thread, but which one? 23488460a577SJohn Birrell */ 23497b4a950aSDavid Xu PROC_SUNLOCK(p); 2350e602ba25SJulian Elischer goto runfast; 2351e602ba25SJulian Elischer } 2352e602ba25SJulian Elischer /* 2353e602ba25SJulian Elischer * The signal is not ignored or caught. 2354e602ba25SJulian Elischer */ 235504774f23SJulian Elischer thread_unsuspend(p); 23567b4a950aSDavid Xu PROC_SUNLOCK(p); 2357dc47fdf1SKonstantin Belousov goto out_cont; 2358e602ba25SJulian Elischer } 2359e602ba25SJulian Elischer 2360fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 2361137cf33dSDavid Xu /* 2362137cf33dSDavid Xu * If traced process is already stopped, 2363137cf33dSDavid Xu * then no further action is necessary. 2364137cf33dSDavid Xu */ 236583b718ebSDavid Xu if (p->p_flag & P_TRACED) 236683b718ebSDavid Xu goto out; 2367e602ba25SJulian Elischer /* 2368e602ba25SJulian Elischer * Already stopped, don't need to stop again 2369e602ba25SJulian Elischer * (If we did the shell could get confused). 237004774f23SJulian Elischer * Just make sure the signal STOP bit set. 2371e602ba25SJulian Elischer */ 23721279572aSDavid Xu p->p_flag |= P_STOPPED_SIG; 23739104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2374e602ba25SJulian Elischer goto out; 2375e602ba25SJulian Elischer } 2376e602ba25SJulian Elischer 2377e602ba25SJulian Elischer /* 2378e602ba25SJulian Elischer * All other kinds of signals: 2379e602ba25SJulian Elischer * If a thread is sleeping interruptibly, simulate a 2380e602ba25SJulian Elischer * wakeup so that when it is continued it will be made 2381e602ba25SJulian Elischer * runnable and can look at the signal. However, don't make 238204774f23SJulian Elischer * the PROCESS runnable, leave it stopped. 2383e602ba25SJulian Elischer * It may run a bit until it hits a thread_suspend_check(). 2384e602ba25SJulian Elischer */ 23857b4a950aSDavid Xu PROC_SLOCK(p); 2386a54e85fdSJeff Roberson thread_lock(td); 238761a74c5cSJeff Roberson if (TD_CAN_ABORT(td)) 23889b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 238961a74c5cSJeff Roberson else 2390a54e85fdSJeff Roberson thread_unlock(td); 23917b4a950aSDavid Xu PROC_SUNLOCK(p); 2392df8bae1dSRodney W. Grimes goto out; 2393df8bae1dSRodney W. Grimes /* 23949a6a4cb5SPeter Wemm * Mutexes are short lived. Threads waiting on them will 23959a6a4cb5SPeter Wemm * hit thread_suspend_check() soon. 2396df8bae1dSRodney W. Grimes */ 2397e602ba25SJulian Elischer } else if (p->p_state == PRS_NORMAL) { 2398ec8297bdSDavid Xu if (p->p_flag & P_TRACED || action == SIG_CATCH) { 239994f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 2400df8bae1dSRodney W. Grimes goto out; 240104774f23SJulian Elischer } 2402ec8297bdSDavid Xu 2403ec8297bdSDavid Xu MPASS(action == SIG_DFL); 2404ec8297bdSDavid Xu 2405fd50a707SBrooks Davis if (prop & SIGPROP_STOP) { 24060f14f15bSJohn Baldwin if (p->p_flag & (P_PPWAIT|P_WEXIT)) 240735c32a76SDavid Xu goto out; 2408e574e444SDavid Xu p->p_flag |= P_STOPPED_SIG; 2409b4490c6eSKonstantin Belousov p->p_xsig = sig; 24107b4a950aSDavid Xu PROC_SLOCK(p); 24116f56cb8dSKonstantin Belousov wakeup_swapper = sig_suspend_threads(td, p, 1); 24124093529dSJeff Roberson if (p->p_numthreads == p->p_suspcount) { 2413ebceaf6dSDavid Xu /* 2414ebceaf6dSDavid Xu * only thread sending signal to another 2415ebceaf6dSDavid Xu * process can reach here, if thread is sending 2416ebceaf6dSDavid Xu * signal to its process, because thread does 2417ebceaf6dSDavid Xu * not suspend itself here, p_numthreads 2418ebceaf6dSDavid Xu * should never be equal to p_suspcount. 2419ebceaf6dSDavid Xu */ 2420ebceaf6dSDavid Xu thread_stopped(p); 24217b4a950aSDavid Xu PROC_SUNLOCK(p); 2422b4490c6eSKonstantin Belousov sigqueue_delete_proc(p, p->p_xsig); 24237b4a950aSDavid Xu } else 24247b4a950aSDavid Xu PROC_SUNLOCK(p); 242535c32a76SDavid Xu goto out; 242635c32a76SDavid Xu } 2427e602ba25SJulian Elischer } else { 2428e602ba25SJulian Elischer /* Not in "NORMAL" state. discard the signal. */ 24299104847fSDavid Xu sigqueue_delete(sigqueue, sig); 2430e602ba25SJulian Elischer goto out; 2431e602ba25SJulian Elischer } 2432e602ba25SJulian Elischer 2433b40ce416SJulian Elischer /* 2434e602ba25SJulian Elischer * The process is not stopped so we need to apply the signal to all the 2435e602ba25SJulian Elischer * running threads. 2436b40ce416SJulian Elischer */ 2437e602ba25SJulian Elischer runfast: 243894f0972bSDavid Xu tdsigwakeup(td, sig, action, intrval); 24397b4a950aSDavid Xu PROC_SLOCK(p); 2440e602ba25SJulian Elischer thread_unsuspend(p); 24417b4a950aSDavid Xu PROC_SUNLOCK(p); 2442dc47fdf1SKonstantin Belousov out_cont: 2443dc47fdf1SKonstantin Belousov itimer_proc_continue(p); 24442fd1ffefSKonstantin Belousov kqtimer_proc_continue(p); 2445e602ba25SJulian Elischer out: 24467b4a950aSDavid Xu /* If we jump here, proc slock should not be owned. */ 24477b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 244861a74c5cSJeff Roberson if (wakeup_swapper) 244961a74c5cSJeff Roberson kick_proc0(); 245061a74c5cSJeff Roberson 24519104847fSDavid Xu return (ret); 2452e602ba25SJulian Elischer } 2453e602ba25SJulian Elischer 2454e602ba25SJulian Elischer /* 2455e602ba25SJulian Elischer * The force of a signal has been directed against a single 2456e602ba25SJulian Elischer * thread. We need to see what we can do about knocking it 2457e602ba25SJulian Elischer * out of any sleep it may be in etc. 2458e602ba25SJulian Elischer */ 2459e602ba25SJulian Elischer static void 246094f0972bSDavid Xu tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2461e602ba25SJulian Elischer { 2462e602ba25SJulian Elischer struct proc *p = td->td_proc; 246361a74c5cSJeff Roberson int prop, wakeup_swapper; 2464e602ba25SJulian Elischer 24658b94a061SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 2466e602ba25SJulian Elischer prop = sigprop(sig); 2467a4c2da15SBruce Evans 24687b4a950aSDavid Xu PROC_SLOCK(p); 2469374ae2a3SJeff Roberson thread_lock(td); 2470e602ba25SJulian Elischer /* 2471aa0fa334SJulian Elischer * Bring the priority of a thread up if we want it to get 2472aef68c96SKonstantin Belousov * killed in this lifetime. Be careful to avoid bumping the 2473aef68c96SKonstantin Belousov * priority of the idle thread, since we still allow to signal 2474aef68c96SKonstantin Belousov * kernel processes. 2475e602ba25SJulian Elischer */ 2476fd50a707SBrooks Davis if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2477aef68c96SKonstantin Belousov td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2478b3a4fb14SDavid Xu sched_prio(td, PUSER); 247980c4433cSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 2480e602ba25SJulian Elischer /* 2481e602ba25SJulian Elischer * If thread is sleeping uninterruptibly 2482e602ba25SJulian Elischer * we can't interrupt the sleep... the signal will 2483e602ba25SJulian Elischer * be noticed when the process returns through 2484e602ba25SJulian Elischer * trap() or syscall(). 2485e602ba25SJulian Elischer */ 248644f3b092SJohn Baldwin if ((td->td_flags & TDF_SINTR) == 0) 2487374ae2a3SJeff Roberson goto out; 2488e602ba25SJulian Elischer /* 2489e602ba25SJulian Elischer * If SIGCONT is default (or ignored) and process is 2490e602ba25SJulian Elischer * asleep, we are finished; the process should not 2491e602ba25SJulian Elischer * be awakened. 2492df8bae1dSRodney W. Grimes */ 2493fd50a707SBrooks Davis if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2494a54e85fdSJeff Roberson thread_unlock(td); 24957b4a950aSDavid Xu PROC_SUNLOCK(p); 24969104847fSDavid Xu sigqueue_delete(&p->p_sigqueue, sig); 24974093529dSJeff Roberson /* 24984093529dSJeff Roberson * It may be on either list in this state. 24994093529dSJeff Roberson * Remove from both for now. 25004093529dSJeff Roberson */ 25019104847fSDavid Xu sigqueue_delete(&td->td_sigqueue, sig); 2502aa0fa334SJulian Elischer return; 2503df8bae1dSRodney W. Grimes } 2504df8bae1dSRodney W. Grimes 2505aa0fa334SJulian Elischer /* 2506a120a7a3SJohn Baldwin * Don't awaken a sleeping thread for SIGSTOP if the 2507a120a7a3SJohn Baldwin * STOP signal is deferred. 2508a120a7a3SJohn Baldwin */ 2509fd50a707SBrooks Davis if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 25106f56cb8dSKonstantin Belousov TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2511a120a7a3SJohn Baldwin goto out; 2512a120a7a3SJohn Baldwin 2513a120a7a3SJohn Baldwin /* 2514a4c2da15SBruce Evans * Give low priority threads a better chance to run. 2515aa0fa334SJulian Elischer */ 2516aef68c96SKonstantin Belousov if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2517b3a4fb14SDavid Xu sched_prio(td, PUSER); 2518ec8297bdSDavid Xu 25199b86d3e5SKonstantin Belousov wakeup_swapper = sig_sleepq_abort(td, intrval); 252061a74c5cSJeff Roberson PROC_SUNLOCK(p); 252161a74c5cSJeff Roberson if (wakeup_swapper) 252261a74c5cSJeff Roberson kick_proc0(); 252361a74c5cSJeff Roberson return; 252461a74c5cSJeff Roberson } 252561a74c5cSJeff Roberson 2526df8bae1dSRodney W. Grimes /* 2527a4c2da15SBruce Evans * Other states do nothing with the signal immediately, 2528df8bae1dSRodney W. Grimes * other than kicking ourselves if we are running. 2529df8bae1dSRodney W. Grimes * It will either never be noticed, or noticed very soon. 2530df8bae1dSRodney W. Grimes */ 2531a4c2da15SBruce Evans #ifdef SMP 253244f3b092SJohn Baldwin if (TD_IS_RUNNING(td) && td != curthread) 2533e602ba25SJulian Elischer forward_signal(td); 25343163861cSTor Egge #endif 253561a74c5cSJeff Roberson 2536374ae2a3SJeff Roberson out: 25377b4a950aSDavid Xu PROC_SUNLOCK(p); 2538374ae2a3SJeff Roberson thread_unlock(td); 2539a4c2da15SBruce Evans } 2540df8bae1dSRodney W. Grimes 254187a64872SKonstantin Belousov static void 254287a64872SKonstantin Belousov ptrace_coredump(struct thread *td) 254387a64872SKonstantin Belousov { 254487a64872SKonstantin Belousov struct proc *p; 254587a64872SKonstantin Belousov struct thr_coredump_req *tcq; 254687a64872SKonstantin Belousov void *rl_cookie; 254787a64872SKonstantin Belousov 254887a64872SKonstantin Belousov MPASS(td == curthread); 254987a64872SKonstantin Belousov p = td->td_proc; 255087a64872SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 255187a64872SKonstantin Belousov if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0) 255287a64872SKonstantin Belousov return; 255387a64872SKonstantin Belousov KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped")); 255487a64872SKonstantin Belousov 255587a64872SKonstantin Belousov tcq = td->td_coredump; 255687a64872SKonstantin Belousov KASSERT(tcq != NULL, ("td_coredump is NULL")); 255787a64872SKonstantin Belousov 255887a64872SKonstantin Belousov if (p->p_sysent->sv_coredump == NULL) { 255987a64872SKonstantin Belousov tcq->tc_error = ENOSYS; 256087a64872SKonstantin Belousov goto wake; 256187a64872SKonstantin Belousov } 256287a64872SKonstantin Belousov 256387a64872SKonstantin Belousov PROC_UNLOCK(p); 256487a64872SKonstantin Belousov rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX); 256587a64872SKonstantin Belousov 256687a64872SKonstantin Belousov tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp, 256787a64872SKonstantin Belousov tcq->tc_limit, tcq->tc_flags); 256887a64872SKonstantin Belousov 256987a64872SKonstantin Belousov vn_rangelock_unlock(tcq->tc_vp, rl_cookie); 257087a64872SKonstantin Belousov PROC_LOCK(p); 257187a64872SKonstantin Belousov wake: 257287a64872SKonstantin Belousov td->td_dbgflags &= ~TDB_COREDUMPRQ; 257387a64872SKonstantin Belousov td->td_coredump = NULL; 257487a64872SKonstantin Belousov wakeup(p); 257587a64872SKonstantin Belousov } 257687a64872SKonstantin Belousov 25776f56cb8dSKonstantin Belousov static int 2578d8267df7SDavid Xu sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2579d8267df7SDavid Xu { 2580d8267df7SDavid Xu struct thread *td2; 25816f56cb8dSKonstantin Belousov int wakeup_swapper; 2582d8267df7SDavid Xu 2583d8267df7SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 25847b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 25850c46712cSMark Johnston MPASS(sending || td == curthread); 2586d8267df7SDavid Xu 25876f56cb8dSKonstantin Belousov wakeup_swapper = 0; 2588d8267df7SDavid Xu FOREACH_THREAD_IN_PROC(p, td2) { 2589a54e85fdSJeff Roberson thread_lock(td2); 2590b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2591d8267df7SDavid Xu if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2592f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) { 2593f33a947bSKonstantin Belousov if (td2->td_flags & TDF_SBDRY) { 2594a120a7a3SJohn Baldwin /* 2595a120a7a3SJohn Baldwin * Once a thread is asleep with 25966f56cb8dSKonstantin Belousov * TDF_SBDRY and without TDF_SERESTART 25976f56cb8dSKonstantin Belousov * or TDF_SEINTR set, it should never 2598a120a7a3SJohn Baldwin * become suspended due to this check. 2599a120a7a3SJohn Baldwin */ 2600a120a7a3SJohn Baldwin KASSERT(!TD_IS_SUSPENDED(td2), 2601a120a7a3SJohn Baldwin ("thread with deferred stops suspended")); 260261a74c5cSJeff Roberson if (TD_SBDRY_INTR(td2)) { 260346e47c4fSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, 260446e47c4fSKonstantin Belousov TD_SBDRY_ERRNO(td2)); 260561a74c5cSJeff Roberson continue; 2606f33a947bSKonstantin Belousov } 260761a74c5cSJeff Roberson } else if (!TD_IS_SUSPENDED(td2)) 260861a74c5cSJeff Roberson thread_suspend_one(td2); 2609f33a947bSKonstantin Belousov } else if (!TD_IS_SUSPENDED(td2)) { 2610d8267df7SDavid Xu if (sending || td != td2) 2611d8267df7SDavid Xu td2->td_flags |= TDF_ASTPENDING; 2612d8267df7SDavid Xu #ifdef SMP 2613d8267df7SDavid Xu if (TD_IS_RUNNING(td2) && td2 != td) 2614d8267df7SDavid Xu forward_signal(td2); 2615d8267df7SDavid Xu #endif 2616d8267df7SDavid Xu } 2617a54e85fdSJeff Roberson thread_unlock(td2); 2618d8267df7SDavid Xu } 26196f56cb8dSKonstantin Belousov return (wakeup_swapper); 2620d8267df7SDavid Xu } 2621d8267df7SDavid Xu 262282a4538fSEric Badger /* 262382a4538fSEric Badger * Stop the process for an event deemed interesting to the debugger. If si is 262482a4538fSEric Badger * non-NULL, this is a signal exchange; the new signal requested by the 262582a4538fSEric Badger * debugger will be returned for handling. If si is NULL, this is some other 262682a4538fSEric Badger * type of interesting event. The debugger may request a signal be delivered in 262782a4538fSEric Badger * that case as well, however it will be deferred until it can be handled. 262882a4538fSEric Badger */ 2629cbf4e354SDavid Xu int 263082a4538fSEric Badger ptracestop(struct thread *td, int sig, ksiginfo_t *si) 26314cc9f52fSRobert Drehmel { 26324cc9f52fSRobert Drehmel struct proc *p = td->td_proc; 263382a4538fSEric Badger struct thread *td2; 263482a4538fSEric Badger ksiginfo_t ksi; 26354cc9f52fSRobert Drehmel 263630a9f26dSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 26370f14f15bSJohn Baldwin KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 26384cc9f52fSRobert Drehmel WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2639aa89d8cdSJohn Baldwin &p->p_mtx.lock_object, "Stopping for traced signal"); 26404cc9f52fSRobert Drehmel 2641cbf4e354SDavid Xu td->td_xsig = sig; 264282a4538fSEric Badger 264382a4538fSEric Badger if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 264482a4538fSEric Badger td->td_dbgflags |= TDB_XSIG; 2645515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2646515b7a0bSJohn Baldwin td->td_tid, p->p_pid, td->td_dbgflags, sig); 26477b4a950aSDavid Xu PROC_SLOCK(p); 2648904c5ec4SDavid Xu while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 264982a4538fSEric Badger if (P_KILLED(p)) { 265082a4538fSEric Badger /* 265182a4538fSEric Badger * Ensure that, if we've been PT_KILLed, the 265282a4538fSEric Badger * exit status reflects that. Another thread 265382a4538fSEric Badger * may also be in ptracestop(), having just 265482a4538fSEric Badger * received the SIGKILL, but this thread was 265582a4538fSEric Badger * unsuspended first. 265682a4538fSEric Badger */ 265782a4538fSEric Badger td->td_dbgflags &= ~TDB_XSIG; 265882a4538fSEric Badger td->td_xsig = SIGKILL; 265982a4538fSEric Badger p->p_ptevents = 0; 266082a4538fSEric Badger break; 266182a4538fSEric Badger } 26625fcfab6eSJohn Baldwin if (p->p_flag & P_SINGLE_EXIT && 26635fcfab6eSJohn Baldwin !(td->td_dbgflags & TDB_EXIT)) { 26645fcfab6eSJohn Baldwin /* 26655fcfab6eSJohn Baldwin * Ignore ptrace stops except for thread exit 26665fcfab6eSJohn Baldwin * events when the process exits. 26675fcfab6eSJohn Baldwin */ 2668904c5ec4SDavid Xu td->td_dbgflags &= ~TDB_XSIG; 26697b4a950aSDavid Xu PROC_SUNLOCK(p); 267082a4538fSEric Badger return (0); 2671cbf4e354SDavid Xu } 2672b7a25e63SKonstantin Belousov 2673cbf4e354SDavid Xu /* 2674b7a25e63SKonstantin Belousov * Make wait(2) work. Ensure that right after the 2675b7a25e63SKonstantin Belousov * attach, the thread which was decided to become the 2676b7a25e63SKonstantin Belousov * leader of attach gets reported to the waiter. 2677b7a25e63SKonstantin Belousov * Otherwise, just avoid overwriting another thread's 2678b7a25e63SKonstantin Belousov * assignment to p_xthread. If another thread has 2679b7a25e63SKonstantin Belousov * already set p_xthread, the current thread will get 2680b7a25e63SKonstantin Belousov * a chance to report itself upon the next iteration. 2681cbf4e354SDavid Xu */ 2682b7a25e63SKonstantin Belousov if ((td->td_dbgflags & TDB_FSTP) != 0 || 26837f649ddaSMark Johnston ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2684b7a25e63SKonstantin Belousov p->p_xthread == NULL)) { 2685b4490c6eSKonstantin Belousov p->p_xsig = sig; 2686cbf4e354SDavid Xu p->p_xthread = td; 2687ab74c843SKonstantin Belousov 2688ab74c843SKonstantin Belousov /* 2689ab74c843SKonstantin Belousov * If we are on sleepqueue already, 2690ab74c843SKonstantin Belousov * let sleepqueue code decide if it 2691ab74c843SKonstantin Belousov * needs to go sleep after attach. 2692ab74c843SKonstantin Belousov */ 2693ab74c843SKonstantin Belousov if (td->td_wchan == NULL) 2694b7a25e63SKonstantin Belousov td->td_dbgflags &= ~TDB_FSTP; 2695ab74c843SKonstantin Belousov 2696b7a25e63SKonstantin Belousov p->p_flag2 &= ~P2_PTRACE_FSTP; 2697b7a25e63SKonstantin Belousov p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2698d8267df7SDavid Xu sig_suspend_threads(td, p, 0); 2699b7a25e63SKonstantin Belousov } 27006fa39a73SKonstantin Belousov if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 27016fa39a73SKonstantin Belousov td->td_dbgflags &= ~TDB_STOPATFORK; 27026fa39a73SKonstantin Belousov } 2703cbf4e354SDavid Xu stopme: 270468d311b6SKonstantin Belousov td->td_dbgflags |= TDB_SSWITCH; 27056ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 270668d311b6SKonstantin Belousov td->td_dbgflags &= ~TDB_SSWITCH; 270787a64872SKonstantin Belousov if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) { 270887a64872SKonstantin Belousov PROC_SUNLOCK(p); 270987a64872SKonstantin Belousov ptrace_coredump(td); 271087a64872SKonstantin Belousov PROC_SLOCK(p); 271187a64872SKonstantin Belousov goto stopme; 271287a64872SKonstantin Belousov } 27137ce60f60SDavid Xu if (p->p_xthread == td) 27147ce60f60SDavid Xu p->p_xthread = NULL; 27157ce60f60SDavid Xu if (!(p->p_flag & P_TRACED)) 2716cbf4e354SDavid Xu break; 2717904c5ec4SDavid Xu if (td->td_dbgflags & TDB_SUSPEND) { 2718cbf4e354SDavid Xu if (p->p_flag & P_SINGLE_EXIT) 2719cbf4e354SDavid Xu break; 2720cbf4e354SDavid Xu goto stopme; 2721cbf4e354SDavid Xu } 2722cbf4e354SDavid Xu } 27237b4a950aSDavid Xu PROC_SUNLOCK(p); 272482a4538fSEric Badger } 272582a4538fSEric Badger 272682a4538fSEric Badger if (si != NULL && sig == td->td_xsig) { 272782a4538fSEric Badger /* Parent wants us to take the original signal unchanged. */ 272882a4538fSEric Badger si->ksi_flags |= KSI_HEAD; 272982a4538fSEric Badger if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 273082a4538fSEric Badger si->ksi_signo = 0; 273182a4538fSEric Badger } else if (td->td_xsig != 0) { 273282a4538fSEric Badger /* 273382a4538fSEric Badger * If parent wants us to take a new signal, then it will leave 273482a4538fSEric Badger * it in td->td_xsig; otherwise we just look for signals again. 273582a4538fSEric Badger */ 273682a4538fSEric Badger ksiginfo_init(&ksi); 273782a4538fSEric Badger ksi.ksi_signo = td->td_xsig; 273882a4538fSEric Badger ksi.ksi_flags |= KSI_PTRACE; 2739146fc63fSKonstantin Belousov td2 = sigtd(p, td->td_xsig, false); 274082a4538fSEric Badger tdsendsignal(p, td2, td->td_xsig, &ksi); 274182a4538fSEric Badger if (td != td2) 274282a4538fSEric Badger return (0); 274382a4538fSEric Badger } 274482a4538fSEric Badger 2745cbf4e354SDavid Xu return (td->td_xsig); 27464cc9f52fSRobert Drehmel } 27474cc9f52fSRobert Drehmel 27480bc52b0bSKonstantin Belousov static void 274980a8b0f3SKonstantin Belousov reschedule_signals(struct proc *p, sigset_t block, int flags) 27506b286ee8SKonstantin Belousov { 27516b286ee8SKonstantin Belousov struct sigacts *ps; 27526b286ee8SKonstantin Belousov struct thread *td; 2753407af02bSDavid Xu int sig; 2754146fc63fSKonstantin Belousov bool fastblk, pslocked; 27556b286ee8SKonstantin Belousov 27566b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 275770778bbaSKonstantin Belousov ps = p->p_sigacts; 2758146fc63fSKonstantin Belousov pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0; 2759146fc63fSKonstantin Belousov mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED); 2760407af02bSDavid Xu if (SIGISEMPTY(p->p_siglist)) 2761407af02bSDavid Xu return; 2762407af02bSDavid Xu SIGSETAND(block, p->p_siglist); 2763146fc63fSKonstantin Belousov fastblk = (flags & SIGPROCMASK_FASTBLK) != 0; 276481f2e906SMark Johnston SIG_FOREACH(sig, &block) { 2765146fc63fSKonstantin Belousov td = sigtd(p, sig, fastblk); 2766146fc63fSKonstantin Belousov 2767146fc63fSKonstantin Belousov /* 2768146fc63fSKonstantin Belousov * If sigtd() selected us despite sigfastblock is 2769146fc63fSKonstantin Belousov * blocking, do not activate AST or wake us, to avoid 2770146fc63fSKonstantin Belousov * loop in AST handler. 2771146fc63fSKonstantin Belousov */ 2772146fc63fSKonstantin Belousov if (fastblk && td == curthread) 2773146fc63fSKonstantin Belousov continue; 2774146fc63fSKonstantin Belousov 27756b286ee8SKonstantin Belousov signotify(td); 2776146fc63fSKonstantin Belousov if (!pslocked) 27776b286ee8SKonstantin Belousov mtx_lock(&ps->ps_mtx); 2778396a0d44SKonstantin Belousov if (p->p_flag & P_TRACED || 2779396a0d44SKonstantin Belousov (SIGISMEMBER(ps->ps_sigcatch, sig) && 2780146fc63fSKonstantin Belousov !SIGISMEMBER(td->td_sigmask, sig))) { 2781407af02bSDavid Xu tdsigwakeup(td, sig, SIG_CATCH, 2782407af02bSDavid Xu (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 27836b286ee8SKonstantin Belousov ERESTART)); 2784146fc63fSKonstantin Belousov } 2785146fc63fSKonstantin Belousov if (!pslocked) 27866b286ee8SKonstantin Belousov mtx_unlock(&ps->ps_mtx); 27876b286ee8SKonstantin Belousov } 27886b286ee8SKonstantin Belousov } 27896b286ee8SKonstantin Belousov 27906b286ee8SKonstantin Belousov void 27916b286ee8SKonstantin Belousov tdsigcleanup(struct thread *td) 27926b286ee8SKonstantin Belousov { 27936b286ee8SKonstantin Belousov struct proc *p; 27946b286ee8SKonstantin Belousov sigset_t unblocked; 27956b286ee8SKonstantin Belousov 27966b286ee8SKonstantin Belousov p = td->td_proc; 27976b286ee8SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 27986b286ee8SKonstantin Belousov 27996b286ee8SKonstantin Belousov sigqueue_flush(&td->td_sigqueue); 28006b286ee8SKonstantin Belousov if (p->p_numthreads == 1) 28016b286ee8SKonstantin Belousov return; 28026b286ee8SKonstantin Belousov 28036b286ee8SKonstantin Belousov /* 28046b286ee8SKonstantin Belousov * Since we cannot handle signals, notify signal post code 28056b286ee8SKonstantin Belousov * about this by filling the sigmask. 28066b286ee8SKonstantin Belousov * 28076b286ee8SKonstantin Belousov * Also, if needed, wake up thread(s) that do not block the 28086b286ee8SKonstantin Belousov * same signals as the exiting thread, since the thread might 28096b286ee8SKonstantin Belousov * have been selected for delivery and woken up. 28106b286ee8SKonstantin Belousov */ 28116b286ee8SKonstantin Belousov SIGFILLSET(unblocked); 28126b286ee8SKonstantin Belousov SIGSETNAND(unblocked, td->td_sigmask); 28136b286ee8SKonstantin Belousov SIGFILLSET(td->td_sigmask); 281480a8b0f3SKonstantin Belousov reschedule_signals(p, unblocked, 0); 28156b286ee8SKonstantin Belousov 28166b286ee8SKonstantin Belousov } 28176b286ee8SKonstantin Belousov 28183a1e5dd8SKonstantin Belousov static int 28193a1e5dd8SKonstantin Belousov sigdeferstop_curr_flags(int cflags) 2820a120a7a3SJohn Baldwin { 2821a120a7a3SJohn Baldwin 28223a1e5dd8SKonstantin Belousov MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 28233a1e5dd8SKonstantin Belousov (cflags & TDF_SBDRY) != 0); 28243a1e5dd8SKonstantin Belousov return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 2825a120a7a3SJohn Baldwin } 2826a120a7a3SJohn Baldwin 2827a120a7a3SJohn Baldwin /* 28283a1e5dd8SKonstantin Belousov * Defer the delivery of SIGSTOP for the current thread, according to 28293a1e5dd8SKonstantin Belousov * the requested mode. Returns previous flags, which must be restored 28303a1e5dd8SKonstantin Belousov * by sigallowstop(). 28313a1e5dd8SKonstantin Belousov * 28323a1e5dd8SKonstantin Belousov * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 28333a1e5dd8SKonstantin Belousov * cleared by the current thread, which allow the lock-less read-only 28343a1e5dd8SKonstantin Belousov * accesses below. 2835a120a7a3SJohn Baldwin */ 2836e3612a4cSKonstantin Belousov int 283746e47c4fSKonstantin Belousov sigdeferstop_impl(int mode) 2838a120a7a3SJohn Baldwin { 2839593efaf9SJohn Baldwin struct thread *td; 28403a1e5dd8SKonstantin Belousov int cflags, nflags; 2841a120a7a3SJohn Baldwin 2842593efaf9SJohn Baldwin td = curthread; 28433a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 28443a1e5dd8SKonstantin Belousov switch (mode) { 28453a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_NOP: 28463a1e5dd8SKonstantin Belousov nflags = cflags; 28473a1e5dd8SKonstantin Belousov break; 28483a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_OFF: 28493a1e5dd8SKonstantin Belousov nflags = 0; 28503a1e5dd8SKonstantin Belousov break; 28513a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_SILENT: 28523a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 28533a1e5dd8SKonstantin Belousov break; 28543a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_EINTR: 28553a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 28563a1e5dd8SKonstantin Belousov break; 28573a1e5dd8SKonstantin Belousov case SIGDEFERSTOP_ERESTART: 28583a1e5dd8SKonstantin Belousov nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 28593a1e5dd8SKonstantin Belousov break; 28603a1e5dd8SKonstantin Belousov default: 28613a1e5dd8SKonstantin Belousov panic("sigdeferstop: invalid mode %x", mode); 28623a1e5dd8SKonstantin Belousov break; 28633a1e5dd8SKonstantin Belousov } 286446e47c4fSKonstantin Belousov if (cflags == nflags) 286546e47c4fSKonstantin Belousov return (SIGDEFERSTOP_VAL_NCHG); 2866a120a7a3SJohn Baldwin thread_lock(td); 28673a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | nflags; 2868a120a7a3SJohn Baldwin thread_unlock(td); 28693a1e5dd8SKonstantin Belousov return (cflags); 28703a1e5dd8SKonstantin Belousov } 28713a1e5dd8SKonstantin Belousov 28723a1e5dd8SKonstantin Belousov /* 28733a1e5dd8SKonstantin Belousov * Restores the STOP handling mode, typically permitting the delivery 28743a1e5dd8SKonstantin Belousov * of SIGSTOP for the current thread. This does not immediately 28753a1e5dd8SKonstantin Belousov * suspend if a stop was posted. Instead, the thread will suspend 28763a1e5dd8SKonstantin Belousov * either via ast() or a subsequent interruptible sleep. 28773a1e5dd8SKonstantin Belousov */ 28783a1e5dd8SKonstantin Belousov void 287946e47c4fSKonstantin Belousov sigallowstop_impl(int prev) 28803a1e5dd8SKonstantin Belousov { 28813a1e5dd8SKonstantin Belousov struct thread *td; 28823a1e5dd8SKonstantin Belousov int cflags; 28833a1e5dd8SKonstantin Belousov 288446e47c4fSKonstantin Belousov KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 28853a1e5dd8SKonstantin Belousov KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 28863a1e5dd8SKonstantin Belousov ("sigallowstop: incorrect previous mode %x", prev)); 28873a1e5dd8SKonstantin Belousov td = curthread; 28883a1e5dd8SKonstantin Belousov cflags = sigdeferstop_curr_flags(td->td_flags); 28893a1e5dd8SKonstantin Belousov if (cflags != prev) { 28903a1e5dd8SKonstantin Belousov thread_lock(td); 28913a1e5dd8SKonstantin Belousov td->td_flags = (td->td_flags & ~cflags) | prev; 28923a1e5dd8SKonstantin Belousov thread_unlock(td); 28933a1e5dd8SKonstantin Belousov } 2894a120a7a3SJohn Baldwin } 2895a120a7a3SJohn Baldwin 289681f2e906SMark Johnston enum sigstatus { 289781f2e906SMark Johnston SIGSTATUS_HANDLE, 289881f2e906SMark Johnston SIGSTATUS_HANDLED, 289981f2e906SMark Johnston SIGSTATUS_IGNORE, 290081f2e906SMark Johnston SIGSTATUS_SBDRY_STOP, 290181f2e906SMark Johnston }; 290281f2e906SMark Johnston 290381f2e906SMark Johnston /* 290481f2e906SMark Johnston * The thread has signal "sig" pending. Figure out what to do with it: 290581f2e906SMark Johnston * 290681f2e906SMark Johnston * _HANDLE -> the caller should handle the signal 290781f2e906SMark Johnston * _HANDLED -> handled internally, reload pending signal set 290881f2e906SMark Johnston * _IGNORE -> ignored, remove from the set of pending signals and try the 290981f2e906SMark Johnston * next pending signal 291081f2e906SMark Johnston * _SBDRY_STOP -> the signal should stop the thread but this is not 291181f2e906SMark Johnston * permitted in the current context 291281f2e906SMark Johnston */ 291381f2e906SMark Johnston static enum sigstatus 291481f2e906SMark Johnston sigprocess(struct thread *td, int sig) 291581f2e906SMark Johnston { 291681f2e906SMark Johnston struct proc *p; 291781f2e906SMark Johnston struct sigacts *ps; 291881f2e906SMark Johnston struct sigqueue *queue; 291981f2e906SMark Johnston ksiginfo_t ksi; 292081f2e906SMark Johnston int prop; 292181f2e906SMark Johnston 292281f2e906SMark Johnston KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig)); 292381f2e906SMark Johnston 292481f2e906SMark Johnston p = td->td_proc; 292581f2e906SMark Johnston ps = p->p_sigacts; 292681f2e906SMark Johnston mtx_assert(&ps->ps_mtx, MA_OWNED); 292781f2e906SMark Johnston PROC_LOCK_ASSERT(p, MA_OWNED); 292881f2e906SMark Johnston 292981f2e906SMark Johnston /* 293081f2e906SMark Johnston * We should allow pending but ignored signals below 293102a2aacbSKonstantin Belousov * if there is sigwait() active, or P_TRACED was 293281f2e906SMark Johnston * on when they were posted. 293381f2e906SMark Johnston */ 293481f2e906SMark Johnston if (SIGISMEMBER(ps->ps_sigignore, sig) && 293581f2e906SMark Johnston (p->p_flag & P_TRACED) == 0 && 293681f2e906SMark Johnston (td->td_flags & TDF_SIGWAIT) == 0) { 293781f2e906SMark Johnston return (SIGSTATUS_IGNORE); 293881f2e906SMark Johnston } 293981f2e906SMark Johnston 294002a2aacbSKonstantin Belousov /* 294102a2aacbSKonstantin Belousov * If the process is going to single-thread mode to prepare 294202a2aacbSKonstantin Belousov * for exit, there is no sense in delivering any signal 294302a2aacbSKonstantin Belousov * to usermode. Another important consequence is that 294402a2aacbSKonstantin Belousov * msleep(..., PCATCH, ...) now is only interruptible by a 294502a2aacbSKonstantin Belousov * suspend request. 294602a2aacbSKonstantin Belousov */ 294702a2aacbSKonstantin Belousov if ((p->p_flag2 & P2_WEXIT) != 0) 294802a2aacbSKonstantin Belousov return (SIGSTATUS_IGNORE); 294902a2aacbSKonstantin Belousov 295081f2e906SMark Johnston if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 295181f2e906SMark Johnston /* 295281f2e906SMark Johnston * If traced, always stop. 295381f2e906SMark Johnston * Remove old signal from queue before the stop. 295481f2e906SMark Johnston * XXX shrug off debugger, it causes siginfo to 295581f2e906SMark Johnston * be thrown away. 295681f2e906SMark Johnston */ 295781f2e906SMark Johnston queue = &td->td_sigqueue; 295881f2e906SMark Johnston ksiginfo_init(&ksi); 295981f2e906SMark Johnston if (sigqueue_get(queue, sig, &ksi) == 0) { 296081f2e906SMark Johnston queue = &p->p_sigqueue; 296181f2e906SMark Johnston sigqueue_get(queue, sig, &ksi); 296281f2e906SMark Johnston } 296381f2e906SMark Johnston td->td_si = ksi.ksi_info; 296481f2e906SMark Johnston 296581f2e906SMark Johnston mtx_unlock(&ps->ps_mtx); 296681f2e906SMark Johnston sig = ptracestop(td, sig, &ksi); 296781f2e906SMark Johnston mtx_lock(&ps->ps_mtx); 296881f2e906SMark Johnston 296981f2e906SMark Johnston td->td_si.si_signo = 0; 297081f2e906SMark Johnston 297181f2e906SMark Johnston /* 297281f2e906SMark Johnston * Keep looking if the debugger discarded or 297381f2e906SMark Johnston * replaced the signal. 297481f2e906SMark Johnston */ 297581f2e906SMark Johnston if (sig == 0) 297681f2e906SMark Johnston return (SIGSTATUS_HANDLED); 297781f2e906SMark Johnston 297881f2e906SMark Johnston /* 297981f2e906SMark Johnston * If the signal became masked, re-queue it. 298081f2e906SMark Johnston */ 298181f2e906SMark Johnston if (SIGISMEMBER(td->td_sigmask, sig)) { 298281f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 298381f2e906SMark Johnston sigqueue_add(&p->p_sigqueue, sig, &ksi); 298481f2e906SMark Johnston return (SIGSTATUS_HANDLED); 298581f2e906SMark Johnston } 298681f2e906SMark Johnston 298781f2e906SMark Johnston /* 298881f2e906SMark Johnston * If the traced bit got turned off, requeue the signal and 298981f2e906SMark Johnston * reload the set of pending signals. This ensures that p_sig* 299081f2e906SMark Johnston * and p_sigact are consistent. 299181f2e906SMark Johnston */ 299281f2e906SMark Johnston if ((p->p_flag & P_TRACED) == 0) { 2993a24afbb4SKonstantin Belousov if ((ksi.ksi_flags & KSI_PTRACE) == 0) { 299481f2e906SMark Johnston ksi.ksi_flags |= KSI_HEAD; 299581f2e906SMark Johnston sigqueue_add(queue, sig, &ksi); 2996a24afbb4SKonstantin Belousov } 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); 3419*4493a13eSKonstantin Belousov proc_set_p2_wexit(p); 3420*4493a13eSKonstantin Belousov 3421df8bae1dSRodney W. Grimes p->p_acflag |= AXSIG; 3422f97c3df1SDavid Schultz /* 3423f97c3df1SDavid Schultz * We must be single-threading to generate a core dump. This 3424f97c3df1SDavid Schultz * ensures that the registers in the core file are up-to-date. 3425f97c3df1SDavid Schultz * Also, the ELF dump handler assumes that the thread list doesn't 3426f97c3df1SDavid Schultz * change out from under it. 3427f97c3df1SDavid Schultz * 3428f97c3df1SDavid Schultz * XXX If another thread attempts to single-thread before us 3429f97c3df1SDavid Schultz * (e.g. via fork()), we won't get a dump at all. 3430f97c3df1SDavid Schultz */ 3431fd50a707SBrooks Davis if ((sigprop(sig) & SIGPROP_CORE) && 3432fd50a707SBrooks Davis thread_single(p, SINGLE_NO_EXIT) == 0) { 34332c42a146SMarcel Moolenaar p->p_sig = sig; 3434c364e17eSAndrey A. Chernov /* 3435c364e17eSAndrey A. Chernov * Log signals which would cause core dumps 3436c364e17eSAndrey A. Chernov * (Log as LOG_INFO to appease those who don't want 3437c364e17eSAndrey A. Chernov * these messages.) 3438c364e17eSAndrey A. Chernov * XXX : Todo, as well as euid, write out ruid too 34394ae89b95SJohn Baldwin * Note that coredump() drops proc lock. 3440c364e17eSAndrey A. Chernov */ 3441b40ce416SJulian Elischer if (coredump(td) == 0) 34422c42a146SMarcel Moolenaar sig |= WCOREFLAG; 344357308494SJoerg Wunsch if (kern_logsigexit) 344457308494SJoerg Wunsch log(LOG_INFO, 3445af8beccaSKristof Provost "pid %d (%s), jid %d, uid %d: exited on " 34464ae4822dSKristof Provost "signal %d%s\n", p->p_pid, p->p_comm, 34474ae4822dSKristof Provost p->p_ucred->cr_prison->pr_id, 3448de2d0d29SKristof Provost td->td_ucred->cr_uid, 34492c42a146SMarcel Moolenaar sig &~ WCOREFLAG, 34502c42a146SMarcel Moolenaar sig & WCOREFLAG ? " (core dumped)" : ""); 34514ae89b95SJohn Baldwin } else 3452628d2653SJohn Baldwin PROC_UNLOCK(p); 3453b4490c6eSKonstantin Belousov exit1(td, 0, sig); 3454df8bae1dSRodney W. Grimes /* NOTREACHED */ 3455df8bae1dSRodney W. Grimes } 3456df8bae1dSRodney W. Grimes 3457ebceaf6dSDavid Xu /* 34587f96995eSDavid Xu * Send queued SIGCHLD to parent when child process's state 34597f96995eSDavid Xu * is changed. 3460ebceaf6dSDavid Xu */ 34617f96995eSDavid Xu static void 34627f96995eSDavid Xu sigparent(struct proc *p, int reason, int status) 3463ebceaf6dSDavid Xu { 3464ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 3465ebceaf6dSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3466ebceaf6dSDavid Xu 3467ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 3468ebceaf6dSDavid Xu p->p_ksi->ksi_signo = SIGCHLD; 3469ebceaf6dSDavid Xu p->p_ksi->ksi_code = reason; 34707f96995eSDavid Xu p->p_ksi->ksi_status = status; 3471ebceaf6dSDavid Xu p->p_ksi->ksi_pid = p->p_pid; 3472ebceaf6dSDavid Xu p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3473ebceaf6dSDavid Xu if (KSI_ONQ(p->p_ksi)) 3474ebceaf6dSDavid Xu return; 3475ebceaf6dSDavid Xu } 3476ad6eec7bSJohn Baldwin pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3477ebceaf6dSDavid Xu } 3478ebceaf6dSDavid Xu 34797f96995eSDavid Xu static void 3480b20a9aa9SJilles Tjoelker childproc_jobstate(struct proc *p, int reason, int sig) 34817f96995eSDavid Xu { 34827f96995eSDavid Xu struct sigacts *ps; 34837f96995eSDavid Xu 34847f96995eSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 34857f96995eSDavid Xu PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 34867f96995eSDavid Xu 34877f96995eSDavid Xu /* 34887f96995eSDavid Xu * Wake up parent sleeping in kern_wait(), also send 34897f96995eSDavid Xu * SIGCHLD to parent, but SIGCHLD does not guarantee 34907f96995eSDavid Xu * that parent will awake, because parent may masked 34917f96995eSDavid Xu * the signal. 34927f96995eSDavid Xu */ 34937f96995eSDavid Xu p->p_pptr->p_flag |= P_STATCHILD; 34947f96995eSDavid Xu wakeup(p->p_pptr); 34957f96995eSDavid Xu 34967f96995eSDavid Xu ps = p->p_pptr->p_sigacts; 34977f96995eSDavid Xu mtx_lock(&ps->ps_mtx); 34987f96995eSDavid Xu if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 34997f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 3500b20a9aa9SJilles Tjoelker sigparent(p, reason, sig); 35017f96995eSDavid Xu } else 35027f96995eSDavid Xu mtx_unlock(&ps->ps_mtx); 35037f96995eSDavid Xu } 35047f96995eSDavid Xu 35057f96995eSDavid Xu void 35067f96995eSDavid Xu childproc_stopped(struct proc *p, int reason) 35077f96995eSDavid Xu { 3508b4490c6eSKonstantin Belousov 3509b4490c6eSKonstantin Belousov childproc_jobstate(p, reason, p->p_xsig); 35107f96995eSDavid Xu } 35117f96995eSDavid Xu 3512ebceaf6dSDavid Xu void 3513ebceaf6dSDavid Xu childproc_continued(struct proc *p) 3514ebceaf6dSDavid Xu { 35157f96995eSDavid Xu childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3516ebceaf6dSDavid Xu } 3517ebceaf6dSDavid Xu 3518ebceaf6dSDavid Xu void 3519ebceaf6dSDavid Xu childproc_exited(struct proc *p) 3520ebceaf6dSDavid Xu { 3521b4490c6eSKonstantin Belousov int reason, status; 3522ebceaf6dSDavid Xu 3523b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 3524b4490c6eSKonstantin Belousov reason = CLD_DUMPED; 3525b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3526b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 3527b4490c6eSKonstantin Belousov reason = CLD_KILLED; 3528b4490c6eSKonstantin Belousov status = WTERMSIG(p->p_xsig); 3529b4490c6eSKonstantin Belousov } else { 3530b4490c6eSKonstantin Belousov reason = CLD_EXITED; 3531b4490c6eSKonstantin Belousov status = p->p_xexit; 3532b4490c6eSKonstantin Belousov } 35337f96995eSDavid Xu /* 35347f96995eSDavid Xu * XXX avoid calling wakeup(p->p_pptr), the work is 35357f96995eSDavid Xu * done in exit1(). 35367f96995eSDavid Xu */ 35377f96995eSDavid Xu sigparent(p, reason, status); 3538ebceaf6dSDavid Xu } 3539ebceaf6dSDavid Xu 3540f1fe1e02SMariusz Zaborski #define MAX_NUM_CORE_FILES 100000 3541cc37baeaSStephen J. Kiernan #ifndef NUM_CORE_FILES 3542cc37baeaSStephen J. Kiernan #define NUM_CORE_FILES 5 3543cc37baeaSStephen J. Kiernan #endif 3544cc37baeaSStephen J. Kiernan CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3545cc37baeaSStephen J. Kiernan static int num_cores = NUM_CORE_FILES; 3546e7228204SAlfred Perlstein 3547e7228204SAlfred Perlstein static int 3548e7228204SAlfred Perlstein sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3549e7228204SAlfred Perlstein { 3550e7228204SAlfred Perlstein int error; 3551e7228204SAlfred Perlstein int new_val; 3552e7228204SAlfred Perlstein 3553c5105012SKonstantin Belousov new_val = num_cores; 3554e7228204SAlfred Perlstein error = sysctl_handle_int(oidp, &new_val, 0, req); 3555e7228204SAlfred Perlstein if (error != 0 || req->newptr == NULL) 3556e7228204SAlfred Perlstein return (error); 3557cc37baeaSStephen J. Kiernan if (new_val > MAX_NUM_CORE_FILES) 3558cc37baeaSStephen J. Kiernan new_val = MAX_NUM_CORE_FILES; 3559e7228204SAlfred Perlstein if (new_val < 0) 3560e7228204SAlfred Perlstein new_val = 0; 3561e7228204SAlfred Perlstein num_cores = new_val; 3562e7228204SAlfred Perlstein return (0); 3563e7228204SAlfred Perlstein } 35647029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ncores, 3565fe27f1dbSAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int), 35667029da5cSPawel Biernacki sysctl_debug_num_cores_check, "I", 35676cad1a5dSMariusz Zaborski "Maximum number of generated process corefiles while using index format"); 3568e7228204SAlfred Perlstein 35696026dcd7SMark Johnston #define GZIP_SUFFIX ".gz" 35706026dcd7SMark Johnston #define ZSTD_SUFFIX ".zst" 3571aa14e9b7SMark Johnston 357278f57a9cSMark Johnston int compress_user_cores = 0; 3573e7228204SAlfred Perlstein 357478f57a9cSMark Johnston static int 357578f57a9cSMark Johnston sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS) 357678f57a9cSMark Johnston { 357778f57a9cSMark Johnston int error, val; 357878f57a9cSMark Johnston 357978f57a9cSMark Johnston val = compress_user_cores; 358078f57a9cSMark Johnston error = sysctl_handle_int(oidp, &val, 0, req); 358178f57a9cSMark Johnston if (error != 0 || req->newptr == NULL) 358278f57a9cSMark Johnston return (error); 358378f57a9cSMark Johnston if (val != 0 && !compressor_avail(val)) 358478f57a9cSMark Johnston return (EINVAL); 358578f57a9cSMark Johnston compress_user_cores = val; 358678f57a9cSMark Johnston return (error); 358778f57a9cSMark Johnston } 35887029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores, 35897029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int), 35907029da5cSPawel Biernacki sysctl_compress_user_cores, "I", 35916026dcd7SMark Johnston "Enable compression of user corefiles (" 35926026dcd7SMark Johnston __XSTRING(COMPRESS_GZIP) " = gzip, " 35936026dcd7SMark Johnston __XSTRING(COMPRESS_ZSTD) " = zstd)"); 359478f57a9cSMark Johnston 359578f57a9cSMark Johnston int compress_user_cores_level = 6; 359678f57a9cSMark Johnston SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN, 359778f57a9cSMark Johnston &compress_user_cores_level, 0, 359878f57a9cSMark Johnston "Corefile compression level"); 3599e7228204SAlfred Perlstein 36005bc0ff88SMateusz Guzik /* 36015bc0ff88SMateusz Guzik * Protect the access to corefilename[] by allproc_lock. 36025bc0ff88SMateusz Guzik */ 36035bc0ff88SMateusz Guzik #define corefilename_lock allproc_lock 36045bc0ff88SMateusz Guzik 36056d1ab6edSWarner Losh static char corefilename[MAXPATHLEN] = {"%N.core"}; 3606fb4cdc96SMariusz Zaborski TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 36075bc0ff88SMateusz Guzik 36085bc0ff88SMateusz Guzik static int 36095bc0ff88SMateusz Guzik sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 36105bc0ff88SMateusz Guzik { 36115bc0ff88SMateusz Guzik int error; 36125bc0ff88SMateusz Guzik 36135bc0ff88SMateusz Guzik sx_xlock(&corefilename_lock); 36145bc0ff88SMateusz Guzik error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 36155bc0ff88SMateusz Guzik req); 36165bc0ff88SMateusz Guzik sx_xunlock(&corefilename_lock); 36175bc0ff88SMateusz Guzik 36185bc0ff88SMateusz Guzik return (error); 36195bc0ff88SMateusz Guzik } 3620fb4cdc96SMariusz Zaborski SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 36215bc0ff88SMateusz Guzik CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 36225bc0ff88SMateusz Guzik "Process corefile name format string"); 3623c5edb423SSean Eric Fagan 36240dea6e3cSMariusz Zaborski static void 36250dea6e3cSMariusz Zaborski vnode_close_locked(struct thread *td, struct vnode *vp) 36260dea6e3cSMariusz Zaborski { 36270dea6e3cSMariusz Zaborski 3628b249ce48SMateusz Guzik VOP_UNLOCK(vp); 36290dea6e3cSMariusz Zaborski vn_close(vp, FWRITE, td->td_ucred, td); 36300dea6e3cSMariusz Zaborski } 36310dea6e3cSMariusz Zaborski 36320dea6e3cSMariusz Zaborski /* 36330dea6e3cSMariusz Zaborski * If the core format has a %I in it, then we need to check 36340dea6e3cSMariusz Zaborski * for existing corefiles before defining a name. 3635f1fe1e02SMariusz Zaborski * To do this we iterate over 0..ncores to find a 36360dea6e3cSMariusz Zaborski * non-existing core file name to use. If all core files are 36370dea6e3cSMariusz Zaborski * already used we choose the oldest one. 36380dea6e3cSMariusz Zaborski */ 36390dea6e3cSMariusz Zaborski static int 36400dea6e3cSMariusz Zaborski corefile_open_last(struct thread *td, char *name, int indexpos, 3641f1fe1e02SMariusz Zaborski int indexlen, int ncores, struct vnode **vpp) 36420dea6e3cSMariusz Zaborski { 36430dea6e3cSMariusz Zaborski struct vnode *oldvp, *nextvp, *vp; 36440dea6e3cSMariusz Zaborski struct vattr vattr; 36450dea6e3cSMariusz Zaborski struct nameidata nd; 36460dea6e3cSMariusz Zaborski int error, i, flags, oflags, cmode; 3647f1fe1e02SMariusz Zaborski char ch; 36480dea6e3cSMariusz Zaborski struct timespec lasttime; 36490dea6e3cSMariusz Zaborski 36500dea6e3cSMariusz Zaborski nextvp = oldvp = NULL; 36510dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 36520dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 36530dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 36540dea6e3cSMariusz Zaborski 3655f1fe1e02SMariusz Zaborski for (i = 0; i < ncores; i++) { 36560dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 3657f1fe1e02SMariusz Zaborski 3658f1fe1e02SMariusz Zaborski ch = name[indexpos + indexlen]; 3659f1fe1e02SMariusz Zaborski (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen, 3660f1fe1e02SMariusz Zaborski i); 3661f1fe1e02SMariusz Zaborski name[indexpos + indexlen] = ch; 36620dea6e3cSMariusz Zaborski 36637e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 36640dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 36650dea6e3cSMariusz Zaborski NULL); 36660dea6e3cSMariusz Zaborski if (error != 0) 36670dea6e3cSMariusz Zaborski break; 36680dea6e3cSMariusz Zaborski 36690dea6e3cSMariusz Zaborski vp = nd.ni_vp; 3670bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 36710dea6e3cSMariusz Zaborski if ((flags & O_CREAT) == O_CREAT) { 36720dea6e3cSMariusz Zaborski nextvp = vp; 36730dea6e3cSMariusz Zaborski break; 36740dea6e3cSMariusz Zaborski } 36750dea6e3cSMariusz Zaborski 36760dea6e3cSMariusz Zaborski error = VOP_GETATTR(vp, &vattr, td->td_ucred); 36770dea6e3cSMariusz Zaborski if (error != 0) { 36780dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 36790dea6e3cSMariusz Zaborski break; 36800dea6e3cSMariusz Zaborski } 36810dea6e3cSMariusz Zaborski 36820dea6e3cSMariusz Zaborski if (oldvp == NULL || 36830dea6e3cSMariusz Zaborski lasttime.tv_sec > vattr.va_mtime.tv_sec || 36840dea6e3cSMariusz Zaborski (lasttime.tv_sec == vattr.va_mtime.tv_sec && 36850dea6e3cSMariusz Zaborski lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) { 36860dea6e3cSMariusz Zaborski if (oldvp != NULL) 3687e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 36880dea6e3cSMariusz Zaborski oldvp = vp; 3689e298466eSAndriy Gapon VOP_UNLOCK(oldvp); 36900dea6e3cSMariusz Zaborski lasttime = vattr.va_mtime; 36910dea6e3cSMariusz Zaborski } else { 36920dea6e3cSMariusz Zaborski vnode_close_locked(td, vp); 36930dea6e3cSMariusz Zaborski } 36940dea6e3cSMariusz Zaborski } 36950dea6e3cSMariusz Zaborski 36960dea6e3cSMariusz Zaborski if (oldvp != NULL) { 369789f2ab06SKonstantin Belousov if (nextvp == NULL) { 369889f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) { 369989f2ab06SKonstantin Belousov error = EFAULT; 3700e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 370189f2ab06SKonstantin Belousov } else { 370289f2ab06SKonstantin Belousov nextvp = oldvp; 3703e298466eSAndriy Gapon error = vn_lock(nextvp, LK_EXCLUSIVE); 3704e298466eSAndriy Gapon if (error != 0) { 3705e298466eSAndriy Gapon vn_close(nextvp, FWRITE, td->td_ucred, 3706e298466eSAndriy Gapon td); 3707e298466eSAndriy Gapon nextvp = NULL; 3708e298466eSAndriy Gapon } 370989f2ab06SKonstantin Belousov } 371089f2ab06SKonstantin Belousov } else { 3711e298466eSAndriy Gapon vn_close(oldvp, FWRITE, td->td_ucred, td); 371289f2ab06SKonstantin Belousov } 37130dea6e3cSMariusz Zaborski } 37140dea6e3cSMariusz Zaborski if (error != 0) { 37150dea6e3cSMariusz Zaborski if (nextvp != NULL) 37160dea6e3cSMariusz Zaborski vnode_close_locked(td, oldvp); 37170dea6e3cSMariusz Zaborski } else { 37180dea6e3cSMariusz Zaborski *vpp = nextvp; 37190dea6e3cSMariusz Zaborski } 37200dea6e3cSMariusz Zaborski 37210dea6e3cSMariusz Zaborski return (error); 37220dea6e3cSMariusz Zaborski } 37230dea6e3cSMariusz Zaborski 3724c5edb423SSean Eric Fagan /* 3725c345faeaSPawel Jakub Dawidek * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3726c345faeaSPawel Jakub Dawidek * Expand the name described in corefilename, using name, uid, and pid 3727c345faeaSPawel Jakub Dawidek * and open/create core file. 3728c5edb423SSean Eric Fagan * corefilename is a printf-like string, with three format specifiers: 3729c5edb423SSean Eric Fagan * %N name of process ("name") 3730c5edb423SSean Eric Fagan * %P process id (pid) 3731c5edb423SSean Eric Fagan * %U user id (uid) 3732c5edb423SSean Eric Fagan * For example, "%N.core" is the default; they can be disabled completely 3733c5edb423SSean Eric Fagan * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3734c5edb423SSean Eric Fagan * This is controlled by the sysctl variable kern.corefile (see above). 3735c5edb423SSean Eric Fagan */ 3736c345faeaSPawel Jakub Dawidek static int 3737c345faeaSPawel Jakub Dawidek corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 37389d3ecb7eSEric van Gyzen int compress, int signum, struct vnode **vpp, char **namep) 37398b43b535SAlfred Perlstein { 374036b208e0SRobert Watson struct sbuf sb; 37410dea6e3cSMariusz Zaborski struct nameidata nd; 374236b208e0SRobert Watson const char *format; 3743c345faeaSPawel Jakub Dawidek char *hostname, *name; 3744f1fe1e02SMariusz Zaborski int cmode, error, flags, i, indexpos, indexlen, oflags, ncores; 3745c5edb423SSean Eric Fagan 3746b7402d82SAlfred Perlstein hostname = NULL; 37478b43b535SAlfred Perlstein format = corefilename; 3748086053a3SPawel Jakub Dawidek name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3749f1fe1e02SMariusz Zaborski indexlen = 0; 3750e7228204SAlfred Perlstein indexpos = -1; 3751f1fe1e02SMariusz Zaborski ncores = num_cores; 3752c52ff611SPawel Jakub Dawidek (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 37535bc0ff88SMateusz Guzik sx_slock(&corefilename_lock); 375429146f1aSPawel Jakub Dawidek for (i = 0; format[i] != '\0'; i++) { 3755c5edb423SSean Eric Fagan switch (format[i]) { 3756c5edb423SSean Eric Fagan case '%': /* Format character */ 3757c5edb423SSean Eric Fagan i++; 3758c5edb423SSean Eric Fagan switch (format[i]) { 3759c5edb423SSean Eric Fagan case '%': 376036b208e0SRobert Watson sbuf_putc(&sb, '%'); 3761c5edb423SSean Eric Fagan break; 3762e7228204SAlfred Perlstein case 'H': /* hostname */ 3763b7402d82SAlfred Perlstein if (hostname == NULL) { 3764b7402d82SAlfred Perlstein hostname = malloc(MAXHOSTNAMELEN, 3765086053a3SPawel Jakub Dawidek M_TEMP, M_WAITOK); 3766b7402d82SAlfred Perlstein } 3767e7228204SAlfred Perlstein getcredhostname(td->td_ucred, hostname, 3768b7402d82SAlfred Perlstein MAXHOSTNAMELEN); 3769e7228204SAlfred Perlstein sbuf_printf(&sb, "%s", hostname); 3770e7228204SAlfred Perlstein break; 3771e7228204SAlfred Perlstein case 'I': /* autoincrementing index */ 3772f1fe1e02SMariusz Zaborski if (indexpos != -1) { 3773f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%%I"); 3774f1fe1e02SMariusz Zaborski break; 3775f1fe1e02SMariusz Zaborski } 3776f1fe1e02SMariusz Zaborski 3777f1fe1e02SMariusz Zaborski indexpos = sbuf_len(&sb); 3778f1fe1e02SMariusz Zaborski sbuf_printf(&sb, "%u", ncores - 1); 3779f1fe1e02SMariusz Zaborski indexlen = sbuf_len(&sb) - indexpos; 3780e7228204SAlfred Perlstein break; 3781c5edb423SSean Eric Fagan case 'N': /* process name */ 3782c52ff611SPawel Jakub Dawidek sbuf_printf(&sb, "%s", comm); 3783c5edb423SSean Eric Fagan break; 3784c5edb423SSean Eric Fagan case 'P': /* process id */ 378536b208e0SRobert Watson sbuf_printf(&sb, "%u", pid); 3786c5edb423SSean Eric Fagan break; 37879d3ecb7eSEric van Gyzen case 'S': /* signal number */ 37889d3ecb7eSEric van Gyzen sbuf_printf(&sb, "%i", signum); 37899d3ecb7eSEric van Gyzen break; 3790c5edb423SSean Eric Fagan case 'U': /* user id */ 379136b208e0SRobert Watson sbuf_printf(&sb, "%u", uid); 3792c5edb423SSean Eric Fagan break; 3793c5edb423SSean Eric Fagan default: 37948b43b535SAlfred Perlstein log(LOG_ERR, 379536b208e0SRobert Watson "Unknown format character %c in " 379636b208e0SRobert Watson "corename `%s'\n", format[i], format); 379729146f1aSPawel Jakub Dawidek break; 3798c5edb423SSean Eric Fagan } 3799c5edb423SSean Eric Fagan break; 3800c5edb423SSean Eric Fagan default: 380136b208e0SRobert Watson sbuf_putc(&sb, format[i]); 38026c08be2bSPawel Jakub Dawidek break; 3803c5edb423SSean Eric Fagan } 3804c5edb423SSean Eric Fagan } 38055bc0ff88SMateusz Guzik sx_sunlock(&corefilename_lock); 3806b7402d82SAlfred Perlstein free(hostname, M_TEMP); 380778f57a9cSMark Johnston if (compress == COMPRESS_GZIP) 38086026dcd7SMark Johnston sbuf_printf(&sb, GZIP_SUFFIX); 38096026dcd7SMark Johnston else if (compress == COMPRESS_ZSTD) 38106026dcd7SMark Johnston sbuf_printf(&sb, ZSTD_SUFFIX); 38114d369413SMatthew D Fleming if (sbuf_error(&sb) != 0) { 381236b208e0SRobert Watson log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3813c52ff611SPawel Jakub Dawidek "long\n", (long)pid, comm, (u_long)uid); 3814b7402d82SAlfred Perlstein sbuf_delete(&sb); 3815c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3816c345faeaSPawel Jakub Dawidek return (ENOMEM); 3817c5edb423SSean Eric Fagan } 381836b208e0SRobert Watson sbuf_finish(&sb); 381936b208e0SRobert Watson sbuf_delete(&sb); 3820e7228204SAlfred Perlstein 3821e7228204SAlfred Perlstein if (indexpos != -1) { 3822f1fe1e02SMariusz Zaborski error = corefile_open_last(td, name, indexpos, indexlen, ncores, 3823f1fe1e02SMariusz Zaborski vpp); 38240dea6e3cSMariusz Zaborski if (error != 0) { 3825e7228204SAlfred Perlstein log(LOG_ERR, 3826e7228204SAlfred Perlstein "pid %d (%s), uid (%u): Path `%s' failed " 3827e7228204SAlfred Perlstein "on initial open test, error = %d\n", 3828c52ff611SPawel Jakub Dawidek pid, comm, uid, name, error); 3829c345faeaSPawel Jakub Dawidek } 38300dea6e3cSMariusz Zaborski } else { 38310dea6e3cSMariusz Zaborski cmode = S_IRUSR | S_IWUSR; 38320dea6e3cSMariusz Zaborski oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 38330dea6e3cSMariusz Zaborski (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 38340dea6e3cSMariusz Zaborski flags = O_CREAT | FWRITE | O_NOFOLLOW; 383589f2ab06SKonstantin Belousov if ((td->td_proc->p_flag & P_SUGID) != 0) 383689f2ab06SKonstantin Belousov flags |= O_EXCL; 38370dea6e3cSMariusz Zaborski 38387e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name); 38390dea6e3cSMariusz Zaborski error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, 38400dea6e3cSMariusz Zaborski NULL); 38410dea6e3cSMariusz Zaborski if (error == 0) { 38420dea6e3cSMariusz Zaborski *vpp = nd.ni_vp; 3843bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 3844c345faeaSPawel Jakub Dawidek } 3845c345faeaSPawel Jakub Dawidek } 3846c345faeaSPawel Jakub Dawidek 38470dea6e3cSMariusz Zaborski if (error != 0) { 3848c345faeaSPawel Jakub Dawidek #ifdef AUDIT 3849c345faeaSPawel Jakub Dawidek audit_proc_coredump(td, name, error); 3850c345faeaSPawel Jakub Dawidek #endif 3851c52ff611SPawel Jakub Dawidek free(name, M_TEMP); 3852c345faeaSPawel Jakub Dawidek return (error); 3853e7228204SAlfred Perlstein } 3854c345faeaSPawel Jakub Dawidek *namep = name; 3855c345faeaSPawel Jakub Dawidek return (0); 385636b208e0SRobert Watson } 3857c5edb423SSean Eric Fagan 3858df8bae1dSRodney W. Grimes /* 3859fca666a1SJulian Elischer * Dump a process' core. The main routine does some 3860fca666a1SJulian Elischer * policy checking, and creates the name of the coredump; 3861fca666a1SJulian Elischer * then it passes on a vnode and a size limit to the process-specific 3862fca666a1SJulian Elischer * coredump routine if there is one; if there _is not_ one, it returns 3863fca666a1SJulian Elischer * ENOSYS; otherwise it returns the error from the process-specific routine. 3864fca666a1SJulian Elischer */ 3865fca666a1SJulian Elischer 3866fca666a1SJulian Elischer static int 3867b40ce416SJulian Elischer coredump(struct thread *td) 3868fca666a1SJulian Elischer { 3869b40ce416SJulian Elischer struct proc *p = td->td_proc; 3870f06f465dSPawel Jakub Dawidek struct ucred *cred = td->td_ucred; 3871f06f465dSPawel Jakub Dawidek struct vnode *vp; 387206ae1e91SMatthew Dillon struct flock lf; 3873fca666a1SJulian Elischer struct vattr vattr; 38747739d927SMateusz Guzik size_t fullpathsize; 3875c345faeaSPawel Jakub Dawidek int error, error1, locked; 3876fca666a1SJulian Elischer char *name; /* name of corefile */ 3877539c9eefSKonstantin Belousov void *rl_cookie; 3878fca666a1SJulian Elischer off_t limit; 3879842ab62bSRui Paulo char *fullpath, *freepath = NULL; 3880349fcda4SWarner Losh struct sbuf *sb; 3881fca666a1SJulian Elischer 38824ae89b95SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3883f97c3df1SDavid Schultz MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3884fca666a1SJulian Elischer 3885677258f7SKonstantin Belousov if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 3886677258f7SKonstantin Belousov (p->p_flag2 & P2_NOTRACE) != 0) { 3887628d2653SJohn Baldwin PROC_UNLOCK(p); 3888fca666a1SJulian Elischer return (EFAULT); 3889628d2653SJohn Baldwin } 3890fca666a1SJulian Elischer 3891fca666a1SJulian Elischer /* 389235a2598fSSean Eric Fagan * Note that the bulk of limit checking is done after 389335a2598fSSean Eric Fagan * the corefile is created. The exception is if the limit 389435a2598fSSean Eric Fagan * for corefiles is 0, in which case we don't bother 389535a2598fSSean Eric Fagan * creating the corefile at all. This layout means that 389635a2598fSSean Eric Fagan * a corefile is truncated instead of not being created, 389735a2598fSSean Eric Fagan * if it is larger than the limit. 3898fca666a1SJulian Elischer */ 3899f6f6d240SMateusz Guzik limit = (off_t)lim_cur(td, RLIMIT_CORE); 3900b8fdb0d9SEdward Tomasz Napierala if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3901628d2653SJohn Baldwin PROC_UNLOCK(p); 390291d5354aSJohn Baldwin return (EFBIG); 390357274c51SChristian S.J. Peron } 3904b8fdb0d9SEdward Tomasz Napierala PROC_UNLOCK(p); 390535a2598fSSean Eric Fagan 3906aa14e9b7SMark Johnston error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 39079d3ecb7eSEric van Gyzen compress_user_cores, p->p_sig, &vp, &name); 3908c345faeaSPawel Jakub Dawidek if (error != 0) 3909fca666a1SJulian Elischer return (error); 391006ae1e91SMatthew Dillon 3911539c9eefSKonstantin Belousov /* 3912539c9eefSKonstantin Belousov * Don't dump to non-regular files or files with links. 391389f2ab06SKonstantin Belousov * Do not dump into system files. Effective user must own the corefile. 3914539c9eefSKonstantin Belousov */ 3915f06f465dSPawel Jakub Dawidek if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 39167a29e0bfSKonstantin Belousov vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 || 391789f2ab06SKonstantin Belousov vattr.va_uid != cred->cr_uid) { 3918b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3919832dafadSDon Lewis error = EFAULT; 3920dacbc9dbSKonstantin Belousov goto out; 3921832dafadSDon Lewis } 3922832dafadSDon Lewis 3923b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3924539c9eefSKonstantin Belousov 3925539c9eefSKonstantin Belousov /* Postpone other writers, including core dumps of other processes. */ 3926539c9eefSKonstantin Belousov rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3927539c9eefSKonstantin Belousov 392806ae1e91SMatthew Dillon lf.l_whence = SEEK_SET; 392906ae1e91SMatthew Dillon lf.l_start = 0; 393006ae1e91SMatthew Dillon lf.l_len = 0; 393106ae1e91SMatthew Dillon lf.l_type = F_WRLCK; 3932c447f5b2SRobert Watson locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 393306ae1e91SMatthew Dillon 3934fca666a1SJulian Elischer VATTR_NULL(&vattr); 3935fca666a1SJulian Elischer vattr.va_size = 0; 39366141e04aSJohn-Mark Gurney if (set_core_nodump_flag) 39376141e04aSJohn-Mark Gurney vattr.va_flags = UF_NODUMP; 3938cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 39390359a12eSAttilio Rao VOP_SETATTR(vp, &vattr, cred); 3940b249ce48SMateusz Guzik VOP_UNLOCK(vp); 3941628d2653SJohn Baldwin PROC_LOCK(p); 3942fca666a1SJulian Elischer p->p_acflag |= ACORE; 3943628d2653SJohn Baldwin PROC_UNLOCK(p); 3944fca666a1SJulian Elischer 394523c6445aSPawel Jakub Dawidek if (p->p_sysent->sv_coredump != NULL) { 394678f57a9cSMark Johnston error = p->p_sysent->sv_coredump(td, vp, limit, 0); 394723c6445aSPawel Jakub Dawidek } else { 394823c6445aSPawel Jakub Dawidek error = ENOSYS; 394923c6445aSPawel Jakub Dawidek } 3950fca666a1SJulian Elischer 3951c447f5b2SRobert Watson if (locked) { 395206ae1e91SMatthew Dillon lf.l_type = F_UNLCK; 395306ae1e91SMatthew Dillon VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3954c447f5b2SRobert Watson } 3955539c9eefSKonstantin Belousov vn_rangelock_unlock(vp, rl_cookie); 3956dacbc9dbSKonstantin Belousov 3957842ab62bSRui Paulo /* 3958842ab62bSRui Paulo * Notify the userland helper that a process triggered a core dump. 3959842ab62bSRui Paulo * This allows the helper to run an automated debugging session. 3960842ab62bSRui Paulo */ 3961dacbc9dbSKonstantin Belousov if (error != 0 || coredump_devctl == 0) 3962842ab62bSRui Paulo goto out; 3963349fcda4SWarner Losh sb = sbuf_new_auto(); 3964feabaaf9SMateusz Guzik if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0) 3965349fcda4SWarner Losh goto out2; 3966349fcda4SWarner Losh sbuf_printf(sb, "comm=\""); 3967349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 3968842ab62bSRui Paulo free(freepath, M_TEMP); 3969349fcda4SWarner Losh sbuf_printf(sb, "\" core=\""); 3970349fcda4SWarner Losh 3971349fcda4SWarner Losh /* 3972349fcda4SWarner Losh * We can't lookup core file vp directly. When we're replacing a core, and 3973349fcda4SWarner Losh * other random times, we flush the name cache, so it will fail. Instead, 3974349fcda4SWarner Losh * if the path of the core is relative, add the current dir in front if it. 3975349fcda4SWarner Losh */ 3976349fcda4SWarner Losh if (name[0] != '/') { 39777739d927SMateusz Guzik fullpathsize = MAXPATHLEN; 39787739d927SMateusz Guzik freepath = malloc(fullpathsize, M_TEMP, M_WAITOK); 3979feabaaf9SMateusz Guzik if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) { 39807739d927SMateusz Guzik free(freepath, M_TEMP); 3981349fcda4SWarner Losh goto out2; 3982349fcda4SWarner Losh } 3983349fcda4SWarner Losh devctl_safe_quote_sb(sb, fullpath); 39847739d927SMateusz Guzik free(freepath, M_TEMP); 3985349fcda4SWarner Losh sbuf_putc(sb, '/'); 3986349fcda4SWarner Losh } 3987349fcda4SWarner Losh devctl_safe_quote_sb(sb, name); 3988349fcda4SWarner Losh sbuf_printf(sb, "\""); 3989349fcda4SWarner Losh if (sbuf_finish(sb) == 0) 3990349fcda4SWarner Losh devctl_notify("kernel", "signal", "coredump", sbuf_data(sb)); 3991349fcda4SWarner Losh out2: 3992349fcda4SWarner Losh sbuf_delete(sb); 3993842ab62bSRui Paulo out: 3994dacbc9dbSKonstantin Belousov error1 = vn_close(vp, FWRITE, cred, td); 3995dacbc9dbSKonstantin Belousov if (error == 0) 3996dacbc9dbSKonstantin Belousov error = error1; 399757274c51SChristian S.J. Peron #ifdef AUDIT 399857274c51SChristian S.J. Peron audit_proc_coredump(td, name, error); 399957274c51SChristian S.J. Peron #endif 400057274c51SChristian S.J. Peron free(name, M_TEMP); 4001fca666a1SJulian Elischer return (error); 4002fca666a1SJulian Elischer } 4003fca666a1SJulian Elischer 4004fca666a1SJulian Elischer /* 40050c14ff0eSRobert Watson * Nonexistent system call-- signal process (may want to handle it). Flag 40060c14ff0eSRobert Watson * error in case process won't see signal immediately (blocked or ignored). 4007df8bae1dSRodney W. Grimes */ 4008d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 4009df8bae1dSRodney W. Grimes struct nosys_args { 4010df8bae1dSRodney W. Grimes int dummy; 4011df8bae1dSRodney W. Grimes }; 4012d2d3e875SBruce Evans #endif 4013df8bae1dSRodney W. Grimes /* ARGSUSED */ 401426f9a767SRodney W. Grimes int 4015e052a8b9SEd Maste nosys(struct thread *td, struct nosys_args *args) 4016df8bae1dSRodney W. Grimes { 4017f5a077c3SKonstantin Belousov struct proc *p; 4018f5a077c3SKonstantin Belousov 4019f5a077c3SKonstantin Belousov p = td->td_proc; 4020b40ce416SJulian Elischer 4021628d2653SJohn Baldwin PROC_LOCK(p); 4022888aefefSKonstantin Belousov tdsignal(td, SIGSYS); 4023628d2653SJohn Baldwin PROC_UNLOCK(p); 40247ceeb35bSKonstantin Belousov if (kern_lognosys == 1 || kern_lognosys == 3) { 4025f5a077c3SKonstantin Belousov uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 4026f5a077c3SKonstantin Belousov td->td_sa.code); 40277ceeb35bSKonstantin Belousov } 402818f917a9SBrooks Davis if (kern_lognosys == 2 || kern_lognosys == 3 || 402918f917a9SBrooks Davis (p->p_pid == 1 && (kern_lognosys & 3) == 0)) { 40307ceeb35bSKonstantin Belousov printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 40317ceeb35bSKonstantin Belousov td->td_sa.code); 40327ceeb35bSKonstantin Belousov } 4033f5216b9aSBruce Evans return (ENOSYS); 4034df8bae1dSRodney W. Grimes } 4035831d27a9SDon Lewis 4036831d27a9SDon Lewis /* 40370c14ff0eSRobert Watson * Send a SIGIO or SIGURG signal to a process or process group using stored 40380c14ff0eSRobert Watson * credentials rather than those of the current process. 4039831d27a9SDon Lewis */ 4040831d27a9SDon Lewis void 4041e052a8b9SEd Maste pgsigio(struct sigio **sigiop, int sig, int checkctty) 4042831d27a9SDon Lewis { 4043a3de221dSKonstantin Belousov ksiginfo_t ksi; 4044f1320723SAlfred Perlstein struct sigio *sigio; 4045831d27a9SDon Lewis 4046a3de221dSKonstantin Belousov ksiginfo_init(&ksi); 4047a3de221dSKonstantin Belousov ksi.ksi_signo = sig; 4048a3de221dSKonstantin Belousov ksi.ksi_code = SI_KERNEL; 4049a3de221dSKonstantin Belousov 4050f1320723SAlfred Perlstein SIGIO_LOCK(); 4051f1320723SAlfred Perlstein sigio = *sigiop; 4052f1320723SAlfred Perlstein if (sigio == NULL) { 4053f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4054f1320723SAlfred Perlstein return; 4055f1320723SAlfred Perlstein } 4056831d27a9SDon Lewis if (sigio->sio_pgid > 0) { 4057628d2653SJohn Baldwin PROC_LOCK(sigio->sio_proc); 40582b87b6d4SRobert Watson if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 40598451d0ddSKip Macy kern_psignal(sigio->sio_proc, sig); 4060628d2653SJohn Baldwin PROC_UNLOCK(sigio->sio_proc); 4061831d27a9SDon Lewis } else if (sigio->sio_pgid < 0) { 4062831d27a9SDon Lewis struct proc *p; 4063831d27a9SDon Lewis 4064f591779bSSeigo Tanimura PGRP_LOCK(sigio->sio_pgrp); 4065628d2653SJohn Baldwin LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 4066628d2653SJohn Baldwin PROC_LOCK(p); 4067e806d352SJohn Baldwin if (p->p_state == PRS_NORMAL && 4068e806d352SJohn Baldwin CANSIGIO(sigio->sio_ucred, p->p_ucred) && 4069831d27a9SDon Lewis (checkctty == 0 || (p->p_flag & P_CONTROLT))) 40708451d0ddSKip Macy kern_psignal(p, sig); 4071628d2653SJohn Baldwin PROC_UNLOCK(p); 4072628d2653SJohn Baldwin } 4073f591779bSSeigo Tanimura PGRP_UNLOCK(sigio->sio_pgrp); 4074831d27a9SDon Lewis } 4075f1320723SAlfred Perlstein SIGIO_UNLOCK(); 4076831d27a9SDon Lewis } 4077cb679c38SJonathan Lemon 4078cb679c38SJonathan Lemon static int 4079cb679c38SJonathan Lemon filt_sigattach(struct knote *kn) 4080cb679c38SJonathan Lemon { 4081cb679c38SJonathan Lemon struct proc *p = curproc; 4082cb679c38SJonathan Lemon 4083cb679c38SJonathan Lemon kn->kn_ptr.p_proc = p; 4084cb679c38SJonathan Lemon kn->kn_flags |= EV_CLEAR; /* automatically set */ 4085cb679c38SJonathan Lemon 40869e590ff0SKonstantin Belousov knlist_add(p->p_klist, kn, 0); 4087cb679c38SJonathan Lemon 4088cb679c38SJonathan Lemon return (0); 4089cb679c38SJonathan Lemon } 4090cb679c38SJonathan Lemon 4091cb679c38SJonathan Lemon static void 4092cb679c38SJonathan Lemon filt_sigdetach(struct knote *kn) 4093cb679c38SJonathan Lemon { 4094cb679c38SJonathan Lemon struct proc *p = kn->kn_ptr.p_proc; 4095cb679c38SJonathan Lemon 40969e590ff0SKonstantin Belousov knlist_remove(p->p_klist, kn, 0); 4097cb679c38SJonathan Lemon } 4098cb679c38SJonathan Lemon 4099cb679c38SJonathan Lemon /* 4100cb679c38SJonathan Lemon * signal knotes are shared with proc knotes, so we apply a mask to 4101cb679c38SJonathan Lemon * the hint in order to differentiate them from process hints. This 4102cb679c38SJonathan Lemon * could be avoided by using a signal-specific knote list, but probably 4103cb679c38SJonathan Lemon * isn't worth the trouble. 4104cb679c38SJonathan Lemon */ 4105cb679c38SJonathan Lemon static int 4106cb679c38SJonathan Lemon filt_signal(struct knote *kn, long hint) 4107cb679c38SJonathan Lemon { 4108cb679c38SJonathan Lemon 4109cb679c38SJonathan Lemon if (hint & NOTE_SIGNAL) { 4110cb679c38SJonathan Lemon hint &= ~NOTE_SIGNAL; 4111cb679c38SJonathan Lemon 4112cb679c38SJonathan Lemon if (kn->kn_id == hint) 4113cb679c38SJonathan Lemon kn->kn_data++; 4114cb679c38SJonathan Lemon } 4115cb679c38SJonathan Lemon return (kn->kn_data != 0); 4116cb679c38SJonathan Lemon } 411790af4afaSJohn Baldwin 411890af4afaSJohn Baldwin struct sigacts * 411990af4afaSJohn Baldwin sigacts_alloc(void) 412090af4afaSJohn Baldwin { 412190af4afaSJohn Baldwin struct sigacts *ps; 412290af4afaSJohn Baldwin 412390af4afaSJohn Baldwin ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 4124ce8daaadSMateusz Guzik refcount_init(&ps->ps_refcnt, 1); 412590af4afaSJohn Baldwin mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 412690af4afaSJohn Baldwin return (ps); 412790af4afaSJohn Baldwin } 412890af4afaSJohn Baldwin 412990af4afaSJohn Baldwin void 413090af4afaSJohn Baldwin sigacts_free(struct sigacts *ps) 413190af4afaSJohn Baldwin { 413290af4afaSJohn Baldwin 4133c959c237SMateusz Guzik if (refcount_release(&ps->ps_refcnt) == 0) 4134c959c237SMateusz Guzik return; 413590af4afaSJohn Baldwin mtx_destroy(&ps->ps_mtx); 413690af4afaSJohn Baldwin free(ps, M_SUBPROC); 413790af4afaSJohn Baldwin } 413890af4afaSJohn Baldwin 413990af4afaSJohn Baldwin struct sigacts * 414090af4afaSJohn Baldwin sigacts_hold(struct sigacts *ps) 414190af4afaSJohn Baldwin { 4142c959c237SMateusz Guzik 4143c959c237SMateusz Guzik refcount_acquire(&ps->ps_refcnt); 414490af4afaSJohn Baldwin return (ps); 414590af4afaSJohn Baldwin } 414690af4afaSJohn Baldwin 414790af4afaSJohn Baldwin void 414890af4afaSJohn Baldwin sigacts_copy(struct sigacts *dest, struct sigacts *src) 414990af4afaSJohn Baldwin { 415090af4afaSJohn Baldwin 415190af4afaSJohn Baldwin KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 415290af4afaSJohn Baldwin mtx_lock(&src->ps_mtx); 415390af4afaSJohn Baldwin bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 415490af4afaSJohn Baldwin mtx_unlock(&src->ps_mtx); 415590af4afaSJohn Baldwin } 415690af4afaSJohn Baldwin 415790af4afaSJohn Baldwin int 415890af4afaSJohn Baldwin sigacts_shared(struct sigacts *ps) 415990af4afaSJohn Baldwin { 416090af4afaSJohn Baldwin 4161d00c8ea4SMateusz Guzik return (ps->ps_refcnt > 1); 416290af4afaSJohn Baldwin } 4163079c5b9eSKyle Evans 4164079c5b9eSKyle Evans void 4165079c5b9eSKyle Evans sig_drop_caught(struct proc *p) 4166079c5b9eSKyle Evans { 4167079c5b9eSKyle Evans int sig; 4168079c5b9eSKyle Evans struct sigacts *ps; 4169079c5b9eSKyle Evans 4170079c5b9eSKyle Evans ps = p->p_sigacts; 4171079c5b9eSKyle Evans PROC_LOCK_ASSERT(p, MA_OWNED); 4172079c5b9eSKyle Evans mtx_assert(&ps->ps_mtx, MA_OWNED); 417381f2e906SMark Johnston SIG_FOREACH(sig, &ps->ps_sigcatch) { 4174079c5b9eSKyle Evans sigdflt(ps, sig); 4175079c5b9eSKyle Evans if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 4176079c5b9eSKyle Evans sigqueue_delete_proc(p, sig); 4177079c5b9eSKyle Evans } 4178079c5b9eSKyle Evans } 4179146fc63fSKonstantin Belousov 4180a113b17fSKonstantin Belousov static void 4181a113b17fSKonstantin Belousov sigfastblock_failed(struct thread *td, bool sendsig, bool write) 4182a113b17fSKonstantin Belousov { 4183a113b17fSKonstantin Belousov ksiginfo_t ksi; 4184a113b17fSKonstantin Belousov 4185a113b17fSKonstantin Belousov /* 4186a113b17fSKonstantin Belousov * Prevent further fetches and SIGSEGVs, allowing thread to 4187a113b17fSKonstantin Belousov * issue syscalls despite corruption. 4188a113b17fSKonstantin Belousov */ 4189a113b17fSKonstantin Belousov sigfastblock_clear(td); 4190a113b17fSKonstantin Belousov 4191a113b17fSKonstantin Belousov if (!sendsig) 4192a113b17fSKonstantin Belousov return; 4193a113b17fSKonstantin Belousov ksiginfo_init_trap(&ksi); 4194a113b17fSKonstantin Belousov ksi.ksi_signo = SIGSEGV; 4195a113b17fSKonstantin Belousov ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR; 4196a113b17fSKonstantin Belousov ksi.ksi_addr = td->td_sigblock_ptr; 4197a113b17fSKonstantin Belousov trapsignal(td, &ksi); 4198a113b17fSKonstantin Belousov } 4199a113b17fSKonstantin Belousov 4200a113b17fSKonstantin Belousov static bool 4201a113b17fSKonstantin Belousov sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp) 4202a113b17fSKonstantin Belousov { 4203a113b17fSKonstantin Belousov uint32_t res; 4204a113b17fSKonstantin Belousov 4205a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4206a113b17fSKonstantin Belousov return (true); 4207a113b17fSKonstantin Belousov if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) { 4208a113b17fSKonstantin Belousov sigfastblock_failed(td, sendsig, false); 4209a113b17fSKonstantin Belousov return (false); 4210a113b17fSKonstantin Belousov } 4211a113b17fSKonstantin Belousov *valp = res; 4212a113b17fSKonstantin Belousov td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS; 4213a113b17fSKonstantin Belousov return (true); 4214a113b17fSKonstantin Belousov } 4215a113b17fSKonstantin Belousov 4216fb3c434bSKonstantin Belousov static void 4217fb3c434bSKonstantin Belousov sigfastblock_resched(struct thread *td, bool resched) 4218fb3c434bSKonstantin Belousov { 4219fb3c434bSKonstantin Belousov struct proc *p; 4220fb3c434bSKonstantin Belousov 4221fb3c434bSKonstantin Belousov if (resched) { 4222fb3c434bSKonstantin Belousov p = td->td_proc; 4223fb3c434bSKonstantin Belousov PROC_LOCK(p); 4224fb3c434bSKonstantin Belousov reschedule_signals(p, td->td_sigmask, 0); 4225fb3c434bSKonstantin Belousov PROC_UNLOCK(p); 4226fb3c434bSKonstantin Belousov } 4227fb3c434bSKonstantin Belousov thread_lock(td); 4228fb3c434bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING | TDF_NEEDSIGCHK; 4229fb3c434bSKonstantin Belousov thread_unlock(td); 4230fb3c434bSKonstantin Belousov } 4231fb3c434bSKonstantin Belousov 4232146fc63fSKonstantin Belousov int 4233146fc63fSKonstantin Belousov sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap) 4234146fc63fSKonstantin Belousov { 4235146fc63fSKonstantin Belousov struct proc *p; 4236146fc63fSKonstantin Belousov int error, res; 4237146fc63fSKonstantin Belousov uint32_t oldval; 4238146fc63fSKonstantin Belousov 4239146fc63fSKonstantin Belousov error = 0; 4240a113b17fSKonstantin Belousov p = td->td_proc; 4241146fc63fSKonstantin Belousov switch (uap->cmd) { 4242146fc63fSKonstantin Belousov case SIGFASTBLOCK_SETPTR: 4243146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 4244146fc63fSKonstantin Belousov error = EBUSY; 4245146fc63fSKonstantin Belousov break; 4246146fc63fSKonstantin Belousov } 4247146fc63fSKonstantin Belousov if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) { 4248146fc63fSKonstantin Belousov error = EINVAL; 4249146fc63fSKonstantin Belousov break; 4250146fc63fSKonstantin Belousov } 4251146fc63fSKonstantin Belousov td->td_pflags |= TDP_SIGFASTBLOCK; 4252146fc63fSKonstantin Belousov td->td_sigblock_ptr = uap->ptr; 4253146fc63fSKonstantin Belousov break; 4254146fc63fSKonstantin Belousov 4255146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNBLOCK: 4256a113b17fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4257146fc63fSKonstantin Belousov error = EINVAL; 4258146fc63fSKonstantin Belousov break; 4259146fc63fSKonstantin Belousov } 4260a113b17fSKonstantin Belousov 4261a113b17fSKonstantin Belousov for (;;) { 4262a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, 4263a113b17fSKonstantin Belousov SIGFASTBLOCK_PEND, &oldval, 0); 4264146fc63fSKonstantin Belousov if (res == -1) { 4265146fc63fSKonstantin Belousov error = EFAULT; 4266a113b17fSKonstantin Belousov sigfastblock_failed(td, false, true); 4267146fc63fSKonstantin Belousov break; 4268146fc63fSKonstantin Belousov } 4269a113b17fSKonstantin Belousov if (res == 0) 4270a113b17fSKonstantin Belousov break; 4271a113b17fSKonstantin Belousov MPASS(res == 1); 4272146fc63fSKonstantin Belousov if (oldval != SIGFASTBLOCK_PEND) { 4273146fc63fSKonstantin Belousov error = EBUSY; 4274146fc63fSKonstantin Belousov break; 4275146fc63fSKonstantin Belousov } 4276146fc63fSKonstantin Belousov error = thread_check_susp(td, false); 4277146fc63fSKonstantin Belousov if (error != 0) 4278146fc63fSKonstantin Belousov break; 4279146fc63fSKonstantin Belousov } 4280a113b17fSKonstantin Belousov if (error != 0) 4281a113b17fSKonstantin Belousov break; 4282a113b17fSKonstantin Belousov 4283a113b17fSKonstantin Belousov /* 4284a113b17fSKonstantin Belousov * td_sigblock_val is cleared there, but not on a 4285a113b17fSKonstantin Belousov * syscall exit. The end effect is that a single 4286a113b17fSKonstantin Belousov * interruptible sleep, while user sigblock word is 4287a113b17fSKonstantin Belousov * set, might return EINTR or ERESTART to usermode 4288a113b17fSKonstantin Belousov * without delivering signal. All further sleeps, 4289a113b17fSKonstantin Belousov * until userspace clears the word and does 4290a113b17fSKonstantin Belousov * sigfastblock(UNBLOCK), observe current word and no 4291a113b17fSKonstantin Belousov * longer get interrupted. It is slight 4292a113b17fSKonstantin Belousov * non-conformance, with alternative to have read the 4293a113b17fSKonstantin Belousov * sigblock word on each syscall entry. 4294a113b17fSKonstantin Belousov */ 4295146fc63fSKonstantin Belousov td->td_sigblock_val = 0; 4296146fc63fSKonstantin Belousov 4297146fc63fSKonstantin Belousov /* 4298146fc63fSKonstantin Belousov * Rely on normal ast mechanism to deliver pending 4299146fc63fSKonstantin Belousov * signals to current thread. But notify others about 4300146fc63fSKonstantin Belousov * fake unblock. 4301146fc63fSKonstantin Belousov */ 4302fb3c434bSKonstantin Belousov sigfastblock_resched(td, error == 0 && p->p_numthreads != 1); 4303fb3c434bSKonstantin Belousov 4304146fc63fSKonstantin Belousov break; 4305146fc63fSKonstantin Belousov 4306146fc63fSKonstantin Belousov case SIGFASTBLOCK_UNSETPTR: 4307146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4308146fc63fSKonstantin Belousov error = EINVAL; 4309146fc63fSKonstantin Belousov break; 4310146fc63fSKonstantin Belousov } 4311a113b17fSKonstantin Belousov if (!sigfastblock_fetch_sig(td, false, &oldval)) { 4312146fc63fSKonstantin Belousov error = EFAULT; 4313146fc63fSKonstantin Belousov break; 4314146fc63fSKonstantin Belousov } 4315146fc63fSKonstantin Belousov if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) { 4316146fc63fSKonstantin Belousov error = EBUSY; 4317146fc63fSKonstantin Belousov break; 4318146fc63fSKonstantin Belousov } 4319a113b17fSKonstantin Belousov sigfastblock_clear(td); 4320146fc63fSKonstantin Belousov break; 4321146fc63fSKonstantin Belousov 4322146fc63fSKonstantin Belousov default: 4323146fc63fSKonstantin Belousov error = EINVAL; 4324146fc63fSKonstantin Belousov break; 4325146fc63fSKonstantin Belousov } 4326146fc63fSKonstantin Belousov return (error); 4327146fc63fSKonstantin Belousov } 4328146fc63fSKonstantin Belousov 4329146fc63fSKonstantin Belousov void 4330a113b17fSKonstantin Belousov sigfastblock_clear(struct thread *td) 4331146fc63fSKonstantin Belousov { 4332a113b17fSKonstantin Belousov bool resched; 4333146fc63fSKonstantin Belousov 4334146fc63fSKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4335146fc63fSKonstantin Belousov return; 4336a113b17fSKonstantin Belousov td->td_sigblock_val = 0; 433700ebd809SKonstantin Belousov resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 || 433800ebd809SKonstantin Belousov SIGPENDING(td); 4339a113b17fSKonstantin Belousov td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING); 4340fb3c434bSKonstantin Belousov sigfastblock_resched(td, resched); 4341146fc63fSKonstantin Belousov } 4342146fc63fSKonstantin Belousov 4343146fc63fSKonstantin Belousov void 4344a113b17fSKonstantin Belousov sigfastblock_fetch(struct thread *td) 4345146fc63fSKonstantin Belousov { 4346a113b17fSKonstantin Belousov uint32_t val; 4347146fc63fSKonstantin Belousov 4348a113b17fSKonstantin Belousov (void)sigfastblock_fetch_sig(td, true, &val); 4349a113b17fSKonstantin Belousov } 4350146fc63fSKonstantin Belousov 43510bc52b0bSKonstantin Belousov static void 43520bc52b0bSKonstantin Belousov sigfastblock_setpend1(struct thread *td) 4353a113b17fSKonstantin Belousov { 4354a113b17fSKonstantin Belousov int res; 4355a113b17fSKonstantin Belousov uint32_t oldval; 4356a113b17fSKonstantin Belousov 4357513320c0SKonstantin Belousov if ((td->td_pflags & TDP_SIGFASTPENDING) == 0) 4358a113b17fSKonstantin Belousov return; 4359a113b17fSKonstantin Belousov res = fueword32((void *)td->td_sigblock_ptr, &oldval); 4360a113b17fSKonstantin Belousov if (res == -1) { 4361a113b17fSKonstantin Belousov sigfastblock_failed(td, true, false); 4362a113b17fSKonstantin Belousov return; 4363a113b17fSKonstantin Belousov } 4364a113b17fSKonstantin Belousov for (;;) { 4365a113b17fSKonstantin Belousov res = casueword32(td->td_sigblock_ptr, oldval, &oldval, 4366a113b17fSKonstantin Belousov oldval | SIGFASTBLOCK_PEND); 4367a113b17fSKonstantin Belousov if (res == -1) { 4368a113b17fSKonstantin Belousov sigfastblock_failed(td, true, true); 4369a113b17fSKonstantin Belousov return; 4370a113b17fSKonstantin Belousov } 4371a113b17fSKonstantin Belousov if (res == 0) { 4372a113b17fSKonstantin Belousov td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS; 4373a113b17fSKonstantin Belousov td->td_pflags &= ~TDP_SIGFASTPENDING; 4374a113b17fSKonstantin Belousov break; 4375a113b17fSKonstantin Belousov } 4376a113b17fSKonstantin Belousov MPASS(res == 1); 4377a113b17fSKonstantin Belousov if (thread_check_susp(td, false) != 0) 4378a113b17fSKonstantin Belousov break; 4379a113b17fSKonstantin Belousov } 4380146fc63fSKonstantin Belousov } 43810bc52b0bSKonstantin Belousov 43820bc52b0bSKonstantin Belousov void 43830bc52b0bSKonstantin Belousov sigfastblock_setpend(struct thread *td, bool resched) 43840bc52b0bSKonstantin Belousov { 43850bc52b0bSKonstantin Belousov struct proc *p; 43860bc52b0bSKonstantin Belousov 43870bc52b0bSKonstantin Belousov sigfastblock_setpend1(td); 43880bc52b0bSKonstantin Belousov if (resched) { 43890bc52b0bSKonstantin Belousov p = td->td_proc; 43900bc52b0bSKonstantin Belousov PROC_LOCK(p); 43910bc52b0bSKonstantin Belousov reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK); 43920bc52b0bSKonstantin Belousov PROC_UNLOCK(p); 43930bc52b0bSKonstantin Belousov } 43940bc52b0bSKonstantin Belousov } 4395