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