1 /* $OpenBSD: kern_sig.c,v 1.222 2018/07/11 19:28:16 bluhm 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 #define SIGPROP /* include signal properties table */ 42 #include <sys/param.h> 43 #include <sys/signalvar.h> 44 #include <sys/resourcevar.h> 45 #include <sys/queue.h> 46 #include <sys/namei.h> 47 #include <sys/vnode.h> 48 #include <sys/event.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/acct.h> 52 #include <sys/fcntl.h> 53 #include <sys/filedesc.h> 54 #include <sys/kernel.h> 55 #include <sys/wait.h> 56 #include <sys/ktrace.h> 57 #include <sys/stat.h> 58 #include <sys/core.h> 59 #include <sys/malloc.h> 60 #include <sys/pool.h> 61 #include <sys/ptrace.h> 62 #include <sys/sched.h> 63 #include <sys/user.h> 64 #include <sys/syslog.h> 65 #include <sys/pledge.h> 66 #include <sys/witness.h> 67 68 #include <sys/mount.h> 69 #include <sys/syscallargs.h> 70 71 #include <uvm/uvm_extern.h> 72 #include <machine/tcb.h> 73 74 int filt_sigattach(struct knote *kn); 75 void filt_sigdetach(struct knote *kn); 76 int filt_signal(struct knote *kn, long hint); 77 78 struct filterops sig_filtops = 79 { 0, filt_sigattach, filt_sigdetach, filt_signal }; 80 81 void proc_stop(struct proc *p, int); 82 void proc_stop_sweep(void *); 83 struct timeout proc_stop_to; 84 85 void postsig(struct proc *, int); 86 int cansignal(struct proc *, struct process *, int); 87 88 struct pool sigacts_pool; /* memory pool for sigacts structures */ 89 90 /* 91 * Can thread p, send the signal signum to process qr? 92 */ 93 int 94 cansignal(struct proc *p, struct process *qr, int signum) 95 { 96 struct process *pr = p->p_p; 97 struct ucred *uc = p->p_ucred; 98 struct ucred *quc = qr->ps_ucred; 99 100 if (uc->cr_uid == 0) 101 return (1); /* root can always signal */ 102 103 if (pr == qr) 104 return (1); /* process can always signal itself */ 105 106 /* optimization: if the same creds then the tests below will pass */ 107 if (uc == quc) 108 return (1); 109 110 if (signum == SIGCONT && qr->ps_session == pr->ps_session) 111 return (1); /* SIGCONT in session */ 112 113 /* 114 * Using kill(), only certain signals can be sent to setugid 115 * child processes 116 */ 117 if (qr->ps_flags & PS_SUGID) { 118 switch (signum) { 119 case 0: 120 case SIGKILL: 121 case SIGINT: 122 case SIGTERM: 123 case SIGALRM: 124 case SIGSTOP: 125 case SIGTTIN: 126 case SIGTTOU: 127 case SIGTSTP: 128 case SIGHUP: 129 case SIGUSR1: 130 case SIGUSR2: 131 if (uc->cr_ruid == quc->cr_ruid || 132 uc->cr_uid == quc->cr_ruid) 133 return (1); 134 } 135 return (0); 136 } 137 138 if (uc->cr_ruid == quc->cr_ruid || 139 uc->cr_ruid == quc->cr_svuid || 140 uc->cr_uid == quc->cr_ruid || 141 uc->cr_uid == quc->cr_svuid) 142 return (1); 143 return (0); 144 } 145 146 /* 147 * Initialize signal-related data structures. 148 */ 149 void 150 signal_init(void) 151 { 152 timeout_set(&proc_stop_to, proc_stop_sweep, NULL); 153 154 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE, 155 PR_WAITOK, "sigapl", NULL); 156 } 157 158 /* 159 * Create an initial sigacts structure, using the same signal state 160 * as p. 161 */ 162 struct sigacts * 163 sigactsinit(struct process *pr) 164 { 165 struct sigacts *ps; 166 167 ps = pool_get(&sigacts_pool, PR_WAITOK); 168 memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts)); 169 ps->ps_refcnt = 1; 170 return (ps); 171 } 172 173 /* 174 * Share a sigacts structure. 175 */ 176 struct sigacts * 177 sigactsshare(struct process *pr) 178 { 179 struct sigacts *ps = pr->ps_sigacts; 180 181 ps->ps_refcnt++; 182 return ps; 183 } 184 185 /* 186 * Initialize a new sigaltstack structure. 187 */ 188 void 189 sigstkinit(struct sigaltstack *ss) 190 { 191 ss->ss_flags = SS_DISABLE; 192 ss->ss_size = 0; 193 ss->ss_sp = 0; 194 } 195 196 /* 197 * Make this process not share its sigacts, maintaining all 198 * signal state. 199 */ 200 void 201 sigactsunshare(struct process *pr) 202 { 203 struct sigacts *newps; 204 205 if (pr->ps_sigacts->ps_refcnt == 1) 206 return; 207 208 newps = sigactsinit(pr); 209 sigactsfree(pr); 210 pr->ps_sigacts = newps; 211 } 212 213 /* 214 * Release a sigacts structure. 215 */ 216 void 217 sigactsfree(struct process *pr) 218 { 219 struct sigacts *ps = pr->ps_sigacts; 220 221 if (--ps->ps_refcnt > 0) 222 return; 223 224 pr->ps_sigacts = NULL; 225 226 pool_put(&sigacts_pool, ps); 227 } 228 229 int 230 sys_sigaction(struct proc *p, void *v, register_t *retval) 231 { 232 struct sys_sigaction_args /* { 233 syscallarg(int) signum; 234 syscallarg(const struct sigaction *) nsa; 235 syscallarg(struct sigaction *) osa; 236 } */ *uap = v; 237 struct sigaction vec; 238 #ifdef KTRACE 239 struct sigaction ovec; 240 #endif 241 struct sigaction *sa; 242 const struct sigaction *nsa; 243 struct sigaction *osa; 244 struct sigacts *ps = p->p_p->ps_sigacts; 245 int signum; 246 int bit, error; 247 248 signum = SCARG(uap, signum); 249 nsa = SCARG(uap, nsa); 250 osa = SCARG(uap, osa); 251 252 if (signum <= 0 || signum >= NSIG || 253 (nsa && (signum == SIGKILL || signum == SIGSTOP))) 254 return (EINVAL); 255 sa = &vec; 256 if (osa) { 257 sa->sa_handler = ps->ps_sigact[signum]; 258 sa->sa_mask = ps->ps_catchmask[signum]; 259 bit = sigmask(signum); 260 sa->sa_flags = 0; 261 if ((ps->ps_sigonstack & bit) != 0) 262 sa->sa_flags |= SA_ONSTACK; 263 if ((ps->ps_sigintr & bit) == 0) 264 sa->sa_flags |= SA_RESTART; 265 if ((ps->ps_sigreset & bit) != 0) 266 sa->sa_flags |= SA_RESETHAND; 267 if ((ps->ps_siginfo & bit) != 0) 268 sa->sa_flags |= SA_SIGINFO; 269 if (signum == SIGCHLD) { 270 if ((ps->ps_flags & SAS_NOCLDSTOP) != 0) 271 sa->sa_flags |= SA_NOCLDSTOP; 272 if ((ps->ps_flags & SAS_NOCLDWAIT) != 0) 273 sa->sa_flags |= SA_NOCLDWAIT; 274 } 275 if ((sa->sa_mask & bit) == 0) 276 sa->sa_flags |= SA_NODEFER; 277 sa->sa_mask &= ~bit; 278 error = copyout(sa, osa, sizeof (vec)); 279 if (error) 280 return (error); 281 #ifdef KTRACE 282 if (KTRPOINT(p, KTR_STRUCT)) 283 ovec = vec; 284 #endif 285 } 286 if (nsa) { 287 error = copyin(nsa, sa, sizeof (vec)); 288 if (error) 289 return (error); 290 #ifdef KTRACE 291 if (KTRPOINT(p, KTR_STRUCT)) 292 ktrsigaction(p, sa); 293 #endif 294 setsigvec(p, signum, sa); 295 } 296 #ifdef KTRACE 297 if (osa && KTRPOINT(p, KTR_STRUCT)) 298 ktrsigaction(p, &ovec); 299 #endif 300 return (0); 301 } 302 303 void 304 setsigvec(struct proc *p, int signum, struct sigaction *sa) 305 { 306 struct sigacts *ps = p->p_p->ps_sigacts; 307 int bit; 308 int s; 309 310 bit = sigmask(signum); 311 /* 312 * Change setting atomically. 313 */ 314 s = splhigh(); 315 ps->ps_sigact[signum] = sa->sa_handler; 316 if ((sa->sa_flags & SA_NODEFER) == 0) 317 sa->sa_mask |= sigmask(signum); 318 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; 319 if (signum == SIGCHLD) { 320 if (sa->sa_flags & SA_NOCLDSTOP) 321 atomic_setbits_int(&ps->ps_flags, SAS_NOCLDSTOP); 322 else 323 atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDSTOP); 324 /* 325 * If the SA_NOCLDWAIT flag is set or the handler 326 * is SIG_IGN we reparent the dying child to PID 1 327 * (init) which will reap the zombie. Because we use 328 * init to do our dirty work we never set SAS_NOCLDWAIT 329 * for PID 1. 330 * XXX exit1 rework means this is unnecessary? 331 */ 332 if (initprocess->ps_sigacts != ps && 333 ((sa->sa_flags & SA_NOCLDWAIT) || 334 sa->sa_handler == SIG_IGN)) 335 atomic_setbits_int(&ps->ps_flags, SAS_NOCLDWAIT); 336 else 337 atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDWAIT); 338 } 339 if ((sa->sa_flags & SA_RESETHAND) != 0) 340 ps->ps_sigreset |= bit; 341 else 342 ps->ps_sigreset &= ~bit; 343 if ((sa->sa_flags & SA_SIGINFO) != 0) 344 ps->ps_siginfo |= bit; 345 else 346 ps->ps_siginfo &= ~bit; 347 if ((sa->sa_flags & SA_RESTART) == 0) 348 ps->ps_sigintr |= bit; 349 else 350 ps->ps_sigintr &= ~bit; 351 if ((sa->sa_flags & SA_ONSTACK) != 0) 352 ps->ps_sigonstack |= bit; 353 else 354 ps->ps_sigonstack &= ~bit; 355 /* 356 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 357 * and for signals set to SIG_DFL where the default is to ignore. 358 * However, don't put SIGCONT in ps_sigignore, 359 * as we have to restart the process. 360 */ 361 if (sa->sa_handler == SIG_IGN || 362 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { 363 atomic_clearbits_int(&p->p_siglist, bit); 364 if (signum != SIGCONT) 365 ps->ps_sigignore |= bit; /* easier in psignal */ 366 ps->ps_sigcatch &= ~bit; 367 } else { 368 ps->ps_sigignore &= ~bit; 369 if (sa->sa_handler == SIG_DFL) 370 ps->ps_sigcatch &= ~bit; 371 else 372 ps->ps_sigcatch |= bit; 373 } 374 splx(s); 375 } 376 377 /* 378 * Initialize signal state for process 0; 379 * set to ignore signals that are ignored by default. 380 */ 381 void 382 siginit(struct process *pr) 383 { 384 struct sigacts *ps = pr->ps_sigacts; 385 int i; 386 387 for (i = 0; i < NSIG; i++) 388 if (sigprop[i] & SA_IGNORE && i != SIGCONT) 389 ps->ps_sigignore |= sigmask(i); 390 ps->ps_flags = SAS_NOCLDWAIT | SAS_NOCLDSTOP; 391 } 392 393 /* 394 * Reset signals for an exec by the specified thread. 395 */ 396 void 397 execsigs(struct proc *p) 398 { 399 struct sigacts *ps; 400 int nc, mask; 401 402 sigactsunshare(p->p_p); 403 ps = p->p_p->ps_sigacts; 404 405 /* 406 * Reset caught signals. Held signals remain held 407 * through p_sigmask (unless they were caught, 408 * and are now ignored by default). 409 */ 410 while (ps->ps_sigcatch) { 411 nc = ffs((long)ps->ps_sigcatch); 412 mask = sigmask(nc); 413 ps->ps_sigcatch &= ~mask; 414 if (sigprop[nc] & SA_IGNORE) { 415 if (nc != SIGCONT) 416 ps->ps_sigignore |= mask; 417 atomic_clearbits_int(&p->p_siglist, mask); 418 } 419 ps->ps_sigact[nc] = SIG_DFL; 420 } 421 /* 422 * Reset stack state to the user stack. 423 * Clear set of signals caught on the signal stack. 424 */ 425 sigstkinit(&p->p_sigstk); 426 ps->ps_flags &= ~SAS_NOCLDWAIT; 427 if (ps->ps_sigact[SIGCHLD] == SIG_IGN) 428 ps->ps_sigact[SIGCHLD] = SIG_DFL; 429 } 430 431 /* 432 * Manipulate signal mask. 433 * Note that we receive new mask, not pointer, 434 * and return old mask as return value; 435 * the library stub does the rest. 436 */ 437 int 438 sys_sigprocmask(struct proc *p, void *v, register_t *retval) 439 { 440 struct sys_sigprocmask_args /* { 441 syscallarg(int) how; 442 syscallarg(sigset_t) mask; 443 } */ *uap = v; 444 int error = 0; 445 sigset_t mask; 446 447 *retval = p->p_sigmask; 448 mask = SCARG(uap, mask) &~ sigcantmask; 449 450 switch (SCARG(uap, how)) { 451 case SIG_BLOCK: 452 atomic_setbits_int(&p->p_sigmask, mask); 453 break; 454 case SIG_UNBLOCK: 455 atomic_clearbits_int(&p->p_sigmask, mask); 456 break; 457 case SIG_SETMASK: 458 p->p_sigmask = mask; 459 break; 460 default: 461 error = EINVAL; 462 break; 463 } 464 return (error); 465 } 466 467 int 468 sys_sigpending(struct proc *p, void *v, register_t *retval) 469 { 470 471 *retval = p->p_siglist; 472 return (0); 473 } 474 475 /* 476 * Temporarily replace calling proc's signal mask for the duration of a 477 * system call. Original signal mask will be restored by userret(). 478 */ 479 void 480 dosigsuspend(struct proc *p, sigset_t newmask) 481 { 482 KASSERT(p == curproc); 483 484 p->p_oldmask = p->p_sigmask; 485 atomic_setbits_int(&p->p_flag, P_SIGSUSPEND); 486 p->p_sigmask = newmask; 487 } 488 489 /* 490 * Suspend process until signal, providing mask to be set 491 * in the meantime. Note nonstandard calling convention: 492 * libc stub passes mask, not pointer, to save a copyin. 493 */ 494 int 495 sys_sigsuspend(struct proc *p, void *v, register_t *retval) 496 { 497 struct sys_sigsuspend_args /* { 498 syscallarg(int) mask; 499 } */ *uap = v; 500 struct process *pr = p->p_p; 501 struct sigacts *ps = pr->ps_sigacts; 502 503 dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask); 504 while (tsleep(ps, PPAUSE|PCATCH, "pause", 0) == 0) 505 /* void */; 506 /* always return EINTR rather than ERESTART... */ 507 return (EINTR); 508 } 509 510 int 511 sigonstack(size_t stack) 512 { 513 const struct sigaltstack *ss = &curproc->p_sigstk; 514 515 return (ss->ss_flags & SS_DISABLE ? 0 : 516 (stack - (size_t)ss->ss_sp < ss->ss_size)); 517 } 518 519 int 520 sys_sigaltstack(struct proc *p, void *v, register_t *retval) 521 { 522 struct sys_sigaltstack_args /* { 523 syscallarg(const struct sigaltstack *) nss; 524 syscallarg(struct sigaltstack *) oss; 525 } */ *uap = v; 526 struct sigaltstack ss; 527 const struct sigaltstack *nss; 528 struct sigaltstack *oss; 529 int onstack = sigonstack(PROC_STACK(p)); 530 int error; 531 532 nss = SCARG(uap, nss); 533 oss = SCARG(uap, oss); 534 535 if (oss != NULL) { 536 ss = p->p_sigstk; 537 if (onstack) 538 ss.ss_flags |= SS_ONSTACK; 539 if ((error = copyout(&ss, oss, sizeof(ss)))) 540 return (error); 541 } 542 if (nss == NULL) 543 return (0); 544 error = copyin(nss, &ss, sizeof(ss)); 545 if (error) 546 return (error); 547 if (onstack) 548 return (EPERM); 549 if (ss.ss_flags & ~SS_DISABLE) 550 return (EINVAL); 551 if (ss.ss_flags & SS_DISABLE) { 552 p->p_sigstk.ss_flags = ss.ss_flags; 553 return (0); 554 } 555 if (ss.ss_size < MINSIGSTKSZ) 556 return (ENOMEM); 557 558 error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size); 559 if (error) 560 return (error); 561 562 p->p_sigstk = ss; 563 return (0); 564 } 565 566 int 567 sys_kill(struct proc *cp, void *v, register_t *retval) 568 { 569 struct sys_kill_args /* { 570 syscallarg(int) pid; 571 syscallarg(int) signum; 572 } */ *uap = v; 573 struct process *pr; 574 int pid = SCARG(uap, pid); 575 int signum = SCARG(uap, signum); 576 int error; 577 int zombie = 0; 578 579 if ((error = pledge_kill(cp, pid)) != 0) 580 return (error); 581 if (((u_int)signum) >= NSIG) 582 return (EINVAL); 583 if (pid > 0) { 584 if ((pr = prfind(pid)) == NULL) { 585 if ((pr = zombiefind(pid)) == NULL) 586 return (ESRCH); 587 else 588 zombie = 1; 589 } 590 if (!cansignal(cp, pr, signum)) 591 return (EPERM); 592 593 /* kill single process */ 594 if (signum && !zombie) 595 prsignal(pr, signum); 596 return (0); 597 } 598 switch (pid) { 599 case -1: /* broadcast signal */ 600 return (killpg1(cp, signum, 0, 1)); 601 case 0: /* signal own process group */ 602 return (killpg1(cp, signum, 0, 0)); 603 default: /* negative explicit process group */ 604 return (killpg1(cp, signum, -pid, 0)); 605 } 606 } 607 608 int 609 sys_thrkill(struct proc *cp, void *v, register_t *retval) 610 { 611 struct sys_thrkill_args /* { 612 syscallarg(pid_t) tid; 613 syscallarg(int) signum; 614 syscallarg(void *) tcb; 615 } */ *uap = v; 616 struct proc *p; 617 int tid = SCARG(uap, tid); 618 int signum = SCARG(uap, signum); 619 void *tcb; 620 621 if (((u_int)signum) >= NSIG) 622 return (EINVAL); 623 if (tid > THREAD_PID_OFFSET) { 624 if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL) 625 return (ESRCH); 626 627 /* can only kill threads in the same process */ 628 if (p->p_p != cp->p_p) 629 return (ESRCH); 630 } else if (tid == 0) 631 p = cp; 632 else 633 return (EINVAL); 634 635 /* optionally require the target thread to have the given tcb addr */ 636 tcb = SCARG(uap, tcb); 637 if (tcb != NULL && tcb != TCB_GET(p)) 638 return (ESRCH); 639 640 if (signum) 641 ptsignal(p, signum, STHREAD); 642 return (0); 643 } 644 645 /* 646 * Common code for kill process group/broadcast kill. 647 * cp is calling process. 648 */ 649 int 650 killpg1(struct proc *cp, int signum, int pgid, int all) 651 { 652 struct process *pr; 653 struct pgrp *pgrp; 654 int nfound = 0; 655 656 if (all) { 657 /* 658 * broadcast 659 */ 660 LIST_FOREACH(pr, &allprocess, ps_list) { 661 if (pr->ps_pid <= 1 || 662 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) || 663 pr == cp->p_p || !cansignal(cp, pr, signum)) 664 continue; 665 nfound++; 666 if (signum) 667 prsignal(pr, signum); 668 } 669 } else { 670 if (pgid == 0) 671 /* 672 * zero pgid means send to my process group. 673 */ 674 pgrp = cp->p_p->ps_pgrp; 675 else { 676 pgrp = pgfind(pgid); 677 if (pgrp == NULL) 678 return (ESRCH); 679 } 680 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 681 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM || 682 !cansignal(cp, pr, signum)) 683 continue; 684 nfound++; 685 if (signum) 686 prsignal(pr, signum); 687 } 688 } 689 return (nfound ? 0 : ESRCH); 690 } 691 692 #define CANDELIVER(uid, euid, pr) \ 693 (euid == 0 || \ 694 (uid) == (pr)->ps_ucred->cr_ruid || \ 695 (uid) == (pr)->ps_ucred->cr_svuid || \ 696 (uid) == (pr)->ps_ucred->cr_uid || \ 697 (euid) == (pr)->ps_ucred->cr_ruid || \ 698 (euid) == (pr)->ps_ucred->cr_svuid || \ 699 (euid) == (pr)->ps_ucred->cr_uid) 700 701 /* 702 * Deliver signum to pgid, but first check uid/euid against each 703 * process and see if it is permitted. 704 */ 705 void 706 csignal(pid_t pgid, int signum, uid_t uid, uid_t euid) 707 { 708 struct pgrp *pgrp; 709 struct process *pr; 710 711 if (pgid == 0) 712 return; 713 if (pgid < 0) { 714 pgid = -pgid; 715 if ((pgrp = pgfind(pgid)) == NULL) 716 return; 717 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 718 if (CANDELIVER(uid, euid, pr)) 719 prsignal(pr, signum); 720 } else { 721 if ((pr = prfind(pgid)) == NULL) 722 return; 723 if (CANDELIVER(uid, euid, pr)) 724 prsignal(pr, signum); 725 } 726 } 727 728 /* 729 * Send a signal to a process group. 730 */ 731 void 732 gsignal(int pgid, int signum) 733 { 734 struct pgrp *pgrp; 735 736 if (pgid && (pgrp = pgfind(pgid))) 737 pgsignal(pgrp, signum, 0); 738 } 739 740 /* 741 * Send a signal to a process group. If checktty is 1, 742 * limit to members which have a controlling terminal. 743 */ 744 void 745 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 746 { 747 struct process *pr; 748 749 if (pgrp) 750 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 751 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT) 752 prsignal(pr, signum); 753 } 754 755 /* 756 * Recalculate the signal mask and reset the signal disposition after 757 * usermode frame for delivery is formed. 758 */ 759 void 760 postsig_done(struct proc *p, int signum, struct sigacts *ps) 761 { 762 int mask = sigmask(signum); 763 764 KERNEL_ASSERT_LOCKED(); 765 766 p->p_ru.ru_nsignals++; 767 atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]); 768 if ((ps->ps_sigreset & mask) != 0) { 769 ps->ps_sigcatch &= ~mask; 770 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 771 ps->ps_sigignore |= mask; 772 ps->ps_sigact[signum] = SIG_DFL; 773 } 774 } 775 776 /* 777 * Send a signal caused by a trap to the current thread 778 * If it will be caught immediately, deliver it with correct code. 779 * Otherwise, post it normally. 780 */ 781 void 782 trapsignal(struct proc *p, int signum, u_long trapno, int code, 783 union sigval sigval) 784 { 785 struct process *pr = p->p_p; 786 struct sigacts *ps = pr->ps_sigacts; 787 int mask; 788 789 switch (signum) { 790 case SIGILL: 791 case SIGBUS: 792 case SIGSEGV: 793 pr->ps_acflag |= ATRAP; 794 break; 795 } 796 797 mask = sigmask(signum); 798 if ((pr->ps_flags & PS_TRACED) == 0 && 799 (ps->ps_sigcatch & mask) != 0 && 800 (p->p_sigmask & mask) == 0) { 801 siginfo_t si; 802 initsiginfo(&si, signum, trapno, code, sigval); 803 #ifdef KTRACE 804 if (KTRPOINT(p, KTR_PSIG)) { 805 ktrpsig(p, signum, ps->ps_sigact[signum], 806 p->p_sigmask, code, &si); 807 } 808 #endif 809 sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si); 810 postsig_done(p, signum, ps); 811 } else { 812 p->p_sisig = signum; 813 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 814 p->p_sicode = code; 815 p->p_sigval = sigval; 816 817 /* 818 * Signals like SIGBUS and SIGSEGV should not, when 819 * generated by the kernel, be ignorable or blockable. 820 * If it is and we're not being traced, then just kill 821 * the process. 822 */ 823 if ((pr->ps_flags & PS_TRACED) == 0 && 824 (sigprop[signum] & SA_KILL) && 825 ((p->p_sigmask & mask) || (ps->ps_sigignore & mask))) 826 sigexit(p, signum); 827 ptsignal(p, signum, STHREAD); 828 } 829 } 830 831 /* 832 * Send the signal to the process. If the signal has an action, the action 833 * is usually performed by the target process rather than the caller; we add 834 * the signal to the set of pending signals for the process. 835 * 836 * Exceptions: 837 * o When a stop signal is sent to a sleeping process that takes the 838 * default action, the process is stopped without awakening it. 839 * o SIGCONT restarts stopped processes (or puts them back to sleep) 840 * regardless of the signal action (eg, blocked or ignored). 841 * 842 * Other ignored signals are discarded immediately. 843 */ 844 void 845 psignal(struct proc *p, int signum) 846 { 847 ptsignal(p, signum, SPROCESS); 848 } 849 850 /* 851 * type = SPROCESS process signal, can be diverted (sigwait()) 852 * XXX if blocked in all threads, mark as pending in struct process 853 * type = STHREAD thread signal, but should be propagated if unhandled 854 * type = SPROPAGATED propagated to this thread, so don't propagate again 855 */ 856 void 857 ptsignal(struct proc *p, int signum, enum signal_type type) 858 { 859 int s, prop; 860 sig_t action; 861 int mask; 862 struct process *pr = p->p_p; 863 struct proc *q; 864 int wakeparent = 0; 865 866 #ifdef DIAGNOSTIC 867 if ((u_int)signum >= NSIG || signum == 0) 868 panic("psignal signal number"); 869 #endif 870 871 /* Ignore signal if the target process is exiting */ 872 if (pr->ps_flags & PS_EXITING) 873 return; 874 875 mask = sigmask(signum); 876 877 if (type == SPROCESS) { 878 /* Accept SIGKILL to coredumping processes */ 879 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 880 if (pr->ps_single != NULL) 881 p = pr->ps_single; 882 atomic_setbits_int(&p->p_siglist, mask); 883 return; 884 } 885 886 /* 887 * If the current thread can process the signal 888 * immediately (it's unblocked) then have it take it. 889 */ 890 q = curproc; 891 if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 892 (q->p_sigmask & mask) == 0) 893 p = q; 894 else { 895 /* 896 * A process-wide signal can be diverted to a 897 * different thread that's in sigwait() for this 898 * signal. If there isn't such a thread, then 899 * pick a thread that doesn't have it blocked so 900 * that the stop/kill consideration isn't 901 * delayed. Otherwise, mark it pending on the 902 * main thread. 903 */ 904 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 905 /* ignore exiting threads */ 906 if (q->p_flag & P_WEXIT) 907 continue; 908 909 /* skip threads that have the signal blocked */ 910 if ((q->p_sigmask & mask) != 0) 911 continue; 912 913 /* okay, could send to this thread */ 914 p = q; 915 916 /* 917 * sigsuspend, sigwait, ppoll/pselect, etc? 918 * Definitely go to this thread, as it's 919 * already blocked in the kernel. 920 */ 921 if (q->p_flag & P_SIGSUSPEND) 922 break; 923 } 924 } 925 } 926 927 if (type != SPROPAGATED) 928 KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum); 929 930 prop = sigprop[signum]; 931 932 /* 933 * If proc is traced, always give parent a chance. 934 */ 935 if (pr->ps_flags & PS_TRACED) { 936 action = SIG_DFL; 937 atomic_setbits_int(&p->p_siglist, mask); 938 } else { 939 /* 940 * If the signal is being ignored, 941 * then we forget about it immediately. 942 * (Note: we don't set SIGCONT in ps_sigignore, 943 * and if it is set to SIG_IGN, 944 * action will be SIG_DFL here.) 945 */ 946 if (pr->ps_sigacts->ps_sigignore & mask) 947 return; 948 if (p->p_sigmask & mask) { 949 action = SIG_HOLD; 950 } else if (pr->ps_sigacts->ps_sigcatch & mask) { 951 action = SIG_CATCH; 952 } else { 953 action = SIG_DFL; 954 955 if (prop & SA_KILL && pr->ps_nice > NZERO) 956 pr->ps_nice = NZERO; 957 958 /* 959 * If sending a tty stop signal to a member of an 960 * orphaned process group, discard the signal here if 961 * the action is default; don't stop the process below 962 * if sleeping, and don't clear any pending SIGCONT. 963 */ 964 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 965 return; 966 } 967 968 atomic_setbits_int(&p->p_siglist, mask); 969 } 970 971 if (prop & SA_CONT) 972 atomic_clearbits_int(&p->p_siglist, stopsigmask); 973 974 if (prop & SA_STOP) { 975 atomic_clearbits_int(&p->p_siglist, contsigmask); 976 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 977 } 978 979 /* 980 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 981 */ 982 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 983 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 984 if (q != p) 985 ptsignal(q, signum, SPROPAGATED); 986 987 /* 988 * Defer further processing for signals which are held, 989 * except that stopped processes must be continued by SIGCONT. 990 */ 991 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 992 return; 993 994 SCHED_LOCK(s); 995 996 switch (p->p_stat) { 997 998 case SSLEEP: 999 /* 1000 * If process is sleeping uninterruptibly 1001 * we can't interrupt the sleep... the signal will 1002 * be noticed when the process returns through 1003 * trap() or syscall(). 1004 */ 1005 if ((p->p_flag & P_SINTR) == 0) 1006 goto out; 1007 /* 1008 * Process is sleeping and traced... make it runnable 1009 * so it can discover the signal in issignal() and stop 1010 * for the parent. 1011 */ 1012 if (pr->ps_flags & PS_TRACED) 1013 goto run; 1014 /* 1015 * If SIGCONT is default (or ignored) and process is 1016 * asleep, we are finished; the process should not 1017 * be awakened. 1018 */ 1019 if ((prop & SA_CONT) && action == SIG_DFL) { 1020 atomic_clearbits_int(&p->p_siglist, mask); 1021 goto out; 1022 } 1023 /* 1024 * When a sleeping process receives a stop 1025 * signal, process immediately if possible. 1026 */ 1027 if ((prop & SA_STOP) && action == SIG_DFL) { 1028 /* 1029 * If a child holding parent blocked, 1030 * stopping could cause deadlock. 1031 */ 1032 if (pr->ps_flags & PS_PPWAIT) 1033 goto out; 1034 atomic_clearbits_int(&p->p_siglist, mask); 1035 p->p_xstat = signum; 1036 proc_stop(p, 0); 1037 goto out; 1038 } 1039 /* 1040 * All other (caught or default) signals 1041 * cause the process to run. 1042 */ 1043 goto runfast; 1044 /*NOTREACHED*/ 1045 1046 case SSTOP: 1047 /* 1048 * If traced process is already stopped, 1049 * then no further action is necessary. 1050 */ 1051 if (pr->ps_flags & PS_TRACED) 1052 goto out; 1053 1054 /* 1055 * Kill signal always sets processes running. 1056 */ 1057 if (signum == SIGKILL) { 1058 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1059 goto runfast; 1060 } 1061 1062 if (prop & SA_CONT) { 1063 /* 1064 * If SIGCONT is default (or ignored), we continue the 1065 * process but don't leave the signal in p_siglist, as 1066 * it has no further action. If SIGCONT is held, we 1067 * continue the process and leave the signal in 1068 * p_siglist. If the process catches SIGCONT, let it 1069 * handle the signal itself. If it isn't waiting on 1070 * an event, then it goes back to run state. 1071 * Otherwise, process goes back to sleep state. 1072 */ 1073 atomic_setbits_int(&p->p_flag, P_CONTINUED); 1074 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1075 wakeparent = 1; 1076 if (action == SIG_DFL) 1077 atomic_clearbits_int(&p->p_siglist, mask); 1078 if (action == SIG_CATCH) 1079 goto runfast; 1080 if (p->p_wchan == 0) 1081 goto run; 1082 p->p_stat = SSLEEP; 1083 goto out; 1084 } 1085 1086 if (prop & SA_STOP) { 1087 /* 1088 * Already stopped, don't need to stop again. 1089 * (If we did the shell could get confused.) 1090 */ 1091 atomic_clearbits_int(&p->p_siglist, mask); 1092 goto out; 1093 } 1094 1095 /* 1096 * If process is sleeping interruptibly, then simulate a 1097 * wakeup so that when it is continued, it will be made 1098 * runnable and can look at the signal. But don't make 1099 * the process runnable, leave it stopped. 1100 */ 1101 if (p->p_wchan && p->p_flag & P_SINTR) 1102 unsleep(p); 1103 goto out; 1104 1105 case SONPROC: 1106 signotify(p); 1107 /* FALLTHROUGH */ 1108 default: 1109 /* 1110 * SRUN, SIDL, SDEAD do nothing with the signal, 1111 * other than kicking ourselves if we are running. 1112 * It will either never be noticed, or noticed very soon. 1113 */ 1114 goto out; 1115 } 1116 /*NOTREACHED*/ 1117 1118 runfast: 1119 /* 1120 * Raise priority to at least PUSER. 1121 */ 1122 if (p->p_priority > PUSER) 1123 p->p_priority = PUSER; 1124 run: 1125 setrunnable(p); 1126 out: 1127 SCHED_UNLOCK(s); 1128 if (wakeparent) 1129 wakeup(pr->ps_pptr); 1130 } 1131 1132 /* 1133 * If the current process has received a signal (should be caught or cause 1134 * termination, should interrupt current syscall), return the signal number. 1135 * Stop signals with default action are processed immediately, then cleared; 1136 * they aren't returned. This is checked after each entry to the system for 1137 * a syscall or trap (though this can usually be done without calling issignal 1138 * by checking the pending signal masks in the CURSIG macro.) The normal call 1139 * sequence is 1140 * 1141 * while (signum = CURSIG(curproc)) 1142 * postsig(signum); 1143 * 1144 * Assumes that if the P_SINTR flag is set, we're holding both the 1145 * kernel and scheduler locks. 1146 */ 1147 int 1148 issignal(struct proc *p) 1149 { 1150 struct process *pr = p->p_p; 1151 int signum, mask, prop; 1152 int dolock = (p->p_flag & P_SINTR) == 0; 1153 int s; 1154 1155 for (;;) { 1156 mask = SIGPENDING(p); 1157 if (pr->ps_flags & PS_PPWAIT) 1158 mask &= ~stopsigmask; 1159 if (mask == 0) /* no signal to send */ 1160 return (0); 1161 signum = ffs((long)mask); 1162 mask = sigmask(signum); 1163 if (p->p_siglist & mask) 1164 atomic_clearbits_int(&p->p_siglist, mask); 1165 else 1166 atomic_clearbits_int(&pr->ps_mainproc->p_siglist, mask); 1167 1168 /* 1169 * We should see pending but ignored signals 1170 * only if PS_TRACED was on when they were posted. 1171 */ 1172 if (mask & pr->ps_sigacts->ps_sigignore && 1173 (pr->ps_flags & PS_TRACED) == 0) 1174 continue; 1175 1176 /* 1177 * If traced, always stop, and stay stopped until released 1178 * by the debugger. If our parent process is waiting for 1179 * us, don't hang as we could deadlock. 1180 */ 1181 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1182 signum != SIGKILL) { 1183 p->p_xstat = signum; 1184 1185 if (dolock) 1186 KERNEL_LOCK(); 1187 single_thread_set(p, SINGLE_PTRACE, 0); 1188 if (dolock) 1189 KERNEL_UNLOCK(); 1190 1191 if (dolock) 1192 SCHED_LOCK(s); 1193 proc_stop(p, 1); 1194 if (dolock) 1195 SCHED_UNLOCK(s); 1196 1197 if (dolock) 1198 KERNEL_LOCK(); 1199 single_thread_clear(p, 0); 1200 if (dolock) 1201 KERNEL_UNLOCK(); 1202 1203 /* 1204 * If we are no longer being traced, or the parent 1205 * didn't give us a signal, look for more signals. 1206 */ 1207 if ((pr->ps_flags & PS_TRACED) == 0 || p->p_xstat == 0) 1208 continue; 1209 1210 /* 1211 * If the new signal is being masked, look for other 1212 * signals. 1213 */ 1214 signum = p->p_xstat; 1215 mask = sigmask(signum); 1216 if ((p->p_sigmask & mask) != 0) 1217 continue; 1218 1219 /* take the signal! */ 1220 atomic_clearbits_int(&p->p_siglist, mask); 1221 } 1222 1223 prop = sigprop[signum]; 1224 1225 /* 1226 * Decide whether the signal should be returned. 1227 * Return the signal's number, or fall through 1228 * to clear it from the pending mask. 1229 */ 1230 switch ((long)pr->ps_sigacts->ps_sigact[signum]) { 1231 case (long)SIG_DFL: 1232 /* 1233 * Don't take default actions on system processes. 1234 */ 1235 if (pr->ps_pid <= 1) { 1236 #ifdef DIAGNOSTIC 1237 /* 1238 * Are you sure you want to ignore SIGSEGV 1239 * in init? XXX 1240 */ 1241 printf("Process (pid %d) got signal" 1242 " %d\n", pr->ps_pid, signum); 1243 #endif 1244 break; /* == ignore */ 1245 } 1246 /* 1247 * If there is a pending stop signal to process 1248 * with default action, stop here, 1249 * then clear the signal. However, 1250 * if process is member of an orphaned 1251 * process group, ignore tty stop signals. 1252 */ 1253 if (prop & SA_STOP) { 1254 if (pr->ps_flags & PS_TRACED || 1255 (pr->ps_pgrp->pg_jobc == 0 && 1256 prop & SA_TTYSTOP)) 1257 break; /* == ignore */ 1258 p->p_xstat = signum; 1259 if (dolock) 1260 SCHED_LOCK(s); 1261 proc_stop(p, 1); 1262 if (dolock) 1263 SCHED_UNLOCK(s); 1264 break; 1265 } else if (prop & SA_IGNORE) { 1266 /* 1267 * Except for SIGCONT, shouldn't get here. 1268 * Default action is to ignore; drop it. 1269 */ 1270 break; /* == ignore */ 1271 } else 1272 goto keep; 1273 /*NOTREACHED*/ 1274 case (long)SIG_IGN: 1275 /* 1276 * Masking above should prevent us ever trying 1277 * to take action on an ignored signal other 1278 * than SIGCONT, unless process is traced. 1279 */ 1280 if ((prop & SA_CONT) == 0 && 1281 (pr->ps_flags & PS_TRACED) == 0) 1282 printf("issignal\n"); 1283 break; /* == ignore */ 1284 default: 1285 /* 1286 * This signal has an action, let 1287 * postsig() process it. 1288 */ 1289 goto keep; 1290 } 1291 } 1292 /* NOTREACHED */ 1293 1294 keep: 1295 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */ 1296 return (signum); 1297 } 1298 1299 /* 1300 * Put the argument process into the stopped state and notify the parent 1301 * via wakeup. Signals are handled elsewhere. The process must not be 1302 * on the run queue. 1303 */ 1304 void 1305 proc_stop(struct proc *p, int sw) 1306 { 1307 struct process *pr = p->p_p; 1308 extern void *softclock_si; 1309 1310 #ifdef MULTIPROCESSOR 1311 SCHED_ASSERT_LOCKED(); 1312 #endif 1313 1314 p->p_stat = SSTOP; 1315 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1316 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1317 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1318 if (!timeout_pending(&proc_stop_to)) { 1319 timeout_add(&proc_stop_to, 0); 1320 /* 1321 * We need this soft interrupt to be handled fast. 1322 * Extra calls to softclock don't hurt. 1323 */ 1324 softintr_schedule(softclock_si); 1325 } 1326 if (sw) 1327 mi_switch(); 1328 } 1329 1330 /* 1331 * Called from a timeout to send signals to the parents of stopped processes. 1332 * We can't do this in proc_stop because it's called with nasty locks held 1333 * and we would need recursive scheduler lock to deal with that. 1334 */ 1335 void 1336 proc_stop_sweep(void *v) 1337 { 1338 struct process *pr; 1339 1340 LIST_FOREACH(pr, &allprocess, ps_list) { 1341 if ((pr->ps_flags & PS_STOPPED) == 0) 1342 continue; 1343 atomic_clearbits_int(&pr->ps_flags, PS_STOPPED); 1344 1345 if ((pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDSTOP) == 0) 1346 prsignal(pr->ps_pptr, SIGCHLD); 1347 wakeup(pr->ps_pptr); 1348 } 1349 } 1350 1351 /* 1352 * Take the action for the specified signal 1353 * from the current set of pending signals. 1354 */ 1355 void 1356 postsig(struct proc *p, int signum) 1357 { 1358 struct process *pr = p->p_p; 1359 struct sigacts *ps = pr->ps_sigacts; 1360 sig_t action; 1361 u_long trapno; 1362 int mask, returnmask; 1363 siginfo_t si; 1364 union sigval sigval; 1365 int s, code; 1366 1367 KASSERT(signum != 0); 1368 KERNEL_ASSERT_LOCKED(); 1369 1370 mask = sigmask(signum); 1371 atomic_clearbits_int(&p->p_siglist, mask); 1372 action = ps->ps_sigact[signum]; 1373 sigval.sival_ptr = 0; 1374 1375 if (p->p_sisig != signum) { 1376 trapno = 0; 1377 code = SI_USER; 1378 sigval.sival_ptr = 0; 1379 } else { 1380 trapno = p->p_sitrapno; 1381 code = p->p_sicode; 1382 sigval = p->p_sigval; 1383 } 1384 initsiginfo(&si, signum, trapno, code, sigval); 1385 1386 #ifdef KTRACE 1387 if (KTRPOINT(p, KTR_PSIG)) { 1388 ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ? 1389 p->p_oldmask : p->p_sigmask, code, &si); 1390 } 1391 #endif 1392 if (action == SIG_DFL) { 1393 /* 1394 * Default action, where the default is to kill 1395 * the process. (Other cases were ignored above.) 1396 */ 1397 sigexit(p, signum); 1398 /* NOTREACHED */ 1399 } else { 1400 /* 1401 * If we get here, the signal must be caught. 1402 */ 1403 #ifdef DIAGNOSTIC 1404 if (action == SIG_IGN || (p->p_sigmask & mask)) 1405 panic("postsig action"); 1406 #endif 1407 /* 1408 * Set the new mask value and also defer further 1409 * occurrences of this signal. 1410 * 1411 * Special case: user has done a sigpause. Here the 1412 * current mask is not of interest, but rather the 1413 * mask from before the sigpause is what we want 1414 * restored after the signal processing is completed. 1415 */ 1416 #ifdef MULTIPROCESSOR 1417 s = splsched(); 1418 #else 1419 s = splhigh(); 1420 #endif 1421 if (p->p_flag & P_SIGSUSPEND) { 1422 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1423 returnmask = p->p_oldmask; 1424 } else { 1425 returnmask = p->p_sigmask; 1426 } 1427 if (p->p_sisig == signum) { 1428 p->p_sisig = 0; 1429 p->p_sitrapno = 0; 1430 p->p_sicode = SI_USER; 1431 p->p_sigval.sival_ptr = NULL; 1432 } 1433 1434 sendsig(action, signum, returnmask, &si); 1435 postsig_done(p, signum, ps); 1436 splx(s); 1437 } 1438 } 1439 1440 /* 1441 * Force the current process to exit with the specified signal, dumping core 1442 * if appropriate. We bypass the normal tests for masked and caught signals, 1443 * allowing unrecoverable failures to terminate the process without changing 1444 * signal state. Mark the accounting record with the signal termination. 1445 * If dumping core, save the signal number for the debugger. Calls exit and 1446 * does not return. 1447 */ 1448 void 1449 sigexit(struct proc *p, int signum) 1450 { 1451 /* Mark process as going away */ 1452 atomic_setbits_int(&p->p_flag, P_WEXIT); 1453 1454 p->p_p->ps_acflag |= AXSIG; 1455 if (sigprop[signum] & SA_CORE) { 1456 p->p_sisig = signum; 1457 1458 /* if there are other threads, pause them */ 1459 if (P_HASSIBLING(p)) 1460 single_thread_set(p, SINGLE_SUSPEND, 0); 1461 1462 if (coredump(p) == 0) 1463 signum |= WCOREFLAG; 1464 } 1465 exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL); 1466 /* NOTREACHED */ 1467 } 1468 1469 int nosuidcoredump = 1; 1470 1471 struct coredump_iostate { 1472 struct proc *io_proc; 1473 struct vnode *io_vp; 1474 struct ucred *io_cred; 1475 off_t io_offset; 1476 }; 1477 1478 /* 1479 * Dump core, into a file named "progname.core", unless the process was 1480 * setuid/setgid. 1481 */ 1482 int 1483 coredump(struct proc *p) 1484 { 1485 #ifdef SMALL_KERNEL 1486 return EPERM; 1487 #else 1488 struct process *pr = p->p_p; 1489 struct vnode *vp; 1490 struct ucred *cred = p->p_ucred; 1491 struct vmspace *vm = p->p_vmspace; 1492 struct nameidata nd; 1493 struct vattr vattr; 1494 struct coredump_iostate io; 1495 int error, len, incrash = 0; 1496 char name[MAXPATHLEN]; 1497 const char *dir = "/var/crash"; 1498 1499 if (pr->ps_emul->e_coredump == NULL) 1500 return (EINVAL); 1501 1502 pr->ps_flags |= PS_COREDUMP; 1503 1504 /* 1505 * If the process has inconsistent uids, nosuidcoredump 1506 * determines coredump placement policy. 1507 */ 1508 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1509 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1510 if (nosuidcoredump == 3 || nosuidcoredump == 2) 1511 incrash = 1; 1512 else 1513 return (EPERM); 1514 } 1515 1516 /* Don't dump if will exceed file size limit. */ 1517 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= 1518 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1519 return (EFBIG); 1520 1521 if (incrash && nosuidcoredump == 3) { 1522 /* 1523 * If the program directory does not exist, dumps of 1524 * that core will silently fail. 1525 */ 1526 len = snprintf(name, sizeof(name), "%s/%s/%u.core", 1527 dir, pr->ps_comm, pr->ps_pid); 1528 } else if (incrash && nosuidcoredump == 2) 1529 len = snprintf(name, sizeof(name), "%s/%s.core", 1530 dir, pr->ps_comm); 1531 else 1532 len = snprintf(name, sizeof(name), "%s.core", pr->ps_comm); 1533 if (len >= sizeof(name)) 1534 return (EACCES); 1535 1536 /* 1537 * Control the UID used to write out. The normal case uses 1538 * the real UID. If the sugid case is going to write into the 1539 * controlled directory, we do so as root. 1540 */ 1541 if (incrash == 0) { 1542 cred = crdup(cred); 1543 cred->cr_uid = cred->cr_ruid; 1544 cred->cr_gid = cred->cr_rgid; 1545 } else { 1546 if (p->p_fd->fd_rdir) { 1547 vrele(p->p_fd->fd_rdir); 1548 p->p_fd->fd_rdir = NULL; 1549 } 1550 p->p_ucred = crdup(p->p_ucred); 1551 crfree(cred); 1552 cred = p->p_ucred; 1553 crhold(cred); 1554 cred->cr_uid = 0; 1555 cred->cr_gid = 0; 1556 } 1557 1558 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1559 1560 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR); 1561 1562 if (error) 1563 goto out; 1564 1565 /* 1566 * Don't dump to non-regular files, files with links, or files 1567 * owned by someone else. 1568 */ 1569 vp = nd.ni_vp; 1570 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1571 VOP_UNLOCK(vp); 1572 vn_close(vp, FWRITE, cred, p); 1573 goto out; 1574 } 1575 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1576 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1577 vattr.va_uid != cred->cr_uid) { 1578 error = EACCES; 1579 VOP_UNLOCK(vp); 1580 vn_close(vp, FWRITE, cred, p); 1581 goto out; 1582 } 1583 VATTR_NULL(&vattr); 1584 vattr.va_size = 0; 1585 VOP_SETATTR(vp, &vattr, cred, p); 1586 pr->ps_acflag |= ACORE; 1587 1588 io.io_proc = p; 1589 io.io_vp = vp; 1590 io.io_cred = cred; 1591 io.io_offset = 0; 1592 VOP_UNLOCK(vp); 1593 vref(vp); 1594 error = vn_close(vp, FWRITE, cred, p); 1595 if (error == 0) 1596 error = (*pr->ps_emul->e_coredump)(p, &io); 1597 vrele(vp); 1598 out: 1599 crfree(cred); 1600 return (error); 1601 #endif 1602 } 1603 1604 #ifndef SMALL_KERNEL 1605 int 1606 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 1607 { 1608 struct coredump_iostate *io = cookie; 1609 off_t coffset = 0; 1610 size_t csize; 1611 int chunk, error; 1612 1613 csize = len; 1614 do { 1615 if (io->io_proc->p_siglist & sigmask(SIGKILL)) 1616 return (EINTR); 1617 1618 /* Rest of the loop sleeps with lock held, so... */ 1619 yield(); 1620 1621 chunk = MIN(csize, MAXPHYS); 1622 error = vn_rdwr(UIO_WRITE, io->io_vp, 1623 (caddr_t)data + coffset, chunk, 1624 io->io_offset + coffset, segflg, 1625 IO_UNIT, io->io_cred, NULL, io->io_proc); 1626 if (error) { 1627 struct process *pr = io->io_proc->p_p; 1628 if (error == ENOSPC) 1629 log(LOG_ERR, "coredump of %s(%d) failed, filesystem full\n", 1630 pr->ps_comm, pr->ps_pid); 1631 else 1632 log(LOG_ERR, "coredump of %s(%d), write failed: errno %d\n", 1633 pr->ps_comm, pr->ps_pid, error); 1634 return (error); 1635 } 1636 1637 coffset += chunk; 1638 csize -= chunk; 1639 } while (csize > 0); 1640 1641 io->io_offset += len; 1642 return (0); 1643 } 1644 1645 void 1646 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1647 { 1648 struct coredump_iostate *io = cookie; 1649 1650 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1651 } 1652 1653 #endif /* !SMALL_KERNEL */ 1654 1655 /* 1656 * Nonexistent system call-- signal process (may want to handle it). 1657 * Flag error in case process won't see signal immediately (blocked or ignored). 1658 */ 1659 int 1660 sys_nosys(struct proc *p, void *v, register_t *retval) 1661 { 1662 1663 ptsignal(p, SIGSYS, STHREAD); 1664 return (ENOSYS); 1665 } 1666 1667 int 1668 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1669 { 1670 static int sigwaitsleep; 1671 struct sys___thrsigdivert_args /* { 1672 syscallarg(sigset_t) sigmask; 1673 syscallarg(siginfo_t *) info; 1674 syscallarg(const struct timespec *) timeout; 1675 } */ *uap = v; 1676 struct process *pr = p->p_p; 1677 sigset_t *m; 1678 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1679 siginfo_t si; 1680 uint64_t to_ticks = 0; 1681 int timeinvalid = 0; 1682 int error = 0; 1683 1684 memset(&si, 0, sizeof(si)); 1685 1686 if (SCARG(uap, timeout) != NULL) { 1687 struct timespec ts; 1688 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1689 return (error); 1690 #ifdef KTRACE 1691 if (KTRPOINT(p, KTR_STRUCT)) 1692 ktrreltimespec(p, &ts); 1693 #endif 1694 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) 1695 timeinvalid = 1; 1696 else { 1697 to_ticks = (uint64_t)hz * ts.tv_sec + 1698 ts.tv_nsec / (tick * 1000); 1699 if (to_ticks > INT_MAX) 1700 to_ticks = INT_MAX; 1701 if (to_ticks == 0 && ts.tv_nsec) 1702 to_ticks = 1; 1703 } 1704 } 1705 1706 dosigsuspend(p, p->p_sigmask &~ mask); 1707 for (;;) { 1708 si.si_signo = CURSIG(p); 1709 if (si.si_signo != 0) { 1710 sigset_t smask = sigmask(si.si_signo); 1711 if (smask & mask) { 1712 if (p->p_siglist & smask) 1713 m = &p->p_siglist; 1714 else if (pr->ps_mainproc->p_siglist & smask) 1715 m = &pr->ps_mainproc->p_siglist; 1716 else { 1717 /* signal got eaten by someone else? */ 1718 continue; 1719 } 1720 atomic_clearbits_int(m, smask); 1721 error = 0; 1722 break; 1723 } 1724 } 1725 1726 /* per-POSIX, delay this error until after the above */ 1727 if (timeinvalid) 1728 error = EINVAL; 1729 1730 if (SCARG(uap, timeout) != NULL && to_ticks == 0) 1731 error = EAGAIN; 1732 1733 if (error != 0) 1734 break; 1735 1736 error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait", 1737 (int)to_ticks); 1738 } 1739 1740 if (error == 0) { 1741 *retval = si.si_signo; 1742 if (SCARG(uap, info) != NULL) 1743 error = copyout(&si, SCARG(uap, info), sizeof(si)); 1744 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) { 1745 /* 1746 * Restarting is wrong if there's a timeout, as it'll be 1747 * for the same interval again 1748 */ 1749 error = EINTR; 1750 } 1751 1752 return (error); 1753 } 1754 1755 void 1756 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val) 1757 { 1758 memset(si, 0, sizeof(*si)); 1759 1760 si->si_signo = sig; 1761 si->si_code = code; 1762 if (code == SI_USER) { 1763 si->si_value = val; 1764 } else { 1765 switch (sig) { 1766 case SIGSEGV: 1767 case SIGILL: 1768 case SIGBUS: 1769 case SIGFPE: 1770 si->si_addr = val.sival_ptr; 1771 si->si_trapno = trapno; 1772 break; 1773 case SIGXFSZ: 1774 break; 1775 } 1776 } 1777 } 1778 1779 int 1780 filt_sigattach(struct knote *kn) 1781 { 1782 struct process *pr = curproc->p_p; 1783 1784 if (kn->kn_id >= NSIG) 1785 return EINVAL; 1786 1787 kn->kn_ptr.p_process = pr; 1788 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1789 1790 /* XXX lock the proc here while adding to the list? */ 1791 SLIST_INSERT_HEAD(&pr->ps_klist, kn, kn_selnext); 1792 1793 return (0); 1794 } 1795 1796 void 1797 filt_sigdetach(struct knote *kn) 1798 { 1799 struct process *pr = kn->kn_ptr.p_process; 1800 1801 SLIST_REMOVE(&pr->ps_klist, kn, knote, kn_selnext); 1802 } 1803 1804 /* 1805 * signal knotes are shared with proc knotes, so we apply a mask to 1806 * the hint in order to differentiate them from process hints. This 1807 * could be avoided by using a signal-specific knote list, but probably 1808 * isn't worth the trouble. 1809 */ 1810 int 1811 filt_signal(struct knote *kn, long hint) 1812 { 1813 1814 if (hint & NOTE_SIGNAL) { 1815 hint &= ~NOTE_SIGNAL; 1816 1817 if (kn->kn_id == hint) 1818 kn->kn_data++; 1819 } 1820 return (kn->kn_data != 0); 1821 } 1822 1823 void 1824 userret(struct proc *p) 1825 { 1826 int signum; 1827 1828 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */ 1829 if (p->p_flag & P_PROFPEND) { 1830 atomic_clearbits_int(&p->p_flag, P_PROFPEND); 1831 KERNEL_LOCK(); 1832 psignal(p, SIGPROF); 1833 KERNEL_UNLOCK(); 1834 } 1835 if (p->p_flag & P_ALRMPEND) { 1836 atomic_clearbits_int(&p->p_flag, P_ALRMPEND); 1837 KERNEL_LOCK(); 1838 psignal(p, SIGVTALRM); 1839 KERNEL_UNLOCK(); 1840 } 1841 1842 if (SIGPENDING(p) != 0) { 1843 KERNEL_LOCK(); 1844 while ((signum = CURSIG(p)) != 0) 1845 postsig(p, signum); 1846 KERNEL_UNLOCK(); 1847 } 1848 1849 /* 1850 * If P_SIGSUSPEND is still set here, then we still need to restore 1851 * the original sigmask before returning to userspace. Also, this 1852 * might unmask some pending signals, so we need to check a second 1853 * time for signals to post. 1854 */ 1855 if (p->p_flag & P_SIGSUSPEND) { 1856 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1857 p->p_sigmask = p->p_oldmask; 1858 1859 KERNEL_LOCK(); 1860 while ((signum = CURSIG(p)) != 0) 1861 postsig(p, signum); 1862 KERNEL_UNLOCK(); 1863 } 1864 1865 if (p->p_flag & P_SUSPSINGLE) { 1866 KERNEL_LOCK(); 1867 single_thread_check(p, 0); 1868 KERNEL_UNLOCK(); 1869 } 1870 1871 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning"); 1872 1873 p->p_cpu->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri; 1874 } 1875 1876 int 1877 single_thread_check(struct proc *p, int deep) 1878 { 1879 struct process *pr = p->p_p; 1880 1881 if (pr->ps_single != NULL && pr->ps_single != p) { 1882 do { 1883 int s; 1884 1885 /* if we're in deep, we need to unwind to the edge */ 1886 if (deep) { 1887 if (pr->ps_flags & PS_SINGLEUNWIND) 1888 return (ERESTART); 1889 if (pr->ps_flags & PS_SINGLEEXIT) 1890 return (EINTR); 1891 } 1892 1893 if (--pr->ps_singlecount == 0) 1894 wakeup(&pr->ps_singlecount); 1895 if (pr->ps_flags & PS_SINGLEEXIT) 1896 exit1(p, 0, EXIT_THREAD_NOCHECK); 1897 1898 /* not exiting and don't need to unwind, so suspend */ 1899 SCHED_LOCK(s); 1900 p->p_stat = SSTOP; 1901 mi_switch(); 1902 SCHED_UNLOCK(s); 1903 } while (pr->ps_single != NULL); 1904 } 1905 1906 return (0); 1907 } 1908 1909 /* 1910 * Stop other threads in the process. The mode controls how and 1911 * where the other threads should stop: 1912 * - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit 1913 * (by setting to SINGLE_EXIT) or be released (via single_thread_clear()) 1914 * - SINGLE_PTRACE: stop wherever they are, will wait for them to stop 1915 * later (via single_thread_wait()) and released as with SINGLE_SUSPEND 1916 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit 1917 * or released as with SINGLE_SUSPEND 1918 * - SINGLE_EXIT: unwind to kernel boundary and exit 1919 */ 1920 int 1921 single_thread_set(struct proc *p, enum single_thread_mode mode, int deep) 1922 { 1923 struct process *pr = p->p_p; 1924 struct proc *q; 1925 int error; 1926 1927 KERNEL_ASSERT_LOCKED(); 1928 1929 if ((error = single_thread_check(p, deep))) 1930 return error; 1931 1932 switch (mode) { 1933 case SINGLE_SUSPEND: 1934 case SINGLE_PTRACE: 1935 break; 1936 case SINGLE_UNWIND: 1937 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 1938 break; 1939 case SINGLE_EXIT: 1940 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT); 1941 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 1942 break; 1943 #ifdef DIAGNOSTIC 1944 default: 1945 panic("single_thread_mode = %d", mode); 1946 #endif 1947 } 1948 pr->ps_single = p; 1949 pr->ps_singlecount = 0; 1950 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 1951 int s; 1952 1953 if (q == p) 1954 continue; 1955 if (q->p_flag & P_WEXIT) { 1956 if (mode == SINGLE_EXIT) { 1957 SCHED_LOCK(s); 1958 if (q->p_stat == SSTOP) { 1959 setrunnable(q); 1960 pr->ps_singlecount++; 1961 } 1962 SCHED_UNLOCK(s); 1963 } 1964 continue; 1965 } 1966 SCHED_LOCK(s); 1967 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE); 1968 switch (q->p_stat) { 1969 case SIDL: 1970 case SRUN: 1971 pr->ps_singlecount++; 1972 break; 1973 case SSLEEP: 1974 /* if it's not interruptible, then just have to wait */ 1975 if (q->p_flag & P_SINTR) { 1976 /* merely need to suspend? just stop it */ 1977 if (mode == SINGLE_SUSPEND || 1978 mode == SINGLE_PTRACE) { 1979 q->p_stat = SSTOP; 1980 break; 1981 } 1982 /* need to unwind or exit, so wake it */ 1983 setrunnable(q); 1984 } 1985 pr->ps_singlecount++; 1986 break; 1987 case SSTOP: 1988 if (mode == SINGLE_EXIT) { 1989 setrunnable(q); 1990 pr->ps_singlecount++; 1991 } 1992 break; 1993 case SDEAD: 1994 break; 1995 case SONPROC: 1996 pr->ps_singlecount++; 1997 signotify(q); 1998 break; 1999 } 2000 SCHED_UNLOCK(s); 2001 } 2002 2003 if (mode != SINGLE_PTRACE) 2004 single_thread_wait(pr); 2005 2006 return 0; 2007 } 2008 2009 void 2010 single_thread_wait(struct process *pr) 2011 { 2012 /* wait until they're all suspended */ 2013 while (pr->ps_singlecount > 0) 2014 tsleep(&pr->ps_singlecount, PUSER, "suspend", 0); 2015 } 2016 2017 void 2018 single_thread_clear(struct proc *p, int flag) 2019 { 2020 struct process *pr = p->p_p; 2021 struct proc *q; 2022 2023 KASSERT(pr->ps_single == p); 2024 KERNEL_ASSERT_LOCKED(); 2025 2026 pr->ps_single = NULL; 2027 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); 2028 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2029 int s; 2030 2031 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0) 2032 continue; 2033 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE); 2034 2035 /* 2036 * if the thread was only stopped for single threading 2037 * then clearing that either makes it runnable or puts 2038 * it back into some sleep queue 2039 */ 2040 SCHED_LOCK(s); 2041 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) { 2042 if (q->p_wchan == 0) 2043 setrunnable(q); 2044 else 2045 q->p_stat = SSLEEP; 2046 } 2047 SCHED_UNLOCK(s); 2048 } 2049 } 2050