1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)kern_sig.c 7.17 (Berkeley) 05/29/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "syscontext.h" /* XXX */ 23 #include "vnode.h" 24 #include "proc.h" 25 #include "timeb.h" 26 #include "times.h" 27 #include "buf.h" 28 #include "text.h" 29 #include "seg.h" 30 #include "vm.h" 31 #include "acct.h" 32 #include "uio.h" 33 #include "file.h" 34 #include "kernel.h" 35 #include "wait.h" 36 #include "ktrace.h" 37 38 #include "machine/reg.h" 39 #include "machine/pte.h" 40 #include "machine/psl.h" 41 #include "machine/mtpr.h" 42 43 #define ttystopsigmask (sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU)) 44 #define stopsigmask (sigmask(SIGSTOP)|ttystopsigmask) 45 #define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \ 46 sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO)) 47 48 /* 49 * Can the current process (u.u_procp) send the specified signal 50 * to the specified process? 51 */ 52 #define CANSIGNAL(p, signo) \ 53 (u.u_uid == 0 || \ 54 u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \ 55 u.u_procp->p_ruid == (p)->p_uid || \ 56 u.u_procp->p_ruid == (p)->p_ruid || \ 57 ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session)) 58 59 sigaction() 60 { 61 register struct a { 62 int signo; 63 struct sigaction *nsa; 64 struct sigaction *osa; 65 } *uap = (struct a *)u.u_ap; 66 struct sigaction vec; 67 register struct sigaction *sa; 68 register int sig; 69 int bit, error; 70 71 sig = uap->signo; 72 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 73 RETURN (EINVAL); 74 sa = &vec; 75 if (uap->osa) { 76 sa->sa_handler = u.u_signal[sig]; 77 sa->sa_mask = u.u_sigmask[sig]; 78 bit = sigmask(sig); 79 sa->sa_flags = 0; 80 if ((u.u_sigonstack & bit) != 0) 81 sa->sa_flags |= SA_ONSTACK; 82 if ((u.u_sigintr & bit) == 0) 83 sa->sa_flags |= SA_RESTART; 84 if (u.u_procp->p_flag & SNOCLDSTOP) 85 sa->sa_flags |= SA_NOCLDSTOP; 86 if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, 87 sizeof (vec))) 88 RETURN (error); 89 } 90 if (uap->nsa) { 91 if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 92 sizeof (vec))) 93 RETURN (error); 94 setsigvec(sig, sa); 95 } 96 RETURN (0); 97 } 98 99 setsigvec(sig, sa) 100 int sig; 101 register struct sigaction *sa; 102 { 103 register struct proc *p; 104 register int bit; 105 106 bit = sigmask(sig); 107 p = u.u_procp; 108 /* 109 * Change setting atomically. 110 */ 111 (void) splhigh(); 112 u.u_signal[sig] = sa->sa_handler; 113 u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask; 114 if ((sa->sa_flags & SA_RESTART) == 0) 115 u.u_sigintr |= bit; 116 else 117 u.u_sigintr &= ~bit; 118 if (sa->sa_flags & SA_ONSTACK) 119 u.u_sigonstack |= bit; 120 else 121 u.u_sigonstack &= ~bit; 122 if (sig == SIGCHLD) { 123 if (sa->sa_flags & SA_NOCLDSTOP) 124 p->p_flag |= SNOCLDSTOP; 125 else 126 p->p_flag &= ~SNOCLDSTOP; 127 } 128 /* 129 * Set bit in p_sigignore for signals that are set to SIG_IGN, 130 * and for signals set to SIG_DFL where the default is to ignore. 131 * However, don't put SIGCONT in p_sigignore, 132 * as we have to restart the process. 133 */ 134 if (sa->sa_handler == SIG_IGN || 135 (bit & defaultignmask && sa->sa_handler == SIG_DFL)) { 136 p->p_sig &= ~bit; /* never to be seen again */ 137 if (sig != SIGCONT) 138 p->p_sigignore |= bit; /* easier in psignal */ 139 p->p_sigcatch &= ~bit; 140 } else { 141 p->p_sigignore &= ~bit; 142 if (sa->sa_handler == SIG_DFL) 143 p->p_sigcatch &= ~bit; 144 else 145 p->p_sigcatch |= bit; 146 } 147 (void) spl0(); 148 } 149 150 /* 151 * Initialize signal state for process 0; 152 * set to ignore signals that are ignored by default. 153 */ 154 siginit(p) 155 struct proc *p; 156 { 157 158 p->p_sigignore = defaultignmask &~ sigmask(SIGCONT); 159 } 160 161 /* 162 * Reset signals for an exec of the specified process. 163 */ 164 execsigs(p) 165 register struct proc *p; 166 { 167 register int nc, mask; 168 169 /* 170 * Reset caught signals. Held signals remain held 171 * through p_sigmask (unless they were caught, 172 * and are now ignored by default). 173 */ 174 while (p->p_sigcatch) { 175 nc = ffs((long)p->p_sigcatch); 176 mask = sigmask(nc); 177 p->p_sigcatch &= ~mask; 178 if (mask & defaultignmask) { 179 if (nc != SIGCONT) 180 p->p_sigignore |= mask; 181 p->p_sig &= ~mask; 182 } 183 u.u_signal[nc] = SIG_DFL; 184 } 185 /* 186 * Reset stack state to the user stack. 187 * Clear set of signals caught on the signal stack. 188 */ 189 u.u_onstack = 0; 190 u.u_sigsp = 0; 191 u.u_sigonstack = 0; 192 } 193 194 /* 195 * Manipulate signal mask. 196 * Note that we receive new mask, not pointer, 197 * and return old mask as return value; 198 * the library stub does the rest. 199 */ 200 sigprocmask() 201 { 202 struct a { 203 int how; 204 sigset_t mask; 205 } *uap = (struct a *)u.u_ap; 206 register struct proc *p = u.u_procp; 207 int error = 0; 208 209 u.u_r.r_val1 = p->p_sigmask; 210 (void) splhigh(); 211 212 switch (uap->how) { 213 case SIG_BLOCK: 214 p->p_sigmask |= uap->mask &~ sigcantmask; 215 break; 216 217 case SIG_UNBLOCK: 218 p->p_sigmask &= ~uap->mask; 219 break; 220 221 case SIG_SETMASK: 222 p->p_sigmask = uap->mask &~ sigcantmask; 223 break; 224 225 default: 226 error = EINVAL; 227 break; 228 } 229 (void) spl0(); 230 RETURN (error); 231 } 232 233 sigpending() 234 { 235 236 u.u_r.r_val1 = u.u_procp->p_sig; 237 RETURN (0); 238 } 239 240 #ifdef COMPAT_43 241 /* 242 * Generalized interface signal handler, 4.3-compatible. 243 */ 244 osigvec() 245 { 246 register struct a { 247 int signo; 248 struct sigvec *nsv; 249 struct sigvec *osv; 250 } *uap = (struct a *)u.u_ap; 251 struct sigvec vec; 252 register struct sigvec *sv; 253 register int sig; 254 int bit, error; 255 256 sig = uap->signo; 257 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 258 RETURN (EINVAL); 259 sv = &vec; 260 if (uap->osv) { 261 *(sig_t *)&sv->sv_handler = u.u_signal[sig]; 262 sv->sv_mask = u.u_sigmask[sig]; 263 bit = sigmask(sig); 264 sv->sv_flags = 0; 265 if ((u.u_sigonstack & bit) != 0) 266 sv->sv_flags |= SV_ONSTACK; 267 if ((u.u_sigintr & bit) != 0) 268 sv->sv_flags |= SV_INTERRUPT; 269 if (u.u_procp->p_flag & SNOCLDSTOP) 270 sv->sv_flags |= SA_NOCLDSTOP; 271 if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, 272 sizeof (vec))) 273 RETURN (error); 274 } 275 if (uap->nsv) { 276 if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 277 sizeof (vec))) 278 RETURN (error); 279 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 280 setsigvec(sig, (struct sigaction *)sv); 281 } 282 RETURN (0); 283 } 284 285 osigblock() 286 { 287 struct a { 288 int mask; 289 } *uap = (struct a *)u.u_ap; 290 register struct proc *p = u.u_procp; 291 292 (void) splhigh(); 293 u.u_r.r_val1 = p->p_sigmask; 294 p->p_sigmask |= uap->mask &~ sigcantmask; 295 (void) spl0(); 296 RETURN (0); 297 } 298 299 osigsetmask() 300 { 301 struct a { 302 int mask; 303 } *uap = (struct a *)u.u_ap; 304 register struct proc *p = u.u_procp; 305 306 (void) splhigh(); 307 u.u_r.r_val1 = p->p_sigmask; 308 p->p_sigmask = uap->mask &~ sigcantmask; 309 (void) spl0(); 310 RETURN (0); 311 } 312 #endif 313 314 /* 315 * Suspend process until signal, providing mask to be set 316 * in the meantime. Note nonstandard calling convention: 317 * libc stub passes mask, not pointer, to save a copyin. 318 */ 319 sigsuspend() 320 { 321 struct a { 322 sigset_t mask; 323 } *uap = (struct a *)u.u_ap; 324 register struct proc *p = u.u_procp; 325 326 /* 327 * When returning from sigpause, we want 328 * the old mask to be restored after the 329 * signal handler has finished. Thus, we 330 * save it here and mark the proc structure 331 * to indicate this (should be in u.). 332 */ 333 u.u_oldmask = p->p_sigmask; 334 p->p_flag |= SOMASK; 335 p->p_sigmask = uap->mask &~ sigcantmask; 336 (void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0); 337 /* always return EINTR rather than ERESTART... */ 338 RETURN (EINTR); 339 } 340 341 sigstack() 342 { 343 register struct a { 344 struct sigstack *nss; 345 struct sigstack *oss; 346 } *uap = (struct a *)u.u_ap; 347 struct sigstack ss; 348 int error = 0; 349 350 if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack, 351 (caddr_t)uap->oss, sizeof (struct sigstack)))) 352 RETURN (error); 353 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 354 sizeof (ss))) == 0) 355 u.u_sigstack = ss; 356 RETURN (error); 357 } 358 359 kill() 360 { 361 register struct a { 362 int pid; 363 int signo; 364 } *uap = (struct a *)u.u_ap; 365 register struct proc *p; 366 367 if ((unsigned) uap->signo >= NSIG) 368 RETURN (EINVAL); 369 if (uap->pid > 0) { 370 /* kill single process */ 371 p = pfind(uap->pid); 372 if (p == 0) 373 RETURN (ESRCH); 374 if (!CANSIGNAL(p, uap->signo)) 375 RETURN (EPERM); 376 if (uap->signo) 377 psignal(p, uap->signo); 378 RETURN (0); 379 } 380 switch (uap->pid) { 381 case -1: /* broadcast signal */ 382 RETURN (killpg1(uap->signo, 0, 1)); 383 case 0: /* signal own process group */ 384 RETURN (killpg1(uap->signo, 0, 0)); 385 default: /* negative explicit process group */ 386 RETURN (killpg1(uap->signo, -uap->pid, 0)); 387 } 388 /* NOTREACHED */ 389 } 390 391 #ifdef COMPAT_43 392 okillpg() 393 { 394 register struct a { 395 int pgid; 396 int signo; 397 } *uap = (struct a *)u.u_ap; 398 399 if ((unsigned) uap->signo >= NSIG) 400 RETURN (EINVAL); 401 RETURN (killpg1(uap->signo, uap->pgid, 0)); 402 } 403 #endif 404 405 killpg1(signo, pgid, all) 406 int signo, pgid, all; 407 { 408 register struct proc *p; 409 struct pgrp *pgrp; 410 int f = 0, error = ESRCH; 411 412 if (all) 413 /* 414 * broadcast 415 */ 416 for (p = allproc; p != NULL; p = p->p_nxt) { 417 if (p->p_ppid == 0 || p->p_flag&SSYS || 418 p == u.u_procp || !CANSIGNAL(p, signo)) 419 continue; 420 f++; 421 if (signo) 422 psignal(p, signo); 423 } 424 else { 425 if (pgid == 0) 426 /* 427 * zero pgid means send to my process group. 428 */ 429 pgrp = u.u_procp->p_pgrp; 430 else { 431 pgrp = pgfind(pgid); 432 if (pgrp == NULL) 433 return (ESRCH); 434 } 435 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { 436 if (p->p_ppid == 0 || p->p_flag&SSYS || 437 !CANSIGNAL(p, signo)) 438 continue; 439 f++; 440 if (signo) 441 psignal(p, signo); 442 } 443 } 444 return (f ? 0 : error); 445 } 446 447 /* XXX - to be removed, as soon as sockets are changed to operate on pgrps 448 * Send the specified signal to 449 * all processes with 'pgid' as 450 * process group. 451 */ 452 gsignal(pgid, sig) 453 { 454 struct pgrp *pgrp; 455 456 if (pgid && (pgrp = pgfind(pgid))) 457 pgsignal(pgrp, sig, 0); 458 } 459 /* 460 * Send sig to every member of a process group. 461 * If checktty is 1, limit to members which have a controlling 462 * terminal. 463 */ 464 pgsignal(pgrp, sig, checkctty) 465 struct pgrp *pgrp; 466 { 467 register struct proc *p; 468 469 if (pgrp) 470 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) 471 if (checkctty == 0 || p->p_flag&SCTTY) 472 psignal(p, sig); 473 } 474 475 /* 476 * Send a signal caused by a trap to the current process. 477 * If it will be caught immediately, deliver it with correct code. 478 * Otherwise, post it normally. 479 */ 480 trapsignal(sig, code) 481 register int sig; 482 unsigned code; 483 { 484 register struct proc *p = u.u_procp; 485 int mask; 486 487 mask = sigmask(sig); 488 if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 && 489 (p->p_sigmask & mask) == 0) { 490 u.u_ru.ru_nsignals++; 491 #ifdef KTRACE 492 if (KTRPOINT(p, KTR_PSIG)) 493 ktrpsig(p->p_tracep, sig, u.u_signal[sig], 494 p->p_sigmask, code); 495 #endif 496 sendsig(u.u_signal[sig], sig, p->p_sigmask, code); 497 p->p_sigmask |= u.u_sigmask[sig] | mask; 498 } else { 499 u.u_arg[1] = code; /* XXX for core dump/debugger */ 500 psignal(p, sig); 501 } 502 } 503 504 /* 505 * Send the specified signal to the specified process. 506 * Most signals do not do anything directly to a process; 507 * they set a flag that asks the process to do something to itself. 508 * Exceptions: 509 * o When a stop signal is sent to a sleeping process that takes the default 510 * action, the process is stopped without awakening it. 511 * o SIGCONT restarts stopped processes (or puts them back to sleep) 512 * regardless of the signal action (eg, blocked or ignored). 513 * Other ignored signals are discarded immediately. 514 */ 515 psignal(p, sig) 516 register struct proc *p; 517 register int sig; 518 { 519 register int s; 520 register sig_t action; 521 int mask; 522 523 if ((unsigned)sig >= NSIG || sig == 0) 524 panic("psignal sig"); 525 mask = sigmask(sig); 526 527 /* 528 * If proc is traced, always give parent a chance. 529 */ 530 if (p->p_flag & STRC) 531 action = SIG_DFL; 532 else { 533 /* 534 * If the signal is being ignored, 535 * then we forget about it immediately. 536 * (Note: we don't set SIGCONT in p_sigignore, 537 * and if it is set to SIG_IGN, 538 * action will be SIG_DFL here.) 539 */ 540 if (p->p_sigignore & mask) 541 return; 542 if (p->p_sigmask & mask) 543 action = SIG_HOLD; 544 else if (p->p_sigcatch & mask) 545 action = SIG_CATCH; 546 else 547 action = SIG_DFL; 548 } 549 switch (sig) { 550 551 case SIGTERM: 552 if ((p->p_flag&STRC) || action != SIG_DFL) 553 break; 554 /* FALLTHROUGH */ 555 556 case SIGKILL: 557 if (p->p_nice > NZERO) 558 p->p_nice = NZERO; 559 break; 560 561 case SIGCONT: 562 p->p_sig &= ~stopsigmask; 563 break; 564 565 case SIGTSTP: 566 case SIGTTIN: 567 case SIGTTOU: 568 case SIGSTOP: 569 p->p_sig &= ~sigmask(SIGCONT); 570 break; 571 } 572 p->p_sig |= mask; 573 574 /* 575 * Defer further processing for signals which are held, 576 * except that stopped processes must be continued by SIGCONT. 577 */ 578 if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 579 return; 580 s = splhigh(); 581 switch (p->p_stat) { 582 583 case SSLEEP: 584 /* 585 * If process is sleeping uninterruptibly 586 * we can't interrupt the sleep... the signal will 587 * be noticed when the process returns through 588 * trap() or syscall(). 589 */ 590 if ((p->p_flag & SSINTR) == 0) 591 goto out; 592 /* 593 * Process is sleeping and traced... make it runnable 594 * so it can discover the signal in issig() and stop 595 * for the parent. 596 */ 597 if (p->p_flag&STRC) 598 goto run; 599 /* 600 * When a sleeping process receives a stop 601 * signal, process immediately if possible. 602 * All other (caught or default) signals 603 * cause the process to run. 604 */ 605 if (mask & stopsigmask) { 606 if (action != SIG_DFL) 607 goto runfast; 608 /* 609 * If a child in vfork(), stopping could 610 * cause deadlock. 611 */ 612 if (p->p_flag&SVFORK) 613 goto out; 614 p->p_sig &= ~mask; 615 p->p_cursig = sig; 616 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 617 psignal(p->p_pptr, SIGCHLD); 618 stop(p); 619 goto out; 620 } else 621 goto runfast; 622 /*NOTREACHED*/ 623 624 case SSTOP: 625 /* 626 * If traced process is already stopped, 627 * then no further action is necessary. 628 */ 629 if (p->p_flag&STRC) 630 goto out; 631 switch (sig) { 632 633 case SIGKILL: 634 /* 635 * Kill signal always sets processes running. 636 */ 637 goto runfast; 638 639 case SIGCONT: 640 /* 641 * If SIGCONT is default (or ignored), we continue 642 * the process but don't leave the signal in p_sig, 643 * as it has no further action. If SIGCONT is held, 644 * continue the process and leave the signal in p_sig. 645 * If the process catches SIGCONT, let it handle 646 * the signal itself. If it isn't waiting on 647 * an event, then it goes back to run state. 648 * Otherwise, process goes back to sleep state. 649 */ 650 p->p_cursig = 0; /* ??? XXX */ 651 if (action == SIG_DFL) 652 p->p_sig &= ~mask; 653 if (action == SIG_CATCH) 654 goto runfast; 655 if (p->p_wchan == 0) 656 goto run; 657 p->p_stat = SSLEEP; 658 goto out; 659 660 case SIGSTOP: 661 case SIGTSTP: 662 case SIGTTIN: 663 case SIGTTOU: 664 /* 665 * Already stopped, don't need to stop again. 666 * (If we did the shell could get confused.) 667 */ 668 p->p_sig &= ~mask; /* take it away */ 669 goto out; 670 671 default: 672 /* 673 * If process is sleeping interruptibly, then 674 * simulate a wakeup so that when it is continued, 675 * it will be made runnable and can look at the signal. 676 * But don't setrun the process, leave it stopped. 677 */ 678 if (p->p_wchan && p->p_flag & SSINTR) 679 unsleep(p); 680 goto out; 681 } 682 /*NOTREACHED*/ 683 684 default: 685 /* 686 * SRUN, SIDL, SZOMB do nothing with the signal, 687 * other than kicking ourselves if we are running. 688 * It will either never be noticed, or noticed very soon. 689 */ 690 if (p == u.u_procp && !noproc) 691 aston(); 692 goto out; 693 } 694 /*NOTREACHED*/ 695 696 runfast: 697 /* 698 * Raise priority to at least PUSER. 699 */ 700 if (p->p_pri > PUSER) 701 p->p_pri = PUSER; 702 run: 703 setrun(p); 704 out: 705 splx(s); 706 } 707 708 /* 709 * If the current process has a signal to process (should be caught 710 * or cause termination, should interrupt current syscall), 711 * return the signal number. Stop signals with default action 712 * are processed immediately, then cleared; they aren't returned. 713 * This is asked at least once each time a process enters the 714 * system (though this can usually be done without actually 715 * calling issig by checking the pending signal masks.) 716 */ 717 issig() 718 { 719 register struct proc *p; 720 register int sig, mask; 721 722 p = u.u_procp; 723 for (;;) { 724 mask = p->p_sig &~ p->p_sigmask; 725 if (p->p_flag&SVFORK) 726 mask &= ~stopsigmask; 727 if (mask == 0) /* no signal to send */ 728 return (0); 729 sig = ffs((long)mask); 730 mask = sigmask(sig); 731 /* 732 * We should see pending but ignored signals 733 * only if STRC was on when they were posted. 734 */ 735 if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) { 736 p->p_sig &= ~mask; 737 continue; 738 } 739 if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 740 /* 741 * If traced, always stop, and stay 742 * stopped until released by the parent. 743 */ 744 p->p_cursig = sig; 745 psignal(p->p_pptr, SIGCHLD); 746 do { 747 stop(p); 748 swtch(); 749 } while (!procxmt() && p->p_flag&STRC); 750 751 /* 752 * If the traced bit got turned off, 753 * go back up to the top to rescan signals. 754 * This ensures that p_sig* and u_signal are consistent. 755 */ 756 if ((p->p_flag&STRC) == 0) 757 continue; 758 759 /* 760 * If parent wants us to take the signal, 761 * then it will leave it in p->p_cursig; 762 * otherwise we just look for signals again. 763 */ 764 p->p_sig &= ~mask; /* clear the old signal */ 765 sig = p->p_cursig; 766 if (sig == 0) 767 continue; 768 769 /* 770 * Put the new signal into p_sig. 771 * If signal is being masked, 772 * look for other signals. 773 */ 774 mask = sigmask(sig); 775 p->p_sig |= mask; 776 if (p->p_sigmask & mask) 777 continue; 778 } 779 780 /* 781 * Decide whether the signal should be returned. 782 * Return the signal's number, or fall through 783 * to clear it from the pending mask. 784 */ 785 switch ((int)u.u_signal[sig]) { 786 787 case SIG_DFL: 788 /* 789 * Don't take default actions on system processes. 790 */ 791 if (p->p_ppid == 0) 792 break; /* == ignore */ 793 /* 794 * If there is a pending stop signal to process 795 * with default action, stop here, 796 * then clear the signal. However, 797 * if process is member of an orphaned 798 * process group, ignore tty stop signals. 799 */ 800 if (mask & stopsigmask) { 801 if (p->p_flag&STRC || 802 (p->p_pgrp->pg_jobc == 0 && 803 mask & ttystopsigmask)) 804 break; /* == ignore */ 805 p->p_cursig = sig; 806 stop(p); 807 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 808 psignal(p->p_pptr, SIGCHLD); 809 swtch(); 810 break; 811 } else if (mask & defaultignmask) { 812 /* 813 * Except for SIGCONT, shouldn't get here. 814 * Default action is to ignore; drop it. 815 */ 816 break; /* == ignore */ 817 } else 818 return (sig); 819 /*NOTREACHED*/ 820 821 case SIG_IGN: 822 /* 823 * Masking above should prevent us ever trying 824 * to take action on an ignored signal other 825 * than SIGCONT, unless process is traced. 826 */ 827 if (sig != SIGCONT && (p->p_flag&STRC) == 0) 828 printf("issig\n"); 829 break; /* == ignore */ 830 831 default: 832 /* 833 * This signal has an action, let 834 * psig process it. 835 */ 836 return (sig); 837 } 838 p->p_sig &= ~mask; /* take the signal! */ 839 } 840 /* NOTREACHED */ 841 } 842 843 /* 844 * Put the argument process into the stopped 845 * state and notify the parent via wakeup. 846 * Signals are handled elsewhere. 847 * The process must not be on the run queue. 848 */ 849 stop(p) 850 register struct proc *p; 851 { 852 853 p->p_stat = SSTOP; 854 p->p_flag &= ~SWTED; 855 wakeup((caddr_t)p->p_pptr); 856 } 857 858 /* 859 * Perform the action specified by the current signal. 860 * The usual sequence is: 861 * if (sig = CURSIG(p)) 862 * psig(sig); 863 */ 864 psig(sig) 865 register int sig; 866 { 867 register struct proc *p = u.u_procp; 868 int mask, returnmask; 869 register sig_t action; 870 871 do { 872 #ifdef DIAGNOSTIC 873 if (sig == 0) 874 panic("psig"); 875 #endif 876 mask = sigmask(sig); 877 p->p_sig &= ~mask; 878 action = u.u_signal[sig]; 879 #ifdef KTRACE 880 if (KTRPOINT(p, KTR_PSIG)) 881 ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ? 882 u.u_oldmask : p->p_sigmask, 0); 883 #endif 884 if (action != SIG_DFL) { 885 #ifdef DIAGNOSTIC 886 if (action == SIG_IGN || (p->p_sigmask & mask)) 887 panic("psig action"); 888 #endif 889 u.u_error = 0; 890 /* 891 * Set the new mask value and also defer further 892 * occurences of this signal. 893 * 894 * Special case: user has done a sigpause. Here the 895 * current mask is not of interest, but rather the 896 * mask from before the sigpause is what we want 897 * restored after the signal processing is completed. 898 */ 899 (void) splhigh(); 900 if (p->p_flag & SOMASK) { 901 returnmask = u.u_oldmask; 902 p->p_flag &= ~SOMASK; 903 } else 904 returnmask = p->p_sigmask; 905 p->p_sigmask |= u.u_sigmask[sig] | mask; 906 (void) spl0(); 907 u.u_ru.ru_nsignals++; 908 sendsig(action, sig, returnmask, 0); 909 continue; 910 } 911 u.u_acflag |= AXSIG; 912 switch (sig) { 913 914 case SIGILL: 915 case SIGIOT: 916 case SIGBUS: 917 case SIGQUIT: 918 case SIGTRAP: 919 case SIGEMT: 920 case SIGFPE: 921 case SIGSEGV: 922 case SIGSYS: 923 u.u_arg[0] = sig; 924 if (core() == 0) 925 sig |= WCOREFLAG; 926 } 927 exit(W_EXITCODE(0, sig)); 928 /* NOTREACHED */ 929 } while (sig = CURSIG(p)); 930 } 931 932 /* 933 * Create a core image on the file "core". 934 * It writes UPAGES block of the 935 * user.h area followed by the entire 936 * data+stack segments. 937 */ 938 core() 939 { 940 register struct vnode *vp; 941 register struct proc *p = u.u_procp; 942 register struct nameidata *ndp = &u.u_nd; 943 struct vattr vattr; 944 int error; 945 946 if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 947 return (EFAULT); 948 if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 949 u.u_rlimit[RLIMIT_CORE].rlim_cur) 950 return (EFAULT); 951 if (p->p_textp) { 952 VOP_LOCK(p->p_textp->x_vptr); 953 error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred); 954 VOP_UNLOCK(p->p_textp->x_vptr); 955 if (error) 956 return (EFAULT); 957 } 958 ndp->ni_segflg = UIO_SYSSPACE; 959 ndp->ni_dirp = "core"; 960 if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 961 return (error); 962 vp = ndp->ni_vp; 963 VOP_LOCK(vp); 964 if (vp->v_type != VREG || 965 VOP_GETATTR(vp, &vattr, u.u_cred) || 966 vattr.va_nlink != 1) { 967 vput(vp); 968 return (EFAULT); 969 } 970 #ifdef MAPMEM 971 mmcore(); 972 #endif 973 VATTR_NULL(&vattr); 974 vattr.va_size = 0; 975 VOP_SETATTR(vp, &vattr, u.u_cred); 976 u.u_acflag |= ACORE; 977 #ifdef HPUXCOMPAT 978 /* 979 * BLETCH! If we loaded from an HPUX format binary file 980 * we have to dump an HPUX style user struct so that the 981 * HPUX debuggers can grok it. 982 */ 983 if (u.u_pcb.pcb_flags & PCB_HPUXBIN) 984 error = hpuxdumpu(vp, ndp->ni_cred); 985 else 986 #endif 987 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 988 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 989 if (error == 0) 990 error = vn_rdwr(UIO_WRITE, vp, 991 (caddr_t)ctob(dptov(p, 0)), 992 (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 993 IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 994 if (error == 0) 995 error = vn_rdwr(UIO_WRITE, vp, 996 (caddr_t)ctob(sptov(p, u.u_ssize - 1)), 997 (int)ctob(u.u_ssize), 998 (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 999 IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 1000 vput(vp); 1001 return (error); 1002 } 1003 1004 /* 1005 * Nonexistent system call-- signal process (may want to handle it). 1006 * Flag error in case process won't see signal immediately (blocked or ignored). 1007 */ 1008 nosys() 1009 { 1010 1011 psignal(u.u_procp, SIGSYS); 1012 u.u_error = EINVAL; 1013 } 1014