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