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