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