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.16 (Berkeley) 05/17/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 stopsigmask (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \ 44 sigmask(SIGTTIN)|sigmask(SIGTTOU)) 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 if (p->p_pgrp->pg_jobc == 0 && (sig == SIGTTIN || 548 sig == SIGTTOU || sig == SIGTSTP)) 549 return; 550 action = SIG_DFL; 551 } 552 } 553 switch (sig) { 554 555 case SIGTERM: 556 if ((p->p_flag&STRC) || action != SIG_DFL) 557 break; 558 /* FALLTHROUGH */ 559 560 case SIGKILL: 561 if (p->p_nice > NZERO) 562 p->p_nice = NZERO; 563 break; 564 565 case SIGCONT: 566 p->p_sig &= ~stopsigmask; 567 break; 568 569 case SIGTSTP: 570 case SIGTTIN: 571 case SIGTTOU: 572 case SIGSTOP: 573 p->p_sig &= ~sigmask(SIGCONT); 574 break; 575 } 576 p->p_sig |= mask; 577 578 /* 579 * Defer further processing for signals which are held, 580 * except that stopped processes must be continued by SIGCONT. 581 */ 582 if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 583 return; 584 s = splhigh(); 585 switch (p->p_stat) { 586 587 case SSLEEP: 588 /* 589 * If process is sleeping uninterruptibly 590 * we can't interrupt the sleep... the signal will 591 * be noticed when the process returns through 592 * trap() or syscall(). 593 */ 594 if ((p->p_flag & SSINTR) == 0) 595 goto out; 596 /* 597 * Process is sleeping and traced... make it runnable 598 * so it can discover the signal in issig() and stop 599 * for the parent. 600 */ 601 if (p->p_flag&STRC) 602 goto run; 603 /* 604 * When a sleeping process receives a stop 605 * signal, process immediately if possible. 606 * All other (caught or default) signals 607 * cause the process to run. 608 */ 609 if (mask & stopsigmask) { 610 if (action != SIG_DFL) 611 goto runfast; 612 /* 613 * If a child in vfork(), stopping could 614 * cause deadlock. 615 */ 616 if (p->p_flag&SVFORK) 617 goto out; 618 p->p_sig &= ~mask; 619 p->p_cursig = sig; 620 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 621 psignal(p->p_pptr, SIGCHLD); 622 stop(p); 623 goto out; 624 } else 625 goto runfast; 626 /*NOTREACHED*/ 627 628 case SSTOP: 629 /* 630 * If traced process is already stopped, 631 * then no further action is necessary. 632 */ 633 if (p->p_flag&STRC) 634 goto out; 635 switch (sig) { 636 637 case SIGKILL: 638 /* 639 * Kill signal always sets processes running. 640 */ 641 goto runfast; 642 643 case SIGCONT: 644 /* 645 * If SIGCONT is default (or ignored), we continue 646 * the process but don't leave the signal in p_sig, 647 * as it has no further action. If SIGCONT is held, 648 * continue the process and leave the signal in p_sig. 649 * If the process catches SIGCONT, let it handle 650 * the signal itself. If it isn't waiting on 651 * an event, then it goes back to run state. 652 * Otherwise, process goes back to sleep state. 653 */ 654 p->p_cursig = 0; /* ??? XXX */ 655 if (action == SIG_DFL) 656 p->p_sig &= ~mask; 657 if (action == SIG_CATCH) 658 goto runfast; 659 if (p->p_wchan == 0) 660 goto run; 661 p->p_stat = SSLEEP; 662 goto out; 663 664 case SIGSTOP: 665 case SIGTSTP: 666 case SIGTTIN: 667 case SIGTTOU: 668 /* 669 * Already stopped, don't need to stop again. 670 * (If we did the shell could get confused.) 671 */ 672 p->p_sig &= ~mask; /* take it away */ 673 goto out; 674 675 default: 676 /* 677 * If process is sleeping interruptibly, then 678 * simulate a wakeup so that when it is continued, 679 * it will be made runnable and can look at the signal. 680 * But don't setrun the process, leave it stopped. 681 */ 682 if (p->p_wchan && p->p_flag & SSINTR) 683 unsleep(p); 684 goto out; 685 } 686 /*NOTREACHED*/ 687 688 default: 689 /* 690 * SRUN, SIDL, SZOMB do nothing with the signal, 691 * other than kicking ourselves if we are running. 692 * It will either never be noticed, or noticed very soon. 693 */ 694 if (p == u.u_procp && !noproc) 695 aston(); 696 goto out; 697 } 698 /*NOTREACHED*/ 699 700 runfast: 701 /* 702 * Raise priority to at least PUSER. 703 */ 704 if (p->p_pri > PUSER) 705 p->p_pri = PUSER; 706 run: 707 setrun(p); 708 out: 709 splx(s); 710 } 711 712 /* 713 * If the current process has a signal to process (should be caught 714 * or cause termination, should interrupt current syscall), 715 * return the signal number. Stop signals with default action 716 * are processed immediately, then cleared; they aren't returned. 717 * This is asked at least once each time a process enters the 718 * system (though this can usually be done without actually 719 * calling issig by checking the pending signal masks.) 720 */ 721 issig() 722 { 723 register struct proc *p; 724 register int sig, mask; 725 726 p = u.u_procp; 727 for (;;) { 728 mask = p->p_sig &~ p->p_sigmask; 729 if (p->p_flag&SVFORK) 730 mask &= ~stopsigmask; 731 if (mask == 0) /* no signal to send */ 732 return (0); 733 sig = ffs((long)mask); 734 mask = sigmask(sig); 735 /* 736 * We should see pending but ignored signals 737 * only if STRC was on when they were posted. 738 */ 739 if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) { 740 p->p_sig &= ~mask; 741 continue; 742 } 743 if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 744 /* 745 * If traced, always stop, and stay 746 * stopped until released by the parent. 747 */ 748 p->p_cursig = sig; 749 psignal(p->p_pptr, SIGCHLD); 750 do { 751 stop(p); 752 swtch(); 753 } while (!procxmt() && p->p_flag&STRC); 754 755 /* 756 * If the traced bit got turned off, 757 * go back up to the top to rescan signals. 758 * This ensures that p_sig* and u_signal are consistent. 759 */ 760 if ((p->p_flag&STRC) == 0) 761 continue; 762 763 /* 764 * If parent wants us to take the signal, 765 * then it will leave it in p->p_cursig; 766 * otherwise we just look for signals again. 767 */ 768 p->p_sig &= ~mask; /* clear the old signal */ 769 sig = p->p_cursig; 770 if (sig == 0) 771 continue; 772 773 /* 774 * Put the new signal into p_sig. 775 * If signal is being masked, 776 * look for other signals. 777 */ 778 mask = sigmask(sig); 779 p->p_sig |= mask; 780 if (p->p_sigmask & mask) 781 continue; 782 } 783 784 /* 785 * Decide whether the signal should be returned. 786 * Return the signal's number, or fall through 787 * to clear it from the pending mask. 788 */ 789 switch ((int)u.u_signal[sig]) { 790 791 case SIG_DFL: 792 /* 793 * Don't take default actions on system processes. 794 */ 795 if (p->p_ppid == 0) 796 break; /* == ignore */ 797 /* 798 * If there is a pending stop signal to process 799 * with default action, stop here, 800 * then clear the signal. 801 */ 802 if (mask & stopsigmask) { 803 if (p->p_flag&STRC) 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