1 /* $OpenBSD: kern_sig.c,v 1.288 2021/11/12 17:57:13 cheloha Exp $ */ 2 /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Theo de Raadt. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1991, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 39 */ 40 41 #include <sys/param.h> 42 #include <sys/signalvar.h> 43 #include <sys/resourcevar.h> 44 #include <sys/queue.h> 45 #include <sys/namei.h> 46 #include <sys/vnode.h> 47 #include <sys/event.h> 48 #include <sys/proc.h> 49 #include <sys/systm.h> 50 #include <sys/acct.h> 51 #include <sys/fcntl.h> 52 #include <sys/filedesc.h> 53 #include <sys/kernel.h> 54 #include <sys/wait.h> 55 #include <sys/ktrace.h> 56 #include <sys/stat.h> 57 #include <sys/core.h> 58 #include <sys/malloc.h> 59 #include <sys/pool.h> 60 #include <sys/ptrace.h> 61 #include <sys/sched.h> 62 #include <sys/user.h> 63 #include <sys/syslog.h> 64 #include <sys/ttycom.h> 65 #include <sys/pledge.h> 66 #include <sys/witness.h> 67 68 #include <sys/mount.h> 69 #include <sys/syscallargs.h> 70 71 #include <uvm/uvm_extern.h> 72 #include <machine/tcb.h> 73 74 int filt_sigattach(struct knote *kn); 75 void filt_sigdetach(struct knote *kn); 76 int filt_signal(struct knote *kn, long hint); 77 78 const struct filterops sig_filtops = { 79 .f_flags = 0, 80 .f_attach = filt_sigattach, 81 .f_detach = filt_sigdetach, 82 .f_event = filt_signal, 83 }; 84 85 const int sigprop[NSIG + 1] = { 86 0, /* unused */ 87 SA_KILL, /* SIGHUP */ 88 SA_KILL, /* SIGINT */ 89 SA_KILL|SA_CORE, /* SIGQUIT */ 90 SA_KILL|SA_CORE, /* SIGILL */ 91 SA_KILL|SA_CORE, /* SIGTRAP */ 92 SA_KILL|SA_CORE, /* SIGABRT */ 93 SA_KILL|SA_CORE, /* SIGEMT */ 94 SA_KILL|SA_CORE, /* SIGFPE */ 95 SA_KILL, /* SIGKILL */ 96 SA_KILL|SA_CORE, /* SIGBUS */ 97 SA_KILL|SA_CORE, /* SIGSEGV */ 98 SA_KILL|SA_CORE, /* SIGSYS */ 99 SA_KILL, /* SIGPIPE */ 100 SA_KILL, /* SIGALRM */ 101 SA_KILL, /* SIGTERM */ 102 SA_IGNORE, /* SIGURG */ 103 SA_STOP, /* SIGSTOP */ 104 SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 105 SA_IGNORE|SA_CONT, /* SIGCONT */ 106 SA_IGNORE, /* SIGCHLD */ 107 SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 108 SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 109 SA_IGNORE, /* SIGIO */ 110 SA_KILL, /* SIGXCPU */ 111 SA_KILL, /* SIGXFSZ */ 112 SA_KILL, /* SIGVTALRM */ 113 SA_KILL, /* SIGPROF */ 114 SA_IGNORE, /* SIGWINCH */ 115 SA_IGNORE, /* SIGINFO */ 116 SA_KILL, /* SIGUSR1 */ 117 SA_KILL, /* SIGUSR2 */ 118 SA_IGNORE, /* SIGTHR */ 119 }; 120 121 #define CONTSIGMASK (sigmask(SIGCONT)) 122 #define STOPSIGMASK (sigmask(SIGSTOP) | sigmask(SIGTSTP) | \ 123 sigmask(SIGTTIN) | sigmask(SIGTTOU)) 124 125 void setsigvec(struct proc *, int, struct sigaction *); 126 127 void proc_stop(struct proc *p, int); 128 void proc_stop_sweep(void *); 129 void *proc_stop_si; 130 131 void postsig(struct proc *, int); 132 int cansignal(struct proc *, struct process *, int); 133 134 struct pool sigacts_pool; /* memory pool for sigacts structures */ 135 136 void sigio_del(struct sigiolst *); 137 void sigio_unlink(struct sigio_ref *, struct sigiolst *); 138 struct mutex sigio_lock = MUTEX_INITIALIZER(IPL_HIGH); 139 140 /* 141 * Can thread p, send the signal signum to process qr? 142 */ 143 int 144 cansignal(struct proc *p, struct process *qr, int signum) 145 { 146 struct process *pr = p->p_p; 147 struct ucred *uc = p->p_ucred; 148 struct ucred *quc = qr->ps_ucred; 149 150 if (uc->cr_uid == 0) 151 return (1); /* root can always signal */ 152 153 if (pr == qr) 154 return (1); /* process can always signal itself */ 155 156 /* optimization: if the same creds then the tests below will pass */ 157 if (uc == quc) 158 return (1); 159 160 if (signum == SIGCONT && qr->ps_session == pr->ps_session) 161 return (1); /* SIGCONT in session */ 162 163 /* 164 * Using kill(), only certain signals can be sent to setugid 165 * child processes 166 */ 167 if (qr->ps_flags & PS_SUGID) { 168 switch (signum) { 169 case 0: 170 case SIGKILL: 171 case SIGINT: 172 case SIGTERM: 173 case SIGALRM: 174 case SIGSTOP: 175 case SIGTTIN: 176 case SIGTTOU: 177 case SIGTSTP: 178 case SIGHUP: 179 case SIGUSR1: 180 case SIGUSR2: 181 if (uc->cr_ruid == quc->cr_ruid || 182 uc->cr_uid == quc->cr_ruid) 183 return (1); 184 } 185 return (0); 186 } 187 188 if (uc->cr_ruid == quc->cr_ruid || 189 uc->cr_ruid == quc->cr_svuid || 190 uc->cr_uid == quc->cr_ruid || 191 uc->cr_uid == quc->cr_svuid) 192 return (1); 193 return (0); 194 } 195 196 /* 197 * Initialize signal-related data structures. 198 */ 199 void 200 signal_init(void) 201 { 202 proc_stop_si = softintr_establish(IPL_SOFTCLOCK, proc_stop_sweep, 203 NULL); 204 if (proc_stop_si == NULL) 205 panic("signal_init failed to register softintr"); 206 207 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE, 208 PR_WAITOK, "sigapl", NULL); 209 } 210 211 /* 212 * Create an initial sigacts structure, using the same signal state 213 * as pr. 214 */ 215 struct sigacts * 216 sigactsinit(struct process *pr) 217 { 218 struct sigacts *ps; 219 220 ps = pool_get(&sigacts_pool, PR_WAITOK); 221 memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts)); 222 return (ps); 223 } 224 225 /* 226 * Initialize a new sigaltstack structure. 227 */ 228 void 229 sigstkinit(struct sigaltstack *ss) 230 { 231 ss->ss_flags = SS_DISABLE; 232 ss->ss_size = 0; 233 ss->ss_sp = NULL; 234 } 235 236 /* 237 * Release a sigacts structure. 238 */ 239 void 240 sigactsfree(struct process *pr) 241 { 242 struct sigacts *ps = pr->ps_sigacts; 243 244 pr->ps_sigacts = NULL; 245 246 pool_put(&sigacts_pool, ps); 247 } 248 249 int 250 sys_sigaction(struct proc *p, void *v, register_t *retval) 251 { 252 struct sys_sigaction_args /* { 253 syscallarg(int) signum; 254 syscallarg(const struct sigaction *) nsa; 255 syscallarg(struct sigaction *) osa; 256 } */ *uap = v; 257 struct sigaction vec; 258 #ifdef KTRACE 259 struct sigaction ovec; 260 #endif 261 struct sigaction *sa; 262 const struct sigaction *nsa; 263 struct sigaction *osa; 264 struct sigacts *ps = p->p_p->ps_sigacts; 265 int signum; 266 int bit, error; 267 268 signum = SCARG(uap, signum); 269 nsa = SCARG(uap, nsa); 270 osa = SCARG(uap, osa); 271 272 if (signum <= 0 || signum >= NSIG || 273 (nsa && (signum == SIGKILL || signum == SIGSTOP))) 274 return (EINVAL); 275 sa = &vec; 276 if (osa) { 277 sa->sa_handler = ps->ps_sigact[signum]; 278 sa->sa_mask = ps->ps_catchmask[signum]; 279 bit = sigmask(signum); 280 sa->sa_flags = 0; 281 if ((ps->ps_sigonstack & bit) != 0) 282 sa->sa_flags |= SA_ONSTACK; 283 if ((ps->ps_sigintr & bit) == 0) 284 sa->sa_flags |= SA_RESTART; 285 if ((ps->ps_sigreset & bit) != 0) 286 sa->sa_flags |= SA_RESETHAND; 287 if ((ps->ps_siginfo & bit) != 0) 288 sa->sa_flags |= SA_SIGINFO; 289 if (signum == SIGCHLD) { 290 if ((ps->ps_sigflags & SAS_NOCLDSTOP) != 0) 291 sa->sa_flags |= SA_NOCLDSTOP; 292 if ((ps->ps_sigflags & SAS_NOCLDWAIT) != 0) 293 sa->sa_flags |= SA_NOCLDWAIT; 294 } 295 if ((sa->sa_mask & bit) == 0) 296 sa->sa_flags |= SA_NODEFER; 297 sa->sa_mask &= ~bit; 298 error = copyout(sa, osa, sizeof (vec)); 299 if (error) 300 return (error); 301 #ifdef KTRACE 302 if (KTRPOINT(p, KTR_STRUCT)) 303 ovec = vec; 304 #endif 305 } 306 if (nsa) { 307 error = copyin(nsa, sa, sizeof (vec)); 308 if (error) 309 return (error); 310 #ifdef KTRACE 311 if (KTRPOINT(p, KTR_STRUCT)) 312 ktrsigaction(p, sa); 313 #endif 314 setsigvec(p, signum, sa); 315 } 316 #ifdef KTRACE 317 if (osa && KTRPOINT(p, KTR_STRUCT)) 318 ktrsigaction(p, &ovec); 319 #endif 320 return (0); 321 } 322 323 void 324 setsigvec(struct proc *p, int signum, struct sigaction *sa) 325 { 326 struct sigacts *ps = p->p_p->ps_sigacts; 327 int bit; 328 int s; 329 330 bit = sigmask(signum); 331 /* 332 * Change setting atomically. 333 */ 334 s = splhigh(); 335 ps->ps_sigact[signum] = sa->sa_handler; 336 if ((sa->sa_flags & SA_NODEFER) == 0) 337 sa->sa_mask |= sigmask(signum); 338 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask; 339 if (signum == SIGCHLD) { 340 if (sa->sa_flags & SA_NOCLDSTOP) 341 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP); 342 else 343 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP); 344 /* 345 * If the SA_NOCLDWAIT flag is set or the handler 346 * is SIG_IGN we reparent the dying child to PID 1 347 * (init) which will reap the zombie. Because we use 348 * init to do our dirty work we never set SAS_NOCLDWAIT 349 * for PID 1. 350 * XXX exit1 rework means this is unnecessary? 351 */ 352 if (initprocess->ps_sigacts != ps && 353 ((sa->sa_flags & SA_NOCLDWAIT) || 354 sa->sa_handler == SIG_IGN)) 355 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 356 else 357 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 358 } 359 if ((sa->sa_flags & SA_RESETHAND) != 0) 360 ps->ps_sigreset |= bit; 361 else 362 ps->ps_sigreset &= ~bit; 363 if ((sa->sa_flags & SA_SIGINFO) != 0) 364 ps->ps_siginfo |= bit; 365 else 366 ps->ps_siginfo &= ~bit; 367 if ((sa->sa_flags & SA_RESTART) == 0) 368 ps->ps_sigintr |= bit; 369 else 370 ps->ps_sigintr &= ~bit; 371 if ((sa->sa_flags & SA_ONSTACK) != 0) 372 ps->ps_sigonstack |= bit; 373 else 374 ps->ps_sigonstack &= ~bit; 375 /* 376 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 377 * and for signals set to SIG_DFL where the default is to ignore. 378 * However, don't put SIGCONT in ps_sigignore, 379 * as we have to restart the process. 380 */ 381 if (sa->sa_handler == SIG_IGN || 382 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) { 383 atomic_clearbits_int(&p->p_siglist, bit); 384 atomic_clearbits_int(&p->p_p->ps_siglist, bit); 385 if (signum != SIGCONT) 386 ps->ps_sigignore |= bit; /* easier in psignal */ 387 ps->ps_sigcatch &= ~bit; 388 } else { 389 ps->ps_sigignore &= ~bit; 390 if (sa->sa_handler == SIG_DFL) 391 ps->ps_sigcatch &= ~bit; 392 else 393 ps->ps_sigcatch |= bit; 394 } 395 splx(s); 396 } 397 398 /* 399 * Initialize signal state for process 0; 400 * set to ignore signals that are ignored by default. 401 */ 402 void 403 siginit(struct sigacts *ps) 404 { 405 int i; 406 407 for (i = 0; i < NSIG; i++) 408 if (sigprop[i] & SA_IGNORE && i != SIGCONT) 409 ps->ps_sigignore |= sigmask(i); 410 ps->ps_sigflags = SAS_NOCLDWAIT | SAS_NOCLDSTOP; 411 } 412 413 /* 414 * Reset signals for an exec by the specified thread. 415 */ 416 void 417 execsigs(struct proc *p) 418 { 419 struct sigacts *ps; 420 int nc, mask; 421 422 ps = p->p_p->ps_sigacts; 423 424 /* 425 * Reset caught signals. Held signals remain held 426 * through p_sigmask (unless they were caught, 427 * and are now ignored by default). 428 */ 429 while (ps->ps_sigcatch) { 430 nc = ffs((long)ps->ps_sigcatch); 431 mask = sigmask(nc); 432 ps->ps_sigcatch &= ~mask; 433 if (sigprop[nc] & SA_IGNORE) { 434 if (nc != SIGCONT) 435 ps->ps_sigignore |= mask; 436 atomic_clearbits_int(&p->p_siglist, mask); 437 atomic_clearbits_int(&p->p_p->ps_siglist, mask); 438 } 439 ps->ps_sigact[nc] = SIG_DFL; 440 } 441 /* 442 * Reset stack state to the user stack. 443 * Clear set of signals caught on the signal stack. 444 */ 445 sigstkinit(&p->p_sigstk); 446 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT); 447 if (ps->ps_sigact[SIGCHLD] == SIG_IGN) 448 ps->ps_sigact[SIGCHLD] = SIG_DFL; 449 } 450 451 /* 452 * Manipulate signal mask. 453 * Note that we receive new mask, not pointer, 454 * and return old mask as return value; 455 * the library stub does the rest. 456 */ 457 int 458 sys_sigprocmask(struct proc *p, void *v, register_t *retval) 459 { 460 struct sys_sigprocmask_args /* { 461 syscallarg(int) how; 462 syscallarg(sigset_t) mask; 463 } */ *uap = v; 464 int error = 0; 465 sigset_t mask; 466 467 KASSERT(p == curproc); 468 469 *retval = p->p_sigmask; 470 mask = SCARG(uap, mask) &~ sigcantmask; 471 472 switch (SCARG(uap, how)) { 473 case SIG_BLOCK: 474 atomic_setbits_int(&p->p_sigmask, mask); 475 break; 476 case SIG_UNBLOCK: 477 atomic_clearbits_int(&p->p_sigmask, mask); 478 break; 479 case SIG_SETMASK: 480 p->p_sigmask = mask; 481 break; 482 default: 483 error = EINVAL; 484 break; 485 } 486 return (error); 487 } 488 489 int 490 sys_sigpending(struct proc *p, void *v, register_t *retval) 491 { 492 493 *retval = p->p_siglist | p->p_p->ps_siglist; 494 return (0); 495 } 496 497 /* 498 * Temporarily replace calling proc's signal mask for the duration of a 499 * system call. Original signal mask will be restored by userret(). 500 */ 501 void 502 dosigsuspend(struct proc *p, sigset_t newmask) 503 { 504 KASSERT(p == curproc); 505 506 p->p_oldmask = p->p_sigmask; 507 atomic_setbits_int(&p->p_flag, P_SIGSUSPEND); 508 p->p_sigmask = newmask; 509 } 510 511 /* 512 * Suspend thread until signal, providing mask to be set 513 * in the meantime. Note nonstandard calling convention: 514 * libc stub passes mask, not pointer, to save a copyin. 515 */ 516 int 517 sys_sigsuspend(struct proc *p, void *v, register_t *retval) 518 { 519 struct sys_sigsuspend_args /* { 520 syscallarg(int) mask; 521 } */ *uap = v; 522 523 dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask); 524 while (tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigsusp", INFSLP) == 0) 525 continue; 526 /* always return EINTR rather than ERESTART... */ 527 return (EINTR); 528 } 529 530 int 531 sigonstack(size_t stack) 532 { 533 const struct sigaltstack *ss = &curproc->p_sigstk; 534 535 return (ss->ss_flags & SS_DISABLE ? 0 : 536 (stack - (size_t)ss->ss_sp < ss->ss_size)); 537 } 538 539 int 540 sys_sigaltstack(struct proc *p, void *v, register_t *retval) 541 { 542 struct sys_sigaltstack_args /* { 543 syscallarg(const struct sigaltstack *) nss; 544 syscallarg(struct sigaltstack *) oss; 545 } */ *uap = v; 546 struct sigaltstack ss; 547 const struct sigaltstack *nss; 548 struct sigaltstack *oss; 549 int onstack = sigonstack(PROC_STACK(p)); 550 int error; 551 552 nss = SCARG(uap, nss); 553 oss = SCARG(uap, oss); 554 555 if (oss != NULL) { 556 ss = p->p_sigstk; 557 if (onstack) 558 ss.ss_flags |= SS_ONSTACK; 559 if ((error = copyout(&ss, oss, sizeof(ss)))) 560 return (error); 561 } 562 if (nss == NULL) 563 return (0); 564 error = copyin(nss, &ss, sizeof(ss)); 565 if (error) 566 return (error); 567 if (onstack) 568 return (EPERM); 569 if (ss.ss_flags & ~SS_DISABLE) 570 return (EINVAL); 571 if (ss.ss_flags & SS_DISABLE) { 572 p->p_sigstk.ss_flags = ss.ss_flags; 573 return (0); 574 } 575 if (ss.ss_size < MINSIGSTKSZ) 576 return (ENOMEM); 577 578 error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size); 579 if (error) 580 return (error); 581 582 p->p_sigstk = ss; 583 return (0); 584 } 585 586 int 587 sys_kill(struct proc *cp, void *v, register_t *retval) 588 { 589 struct sys_kill_args /* { 590 syscallarg(int) pid; 591 syscallarg(int) signum; 592 } */ *uap = v; 593 struct process *pr; 594 int pid = SCARG(uap, pid); 595 int signum = SCARG(uap, signum); 596 int error; 597 int zombie = 0; 598 599 if ((error = pledge_kill(cp, pid)) != 0) 600 return (error); 601 if (((u_int)signum) >= NSIG) 602 return (EINVAL); 603 if (pid > 0) { 604 if ((pr = prfind(pid)) == NULL) { 605 if ((pr = zombiefind(pid)) == NULL) 606 return (ESRCH); 607 else 608 zombie = 1; 609 } 610 if (!cansignal(cp, pr, signum)) 611 return (EPERM); 612 613 /* kill single process */ 614 if (signum && !zombie) 615 prsignal(pr, signum); 616 return (0); 617 } 618 switch (pid) { 619 case -1: /* broadcast signal */ 620 return (killpg1(cp, signum, 0, 1)); 621 case 0: /* signal own process group */ 622 return (killpg1(cp, signum, 0, 0)); 623 default: /* negative explicit process group */ 624 return (killpg1(cp, signum, -pid, 0)); 625 } 626 } 627 628 int 629 sys_thrkill(struct proc *cp, void *v, register_t *retval) 630 { 631 struct sys_thrkill_args /* { 632 syscallarg(pid_t) tid; 633 syscallarg(int) signum; 634 syscallarg(void *) tcb; 635 } */ *uap = v; 636 struct proc *p; 637 int tid = SCARG(uap, tid); 638 int signum = SCARG(uap, signum); 639 void *tcb; 640 641 if (((u_int)signum) >= NSIG) 642 return (EINVAL); 643 if (tid > THREAD_PID_OFFSET) { 644 if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL) 645 return (ESRCH); 646 647 /* can only kill threads in the same process */ 648 if (p->p_p != cp->p_p) 649 return (ESRCH); 650 } else if (tid == 0) 651 p = cp; 652 else 653 return (EINVAL); 654 655 /* optionally require the target thread to have the given tcb addr */ 656 tcb = SCARG(uap, tcb); 657 if (tcb != NULL && tcb != TCB_GET(p)) 658 return (ESRCH); 659 660 if (signum) 661 ptsignal(p, signum, STHREAD); 662 return (0); 663 } 664 665 /* 666 * Common code for kill process group/broadcast kill. 667 * cp is calling process. 668 */ 669 int 670 killpg1(struct proc *cp, int signum, int pgid, int all) 671 { 672 struct process *pr; 673 struct pgrp *pgrp; 674 int nfound = 0; 675 676 if (all) { 677 /* 678 * broadcast 679 */ 680 LIST_FOREACH(pr, &allprocess, ps_list) { 681 if (pr->ps_pid <= 1 || 682 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) || 683 pr == cp->p_p || !cansignal(cp, pr, signum)) 684 continue; 685 nfound++; 686 if (signum) 687 prsignal(pr, signum); 688 } 689 } else { 690 if (pgid == 0) 691 /* 692 * zero pgid means send to my process group. 693 */ 694 pgrp = cp->p_p->ps_pgrp; 695 else { 696 pgrp = pgfind(pgid); 697 if (pgrp == NULL) 698 return (ESRCH); 699 } 700 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 701 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM || 702 !cansignal(cp, pr, signum)) 703 continue; 704 nfound++; 705 if (signum) 706 prsignal(pr, signum); 707 } 708 } 709 return (nfound ? 0 : ESRCH); 710 } 711 712 #define CANDELIVER(uid, euid, pr) \ 713 (euid == 0 || \ 714 (uid) == (pr)->ps_ucred->cr_ruid || \ 715 (uid) == (pr)->ps_ucred->cr_svuid || \ 716 (uid) == (pr)->ps_ucred->cr_uid || \ 717 (euid) == (pr)->ps_ucred->cr_ruid || \ 718 (euid) == (pr)->ps_ucred->cr_svuid || \ 719 (euid) == (pr)->ps_ucred->cr_uid) 720 721 #define CANSIGIO(cr, pr) \ 722 CANDELIVER((cr)->cr_ruid, (cr)->cr_uid, (pr)) 723 724 /* 725 * Send a signal to a process group. If checktty is 1, 726 * limit to members which have a controlling terminal. 727 */ 728 void 729 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 730 { 731 struct process *pr; 732 733 if (pgrp) 734 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 735 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT) 736 prsignal(pr, signum); 737 } 738 739 /* 740 * Send a SIGIO or SIGURG signal to a process or process group using stored 741 * credentials rather than those of the current process. 742 */ 743 void 744 pgsigio(struct sigio_ref *sir, int sig, int checkctty) 745 { 746 struct process *pr; 747 struct sigio *sigio; 748 749 if (sir->sir_sigio == NULL) 750 return; 751 752 KERNEL_LOCK(); 753 mtx_enter(&sigio_lock); 754 sigio = sir->sir_sigio; 755 if (sigio == NULL) 756 goto out; 757 if (sigio->sio_pgid > 0) { 758 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc)) 759 prsignal(sigio->sio_proc, sig); 760 } else if (sigio->sio_pgid < 0) { 761 LIST_FOREACH(pr, &sigio->sio_pgrp->pg_members, ps_pglist) { 762 if (CANSIGIO(sigio->sio_ucred, pr) && 763 (checkctty == 0 || (pr->ps_flags & PS_CONTROLT))) 764 prsignal(pr, sig); 765 } 766 } 767 out: 768 mtx_leave(&sigio_lock); 769 KERNEL_UNLOCK(); 770 } 771 772 /* 773 * Recalculate the signal mask and reset the signal disposition after 774 * usermode frame for delivery is formed. 775 */ 776 void 777 postsig_done(struct proc *p, int signum, struct sigacts *ps) 778 { 779 int mask = sigmask(signum); 780 781 KERNEL_ASSERT_LOCKED(); 782 783 p->p_ru.ru_nsignals++; 784 atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]); 785 if ((ps->ps_sigreset & mask) != 0) { 786 ps->ps_sigcatch &= ~mask; 787 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 788 ps->ps_sigignore |= mask; 789 ps->ps_sigact[signum] = SIG_DFL; 790 } 791 } 792 793 /* 794 * Send a signal caused by a trap to the current thread 795 * If it will be caught immediately, deliver it with correct code. 796 * Otherwise, post it normally. 797 */ 798 void 799 trapsignal(struct proc *p, int signum, u_long trapno, int code, 800 union sigval sigval) 801 { 802 struct process *pr = p->p_p; 803 struct sigacts *ps = pr->ps_sigacts; 804 int mask; 805 806 KERNEL_LOCK(); 807 switch (signum) { 808 case SIGILL: 809 case SIGBUS: 810 case SIGSEGV: 811 pr->ps_acflag |= ATRAP; 812 break; 813 } 814 815 mask = sigmask(signum); 816 if ((pr->ps_flags & PS_TRACED) == 0 && 817 (ps->ps_sigcatch & mask) != 0 && 818 (p->p_sigmask & mask) == 0) { 819 siginfo_t si; 820 int info = (ps->ps_siginfo & mask) != 0; 821 int onstack = (ps->ps_sigonstack & mask) != 0; 822 823 initsiginfo(&si, signum, trapno, code, sigval); 824 #ifdef KTRACE 825 if (KTRPOINT(p, KTR_PSIG)) { 826 ktrpsig(p, signum, ps->ps_sigact[signum], 827 p->p_sigmask, code, &si); 828 } 829 #endif 830 if (sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si, 831 info, onstack)) { 832 sigexit(p, SIGILL); 833 /* NOTREACHED */ 834 } 835 postsig_done(p, signum, ps); 836 } else { 837 p->p_sisig = signum; 838 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 839 p->p_sicode = code; 840 p->p_sigval = sigval; 841 842 /* 843 * Signals like SIGBUS and SIGSEGV should not, when 844 * generated by the kernel, be ignorable or blockable. 845 * If it is and we're not being traced, then just kill 846 * the process. 847 * After vfs_shutdown(9), init(8) cannot receive signals 848 * because new code pages of the signal handler cannot be 849 * mapped from halted storage. init(8) may not die or the 850 * kernel panics. Better loop between signal handler and 851 * page fault trap until the machine is halted. 852 */ 853 if ((pr->ps_flags & PS_TRACED) == 0 && 854 (sigprop[signum] & SA_KILL) && 855 ((p->p_sigmask & mask) || (ps->ps_sigignore & mask)) && 856 pr->ps_pid != 1) 857 sigexit(p, signum); 858 ptsignal(p, signum, STHREAD); 859 } 860 KERNEL_UNLOCK(); 861 } 862 863 /* 864 * Send the signal to the process. If the signal has an action, the action 865 * is usually performed by the target process rather than the caller; we add 866 * the signal to the set of pending signals for the process. 867 * 868 * Exceptions: 869 * o When a stop signal is sent to a sleeping process that takes the 870 * default action, the process is stopped without awakening it. 871 * o SIGCONT restarts stopped processes (or puts them back to sleep) 872 * regardless of the signal action (eg, blocked or ignored). 873 * 874 * Other ignored signals are discarded immediately. 875 */ 876 void 877 psignal(struct proc *p, int signum) 878 { 879 ptsignal(p, signum, SPROCESS); 880 } 881 882 /* 883 * type = SPROCESS process signal, can be diverted (sigwait()) 884 * type = STHREAD thread signal, but should be propagated if unhandled 885 * type = SPROPAGATED propagated to this thread, so don't propagate again 886 */ 887 void 888 ptsignal(struct proc *p, int signum, enum signal_type type) 889 { 890 int s, prop; 891 sig_t action; 892 int mask; 893 int *siglist; 894 struct process *pr = p->p_p; 895 struct proc *q; 896 int wakeparent = 0; 897 898 KERNEL_ASSERT_LOCKED(); 899 900 #ifdef DIAGNOSTIC 901 if ((u_int)signum >= NSIG || signum == 0) 902 panic("psignal signal number"); 903 #endif 904 905 /* Ignore signal if the target process is exiting */ 906 if (pr->ps_flags & PS_EXITING) 907 return; 908 909 mask = sigmask(signum); 910 911 if (type == SPROCESS) { 912 /* Accept SIGKILL to coredumping processes */ 913 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 914 atomic_setbits_int(&pr->ps_siglist, mask); 915 return; 916 } 917 918 /* 919 * If the current thread can process the signal 920 * immediately (it's unblocked) then have it take it. 921 */ 922 q = curproc; 923 if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 924 (q->p_sigmask & mask) == 0) 925 p = q; 926 else { 927 /* 928 * A process-wide signal can be diverted to a 929 * different thread that's in sigwait() for this 930 * signal. If there isn't such a thread, then 931 * pick a thread that doesn't have it blocked so 932 * that the stop/kill consideration isn't 933 * delayed. Otherwise, mark it pending on the 934 * main thread. 935 */ 936 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 937 /* ignore exiting threads */ 938 if (q->p_flag & P_WEXIT) 939 continue; 940 941 /* skip threads that have the signal blocked */ 942 if ((q->p_sigmask & mask) != 0) 943 continue; 944 945 /* okay, could send to this thread */ 946 p = q; 947 948 /* 949 * sigsuspend, sigwait, ppoll/pselect, etc? 950 * Definitely go to this thread, as it's 951 * already blocked in the kernel. 952 */ 953 if (q->p_flag & P_SIGSUSPEND) 954 break; 955 } 956 } 957 } 958 959 if (type != SPROPAGATED) 960 KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum); 961 962 prop = sigprop[signum]; 963 964 /* 965 * If proc is traced, always give parent a chance. 966 */ 967 if (pr->ps_flags & PS_TRACED) { 968 action = SIG_DFL; 969 } else { 970 /* 971 * If the signal is being ignored, 972 * then we forget about it immediately. 973 * (Note: we don't set SIGCONT in ps_sigignore, 974 * and if it is set to SIG_IGN, 975 * action will be SIG_DFL here.) 976 */ 977 if (pr->ps_sigacts->ps_sigignore & mask) 978 return; 979 if (p->p_sigmask & mask) { 980 action = SIG_HOLD; 981 } else if (pr->ps_sigacts->ps_sigcatch & mask) { 982 action = SIG_CATCH; 983 } else { 984 action = SIG_DFL; 985 986 if (prop & SA_KILL && pr->ps_nice > NZERO) 987 pr->ps_nice = NZERO; 988 989 /* 990 * If sending a tty stop signal to a member of an 991 * orphaned process group, discard the signal here if 992 * the action is default; don't stop the process below 993 * if sleeping, and don't clear any pending SIGCONT. 994 */ 995 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 996 return; 997 } 998 } 999 /* 1000 * If delivered to process, mark as pending there. Continue and stop 1001 * signals will be propagated to all threads. So they are always 1002 * marked at thread level. 1003 */ 1004 siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist; 1005 if (prop & SA_CONT) { 1006 siglist = &p->p_siglist; 1007 atomic_clearbits_int(siglist, STOPSIGMASK); 1008 } 1009 if (prop & SA_STOP) { 1010 siglist = &p->p_siglist; 1011 atomic_clearbits_int(siglist, CONTSIGMASK); 1012 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 1013 } 1014 atomic_setbits_int(siglist, mask); 1015 1016 /* 1017 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 1018 */ 1019 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 1020 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 1021 if (q != p) 1022 ptsignal(q, signum, SPROPAGATED); 1023 1024 /* 1025 * Defer further processing for signals which are held, 1026 * except that stopped processes must be continued by SIGCONT. 1027 */ 1028 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 1029 return; 1030 1031 SCHED_LOCK(s); 1032 1033 switch (p->p_stat) { 1034 1035 case SSLEEP: 1036 /* 1037 * If process is sleeping uninterruptibly 1038 * we can't interrupt the sleep... the signal will 1039 * be noticed when the process returns through 1040 * trap() or syscall(). 1041 */ 1042 if ((p->p_flag & P_SINTR) == 0) 1043 goto out; 1044 /* 1045 * Process is sleeping and traced... make it runnable 1046 * so it can discover the signal in cursig() and stop 1047 * for the parent. 1048 */ 1049 if (pr->ps_flags & PS_TRACED) 1050 goto run; 1051 /* 1052 * If SIGCONT is default (or ignored) and process is 1053 * asleep, we are finished; the process should not 1054 * be awakened. 1055 */ 1056 if ((prop & SA_CONT) && action == SIG_DFL) { 1057 atomic_clearbits_int(siglist, mask); 1058 goto out; 1059 } 1060 /* 1061 * When a sleeping process receives a stop 1062 * signal, process immediately if possible. 1063 */ 1064 if ((prop & SA_STOP) && action == SIG_DFL) { 1065 /* 1066 * If a child holding parent blocked, 1067 * stopping could cause deadlock. 1068 */ 1069 if (pr->ps_flags & PS_PPWAIT) 1070 goto out; 1071 atomic_clearbits_int(siglist, mask); 1072 pr->ps_xsig = signum; 1073 proc_stop(p, 0); 1074 goto out; 1075 } 1076 /* 1077 * All other (caught or default) signals 1078 * cause the process to run. 1079 */ 1080 goto runfast; 1081 /* NOTREACHED */ 1082 1083 case SSTOP: 1084 /* 1085 * If traced process is already stopped, 1086 * then no further action is necessary. 1087 */ 1088 if (pr->ps_flags & PS_TRACED) 1089 goto out; 1090 1091 /* 1092 * Kill signal always sets processes running. 1093 */ 1094 if (signum == SIGKILL) { 1095 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1096 goto runfast; 1097 } 1098 1099 if (prop & SA_CONT) { 1100 /* 1101 * If SIGCONT is default (or ignored), we continue the 1102 * process but don't leave the signal in p_siglist, as 1103 * it has no further action. If SIGCONT is held, we 1104 * continue the process and leave the signal in 1105 * p_siglist. If the process catches SIGCONT, let it 1106 * handle the signal itself. If it isn't waiting on 1107 * an event, then it goes back to run state. 1108 * Otherwise, process goes back to sleep state. 1109 */ 1110 atomic_setbits_int(&p->p_flag, P_CONTINUED); 1111 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1112 wakeparent = 1; 1113 if (action == SIG_DFL) 1114 atomic_clearbits_int(siglist, mask); 1115 if (action == SIG_CATCH) 1116 goto runfast; 1117 if (p->p_wchan == NULL) 1118 goto run; 1119 p->p_stat = SSLEEP; 1120 goto out; 1121 } 1122 1123 if (prop & SA_STOP) { 1124 /* 1125 * Already stopped, don't need to stop again. 1126 * (If we did the shell could get confused.) 1127 */ 1128 atomic_clearbits_int(siglist, mask); 1129 goto out; 1130 } 1131 1132 /* 1133 * If process is sleeping interruptibly, then simulate a 1134 * wakeup so that when it is continued, it will be made 1135 * runnable and can look at the signal. But don't make 1136 * the process runnable, leave it stopped. 1137 */ 1138 if (p->p_flag & P_SINTR) 1139 unsleep(p); 1140 goto out; 1141 1142 case SONPROC: 1143 signotify(p); 1144 /* FALLTHROUGH */ 1145 default: 1146 /* 1147 * SRUN, SIDL, SDEAD do nothing with the signal, 1148 * other than kicking ourselves if we are running. 1149 * It will either never be noticed, or noticed very soon. 1150 */ 1151 goto out; 1152 } 1153 /* NOTREACHED */ 1154 1155 runfast: 1156 /* 1157 * Raise priority to at least PUSER. 1158 */ 1159 if (p->p_usrpri > PUSER) 1160 p->p_usrpri = PUSER; 1161 run: 1162 setrunnable(p); 1163 out: 1164 SCHED_UNLOCK(s); 1165 if (wakeparent) 1166 wakeup(pr->ps_pptr); 1167 } 1168 1169 /* 1170 * Determine signal that should be delivered to process p, the current 1171 * process, 0 if none. 1172 * 1173 * If the current process has received a signal (should be caught or cause 1174 * termination, should interrupt current syscall), return the signal number. 1175 * Stop signals with default action are processed immediately, then cleared; 1176 * they aren't returned. This is checked after each entry to the system for 1177 * a syscall or trap. The normal call sequence is 1178 * 1179 * while (signum = cursig(curproc)) 1180 * postsig(signum); 1181 * 1182 * Assumes that if the P_SINTR flag is set, we're holding both the 1183 * kernel and scheduler locks. 1184 */ 1185 int 1186 cursig(struct proc *p) 1187 { 1188 struct process *pr = p->p_p; 1189 int sigpending, signum, mask, prop; 1190 int dolock = (p->p_flag & P_SINTR) == 0; 1191 int s; 1192 1193 KERNEL_ASSERT_LOCKED(); 1194 1195 sigpending = (p->p_siglist | pr->ps_siglist); 1196 if (sigpending == 0) 1197 return 0; 1198 1199 if (!ISSET(pr->ps_flags, PS_TRACED) && SIGPENDING(p) == 0) 1200 return 0; 1201 1202 for (;;) { 1203 mask = SIGPENDING(p); 1204 if (pr->ps_flags & PS_PPWAIT) 1205 mask &= ~STOPSIGMASK; 1206 if (mask == 0) /* no signal to send */ 1207 return (0); 1208 signum = ffs((long)mask); 1209 mask = sigmask(signum); 1210 atomic_clearbits_int(&p->p_siglist, mask); 1211 atomic_clearbits_int(&pr->ps_siglist, mask); 1212 1213 /* 1214 * We should see pending but ignored signals 1215 * only if PS_TRACED was on when they were posted. 1216 */ 1217 if (mask & pr->ps_sigacts->ps_sigignore && 1218 (pr->ps_flags & PS_TRACED) == 0) 1219 continue; 1220 1221 /* 1222 * If traced, always stop, and stay stopped until released 1223 * by the debugger. If our parent process is waiting for 1224 * us, don't hang as we could deadlock. 1225 */ 1226 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1227 signum != SIGKILL) { 1228 pr->ps_xsig = signum; 1229 1230 single_thread_set(p, SINGLE_SUSPEND, 0); 1231 1232 if (dolock) 1233 SCHED_LOCK(s); 1234 proc_stop(p, 1); 1235 if (dolock) 1236 SCHED_UNLOCK(s); 1237 1238 single_thread_clear(p, 0); 1239 1240 /* 1241 * If we are no longer being traced, or the parent 1242 * didn't give us a signal, look for more signals. 1243 */ 1244 if ((pr->ps_flags & PS_TRACED) == 0 || 1245 pr->ps_xsig == 0) 1246 continue; 1247 1248 /* 1249 * If the new signal is being masked, look for other 1250 * signals. 1251 */ 1252 signum = pr->ps_xsig; 1253 mask = sigmask(signum); 1254 if ((p->p_sigmask & mask) != 0) 1255 continue; 1256 1257 /* take the signal! */ 1258 atomic_clearbits_int(&p->p_siglist, mask); 1259 atomic_clearbits_int(&pr->ps_siglist, mask); 1260 } 1261 1262 prop = sigprop[signum]; 1263 1264 /* 1265 * Decide whether the signal should be returned. 1266 * Return the signal's number, or fall through 1267 * to clear it from the pending mask. 1268 */ 1269 switch ((long)pr->ps_sigacts->ps_sigact[signum]) { 1270 case (long)SIG_DFL: 1271 /* 1272 * Don't take default actions on system processes. 1273 */ 1274 if (pr->ps_pid <= 1) { 1275 #ifdef DIAGNOSTIC 1276 /* 1277 * Are you sure you want to ignore SIGSEGV 1278 * in init? XXX 1279 */ 1280 printf("Process (pid %d) got signal" 1281 " %d\n", pr->ps_pid, signum); 1282 #endif 1283 break; /* == ignore */ 1284 } 1285 /* 1286 * If there is a pending stop signal to process 1287 * with default action, stop here, 1288 * then clear the signal. However, 1289 * if process is member of an orphaned 1290 * process group, ignore tty stop signals. 1291 */ 1292 if (prop & SA_STOP) { 1293 if (pr->ps_flags & PS_TRACED || 1294 (pr->ps_pgrp->pg_jobc == 0 && 1295 prop & SA_TTYSTOP)) 1296 break; /* == ignore */ 1297 pr->ps_xsig = signum; 1298 if (dolock) 1299 SCHED_LOCK(s); 1300 proc_stop(p, 1); 1301 if (dolock) 1302 SCHED_UNLOCK(s); 1303 break; 1304 } else if (prop & SA_IGNORE) { 1305 /* 1306 * Except for SIGCONT, shouldn't get here. 1307 * Default action is to ignore; drop it. 1308 */ 1309 break; /* == ignore */ 1310 } else 1311 goto keep; 1312 /* NOTREACHED */ 1313 case (long)SIG_IGN: 1314 /* 1315 * Masking above should prevent us ever trying 1316 * to take action on an ignored signal other 1317 * than SIGCONT, unless process is traced. 1318 */ 1319 if ((prop & SA_CONT) == 0 && 1320 (pr->ps_flags & PS_TRACED) == 0) 1321 printf("%s\n", __func__); 1322 break; /* == ignore */ 1323 default: 1324 /* 1325 * This signal has an action, let 1326 * postsig() process it. 1327 */ 1328 goto keep; 1329 } 1330 } 1331 /* NOTREACHED */ 1332 1333 keep: 1334 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */ 1335 return (signum); 1336 } 1337 1338 /* 1339 * Put the argument process into the stopped state and notify the parent 1340 * via wakeup. Signals are handled elsewhere. The process must not be 1341 * on the run queue. 1342 */ 1343 void 1344 proc_stop(struct proc *p, int sw) 1345 { 1346 struct process *pr = p->p_p; 1347 1348 #ifdef MULTIPROCESSOR 1349 SCHED_ASSERT_LOCKED(); 1350 #endif 1351 1352 p->p_stat = SSTOP; 1353 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1354 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1355 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1356 /* 1357 * We need this soft interrupt to be handled fast. 1358 * Extra calls to softclock don't hurt. 1359 */ 1360 softintr_schedule(proc_stop_si); 1361 if (sw) 1362 mi_switch(); 1363 } 1364 1365 /* 1366 * Called from a soft interrupt to send signals to the parents of stopped 1367 * processes. 1368 * We can't do this in proc_stop because it's called with nasty locks held 1369 * and we would need recursive scheduler lock to deal with that. 1370 */ 1371 void 1372 proc_stop_sweep(void *v) 1373 { 1374 struct process *pr; 1375 1376 LIST_FOREACH(pr, &allprocess, ps_list) { 1377 if ((pr->ps_flags & PS_STOPPED) == 0) 1378 continue; 1379 atomic_clearbits_int(&pr->ps_flags, PS_STOPPED); 1380 1381 if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0) 1382 prsignal(pr->ps_pptr, SIGCHLD); 1383 wakeup(pr->ps_pptr); 1384 } 1385 } 1386 1387 /* 1388 * Take the action for the specified signal 1389 * from the current set of pending signals. 1390 */ 1391 void 1392 postsig(struct proc *p, int signum) 1393 { 1394 struct process *pr = p->p_p; 1395 struct sigacts *ps = pr->ps_sigacts; 1396 sig_t action; 1397 u_long trapno; 1398 int mask, returnmask; 1399 siginfo_t si; 1400 union sigval sigval; 1401 int s, code, info, onstack; 1402 1403 KASSERT(signum != 0); 1404 KERNEL_ASSERT_LOCKED(); 1405 1406 mask = sigmask(signum); 1407 atomic_clearbits_int(&p->p_siglist, mask); 1408 action = ps->ps_sigact[signum]; 1409 info = (ps->ps_siginfo & mask) != 0; 1410 onstack = (ps->ps_sigonstack & mask) != 0; 1411 sigval.sival_ptr = NULL; 1412 1413 if (p->p_sisig != signum) { 1414 trapno = 0; 1415 code = SI_USER; 1416 sigval.sival_ptr = NULL; 1417 } else { 1418 trapno = p->p_sitrapno; 1419 code = p->p_sicode; 1420 sigval = p->p_sigval; 1421 } 1422 initsiginfo(&si, signum, trapno, code, sigval); 1423 1424 #ifdef KTRACE 1425 if (KTRPOINT(p, KTR_PSIG)) { 1426 ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ? 1427 p->p_oldmask : p->p_sigmask, code, &si); 1428 } 1429 #endif 1430 if (action == SIG_DFL) { 1431 /* 1432 * Default action, where the default is to kill 1433 * the process. (Other cases were ignored above.) 1434 */ 1435 sigexit(p, signum); 1436 /* NOTREACHED */ 1437 } else { 1438 /* 1439 * If we get here, the signal must be caught. 1440 */ 1441 #ifdef DIAGNOSTIC 1442 if (action == SIG_IGN || (p->p_sigmask & mask)) 1443 panic("postsig action"); 1444 #endif 1445 /* 1446 * Set the new mask value and also defer further 1447 * occurrences of this signal. 1448 * 1449 * Special case: user has done a sigpause. Here the 1450 * current mask is not of interest, but rather the 1451 * mask from before the sigpause is what we want 1452 * restored after the signal processing is completed. 1453 */ 1454 #ifdef MULTIPROCESSOR 1455 s = splsched(); 1456 #else 1457 s = splhigh(); 1458 #endif 1459 if (p->p_flag & P_SIGSUSPEND) { 1460 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1461 returnmask = p->p_oldmask; 1462 } else { 1463 returnmask = p->p_sigmask; 1464 } 1465 if (p->p_sisig == signum) { 1466 p->p_sisig = 0; 1467 p->p_sitrapno = 0; 1468 p->p_sicode = SI_USER; 1469 p->p_sigval.sival_ptr = NULL; 1470 } 1471 1472 if (sendsig(action, signum, returnmask, &si, info, onstack)) { 1473 sigexit(p, SIGILL); 1474 /* NOTREACHED */ 1475 } 1476 postsig_done(p, signum, ps); 1477 splx(s); 1478 } 1479 } 1480 1481 /* 1482 * Force the current process to exit with the specified signal, dumping core 1483 * if appropriate. We bypass the normal tests for masked and caught signals, 1484 * allowing unrecoverable failures to terminate the process without changing 1485 * signal state. Mark the accounting record with the signal termination. 1486 * If dumping core, save the signal number for the debugger. Calls exit and 1487 * does not return. 1488 */ 1489 void 1490 sigexit(struct proc *p, int signum) 1491 { 1492 /* Mark process as going away */ 1493 atomic_setbits_int(&p->p_flag, P_WEXIT); 1494 1495 p->p_p->ps_acflag |= AXSIG; 1496 if (sigprop[signum] & SA_CORE) { 1497 p->p_sisig = signum; 1498 1499 /* if there are other threads, pause them */ 1500 if (P_HASSIBLING(p)) 1501 single_thread_set(p, SINGLE_SUSPEND, 1); 1502 1503 if (coredump(p) == 0) 1504 signum |= WCOREFLAG; 1505 } 1506 exit1(p, 0, signum, EXIT_NORMAL); 1507 /* NOTREACHED */ 1508 } 1509 1510 /* 1511 * Send uncatchable SIGABRT for coredump. 1512 */ 1513 void 1514 sigabort(struct proc *p) 1515 { 1516 struct sigaction sa; 1517 1518 memset(&sa, 0, sizeof sa); 1519 sa.sa_handler = SIG_DFL; 1520 setsigvec(p, SIGABRT, &sa); 1521 atomic_clearbits_int(&p->p_sigmask, sigmask(SIGABRT)); 1522 psignal(p, SIGABRT); 1523 } 1524 1525 /* 1526 * Return 1 if `sig', a given signal, is ignored or masked for `p', a given 1527 * thread, and 0 otherwise. 1528 */ 1529 int 1530 sigismasked(struct proc *p, int sig) 1531 { 1532 struct process *pr = p->p_p; 1533 1534 if ((pr->ps_sigacts->ps_sigignore & sigmask(sig)) || 1535 (p->p_sigmask & sigmask(sig))) 1536 return 1; 1537 1538 return 0; 1539 } 1540 1541 int nosuidcoredump = 1; 1542 1543 struct coredump_iostate { 1544 struct proc *io_proc; 1545 struct vnode *io_vp; 1546 struct ucred *io_cred; 1547 off_t io_offset; 1548 }; 1549 1550 /* 1551 * Dump core, into a file named "progname.core", unless the process was 1552 * setuid/setgid. 1553 */ 1554 int 1555 coredump(struct proc *p) 1556 { 1557 #ifdef SMALL_KERNEL 1558 return EPERM; 1559 #else 1560 struct process *pr = p->p_p; 1561 struct vnode *vp; 1562 struct ucred *cred = p->p_ucred; 1563 struct vmspace *vm = p->p_vmspace; 1564 struct nameidata nd; 1565 struct vattr vattr; 1566 struct coredump_iostate io; 1567 int error, len, incrash = 0; 1568 char *name; 1569 const char *dir = "/var/crash"; 1570 1571 if (pr->ps_emul->e_coredump == NULL) 1572 return (EINVAL); 1573 1574 atomic_setbits_int(&pr->ps_flags, PS_COREDUMP); 1575 1576 /* Don't dump if will exceed file size limit. */ 1577 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE)) 1578 return (EFBIG); 1579 1580 name = pool_get(&namei_pool, PR_WAITOK); 1581 1582 /* 1583 * If the process has inconsistent uids, nosuidcoredump 1584 * determines coredump placement policy. 1585 */ 1586 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1587 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1588 if (nosuidcoredump == 3) { 1589 /* 1590 * If the program directory does not exist, dumps of 1591 * that core will silently fail. 1592 */ 1593 len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core", 1594 dir, pr->ps_comm, pr->ps_pid); 1595 incrash = KERNELPATH; 1596 } else if (nosuidcoredump == 2) { 1597 len = snprintf(name, MAXPATHLEN, "%s/%s.core", 1598 dir, pr->ps_comm); 1599 incrash = KERNELPATH; 1600 } else { 1601 pool_put(&namei_pool, name); 1602 return (EPERM); 1603 } 1604 } else 1605 len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm); 1606 1607 if (len >= MAXPATHLEN) { 1608 pool_put(&namei_pool, name); 1609 return (EACCES); 1610 } 1611 1612 /* 1613 * Control the UID used to write out. The normal case uses 1614 * the real UID. If the sugid case is going to write into the 1615 * controlled directory, we do so as root. 1616 */ 1617 if (incrash == 0) { 1618 cred = crdup(cred); 1619 cred->cr_uid = cred->cr_ruid; 1620 cred->cr_gid = cred->cr_rgid; 1621 } else { 1622 if (p->p_fd->fd_rdir) { 1623 vrele(p->p_fd->fd_rdir); 1624 p->p_fd->fd_rdir = NULL; 1625 } 1626 p->p_ucred = crdup(p->p_ucred); 1627 crfree(cred); 1628 cred = p->p_ucred; 1629 crhold(cred); 1630 cred->cr_uid = 0; 1631 cred->cr_gid = 0; 1632 } 1633 1634 /* incrash should be 0 or KERNELPATH only */ 1635 NDINIT(&nd, 0, incrash, UIO_SYSSPACE, name, p); 1636 1637 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK, 1638 S_IRUSR | S_IWUSR); 1639 1640 if (error) 1641 goto out; 1642 1643 /* 1644 * Don't dump to non-regular files, files with links, or files 1645 * owned by someone else. 1646 */ 1647 vp = nd.ni_vp; 1648 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1649 VOP_UNLOCK(vp); 1650 vn_close(vp, FWRITE, cred, p); 1651 goto out; 1652 } 1653 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1654 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1655 vattr.va_uid != cred->cr_uid) { 1656 error = EACCES; 1657 VOP_UNLOCK(vp); 1658 vn_close(vp, FWRITE, cred, p); 1659 goto out; 1660 } 1661 VATTR_NULL(&vattr); 1662 vattr.va_size = 0; 1663 VOP_SETATTR(vp, &vattr, cred, p); 1664 pr->ps_acflag |= ACORE; 1665 1666 io.io_proc = p; 1667 io.io_vp = vp; 1668 io.io_cred = cred; 1669 io.io_offset = 0; 1670 VOP_UNLOCK(vp); 1671 vref(vp); 1672 error = vn_close(vp, FWRITE, cred, p); 1673 if (error == 0) 1674 error = (*pr->ps_emul->e_coredump)(p, &io); 1675 vrele(vp); 1676 out: 1677 crfree(cred); 1678 pool_put(&namei_pool, name); 1679 return (error); 1680 #endif 1681 } 1682 1683 #ifndef SMALL_KERNEL 1684 int 1685 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 1686 { 1687 struct coredump_iostate *io = cookie; 1688 off_t coffset = 0; 1689 size_t csize; 1690 int chunk, error; 1691 1692 csize = len; 1693 do { 1694 if (sigmask(SIGKILL) & 1695 (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist)) 1696 return (EINTR); 1697 1698 /* Rest of the loop sleeps with lock held, so... */ 1699 yield(); 1700 1701 chunk = MIN(csize, MAXPHYS); 1702 error = vn_rdwr(UIO_WRITE, io->io_vp, 1703 (caddr_t)data + coffset, chunk, 1704 io->io_offset + coffset, segflg, 1705 IO_UNIT, io->io_cred, NULL, io->io_proc); 1706 if (error) { 1707 struct process *pr = io->io_proc->p_p; 1708 1709 if (error == ENOSPC) 1710 log(LOG_ERR, 1711 "coredump of %s(%d) failed, filesystem full\n", 1712 pr->ps_comm, pr->ps_pid); 1713 else 1714 log(LOG_ERR, 1715 "coredump of %s(%d), write failed: errno %d\n", 1716 pr->ps_comm, pr->ps_pid, error); 1717 return (error); 1718 } 1719 1720 coffset += chunk; 1721 csize -= chunk; 1722 } while (csize > 0); 1723 1724 io->io_offset += len; 1725 return (0); 1726 } 1727 1728 void 1729 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1730 { 1731 struct coredump_iostate *io = cookie; 1732 1733 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1734 } 1735 1736 #endif /* !SMALL_KERNEL */ 1737 1738 /* 1739 * Nonexistent system call-- signal process (may want to handle it). 1740 * Flag error in case process won't see signal immediately (blocked or ignored). 1741 */ 1742 int 1743 sys_nosys(struct proc *p, void *v, register_t *retval) 1744 { 1745 1746 ptsignal(p, SIGSYS, STHREAD); 1747 return (ENOSYS); 1748 } 1749 1750 int 1751 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1752 { 1753 static int sigwaitsleep; 1754 struct sys___thrsigdivert_args /* { 1755 syscallarg(sigset_t) sigmask; 1756 syscallarg(siginfo_t *) info; 1757 syscallarg(const struct timespec *) timeout; 1758 } */ *uap = v; 1759 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1760 siginfo_t si; 1761 uint64_t nsecs = INFSLP; 1762 int timeinvalid = 0; 1763 int error = 0; 1764 1765 memset(&si, 0, sizeof(si)); 1766 1767 if (SCARG(uap, timeout) != NULL) { 1768 struct timespec ts; 1769 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1770 return (error); 1771 #ifdef KTRACE 1772 if (KTRPOINT(p, KTR_STRUCT)) 1773 ktrreltimespec(p, &ts); 1774 #endif 1775 if (!timespecisvalid(&ts)) 1776 timeinvalid = 1; 1777 else 1778 nsecs = TIMESPEC_TO_NSEC(&ts); 1779 } 1780 1781 dosigsuspend(p, p->p_sigmask &~ mask); 1782 for (;;) { 1783 si.si_signo = cursig(p); 1784 if (si.si_signo != 0) { 1785 sigset_t smask = sigmask(si.si_signo); 1786 if (smask & mask) { 1787 atomic_clearbits_int(&p->p_siglist, smask); 1788 error = 0; 1789 break; 1790 } 1791 } 1792 1793 /* per-POSIX, delay this error until after the above */ 1794 if (timeinvalid) 1795 error = EINVAL; 1796 /* per-POSIX, return immediatly if timeout is zero-valued */ 1797 if (nsecs == 0) 1798 error = EAGAIN; 1799 1800 if (error != 0) 1801 break; 1802 1803 error = tsleep_nsec(&sigwaitsleep, PPAUSE|PCATCH, "sigwait", 1804 nsecs); 1805 } 1806 1807 if (error == 0) { 1808 *retval = si.si_signo; 1809 if (SCARG(uap, info) != NULL) 1810 error = copyout(&si, SCARG(uap, info), sizeof(si)); 1811 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) { 1812 /* 1813 * Restarting is wrong if there's a timeout, as it'll be 1814 * for the same interval again 1815 */ 1816 error = EINTR; 1817 } 1818 1819 return (error); 1820 } 1821 1822 void 1823 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val) 1824 { 1825 memset(si, 0, sizeof(*si)); 1826 1827 si->si_signo = sig; 1828 si->si_code = code; 1829 if (code == SI_USER) { 1830 si->si_value = val; 1831 } else { 1832 switch (sig) { 1833 case SIGSEGV: 1834 case SIGILL: 1835 case SIGBUS: 1836 case SIGFPE: 1837 si->si_addr = val.sival_ptr; 1838 si->si_trapno = trapno; 1839 break; 1840 case SIGXFSZ: 1841 break; 1842 } 1843 } 1844 } 1845 1846 int 1847 filt_sigattach(struct knote *kn) 1848 { 1849 struct process *pr = curproc->p_p; 1850 int s; 1851 1852 if (kn->kn_id >= NSIG) 1853 return EINVAL; 1854 1855 kn->kn_ptr.p_process = pr; 1856 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1857 1858 s = splhigh(); 1859 klist_insert_locked(&pr->ps_klist, kn); 1860 splx(s); 1861 1862 return (0); 1863 } 1864 1865 void 1866 filt_sigdetach(struct knote *kn) 1867 { 1868 struct process *pr = kn->kn_ptr.p_process; 1869 int s; 1870 1871 s = splhigh(); 1872 klist_remove_locked(&pr->ps_klist, kn); 1873 splx(s); 1874 } 1875 1876 /* 1877 * signal knotes are shared with proc knotes, so we apply a mask to 1878 * the hint in order to differentiate them from process hints. This 1879 * could be avoided by using a signal-specific knote list, but probably 1880 * isn't worth the trouble. 1881 */ 1882 int 1883 filt_signal(struct knote *kn, long hint) 1884 { 1885 1886 if (hint & NOTE_SIGNAL) { 1887 hint &= ~NOTE_SIGNAL; 1888 1889 if (kn->kn_id == hint) 1890 kn->kn_data++; 1891 } 1892 return (kn->kn_data != 0); 1893 } 1894 1895 void 1896 userret(struct proc *p) 1897 { 1898 int signum; 1899 1900 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */ 1901 if (p->p_flag & P_PROFPEND) { 1902 atomic_clearbits_int(&p->p_flag, P_PROFPEND); 1903 KERNEL_LOCK(); 1904 psignal(p, SIGPROF); 1905 KERNEL_UNLOCK(); 1906 } 1907 if (p->p_flag & P_ALRMPEND) { 1908 atomic_clearbits_int(&p->p_flag, P_ALRMPEND); 1909 KERNEL_LOCK(); 1910 psignal(p, SIGVTALRM); 1911 KERNEL_UNLOCK(); 1912 } 1913 1914 if (SIGPENDING(p) != 0) { 1915 KERNEL_LOCK(); 1916 while ((signum = cursig(p)) != 0) 1917 postsig(p, signum); 1918 KERNEL_UNLOCK(); 1919 } 1920 1921 /* 1922 * If P_SIGSUSPEND is still set here, then we still need to restore 1923 * the original sigmask before returning to userspace. Also, this 1924 * might unmask some pending signals, so we need to check a second 1925 * time for signals to post. 1926 */ 1927 if (p->p_flag & P_SIGSUSPEND) { 1928 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1929 p->p_sigmask = p->p_oldmask; 1930 1931 KERNEL_LOCK(); 1932 while ((signum = cursig(p)) != 0) 1933 postsig(p, signum); 1934 KERNEL_UNLOCK(); 1935 } 1936 1937 if (p->p_flag & P_SUSPSINGLE) 1938 single_thread_check(p, 0); 1939 1940 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning"); 1941 1942 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri; 1943 } 1944 1945 int 1946 single_thread_check_locked(struct proc *p, int deep, int s) 1947 { 1948 struct process *pr = p->p_p; 1949 1950 SCHED_ASSERT_LOCKED(); 1951 1952 if (pr->ps_single != NULL && pr->ps_single != p) { 1953 do { 1954 /* if we're in deep, we need to unwind to the edge */ 1955 if (deep) { 1956 if (pr->ps_flags & PS_SINGLEUNWIND) 1957 return (ERESTART); 1958 if (pr->ps_flags & PS_SINGLEEXIT) 1959 return (EINTR); 1960 } 1961 1962 if (pr->ps_single == NULL) 1963 continue; 1964 1965 if (atomic_dec_int_nv(&pr->ps_singlecount) == 0) 1966 wakeup(&pr->ps_singlecount); 1967 1968 if (pr->ps_flags & PS_SINGLEEXIT) { 1969 SCHED_UNLOCK(s); 1970 KERNEL_LOCK(); 1971 exit1(p, 0, 0, EXIT_THREAD_NOCHECK); 1972 /* NOTREACHED */ 1973 } 1974 1975 /* not exiting and don't need to unwind, so suspend */ 1976 p->p_stat = SSTOP; 1977 mi_switch(); 1978 } while (pr->ps_single != NULL); 1979 } 1980 1981 return (0); 1982 } 1983 1984 int 1985 single_thread_check(struct proc *p, int deep) 1986 { 1987 int s, error; 1988 1989 SCHED_LOCK(s); 1990 error = single_thread_check_locked(p, deep, s); 1991 SCHED_UNLOCK(s); 1992 1993 return error; 1994 } 1995 1996 /* 1997 * Stop other threads in the process. The mode controls how and 1998 * where the other threads should stop: 1999 * - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit 2000 * (by setting to SINGLE_EXIT) or be released (via single_thread_clear()) 2001 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit 2002 * or released as with SINGLE_SUSPEND 2003 * - SINGLE_EXIT: unwind to kernel boundary and exit 2004 */ 2005 int 2006 single_thread_set(struct proc *p, enum single_thread_mode mode, int wait) 2007 { 2008 struct process *pr = p->p_p; 2009 struct proc *q; 2010 int error, s; 2011 2012 KASSERT(curproc == p); 2013 2014 SCHED_LOCK(s); 2015 error = single_thread_check_locked(p, (mode == SINGLE_UNWIND), s); 2016 if (error) { 2017 SCHED_UNLOCK(s); 2018 return error; 2019 } 2020 2021 switch (mode) { 2022 case SINGLE_SUSPEND: 2023 break; 2024 case SINGLE_UNWIND: 2025 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2026 break; 2027 case SINGLE_EXIT: 2028 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT); 2029 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2030 break; 2031 #ifdef DIAGNOSTIC 2032 default: 2033 panic("single_thread_mode = %d", mode); 2034 #endif 2035 } 2036 pr->ps_singlecount = 0; 2037 membar_producer(); 2038 pr->ps_single = p; 2039 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2040 if (q == p) 2041 continue; 2042 if (q->p_flag & P_WEXIT) { 2043 if (mode == SINGLE_EXIT) { 2044 if (q->p_stat == SSTOP) { 2045 setrunnable(q); 2046 atomic_inc_int(&pr->ps_singlecount); 2047 } 2048 } 2049 continue; 2050 } 2051 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE); 2052 switch (q->p_stat) { 2053 case SIDL: 2054 case SRUN: 2055 atomic_inc_int(&pr->ps_singlecount); 2056 break; 2057 case SSLEEP: 2058 /* if it's not interruptible, then just have to wait */ 2059 if (q->p_flag & P_SINTR) { 2060 /* merely need to suspend? just stop it */ 2061 if (mode == SINGLE_SUSPEND) { 2062 q->p_stat = SSTOP; 2063 break; 2064 } 2065 /* need to unwind or exit, so wake it */ 2066 setrunnable(q); 2067 } 2068 atomic_inc_int(&pr->ps_singlecount); 2069 break; 2070 case SSTOP: 2071 if (mode == SINGLE_EXIT) { 2072 setrunnable(q); 2073 atomic_inc_int(&pr->ps_singlecount); 2074 } 2075 break; 2076 case SDEAD: 2077 break; 2078 case SONPROC: 2079 atomic_inc_int(&pr->ps_singlecount); 2080 signotify(q); 2081 break; 2082 } 2083 } 2084 SCHED_UNLOCK(s); 2085 2086 if (wait) 2087 single_thread_wait(pr, 1); 2088 2089 return 0; 2090 } 2091 2092 /* 2093 * Wait for other threads to stop. If recheck is false then the function 2094 * returns non-zero if the caller needs to restart the check else 0 is 2095 * returned. If recheck is true the return value is always 0. 2096 */ 2097 int 2098 single_thread_wait(struct process *pr, int recheck) 2099 { 2100 struct sleep_state sls; 2101 int wait; 2102 2103 /* wait until they're all suspended */ 2104 wait = pr->ps_singlecount > 0; 2105 while (wait) { 2106 sleep_setup(&sls, &pr->ps_singlecount, PWAIT, "suspend", 0); 2107 wait = pr->ps_singlecount > 0; 2108 sleep_finish(&sls, wait); 2109 if (!recheck) 2110 break; 2111 } 2112 2113 return wait; 2114 } 2115 2116 void 2117 single_thread_clear(struct proc *p, int flag) 2118 { 2119 struct process *pr = p->p_p; 2120 struct proc *q; 2121 int s; 2122 2123 KASSERT(pr->ps_single == p); 2124 KASSERT(curproc == p); 2125 2126 SCHED_LOCK(s); 2127 pr->ps_single = NULL; 2128 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); 2129 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2130 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0) 2131 continue; 2132 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE); 2133 2134 /* 2135 * if the thread was only stopped for single threading 2136 * then clearing that either makes it runnable or puts 2137 * it back into some sleep queue 2138 */ 2139 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) { 2140 if (q->p_wchan == NULL) 2141 setrunnable(q); 2142 else 2143 q->p_stat = SSLEEP; 2144 } 2145 } 2146 SCHED_UNLOCK(s); 2147 } 2148 2149 void 2150 sigio_del(struct sigiolst *rmlist) 2151 { 2152 struct sigio *sigio; 2153 2154 while ((sigio = LIST_FIRST(rmlist)) != NULL) { 2155 LIST_REMOVE(sigio, sio_pgsigio); 2156 crfree(sigio->sio_ucred); 2157 free(sigio, M_SIGIO, sizeof(*sigio)); 2158 } 2159 } 2160 2161 void 2162 sigio_unlink(struct sigio_ref *sir, struct sigiolst *rmlist) 2163 { 2164 struct sigio *sigio; 2165 2166 MUTEX_ASSERT_LOCKED(&sigio_lock); 2167 2168 sigio = sir->sir_sigio; 2169 if (sigio != NULL) { 2170 KASSERT(sigio->sio_myref == sir); 2171 sir->sir_sigio = NULL; 2172 2173 if (sigio->sio_pgid > 0) 2174 sigio->sio_proc = NULL; 2175 else 2176 sigio->sio_pgrp = NULL; 2177 LIST_REMOVE(sigio, sio_pgsigio); 2178 2179 LIST_INSERT_HEAD(rmlist, sigio, sio_pgsigio); 2180 } 2181 } 2182 2183 void 2184 sigio_free(struct sigio_ref *sir) 2185 { 2186 struct sigiolst rmlist; 2187 2188 if (sir->sir_sigio == NULL) 2189 return; 2190 2191 LIST_INIT(&rmlist); 2192 2193 mtx_enter(&sigio_lock); 2194 sigio_unlink(sir, &rmlist); 2195 mtx_leave(&sigio_lock); 2196 2197 sigio_del(&rmlist); 2198 } 2199 2200 void 2201 sigio_freelist(struct sigiolst *sigiolst) 2202 { 2203 struct sigiolst rmlist; 2204 struct sigio *sigio; 2205 2206 if (LIST_EMPTY(sigiolst)) 2207 return; 2208 2209 LIST_INIT(&rmlist); 2210 2211 mtx_enter(&sigio_lock); 2212 while ((sigio = LIST_FIRST(sigiolst)) != NULL) 2213 sigio_unlink(sigio->sio_myref, &rmlist); 2214 mtx_leave(&sigio_lock); 2215 2216 sigio_del(&rmlist); 2217 } 2218 2219 int 2220 sigio_setown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2221 { 2222 struct sigiolst rmlist; 2223 struct proc *p = curproc; 2224 struct pgrp *pgrp = NULL; 2225 struct process *pr = NULL; 2226 struct sigio *sigio; 2227 int error; 2228 pid_t pgid = *(int *)data; 2229 2230 if (pgid == 0) { 2231 sigio_free(sir); 2232 return (0); 2233 } 2234 2235 if (cmd == TIOCSPGRP) { 2236 if (pgid < 0) 2237 return (EINVAL); 2238 pgid = -pgid; 2239 } 2240 2241 sigio = malloc(sizeof(*sigio), M_SIGIO, M_WAITOK); 2242 sigio->sio_pgid = pgid; 2243 sigio->sio_ucred = crhold(p->p_ucred); 2244 sigio->sio_myref = sir; 2245 2246 LIST_INIT(&rmlist); 2247 2248 /* 2249 * The kernel lock, and not sleeping between prfind()/pgfind() and 2250 * linking of the sigio ensure that the process or process group does 2251 * not disappear unexpectedly. 2252 */ 2253 KERNEL_LOCK(); 2254 mtx_enter(&sigio_lock); 2255 2256 if (pgid > 0) { 2257 pr = prfind(pgid); 2258 if (pr == NULL) { 2259 error = ESRCH; 2260 goto fail; 2261 } 2262 2263 /* 2264 * Policy - Don't allow a process to FSETOWN a process 2265 * in another session. 2266 * 2267 * Remove this test to allow maximum flexibility or 2268 * restrict FSETOWN to the current process or process 2269 * group for maximum safety. 2270 */ 2271 if (pr->ps_session != p->p_p->ps_session) { 2272 error = EPERM; 2273 goto fail; 2274 } 2275 2276 if ((pr->ps_flags & PS_EXITING) != 0) { 2277 error = ESRCH; 2278 goto fail; 2279 } 2280 } else /* if (pgid < 0) */ { 2281 pgrp = pgfind(-pgid); 2282 if (pgrp == NULL) { 2283 error = ESRCH; 2284 goto fail; 2285 } 2286 2287 /* 2288 * Policy - Don't allow a process to FSETOWN a process 2289 * in another session. 2290 * 2291 * Remove this test to allow maximum flexibility or 2292 * restrict FSETOWN to the current process or process 2293 * group for maximum safety. 2294 */ 2295 if (pgrp->pg_session != p->p_p->ps_session) { 2296 error = EPERM; 2297 goto fail; 2298 } 2299 } 2300 2301 if (pgid > 0) { 2302 sigio->sio_proc = pr; 2303 LIST_INSERT_HEAD(&pr->ps_sigiolst, sigio, sio_pgsigio); 2304 } else { 2305 sigio->sio_pgrp = pgrp; 2306 LIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); 2307 } 2308 2309 sigio_unlink(sir, &rmlist); 2310 sir->sir_sigio = sigio; 2311 2312 mtx_leave(&sigio_lock); 2313 KERNEL_UNLOCK(); 2314 2315 sigio_del(&rmlist); 2316 2317 return (0); 2318 2319 fail: 2320 mtx_leave(&sigio_lock); 2321 KERNEL_UNLOCK(); 2322 2323 crfree(sigio->sio_ucred); 2324 free(sigio, M_SIGIO, sizeof(*sigio)); 2325 2326 return (error); 2327 } 2328 2329 void 2330 sigio_getown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2331 { 2332 struct sigio *sigio; 2333 pid_t pgid = 0; 2334 2335 mtx_enter(&sigio_lock); 2336 sigio = sir->sir_sigio; 2337 if (sigio != NULL) 2338 pgid = sigio->sio_pgid; 2339 mtx_leave(&sigio_lock); 2340 2341 if (cmd == TIOCGPGRP) 2342 pgid = -pgid; 2343 2344 *(int *)data = pgid; 2345 } 2346 2347 void 2348 sigio_copy(struct sigio_ref *dst, struct sigio_ref *src) 2349 { 2350 struct sigiolst rmlist; 2351 struct sigio *newsigio, *sigio; 2352 2353 sigio_free(dst); 2354 2355 if (src->sir_sigio == NULL) 2356 return; 2357 2358 newsigio = malloc(sizeof(*newsigio), M_SIGIO, M_WAITOK); 2359 LIST_INIT(&rmlist); 2360 2361 mtx_enter(&sigio_lock); 2362 2363 sigio = src->sir_sigio; 2364 if (sigio == NULL) { 2365 mtx_leave(&sigio_lock); 2366 free(newsigio, M_SIGIO, sizeof(*newsigio)); 2367 return; 2368 } 2369 2370 newsigio->sio_pgid = sigio->sio_pgid; 2371 newsigio->sio_ucred = crhold(sigio->sio_ucred); 2372 newsigio->sio_myref = dst; 2373 if (newsigio->sio_pgid > 0) { 2374 newsigio->sio_proc = sigio->sio_proc; 2375 LIST_INSERT_HEAD(&newsigio->sio_proc->ps_sigiolst, newsigio, 2376 sio_pgsigio); 2377 } else { 2378 newsigio->sio_pgrp = sigio->sio_pgrp; 2379 LIST_INSERT_HEAD(&newsigio->sio_pgrp->pg_sigiolst, newsigio, 2380 sio_pgsigio); 2381 } 2382 2383 sigio_unlink(dst, &rmlist); 2384 dst->sir_sigio = newsigio; 2385 2386 mtx_leave(&sigio_lock); 2387 2388 sigio_del(&rmlist); 2389 } 2390