1 /* $OpenBSD: kern_sig.c,v 1.351 2024/11/20 10:21:22 claudio Exp $ */ 2 /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Theo de Raadt. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1991, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 39 */ 40 41 #include <sys/param.h> 42 #include <sys/signalvar.h> 43 #include <sys/queue.h> 44 #include <sys/namei.h> 45 #include <sys/vnode.h> 46 #include <sys/event.h> 47 #include <sys/proc.h> 48 #include <sys/systm.h> 49 #include <sys/acct.h> 50 #include <sys/fcntl.h> 51 #include <sys/filedesc.h> 52 #include <sys/wait.h> 53 #include <sys/ktrace.h> 54 #include <sys/stat.h> 55 #include <sys/malloc.h> 56 #include <sys/pool.h> 57 #include <sys/sched.h> 58 #include <sys/user.h> 59 #include <sys/syslog.h> 60 #include <sys/ttycom.h> 61 #include <sys/pledge.h> 62 #include <sys/witness.h> 63 #include <sys/exec_elf.h> 64 65 #include <sys/mount.h> 66 #include <sys/syscallargs.h> 67 68 #include <uvm/uvm_extern.h> 69 #include <machine/tcb.h> 70 71 int nosuidcoredump = 1; 72 73 /* 74 * The array below categorizes the signals and their default actions. 75 */ 76 const int sigprop[NSIG] = { 77 0, /* unused */ 78 SA_KILL, /* SIGHUP */ 79 SA_KILL, /* SIGINT */ 80 SA_KILL|SA_CORE, /* SIGQUIT */ 81 SA_KILL|SA_CORE, /* SIGILL */ 82 SA_KILL|SA_CORE, /* SIGTRAP */ 83 SA_KILL|SA_CORE, /* SIGABRT */ 84 SA_KILL|SA_CORE, /* SIGEMT */ 85 SA_KILL|SA_CORE, /* SIGFPE */ 86 SA_KILL, /* SIGKILL */ 87 SA_KILL|SA_CORE, /* SIGBUS */ 88 SA_KILL|SA_CORE, /* SIGSEGV */ 89 SA_KILL|SA_CORE, /* SIGSYS */ 90 SA_KILL, /* SIGPIPE */ 91 SA_KILL, /* SIGALRM */ 92 SA_KILL, /* SIGTERM */ 93 SA_IGNORE, /* SIGURG */ 94 SA_STOP, /* SIGSTOP */ 95 SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 96 SA_IGNORE|SA_CONT, /* SIGCONT */ 97 SA_IGNORE, /* SIGCHLD */ 98 SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 99 SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 100 SA_IGNORE, /* SIGIO */ 101 SA_KILL, /* SIGXCPU */ 102 SA_KILL, /* SIGXFSZ */ 103 SA_KILL, /* SIGVTALRM */ 104 SA_KILL, /* SIGPROF */ 105 SA_IGNORE, /* SIGWINCH */ 106 SA_IGNORE, /* SIGINFO */ 107 SA_KILL, /* SIGUSR1 */ 108 SA_KILL, /* SIGUSR2 */ 109 SA_IGNORE, /* SIGTHR */ 110 }; 111 112 #define CONTSIGMASK (sigmask(SIGCONT)) 113 #define STOPSIGMASK (sigmask(SIGSTOP) | sigmask(SIGTSTP) | \ 114 sigmask(SIGTTIN) | sigmask(SIGTTOU)) 115 116 void setsigvec(struct proc *, int, struct sigaction *); 117 118 int proc_trap(struct proc *, int); 119 void proc_stop(struct proc *p, int); 120 void proc_stop_sweep(void *); 121 void *proc_stop_si; 122 123 void setsigctx(struct proc *, int, struct sigctx *); 124 void postsig_done(struct proc *, int, sigset_t, int); 125 void postsig(struct proc *, int, struct sigctx *); 126 int cansignal(struct proc *, struct process *, int); 127 128 void ptsignal_locked(struct proc *, int, enum signal_type); 129 130 struct pool sigacts_pool; /* memory pool for sigacts structures */ 131 132 void sigio_del(struct sigiolst *); 133 void sigio_unlink(struct sigio_ref *, struct sigiolst *); 134 struct mutex sigio_lock = MUTEX_INITIALIZER(IPL_HIGH); 135 136 /* 137 * Can thread p, send the signal signum to process qr? 138 */ 139 int 140 cansignal(struct proc *p, struct process *qr, int signum) 141 { 142 struct process *pr = p->p_p; 143 struct ucred *uc = p->p_ucred; 144 struct ucred *quc = qr->ps_ucred; 145 146 if (uc->cr_uid == 0) 147 return (1); /* root can always signal */ 148 149 if (pr == qr) 150 return (1); /* process can always signal itself */ 151 152 /* optimization: if the same creds then the tests below will pass */ 153 if (uc == quc) 154 return (1); 155 156 if (signum == SIGCONT && qr->ps_session == pr->ps_session) 157 return (1); /* SIGCONT in session */ 158 159 /* 160 * Using kill(), only certain signals can be sent to setugid 161 * child processes 162 */ 163 if (qr->ps_flags & PS_SUGID) { 164 switch (signum) { 165 case 0: 166 case SIGKILL: 167 case SIGINT: 168 case SIGTERM: 169 case SIGALRM: 170 case SIGSTOP: 171 case SIGTTIN: 172 case SIGTTOU: 173 case SIGTSTP: 174 case SIGHUP: 175 case SIGUSR1: 176 case SIGUSR2: 177 if (uc->cr_ruid == quc->cr_ruid || 178 uc->cr_uid == quc->cr_ruid) 179 return (1); 180 } 181 return (0); 182 } 183 184 if (uc->cr_ruid == quc->cr_ruid || 185 uc->cr_ruid == quc->cr_svuid || 186 uc->cr_uid == quc->cr_ruid || 187 uc->cr_uid == quc->cr_svuid) 188 return (1); 189 return (0); 190 } 191 192 /* 193 * Initialize signal-related data structures. 194 */ 195 void 196 signal_init(void) 197 { 198 proc_stop_si = softintr_establish(IPL_SOFTCLOCK, proc_stop_sweep, 199 NULL); 200 if (proc_stop_si == NULL) 201 panic("signal_init failed to register softintr"); 202 203 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE, 204 PR_WAITOK, "sigapl", NULL); 205 } 206 207 /* 208 * Initialize a new sigaltstack structure. 209 */ 210 void 211 sigstkinit(struct sigaltstack *ss) 212 { 213 ss->ss_flags = SS_DISABLE; 214 ss->ss_size = 0; 215 ss->ss_sp = NULL; 216 } 217 218 /* 219 * Create an initial sigacts structure, using the same signal state 220 * as pr. 221 */ 222 struct sigacts * 223 sigactsinit(struct process *pr) 224 { 225 struct sigacts *ps; 226 227 ps = pool_get(&sigacts_pool, PR_WAITOK); 228 memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts)); 229 return (ps); 230 } 231 232 /* 233 * Release a sigacts structure. 234 */ 235 void 236 sigactsfree(struct sigacts *ps) 237 { 238 pool_put(&sigacts_pool, ps); 239 } 240 241 int 242 sys_sigaction(struct proc *p, void *v, register_t *retval) 243 { 244 struct sys_sigaction_args /* { 245 syscallarg(int) signum; 246 syscallarg(const struct sigaction *) nsa; 247 syscallarg(struct sigaction *) osa; 248 } */ *uap = v; 249 struct sigaction vec; 250 #ifdef KTRACE 251 struct sigaction ovec; 252 #endif 253 struct sigaction *sa; 254 const struct sigaction *nsa; 255 struct sigaction *osa; 256 struct sigacts *ps = p->p_p->ps_sigacts; 257 int signum; 258 int bit, error; 259 260 signum = SCARG(uap, signum); 261 nsa = SCARG(uap, nsa); 262 osa = SCARG(uap, osa); 263 264 if (signum <= 0 || signum >= NSIG || 265 (nsa && (signum == SIGKILL || signum == SIGSTOP))) 266 return (EINVAL); 267 sa = &vec; 268 if (osa) { 269 mtx_enter(&p->p_p->ps_mtx); 270 sa->sa_handler = ps->ps_sigact[signum]; 271 sa->sa_mask = ps->ps_catchmask[signum]; 272 bit = sigmask(signum); 273 sa->sa_flags = 0; 274 if ((ps->ps_sigonstack & bit) != 0) 275 sa->sa_flags |= SA_ONSTACK; 276 if ((ps->ps_sigintr & bit) == 0) 277 sa->sa_flags |= SA_RESTART; 278 if ((ps->ps_sigreset & bit) != 0) 279 sa->sa_flags |= SA_RESETHAND; 280 if ((ps->ps_siginfo & bit) != 0) 281 sa->sa_flags |= SA_SIGINFO; 282 if (signum == SIGCHLD) { 283 if ((ps->ps_sigflags & SAS_NOCLDSTOP) != 0) 284 sa->sa_flags |= SA_NOCLDSTOP; 285 if ((ps->ps_sigflags & SAS_NOCLDWAIT) != 0) 286 sa->sa_flags |= SA_NOCLDWAIT; 287 } 288 mtx_leave(&p->p_p->ps_mtx); 289 if ((sa->sa_mask & bit) == 0) 290 sa->sa_flags |= SA_NODEFER; 291 sa->sa_mask &= ~bit; 292 error = copyout(sa, osa, sizeof (vec)); 293 if (error) 294 return (error); 295 #ifdef KTRACE 296 if (KTRPOINT(p, KTR_STRUCT)) 297 ovec = vec; 298 #endif 299 } 300 if (nsa) { 301 error = copyin(nsa, sa, sizeof (vec)); 302 if (error) 303 return (error); 304 #ifdef KTRACE 305 if (KTRPOINT(p, KTR_STRUCT)) 306 ktrsigaction(p, sa); 307 #endif 308 setsigvec(p, signum, sa); 309 } 310 #ifdef KTRACE 311 if (osa && KTRPOINT(p, KTR_STRUCT)) 312 ktrsigaction(p, &ovec); 313 #endif 314 return (0); 315 } 316 317 void 318 setsigvec(struct proc *p, int signum, struct sigaction *sa) 319 { 320 struct sigacts *ps = p->p_p->ps_sigacts; 321 int bit; 322 323 bit = sigmask(signum); 324 325 mtx_enter(&p->p_p->ps_mtx); 326 ps->ps_sigact[signum] = sa->sa_handler; 327 if ((sa->sa_flags & SA_NODEFER) == 0) 328 sa->sa_mask |= sigmask(signum); 329 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; 330 if (signum == SIGCHLD) { 331 if (sa->sa_flags & SA_NOCLDSTOP) 332 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP); 333 else 334 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP); 335 /* 336 * If the SA_NOCLDWAIT flag is set or the handler 337 * is SIG_IGN we reparent the dying child to PID 1 338 * (init) which will reap the zombie. Because we use 339 * init to do our dirty work we never set SAS_NOCLDWAIT 340 * for PID 1. 341 * XXX exit1 rework means this is unnecessary? 342 */ 343 if (initprocess->ps_sigacts != ps && 344 ((sa->sa_flags & SA_NOCLDWAIT) || 345 sa->sa_handler == SIG_IGN)) 346 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 347 else 348 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 349 } 350 if ((sa->sa_flags & SA_RESETHAND) != 0) 351 ps->ps_sigreset |= bit; 352 else 353 ps->ps_sigreset &= ~bit; 354 if ((sa->sa_flags & SA_SIGINFO) != 0) 355 ps->ps_siginfo |= bit; 356 else 357 ps->ps_siginfo &= ~bit; 358 if ((sa->sa_flags & SA_RESTART) == 0) 359 ps->ps_sigintr |= bit; 360 else 361 ps->ps_sigintr &= ~bit; 362 if ((sa->sa_flags & SA_ONSTACK) != 0) 363 ps->ps_sigonstack |= bit; 364 else 365 ps->ps_sigonstack &= ~bit; 366 /* 367 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 368 * and for signals set to SIG_DFL where the default is to ignore. 369 * However, don't put SIGCONT in ps_sigignore, 370 * as we have to restart the process. 371 */ 372 if (sa->sa_handler == SIG_IGN || 373 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { 374 atomic_clearbits_int(&p->p_siglist, bit); 375 atomic_clearbits_int(&p->p_p->ps_siglist, bit); 376 if (signum != SIGCONT) 377 ps->ps_sigignore |= bit; /* easier in psignal */ 378 ps->ps_sigcatch &= ~bit; 379 } else { 380 ps->ps_sigignore &= ~bit; 381 if (sa->sa_handler == SIG_DFL) 382 ps->ps_sigcatch &= ~bit; 383 else 384 ps->ps_sigcatch |= bit; 385 } 386 mtx_leave(&p->p_p->ps_mtx); 387 } 388 389 /* 390 * Initialize signal state for process 0; 391 * set to ignore signals that are ignored by default. 392 */ 393 void 394 siginit(struct sigacts *ps) 395 { 396 int i; 397 398 for (i = 0; i < NSIG; i++) 399 if (sigprop[i] & SA_IGNORE && i != SIGCONT) 400 ps->ps_sigignore |= sigmask(i); 401 ps->ps_sigflags = SAS_NOCLDWAIT | SAS_NOCLDSTOP; 402 } 403 404 /* 405 * Reset signals for an exec by the specified thread. 406 */ 407 void 408 execsigs(struct proc *p) 409 { 410 struct sigacts *ps; 411 int nc, mask; 412 413 ps = p->p_p->ps_sigacts; 414 mtx_enter(&p->p_p->ps_mtx); 415 416 /* 417 * Reset caught signals. Held signals remain held 418 * through p_sigmask (unless they were caught, 419 * and are now ignored by default). 420 */ 421 while (ps->ps_sigcatch) { 422 nc = ffs((long)ps->ps_sigcatch); 423 mask = sigmask(nc); 424 ps->ps_sigcatch &= ~mask; 425 if (sigprop[nc] & SA_IGNORE) { 426 if (nc != SIGCONT) 427 ps->ps_sigignore |= mask; 428 atomic_clearbits_int(&p->p_siglist, mask); 429 atomic_clearbits_int(&p->p_p->ps_siglist, mask); 430 } 431 ps->ps_sigact[nc] = SIG_DFL; 432 } 433 /* 434 * Reset stack state to the user stack. 435 * Clear set of signals caught on the signal stack. 436 */ 437 sigstkinit(&p->p_sigstk); 438 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 439 if (ps->ps_sigact[SIGCHLD] == SIG_IGN) 440 ps->ps_sigact[SIGCHLD] = SIG_DFL; 441 mtx_leave(&p->p_p->ps_mtx); 442 } 443 444 /* 445 * Manipulate signal mask. 446 * Note that we receive new mask, not pointer, 447 * and return old mask as return value; 448 * the library stub does the rest. 449 */ 450 int 451 sys_sigprocmask(struct proc *p, void *v, register_t *retval) 452 { 453 struct sys_sigprocmask_args /* { 454 syscallarg(int) how; 455 syscallarg(sigset_t) mask; 456 } */ *uap = v; 457 int error = 0; 458 sigset_t mask; 459 460 KASSERT(p == curproc); 461 462 *retval = p->p_sigmask; 463 mask = SCARG(uap, mask) &~ sigcantmask; 464 465 switch (SCARG(uap, how)) { 466 case SIG_BLOCK: 467 SET(p->p_sigmask, mask); 468 break; 469 case SIG_UNBLOCK: 470 CLR(p->p_sigmask, mask); 471 break; 472 case SIG_SETMASK: 473 p->p_sigmask = mask; 474 break; 475 default: 476 error = EINVAL; 477 break; 478 } 479 return (error); 480 } 481 482 int 483 sys_sigpending(struct proc *p, void *v, register_t *retval) 484 { 485 *retval = p->p_siglist | p->p_p->ps_siglist; 486 return (0); 487 } 488 489 /* 490 * Temporarily replace calling proc's signal mask for the duration of a 491 * system call. Original signal mask will be restored by userret(). 492 */ 493 void 494 dosigsuspend(struct proc *p, sigset_t newmask) 495 { 496 KASSERT(p == curproc); 497 498 p->p_oldmask = p->p_sigmask; 499 p->p_sigmask = newmask; 500 atomic_setbits_int(&p->p_flag, P_SIGSUSPEND); 501 } 502 503 /* 504 * Suspend thread until signal, providing mask to be set 505 * in the meantime. Note nonstandard calling convention: 506 * libc stub passes mask, not pointer, to save a copyin. 507 */ 508 int 509 sys_sigsuspend(struct proc *p, void *v, register_t *retval) 510 { 511 struct sys_sigsuspend_args /* { 512 syscallarg(int) mask; 513 } */ *uap = v; 514 515 dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask); 516 while (tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigsusp", INFSLP) == 0) 517 continue; 518 /* always return EINTR rather than ERESTART... */ 519 return (EINTR); 520 } 521 522 int 523 sigonstack(size_t stack) 524 { 525 const struct sigaltstack *ss = &curproc->p_sigstk; 526 527 return (ss->ss_flags & SS_DISABLE ? 0 : 528 (stack - (size_t)ss->ss_sp < ss->ss_size)); 529 } 530 531 int 532 sys_sigaltstack(struct proc *p, void *v, register_t *retval) 533 { 534 struct sys_sigaltstack_args /* { 535 syscallarg(const struct sigaltstack *) nss; 536 syscallarg(struct sigaltstack *) oss; 537 } */ *uap = v; 538 struct sigaltstack ss; 539 const struct sigaltstack *nss; 540 struct sigaltstack *oss; 541 int onstack = sigonstack(PROC_STACK(p)); 542 int error; 543 544 nss = SCARG(uap, nss); 545 oss = SCARG(uap, oss); 546 547 if (oss != NULL) { 548 ss = p->p_sigstk; 549 if (onstack) 550 ss.ss_flags |= SS_ONSTACK; 551 if ((error = copyout(&ss, oss, sizeof(ss)))) 552 return (error); 553 } 554 if (nss == NULL) 555 return (0); 556 error = copyin(nss, &ss, sizeof(ss)); 557 if (error) 558 return (error); 559 if (onstack) 560 return (EPERM); 561 if (ss.ss_flags & ~SS_DISABLE) 562 return (EINVAL); 563 if (ss.ss_flags & SS_DISABLE) { 564 p->p_sigstk.ss_flags = ss.ss_flags; 565 return (0); 566 } 567 if (ss.ss_size < MINSIGSTKSZ) 568 return (ENOMEM); 569 570 error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size); 571 if (error) 572 return (error); 573 574 p->p_sigstk = ss; 575 return (0); 576 } 577 578 int 579 sys_kill(struct proc *cp, void *v, register_t *retval) 580 { 581 struct sys_kill_args /* { 582 syscallarg(int) pid; 583 syscallarg(int) signum; 584 } */ *uap = v; 585 struct process *pr; 586 int pid = SCARG(uap, pid); 587 int signum = SCARG(uap, signum); 588 int error; 589 int zombie = 0; 590 591 if ((error = pledge_kill(cp, pid)) != 0) 592 return (error); 593 if (((u_int)signum) >= NSIG) 594 return (EINVAL); 595 if (pid > 0) { 596 if ((pr = prfind(pid)) == NULL) { 597 if ((pr = zombiefind(pid)) == NULL) 598 return (ESRCH); 599 else 600 zombie = 1; 601 } 602 if (!cansignal(cp, pr, signum)) 603 return (EPERM); 604 605 /* kill single process */ 606 if (signum && !zombie) 607 prsignal(pr, signum); 608 return (0); 609 } 610 switch (pid) { 611 case -1: /* broadcast signal */ 612 return (killpg1(cp, signum, 0, 1)); 613 case 0: /* signal own process group */ 614 return (killpg1(cp, signum, 0, 0)); 615 default: /* negative explicit process group */ 616 return (killpg1(cp, signum, -pid, 0)); 617 } 618 } 619 620 int 621 sys_thrkill(struct proc *cp, void *v, register_t *retval) 622 { 623 struct sys_thrkill_args /* { 624 syscallarg(pid_t) tid; 625 syscallarg(int) signum; 626 syscallarg(void *) tcb; 627 } */ *uap = v; 628 struct proc *p; 629 int tid = SCARG(uap, tid); 630 int signum = SCARG(uap, signum); 631 void *tcb; 632 633 if (((u_int)signum) >= NSIG) 634 return (EINVAL); 635 636 p = tid ? tfind_user(tid, cp->p_p) : cp; 637 if (p == NULL) 638 return (ESRCH); 639 640 /* optionally require the target thread to have the given tcb addr */ 641 tcb = SCARG(uap, tcb); 642 if (tcb != NULL && tcb != TCB_GET(p)) 643 return (ESRCH); 644 645 if (signum) 646 ptsignal(p, signum, STHREAD); 647 return (0); 648 } 649 650 /* 651 * Common code for kill process group/broadcast kill. 652 * cp is calling process. 653 */ 654 int 655 killpg1(struct proc *cp, int signum, int pgid, int all) 656 { 657 struct process *pr; 658 struct pgrp *pgrp; 659 int nfound = 0; 660 661 if (all) { 662 /* 663 * broadcast 664 */ 665 LIST_FOREACH(pr, &allprocess, ps_list) { 666 if (pr->ps_pid <= 1 || 667 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) || 668 pr == cp->p_p || !cansignal(cp, pr, signum)) 669 continue; 670 nfound++; 671 if (signum) 672 prsignal(pr, signum); 673 } 674 } else { 675 if (pgid == 0) 676 /* 677 * zero pgid means send to my process group. 678 */ 679 pgrp = cp->p_p->ps_pgrp; 680 else { 681 pgrp = pgfind(pgid); 682 if (pgrp == NULL) 683 return (ESRCH); 684 } 685 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 686 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM || 687 !cansignal(cp, pr, signum)) 688 continue; 689 nfound++; 690 if (signum) 691 prsignal(pr, signum); 692 } 693 } 694 return (nfound ? 0 : ESRCH); 695 } 696 697 #define CANDELIVER(uid, euid, pr) \ 698 (euid == 0 || \ 699 (uid) == (pr)->ps_ucred->cr_ruid || \ 700 (uid) == (pr)->ps_ucred->cr_svuid || \ 701 (uid) == (pr)->ps_ucred->cr_uid || \ 702 (euid) == (pr)->ps_ucred->cr_ruid || \ 703 (euid) == (pr)->ps_ucred->cr_svuid || \ 704 (euid) == (pr)->ps_ucred->cr_uid) 705 706 #define CANSIGIO(cr, pr) \ 707 CANDELIVER((cr)->cr_ruid, (cr)->cr_uid, (pr)) 708 709 /* 710 * Send a signal to a process group. If checktty is 1, 711 * limit to members which have a controlling terminal. 712 */ 713 void 714 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 715 { 716 struct process *pr; 717 718 if (pgrp) 719 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 720 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT) 721 prsignal(pr, signum); 722 } 723 724 /* 725 * Send a SIGIO or SIGURG signal to a process or process group using stored 726 * credentials rather than those of the current process. 727 */ 728 void 729 pgsigio(struct sigio_ref *sir, int sig, int checkctty) 730 { 731 struct process *pr; 732 struct sigio *sigio; 733 734 if (sir->sir_sigio == NULL) 735 return; 736 737 KERNEL_LOCK(); 738 mtx_enter(&sigio_lock); 739 sigio = sir->sir_sigio; 740 if (sigio == NULL) 741 goto out; 742 if (sigio->sio_pgid > 0) { 743 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc)) 744 prsignal(sigio->sio_proc, sig); 745 } else if (sigio->sio_pgid < 0) { 746 LIST_FOREACH(pr, &sigio->sio_pgrp->pg_members, ps_pglist) { 747 if (CANSIGIO(sigio->sio_ucred, pr) && 748 (checkctty == 0 || (pr->ps_flags & PS_CONTROLT))) 749 prsignal(pr, sig); 750 } 751 } 752 out: 753 mtx_leave(&sigio_lock); 754 KERNEL_UNLOCK(); 755 } 756 757 /* 758 * Recalculate the signal mask and reset the signal disposition after 759 * usermode frame for delivery is formed. 760 */ 761 void 762 postsig_done(struct proc *p, int signum, sigset_t catchmask, int reset) 763 { 764 p->p_ru.ru_nsignals++; 765 SET(p->p_sigmask, catchmask); 766 if (reset != 0) { 767 sigset_t mask = sigmask(signum); 768 struct sigacts *ps = p->p_p->ps_sigacts; 769 770 mtx_enter(&p->p_p->ps_mtx); 771 ps->ps_sigcatch &= ~mask; 772 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 773 ps->ps_sigignore |= mask; 774 ps->ps_sigact[signum] = SIG_DFL; 775 mtx_leave(&p->p_p->ps_mtx); 776 } 777 } 778 779 /* 780 * Send a signal caused by a trap to the current thread 781 * If it will be caught immediately, deliver it with correct code. 782 * Otherwise, post it normally. 783 */ 784 void 785 trapsignal(struct proc *p, int signum, u_long trapno, int code, 786 union sigval sigval) 787 { 788 struct process *pr = p->p_p; 789 struct sigctx ctx; 790 int mask; 791 792 switch (signum) { 793 case SIGILL: 794 if (code == ILL_BTCFI) { 795 pr->ps_acflag |= ABTCFI; 796 break; 797 } 798 /* FALLTHROUGH */ 799 case SIGBUS: 800 case SIGSEGV: 801 pr->ps_acflag |= ATRAP; 802 break; 803 } 804 805 mask = sigmask(signum); 806 setsigctx(p, signum, &ctx); 807 if ((pr->ps_flags & PS_TRACED) == 0 && ctx.sig_catch != 0 && 808 (p->p_sigmask & mask) == 0) { 809 siginfo_t si; 810 811 initsiginfo(&si, signum, trapno, code, sigval); 812 #ifdef KTRACE 813 if (KTRPOINT(p, KTR_PSIG)) { 814 ktrpsig(p, signum, ctx.sig_action, 815 p->p_sigmask, code, &si); 816 } 817 #endif 818 if (sendsig(ctx.sig_action, signum, p->p_sigmask, &si, 819 ctx.sig_info, ctx.sig_onstack)) { 820 KERNEL_LOCK(); 821 sigexit(p, SIGILL); 822 /* NOTREACHED */ 823 } 824 postsig_done(p, signum, ctx.sig_catchmask, ctx.sig_reset); 825 } else { 826 p->p_sisig = signum; 827 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 828 p->p_sicode = code; 829 p->p_sigval = sigval; 830 831 /* 832 * If traced, stop if signal is masked, and stay stopped 833 * until released by the debugger. If our parent process 834 * is waiting for us, don't hang as we could deadlock. 835 */ 836 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 837 signum != SIGKILL && (p->p_sigmask & mask) != 0) { 838 signum = proc_trap(p, signum); 839 840 mask = sigmask(signum); 841 setsigctx(p, signum, &ctx); 842 843 /* 844 * If we are no longer being traced, or the parent 845 * didn't give us a signal, skip sending the signal. 846 */ 847 if ((pr->ps_flags & PS_TRACED) == 0 || signum == 0) 848 return; 849 850 /* update signal info */ 851 p->p_sisig = signum; 852 } 853 854 /* 855 * Signals like SIGBUS and SIGSEGV should not, when 856 * generated by the kernel, be ignorable or blockable. 857 * If it is and we're not being traced, then just kill 858 * the process. 859 * After vfs_shutdown(9), init(8) cannot receive signals 860 * because new code pages of the signal handler cannot be 861 * mapped from halted storage. init(8) may not die or the 862 * kernel panics. Better loop between signal handler and 863 * page fault trap until the machine is halted. 864 */ 865 if ((pr->ps_flags & PS_TRACED) == 0 && 866 (sigprop[signum] & SA_KILL) && 867 ((p->p_sigmask & mask) || ctx.sig_ignore) && 868 pr->ps_pid != 1) { 869 KERNEL_LOCK(); 870 sigexit(p, signum); 871 /* NOTREACHED */ 872 } 873 ptsignal(p, signum, STHREAD); 874 } 875 } 876 877 /* 878 * Send the signal to the process. If the signal has an action, the action 879 * is usually performed by the target process rather than the caller; we add 880 * the signal to the set of pending signals for the process. 881 * 882 * Exceptions: 883 * o When a stop signal is sent to a sleeping process that takes the 884 * default action, the process is stopped without awakening it. 885 * o SIGCONT restarts stopped processes (or puts them back to sleep) 886 * regardless of the signal action (eg, blocked or ignored). 887 * 888 * Other ignored signals are discarded immediately. 889 */ 890 void 891 psignal(struct proc *p, int signum) 892 { 893 ptsignal(p, signum, SPROCESS); 894 } 895 896 void 897 prsignal(struct process *pr, int signum) 898 { 899 mtx_enter(&pr->ps_mtx); 900 /* Ignore signal if the target process is exiting */ 901 if (pr->ps_flags & PS_EXITING) { 902 mtx_leave(&pr->ps_mtx); 903 return; 904 } 905 ptsignal_locked(TAILQ_FIRST(&pr->ps_threads), signum, SPROCESS); 906 mtx_leave(&pr->ps_mtx); 907 } 908 909 /* 910 * type = SPROCESS process signal, can be diverted (sigwait()) 911 * type = STHREAD thread signal, but should be propagated if unhandled 912 * type = SPROPAGATED propagated to this thread, so don't propagate again 913 */ 914 void 915 ptsignal(struct proc *p, int signum, enum signal_type type) 916 { 917 struct process *pr = p->p_p; 918 919 mtx_enter(&pr->ps_mtx); 920 ptsignal_locked(p, signum, type); 921 mtx_leave(&pr->ps_mtx); 922 } 923 924 void 925 ptsignal_locked(struct proc *p, int signum, enum signal_type type) 926 { 927 int prop; 928 sig_t action, altaction = SIG_DFL; 929 sigset_t mask, sigmask; 930 int *siglist; 931 struct process *pr = p->p_p; 932 struct proc *q; 933 int wakeparent = 0; 934 935 MUTEX_ASSERT_LOCKED(&pr->ps_mtx); 936 937 #ifdef DIAGNOSTIC 938 if ((u_int)signum >= NSIG || signum == 0) 939 panic("psignal signal number"); 940 #endif 941 942 /* Ignore signal if the target process is exiting */ 943 if (pr->ps_flags & PS_EXITING) 944 return; 945 946 mask = sigmask(signum); 947 sigmask = READ_ONCE(p->p_sigmask); 948 949 if (type == SPROCESS) { 950 sigset_t tmpmask; 951 952 /* Accept SIGKILL to coredumping processes */ 953 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 954 atomic_setbits_int(&pr->ps_siglist, mask); 955 return; 956 } 957 958 /* 959 * If the current thread can process the signal 960 * immediately (it's unblocked) then have it take it. 961 */ 962 q = curproc; 963 tmpmask = READ_ONCE(q->p_sigmask); 964 if (q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 965 (tmpmask & mask) == 0) { 966 p = q; 967 sigmask = tmpmask; 968 } else { 969 /* 970 * A process-wide signal can be diverted to a 971 * different thread that's in sigwait() for this 972 * signal. If there isn't such a thread, then 973 * pick a thread that doesn't have it blocked so 974 * that the stop/kill consideration isn't 975 * delayed. Otherwise, mark it pending on the 976 * main thread. 977 */ 978 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 979 980 /* ignore exiting threads */ 981 if (q->p_flag & P_WEXIT) 982 continue; 983 984 /* skip threads that have the signal blocked */ 985 tmpmask = READ_ONCE(q->p_sigmask); 986 if ((tmpmask & mask) != 0) 987 continue; 988 989 /* okay, could send to this thread */ 990 p = q; 991 sigmask = tmpmask; 992 993 /* 994 * sigsuspend, sigwait, ppoll/pselect, etc? 995 * Definitely go to this thread, as it's 996 * already blocked in the kernel. 997 */ 998 if (q->p_flag & P_SIGSUSPEND) 999 break; 1000 } 1001 } 1002 } 1003 1004 if (type != SPROPAGATED) 1005 knote_locked(&pr->ps_klist, NOTE_SIGNAL | signum); 1006 1007 prop = sigprop[signum]; 1008 1009 /* 1010 * If proc is traced, always give parent a chance. 1011 */ 1012 if (pr->ps_flags & PS_TRACED) { 1013 action = SIG_DFL; 1014 } else { 1015 sigset_t sigcatch, sigignore; 1016 1017 /* 1018 * If the signal is being ignored, 1019 * then we forget about it immediately. 1020 * (Note: we don't set SIGCONT in ps_sigignore, 1021 * and if it is set to SIG_IGN, 1022 * action will be SIG_DFL here.) 1023 */ 1024 sigignore = pr->ps_sigacts->ps_sigignore; 1025 sigcatch = pr->ps_sigacts->ps_sigcatch; 1026 1027 if (sigignore & mask) 1028 return; 1029 if (sigmask & mask) { 1030 action = SIG_HOLD; 1031 if (sigcatch & mask) 1032 altaction = SIG_CATCH; 1033 } else if (sigcatch & mask) { 1034 action = SIG_CATCH; 1035 } else { 1036 action = SIG_DFL; 1037 1038 if (prop & SA_KILL && pr->ps_nice > NZERO) 1039 pr->ps_nice = NZERO; 1040 1041 /* 1042 * If sending a tty stop signal to a member of an 1043 * orphaned process group, discard the signal here if 1044 * the action is default; don't stop the process below 1045 * if sleeping, and don't clear any pending SIGCONT. 1046 */ 1047 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 1048 return; 1049 } 1050 } 1051 /* 1052 * If delivered to process, mark as pending there. Continue and stop 1053 * signals will be propagated to all threads. So they are always 1054 * marked at thread level. 1055 */ 1056 siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist; 1057 if (prop & (SA_CONT | SA_STOP)) 1058 siglist = &p->p_siglist; 1059 1060 /* 1061 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 1062 */ 1063 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 1064 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 1065 if (q != p) 1066 ptsignal_locked(q, signum, SPROPAGATED); 1067 1068 SCHED_LOCK(); 1069 1070 switch (p->p_stat) { 1071 1072 case SSTOP: 1073 /* 1074 * If traced process is already stopped, 1075 * then no further action is necessary. 1076 */ 1077 if (pr->ps_flags & PS_TRACED) 1078 goto out; 1079 1080 /* 1081 * Kill signal always sets processes running. 1082 */ 1083 if (signum == SIGKILL) { 1084 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1085 /* Raise priority to at least PUSER. */ 1086 if (p->p_usrpri > PUSER) 1087 p->p_usrpri = PUSER; 1088 unsleep(p); 1089 setrunnable(p); 1090 goto out; 1091 } 1092 1093 if (prop & SA_CONT) { 1094 /* 1095 * If SIGCONT is default (or ignored), we continue the 1096 * process but don't leave the signal in p_siglist, as 1097 * it has no further action. If SIGCONT is held, we 1098 * continue the process and leave the signal in 1099 * p_siglist. If the process catches SIGCONT, let it 1100 * handle the signal itself. If it isn't waiting on 1101 * an event, then it goes back to run state. 1102 * Otherwise, process goes back to sleep state. 1103 */ 1104 atomic_setbits_int(&pr->ps_flags, PS_CONTINUED); 1105 atomic_clearbits_int(&pr->ps_flags, 1106 PS_WAITED | PS_STOPPED); 1107 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1108 wakeparent = 1; 1109 if (action == SIG_DFL) 1110 mask = 0; 1111 if (action == SIG_CATCH) { 1112 /* Raise priority to at least PUSER. */ 1113 if (p->p_usrpri > PUSER) 1114 p->p_usrpri = PUSER; 1115 unsleep(p); 1116 setrunnable(p); 1117 goto out; 1118 } 1119 if (p->p_wchan == NULL) { 1120 setrunnable(p); 1121 goto out; 1122 } 1123 atomic_clearbits_int(&p->p_flag, P_WSLEEP); 1124 p->p_stat = SSLEEP; 1125 goto out; 1126 } 1127 1128 /* 1129 * Defer further processing for signals which are held, 1130 * except that stopped processes must be continued by SIGCONT. 1131 */ 1132 if (action == SIG_HOLD) 1133 goto out; 1134 1135 if (prop & SA_STOP) { 1136 /* 1137 * Already stopped, don't need to stop again. 1138 * (If we did the shell could get confused.) 1139 */ 1140 mask = 0; 1141 goto out; 1142 } 1143 1144 /* 1145 * If process is sleeping interruptibly, then simulate a 1146 * wakeup so that when it is continued, it will be made 1147 * runnable and can look at the signal. But don't make 1148 * the process runnable, leave it stopped. 1149 */ 1150 if (p->p_flag & P_SINTR) 1151 unsleep(p); 1152 goto out; 1153 1154 case SSLEEP: 1155 /* 1156 * If process is sleeping uninterruptibly 1157 * we can't interrupt the sleep... the signal will 1158 * be noticed when the process returns through 1159 * trap() or syscall(). 1160 */ 1161 if ((p->p_flag & P_SINTR) == 0) 1162 goto out; 1163 /* 1164 * Process is sleeping and traced... make it runnable 1165 * so it can discover the signal in cursig() and stop 1166 * for the parent. 1167 */ 1168 if (pr->ps_flags & PS_TRACED) { 1169 unsleep(p); 1170 setrunnable(p); 1171 goto out; 1172 } 1173 1174 /* 1175 * Recheck sigmask before waking up the process, 1176 * there is a chance that while sending the signal 1177 * the process changed sigmask and went to sleep. 1178 */ 1179 sigmask = READ_ONCE(p->p_sigmask); 1180 if (sigmask & mask) 1181 goto out; 1182 else if (action == SIG_HOLD) { 1183 /* signal got unmasked, get proper action */ 1184 action = altaction; 1185 1186 if (action == SIG_DFL) { 1187 if (prop & SA_KILL && pr->ps_nice > NZERO) 1188 pr->ps_nice = NZERO; 1189 1190 /* 1191 * Discard tty stop signals sent to an 1192 * orphaned process group, see above. 1193 */ 1194 if (prop & SA_TTYSTOP && 1195 pr->ps_pgrp->pg_jobc == 0) { 1196 mask = 0; 1197 prop = 0; 1198 goto out; 1199 } 1200 } 1201 } 1202 1203 /* 1204 * If SIGCONT is default (or ignored) and process is 1205 * asleep, we are finished; the process should not 1206 * be awakened. 1207 */ 1208 if ((prop & SA_CONT) && action == SIG_DFL) { 1209 mask = 0; 1210 goto out; 1211 } 1212 /* 1213 * When a sleeping process receives a stop 1214 * signal, process immediately if possible. 1215 */ 1216 if ((prop & SA_STOP) && action == SIG_DFL) { 1217 /* 1218 * If a child holding parent blocked, 1219 * stopping could cause deadlock. 1220 */ 1221 if (pr->ps_flags & PS_PPWAIT) 1222 goto out; 1223 mask = 0; 1224 pr->ps_xsig = signum; 1225 proc_stop(p, 0); 1226 goto out; 1227 } 1228 /* 1229 * All other (caught or default) signals 1230 * cause the process to run. 1231 * Raise priority to at least PUSER. 1232 */ 1233 if (p->p_usrpri > PUSER) 1234 p->p_usrpri = PUSER; 1235 unsleep(p); 1236 setrunnable(p); 1237 goto out; 1238 /* NOTREACHED */ 1239 1240 case SONPROC: 1241 if (action == SIG_HOLD) 1242 goto out; 1243 1244 /* set siglist before issuing the ast */ 1245 atomic_setbits_int(siglist, mask); 1246 mask = 0; 1247 signotify(p); 1248 /* FALLTHROUGH */ 1249 default: 1250 /* 1251 * SRUN, SIDL, SDEAD do nothing with the signal, 1252 * other than kicking ourselves if we are running. 1253 * It will either never be noticed, or noticed very soon. 1254 */ 1255 goto out; 1256 } 1257 /* NOTREACHED */ 1258 1259 out: 1260 /* finally adjust siglist */ 1261 if (mask) 1262 atomic_setbits_int(siglist, mask); 1263 if (prop & SA_CONT) { 1264 atomic_clearbits_int(siglist, STOPSIGMASK); 1265 } 1266 if (prop & SA_STOP) { 1267 atomic_clearbits_int(siglist, CONTSIGMASK); 1268 atomic_clearbits_int(&pr->ps_flags, PS_CONTINUED); 1269 } 1270 1271 SCHED_UNLOCK(); 1272 if (wakeparent) 1273 wakeup(pr->ps_pptr); 1274 } 1275 1276 /* fill the signal context which should be used by postsig() and issignal() */ 1277 void 1278 setsigctx(struct proc *p, int signum, struct sigctx *sctx) 1279 { 1280 struct process *pr = p->p_p; 1281 struct sigacts *ps = pr->ps_sigacts; 1282 sigset_t mask; 1283 1284 mtx_enter(&pr->ps_mtx); 1285 mask = sigmask(signum); 1286 sctx->sig_action = ps->ps_sigact[signum]; 1287 sctx->sig_catchmask = ps->ps_catchmask[signum]; 1288 sctx->sig_reset = (ps->ps_sigreset & mask) != 0; 1289 sctx->sig_info = (ps->ps_siginfo & mask) != 0; 1290 sctx->sig_intr = (ps->ps_sigintr & mask) != 0; 1291 sctx->sig_onstack = (ps->ps_sigonstack & mask) != 0; 1292 sctx->sig_ignore = (ps->ps_sigignore & mask) != 0; 1293 sctx->sig_catch = (ps->ps_sigcatch & mask) != 0; 1294 sctx->sig_stop = sigprop[signum] & SA_STOP && 1295 (long)sctx->sig_action == (long)SIG_DFL; 1296 if (sctx->sig_stop) { 1297 /* 1298 * If the process is a member of an orphaned 1299 * process group, ignore tty stop signals. 1300 */ 1301 if (pr->ps_flags & PS_TRACED || 1302 (pr->ps_pgrp->pg_jobc == 0 && 1303 sigprop[signum] & SA_TTYSTOP)) { 1304 sctx->sig_stop = 0; 1305 sctx->sig_ignore = 1; 1306 } 1307 } 1308 mtx_leave(&pr->ps_mtx); 1309 } 1310 1311 /* 1312 * Determine signal that should be delivered to process p, the current 1313 * process, 0 if none. 1314 * 1315 * If the current process has received a signal (should be caught or cause 1316 * termination, should interrupt current syscall), return the signal number. 1317 * Stop signals with default action are processed immediately, then cleared; 1318 * they aren't returned. This is checked after each entry to the system for 1319 * a syscall or trap. The normal call sequence is 1320 * 1321 * while (signum = cursig(curproc, &ctx, 0)) 1322 * postsig(signum, &ctx); 1323 * 1324 * Assumes that if the P_SINTR flag is set, we're holding both the 1325 * kernel and scheduler locks. 1326 */ 1327 int 1328 cursig(struct proc *p, struct sigctx *sctx, int deep) 1329 { 1330 struct process *pr = p->p_p; 1331 int signum, mask, keep = 0, prop; 1332 sigset_t ps_siglist; 1333 1334 KASSERT(p == curproc); 1335 1336 for (;;) { 1337 ps_siglist = READ_ONCE(pr->ps_siglist); 1338 membar_consumer(); 1339 mask = SIGPENDING(p); 1340 if (pr->ps_flags & PS_PPWAIT) 1341 mask &= ~STOPSIGMASK; 1342 signum = ffs(mask); 1343 if (signum == 0) /* no signal to send */ 1344 goto keep; 1345 mask = sigmask(signum); 1346 1347 /* take the signal! */ 1348 if (atomic_cas_uint(&pr->ps_siglist, ps_siglist, 1349 ps_siglist & ~mask) != ps_siglist) { 1350 /* lost race taking the process signal, restart */ 1351 continue; 1352 } 1353 atomic_clearbits_int(&p->p_siglist, mask); 1354 setsigctx(p, signum, sctx); 1355 1356 /* 1357 * We should see pending but ignored signals 1358 * only if PS_TRACED was on when they were posted. 1359 */ 1360 if (sctx->sig_ignore && (pr->ps_flags & PS_TRACED) == 0) 1361 continue; 1362 1363 /* 1364 * If cursig is called while going to sleep, abort now 1365 * and stop the sleep. When the call unwinded to userret 1366 * cursig is called again and there the signal can be 1367 * handled cleanly. 1368 */ 1369 if (deep) { 1370 /* 1371 * Do not stop the thread here if multiple 1372 * signals are pending and at least one of 1373 * them would force an unwind. 1374 * 1375 * ffs() favors low numbered signals and 1376 * so stop signals may be picked up before 1377 * other pending signals. 1378 */ 1379 if (sctx->sig_stop && SIGPENDING(p)) { 1380 keep |= mask; 1381 continue; 1382 } 1383 goto keep; 1384 } 1385 1386 /* 1387 * If traced, always stop, and stay stopped until released 1388 * by the debugger. If our parent process is waiting for 1389 * us, don't hang as we could deadlock. 1390 */ 1391 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1392 signum != SIGKILL) { 1393 signum = proc_trap(p, signum); 1394 1395 mask = sigmask(signum); 1396 setsigctx(p, signum, sctx); 1397 1398 /* 1399 * If we are no longer being traced, or the parent 1400 * didn't give us a signal, or the signal is ignored, 1401 * look for more signals. 1402 */ 1403 if ((pr->ps_flags & PS_TRACED) == 0 || signum == 0 || 1404 sctx->sig_ignore) 1405 continue; 1406 1407 /* 1408 * If the new signal is being masked, look for other 1409 * signals but leave it for later. 1410 */ 1411 if ((p->p_sigmask & mask) != 0) { 1412 atomic_setbits_int(&p->p_siglist, mask); 1413 continue; 1414 } 1415 1416 } 1417 1418 prop = sigprop[signum]; 1419 1420 /* 1421 * Decide whether the signal should be returned. 1422 * Return the signal's number, or fall through 1423 * to clear it from the pending mask. 1424 */ 1425 switch ((long)sctx->sig_action) { 1426 case (long)SIG_DFL: 1427 /* 1428 * Don't take default actions on system processes. 1429 */ 1430 if (pr->ps_pid <= 1) { 1431 #ifdef DIAGNOSTIC 1432 /* 1433 * Are you sure you want to ignore SIGSEGV 1434 * in init? XXX 1435 */ 1436 printf("Process (pid %d) got signal" 1437 " %d\n", pr->ps_pid, signum); 1438 #endif 1439 break; /* == ignore */ 1440 } 1441 /* 1442 * If there is a pending stop signal to process 1443 * with default action, stop here, 1444 * then clear the signal. 1445 */ 1446 if (sctx->sig_stop) { 1447 pr->ps_xsig = signum; 1448 SCHED_LOCK(); 1449 proc_stop(p, 1); 1450 SCHED_UNLOCK(); 1451 break; 1452 } else if (prop & SA_IGNORE) { 1453 /* 1454 * Except for SIGCONT, shouldn't get here. 1455 * Default action is to ignore; drop it. 1456 */ 1457 break; /* == ignore */ 1458 } else 1459 goto keep; 1460 /* NOTREACHED */ 1461 case (long)SIG_IGN: 1462 /* 1463 * Masking above should prevent us ever trying 1464 * to take action on an ignored signal other 1465 * than SIGCONT, unless process is traced. 1466 */ 1467 if ((prop & SA_CONT) == 0 && 1468 (pr->ps_flags & PS_TRACED) == 0) 1469 printf("%s\n", __func__); 1470 break; /* == ignore */ 1471 default: 1472 /* 1473 * This signal has an action, let 1474 * postsig() process it. 1475 */ 1476 goto keep; 1477 } 1478 } 1479 /* NOTREACHED */ 1480 1481 keep: 1482 /* 1483 * if we stashed a stop signal but no other signal is pending 1484 * anymore pick the stop signal up again. 1485 */ 1486 if (keep != 0 && signum == 0) { 1487 signum = ffs(keep); 1488 setsigctx(p, signum, sctx); 1489 } 1490 /* move the signal to p_siglist for later */ 1491 atomic_setbits_int(&p->p_siglist, mask | keep); 1492 return (signum); 1493 } 1494 1495 int 1496 proc_trap(struct proc *p, int signum) 1497 { 1498 struct process *pr = p->p_p; 1499 1500 single_thread_set(p, SINGLE_SUSPEND | SINGLE_NOWAIT); 1501 pr->ps_xsig = signum; 1502 1503 SCHED_LOCK(); 1504 proc_stop(p, 1); 1505 SCHED_UNLOCK(); 1506 1507 signum = pr->ps_xsig; 1508 pr->ps_xsig = 0; 1509 if ((p->p_flag & P_TRACESINGLE) == 0) 1510 single_thread_clear(p, 0); 1511 atomic_clearbits_int(&p->p_flag, P_TRACESINGLE); 1512 1513 return signum; 1514 } 1515 1516 /* 1517 * Put the argument process into the stopped state and notify the parent 1518 * via wakeup. Signals are handled elsewhere. The process must not be 1519 * on the run queue. 1520 */ 1521 void 1522 proc_stop(struct proc *p, int sw) 1523 { 1524 struct process *pr = p->p_p; 1525 1526 #ifdef MULTIPROCESSOR 1527 SCHED_ASSERT_LOCKED(); 1528 #endif 1529 /* do not stop exiting procs */ 1530 if (ISSET(p->p_flag, P_WEXIT)) 1531 return; 1532 1533 p->p_stat = SSTOP; 1534 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1535 atomic_setbits_int(&pr->ps_flags, PS_STOPPING); 1536 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1537 /* 1538 * We need this soft interrupt to be handled fast. 1539 * Extra calls to softclock don't hurt. 1540 */ 1541 softintr_schedule(proc_stop_si); 1542 if (sw) 1543 mi_switch(); 1544 } 1545 1546 /* 1547 * Called from a soft interrupt to send signals to the parents of stopped 1548 * processes. 1549 * We can't do this in proc_stop because it's called with nasty locks held 1550 * and we would need recursive scheduler lock to deal with that. 1551 */ 1552 void 1553 proc_stop_sweep(void *v) 1554 { 1555 struct process *pr; 1556 1557 LIST_FOREACH(pr, &allprocess, ps_list) { 1558 if ((pr->ps_flags & PS_STOPPING) == 0) 1559 continue; 1560 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1561 atomic_clearbits_int(&pr->ps_flags, PS_STOPPING); 1562 1563 if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0) 1564 prsignal(pr->ps_pptr, SIGCHLD); 1565 wakeup(pr->ps_pptr); 1566 } 1567 } 1568 1569 /* 1570 * Take the action for the specified signal 1571 * from the current set of pending signals. 1572 */ 1573 void 1574 postsig(struct proc *p, int signum, struct sigctx *sctx) 1575 { 1576 u_long trapno; 1577 int mask, returnmask; 1578 siginfo_t si; 1579 union sigval sigval; 1580 int code; 1581 1582 KASSERT(signum != 0); 1583 1584 mask = sigmask(signum); 1585 atomic_clearbits_int(&p->p_siglist, mask); 1586 sigval.sival_ptr = NULL; 1587 1588 if (p->p_sisig != signum) { 1589 trapno = 0; 1590 code = SI_USER; 1591 sigval.sival_ptr = NULL; 1592 } else { 1593 trapno = p->p_sitrapno; 1594 code = p->p_sicode; 1595 sigval = p->p_sigval; 1596 } 1597 initsiginfo(&si, signum, trapno, code, sigval); 1598 1599 #ifdef KTRACE 1600 if (KTRPOINT(p, KTR_PSIG)) { 1601 ktrpsig(p, signum, sctx->sig_action, p->p_flag & P_SIGSUSPEND ? 1602 p->p_oldmask : p->p_sigmask, code, &si); 1603 } 1604 #endif 1605 if (sctx->sig_action == SIG_DFL) { 1606 /* 1607 * Default action, where the default is to kill 1608 * the process. (Other cases were ignored above.) 1609 */ 1610 KERNEL_LOCK(); 1611 sigexit(p, signum); 1612 /* NOTREACHED */ 1613 } else { 1614 /* 1615 * If we get here, the signal must be caught. 1616 */ 1617 #ifdef DIAGNOSTIC 1618 if (sctx->sig_action == SIG_IGN || (p->p_sigmask & mask)) 1619 panic("postsig action"); 1620 #endif 1621 /* 1622 * Set the new mask value and also defer further 1623 * occurrences of this signal. 1624 * 1625 * Special case: user has done a sigpause. Here the 1626 * current mask is not of interest, but rather the 1627 * mask from before the sigpause is what we want 1628 * restored after the signal processing is completed. 1629 */ 1630 if (p->p_flag & P_SIGSUSPEND) { 1631 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1632 returnmask = p->p_oldmask; 1633 } else { 1634 returnmask = p->p_sigmask; 1635 } 1636 if (p->p_sisig == signum) { 1637 p->p_sisig = 0; 1638 p->p_sitrapno = 0; 1639 p->p_sicode = SI_USER; 1640 p->p_sigval.sival_ptr = NULL; 1641 } 1642 1643 if (sendsig(sctx->sig_action, signum, returnmask, &si, 1644 sctx->sig_info, sctx->sig_onstack)) { 1645 KERNEL_LOCK(); 1646 sigexit(p, SIGILL); 1647 /* NOTREACHED */ 1648 } 1649 postsig_done(p, signum, sctx->sig_catchmask, sctx->sig_reset); 1650 } 1651 } 1652 1653 /* 1654 * Force the current process to exit with the specified signal, dumping core 1655 * if appropriate. We bypass the normal tests for masked and caught signals, 1656 * allowing unrecoverable failures to terminate the process without changing 1657 * signal state. Mark the accounting record with the signal termination. 1658 * If dumping core, save the signal number for the debugger. Calls exit and 1659 * does not return. 1660 */ 1661 void 1662 sigexit(struct proc *p, int signum) 1663 { 1664 /* Mark process as going away */ 1665 atomic_setbits_int(&p->p_flag, P_WEXIT); 1666 1667 p->p_p->ps_acflag |= AXSIG; 1668 if (sigprop[signum] & SA_CORE) { 1669 p->p_sisig = signum; 1670 1671 /* if there are other threads, pause them */ 1672 if (P_HASSIBLING(p)) 1673 single_thread_set(p, SINGLE_UNWIND); 1674 1675 if (coredump(p) == 0) 1676 signum |= WCOREFLAG; 1677 } 1678 exit1(p, 0, signum, EXIT_NORMAL); 1679 /* NOTREACHED */ 1680 } 1681 1682 /* 1683 * Send uncatchable SIGABRT for coredump. 1684 */ 1685 void 1686 sigabort(struct proc *p) 1687 { 1688 struct sigaction sa; 1689 1690 KASSERT(p == curproc || panicstr || db_active); 1691 1692 memset(&sa, 0, sizeof sa); 1693 sa.sa_handler = SIG_DFL; 1694 setsigvec(p, SIGABRT, &sa); 1695 CLR(p->p_sigmask, sigmask(SIGABRT)); 1696 psignal(p, SIGABRT); 1697 } 1698 1699 /* 1700 * Return 1 if `sig', a given signal, is ignored or masked for `p', a given 1701 * thread, and 0 otherwise. 1702 */ 1703 int 1704 sigismasked(struct proc *p, int sig) 1705 { 1706 struct process *pr = p->p_p; 1707 int rv; 1708 1709 KASSERT(p == curproc); 1710 1711 mtx_enter(&pr->ps_mtx); 1712 rv = (pr->ps_sigacts->ps_sigignore & sigmask(sig)) || 1713 (p->p_sigmask & sigmask(sig)); 1714 mtx_leave(&pr->ps_mtx); 1715 1716 return !!rv; 1717 } 1718 1719 struct coredump_iostate { 1720 struct proc *io_proc; 1721 struct vnode *io_vp; 1722 struct ucred *io_cred; 1723 off_t io_offset; 1724 }; 1725 1726 /* 1727 * Dump core, into a file named "progname.core", unless the process was 1728 * setuid/setgid. 1729 */ 1730 int 1731 coredump(struct proc *p) 1732 { 1733 #ifdef SMALL_KERNEL 1734 return EPERM; 1735 #else 1736 struct process *pr = p->p_p; 1737 struct vnode *vp; 1738 struct ucred *cred = p->p_ucred; 1739 struct vmspace *vm = p->p_vmspace; 1740 struct nameidata nd; 1741 struct vattr vattr; 1742 struct coredump_iostate io; 1743 int error, len, incrash = 0; 1744 char *name; 1745 const char *dir = "/var/crash"; 1746 1747 atomic_setbits_int(&pr->ps_flags, PS_COREDUMP); 1748 1749 #ifdef PMAP_CHECK_COPYIN 1750 /* disable copyin checks, so we can write out text sections if needed */ 1751 p->p_vmspace->vm_map.check_copyin_count = 0; 1752 #endif 1753 1754 /* Don't dump if will exceed file size limit. */ 1755 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE)) 1756 return (EFBIG); 1757 1758 name = pool_get(&namei_pool, PR_WAITOK); 1759 1760 /* 1761 * If the process has inconsistent uids, nosuidcoredump 1762 * determines coredump placement policy. 1763 */ 1764 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1765 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1766 if (nosuidcoredump == 3) { 1767 /* 1768 * If the program directory does not exist, dumps of 1769 * that core will silently fail. 1770 */ 1771 len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core", 1772 dir, pr->ps_comm, pr->ps_pid); 1773 incrash = KERNELPATH; 1774 } else if (nosuidcoredump == 2) { 1775 len = snprintf(name, MAXPATHLEN, "%s/%s.core", 1776 dir, pr->ps_comm); 1777 incrash = KERNELPATH; 1778 } else { 1779 pool_put(&namei_pool, name); 1780 return (EPERM); 1781 } 1782 } else 1783 len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm); 1784 1785 if (len >= MAXPATHLEN) { 1786 pool_put(&namei_pool, name); 1787 return (EACCES); 1788 } 1789 1790 /* 1791 * Control the UID used to write out. The normal case uses 1792 * the real UID. If the sugid case is going to write into the 1793 * controlled directory, we do so as root. 1794 */ 1795 if (incrash == 0) { 1796 cred = crdup(cred); 1797 cred->cr_uid = cred->cr_ruid; 1798 cred->cr_gid = cred->cr_rgid; 1799 } else { 1800 if (p->p_fd->fd_rdir) { 1801 vrele(p->p_fd->fd_rdir); 1802 p->p_fd->fd_rdir = NULL; 1803 } 1804 p->p_ucred = crdup(p->p_ucred); 1805 crfree(cred); 1806 cred = p->p_ucred; 1807 crhold(cred); 1808 cred->cr_uid = 0; 1809 cred->cr_gid = 0; 1810 } 1811 1812 /* incrash should be 0 or KERNELPATH only */ 1813 NDINIT(&nd, 0, BYPASSUNVEIL | incrash, UIO_SYSSPACE, name, p); 1814 1815 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK, 1816 S_IRUSR | S_IWUSR); 1817 1818 if (error) 1819 goto out; 1820 1821 /* 1822 * Don't dump to non-regular files, files with links, or files 1823 * owned by someone else. 1824 */ 1825 vp = nd.ni_vp; 1826 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1827 VOP_UNLOCK(vp); 1828 vn_close(vp, FWRITE, cred, p); 1829 goto out; 1830 } 1831 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1832 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1833 vattr.va_uid != cred->cr_uid) { 1834 error = EACCES; 1835 VOP_UNLOCK(vp); 1836 vn_close(vp, FWRITE, cred, p); 1837 goto out; 1838 } 1839 vattr_null(&vattr); 1840 vattr.va_size = 0; 1841 VOP_SETATTR(vp, &vattr, cred, p); 1842 pr->ps_acflag |= ACORE; 1843 1844 io.io_proc = p; 1845 io.io_vp = vp; 1846 io.io_cred = cred; 1847 io.io_offset = 0; 1848 VOP_UNLOCK(vp); 1849 vref(vp); 1850 error = vn_close(vp, FWRITE, cred, p); 1851 if (error == 0) 1852 error = coredump_elf(p, &io); 1853 vrele(vp); 1854 out: 1855 crfree(cred); 1856 pool_put(&namei_pool, name); 1857 return (error); 1858 #endif 1859 } 1860 1861 #ifndef SMALL_KERNEL 1862 int 1863 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len, 1864 int isvnode) 1865 { 1866 struct coredump_iostate *io = cookie; 1867 off_t coffset = 0; 1868 size_t csize; 1869 int chunk, error; 1870 1871 csize = len; 1872 do { 1873 if (sigmask(SIGKILL) & 1874 (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist)) 1875 return (EINTR); 1876 1877 /* Rest of the loop sleeps with lock held, so... */ 1878 yield(); 1879 1880 chunk = MIN(csize, MAXPHYS); 1881 error = vn_rdwr(UIO_WRITE, io->io_vp, 1882 (caddr_t)data + coffset, chunk, 1883 io->io_offset + coffset, segflg, 1884 IO_UNIT, io->io_cred, NULL, io->io_proc); 1885 if (error && (error != EFAULT || !isvnode)) { 1886 struct process *pr = io->io_proc->p_p; 1887 1888 if (error == ENOSPC) 1889 log(LOG_ERR, 1890 "coredump of %s(%d) failed, filesystem full\n", 1891 pr->ps_comm, pr->ps_pid); 1892 else 1893 log(LOG_ERR, 1894 "coredump of %s(%d), write failed: errno %d\n", 1895 pr->ps_comm, pr->ps_pid, error); 1896 return (error); 1897 } 1898 1899 coffset += chunk; 1900 csize -= chunk; 1901 } while (csize > 0); 1902 1903 io->io_offset += len; 1904 return (0); 1905 } 1906 1907 void 1908 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1909 { 1910 struct coredump_iostate *io = cookie; 1911 1912 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1913 } 1914 1915 #endif /* !SMALL_KERNEL */ 1916 1917 /* 1918 * Nonexistent system call-- signal process (may want to handle it). 1919 * Flag error in case process won't see signal immediately (blocked or ignored). 1920 */ 1921 int 1922 sys_nosys(struct proc *p, void *v, register_t *retval) 1923 { 1924 ptsignal(p, SIGSYS, STHREAD); 1925 return (ENOSYS); 1926 } 1927 1928 int 1929 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1930 { 1931 struct sys___thrsigdivert_args /* { 1932 syscallarg(sigset_t) sigmask; 1933 syscallarg(siginfo_t *) info; 1934 syscallarg(const struct timespec *) timeout; 1935 } */ *uap = v; 1936 struct sigctx ctx; 1937 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1938 siginfo_t si; 1939 uint64_t nsecs = INFSLP; 1940 int timeinvalid = 0; 1941 int error = 0; 1942 1943 memset(&si, 0, sizeof(si)); 1944 1945 if (SCARG(uap, timeout) != NULL) { 1946 struct timespec ts; 1947 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1948 return (error); 1949 #ifdef KTRACE 1950 if (KTRPOINT(p, KTR_STRUCT)) 1951 ktrreltimespec(p, &ts); 1952 #endif 1953 if (!timespecisvalid(&ts)) 1954 timeinvalid = 1; 1955 else 1956 nsecs = TIMESPEC_TO_NSEC(&ts); 1957 } 1958 1959 dosigsuspend(p, p->p_sigmask &~ mask); 1960 for (;;) { 1961 si.si_signo = cursig(p, &ctx, 0); 1962 if (si.si_signo != 0) { 1963 sigset_t smask = sigmask(si.si_signo); 1964 if (smask & mask) { 1965 atomic_clearbits_int(&p->p_siglist, smask); 1966 error = 0; 1967 break; 1968 } 1969 } 1970 1971 /* per-POSIX, delay this error until after the above */ 1972 if (timeinvalid) 1973 error = EINVAL; 1974 /* per-POSIX, return immediately if timeout is zero-valued */ 1975 if (nsecs == 0) 1976 error = EAGAIN; 1977 1978 if (error != 0) 1979 break; 1980 1981 error = tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigwait", nsecs); 1982 } 1983 1984 if (error == 0) { 1985 *retval = si.si_signo; 1986 if (SCARG(uap, info) != NULL) { 1987 error = copyout(&si, SCARG(uap, info), sizeof(si)); 1988 #ifdef KTRACE 1989 if (error == 0 && KTRPOINT(p, KTR_STRUCT)) 1990 ktrsiginfo(p, &si); 1991 #endif 1992 } 1993 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) { 1994 /* 1995 * Restarting is wrong if there's a timeout, as it'll be 1996 * for the same interval again 1997 */ 1998 error = EINTR; 1999 } 2000 2001 return (error); 2002 } 2003 2004 void 2005 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val) 2006 { 2007 memset(si, 0, sizeof(*si)); 2008 2009 si->si_signo = sig; 2010 si->si_code = code; 2011 if (code == SI_USER) { 2012 si->si_value = val; 2013 } else { 2014 switch (sig) { 2015 case SIGSEGV: 2016 case SIGILL: 2017 case SIGBUS: 2018 case SIGFPE: 2019 si->si_addr = val.sival_ptr; 2020 si->si_trapno = trapno; 2021 break; 2022 case SIGXFSZ: 2023 break; 2024 } 2025 } 2026 } 2027 2028 void 2029 userret(struct proc *p) 2030 { 2031 struct sigctx ctx; 2032 int signum; 2033 2034 if (p->p_flag & P_SUSPSINGLE) 2035 single_thread_check(p, 0); 2036 2037 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */ 2038 if (p->p_flag & P_PROFPEND) { 2039 atomic_clearbits_int(&p->p_flag, P_PROFPEND); 2040 psignal(p, SIGPROF); 2041 } 2042 if (p->p_flag & P_ALRMPEND) { 2043 atomic_clearbits_int(&p->p_flag, P_ALRMPEND); 2044 psignal(p, SIGVTALRM); 2045 } 2046 2047 if (SIGPENDING(p) != 0) { 2048 while ((signum = cursig(p, &ctx, 0)) != 0) 2049 postsig(p, signum, &ctx); 2050 } 2051 2052 /* 2053 * If P_SIGSUSPEND is still set here, then we still need to restore 2054 * the original sigmask before returning to userspace. Also, this 2055 * might unmask some pending signals, so we need to check a second 2056 * time for signals to post. 2057 */ 2058 if (p->p_flag & P_SIGSUSPEND) { 2059 p->p_sigmask = p->p_oldmask; 2060 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 2061 2062 while ((signum = cursig(p, &ctx, 0)) != 0) 2063 postsig(p, signum, &ctx); 2064 } 2065 2066 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning"); 2067 2068 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri; 2069 } 2070 2071 int 2072 single_thread_check_locked(struct proc *p, int deep) 2073 { 2074 struct process *pr = p->p_p; 2075 2076 MUTEX_ASSERT_LOCKED(&pr->ps_mtx); 2077 2078 if (pr->ps_single == NULL || pr->ps_single == p) 2079 return (0); 2080 2081 do { 2082 /* if we're in deep, we need to unwind to the edge */ 2083 if (deep) { 2084 if (pr->ps_flags & PS_SINGLEUNWIND) 2085 return (ERESTART); 2086 if (pr->ps_flags & PS_SINGLEEXIT) 2087 return (EINTR); 2088 } 2089 2090 if (pr->ps_flags & PS_SINGLEEXIT) { 2091 mtx_leave(&pr->ps_mtx); 2092 KERNEL_LOCK(); 2093 exit1(p, 0, 0, EXIT_THREAD_NOCHECK); 2094 /* NOTREACHED */ 2095 } 2096 2097 if (--pr->ps_singlecnt == 0) 2098 wakeup(&pr->ps_singlecnt); 2099 2100 /* not exiting and don't need to unwind, so suspend */ 2101 mtx_leave(&pr->ps_mtx); 2102 2103 SCHED_LOCK(); 2104 p->p_stat = SSTOP; 2105 mi_switch(); 2106 SCHED_UNLOCK(); 2107 mtx_enter(&pr->ps_mtx); 2108 } while (pr->ps_single != NULL); 2109 2110 return (0); 2111 } 2112 2113 int 2114 single_thread_check(struct proc *p, int deep) 2115 { 2116 int error; 2117 2118 mtx_enter(&p->p_p->ps_mtx); 2119 error = single_thread_check_locked(p, deep); 2120 mtx_leave(&p->p_p->ps_mtx); 2121 2122 return error; 2123 } 2124 2125 /* 2126 * Stop other threads in the process. The mode controls how and 2127 * where the other threads should stop: 2128 * - SINGLE_SUSPEND: stop wherever they are, will later be released (via 2129 * single_thread_clear()) 2130 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit 2131 * (by setting to SINGLE_EXIT) or released as with SINGLE_SUSPEND 2132 * - SINGLE_EXIT: unwind to kernel boundary and exit 2133 */ 2134 int 2135 single_thread_set(struct proc *p, int flags) 2136 { 2137 struct process *pr = p->p_p; 2138 struct proc *q; 2139 int error, mode = flags & SINGLE_MASK; 2140 2141 KASSERT(curproc == p); 2142 2143 mtx_enter(&pr->ps_mtx); 2144 error = single_thread_check_locked(p, flags & SINGLE_DEEP); 2145 if (error) { 2146 mtx_leave(&pr->ps_mtx); 2147 return error; 2148 } 2149 2150 switch (mode) { 2151 case SINGLE_SUSPEND: 2152 break; 2153 case SINGLE_UNWIND: 2154 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2155 break; 2156 case SINGLE_EXIT: 2157 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT); 2158 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2159 break; 2160 #ifdef DIAGNOSTIC 2161 default: 2162 panic("single_thread_mode = %d", mode); 2163 #endif 2164 } 2165 KASSERT((p->p_flag & P_SUSPSINGLE) == 0); 2166 pr->ps_single = p; 2167 pr->ps_singlecnt = pr->ps_threadcnt; 2168 2169 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2170 if (q == p) 2171 continue; 2172 SCHED_LOCK(); 2173 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE); 2174 switch (q->p_stat) { 2175 case SSTOP: 2176 if (mode == SINGLE_EXIT) { 2177 unsleep(q); 2178 setrunnable(q); 2179 } else 2180 --pr->ps_singlecnt; 2181 break; 2182 case SSLEEP: 2183 /* if it's not interruptible, then just have to wait */ 2184 if (q->p_flag & P_SINTR) { 2185 /* merely need to suspend? just stop it */ 2186 if (mode == SINGLE_SUSPEND) { 2187 q->p_stat = SSTOP; 2188 --pr->ps_singlecnt; 2189 break; 2190 } 2191 /* need to unwind or exit, so wake it */ 2192 unsleep(q); 2193 setrunnable(q); 2194 } 2195 break; 2196 case SONPROC: 2197 signotify(q); 2198 break; 2199 case SRUN: 2200 case SIDL: 2201 case SDEAD: 2202 break; 2203 } 2204 SCHED_UNLOCK(); 2205 } 2206 2207 /* count ourself out */ 2208 --pr->ps_singlecnt; 2209 mtx_leave(&pr->ps_mtx); 2210 2211 if ((flags & SINGLE_NOWAIT) == 0) 2212 single_thread_wait(pr, 1); 2213 2214 return 0; 2215 } 2216 2217 /* 2218 * Wait for other threads to stop. If recheck is false then the function 2219 * returns non-zero if the caller needs to restart the check else 0 is 2220 * returned. If recheck is true the return value is always 0. 2221 */ 2222 int 2223 single_thread_wait(struct process *pr, int recheck) 2224 { 2225 int wait; 2226 2227 /* wait until they're all suspended */ 2228 mtx_enter(&pr->ps_mtx); 2229 while ((wait = pr->ps_singlecnt > 0)) { 2230 msleep_nsec(&pr->ps_singlecnt, &pr->ps_mtx, PWAIT, "suspend", 2231 INFSLP); 2232 if (!recheck) 2233 break; 2234 } 2235 KASSERT((pr->ps_single->p_flag & P_SUSPSINGLE) == 0); 2236 mtx_leave(&pr->ps_mtx); 2237 2238 return wait; 2239 } 2240 2241 void 2242 single_thread_clear(struct proc *p, int flag) 2243 { 2244 struct process *pr = p->p_p; 2245 struct proc *q; 2246 2247 KASSERT(pr->ps_single == p); 2248 KASSERT(curproc == p); 2249 2250 mtx_enter(&pr->ps_mtx); 2251 pr->ps_single = NULL; 2252 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); 2253 2254 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2255 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0) 2256 continue; 2257 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE); 2258 2259 /* 2260 * if the thread was only stopped for single threading 2261 * then clearing that either makes it runnable or puts 2262 * it back into some sleep queue 2263 */ 2264 SCHED_LOCK(); 2265 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) { 2266 if (q->p_wchan == NULL) 2267 setrunnable(q); 2268 else { 2269 atomic_clearbits_int(&q->p_flag, P_WSLEEP); 2270 q->p_stat = SSLEEP; 2271 } 2272 } 2273 SCHED_UNLOCK(); 2274 } 2275 mtx_leave(&pr->ps_mtx); 2276 } 2277 2278 void 2279 sigio_del(struct sigiolst *rmlist) 2280 { 2281 struct sigio *sigio; 2282 2283 while ((sigio = LIST_FIRST(rmlist)) != NULL) { 2284 LIST_REMOVE(sigio, sio_pgsigio); 2285 crfree(sigio->sio_ucred); 2286 free(sigio, M_SIGIO, sizeof(*sigio)); 2287 } 2288 } 2289 2290 void 2291 sigio_unlink(struct sigio_ref *sir, struct sigiolst *rmlist) 2292 { 2293 struct sigio *sigio; 2294 2295 MUTEX_ASSERT_LOCKED(&sigio_lock); 2296 2297 sigio = sir->sir_sigio; 2298 if (sigio != NULL) { 2299 KASSERT(sigio->sio_myref == sir); 2300 sir->sir_sigio = NULL; 2301 2302 if (sigio->sio_pgid > 0) 2303 sigio->sio_proc = NULL; 2304 else 2305 sigio->sio_pgrp = NULL; 2306 LIST_REMOVE(sigio, sio_pgsigio); 2307 2308 LIST_INSERT_HEAD(rmlist, sigio, sio_pgsigio); 2309 } 2310 } 2311 2312 void 2313 sigio_free(struct sigio_ref *sir) 2314 { 2315 struct sigiolst rmlist; 2316 2317 if (sir->sir_sigio == NULL) 2318 return; 2319 2320 LIST_INIT(&rmlist); 2321 2322 mtx_enter(&sigio_lock); 2323 sigio_unlink(sir, &rmlist); 2324 mtx_leave(&sigio_lock); 2325 2326 sigio_del(&rmlist); 2327 } 2328 2329 void 2330 sigio_freelist(struct sigiolst *sigiolst) 2331 { 2332 struct sigiolst rmlist; 2333 struct sigio *sigio; 2334 2335 if (LIST_EMPTY(sigiolst)) 2336 return; 2337 2338 LIST_INIT(&rmlist); 2339 2340 mtx_enter(&sigio_lock); 2341 while ((sigio = LIST_FIRST(sigiolst)) != NULL) 2342 sigio_unlink(sigio->sio_myref, &rmlist); 2343 mtx_leave(&sigio_lock); 2344 2345 sigio_del(&rmlist); 2346 } 2347 2348 int 2349 sigio_setown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2350 { 2351 struct sigiolst rmlist; 2352 struct proc *p = curproc; 2353 struct pgrp *pgrp = NULL; 2354 struct process *pr = NULL; 2355 struct sigio *sigio; 2356 int error; 2357 pid_t pgid = *(int *)data; 2358 2359 if (pgid == 0) { 2360 sigio_free(sir); 2361 return (0); 2362 } 2363 2364 if (cmd == TIOCSPGRP) { 2365 if (pgid < 0) 2366 return (EINVAL); 2367 pgid = -pgid; 2368 } 2369 2370 sigio = malloc(sizeof(*sigio), M_SIGIO, M_WAITOK); 2371 sigio->sio_pgid = pgid; 2372 sigio->sio_ucred = crhold(p->p_ucred); 2373 sigio->sio_myref = sir; 2374 2375 LIST_INIT(&rmlist); 2376 2377 /* 2378 * The kernel lock, and not sleeping between prfind()/pgfind() and 2379 * linking of the sigio ensure that the process or process group does 2380 * not disappear unexpectedly. 2381 */ 2382 KERNEL_LOCK(); 2383 mtx_enter(&sigio_lock); 2384 2385 if (pgid > 0) { 2386 pr = prfind(pgid); 2387 if (pr == NULL) { 2388 error = ESRCH; 2389 goto fail; 2390 } 2391 2392 /* 2393 * Policy - Don't allow a process to FSETOWN a process 2394 * in another session. 2395 * 2396 * Remove this test to allow maximum flexibility or 2397 * restrict FSETOWN to the current process or process 2398 * group for maximum safety. 2399 */ 2400 if (pr->ps_session != p->p_p->ps_session) { 2401 error = EPERM; 2402 goto fail; 2403 } 2404 2405 if ((pr->ps_flags & PS_EXITING) != 0) { 2406 error = ESRCH; 2407 goto fail; 2408 } 2409 } else /* if (pgid < 0) */ { 2410 pgrp = pgfind(-pgid); 2411 if (pgrp == NULL) { 2412 error = ESRCH; 2413 goto fail; 2414 } 2415 2416 /* 2417 * Policy - Don't allow a process to FSETOWN a process 2418 * in another session. 2419 * 2420 * Remove this test to allow maximum flexibility or 2421 * restrict FSETOWN to the current process or process 2422 * group for maximum safety. 2423 */ 2424 if (pgrp->pg_session != p->p_p->ps_session) { 2425 error = EPERM; 2426 goto fail; 2427 } 2428 } 2429 2430 if (pgid > 0) { 2431 sigio->sio_proc = pr; 2432 LIST_INSERT_HEAD(&pr->ps_sigiolst, sigio, sio_pgsigio); 2433 } else { 2434 sigio->sio_pgrp = pgrp; 2435 LIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); 2436 } 2437 2438 sigio_unlink(sir, &rmlist); 2439 sir->sir_sigio = sigio; 2440 2441 mtx_leave(&sigio_lock); 2442 KERNEL_UNLOCK(); 2443 2444 sigio_del(&rmlist); 2445 2446 return (0); 2447 2448 fail: 2449 mtx_leave(&sigio_lock); 2450 KERNEL_UNLOCK(); 2451 2452 crfree(sigio->sio_ucred); 2453 free(sigio, M_SIGIO, sizeof(*sigio)); 2454 2455 return (error); 2456 } 2457 2458 void 2459 sigio_getown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2460 { 2461 struct sigio *sigio; 2462 pid_t pgid = 0; 2463 2464 mtx_enter(&sigio_lock); 2465 sigio = sir->sir_sigio; 2466 if (sigio != NULL) 2467 pgid = sigio->sio_pgid; 2468 mtx_leave(&sigio_lock); 2469 2470 if (cmd == TIOCGPGRP) 2471 pgid = -pgid; 2472 2473 *(int *)data = pgid; 2474 } 2475 2476 void 2477 sigio_copy(struct sigio_ref *dst, struct sigio_ref *src) 2478 { 2479 struct sigiolst rmlist; 2480 struct sigio *newsigio, *sigio; 2481 2482 sigio_free(dst); 2483 2484 if (src->sir_sigio == NULL) 2485 return; 2486 2487 newsigio = malloc(sizeof(*newsigio), M_SIGIO, M_WAITOK); 2488 LIST_INIT(&rmlist); 2489 2490 mtx_enter(&sigio_lock); 2491 2492 sigio = src->sir_sigio; 2493 if (sigio == NULL) { 2494 mtx_leave(&sigio_lock); 2495 free(newsigio, M_SIGIO, sizeof(*newsigio)); 2496 return; 2497 } 2498 2499 newsigio->sio_pgid = sigio->sio_pgid; 2500 newsigio->sio_ucred = crhold(sigio->sio_ucred); 2501 newsigio->sio_myref = dst; 2502 if (newsigio->sio_pgid > 0) { 2503 newsigio->sio_proc = sigio->sio_proc; 2504 LIST_INSERT_HEAD(&newsigio->sio_proc->ps_sigiolst, newsigio, 2505 sio_pgsigio); 2506 } else { 2507 newsigio->sio_pgrp = sigio->sio_pgrp; 2508 LIST_INSERT_HEAD(&newsigio->sio_pgrp->pg_sigiolst, newsigio, 2509 sio_pgsigio); 2510 } 2511 2512 sigio_unlink(dst, &rmlist); 2513 dst->sir_sigio = newsigio; 2514 2515 mtx_leave(&sigio_lock); 2516 2517 sigio_del(&rmlist); 2518 } 2519