390#ifndef COMPAT_SUNOS 391 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP) 392 sv->sv_flags |= SV_NOCLDSTOP; 393#endif 394 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv, 395 sizeof (vec)))) 396 return (error); 397 } 398 if (uap->nsv) { 399 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 400 sizeof (vec)))) 401 return (error); 402 if ((signum == SIGKILL || signum == SIGSTOP) && 403 sv->sv_handler != SIG_DFL) 404 return (EINVAL); 405#ifdef COMPAT_SUNOS 406 sv->sv_flags |= SA_USERTRAMP; 407#endif 408 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 409 setsigvec(p, signum, (struct sigaction *)sv); 410 } 411 return (0); 412} 413 414#ifndef _SYS_SYSPROTO_H_ 415struct osigblock_args { 416 int mask; 417}; 418#endif 419int 420osigblock(p, uap) 421 register struct proc *p; 422 struct osigblock_args *uap; 423{ 424 425 (void) splhigh(); 426 p->p_retval[0] = p->p_sigmask; 427 p->p_sigmask |= uap->mask &~ sigcantmask; 428 (void) spl0(); 429 return (0); 430} 431 432#ifndef _SYS_SYSPROTO_H_ 433struct osigsetmask_args { 434 int mask; 435}; 436#endif 437int 438osigsetmask(p, uap) 439 struct proc *p; 440 struct osigsetmask_args *uap; 441{ 442 443 (void) splhigh(); 444 p->p_retval[0] = p->p_sigmask; 445 p->p_sigmask = uap->mask &~ sigcantmask; 446 (void) spl0(); 447 return (0); 448} 449#endif /* COMPAT_43 || COMPAT_SUNOS */ 450 451/* 452 * Suspend process until signal, providing mask to be set 453 * in the meantime. Note nonstandard calling convention: 454 * libc stub passes mask, not pointer, to save a copyin. 455 */ 456#ifndef _SYS_SYSPROTO_H_ 457struct sigsuspend_args { 458 sigset_t mask; 459}; 460#endif 461/* ARGSUSED */ 462int 463sigsuspend(p, uap) 464 register struct proc *p; 465 struct sigsuspend_args *uap; 466{ 467 register struct sigacts *ps = p->p_sigacts; 468 469 /* 470 * When returning from sigpause, we want 471 * the old mask to be restored after the 472 * signal handler has finished. Thus, we 473 * save it here and mark the sigacts structure 474 * to indicate this. 475 */ 476 p->p_oldsigmask = p->p_sigmask; 477 p->p_sigmask = uap->mask &~ sigcantmask; 478 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 479 /* void */; 480 /* always return EINTR rather than ERESTART... */ 481 return (EINTR); 482} 483 484#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 485#ifndef _SYS_SYSPROTO_H_ 486struct osigstack_args { 487 struct sigstack *nss; 488 struct sigstack *oss; 489}; 490#endif 491/* ARGSUSED */ 492int 493osigstack(p, uap) 494 struct proc *p; 495 register struct osigstack_args *uap; 496{ 497 struct sigstack ss; 498 struct sigacts *psp; 499 int error = 0; 500 501 psp = p->p_sigacts; 502 ss.ss_sp = psp->ps_sigstk.ss_sp; 503 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 504 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss, 505 sizeof (struct sigstack)))) 506 return (error); 507 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 508 sizeof (ss))) == 0) { 509 psp->ps_sigstk.ss_sp = ss.ss_sp; 510 psp->ps_sigstk.ss_size = 0; 511 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 512 psp->ps_flags |= SAS_ALTSTACK; 513 } 514 return (error); 515} 516#endif /* COMPAT_43 || COMPAT_SUNOS */ 517 518#ifndef _SYS_SYSPROTO_H_ 519struct sigaltstack_args { 520 struct sigaltstack *nss; 521 struct sigaltstack *oss; 522}; 523#endif 524/* ARGSUSED */ 525int 526sigaltstack(p, uap) 527 struct proc *p; 528 register struct sigaltstack_args *uap; 529{ 530 struct sigacts *psp; 531 struct sigaltstack ss; 532 int error; 533 534 psp = p->p_sigacts; 535 if ((psp->ps_flags & SAS_ALTSTACK) == 0) 536 psp->ps_sigstk.ss_flags |= SS_DISABLE; 537 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk, 538 (caddr_t)uap->oss, sizeof (struct sigaltstack)))) 539 return (error); 540 if (uap->nss == 0) 541 return (0); 542 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))) 543 return (error); 544 if (ss.ss_flags & SS_DISABLE) { 545 if (psp->ps_sigstk.ss_flags & SS_ONSTACK) 546 return (EINVAL); 547 psp->ps_flags &= ~SAS_ALTSTACK; 548 psp->ps_sigstk.ss_flags = ss.ss_flags; 549 return (0); 550 } 551 if (ss.ss_size < MINSIGSTKSZ) 552 return (ENOMEM); 553 psp->ps_flags |= SAS_ALTSTACK; 554 psp->ps_sigstk= ss; 555 return (0); 556} 557 558/* 559 * Common code for kill process group/broadcast kill. 560 * cp is calling process. 561 */ 562int 563killpg1(cp, signum, pgid, all) 564 register struct proc *cp; 565 int signum, pgid, all; 566{ 567 register struct proc *p; 568 register struct pcred *pc = cp->p_cred; 569 struct pgrp *pgrp; 570 int nfound = 0; 571 572 if (all) 573 /* 574 * broadcast 575 */ 576 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 577 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 578 p == cp || !CANSIGNAL(cp, pc, p, signum)) 579 continue; 580 nfound++; 581 if (signum) 582 psignal(p, signum); 583 } 584 else { 585 if (pgid == 0) 586 /* 587 * zero pgid means send to my process group. 588 */ 589 pgrp = cp->p_pgrp; 590 else { 591 pgrp = pgfind(pgid); 592 if (pgrp == NULL) 593 return (ESRCH); 594 } 595 for (p = pgrp->pg_members.lh_first; p != 0; 596 p = p->p_pglist.le_next) { 597 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 598 p->p_stat == SZOMB || 599 !CANSIGNAL(cp, pc, p, signum)) 600 continue; 601 nfound++; 602 if (signum) 603 psignal(p, signum); 604 } 605 } 606 return (nfound ? 0 : ESRCH); 607} 608 609#ifndef _SYS_SYSPROTO_H_ 610struct kill_args { 611 int pid; 612 int signum; 613}; 614#endif 615/* ARGSUSED */ 616int 617kill(cp, uap) 618 register struct proc *cp; 619 register struct kill_args *uap; 620{ 621 register struct proc *p; 622 register struct pcred *pc = cp->p_cred; 623 624 if ((u_int)uap->signum >= NSIG) 625 return (EINVAL); 626 if (uap->pid > 0) { 627 /* kill single process */ 628 if ((p = pfind(uap->pid)) == NULL) 629 return (ESRCH); 630 if (!CANSIGNAL(cp, pc, p, uap->signum)) 631 return (EPERM); 632 if (uap->signum) 633 psignal(p, uap->signum); 634 return (0); 635 } 636 switch (uap->pid) { 637 case -1: /* broadcast signal */ 638 return (killpg1(cp, uap->signum, 0, 1)); 639 case 0: /* signal own process group */ 640 return (killpg1(cp, uap->signum, 0, 0)); 641 default: /* negative explicit process group */ 642 return (killpg1(cp, uap->signum, -uap->pid, 0)); 643 } 644 /* NOTREACHED */ 645} 646 647#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 648#ifndef _SYS_SYSPROTO_H_ 649struct okillpg_args { 650 int pgid; 651 int signum; 652}; 653#endif 654/* ARGSUSED */ 655int 656okillpg(p, uap) 657 struct proc *p; 658 register struct okillpg_args *uap; 659{ 660 661 if ((u_int)uap->signum >= NSIG) 662 return (EINVAL); 663 return (killpg1(p, uap->signum, uap->pgid, 0)); 664} 665#endif /* COMPAT_43 || COMPAT_SUNOS */ 666 667/* 668 * Send a signal to a process group. 669 */ 670void 671gsignal(pgid, signum) 672 int pgid, signum; 673{ 674 struct pgrp *pgrp; 675 676 if (pgid && (pgrp = pgfind(pgid))) 677 pgsignal(pgrp, signum, 0); 678} 679 680/* 681 * Send a signal to a process group. If checktty is 1, 682 * limit to members which have a controlling terminal. 683 */ 684void 685pgsignal(pgrp, signum, checkctty) 686 struct pgrp *pgrp; 687 int signum, checkctty; 688{ 689 register struct proc *p; 690 691 if (pgrp) 692 for (p = pgrp->pg_members.lh_first; p != 0; 693 p = p->p_pglist.le_next) 694 if (checkctty == 0 || p->p_flag & P_CONTROLT) 695 psignal(p, signum); 696} 697 698/* 699 * Send a signal caused by a trap to the current process. 700 * If it will be caught immediately, deliver it with correct code. 701 * Otherwise, post it normally. 702 */ 703void 704trapsignal(p, signum, code) 705 struct proc *p; 706 register int signum; 707 u_long code; 708{ 709 register struct sigacts *ps = p->p_sigacts; 710 int mask; 711 712 mask = sigmask(signum); 713 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 && 714 (p->p_sigmask & mask) == 0) { 715 p->p_stats->p_ru.ru_nsignals++; 716#ifdef KTRACE 717 if (KTRPOINT(p, KTR_PSIG)) 718 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum], 719 p->p_sigmask, code); 720#endif 721 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum, 722 p->p_sigmask, code); 723 p->p_sigmask |= ps->ps_catchmask[signum] | 724 (mask & ~ps->ps_signodefer); 725 if ((ps->ps_sigreset & mask) != 0) { 726 /* 727 * See setsigvec() for origin of this code. 728 */ 729 p->p_sigcatch &= ~mask; 730 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 731 p->p_sigignore |= mask; 732 ps->ps_sigact[signum] = SIG_DFL; 733 } 734 } else { 735 p->p_code = code; /* XXX for core dump/debugger */ 736 p->p_sig = signum; /* XXX to verify code */ 737 psignal(p, signum); 738 } 739} 740 741/* 742 * Send the signal to the process. If the signal has an action, the action 743 * is usually performed by the target process rather than the caller; we add 744 * the signal to the set of pending signals for the process. 745 * 746 * Exceptions: 747 * o When a stop signal is sent to a sleeping process that takes the 748 * default action, the process is stopped without awakening it. 749 * o SIGCONT restarts stopped processes (or puts them back to sleep) 750 * regardless of the signal action (eg, blocked or ignored). 751 * 752 * Other ignored signals are discarded immediately. 753 */ 754void 755psignal(p, signum) 756 register struct proc *p; 757 register int signum; 758{ 759 register int s, prop; 760 register sig_t action; 761 int mask; 762 763 if ((u_int)signum >= NSIG || signum == 0) { 764 printf("psignal: signum %d\n", signum); 765 panic("psignal signal number"); 766 } 767 mask = sigmask(signum); 768 prop = sigprop[signum]; 769 770 /* 771 * If proc is traced, always give parent a chance; 772 * if signal event is tracked by procfs, give *that* 773 * a chance, as well. 774 */ 775 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 776 action = SIG_DFL; 777 else { 778 /* 779 * If the signal is being ignored, 780 * then we forget about it immediately. 781 * (Note: we don't set SIGCONT in p_sigignore, 782 * and if it is set to SIG_IGN, 783 * action will be SIG_DFL here.) 784 */ 785 if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT)) 786 return; 787 if (p->p_sigmask & mask) 788 action = SIG_HOLD; 789 else if (p->p_sigcatch & mask) 790 action = SIG_CATCH; 791 else 792 action = SIG_DFL; 793 } 794 795 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 796 (p->p_flag & P_TRACED) == 0) 797 p->p_nice = NZERO; 798 799 if (prop & SA_CONT) 800 p->p_siglist &= ~stopsigmask; 801 802 if (prop & SA_STOP) { 803 /* 804 * If sending a tty stop signal to a member of an orphaned 805 * process group, discard the signal here if the action 806 * is default; don't stop the process below if sleeping, 807 * and don't clear any pending SIGCONT. 808 */ 809 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 810 action == SIG_DFL) 811 return; 812 p->p_siglist &= ~contsigmask; 813 } 814 p->p_siglist |= mask; 815 816 /* 817 * Defer further processing for signals which are held, 818 * except that stopped processes must be continued by SIGCONT. 819 */ 820 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 821 return; 822 s = splhigh(); 823 switch (p->p_stat) { 824 825 case SSLEEP: 826 /* 827 * If process is sleeping uninterruptibly 828 * we can't interrupt the sleep... the signal will 829 * be noticed when the process returns through 830 * trap() or syscall(). 831 */ 832 if ((p->p_flag & P_SINTR) == 0) 833 goto out; 834 /* 835 * Process is sleeping and traced... make it runnable 836 * so it can discover the signal in issignal() and stop 837 * for the parent. 838 */ 839 if (p->p_flag & P_TRACED) 840 goto run; 841 /* 842 * If SIGCONT is default (or ignored) and process is 843 * asleep, we are finished; the process should not 844 * be awakened. 845 */ 846 if ((prop & SA_CONT) && action == SIG_DFL) { 847 p->p_siglist &= ~mask; 848 goto out; 849 } 850 /* 851 * When a sleeping process receives a stop 852 * signal, process immediately if possible. 853 * All other (caught or default) signals 854 * cause the process to run. 855 */ 856 if (prop & SA_STOP) { 857 if (action != SIG_DFL) 858 goto runfast; 859 /* 860 * If a child holding parent blocked, 861 * stopping could cause deadlock. 862 */ 863 if (p->p_flag & P_PPWAIT) 864 goto out; 865 p->p_siglist &= ~mask; 866 p->p_xstat = signum; 867 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0) 868 psignal(p->p_pptr, SIGCHLD); 869 stop(p); 870 goto out; 871 } else 872 goto runfast; 873 /*NOTREACHED*/ 874 875 case SSTOP: 876 /* 877 * If traced process is already stopped, 878 * then no further action is necessary. 879 */ 880 if (p->p_flag & P_TRACED) 881 goto out; 882 883 /* 884 * Kill signal always sets processes running. 885 */ 886 if (signum == SIGKILL) 887 goto runfast; 888 889 if (prop & SA_CONT) { 890 /* 891 * If SIGCONT is default (or ignored), we continue the 892 * process but don't leave the signal in p_siglist, as 893 * it has no further action. If SIGCONT is held, we 894 * continue the process and leave the signal in 895 * p_siglist. If the process catches SIGCONT, let it 896 * handle the signal itself. If it isn't waiting on 897 * an event, then it goes back to run state. 898 * Otherwise, process goes back to sleep state. 899 */ 900 if (action == SIG_DFL) 901 p->p_siglist &= ~mask; 902 if (action == SIG_CATCH) 903 goto runfast; 904 if (p->p_wchan == 0) 905 goto run; 906 p->p_stat = SSLEEP; 907 goto out; 908 } 909 910 if (prop & SA_STOP) { 911 /* 912 * Already stopped, don't need to stop again. 913 * (If we did the shell could get confused.) 914 */ 915 p->p_siglist &= ~mask; /* take it away */ 916 goto out; 917 } 918 919 /* 920 * If process is sleeping interruptibly, then simulate a 921 * wakeup so that when it is continued, it will be made 922 * runnable and can look at the signal. But don't make 923 * the process runnable, leave it stopped. 924 */ 925 if (p->p_wchan && p->p_flag & P_SINTR) 926 unsleep(p); 927 goto out; 928 929 default: 930 /* 931 * SRUN, SIDL, SZOMB do nothing with the signal, 932 * other than kicking ourselves if we are running. 933 * It will either never be noticed, or noticed very soon. 934 */ 935 if (p == curproc) 936 signotify(p); 937#ifdef SMP 938 else if (p->p_stat == SRUN) 939 forward_signal(p); 940#endif 941 goto out; 942 } 943 /*NOTREACHED*/ 944 945runfast: 946 /* 947 * Raise priority to at least PUSER. 948 */ 949 if (p->p_priority > PUSER) 950 p->p_priority = PUSER; 951run: 952 setrunnable(p); 953out: 954 splx(s); 955} 956 957/* 958 * If the current process has received a signal (should be caught or cause 959 * termination, should interrupt current syscall), return the signal number. 960 * Stop signals with default action are processed immediately, then cleared; 961 * they aren't returned. This is checked after each entry to the system for 962 * a syscall or trap (though this can usually be done without calling issignal 963 * by checking the pending signal masks in the CURSIG macro.) The normal call 964 * sequence is 965 * 966 * while (signum = CURSIG(curproc)) 967 * postsig(signum); 968 */ 969int 970issignal(p) 971 register struct proc *p; 972{ 973 register int signum, mask, prop; 974 975 for (;;) { 976 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 977 978 mask = p->p_siglist & ~p->p_sigmask; 979 if (p->p_flag & P_PPWAIT) 980 mask &= ~stopsigmask; 981 if (mask == 0) /* no signal to send */ 982 return (0); 983 signum = ffs((long)mask); 984 mask = sigmask(signum); 985 prop = sigprop[signum]; 986 987 STOPEVENT(p, S_SIG, signum); 988 989 /* 990 * We should see pending but ignored signals 991 * only if P_TRACED was on when they were posted. 992 */ 993 if ((mask & p->p_sigignore) && (traced == 0)) { 994 p->p_siglist &= ~mask; 995 continue; 996 } 997 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 998 /* 999 * If traced, always stop, and stay 1000 * stopped until released by the parent. 1001 */ 1002 p->p_xstat = signum; 1003 psignal(p->p_pptr, SIGCHLD); 1004 do { 1005 stop(p); 1006 mi_switch(); 1007 } while (!trace_req(p) 1008 && p->p_flag & P_TRACED); 1009 1010 /* 1011 * If the traced bit got turned off, go back up 1012 * to the top to rescan signals. This ensures 1013 * that p_sig* and ps_sigact are consistent. 1014 */ 1015 if ((p->p_flag & P_TRACED) == 0) 1016 continue; 1017 1018 /* 1019 * If parent wants us to take the signal, 1020 * then it will leave it in p->p_xstat; 1021 * otherwise we just look for signals again. 1022 */ 1023 p->p_siglist &= ~mask; /* clear the old signal */ 1024 signum = p->p_xstat; 1025 if (signum == 0) 1026 continue; 1027 1028 /* 1029 * Put the new signal into p_siglist. If the 1030 * signal is being masked, look for other signals. 1031 */ 1032 mask = sigmask(signum); 1033 p->p_siglist |= mask; 1034 if (p->p_sigmask & mask) 1035 continue; 1036 } 1037 1038 /* 1039 * Decide whether the signal should be returned. 1040 * Return the signal's number, or fall through 1041 * to clear it from the pending mask. 1042 */ 1043 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) { 1044 1045 case (int)SIG_DFL: 1046 /* 1047 * Don't take default actions on system processes. 1048 */ 1049 if (p->p_pid <= 1) { 1050#ifdef DIAGNOSTIC 1051 /* 1052 * Are you sure you want to ignore SIGSEGV 1053 * in init? XXX 1054 */ 1055 printf("Process (pid %lu) got signal %d\n", 1056 (u_long)p->p_pid, signum); 1057#endif 1058 break; /* == ignore */ 1059 } 1060 /* 1061 * If there is a pending stop signal to process 1062 * with default action, stop here, 1063 * then clear the signal. However, 1064 * if process is member of an orphaned 1065 * process group, ignore tty stop signals. 1066 */ 1067 if (prop & SA_STOP) { 1068 if (p->p_flag & P_TRACED || 1069 (p->p_pgrp->pg_jobc == 0 && 1070 prop & SA_TTYSTOP)) 1071 break; /* == ignore */ 1072 p->p_xstat = signum; 1073 stop(p); 1074 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0) 1075 psignal(p->p_pptr, SIGCHLD); 1076 mi_switch(); 1077 break; 1078 } else if (prop & SA_IGNORE) { 1079 /* 1080 * Except for SIGCONT, shouldn't get here. 1081 * Default action is to ignore; drop it. 1082 */ 1083 break; /* == ignore */ 1084 } else 1085 return (signum); 1086 /*NOTREACHED*/ 1087 1088 case (int)SIG_IGN: 1089 /* 1090 * Masking above should prevent us ever trying 1091 * to take action on an ignored signal other 1092 * than SIGCONT, unless process is traced. 1093 */ 1094 if ((prop & SA_CONT) == 0 && 1095 (p->p_flag & P_TRACED) == 0) 1096 printf("issignal\n"); 1097 break; /* == ignore */ 1098 1099 default: 1100 /* 1101 * This signal has an action, let 1102 * postsig() process it. 1103 */ 1104 return (signum); 1105 } 1106 p->p_siglist &= ~mask; /* take the signal! */ 1107 } 1108 /* NOTREACHED */ 1109} 1110 1111/* 1112 * Put the argument process into the stopped state and notify the parent 1113 * via wakeup. Signals are handled elsewhere. The process must not be 1114 * on the run queue. 1115 */ 1116void 1117stop(p) 1118 register struct proc *p; 1119{ 1120 1121 p->p_stat = SSTOP; 1122 p->p_flag &= ~P_WAITED; 1123 wakeup((caddr_t)p->p_pptr); 1124} 1125 1126/* 1127 * Take the action for the specified signal 1128 * from the current set of pending signals. 1129 */ 1130void 1131postsig(signum) 1132 register int signum; 1133{ 1134 register struct proc *p = curproc; 1135 register struct sigacts *ps = p->p_sigacts; 1136 register sig_t action; 1137 int code, mask, returnmask; 1138 1139 KASSERT(signum != 0, ("postsig")); 1140 1141 mask = sigmask(signum); 1142 p->p_siglist &= ~mask; 1143 action = ps->ps_sigact[signum]; 1144#ifdef KTRACE 1145 if (KTRPOINT(p, KTR_PSIG)) 1146 ktrpsig(p->p_tracep, 1147 signum, action, p->p_oldsigmask ? 1148 p->p_oldsigmask : p->p_sigmask, 0); 1149#endif 1150 STOPEVENT(p, S_SIG, signum); 1151 1152 if (action == SIG_DFL) { 1153 /* 1154 * Default action, where the default is to kill 1155 * the process. (Other cases were ignored above.) 1156 */ 1157 sigexit(p, signum); 1158 /* NOTREACHED */ 1159 } else { 1160 /* 1161 * If we get here, the signal must be caught. 1162 */ 1163 KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0, 1164 ("postsig action")); 1165 /* 1166 * Set the new mask value and also defer further 1167 * occurences of this signal. 1168 * 1169 * Special case: user has done a sigpause. Here the 1170 * current mask is not of interest, but rather the 1171 * mask from before the sigpause is what we want 1172 * restored after the signal processing is completed. 1173 */ 1174 (void) splhigh(); 1175 if (p->p_oldsigmask) { 1176 returnmask = p->p_oldsigmask; 1177 p->p_oldsigmask = 0; 1178 } else 1179 returnmask = p->p_sigmask; 1180 p->p_sigmask |= ps->ps_catchmask[signum] | 1181 (mask & ~ps->ps_signodefer); 1182 if ((ps->ps_sigreset & mask) != 0) { 1183 /* 1184 * See setsigvec() for origin of this code. 1185 */ 1186 p->p_sigcatch &= ~mask; 1187 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1188 p->p_sigignore |= mask; 1189 ps->ps_sigact[signum] = SIG_DFL; 1190 } 1191 (void) spl0(); 1192 p->p_stats->p_ru.ru_nsignals++; 1193 if (p->p_sig != signum) { 1194 code = 0; 1195 } else { 1196 code = p->p_code; 1197 p->p_code = 0; 1198 p->p_sig = 0; 1199 } 1200 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code); 1201 } 1202} 1203 1204/* 1205 * Kill the current process for stated reason. 1206 */ 1207void 1208killproc(p, why) 1209 struct proc *p; 1210 char *why; 1211{ 1212 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1213 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1214 psignal(p, SIGKILL); 1215} 1216 1217/* 1218 * Force the current process to exit with the specified signal, dumping core 1219 * if appropriate. We bypass the normal tests for masked and caught signals, 1220 * allowing unrecoverable failures to terminate the process without changing 1221 * signal state. Mark the accounting record with the signal termination. 1222 * If dumping core, save the signal number for the debugger. Calls exit and 1223 * does not return. 1224 */ 1225void 1226sigexit(p, signum) 1227 register struct proc *p; 1228 int signum; 1229{ 1230 1231 p->p_acflag |= AXSIG; 1232 if (sigprop[signum] & SA_CORE) { 1233 p->p_sig = signum; 1234 /* 1235 * Log signals which would cause core dumps 1236 * (Log as LOG_INFO to appease those who don't want 1237 * these messages.) 1238 * XXX : Todo, as well as euid, write out ruid too 1239 */ 1240 if (p->p_sysent->sv_coredump != NULL && 1241 (*p->p_sysent->sv_coredump)(p) == 0) 1242 signum |= WCOREFLAG; 1243 if (kern_logsigexit) 1244 log(LOG_INFO, 1245 "pid %d (%s), uid %d: exited on signal %d%s\n", 1246 p->p_pid, p->p_comm, 1247 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1248 signum &~ WCOREFLAG, 1249 signum & WCOREFLAG ? " (core dumped)" : ""); 1250 } 1251 exit1(p, W_EXITCODE(0, signum)); 1252 /* NOTREACHED */ 1253} 1254 1255static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1256SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1257 sizeof(corefilename), "process corefile name format string"); 1258 1259/* 1260 * expand_name(name, uid, pid) 1261 * Expand the name described in corefilename, using name, uid, and pid. 1262 * corefilename is a printf-like string, with three format specifiers: 1263 * %N name of process ("name") 1264 * %P process id (pid) 1265 * %U user id (uid) 1266 * For example, "%N.core" is the default; they can be disabled completely 1267 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1268 * This is controlled by the sysctl variable kern.corefile (see above). 1269 */ 1270 1271char * 1272expand_name(name, uid, pid) 1273const char *name; int uid; int pid; { 1274 char *temp; 1275 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1276 int i, n; 1277 char *format = corefilename; 1278 1279 temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT); 1280 if (temp == NULL) 1281 return NULL; 1282 bzero(temp, MAXPATHLEN+3); 1283 for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) { 1284 int l; 1285 switch (format[i]) { 1286 case '%': /* Format character */ 1287 i++; 1288 switch (format[i]) { 1289 case '%': 1290 temp[n++] = '%'; 1291 break; 1292 case 'N': /* process name */ 1293 l = strlen(name); 1294 if ((n + l) > MAXPATHLEN) { 1295 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1296 pid, name, uid, temp, name); 1297 free(temp, M_TEMP); 1298 return NULL; 1299 } 1300 memcpy(temp+n, name, l); 1301 n += l; 1302 break; 1303 case 'P': /* process id */ 1304 sprintf(buf, "%u", pid); 1305 l = strlen(buf); 1306 if ((n + l) > MAXPATHLEN) { 1307 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1308 pid, name, uid, temp, name); 1309 free(temp, M_TEMP); 1310 return NULL; 1311 } 1312 memcpy(temp+n, buf, l); 1313 n += l; 1314 break; 1315 case 'U': /* user id */ 1316 sprintf(buf, "%u", uid); 1317 l = strlen(buf); 1318 if ((n + l) > MAXPATHLEN) { 1319 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1320 pid, name, uid, temp, name); 1321 free(temp, M_TEMP); 1322 return NULL; 1323 } 1324 memcpy(temp+n, buf, l); 1325 n += l; 1326 break; 1327 default: 1328 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1329 } 1330 break; 1331 default: 1332 temp[n++] = format[i]; 1333 } 1334 } 1335 return temp; 1336} 1337 1338/* 1339 * Nonexistent system call-- signal process (may want to handle it). 1340 * Flag error in case process won't see signal immediately (blocked or ignored). 1341 */ 1342#ifndef _SYS_SYSPROTO_H_ 1343struct nosys_args { 1344 int dummy; 1345}; 1346#endif 1347/* ARGSUSED */ 1348int 1349nosys(p, args) 1350 struct proc *p; 1351 struct nosys_args *args; 1352{ 1353 1354 psignal(p, SIGSYS); 1355 return (EINVAL); 1356} 1357 1358/* 1359 * Send a signal to a SIGIO or SIGURG to a process or process group using 1360 * stored credentials rather than those of the current process. 1361 */ 1362void 1363pgsigio(sigio, signum, checkctty) 1364 struct sigio *sigio; 1365 int signum, checkctty; 1366{ 1367 if (sigio == NULL) 1368 return; 1369 1370 if (sigio->sio_pgid > 0) { 1371 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1372 sigio->sio_proc)) 1373 psignal(sigio->sio_proc, signum); 1374 } else if (sigio->sio_pgid < 0) { 1375 struct proc *p; 1376 1377 for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL; 1378 p = p->p_pglist.le_next) 1379 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1380 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1381 psignal(p, signum); 1382 } 1383}
| 404#ifndef COMPAT_SUNOS 405 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP) 406 sv->sv_flags |= SV_NOCLDSTOP; 407#endif 408 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv, 409 sizeof (vec)))) 410 return (error); 411 } 412 if (uap->nsv) { 413 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 414 sizeof (vec)))) 415 return (error); 416 if ((signum == SIGKILL || signum == SIGSTOP) && 417 sv->sv_handler != SIG_DFL) 418 return (EINVAL); 419#ifdef COMPAT_SUNOS 420 sv->sv_flags |= SA_USERTRAMP; 421#endif 422 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 423 setsigvec(p, signum, (struct sigaction *)sv); 424 } 425 return (0); 426} 427 428#ifndef _SYS_SYSPROTO_H_ 429struct osigblock_args { 430 int mask; 431}; 432#endif 433int 434osigblock(p, uap) 435 register struct proc *p; 436 struct osigblock_args *uap; 437{ 438 439 (void) splhigh(); 440 p->p_retval[0] = p->p_sigmask; 441 p->p_sigmask |= uap->mask &~ sigcantmask; 442 (void) spl0(); 443 return (0); 444} 445 446#ifndef _SYS_SYSPROTO_H_ 447struct osigsetmask_args { 448 int mask; 449}; 450#endif 451int 452osigsetmask(p, uap) 453 struct proc *p; 454 struct osigsetmask_args *uap; 455{ 456 457 (void) splhigh(); 458 p->p_retval[0] = p->p_sigmask; 459 p->p_sigmask = uap->mask &~ sigcantmask; 460 (void) spl0(); 461 return (0); 462} 463#endif /* COMPAT_43 || COMPAT_SUNOS */ 464 465/* 466 * Suspend process until signal, providing mask to be set 467 * in the meantime. Note nonstandard calling convention: 468 * libc stub passes mask, not pointer, to save a copyin. 469 */ 470#ifndef _SYS_SYSPROTO_H_ 471struct sigsuspend_args { 472 sigset_t mask; 473}; 474#endif 475/* ARGSUSED */ 476int 477sigsuspend(p, uap) 478 register struct proc *p; 479 struct sigsuspend_args *uap; 480{ 481 register struct sigacts *ps = p->p_sigacts; 482 483 /* 484 * When returning from sigpause, we want 485 * the old mask to be restored after the 486 * signal handler has finished. Thus, we 487 * save it here and mark the sigacts structure 488 * to indicate this. 489 */ 490 p->p_oldsigmask = p->p_sigmask; 491 p->p_sigmask = uap->mask &~ sigcantmask; 492 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 493 /* void */; 494 /* always return EINTR rather than ERESTART... */ 495 return (EINTR); 496} 497 498#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 499#ifndef _SYS_SYSPROTO_H_ 500struct osigstack_args { 501 struct sigstack *nss; 502 struct sigstack *oss; 503}; 504#endif 505/* ARGSUSED */ 506int 507osigstack(p, uap) 508 struct proc *p; 509 register struct osigstack_args *uap; 510{ 511 struct sigstack ss; 512 struct sigacts *psp; 513 int error = 0; 514 515 psp = p->p_sigacts; 516 ss.ss_sp = psp->ps_sigstk.ss_sp; 517 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 518 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss, 519 sizeof (struct sigstack)))) 520 return (error); 521 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 522 sizeof (ss))) == 0) { 523 psp->ps_sigstk.ss_sp = ss.ss_sp; 524 psp->ps_sigstk.ss_size = 0; 525 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 526 psp->ps_flags |= SAS_ALTSTACK; 527 } 528 return (error); 529} 530#endif /* COMPAT_43 || COMPAT_SUNOS */ 531 532#ifndef _SYS_SYSPROTO_H_ 533struct sigaltstack_args { 534 struct sigaltstack *nss; 535 struct sigaltstack *oss; 536}; 537#endif 538/* ARGSUSED */ 539int 540sigaltstack(p, uap) 541 struct proc *p; 542 register struct sigaltstack_args *uap; 543{ 544 struct sigacts *psp; 545 struct sigaltstack ss; 546 int error; 547 548 psp = p->p_sigacts; 549 if ((psp->ps_flags & SAS_ALTSTACK) == 0) 550 psp->ps_sigstk.ss_flags |= SS_DISABLE; 551 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk, 552 (caddr_t)uap->oss, sizeof (struct sigaltstack)))) 553 return (error); 554 if (uap->nss == 0) 555 return (0); 556 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))) 557 return (error); 558 if (ss.ss_flags & SS_DISABLE) { 559 if (psp->ps_sigstk.ss_flags & SS_ONSTACK) 560 return (EINVAL); 561 psp->ps_flags &= ~SAS_ALTSTACK; 562 psp->ps_sigstk.ss_flags = ss.ss_flags; 563 return (0); 564 } 565 if (ss.ss_size < MINSIGSTKSZ) 566 return (ENOMEM); 567 psp->ps_flags |= SAS_ALTSTACK; 568 psp->ps_sigstk= ss; 569 return (0); 570} 571 572/* 573 * Common code for kill process group/broadcast kill. 574 * cp is calling process. 575 */ 576int 577killpg1(cp, signum, pgid, all) 578 register struct proc *cp; 579 int signum, pgid, all; 580{ 581 register struct proc *p; 582 register struct pcred *pc = cp->p_cred; 583 struct pgrp *pgrp; 584 int nfound = 0; 585 586 if (all) 587 /* 588 * broadcast 589 */ 590 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 591 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 592 p == cp || !CANSIGNAL(cp, pc, p, signum)) 593 continue; 594 nfound++; 595 if (signum) 596 psignal(p, signum); 597 } 598 else { 599 if (pgid == 0) 600 /* 601 * zero pgid means send to my process group. 602 */ 603 pgrp = cp->p_pgrp; 604 else { 605 pgrp = pgfind(pgid); 606 if (pgrp == NULL) 607 return (ESRCH); 608 } 609 for (p = pgrp->pg_members.lh_first; p != 0; 610 p = p->p_pglist.le_next) { 611 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 612 p->p_stat == SZOMB || 613 !CANSIGNAL(cp, pc, p, signum)) 614 continue; 615 nfound++; 616 if (signum) 617 psignal(p, signum); 618 } 619 } 620 return (nfound ? 0 : ESRCH); 621} 622 623#ifndef _SYS_SYSPROTO_H_ 624struct kill_args { 625 int pid; 626 int signum; 627}; 628#endif 629/* ARGSUSED */ 630int 631kill(cp, uap) 632 register struct proc *cp; 633 register struct kill_args *uap; 634{ 635 register struct proc *p; 636 register struct pcred *pc = cp->p_cred; 637 638 if ((u_int)uap->signum >= NSIG) 639 return (EINVAL); 640 if (uap->pid > 0) { 641 /* kill single process */ 642 if ((p = pfind(uap->pid)) == NULL) 643 return (ESRCH); 644 if (!CANSIGNAL(cp, pc, p, uap->signum)) 645 return (EPERM); 646 if (uap->signum) 647 psignal(p, uap->signum); 648 return (0); 649 } 650 switch (uap->pid) { 651 case -1: /* broadcast signal */ 652 return (killpg1(cp, uap->signum, 0, 1)); 653 case 0: /* signal own process group */ 654 return (killpg1(cp, uap->signum, 0, 0)); 655 default: /* negative explicit process group */ 656 return (killpg1(cp, uap->signum, -uap->pid, 0)); 657 } 658 /* NOTREACHED */ 659} 660 661#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 662#ifndef _SYS_SYSPROTO_H_ 663struct okillpg_args { 664 int pgid; 665 int signum; 666}; 667#endif 668/* ARGSUSED */ 669int 670okillpg(p, uap) 671 struct proc *p; 672 register struct okillpg_args *uap; 673{ 674 675 if ((u_int)uap->signum >= NSIG) 676 return (EINVAL); 677 return (killpg1(p, uap->signum, uap->pgid, 0)); 678} 679#endif /* COMPAT_43 || COMPAT_SUNOS */ 680 681/* 682 * Send a signal to a process group. 683 */ 684void 685gsignal(pgid, signum) 686 int pgid, signum; 687{ 688 struct pgrp *pgrp; 689 690 if (pgid && (pgrp = pgfind(pgid))) 691 pgsignal(pgrp, signum, 0); 692} 693 694/* 695 * Send a signal to a process group. If checktty is 1, 696 * limit to members which have a controlling terminal. 697 */ 698void 699pgsignal(pgrp, signum, checkctty) 700 struct pgrp *pgrp; 701 int signum, checkctty; 702{ 703 register struct proc *p; 704 705 if (pgrp) 706 for (p = pgrp->pg_members.lh_first; p != 0; 707 p = p->p_pglist.le_next) 708 if (checkctty == 0 || p->p_flag & P_CONTROLT) 709 psignal(p, signum); 710} 711 712/* 713 * Send a signal caused by a trap to the current process. 714 * If it will be caught immediately, deliver it with correct code. 715 * Otherwise, post it normally. 716 */ 717void 718trapsignal(p, signum, code) 719 struct proc *p; 720 register int signum; 721 u_long code; 722{ 723 register struct sigacts *ps = p->p_sigacts; 724 int mask; 725 726 mask = sigmask(signum); 727 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 && 728 (p->p_sigmask & mask) == 0) { 729 p->p_stats->p_ru.ru_nsignals++; 730#ifdef KTRACE 731 if (KTRPOINT(p, KTR_PSIG)) 732 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum], 733 p->p_sigmask, code); 734#endif 735 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum, 736 p->p_sigmask, code); 737 p->p_sigmask |= ps->ps_catchmask[signum] | 738 (mask & ~ps->ps_signodefer); 739 if ((ps->ps_sigreset & mask) != 0) { 740 /* 741 * See setsigvec() for origin of this code. 742 */ 743 p->p_sigcatch &= ~mask; 744 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 745 p->p_sigignore |= mask; 746 ps->ps_sigact[signum] = SIG_DFL; 747 } 748 } else { 749 p->p_code = code; /* XXX for core dump/debugger */ 750 p->p_sig = signum; /* XXX to verify code */ 751 psignal(p, signum); 752 } 753} 754 755/* 756 * Send the signal to the process. If the signal has an action, the action 757 * is usually performed by the target process rather than the caller; we add 758 * the signal to the set of pending signals for the process. 759 * 760 * Exceptions: 761 * o When a stop signal is sent to a sleeping process that takes the 762 * default action, the process is stopped without awakening it. 763 * o SIGCONT restarts stopped processes (or puts them back to sleep) 764 * regardless of the signal action (eg, blocked or ignored). 765 * 766 * Other ignored signals are discarded immediately. 767 */ 768void 769psignal(p, signum) 770 register struct proc *p; 771 register int signum; 772{ 773 register int s, prop; 774 register sig_t action; 775 int mask; 776 777 if ((u_int)signum >= NSIG || signum == 0) { 778 printf("psignal: signum %d\n", signum); 779 panic("psignal signal number"); 780 } 781 mask = sigmask(signum); 782 prop = sigprop[signum]; 783 784 /* 785 * If proc is traced, always give parent a chance; 786 * if signal event is tracked by procfs, give *that* 787 * a chance, as well. 788 */ 789 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 790 action = SIG_DFL; 791 else { 792 /* 793 * If the signal is being ignored, 794 * then we forget about it immediately. 795 * (Note: we don't set SIGCONT in p_sigignore, 796 * and if it is set to SIG_IGN, 797 * action will be SIG_DFL here.) 798 */ 799 if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT)) 800 return; 801 if (p->p_sigmask & mask) 802 action = SIG_HOLD; 803 else if (p->p_sigcatch & mask) 804 action = SIG_CATCH; 805 else 806 action = SIG_DFL; 807 } 808 809 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 810 (p->p_flag & P_TRACED) == 0) 811 p->p_nice = NZERO; 812 813 if (prop & SA_CONT) 814 p->p_siglist &= ~stopsigmask; 815 816 if (prop & SA_STOP) { 817 /* 818 * If sending a tty stop signal to a member of an orphaned 819 * process group, discard the signal here if the action 820 * is default; don't stop the process below if sleeping, 821 * and don't clear any pending SIGCONT. 822 */ 823 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 824 action == SIG_DFL) 825 return; 826 p->p_siglist &= ~contsigmask; 827 } 828 p->p_siglist |= mask; 829 830 /* 831 * Defer further processing for signals which are held, 832 * except that stopped processes must be continued by SIGCONT. 833 */ 834 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 835 return; 836 s = splhigh(); 837 switch (p->p_stat) { 838 839 case SSLEEP: 840 /* 841 * If process is sleeping uninterruptibly 842 * we can't interrupt the sleep... the signal will 843 * be noticed when the process returns through 844 * trap() or syscall(). 845 */ 846 if ((p->p_flag & P_SINTR) == 0) 847 goto out; 848 /* 849 * Process is sleeping and traced... make it runnable 850 * so it can discover the signal in issignal() and stop 851 * for the parent. 852 */ 853 if (p->p_flag & P_TRACED) 854 goto run; 855 /* 856 * If SIGCONT is default (or ignored) and process is 857 * asleep, we are finished; the process should not 858 * be awakened. 859 */ 860 if ((prop & SA_CONT) && action == SIG_DFL) { 861 p->p_siglist &= ~mask; 862 goto out; 863 } 864 /* 865 * When a sleeping process receives a stop 866 * signal, process immediately if possible. 867 * All other (caught or default) signals 868 * cause the process to run. 869 */ 870 if (prop & SA_STOP) { 871 if (action != SIG_DFL) 872 goto runfast; 873 /* 874 * If a child holding parent blocked, 875 * stopping could cause deadlock. 876 */ 877 if (p->p_flag & P_PPWAIT) 878 goto out; 879 p->p_siglist &= ~mask; 880 p->p_xstat = signum; 881 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0) 882 psignal(p->p_pptr, SIGCHLD); 883 stop(p); 884 goto out; 885 } else 886 goto runfast; 887 /*NOTREACHED*/ 888 889 case SSTOP: 890 /* 891 * If traced process is already stopped, 892 * then no further action is necessary. 893 */ 894 if (p->p_flag & P_TRACED) 895 goto out; 896 897 /* 898 * Kill signal always sets processes running. 899 */ 900 if (signum == SIGKILL) 901 goto runfast; 902 903 if (prop & SA_CONT) { 904 /* 905 * If SIGCONT is default (or ignored), we continue the 906 * process but don't leave the signal in p_siglist, as 907 * it has no further action. If SIGCONT is held, we 908 * continue the process and leave the signal in 909 * p_siglist. If the process catches SIGCONT, let it 910 * handle the signal itself. If it isn't waiting on 911 * an event, then it goes back to run state. 912 * Otherwise, process goes back to sleep state. 913 */ 914 if (action == SIG_DFL) 915 p->p_siglist &= ~mask; 916 if (action == SIG_CATCH) 917 goto runfast; 918 if (p->p_wchan == 0) 919 goto run; 920 p->p_stat = SSLEEP; 921 goto out; 922 } 923 924 if (prop & SA_STOP) { 925 /* 926 * Already stopped, don't need to stop again. 927 * (If we did the shell could get confused.) 928 */ 929 p->p_siglist &= ~mask; /* take it away */ 930 goto out; 931 } 932 933 /* 934 * If process is sleeping interruptibly, then simulate a 935 * wakeup so that when it is continued, it will be made 936 * runnable and can look at the signal. But don't make 937 * the process runnable, leave it stopped. 938 */ 939 if (p->p_wchan && p->p_flag & P_SINTR) 940 unsleep(p); 941 goto out; 942 943 default: 944 /* 945 * SRUN, SIDL, SZOMB do nothing with the signal, 946 * other than kicking ourselves if we are running. 947 * It will either never be noticed, or noticed very soon. 948 */ 949 if (p == curproc) 950 signotify(p); 951#ifdef SMP 952 else if (p->p_stat == SRUN) 953 forward_signal(p); 954#endif 955 goto out; 956 } 957 /*NOTREACHED*/ 958 959runfast: 960 /* 961 * Raise priority to at least PUSER. 962 */ 963 if (p->p_priority > PUSER) 964 p->p_priority = PUSER; 965run: 966 setrunnable(p); 967out: 968 splx(s); 969} 970 971/* 972 * If the current process has received a signal (should be caught or cause 973 * termination, should interrupt current syscall), return the signal number. 974 * Stop signals with default action are processed immediately, then cleared; 975 * they aren't returned. This is checked after each entry to the system for 976 * a syscall or trap (though this can usually be done without calling issignal 977 * by checking the pending signal masks in the CURSIG macro.) The normal call 978 * sequence is 979 * 980 * while (signum = CURSIG(curproc)) 981 * postsig(signum); 982 */ 983int 984issignal(p) 985 register struct proc *p; 986{ 987 register int signum, mask, prop; 988 989 for (;;) { 990 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 991 992 mask = p->p_siglist & ~p->p_sigmask; 993 if (p->p_flag & P_PPWAIT) 994 mask &= ~stopsigmask; 995 if (mask == 0) /* no signal to send */ 996 return (0); 997 signum = ffs((long)mask); 998 mask = sigmask(signum); 999 prop = sigprop[signum]; 1000 1001 STOPEVENT(p, S_SIG, signum); 1002 1003 /* 1004 * We should see pending but ignored signals 1005 * only if P_TRACED was on when they were posted. 1006 */ 1007 if ((mask & p->p_sigignore) && (traced == 0)) { 1008 p->p_siglist &= ~mask; 1009 continue; 1010 } 1011 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1012 /* 1013 * If traced, always stop, and stay 1014 * stopped until released by the parent. 1015 */ 1016 p->p_xstat = signum; 1017 psignal(p->p_pptr, SIGCHLD); 1018 do { 1019 stop(p); 1020 mi_switch(); 1021 } while (!trace_req(p) 1022 && p->p_flag & P_TRACED); 1023 1024 /* 1025 * If the traced bit got turned off, go back up 1026 * to the top to rescan signals. This ensures 1027 * that p_sig* and ps_sigact are consistent. 1028 */ 1029 if ((p->p_flag & P_TRACED) == 0) 1030 continue; 1031 1032 /* 1033 * If parent wants us to take the signal, 1034 * then it will leave it in p->p_xstat; 1035 * otherwise we just look for signals again. 1036 */ 1037 p->p_siglist &= ~mask; /* clear the old signal */ 1038 signum = p->p_xstat; 1039 if (signum == 0) 1040 continue; 1041 1042 /* 1043 * Put the new signal into p_siglist. If the 1044 * signal is being masked, look for other signals. 1045 */ 1046 mask = sigmask(signum); 1047 p->p_siglist |= mask; 1048 if (p->p_sigmask & mask) 1049 continue; 1050 } 1051 1052 /* 1053 * Decide whether the signal should be returned. 1054 * Return the signal's number, or fall through 1055 * to clear it from the pending mask. 1056 */ 1057 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) { 1058 1059 case (int)SIG_DFL: 1060 /* 1061 * Don't take default actions on system processes. 1062 */ 1063 if (p->p_pid <= 1) { 1064#ifdef DIAGNOSTIC 1065 /* 1066 * Are you sure you want to ignore SIGSEGV 1067 * in init? XXX 1068 */ 1069 printf("Process (pid %lu) got signal %d\n", 1070 (u_long)p->p_pid, signum); 1071#endif 1072 break; /* == ignore */ 1073 } 1074 /* 1075 * If there is a pending stop signal to process 1076 * with default action, stop here, 1077 * then clear the signal. However, 1078 * if process is member of an orphaned 1079 * process group, ignore tty stop signals. 1080 */ 1081 if (prop & SA_STOP) { 1082 if (p->p_flag & P_TRACED || 1083 (p->p_pgrp->pg_jobc == 0 && 1084 prop & SA_TTYSTOP)) 1085 break; /* == ignore */ 1086 p->p_xstat = signum; 1087 stop(p); 1088 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0) 1089 psignal(p->p_pptr, SIGCHLD); 1090 mi_switch(); 1091 break; 1092 } else if (prop & SA_IGNORE) { 1093 /* 1094 * Except for SIGCONT, shouldn't get here. 1095 * Default action is to ignore; drop it. 1096 */ 1097 break; /* == ignore */ 1098 } else 1099 return (signum); 1100 /*NOTREACHED*/ 1101 1102 case (int)SIG_IGN: 1103 /* 1104 * Masking above should prevent us ever trying 1105 * to take action on an ignored signal other 1106 * than SIGCONT, unless process is traced. 1107 */ 1108 if ((prop & SA_CONT) == 0 && 1109 (p->p_flag & P_TRACED) == 0) 1110 printf("issignal\n"); 1111 break; /* == ignore */ 1112 1113 default: 1114 /* 1115 * This signal has an action, let 1116 * postsig() process it. 1117 */ 1118 return (signum); 1119 } 1120 p->p_siglist &= ~mask; /* take the signal! */ 1121 } 1122 /* NOTREACHED */ 1123} 1124 1125/* 1126 * Put the argument process into the stopped state and notify the parent 1127 * via wakeup. Signals are handled elsewhere. The process must not be 1128 * on the run queue. 1129 */ 1130void 1131stop(p) 1132 register struct proc *p; 1133{ 1134 1135 p->p_stat = SSTOP; 1136 p->p_flag &= ~P_WAITED; 1137 wakeup((caddr_t)p->p_pptr); 1138} 1139 1140/* 1141 * Take the action for the specified signal 1142 * from the current set of pending signals. 1143 */ 1144void 1145postsig(signum) 1146 register int signum; 1147{ 1148 register struct proc *p = curproc; 1149 register struct sigacts *ps = p->p_sigacts; 1150 register sig_t action; 1151 int code, mask, returnmask; 1152 1153 KASSERT(signum != 0, ("postsig")); 1154 1155 mask = sigmask(signum); 1156 p->p_siglist &= ~mask; 1157 action = ps->ps_sigact[signum]; 1158#ifdef KTRACE 1159 if (KTRPOINT(p, KTR_PSIG)) 1160 ktrpsig(p->p_tracep, 1161 signum, action, p->p_oldsigmask ? 1162 p->p_oldsigmask : p->p_sigmask, 0); 1163#endif 1164 STOPEVENT(p, S_SIG, signum); 1165 1166 if (action == SIG_DFL) { 1167 /* 1168 * Default action, where the default is to kill 1169 * the process. (Other cases were ignored above.) 1170 */ 1171 sigexit(p, signum); 1172 /* NOTREACHED */ 1173 } else { 1174 /* 1175 * If we get here, the signal must be caught. 1176 */ 1177 KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0, 1178 ("postsig action")); 1179 /* 1180 * Set the new mask value and also defer further 1181 * occurences of this signal. 1182 * 1183 * Special case: user has done a sigpause. Here the 1184 * current mask is not of interest, but rather the 1185 * mask from before the sigpause is what we want 1186 * restored after the signal processing is completed. 1187 */ 1188 (void) splhigh(); 1189 if (p->p_oldsigmask) { 1190 returnmask = p->p_oldsigmask; 1191 p->p_oldsigmask = 0; 1192 } else 1193 returnmask = p->p_sigmask; 1194 p->p_sigmask |= ps->ps_catchmask[signum] | 1195 (mask & ~ps->ps_signodefer); 1196 if ((ps->ps_sigreset & mask) != 0) { 1197 /* 1198 * See setsigvec() for origin of this code. 1199 */ 1200 p->p_sigcatch &= ~mask; 1201 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1202 p->p_sigignore |= mask; 1203 ps->ps_sigact[signum] = SIG_DFL; 1204 } 1205 (void) spl0(); 1206 p->p_stats->p_ru.ru_nsignals++; 1207 if (p->p_sig != signum) { 1208 code = 0; 1209 } else { 1210 code = p->p_code; 1211 p->p_code = 0; 1212 p->p_sig = 0; 1213 } 1214 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code); 1215 } 1216} 1217 1218/* 1219 * Kill the current process for stated reason. 1220 */ 1221void 1222killproc(p, why) 1223 struct proc *p; 1224 char *why; 1225{ 1226 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1227 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1228 psignal(p, SIGKILL); 1229} 1230 1231/* 1232 * Force the current process to exit with the specified signal, dumping core 1233 * if appropriate. We bypass the normal tests for masked and caught signals, 1234 * allowing unrecoverable failures to terminate the process without changing 1235 * signal state. Mark the accounting record with the signal termination. 1236 * If dumping core, save the signal number for the debugger. Calls exit and 1237 * does not return. 1238 */ 1239void 1240sigexit(p, signum) 1241 register struct proc *p; 1242 int signum; 1243{ 1244 1245 p->p_acflag |= AXSIG; 1246 if (sigprop[signum] & SA_CORE) { 1247 p->p_sig = signum; 1248 /* 1249 * Log signals which would cause core dumps 1250 * (Log as LOG_INFO to appease those who don't want 1251 * these messages.) 1252 * XXX : Todo, as well as euid, write out ruid too 1253 */ 1254 if (p->p_sysent->sv_coredump != NULL && 1255 (*p->p_sysent->sv_coredump)(p) == 0) 1256 signum |= WCOREFLAG; 1257 if (kern_logsigexit) 1258 log(LOG_INFO, 1259 "pid %d (%s), uid %d: exited on signal %d%s\n", 1260 p->p_pid, p->p_comm, 1261 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1262 signum &~ WCOREFLAG, 1263 signum & WCOREFLAG ? " (core dumped)" : ""); 1264 } 1265 exit1(p, W_EXITCODE(0, signum)); 1266 /* NOTREACHED */ 1267} 1268 1269static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1270SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1271 sizeof(corefilename), "process corefile name format string"); 1272 1273/* 1274 * expand_name(name, uid, pid) 1275 * Expand the name described in corefilename, using name, uid, and pid. 1276 * corefilename is a printf-like string, with three format specifiers: 1277 * %N name of process ("name") 1278 * %P process id (pid) 1279 * %U user id (uid) 1280 * For example, "%N.core" is the default; they can be disabled completely 1281 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1282 * This is controlled by the sysctl variable kern.corefile (see above). 1283 */ 1284 1285char * 1286expand_name(name, uid, pid) 1287const char *name; int uid; int pid; { 1288 char *temp; 1289 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1290 int i, n; 1291 char *format = corefilename; 1292 1293 temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT); 1294 if (temp == NULL) 1295 return NULL; 1296 bzero(temp, MAXPATHLEN+3); 1297 for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) { 1298 int l; 1299 switch (format[i]) { 1300 case '%': /* Format character */ 1301 i++; 1302 switch (format[i]) { 1303 case '%': 1304 temp[n++] = '%'; 1305 break; 1306 case 'N': /* process name */ 1307 l = strlen(name); 1308 if ((n + l) > MAXPATHLEN) { 1309 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1310 pid, name, uid, temp, name); 1311 free(temp, M_TEMP); 1312 return NULL; 1313 } 1314 memcpy(temp+n, name, l); 1315 n += l; 1316 break; 1317 case 'P': /* process id */ 1318 sprintf(buf, "%u", pid); 1319 l = strlen(buf); 1320 if ((n + l) > MAXPATHLEN) { 1321 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1322 pid, name, uid, temp, name); 1323 free(temp, M_TEMP); 1324 return NULL; 1325 } 1326 memcpy(temp+n, buf, l); 1327 n += l; 1328 break; 1329 case 'U': /* user id */ 1330 sprintf(buf, "%u", uid); 1331 l = strlen(buf); 1332 if ((n + l) > MAXPATHLEN) { 1333 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n", 1334 pid, name, uid, temp, name); 1335 free(temp, M_TEMP); 1336 return NULL; 1337 } 1338 memcpy(temp+n, buf, l); 1339 n += l; 1340 break; 1341 default: 1342 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1343 } 1344 break; 1345 default: 1346 temp[n++] = format[i]; 1347 } 1348 } 1349 return temp; 1350} 1351 1352/* 1353 * Nonexistent system call-- signal process (may want to handle it). 1354 * Flag error in case process won't see signal immediately (blocked or ignored). 1355 */ 1356#ifndef _SYS_SYSPROTO_H_ 1357struct nosys_args { 1358 int dummy; 1359}; 1360#endif 1361/* ARGSUSED */ 1362int 1363nosys(p, args) 1364 struct proc *p; 1365 struct nosys_args *args; 1366{ 1367 1368 psignal(p, SIGSYS); 1369 return (EINVAL); 1370} 1371 1372/* 1373 * Send a signal to a SIGIO or SIGURG to a process or process group using 1374 * stored credentials rather than those of the current process. 1375 */ 1376void 1377pgsigio(sigio, signum, checkctty) 1378 struct sigio *sigio; 1379 int signum, checkctty; 1380{ 1381 if (sigio == NULL) 1382 return; 1383 1384 if (sigio->sio_pgid > 0) { 1385 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1386 sigio->sio_proc)) 1387 psignal(sigio->sio_proc, signum); 1388 } else if (sigio->sio_pgid < 0) { 1389 struct proc *p; 1390 1391 for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL; 1392 p = p->p_pglist.le_next) 1393 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1394 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1395 psignal(p, signum); 1396 } 1397}
|