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