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