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