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