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