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