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