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