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