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