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