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