1 /* $OpenBSD: kern_sig.c,v 1.218 2018/03/27 08:22:41 mpi 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 p->p_sigstk = ss; 558 return (0); 559 } 560 561 int 562 sys_kill(struct proc *cp, void *v, register_t *retval) 563 { 564 struct sys_kill_args /* { 565 syscallarg(int) pid; 566 syscallarg(int) signum; 567 } */ *uap = v; 568 struct process *pr; 569 int pid = SCARG(uap, pid); 570 int signum = SCARG(uap, signum); 571 int error; 572 int zombie = 0; 573 574 if ((error = pledge_kill(cp, pid)) != 0) 575 return (error); 576 if (((u_int)signum) >= NSIG) 577 return (EINVAL); 578 if (pid > 0) { 579 if ((pr = prfind(pid)) == NULL) { 580 if ((pr = zombiefind(pid)) == NULL) 581 return (ESRCH); 582 else 583 zombie = 1; 584 } 585 if (!cansignal(cp, pr, signum)) 586 return (EPERM); 587 588 /* kill single process */ 589 if (signum && !zombie) 590 prsignal(pr, signum); 591 return (0); 592 } 593 switch (pid) { 594 case -1: /* broadcast signal */ 595 return (killpg1(cp, signum, 0, 1)); 596 case 0: /* signal own process group */ 597 return (killpg1(cp, signum, 0, 0)); 598 default: /* negative explicit process group */ 599 return (killpg1(cp, signum, -pid, 0)); 600 } 601 } 602 603 int 604 sys_thrkill(struct proc *cp, void *v, register_t *retval) 605 { 606 struct sys_thrkill_args /* { 607 syscallarg(pid_t) tid; 608 syscallarg(int) signum; 609 syscallarg(void *) tcb; 610 } */ *uap = v; 611 struct proc *p; 612 int tid = SCARG(uap, tid); 613 int signum = SCARG(uap, signum); 614 void *tcb; 615 616 if (((u_int)signum) >= NSIG) 617 return (EINVAL); 618 if (tid > THREAD_PID_OFFSET) { 619 if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL) 620 return (ESRCH); 621 622 /* can only kill threads in the same process */ 623 if (p->p_p != cp->p_p) 624 return (ESRCH); 625 } else if (tid == 0) 626 p = cp; 627 else 628 return (EINVAL); 629 630 /* optionally require the target thread to have the given tcb addr */ 631 tcb = SCARG(uap, tcb); 632 if (tcb != NULL && tcb != TCB_GET(p)) 633 return (ESRCH); 634 635 if (signum) 636 ptsignal(p, signum, STHREAD); 637 return (0); 638 } 639 640 /* 641 * Common code for kill process group/broadcast kill. 642 * cp is calling process. 643 */ 644 int 645 killpg1(struct proc *cp, int signum, int pgid, int all) 646 { 647 struct process *pr; 648 struct pgrp *pgrp; 649 int nfound = 0; 650 651 if (all) { 652 /* 653 * broadcast 654 */ 655 LIST_FOREACH(pr, &allprocess, ps_list) { 656 if (pr->ps_pid <= 1 || 657 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) || 658 pr == cp->p_p || !cansignal(cp, pr, signum)) 659 continue; 660 nfound++; 661 if (signum) 662 prsignal(pr, signum); 663 } 664 } else { 665 if (pgid == 0) 666 /* 667 * zero pgid means send to my process group. 668 */ 669 pgrp = cp->p_p->ps_pgrp; 670 else { 671 pgrp = pgfind(pgid); 672 if (pgrp == NULL) 673 return (ESRCH); 674 } 675 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 676 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM || 677 !cansignal(cp, pr, signum)) 678 continue; 679 nfound++; 680 if (signum) 681 prsignal(pr, signum); 682 } 683 } 684 return (nfound ? 0 : ESRCH); 685 } 686 687 #define CANDELIVER(uid, euid, pr) \ 688 (euid == 0 || \ 689 (uid) == (pr)->ps_ucred->cr_ruid || \ 690 (uid) == (pr)->ps_ucred->cr_svuid || \ 691 (uid) == (pr)->ps_ucred->cr_uid || \ 692 (euid) == (pr)->ps_ucred->cr_ruid || \ 693 (euid) == (pr)->ps_ucred->cr_svuid || \ 694 (euid) == (pr)->ps_ucred->cr_uid) 695 696 /* 697 * Deliver signum to pgid, but first check uid/euid against each 698 * process and see if it is permitted. 699 */ 700 void 701 csignal(pid_t pgid, int signum, uid_t uid, uid_t euid) 702 { 703 struct pgrp *pgrp; 704 struct process *pr; 705 706 if (pgid == 0) 707 return; 708 if (pgid < 0) { 709 pgid = -pgid; 710 if ((pgrp = pgfind(pgid)) == NULL) 711 return; 712 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 713 if (CANDELIVER(uid, euid, pr)) 714 prsignal(pr, signum); 715 } else { 716 if ((pr = prfind(pgid)) == NULL) 717 return; 718 if (CANDELIVER(uid, euid, pr)) 719 prsignal(pr, signum); 720 } 721 } 722 723 /* 724 * Send a signal to a process group. 725 */ 726 void 727 gsignal(int pgid, int signum) 728 { 729 struct pgrp *pgrp; 730 731 if (pgid && (pgrp = pgfind(pgid))) 732 pgsignal(pgrp, signum, 0); 733 } 734 735 /* 736 * Send a signal to a process group. If checktty is 1, 737 * limit to members which have a controlling terminal. 738 */ 739 void 740 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 741 { 742 struct process *pr; 743 744 if (pgrp) 745 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 746 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT) 747 prsignal(pr, signum); 748 } 749 750 /* 751 * Recalculate the signal mask and reset the signal disposition after 752 * usermode frame for delivery is formed. 753 */ 754 void 755 postsig_done(struct proc *p, int signum, struct sigacts *ps) 756 { 757 int mask = sigmask(signum); 758 759 KERNEL_ASSERT_LOCKED(); 760 761 p->p_ru.ru_nsignals++; 762 atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]); 763 if ((ps->ps_sigreset & mask) != 0) { 764 ps->ps_sigcatch &= ~mask; 765 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 766 ps->ps_sigignore |= mask; 767 ps->ps_sigact[signum] = SIG_DFL; 768 } 769 } 770 771 /* 772 * Send a signal caused by a trap to the current thread 773 * If it will be caught immediately, deliver it with correct code. 774 * Otherwise, post it normally. 775 */ 776 void 777 trapsignal(struct proc *p, int signum, u_long trapno, int code, 778 union sigval sigval) 779 { 780 struct process *pr = p->p_p; 781 struct sigacts *ps = pr->ps_sigacts; 782 int mask; 783 784 switch (signum) { 785 case SIGILL: 786 case SIGBUS: 787 case SIGSEGV: 788 pr->ps_acflag |= ATRAP; 789 break; 790 } 791 792 mask = sigmask(signum); 793 if ((pr->ps_flags & PS_TRACED) == 0 && 794 (ps->ps_sigcatch & mask) != 0 && 795 (p->p_sigmask & mask) == 0) { 796 #ifdef KTRACE 797 if (KTRPOINT(p, KTR_PSIG)) { 798 siginfo_t si; 799 800 initsiginfo(&si, signum, trapno, code, sigval); 801 ktrpsig(p, signum, ps->ps_sigact[signum], 802 p->p_sigmask, code, &si); 803 } 804 #endif 805 (*pr->ps_emul->e_sendsig)(ps->ps_sigact[signum], signum, 806 p->p_sigmask, trapno, code, sigval); 807 postsig_done(p, signum, ps); 808 } else { 809 p->p_sisig = signum; 810 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 811 p->p_sicode = code; 812 p->p_sigval = sigval; 813 814 /* 815 * Signals like SIGBUS and SIGSEGV should not, when 816 * generated by the kernel, be ignorable or blockable. 817 * If it is and we're not being traced, then just kill 818 * the process. 819 */ 820 if ((pr->ps_flags & PS_TRACED) == 0 && 821 (sigprop[signum] & SA_KILL) && 822 ((p->p_sigmask & mask) || (ps->ps_sigignore & mask))) 823 sigexit(p, signum); 824 ptsignal(p, signum, STHREAD); 825 } 826 } 827 828 /* 829 * Send the signal to the process. If the signal has an action, the action 830 * is usually performed by the target process rather than the caller; we add 831 * the signal to the set of pending signals for the process. 832 * 833 * Exceptions: 834 * o When a stop signal is sent to a sleeping process that takes the 835 * default action, the process is stopped without awakening it. 836 * o SIGCONT restarts stopped processes (or puts them back to sleep) 837 * regardless of the signal action (eg, blocked or ignored). 838 * 839 * Other ignored signals are discarded immediately. 840 */ 841 void 842 psignal(struct proc *p, int signum) 843 { 844 ptsignal(p, signum, SPROCESS); 845 } 846 847 /* 848 * type = SPROCESS process signal, can be diverted (sigwait()) 849 * XXX if blocked in all threads, mark as pending in struct process 850 * type = STHREAD thread signal, but should be propagated if unhandled 851 * type = SPROPAGATED propagated to this thread, so don't propagate again 852 */ 853 void 854 ptsignal(struct proc *p, int signum, enum signal_type type) 855 { 856 int s, prop; 857 sig_t action; 858 int mask; 859 struct process *pr = p->p_p; 860 struct proc *q; 861 int wakeparent = 0; 862 863 #ifdef DIAGNOSTIC 864 if ((u_int)signum >= NSIG || signum == 0) 865 panic("psignal signal number"); 866 #endif 867 868 /* Ignore signal if the target process is exiting */ 869 if (pr->ps_flags & PS_EXITING) 870 return; 871 872 mask = sigmask(signum); 873 874 if (type == SPROCESS) { 875 /* Accept SIGKILL to coredumping processes */ 876 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 877 if (pr->ps_single != NULL) 878 p = pr->ps_single; 879 atomic_setbits_int(&p->p_siglist, mask); 880 return; 881 } 882 883 /* 884 * If the current thread can process the signal 885 * immediately (it's unblocked) then have it take it. 886 */ 887 q = curproc; 888 if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 889 (q->p_sigmask & mask) == 0) 890 p = q; 891 else { 892 /* 893 * A process-wide signal can be diverted to a 894 * different thread that's in sigwait() for this 895 * signal. If there isn't such a thread, then 896 * pick a thread that doesn't have it blocked so 897 * that the stop/kill consideration isn't 898 * delayed. Otherwise, mark it pending on the 899 * main thread. 900 */ 901 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 902 /* ignore exiting threads */ 903 if (q->p_flag & P_WEXIT) 904 continue; 905 906 /* skip threads that have the signal blocked */ 907 if ((q->p_sigmask & mask) != 0) 908 continue; 909 910 /* okay, could send to this thread */ 911 p = q; 912 913 /* 914 * sigsuspend, sigwait, ppoll/pselect, etc? 915 * Definitely go to this thread, as it's 916 * already blocked in the kernel. 917 */ 918 if (q->p_flag & P_SIGSUSPEND) 919 break; 920 } 921 } 922 } 923 924 if (type != SPROPAGATED) 925 KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum); 926 927 prop = sigprop[signum]; 928 929 /* 930 * If proc is traced, always give parent a chance. 931 */ 932 if (pr->ps_flags & PS_TRACED) { 933 action = SIG_DFL; 934 atomic_setbits_int(&p->p_siglist, mask); 935 } else { 936 /* 937 * If the signal is being ignored, 938 * then we forget about it immediately. 939 * (Note: we don't set SIGCONT in ps_sigignore, 940 * and if it is set to SIG_IGN, 941 * action will be SIG_DFL here.) 942 */ 943 if (pr->ps_sigacts->ps_sigignore & mask) 944 return; 945 if (p->p_sigmask & mask) { 946 action = SIG_HOLD; 947 } else if (pr->ps_sigacts->ps_sigcatch & mask) { 948 action = SIG_CATCH; 949 } else { 950 action = SIG_DFL; 951 952 if (prop & SA_KILL && pr->ps_nice > NZERO) 953 pr->ps_nice = NZERO; 954 955 /* 956 * If sending a tty stop signal to a member of an 957 * orphaned process group, discard the signal here if 958 * the action is default; don't stop the process below 959 * if sleeping, and don't clear any pending SIGCONT. 960 */ 961 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 962 return; 963 } 964 965 atomic_setbits_int(&p->p_siglist, mask); 966 } 967 968 if (prop & SA_CONT) 969 atomic_clearbits_int(&p->p_siglist, stopsigmask); 970 971 if (prop & SA_STOP) { 972 atomic_clearbits_int(&p->p_siglist, contsigmask); 973 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 974 } 975 976 /* 977 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 978 */ 979 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 980 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 981 if (q != p) 982 ptsignal(q, signum, SPROPAGATED); 983 984 /* 985 * Defer further processing for signals which are held, 986 * except that stopped processes must be continued by SIGCONT. 987 */ 988 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 989 return; 990 991 SCHED_LOCK(s); 992 993 switch (p->p_stat) { 994 995 case SSLEEP: 996 /* 997 * If process is sleeping uninterruptibly 998 * we can't interrupt the sleep... the signal will 999 * be noticed when the process returns through 1000 * trap() or syscall(). 1001 */ 1002 if ((p->p_flag & P_SINTR) == 0) 1003 goto out; 1004 /* 1005 * Process is sleeping and traced... make it runnable 1006 * so it can discover the signal in issignal() and stop 1007 * for the parent. 1008 */ 1009 if (pr->ps_flags & PS_TRACED) 1010 goto run; 1011 /* 1012 * If SIGCONT is default (or ignored) and process is 1013 * asleep, we are finished; the process should not 1014 * be awakened. 1015 */ 1016 if ((prop & SA_CONT) && action == SIG_DFL) { 1017 atomic_clearbits_int(&p->p_siglist, mask); 1018 goto out; 1019 } 1020 /* 1021 * When a sleeping process receives a stop 1022 * signal, process immediately if possible. 1023 */ 1024 if ((prop & SA_STOP) && action == SIG_DFL) { 1025 /* 1026 * If a child holding parent blocked, 1027 * stopping could cause deadlock. 1028 */ 1029 if (pr->ps_flags & PS_PPWAIT) 1030 goto out; 1031 atomic_clearbits_int(&p->p_siglist, mask); 1032 p->p_xstat = signum; 1033 proc_stop(p, 0); 1034 goto out; 1035 } 1036 /* 1037 * All other (caught or default) signals 1038 * cause the process to run. 1039 */ 1040 goto runfast; 1041 /*NOTREACHED*/ 1042 1043 case SSTOP: 1044 /* 1045 * If traced process is already stopped, 1046 * then no further action is necessary. 1047 */ 1048 if (pr->ps_flags & PS_TRACED) 1049 goto out; 1050 1051 /* 1052 * Kill signal always sets processes running. 1053 */ 1054 if (signum == SIGKILL) { 1055 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1056 goto runfast; 1057 } 1058 1059 if (prop & SA_CONT) { 1060 /* 1061 * If SIGCONT is default (or ignored), we continue the 1062 * process but don't leave the signal in p_siglist, as 1063 * it has no further action. If SIGCONT is held, we 1064 * continue the process and leave the signal in 1065 * p_siglist. If the process catches SIGCONT, let it 1066 * handle the signal itself. If it isn't waiting on 1067 * an event, then it goes back to run state. 1068 * Otherwise, process goes back to sleep state. 1069 */ 1070 atomic_setbits_int(&p->p_flag, P_CONTINUED); 1071 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1072 wakeparent = 1; 1073 if (action == SIG_DFL) 1074 atomic_clearbits_int(&p->p_siglist, mask); 1075 if (action == SIG_CATCH) 1076 goto runfast; 1077 if (p->p_wchan == 0) 1078 goto run; 1079 p->p_stat = SSLEEP; 1080 goto out; 1081 } 1082 1083 if (prop & SA_STOP) { 1084 /* 1085 * Already stopped, don't need to stop again. 1086 * (If we did the shell could get confused.) 1087 */ 1088 atomic_clearbits_int(&p->p_siglist, mask); 1089 goto out; 1090 } 1091 1092 /* 1093 * If process is sleeping interruptibly, then simulate a 1094 * wakeup so that when it is continued, it will be made 1095 * runnable and can look at the signal. But don't make 1096 * the process runnable, leave it stopped. 1097 */ 1098 if (p->p_wchan && p->p_flag & P_SINTR) 1099 unsleep(p); 1100 goto out; 1101 1102 case SONPROC: 1103 signotify(p); 1104 /* FALLTHROUGH */ 1105 default: 1106 /* 1107 * SRUN, SIDL, SDEAD do nothing with the signal, 1108 * other than kicking ourselves if we are running. 1109 * It will either never be noticed, or noticed very soon. 1110 */ 1111 goto out; 1112 } 1113 /*NOTREACHED*/ 1114 1115 runfast: 1116 /* 1117 * Raise priority to at least PUSER. 1118 */ 1119 if (p->p_priority > PUSER) 1120 p->p_priority = PUSER; 1121 run: 1122 setrunnable(p); 1123 out: 1124 SCHED_UNLOCK(s); 1125 if (wakeparent) 1126 wakeup(pr->ps_pptr); 1127 } 1128 1129 /* 1130 * If the current process has received a signal (should be caught or cause 1131 * termination, should interrupt current syscall), return the signal number. 1132 * Stop signals with default action are processed immediately, then cleared; 1133 * they aren't returned. This is checked after each entry to the system for 1134 * a syscall or trap (though this can usually be done without calling issignal 1135 * by checking the pending signal masks in the CURSIG macro.) The normal call 1136 * sequence is 1137 * 1138 * while (signum = CURSIG(curproc)) 1139 * postsig(signum); 1140 * 1141 * Assumes that if the P_SINTR flag is set, we're holding both the 1142 * kernel and scheduler locks. 1143 */ 1144 int 1145 issignal(struct proc *p) 1146 { 1147 struct process *pr = p->p_p; 1148 int signum, mask, prop; 1149 int dolock = (p->p_flag & P_SINTR) == 0; 1150 int s; 1151 1152 for (;;) { 1153 mask = p->p_siglist & ~p->p_sigmask; 1154 if (pr->ps_flags & PS_PPWAIT) 1155 mask &= ~stopsigmask; 1156 if (mask == 0) /* no signal to send */ 1157 return (0); 1158 signum = ffs((long)mask); 1159 mask = sigmask(signum); 1160 atomic_clearbits_int(&p->p_siglist, mask); 1161 1162 /* 1163 * We should see pending but ignored signals 1164 * only if PS_TRACED was on when they were posted. 1165 */ 1166 if (mask & pr->ps_sigacts->ps_sigignore && 1167 (pr->ps_flags & PS_TRACED) == 0) 1168 continue; 1169 1170 /* 1171 * If traced, always stop, and stay stopped until released 1172 * by the debugger. If our parent process is waiting for 1173 * us, don't hang as we could deadlock. 1174 */ 1175 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1176 signum != SIGKILL) { 1177 p->p_xstat = signum; 1178 1179 if (dolock) 1180 KERNEL_LOCK(); 1181 single_thread_set(p, SINGLE_PTRACE, 0); 1182 if (dolock) 1183 KERNEL_UNLOCK(); 1184 1185 if (dolock) 1186 SCHED_LOCK(s); 1187 proc_stop(p, 1); 1188 if (dolock) 1189 SCHED_UNLOCK(s); 1190 1191 if (dolock) 1192 KERNEL_LOCK(); 1193 single_thread_clear(p, 0); 1194 if (dolock) 1195 KERNEL_UNLOCK(); 1196 1197 /* 1198 * If we are no longer being traced, or the parent 1199 * didn't give us a signal, look for more signals. 1200 */ 1201 if ((pr->ps_flags & PS_TRACED) == 0 || p->p_xstat == 0) 1202 continue; 1203 1204 /* 1205 * If the new signal is being masked, look for other 1206 * signals. 1207 */ 1208 signum = p->p_xstat; 1209 mask = sigmask(signum); 1210 if ((p->p_sigmask & mask) != 0) 1211 continue; 1212 1213 /* take the signal! */ 1214 atomic_clearbits_int(&p->p_siglist, mask); 1215 } 1216 1217 prop = sigprop[signum]; 1218 1219 /* 1220 * Decide whether the signal should be returned. 1221 * Return the signal's number, or fall through 1222 * to clear it from the pending mask. 1223 */ 1224 switch ((long)pr->ps_sigacts->ps_sigact[signum]) { 1225 case (long)SIG_DFL: 1226 /* 1227 * Don't take default actions on system processes. 1228 */ 1229 if (pr->ps_pid <= 1) { 1230 #ifdef DIAGNOSTIC 1231 /* 1232 * Are you sure you want to ignore SIGSEGV 1233 * in init? XXX 1234 */ 1235 printf("Process (pid %d) got signal" 1236 " %d\n", pr->ps_pid, signum); 1237 #endif 1238 break; /* == ignore */ 1239 } 1240 /* 1241 * If there is a pending stop signal to process 1242 * with default action, stop here, 1243 * then clear the signal. However, 1244 * if process is member of an orphaned 1245 * process group, ignore tty stop signals. 1246 */ 1247 if (prop & SA_STOP) { 1248 if (pr->ps_flags & PS_TRACED || 1249 (pr->ps_pgrp->pg_jobc == 0 && 1250 prop & SA_TTYSTOP)) 1251 break; /* == ignore */ 1252 p->p_xstat = signum; 1253 if (dolock) 1254 SCHED_LOCK(s); 1255 proc_stop(p, 1); 1256 if (dolock) 1257 SCHED_UNLOCK(s); 1258 break; 1259 } else if (prop & SA_IGNORE) { 1260 /* 1261 * Except for SIGCONT, shouldn't get here. 1262 * Default action is to ignore; drop it. 1263 */ 1264 break; /* == ignore */ 1265 } else 1266 goto keep; 1267 /*NOTREACHED*/ 1268 case (long)SIG_IGN: 1269 /* 1270 * Masking above should prevent us ever trying 1271 * to take action on an ignored signal other 1272 * than SIGCONT, unless process is traced. 1273 */ 1274 if ((prop & SA_CONT) == 0 && 1275 (pr->ps_flags & PS_TRACED) == 0) 1276 printf("issignal\n"); 1277 break; /* == ignore */ 1278 default: 1279 /* 1280 * This signal has an action, let 1281 * postsig() process it. 1282 */ 1283 goto keep; 1284 } 1285 } 1286 /* NOTREACHED */ 1287 1288 keep: 1289 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */ 1290 return (signum); 1291 } 1292 1293 /* 1294 * Put the argument process into the stopped state and notify the parent 1295 * via wakeup. Signals are handled elsewhere. The process must not be 1296 * on the run queue. 1297 */ 1298 void 1299 proc_stop(struct proc *p, int sw) 1300 { 1301 struct process *pr = p->p_p; 1302 extern void *softclock_si; 1303 1304 #ifdef MULTIPROCESSOR 1305 SCHED_ASSERT_LOCKED(); 1306 #endif 1307 1308 p->p_stat = SSTOP; 1309 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1310 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1311 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1312 if (!timeout_pending(&proc_stop_to)) { 1313 timeout_add(&proc_stop_to, 0); 1314 /* 1315 * We need this soft interrupt to be handled fast. 1316 * Extra calls to softclock don't hurt. 1317 */ 1318 softintr_schedule(softclock_si); 1319 } 1320 if (sw) 1321 mi_switch(); 1322 } 1323 1324 /* 1325 * Called from a timeout to send signals to the parents of stopped processes. 1326 * We can't do this in proc_stop because it's called with nasty locks held 1327 * and we would need recursive scheduler lock to deal with that. 1328 */ 1329 void 1330 proc_stop_sweep(void *v) 1331 { 1332 struct process *pr; 1333 1334 LIST_FOREACH(pr, &allprocess, ps_list) { 1335 if ((pr->ps_flags & PS_STOPPED) == 0) 1336 continue; 1337 atomic_clearbits_int(&pr->ps_flags, PS_STOPPED); 1338 1339 if ((pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDSTOP) == 0) 1340 prsignal(pr->ps_pptr, SIGCHLD); 1341 wakeup(pr->ps_pptr); 1342 } 1343 } 1344 1345 /* 1346 * Take the action for the specified signal 1347 * from the current set of pending signals. 1348 */ 1349 void 1350 postsig(struct proc *p, int signum) 1351 { 1352 struct process *pr = p->p_p; 1353 struct sigacts *ps = pr->ps_sigacts; 1354 sig_t action; 1355 u_long trapno; 1356 int mask, returnmask; 1357 union sigval sigval; 1358 int s, code; 1359 1360 KASSERT(signum != 0); 1361 KERNEL_ASSERT_LOCKED(); 1362 1363 mask = sigmask(signum); 1364 atomic_clearbits_int(&p->p_siglist, mask); 1365 action = ps->ps_sigact[signum]; 1366 sigval.sival_ptr = 0; 1367 1368 if (p->p_sisig != signum) { 1369 trapno = 0; 1370 code = SI_USER; 1371 sigval.sival_ptr = 0; 1372 } else { 1373 trapno = p->p_sitrapno; 1374 code = p->p_sicode; 1375 sigval = p->p_sigval; 1376 } 1377 1378 #ifdef KTRACE 1379 if (KTRPOINT(p, KTR_PSIG)) { 1380 siginfo_t si; 1381 1382 initsiginfo(&si, signum, trapno, code, sigval); 1383 ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ? 1384 p->p_oldmask : p->p_sigmask, code, &si); 1385 } 1386 #endif 1387 if (action == SIG_DFL) { 1388 /* 1389 * Default action, where the default is to kill 1390 * the process. (Other cases were ignored above.) 1391 */ 1392 sigexit(p, signum); 1393 /* NOTREACHED */ 1394 } else { 1395 /* 1396 * If we get here, the signal must be caught. 1397 */ 1398 #ifdef DIAGNOSTIC 1399 if (action == SIG_IGN || (p->p_sigmask & mask)) 1400 panic("postsig action"); 1401 #endif 1402 /* 1403 * Set the new mask value and also defer further 1404 * occurrences of this signal. 1405 * 1406 * Special case: user has done a sigpause. Here the 1407 * current mask is not of interest, but rather the 1408 * mask from before the sigpause is what we want 1409 * restored after the signal processing is completed. 1410 */ 1411 #ifdef MULTIPROCESSOR 1412 s = splsched(); 1413 #else 1414 s = splhigh(); 1415 #endif 1416 if (p->p_flag & P_SIGSUSPEND) { 1417 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1418 returnmask = p->p_oldmask; 1419 } else { 1420 returnmask = p->p_sigmask; 1421 } 1422 if (p->p_sisig == signum) { 1423 p->p_sisig = 0; 1424 p->p_sitrapno = 0; 1425 p->p_sicode = SI_USER; 1426 p->p_sigval.sival_ptr = NULL; 1427 } 1428 1429 (*pr->ps_emul->e_sendsig)(action, signum, returnmask, trapno, 1430 code, sigval); 1431 postsig_done(p, signum, ps); 1432 splx(s); 1433 } 1434 } 1435 1436 /* 1437 * Force the current process to exit with the specified signal, dumping core 1438 * if appropriate. We bypass the normal tests for masked and caught signals, 1439 * allowing unrecoverable failures to terminate the process without changing 1440 * signal state. Mark the accounting record with the signal termination. 1441 * If dumping core, save the signal number for the debugger. Calls exit and 1442 * does not return. 1443 */ 1444 void 1445 sigexit(struct proc *p, int signum) 1446 { 1447 /* Mark process as going away */ 1448 atomic_setbits_int(&p->p_flag, P_WEXIT); 1449 1450 p->p_p->ps_acflag |= AXSIG; 1451 if (sigprop[signum] & SA_CORE) { 1452 p->p_sisig = signum; 1453 1454 /* if there are other threads, pause them */ 1455 if (P_HASSIBLING(p)) 1456 single_thread_set(p, SINGLE_SUSPEND, 0); 1457 1458 if (coredump(p) == 0) 1459 signum |= WCOREFLAG; 1460 } 1461 exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL); 1462 /* NOTREACHED */ 1463 } 1464 1465 int nosuidcoredump = 1; 1466 1467 struct coredump_iostate { 1468 struct proc *io_proc; 1469 struct vnode *io_vp; 1470 struct ucred *io_cred; 1471 off_t io_offset; 1472 }; 1473 1474 /* 1475 * Dump core, into a file named "progname.core", unless the process was 1476 * setuid/setgid. 1477 */ 1478 int 1479 coredump(struct proc *p) 1480 { 1481 #ifdef SMALL_KERNEL 1482 return EPERM; 1483 #else 1484 struct process *pr = p->p_p; 1485 struct vnode *vp; 1486 struct ucred *cred = p->p_ucred; 1487 struct vmspace *vm = p->p_vmspace; 1488 struct nameidata nd; 1489 struct vattr vattr; 1490 struct coredump_iostate io; 1491 int error, len, incrash = 0; 1492 char name[MAXPATHLEN]; 1493 const char *dir = "/var/crash"; 1494 1495 if (pr->ps_emul->e_coredump == NULL) 1496 return (EINVAL); 1497 1498 pr->ps_flags |= PS_COREDUMP; 1499 1500 /* 1501 * If the process has inconsistent uids, nosuidcoredump 1502 * determines coredump placement policy. 1503 */ 1504 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1505 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1506 if (nosuidcoredump == 3 || nosuidcoredump == 2) 1507 incrash = 1; 1508 else 1509 return (EPERM); 1510 } 1511 1512 /* Don't dump if will exceed file size limit. */ 1513 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= 1514 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1515 return (EFBIG); 1516 1517 if (incrash && nosuidcoredump == 3) { 1518 /* 1519 * If the program directory does not exist, dumps of 1520 * that core will silently fail. 1521 */ 1522 len = snprintf(name, sizeof(name), "%s/%s/%u.core", 1523 dir, pr->ps_comm, pr->ps_pid); 1524 } else if (incrash && nosuidcoredump == 2) 1525 len = snprintf(name, sizeof(name), "%s/%s.core", 1526 dir, pr->ps_comm); 1527 else 1528 len = snprintf(name, sizeof(name), "%s.core", pr->ps_comm); 1529 if (len >= sizeof(name)) 1530 return (EACCES); 1531 1532 /* 1533 * Control the UID used to write out. The normal case uses 1534 * the real UID. If the sugid case is going to write into the 1535 * controlled directory, we do so as root. 1536 */ 1537 if (incrash == 0) { 1538 cred = crdup(cred); 1539 cred->cr_uid = cred->cr_ruid; 1540 cred->cr_gid = cred->cr_rgid; 1541 } else { 1542 if (p->p_fd->fd_rdir) { 1543 vrele(p->p_fd->fd_rdir); 1544 p->p_fd->fd_rdir = NULL; 1545 } 1546 p->p_ucred = crdup(p->p_ucred); 1547 crfree(cred); 1548 cred = p->p_ucred; 1549 crhold(cred); 1550 cred->cr_uid = 0; 1551 cred->cr_gid = 0; 1552 } 1553 1554 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1555 1556 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR); 1557 1558 if (error) 1559 goto out; 1560 1561 /* 1562 * Don't dump to non-regular files, files with links, or files 1563 * owned by someone else. 1564 */ 1565 vp = nd.ni_vp; 1566 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1567 VOP_UNLOCK(vp, p); 1568 vn_close(vp, FWRITE, cred, p); 1569 goto out; 1570 } 1571 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1572 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1573 vattr.va_uid != cred->cr_uid) { 1574 error = EACCES; 1575 VOP_UNLOCK(vp, p); 1576 vn_close(vp, FWRITE, cred, p); 1577 goto out; 1578 } 1579 VATTR_NULL(&vattr); 1580 vattr.va_size = 0; 1581 VOP_SETATTR(vp, &vattr, cred, p); 1582 pr->ps_acflag |= ACORE; 1583 1584 io.io_proc = p; 1585 io.io_vp = vp; 1586 io.io_cred = cred; 1587 io.io_offset = 0; 1588 VOP_UNLOCK(vp, p); 1589 vref(vp); 1590 error = vn_close(vp, FWRITE, cred, p); 1591 if (error == 0) 1592 error = (*pr->ps_emul->e_coredump)(p, &io); 1593 vrele(vp); 1594 out: 1595 crfree(cred); 1596 return (error); 1597 #endif 1598 } 1599 1600 #ifndef SMALL_KERNEL 1601 int 1602 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 1603 { 1604 struct coredump_iostate *io = cookie; 1605 off_t coffset = 0; 1606 size_t csize; 1607 int chunk, error; 1608 1609 csize = len; 1610 do { 1611 if (io->io_proc->p_siglist & sigmask(SIGKILL)) 1612 return (EINTR); 1613 1614 /* Rest of the loop sleeps with lock held, so... */ 1615 yield(); 1616 1617 chunk = MIN(csize, MAXPHYS); 1618 error = vn_rdwr(UIO_WRITE, io->io_vp, 1619 (caddr_t)data + coffset, chunk, 1620 io->io_offset + coffset, segflg, 1621 IO_UNIT, io->io_cred, NULL, io->io_proc); 1622 if (error) { 1623 struct process *pr = io->io_proc->p_p; 1624 if (error == ENOSPC) 1625 log(LOG_ERR, "coredump of %s(%d) failed, filesystem full\n", 1626 pr->ps_comm, pr->ps_pid); 1627 else 1628 log(LOG_ERR, "coredump of %s(%d), write failed: errno %d\n", 1629 pr->ps_comm, pr->ps_pid, error); 1630 return (error); 1631 } 1632 1633 coffset += chunk; 1634 csize -= chunk; 1635 } while (csize > 0); 1636 1637 io->io_offset += len; 1638 return (0); 1639 } 1640 1641 void 1642 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1643 { 1644 struct coredump_iostate *io = cookie; 1645 1646 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1647 } 1648 1649 #endif /* !SMALL_KERNEL */ 1650 1651 /* 1652 * Nonexistent system call-- signal process (may want to handle it). 1653 * Flag error in case process won't see signal immediately (blocked or ignored). 1654 */ 1655 int 1656 sys_nosys(struct proc *p, void *v, register_t *retval) 1657 { 1658 1659 ptsignal(p, SIGSYS, STHREAD); 1660 return (ENOSYS); 1661 } 1662 1663 int 1664 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1665 { 1666 static int sigwaitsleep; 1667 struct sys___thrsigdivert_args /* { 1668 syscallarg(sigset_t) sigmask; 1669 syscallarg(siginfo_t *) info; 1670 syscallarg(const struct timespec *) timeout; 1671 } */ *uap = v; 1672 struct process *pr = p->p_p; 1673 sigset_t *m; 1674 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1675 siginfo_t si; 1676 uint64_t to_ticks = 0; 1677 int timeinvalid = 0; 1678 int error = 0; 1679 1680 memset(&si, 0, sizeof(si)); 1681 1682 if (SCARG(uap, timeout) != NULL) { 1683 struct timespec ts; 1684 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1685 return (error); 1686 #ifdef KTRACE 1687 if (KTRPOINT(p, KTR_STRUCT)) 1688 ktrreltimespec(p, &ts); 1689 #endif 1690 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) 1691 timeinvalid = 1; 1692 else { 1693 to_ticks = (uint64_t)hz * ts.tv_sec + 1694 ts.tv_nsec / (tick * 1000); 1695 if (to_ticks > INT_MAX) 1696 to_ticks = INT_MAX; 1697 if (to_ticks == 0 && ts.tv_nsec) 1698 to_ticks = 1; 1699 } 1700 } 1701 1702 dosigsuspend(p, p->p_sigmask &~ mask); 1703 for (;;) { 1704 si.si_signo = CURSIG(p); 1705 if (si.si_signo != 0) { 1706 sigset_t smask = sigmask(si.si_signo); 1707 if (smask & mask) { 1708 if (p->p_siglist & smask) 1709 m = &p->p_siglist; 1710 else if (pr->ps_mainproc->p_siglist & smask) 1711 m = &pr->ps_mainproc->p_siglist; 1712 else { 1713 /* signal got eaten by someone else? */ 1714 continue; 1715 } 1716 atomic_clearbits_int(m, smask); 1717 error = 0; 1718 break; 1719 } 1720 } 1721 1722 /* per-POSIX, delay this error until after the above */ 1723 if (timeinvalid) 1724 error = EINVAL; 1725 1726 if (SCARG(uap, timeout) != NULL && to_ticks == 0) 1727 error = EAGAIN; 1728 1729 if (error != 0) 1730 break; 1731 1732 error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait", 1733 (int)to_ticks); 1734 } 1735 1736 if (error == 0) { 1737 *retval = si.si_signo; 1738 if (SCARG(uap, info) != NULL) 1739 error = copyout(&si, SCARG(uap, info), sizeof(si)); 1740 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) { 1741 /* 1742 * Restarting is wrong if there's a timeout, as it'll be 1743 * for the same interval again 1744 */ 1745 error = EINTR; 1746 } 1747 1748 return (error); 1749 } 1750 1751 void 1752 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val) 1753 { 1754 memset(si, 0, sizeof(*si)); 1755 1756 si->si_signo = sig; 1757 si->si_code = code; 1758 if (code == SI_USER) { 1759 si->si_value = val; 1760 } else { 1761 switch (sig) { 1762 case SIGSEGV: 1763 case SIGILL: 1764 case SIGBUS: 1765 case SIGFPE: 1766 si->si_addr = val.sival_ptr; 1767 si->si_trapno = trapno; 1768 break; 1769 case SIGXFSZ: 1770 break; 1771 } 1772 } 1773 } 1774 1775 int 1776 filt_sigattach(struct knote *kn) 1777 { 1778 struct process *pr = curproc->p_p; 1779 1780 if (kn->kn_id >= NSIG) 1781 return EINVAL; 1782 1783 kn->kn_ptr.p_process = pr; 1784 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1785 1786 /* XXX lock the proc here while adding to the list? */ 1787 SLIST_INSERT_HEAD(&pr->ps_klist, kn, kn_selnext); 1788 1789 return (0); 1790 } 1791 1792 void 1793 filt_sigdetach(struct knote *kn) 1794 { 1795 struct process *pr = kn->kn_ptr.p_process; 1796 1797 SLIST_REMOVE(&pr->ps_klist, kn, knote, kn_selnext); 1798 } 1799 1800 /* 1801 * signal knotes are shared with proc knotes, so we apply a mask to 1802 * the hint in order to differentiate them from process hints. This 1803 * could be avoided by using a signal-specific knote list, but probably 1804 * isn't worth the trouble. 1805 */ 1806 int 1807 filt_signal(struct knote *kn, long hint) 1808 { 1809 1810 if (hint & NOTE_SIGNAL) { 1811 hint &= ~NOTE_SIGNAL; 1812 1813 if (kn->kn_id == hint) 1814 kn->kn_data++; 1815 } 1816 return (kn->kn_data != 0); 1817 } 1818 1819 void 1820 userret(struct proc *p) 1821 { 1822 int signum; 1823 1824 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */ 1825 if (p->p_flag & P_PROFPEND) { 1826 atomic_clearbits_int(&p->p_flag, P_PROFPEND); 1827 KERNEL_LOCK(); 1828 psignal(p, SIGPROF); 1829 KERNEL_UNLOCK(); 1830 } 1831 if (p->p_flag & P_ALRMPEND) { 1832 atomic_clearbits_int(&p->p_flag, P_ALRMPEND); 1833 KERNEL_LOCK(); 1834 psignal(p, SIGVTALRM); 1835 KERNEL_UNLOCK(); 1836 } 1837 1838 if (SIGPENDING(p)) { 1839 KERNEL_LOCK(); 1840 while ((signum = CURSIG(p)) != 0) 1841 postsig(p, signum); 1842 KERNEL_UNLOCK(); 1843 } 1844 1845 /* 1846 * If P_SIGSUSPEND is still set here, then we still need to restore 1847 * the original sigmask before returning to userspace. Also, this 1848 * might unmask some pending signals, so we need to check a second 1849 * time for signals to post. 1850 */ 1851 if (p->p_flag & P_SIGSUSPEND) { 1852 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1853 p->p_sigmask = p->p_oldmask; 1854 1855 KERNEL_LOCK(); 1856 while ((signum = CURSIG(p)) != 0) 1857 postsig(p, signum); 1858 KERNEL_UNLOCK(); 1859 } 1860 1861 if (p->p_flag & P_SUSPSINGLE) { 1862 KERNEL_LOCK(); 1863 single_thread_check(p, 0); 1864 KERNEL_UNLOCK(); 1865 } 1866 1867 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning"); 1868 1869 p->p_cpu->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri; 1870 } 1871 1872 int 1873 single_thread_check(struct proc *p, int deep) 1874 { 1875 struct process *pr = p->p_p; 1876 1877 if (pr->ps_single != NULL && pr->ps_single != p) { 1878 do { 1879 int s; 1880 1881 /* if we're in deep, we need to unwind to the edge */ 1882 if (deep) { 1883 if (pr->ps_flags & PS_SINGLEUNWIND) 1884 return (ERESTART); 1885 if (pr->ps_flags & PS_SINGLEEXIT) 1886 return (EINTR); 1887 } 1888 1889 if (--pr->ps_singlecount == 0) 1890 wakeup(&pr->ps_singlecount); 1891 if (pr->ps_flags & PS_SINGLEEXIT) 1892 exit1(p, 0, EXIT_THREAD_NOCHECK); 1893 1894 /* not exiting and don't need to unwind, so suspend */ 1895 SCHED_LOCK(s); 1896 p->p_stat = SSTOP; 1897 mi_switch(); 1898 SCHED_UNLOCK(s); 1899 } while (pr->ps_single != NULL); 1900 } 1901 1902 return (0); 1903 } 1904 1905 /* 1906 * Stop other threads in the process. The mode controls how and 1907 * where the other threads should stop: 1908 * - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit 1909 * (by setting to SINGLE_EXIT) or be released (via single_thread_clear()) 1910 * - SINGLE_PTRACE: stop wherever they are, will wait for them to stop 1911 * later (via single_thread_wait()) and released as with SINGLE_SUSPEND 1912 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit 1913 * or released as with SINGLE_SUSPEND 1914 * - SINGLE_EXIT: unwind to kernel boundary and exit 1915 */ 1916 int 1917 single_thread_set(struct proc *p, enum single_thread_mode mode, int deep) 1918 { 1919 struct process *pr = p->p_p; 1920 struct proc *q; 1921 int error; 1922 1923 KERNEL_ASSERT_LOCKED(); 1924 1925 if ((error = single_thread_check(p, deep))) 1926 return error; 1927 1928 switch (mode) { 1929 case SINGLE_SUSPEND: 1930 case SINGLE_PTRACE: 1931 break; 1932 case SINGLE_UNWIND: 1933 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 1934 break; 1935 case SINGLE_EXIT: 1936 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT); 1937 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 1938 break; 1939 #ifdef DIAGNOSTIC 1940 default: 1941 panic("single_thread_mode = %d", mode); 1942 #endif 1943 } 1944 pr->ps_single = p; 1945 pr->ps_singlecount = 0; 1946 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 1947 int s; 1948 1949 if (q == p) 1950 continue; 1951 if (q->p_flag & P_WEXIT) { 1952 if (mode == SINGLE_EXIT) { 1953 SCHED_LOCK(s); 1954 if (q->p_stat == SSTOP) { 1955 setrunnable(q); 1956 pr->ps_singlecount++; 1957 } 1958 SCHED_UNLOCK(s); 1959 } 1960 continue; 1961 } 1962 SCHED_LOCK(s); 1963 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE); 1964 switch (q->p_stat) { 1965 case SIDL: 1966 case SRUN: 1967 pr->ps_singlecount++; 1968 break; 1969 case SSLEEP: 1970 /* if it's not interruptible, then just have to wait */ 1971 if (q->p_flag & P_SINTR) { 1972 /* merely need to suspend? just stop it */ 1973 if (mode == SINGLE_SUSPEND || 1974 mode == SINGLE_PTRACE) { 1975 q->p_stat = SSTOP; 1976 break; 1977 } 1978 /* need to unwind or exit, so wake it */ 1979 setrunnable(q); 1980 } 1981 pr->ps_singlecount++; 1982 break; 1983 case SSTOP: 1984 if (mode == SINGLE_EXIT) { 1985 setrunnable(q); 1986 pr->ps_singlecount++; 1987 } 1988 break; 1989 case SDEAD: 1990 break; 1991 case SONPROC: 1992 pr->ps_singlecount++; 1993 signotify(q); 1994 break; 1995 } 1996 SCHED_UNLOCK(s); 1997 } 1998 1999 if (mode != SINGLE_PTRACE) 2000 single_thread_wait(pr); 2001 2002 return 0; 2003 } 2004 2005 void 2006 single_thread_wait(struct process *pr) 2007 { 2008 /* wait until they're all suspended */ 2009 while (pr->ps_singlecount > 0) 2010 tsleep(&pr->ps_singlecount, PUSER, "suspend", 0); 2011 } 2012 2013 void 2014 single_thread_clear(struct proc *p, int flag) 2015 { 2016 struct process *pr = p->p_p; 2017 struct proc *q; 2018 2019 KASSERT(pr->ps_single == p); 2020 KERNEL_ASSERT_LOCKED(); 2021 2022 pr->ps_single = NULL; 2023 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); 2024 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2025 int s; 2026 2027 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0) 2028 continue; 2029 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE); 2030 2031 /* 2032 * if the thread was only stopped for single threading 2033 * then clearing that either makes it runnable or puts 2034 * it back into some sleep queue 2035 */ 2036 SCHED_LOCK(s); 2037 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) { 2038 if (q->p_wchan == 0) 2039 setrunnable(q); 2040 else 2041 q->p_stat = SSLEEP; 2042 } 2043 SCHED_UNLOCK(s); 2044 } 2045 } 2046