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