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