1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)kern_sig.c 7.3.1.1 (Berkeley) 05/01/89 18 */ 19 20 #include "machine/reg.h" 21 #include "machine/pte.h" 22 #include "machine/psl.h" 23 #include "machine/mtpr.h" 24 25 #include "param.h" 26 #include "systm.h" 27 #include "dir.h" 28 #include "ucred.h" 29 #include "user.h" 30 #include "vnode.h" 31 #include "proc.h" 32 #include "timeb.h" 33 #include "times.h" 34 #include "buf.h" 35 #include "mount.h" 36 #include "text.h" 37 #include "seg.h" 38 #include "vm.h" 39 #include "acct.h" 40 #include "uio.h" 41 #include "file.h" 42 #include "kernel.h" 43 44 #define cantmask (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)) 45 #define stopsigmask (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \ 46 sigmask(SIGTTIN)|sigmask(SIGTTOU)) 47 48 /* 49 * Generalized interface signal handler. 50 */ 51 sigvec() 52 { 53 register struct a { 54 int signo; 55 struct sigvec *nsv; 56 struct sigvec *osv; 57 } *uap = (struct a *)u.u_ap; 58 struct sigvec vec; 59 register struct sigvec *sv; 60 register int sig; 61 int bit; 62 63 sig = uap->signo; 64 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) { 65 u.u_error = EINVAL; 66 return; 67 } 68 sv = &vec; 69 if (uap->osv) { 70 sv->sv_handler = u.u_signal[sig]; 71 sv->sv_mask = u.u_sigmask[sig]; 72 bit = sigmask(sig); 73 sv->sv_flags = 0; 74 if ((u.u_sigonstack & bit) != 0) 75 sv->sv_flags |= SV_ONSTACK; 76 if ((u.u_sigintr & bit) != 0) 77 sv->sv_flags |= SV_INTERRUPT; 78 u.u_error = 79 copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec)); 80 if (u.u_error) 81 return; 82 } 83 if (uap->nsv) { 84 u.u_error = 85 copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec)); 86 if (u.u_error) 87 return; 88 if (sig == SIGCONT && sv->sv_handler == SIG_IGN) { 89 u.u_error = EINVAL; 90 return; 91 } 92 setsigvec(sig, sv); 93 } 94 } 95 96 setsigvec(sig, sv) 97 int sig; 98 register struct sigvec *sv; 99 { 100 register struct proc *p; 101 register int bit; 102 103 bit = sigmask(sig); 104 p = u.u_procp; 105 /* 106 * Change setting atomically. 107 */ 108 (void) splhigh(); 109 u.u_signal[sig] = sv->sv_handler; 110 u.u_sigmask[sig] = sv->sv_mask &~ cantmask; 111 if (sv->sv_flags & SV_INTERRUPT) 112 u.u_sigintr |= bit; 113 else 114 u.u_sigintr &= ~bit; 115 if (sv->sv_flags & SV_ONSTACK) 116 u.u_sigonstack |= bit; 117 else 118 u.u_sigonstack &= ~bit; 119 if (sv->sv_handler == SIG_IGN) { 120 p->p_sig &= ~bit; /* never to be seen again */ 121 p->p_sigignore |= bit; 122 p->p_sigcatch &= ~bit; 123 } else { 124 p->p_sigignore &= ~bit; 125 if (sv->sv_handler == SIG_DFL) 126 p->p_sigcatch &= ~bit; 127 else 128 p->p_sigcatch |= bit; 129 } 130 (void) spl0(); 131 } 132 133 sigblock() 134 { 135 struct a { 136 int mask; 137 } *uap = (struct a *)u.u_ap; 138 register struct proc *p = u.u_procp; 139 140 (void) splhigh(); 141 u.u_r.r_val1 = p->p_sigmask; 142 p->p_sigmask |= uap->mask &~ cantmask; 143 (void) spl0(); 144 } 145 146 sigsetmask() 147 { 148 struct a { 149 int mask; 150 } *uap = (struct a *)u.u_ap; 151 register struct proc *p = u.u_procp; 152 153 (void) splhigh(); 154 u.u_r.r_val1 = p->p_sigmask; 155 p->p_sigmask = uap->mask &~ cantmask; 156 (void) spl0(); 157 } 158 159 sigpause() 160 { 161 struct a { 162 int mask; 163 } *uap = (struct a *)u.u_ap; 164 register struct proc *p = u.u_procp; 165 166 /* 167 * When returning from sigpause, we want 168 * the old mask to be restored after the 169 * signal handler has finished. Thus, we 170 * save it here and mark the proc structure 171 * to indicate this (should be in u.). 172 */ 173 u.u_oldmask = p->p_sigmask; 174 p->p_flag |= SOMASK; 175 p->p_sigmask = uap->mask &~ cantmask; 176 for (;;) 177 sleep((caddr_t)&u, PSLEP); 178 /*NOTREACHED*/ 179 } 180 #undef cantmask 181 182 sigstack() 183 { 184 register struct a { 185 struct sigstack *nss; 186 struct sigstack *oss; 187 } *uap = (struct a *)u.u_ap; 188 struct sigstack ss; 189 190 if (uap->oss) { 191 u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss, 192 sizeof (struct sigstack)); 193 if (u.u_error) 194 return; 195 } 196 if (uap->nss) { 197 u.u_error = 198 copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)); 199 if (u.u_error == 0) 200 u.u_sigstack = ss; 201 } 202 } 203 204 kill() 205 { 206 register struct a { 207 int pid; 208 int signo; 209 } *uap = (struct a *)u.u_ap; 210 register struct proc *p; 211 212 if (uap->signo < 0 || uap->signo > NSIG) { 213 u.u_error = EINVAL; 214 return; 215 } 216 if (uap->pid > 0) { 217 /* kill single process */ 218 p = pfind(uap->pid); 219 if (p == 0) { 220 u.u_error = ESRCH; 221 return; 222 } 223 if (u.u_uid && u.u_uid != p->p_uid) 224 u.u_error = EPERM; 225 else if (uap->signo) 226 psignal(p, uap->signo); 227 return; 228 } 229 switch (uap->pid) { 230 case -1: /* broadcast signal */ 231 u.u_error = killpg1(uap->signo, 0, 1); 232 break; 233 case 0: /* signal own process group */ 234 u.u_error = killpg1(uap->signo, 0, 0); 235 break; 236 default: /* negative explicit process group */ 237 u.u_error = killpg1(uap->signo, -uap->pid, 0); 238 break; 239 } 240 return; 241 } 242 243 killpg() 244 { 245 register struct a { 246 int pgrp; 247 int signo; 248 } *uap = (struct a *)u.u_ap; 249 250 if (uap->signo < 0 || uap->signo > NSIG) { 251 u.u_error = EINVAL; 252 return; 253 } 254 u.u_error = killpg1(uap->signo, uap->pgrp, 0); 255 } 256 257 /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */ 258 259 killpg1(signo, pgrp, all) 260 int signo, pgrp, all; 261 { 262 register struct proc *p; 263 int f, error = 0; 264 265 if (!all && pgrp == 0) { 266 /* 267 * Zero process id means send to my process group. 268 */ 269 pgrp = u.u_procp->p_pgrp; 270 if (pgrp == 0) 271 return (ESRCH); 272 } 273 for (f = 0, p = allproc; p != NULL; p = p->p_nxt) { 274 if ((p->p_pgrp != pgrp && !all) || p->p_ppid == 0 || 275 (p->p_flag&SSYS) || (all && p == u.u_procp)) 276 continue; 277 if (u.u_uid != 0 && u.u_uid != p->p_uid && 278 (signo != SIGCONT || !inferior(p))) { 279 if (!all) 280 error = EPERM; 281 continue; 282 } 283 f++; 284 if (signo) 285 psignal(p, signo); 286 } 287 return (error ? error : (f == 0 ? ESRCH : 0)); 288 } 289 290 /* 291 * Send the specified signal to 292 * all processes with 'pgrp' as 293 * process group. 294 */ 295 gsignal(pgrp, sig) 296 register int pgrp; 297 { 298 register struct proc *p; 299 300 if (pgrp == 0) 301 return; 302 for (p = allproc; p != NULL; p = p->p_nxt) 303 if (p->p_pgrp == pgrp) 304 psignal(p, sig); 305 } 306 307 /* 308 * Send the specified signal to 309 * the specified process. 310 */ 311 psignal(p, sig) 312 register struct proc *p; 313 register int sig; 314 { 315 register int s; 316 register int (*action)(); 317 int mask; 318 319 if ((unsigned)sig >= NSIG) 320 return; 321 mask = sigmask(sig); 322 323 /* 324 * If proc is traced, always give parent a chance. 325 */ 326 if (p->p_flag & STRC) 327 action = SIG_DFL; 328 else { 329 /* 330 * If the signal is being ignored, 331 * then we forget about it immediately. 332 */ 333 if (p->p_sigignore & mask) 334 return; 335 if (p->p_sigmask & mask) 336 action = SIG_HOLD; 337 else if (p->p_sigcatch & mask) 338 action = SIG_CATCH; 339 else 340 action = SIG_DFL; 341 } 342 if (sig) { 343 p->p_sig |= mask; 344 switch (sig) { 345 346 case SIGTERM: 347 if ((p->p_flag&STRC) || action != SIG_DFL) 348 break; 349 /* fall into ... */ 350 351 case SIGKILL: 352 if (p->p_nice > NZERO) 353 p->p_nice = NZERO; 354 break; 355 356 case SIGCONT: 357 p->p_sig &= ~stopsigmask; 358 break; 359 360 case SIGSTOP: 361 case SIGTSTP: 362 case SIGTTIN: 363 case SIGTTOU: 364 p->p_sig &= ~sigmask(SIGCONT); 365 break; 366 } 367 } 368 /* 369 * Defer further processing for signals which are held. 370 */ 371 if (action == SIG_HOLD) 372 return; 373 s = splhigh(); 374 switch (p->p_stat) { 375 376 case SSLEEP: 377 /* 378 * If process is sleeping at negative priority 379 * we can't interrupt the sleep... the signal will 380 * be noticed when the process returns through 381 * trap() or syscall(). 382 */ 383 if (p->p_pri <= PZERO) 384 goto out; 385 /* 386 * Process is sleeping and traced... make it runnable 387 * so it can discover the signal in issig() and stop 388 * for the parent. 389 */ 390 if (p->p_flag&STRC) 391 goto run; 392 switch (sig) { 393 394 case SIGSTOP: 395 case SIGTSTP: 396 case SIGTTIN: 397 case SIGTTOU: 398 /* 399 * These are the signals which by default 400 * stop a process. 401 */ 402 if (action != SIG_DFL) 403 goto run; 404 /* 405 * Don't clog system with children of init 406 * stopped from the keyboard. 407 */ 408 if (sig != SIGSTOP && p->p_pptr == &proc[1]) { 409 psignal(p, SIGKILL); 410 p->p_sig &= ~mask; 411 splx(s); 412 return; 413 } 414 /* 415 * If a child in vfork(), stopping could 416 * cause deadlock. 417 */ 418 if (p->p_flag&SVFORK) 419 goto out; 420 p->p_sig &= ~mask; 421 p->p_cursig = sig; 422 psignal(p->p_pptr, SIGCHLD); 423 stop(p); 424 goto out; 425 426 case SIGIO: 427 case SIGURG: 428 case SIGCHLD: 429 case SIGWINCH: 430 /* 431 * These signals are special in that they 432 * don't get propogated... if the process 433 * isn't interested, forget it. 434 */ 435 if (action != SIG_DFL) 436 goto run; 437 p->p_sig &= ~mask; /* take it away */ 438 goto out; 439 440 default: 441 /* 442 * All other signals cause the process to run 443 */ 444 goto run; 445 } 446 /*NOTREACHED*/ 447 448 case SSTOP: 449 /* 450 * If traced process is already stopped, 451 * then no further action is necessary. 452 */ 453 if (p->p_flag&STRC) 454 goto out; 455 switch (sig) { 456 457 case SIGKILL: 458 /* 459 * Kill signal always sets processes running. 460 */ 461 goto run; 462 463 case SIGCONT: 464 /* 465 * If the process catches SIGCONT, let it handle 466 * the signal itself. If it isn't waiting on 467 * an event, then it goes back to run state. 468 * Otherwise, process goes back to sleep state. 469 */ 470 if (action != SIG_DFL || p->p_wchan == 0) 471 goto run; 472 p->p_stat = SSLEEP; 473 goto out; 474 475 case SIGSTOP: 476 case SIGTSTP: 477 case SIGTTIN: 478 case SIGTTOU: 479 /* 480 * Already stopped, don't need to stop again. 481 * (If we did the shell could get confused.) 482 */ 483 p->p_sig &= ~mask; /* take it away */ 484 goto out; 485 486 default: 487 /* 488 * If process is sleeping interruptibly, then 489 * unstick it so that when it is continued 490 * it can look at the signal. 491 * But don't setrun the process as its not to 492 * be unstopped by the signal alone. 493 */ 494 if (p->p_wchan && p->p_pri > PZERO) 495 unsleep(p); 496 goto out; 497 } 498 /*NOTREACHED*/ 499 500 default: 501 /* 502 * SRUN, SIDL, SZOMB do nothing with the signal, 503 * other than kicking ourselves if we are running. 504 * It will either never be noticed, or noticed very soon. 505 */ 506 if (p == u.u_procp && !noproc) 507 aston(); 508 goto out; 509 } 510 /*NOTREACHED*/ 511 run: 512 /* 513 * Raise priority to at least PUSER. 514 */ 515 if (p->p_pri > PUSER) 516 p->p_pri = PUSER; 517 setrun(p); 518 out: 519 splx(s); 520 } 521 522 /* 523 * Returns true if the current 524 * process has a signal to process. 525 * The signal to process is put in p_cursig. 526 * This is asked at least once each time a process enters the 527 * system (though this can usually be done without actually 528 * calling issig by checking the pending signal masks.) 529 * A signal does not do anything 530 * directly to a process; it sets 531 * a flag that asks the process to 532 * do something to itself. 533 */ 534 issig() 535 { 536 register struct proc *p; 537 register int sig; 538 int sigbits, mask; 539 540 p = u.u_procp; 541 for (;;) { 542 sigbits = p->p_sig &~ p->p_sigmask; 543 if ((p->p_flag&STRC) == 0) 544 sigbits &= ~p->p_sigignore; 545 if (p->p_flag&SVFORK) 546 sigbits &= ~stopsigmask; 547 if (sigbits == 0) 548 break; 549 sig = ffs((long)sigbits); 550 mask = sigmask(sig); 551 p->p_sig &= ~mask; /* take the signal! */ 552 p->p_cursig = sig; 553 if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 554 /* 555 * If traced, always stop, and stay 556 * stopped until released by the parent. 557 */ 558 psignal(p->p_pptr, SIGCHLD); 559 do { 560 stop(p); 561 swtch(); 562 } while (!procxmt() && p->p_flag&STRC); 563 564 /* 565 * If the traced bit got turned off, 566 * then put the signal taken above back into p_sig 567 * and go back up to the top to rescan signals. 568 * This ensures that p_sig* and u_signal are consistent. 569 */ 570 if ((p->p_flag&STRC) == 0) { 571 p->p_sig |= mask; 572 continue; 573 } 574 575 /* 576 * If parent wants us to take the signal, 577 * then it will leave it in p->p_cursig; 578 * otherwise we just look for signals again. 579 */ 580 sig = p->p_cursig; 581 if (sig == 0) 582 continue; 583 584 /* 585 * If signal is being masked put it back 586 * into p_sig and look for other signals. 587 */ 588 mask = sigmask(sig); 589 if (p->p_sigmask & mask) { 590 p->p_sig |= mask; 591 continue; 592 } 593 } 594 switch ((int)u.u_signal[sig]) { 595 596 case SIG_DFL: 597 /* 598 * Don't take default actions on system processes. 599 */ 600 if (p->p_ppid == 0) 601 break; 602 switch (sig) { 603 604 case SIGTSTP: 605 case SIGTTIN: 606 case SIGTTOU: 607 /* 608 * Children of init aren't allowed to stop 609 * on signals from the keyboard. 610 */ 611 if (p->p_pptr == &proc[1]) { 612 psignal(p, SIGKILL); 613 continue; 614 } 615 /* fall into ... */ 616 617 case SIGSTOP: 618 if (p->p_flag&STRC) 619 continue; 620 psignal(p->p_pptr, SIGCHLD); 621 stop(p); 622 swtch(); 623 continue; 624 625 case SIGCONT: 626 case SIGCHLD: 627 case SIGURG: 628 case SIGIO: 629 case SIGWINCH: 630 /* 631 * These signals are normally not 632 * sent if the action is the default. 633 */ 634 continue; /* == ignore */ 635 636 default: 637 goto send; 638 } 639 /*NOTREACHED*/ 640 641 case SIG_HOLD: 642 case SIG_IGN: 643 /* 644 * Masking above should prevent us 645 * ever trying to take action on a held 646 * or ignored signal, unless process is traced. 647 */ 648 if ((p->p_flag&STRC) == 0) 649 printf("issig\n"); 650 continue; 651 652 default: 653 /* 654 * This signal has an action, let 655 * psig process it. 656 */ 657 goto send; 658 } 659 /*NOTREACHED*/ 660 } 661 /* 662 * Didn't find a signal to send. 663 */ 664 p->p_cursig = 0; 665 return (0); 666 667 send: 668 /* 669 * Let psig process the signal. 670 */ 671 return (sig); 672 } 673 674 /* 675 * Put the argument process into the stopped 676 * state and notify the parent via wakeup. 677 * Signals are handled elsewhere. 678 */ 679 stop(p) 680 register struct proc *p; 681 { 682 683 p->p_stat = SSTOP; 684 p->p_flag &= ~SWTED; 685 wakeup((caddr_t)p->p_pptr); 686 } 687 688 /* 689 * Perform the action specified by 690 * the current signal. 691 * The usual sequence is: 692 * if (issig()) 693 * psig(); 694 * The signal bit has already been cleared by issig, 695 * and the current signal number stored in p->p_cursig. 696 */ 697 psig() 698 { 699 register struct proc *p = u.u_procp; 700 register int sig = p->p_cursig; 701 int mask = sigmask(sig), returnmask; 702 register int (*action)(); 703 704 if (sig == 0) 705 panic("psig"); 706 action = u.u_signal[sig]; 707 if (action != SIG_DFL) { 708 if (action == SIG_IGN || (p->p_sigmask & mask)) 709 panic("psig action"); 710 u.u_error = 0; 711 /* 712 * Set the new mask value and also defer further 713 * occurences of this signal (unless we're simulating 714 * the old signal facilities). 715 * 716 * Special case: user has done a sigpause. Here the 717 * current mask is not of interest, but rather the 718 * mask from before the sigpause is what we want restored 719 * after the signal processing is completed. 720 */ 721 (void) splhigh(); 722 if (p->p_flag & SOUSIG) { 723 if (sig != SIGILL && sig != SIGTRAP) { 724 u.u_signal[sig] = SIG_DFL; 725 p->p_sigcatch &= ~mask; 726 } 727 mask = 0; 728 } 729 if (p->p_flag & SOMASK) { 730 returnmask = u.u_oldmask; 731 p->p_flag &= ~SOMASK; 732 } else 733 returnmask = p->p_sigmask; 734 p->p_sigmask |= u.u_sigmask[sig] | mask; 735 (void) spl0(); 736 u.u_ru.ru_nsignals++; 737 sendsig(action, sig, returnmask); 738 p->p_cursig = 0; 739 return; 740 } 741 u.u_acflag |= AXSIG; 742 switch (sig) { 743 744 case SIGILL: 745 case SIGIOT: 746 case SIGBUS: 747 case SIGQUIT: 748 case SIGTRAP: 749 case SIGEMT: 750 case SIGFPE: 751 case SIGSEGV: 752 case SIGSYS: 753 u.u_arg[0] = sig; 754 if (core() == 0) 755 sig += 0200; 756 } 757 exit(sig); 758 } 759 760 /* 761 * Create a core image on the file "core" 762 * If you are looking for protection glitches, 763 * there are probably a wealth of them here 764 * when this occurs to a suid command. 765 * 766 * It writes UPAGES block of the 767 * user.h area followed by the entire 768 * data+stack segments. 769 */ 770 core() 771 { 772 register struct vnode *vp, *dvp; 773 register struct nameidata *ndp = &u.u_nd; 774 struct vattr vattr; 775 int error; 776 777 if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid) 778 return (EFAULT); 779 if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 780 u.u_rlimit[RLIMIT_CORE].rlim_cur) 781 return (EFAULT); 782 if (u.u_procp->p_textp) { 783 vop_lock(u.u_procp->p_textp->x_vptr); 784 error = vn_access(u.u_procp->p_textp->x_vptr, VREAD, u.u_cred); 785 vop_unlock(u.u_procp->p_textp->x_vptr); 786 if (error) 787 return (EFAULT); 788 } 789 ndp->ni_segflg = UIO_SYSSPACE; 790 ndp->ni_dirp = "core"; 791 if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 792 return (error); 793 vp = ndp->ni_vp; 794 if (vp->v_type != VREG || 795 vop_getattr(vp, &vattr, u.u_cred) || 796 vattr.va_nlink != 1) { 797 error = EFAULT; 798 goto out; 799 } 800 #ifdef MMAP 801 { register int fd; 802 /* unmasp funky devices in the user's address space */ 803 for (fd = 0; fd < u.u_lastfile; fd++) 804 if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED)) 805 munmapfd(fd); 806 } 807 #endif 808 vattr_null(&vattr); 809 vattr.va_size = 0; 810 vop_setattr(vp, &vattr, u.u_cred); 811 u.u_acflag |= ACORE; 812 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 813 UIO_SYSSPACE, IO_UNIT, (int *)0); 814 if (error == 0) 815 error = vn_rdwr(UIO_WRITE, vp, 816 (caddr_t)ctob(dptov(u.u_procp, 0)), 817 (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), 818 UIO_USERSPACE, IO_UNIT, (int *)0); 819 if (error == 0) 820 error = vn_rdwr(UIO_WRITE, vp, 821 (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)), 822 (int)ctob(u.u_ssize), 823 (off_t)ctob(UPAGES) + ctob(u.u_dsize), 824 UIO_USERSPACE, IO_UNIT, (int *)0); 825 out: 826 if (vp) 827 vrele(vp); 828 return (error); 829 } 830