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