1 /* $NetBSD: kern_sig.c,v 1.239 2006/11/08 20:18:33 drochner 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.239 2006/11/08 20:18:33 drochner Exp $"); 41 42 #include "opt_coredump.h" 43 #include "opt_ktrace.h" 44 #include "opt_ptrace.h" 45 #include "opt_multiprocessor.h" 46 #include "opt_compat_sunos.h" 47 #include "opt_compat_netbsd.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/ucontext.h> 73 #include <sys/sa.h> 74 #include <sys/savar.h> 75 #include <sys/exec.h> 76 #include <sys/sysctl.h> 77 #include <sys/kauth.h> 78 79 #include <sys/mount.h> 80 #include <sys/syscallargs.h> 81 82 #include <machine/cpu.h> 83 84 #include <sys/user.h> /* for coredump */ 85 86 #include <uvm/uvm.h> 87 #include <uvm/uvm_extern.h> 88 89 #ifdef COREDUMP 90 static int build_corename(struct proc *, char *, const char *, size_t); 91 #endif 92 static void ksiginfo_exithook(struct proc *, void *); 93 static void ksiginfo_queue(struct proc *, const ksiginfo_t *, ksiginfo_t **); 94 static ksiginfo_t *ksiginfo_dequeue(struct proc *, int); 95 static void kpsignal2(struct proc *, const ksiginfo_t *); 96 97 sigset_t contsigmask, stopsigmask, sigcantmask; 98 99 struct pool sigacts_pool; /* memory pool for sigacts structures */ 100 101 /* 102 * struct sigacts memory pool allocator. 103 */ 104 105 static void * 106 sigacts_poolpage_alloc(struct pool *pp, int flags) 107 { 108 109 return (void *)uvm_km_alloc(kernel_map, 110 (PAGE_SIZE)*2, (PAGE_SIZE)*2, 111 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) 112 | UVM_KMF_WIRED); 113 } 114 115 static void 116 sigacts_poolpage_free(struct pool *pp, void *v) 117 { 118 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED); 119 } 120 121 static struct pool_allocator sigactspool_allocator = { 122 .pa_alloc = sigacts_poolpage_alloc, 123 .pa_free = sigacts_poolpage_free, 124 }; 125 126 static POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo", 127 &pool_allocator_nointr); 128 static POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL); 129 130 static ksiginfo_t * 131 ksiginfo_alloc(int prflags) 132 { 133 int s; 134 ksiginfo_t *ksi; 135 136 s = splsoftclock(); 137 ksi = pool_get(&ksiginfo_pool, prflags); 138 splx(s); 139 return ksi; 140 } 141 142 static void 143 ksiginfo_free(ksiginfo_t *ksi) 144 { 145 int s; 146 147 s = splsoftclock(); 148 pool_put(&ksiginfo_pool, ksi); 149 splx(s); 150 } 151 152 /* 153 * Remove and return the first ksiginfo element that matches our requested 154 * signal, or return NULL if one not found. 155 */ 156 static ksiginfo_t * 157 ksiginfo_dequeue(struct proc *p, int signo) 158 { 159 ksiginfo_t *ksi; 160 int s; 161 162 s = splsoftclock(); 163 simple_lock(&p->p_sigctx.ps_silock); 164 CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) { 165 if (ksi->ksi_signo == signo) { 166 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list); 167 goto out; 168 } 169 } 170 ksi = NULL; 171 out: 172 simple_unlock(&p->p_sigctx.ps_silock); 173 splx(s); 174 return ksi; 175 } 176 177 /* 178 * Append a new ksiginfo element to the list of pending ksiginfo's, if 179 * we need to (SA_SIGINFO was requested). We replace non RT signals if 180 * they already existed in the queue and we add new entries for RT signals, 181 * or for non RT signals with non-existing entries. 182 */ 183 static void 184 ksiginfo_queue(struct proc *p, const ksiginfo_t *ksi, ksiginfo_t **newkp) 185 { 186 ksiginfo_t *kp; 187 struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo); 188 int s; 189 190 if ((sa->sa_flags & SA_SIGINFO) == 0) 191 return; 192 193 /* 194 * If there's no info, don't save it. 195 */ 196 if (KSI_EMPTY_P(ksi)) 197 return; 198 199 s = splsoftclock(); 200 simple_lock(&p->p_sigctx.ps_silock); 201 #ifdef notyet /* XXX: QUEUING */ 202 if (ksi->ksi_signo < SIGRTMIN) 203 #endif 204 { 205 CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) { 206 if (kp->ksi_signo == ksi->ksi_signo) { 207 KSI_COPY(ksi, kp); 208 goto out; 209 } 210 } 211 } 212 if (newkp && *newkp) { 213 kp = *newkp; 214 *newkp = NULL; 215 } else { 216 SCHED_ASSERT_UNLOCKED(); 217 kp = ksiginfo_alloc(PR_NOWAIT); 218 if (kp == NULL) { 219 #ifdef DIAGNOSTIC 220 printf("Out of memory allocating siginfo for pid %d\n", 221 p->p_pid); 222 #endif 223 goto out; 224 } 225 } 226 *kp = *ksi; 227 CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list); 228 out: 229 simple_unlock(&p->p_sigctx.ps_silock); 230 splx(s); 231 } 232 233 /* 234 * free all pending ksiginfo on exit 235 */ 236 static void 237 ksiginfo_exithook(struct proc *p, void *v) 238 { 239 int s; 240 241 s = splsoftclock(); 242 simple_lock(&p->p_sigctx.ps_silock); 243 while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) { 244 ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo); 245 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list); 246 ksiginfo_free(ksi); 247 } 248 simple_unlock(&p->p_sigctx.ps_silock); 249 splx(s); 250 } 251 252 /* 253 * Initialize signal-related data structures. 254 */ 255 void 256 signal_init(void) 257 { 258 259 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2; 260 261 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl", 262 sizeof(struct sigacts) > PAGE_SIZE ? 263 &sigactspool_allocator : &pool_allocator_nointr); 264 265 exithook_establish(ksiginfo_exithook, NULL); 266 exechook_establish(ksiginfo_exithook, NULL); 267 } 268 269 /* 270 * Create an initial sigctx structure, using the same signal state 271 * as p. If 'share' is set, share the sigctx_proc part, otherwise just 272 * copy it from parent. 273 */ 274 void 275 sigactsinit(struct proc *np, struct proc *pp, int share) 276 { 277 struct sigacts *ps; 278 279 if (share) { 280 np->p_sigacts = pp->p_sigacts; 281 pp->p_sigacts->sa_refcnt++; 282 } else { 283 ps = pool_get(&sigacts_pool, PR_WAITOK); 284 if (pp) 285 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts)); 286 else 287 memset(ps, '\0', sizeof(struct sigacts)); 288 ps->sa_refcnt = 1; 289 np->p_sigacts = ps; 290 } 291 } 292 293 /* 294 * Make this process not share its sigctx, maintaining all 295 * signal state. 296 */ 297 void 298 sigactsunshare(struct proc *p) 299 { 300 struct sigacts *oldps; 301 302 if (p->p_sigacts->sa_refcnt == 1) 303 return; 304 305 oldps = p->p_sigacts; 306 sigactsinit(p, NULL, 0); 307 308 if (--oldps->sa_refcnt == 0) 309 pool_put(&sigacts_pool, oldps); 310 } 311 312 /* 313 * Release a sigctx structure. 314 */ 315 void 316 sigactsfree(struct sigacts *ps) 317 { 318 319 if (--ps->sa_refcnt > 0) 320 return; 321 322 pool_put(&sigacts_pool, ps); 323 } 324 325 int 326 sigaction1(struct proc *p, int signum, const struct sigaction *nsa, 327 struct sigaction *osa, const void *tramp, int vers) 328 { 329 struct sigacts *ps; 330 int prop; 331 332 ps = p->p_sigacts; 333 if (signum <= 0 || signum >= NSIG) 334 return (EINVAL); 335 336 /* 337 * Trampoline ABI version 0 is reserved for the legacy 338 * kernel-provided on-stack trampoline. Conversely, if we are 339 * using a non-0 ABI version, we must have a trampoline. Only 340 * validate the vers if a new sigaction was supplied. Emulations 341 * use legacy kernel trampolines with version 0, alternatively 342 * check for that too. 343 */ 344 if ((vers != 0 && tramp == NULL) || 345 #ifdef SIGTRAMP_VALID 346 (nsa != NULL && 347 ((vers == 0) ? 348 (p->p_emul->e_sigcode == NULL) : 349 !SIGTRAMP_VALID(vers))) || 350 #endif 351 (vers == 0 && tramp != NULL)) 352 return (EINVAL); 353 354 if (osa) 355 *osa = SIGACTION_PS(ps, signum); 356 357 if (nsa) { 358 if (nsa->sa_flags & ~SA_ALLBITS) 359 return (EINVAL); 360 361 prop = sigprop[signum]; 362 if (prop & SA_CANTMASK) 363 return (EINVAL); 364 365 (void) splsched(); /* XXXSMP */ 366 SIGACTION_PS(ps, signum) = *nsa; 367 ps->sa_sigdesc[signum].sd_tramp = tramp; 368 ps->sa_sigdesc[signum].sd_vers = vers; 369 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask); 370 if ((prop & SA_NORESET) != 0) 371 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND; 372 if (signum == SIGCHLD) { 373 if (nsa->sa_flags & SA_NOCLDSTOP) 374 p->p_flag |= P_NOCLDSTOP; 375 else 376 p->p_flag &= ~P_NOCLDSTOP; 377 if (nsa->sa_flags & SA_NOCLDWAIT) { 378 /* 379 * Paranoia: since SA_NOCLDWAIT is implemented 380 * by reparenting the dying child to PID 1 (and 381 * trust it to reap the zombie), PID 1 itself 382 * is forbidden to set SA_NOCLDWAIT. 383 */ 384 if (p->p_pid == 1) 385 p->p_flag &= ~P_NOCLDWAIT; 386 else 387 p->p_flag |= P_NOCLDWAIT; 388 } else 389 p->p_flag &= ~P_NOCLDWAIT; 390 391 if (nsa->sa_handler == SIG_IGN) { 392 /* 393 * Paranoia: same as above. 394 */ 395 if (p->p_pid == 1) 396 p->p_flag &= ~P_CLDSIGIGN; 397 else 398 p->p_flag |= P_CLDSIGIGN; 399 } else 400 p->p_flag &= ~P_CLDSIGIGN; 401 402 } 403 if ((nsa->sa_flags & SA_NODEFER) == 0) 404 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum); 405 else 406 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum); 407 /* 408 * Set bit in p_sigctx.ps_sigignore for signals that are set to 409 * SIG_IGN, and for signals set to SIG_DFL where the default is 410 * to ignore. However, don't put SIGCONT in 411 * p_sigctx.ps_sigignore, as we have to restart the process. 412 */ 413 if (nsa->sa_handler == SIG_IGN || 414 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) { 415 /* never to be seen again */ 416 sigdelset(&p->p_sigctx.ps_siglist, signum); 417 if (signum != SIGCONT) { 418 /* easier in psignal */ 419 sigaddset(&p->p_sigctx.ps_sigignore, signum); 420 } 421 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 422 } else { 423 sigdelset(&p->p_sigctx.ps_sigignore, signum); 424 if (nsa->sa_handler == SIG_DFL) 425 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 426 else 427 sigaddset(&p->p_sigctx.ps_sigcatch, signum); 428 } 429 (void) spl0(); 430 } 431 432 return (0); 433 } 434 435 #ifdef COMPAT_16 436 /* ARGSUSED */ 437 int 438 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval) 439 { 440 struct compat_16_sys___sigaction14_args /* { 441 syscallarg(int) signum; 442 syscallarg(const struct sigaction *) nsa; 443 syscallarg(struct sigaction *) osa; 444 } */ *uap = v; 445 struct proc *p; 446 struct sigaction nsa, osa; 447 int error; 448 449 if (SCARG(uap, nsa)) { 450 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 451 if (error) 452 return (error); 453 } 454 p = l->l_proc; 455 error = sigaction1(p, SCARG(uap, signum), 456 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 457 NULL, 0); 458 if (error) 459 return (error); 460 if (SCARG(uap, osa)) { 461 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 462 if (error) 463 return (error); 464 } 465 return (0); 466 } 467 #endif 468 469 /* ARGSUSED */ 470 int 471 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval) 472 { 473 struct sys___sigaction_sigtramp_args /* { 474 syscallarg(int) signum; 475 syscallarg(const struct sigaction *) nsa; 476 syscallarg(struct sigaction *) osa; 477 syscallarg(void *) tramp; 478 syscallarg(int) vers; 479 } */ *uap = v; 480 struct proc *p = l->l_proc; 481 struct sigaction nsa, osa; 482 int error; 483 484 if (SCARG(uap, nsa)) { 485 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 486 if (error) 487 return (error); 488 } 489 error = sigaction1(p, SCARG(uap, signum), 490 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 491 SCARG(uap, tramp), SCARG(uap, vers)); 492 if (error) 493 return (error); 494 if (SCARG(uap, osa)) { 495 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 496 if (error) 497 return (error); 498 } 499 return (0); 500 } 501 502 /* 503 * Initialize signal state for process 0; 504 * set to ignore signals that are ignored by default and disable the signal 505 * stack. 506 */ 507 void 508 siginit(struct proc *p) 509 { 510 struct sigacts *ps; 511 int signum, prop; 512 513 ps = p->p_sigacts; 514 sigemptyset(&contsigmask); 515 sigemptyset(&stopsigmask); 516 sigemptyset(&sigcantmask); 517 for (signum = 1; signum < NSIG; signum++) { 518 prop = sigprop[signum]; 519 if (prop & SA_CONT) 520 sigaddset(&contsigmask, signum); 521 if (prop & SA_STOP) 522 sigaddset(&stopsigmask, signum); 523 if (prop & SA_CANTMASK) 524 sigaddset(&sigcantmask, signum); 525 if (prop & SA_IGNORE && signum != SIGCONT) 526 sigaddset(&p->p_sigctx.ps_sigignore, signum); 527 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 528 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 529 } 530 sigemptyset(&p->p_sigctx.ps_sigcatch); 531 p->p_sigctx.ps_sigwaited = NULL; 532 p->p_flag &= ~P_NOCLDSTOP; 533 534 /* 535 * Reset stack state to the user stack. 536 */ 537 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 538 p->p_sigctx.ps_sigstk.ss_size = 0; 539 p->p_sigctx.ps_sigstk.ss_sp = 0; 540 541 /* One reference. */ 542 ps->sa_refcnt = 1; 543 } 544 545 /* 546 * Reset signals for an exec of the specified process. 547 */ 548 void 549 execsigs(struct proc *p) 550 { 551 struct sigacts *ps; 552 int signum, prop; 553 554 sigactsunshare(p); 555 556 ps = p->p_sigacts; 557 558 /* 559 * Reset caught signals. Held signals remain held 560 * through p_sigctx.ps_sigmask (unless they were caught, 561 * and are now ignored by default). 562 */ 563 for (signum = 1; signum < NSIG; signum++) { 564 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 565 prop = sigprop[signum]; 566 if (prop & SA_IGNORE) { 567 if ((prop & SA_CONT) == 0) 568 sigaddset(&p->p_sigctx.ps_sigignore, 569 signum); 570 sigdelset(&p->p_sigctx.ps_siglist, signum); 571 } 572 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 573 } 574 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask); 575 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART; 576 } 577 sigemptyset(&p->p_sigctx.ps_sigcatch); 578 p->p_sigctx.ps_sigwaited = NULL; 579 580 /* 581 * Reset no zombies if child dies flag as Solaris does. 582 */ 583 p->p_flag &= ~(P_NOCLDWAIT | P_CLDSIGIGN); 584 if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN) 585 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL; 586 587 /* 588 * Reset stack state to the user stack. 589 */ 590 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE; 591 p->p_sigctx.ps_sigstk.ss_size = 0; 592 p->p_sigctx.ps_sigstk.ss_sp = 0; 593 } 594 595 int 596 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss) 597 { 598 599 if (oss) 600 *oss = p->p_sigctx.ps_sigmask; 601 602 if (nss) { 603 (void)splsched(); /* XXXSMP */ 604 switch (how) { 605 case SIG_BLOCK: 606 sigplusset(nss, &p->p_sigctx.ps_sigmask); 607 break; 608 case SIG_UNBLOCK: 609 sigminusset(nss, &p->p_sigctx.ps_sigmask); 610 CHECKSIGS(p); 611 break; 612 case SIG_SETMASK: 613 p->p_sigctx.ps_sigmask = *nss; 614 CHECKSIGS(p); 615 break; 616 default: 617 (void)spl0(); /* XXXSMP */ 618 return (EINVAL); 619 } 620 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 621 (void)spl0(); /* XXXSMP */ 622 } 623 624 return (0); 625 } 626 627 /* 628 * Manipulate signal mask. 629 * Note that we receive new mask, not pointer, 630 * and return old mask as return value; 631 * the library stub does the rest. 632 */ 633 int 634 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval) 635 { 636 struct sys___sigprocmask14_args /* { 637 syscallarg(int) how; 638 syscallarg(const sigset_t *) set; 639 syscallarg(sigset_t *) oset; 640 } */ *uap = v; 641 struct proc *p; 642 sigset_t nss, oss; 643 int error; 644 645 if (SCARG(uap, set)) { 646 error = copyin(SCARG(uap, set), &nss, sizeof(nss)); 647 if (error) 648 return (error); 649 } 650 p = l->l_proc; 651 error = sigprocmask1(p, SCARG(uap, how), 652 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0); 653 if (error) 654 return (error); 655 if (SCARG(uap, oset)) { 656 error = copyout(&oss, SCARG(uap, oset), sizeof(oss)); 657 if (error) 658 return (error); 659 } 660 return (0); 661 } 662 663 void 664 sigpending1(struct proc *p, sigset_t *ss) 665 { 666 667 *ss = p->p_sigctx.ps_siglist; 668 sigminusset(&p->p_sigctx.ps_sigmask, ss); 669 } 670 671 /* ARGSUSED */ 672 int 673 sys___sigpending14(struct lwp *l, void *v, register_t *retval) 674 { 675 struct sys___sigpending14_args /* { 676 syscallarg(sigset_t *) set; 677 } */ *uap = v; 678 struct proc *p; 679 sigset_t ss; 680 681 p = l->l_proc; 682 sigpending1(p, &ss); 683 return (copyout(&ss, SCARG(uap, set), sizeof(ss))); 684 } 685 686 int 687 sigsuspend1(struct proc *p, const sigset_t *ss) 688 { 689 struct sigacts *ps; 690 691 ps = p->p_sigacts; 692 if (ss) { 693 /* 694 * When returning from sigpause, we want 695 * the old mask to be restored after the 696 * signal handler has finished. Thus, we 697 * save it here and mark the sigctx structure 698 * to indicate this. 699 */ 700 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask; 701 p->p_sigctx.ps_flags |= SAS_OLDMASK; 702 (void) splsched(); /* XXXSMP */ 703 p->p_sigctx.ps_sigmask = *ss; 704 CHECKSIGS(p); 705 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask); 706 (void) spl0(); /* XXXSMP */ 707 } 708 709 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 710 /* void */; 711 712 /* always return EINTR rather than ERESTART... */ 713 return (EINTR); 714 } 715 716 /* 717 * Suspend process until signal, providing mask to be set 718 * in the meantime. Note nonstandard calling convention: 719 * libc stub passes mask, not pointer, to save a copyin. 720 */ 721 /* ARGSUSED */ 722 int 723 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval) 724 { 725 struct sys___sigsuspend14_args /* { 726 syscallarg(const sigset_t *) set; 727 } */ *uap = v; 728 struct proc *p; 729 sigset_t ss; 730 int error; 731 732 if (SCARG(uap, set)) { 733 error = copyin(SCARG(uap, set), &ss, sizeof(ss)); 734 if (error) 735 return (error); 736 } 737 738 p = l->l_proc; 739 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0)); 740 } 741 742 int 743 sigaltstack1(struct proc *p, const struct sigaltstack *nss, 744 struct sigaltstack *oss) 745 { 746 747 if (oss) 748 *oss = p->p_sigctx.ps_sigstk; 749 750 if (nss) { 751 if (nss->ss_flags & ~SS_ALLBITS) 752 return (EINVAL); 753 754 if (nss->ss_flags & SS_DISABLE) { 755 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) 756 return (EINVAL); 757 } else { 758 if (nss->ss_size < MINSIGSTKSZ) 759 return (ENOMEM); 760 } 761 p->p_sigctx.ps_sigstk = *nss; 762 } 763 764 return (0); 765 } 766 767 /* ARGSUSED */ 768 int 769 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval) 770 { 771 struct sys___sigaltstack14_args /* { 772 syscallarg(const struct sigaltstack *) nss; 773 syscallarg(struct sigaltstack *) oss; 774 } */ *uap = v; 775 struct proc *p; 776 struct sigaltstack nss, oss; 777 int error; 778 779 if (SCARG(uap, nss)) { 780 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 781 if (error) 782 return (error); 783 } 784 p = l->l_proc; 785 error = sigaltstack1(p, 786 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0); 787 if (error) 788 return (error); 789 if (SCARG(uap, oss)) { 790 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 791 if (error) 792 return (error); 793 } 794 return (0); 795 } 796 797 /* ARGSUSED */ 798 int 799 sys_kill(struct lwp *l, void *v, register_t *retval) 800 { 801 struct sys_kill_args /* { 802 syscallarg(int) pid; 803 syscallarg(int) signum; 804 } */ *uap = v; 805 struct proc *p; 806 ksiginfo_t ksi; 807 int signum = SCARG(uap, signum); 808 int error; 809 810 if ((u_int)signum >= NSIG) 811 return (EINVAL); 812 KSI_INIT(&ksi); 813 ksi.ksi_signo = signum; 814 ksi.ksi_code = SI_USER; 815 ksi.ksi_pid = l->l_proc->p_pid; 816 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 817 if (SCARG(uap, pid) > 0) { 818 /* kill single process */ 819 if ((p = pfind(SCARG(uap, pid))) == NULL) 820 return (ESRCH); 821 error = kauth_authorize_process(l->l_cred, 822 KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum, 823 NULL, NULL); 824 if (error) 825 return error; 826 if (signum) 827 kpsignal2(p, &ksi); 828 return (0); 829 } 830 switch (SCARG(uap, pid)) { 831 case -1: /* broadcast signal */ 832 return (killpg1(l, &ksi, 0, 1)); 833 case 0: /* signal own process group */ 834 return (killpg1(l, &ksi, 0, 0)); 835 default: /* negative explicit process group */ 836 return (killpg1(l, &ksi, -SCARG(uap, pid), 0)); 837 } 838 /* NOTREACHED */ 839 } 840 841 /* 842 * Common code for kill process group/broadcast kill. 843 * cp is calling process. 844 */ 845 int 846 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all) 847 { 848 struct proc *p, *cp; 849 kauth_cred_t pc; 850 struct pgrp *pgrp; 851 int nfound; 852 int signum = ksi->ksi_signo; 853 854 cp = l->l_proc; 855 pc = l->l_cred; 856 nfound = 0; 857 if (all) { 858 /* 859 * broadcast 860 */ 861 proclist_lock_read(); 862 PROCLIST_FOREACH(p, &allproc) { 863 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp || 864 kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL, 865 p, (void *)(uintptr_t)signum, NULL, NULL) != 0) 866 continue; 867 nfound++; 868 if (signum) 869 kpsignal2(p, ksi); 870 } 871 proclist_unlock_read(); 872 } else { 873 if (pgid == 0) 874 /* 875 * zero pgid means send to my process group. 876 */ 877 pgrp = cp->p_pgrp; 878 else { 879 pgrp = pgfind(pgid); 880 if (pgrp == NULL) 881 return (ESRCH); 882 } 883 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 884 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 885 kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL, 886 p, (void *)(uintptr_t)signum, NULL, NULL) != 0) 887 continue; 888 nfound++; 889 if (signum && P_ZOMBIE(p) == 0) 890 kpsignal2(p, ksi); 891 } 892 } 893 return (nfound ? 0 : ESRCH); 894 } 895 896 /* 897 * Send a signal to a process group. 898 */ 899 void 900 gsignal(int pgid, int signum) 901 { 902 ksiginfo_t ksi; 903 KSI_INIT_EMPTY(&ksi); 904 ksi.ksi_signo = signum; 905 kgsignal(pgid, &ksi, NULL); 906 } 907 908 void 909 kgsignal(int pgid, ksiginfo_t *ksi, void *data) 910 { 911 struct pgrp *pgrp; 912 913 if (pgid && (pgrp = pgfind(pgid))) 914 kpgsignal(pgrp, ksi, data, 0); 915 } 916 917 /* 918 * Send a signal to a process group. If checktty is 1, 919 * limit to members which have a controlling terminal. 920 */ 921 void 922 pgsignal(struct pgrp *pgrp, int sig, int checkctty) 923 { 924 ksiginfo_t ksi; 925 KSI_INIT_EMPTY(&ksi); 926 ksi.ksi_signo = sig; 927 kpgsignal(pgrp, &ksi, NULL, checkctty); 928 } 929 930 void 931 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty) 932 { 933 struct proc *p; 934 935 if (pgrp) 936 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 937 if (checkctty == 0 || p->p_flag & P_CONTROLT) 938 kpsignal(p, ksi, data); 939 } 940 941 /* 942 * Send a signal caused by a trap to the current process. 943 * If it will be caught immediately, deliver it with correct code. 944 * Otherwise, post it normally. 945 */ 946 void 947 trapsignal(struct lwp *l, const ksiginfo_t *ksi) 948 { 949 struct proc *p; 950 struct sigacts *ps; 951 int signum = ksi->ksi_signo; 952 953 KASSERT(KSI_TRAP_P(ksi)); 954 955 p = l->l_proc; 956 ps = p->p_sigacts; 957 if ((p->p_flag & P_TRACED) == 0 && 958 sigismember(&p->p_sigctx.ps_sigcatch, signum) && 959 !sigismember(&p->p_sigctx.ps_sigmask, signum)) { 960 p->p_stats->p_ru.ru_nsignals++; 961 #ifdef KTRACE 962 if (KTRPOINT(p, KTR_PSIG)) 963 ktrpsig(l, signum, SIGACTION_PS(ps, signum).sa_handler, 964 &p->p_sigctx.ps_sigmask, ksi); 965 #endif 966 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask); 967 (void) splsched(); /* XXXSMP */ 968 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 969 &p->p_sigctx.ps_sigmask); 970 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 971 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 972 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 973 sigaddset(&p->p_sigctx.ps_sigignore, signum); 974 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 975 } 976 (void) spl0(); /* XXXSMP */ 977 } else { 978 p->p_sigctx.ps_lwp = l->l_lid; 979 /* XXX for core dump/debugger */ 980 p->p_sigctx.ps_signo = ksi->ksi_signo; 981 p->p_sigctx.ps_code = ksi->ksi_trap; 982 kpsignal2(p, ksi); 983 } 984 } 985 986 /* 987 * Fill in signal information and signal the parent for a child status change. 988 */ 989 void 990 child_psignal(struct proc *p) 991 { 992 ksiginfo_t ksi; 993 994 KSI_INIT(&ksi); 995 ksi.ksi_signo = SIGCHLD; 996 ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED; 997 ksi.ksi_pid = p->p_pid; 998 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred); 999 ksi.ksi_status = p->p_xstat; 1000 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 1001 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 1002 kpsignal2(p->p_pptr, &ksi); 1003 } 1004 1005 /* 1006 * Send the signal to the process. If the signal has an action, the action 1007 * is usually performed by the target process rather than the caller; we add 1008 * the signal to the set of pending signals for the process. 1009 * 1010 * Exceptions: 1011 * o When a stop signal is sent to a sleeping process that takes the 1012 * default action, the process is stopped without awakening it. 1013 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1014 * regardless of the signal action (eg, blocked or ignored). 1015 * 1016 * Other ignored signals are discarded immediately. 1017 */ 1018 void 1019 psignal(struct proc *p, int signum) 1020 { 1021 ksiginfo_t ksi; 1022 1023 KSI_INIT_EMPTY(&ksi); 1024 ksi.ksi_signo = signum; 1025 kpsignal2(p, &ksi); 1026 } 1027 1028 void 1029 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data) 1030 { 1031 1032 if ((p->p_flag & P_WEXIT) == 0 && data) { 1033 size_t fd; 1034 struct filedesc *fdp = p->p_fd; 1035 1036 ksi->ksi_fd = -1; 1037 for (fd = 0; fd < fdp->fd_nfiles; fd++) { 1038 struct file *fp = fdp->fd_ofiles[fd]; 1039 /* XXX: lock? */ 1040 if (fp && fp->f_data == data) { 1041 ksi->ksi_fd = fd; 1042 break; 1043 } 1044 } 1045 } 1046 kpsignal2(p, ksi); 1047 } 1048 1049 static void 1050 kpsignal2(struct proc *p, const ksiginfo_t *ksi) 1051 { 1052 struct lwp *l, *suspended = NULL; 1053 struct sadata_vp *vp; 1054 ksiginfo_t *newkp; 1055 int s = 0, prop, allsusp; 1056 sig_t action; 1057 int signum = ksi->ksi_signo; 1058 1059 #ifdef DIAGNOSTIC 1060 if (signum <= 0 || signum >= NSIG) 1061 panic("psignal signal number %d", signum); 1062 1063 SCHED_ASSERT_UNLOCKED(); 1064 #endif 1065 1066 /* 1067 * Notify any interested parties in the signal. 1068 */ 1069 KNOTE(&p->p_klist, NOTE_SIGNAL | signum); 1070 1071 prop = sigprop[signum]; 1072 1073 /* 1074 * If proc is traced, always give parent a chance. 1075 */ 1076 if (p->p_flag & P_TRACED) { 1077 action = SIG_DFL; 1078 1079 /* 1080 * If the process is being traced and the signal is being 1081 * caught, make sure to save any ksiginfo. 1082 */ 1083 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 1084 SCHED_ASSERT_UNLOCKED(); 1085 ksiginfo_queue(p, ksi, NULL); 1086 } 1087 } else { 1088 /* 1089 * If the signal was the result of a trap, reset it 1090 * to default action if it's currently masked, so that it would 1091 * coredump immediatelly instead of spinning repeatedly 1092 * taking the signal. 1093 */ 1094 if (KSI_TRAP_P(ksi) 1095 && sigismember(&p->p_sigctx.ps_sigmask, signum) 1096 && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) { 1097 sigdelset(&p->p_sigctx.ps_sigignore, signum); 1098 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1099 sigdelset(&p->p_sigctx.ps_sigmask, signum); 1100 SIGACTION(p, signum).sa_handler = SIG_DFL; 1101 } 1102 1103 /* 1104 * If the signal is being ignored, 1105 * then we forget about it immediately. 1106 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore, 1107 * and if it is set to SIG_IGN, 1108 * action will be SIG_DFL here.) 1109 */ 1110 if (sigismember(&p->p_sigctx.ps_sigignore, signum)) 1111 return; 1112 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1113 action = SIG_HOLD; 1114 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) 1115 action = SIG_CATCH; 1116 else { 1117 action = SIG_DFL; 1118 1119 if (prop & SA_KILL && p->p_nice > NZERO) 1120 p->p_nice = NZERO; 1121 1122 /* 1123 * If sending a tty stop signal to a member of an 1124 * orphaned process group, discard the signal here if 1125 * the action is default; don't stop the process below 1126 * if sleeping, and don't clear any pending SIGCONT. 1127 */ 1128 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0) 1129 return; 1130 } 1131 } 1132 1133 if (prop & SA_CONT) 1134 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist); 1135 1136 if (prop & SA_STOP) 1137 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist); 1138 1139 /* 1140 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL, 1141 * please!), check if anything waits on it. If yes, save the 1142 * info into provided ps_sigwaited, and wake-up the waiter. 1143 * The signal won't be processed further here. 1144 */ 1145 if ((prop & SA_CANTMASK) == 0 1146 && p->p_sigctx.ps_sigwaited 1147 && sigismember(p->p_sigctx.ps_sigwait, signum) 1148 && p->p_stat != SSTOP) { 1149 p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info; 1150 p->p_sigctx.ps_sigwaited = NULL; 1151 wakeup_one(&p->p_sigctx.ps_sigwait); 1152 return; 1153 } 1154 1155 sigaddset(&p->p_sigctx.ps_siglist, signum); 1156 1157 /* CHECKSIGS() is "inlined" here. */ 1158 p->p_sigctx.ps_sigcheck = 1; 1159 1160 /* 1161 * Defer further processing for signals which are held, 1162 * except that stopped processes must be continued by SIGCONT. 1163 */ 1164 if (action == SIG_HOLD && 1165 ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) { 1166 SCHED_ASSERT_UNLOCKED(); 1167 ksiginfo_queue(p, ksi, NULL); 1168 return; 1169 } 1170 1171 /* 1172 * Allocate a ksiginfo_t incase we need to insert it with the 1173 * scheduler lock held, but only if this ksiginfo_t isn't empty. 1174 */ 1175 if (!KSI_EMPTY_P(ksi)) { 1176 newkp = ksiginfo_alloc(PR_NOWAIT); 1177 if (newkp == NULL) { 1178 #ifdef DIAGNOSTIC 1179 printf("kpsignal2: couldn't allocated ksiginfo\n"); 1180 #endif 1181 return; 1182 } 1183 } else 1184 newkp = NULL; 1185 1186 SCHED_LOCK(s); 1187 1188 if (p->p_flag & P_SA) { 1189 allsusp = 0; 1190 l = NULL; 1191 if (p->p_stat == SACTIVE) { 1192 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1193 l = vp->savp_lwp; 1194 KDASSERT(l != NULL); 1195 if (l->l_flag & L_SA_IDLE) { 1196 /* wakeup idle LWP */ 1197 goto found; 1198 /*NOTREACHED*/ 1199 } else if (l->l_flag & L_SA_YIELD) { 1200 /* idle LWP is already waking up */ 1201 goto out; 1202 /*NOTREACHED*/ 1203 } 1204 } 1205 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1206 l = vp->savp_lwp; 1207 if (l->l_stat == LSRUN || 1208 l->l_stat == LSONPROC) { 1209 signotify(p); 1210 goto out; 1211 /*NOTREACHED*/ 1212 } 1213 if (l->l_stat == LSSLEEP && 1214 l->l_flag & L_SINTR) { 1215 /* ok to signal vp lwp */ 1216 break; 1217 } else 1218 l = NULL; 1219 } 1220 } else if (p->p_stat == SSTOP) { 1221 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1222 l = vp->savp_lwp; 1223 if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0) 1224 break; 1225 l = NULL; 1226 } 1227 } 1228 } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) { 1229 /* 1230 * At least one LWP is running or on a run queue. 1231 * The signal will be noticed when one of them returns 1232 * to userspace. 1233 */ 1234 signotify(p); 1235 /* 1236 * The signal will be noticed very soon. 1237 */ 1238 goto out; 1239 /*NOTREACHED*/ 1240 } else { 1241 /* 1242 * Find out if any of the sleeps are interruptable, 1243 * and if all the live LWPs remaining are suspended. 1244 */ 1245 allsusp = 1; 1246 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1247 if (l->l_stat == LSSLEEP && 1248 l->l_flag & L_SINTR) 1249 break; 1250 if (l->l_stat == LSSUSPENDED) 1251 suspended = l; 1252 else if ((l->l_stat != LSZOMB) && 1253 (l->l_stat != LSDEAD)) 1254 allsusp = 0; 1255 } 1256 } 1257 1258 found: 1259 switch (p->p_stat) { 1260 case SACTIVE: 1261 1262 if (l != NULL && (p->p_flag & P_TRACED)) 1263 goto run; 1264 1265 /* 1266 * If SIGCONT is default (or ignored) and process is 1267 * asleep, we are finished; the process should not 1268 * be awakened. 1269 */ 1270 if ((prop & SA_CONT) && action == SIG_DFL) { 1271 sigdelset(&p->p_sigctx.ps_siglist, signum); 1272 goto done; 1273 } 1274 1275 /* 1276 * When a sleeping process receives a stop 1277 * signal, process immediately if possible. 1278 */ 1279 if ((prop & SA_STOP) && action == SIG_DFL) { 1280 /* 1281 * If a child holding parent blocked, 1282 * stopping could cause deadlock. 1283 */ 1284 if (p->p_flag & P_PPWAIT) { 1285 goto out; 1286 } 1287 sigdelset(&p->p_sigctx.ps_siglist, signum); 1288 p->p_xstat = signum; 1289 proc_stop(p, 1); /* XXXSMP: recurse? */ 1290 SCHED_UNLOCK(s); 1291 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) { 1292 child_psignal(p); 1293 } 1294 goto done_unlocked; 1295 } 1296 1297 if (l == NULL) { 1298 /* 1299 * Special case: SIGKILL of a process 1300 * which is entirely composed of 1301 * suspended LWPs should succeed. We 1302 * make this happen by unsuspending one of 1303 * them. 1304 */ 1305 if (allsusp && (signum == SIGKILL)) { 1306 lwp_continue(suspended); 1307 } 1308 goto done; 1309 } 1310 /* 1311 * All other (caught or default) signals 1312 * cause the process to run. 1313 */ 1314 goto runfast; 1315 /*NOTREACHED*/ 1316 case SSTOP: 1317 /* Process is stopped */ 1318 /* 1319 * If traced process is already stopped, 1320 * then no further action is necessary. 1321 */ 1322 if (p->p_flag & P_TRACED) 1323 goto done; 1324 1325 /* 1326 * Kill signal always sets processes running, 1327 * if possible. 1328 */ 1329 if (signum == SIGKILL) { 1330 l = proc_unstop(p); 1331 if (l) 1332 goto runfast; 1333 goto done; 1334 } 1335 1336 if (prop & SA_CONT) { 1337 /* 1338 * If SIGCONT is default (or ignored), 1339 * we continue the process but don't 1340 * leave the signal in ps_siglist, as 1341 * it has no further action. If 1342 * SIGCONT is held, we continue the 1343 * process and leave the signal in 1344 * ps_siglist. If the process catches 1345 * SIGCONT, let it handle the signal 1346 * itself. If it isn't waiting on an 1347 * event, then it goes back to run 1348 * state. Otherwise, process goes 1349 * back to sleep state. 1350 */ 1351 if (action == SIG_DFL) 1352 sigdelset(&p->p_sigctx.ps_siglist, 1353 signum); 1354 l = proc_unstop(p); 1355 if (l && (action == SIG_CATCH)) 1356 goto runfast; 1357 goto out; 1358 } 1359 1360 if (prop & SA_STOP) { 1361 /* 1362 * Already stopped, don't need to stop again. 1363 * (If we did the shell could get confused.) 1364 */ 1365 sigdelset(&p->p_sigctx.ps_siglist, signum); 1366 goto done; 1367 } 1368 1369 /* 1370 * If a lwp is sleeping interruptibly, then 1371 * wake it up; it will run until the kernel 1372 * boundary, where it will stop in issignal(), 1373 * since p->p_stat is still SSTOP. When the 1374 * process is continued, it will be made 1375 * runnable and can look at the signal. 1376 */ 1377 if (l) 1378 goto run; 1379 goto out; 1380 case SIDL: 1381 /* Process is being created by fork */ 1382 /* XXX: We are not ready to receive signals yet */ 1383 goto done; 1384 default: 1385 /* Else what? */ 1386 panic("psignal: Invalid process state %d.", p->p_stat); 1387 } 1388 /*NOTREACHED*/ 1389 1390 runfast: 1391 if (action == SIG_CATCH) { 1392 ksiginfo_queue(p, ksi, &newkp); 1393 action = SIG_HOLD; 1394 } 1395 /* 1396 * Raise priority to at least PUSER. 1397 */ 1398 if (l->l_priority > PUSER) 1399 l->l_priority = PUSER; 1400 run: 1401 if (action == SIG_CATCH) { 1402 ksiginfo_queue(p, ksi, &newkp); 1403 action = SIG_HOLD; 1404 } 1405 1406 setrunnable(l); /* XXXSMP: recurse? */ 1407 out: 1408 if (action == SIG_CATCH) 1409 ksiginfo_queue(p, ksi, &newkp); 1410 done: 1411 SCHED_UNLOCK(s); 1412 1413 done_unlocked: 1414 if (newkp) 1415 ksiginfo_free(newkp); 1416 } 1417 1418 siginfo_t * 1419 siginfo_alloc(int flags) 1420 { 1421 1422 return pool_get(&siginfo_pool, flags); 1423 } 1424 1425 void 1426 siginfo_free(void *arg) 1427 { 1428 1429 pool_put(&siginfo_pool, arg); 1430 } 1431 1432 void 1433 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask) 1434 { 1435 struct proc *p = l->l_proc; 1436 struct lwp *le, *li; 1437 siginfo_t *si; 1438 int f; 1439 1440 if (p->p_flag & P_SA) { 1441 1442 /* XXXUPSXXX What if not on sa_vp ? */ 1443 1444 f = l->l_flag & L_SA; 1445 l->l_flag &= ~L_SA; 1446 si = siginfo_alloc(PR_WAITOK); 1447 si->_info = ksi->ksi_info; 1448 le = li = NULL; 1449 if (KSI_TRAP_P(ksi)) 1450 le = l; 1451 else 1452 li = l; 1453 if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 1454 sizeof(*si), si, siginfo_free) != 0) { 1455 siginfo_free(si); 1456 #if 0 1457 if (KSI_TRAP_P(ksi)) 1458 /* XXX What do we do here?? */; 1459 #endif 1460 } 1461 l->l_flag |= f; 1462 return; 1463 } 1464 1465 (*p->p_emul->e_sendsig)(ksi, mask); 1466 } 1467 1468 static inline int firstsig(const sigset_t *); 1469 1470 static inline int 1471 firstsig(const sigset_t *ss) 1472 { 1473 int sig; 1474 1475 sig = ffs(ss->__bits[0]); 1476 if (sig != 0) 1477 return (sig); 1478 #if NSIG > 33 1479 sig = ffs(ss->__bits[1]); 1480 if (sig != 0) 1481 return (sig + 32); 1482 #endif 1483 #if NSIG > 65 1484 sig = ffs(ss->__bits[2]); 1485 if (sig != 0) 1486 return (sig + 64); 1487 #endif 1488 #if NSIG > 97 1489 sig = ffs(ss->__bits[3]); 1490 if (sig != 0) 1491 return (sig + 96); 1492 #endif 1493 return (0); 1494 } 1495 1496 /* 1497 * If the current process has received a signal (should be caught or cause 1498 * termination, should interrupt current syscall), return the signal number. 1499 * Stop signals with default action are processed immediately, then cleared; 1500 * they aren't returned. This is checked after each entry to the system for 1501 * a syscall or trap (though this can usually be done without calling issignal 1502 * by checking the pending signal masks in the CURSIG macro.) The normal call 1503 * sequence is 1504 * 1505 * while (signum = CURSIG(curlwp)) 1506 * postsig(signum); 1507 */ 1508 int 1509 issignal(struct lwp *l) 1510 { 1511 struct proc *p = l->l_proc; 1512 int s, signum, prop; 1513 sigset_t ss; 1514 1515 /* Bail out if we do not own the virtual processor */ 1516 if (l->l_flag & L_SA && l->l_savp->savp_lwp != l) 1517 return 0; 1518 1519 KERNEL_PROC_LOCK(l); 1520 1521 if (p->p_stat == SSTOP) { 1522 /* 1523 * The process is stopped/stopping. Stop ourselves now that 1524 * we're on the kernel/userspace boundary. 1525 */ 1526 SCHED_LOCK(s); 1527 l->l_stat = LSSTOP; 1528 p->p_nrlwps--; 1529 if (p->p_flag & P_TRACED) 1530 goto sigtraceswitch; 1531 else 1532 goto sigswitch; 1533 } 1534 for (;;) { 1535 sigpending1(p, &ss); 1536 if (p->p_flag & P_PPWAIT) 1537 sigminusset(&stopsigmask, &ss); 1538 signum = firstsig(&ss); 1539 if (signum == 0) { /* no signal to send */ 1540 p->p_sigctx.ps_sigcheck = 0; 1541 KERNEL_PROC_UNLOCK(l); 1542 return (0); 1543 } 1544 /* take the signal! */ 1545 sigdelset(&p->p_sigctx.ps_siglist, signum); 1546 1547 /* 1548 * We should see pending but ignored signals 1549 * only if P_TRACED was on when they were posted. 1550 */ 1551 if (sigismember(&p->p_sigctx.ps_sigignore, signum) && 1552 (p->p_flag & P_TRACED) == 0) 1553 continue; 1554 1555 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1556 /* 1557 * If traced, always stop, and stay 1558 * stopped until released by the debugger. 1559 */ 1560 p->p_xstat = signum; 1561 1562 /* Emulation-specific handling of signal trace */ 1563 if ((p->p_emul->e_tracesig != NULL) && 1564 ((*p->p_emul->e_tracesig)(p, signum) != 0)) 1565 goto childresumed; 1566 1567 if ((p->p_flag & P_FSTRACE) == 0) 1568 child_psignal(p); 1569 SCHED_LOCK(s); 1570 proc_stop(p, 1); 1571 sigtraceswitch: 1572 mi_switch(l, NULL); 1573 SCHED_ASSERT_UNLOCKED(); 1574 splx(s); 1575 1576 childresumed: 1577 /* 1578 * If we are no longer being traced, or the parent 1579 * didn't give us a signal, look for more signals. 1580 */ 1581 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0) 1582 continue; 1583 1584 /* 1585 * If the new signal is being masked, look for other 1586 * signals. 1587 */ 1588 signum = p->p_xstat; 1589 p->p_xstat = 0; 1590 /* 1591 * `p->p_sigctx.ps_siglist |= mask' is done 1592 * in setrunnable(). 1593 */ 1594 if (sigismember(&p->p_sigctx.ps_sigmask, signum)) 1595 continue; 1596 /* take the signal! */ 1597 sigdelset(&p->p_sigctx.ps_siglist, signum); 1598 } 1599 1600 prop = sigprop[signum]; 1601 1602 /* 1603 * Decide whether the signal should be returned. 1604 * Return the signal's number, or fall through 1605 * to clear it from the pending mask. 1606 */ 1607 switch ((long)SIGACTION(p, signum).sa_handler) { 1608 1609 case (long)SIG_DFL: 1610 /* 1611 * Don't take default actions on system processes. 1612 */ 1613 if (p->p_pid <= 1) { 1614 #ifdef DIAGNOSTIC 1615 /* 1616 * Are you sure you want to ignore SIGSEGV 1617 * in init? XXX 1618 */ 1619 printf("Process (pid %d) got signal %d\n", 1620 p->p_pid, signum); 1621 #endif 1622 break; /* == ignore */ 1623 } 1624 /* 1625 * If there is a pending stop signal to process 1626 * with default action, stop here, 1627 * then clear the signal. However, 1628 * if process is member of an orphaned 1629 * process group, ignore tty stop signals. 1630 */ 1631 if (prop & SA_STOP) { 1632 if (p->p_flag & P_TRACED || 1633 (p->p_pgrp->pg_jobc == 0 && 1634 prop & SA_TTYSTOP)) 1635 break; /* == ignore */ 1636 p->p_xstat = signum; 1637 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) 1638 child_psignal(p); 1639 SCHED_LOCK(s); 1640 proc_stop(p, 1); 1641 sigswitch: 1642 mi_switch(l, NULL); 1643 SCHED_ASSERT_UNLOCKED(); 1644 splx(s); 1645 break; 1646 } else if (prop & SA_IGNORE) { 1647 /* 1648 * Except for SIGCONT, shouldn't get here. 1649 * Default action is to ignore; drop it. 1650 */ 1651 break; /* == ignore */ 1652 } else 1653 goto keep; 1654 /*NOTREACHED*/ 1655 1656 case (long)SIG_IGN: 1657 /* 1658 * Masking above should prevent us ever trying 1659 * to take action on an ignored signal other 1660 * than SIGCONT, unless process is traced. 1661 */ 1662 #ifdef DEBUG_ISSIGNAL 1663 if ((prop & SA_CONT) == 0 && 1664 (p->p_flag & P_TRACED) == 0) 1665 printf("issignal\n"); 1666 #endif 1667 break; /* == ignore */ 1668 1669 default: 1670 /* 1671 * This signal has an action, let 1672 * postsig() process it. 1673 */ 1674 goto keep; 1675 } 1676 } 1677 /* NOTREACHED */ 1678 1679 keep: 1680 /* leave the signal for later */ 1681 sigaddset(&p->p_sigctx.ps_siglist, signum); 1682 CHECKSIGS(p); 1683 KERNEL_PROC_UNLOCK(l); 1684 return (signum); 1685 } 1686 1687 /* 1688 * Put the argument process into the stopped state and notify the parent 1689 * via wakeup. Signals are handled elsewhere. The process must not be 1690 * on the run queue. 1691 */ 1692 void 1693 proc_stop(struct proc *p, int dowakeup) 1694 { 1695 struct lwp *l; 1696 struct proc *parent; 1697 struct sadata_vp *vp; 1698 1699 SCHED_ASSERT_LOCKED(); 1700 1701 /* XXX lock process LWP state */ 1702 p->p_flag &= ~P_WAITED; 1703 p->p_stat = SSTOP; 1704 parent = p->p_pptr; 1705 parent->p_nstopchild++; 1706 1707 if (p->p_flag & P_SA) { 1708 /* 1709 * Only (try to) put the LWP on the VP in stopped 1710 * state. 1711 * All other LWPs will suspend in sa_setwoken() 1712 * because the VP-LWP in stopped state cannot be 1713 * repossessed. 1714 */ 1715 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1716 l = vp->savp_lwp; 1717 if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) { 1718 l->l_stat = LSSTOP; 1719 p->p_nrlwps--; 1720 } else if (l->l_stat == LSRUN) { 1721 /* Remove LWP from the run queue */ 1722 remrunqueue(l); 1723 l->l_stat = LSSTOP; 1724 p->p_nrlwps--; 1725 } else if (l->l_stat == LSSLEEP && 1726 l->l_flag & L_SA_IDLE) { 1727 l->l_flag &= ~L_SA_IDLE; 1728 l->l_stat = LSSTOP; 1729 } 1730 } 1731 goto out; 1732 } 1733 1734 /* 1735 * Put as many LWP's as possible in stopped state. 1736 * Sleeping ones will notice the stopped state as they try to 1737 * return to userspace. 1738 */ 1739 1740 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1741 if (l->l_stat == LSONPROC) { 1742 /* XXX SMP this assumes that a LWP that is LSONPROC 1743 * is curlwp and hence is about to be mi_switched 1744 * away; the only callers of proc_stop() are: 1745 * - psignal 1746 * - issignal() 1747 * For the former, proc_stop() is only called when 1748 * no processes are running, so we don't worry. 1749 * For the latter, proc_stop() is called right 1750 * before mi_switch(). 1751 */ 1752 l->l_stat = LSSTOP; 1753 p->p_nrlwps--; 1754 } else if (l->l_stat == LSRUN) { 1755 /* Remove LWP from the run queue */ 1756 remrunqueue(l); 1757 l->l_stat = LSSTOP; 1758 p->p_nrlwps--; 1759 } else if ((l->l_stat == LSSLEEP) || 1760 (l->l_stat == LSSUSPENDED) || 1761 (l->l_stat == LSZOMB) || 1762 (l->l_stat == LSDEAD)) { 1763 /* 1764 * Don't do anything; let sleeping LWPs 1765 * discover the stopped state of the process 1766 * on their way out of the kernel; otherwise, 1767 * things like NFS threads that sleep with 1768 * locks will block the rest of the system 1769 * from getting any work done. 1770 * 1771 * Suspended/dead/zombie LWPs aren't going 1772 * anywhere, so we don't need to touch them. 1773 */ 1774 } 1775 #ifdef DIAGNOSTIC 1776 else { 1777 panic("proc_stop: process %d lwp %d " 1778 "in unstoppable state %d.\n", 1779 p->p_pid, l->l_lid, l->l_stat); 1780 } 1781 #endif 1782 } 1783 1784 out: 1785 /* XXX unlock process LWP state */ 1786 1787 if (dowakeup) 1788 sched_wakeup((caddr_t)p->p_pptr); 1789 } 1790 1791 /* 1792 * Given a process in state SSTOP, set the state back to SACTIVE and 1793 * move LSSTOP'd LWPs to LSSLEEP or make them runnable. 1794 * 1795 * If no LWPs ended up runnable (and therefore able to take a signal), 1796 * return a LWP that is sleeping interruptably. The caller can wake 1797 * that LWP up to take a signal. 1798 */ 1799 struct lwp * 1800 proc_unstop(struct proc *p) 1801 { 1802 struct lwp *l, *lr = NULL; 1803 struct sadata_vp *vp; 1804 int cantake = 0; 1805 1806 SCHED_ASSERT_LOCKED(); 1807 1808 /* 1809 * Our caller wants to be informed if there are only sleeping 1810 * and interruptable LWPs left after we have run so that it 1811 * can invoke setrunnable() if required - return one of the 1812 * interruptable LWPs if this is the case. 1813 */ 1814 1815 if (!(p->p_flag & P_WAITED)) 1816 p->p_pptr->p_nstopchild--; 1817 p->p_stat = SACTIVE; 1818 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 1819 if (l->l_stat == LSRUN) { 1820 lr = NULL; 1821 cantake = 1; 1822 } 1823 if (l->l_stat != LSSTOP) 1824 continue; 1825 1826 if (l->l_wchan != NULL) { 1827 l->l_stat = LSSLEEP; 1828 if ((cantake == 0) && (l->l_flag & L_SINTR)) { 1829 lr = l; 1830 cantake = 1; 1831 } 1832 } else { 1833 setrunnable(l); 1834 lr = NULL; 1835 cantake = 1; 1836 } 1837 } 1838 if (p->p_flag & P_SA) { 1839 /* Only consider returning the LWP on the VP. */ 1840 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 1841 lr = vp->savp_lwp; 1842 if (lr->l_stat == LSSLEEP) { 1843 if (lr->l_flag & L_SA_YIELD) { 1844 setrunnable(lr); 1845 break; 1846 } else if (lr->l_flag & L_SINTR) 1847 return lr; 1848 } 1849 } 1850 return NULL; 1851 } 1852 return lr; 1853 } 1854 1855 /* 1856 * Take the action for the specified signal 1857 * from the current set of pending signals. 1858 */ 1859 void 1860 postsig(int signum) 1861 { 1862 struct lwp *l; 1863 struct proc *p; 1864 struct sigacts *ps; 1865 sig_t action; 1866 sigset_t *returnmask; 1867 1868 l = curlwp; 1869 p = l->l_proc; 1870 ps = p->p_sigacts; 1871 #ifdef DIAGNOSTIC 1872 if (signum == 0) 1873 panic("postsig"); 1874 #endif 1875 1876 KERNEL_PROC_LOCK(l); 1877 1878 #ifdef MULTIPROCESSOR 1879 /* 1880 * On MP, issignal() can return the same signal to multiple 1881 * LWPs. The LWPs will block above waiting for the kernel 1882 * lock and the first LWP which gets through will then remove 1883 * the signal from ps_siglist. All other LWPs exit here. 1884 */ 1885 if (!sigismember(&p->p_sigctx.ps_siglist, signum)) { 1886 KERNEL_PROC_UNLOCK(l); 1887 return; 1888 } 1889 #endif 1890 sigdelset(&p->p_sigctx.ps_siglist, signum); 1891 action = SIGACTION_PS(ps, signum).sa_handler; 1892 if (action == SIG_DFL) { 1893 #ifdef KTRACE 1894 if (KTRPOINT(p, KTR_PSIG)) 1895 ktrpsig(l, signum, action, 1896 p->p_sigctx.ps_flags & SAS_OLDMASK ? 1897 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 1898 NULL); 1899 #endif 1900 /* 1901 * Default action, where the default is to kill 1902 * the process. (Other cases were ignored above.) 1903 */ 1904 sigexit(l, signum); 1905 /* NOTREACHED */ 1906 } else { 1907 ksiginfo_t *ksi; 1908 /* 1909 * If we get here, the signal must be caught. 1910 */ 1911 #ifdef DIAGNOSTIC 1912 if (action == SIG_IGN || 1913 sigismember(&p->p_sigctx.ps_sigmask, signum)) 1914 panic("postsig action"); 1915 #endif 1916 /* 1917 * Set the new mask value and also defer further 1918 * occurrences of this signal. 1919 * 1920 * Special case: user has done a sigpause. Here the 1921 * current mask is not of interest, but rather the 1922 * mask from before the sigpause is what we want 1923 * restored after the signal processing is completed. 1924 */ 1925 if (p->p_sigctx.ps_flags & SAS_OLDMASK) { 1926 returnmask = &p->p_sigctx.ps_oldmask; 1927 p->p_sigctx.ps_flags &= ~SAS_OLDMASK; 1928 } else 1929 returnmask = &p->p_sigctx.ps_sigmask; 1930 p->p_stats->p_ru.ru_nsignals++; 1931 ksi = ksiginfo_dequeue(p, signum); 1932 #ifdef KTRACE 1933 if (KTRPOINT(p, KTR_PSIG)) 1934 ktrpsig(l, signum, action, 1935 p->p_sigctx.ps_flags & SAS_OLDMASK ? 1936 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 1937 ksi); 1938 #endif 1939 if (ksi == NULL) { 1940 ksiginfo_t ksi1; 1941 /* 1942 * we did not save any siginfo for this, either 1943 * because the signal was not caught, or because the 1944 * user did not request SA_SIGINFO 1945 */ 1946 KSI_INIT_EMPTY(&ksi1); 1947 ksi1.ksi_signo = signum; 1948 kpsendsig(l, &ksi1, returnmask); 1949 } else { 1950 kpsendsig(l, ksi, returnmask); 1951 ksiginfo_free(ksi); 1952 } 1953 p->p_sigctx.ps_lwp = 0; 1954 p->p_sigctx.ps_code = 0; 1955 p->p_sigctx.ps_signo = 0; 1956 (void) splsched(); /* XXXSMP */ 1957 sigplusset(&SIGACTION_PS(ps, signum).sa_mask, 1958 &p->p_sigctx.ps_sigmask); 1959 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) { 1960 sigdelset(&p->p_sigctx.ps_sigcatch, signum); 1961 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 1962 sigaddset(&p->p_sigctx.ps_sigignore, signum); 1963 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL; 1964 } 1965 (void) spl0(); /* XXXSMP */ 1966 } 1967 1968 KERNEL_PROC_UNLOCK(l); 1969 } 1970 1971 /* 1972 * Kill the current process for stated reason. 1973 */ 1974 void 1975 killproc(struct proc *p, const char *why) 1976 { 1977 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why); 1978 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why); 1979 psignal(p, SIGKILL); 1980 } 1981 1982 /* 1983 * Force the current process to exit with the specified signal, dumping core 1984 * if appropriate. We bypass the normal tests for masked and caught signals, 1985 * allowing unrecoverable failures to terminate the process without changing 1986 * signal state. Mark the accounting record with the signal termination. 1987 * If dumping core, save the signal number for the debugger. Calls exit and 1988 * does not return. 1989 */ 1990 1991 #if defined(DEBUG) 1992 int kern_logsigexit = 1; /* not static to make public for sysctl */ 1993 #else 1994 int kern_logsigexit = 0; /* not static to make public for sysctl */ 1995 #endif 1996 1997 static const char logcoredump[] = 1998 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n"; 1999 static const char lognocoredump[] = 2000 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n"; 2001 2002 /* Wrapper function for use in p_userret */ 2003 static void 2004 lwp_coredump_hook(struct lwp *l, void *arg) 2005 { 2006 int s; 2007 2008 /* 2009 * Suspend ourselves, so that the kernel stack and therefore 2010 * the userland registers saved in the trapframe are around 2011 * for coredump() to write them out. 2012 */ 2013 KERNEL_PROC_LOCK(l); 2014 l->l_flag &= ~L_DETACHED; 2015 SCHED_LOCK(s); 2016 l->l_stat = LSSUSPENDED; 2017 l->l_proc->p_nrlwps--; 2018 /* XXX NJWLWP check if this makes sense here: */ 2019 l->l_proc->p_stats->p_ru.ru_nvcsw++; 2020 mi_switch(l, NULL); 2021 SCHED_ASSERT_UNLOCKED(); 2022 splx(s); 2023 2024 lwp_exit(l); 2025 } 2026 2027 void 2028 sigexit(struct lwp *l, int signum) 2029 { 2030 struct proc *p; 2031 #if 0 2032 struct lwp *l2; 2033 #endif 2034 int exitsig; 2035 #ifdef COREDUMP 2036 int error; 2037 #endif 2038 2039 p = l->l_proc; 2040 2041 /* 2042 * Don't permit coredump() or exit1() multiple times 2043 * in the same process. 2044 */ 2045 if (p->p_flag & P_WEXIT) { 2046 KERNEL_PROC_UNLOCK(l); 2047 (*p->p_userret)(l, p->p_userret_arg); 2048 } 2049 p->p_flag |= P_WEXIT; 2050 /* We don't want to switch away from exiting. */ 2051 /* XXX multiprocessor: stop LWPs on other processors. */ 2052 #if 0 2053 if (p->p_flag & P_SA) { 2054 LIST_FOREACH(l2, &p->p_lwps, l_sibling) 2055 l2->l_flag &= ~L_SA; 2056 p->p_flag &= ~P_SA; 2057 } 2058 #endif 2059 2060 /* Make other LWPs stick around long enough to be dumped */ 2061 p->p_userret = lwp_coredump_hook; 2062 p->p_userret_arg = NULL; 2063 2064 exitsig = signum; 2065 p->p_acflag |= AXSIG; 2066 if (sigprop[signum] & SA_CORE) { 2067 p->p_sigctx.ps_signo = signum; 2068 #ifdef COREDUMP 2069 if ((error = coredump(l, NULL)) == 0) 2070 exitsig |= WCOREFLAG; 2071 #endif 2072 2073 if (kern_logsigexit) { 2074 /* XXX What if we ever have really large UIDs? */ 2075 int uid = l->l_cred ? 2076 (int)kauth_cred_geteuid(l->l_cred) : -1; 2077 2078 #ifdef COREDUMP 2079 if (error) 2080 log(LOG_INFO, lognocoredump, p->p_pid, 2081 p->p_comm, uid, signum, error); 2082 else 2083 #endif 2084 log(LOG_INFO, logcoredump, p->p_pid, 2085 p->p_comm, uid, signum); 2086 } 2087 2088 } 2089 2090 exit1(l, W_EXITCODE(0, exitsig)); 2091 /* NOTREACHED */ 2092 } 2093 2094 #ifdef COREDUMP 2095 struct coredump_iostate { 2096 struct lwp *io_lwp; 2097 struct vnode *io_vp; 2098 kauth_cred_t io_cred; 2099 off_t io_offset; 2100 }; 2101 2102 int 2103 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 2104 { 2105 struct coredump_iostate *io = cookie; 2106 int error; 2107 2108 error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len, 2109 io->io_offset, segflg, 2110 IO_NODELOCKED|IO_UNIT, io->io_cred, NULL, 2111 segflg == UIO_USERSPACE ? io->io_lwp : NULL); 2112 if (error) { 2113 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n", 2114 io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm, 2115 segflg == UIO_USERSPACE ? "user" : "system", 2116 len, data, (long long) io->io_offset, error); 2117 return (error); 2118 } 2119 2120 io->io_offset += len; 2121 return (0); 2122 } 2123 2124 /* 2125 * Dump core, into a file named "progname.core" or "core" (depending on the 2126 * value of shortcorename), unless the process was setuid/setgid. 2127 */ 2128 int 2129 coredump(struct lwp *l, const char *pattern) 2130 { 2131 struct vnode *vp; 2132 struct proc *p; 2133 struct vmspace *vm; 2134 kauth_cred_t cred; 2135 struct nameidata nd; 2136 struct vattr vattr; 2137 struct mount *mp; 2138 struct coredump_iostate io; 2139 int error, error1; 2140 char *name = NULL; 2141 2142 p = l->l_proc; 2143 vm = p->p_vmspace; 2144 cred = l->l_cred; 2145 2146 /* 2147 * Make sure the process has not set-id, to prevent data leaks, 2148 * unless it was specifically requested to allow set-id coredumps. 2149 */ 2150 if ((p->p_flag & P_SUGID) && !security_setidcore_dump) 2151 return EPERM; 2152 2153 /* 2154 * Refuse to core if the data + stack + user size is larger than 2155 * the core dump limit. XXX THIS IS WRONG, because of mapped 2156 * data. 2157 */ 2158 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >= 2159 p->p_rlimit[RLIMIT_CORE].rlim_cur) 2160 return EFBIG; /* better error code? */ 2161 2162 restart: 2163 /* 2164 * The core dump will go in the current working directory. Make 2165 * sure that the directory is still there and that the mount flags 2166 * allow us to write core dumps there. 2167 */ 2168 vp = p->p_cwdi->cwdi_cdir; 2169 if (vp->v_mount == NULL || 2170 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) { 2171 error = EPERM; 2172 goto done; 2173 } 2174 2175 if ((p->p_flag & P_SUGID) && security_setidcore_dump) 2176 pattern = security_setidcore_path; 2177 2178 if (pattern == NULL) 2179 pattern = p->p_limit->pl_corename; 2180 if (name == NULL) { 2181 name = PNBUF_GET(); 2182 } 2183 if ((error = build_corename(p, name, pattern, MAXPATHLEN)) != 0) 2184 goto done; 2185 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l); 2186 if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, 2187 S_IRUSR | S_IWUSR)) != 0) 2188 goto done; 2189 vp = nd.ni_vp; 2190 2191 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2192 VOP_UNLOCK(vp, 0); 2193 if ((error = vn_close(vp, FWRITE, cred, l)) != 0) 2194 goto done; 2195 if ((error = vn_start_write(NULL, &mp, 2196 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 2197 goto done; 2198 goto restart; 2199 } 2200 2201 /* Don't dump to non-regular files or files with links. */ 2202 if (vp->v_type != VREG || 2203 VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) { 2204 error = EINVAL; 2205 goto out; 2206 } 2207 VATTR_NULL(&vattr); 2208 vattr.va_size = 0; 2209 2210 if ((p->p_flag & P_SUGID) && security_setidcore_dump) { 2211 vattr.va_uid = security_setidcore_owner; 2212 vattr.va_gid = security_setidcore_group; 2213 vattr.va_mode = security_setidcore_mode; 2214 } 2215 2216 VOP_LEASE(vp, l, cred, LEASE_WRITE); 2217 VOP_SETATTR(vp, &vattr, cred, l); 2218 p->p_acflag |= ACORE; 2219 2220 io.io_lwp = l; 2221 io.io_vp = vp; 2222 io.io_cred = cred; 2223 io.io_offset = 0; 2224 2225 /* Now dump the actual core file. */ 2226 error = (*p->p_execsw->es_coredump)(l, &io); 2227 out: 2228 VOP_UNLOCK(vp, 0); 2229 vn_finished_write(mp, 0); 2230 error1 = vn_close(vp, FWRITE, cred, l); 2231 if (error == 0) 2232 error = error1; 2233 done: 2234 if (name != NULL) 2235 PNBUF_PUT(name); 2236 return error; 2237 } 2238 #endif /* COREDUMP */ 2239 2240 /* 2241 * Nonexistent system call-- signal process (may want to handle it). 2242 * Flag error in case process won't see signal immediately (blocked or ignored). 2243 */ 2244 #ifndef PTRACE 2245 __weak_alias(sys_ptrace, sys_nosys); 2246 #endif 2247 2248 /* ARGSUSED */ 2249 int 2250 sys_nosys(struct lwp *l, void *v, register_t *retval) 2251 { 2252 struct proc *p; 2253 2254 p = l->l_proc; 2255 psignal(p, SIGSYS); 2256 return (ENOSYS); 2257 } 2258 2259 #ifdef COREDUMP 2260 static int 2261 build_corename(struct proc *p, char *dst, const char *src, size_t len) 2262 { 2263 const char *s; 2264 char *d, *end; 2265 int i; 2266 2267 for (s = src, d = dst, end = d + len; *s != '\0'; s++) { 2268 if (*s == '%') { 2269 switch (*(s + 1)) { 2270 case 'n': 2271 i = snprintf(d, end - d, "%s", p->p_comm); 2272 break; 2273 case 'p': 2274 i = snprintf(d, end - d, "%d", p->p_pid); 2275 break; 2276 case 'u': 2277 i = snprintf(d, end - d, "%.*s", 2278 (int)sizeof p->p_pgrp->pg_session->s_login, 2279 p->p_pgrp->pg_session->s_login); 2280 break; 2281 case 't': 2282 i = snprintf(d, end - d, "%ld", 2283 p->p_stats->p_start.tv_sec); 2284 break; 2285 default: 2286 goto copy; 2287 } 2288 d += i; 2289 s++; 2290 } else { 2291 copy: *d = *s; 2292 d++; 2293 } 2294 if (d >= end) 2295 return (ENAMETOOLONG); 2296 } 2297 *d = '\0'; 2298 return 0; 2299 } 2300 #endif /* COREDUMP */ 2301 2302 void 2303 getucontext(struct lwp *l, ucontext_t *ucp) 2304 { 2305 struct proc *p; 2306 2307 p = l->l_proc; 2308 2309 ucp->uc_flags = 0; 2310 ucp->uc_link = l->l_ctxlink; 2311 2312 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask); 2313 ucp->uc_flags |= _UC_SIGMASK; 2314 2315 /* 2316 * The (unsupplied) definition of the `current execution stack' 2317 * in the System V Interface Definition appears to allow returning 2318 * the main context stack. 2319 */ 2320 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) { 2321 ucp->uc_stack.ss_sp = (void *)USRSTACK; 2322 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize); 2323 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 2324 } else { 2325 /* Simply copy alternate signal execution stack. */ 2326 ucp->uc_stack = p->p_sigctx.ps_sigstk; 2327 } 2328 ucp->uc_flags |= _UC_STACK; 2329 2330 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags); 2331 } 2332 2333 /* ARGSUSED */ 2334 int 2335 sys_getcontext(struct lwp *l, void *v, register_t *retval) 2336 { 2337 struct sys_getcontext_args /* { 2338 syscallarg(struct __ucontext *) ucp; 2339 } */ *uap = v; 2340 ucontext_t uc; 2341 2342 getucontext(l, &uc); 2343 2344 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)))); 2345 } 2346 2347 int 2348 setucontext(struct lwp *l, const ucontext_t *ucp) 2349 { 2350 struct proc *p; 2351 int error; 2352 2353 p = l->l_proc; 2354 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0) 2355 return (error); 2356 l->l_ctxlink = ucp->uc_link; 2357 2358 if ((ucp->uc_flags & _UC_SIGMASK) != 0) 2359 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL); 2360 2361 /* 2362 * If there was stack information, update whether or not we are 2363 * still running on an alternate signal stack. 2364 */ 2365 if ((ucp->uc_flags & _UC_STACK) != 0) { 2366 if (ucp->uc_stack.ss_flags & SS_ONSTACK) 2367 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 2368 else 2369 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK; 2370 } 2371 2372 return 0; 2373 } 2374 2375 /* ARGSUSED */ 2376 int 2377 sys_setcontext(struct lwp *l, void *v, register_t *retval) 2378 { 2379 struct sys_setcontext_args /* { 2380 syscallarg(const ucontext_t *) ucp; 2381 } */ *uap = v; 2382 ucontext_t uc; 2383 int error; 2384 2385 error = copyin(SCARG(uap, ucp), &uc, sizeof (uc)); 2386 if (error) 2387 return (error); 2388 if (!(uc.uc_flags & _UC_CPU)) 2389 return (EINVAL); 2390 error = setucontext(l, &uc); 2391 if (error) 2392 return (error); 2393 2394 return (EJUSTRETURN); 2395 } 2396 2397 /* 2398 * sigtimedwait(2) system call, used also for implementation 2399 * of sigwaitinfo() and sigwait(). 2400 * 2401 * This only handles single LWP in signal wait. libpthread provides 2402 * it's own sigtimedwait() wrapper to DTRT WRT individual threads. 2403 */ 2404 int 2405 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval) 2406 { 2407 return __sigtimedwait1(l, v, retval, copyout, copyin, copyout); 2408 } 2409 2410 int 2411 __sigtimedwait1(struct lwp *l, void *v, register_t *retval, 2412 copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout) 2413 { 2414 struct sys___sigtimedwait_args /* { 2415 syscallarg(const sigset_t *) set; 2416 syscallarg(siginfo_t *) info; 2417 syscallarg(struct timespec *) timeout; 2418 } */ *uap = v; 2419 sigset_t *waitset, twaitset; 2420 struct proc *p = l->l_proc; 2421 int error, signum; 2422 int timo = 0; 2423 struct timespec ts, tsstart; 2424 ksiginfo_t *ksi; 2425 2426 memset(&tsstart, 0, sizeof tsstart); /* XXX gcc */ 2427 2428 MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK); 2429 2430 if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) { 2431 FREE(waitset, M_TEMP); 2432 return (error); 2433 } 2434 2435 /* 2436 * Silently ignore SA_CANTMASK signals. psignal() would 2437 * ignore SA_CANTMASK signals in waitset, we do this 2438 * only for the below siglist check. 2439 */ 2440 sigminusset(&sigcantmask, waitset); 2441 2442 /* 2443 * First scan siglist and check if there is signal from 2444 * our waitset already pending. 2445 */ 2446 twaitset = *waitset; 2447 __sigandset(&p->p_sigctx.ps_siglist, &twaitset); 2448 if ((signum = firstsig(&twaitset))) { 2449 /* found pending signal */ 2450 sigdelset(&p->p_sigctx.ps_siglist, signum); 2451 ksi = ksiginfo_dequeue(p, signum); 2452 if (!ksi) { 2453 /* No queued siginfo, manufacture one */ 2454 ksi = ksiginfo_alloc(PR_WAITOK); 2455 KSI_INIT(ksi); 2456 ksi->ksi_info._signo = signum; 2457 ksi->ksi_info._code = SI_USER; 2458 } 2459 2460 goto sig; 2461 } 2462 2463 /* 2464 * Calculate timeout, if it was specified. 2465 */ 2466 if (SCARG(uap, timeout)) { 2467 uint64_t ms; 2468 2469 if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts)))) 2470 return (error); 2471 2472 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); 2473 timo = mstohz(ms); 2474 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0) 2475 timo = 1; 2476 if (timo <= 0) 2477 return (EAGAIN); 2478 2479 /* 2480 * Remember current uptime, it would be used in 2481 * ECANCELED/ERESTART case. 2482 */ 2483 getnanouptime(&tsstart); 2484 } 2485 2486 /* 2487 * Setup ps_sigwait list. Pass pointer to malloced memory 2488 * here; it's not possible to pass pointer to a structure 2489 * on current process's stack, the current process might 2490 * be swapped out at the time the signal would get delivered. 2491 */ 2492 ksi = ksiginfo_alloc(PR_WAITOK); 2493 p->p_sigctx.ps_sigwaited = ksi; 2494 p->p_sigctx.ps_sigwait = waitset; 2495 2496 /* 2497 * Wait for signal to arrive. We can either be woken up or 2498 * time out. 2499 */ 2500 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo); 2501 2502 /* 2503 * Need to find out if we woke as a result of lwp_wakeup() 2504 * or a signal outside our wait set. 2505 */ 2506 if (error == EINTR && p->p_sigctx.ps_sigwaited 2507 && !firstsig(&p->p_sigctx.ps_siglist)) { 2508 /* wakeup via _lwp_wakeup() */ 2509 error = ECANCELED; 2510 } else if (!error && p->p_sigctx.ps_sigwaited) { 2511 /* spurious wakeup - arrange for syscall restart */ 2512 error = ERESTART; 2513 goto fail; 2514 } 2515 2516 /* 2517 * On error, clear sigwait indication. psignal() clears it 2518 * in !error case. 2519 */ 2520 if (error) { 2521 p->p_sigctx.ps_sigwaited = NULL; 2522 2523 /* 2524 * If the sleep was interrupted (either by signal or wakeup), 2525 * update the timeout and copyout new value back. 2526 * It would be used when the syscall would be restarted 2527 * or called again. 2528 */ 2529 if (timo && (error == ERESTART || error == ECANCELED)) { 2530 struct timespec tsnow; 2531 int err; 2532 2533 /* XXX double check the following change */ 2534 getnanouptime(&tsnow); 2535 2536 /* compute how much time has passed since start */ 2537 timespecsub(&tsnow, &tsstart, &tsnow); 2538 /* substract passed time from timeout */ 2539 timespecsub(&ts, &tsnow, &ts); 2540 2541 if (ts.tv_sec < 0) { 2542 error = EAGAIN; 2543 goto fail; 2544 } 2545 /* XXX double check the previous change */ 2546 2547 /* copy updated timeout to userland */ 2548 if ((err = (*put_timeout)(&ts, SCARG(uap, timeout), 2549 sizeof(ts)))) { 2550 error = err; 2551 goto fail; 2552 } 2553 } 2554 2555 goto fail; 2556 } 2557 2558 /* 2559 * If a signal from the wait set arrived, copy it to userland. 2560 * Copy only the used part of siginfo, the padding part is 2561 * left unchanged (userland is not supposed to touch it anyway). 2562 */ 2563 sig: 2564 return (*put_info)(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info)); 2565 2566 fail: 2567 FREE(waitset, M_TEMP); 2568 ksiginfo_free(ksi); 2569 p->p_sigctx.ps_sigwait = NULL; 2570 2571 return (error); 2572 } 2573 2574 /* 2575 * Returns true if signal is ignored or masked for passed process. 2576 */ 2577 int 2578 sigismasked(struct proc *p, int sig) 2579 { 2580 2581 return (sigismember(&p->p_sigctx.ps_sigignore, sig) || 2582 sigismember(&p->p_sigctx.ps_sigmask, sig)); 2583 } 2584 2585 static int 2586 filt_sigattach(struct knote *kn) 2587 { 2588 struct proc *p = curproc; 2589 2590 kn->kn_ptr.p_proc = p; 2591 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2592 2593 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2594 2595 return (0); 2596 } 2597 2598 static void 2599 filt_sigdetach(struct knote *kn) 2600 { 2601 struct proc *p = kn->kn_ptr.p_proc; 2602 2603 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2604 } 2605 2606 /* 2607 * signal knotes are shared with proc knotes, so we apply a mask to 2608 * the hint in order to differentiate them from process hints. This 2609 * could be avoided by using a signal-specific knote list, but probably 2610 * isn't worth the trouble. 2611 */ 2612 static int 2613 filt_signal(struct knote *kn, long hint) 2614 { 2615 2616 if (hint & NOTE_SIGNAL) { 2617 hint &= ~NOTE_SIGNAL; 2618 2619 if (kn->kn_id == hint) 2620 kn->kn_data++; 2621 } 2622 return (kn->kn_data != 0); 2623 } 2624 2625 const struct filterops sig_filtops = { 2626 0, filt_sigattach, filt_sigdetach, filt_signal 2627 }; 2628