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