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