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