1 /* $OpenBSD: kern_sig.c,v 1.281 2021/05/10 18:01:24 mpi 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 = 0; 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 process 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 struct process *pr = p->p_p; 523 struct sigacts *ps = pr->ps_sigacts; 524 525 dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask); 526 while (tsleep_nsec(ps, PPAUSE|PCATCH, "sigsusp", INFSLP) == 0) 527 /* void */; 528 /* always return EINTR rather than ERESTART... */ 529 return (EINTR); 530 } 531 532 int 533 sigonstack(size_t stack) 534 { 535 const struct sigaltstack *ss = &curproc->p_sigstk; 536 537 return (ss->ss_flags & SS_DISABLE ? 0 : 538 (stack - (size_t)ss->ss_sp < ss->ss_size)); 539 } 540 541 int 542 sys_sigaltstack(struct proc *p, void *v, register_t *retval) 543 { 544 struct sys_sigaltstack_args /* { 545 syscallarg(const struct sigaltstack *) nss; 546 syscallarg(struct sigaltstack *) oss; 547 } */ *uap = v; 548 struct sigaltstack ss; 549 const struct sigaltstack *nss; 550 struct sigaltstack *oss; 551 int onstack = sigonstack(PROC_STACK(p)); 552 int error; 553 554 nss = SCARG(uap, nss); 555 oss = SCARG(uap, oss); 556 557 if (oss != NULL) { 558 ss = p->p_sigstk; 559 if (onstack) 560 ss.ss_flags |= SS_ONSTACK; 561 if ((error = copyout(&ss, oss, sizeof(ss)))) 562 return (error); 563 } 564 if (nss == NULL) 565 return (0); 566 error = copyin(nss, &ss, sizeof(ss)); 567 if (error) 568 return (error); 569 if (onstack) 570 return (EPERM); 571 if (ss.ss_flags & ~SS_DISABLE) 572 return (EINVAL); 573 if (ss.ss_flags & SS_DISABLE) { 574 p->p_sigstk.ss_flags = ss.ss_flags; 575 return (0); 576 } 577 if (ss.ss_size < MINSIGSTKSZ) 578 return (ENOMEM); 579 580 error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size); 581 if (error) 582 return (error); 583 584 p->p_sigstk = ss; 585 return (0); 586 } 587 588 int 589 sys_kill(struct proc *cp, void *v, register_t *retval) 590 { 591 struct sys_kill_args /* { 592 syscallarg(int) pid; 593 syscallarg(int) signum; 594 } */ *uap = v; 595 struct process *pr; 596 int pid = SCARG(uap, pid); 597 int signum = SCARG(uap, signum); 598 int error; 599 int zombie = 0; 600 601 if ((error = pledge_kill(cp, pid)) != 0) 602 return (error); 603 if (((u_int)signum) >= NSIG) 604 return (EINVAL); 605 if (pid > 0) { 606 if ((pr = prfind(pid)) == NULL) { 607 if ((pr = zombiefind(pid)) == NULL) 608 return (ESRCH); 609 else 610 zombie = 1; 611 } 612 if (!cansignal(cp, pr, signum)) 613 return (EPERM); 614 615 /* kill single process */ 616 if (signum && !zombie) 617 prsignal(pr, signum); 618 return (0); 619 } 620 switch (pid) { 621 case -1: /* broadcast signal */ 622 return (killpg1(cp, signum, 0, 1)); 623 case 0: /* signal own process group */ 624 return (killpg1(cp, signum, 0, 0)); 625 default: /* negative explicit process group */ 626 return (killpg1(cp, signum, -pid, 0)); 627 } 628 } 629 630 int 631 sys_thrkill(struct proc *cp, void *v, register_t *retval) 632 { 633 struct sys_thrkill_args /* { 634 syscallarg(pid_t) tid; 635 syscallarg(int) signum; 636 syscallarg(void *) tcb; 637 } */ *uap = v; 638 struct proc *p; 639 int tid = SCARG(uap, tid); 640 int signum = SCARG(uap, signum); 641 void *tcb; 642 643 if (((u_int)signum) >= NSIG) 644 return (EINVAL); 645 if (tid > THREAD_PID_OFFSET) { 646 if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL) 647 return (ESRCH); 648 649 /* can only kill threads in the same process */ 650 if (p->p_p != cp->p_p) 651 return (ESRCH); 652 } else if (tid == 0) 653 p = cp; 654 else 655 return (EINVAL); 656 657 /* optionally require the target thread to have the given tcb addr */ 658 tcb = SCARG(uap, tcb); 659 if (tcb != NULL && tcb != TCB_GET(p)) 660 return (ESRCH); 661 662 if (signum) 663 ptsignal(p, signum, STHREAD); 664 return (0); 665 } 666 667 /* 668 * Common code for kill process group/broadcast kill. 669 * cp is calling process. 670 */ 671 int 672 killpg1(struct proc *cp, int signum, int pgid, int all) 673 { 674 struct process *pr; 675 struct pgrp *pgrp; 676 int nfound = 0; 677 678 if (all) { 679 /* 680 * broadcast 681 */ 682 LIST_FOREACH(pr, &allprocess, ps_list) { 683 if (pr->ps_pid <= 1 || 684 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) || 685 pr == cp->p_p || !cansignal(cp, pr, signum)) 686 continue; 687 nfound++; 688 if (signum) 689 prsignal(pr, signum); 690 } 691 } else { 692 if (pgid == 0) 693 /* 694 * zero pgid means send to my process group. 695 */ 696 pgrp = cp->p_p->ps_pgrp; 697 else { 698 pgrp = pgfind(pgid); 699 if (pgrp == NULL) 700 return (ESRCH); 701 } 702 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 703 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM || 704 !cansignal(cp, pr, signum)) 705 continue; 706 nfound++; 707 if (signum) 708 prsignal(pr, signum); 709 } 710 } 711 return (nfound ? 0 : ESRCH); 712 } 713 714 #define CANDELIVER(uid, euid, pr) \ 715 (euid == 0 || \ 716 (uid) == (pr)->ps_ucred->cr_ruid || \ 717 (uid) == (pr)->ps_ucred->cr_svuid || \ 718 (uid) == (pr)->ps_ucred->cr_uid || \ 719 (euid) == (pr)->ps_ucred->cr_ruid || \ 720 (euid) == (pr)->ps_ucred->cr_svuid || \ 721 (euid) == (pr)->ps_ucred->cr_uid) 722 723 #define CANSIGIO(cr, pr) \ 724 CANDELIVER((cr)->cr_ruid, (cr)->cr_uid, (pr)) 725 726 /* 727 * Send a signal to a process group. If checktty is 1, 728 * limit to members which have a controlling terminal. 729 */ 730 void 731 pgsignal(struct pgrp *pgrp, int signum, int checkctty) 732 { 733 struct process *pr; 734 735 if (pgrp) 736 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) 737 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT) 738 prsignal(pr, signum); 739 } 740 741 /* 742 * Send a SIGIO or SIGURG signal to a process or process group using stored 743 * credentials rather than those of the current process. 744 */ 745 void 746 pgsigio(struct sigio_ref *sir, int sig, int checkctty) 747 { 748 struct process *pr; 749 struct sigio *sigio; 750 751 if (sir->sir_sigio == NULL) 752 return; 753 754 KERNEL_LOCK(); 755 mtx_enter(&sigio_lock); 756 sigio = sir->sir_sigio; 757 if (sigio == NULL) 758 goto out; 759 if (sigio->sio_pgid > 0) { 760 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc)) 761 prsignal(sigio->sio_proc, sig); 762 } else if (sigio->sio_pgid < 0) { 763 LIST_FOREACH(pr, &sigio->sio_pgrp->pg_members, ps_pglist) { 764 if (CANSIGIO(sigio->sio_ucred, pr) && 765 (checkctty == 0 || (pr->ps_flags & PS_CONTROLT))) 766 prsignal(pr, sig); 767 } 768 } 769 out: 770 mtx_leave(&sigio_lock); 771 KERNEL_UNLOCK(); 772 } 773 774 /* 775 * Recalculate the signal mask and reset the signal disposition after 776 * usermode frame for delivery is formed. 777 */ 778 void 779 postsig_done(struct proc *p, int signum, struct sigacts *ps) 780 { 781 int mask = sigmask(signum); 782 783 KERNEL_ASSERT_LOCKED(); 784 785 p->p_ru.ru_nsignals++; 786 atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]); 787 if ((ps->ps_sigreset & mask) != 0) { 788 ps->ps_sigcatch &= ~mask; 789 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE) 790 ps->ps_sigignore |= mask; 791 ps->ps_sigact[signum] = SIG_DFL; 792 } 793 } 794 795 /* 796 * Send a signal caused by a trap to the current thread 797 * If it will be caught immediately, deliver it with correct code. 798 * Otherwise, post it normally. 799 */ 800 void 801 trapsignal(struct proc *p, int signum, u_long trapno, int code, 802 union sigval sigval) 803 { 804 struct process *pr = p->p_p; 805 struct sigacts *ps = pr->ps_sigacts; 806 int mask; 807 808 KERNEL_LOCK(); 809 switch (signum) { 810 case SIGILL: 811 case SIGBUS: 812 case SIGSEGV: 813 pr->ps_acflag |= ATRAP; 814 break; 815 } 816 817 mask = sigmask(signum); 818 if ((pr->ps_flags & PS_TRACED) == 0 && 819 (ps->ps_sigcatch & mask) != 0 && 820 (p->p_sigmask & mask) == 0) { 821 siginfo_t si; 822 initsiginfo(&si, signum, trapno, code, sigval); 823 #ifdef KTRACE 824 if (KTRPOINT(p, KTR_PSIG)) { 825 ktrpsig(p, signum, ps->ps_sigact[signum], 826 p->p_sigmask, code, &si); 827 } 828 #endif 829 if (sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si)) { 830 sigexit(p, SIGILL); 831 /* NOTREACHED */ 832 } 833 postsig_done(p, signum, ps); 834 } else { 835 p->p_sisig = signum; 836 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 837 p->p_sicode = code; 838 p->p_sigval = sigval; 839 840 /* 841 * Signals like SIGBUS and SIGSEGV should not, when 842 * generated by the kernel, be ignorable or blockable. 843 * If it is and we're not being traced, then just kill 844 * the process. 845 */ 846 if ((pr->ps_flags & PS_TRACED) == 0 && 847 (sigprop[signum] & SA_KILL) && 848 ((p->p_sigmask & mask) || (ps->ps_sigignore & mask))) 849 sigexit(p, signum); 850 ptsignal(p, signum, STHREAD); 851 } 852 KERNEL_UNLOCK(); 853 } 854 855 /* 856 * Send the signal to the process. If the signal has an action, the action 857 * is usually performed by the target process rather than the caller; we add 858 * the signal to the set of pending signals for the process. 859 * 860 * Exceptions: 861 * o When a stop signal is sent to a sleeping process that takes the 862 * default action, the process is stopped without awakening it. 863 * o SIGCONT restarts stopped processes (or puts them back to sleep) 864 * regardless of the signal action (eg, blocked or ignored). 865 * 866 * Other ignored signals are discarded immediately. 867 */ 868 void 869 psignal(struct proc *p, int signum) 870 { 871 ptsignal(p, signum, SPROCESS); 872 } 873 874 /* 875 * type = SPROCESS process signal, can be diverted (sigwait()) 876 * type = STHREAD thread signal, but should be propagated if unhandled 877 * type = SPROPAGATED propagated to this thread, so don't propagate again 878 */ 879 void 880 ptsignal(struct proc *p, int signum, enum signal_type type) 881 { 882 int s, prop; 883 sig_t action; 884 int mask; 885 int *siglist; 886 struct process *pr = p->p_p; 887 struct proc *q; 888 int wakeparent = 0; 889 890 KERNEL_ASSERT_LOCKED(); 891 892 #ifdef DIAGNOSTIC 893 if ((u_int)signum >= NSIG || signum == 0) 894 panic("psignal signal number"); 895 #endif 896 897 /* Ignore signal if the target process is exiting */ 898 if (pr->ps_flags & PS_EXITING) 899 return; 900 901 mask = sigmask(signum); 902 903 if (type == SPROCESS) { 904 /* Accept SIGKILL to coredumping processes */ 905 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 906 atomic_setbits_int(&pr->ps_siglist, mask); 907 return; 908 } 909 910 /* 911 * If the current thread can process the signal 912 * immediately (it's unblocked) then have it take it. 913 */ 914 q = curproc; 915 if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 916 (q->p_sigmask & mask) == 0) 917 p = q; 918 else { 919 /* 920 * A process-wide signal can be diverted to a 921 * different thread that's in sigwait() for this 922 * signal. If there isn't such a thread, then 923 * pick a thread that doesn't have it blocked so 924 * that the stop/kill consideration isn't 925 * delayed. Otherwise, mark it pending on the 926 * main thread. 927 */ 928 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 929 /* ignore exiting threads */ 930 if (q->p_flag & P_WEXIT) 931 continue; 932 933 /* skip threads that have the signal blocked */ 934 if ((q->p_sigmask & mask) != 0) 935 continue; 936 937 /* okay, could send to this thread */ 938 p = q; 939 940 /* 941 * sigsuspend, sigwait, ppoll/pselect, etc? 942 * Definitely go to this thread, as it's 943 * already blocked in the kernel. 944 */ 945 if (q->p_flag & P_SIGSUSPEND) 946 break; 947 } 948 } 949 } 950 951 if (type != SPROPAGATED) 952 KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum); 953 954 prop = sigprop[signum]; 955 956 /* 957 * If proc is traced, always give parent a chance. 958 */ 959 if (pr->ps_flags & PS_TRACED) { 960 action = SIG_DFL; 961 } else { 962 /* 963 * If the signal is being ignored, 964 * then we forget about it immediately. 965 * (Note: we don't set SIGCONT in ps_sigignore, 966 * and if it is set to SIG_IGN, 967 * action will be SIG_DFL here.) 968 */ 969 if (pr->ps_sigacts->ps_sigignore & mask) 970 return; 971 if (p->p_sigmask & mask) { 972 action = SIG_HOLD; 973 } else if (pr->ps_sigacts->ps_sigcatch & mask) { 974 action = SIG_CATCH; 975 } else { 976 action = SIG_DFL; 977 978 if (prop & SA_KILL && pr->ps_nice > NZERO) 979 pr->ps_nice = NZERO; 980 981 /* 982 * If sending a tty stop signal to a member of an 983 * orphaned process group, discard the signal here if 984 * the action is default; don't stop the process below 985 * if sleeping, and don't clear any pending SIGCONT. 986 */ 987 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 988 return; 989 } 990 } 991 /* 992 * If delivered to process, mark as pending there. Continue and stop 993 * signals will be propagated to all threads. So they are always 994 * marked at thread level. 995 */ 996 siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist; 997 if (prop & SA_CONT) { 998 siglist = &p->p_siglist; 999 atomic_clearbits_int(siglist, STOPSIGMASK); 1000 } 1001 if (prop & SA_STOP) { 1002 siglist = &p->p_siglist; 1003 atomic_clearbits_int(siglist, CONTSIGMASK); 1004 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 1005 } 1006 atomic_setbits_int(siglist, mask); 1007 1008 /* 1009 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 1010 */ 1011 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 1012 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 1013 if (q != p) 1014 ptsignal(q, signum, SPROPAGATED); 1015 1016 /* 1017 * Defer further processing for signals which are held, 1018 * except that stopped processes must be continued by SIGCONT. 1019 */ 1020 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 1021 return; 1022 1023 SCHED_LOCK(s); 1024 1025 switch (p->p_stat) { 1026 1027 case SSLEEP: 1028 /* 1029 * If process is sleeping uninterruptibly 1030 * we can't interrupt the sleep... the signal will 1031 * be noticed when the process returns through 1032 * trap() or syscall(). 1033 */ 1034 if ((p->p_flag & P_SINTR) == 0) 1035 goto out; 1036 /* 1037 * Process is sleeping and traced... make it runnable 1038 * so it can discover the signal in cursig() and stop 1039 * for the parent. 1040 */ 1041 if (pr->ps_flags & PS_TRACED) 1042 goto run; 1043 /* 1044 * If SIGCONT is default (or ignored) and process is 1045 * asleep, we are finished; the process should not 1046 * be awakened. 1047 */ 1048 if ((prop & SA_CONT) && action == SIG_DFL) { 1049 atomic_clearbits_int(siglist, mask); 1050 goto out; 1051 } 1052 /* 1053 * When a sleeping process receives a stop 1054 * signal, process immediately if possible. 1055 */ 1056 if ((prop & SA_STOP) && action == SIG_DFL) { 1057 /* 1058 * If a child holding parent blocked, 1059 * stopping could cause deadlock. 1060 */ 1061 if (pr->ps_flags & PS_PPWAIT) 1062 goto out; 1063 atomic_clearbits_int(siglist, mask); 1064 pr->ps_xsig = signum; 1065 proc_stop(p, 0); 1066 goto out; 1067 } 1068 /* 1069 * All other (caught or default) signals 1070 * cause the process to run. 1071 */ 1072 goto runfast; 1073 /*NOTREACHED*/ 1074 1075 case SSTOP: 1076 /* 1077 * If traced process is already stopped, 1078 * then no further action is necessary. 1079 */ 1080 if (pr->ps_flags & PS_TRACED) 1081 goto out; 1082 1083 /* 1084 * Kill signal always sets processes running. 1085 */ 1086 if (signum == SIGKILL) { 1087 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1088 goto runfast; 1089 } 1090 1091 if (prop & SA_CONT) { 1092 /* 1093 * If SIGCONT is default (or ignored), we continue the 1094 * process but don't leave the signal in p_siglist, as 1095 * it has no further action. If SIGCONT is held, we 1096 * continue the process and leave the signal in 1097 * p_siglist. If the process catches SIGCONT, let it 1098 * handle the signal itself. If it isn't waiting on 1099 * an event, then it goes back to run state. 1100 * Otherwise, process goes back to sleep state. 1101 */ 1102 atomic_setbits_int(&p->p_flag, P_CONTINUED); 1103 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1104 wakeparent = 1; 1105 if (action == SIG_DFL) 1106 atomic_clearbits_int(siglist, mask); 1107 if (action == SIG_CATCH) 1108 goto runfast; 1109 if (p->p_wchan == 0) 1110 goto run; 1111 p->p_stat = SSLEEP; 1112 goto out; 1113 } 1114 1115 if (prop & SA_STOP) { 1116 /* 1117 * Already stopped, don't need to stop again. 1118 * (If we did the shell could get confused.) 1119 */ 1120 atomic_clearbits_int(siglist, mask); 1121 goto out; 1122 } 1123 1124 /* 1125 * If process is sleeping interruptibly, then simulate a 1126 * wakeup so that when it is continued, it will be made 1127 * runnable and can look at the signal. But don't make 1128 * the process runnable, leave it stopped. 1129 */ 1130 if (p->p_flag & P_SINTR) 1131 unsleep(p); 1132 goto out; 1133 1134 case SONPROC: 1135 signotify(p); 1136 /* FALLTHROUGH */ 1137 default: 1138 /* 1139 * SRUN, SIDL, SDEAD do nothing with the signal, 1140 * other than kicking ourselves if we are running. 1141 * It will either never be noticed, or noticed very soon. 1142 */ 1143 goto out; 1144 } 1145 /*NOTREACHED*/ 1146 1147 runfast: 1148 /* 1149 * Raise priority to at least PUSER. 1150 */ 1151 if (p->p_usrpri > PUSER) 1152 p->p_usrpri = PUSER; 1153 run: 1154 setrunnable(p); 1155 out: 1156 SCHED_UNLOCK(s); 1157 if (wakeparent) 1158 wakeup(pr->ps_pptr); 1159 } 1160 1161 /* 1162 * Determine signal that should be delivered to process p, the current 1163 * process, 0 if none. 1164 * 1165 * If the current process has received a signal (should be caught or cause 1166 * termination, should interrupt current syscall), return the signal number. 1167 * Stop signals with default action are processed immediately, then cleared; 1168 * they aren't returned. This is checked after each entry to the system for 1169 * a syscall or trap. The normal call sequence is 1170 * 1171 * while (signum = cursig(curproc)) 1172 * postsig(signum); 1173 * 1174 * Assumes that if the P_SINTR flag is set, we're holding both the 1175 * kernel and scheduler locks. 1176 */ 1177 int 1178 cursig(struct proc *p) 1179 { 1180 struct process *pr = p->p_p; 1181 int sigpending, signum, mask, prop; 1182 int dolock = (p->p_flag & P_SINTR) == 0; 1183 int s; 1184 1185 KERNEL_ASSERT_LOCKED(); 1186 1187 sigpending = (p->p_siglist | pr->ps_siglist); 1188 if (sigpending == 0) 1189 return 0; 1190 1191 if (!ISSET(pr->ps_flags, PS_TRACED) && SIGPENDING(p) == 0) 1192 return 0; 1193 1194 for (;;) { 1195 mask = SIGPENDING(p); 1196 if (pr->ps_flags & PS_PPWAIT) 1197 mask &= ~STOPSIGMASK; 1198 if (mask == 0) /* no signal to send */ 1199 return (0); 1200 signum = ffs((long)mask); 1201 mask = sigmask(signum); 1202 atomic_clearbits_int(&p->p_siglist, mask); 1203 atomic_clearbits_int(&pr->ps_siglist, mask); 1204 1205 /* 1206 * We should see pending but ignored signals 1207 * only if PS_TRACED was on when they were posted. 1208 */ 1209 if (mask & pr->ps_sigacts->ps_sigignore && 1210 (pr->ps_flags & PS_TRACED) == 0) 1211 continue; 1212 1213 /* 1214 * If traced, always stop, and stay stopped until released 1215 * by the debugger. If our parent process is waiting for 1216 * us, don't hang as we could deadlock. 1217 */ 1218 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1219 signum != SIGKILL) { 1220 pr->ps_xsig = signum; 1221 1222 single_thread_set(p, SINGLE_SUSPEND, 0); 1223 1224 if (dolock) 1225 SCHED_LOCK(s); 1226 proc_stop(p, 1); 1227 if (dolock) 1228 SCHED_UNLOCK(s); 1229 1230 single_thread_clear(p, 0); 1231 1232 /* 1233 * If we are no longer being traced, or the parent 1234 * didn't give us a signal, look for more signals. 1235 */ 1236 if ((pr->ps_flags & PS_TRACED) == 0 || 1237 pr->ps_xsig == 0) 1238 continue; 1239 1240 /* 1241 * If the new signal is being masked, look for other 1242 * signals. 1243 */ 1244 signum = pr->ps_xsig; 1245 mask = sigmask(signum); 1246 if ((p->p_sigmask & mask) != 0) 1247 continue; 1248 1249 /* take the signal! */ 1250 atomic_clearbits_int(&p->p_siglist, mask); 1251 atomic_clearbits_int(&pr->ps_siglist, mask); 1252 } 1253 1254 prop = sigprop[signum]; 1255 1256 /* 1257 * Decide whether the signal should be returned. 1258 * Return the signal's number, or fall through 1259 * to clear it from the pending mask. 1260 */ 1261 switch ((long)pr->ps_sigacts->ps_sigact[signum]) { 1262 case (long)SIG_DFL: 1263 /* 1264 * Don't take default actions on system processes. 1265 */ 1266 if (pr->ps_pid <= 1) { 1267 #ifdef DIAGNOSTIC 1268 /* 1269 * Are you sure you want to ignore SIGSEGV 1270 * in init? XXX 1271 */ 1272 printf("Process (pid %d) got signal" 1273 " %d\n", pr->ps_pid, signum); 1274 #endif 1275 break; /* == ignore */ 1276 } 1277 /* 1278 * If there is a pending stop signal to process 1279 * with default action, stop here, 1280 * then clear the signal. However, 1281 * if process is member of an orphaned 1282 * process group, ignore tty stop signals. 1283 */ 1284 if (prop & SA_STOP) { 1285 if (pr->ps_flags & PS_TRACED || 1286 (pr->ps_pgrp->pg_jobc == 0 && 1287 prop & SA_TTYSTOP)) 1288 break; /* == ignore */ 1289 pr->ps_xsig = signum; 1290 if (dolock) 1291 SCHED_LOCK(s); 1292 proc_stop(p, 1); 1293 if (dolock) 1294 SCHED_UNLOCK(s); 1295 break; 1296 } else if (prop & SA_IGNORE) { 1297 /* 1298 * Except for SIGCONT, shouldn't get here. 1299 * Default action is to ignore; drop it. 1300 */ 1301 break; /* == ignore */ 1302 } else 1303 goto keep; 1304 /*NOTREACHED*/ 1305 case (long)SIG_IGN: 1306 /* 1307 * Masking above should prevent us ever trying 1308 * to take action on an ignored signal other 1309 * than SIGCONT, unless process is traced. 1310 */ 1311 if ((prop & SA_CONT) == 0 && 1312 (pr->ps_flags & PS_TRACED) == 0) 1313 printf("%s\n", __func__); 1314 break; /* == ignore */ 1315 default: 1316 /* 1317 * This signal has an action, let 1318 * postsig() process it. 1319 */ 1320 goto keep; 1321 } 1322 } 1323 /* NOTREACHED */ 1324 1325 keep: 1326 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */ 1327 return (signum); 1328 } 1329 1330 /* 1331 * Put the argument process into the stopped state and notify the parent 1332 * via wakeup. Signals are handled elsewhere. The process must not be 1333 * on the run queue. 1334 */ 1335 void 1336 proc_stop(struct proc *p, int sw) 1337 { 1338 struct process *pr = p->p_p; 1339 1340 #ifdef MULTIPROCESSOR 1341 SCHED_ASSERT_LOCKED(); 1342 #endif 1343 1344 p->p_stat = SSTOP; 1345 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1346 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1347 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1348 /* 1349 * We need this soft interrupt to be handled fast. 1350 * Extra calls to softclock don't hurt. 1351 */ 1352 softintr_schedule(proc_stop_si); 1353 if (sw) 1354 mi_switch(); 1355 } 1356 1357 /* 1358 * Called from a soft interrupt to send signals to the parents of stopped 1359 * processes. 1360 * We can't do this in proc_stop because it's called with nasty locks held 1361 * and we would need recursive scheduler lock to deal with that. 1362 */ 1363 void 1364 proc_stop_sweep(void *v) 1365 { 1366 struct process *pr; 1367 1368 LIST_FOREACH(pr, &allprocess, ps_list) { 1369 if ((pr->ps_flags & PS_STOPPED) == 0) 1370 continue; 1371 atomic_clearbits_int(&pr->ps_flags, PS_STOPPED); 1372 1373 if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0) 1374 prsignal(pr->ps_pptr, SIGCHLD); 1375 wakeup(pr->ps_pptr); 1376 } 1377 } 1378 1379 /* 1380 * Take the action for the specified signal 1381 * from the current set of pending signals. 1382 */ 1383 void 1384 postsig(struct proc *p, int signum) 1385 { 1386 struct process *pr = p->p_p; 1387 struct sigacts *ps = pr->ps_sigacts; 1388 sig_t action; 1389 u_long trapno; 1390 int mask, returnmask; 1391 siginfo_t si; 1392 union sigval sigval; 1393 int s, code; 1394 1395 KASSERT(signum != 0); 1396 KERNEL_ASSERT_LOCKED(); 1397 1398 mask = sigmask(signum); 1399 atomic_clearbits_int(&p->p_siglist, mask); 1400 action = ps->ps_sigact[signum]; 1401 sigval.sival_ptr = 0; 1402 1403 if (p->p_sisig != signum) { 1404 trapno = 0; 1405 code = SI_USER; 1406 sigval.sival_ptr = 0; 1407 } else { 1408 trapno = p->p_sitrapno; 1409 code = p->p_sicode; 1410 sigval = p->p_sigval; 1411 } 1412 initsiginfo(&si, signum, trapno, code, sigval); 1413 1414 #ifdef KTRACE 1415 if (KTRPOINT(p, KTR_PSIG)) { 1416 ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ? 1417 p->p_oldmask : p->p_sigmask, code, &si); 1418 } 1419 #endif 1420 if (action == SIG_DFL) { 1421 /* 1422 * Default action, where the default is to kill 1423 * the process. (Other cases were ignored above.) 1424 */ 1425 sigexit(p, signum); 1426 /* NOTREACHED */ 1427 } else { 1428 /* 1429 * If we get here, the signal must be caught. 1430 */ 1431 #ifdef DIAGNOSTIC 1432 if (action == SIG_IGN || (p->p_sigmask & mask)) 1433 panic("postsig action"); 1434 #endif 1435 /* 1436 * Set the new mask value and also defer further 1437 * occurrences of this signal. 1438 * 1439 * Special case: user has done a sigpause. Here the 1440 * current mask is not of interest, but rather the 1441 * mask from before the sigpause is what we want 1442 * restored after the signal processing is completed. 1443 */ 1444 #ifdef MULTIPROCESSOR 1445 s = splsched(); 1446 #else 1447 s = splhigh(); 1448 #endif 1449 if (p->p_flag & P_SIGSUSPEND) { 1450 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1451 returnmask = p->p_oldmask; 1452 } else { 1453 returnmask = p->p_sigmask; 1454 } 1455 if (p->p_sisig == signum) { 1456 p->p_sisig = 0; 1457 p->p_sitrapno = 0; 1458 p->p_sicode = SI_USER; 1459 p->p_sigval.sival_ptr = NULL; 1460 } 1461 1462 if (sendsig(action, signum, returnmask, &si)) { 1463 sigexit(p, SIGILL); 1464 /* NOTREACHED */ 1465 } 1466 postsig_done(p, signum, ps); 1467 splx(s); 1468 } 1469 } 1470 1471 /* 1472 * Force the current process to exit with the specified signal, dumping core 1473 * if appropriate. We bypass the normal tests for masked and caught signals, 1474 * allowing unrecoverable failures to terminate the process without changing 1475 * signal state. Mark the accounting record with the signal termination. 1476 * If dumping core, save the signal number for the debugger. Calls exit and 1477 * does not return. 1478 */ 1479 void 1480 sigexit(struct proc *p, int signum) 1481 { 1482 /* Mark process as going away */ 1483 atomic_setbits_int(&p->p_flag, P_WEXIT); 1484 1485 p->p_p->ps_acflag |= AXSIG; 1486 if (sigprop[signum] & SA_CORE) { 1487 p->p_sisig = signum; 1488 1489 /* if there are other threads, pause them */ 1490 if (P_HASSIBLING(p)) 1491 single_thread_set(p, SINGLE_SUSPEND, 1); 1492 1493 if (coredump(p) == 0) 1494 signum |= WCOREFLAG; 1495 } 1496 exit1(p, 0, signum, EXIT_NORMAL); 1497 /* NOTREACHED */ 1498 } 1499 1500 /* 1501 * Send uncatchable SIGABRT for coredump. 1502 */ 1503 void 1504 sigabort(struct proc *p) 1505 { 1506 struct sigaction sa; 1507 1508 memset(&sa, 0, sizeof sa); 1509 sa.sa_handler = SIG_DFL; 1510 setsigvec(p, SIGABRT, &sa); 1511 atomic_clearbits_int(&p->p_sigmask, sigmask(SIGABRT)); 1512 psignal(p, SIGABRT); 1513 } 1514 1515 /* 1516 * Return 1 if `sig', a given signal, is ignored or masked for `p', a given 1517 * thread, and 0 otherwise. 1518 */ 1519 int 1520 sigismasked(struct proc *p, int sig) 1521 { 1522 struct process *pr = p->p_p; 1523 1524 if ((pr->ps_sigacts->ps_sigignore & sigmask(sig)) || 1525 (p->p_sigmask & sigmask(sig))) 1526 return 1; 1527 1528 return 0; 1529 } 1530 1531 int nosuidcoredump = 1; 1532 1533 struct coredump_iostate { 1534 struct proc *io_proc; 1535 struct vnode *io_vp; 1536 struct ucred *io_cred; 1537 off_t io_offset; 1538 }; 1539 1540 /* 1541 * Dump core, into a file named "progname.core", unless the process was 1542 * setuid/setgid. 1543 */ 1544 int 1545 coredump(struct proc *p) 1546 { 1547 #ifdef SMALL_KERNEL 1548 return EPERM; 1549 #else 1550 struct process *pr = p->p_p; 1551 struct vnode *vp; 1552 struct ucred *cred = p->p_ucred; 1553 struct vmspace *vm = p->p_vmspace; 1554 struct nameidata nd; 1555 struct vattr vattr; 1556 struct coredump_iostate io; 1557 int error, len, incrash = 0; 1558 char *name; 1559 const char *dir = "/var/crash"; 1560 1561 if (pr->ps_emul->e_coredump == NULL) 1562 return (EINVAL); 1563 1564 atomic_setbits_int(&pr->ps_flags, PS_COREDUMP); 1565 1566 /* Don't dump if will exceed file size limit. */ 1567 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE)) 1568 return (EFBIG); 1569 1570 name = pool_get(&namei_pool, PR_WAITOK); 1571 1572 /* 1573 * If the process has inconsistent uids, nosuidcoredump 1574 * determines coredump placement policy. 1575 */ 1576 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1577 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1578 if (nosuidcoredump == 3) { 1579 /* 1580 * If the program directory does not exist, dumps of 1581 * that core will silently fail. 1582 */ 1583 len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core", 1584 dir, pr->ps_comm, pr->ps_pid); 1585 incrash = KERNELPATH; 1586 } else if (nosuidcoredump == 2) { 1587 len = snprintf(name, MAXPATHLEN, "%s/%s.core", 1588 dir, pr->ps_comm); 1589 incrash = KERNELPATH; 1590 } else { 1591 pool_put(&namei_pool, name); 1592 return (EPERM); 1593 } 1594 } else 1595 len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm); 1596 1597 if (len >= MAXPATHLEN) { 1598 pool_put(&namei_pool, name); 1599 return (EACCES); 1600 } 1601 1602 /* 1603 * Control the UID used to write out. The normal case uses 1604 * the real UID. If the sugid case is going to write into the 1605 * controlled directory, we do so as root. 1606 */ 1607 if (incrash == 0) { 1608 cred = crdup(cred); 1609 cred->cr_uid = cred->cr_ruid; 1610 cred->cr_gid = cred->cr_rgid; 1611 } else { 1612 if (p->p_fd->fd_rdir) { 1613 vrele(p->p_fd->fd_rdir); 1614 p->p_fd->fd_rdir = NULL; 1615 } 1616 p->p_ucred = crdup(p->p_ucred); 1617 crfree(cred); 1618 cred = p->p_ucred; 1619 crhold(cred); 1620 cred->cr_uid = 0; 1621 cred->cr_gid = 0; 1622 } 1623 1624 /* incrash should be 0 or KERNELPATH only */ 1625 NDINIT(&nd, 0, incrash, UIO_SYSSPACE, name, p); 1626 1627 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK, 1628 S_IRUSR | S_IWUSR); 1629 1630 if (error) 1631 goto out; 1632 1633 /* 1634 * Don't dump to non-regular files, files with links, or files 1635 * owned by someone else. 1636 */ 1637 vp = nd.ni_vp; 1638 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1639 VOP_UNLOCK(vp); 1640 vn_close(vp, FWRITE, cred, p); 1641 goto out; 1642 } 1643 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1644 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1645 vattr.va_uid != cred->cr_uid) { 1646 error = EACCES; 1647 VOP_UNLOCK(vp); 1648 vn_close(vp, FWRITE, cred, p); 1649 goto out; 1650 } 1651 VATTR_NULL(&vattr); 1652 vattr.va_size = 0; 1653 VOP_SETATTR(vp, &vattr, cred, p); 1654 pr->ps_acflag |= ACORE; 1655 1656 io.io_proc = p; 1657 io.io_vp = vp; 1658 io.io_cred = cred; 1659 io.io_offset = 0; 1660 VOP_UNLOCK(vp); 1661 vref(vp); 1662 error = vn_close(vp, FWRITE, cred, p); 1663 if (error == 0) 1664 error = (*pr->ps_emul->e_coredump)(p, &io); 1665 vrele(vp); 1666 out: 1667 crfree(cred); 1668 pool_put(&namei_pool, name); 1669 return (error); 1670 #endif 1671 } 1672 1673 #ifndef SMALL_KERNEL 1674 int 1675 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 1676 { 1677 struct coredump_iostate *io = cookie; 1678 off_t coffset = 0; 1679 size_t csize; 1680 int chunk, error; 1681 1682 csize = len; 1683 do { 1684 if (sigmask(SIGKILL) & 1685 (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist)) 1686 return (EINTR); 1687 1688 /* Rest of the loop sleeps with lock held, so... */ 1689 yield(); 1690 1691 chunk = MIN(csize, MAXPHYS); 1692 error = vn_rdwr(UIO_WRITE, io->io_vp, 1693 (caddr_t)data + coffset, chunk, 1694 io->io_offset + coffset, segflg, 1695 IO_UNIT, io->io_cred, NULL, io->io_proc); 1696 if (error) { 1697 struct process *pr = io->io_proc->p_p; 1698 1699 if (error == ENOSPC) 1700 log(LOG_ERR, 1701 "coredump of %s(%d) failed, filesystem full\n", 1702 pr->ps_comm, pr->ps_pid); 1703 else 1704 log(LOG_ERR, 1705 "coredump of %s(%d), write failed: errno %d\n", 1706 pr->ps_comm, pr->ps_pid, error); 1707 return (error); 1708 } 1709 1710 coffset += chunk; 1711 csize -= chunk; 1712 } while (csize > 0); 1713 1714 io->io_offset += len; 1715 return (0); 1716 } 1717 1718 void 1719 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1720 { 1721 struct coredump_iostate *io = cookie; 1722 1723 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1724 } 1725 1726 #endif /* !SMALL_KERNEL */ 1727 1728 /* 1729 * Nonexistent system call-- signal process (may want to handle it). 1730 * Flag error in case process won't see signal immediately (blocked or ignored). 1731 */ 1732 int 1733 sys_nosys(struct proc *p, void *v, register_t *retval) 1734 { 1735 1736 ptsignal(p, SIGSYS, STHREAD); 1737 return (ENOSYS); 1738 } 1739 1740 int 1741 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1742 { 1743 static int sigwaitsleep; 1744 struct sys___thrsigdivert_args /* { 1745 syscallarg(sigset_t) sigmask; 1746 syscallarg(siginfo_t *) info; 1747 syscallarg(const struct timespec *) timeout; 1748 } */ *uap = v; 1749 struct process *pr = p->p_p; 1750 sigset_t *m; 1751 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1752 siginfo_t si; 1753 uint64_t nsecs = INFSLP; 1754 int timeinvalid = 0; 1755 int error = 0; 1756 1757 memset(&si, 0, sizeof(si)); 1758 1759 if (SCARG(uap, timeout) != NULL) { 1760 struct timespec ts; 1761 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1762 return (error); 1763 #ifdef KTRACE 1764 if (KTRPOINT(p, KTR_STRUCT)) 1765 ktrreltimespec(p, &ts); 1766 #endif 1767 if (!timespecisvalid(&ts)) 1768 timeinvalid = 1; 1769 else 1770 nsecs = TIMESPEC_TO_NSEC(&ts); 1771 } 1772 1773 dosigsuspend(p, p->p_sigmask &~ mask); 1774 for (;;) { 1775 si.si_signo = cursig(p); 1776 if (si.si_signo != 0) { 1777 sigset_t smask = sigmask(si.si_signo); 1778 if (smask & mask) { 1779 if (p->p_siglist & smask) 1780 m = &p->p_siglist; 1781 else if (pr->ps_siglist & smask) 1782 m = &pr->ps_siglist; 1783 else { 1784 /* signal got eaten by someone else? */ 1785 continue; 1786 } 1787 atomic_clearbits_int(m, 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 1797 if (SCARG(uap, timeout) != NULL && nsecs == INFSLP) 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 == 0) 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