1 /* $NetBSD: kern_sig.c,v 1.112 2001/02/26 21:58:30 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95 41 */ 42 43 #include "opt_ktrace.h" 44 #include "opt_compat_sunos.h" 45 #include "opt_compat_netbsd32.h" 46 47 #define SIGPROP /* include signal properties table */ 48 #include <sys/param.h> 49 #include <sys/signalvar.h> 50 #include <sys/resourcevar.h> 51 #include <sys/namei.h> 52 #include <sys/vnode.h> 53 #include <sys/proc.h> 54 #include <sys/systm.h> 55 #include <sys/timeb.h> 56 #include <sys/times.h> 57 #include <sys/buf.h> 58 #include <sys/acct.h> 59 #include <sys/file.h> 60 #include <sys/kernel.h> 61 #include <sys/wait.h> 62 #include <sys/ktrace.h> 63 #include <sys/syslog.h> 64 #include <sys/stat.h> 65 #include <sys/core.h> 66 #include <sys/ptrace.h> 67 #include <sys/filedesc.h> 68 #include <sys/malloc.h> 69 #include <sys/pool.h> 70 71 #include <sys/mount.h> 72 #include <sys/syscallargs.h> 73 74 #include <machine/cpu.h> 75 76 #include <sys/user.h> /* for coredump */ 77 78 #include <uvm/uvm_extern.h> 79 80 static void proc_stop(struct proc *p); 81 void killproc(struct proc *, char *); 82 static int build_corename(struct proc *, char [MAXPATHLEN]); 83 #if COMPAT_NETBSD32 84 static int coredump32(struct proc *, struct vnode *); 85 #endif 86 sigset_t contsigmask, stopsigmask, sigcantmask; 87 88 struct pool sigacts_pool; /* memory pool for sigacts structures */ 89 90 /* 91 * Can process p, with pcred pc, send the signal signum to process q? 92 */ 93 #define CANSIGNAL(p, pc, q, signum) \ 94 ((pc)->pc_ucred->cr_uid == 0 || \ 95 (pc)->p_ruid == (q)->p_cred->p_ruid || \ 96 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \ 97 (pc)->p_ruid == (q)->p_ucred->cr_uid || \ 98 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \ 99 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)) 100 101 /* 102 * Initialize signal-related data structures. 103 */ 104 void 105 signal_init(void) 106 { 107 108 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl", 109 0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC); 110 } 111 112 /* 113 * Create an initial sigctx structure, using the same signal state 114 * as p. If 'share' is set, share the sigctx_proc part, otherwise just 115 * copy it from parent. 116 */ 117 void 118 sigactsinit(struct proc *np, struct proc *pp, int share) 119 { 120 struct sigacts *ps; 121 122 if (share) { 123 np->p_sigacts = pp->p_sigacts; 124 pp->p_sigacts->sa_refcnt++; 125 } else { 126 ps = pool_get(&sigacts_pool, PR_WAITOK); 127 if (pp) 128 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts)); 129 else 130 memset(ps, '\0', sizeof(struct sigacts)); 131 ps->sa_refcnt = 1; 132 np->p_sigacts = ps; 133 } 134 } 135 136 /* 137 * Make this process not share its sigctx, maintaining all 138 * signal state. 139 */ 140 void 141 sigactsunshare(struct proc *p) 142 { 143 struct sigacts *oldps; 144 145 if (p->p_sigacts->sa_refcnt == 1) 146 return; 147 148 oldps = p->p_sigacts; 149 sigactsinit(p, NULL, 0); 150 151 if (--oldps->sa_refcnt == 0) 152 pool_put(&sigacts_pool, oldps); 153 } 154 155 /* 156 * Release a sigctx structure. 157 */ 158 void 159 sigactsfree(struct proc *p) 160 { 161 struct sigacts *ps; 162 163 ps = p->p_sigacts; 164 if (--ps->sa_refcnt > 0) 165 return; 166 167 pool_put(&sigacts_pool, ps); 168 } 169 170 int 171 sigaction1(struct proc *p, int signum, const struct sigaction *nsa, 172 struct sigaction *osa) 173 { 174 struct sigacts *ps; 175 int prop; 176 177 ps = p->p_sigacts; 178 if (signum <= 0 || signum >= NSIG) 179 return (EINVAL); 180 181 if (osa) 182 *osa = SIGACTION_PS(ps, signum); 183 184 if (nsa) { 185 if (nsa->sa_flags & ~SA_ALLBITS) 186 return (EINVAL); 187 188 prop = sigprop[signum]; 189 if (prop & SA_CANTMASK) 190 return (EINVAL); 191 192 (void) splsched(); /* XXXSMP */ 193 SIGACTION_PS(ps, signum) = *nsa; 194 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask); 195 if ((prop & SA_NORESET) != 0) 196 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND; 197 if (signum == SIGCHLD) { 198 if (nsa->sa_flags & SA_NOCLDSTOP) 199 p->p_flag |= P_NOCLDSTOP; 200 else 201 p->p_flag &= ~P_NOCLDSTOP; 202 if (nsa->sa_flags & SA_NOCLDWAIT) { 203 /* 204 * Paranoia: since SA_NOCLDWAIT is implemented 205 * by reparenting the dying child to PID 1 (and 206 * trust it to reap the zombie), PID 1 itself 207 * is forbidden to set SA_NOCLDWAIT. 208 */ 209 if (p->p_pid == 1) 210 p->p_flag &= ~P_NOCLDWAIT; 211 else 212 p->p_flag |= P_NOCLDWAIT; 213 } else 214 p->p_flag &= ~P_NOCLDWAIT; 215 } 216 if ((nsa->sa_flags & SA_NODEFER) == 0) 217 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum); 218 else 219 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum); 220 /* 221 * Set bit in p_sigctx.ps_sigignore for signals that are set to 222 * SIG_IGN, and for signals set to SIG_DFL where the default is 223 * to ignore. However, don't put SIGCONT in 224 * p_sigctx.ps_sigignore, as we have to restart the process. 225 */ 226 if (nsa->sa_handler == SIG_IGN || 227 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) { 228 /* never to be seen again */ 229 sigdelset(&p->p_sigctx.ps_siglist, signum); 230 if (signum != SIGCONT) { 231 /* easier in psignal */ 232 sigaddset(&p->p_sigctx.ps_sigignore, signum); 233 } 234 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 235 } else { 236 sigdelset(&p->p_sigctx.ps_sigignore, signum); 237 if (nsa->sa_handler == SIG_DFL) 238 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 239 else 240 sigaddset(&p->p_sigctx.ps_sigcatch, signum); 241 } 242 (void) spl0(); 243 } 244 245 return (0); 246 } 247 248 /* ARGSUSED */ 249 int 250 sys___sigaction14(struct proc *p, void *v, register_t *retval) 251 { 252 struct sys___sigaction14_args /* { 253 syscallarg(int) signum; 254 syscallarg(const struct sigaction *) nsa; 255 syscallarg(struct sigaction *) osa; 256 } */ *uap = v; 257 struct sigaction nsa, osa; 258 int error; 259 260 if (SCARG(uap, nsa)) { 261 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 262 if (error) 263 return (error); 264 } 265 error = sigaction1(p, SCARG(uap, signum), 266 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0); 267 if (error) 268 return (error); 269 if (SCARG(uap, osa)) { 270 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 271 if (error) 272 return (error); 273 } 274 return (0); 275 } 276 277 /* 278 * Initialize signal state for process 0; 279 * set to ignore signals that are ignored by default and disable the signal 280 * stack. 281 */ 282 void 283 siginit(struct proc *p) 284 { 285 struct sigacts *ps; 286 int signum, prop; 287 288 ps = p->p_sigacts; 289 sigemptyset(&contsigmask); 290 sigemptyset(&stopsigmask); 291 sigemptyset(&sigcantmask); 292 for (signum = 1; signum < NSIG; signum++) { 293 prop = sigprop[signum]; 294 if (prop & SA_CONT) 295 sigaddset(&contsigmask, signum); 296 if (prop & SA_STOP) 297 sigaddset(&stopsigmask, signum); 298 if (prop & SA_CANTMASK) 299 sigaddset(&sigcantmask, signum); 300 if (prop & SA_IGNORE && signum != SIGCONT) 301 sigaddset(&p->p_sigctx.ps_sigignore, signum); 302 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 303 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 304 } 305 sigemptyset(&p->p_sigctx.ps_sigcatch); 306 p->p_flag &= ~P_NOCLDSTOP; 307 308 /* 309 * Reset stack state to the user stack. 310 */ 311 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 312 p->p_sigctx.ps_sigstk.ss_size = 0; 313 p->p_sigctx.ps_sigstk.ss_sp = 0; 314 315 /* One reference. */ 316 ps->sa_refcnt = 1; 317 } 318 319 /* 320 * Reset signals for an exec of the specified process. 321 */ 322 void 323 execsigs(struct proc *p) 324 { 325 struct sigacts *ps; 326 int signum, prop; 327 328 ps = p->p_sigacts; 329 /* 330 * Reset caught signals. Held signals remain held 331 * through p_sigctx.ps_sigmask (unless they were caught, 332 * and are now ignored by default). 333 */ 334 for (signum = 1; signum < NSIG; signum++) { 335 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 336 prop = sigprop[signum]; 337 if (prop & SA_IGNORE) { 338 if ((prop & SA_CONT) == 0) 339 sigaddset(&p->p_sigctx.ps_sigignore, 340 signum); 341 sigdelset(&p->p_sigctx.ps_siglist, signum); 342 } 343 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 344 } 345 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 346 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 347 } 348 sigemptyset(&p->p_sigctx.ps_sigcatch); 349 p->p_flag &= ~P_NOCLDSTOP; 350 351 /* 352 * Reset stack state to the user stack. 353 */ 354 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 355 p->p_sigctx.ps_sigstk.ss_size = 0; 356 p->p_sigctx.ps_sigstk.ss_sp = 0; 357 } 358 359 int 360 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss) 361 { 362 363 if (oss) 364 *oss = p->p_sigctx.ps_sigmask; 365 366 if (nss) { 367 (void)splsched(); /* XXXSMP */ 368 switch (how) { 369 case SIG_BLOCK: 370 sigplusset(nss, &p->p_sigctx.ps_sigmask); 371 break; 372 case SIG_UNBLOCK: 373 sigminusset(nss, &p->p_sigctx.ps_sigmask); 374 CHECKSIGS(p); 375 break; 376 case SIG_SETMASK: 377 p->p_sigctx.ps_sigmask = *nss; 378 CHECKSIGS(p); 379 break; 380 default: 381 (void)spl0(); /* XXXSMP */ 382 return (EINVAL); 383 } 384 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 385 (void)spl0(); /* XXXSMP */ 386 } 387 388 return (0); 389 } 390 391 /* 392 * Manipulate signal mask. 393 * Note that we receive new mask, not pointer, 394 * and return old mask as return value; 395 * the library stub does the rest. 396 */ 397 int 398 sys___sigprocmask14(struct proc *p, void *v, register_t *retval) 399 { 400 struct sys___sigprocmask14_args /* { 401 syscallarg(int) how; 402 syscallarg(const sigset_t *) set; 403 syscallarg(sigset_t *) oset; 404 } */ *uap = v; 405 sigset_t nss, oss; 406 int error; 407 408 if (SCARG(uap, set)) { 409 error = copyin(SCARG(uap, set), &nss, sizeof(nss)); 410 if (error) 411 return (error); 412 } 413 error = sigprocmask1(p, SCARG(uap, how), 414 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0); 415 if (error) 416 return (error); 417 if (SCARG(uap, oset)) { 418 error = copyout(&oss, SCARG(uap, oset), sizeof(oss)); 419 if (error) 420 return (error); 421 } 422 return (0); 423 } 424 425 void 426 sigpending1(struct proc *p, sigset_t *ss) 427 { 428 429 *ss = p->p_sigctx.ps_siglist; 430 sigminusset(&p->p_sigctx.ps_sigmask, ss); 431 } 432 433 /* ARGSUSED */ 434 int 435 sys___sigpending14(struct proc *p, void *v, register_t *retval) 436 { 437 struct sys___sigpending14_args /* { 438 syscallarg(sigset_t *) set; 439 } */ *uap = v; 440 sigset_t ss; 441 442 sigpending1(p, &ss); 443 return (copyout(&ss, SCARG(uap, set), sizeof(ss))); 444 } 445 446 int 447 sigsuspend1(struct proc *p, const sigset_t *ss) 448 { 449 struct sigacts *ps; 450 451 ps = p->p_sigacts; 452 if (ss) { 453 /* 454 * When returning from sigpause, we want 455 * the old mask to be restored after the 456 * signal handler has finished. Thus, we 457 * save it here and mark the sigctx structure 458 * to indicate this. 459 */ 460 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask; 461 p->p_sigctx.ps_flags |= SAS_OLDMASK; 462 (void) splsched(); /* XXXSMP */ 463 p->p_sigctx.ps_sigmask = *ss; 464 CHECKSIGS(p); 465 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 466 (void) spl0(); /* XXXSMP */ 467 } 468 469 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 470 /* void */; 471 /* always return EINTR rather than ERESTART... */ 472 return (EINTR); 473 } 474 475 /* 476 * Suspend process until signal, providing mask to be set 477 * in the meantime. Note nonstandard calling convention: 478 * libc stub passes mask, not pointer, to save a copyin. 479 */ 480 /* ARGSUSED */ 481 int 482 sys___sigsuspend14(struct proc *p, void *v, register_t *retval) 483 { 484 struct sys___sigsuspend14_args /* { 485 syscallarg(const sigset_t *) set; 486 } */ *uap = v; 487 sigset_t ss; 488 int error; 489 490 if (SCARG(uap, set)) { 491 error = copyin(SCARG(uap, set), &ss, sizeof(ss)); 492 if (error) 493 return (error); 494 } 495 496 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0)); 497 } 498 499 int 500 sigaltstack1(struct proc *p, const struct sigaltstack *nss, 501 struct sigaltstack *oss) 502 { 503 504 if (oss) 505 *oss = p->p_sigctx.ps_sigstk; 506 507 if (nss) { 508 if (nss->ss_flags & ~SS_ALLBITS) 509 return (EINVAL); 510 511 if (nss->ss_flags & SS_DISABLE) { 512 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) 513 return (EINVAL); 514 } else { 515 if (nss->ss_size < MINSIGSTKSZ) 516 return (ENOMEM); 517 } 518 p->p_sigctx.ps_sigstk = *nss; 519 } 520 521 return (0); 522 } 523 524 /* ARGSUSED */ 525 int 526 sys___sigaltstack14(struct proc *p, void *v, register_t *retval) 527 { 528 struct sys___sigaltstack14_args /* { 529 syscallarg(const struct sigaltstack *) nss; 530 syscallarg(struct sigaltstack *) oss; 531 } */ *uap = v; 532 struct sigaltstack nss, oss; 533 int error; 534 535 if (SCARG(uap, nss)) { 536 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 537 if (error) 538 return (error); 539 } 540 error = sigaltstack1(p, 541 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0); 542 if (error) 543 return (error); 544 if (SCARG(uap, oss)) { 545 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 546 if (error) 547 return (error); 548 } 549 return (0); 550 } 551 552 /* ARGSUSED */ 553 int 554 sys_kill(struct proc *cp, void *v, register_t *retval) 555 { 556 struct sys_kill_args /* { 557 syscallarg(int) pid; 558 syscallarg(int) signum; 559 } */ *uap = v; 560 struct proc *p; 561 struct pcred *pc; 562 563 pc = cp->p_cred; 564 if ((u_int)SCARG(uap, signum) >= NSIG) 565 return (EINVAL); 566 if (SCARG(uap, pid) > 0) { 567 /* kill single process */ 568 if ((p = pfind(SCARG(uap, pid))) == NULL) 569 return (ESRCH); 570 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum))) 571 return (EPERM); 572 if (SCARG(uap, signum)) 573 psignal(p, SCARG(uap, signum)); 574 return (0); 575 } 576 switch (SCARG(uap, pid)) { 577 case -1: /* broadcast signal */ 578 return (killpg1(cp, SCARG(uap, signum), 0, 1)); 579 case 0: /* signal own process group */ 580 return (killpg1(cp, SCARG(uap, signum), 0, 0)); 581 default: /* negative explicit process group */ 582 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0)); 583 } 584 /* NOTREACHED */ 585 } 586 587 /* 588 * Common code for kill process group/broadcast kill. 589 * cp is calling process. 590 */ 591 int 592 killpg1(struct proc *cp, int signum, int pgid, int all) 593 { 594 struct proc *p; 595 struct pcred *pc; 596 struct pgrp *pgrp; 597 int nfound; 598 599 pc = cp->p_cred; 600 nfound = 0; 601 if (all) { 602 /* 603 * broadcast 604 */ 605 proclist_lock_read(); 606 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 607 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 608 p == cp || !CANSIGNAL(cp, pc, p, signum)) 609 continue; 610 nfound++; 611 if (signum) 612 psignal(p, signum); 613 } 614 proclist_unlock_read(); 615 } else { 616 if (pgid == 0) 617 /* 618 * zero pgid means send to my process group. 619 */ 620 pgrp = cp->p_pgrp; 621 else { 622 pgrp = pgfind(pgid); 623 if (pgrp == NULL) 624 return (ESRCH); 625 } 626 for (p = pgrp->pg_members.lh_first; 627 p != 0; 628 p = p->p_pglist.le_next) { 629 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 630 !CANSIGNAL(cp, pc, p, signum)) 631 continue; 632 nfound++; 633 if (signum && P_ZOMBIE(p) == 0) 634 psignal(p, signum); 635 } 636 } 637 return (nfound ? 0 : ESRCH); 638 } 639 640 /* 641 * Send a signal to a process group. 642 */ 643 void 644 gsignal(int pgid, int signum) 645 { 646 struct pgrp *pgrp; 647 648 if (pgid && (pgrp = pgfind(pgid))) 649 pgsignal(pgrp, signum, 0); 650 } 651 652 /* 653 * Send a signal to a process group. If checktty is 1, 654 * limit to members which have a controlling terminal. 655 */ 656 void 657 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 658 { 659 struct proc *p; 660 661 if (pgrp) 662 for (p = pgrp->pg_members.lh_first; p != 0; 663 p = p->p_pglist.le_next) 664 if (checkctty == 0 || p->p_flag & P_CONTROLT) 665 psignal(p, signum); 666 } 667 668 /* 669 * Send a signal caused by a trap to the current process. 670 * If it will be caught immediately, deliver it with correct code. 671 * Otherwise, post it normally. 672 */ 673 void 674 trapsignal(struct proc *p, int signum, u_long code) 675 { 676 struct sigacts *ps; 677 678 ps = p->p_sigacts; 679 if ((p->p_flag & P_TRACED) == 0 && 680 sigismember(&p->p_sigctx.ps_sigcatch, signum) && 681 !sigismember(&p->p_sigctx.ps_sigmask, signum)) { 682 p->p_stats->p_ru.ru_nsignals++; 683 #ifdef KTRACE 684 if (KTRPOINT(p, KTR_PSIG)) 685 ktrpsig(p, signum, 686 SIGACTION_PS(ps, signum).sa_handler, 687 &p->p_sigctx.ps_sigmask, code); 688 #endif 689 (*p->p_emul->e_sendsig)(SIGACTION_PS(ps, signum).sa_handler, 690 signum, &p->p_sigctx.ps_sigmask, code); 691 (void) splsched(); /* XXXSMP */ 692 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 693 &p->p_sigctx.ps_sigmask); 694 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 695 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 696 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 697 sigaddset(&p->p_sigctx.ps_sigignore, signum); 698 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 699 } 700 (void) spl0(); /* XXXSMP */ 701 } else { 702 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */ 703 p->p_sigctx.ps_sig = signum; /* XXX to verify code */ 704 psignal(p, signum); 705 } 706 } 707 708 /* 709 * Send the signal to the process. If the signal has an action, the action 710 * is usually performed by the target process rather than the caller; we add 711 * the signal to the set of pending signals for the process. 712 * 713 * Exceptions: 714 * o When a stop signal is sent to a sleeping process that takes the 715 * default action, the process is stopped without awakening it. 716 * o SIGCONT restarts stopped processes (or puts them back to sleep) 717 * regardless of the signal action (eg, blocked or ignored). 718 * 719 * Other ignored signals are discarded immediately. 720 * 721 * XXXSMP: Invoked as psignal() or sched_psignal(). 722 */ 723 void 724 psignal1(struct proc *p, int signum, 725 int dolock) /* XXXSMP: works, but icky */ 726 { 727 int s, prop; 728 sig_t action; 729 730 #ifdef DIAGNOSTIC 731 if (signum <= 0 || signum >= NSIG) 732 panic("psignal signal number"); 733 734 /* XXXSMP: works, but icky */ 735 if (dolock) 736 SCHED_ASSERT_UNLOCKED(); 737 else 738 SCHED_ASSERT_LOCKED(); 739 #endif 740 prop = sigprop[signum]; 741 742 /* 743 * If proc is traced, always give parent a chance. 744 */ 745 if (p->p_flag & P_TRACED) 746 action = SIG_DFL; 747 else { 748 /* 749 * If the signal is being ignored, 750 * then we forget about it immediately. 751 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore, 752 * and if it is set to SIG_IGN, 753 * action will be SIG_DFL here.) 754 */ 755 if (sigismember(&p->p_sigctx.ps_sigignore, signum)) 756 return; 757 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 758 action = SIG_HOLD; 759 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 760 action = SIG_CATCH; 761 else { 762 action = SIG_DFL; 763 764 if (prop & SA_KILL && p->p_nice > NZERO) 765 p->p_nice = NZERO; 766 767 /* 768 * If sending a tty stop signal to a member of an 769 * orphaned process group, discard the signal here if 770 * the action is default; don't stop the process below 771 * if sleeping, and don't clear any pending SIGCONT. 772 */ 773 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 774 return; 775 } 776 } 777 778 if (prop & SA_CONT) 779 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist); 780 781 if (prop & SA_STOP) 782 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist); 783 784 sigaddset(&p->p_sigctx.ps_siglist, signum); 785 786 /* CHECKSIGS() is "inlined" here. */ 787 p->p_sigctx.ps_sigcheck = 1; 788 789 /* 790 * Defer further processing for signals which are held, 791 * except that stopped processes must be continued by SIGCONT. 792 */ 793 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 794 return; 795 796 /* XXXSMP: works, but icky */ 797 if (dolock) 798 SCHED_LOCK(s); 799 800 switch (p->p_stat) { 801 case SSLEEP: 802 /* 803 * If process is sleeping uninterruptibly 804 * we can't interrupt the sleep... the signal will 805 * be noticed when the process returns through 806 * trap() or syscall(). 807 */ 808 if ((p->p_flag & P_SINTR) == 0) 809 goto out; 810 /* 811 * Process is sleeping and traced... make it runnable 812 * so it can discover the signal in issignal() and stop 813 * for the parent. 814 */ 815 if (p->p_flag & P_TRACED) 816 goto run; 817 /* 818 * If SIGCONT is default (or ignored) and process is 819 * asleep, we are finished; the process should not 820 * be awakened. 821 */ 822 if ((prop & SA_CONT) && action == SIG_DFL) { 823 sigdelset(&p->p_sigctx.ps_siglist, signum); 824 goto out; 825 } 826 /* 827 * When a sleeping process receives a stop 828 * signal, process immediately if possible. 829 */ 830 if ((prop & SA_STOP) && action == SIG_DFL) { 831 /* 832 * If a child holding parent blocked, 833 * stopping could cause deadlock. 834 */ 835 if (p->p_flag & P_PPWAIT) 836 goto out; 837 sigdelset(&p->p_sigctx.ps_siglist, signum); 838 p->p_xstat = signum; 839 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { 840 /* 841 * XXXSMP: recursive call; don't lock 842 * the second time around. 843 */ 844 sched_psignal(p->p_pptr, SIGCHLD); 845 } 846 proc_stop(p); /* XXXSMP: recurse? */ 847 goto out; 848 } 849 /* 850 * All other (caught or default) signals 851 * cause the process to run. 852 */ 853 goto runfast; 854 /*NOTREACHED*/ 855 856 case SSTOP: 857 /* 858 * If traced process is already stopped, 859 * then no further action is necessary. 860 */ 861 if (p->p_flag & P_TRACED) 862 goto out; 863 864 /* 865 * Kill signal always sets processes running. 866 */ 867 if (signum == SIGKILL) 868 goto runfast; 869 870 if (prop & SA_CONT) { 871 /* 872 * If SIGCONT is default (or ignored), we continue the 873 * process but don't leave the signal in p_sigctx.ps_siglist, as 874 * it has no further action. If SIGCONT is held, we 875 * continue the process and leave the signal in 876 * p_sigctx.ps_siglist. If the process catches SIGCONT, let it 877 * handle the signal itself. If it isn't waiting on 878 * an event, then it goes back to run state. 879 * Otherwise, process goes back to sleep state. 880 */ 881 if (action == SIG_DFL) 882 sigdelset(&p->p_sigctx.ps_siglist, signum); 883 if (action == SIG_CATCH) 884 goto runfast; 885 if (p->p_wchan == 0) 886 goto run; 887 p->p_stat = SSLEEP; 888 goto out; 889 } 890 891 if (prop & SA_STOP) { 892 /* 893 * Already stopped, don't need to stop again. 894 * (If we did the shell could get confused.) 895 */ 896 sigdelset(&p->p_sigctx.ps_siglist, signum); 897 goto out; 898 } 899 900 /* 901 * If process is sleeping interruptibly, then simulate a 902 * wakeup so that when it is continued, it will be made 903 * runnable and can look at the signal. But don't make 904 * the process runnable, leave it stopped. 905 */ 906 if (p->p_wchan && p->p_flag & P_SINTR) 907 unsleep(p); 908 goto out; 909 #ifdef __HAVE_AST_PERPROC 910 case SONPROC: 911 case SRUN: 912 case SIDL: 913 /* 914 * SONPROC: We're running, notice the signal when 915 * we return back to userspace. 916 * 917 * SRUN, SIDL: Notice the signal when we run again 918 * and return to back to userspace. 919 */ 920 signotify(p); 921 goto out; 922 923 default: 924 /* 925 * SDEAD, SZOMB: The signal will never be noticed. 926 */ 927 goto out; 928 #else /* ! __HAVE_AST_PERPROC */ 929 case SONPROC: 930 /* 931 * We're running; notice the signal. 932 */ 933 signotify(p); 934 goto out; 935 936 default: 937 /* 938 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal. 939 * It will either never be noticed, or noticed very soon. 940 */ 941 goto out; 942 #endif /* __HAVE_AST_PERPROC */ 943 } 944 /*NOTREACHED*/ 945 946 runfast: 947 /* 948 * Raise priority to at least PUSER. 949 */ 950 if (p->p_priority > PUSER) 951 p->p_priority = PUSER; 952 run: 953 setrunnable(p); /* XXXSMP: recurse? */ 954 out: 955 /* XXXSMP: works, but icky */ 956 if (dolock) 957 SCHED_UNLOCK(s); 958 } 959 960 static __inline int firstsig(const sigset_t *); 961 962 static __inline int 963 firstsig(const sigset_t *ss) 964 { 965 int sig; 966 967 sig = ffs(ss->__bits[0]); 968 if (sig != 0) 969 return (sig); 970 #if NSIG > 33 971 sig = ffs(ss->__bits[1]); 972 if (sig != 0) 973 return (sig + 32); 974 #endif 975 #if NSIG > 65 976 sig = ffs(ss->__bits[2]); 977 if (sig != 0) 978 return (sig + 64); 979 #endif 980 #if NSIG > 97 981 sig = ffs(ss->__bits[3]); 982 if (sig != 0) 983 return (sig + 96); 984 #endif 985 return (0); 986 } 987 988 /* 989 * If the current process has received a signal (should be caught or cause 990 * termination, should interrupt current syscall), return the signal number. 991 * Stop signals with default action are processed immediately, then cleared; 992 * they aren't returned. This is checked after each entry to the system for 993 * a syscall or trap (though this can usually be done without calling issignal 994 * by checking the pending signal masks in the CURSIG macro.) The normal call 995 * sequence is 996 * 997 * while (signum = CURSIG(curproc)) 998 * postsig(signum); 999 */ 1000 int 1001 issignal(struct proc *p) 1002 { 1003 int s, signum, prop; 1004 sigset_t ss; 1005 1006 for (;;) { 1007 sigpending1(p, &ss); 1008 if (p->p_flag & P_PPWAIT) 1009 sigminusset(&stopsigmask, &ss); 1010 signum = firstsig(&ss); 1011 if (signum == 0) { /* no signal to send */ 1012 p->p_sigctx.ps_sigcheck = 0; 1013 return (0); 1014 } 1015 /* take the signal! */ 1016 sigdelset(&p->p_sigctx.ps_siglist, signum); 1017 1018 /* 1019 * We should see pending but ignored signals 1020 * only if P_TRACED was on when they were posted. 1021 */ 1022 if (sigismember(&p->p_sigctx.ps_sigignore, signum) && 1023 (p->p_flag & P_TRACED) == 0) 1024 continue; 1025 1026 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1027 /* 1028 * If traced, always stop, and stay 1029 * stopped until released by the debugger. 1030 */ 1031 p->p_xstat = signum; 1032 if ((p->p_flag & P_FSTRACE) == 0) 1033 psignal(p->p_pptr, SIGCHLD); 1034 do { 1035 SCHED_LOCK(s); 1036 proc_stop(p); 1037 mi_switch(p); 1038 SCHED_ASSERT_UNLOCKED(); 1039 splx(s); 1040 } while (!trace_req(p) && p->p_flag & P_TRACED); 1041 1042 /* 1043 * If we are no longer being traced, or the parent 1044 * didn't give us a signal, look for more signals. 1045 */ 1046 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 1047 continue; 1048 1049 /* 1050 * If the new signal is being masked, look for other 1051 * signals. 1052 */ 1053 signum = p->p_xstat; 1054 /* 1055 * `p->p_sigctx.ps_siglist |= mask' is done 1056 * in setrunnable(). 1057 */ 1058 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1059 continue; 1060 /* take the signal! */ 1061 sigdelset(&p->p_sigctx.ps_siglist, signum); 1062 } 1063 1064 prop = sigprop[signum]; 1065 1066 /* 1067 * Decide whether the signal should be returned. 1068 * Return the signal's number, or fall through 1069 * to clear it from the pending mask. 1070 */ 1071 switch ((long)SIGACTION(p, signum).sa_handler) { 1072 1073 case (long)SIG_DFL: 1074 /* 1075 * Don't take default actions on system processes. 1076 */ 1077 if (p->p_pid <= 1) { 1078 #ifdef DIAGNOSTIC 1079 /* 1080 * Are you sure you want to ignore SIGSEGV 1081 * in init? XXX 1082 */ 1083 printf("Process (pid %d) got signal %d\n", 1084 p->p_pid, signum); 1085 #endif 1086 break; /* == ignore */ 1087 } 1088 /* 1089 * If there is a pending stop signal to process 1090 * with default action, stop here, 1091 * then clear the signal. However, 1092 * if process is member of an orphaned 1093 * process group, ignore tty stop signals. 1094 */ 1095 if (prop & SA_STOP) { 1096 if (p->p_flag & P_TRACED || 1097 (p->p_pgrp->pg_jobc == 0 && 1098 prop & SA_TTYSTOP)) 1099 break; /* == ignore */ 1100 p->p_xstat = signum; 1101 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1102 psignal(p->p_pptr, SIGCHLD); 1103 SCHED_LOCK(s); 1104 proc_stop(p); 1105 mi_switch(p); 1106 SCHED_ASSERT_UNLOCKED(); 1107 splx(s); 1108 break; 1109 } else if (prop & SA_IGNORE) { 1110 /* 1111 * Except for SIGCONT, shouldn't get here. 1112 * Default action is to ignore; drop it. 1113 */ 1114 break; /* == ignore */ 1115 } else 1116 goto keep; 1117 /*NOTREACHED*/ 1118 1119 case (long)SIG_IGN: 1120 /* 1121 * Masking above should prevent us ever trying 1122 * to take action on an ignored signal other 1123 * than SIGCONT, unless process is traced. 1124 */ 1125 if ((prop & SA_CONT) == 0 && 1126 (p->p_flag & P_TRACED) == 0) 1127 printf("issignal\n"); 1128 break; /* == ignore */ 1129 1130 default: 1131 /* 1132 * This signal has an action, let 1133 * postsig() process it. 1134 */ 1135 goto keep; 1136 } 1137 } 1138 /* NOTREACHED */ 1139 1140 keep: 1141 /* leave the signal for later */ 1142 sigaddset(&p->p_sigctx.ps_siglist, signum); 1143 CHECKSIGS(p); 1144 return (signum); 1145 } 1146 1147 /* 1148 * Put the argument process into the stopped state and notify the parent 1149 * via wakeup. Signals are handled elsewhere. The process must not be 1150 * on the run queue. 1151 */ 1152 static void 1153 proc_stop(struct proc *p) 1154 { 1155 1156 SCHED_ASSERT_LOCKED(); 1157 1158 p->p_stat = SSTOP; 1159 p->p_flag &= ~P_WAITED; 1160 sched_wakeup((caddr_t)p->p_pptr); 1161 } 1162 1163 /* 1164 * Take the action for the specified signal 1165 * from the current set of pending signals. 1166 */ 1167 void 1168 postsig(int signum) 1169 { 1170 struct proc *p; 1171 struct sigacts *ps; 1172 sig_t action; 1173 u_long code; 1174 sigset_t *returnmask; 1175 1176 p = curproc; 1177 ps = p->p_sigacts; 1178 #ifdef DIAGNOSTIC 1179 if (signum == 0) 1180 panic("postsig"); 1181 #endif 1182 1183 KERNEL_PROC_LOCK(p); 1184 1185 sigdelset(&p->p_sigctx.ps_siglist, signum); 1186 action = SIGACTION_PS(ps, signum).sa_handler; 1187 #ifdef KTRACE 1188 if (KTRPOINT(p, KTR_PSIG)) 1189 ktrpsig(p, 1190 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ? 1191 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0); 1192 #endif 1193 if (action == SIG_DFL) { 1194 /* 1195 * Default action, where the default is to kill 1196 * the process. (Other cases were ignored above.) 1197 */ 1198 sigexit(p, signum); 1199 /* NOTREACHED */ 1200 } else { 1201 /* 1202 * If we get here, the signal must be caught. 1203 */ 1204 #ifdef DIAGNOSTIC 1205 if (action == SIG_IGN || 1206 sigismember(&p->p_sigctx.ps_sigmask, signum)) 1207 panic("postsig action"); 1208 #endif 1209 /* 1210 * Set the new mask value and also defer further 1211 * occurences of this signal. 1212 * 1213 * Special case: user has done a sigpause. Here the 1214 * current mask is not of interest, but rather the 1215 * mask from before the sigpause is what we want 1216 * restored after the signal processing is completed. 1217 */ 1218 if (p->p_sigctx.ps_flags & SAS_OLDMASK) { 1219 returnmask = &p->p_sigctx.ps_oldmask; 1220 p->p_sigctx.ps_flags &= ~SAS_OLDMASK; 1221 } else 1222 returnmask = &p->p_sigctx.ps_sigmask; 1223 p->p_stats->p_ru.ru_nsignals++; 1224 if (p->p_sigctx.ps_sig != signum) { 1225 code = 0; 1226 } else { 1227 code = p->p_sigctx.ps_code; 1228 p->p_sigctx.ps_code = 0; 1229 p->p_sigctx.ps_sig = 0; 1230 } 1231 (*p->p_emul->e_sendsig)(action, signum, returnmask, code); 1232 (void) splsched(); /* XXXSMP */ 1233 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 1234 &p->p_sigctx.ps_sigmask); 1235 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 1236 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1237 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1238 sigaddset(&p->p_sigctx.ps_sigignore, signum); 1239 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 1240 } 1241 (void) spl0(); /* XXXSMP */ 1242 } 1243 1244 KERNEL_PROC_UNLOCK(p); 1245 } 1246 1247 /* 1248 * Kill the current process for stated reason. 1249 */ 1250 void 1251 killproc(struct proc *p, char *why) 1252 { 1253 1254 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1255 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1256 psignal(p, SIGKILL); 1257 } 1258 1259 /* 1260 * Force the current process to exit with the specified signal, dumping core 1261 * if appropriate. We bypass the normal tests for masked and caught signals, 1262 * allowing unrecoverable failures to terminate the process without changing 1263 * signal state. Mark the accounting record with the signal termination. 1264 * If dumping core, save the signal number for the debugger. Calls exit and 1265 * does not return. 1266 */ 1267 1268 #if defined(DEBUG) 1269 int kern_logsigexit = 1; /* not static to make public for sysctl */ 1270 #else 1271 int kern_logsigexit = 0; /* not static to make public for sysctl */ 1272 #endif 1273 1274 static const char logcoredump[] = 1275 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 1276 static const char lognocoredump[] = 1277 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 1278 1279 void 1280 sigexit(struct proc *p, int signum) 1281 { 1282 int error, exitsig; 1283 1284 exitsig = signum; 1285 p->p_acflag |= AXSIG; 1286 if (sigprop[signum] & SA_CORE) { 1287 p->p_sigctx.ps_sig = signum; 1288 if ((error = coredump(p)) == 0) 1289 exitsig |= WCOREFLAG; 1290 1291 if (kern_logsigexit) { 1292 int uid = p->p_cred && p->p_ucred ? 1293 p->p_ucred->cr_uid : -1; 1294 1295 if (error) 1296 log(LOG_INFO, lognocoredump, p->p_pid, 1297 p->p_comm, uid, signum, error); 1298 else 1299 log(LOG_INFO, logcoredump, p->p_pid, 1300 p->p_comm, uid, signum); 1301 } 1302 1303 } 1304 1305 exit1(p, W_EXITCODE(0, exitsig)); 1306 /* NOTREACHED */ 1307 } 1308 1309 /* 1310 * Dump core, into a file named "progname.core" or "core" (depending on the 1311 * value of shortcorename), unless the process was setuid/setgid. 1312 */ 1313 int 1314 coredump(struct proc *p) 1315 { 1316 struct vnode *vp; 1317 struct vmspace *vm; 1318 struct ucred *cred; 1319 struct nameidata nd; 1320 struct vattr vattr; 1321 int error, error1; 1322 char name[MAXPATHLEN]; 1323 struct core core; 1324 1325 vm = p->p_vmspace; 1326 cred = p->p_cred->pc_ucred; 1327 1328 /* 1329 * Make sure the process has not set-id, to prevent data leaks. 1330 */ 1331 if (p->p_flag & P_SUGID) 1332 return (EPERM); 1333 1334 /* 1335 * Refuse to core if the data + stack + user size is larger than 1336 * the core dump limit. XXX THIS IS WRONG, because of mapped 1337 * data. 1338 */ 1339 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 1340 p->p_rlimit[RLIMIT_CORE].rlim_cur) 1341 return (EFBIG); /* better error code? */ 1342 1343 /* 1344 * The core dump will go in the current working directory. Make 1345 * sure that the directory is still there and that the mount flags 1346 * allow us to write core dumps there. 1347 */ 1348 vp = p->p_cwdi->cwdi_cdir; 1349 if (vp->v_mount == NULL || 1350 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) 1351 return (EPERM); 1352 1353 error = build_corename(p, name); 1354 if (error) 1355 return error; 1356 1357 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1358 error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR); 1359 if (error) 1360 return (error); 1361 vp = nd.ni_vp; 1362 1363 /* Don't dump to non-regular files or files with links. */ 1364 if (vp->v_type != VREG || 1365 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1366 error = EINVAL; 1367 goto out; 1368 } 1369 VATTR_NULL(&vattr); 1370 vattr.va_size = 0; 1371 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1372 VOP_SETATTR(vp, &vattr, cred, p); 1373 p->p_acflag |= ACORE; 1374 1375 #if COMPAT_NETBSD32 1376 if (p->p_flag & P_32) 1377 return (coredump32(p, vp)); 1378 #endif 1379 #if 0 1380 /* 1381 * XXX 1382 * It would be nice if we at least dumped the signal state (and made it 1383 * available at run time to the debugger, as well), but this code 1384 * hasn't actually had any effect for a long time, since we don't dump 1385 * the user area. For now, it's dead. 1386 */ 1387 memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc)); 1388 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); 1389 #endif 1390 1391 core.c_midmag = 0; 1392 strncpy(core.c_name, p->p_comm, MAXCOMLEN); 1393 core.c_nseg = 0; 1394 core.c_signo = p->p_sigctx.ps_sig; 1395 core.c_ucode = p->p_sigctx.ps_code; 1396 core.c_cpusize = 0; 1397 core.c_tsize = (u_long)ctob(vm->vm_tsize); 1398 core.c_dsize = (u_long)ctob(vm->vm_dsize); 1399 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize)); 1400 error = cpu_coredump(p, vp, cred, &core); 1401 if (error) 1402 goto out; 1403 /* 1404 * uvm_coredump() spits out all appropriate segments. 1405 * All that's left to do is to write the core header. 1406 */ 1407 error = uvm_coredump(p, vp, cred, &core); 1408 if (error) 1409 goto out; 1410 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core, 1411 (int)core.c_hdrsize, (off_t)0, 1412 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p); 1413 out: 1414 VOP_UNLOCK(vp, 0); 1415 error1 = vn_close(vp, FWRITE, cred, p); 1416 if (error == 0) 1417 error = error1; 1418 return (error); 1419 } 1420 1421 #if COMPAT_NETBSD32 1422 /* 1423 * Same as coredump, but generates a 32-bit image. 1424 */ 1425 int 1426 coredump32(struct proc *p, struct vnode *vp) 1427 { 1428 struct vmspace *vm; 1429 struct ucred *cred; 1430 int error, error1; 1431 struct core32 core; 1432 1433 vm = p->p_vmspace; 1434 cred = p->p_cred->pc_ucred; 1435 #if 0 1436 /* 1437 * XXX 1438 * It would be nice if we at least dumped the signal state (and made it 1439 * available at run time to the debugger, as well), but this code 1440 * hasn't actually had any effect for a long time, since we don't dump 1441 * the user area. For now, it's dead. 1442 */ 1443 memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc)); 1444 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); 1445 #endif 1446 1447 core.c_midmag = 0; 1448 strncpy(core.c_name, p->p_comm, MAXCOMLEN); 1449 core.c_nseg = 0; 1450 core.c_signo = p->p_sigctx.ps_sig; 1451 core.c_ucode = p->p_sigctx.ps_code; 1452 core.c_cpusize = 0; 1453 core.c_tsize = (u_long)ctob(vm->vm_tsize); 1454 core.c_dsize = (u_long)ctob(vm->vm_dsize); 1455 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize)); 1456 error = cpu_coredump32(p, vp, cred, &core); 1457 if (error) 1458 goto out; 1459 /* 1460 * uvm_coredump() spits out all appropriate segments. 1461 * All that's left to do is to write the core header. 1462 */ 1463 error = uvm_coredump32(p, vp, cred, &core); 1464 if (error) 1465 goto out; 1466 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core, 1467 (int)core.c_hdrsize, (off_t)0, 1468 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p); 1469 out: 1470 VOP_UNLOCK(vp, 0); 1471 error1 = vn_close(vp, FWRITE, cred, p); 1472 if (error == 0) 1473 error = error1; 1474 return (error); 1475 } 1476 #endif 1477 1478 /* 1479 * Nonexistent system call-- signal process (may want to handle it). 1480 * Flag error in case process won't see signal immediately (blocked or ignored). 1481 */ 1482 /* ARGSUSED */ 1483 int 1484 sys_nosys(struct proc *p, void *v, register_t *retval) 1485 { 1486 1487 psignal(p, SIGSYS); 1488 return (ENOSYS); 1489 } 1490 1491 static int 1492 build_corename(struct proc *p, char dst[MAXPATHLEN]) 1493 { 1494 const char *s; 1495 char *d, *end; 1496 int i; 1497 1498 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN; 1499 *s != '\0'; s++) { 1500 if (*s == '%') { 1501 switch (*(s + 1)) { 1502 case 'n': 1503 i = snprintf(d, end - d, "%s", p->p_comm); 1504 break; 1505 case 'p': 1506 i = snprintf(d, end - d, "%d", p->p_pid); 1507 break; 1508 case 'u': 1509 i = snprintf(d, end - d, "%s", 1510 p->p_pgrp->pg_session->s_login); 1511 break; 1512 case 't': 1513 i = snprintf(d, end - d, "%ld", 1514 p->p_stats->p_start.tv_sec); 1515 break; 1516 default: 1517 goto copy; 1518 } 1519 d += i; 1520 s++; 1521 } else { 1522 copy: *d = *s; 1523 d++; 1524 } 1525 if (d >= end) 1526 return (ENAMETOOLONG); 1527 } 1528 *d = '\0'; 1529 return (0); 1530 } 1531 1532 /* 1533 * Returns true if signal is ignored or masked for passed process. 1534 */ 1535 int 1536 sigismasked(struct proc *p, int sig) 1537 { 1538 1539 return sigismember(&p->p_sigctx.ps_sigignore, SIGTTOU) 1540 || sigismember(&p->p_sigctx.ps_sigmask, SIGTTOU); 1541 } 1542