1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include "opt_capsicum.h" 38 #include "opt_ktrace.h" 39 40 #include <sys/param.h> 41 #include <sys/capsicum.h> 42 #include <sys/ctype.h> 43 #include <sys/systm.h> 44 #include <sys/signalvar.h> 45 #include <sys/vnode.h> 46 #include <sys/acct.h> 47 #include <sys/capsicum.h> 48 #include <sys/condvar.h> 49 #include <sys/devctl.h> 50 #include <sys/event.h> 51 #include <sys/exec.h> 52 #include <sys/fcntl.h> 53 #include <sys/imgact.h> 54 #include <sys/jail.h> 55 #include <sys/kernel.h> 56 #include <sys/ktr.h> 57 #include <sys/ktrace.h> 58 #include <sys/limits.h> 59 #include <sys/lock.h> 60 #include <sys/malloc.h> 61 #include <sys/mutex.h> 62 #include <sys/refcount.h> 63 #include <sys/namei.h> 64 #include <sys/proc.h> 65 #include <sys/procdesc.h> 66 #include <sys/ptrace.h> 67 #include <sys/posix4.h> 68 #include <sys/racct.h> 69 #include <sys/resourcevar.h> 70 #include <sys/sdt.h> 71 #include <sys/sbuf.h> 72 #include <sys/sleepqueue.h> 73 #include <sys/smp.h> 74 #include <sys/stat.h> 75 #include <sys/sx.h> 76 #include <sys/syscall.h> 77 #include <sys/syscallsubr.h> 78 #include <sys/sysctl.h> 79 #include <sys/sysent.h> 80 #include <sys/syslog.h> 81 #include <sys/sysproto.h> 82 #include <sys/timers.h> 83 #include <sys/ucoredump.h> 84 #include <sys/unistd.h> 85 #include <sys/vmmeter.h> 86 #include <sys/wait.h> 87 #include <vm/vm.h> 88 #include <vm/vm_extern.h> 89 #include <vm/uma.h> 90 91 #include <machine/cpu.h> 92 93 #include <security/audit/audit.h> 94 95 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 96 97 SDT_PROVIDER_DECLARE(proc); 98 SDT_PROBE_DEFINE3(proc, , , signal__send, 99 "struct thread *", "struct proc *", "int"); 100 SDT_PROBE_DEFINE2(proc, , , signal__clear, 101 "int", "ksiginfo_t *"); 102 SDT_PROBE_DEFINE3(proc, , , signal__discard, 103 "struct thread *", "struct proc *", "int"); 104 105 static int killpg1(struct thread *td, int sig, int pgid, int all, 106 ksiginfo_t *ksi); 107 static int issignal(struct thread *td); 108 static void reschedule_signals(struct proc *p, sigset_t block, int flags); 109 static int sigprop(int sig); 110 static void tdsigwakeup(struct thread *, int, sig_t, int); 111 static bool sig_suspend_threads(struct thread *, struct proc *); 112 static int filt_sigattach(struct knote *kn); 113 static void filt_sigdetach(struct knote *kn); 114 static int filt_signal(struct knote *kn, long hint); 115 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock); 116 static void sigqueue_start(void *); 117 static void sigfastblock_setpend(struct thread *td, bool resched); 118 static void sig_handle_first_stop(struct thread *td, struct proc *p, 119 int sig); 120 121 static uma_zone_t ksiginfo_zone = NULL; 122 const struct filterops sig_filtops = { 123 .f_isfd = 0, 124 .f_attach = filt_sigattach, 125 .f_detach = filt_sigdetach, 126 .f_event = filt_signal, 127 .f_copy = knote_triv_copy, 128 }; 129 130 static int kern_forcesigexit = 1; 131 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 132 &kern_forcesigexit, 0, "Force trap signal to be handled"); 133 134 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 135 "POSIX real time signal"); 136 137 static int max_pending_per_proc = 128; 138 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 139 &max_pending_per_proc, 0, "Max pending signals per proc"); 140 141 static int preallocate_siginfo = 1024; 142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN, 143 &preallocate_siginfo, 0, "Preallocated signal memory size"); 144 145 static int signal_overflow = 0; 146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 147 &signal_overflow, 0, "Number of signals overflew"); 148 149 static int signal_alloc_fail = 0; 150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 151 &signal_alloc_fail, 0, "signals failed to be allocated"); 152 153 static int kern_lognosys = 0; 154 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0, 155 "Log invalid syscalls"); 156 157 static int kern_signosys = 1; 158 SYSCTL_INT(_kern, OID_AUTO, signosys, CTLFLAG_RWTUN, &kern_signosys, 0, 159 "Send SIGSYS on return from invalid syscall"); 160 161 __read_frequently bool sigfastblock_fetch_always = false; 162 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN, 163 &sigfastblock_fetch_always, 0, 164 "Fetch sigfastblock word on each syscall entry for proper " 165 "blocking semantic"); 166 167 static bool kern_sig_discard_ign = true; 168 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN, 169 &kern_sig_discard_ign, 0, 170 "Discard ignored signals on delivery, otherwise queue them to " 171 "the target queue"); 172 173 bool pt_attach_transparent = true; 174 SYSCTL_BOOL(_debug, OID_AUTO, ptrace_attach_transparent, CTLFLAG_RWTUN, 175 &pt_attach_transparent, 0, 176 "Hide wakes from PT_ATTACH on interruptible sleeps"); 177 178 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 179 180 /* 181 * Policy -- Can ucred cr1 send SIGIO to process cr2? 182 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 183 * in the right situations. 184 */ 185 #define CANSIGIO(cr1, cr2) \ 186 ((cr1)->cr_uid == 0 || \ 187 (cr1)->cr_ruid == (cr2)->cr_ruid || \ 188 (cr1)->cr_uid == (cr2)->cr_ruid || \ 189 (cr1)->cr_ruid == (cr2)->cr_uid || \ 190 (cr1)->cr_uid == (cr2)->cr_uid) 191 192 /* 193 * Signal properties and actions. 194 * The array below categorizes the signals and their default actions 195 * according to the following properties: 196 */ 197 #define SIGPROP_KILL 0x01 /* terminates process by default */ 198 #define SIGPROP_CORE 0x02 /* ditto and coredumps */ 199 #define SIGPROP_STOP 0x04 /* suspend process */ 200 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */ 201 #define SIGPROP_IGNORE 0x10 /* ignore by default */ 202 #define SIGPROP_CONT 0x20 /* continue if suspended */ 203 204 static const int sigproptbl[NSIG] = { 205 [SIGHUP] = SIGPROP_KILL, 206 [SIGINT] = SIGPROP_KILL, 207 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE, 208 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE, 209 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE, 210 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE, 211 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE, 212 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE, 213 [SIGKILL] = SIGPROP_KILL, 214 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE, 215 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE, 216 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE, 217 [SIGPIPE] = SIGPROP_KILL, 218 [SIGALRM] = SIGPROP_KILL, 219 [SIGTERM] = SIGPROP_KILL, 220 [SIGURG] = SIGPROP_IGNORE, 221 [SIGSTOP] = SIGPROP_STOP, 222 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP, 223 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT, 224 [SIGCHLD] = SIGPROP_IGNORE, 225 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP, 226 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP, 227 [SIGIO] = SIGPROP_IGNORE, 228 [SIGXCPU] = SIGPROP_KILL, 229 [SIGXFSZ] = SIGPROP_KILL, 230 [SIGVTALRM] = SIGPROP_KILL, 231 [SIGPROF] = SIGPROP_KILL, 232 [SIGWINCH] = SIGPROP_IGNORE, 233 [SIGINFO] = SIGPROP_IGNORE, 234 [SIGUSR1] = SIGPROP_KILL, 235 [SIGUSR2] = SIGPROP_KILL, 236 }; 237 238 #define _SIG_FOREACH_ADVANCE(i, set) ({ \ 239 int __found; \ 240 for (;;) { \ 241 if (__bits != 0) { \ 242 int __sig = ffs(__bits); \ 243 __bits &= ~(1u << (__sig - 1)); \ 244 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \ 245 __found = 1; \ 246 break; \ 247 } \ 248 if (++__i == _SIG_WORDS) { \ 249 __found = 0; \ 250 break; \ 251 } \ 252 __bits = (set)->__bits[__i]; \ 253 } \ 254 __found != 0; \ 255 }) 256 257 #define SIG_FOREACH(i, set) \ 258 for (int32_t __i = -1, __bits = 0; \ 259 _SIG_FOREACH_ADVANCE(i, set); ) \ 260 261 static sigset_t fastblock_mask; 262 263 static void 264 ast_sig(struct thread *td, int tda) 265 { 266 struct proc *p; 267 int old_boundary, sig; 268 bool resched_sigs; 269 270 p = td->td_proc; 271 272 #ifdef DIAGNOSTIC 273 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) | 274 TDAI(TDA_AST))) == 0) { 275 PROC_LOCK(p); 276 thread_lock(td); 277 /* 278 * Note that TDA_SIG should be re-read from 279 * td_ast, since signal might have been delivered 280 * after we cleared td_flags above. This is one of 281 * the reason for looping check for AST condition. 282 * See comment in userret() about P_PPWAIT. 283 */ 284 if ((p->p_flag & P_PPWAIT) == 0 && 285 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 286 if (SIGPENDING(td) && ((tda | td->td_ast) & 287 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) { 288 thread_unlock(td); /* fix dumps */ 289 panic( 290 "failed2 to set signal flags for ast p %p " 291 "td %p tda %#x td_ast %#x fl %#x", 292 p, td, tda, td->td_ast, td->td_flags); 293 } 294 } 295 thread_unlock(td); 296 PROC_UNLOCK(p); 297 } 298 #endif 299 300 /* 301 * Check for signals. Unlocked reads of p_pendingcnt or 302 * p_siglist might cause process-directed signal to be handled 303 * later. 304 */ 305 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 || 306 !SIGISEMPTY(p->p_siglist)) { 307 sigfastblock_fetch(td); 308 PROC_LOCK(p); 309 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY); 310 td->td_dbgflags |= TDB_BOUNDARY; 311 mtx_lock(&p->p_sigacts->ps_mtx); 312 while ((sig = cursig(td)) != 0) { 313 KASSERT(sig >= 0, ("sig %d", sig)); 314 postsig(sig); 315 } 316 mtx_unlock(&p->p_sigacts->ps_mtx); 317 td->td_dbgflags &= old_boundary; 318 PROC_UNLOCK(p); 319 resched_sigs = true; 320 } else { 321 resched_sigs = false; 322 } 323 324 /* 325 * Handle deferred update of the fast sigblock value, after 326 * the postsig() loop was performed. 327 */ 328 sigfastblock_setpend(td, resched_sigs); 329 330 /* 331 * Clear td_sa.code: signal to ptrace that syscall arguments 332 * are unavailable after this point. This AST handler is the 333 * last chance for ptracestop() to signal the tracer before 334 * the tracee returns to userspace. 335 */ 336 td->td_sa.code = 0; 337 } 338 339 static void 340 ast_sigsuspend(struct thread *td, int tda __unused) 341 { 342 MPASS((td->td_pflags & TDP_OLDMASK) != 0); 343 td->td_pflags &= ~TDP_OLDMASK; 344 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0); 345 } 346 347 static void 348 sigqueue_start(void *dummy __unused) 349 { 350 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 351 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 352 uma_prealloc(ksiginfo_zone, preallocate_siginfo); 353 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 354 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 355 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 356 SIGFILLSET(fastblock_mask); 357 SIG_CANTMASK(fastblock_mask); 358 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig); 359 360 /* 361 * TDA_PSELECT is for the case where the signal mask should be restored 362 * before delivering any signals so that we do not deliver any that are 363 * blocked by the normal thread mask. It is mutually exclusive with 364 * TDA_SIGSUSPEND, which should be used if we *do* want to deliver 365 * signals that are normally blocked, e.g., if it interrupted our sleep. 366 */ 367 ast_register(TDA_PSELECT, ASTR_ASTF_REQUIRED | ASTR_TDP, 368 TDP_OLDMASK, ast_sigsuspend); 369 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP, 370 TDP_OLDMASK, ast_sigsuspend); 371 } 372 373 ksiginfo_t * 374 ksiginfo_alloc(int mwait) 375 { 376 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT); 377 378 if (ksiginfo_zone == NULL) 379 return (NULL); 380 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO)); 381 } 382 383 void 384 ksiginfo_free(ksiginfo_t *ksi) 385 { 386 uma_zfree(ksiginfo_zone, ksi); 387 } 388 389 static __inline bool 390 ksiginfo_tryfree(ksiginfo_t *ksi) 391 { 392 if ((ksi->ksi_flags & KSI_EXT) == 0) { 393 uma_zfree(ksiginfo_zone, ksi); 394 return (true); 395 } 396 return (false); 397 } 398 399 void 400 sigqueue_init(sigqueue_t *list, struct proc *p) 401 { 402 SIGEMPTYSET(list->sq_signals); 403 SIGEMPTYSET(list->sq_kill); 404 SIGEMPTYSET(list->sq_ptrace); 405 TAILQ_INIT(&list->sq_list); 406 list->sq_proc = p; 407 list->sq_flags = SQ_INIT; 408 } 409 410 /* 411 * Get a signal's ksiginfo. 412 * Return: 413 * 0 - signal not found 414 * others - signal number 415 */ 416 static int 417 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 418 { 419 struct proc *p = sq->sq_proc; 420 struct ksiginfo *ksi, *next; 421 int count = 0; 422 423 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 424 425 if (!SIGISMEMBER(sq->sq_signals, signo)) 426 return (0); 427 428 if (SIGISMEMBER(sq->sq_ptrace, signo)) { 429 count++; 430 SIGDELSET(sq->sq_ptrace, signo); 431 si->ksi_flags |= KSI_PTRACE; 432 } 433 if (SIGISMEMBER(sq->sq_kill, signo)) { 434 count++; 435 if (count == 1) 436 SIGDELSET(sq->sq_kill, signo); 437 } 438 439 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 440 if (ksi->ksi_signo == signo) { 441 if (count == 0) { 442 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 443 ksi->ksi_sigq = NULL; 444 ksiginfo_copy(ksi, si); 445 if (ksiginfo_tryfree(ksi) && p != NULL) 446 p->p_pendingcnt--; 447 } 448 if (++count > 1) 449 break; 450 } 451 } 452 453 if (count <= 1) 454 SIGDELSET(sq->sq_signals, signo); 455 si->ksi_signo = signo; 456 return (signo); 457 } 458 459 void 460 sigqueue_take(ksiginfo_t *ksi) 461 { 462 struct ksiginfo *kp; 463 struct proc *p; 464 sigqueue_t *sq; 465 466 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 467 return; 468 469 p = sq->sq_proc; 470 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 471 ksi->ksi_sigq = NULL; 472 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 473 p->p_pendingcnt--; 474 475 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 476 kp = TAILQ_NEXT(kp, ksi_link)) { 477 if (kp->ksi_signo == ksi->ksi_signo) 478 break; 479 } 480 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) && 481 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo)) 482 SIGDELSET(sq->sq_signals, ksi->ksi_signo); 483 } 484 485 static int 486 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 487 { 488 struct proc *p = sq->sq_proc; 489 struct ksiginfo *ksi; 490 int ret = 0; 491 492 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 493 494 /* 495 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path 496 * for these signals. 497 */ 498 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) { 499 SIGADDSET(sq->sq_kill, signo); 500 goto out_set_bit; 501 } 502 503 /* directly insert the ksi, don't copy it */ 504 if (si->ksi_flags & KSI_INS) { 505 if (si->ksi_flags & KSI_HEAD) 506 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link); 507 else 508 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 509 si->ksi_sigq = sq; 510 goto out_set_bit; 511 } 512 513 if (__predict_false(ksiginfo_zone == NULL)) { 514 SIGADDSET(sq->sq_kill, signo); 515 goto out_set_bit; 516 } 517 518 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 519 signal_overflow++; 520 ret = EAGAIN; 521 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) { 522 signal_alloc_fail++; 523 ret = EAGAIN; 524 } else { 525 if (p != NULL) 526 p->p_pendingcnt++; 527 ksiginfo_copy(si, ksi); 528 ksi->ksi_signo = signo; 529 if (si->ksi_flags & KSI_HEAD) 530 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link); 531 else 532 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 533 ksi->ksi_sigq = sq; 534 } 535 536 if (ret != 0) { 537 if ((si->ksi_flags & KSI_PTRACE) != 0) { 538 SIGADDSET(sq->sq_ptrace, signo); 539 ret = 0; 540 goto out_set_bit; 541 } else if ((si->ksi_flags & KSI_TRAP) != 0 || 542 (si->ksi_flags & KSI_SIGQ) == 0) { 543 SIGADDSET(sq->sq_kill, signo); 544 ret = 0; 545 goto out_set_bit; 546 } 547 return (ret); 548 } 549 550 out_set_bit: 551 SIGADDSET(sq->sq_signals, signo); 552 return (ret); 553 } 554 555 void 556 sigqueue_flush(sigqueue_t *sq) 557 { 558 struct proc *p = sq->sq_proc; 559 ksiginfo_t *ksi; 560 561 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 562 563 if (p != NULL) 564 PROC_LOCK_ASSERT(p, MA_OWNED); 565 566 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 567 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 568 ksi->ksi_sigq = NULL; 569 if (ksiginfo_tryfree(ksi) && p != NULL) 570 p->p_pendingcnt--; 571 } 572 573 SIGEMPTYSET(sq->sq_signals); 574 SIGEMPTYSET(sq->sq_kill); 575 SIGEMPTYSET(sq->sq_ptrace); 576 } 577 578 static void 579 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set) 580 { 581 sigset_t tmp; 582 struct proc *p1, *p2; 583 ksiginfo_t *ksi, *next; 584 585 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 586 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 587 p1 = src->sq_proc; 588 p2 = dst->sq_proc; 589 /* Move siginfo to target list */ 590 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) { 591 if (SIGISMEMBER(*set, ksi->ksi_signo)) { 592 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 593 if (p1 != NULL) 594 p1->p_pendingcnt--; 595 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 596 ksi->ksi_sigq = dst; 597 if (p2 != NULL) 598 p2->p_pendingcnt++; 599 } 600 } 601 602 /* Move pending bits to target list */ 603 tmp = src->sq_kill; 604 SIGSETAND(tmp, *set); 605 SIGSETOR(dst->sq_kill, tmp); 606 SIGSETNAND(src->sq_kill, tmp); 607 608 tmp = src->sq_ptrace; 609 SIGSETAND(tmp, *set); 610 SIGSETOR(dst->sq_ptrace, tmp); 611 SIGSETNAND(src->sq_ptrace, tmp); 612 613 tmp = src->sq_signals; 614 SIGSETAND(tmp, *set); 615 SIGSETOR(dst->sq_signals, tmp); 616 SIGSETNAND(src->sq_signals, tmp); 617 } 618 619 #if 0 620 static void 621 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 622 { 623 sigset_t set; 624 625 SIGEMPTYSET(set); 626 SIGADDSET(set, signo); 627 sigqueue_move_set(src, dst, &set); 628 } 629 #endif 630 631 static void 632 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set) 633 { 634 struct proc *p = sq->sq_proc; 635 ksiginfo_t *ksi, *next; 636 637 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 638 639 /* Remove siginfo queue */ 640 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) { 641 if (SIGISMEMBER(*set, ksi->ksi_signo)) { 642 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 643 ksi->ksi_sigq = NULL; 644 if (ksiginfo_tryfree(ksi) && p != NULL) 645 p->p_pendingcnt--; 646 } 647 } 648 SIGSETNAND(sq->sq_kill, *set); 649 SIGSETNAND(sq->sq_ptrace, *set); 650 SIGSETNAND(sq->sq_signals, *set); 651 } 652 653 void 654 sigqueue_delete(sigqueue_t *sq, int signo) 655 { 656 sigset_t set; 657 658 SIGEMPTYSET(set); 659 SIGADDSET(set, signo); 660 sigqueue_delete_set(sq, &set); 661 } 662 663 /* Remove a set of signals for a process */ 664 static void 665 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set) 666 { 667 sigqueue_t worklist; 668 struct thread *td0; 669 670 PROC_LOCK_ASSERT(p, MA_OWNED); 671 672 sigqueue_init(&worklist, NULL); 673 sigqueue_move_set(&p->p_sigqueue, &worklist, set); 674 675 FOREACH_THREAD_IN_PROC(p, td0) 676 sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 677 678 sigqueue_flush(&worklist); 679 } 680 681 void 682 sigqueue_delete_proc(struct proc *p, int signo) 683 { 684 sigset_t set; 685 686 SIGEMPTYSET(set); 687 SIGADDSET(set, signo); 688 sigqueue_delete_set_proc(p, &set); 689 } 690 691 static void 692 sigqueue_delete_stopmask_proc(struct proc *p) 693 { 694 sigset_t set; 695 696 SIGEMPTYSET(set); 697 SIGADDSET(set, SIGSTOP); 698 SIGADDSET(set, SIGTSTP); 699 SIGADDSET(set, SIGTTIN); 700 SIGADDSET(set, SIGTTOU); 701 sigqueue_delete_set_proc(p, &set); 702 } 703 704 /* 705 * Determine signal that should be delivered to thread td, the current 706 * thread, 0 if none. If there is a pending stop signal with default 707 * action, the process stops in issignal(). 708 */ 709 int 710 cursig(struct thread *td) 711 { 712 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 713 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 714 THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 715 return (SIGPENDING(td) ? issignal(td) : 0); 716 } 717 718 /* 719 * Arrange for ast() to handle unmasked pending signals on return to user 720 * mode. This must be called whenever a signal is added to td_sigqueue or 721 * unmasked in td_sigmask. 722 */ 723 void 724 signotify(struct thread *td) 725 { 726 727 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 728 729 if (SIGPENDING(td)) 730 ast_sched(td, TDA_SIG); 731 } 732 733 /* 734 * Returns 1 (true) if altstack is configured for the thread, and the 735 * passed stack bottom address falls into the altstack range. Handles 736 * the 43 compat special case where the alt stack size is zero. 737 */ 738 int 739 sigonstack(size_t sp) 740 { 741 struct thread *td; 742 743 td = curthread; 744 if ((td->td_pflags & TDP_ALTSTACK) == 0) 745 return (0); 746 #if defined(COMPAT_43) 747 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0) 748 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0); 749 #endif 750 return (sp >= (size_t)td->td_sigstk.ss_sp && 751 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp); 752 } 753 754 static __inline int 755 sigprop(int sig) 756 { 757 758 if (sig > 0 && sig < nitems(sigproptbl)) 759 return (sigproptbl[sig]); 760 return (0); 761 } 762 763 bool 764 sig_do_core(int sig) 765 { 766 767 return ((sigprop(sig) & SIGPROP_CORE) != 0); 768 } 769 770 static bool 771 sigact_flag_test(const struct sigaction *act, int flag) 772 { 773 774 /* 775 * SA_SIGINFO is reset when signal disposition is set to 776 * ignore or default. Other flags are kept according to user 777 * settings. 778 */ 779 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO || 780 ((__sighandler_t *)act->sa_sigaction != SIG_IGN && 781 (__sighandler_t *)act->sa_sigaction != SIG_DFL))); 782 } 783 784 /* 785 * kern_sigaction 786 * sigaction 787 * freebsd4_sigaction 788 * osigaction 789 */ 790 int 791 kern_sigaction(struct thread *td, int sig, const struct sigaction *act, 792 struct sigaction *oact, int flags) 793 { 794 struct sigacts *ps; 795 struct proc *p = td->td_proc; 796 797 if (!_SIG_VALID(sig)) 798 return (EINVAL); 799 if (act != NULL && act->sa_handler != SIG_DFL && 800 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK | 801 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | 802 SA_NOCLDWAIT | SA_SIGINFO)) != 0) 803 return (EINVAL); 804 805 PROC_LOCK(p); 806 ps = p->p_sigacts; 807 mtx_lock(&ps->ps_mtx); 808 if (oact) { 809 memset(oact, 0, sizeof(*oact)); 810 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 811 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 812 oact->sa_flags |= SA_ONSTACK; 813 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 814 oact->sa_flags |= SA_RESTART; 815 if (SIGISMEMBER(ps->ps_sigreset, sig)) 816 oact->sa_flags |= SA_RESETHAND; 817 if (SIGISMEMBER(ps->ps_signodefer, sig)) 818 oact->sa_flags |= SA_NODEFER; 819 if (SIGISMEMBER(ps->ps_siginfo, sig)) { 820 oact->sa_flags |= SA_SIGINFO; 821 oact->sa_sigaction = 822 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)]; 823 } else 824 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 825 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 826 oact->sa_flags |= SA_NOCLDSTOP; 827 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 828 oact->sa_flags |= SA_NOCLDWAIT; 829 } 830 if (act) { 831 if ((sig == SIGKILL || sig == SIGSTOP) && 832 act->sa_handler != SIG_DFL) { 833 mtx_unlock(&ps->ps_mtx); 834 PROC_UNLOCK(p); 835 return (EINVAL); 836 } 837 838 /* 839 * Change setting atomically. 840 */ 841 842 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 843 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 844 if (sigact_flag_test(act, SA_SIGINFO)) { 845 ps->ps_sigact[_SIG_IDX(sig)] = 846 (__sighandler_t *)act->sa_sigaction; 847 SIGADDSET(ps->ps_siginfo, sig); 848 } else { 849 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 850 SIGDELSET(ps->ps_siginfo, sig); 851 } 852 if (!sigact_flag_test(act, SA_RESTART)) 853 SIGADDSET(ps->ps_sigintr, sig); 854 else 855 SIGDELSET(ps->ps_sigintr, sig); 856 if (sigact_flag_test(act, SA_ONSTACK)) 857 SIGADDSET(ps->ps_sigonstack, sig); 858 else 859 SIGDELSET(ps->ps_sigonstack, sig); 860 if (sigact_flag_test(act, SA_RESETHAND)) 861 SIGADDSET(ps->ps_sigreset, sig); 862 else 863 SIGDELSET(ps->ps_sigreset, sig); 864 if (sigact_flag_test(act, SA_NODEFER)) 865 SIGADDSET(ps->ps_signodefer, sig); 866 else 867 SIGDELSET(ps->ps_signodefer, sig); 868 if (sig == SIGCHLD) { 869 if (act->sa_flags & SA_NOCLDSTOP) 870 ps->ps_flag |= PS_NOCLDSTOP; 871 else 872 ps->ps_flag &= ~PS_NOCLDSTOP; 873 if (act->sa_flags & SA_NOCLDWAIT) { 874 /* 875 * Paranoia: since SA_NOCLDWAIT is implemented 876 * by reparenting the dying child to PID 1 (and 877 * trust it to reap the zombie), PID 1 itself 878 * is forbidden to set SA_NOCLDWAIT. 879 */ 880 if (p->p_pid == 1) 881 ps->ps_flag &= ~PS_NOCLDWAIT; 882 else 883 ps->ps_flag |= PS_NOCLDWAIT; 884 } else 885 ps->ps_flag &= ~PS_NOCLDWAIT; 886 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 887 ps->ps_flag |= PS_CLDSIGIGN; 888 else 889 ps->ps_flag &= ~PS_CLDSIGIGN; 890 } 891 /* 892 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 893 * and for signals set to SIG_DFL where the default is to 894 * ignore. However, don't put SIGCONT in ps_sigignore, as we 895 * have to restart the process. 896 */ 897 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 898 (sigprop(sig) & SIGPROP_IGNORE && 899 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 900 /* never to be seen again */ 901 sigqueue_delete_proc(p, sig); 902 if (sig != SIGCONT) 903 /* easier in psignal */ 904 SIGADDSET(ps->ps_sigignore, sig); 905 SIGDELSET(ps->ps_sigcatch, sig); 906 } else { 907 SIGDELSET(ps->ps_sigignore, sig); 908 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 909 SIGDELSET(ps->ps_sigcatch, sig); 910 else 911 SIGADDSET(ps->ps_sigcatch, sig); 912 } 913 #ifdef COMPAT_FREEBSD4 914 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 915 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 916 (flags & KSA_FREEBSD4) == 0) 917 SIGDELSET(ps->ps_freebsd4, sig); 918 else 919 SIGADDSET(ps->ps_freebsd4, sig); 920 #endif 921 #ifdef COMPAT_43 922 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 923 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 924 (flags & KSA_OSIGSET) == 0) 925 SIGDELSET(ps->ps_osigset, sig); 926 else 927 SIGADDSET(ps->ps_osigset, sig); 928 #endif 929 } 930 mtx_unlock(&ps->ps_mtx); 931 PROC_UNLOCK(p); 932 return (0); 933 } 934 935 #ifndef _SYS_SYSPROTO_H_ 936 struct sigaction_args { 937 int sig; 938 struct sigaction *act; 939 struct sigaction *oact; 940 }; 941 #endif 942 int 943 sys_sigaction(struct thread *td, struct sigaction_args *uap) 944 { 945 struct sigaction act, oact; 946 struct sigaction *actp, *oactp; 947 int error; 948 949 actp = (uap->act != NULL) ? &act : NULL; 950 oactp = (uap->oact != NULL) ? &oact : NULL; 951 if (actp) { 952 error = copyin(uap->act, actp, sizeof(act)); 953 if (error) 954 return (error); 955 } 956 error = kern_sigaction(td, uap->sig, actp, oactp, 0); 957 if (oactp && !error) 958 error = copyout(oactp, uap->oact, sizeof(oact)); 959 return (error); 960 } 961 962 #ifdef COMPAT_FREEBSD4 963 #ifndef _SYS_SYSPROTO_H_ 964 struct freebsd4_sigaction_args { 965 int sig; 966 struct sigaction *act; 967 struct sigaction *oact; 968 }; 969 #endif 970 int 971 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap) 972 { 973 struct sigaction act, oact; 974 struct sigaction *actp, *oactp; 975 int error; 976 977 actp = (uap->act != NULL) ? &act : NULL; 978 oactp = (uap->oact != NULL) ? &oact : NULL; 979 if (actp) { 980 error = copyin(uap->act, actp, sizeof(act)); 981 if (error) 982 return (error); 983 } 984 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 985 if (oactp && !error) 986 error = copyout(oactp, uap->oact, sizeof(oact)); 987 return (error); 988 } 989 #endif /* COMAPT_FREEBSD4 */ 990 991 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 992 #ifndef _SYS_SYSPROTO_H_ 993 struct osigaction_args { 994 int signum; 995 struct osigaction *nsa; 996 struct osigaction *osa; 997 }; 998 #endif 999 int 1000 osigaction(struct thread *td, struct osigaction_args *uap) 1001 { 1002 struct osigaction sa; 1003 struct sigaction nsa, osa; 1004 struct sigaction *nsap, *osap; 1005 int error; 1006 1007 if (uap->signum <= 0 || uap->signum >= ONSIG) 1008 return (EINVAL); 1009 1010 nsap = (uap->nsa != NULL) ? &nsa : NULL; 1011 osap = (uap->osa != NULL) ? &osa : NULL; 1012 1013 if (nsap) { 1014 error = copyin(uap->nsa, &sa, sizeof(sa)); 1015 if (error) 1016 return (error); 1017 nsap->sa_handler = sa.sa_handler; 1018 nsap->sa_flags = sa.sa_flags; 1019 OSIG2SIG(sa.sa_mask, nsap->sa_mask); 1020 } 1021 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1022 if (osap && !error) { 1023 sa.sa_handler = osap->sa_handler; 1024 sa.sa_flags = osap->sa_flags; 1025 SIG2OSIG(osap->sa_mask, sa.sa_mask); 1026 error = copyout(&sa, uap->osa, sizeof(sa)); 1027 } 1028 return (error); 1029 } 1030 1031 #if !defined(__i386__) 1032 /* Avoid replicating the same stub everywhere */ 1033 int 1034 osigreturn(struct thread *td, struct osigreturn_args *uap) 1035 { 1036 return (kern_nosys(td, 0)); 1037 } 1038 #endif 1039 #endif /* COMPAT_43 */ 1040 1041 /* 1042 * Initialize signal state for process 0; 1043 * set to ignore signals that are ignored by default. 1044 */ 1045 void 1046 siginit(struct proc *p) 1047 { 1048 int i; 1049 struct sigacts *ps; 1050 1051 PROC_LOCK(p); 1052 ps = p->p_sigacts; 1053 mtx_lock(&ps->ps_mtx); 1054 for (i = 1; i <= NSIG; i++) { 1055 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) { 1056 SIGADDSET(ps->ps_sigignore, i); 1057 } 1058 } 1059 mtx_unlock(&ps->ps_mtx); 1060 PROC_UNLOCK(p); 1061 } 1062 1063 /* 1064 * Reset specified signal to the default disposition. 1065 */ 1066 static void 1067 sigdflt(struct sigacts *ps, int sig) 1068 { 1069 1070 mtx_assert(&ps->ps_mtx, MA_OWNED); 1071 SIGDELSET(ps->ps_sigcatch, sig); 1072 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT) 1073 SIGADDSET(ps->ps_sigignore, sig); 1074 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1075 SIGDELSET(ps->ps_siginfo, sig); 1076 } 1077 1078 /* 1079 * Reset signals for an exec of the specified process. 1080 */ 1081 void 1082 execsigs(struct proc *p) 1083 { 1084 struct sigacts *ps; 1085 struct thread *td; 1086 1087 /* 1088 * Reset caught signals. Held signals remain held 1089 * through td_sigmask (unless they were caught, 1090 * and are now ignored by default). 1091 */ 1092 PROC_LOCK_ASSERT(p, MA_OWNED); 1093 ps = p->p_sigacts; 1094 mtx_lock(&ps->ps_mtx); 1095 sig_drop_caught(p); 1096 1097 /* 1098 * Reset stack state to the user stack. 1099 * Clear set of signals caught on the signal stack. 1100 */ 1101 td = curthread; 1102 MPASS(td->td_proc == p); 1103 td->td_sigstk.ss_flags = SS_DISABLE; 1104 td->td_sigstk.ss_size = 0; 1105 td->td_sigstk.ss_sp = 0; 1106 td->td_pflags &= ~TDP_ALTSTACK; 1107 /* 1108 * Reset no zombies if child dies flag as Solaris does. 1109 */ 1110 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1111 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1112 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 1113 mtx_unlock(&ps->ps_mtx); 1114 } 1115 1116 /* 1117 * kern_sigprocmask() 1118 * 1119 * Manipulate signal mask. 1120 */ 1121 int 1122 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 1123 int flags) 1124 { 1125 sigset_t new_block, oset1; 1126 struct proc *p; 1127 int error; 1128 1129 p = td->td_proc; 1130 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 1131 PROC_LOCK_ASSERT(p, MA_OWNED); 1132 else 1133 PROC_LOCK(p); 1134 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 1135 ? MA_OWNED : MA_NOTOWNED); 1136 if (oset != NULL) 1137 *oset = td->td_sigmask; 1138 1139 error = 0; 1140 if (set != NULL) { 1141 switch (how) { 1142 case SIG_BLOCK: 1143 SIG_CANTMASK(*set); 1144 oset1 = td->td_sigmask; 1145 SIGSETOR(td->td_sigmask, *set); 1146 new_block = td->td_sigmask; 1147 SIGSETNAND(new_block, oset1); 1148 break; 1149 case SIG_UNBLOCK: 1150 SIGSETNAND(td->td_sigmask, *set); 1151 signotify(td); 1152 goto out; 1153 case SIG_SETMASK: 1154 SIG_CANTMASK(*set); 1155 oset1 = td->td_sigmask; 1156 if (flags & SIGPROCMASK_OLD) 1157 SIGSETLO(td->td_sigmask, *set); 1158 else 1159 td->td_sigmask = *set; 1160 new_block = td->td_sigmask; 1161 SIGSETNAND(new_block, oset1); 1162 signotify(td); 1163 break; 1164 default: 1165 error = EINVAL; 1166 goto out; 1167 } 1168 1169 /* 1170 * The new_block set contains signals that were not previously 1171 * blocked, but are blocked now. 1172 * 1173 * In case we block any signal that was not previously blocked 1174 * for td, and process has the signal pending, try to schedule 1175 * signal delivery to some thread that does not block the 1176 * signal, possibly waking it up. 1177 */ 1178 if (p->p_numthreads != 1) 1179 reschedule_signals(p, new_block, flags); 1180 } 1181 1182 out: 1183 if (!(flags & SIGPROCMASK_PROC_LOCKED)) 1184 PROC_UNLOCK(p); 1185 return (error); 1186 } 1187 1188 #ifndef _SYS_SYSPROTO_H_ 1189 struct sigprocmask_args { 1190 int how; 1191 const sigset_t *set; 1192 sigset_t *oset; 1193 }; 1194 #endif 1195 int 1196 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1197 { 1198 sigset_t set, oset; 1199 sigset_t *setp, *osetp; 1200 int error; 1201 1202 setp = (uap->set != NULL) ? &set : NULL; 1203 osetp = (uap->oset != NULL) ? &oset : NULL; 1204 if (setp) { 1205 error = copyin(uap->set, setp, sizeof(set)); 1206 if (error) 1207 return (error); 1208 } 1209 error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 1210 if (osetp && !error) { 1211 error = copyout(osetp, uap->oset, sizeof(oset)); 1212 } 1213 return (error); 1214 } 1215 1216 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1217 #ifndef _SYS_SYSPROTO_H_ 1218 struct osigprocmask_args { 1219 int how; 1220 osigset_t mask; 1221 }; 1222 #endif 1223 int 1224 osigprocmask(struct thread *td, struct osigprocmask_args *uap) 1225 { 1226 sigset_t set, oset; 1227 int error; 1228 1229 OSIG2SIG(uap->mask, set); 1230 error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1231 SIG2OSIG(oset, td->td_retval[0]); 1232 return (error); 1233 } 1234 #endif /* COMPAT_43 */ 1235 1236 int 1237 sys_sigwait(struct thread *td, struct sigwait_args *uap) 1238 { 1239 ksiginfo_t ksi; 1240 sigset_t set; 1241 int error; 1242 1243 error = copyin(uap->set, &set, sizeof(set)); 1244 if (error) { 1245 td->td_retval[0] = error; 1246 return (0); 1247 } 1248 1249 error = kern_sigtimedwait(td, set, &ksi, NULL); 1250 if (error) { 1251 /* 1252 * sigwait() function shall not return EINTR, but 1253 * the syscall does. Non-ancient libc provides the 1254 * wrapper which hides EINTR. Otherwise, EINTR return 1255 * is used by libthr to handle required cancellation 1256 * point in the sigwait(). 1257 */ 1258 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 1259 return (ERESTART); 1260 td->td_retval[0] = error; 1261 return (0); 1262 } 1263 1264 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 1265 td->td_retval[0] = error; 1266 return (0); 1267 } 1268 1269 int 1270 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1271 { 1272 struct timespec ts; 1273 struct timespec *timeout; 1274 sigset_t set; 1275 ksiginfo_t ksi; 1276 int error; 1277 1278 if (uap->timeout) { 1279 error = copyin(uap->timeout, &ts, sizeof(ts)); 1280 if (error) 1281 return (error); 1282 1283 timeout = &ts; 1284 } else 1285 timeout = NULL; 1286 1287 error = copyin(uap->set, &set, sizeof(set)); 1288 if (error) 1289 return (error); 1290 1291 error = kern_sigtimedwait(td, set, &ksi, timeout); 1292 if (error) 1293 return (error); 1294 1295 if (uap->info) 1296 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1297 1298 if (error == 0) 1299 td->td_retval[0] = ksi.ksi_signo; 1300 return (error); 1301 } 1302 1303 int 1304 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1305 { 1306 ksiginfo_t ksi; 1307 sigset_t set; 1308 int error; 1309 1310 error = copyin(uap->set, &set, sizeof(set)); 1311 if (error) 1312 return (error); 1313 1314 error = kern_sigtimedwait(td, set, &ksi, NULL); 1315 if (error) 1316 return (error); 1317 1318 if (uap->info) 1319 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1320 1321 if (error == 0) 1322 td->td_retval[0] = ksi.ksi_signo; 1323 return (error); 1324 } 1325 1326 static void 1327 proc_td_siginfo_capture(struct thread *td, siginfo_t *si) 1328 { 1329 struct thread *thr; 1330 1331 FOREACH_THREAD_IN_PROC(td->td_proc, thr) { 1332 if (thr == td) 1333 thr->td_si = *si; 1334 else 1335 thr->td_si.si_signo = 0; 1336 } 1337 } 1338 1339 int 1340 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1341 struct timespec *timeout) 1342 { 1343 struct sigacts *ps; 1344 sigset_t saved_mask, new_block; 1345 struct proc *p; 1346 int error, sig, timevalid = 0; 1347 sbintime_t sbt, precision, tsbt; 1348 struct timespec ts; 1349 bool traced; 1350 1351 p = td->td_proc; 1352 error = 0; 1353 traced = false; 1354 1355 /* Ensure the sigfastblock value is up to date. */ 1356 sigfastblock_fetch(td); 1357 1358 if (timeout != NULL) { 1359 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 1360 timevalid = 1; 1361 ts = *timeout; 1362 if (ts.tv_sec < INT32_MAX / 2) { 1363 tsbt = tstosbt(ts); 1364 precision = tsbt; 1365 precision >>= tc_precexp; 1366 if (TIMESEL(&sbt, tsbt)) 1367 sbt += tc_tick_sbt; 1368 sbt += tsbt; 1369 } else 1370 precision = sbt = 0; 1371 } 1372 } else 1373 precision = sbt = 0; 1374 ksiginfo_init(ksi); 1375 /* Some signals can not be waited for. */ 1376 SIG_CANTMASK(waitset); 1377 ps = p->p_sigacts; 1378 PROC_LOCK(p); 1379 saved_mask = td->td_sigmask; 1380 SIGSETNAND(td->td_sigmask, waitset); 1381 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 || 1382 !kern_sig_discard_ign) { 1383 thread_lock(td); 1384 td->td_flags |= TDF_SIGWAIT; 1385 thread_unlock(td); 1386 } 1387 for (;;) { 1388 mtx_lock(&ps->ps_mtx); 1389 sig = cursig(td); 1390 mtx_unlock(&ps->ps_mtx); 1391 KASSERT(sig >= 0, ("sig %d", sig)); 1392 if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1393 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1394 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1395 error = 0; 1396 break; 1397 } 1398 } 1399 1400 if (error != 0) 1401 break; 1402 1403 /* 1404 * POSIX says this must be checked after looking for pending 1405 * signals. 1406 */ 1407 if (timeout != NULL && !timevalid) { 1408 error = EINVAL; 1409 break; 1410 } 1411 1412 if (traced) { 1413 error = EINTR; 1414 break; 1415 } 1416 1417 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, 1418 "sigwait", sbt, precision, C_ABSOLUTE); 1419 1420 /* The syscalls can not be restarted. */ 1421 if (error == ERESTART) 1422 error = EINTR; 1423 1424 /* 1425 * If PTRACE_SCE or PTRACE_SCX were set after 1426 * userspace entered the syscall, return spurious 1427 * EINTR after wait was done. Only do this as last 1428 * resort after rechecking for possible queued signals 1429 * and expired timeouts. 1430 */ 1431 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0) 1432 traced = true; 1433 } 1434 thread_lock(td); 1435 td->td_flags &= ~TDF_SIGWAIT; 1436 thread_unlock(td); 1437 1438 new_block = saved_mask; 1439 SIGSETNAND(new_block, td->td_sigmask); 1440 td->td_sigmask = saved_mask; 1441 /* 1442 * Fewer signals can be delivered to us, reschedule signal 1443 * notification. 1444 */ 1445 if (p->p_numthreads != 1) 1446 reschedule_signals(p, new_block, 0); 1447 1448 if (error == 0) { 1449 SDT_PROBE2(proc, , , signal__clear, sig, ksi); 1450 1451 if (ksi->ksi_code == SI_TIMER) 1452 itimer_accept(p, ksi->ksi_timerid, ksi); 1453 1454 #ifdef KTRACE 1455 if (KTRPOINT(td, KTR_PSIG)) { 1456 sig_t action; 1457 1458 mtx_lock(&ps->ps_mtx); 1459 action = ps->ps_sigact[_SIG_IDX(sig)]; 1460 mtx_unlock(&ps->ps_mtx); 1461 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 1462 } 1463 #endif 1464 if (sig == SIGKILL) { 1465 proc_td_siginfo_capture(td, &ksi->ksi_info); 1466 sigexit(td, sig); 1467 } 1468 } 1469 PROC_UNLOCK(p); 1470 return (error); 1471 } 1472 1473 #ifndef _SYS_SYSPROTO_H_ 1474 struct sigpending_args { 1475 sigset_t *set; 1476 }; 1477 #endif 1478 int 1479 sys_sigpending(struct thread *td, struct sigpending_args *uap) 1480 { 1481 struct proc *p = td->td_proc; 1482 sigset_t pending; 1483 1484 PROC_LOCK(p); 1485 pending = p->p_sigqueue.sq_signals; 1486 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1487 PROC_UNLOCK(p); 1488 return (copyout(&pending, uap->set, sizeof(sigset_t))); 1489 } 1490 1491 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1492 #ifndef _SYS_SYSPROTO_H_ 1493 struct osigpending_args { 1494 int dummy; 1495 }; 1496 #endif 1497 int 1498 osigpending(struct thread *td, struct osigpending_args *uap) 1499 { 1500 struct proc *p = td->td_proc; 1501 sigset_t pending; 1502 1503 PROC_LOCK(p); 1504 pending = p->p_sigqueue.sq_signals; 1505 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1506 PROC_UNLOCK(p); 1507 SIG2OSIG(pending, td->td_retval[0]); 1508 return (0); 1509 } 1510 #endif /* COMPAT_43 */ 1511 1512 #if defined(COMPAT_43) 1513 /* 1514 * Generalized interface signal handler, 4.3-compatible. 1515 */ 1516 #ifndef _SYS_SYSPROTO_H_ 1517 struct osigvec_args { 1518 int signum; 1519 struct sigvec *nsv; 1520 struct sigvec *osv; 1521 }; 1522 #endif 1523 /* ARGSUSED */ 1524 int 1525 osigvec(struct thread *td, struct osigvec_args *uap) 1526 { 1527 struct sigvec vec; 1528 struct sigaction nsa, osa; 1529 struct sigaction *nsap, *osap; 1530 int error; 1531 1532 if (uap->signum <= 0 || uap->signum >= ONSIG) 1533 return (EINVAL); 1534 nsap = (uap->nsv != NULL) ? &nsa : NULL; 1535 osap = (uap->osv != NULL) ? &osa : NULL; 1536 if (nsap) { 1537 error = copyin(uap->nsv, &vec, sizeof(vec)); 1538 if (error) 1539 return (error); 1540 nsap->sa_handler = vec.sv_handler; 1541 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 1542 nsap->sa_flags = vec.sv_flags; 1543 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1544 } 1545 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1546 if (osap && !error) { 1547 vec.sv_handler = osap->sa_handler; 1548 SIG2OSIG(osap->sa_mask, vec.sv_mask); 1549 vec.sv_flags = osap->sa_flags; 1550 vec.sv_flags &= ~SA_NOCLDWAIT; 1551 vec.sv_flags ^= SA_RESTART; 1552 error = copyout(&vec, uap->osv, sizeof(vec)); 1553 } 1554 return (error); 1555 } 1556 1557 #ifndef _SYS_SYSPROTO_H_ 1558 struct osigblock_args { 1559 int mask; 1560 }; 1561 #endif 1562 int 1563 osigblock(struct thread *td, struct osigblock_args *uap) 1564 { 1565 sigset_t set, oset; 1566 1567 OSIG2SIG(uap->mask, set); 1568 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1569 SIG2OSIG(oset, td->td_retval[0]); 1570 return (0); 1571 } 1572 1573 #ifndef _SYS_SYSPROTO_H_ 1574 struct osigsetmask_args { 1575 int mask; 1576 }; 1577 #endif 1578 int 1579 osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1580 { 1581 sigset_t set, oset; 1582 1583 OSIG2SIG(uap->mask, set); 1584 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1585 SIG2OSIG(oset, td->td_retval[0]); 1586 return (0); 1587 } 1588 #endif /* COMPAT_43 */ 1589 1590 /* 1591 * Suspend calling thread until signal, providing mask to be set in the 1592 * meantime. 1593 */ 1594 #ifndef _SYS_SYSPROTO_H_ 1595 struct sigsuspend_args { 1596 const sigset_t *sigmask; 1597 }; 1598 #endif 1599 /* ARGSUSED */ 1600 int 1601 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1602 { 1603 sigset_t mask; 1604 int error; 1605 1606 error = copyin(uap->sigmask, &mask, sizeof(mask)); 1607 if (error) 1608 return (error); 1609 return (kern_sigsuspend(td, mask)); 1610 } 1611 1612 int 1613 kern_sigsuspend(struct thread *td, sigset_t mask) 1614 { 1615 struct proc *p = td->td_proc; 1616 int has_sig, sig; 1617 1618 /* Ensure the sigfastblock value is up to date. */ 1619 sigfastblock_fetch(td); 1620 1621 /* 1622 * When returning from sigsuspend, we want 1623 * the old mask to be restored after the 1624 * signal handler has finished. Thus, we 1625 * save it here and mark the sigacts structure 1626 * to indicate this. 1627 */ 1628 PROC_LOCK(p); 1629 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 1630 SIGPROCMASK_PROC_LOCKED); 1631 td->td_pflags |= TDP_OLDMASK; 1632 ast_sched(td, TDA_SIGSUSPEND); 1633 1634 /* 1635 * Process signals now. Otherwise, we can get spurious wakeup 1636 * due to signal entered process queue, but delivered to other 1637 * thread. But sigsuspend should return only on signal 1638 * delivery. 1639 */ 1640 (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 1641 for (has_sig = 0; !has_sig;) { 1642 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, 1643 "sigsusp", 0) == 0) 1644 /* void */; 1645 thread_suspend_check(0); 1646 mtx_lock(&p->p_sigacts->ps_mtx); 1647 while ((sig = cursig(td)) != 0) { 1648 KASSERT(sig >= 0, ("sig %d", sig)); 1649 has_sig += postsig(sig); 1650 } 1651 mtx_unlock(&p->p_sigacts->ps_mtx); 1652 1653 /* 1654 * If PTRACE_SCE or PTRACE_SCX were set after 1655 * userspace entered the syscall, return spurious 1656 * EINTR. 1657 */ 1658 if ((p->p_ptevents & PTRACE_SYSCALL) != 0) 1659 has_sig += 1; 1660 } 1661 PROC_UNLOCK(p); 1662 td->td_errno = EINTR; 1663 td->td_pflags |= TDP_NERRNO; 1664 return (EJUSTRETURN); 1665 } 1666 1667 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1668 /* 1669 * Compatibility sigsuspend call for old binaries. Note nonstandard calling 1670 * convention: libc stub passes mask, not pointer, to save a copyin. 1671 */ 1672 #ifndef _SYS_SYSPROTO_H_ 1673 struct osigsuspend_args { 1674 osigset_t mask; 1675 }; 1676 #endif 1677 /* ARGSUSED */ 1678 int 1679 osigsuspend(struct thread *td, struct osigsuspend_args *uap) 1680 { 1681 sigset_t mask; 1682 1683 OSIG2SIG(uap->mask, mask); 1684 return (kern_sigsuspend(td, mask)); 1685 } 1686 #endif /* COMPAT_43 */ 1687 1688 #if defined(COMPAT_43) 1689 #ifndef _SYS_SYSPROTO_H_ 1690 struct osigstack_args { 1691 struct sigstack *nss; 1692 struct sigstack *oss; 1693 }; 1694 #endif 1695 /* ARGSUSED */ 1696 int 1697 osigstack(struct thread *td, struct osigstack_args *uap) 1698 { 1699 struct sigstack nss, oss; 1700 int error = 0; 1701 1702 if (uap->nss != NULL) { 1703 error = copyin(uap->nss, &nss, sizeof(nss)); 1704 if (error) 1705 return (error); 1706 } 1707 oss.ss_sp = td->td_sigstk.ss_sp; 1708 oss.ss_onstack = sigonstack(cpu_getstack(td)); 1709 if (uap->nss != NULL) { 1710 td->td_sigstk.ss_sp = nss.ss_sp; 1711 td->td_sigstk.ss_size = 0; 1712 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1713 td->td_pflags |= TDP_ALTSTACK; 1714 } 1715 if (uap->oss != NULL) 1716 error = copyout(&oss, uap->oss, sizeof(oss)); 1717 1718 return (error); 1719 } 1720 #endif /* COMPAT_43 */ 1721 1722 #ifndef _SYS_SYSPROTO_H_ 1723 struct sigaltstack_args { 1724 stack_t *ss; 1725 stack_t *oss; 1726 }; 1727 #endif 1728 /* ARGSUSED */ 1729 int 1730 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1731 { 1732 stack_t ss, oss; 1733 int error; 1734 1735 if (uap->ss != NULL) { 1736 error = copyin(uap->ss, &ss, sizeof(ss)); 1737 if (error) 1738 return (error); 1739 } 1740 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 1741 (uap->oss != NULL) ? &oss : NULL); 1742 if (error) 1743 return (error); 1744 if (uap->oss != NULL) 1745 error = copyout(&oss, uap->oss, sizeof(stack_t)); 1746 return (error); 1747 } 1748 1749 int 1750 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 1751 { 1752 struct proc *p = td->td_proc; 1753 int oonstack; 1754 1755 oonstack = sigonstack(cpu_getstack(td)); 1756 1757 if (oss != NULL) { 1758 *oss = td->td_sigstk; 1759 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1760 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1761 } 1762 1763 if (ss != NULL) { 1764 if (oonstack) 1765 return (EPERM); 1766 if ((ss->ss_flags & ~SS_DISABLE) != 0) 1767 return (EINVAL); 1768 if (!(ss->ss_flags & SS_DISABLE)) { 1769 if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1770 return (ENOMEM); 1771 1772 td->td_sigstk = *ss; 1773 td->td_pflags |= TDP_ALTSTACK; 1774 } else { 1775 td->td_pflags &= ~TDP_ALTSTACK; 1776 } 1777 } 1778 return (0); 1779 } 1780 1781 struct killpg1_ctx { 1782 struct thread *td; 1783 ksiginfo_t *ksi; 1784 int sig; 1785 bool sent; 1786 bool found; 1787 int ret; 1788 }; 1789 1790 static void 1791 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg) 1792 { 1793 int err; 1794 1795 err = p_cansignal(arg->td, p, arg->sig); 1796 if (err == 0 && arg->sig != 0) 1797 pksignal(p, arg->sig, arg->ksi); 1798 if (err != ESRCH) 1799 arg->found = true; 1800 if (err == 0) 1801 arg->sent = true; 1802 else if (arg->ret == 0 && err != ESRCH && err != EPERM) 1803 arg->ret = err; 1804 } 1805 1806 static void 1807 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg) 1808 { 1809 1810 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 1811 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW) 1812 return; 1813 1814 PROC_LOCK(p); 1815 killpg1_sendsig_locked(p, arg); 1816 PROC_UNLOCK(p); 1817 } 1818 1819 static void 1820 kill_processes_prison_cb(struct proc *p, void *arg) 1821 { 1822 struct killpg1_ctx *ctx = arg; 1823 1824 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || 1825 (p == ctx->td->td_proc) || p->p_state == PRS_NEW) 1826 return; 1827 1828 killpg1_sendsig_locked(p, ctx); 1829 } 1830 1831 /* 1832 * Common code for kill process group/broadcast kill. 1833 * td is the calling thread, as usual. 1834 */ 1835 static int 1836 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1837 { 1838 struct proc *p; 1839 struct pgrp *pgrp; 1840 struct killpg1_ctx arg; 1841 1842 arg.td = td; 1843 arg.ksi = ksi; 1844 arg.sig = sig; 1845 arg.sent = false; 1846 arg.found = false; 1847 arg.ret = 0; 1848 if (all) { 1849 /* 1850 * broadcast 1851 */ 1852 prison_proc_iterate(td->td_ucred->cr_prison, 1853 kill_processes_prison_cb, &arg); 1854 } else { 1855 again: 1856 sx_slock(&proctree_lock); 1857 if (pgid == 0) { 1858 /* 1859 * zero pgid means send to my process group. 1860 */ 1861 pgrp = td->td_proc->p_pgrp; 1862 PGRP_LOCK(pgrp); 1863 } else { 1864 pgrp = pgfind(pgid); 1865 if (pgrp == NULL) { 1866 sx_sunlock(&proctree_lock); 1867 return (ESRCH); 1868 } 1869 } 1870 sx_sunlock(&proctree_lock); 1871 if (!sx_try_xlock(&pgrp->pg_killsx)) { 1872 PGRP_UNLOCK(pgrp); 1873 sx_xlock(&pgrp->pg_killsx); 1874 sx_xunlock(&pgrp->pg_killsx); 1875 goto again; 1876 } 1877 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1878 killpg1_sendsig(p, false, &arg); 1879 } 1880 PGRP_UNLOCK(pgrp); 1881 sx_xunlock(&pgrp->pg_killsx); 1882 } 1883 MPASS(arg.ret != 0 || arg.found || !arg.sent); 1884 if (arg.ret == 0 && !arg.sent) 1885 arg.ret = arg.found ? EPERM : ESRCH; 1886 return (arg.ret); 1887 } 1888 1889 #ifndef _SYS_SYSPROTO_H_ 1890 struct kill_args { 1891 int pid; 1892 int signum; 1893 }; 1894 #endif 1895 /* ARGSUSED */ 1896 int 1897 sys_kill(struct thread *td, struct kill_args *uap) 1898 { 1899 1900 return (kern_kill(td, uap->pid, uap->signum)); 1901 } 1902 1903 int 1904 kern_kill(struct thread *td, pid_t pid, int signum) 1905 { 1906 ksiginfo_t ksi; 1907 struct proc *p; 1908 int error; 1909 1910 /* 1911 * A process in capability mode can send signals only to himself. 1912 * The main rationale behind this is that abort(3) is implemented as 1913 * kill(getpid(), SIGABRT). 1914 */ 1915 if (pid != td->td_proc->p_pid) { 1916 if (CAP_TRACING(td)) 1917 ktrcapfail(CAPFAIL_SIGNAL, &signum); 1918 if (IN_CAPABILITY_MODE(td)) 1919 return (ECAPMODE); 1920 } 1921 1922 AUDIT_ARG_SIGNUM(signum); 1923 AUDIT_ARG_PID(pid); 1924 if ((u_int)signum > _SIG_MAXSIG) 1925 return (EINVAL); 1926 1927 ksiginfo_init(&ksi); 1928 ksi.ksi_signo = signum; 1929 ksi.ksi_code = SI_USER; 1930 ksi.ksi_pid = td->td_proc->p_pid; 1931 ksi.ksi_uid = td->td_ucred->cr_ruid; 1932 1933 if (pid > 0) { 1934 /* kill single process */ 1935 if ((p = pfind_any(pid)) == NULL) 1936 return (ESRCH); 1937 AUDIT_ARG_PROCESS(p); 1938 error = p_cansignal(td, p, signum); 1939 if (error == 0 && signum) 1940 pksignal(p, signum, &ksi); 1941 PROC_UNLOCK(p); 1942 return (error); 1943 } 1944 switch (pid) { 1945 case -1: /* broadcast signal */ 1946 return (killpg1(td, signum, 0, 1, &ksi)); 1947 case 0: /* signal own process group */ 1948 return (killpg1(td, signum, 0, 0, &ksi)); 1949 default: /* negative explicit process group */ 1950 return (killpg1(td, signum, -pid, 0, &ksi)); 1951 } 1952 /* NOTREACHED */ 1953 } 1954 1955 int 1956 sys_pdkill(struct thread *td, struct pdkill_args *uap) 1957 { 1958 struct proc *p; 1959 int error; 1960 1961 AUDIT_ARG_SIGNUM(uap->signum); 1962 AUDIT_ARG_FD(uap->fd); 1963 if ((u_int)uap->signum > _SIG_MAXSIG) 1964 return (EINVAL); 1965 1966 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p); 1967 if (error) 1968 return (error); 1969 AUDIT_ARG_PROCESS(p); 1970 error = p_cansignal(td, p, uap->signum); 1971 if (error == 0 && uap->signum) 1972 kern_psignal(p, uap->signum); 1973 PROC_UNLOCK(p); 1974 return (error); 1975 } 1976 1977 #if defined(COMPAT_43) 1978 #ifndef _SYS_SYSPROTO_H_ 1979 struct okillpg_args { 1980 int pgid; 1981 int signum; 1982 }; 1983 #endif 1984 /* ARGSUSED */ 1985 int 1986 okillpg(struct thread *td, struct okillpg_args *uap) 1987 { 1988 ksiginfo_t ksi; 1989 1990 AUDIT_ARG_SIGNUM(uap->signum); 1991 AUDIT_ARG_PID(uap->pgid); 1992 if ((u_int)uap->signum > _SIG_MAXSIG) 1993 return (EINVAL); 1994 1995 ksiginfo_init(&ksi); 1996 ksi.ksi_signo = uap->signum; 1997 ksi.ksi_code = SI_USER; 1998 ksi.ksi_pid = td->td_proc->p_pid; 1999 ksi.ksi_uid = td->td_ucred->cr_ruid; 2000 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 2001 } 2002 #endif /* COMPAT_43 */ 2003 2004 #ifndef _SYS_SYSPROTO_H_ 2005 struct sigqueue_args { 2006 pid_t pid; 2007 int signum; 2008 /* union sigval */ void *value; 2009 }; 2010 #endif 2011 int 2012 sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 2013 { 2014 union sigval sv; 2015 2016 sv.sival_ptr = uap->value; 2017 2018 return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 2019 } 2020 2021 int 2022 kern_sigqueue(struct thread *td, pid_t pid, int signumf, union sigval *value) 2023 { 2024 ksiginfo_t ksi; 2025 struct proc *p; 2026 struct thread *td2; 2027 u_int signum; 2028 int error; 2029 2030 signum = signumf & ~__SIGQUEUE_TID; 2031 if (signum > _SIG_MAXSIG) 2032 return (EINVAL); 2033 2034 /* 2035 * Specification says sigqueue can only send signal to 2036 * single process. 2037 */ 2038 if (pid <= 0) 2039 return (EINVAL); 2040 2041 if ((signumf & __SIGQUEUE_TID) == 0) { 2042 if ((p = pfind_any(pid)) == NULL) 2043 return (ESRCH); 2044 td2 = NULL; 2045 } else { 2046 p = td->td_proc; 2047 td2 = tdfind((lwpid_t)pid, p->p_pid); 2048 if (td2 == NULL) 2049 return (ESRCH); 2050 } 2051 2052 error = p_cansignal(td, p, signum); 2053 if (error == 0 && signum != 0) { 2054 ksiginfo_init(&ksi); 2055 ksi.ksi_flags = KSI_SIGQ; 2056 ksi.ksi_signo = signum; 2057 ksi.ksi_code = SI_QUEUE; 2058 ksi.ksi_pid = td->td_proc->p_pid; 2059 ksi.ksi_uid = td->td_ucred->cr_ruid; 2060 ksi.ksi_value = *value; 2061 error = tdsendsignal(p, td2, ksi.ksi_signo, &ksi); 2062 } 2063 PROC_UNLOCK(p); 2064 return (error); 2065 } 2066 2067 /* 2068 * Send a signal to a process group. If checktty is 1, 2069 * limit to members which have a controlling terminal. 2070 */ 2071 void 2072 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 2073 { 2074 struct proc *p; 2075 2076 if (pgrp) { 2077 PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 2078 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 2079 PROC_LOCK(p); 2080 if (p->p_state == PRS_NORMAL && 2081 (checkctty == 0 || p->p_flag & P_CONTROLT)) 2082 pksignal(p, sig, ksi); 2083 PROC_UNLOCK(p); 2084 } 2085 } 2086 } 2087 2088 /* 2089 * Recalculate the signal mask and reset the signal disposition after 2090 * usermode frame for delivery is formed. Should be called after 2091 * mach-specific routine, because sysent->sv_sendsig() needs correct 2092 * ps_siginfo and signal mask. 2093 */ 2094 static void 2095 postsig_done(int sig, struct thread *td, struct sigacts *ps) 2096 { 2097 sigset_t mask; 2098 2099 mtx_assert(&ps->ps_mtx, MA_OWNED); 2100 td->td_ru.ru_nsignals++; 2101 mask = ps->ps_catchmask[_SIG_IDX(sig)]; 2102 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2103 SIGADDSET(mask, sig); 2104 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 2105 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 2106 if (SIGISMEMBER(ps->ps_sigreset, sig)) 2107 sigdflt(ps, sig); 2108 } 2109 2110 /* 2111 * Send a signal caused by a trap to the current thread. If it will be 2112 * caught immediately, deliver it with correct code. Otherwise, post it 2113 * normally. 2114 */ 2115 void 2116 trapsignal(struct thread *td, ksiginfo_t *ksi) 2117 { 2118 struct sigacts *ps; 2119 struct proc *p; 2120 sigset_t sigmask; 2121 int sig; 2122 2123 p = td->td_proc; 2124 sig = ksi->ksi_signo; 2125 KASSERT(_SIG_VALID(sig), ("invalid signal")); 2126 2127 sigfastblock_fetch(td); 2128 PROC_LOCK(p); 2129 ps = p->p_sigacts; 2130 mtx_lock(&ps->ps_mtx); 2131 sigmask = td->td_sigmask; 2132 if (td->td_sigblock_val != 0) 2133 SIGSETOR(sigmask, fastblock_mask); 2134 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 2135 !SIGISMEMBER(sigmask, sig)) { 2136 #ifdef KTRACE 2137 if (KTRPOINT(curthread, KTR_PSIG)) 2138 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 2139 &td->td_sigmask, ksi->ksi_code); 2140 #endif 2141 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 2142 ksi, &td->td_sigmask); 2143 postsig_done(sig, td, ps); 2144 mtx_unlock(&ps->ps_mtx); 2145 } else { 2146 /* 2147 * Avoid a possible infinite loop if the thread 2148 * masking the signal or process is ignoring the 2149 * signal. 2150 */ 2151 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) || 2152 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 2153 SIGDELSET(td->td_sigmask, sig); 2154 SIGDELSET(ps->ps_sigcatch, sig); 2155 SIGDELSET(ps->ps_sigignore, sig); 2156 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2157 td->td_pflags &= ~TDP_SIGFASTBLOCK; 2158 td->td_sigblock_val = 0; 2159 } 2160 mtx_unlock(&ps->ps_mtx); 2161 p->p_sig = sig; /* XXX to verify code */ 2162 tdsendsignal(p, td, sig, ksi); 2163 } 2164 PROC_UNLOCK(p); 2165 } 2166 2167 static struct thread * 2168 sigtd(struct proc *p, int sig, bool fast_sigblock) 2169 { 2170 struct thread *td, *signal_td; 2171 2172 PROC_LOCK_ASSERT(p, MA_OWNED); 2173 MPASS(!fast_sigblock || p == curproc); 2174 2175 /* 2176 * Check if current thread can handle the signal without 2177 * switching context to another thread. 2178 */ 2179 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) && 2180 (!fast_sigblock || curthread->td_sigblock_val == 0)) 2181 return (curthread); 2182 2183 /* Find a non-stopped thread that does not mask the signal. */ 2184 signal_td = NULL; 2185 FOREACH_THREAD_IN_PROC(p, td) { 2186 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock || 2187 td != curthread || td->td_sigblock_val == 0) && 2188 (td->td_flags & TDF_BOUNDARY) == 0) { 2189 signal_td = td; 2190 break; 2191 } 2192 } 2193 /* Select random (first) thread if no better match was found. */ 2194 if (signal_td == NULL) 2195 signal_td = FIRST_THREAD_IN_PROC(p); 2196 return (signal_td); 2197 } 2198 2199 /* 2200 * Send the signal to the process. If the signal has an action, the action 2201 * is usually performed by the target process rather than the caller; we add 2202 * the signal to the set of pending signals for the process. 2203 * 2204 * Exceptions: 2205 * o When a stop signal is sent to a sleeping process that takes the 2206 * default action, the process is stopped without awakening it. 2207 * o SIGCONT restarts stopped processes (or puts them back to sleep) 2208 * regardless of the signal action (eg, blocked or ignored). 2209 * 2210 * Other ignored signals are discarded immediately. 2211 * 2212 * NB: This function may be entered from the debugger via the "kill" DDB 2213 * command. There is little that can be done to mitigate the possibly messy 2214 * side effects of this unwise possibility. 2215 */ 2216 void 2217 kern_psignal(struct proc *p, int sig) 2218 { 2219 ksiginfo_t ksi; 2220 2221 ksiginfo_init(&ksi); 2222 ksi.ksi_signo = sig; 2223 ksi.ksi_code = SI_KERNEL; 2224 (void) tdsendsignal(p, NULL, sig, &ksi); 2225 } 2226 2227 int 2228 pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2229 { 2230 2231 return (tdsendsignal(p, NULL, sig, ksi)); 2232 } 2233 2234 /* Utility function for finding a thread to send signal event to. */ 2235 int 2236 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd) 2237 { 2238 struct thread *td; 2239 2240 if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2241 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 2242 if (td == NULL) 2243 return (ESRCH); 2244 *ttd = td; 2245 } else { 2246 *ttd = NULL; 2247 PROC_LOCK(p); 2248 } 2249 return (0); 2250 } 2251 2252 void 2253 tdsignal(struct thread *td, int sig) 2254 { 2255 ksiginfo_t ksi; 2256 2257 ksiginfo_init(&ksi); 2258 ksi.ksi_signo = sig; 2259 ksi.ksi_code = SI_KERNEL; 2260 (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2261 } 2262 2263 void 2264 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2265 { 2266 2267 (void) tdsendsignal(td->td_proc, td, sig, ksi); 2268 } 2269 2270 static void 2271 sig_sleepq_abort(struct thread *td, int intrval) 2272 { 2273 THREAD_LOCK_ASSERT(td, MA_OWNED); 2274 2275 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) 2276 thread_unlock(td); 2277 else 2278 sleepq_abort(td, intrval); 2279 } 2280 2281 int 2282 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 2283 { 2284 sig_t action; 2285 sigqueue_t *sigqueue; 2286 struct sigacts *ps; 2287 int intrval, prop, ret; 2288 2289 MPASS(td == NULL || p == td->td_proc); 2290 PROC_LOCK_ASSERT(p, MA_OWNED); 2291 2292 if (!_SIG_VALID(sig)) 2293 panic("%s(): invalid signal %d", __func__, sig); 2294 2295 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 2296 2297 /* 2298 * IEEE Std 1003.1-2001: return success when killing a zombie. 2299 */ 2300 if (p->p_state == PRS_ZOMBIE) { 2301 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 2302 ksiginfo_tryfree(ksi); 2303 return (0); 2304 } 2305 2306 ps = p->p_sigacts; 2307 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 2308 prop = sigprop(sig); 2309 2310 if (td == NULL) { 2311 td = sigtd(p, sig, false); 2312 sigqueue = &p->p_sigqueue; 2313 } else 2314 sigqueue = &td->td_sigqueue; 2315 2316 SDT_PROBE3(proc, , , signal__send, td, p, sig); 2317 2318 /* 2319 * If the signal is being ignored, then we forget about it 2320 * immediately, except when the target process executes 2321 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore, 2322 * and if it is set to SIG_IGN, action will be SIG_DFL here.) 2323 */ 2324 mtx_lock(&ps->ps_mtx); 2325 if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2326 if (kern_sig_discard_ign && 2327 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) { 2328 SDT_PROBE3(proc, , , signal__discard, td, p, sig); 2329 2330 mtx_unlock(&ps->ps_mtx); 2331 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 2332 ksiginfo_tryfree(ksi); 2333 return (0); 2334 } else { 2335 action = SIG_CATCH; 2336 intrval = 0; 2337 } 2338 } else { 2339 if (SIGISMEMBER(td->td_sigmask, sig)) 2340 action = SIG_HOLD; 2341 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2342 action = SIG_CATCH; 2343 else 2344 action = SIG_DFL; 2345 if (SIGISMEMBER(ps->ps_sigintr, sig)) 2346 intrval = EINTR; 2347 else 2348 intrval = ERESTART; 2349 } 2350 mtx_unlock(&ps->ps_mtx); 2351 2352 if (prop & SIGPROP_CONT) 2353 sigqueue_delete_stopmask_proc(p); 2354 else if (prop & SIGPROP_STOP) { 2355 if (pt_attach_transparent && 2356 (p->p_flag & P_TRACED) != 0 && 2357 (p->p_flag2 & P2_PTRACE_FSTP) != 0) { 2358 PROC_SLOCK(p); 2359 sig_handle_first_stop(NULL, p, sig); 2360 PROC_SUNLOCK(p); 2361 return (0); 2362 } 2363 2364 /* 2365 * If sending a tty stop signal to a member of an orphaned 2366 * process group, discard the signal here if the action 2367 * is default; don't stop the process below if sleeping, 2368 * and don't clear any pending SIGCONT. 2369 */ 2370 if ((prop & SIGPROP_TTYSTOP) != 0 && 2371 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 && 2372 action == SIG_DFL) { 2373 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0) 2374 ksiginfo_tryfree(ksi); 2375 return (0); 2376 } 2377 sigqueue_delete_proc(p, SIGCONT); 2378 if (p->p_flag & P_CONTINUED) { 2379 p->p_flag &= ~P_CONTINUED; 2380 PROC_LOCK(p->p_pptr); 2381 sigqueue_take(p->p_ksi); 2382 PROC_UNLOCK(p->p_pptr); 2383 } 2384 } 2385 2386 ret = sigqueue_add(sigqueue, sig, ksi); 2387 if (ret != 0) 2388 return (ret); 2389 signotify(td); 2390 /* 2391 * Defer further processing for signals which are held, 2392 * except that stopped processes must be continued by SIGCONT. 2393 */ 2394 if (action == SIG_HOLD && 2395 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 2396 return (0); 2397 2398 /* 2399 * Some signals have a process-wide effect and a per-thread 2400 * component. Most processing occurs when the process next 2401 * tries to cross the user boundary, however there are some 2402 * times when processing needs to be done immediately, such as 2403 * waking up threads so that they can cross the user boundary. 2404 * We try to do the per-process part here. 2405 */ 2406 if (P_SHOULDSTOP(p)) { 2407 KASSERT(!(p->p_flag & P_WEXIT), 2408 ("signal to stopped but exiting process")); 2409 if (sig == SIGKILL) { 2410 /* 2411 * If traced process is already stopped, 2412 * then no further action is necessary. 2413 */ 2414 if (p->p_flag & P_TRACED) 2415 return (0); 2416 /* 2417 * SIGKILL sets process running. 2418 * It will die elsewhere. 2419 * All threads must be restarted. 2420 */ 2421 p->p_flag &= ~P_STOPPED_SIG; 2422 goto runfast; 2423 } 2424 2425 if (prop & SIGPROP_CONT) { 2426 /* 2427 * If traced process is already stopped, 2428 * then no further action is necessary. 2429 */ 2430 if (p->p_flag & P_TRACED) 2431 return (0); 2432 /* 2433 * If SIGCONT is default (or ignored), we continue the 2434 * process but don't leave the signal in sigqueue as 2435 * it has no further action. If SIGCONT is held, we 2436 * continue the process and leave the signal in 2437 * sigqueue. If the process catches SIGCONT, let it 2438 * handle the signal itself. If it isn't waiting on 2439 * an event, it goes back to run state. 2440 * Otherwise, process goes back to sleep state. 2441 */ 2442 p->p_flag &= ~P_STOPPED_SIG; 2443 PROC_SLOCK(p); 2444 if (p->p_numthreads == p->p_suspcount) { 2445 PROC_SUNLOCK(p); 2446 PROC_LOCK(p->p_pptr); 2447 childproc_continued(p); 2448 PROC_UNLOCK(p->p_pptr); 2449 PROC_SLOCK(p); 2450 } 2451 if (action == SIG_DFL) { 2452 thread_unsuspend(p); 2453 PROC_SUNLOCK(p); 2454 sigqueue_delete(sigqueue, sig); 2455 goto out_cont; 2456 } 2457 if (action == SIG_CATCH) { 2458 /* 2459 * The process wants to catch it so it needs 2460 * to run at least one thread, but which one? 2461 */ 2462 PROC_SUNLOCK(p); 2463 goto runfast; 2464 } 2465 /* 2466 * The signal is not ignored or caught. 2467 */ 2468 thread_unsuspend(p); 2469 PROC_SUNLOCK(p); 2470 goto out_cont; 2471 } 2472 2473 if (prop & SIGPROP_STOP) { 2474 /* 2475 * If traced process is already stopped, 2476 * then no further action is necessary. 2477 */ 2478 if (p->p_flag & P_TRACED) 2479 return (0); 2480 /* 2481 * Already stopped, don't need to stop again 2482 * (If we did the shell could get confused). 2483 * Just make sure the signal STOP bit set. 2484 */ 2485 p->p_flag |= P_STOPPED_SIG; 2486 sigqueue_delete(sigqueue, sig); 2487 return (0); 2488 } 2489 2490 /* 2491 * All other kinds of signals: 2492 * If a thread is sleeping interruptibly, simulate a 2493 * wakeup so that when it is continued it will be made 2494 * runnable and can look at the signal. However, don't make 2495 * the PROCESS runnable, leave it stopped. 2496 * It may run a bit until it hits a thread_suspend_check(). 2497 */ 2498 PROC_SLOCK(p); 2499 thread_lock(td); 2500 if (TD_CAN_ABORT(td)) 2501 sig_sleepq_abort(td, intrval); 2502 else 2503 thread_unlock(td); 2504 PROC_SUNLOCK(p); 2505 return (0); 2506 /* 2507 * Mutexes are short lived. Threads waiting on them will 2508 * hit thread_suspend_check() soon. 2509 */ 2510 } else if (p->p_state == PRS_NORMAL) { 2511 if (p->p_flag & P_TRACED || action == SIG_CATCH) { 2512 tdsigwakeup(td, sig, action, intrval); 2513 return (0); 2514 } 2515 2516 MPASS(action == SIG_DFL); 2517 2518 if (prop & SIGPROP_STOP) { 2519 if (p->p_flag & (P_PPWAIT|P_WEXIT)) 2520 return (0); 2521 p->p_flag |= P_STOPPED_SIG; 2522 p->p_xsig = sig; 2523 PROC_SLOCK(p); 2524 sig_suspend_threads(td, p); 2525 if (p->p_numthreads == p->p_suspcount) { 2526 /* 2527 * only thread sending signal to another 2528 * process can reach here, if thread is sending 2529 * signal to its process, because thread does 2530 * not suspend itself here, p_numthreads 2531 * should never be equal to p_suspcount. 2532 */ 2533 thread_stopped(p); 2534 PROC_SUNLOCK(p); 2535 sigqueue_delete_proc(p, p->p_xsig); 2536 } else 2537 PROC_SUNLOCK(p); 2538 return (0); 2539 } 2540 } else { 2541 /* Not in "NORMAL" state. discard the signal. */ 2542 sigqueue_delete(sigqueue, sig); 2543 return (0); 2544 } 2545 2546 /* 2547 * The process is not stopped so we need to apply the signal to all the 2548 * running threads. 2549 */ 2550 runfast: 2551 tdsigwakeup(td, sig, action, intrval); 2552 PROC_SLOCK(p); 2553 thread_unsuspend(p); 2554 PROC_SUNLOCK(p); 2555 out_cont: 2556 itimer_proc_continue(p); 2557 kqtimer_proc_continue(p); 2558 2559 return (0); 2560 } 2561 2562 /* 2563 * The force of a signal has been directed against a single 2564 * thread. We need to see what we can do about knocking it 2565 * out of any sleep it may be in etc. 2566 */ 2567 static void 2568 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2569 { 2570 struct proc *p = td->td_proc; 2571 int prop; 2572 2573 PROC_LOCK_ASSERT(p, MA_OWNED); 2574 prop = sigprop(sig); 2575 2576 PROC_SLOCK(p); 2577 thread_lock(td); 2578 /* 2579 * Bring the priority of a thread up if we want it to get 2580 * killed in this lifetime. Be careful to avoid bumping the 2581 * priority of the idle thread, since we still allow to signal 2582 * kernel processes. 2583 */ 2584 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2585 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2586 sched_prio(td, PUSER); 2587 if (TD_ON_SLEEPQ(td)) { 2588 /* 2589 * If thread is sleeping uninterruptibly 2590 * we can't interrupt the sleep... the signal will 2591 * be noticed when the process returns through 2592 * trap() or syscall(). 2593 */ 2594 if ((td->td_flags & TDF_SINTR) == 0) 2595 goto out; 2596 /* 2597 * If SIGCONT is default (or ignored) and process is 2598 * asleep, we are finished; the process should not 2599 * be awakened. 2600 */ 2601 if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2602 thread_unlock(td); 2603 PROC_SUNLOCK(p); 2604 sigqueue_delete(&p->p_sigqueue, sig); 2605 /* 2606 * It may be on either list in this state. 2607 * Remove from both for now. 2608 */ 2609 sigqueue_delete(&td->td_sigqueue, sig); 2610 return; 2611 } 2612 2613 /* 2614 * Don't awaken a sleeping thread for SIGSTOP if the 2615 * STOP signal is deferred. 2616 */ 2617 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 2618 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2619 goto out; 2620 2621 /* 2622 * Give low priority threads a better chance to run. 2623 */ 2624 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2625 sched_prio(td, PUSER); 2626 2627 sig_sleepq_abort(td, intrval); 2628 PROC_SUNLOCK(p); 2629 return; 2630 } 2631 2632 /* 2633 * Other states do nothing with the signal immediately, 2634 * other than kicking ourselves if we are running. 2635 * It will either never be noticed, or noticed very soon. 2636 */ 2637 #ifdef SMP 2638 if (TD_IS_RUNNING(td) && td != curthread) 2639 forward_signal(td); 2640 #endif 2641 2642 out: 2643 PROC_SUNLOCK(p); 2644 thread_unlock(td); 2645 } 2646 2647 static void 2648 ptrace_coredumpreq(struct thread *td, struct proc *p, 2649 struct thr_coredump_req *tcq) 2650 { 2651 struct coredump_vnode_ctx wctx; 2652 struct coredump_writer cdw; 2653 void *rl_cookie; 2654 2655 if (p->p_sysent->sv_coredump == NULL) { 2656 tcq->tc_error = ENOSYS; 2657 return; 2658 } 2659 2660 memset(&wctx, 0, sizeof(wctx)); 2661 wctx.vp = tcq->tc_vp; 2662 wctx.fcred = NOCRED; 2663 2664 memset(&cdw, 0, sizeof(wctx)); 2665 cdw.ctx = &wctx; 2666 cdw.write_fn = core_vn_write; 2667 cdw.extend_fn = core_vn_extend; 2668 2669 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX); 2670 tcq->tc_error = p->p_sysent->sv_coredump(td, &cdw, 2671 tcq->tc_limit, tcq->tc_flags); 2672 vn_rangelock_unlock(tcq->tc_vp, rl_cookie); 2673 } 2674 2675 static void 2676 ptrace_syscallreq(struct thread *td, struct proc *p, 2677 struct thr_syscall_req *tsr) 2678 { 2679 struct sysentvec *sv; 2680 struct sysent *se; 2681 register_t rv_saved[2]; 2682 unsigned int sc; 2683 int error, nerror; 2684 bool audited, sy_thr_static; 2685 2686 sc = tsr->ts_sa.code; 2687 if (sc == SYS_syscall || sc == SYS___syscall) { 2688 if (tsr->ts_nargs == 0) { 2689 tsr->ts_ret.sr_error = EINVAL; 2690 return; 2691 } 2692 sc = tsr->ts_sa.args[0]; 2693 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1], 2694 sizeof(register_t) * (tsr->ts_nargs - 1)); 2695 } 2696 2697 sv = p->p_sysent; 2698 if (sv->sv_table == NULL || sc >= sv->sv_size) { 2699 tsr->ts_ret.sr_error = ENOSYS; 2700 return; 2701 } 2702 tsr->ts_sa.callp = se = &sv->sv_table[sc]; 2703 2704 VM_CNT_INC(v_syscall); 2705 td->td_pticks = 0; 2706 if (__predict_false(td->td_cowgen != atomic_load_int( 2707 &td->td_proc->p_cowgen))) 2708 thread_cow_update(td); 2709 2710 td->td_sa = tsr->ts_sa; 2711 2712 #ifdef CAPABILITY_MODE 2713 if ((se->sy_flags & SYF_CAPENABLED) == 0) { 2714 if (CAP_TRACING(td)) 2715 ktrcapfail(CAPFAIL_SYSCALL, NULL); 2716 if (IN_CAPABILITY_MODE(td)) { 2717 tsr->ts_ret.sr_error = ECAPMODE; 2718 return; 2719 } 2720 } 2721 #endif 2722 2723 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0; 2724 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0; 2725 2726 if (!sy_thr_static) { 2727 error = syscall_thread_enter(td, &se); 2728 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0; 2729 if (error != 0) { 2730 tsr->ts_ret.sr_error = error; 2731 return; 2732 } 2733 } 2734 2735 rv_saved[0] = td->td_retval[0]; 2736 rv_saved[1] = td->td_retval[1]; 2737 nerror = td->td_errno; 2738 td->td_retval[0] = 0; 2739 td->td_retval[1] = 0; 2740 2741 #ifdef KDTRACE_HOOKS 2742 if (se->sy_entry != 0) 2743 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0); 2744 #endif 2745 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args); 2746 #ifdef KDTRACE_HOOKS 2747 if (se->sy_return != 0) 2748 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN, 2749 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]); 2750 #endif 2751 2752 tsr->ts_ret.sr_retval[0] = td->td_retval[0]; 2753 tsr->ts_ret.sr_retval[1] = td->td_retval[1]; 2754 td->td_retval[0] = rv_saved[0]; 2755 td->td_retval[1] = rv_saved[1]; 2756 td->td_errno = nerror; 2757 2758 if (audited) 2759 AUDIT_SYSCALL_EXIT(error, td); 2760 if (!sy_thr_static) 2761 syscall_thread_exit(td, se); 2762 } 2763 2764 static void 2765 ptrace_remotereq(struct thread *td, int flag) 2766 { 2767 struct proc *p; 2768 2769 MPASS(td == curthread); 2770 p = td->td_proc; 2771 PROC_LOCK_ASSERT(p, MA_OWNED); 2772 if ((td->td_dbgflags & flag) == 0) 2773 return; 2774 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped")); 2775 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL")); 2776 2777 PROC_UNLOCK(p); 2778 switch (flag) { 2779 case TDB_COREDUMPREQ: 2780 ptrace_coredumpreq(td, p, td->td_remotereq); 2781 break; 2782 case TDB_SCREMOTEREQ: 2783 ptrace_syscallreq(td, p, td->td_remotereq); 2784 break; 2785 default: 2786 __unreachable(); 2787 } 2788 PROC_LOCK(p); 2789 2790 MPASS((td->td_dbgflags & flag) != 0); 2791 td->td_dbgflags &= ~flag; 2792 td->td_remotereq = NULL; 2793 wakeup(p); 2794 } 2795 2796 /* 2797 * Suspend threads of the process p, either by directly setting the 2798 * inhibitor for the thread sleeping interruptibly, or by making the 2799 * thread suspend at the userspace boundary by scheduling a suspend AST. 2800 * 2801 * Returns true if some threads were suspended directly from the 2802 * sleeping state, and false if all threads are forced to process AST. 2803 */ 2804 static bool 2805 sig_suspend_threads(struct thread *td, struct proc *p) 2806 { 2807 struct thread *td2; 2808 bool res; 2809 2810 PROC_LOCK_ASSERT(p, MA_OWNED); 2811 PROC_SLOCK_ASSERT(p, MA_OWNED); 2812 2813 res = false; 2814 FOREACH_THREAD_IN_PROC(p, td2) { 2815 thread_lock(td2); 2816 ast_sched_locked(td2, TDA_SUSPEND); 2817 if (TD_IS_SLEEPING(td2) && (td2->td_flags & TDF_SINTR) != 0) { 2818 if (td2->td_flags & TDF_SBDRY) { 2819 /* 2820 * Once a thread is asleep with 2821 * TDF_SBDRY and without TDF_SERESTART 2822 * or TDF_SEINTR set, it should never 2823 * become suspended due to this check. 2824 */ 2825 KASSERT(!TD_IS_SUSPENDED(td2), 2826 ("thread with deferred stops suspended")); 2827 if (TD_SBDRY_INTR(td2)) { 2828 sleepq_abort(td2, TD_SBDRY_ERRNO(td2)); 2829 continue; 2830 } 2831 } else if (!TD_IS_SUSPENDED(td2)) { 2832 thread_suspend_one(td2); 2833 res = true; 2834 } 2835 } else if (!TD_IS_SUSPENDED(td2)) { 2836 #ifdef SMP 2837 if (TD_IS_RUNNING(td2) && td2 != td) 2838 forward_signal(td2); 2839 #endif 2840 } 2841 thread_unlock(td2); 2842 } 2843 return (res); 2844 } 2845 2846 static void 2847 sig_handle_first_stop(struct thread *td, struct proc *p, int sig) 2848 { 2849 if (td != NULL && (td->td_dbgflags & TDB_FSTP) == 0 && 2850 ((p->p_flag2 & P2_PTRACE_FSTP) != 0 || p->p_xthread != NULL)) 2851 return; 2852 2853 p->p_xsig = sig; 2854 p->p_xthread = td; 2855 2856 /* 2857 * If we are on sleepqueue already, let sleepqueue 2858 * code decide if it needs to go sleep after attach. 2859 */ 2860 if (td != NULL && td->td_wchan == NULL) 2861 td->td_dbgflags &= ~TDB_FSTP; 2862 2863 p->p_flag2 &= ~P2_PTRACE_FSTP; 2864 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2865 if (sig_suspend_threads(td, p) && td == NULL) 2866 thread_stopped(p); 2867 } 2868 2869 /* 2870 * Stop the process for an event deemed interesting to the debugger. If si is 2871 * non-NULL, this is a signal exchange; the new signal requested by the 2872 * debugger will be returned for handling. If si is NULL, this is some other 2873 * type of interesting event. The debugger may request a signal be delivered in 2874 * that case as well, however it will be deferred until it can be handled. 2875 */ 2876 int 2877 ptracestop(struct thread *td, int sig, ksiginfo_t *si) 2878 { 2879 struct proc *p = td->td_proc; 2880 struct thread *td2; 2881 ksiginfo_t ksi; 2882 2883 PROC_LOCK_ASSERT(p, MA_OWNED); 2884 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 2885 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2886 &p->p_mtx.lock_object, "Stopping for traced signal"); 2887 2888 td->td_xsig = sig; 2889 2890 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 2891 td->td_dbgflags |= TDB_XSIG; 2892 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2893 td->td_tid, p->p_pid, td->td_dbgflags, sig); 2894 PROC_SLOCK(p); 2895 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 2896 if (P_KILLED(p)) { 2897 /* 2898 * Ensure that, if we've been PT_KILLed, the 2899 * exit status reflects that. Another thread 2900 * may also be in ptracestop(), having just 2901 * received the SIGKILL, but this thread was 2902 * unsuspended first. 2903 */ 2904 td->td_dbgflags &= ~TDB_XSIG; 2905 td->td_xsig = SIGKILL; 2906 p->p_ptevents = 0; 2907 break; 2908 } 2909 if (p->p_flag & P_SINGLE_EXIT && 2910 !(td->td_dbgflags & TDB_EXIT)) { 2911 /* 2912 * Ignore ptrace stops except for thread exit 2913 * events when the process exits. 2914 */ 2915 td->td_dbgflags &= ~TDB_XSIG; 2916 PROC_SUNLOCK(p); 2917 return (0); 2918 } 2919 2920 /* 2921 * Make wait(2) work. Ensure that right after the 2922 * attach, the thread which was decided to become the 2923 * leader of attach gets reported to the waiter. 2924 * Otherwise, just avoid overwriting another thread's 2925 * assignment to p_xthread. If another thread has 2926 * already set p_xthread, the current thread will get 2927 * a chance to report itself upon the next iteration. 2928 */ 2929 sig_handle_first_stop(td, p, sig); 2930 2931 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 2932 td->td_dbgflags &= ~TDB_STOPATFORK; 2933 } 2934 stopme: 2935 td->td_dbgflags |= TDB_SSWITCH; 2936 thread_suspend_switch(td, p); 2937 td->td_dbgflags &= ~TDB_SSWITCH; 2938 if ((td->td_dbgflags & (TDB_COREDUMPREQ | 2939 TDB_SCREMOTEREQ)) != 0) { 2940 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ | 2941 TDB_SCREMOTEREQ)) != 2942 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ)); 2943 PROC_SUNLOCK(p); 2944 ptrace_remotereq(td, td->td_dbgflags & 2945 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ)); 2946 PROC_SLOCK(p); 2947 goto stopme; 2948 } 2949 if (p->p_xthread == td) 2950 p->p_xthread = NULL; 2951 if (!(p->p_flag & P_TRACED)) 2952 break; 2953 if (td->td_dbgflags & TDB_SUSPEND) { 2954 if (p->p_flag & P_SINGLE_EXIT) 2955 break; 2956 goto stopme; 2957 } 2958 } 2959 PROC_SUNLOCK(p); 2960 } 2961 2962 if (si != NULL && sig == td->td_xsig) { 2963 /* Parent wants us to take the original signal unchanged. */ 2964 si->ksi_flags |= KSI_HEAD; 2965 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 2966 si->ksi_signo = 0; 2967 } else if (td->td_xsig != 0) { 2968 /* 2969 * If parent wants us to take a new signal, then it will leave 2970 * it in td->td_xsig; otherwise we just look for signals again. 2971 */ 2972 ksiginfo_init(&ksi); 2973 ksi.ksi_signo = td->td_xsig; 2974 ksi.ksi_flags |= KSI_PTRACE; 2975 td2 = sigtd(p, td->td_xsig, false); 2976 tdsendsignal(p, td2, td->td_xsig, &ksi); 2977 if (td != td2) 2978 return (0); 2979 } 2980 2981 return (td->td_xsig); 2982 } 2983 2984 static void 2985 reschedule_signals(struct proc *p, sigset_t block, int flags) 2986 { 2987 struct sigacts *ps; 2988 struct thread *td; 2989 int sig; 2990 bool fastblk, pslocked; 2991 2992 PROC_LOCK_ASSERT(p, MA_OWNED); 2993 ps = p->p_sigacts; 2994 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0; 2995 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED); 2996 if (SIGISEMPTY(p->p_siglist)) 2997 return; 2998 SIGSETAND(block, p->p_siglist); 2999 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0; 3000 SIG_FOREACH(sig, &block) { 3001 td = sigtd(p, sig, fastblk); 3002 3003 /* 3004 * If sigtd() selected us despite sigfastblock is 3005 * blocking, do not activate AST or wake us, to avoid 3006 * loop in AST handler. 3007 */ 3008 if (fastblk && td == curthread) 3009 continue; 3010 3011 signotify(td); 3012 if (!pslocked) 3013 mtx_lock(&ps->ps_mtx); 3014 if (p->p_flag & P_TRACED || 3015 (SIGISMEMBER(ps->ps_sigcatch, sig) && 3016 !SIGISMEMBER(td->td_sigmask, sig))) { 3017 tdsigwakeup(td, sig, SIG_CATCH, 3018 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 3019 ERESTART)); 3020 } 3021 if (!pslocked) 3022 mtx_unlock(&ps->ps_mtx); 3023 } 3024 } 3025 3026 void 3027 tdsigcleanup(struct thread *td) 3028 { 3029 struct proc *p; 3030 sigset_t unblocked; 3031 3032 p = td->td_proc; 3033 PROC_LOCK_ASSERT(p, MA_OWNED); 3034 3035 sigqueue_flush(&td->td_sigqueue); 3036 if (p->p_numthreads == 1) 3037 return; 3038 3039 /* 3040 * Since we cannot handle signals, notify signal post code 3041 * about this by filling the sigmask. 3042 * 3043 * Also, if needed, wake up thread(s) that do not block the 3044 * same signals as the exiting thread, since the thread might 3045 * have been selected for delivery and woken up. 3046 */ 3047 SIGFILLSET(unblocked); 3048 SIGSETNAND(unblocked, td->td_sigmask); 3049 SIGFILLSET(td->td_sigmask); 3050 reschedule_signals(p, unblocked, 0); 3051 3052 } 3053 3054 static int 3055 sigdeferstop_curr_flags(int cflags) 3056 { 3057 3058 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 3059 (cflags & TDF_SBDRY) != 0); 3060 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 3061 } 3062 3063 /* 3064 * Defer the delivery of SIGSTOP for the current thread, according to 3065 * the requested mode. Returns previous flags, which must be restored 3066 * by sigallowstop(). 3067 * 3068 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 3069 * cleared by the current thread, which allow the lock-less read-only 3070 * accesses below. 3071 */ 3072 int 3073 sigdeferstop_impl(int mode) 3074 { 3075 struct thread *td; 3076 int cflags, nflags; 3077 3078 td = curthread; 3079 cflags = sigdeferstop_curr_flags(td->td_flags); 3080 switch (mode) { 3081 case SIGDEFERSTOP_NOP: 3082 nflags = cflags; 3083 break; 3084 case SIGDEFERSTOP_OFF: 3085 nflags = 0; 3086 break; 3087 case SIGDEFERSTOP_SILENT: 3088 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 3089 break; 3090 case SIGDEFERSTOP_EINTR: 3091 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 3092 break; 3093 case SIGDEFERSTOP_ERESTART: 3094 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 3095 break; 3096 default: 3097 panic("sigdeferstop: invalid mode %x", mode); 3098 break; 3099 } 3100 if (cflags == nflags) 3101 return (SIGDEFERSTOP_VAL_NCHG); 3102 thread_lock(td); 3103 td->td_flags = (td->td_flags & ~cflags) | nflags; 3104 thread_unlock(td); 3105 return (cflags); 3106 } 3107 3108 /* 3109 * Restores the STOP handling mode, typically permitting the delivery 3110 * of SIGSTOP for the current thread. This does not immediately 3111 * suspend if a stop was posted. Instead, the thread will suspend 3112 * either via ast() or a subsequent interruptible sleep. 3113 */ 3114 void 3115 sigallowstop_impl(int prev) 3116 { 3117 struct thread *td; 3118 int cflags; 3119 3120 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 3121 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 3122 ("sigallowstop: incorrect previous mode %x", prev)); 3123 td = curthread; 3124 cflags = sigdeferstop_curr_flags(td->td_flags); 3125 if (cflags != prev) { 3126 thread_lock(td); 3127 td->td_flags = (td->td_flags & ~cflags) | prev; 3128 thread_unlock(td); 3129 } 3130 } 3131 3132 enum sigstatus { 3133 SIGSTATUS_HANDLE, 3134 SIGSTATUS_HANDLED, 3135 SIGSTATUS_IGNORE, 3136 SIGSTATUS_SBDRY_STOP, 3137 }; 3138 3139 /* 3140 * The thread has signal "sig" pending. Figure out what to do with it: 3141 * 3142 * _HANDLE -> the caller should handle the signal 3143 * _HANDLED -> handled internally, reload pending signal set 3144 * _IGNORE -> ignored, remove from the set of pending signals and try the 3145 * next pending signal 3146 * _SBDRY_STOP -> the signal should stop the thread but this is not 3147 * permitted in the current context 3148 */ 3149 static enum sigstatus 3150 sigprocess(struct thread *td, int sig) 3151 { 3152 struct proc *p; 3153 struct sigacts *ps; 3154 struct sigqueue *queue; 3155 ksiginfo_t ksi; 3156 int prop; 3157 3158 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig)); 3159 3160 p = td->td_proc; 3161 ps = p->p_sigacts; 3162 mtx_assert(&ps->ps_mtx, MA_OWNED); 3163 PROC_LOCK_ASSERT(p, MA_OWNED); 3164 3165 /* 3166 * We should allow pending but ignored signals below 3167 * if there is sigwait() active, or P_TRACED was 3168 * on when they were posted. 3169 */ 3170 if (SIGISMEMBER(ps->ps_sigignore, sig) && 3171 (p->p_flag & P_TRACED) == 0 && 3172 (td->td_flags & TDF_SIGWAIT) == 0) { 3173 return (SIGSTATUS_IGNORE); 3174 } 3175 3176 /* 3177 * If the process is going to single-thread mode to prepare 3178 * for exit, there is no sense in delivering any signal 3179 * to usermode. Another important consequence is that 3180 * msleep(..., PCATCH, ...) now is only interruptible by a 3181 * suspend request. 3182 */ 3183 if ((p->p_flag2 & P2_WEXIT) != 0) 3184 return (SIGSTATUS_IGNORE); 3185 3186 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 3187 /* 3188 * If traced, always stop. 3189 * Remove old signal from queue before the stop. 3190 * XXX shrug off debugger, it causes siginfo to 3191 * be thrown away. 3192 */ 3193 queue = &td->td_sigqueue; 3194 ksiginfo_init(&ksi); 3195 if (sigqueue_get(queue, sig, &ksi) == 0) { 3196 queue = &p->p_sigqueue; 3197 sigqueue_get(queue, sig, &ksi); 3198 } 3199 td->td_si = ksi.ksi_info; 3200 3201 mtx_unlock(&ps->ps_mtx); 3202 sig = ptracestop(td, sig, &ksi); 3203 mtx_lock(&ps->ps_mtx); 3204 3205 td->td_si.si_signo = 0; 3206 3207 /* 3208 * Keep looking if the debugger discarded or 3209 * replaced the signal. 3210 */ 3211 if (sig == 0) 3212 return (SIGSTATUS_HANDLED); 3213 3214 /* 3215 * If the signal became masked, re-queue it. 3216 */ 3217 if (SIGISMEMBER(td->td_sigmask, sig)) { 3218 ksi.ksi_flags |= KSI_HEAD; 3219 sigqueue_add(&p->p_sigqueue, sig, &ksi); 3220 return (SIGSTATUS_HANDLED); 3221 } 3222 3223 /* 3224 * If the traced bit got turned off, requeue the signal and 3225 * reload the set of pending signals. This ensures that p_sig* 3226 * and p_sigact are consistent. 3227 */ 3228 if ((p->p_flag & P_TRACED) == 0) { 3229 if ((ksi.ksi_flags & KSI_PTRACE) == 0) { 3230 ksi.ksi_flags |= KSI_HEAD; 3231 sigqueue_add(queue, sig, &ksi); 3232 } 3233 return (SIGSTATUS_HANDLED); 3234 } 3235 } 3236 3237 /* 3238 * Decide whether the signal should be returned. 3239 * Return the signal's number, or fall through 3240 * to clear it from the pending mask. 3241 */ 3242 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 3243 case (intptr_t)SIG_DFL: 3244 /* 3245 * Don't take default actions on system processes. 3246 */ 3247 if (p->p_pid <= 1) { 3248 #ifdef DIAGNOSTIC 3249 /* 3250 * Are you sure you want to ignore SIGSEGV 3251 * in init? XXX 3252 */ 3253 printf("Process (pid %lu) got signal %d\n", 3254 (u_long)p->p_pid, sig); 3255 #endif 3256 return (SIGSTATUS_IGNORE); 3257 } 3258 3259 /* 3260 * If there is a pending stop signal to process with 3261 * default action, stop here, then clear the signal. 3262 * Traced or exiting processes should ignore stops. 3263 * Additionally, a member of an orphaned process group 3264 * should ignore tty stops. 3265 */ 3266 prop = sigprop(sig); 3267 if (prop & SIGPROP_STOP) { 3268 mtx_unlock(&ps->ps_mtx); 3269 if ((p->p_flag & (P_TRACED | P_WEXIT | 3270 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp-> 3271 pg_flags & PGRP_ORPHANED) != 0 && 3272 (prop & SIGPROP_TTYSTOP) != 0)) { 3273 mtx_lock(&ps->ps_mtx); 3274 return (SIGSTATUS_IGNORE); 3275 } 3276 if (TD_SBDRY_INTR(td)) { 3277 KASSERT((td->td_flags & TDF_SBDRY) != 0, 3278 ("lost TDF_SBDRY")); 3279 mtx_lock(&ps->ps_mtx); 3280 return (SIGSTATUS_SBDRY_STOP); 3281 } 3282 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 3283 &p->p_mtx.lock_object, "Catching SIGSTOP"); 3284 sigqueue_delete(&td->td_sigqueue, sig); 3285 sigqueue_delete(&p->p_sigqueue, sig); 3286 p->p_flag |= P_STOPPED_SIG; 3287 p->p_xsig = sig; 3288 PROC_SLOCK(p); 3289 sig_suspend_threads(td, p); 3290 thread_suspend_switch(td, p); 3291 PROC_SUNLOCK(p); 3292 mtx_lock(&ps->ps_mtx); 3293 return (SIGSTATUS_HANDLED); 3294 } else if ((prop & SIGPROP_IGNORE) != 0 && 3295 (td->td_flags & TDF_SIGWAIT) == 0) { 3296 /* 3297 * Default action is to ignore; drop it if 3298 * not in kern_sigtimedwait(). 3299 */ 3300 return (SIGSTATUS_IGNORE); 3301 } else { 3302 return (SIGSTATUS_HANDLE); 3303 } 3304 3305 case (intptr_t)SIG_IGN: 3306 if ((td->td_flags & TDF_SIGWAIT) == 0) 3307 return (SIGSTATUS_IGNORE); 3308 else 3309 return (SIGSTATUS_HANDLE); 3310 3311 default: 3312 /* 3313 * This signal has an action, let postsig() process it. 3314 */ 3315 return (SIGSTATUS_HANDLE); 3316 } 3317 } 3318 3319 /* 3320 * If the current process has received a signal (should be caught or cause 3321 * termination, should interrupt current syscall), return the signal number. 3322 * Stop signals with default action are processed immediately, then cleared; 3323 * they aren't returned. This is checked after each entry to the system for 3324 * a syscall or trap (though this can usually be done without calling 3325 * issignal by checking the pending signal masks in cursig.) The normal call 3326 * sequence is 3327 * 3328 * while (sig = cursig(curthread)) 3329 * postsig(sig); 3330 */ 3331 static int 3332 issignal(struct thread *td) 3333 { 3334 struct proc *p; 3335 sigset_t sigpending; 3336 int sig; 3337 3338 p = td->td_proc; 3339 PROC_LOCK_ASSERT(p, MA_OWNED); 3340 3341 for (;;) { 3342 sigpending = td->td_sigqueue.sq_signals; 3343 SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 3344 SIGSETNAND(sigpending, td->td_sigmask); 3345 3346 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 3347 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 3348 SIG_STOPSIGMASK(sigpending); 3349 if (SIGISEMPTY(sigpending)) /* no signal to send */ 3350 return (0); 3351 3352 /* 3353 * Do fast sigblock if requested by usermode. Since 3354 * we do know that there was a signal pending at this 3355 * point, set the FAST_SIGBLOCK_PEND as indicator for 3356 * usermode to perform a dummy call to 3357 * FAST_SIGBLOCK_UNBLOCK, which causes immediate 3358 * delivery of postponed pending signal. 3359 */ 3360 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 3361 if (td->td_sigblock_val != 0) 3362 SIGSETNAND(sigpending, fastblock_mask); 3363 if (SIGISEMPTY(sigpending)) { 3364 td->td_pflags |= TDP_SIGFASTPENDING; 3365 return (0); 3366 } 3367 } 3368 3369 if (!pt_attach_transparent && 3370 (p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 3371 (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 3372 SIGISMEMBER(sigpending, SIGSTOP)) { 3373 /* 3374 * If debugger just attached, always consume 3375 * SIGSTOP from ptrace(PT_ATTACH) first, to 3376 * execute the debugger attach ritual in 3377 * order. 3378 */ 3379 td->td_dbgflags |= TDB_FSTP; 3380 SIGEMPTYSET(sigpending); 3381 SIGADDSET(sigpending, SIGSTOP); 3382 } 3383 3384 SIG_FOREACH(sig, &sigpending) { 3385 switch (sigprocess(td, sig)) { 3386 case SIGSTATUS_HANDLE: 3387 return (sig); 3388 case SIGSTATUS_HANDLED: 3389 goto next; 3390 case SIGSTATUS_IGNORE: 3391 sigqueue_delete(&td->td_sigqueue, sig); 3392 sigqueue_delete(&p->p_sigqueue, sig); 3393 break; 3394 case SIGSTATUS_SBDRY_STOP: 3395 return (-1); 3396 } 3397 } 3398 next:; 3399 } 3400 } 3401 3402 void 3403 thread_stopped(struct proc *p) 3404 { 3405 int n; 3406 3407 PROC_LOCK_ASSERT(p, MA_OWNED); 3408 PROC_SLOCK_ASSERT(p, MA_OWNED); 3409 n = p->p_suspcount; 3410 if (p == curproc) 3411 n++; 3412 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 3413 PROC_SUNLOCK(p); 3414 p->p_flag &= ~P_WAITED; 3415 PROC_LOCK(p->p_pptr); 3416 childproc_stopped(p, (p->p_flag & P_TRACED) ? 3417 CLD_TRAPPED : CLD_STOPPED); 3418 PROC_UNLOCK(p->p_pptr); 3419 PROC_SLOCK(p); 3420 } 3421 } 3422 3423 /* 3424 * Take the action for the specified signal 3425 * from the current set of pending signals. 3426 */ 3427 int 3428 postsig(int sig) 3429 { 3430 struct thread *td; 3431 struct proc *p; 3432 struct sigacts *ps; 3433 sig_t action; 3434 ksiginfo_t ksi; 3435 sigset_t returnmask; 3436 3437 KASSERT(sig != 0, ("postsig")); 3438 3439 td = curthread; 3440 p = td->td_proc; 3441 PROC_LOCK_ASSERT(p, MA_OWNED); 3442 ps = p->p_sigacts; 3443 mtx_assert(&ps->ps_mtx, MA_OWNED); 3444 ksiginfo_init(&ksi); 3445 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 3446 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 3447 return (0); 3448 ksi.ksi_signo = sig; 3449 if (ksi.ksi_code == SI_TIMER) 3450 itimer_accept(p, ksi.ksi_timerid, &ksi); 3451 action = ps->ps_sigact[_SIG_IDX(sig)]; 3452 #ifdef KTRACE 3453 if (KTRPOINT(td, KTR_PSIG)) 3454 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 3455 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 3456 #endif 3457 3458 if (action == SIG_DFL) { 3459 /* 3460 * Default action, where the default is to kill 3461 * the process. (Other cases were ignored above.) 3462 */ 3463 mtx_unlock(&ps->ps_mtx); 3464 proc_td_siginfo_capture(td, &ksi.ksi_info); 3465 sigexit(td, sig); 3466 /* NOTREACHED */ 3467 } else { 3468 /* 3469 * If we get here, the signal must be caught. 3470 */ 3471 KASSERT(action != SIG_IGN, ("postsig action %p", action)); 3472 KASSERT(!SIGISMEMBER(td->td_sigmask, sig), 3473 ("postsig action: blocked sig %d", sig)); 3474 3475 /* 3476 * Set the new mask value and also defer further 3477 * occurrences of this signal. 3478 * 3479 * Special case: user has done a sigsuspend. Here the 3480 * current mask is not of interest, but rather the 3481 * mask from before the sigsuspend is what we want 3482 * restored after the signal processing is completed. 3483 */ 3484 if (td->td_pflags & TDP_OLDMASK) { 3485 returnmask = td->td_oldsigmask; 3486 td->td_pflags &= ~TDP_OLDMASK; 3487 } else 3488 returnmask = td->td_sigmask; 3489 3490 if (p->p_sig == sig) { 3491 p->p_sig = 0; 3492 } 3493 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3494 postsig_done(sig, td, ps); 3495 } 3496 return (1); 3497 } 3498 3499 int 3500 sig_ast_checksusp(struct thread *td) 3501 { 3502 struct proc *p __diagused; 3503 int ret; 3504 3505 p = td->td_proc; 3506 PROC_LOCK_ASSERT(p, MA_OWNED); 3507 3508 if (!td_ast_pending(td, TDA_SUSPEND)) 3509 return (0); 3510 3511 ret = thread_suspend_check(1); 3512 MPASS(ret == 0 || ret == EINTR || ret == ERESTART); 3513 return (ret); 3514 } 3515 3516 int 3517 sig_ast_needsigchk(struct thread *td) 3518 { 3519 struct proc *p; 3520 struct sigacts *ps; 3521 int ret, sig; 3522 3523 p = td->td_proc; 3524 PROC_LOCK_ASSERT(p, MA_OWNED); 3525 3526 if (!td_ast_pending(td, TDA_SIG)) 3527 return (0); 3528 3529 ps = p->p_sigacts; 3530 mtx_lock(&ps->ps_mtx); 3531 sig = cursig(td); 3532 if (sig == -1) { 3533 mtx_unlock(&ps->ps_mtx); 3534 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY")); 3535 KASSERT(TD_SBDRY_INTR(td), 3536 ("lost TDF_SERESTART of TDF_SEINTR")); 3537 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != 3538 (TDF_SEINTR | TDF_SERESTART), 3539 ("both TDF_SEINTR and TDF_SERESTART")); 3540 ret = TD_SBDRY_ERRNO(td); 3541 } else if (sig != 0) { 3542 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART; 3543 mtx_unlock(&ps->ps_mtx); 3544 } else { 3545 mtx_unlock(&ps->ps_mtx); 3546 ret = 0; 3547 } 3548 3549 /* 3550 * Do not go into sleep if this thread was the ptrace(2) 3551 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH, 3552 * but we usually act on the signal by interrupting sleep, and 3553 * should do that here as well. 3554 */ 3555 if ((td->td_dbgflags & TDB_FSTP) != 0) { 3556 if (ret == 0) 3557 ret = EINTR; 3558 td->td_dbgflags &= ~TDB_FSTP; 3559 } 3560 3561 return (ret); 3562 } 3563 3564 int 3565 sig_intr(void) 3566 { 3567 struct thread *td; 3568 struct proc *p; 3569 int ret; 3570 3571 td = curthread; 3572 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND)) 3573 return (0); 3574 3575 p = td->td_proc; 3576 3577 PROC_LOCK(p); 3578 ret = sig_ast_checksusp(td); 3579 if (ret == 0) 3580 ret = sig_ast_needsigchk(td); 3581 PROC_UNLOCK(p); 3582 return (ret); 3583 } 3584 3585 bool 3586 curproc_sigkilled(void) 3587 { 3588 struct thread *td; 3589 struct proc *p; 3590 struct sigacts *ps; 3591 bool res; 3592 3593 td = curthread; 3594 if (!td_ast_pending(td, TDA_SIG)) 3595 return (false); 3596 3597 p = td->td_proc; 3598 PROC_LOCK(p); 3599 ps = p->p_sigacts; 3600 mtx_lock(&ps->ps_mtx); 3601 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) || 3602 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL); 3603 mtx_unlock(&ps->ps_mtx); 3604 PROC_UNLOCK(p); 3605 return (res); 3606 } 3607 3608 void 3609 proc_wkilled(struct proc *p) 3610 { 3611 3612 PROC_LOCK_ASSERT(p, MA_OWNED); 3613 if ((p->p_flag & P_WKILLED) == 0) 3614 p->p_flag |= P_WKILLED; 3615 } 3616 3617 /* 3618 * Kill the current process for stated reason. 3619 */ 3620 void 3621 killproc(struct proc *p, const char *why) 3622 { 3623 3624 PROC_LOCK_ASSERT(p, MA_OWNED); 3625 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3626 p->p_comm); 3627 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n", 3628 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id, 3629 p->p_ucred->cr_uid, why); 3630 proc_wkilled(p); 3631 kern_psignal(p, SIGKILL); 3632 } 3633 3634 /* 3635 * Send queued SIGCHLD to parent when child process's state 3636 * is changed. 3637 */ 3638 static void 3639 sigparent(struct proc *p, int reason, int status) 3640 { 3641 PROC_LOCK_ASSERT(p, MA_OWNED); 3642 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3643 3644 if (p->p_ksi != NULL) { 3645 p->p_ksi->ksi_signo = SIGCHLD; 3646 p->p_ksi->ksi_code = reason; 3647 p->p_ksi->ksi_status = status; 3648 p->p_ksi->ksi_pid = p->p_pid; 3649 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3650 if (KSI_ONQ(p->p_ksi)) 3651 return; 3652 } 3653 3654 /* 3655 * Do not consume p_ksi if parent is zombie, since signal is 3656 * dropped immediately. Instead, keep it since it might be 3657 * useful for reaper. 3658 */ 3659 if (p->p_pptr->p_state != PRS_ZOMBIE) 3660 pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3661 } 3662 3663 static void 3664 childproc_jobstate(struct proc *p, int reason, int sig) 3665 { 3666 struct sigacts *ps; 3667 3668 PROC_LOCK_ASSERT(p, MA_OWNED); 3669 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3670 3671 /* 3672 * Wake up parent sleeping in kern_wait(), also send 3673 * SIGCHLD to parent, but SIGCHLD does not guarantee 3674 * that parent will awake, because parent may masked 3675 * the signal. 3676 */ 3677 p->p_pptr->p_flag |= P_STATCHILD; 3678 wakeup(p->p_pptr); 3679 3680 ps = p->p_pptr->p_sigacts; 3681 mtx_lock(&ps->ps_mtx); 3682 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 3683 mtx_unlock(&ps->ps_mtx); 3684 sigparent(p, reason, sig); 3685 } else 3686 mtx_unlock(&ps->ps_mtx); 3687 } 3688 3689 void 3690 childproc_stopped(struct proc *p, int reason) 3691 { 3692 3693 childproc_jobstate(p, reason, p->p_xsig); 3694 } 3695 3696 void 3697 childproc_continued(struct proc *p) 3698 { 3699 PROC_LOCK_ASSERT(p, MA_OWNED); 3700 p->p_flag |= P_CONTINUED; 3701 p->p_xsig = SIGCONT; 3702 childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3703 } 3704 3705 void 3706 childproc_exited(struct proc *p) 3707 { 3708 int reason, status; 3709 3710 if (WCOREDUMP(p->p_xsig)) { 3711 reason = CLD_DUMPED; 3712 status = WTERMSIG(p->p_xsig); 3713 } else if (WIFSIGNALED(p->p_xsig)) { 3714 reason = CLD_KILLED; 3715 status = WTERMSIG(p->p_xsig); 3716 } else { 3717 reason = CLD_EXITED; 3718 status = p->p_xexit; 3719 } 3720 /* 3721 * XXX avoid calling wakeup(p->p_pptr), the work is 3722 * done in exit1(). 3723 */ 3724 sigparent(p, reason, status); 3725 } 3726 3727 /* 3728 * Nonexistent system call-- signal process (may want to handle it). Flag 3729 * error in case process won't see signal immediately (blocked or ignored). 3730 */ 3731 #ifndef _SYS_SYSPROTO_H_ 3732 struct nosys_args { 3733 int dummy; 3734 }; 3735 #endif 3736 /* ARGSUSED */ 3737 int 3738 nosys(struct thread *td, struct nosys_args *args) 3739 { 3740 return (kern_nosys(td, args->dummy)); 3741 } 3742 3743 int 3744 kern_nosys(struct thread *td, int dummy) 3745 { 3746 struct proc *p; 3747 3748 p = td->td_proc; 3749 3750 if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) { 3751 PROC_LOCK(p); 3752 tdsignal(td, SIGSYS); 3753 PROC_UNLOCK(p); 3754 } 3755 if (kern_lognosys == 1 || kern_lognosys == 3) { 3756 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 3757 td->td_sa.code); 3758 } 3759 if (kern_lognosys == 2 || kern_lognosys == 3 || 3760 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) { 3761 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm, 3762 td->td_sa.code); 3763 } 3764 return (ENOSYS); 3765 } 3766 3767 /* 3768 * Send a SIGIO or SIGURG signal to a process or process group using stored 3769 * credentials rather than those of the current process. 3770 */ 3771 void 3772 pgsigio(struct sigio **sigiop, int sig, int checkctty) 3773 { 3774 ksiginfo_t ksi; 3775 struct sigio *sigio; 3776 3777 ksiginfo_init(&ksi); 3778 ksi.ksi_signo = sig; 3779 ksi.ksi_code = SI_KERNEL; 3780 3781 SIGIO_LOCK(); 3782 sigio = *sigiop; 3783 if (sigio == NULL) { 3784 SIGIO_UNLOCK(); 3785 return; 3786 } 3787 if (sigio->sio_pgid > 0) { 3788 PROC_LOCK(sigio->sio_proc); 3789 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 3790 kern_psignal(sigio->sio_proc, sig); 3791 PROC_UNLOCK(sigio->sio_proc); 3792 } else if (sigio->sio_pgid < 0) { 3793 struct proc *p; 3794 3795 PGRP_LOCK(sigio->sio_pgrp); 3796 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3797 PROC_LOCK(p); 3798 if (p->p_state == PRS_NORMAL && 3799 CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3800 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 3801 kern_psignal(p, sig); 3802 PROC_UNLOCK(p); 3803 } 3804 PGRP_UNLOCK(sigio->sio_pgrp); 3805 } 3806 SIGIO_UNLOCK(); 3807 } 3808 3809 static int 3810 filt_sigattach(struct knote *kn) 3811 { 3812 struct proc *p = curproc; 3813 3814 kn->kn_ptr.p_proc = p; 3815 kn->kn_flags |= EV_CLEAR; /* automatically set */ 3816 3817 knlist_add(p->p_klist, kn, 0); 3818 3819 return (0); 3820 } 3821 3822 static void 3823 filt_sigdetach(struct knote *kn) 3824 { 3825 knlist_remove(kn->kn_knlist, kn, 0); 3826 } 3827 3828 /* 3829 * signal knotes are shared with proc knotes, so we apply a mask to 3830 * the hint in order to differentiate them from process hints. This 3831 * could be avoided by using a signal-specific knote list, but probably 3832 * isn't worth the trouble. 3833 */ 3834 static int 3835 filt_signal(struct knote *kn, long hint) 3836 { 3837 3838 if (hint & NOTE_SIGNAL) { 3839 hint &= ~NOTE_SIGNAL; 3840 3841 if (kn->kn_id == hint) 3842 kn->kn_data++; 3843 } 3844 return (kn->kn_data != 0); 3845 } 3846 3847 struct sigacts * 3848 sigacts_alloc(void) 3849 { 3850 struct sigacts *ps; 3851 3852 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3853 refcount_init(&ps->ps_refcnt, 1); 3854 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 3855 return (ps); 3856 } 3857 3858 void 3859 sigacts_free(struct sigacts *ps) 3860 { 3861 3862 if (refcount_release(&ps->ps_refcnt) == 0) 3863 return; 3864 mtx_destroy(&ps->ps_mtx); 3865 free(ps, M_SUBPROC); 3866 } 3867 3868 struct sigacts * 3869 sigacts_hold(struct sigacts *ps) 3870 { 3871 3872 refcount_acquire(&ps->ps_refcnt); 3873 return (ps); 3874 } 3875 3876 void 3877 sigacts_copy(struct sigacts *dest, struct sigacts *src) 3878 { 3879 3880 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 3881 mtx_lock(&src->ps_mtx); 3882 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 3883 mtx_unlock(&src->ps_mtx); 3884 } 3885 3886 int 3887 sigacts_shared(struct sigacts *ps) 3888 { 3889 3890 return (ps->ps_refcnt > 1); 3891 } 3892 3893 void 3894 sig_drop_caught(struct proc *p) 3895 { 3896 int sig; 3897 struct sigacts *ps; 3898 3899 ps = p->p_sigacts; 3900 PROC_LOCK_ASSERT(p, MA_OWNED); 3901 mtx_assert(&ps->ps_mtx, MA_OWNED); 3902 SIG_FOREACH(sig, &ps->ps_sigcatch) { 3903 sigdflt(ps, sig); 3904 if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 3905 sigqueue_delete_proc(p, sig); 3906 } 3907 } 3908 3909 static void 3910 sigfastblock_failed(struct thread *td, bool sendsig, bool write) 3911 { 3912 ksiginfo_t ksi; 3913 3914 /* 3915 * Prevent further fetches and SIGSEGVs, allowing thread to 3916 * issue syscalls despite corruption. 3917 */ 3918 sigfastblock_clear(td); 3919 3920 if (!sendsig) 3921 return; 3922 ksiginfo_init_trap(&ksi); 3923 ksi.ksi_signo = SIGSEGV; 3924 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR; 3925 ksi.ksi_addr = td->td_sigblock_ptr; 3926 trapsignal(td, &ksi); 3927 } 3928 3929 static bool 3930 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp) 3931 { 3932 uint32_t res; 3933 3934 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 3935 return (true); 3936 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) { 3937 sigfastblock_failed(td, sendsig, false); 3938 return (false); 3939 } 3940 *valp = res; 3941 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS; 3942 return (true); 3943 } 3944 3945 static void 3946 sigfastblock_resched(struct thread *td, bool resched) 3947 { 3948 struct proc *p; 3949 3950 if (resched) { 3951 p = td->td_proc; 3952 PROC_LOCK(p); 3953 reschedule_signals(p, td->td_sigmask, 0); 3954 PROC_UNLOCK(p); 3955 } 3956 ast_sched(td, TDA_SIG); 3957 } 3958 3959 int 3960 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap) 3961 { 3962 struct proc *p; 3963 int error, res; 3964 uint32_t oldval; 3965 3966 error = 0; 3967 p = td->td_proc; 3968 switch (uap->cmd) { 3969 case SIGFASTBLOCK_SETPTR: 3970 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) { 3971 error = EBUSY; 3972 break; 3973 } 3974 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) { 3975 error = EINVAL; 3976 break; 3977 } 3978 td->td_pflags |= TDP_SIGFASTBLOCK; 3979 td->td_sigblock_ptr = uap->ptr; 3980 break; 3981 3982 case SIGFASTBLOCK_UNBLOCK: 3983 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 3984 error = EINVAL; 3985 break; 3986 } 3987 3988 for (;;) { 3989 res = casueword32(td->td_sigblock_ptr, 3990 SIGFASTBLOCK_PEND, &oldval, 0); 3991 if (res == -1) { 3992 error = EFAULT; 3993 sigfastblock_failed(td, false, true); 3994 break; 3995 } 3996 if (res == 0) 3997 break; 3998 MPASS(res == 1); 3999 if (oldval != SIGFASTBLOCK_PEND) { 4000 error = EBUSY; 4001 break; 4002 } 4003 error = thread_check_susp(td, false); 4004 if (error != 0) 4005 break; 4006 } 4007 if (error != 0) 4008 break; 4009 4010 /* 4011 * td_sigblock_val is cleared there, but not on a 4012 * syscall exit. The end effect is that a single 4013 * interruptible sleep, while user sigblock word is 4014 * set, might return EINTR or ERESTART to usermode 4015 * without delivering signal. All further sleeps, 4016 * until userspace clears the word and does 4017 * sigfastblock(UNBLOCK), observe current word and no 4018 * longer get interrupted. It is slight 4019 * non-conformance, with alternative to have read the 4020 * sigblock word on each syscall entry. 4021 */ 4022 td->td_sigblock_val = 0; 4023 4024 /* 4025 * Rely on normal ast mechanism to deliver pending 4026 * signals to current thread. But notify others about 4027 * fake unblock. 4028 */ 4029 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1); 4030 4031 break; 4032 4033 case SIGFASTBLOCK_UNSETPTR: 4034 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) { 4035 error = EINVAL; 4036 break; 4037 } 4038 if (!sigfastblock_fetch_sig(td, false, &oldval)) { 4039 error = EFAULT; 4040 break; 4041 } 4042 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) { 4043 error = EBUSY; 4044 break; 4045 } 4046 sigfastblock_clear(td); 4047 break; 4048 4049 default: 4050 error = EINVAL; 4051 break; 4052 } 4053 return (error); 4054 } 4055 4056 void 4057 sigfastblock_clear(struct thread *td) 4058 { 4059 bool resched; 4060 4061 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) 4062 return; 4063 td->td_sigblock_val = 0; 4064 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 || 4065 SIGPENDING(td); 4066 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING); 4067 sigfastblock_resched(td, resched); 4068 } 4069 4070 void 4071 sigfastblock_fetch(struct thread *td) 4072 { 4073 uint32_t val; 4074 4075 (void)sigfastblock_fetch_sig(td, true, &val); 4076 } 4077 4078 static void 4079 sigfastblock_setpend1(struct thread *td) 4080 { 4081 int res; 4082 uint32_t oldval; 4083 4084 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0) 4085 return; 4086 res = fueword32((void *)td->td_sigblock_ptr, &oldval); 4087 if (res == -1) { 4088 sigfastblock_failed(td, true, false); 4089 return; 4090 } 4091 for (;;) { 4092 res = casueword32(td->td_sigblock_ptr, oldval, &oldval, 4093 oldval | SIGFASTBLOCK_PEND); 4094 if (res == -1) { 4095 sigfastblock_failed(td, true, true); 4096 return; 4097 } 4098 if (res == 0) { 4099 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS; 4100 td->td_pflags &= ~TDP_SIGFASTPENDING; 4101 break; 4102 } 4103 MPASS(res == 1); 4104 if (thread_check_susp(td, false) != 0) 4105 break; 4106 } 4107 } 4108 4109 static void 4110 sigfastblock_setpend(struct thread *td, bool resched) 4111 { 4112 struct proc *p; 4113 4114 sigfastblock_setpend1(td); 4115 if (resched) { 4116 p = td->td_proc; 4117 PROC_LOCK(p); 4118 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK); 4119 PROC_UNLOCK(p); 4120 } 4121 } 4122