1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. 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 * $FreeBSD$ 40 */ 41 42 #include "opt_ktrace.h" 43 44 #define SIGPROP /* include signal properties table */ 45 #include <sys/param.h> 46 #include <sys/sysproto.h> 47 #include <sys/signalvar.h> 48 #include <sys/resourcevar.h> 49 #include <sys/namei.h> 50 #include <sys/vnode.h> 51 #include <sys/proc.h> 52 #include <sys/systm.h> 53 #include <sys/timeb.h> 54 #include <sys/times.h> 55 #include <sys/buf.h> 56 #include <sys/acct.h> 57 #include <sys/file.h> 58 #include <sys/kernel.h> 59 #include <sys/wait.h> 60 #include <sys/ktrace.h> 61 #include <sys/syslog.h> 62 #include <sys/stat.h> 63 #include <sys/sysent.h> 64 65 #include <machine/cpu.h> 66 67 #include <vm/vm.h> 68 #include <vm/vm_param.h> 69 #include <vm/vm_prot.h> 70 #include <sys/lock.h> 71 #include <vm/pmap.h> 72 #include <vm/vm_map.h> 73 #include <sys/user.h> /* for coredump */ 74 75 static int coredump __P((struct proc *p)); 76 static int killpg1 __P((struct proc *cp, int signum, int pgid, int all)); 77 static void stop __P((struct proc *)); 78 79 /* 80 * Can process p, with pcred pc, send the signal signum to process q? 81 */ 82 #define CANSIGNAL(p, pc, q, signum) \ 83 ((pc)->pc_ucred->cr_uid == 0 || \ 84 (pc)->p_ruid == (q)->p_cred->p_ruid || \ 85 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \ 86 (pc)->p_ruid == (q)->p_ucred->cr_uid || \ 87 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \ 88 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)) 89 90 #ifndef _SYS_SYSPROTO_H_ 91 struct sigaction_args { 92 int signum; 93 struct sigaction *nsa; 94 struct sigaction *osa; 95 }; 96 #endif 97 /* ARGSUSED */ 98 int 99 sigaction(p, uap, retval) 100 struct proc *p; 101 register struct sigaction_args *uap; 102 int *retval; 103 { 104 struct sigaction vec; 105 register struct sigaction *sa; 106 register struct sigacts *ps = p->p_sigacts; 107 register int signum; 108 int bit, error; 109 110 signum = uap->signum; 111 if (signum <= 0 || signum >= NSIG) 112 return (EINVAL); 113 sa = &vec; 114 if (uap->osa) { 115 sa->sa_handler = ps->ps_sigact[signum]; 116 sa->sa_mask = ps->ps_catchmask[signum]; 117 bit = sigmask(signum); 118 sa->sa_flags = 0; 119 if ((ps->ps_sigonstack & bit) != 0) 120 sa->sa_flags |= SA_ONSTACK; 121 if ((ps->ps_sigintr & bit) == 0) 122 sa->sa_flags |= SA_RESTART; 123 if ((ps->ps_sigreset & bit) != 0) 124 sa->sa_flags |= SA_RESETHAND; 125 if ((ps->ps_signodefer & bit) != 0) 126 sa->sa_flags |= SA_NODEFER; 127 if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP) 128 sa->sa_flags |= SA_NOCLDSTOP; 129 if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa, 130 sizeof (vec)))) 131 return (error); 132 } 133 if (uap->nsa) { 134 if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 135 sizeof (vec)))) 136 return (error); 137 if ((signum == SIGKILL || signum == SIGSTOP) && 138 sa->sa_handler != SIG_DFL) 139 return (EINVAL); 140 setsigvec(p, signum, sa); 141 } 142 return (0); 143 } 144 145 void 146 setsigvec(p, signum, sa) 147 register struct proc *p; 148 int signum; 149 register struct sigaction *sa; 150 { 151 register struct sigacts *ps = p->p_sigacts; 152 register int bit; 153 154 bit = sigmask(signum); 155 /* 156 * Change setting atomically. 157 */ 158 (void) splhigh(); 159 ps->ps_sigact[signum] = sa->sa_handler; 160 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; 161 if ((sa->sa_flags & SA_RESTART) == 0) 162 ps->ps_sigintr |= bit; 163 else 164 ps->ps_sigintr &= ~bit; 165 if (sa->sa_flags & SA_ONSTACK) 166 ps->ps_sigonstack |= bit; 167 else 168 ps->ps_sigonstack &= ~bit; 169 if (sa->sa_flags & SA_RESETHAND) 170 ps->ps_sigreset |= bit; 171 else 172 ps->ps_sigreset &= ~bit; 173 if (sa->sa_flags & SA_NODEFER) 174 ps->ps_signodefer |= bit; 175 else 176 ps->ps_signodefer &= ~bit; 177 #ifdef COMPAT_SUNOS 178 if (sa->sa_flags & SA_USERTRAMP) 179 ps->ps_usertramp |= bit; 180 else 181 ps->ps_usertramp &= ~bit; 182 #endif 183 if (signum == SIGCHLD) { 184 if (sa->sa_flags & SA_NOCLDSTOP) 185 p->p_flag |= P_NOCLDSTOP; 186 else 187 p->p_flag &= ~P_NOCLDSTOP; 188 } 189 /* 190 * Set bit in p_sigignore for signals that are set to SIG_IGN, 191 * and for signals set to SIG_DFL where the default is to ignore. 192 * However, don't put SIGCONT in p_sigignore, 193 * as we have to restart the process. 194 */ 195 if (sa->sa_handler == SIG_IGN || 196 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { 197 p->p_siglist &= ~bit; /* never to be seen again */ 198 if (signum != SIGCONT) 199 p->p_sigignore |= bit; /* easier in psignal */ 200 p->p_sigcatch &= ~bit; 201 } else { 202 p->p_sigignore &= ~bit; 203 if (sa->sa_handler == SIG_DFL) 204 p->p_sigcatch &= ~bit; 205 else 206 p->p_sigcatch |= bit; 207 } 208 (void) spl0(); 209 } 210 211 /* 212 * Initialize signal state for process 0; 213 * set to ignore signals that are ignored by default. 214 */ 215 void 216 siginit(p) 217 struct proc *p; 218 { 219 register int i; 220 221 for (i = 0; i < NSIG; i++) 222 if (sigprop[i] & SA_IGNORE && i != SIGCONT) 223 p->p_sigignore |= sigmask(i); 224 } 225 226 /* 227 * Reset signals for an exec of the specified process. 228 */ 229 void 230 execsigs(p) 231 register struct proc *p; 232 { 233 register struct sigacts *ps = p->p_sigacts; 234 register int nc, mask; 235 236 /* 237 * Reset caught signals. Held signals remain held 238 * through p_sigmask (unless they were caught, 239 * and are now ignored by default). 240 */ 241 while (p->p_sigcatch) { 242 nc = ffs((long)p->p_sigcatch); 243 mask = sigmask(nc); 244 p->p_sigcatch &= ~mask; 245 if (sigprop[nc] & SA_IGNORE) { 246 if (nc != SIGCONT) 247 p->p_sigignore |= mask; 248 p->p_siglist &= ~mask; 249 } 250 ps->ps_sigact[nc] = SIG_DFL; 251 } 252 /* 253 * Reset stack state to the user stack. 254 * Clear set of signals caught on the signal stack. 255 */ 256 ps->ps_sigstk.ss_flags = SS_DISABLE; 257 ps->ps_sigstk.ss_size = 0; 258 ps->ps_sigstk.ss_sp = 0; 259 ps->ps_flags = 0; 260 } 261 262 /* 263 * Manipulate signal mask. 264 * Note that we receive new mask, not pointer, 265 * and return old mask as return value; 266 * the library stub does the rest. 267 */ 268 #ifndef _SYS_SYSPROTO_H_ 269 struct sigprocmask_args { 270 int how; 271 sigset_t mask; 272 }; 273 #endif 274 int 275 sigprocmask(p, uap, retval) 276 register struct proc *p; 277 struct sigprocmask_args *uap; 278 int *retval; 279 { 280 int error = 0; 281 282 *retval = p->p_sigmask; 283 (void) splhigh(); 284 285 switch (uap->how) { 286 case SIG_BLOCK: 287 p->p_sigmask |= uap->mask &~ sigcantmask; 288 break; 289 290 case SIG_UNBLOCK: 291 p->p_sigmask &= ~uap->mask; 292 break; 293 294 case SIG_SETMASK: 295 p->p_sigmask = uap->mask &~ sigcantmask; 296 break; 297 298 default: 299 error = EINVAL; 300 break; 301 } 302 (void) spl0(); 303 return (error); 304 } 305 306 #ifndef _SYS_SYSPROTO_H_ 307 struct sigpending_args { 308 int dummy; 309 }; 310 #endif 311 /* ARGSUSED */ 312 int 313 sigpending(p, uap, retval) 314 struct proc *p; 315 struct sigpending_args *uap; 316 int *retval; 317 { 318 319 *retval = p->p_siglist; 320 return (0); 321 } 322 323 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 324 /* 325 * Generalized interface signal handler, 4.3-compatible. 326 */ 327 #ifndef _SYS_SYSPROTO_H_ 328 struct osigvec_args { 329 int signum; 330 struct sigvec *nsv; 331 struct sigvec *osv; 332 }; 333 #endif 334 /* ARGSUSED */ 335 int 336 osigvec(p, uap, retval) 337 struct proc *p; 338 register struct osigvec_args *uap; 339 int *retval; 340 { 341 struct sigvec vec; 342 register struct sigacts *ps = p->p_sigacts; 343 register struct sigvec *sv; 344 register int signum; 345 int bit, error; 346 347 signum = uap->signum; 348 if (signum <= 0 || signum >= NSIG) 349 return (EINVAL); 350 sv = &vec; 351 if (uap->osv) { 352 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum]; 353 sv->sv_mask = ps->ps_catchmask[signum]; 354 bit = sigmask(signum); 355 sv->sv_flags = 0; 356 if ((ps->ps_sigonstack & bit) != 0) 357 sv->sv_flags |= SV_ONSTACK; 358 if ((ps->ps_sigintr & bit) != 0) 359 sv->sv_flags |= SV_INTERRUPT; 360 if ((ps->ps_sigreset & bit) != 0) 361 sv->sv_flags |= SV_RESETHAND; 362 if ((ps->ps_signodefer & bit) != 0) 363 sv->sv_flags |= SV_NODEFER; 364 #ifndef COMPAT_SUNOS 365 if (signum == SIGCHLD && p->p_flag & P_NOCLDSTOP) 366 sv->sv_flags |= SV_NOCLDSTOP; 367 #endif 368 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv, 369 sizeof (vec)))) 370 return (error); 371 } 372 if (uap->nsv) { 373 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 374 sizeof (vec)))) 375 return (error); 376 if ((signum == SIGKILL || signum == SIGSTOP) && 377 sv->sv_handler != SIG_DFL) 378 return (EINVAL); 379 #ifdef COMPAT_SUNOS 380 sv->sv_flags |= SA_USERTRAMP; 381 #endif 382 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 383 setsigvec(p, signum, (struct sigaction *)sv); 384 } 385 return (0); 386 } 387 388 #ifndef _SYS_SYSPROTO_H_ 389 struct osigblock_args { 390 int mask; 391 }; 392 #endif 393 int 394 osigblock(p, uap, retval) 395 register struct proc *p; 396 struct osigblock_args *uap; 397 int *retval; 398 { 399 400 (void) splhigh(); 401 *retval = p->p_sigmask; 402 p->p_sigmask |= uap->mask &~ sigcantmask; 403 (void) spl0(); 404 return (0); 405 } 406 407 #ifndef _SYS_SYSPROTO_H_ 408 struct osigsetmask_args { 409 int mask; 410 }; 411 #endif 412 int 413 osigsetmask(p, uap, retval) 414 struct proc *p; 415 struct osigsetmask_args *uap; 416 int *retval; 417 { 418 419 (void) splhigh(); 420 *retval = p->p_sigmask; 421 p->p_sigmask = uap->mask &~ sigcantmask; 422 (void) spl0(); 423 return (0); 424 } 425 #endif /* COMPAT_43 || COMPAT_SUNOS */ 426 427 /* 428 * Suspend process until signal, providing mask to be set 429 * in the meantime. Note nonstandard calling convention: 430 * libc stub passes mask, not pointer, to save a copyin. 431 */ 432 #ifndef _SYS_SYSPROTO_H_ 433 struct sigsuspend_args { 434 sigset_t mask; 435 }; 436 #endif 437 /* ARGSUSED */ 438 int 439 sigsuspend(p, uap, retval) 440 register struct proc *p; 441 struct sigsuspend_args *uap; 442 int *retval; 443 { 444 register struct sigacts *ps = p->p_sigacts; 445 446 /* 447 * When returning from sigpause, we want 448 * the old mask to be restored after the 449 * signal handler has finished. Thus, we 450 * save it here and mark the sigacts structure 451 * to indicate this. 452 */ 453 ps->ps_oldmask = p->p_sigmask; 454 ps->ps_flags |= SAS_OLDMASK; 455 p->p_sigmask = uap->mask &~ sigcantmask; 456 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 457 /* void */; 458 /* always return EINTR rather than ERESTART... */ 459 return (EINTR); 460 } 461 462 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 463 #ifndef _SYS_SYSPROTO_H_ 464 struct osigstack_args { 465 struct sigstack *nss; 466 struct sigstack *oss; 467 }; 468 #endif 469 /* ARGSUSED */ 470 int 471 osigstack(p, uap, retval) 472 struct proc *p; 473 register struct osigstack_args *uap; 474 int *retval; 475 { 476 struct sigstack ss; 477 struct sigacts *psp; 478 int error = 0; 479 480 psp = p->p_sigacts; 481 ss.ss_sp = psp->ps_sigstk.ss_sp; 482 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 483 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss, 484 sizeof (struct sigstack)))) 485 return (error); 486 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 487 sizeof (ss))) == 0) { 488 psp->ps_sigstk.ss_sp = ss.ss_sp; 489 psp->ps_sigstk.ss_size = 0; 490 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 491 psp->ps_flags |= SAS_ALTSTACK; 492 } 493 return (error); 494 } 495 #endif /* COMPAT_43 || COMPAT_SUNOS */ 496 497 #ifndef _SYS_SYSPROTO_H_ 498 struct sigaltstack_args { 499 struct sigaltstack *nss; 500 struct sigaltstack *oss; 501 }; 502 #endif 503 /* ARGSUSED */ 504 int 505 sigaltstack(p, uap, retval) 506 struct proc *p; 507 register struct sigaltstack_args *uap; 508 int *retval; 509 { 510 struct sigacts *psp; 511 struct sigaltstack ss; 512 int error; 513 514 psp = p->p_sigacts; 515 if ((psp->ps_flags & SAS_ALTSTACK) == 0) 516 psp->ps_sigstk.ss_flags |= SS_DISABLE; 517 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk, 518 (caddr_t)uap->oss, sizeof (struct sigaltstack)))) 519 return (error); 520 if (uap->nss == 0) 521 return (0); 522 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))) 523 return (error); 524 if (ss.ss_flags & SS_DISABLE) { 525 if (psp->ps_sigstk.ss_flags & SS_ONSTACK) 526 return (EINVAL); 527 psp->ps_flags &= ~SAS_ALTSTACK; 528 psp->ps_sigstk.ss_flags = ss.ss_flags; 529 return (0); 530 } 531 if (ss.ss_size < MINSIGSTKSZ) 532 return (ENOMEM); 533 psp->ps_flags |= SAS_ALTSTACK; 534 psp->ps_sigstk= ss; 535 return (0); 536 } 537 538 /* 539 * Common code for kill process group/broadcast kill. 540 * cp is calling process. 541 */ 542 int 543 killpg1(cp, signum, pgid, all) 544 register struct proc *cp; 545 int signum, pgid, all; 546 { 547 register struct proc *p; 548 register struct pcred *pc = cp->p_cred; 549 struct pgrp *pgrp; 550 int nfound = 0; 551 552 if (all) 553 /* 554 * broadcast 555 */ 556 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 557 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 558 p == cp || !CANSIGNAL(cp, pc, p, signum)) 559 continue; 560 nfound++; 561 if (signum) 562 psignal(p, signum); 563 } 564 else { 565 if (pgid == 0) 566 /* 567 * zero pgid means send to my process group. 568 */ 569 pgrp = cp->p_pgrp; 570 else { 571 pgrp = pgfind(pgid); 572 if (pgrp == NULL) 573 return (ESRCH); 574 } 575 for (p = pgrp->pg_members.lh_first; p != 0; 576 p = p->p_pglist.le_next) { 577 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 578 p->p_stat == SZOMB || 579 !CANSIGNAL(cp, pc, p, signum)) 580 continue; 581 nfound++; 582 if (signum) 583 psignal(p, signum); 584 } 585 } 586 return (nfound ? 0 : ESRCH); 587 } 588 589 #ifndef _SYS_SYSPROTO_H_ 590 struct kill_args { 591 int pid; 592 int signum; 593 }; 594 #endif 595 /* ARGSUSED */ 596 int 597 kill(cp, uap, retval) 598 register struct proc *cp; 599 register struct kill_args *uap; 600 int *retval; 601 { 602 register struct proc *p; 603 register struct pcred *pc = cp->p_cred; 604 605 if ((u_int)uap->signum >= NSIG) 606 return (EINVAL); 607 if (uap->pid > 0) { 608 /* kill single process */ 609 if ((p = pfind(uap->pid)) == NULL) 610 return (ESRCH); 611 if (!CANSIGNAL(cp, pc, p, uap->signum)) 612 return (EPERM); 613 if (uap->signum) 614 psignal(p, uap->signum); 615 return (0); 616 } 617 switch (uap->pid) { 618 case -1: /* broadcast signal */ 619 return (killpg1(cp, uap->signum, 0, 1)); 620 case 0: /* signal own process group */ 621 return (killpg1(cp, uap->signum, 0, 0)); 622 default: /* negative explicit process group */ 623 return (killpg1(cp, uap->signum, -uap->pid, 0)); 624 } 625 /* NOTREACHED */ 626 } 627 628 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 629 #ifndef _SYS_SYSPROTO_H_ 630 struct okillpg_args { 631 int pgid; 632 int signum; 633 }; 634 #endif 635 /* ARGSUSED */ 636 int 637 okillpg(p, uap, retval) 638 struct proc *p; 639 register struct okillpg_args *uap; 640 int *retval; 641 { 642 643 if ((u_int)uap->signum >= NSIG) 644 return (EINVAL); 645 return (killpg1(p, uap->signum, uap->pgid, 0)); 646 } 647 #endif /* COMPAT_43 || COMPAT_SUNOS */ 648 649 /* 650 * Send a signal to a process group. 651 */ 652 void 653 gsignal(pgid, signum) 654 int pgid, signum; 655 { 656 struct pgrp *pgrp; 657 658 if (pgid && (pgrp = pgfind(pgid))) 659 pgsignal(pgrp, signum, 0); 660 } 661 662 /* 663 * Send a signal to a process group. If checktty is 1, 664 * limit to members which have a controlling terminal. 665 */ 666 void 667 pgsignal(pgrp, signum, checkctty) 668 struct pgrp *pgrp; 669 int signum, checkctty; 670 { 671 register struct proc *p; 672 673 if (pgrp) 674 for (p = pgrp->pg_members.lh_first; p != 0; 675 p = p->p_pglist.le_next) 676 if (checkctty == 0 || p->p_flag & P_CONTROLT) 677 psignal(p, signum); 678 } 679 680 /* 681 * Send a signal caused by a trap to the current process. 682 * If it will be caught immediately, deliver it with correct code. 683 * Otherwise, post it normally. 684 */ 685 void 686 trapsignal(p, signum, code) 687 struct proc *p; 688 register int signum; 689 u_long code; 690 { 691 register struct sigacts *ps = p->p_sigacts; 692 int mask; 693 694 mask = sigmask(signum); 695 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 && 696 (p->p_sigmask & mask) == 0) { 697 p->p_stats->p_ru.ru_nsignals++; 698 #ifdef KTRACE 699 if (KTRPOINT(p, KTR_PSIG)) 700 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum], 701 p->p_sigmask, code); 702 #endif 703 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum, 704 p->p_sigmask, code); 705 p->p_sigmask |= ps->ps_catchmask[signum] | 706 (mask & ~ps->ps_signodefer); 707 if ((ps->ps_sigreset & mask) != 0) { 708 /* 709 * See setsigvec() for origin of this code. 710 */ 711 p->p_sigcatch &= ~mask; 712 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 713 p->p_sigignore |= mask; 714 ps->ps_sigact[signum] = SIG_DFL; 715 } 716 } else { 717 ps->ps_code = code; /* XXX for core dump/debugger */ 718 ps->ps_sig = signum; /* XXX to verify code */ 719 psignal(p, signum); 720 } 721 } 722 723 /* 724 * Send the signal to the process. If the signal has an action, the action 725 * is usually performed by the target process rather than the caller; we add 726 * the signal to the set of pending signals for the process. 727 * 728 * Exceptions: 729 * o When a stop signal is sent to a sleeping process that takes the 730 * default action, the process is stopped without awakening it. 731 * o SIGCONT restarts stopped processes (or puts them back to sleep) 732 * regardless of the signal action (eg, blocked or ignored). 733 * 734 * Other ignored signals are discarded immediately. 735 */ 736 void 737 psignal(p, signum) 738 register struct proc *p; 739 register int signum; 740 { 741 register int s, prop; 742 register sig_t action; 743 int mask; 744 745 if ((u_int)signum >= NSIG || signum == 0) 746 panic("psignal signal number"); 747 mask = sigmask(signum); 748 prop = sigprop[signum]; 749 750 /* 751 * If proc is traced, always give parent a chance. 752 */ 753 if (p->p_flag & P_TRACED) 754 action = SIG_DFL; 755 else { 756 /* 757 * If the signal is being ignored, 758 * then we forget about it immediately. 759 * (Note: we don't set SIGCONT in p_sigignore, 760 * and if it is set to SIG_IGN, 761 * action will be SIG_DFL here.) 762 */ 763 if (p->p_sigignore & mask) 764 return; 765 if (p->p_sigmask & mask) 766 action = SIG_HOLD; 767 else if (p->p_sigcatch & mask) 768 action = SIG_CATCH; 769 else 770 action = SIG_DFL; 771 } 772 773 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 774 (p->p_flag & P_TRACED) == 0) 775 p->p_nice = NZERO; 776 777 if (prop & SA_CONT) 778 p->p_siglist &= ~stopsigmask; 779 780 if (prop & SA_STOP) { 781 /* 782 * If sending a tty stop signal to a member of an orphaned 783 * process group, discard the signal here if the action 784 * is default; don't stop the process below if sleeping, 785 * and don't clear any pending SIGCONT. 786 */ 787 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 788 action == SIG_DFL) 789 return; 790 p->p_siglist &= ~contsigmask; 791 } 792 p->p_siglist |= mask; 793 794 /* 795 * Defer further processing for signals which are held, 796 * except that stopped processes must be continued by SIGCONT. 797 */ 798 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 799 return; 800 s = splhigh(); 801 switch (p->p_stat) { 802 803 case SSLEEP: 804 /* 805 * If process is sleeping uninterruptibly 806 * we can't interrupt the sleep... the signal will 807 * be noticed when the process returns through 808 * trap() or syscall(). 809 */ 810 if ((p->p_flag & P_SINTR) == 0) 811 goto out; 812 /* 813 * Process is sleeping and traced... make it runnable 814 * so it can discover the signal in issignal() and stop 815 * for the parent. 816 */ 817 if (p->p_flag & P_TRACED) 818 goto run; 819 /* 820 * If SIGCONT is default (or ignored) and process is 821 * asleep, we are finished; the process should not 822 * be awakened. 823 */ 824 if ((prop & SA_CONT) && action == SIG_DFL) { 825 p->p_siglist &= ~mask; 826 goto out; 827 } 828 /* 829 * When a sleeping process receives a stop 830 * signal, process immediately if possible. 831 * All other (caught or default) signals 832 * cause the process to run. 833 */ 834 if (prop & SA_STOP) { 835 if (action != SIG_DFL) 836 goto runfast; 837 /* 838 * If a child holding parent blocked, 839 * stopping could cause deadlock. 840 */ 841 if (p->p_flag & P_PPWAIT) 842 goto out; 843 p->p_siglist &= ~mask; 844 p->p_xstat = signum; 845 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 846 psignal(p->p_pptr, SIGCHLD); 847 stop(p); 848 goto out; 849 } else 850 goto runfast; 851 /*NOTREACHED*/ 852 853 case SSTOP: 854 /* 855 * If traced process is already stopped, 856 * then no further action is necessary. 857 */ 858 if (p->p_flag & P_TRACED) 859 goto out; 860 861 /* 862 * Kill signal always sets processes running. 863 */ 864 if (signum == SIGKILL) 865 goto runfast; 866 867 if (prop & SA_CONT) { 868 /* 869 * If SIGCONT is default (or ignored), we continue the 870 * process but don't leave the signal in p_siglist, as 871 * it has no further action. If SIGCONT is held, we 872 * continue the process and leave the signal in 873 * p_siglist. If the process catches SIGCONT, let it 874 * handle the signal itself. If it isn't waiting on 875 * an event, then it goes back to run state. 876 * Otherwise, process goes back to sleep state. 877 */ 878 if (action == SIG_DFL) 879 p->p_siglist &= ~mask; 880 if (action == SIG_CATCH) 881 goto runfast; 882 if (p->p_wchan == 0) 883 goto run; 884 p->p_stat = SSLEEP; 885 goto out; 886 } 887 888 if (prop & SA_STOP) { 889 /* 890 * Already stopped, don't need to stop again. 891 * (If we did the shell could get confused.) 892 */ 893 p->p_siglist &= ~mask; /* take it away */ 894 goto out; 895 } 896 897 /* 898 * If process is sleeping interruptibly, then simulate a 899 * wakeup so that when it is continued, it will be made 900 * runnable and can look at the signal. But don't make 901 * the process runnable, leave it stopped. 902 */ 903 if (p->p_wchan && p->p_flag & P_SINTR) 904 unsleep(p); 905 goto out; 906 907 default: 908 /* 909 * SRUN, SIDL, SZOMB do nothing with the signal, 910 * other than kicking ourselves if we are running. 911 * It will either never be noticed, or noticed very soon. 912 */ 913 if (p == curproc) 914 signotify(p); 915 goto out; 916 } 917 /*NOTREACHED*/ 918 919 runfast: 920 /* 921 * Raise priority to at least PUSER. 922 */ 923 if (p->p_priority > PUSER) 924 p->p_priority = PUSER; 925 run: 926 setrunnable(p); 927 out: 928 splx(s); 929 } 930 931 /* 932 * If the current process has received a signal (should be caught or cause 933 * termination, should interrupt current syscall), return the signal number. 934 * Stop signals with default action are processed immediately, then cleared; 935 * they aren't returned. This is checked after each entry to the system for 936 * a syscall or trap (though this can usually be done without calling issignal 937 * by checking the pending signal masks in the CURSIG macro.) The normal call 938 * sequence is 939 * 940 * while (signum = CURSIG(curproc)) 941 * postsig(signum); 942 */ 943 int 944 issignal(p) 945 register struct proc *p; 946 { 947 register int signum, mask, prop; 948 949 for (;;) { 950 mask = p->p_siglist & ~p->p_sigmask; 951 if (p->p_flag & P_PPWAIT) 952 mask &= ~stopsigmask; 953 if (mask == 0) /* no signal to send */ 954 return (0); 955 signum = ffs((long)mask); 956 mask = sigmask(signum); 957 prop = sigprop[signum]; 958 /* 959 * We should see pending but ignored signals 960 * only if P_TRACED was on when they were posted. 961 */ 962 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) { 963 p->p_siglist &= ~mask; 964 continue; 965 } 966 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 967 /* 968 * If traced, always stop, and stay 969 * stopped until released by the parent. 970 */ 971 p->p_xstat = signum; 972 psignal(p->p_pptr, SIGCHLD); 973 do { 974 stop(p); 975 mi_switch(); 976 } while (!trace_req(p) && p->p_flag & P_TRACED); 977 978 /* 979 * If the traced bit got turned off, go back up 980 * to the top to rescan signals. This ensures 981 * that p_sig* and ps_sigact are consistent. 982 */ 983 if ((p->p_flag & P_TRACED) == 0) 984 continue; 985 986 /* 987 * If parent wants us to take the signal, 988 * then it will leave it in p->p_xstat; 989 * otherwise we just look for signals again. 990 */ 991 p->p_siglist &= ~mask; /* clear the old signal */ 992 signum = p->p_xstat; 993 if (signum == 0) 994 continue; 995 996 /* 997 * Put the new signal into p_siglist. If the 998 * signal is being masked, look for other signals. 999 */ 1000 mask = sigmask(signum); 1001 p->p_siglist |= mask; 1002 if (p->p_sigmask & mask) 1003 continue; 1004 } 1005 1006 /* 1007 * Decide whether the signal should be returned. 1008 * Return the signal's number, or fall through 1009 * to clear it from the pending mask. 1010 */ 1011 switch ((int)p->p_sigacts->ps_sigact[signum]) { 1012 1013 case (int)SIG_DFL: 1014 /* 1015 * Don't take default actions on system processes. 1016 */ 1017 if (p->p_pid <= 1) { 1018 #ifdef DIAGNOSTIC 1019 /* 1020 * Are you sure you want to ignore SIGSEGV 1021 * in init? XXX 1022 */ 1023 printf("Process (pid %lu) got signal %d\n", 1024 (u_long)p->p_pid, signum); 1025 #endif 1026 break; /* == ignore */ 1027 } 1028 /* 1029 * If there is a pending stop signal to process 1030 * with default action, stop here, 1031 * then clear the signal. However, 1032 * if process is member of an orphaned 1033 * process group, ignore tty stop signals. 1034 */ 1035 if (prop & SA_STOP) { 1036 if (p->p_flag & P_TRACED || 1037 (p->p_pgrp->pg_jobc == 0 && 1038 prop & SA_TTYSTOP)) 1039 break; /* == ignore */ 1040 p->p_xstat = signum; 1041 stop(p); 1042 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1043 psignal(p->p_pptr, SIGCHLD); 1044 mi_switch(); 1045 break; 1046 } else if (prop & SA_IGNORE) { 1047 /* 1048 * Except for SIGCONT, shouldn't get here. 1049 * Default action is to ignore; drop it. 1050 */ 1051 break; /* == ignore */ 1052 } else 1053 return (signum); 1054 /*NOTREACHED*/ 1055 1056 case (int)SIG_IGN: 1057 /* 1058 * Masking above should prevent us ever trying 1059 * to take action on an ignored signal other 1060 * than SIGCONT, unless process is traced. 1061 */ 1062 if ((prop & SA_CONT) == 0 && 1063 (p->p_flag & P_TRACED) == 0) 1064 printf("issignal\n"); 1065 break; /* == ignore */ 1066 1067 default: 1068 /* 1069 * This signal has an action, let 1070 * postsig() process it. 1071 */ 1072 return (signum); 1073 } 1074 p->p_siglist &= ~mask; /* take the signal! */ 1075 } 1076 /* NOTREACHED */ 1077 } 1078 1079 /* 1080 * Put the argument process into the stopped state and notify the parent 1081 * via wakeup. Signals are handled elsewhere. The process must not be 1082 * on the run queue. 1083 */ 1084 void 1085 stop(p) 1086 register struct proc *p; 1087 { 1088 1089 p->p_stat = SSTOP; 1090 p->p_flag &= ~P_WAITED; 1091 wakeup((caddr_t)p->p_pptr); 1092 } 1093 1094 /* 1095 * Take the action for the specified signal 1096 * from the current set of pending signals. 1097 */ 1098 void 1099 postsig(signum) 1100 register int signum; 1101 { 1102 register struct proc *p = curproc; 1103 register struct sigacts *ps = p->p_sigacts; 1104 register sig_t action; 1105 int code, mask, returnmask; 1106 1107 #ifdef DIAGNOSTIC 1108 if (signum == 0) 1109 panic("postsig"); 1110 #endif 1111 mask = sigmask(signum); 1112 p->p_siglist &= ~mask; 1113 action = ps->ps_sigact[signum]; 1114 #ifdef KTRACE 1115 if (KTRPOINT(p, KTR_PSIG)) 1116 ktrpsig(p->p_tracep, 1117 signum, action, ps->ps_flags & SAS_OLDMASK ? 1118 ps->ps_oldmask : p->p_sigmask, 0); 1119 #endif 1120 if (action == SIG_DFL) { 1121 /* 1122 * Default action, where the default is to kill 1123 * the process. (Other cases were ignored above.) 1124 */ 1125 sigexit(p, signum); 1126 /* NOTREACHED */ 1127 } else { 1128 /* 1129 * If we get here, the signal must be caught. 1130 */ 1131 #ifdef DIAGNOSTIC 1132 if (action == SIG_IGN || (p->p_sigmask & mask)) 1133 panic("postsig action"); 1134 #endif 1135 /* 1136 * Set the new mask value and also defer further 1137 * occurences of this signal. 1138 * 1139 * Special case: user has done a sigpause. Here the 1140 * current mask is not of interest, but rather the 1141 * mask from before the sigpause is what we want 1142 * restored after the signal processing is completed. 1143 */ 1144 (void) splhigh(); 1145 if (ps->ps_flags & SAS_OLDMASK) { 1146 returnmask = ps->ps_oldmask; 1147 ps->ps_flags &= ~SAS_OLDMASK; 1148 } else 1149 returnmask = p->p_sigmask; 1150 p->p_sigmask |= ps->ps_catchmask[signum] | 1151 (mask & ~ps->ps_signodefer); 1152 if ((ps->ps_sigreset & mask) != 0) { 1153 /* 1154 * See setsigvec() for origin of this code. 1155 */ 1156 p->p_sigcatch &= ~mask; 1157 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1158 p->p_sigignore |= mask; 1159 ps->ps_sigact[signum] = SIG_DFL; 1160 } 1161 (void) spl0(); 1162 p->p_stats->p_ru.ru_nsignals++; 1163 if (ps->ps_sig != signum) { 1164 code = 0; 1165 } else { 1166 code = ps->ps_code; 1167 ps->ps_code = 0; 1168 ps->ps_sig = 0; 1169 } 1170 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code); 1171 } 1172 } 1173 1174 /* 1175 * Kill the current process for stated reason. 1176 */ 1177 void 1178 killproc(p, why) 1179 struct proc *p; 1180 char *why; 1181 { 1182 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1183 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1184 psignal(p, SIGKILL); 1185 } 1186 1187 /* 1188 * Force the current process to exit with the specified signal, dumping core 1189 * if appropriate. We bypass the normal tests for masked and caught signals, 1190 * allowing unrecoverable failures to terminate the process without changing 1191 * signal state. Mark the accounting record with the signal termination. 1192 * If dumping core, save the signal number for the debugger. Calls exit and 1193 * does not return. 1194 */ 1195 void 1196 sigexit(p, signum) 1197 register struct proc *p; 1198 int signum; 1199 { 1200 1201 p->p_acflag |= AXSIG; 1202 if (sigprop[signum] & SA_CORE) { 1203 p->p_sigacts->ps_sig = signum; 1204 /* 1205 * Log signals which would cause core dumps 1206 * (Log as LOG_INFO to appease those who don't want 1207 * these messages.) 1208 * XXX : Todo, as well as euid, write out ruid too 1209 */ 1210 if (coredump(p) == 0) 1211 signum |= WCOREFLAG; 1212 log(LOG_INFO, "pid %d (%s), uid %d: exited on signal %d%s\n", 1213 p->p_pid, p->p_comm, 1214 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1215 signum &~ WCOREFLAG, 1216 signum & WCOREFLAG ? " (core dumped)" : ""); 1217 } 1218 exit1(p, W_EXITCODE(0, signum)); 1219 /* NOTREACHED */ 1220 } 1221 1222 /* 1223 * Dump core, into a file named "progname.core", unless the process was 1224 * setuid/setgid. 1225 */ 1226 static int 1227 coredump(p) 1228 register struct proc *p; 1229 { 1230 register struct vnode *vp; 1231 register struct ucred *cred = p->p_cred->pc_ucred; 1232 register struct vmspace *vm = p->p_vmspace; 1233 struct nameidata nd; 1234 struct vattr vattr; 1235 int error, error1; 1236 char name[MAXCOMLEN+6]; /* progname.core */ 1237 1238 if (p->p_flag & P_SUGID) 1239 return (EFAULT); 1240 if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >= 1241 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1242 return (EFAULT); 1243 sprintf(name, "%s.core", p->p_comm); 1244 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p); 1245 if ((error = vn_open(&nd, 1246 O_CREAT | FWRITE, S_IRUSR | S_IWUSR))) 1247 return (error); 1248 vp = nd.ni_vp; 1249 1250 /* Don't dump to non-regular files or files with links. */ 1251 if (vp->v_type != VREG || 1252 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1253 error = EFAULT; 1254 goto out; 1255 } 1256 VATTR_NULL(&vattr); 1257 vattr.va_size = 0; 1258 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1259 VOP_SETATTR(vp, &vattr, cred, p); 1260 p->p_acflag |= ACORE; 1261 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc)); 1262 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); 1263 error = cpu_coredump(p, vp, cred); 1264 if (error == 0) 1265 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr, 1266 (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 1267 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1268 if (error == 0) 1269 error = vn_rdwr(UIO_WRITE, vp, 1270 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)), 1271 round_page(ctob(vm->vm_ssize)), 1272 (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE, 1273 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p); 1274 out: 1275 VOP_UNLOCK(vp, 0, p); 1276 error1 = vn_close(vp, FWRITE, cred, p); 1277 if (error == 0) 1278 error = error1; 1279 return (error); 1280 } 1281 1282 /* 1283 * Nonexistent system call-- signal process (may want to handle it). 1284 * Flag error in case process won't see signal immediately (blocked or ignored). 1285 */ 1286 #ifndef _SYS_SYSPROTO_H_ 1287 struct nosys_args { 1288 int dummy; 1289 }; 1290 #endif 1291 /* ARGSUSED */ 1292 int 1293 nosys(p, args, retval) 1294 struct proc *p; 1295 struct nosys_args *args; 1296 int *retval; 1297 { 1298 1299 psignal(p, SIGSYS); 1300 return (EINVAL); 1301 } 1302