1 /* $OpenBSD: kern_sig.c,v 1.285 2021/10/06 15:46:03 claudio 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 int info = (ps->ps_siginfo & mask) != 0; 823 int onstack = (ps->ps_sigonstack & mask) != 0; 824 825 initsiginfo(&si, signum, trapno, code, sigval); 826 #ifdef KTRACE 827 if (KTRPOINT(p, KTR_PSIG)) { 828 ktrpsig(p, signum, ps->ps_sigact[signum], 829 p->p_sigmask, code, &si); 830 } 831 #endif 832 if (sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si, 833 info, onstack)) { 834 sigexit(p, SIGILL); 835 /* NOTREACHED */ 836 } 837 postsig_done(p, signum, ps); 838 } else { 839 p->p_sisig = signum; 840 p->p_sitrapno = trapno; /* XXX for core dump/debugger */ 841 p->p_sicode = code; 842 p->p_sigval = sigval; 843 844 /* 845 * Signals like SIGBUS and SIGSEGV should not, when 846 * generated by the kernel, be ignorable or blockable. 847 * If it is and we're not being traced, then just kill 848 * the process. 849 * After vfs_shutdown(9), init(8) cannot receive signals 850 * because new code pages of the signal handler cannot be 851 * mapped from halted storage. init(8) may not die or the 852 * kernel panics. Better loop between signal handler and 853 * page fault trap until the machine is halted. 854 */ 855 if ((pr->ps_flags & PS_TRACED) == 0 && 856 (sigprop[signum] & SA_KILL) && 857 ((p->p_sigmask & mask) || (ps->ps_sigignore & mask)) && 858 pr->ps_pid != 1) 859 sigexit(p, signum); 860 ptsignal(p, signum, STHREAD); 861 } 862 KERNEL_UNLOCK(); 863 } 864 865 /* 866 * Send the signal to the process. If the signal has an action, the action 867 * is usually performed by the target process rather than the caller; we add 868 * the signal to the set of pending signals for the process. 869 * 870 * Exceptions: 871 * o When a stop signal is sent to a sleeping process that takes the 872 * default action, the process is stopped without awakening it. 873 * o SIGCONT restarts stopped processes (or puts them back to sleep) 874 * regardless of the signal action (eg, blocked or ignored). 875 * 876 * Other ignored signals are discarded immediately. 877 */ 878 void 879 psignal(struct proc *p, int signum) 880 { 881 ptsignal(p, signum, SPROCESS); 882 } 883 884 /* 885 * type = SPROCESS process signal, can be diverted (sigwait()) 886 * type = STHREAD thread signal, but should be propagated if unhandled 887 * type = SPROPAGATED propagated to this thread, so don't propagate again 888 */ 889 void 890 ptsignal(struct proc *p, int signum, enum signal_type type) 891 { 892 int s, prop; 893 sig_t action; 894 int mask; 895 int *siglist; 896 struct process *pr = p->p_p; 897 struct proc *q; 898 int wakeparent = 0; 899 900 KERNEL_ASSERT_LOCKED(); 901 902 #ifdef DIAGNOSTIC 903 if ((u_int)signum >= NSIG || signum == 0) 904 panic("psignal signal number"); 905 #endif 906 907 /* Ignore signal if the target process is exiting */ 908 if (pr->ps_flags & PS_EXITING) 909 return; 910 911 mask = sigmask(signum); 912 913 if (type == SPROCESS) { 914 /* Accept SIGKILL to coredumping processes */ 915 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) { 916 atomic_setbits_int(&pr->ps_siglist, mask); 917 return; 918 } 919 920 /* 921 * If the current thread can process the signal 922 * immediately (it's unblocked) then have it take it. 923 */ 924 q = curproc; 925 if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 && 926 (q->p_sigmask & mask) == 0) 927 p = q; 928 else { 929 /* 930 * A process-wide signal can be diverted to a 931 * different thread that's in sigwait() for this 932 * signal. If there isn't such a thread, then 933 * pick a thread that doesn't have it blocked so 934 * that the stop/kill consideration isn't 935 * delayed. Otherwise, mark it pending on the 936 * main thread. 937 */ 938 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 939 /* ignore exiting threads */ 940 if (q->p_flag & P_WEXIT) 941 continue; 942 943 /* skip threads that have the signal blocked */ 944 if ((q->p_sigmask & mask) != 0) 945 continue; 946 947 /* okay, could send to this thread */ 948 p = q; 949 950 /* 951 * sigsuspend, sigwait, ppoll/pselect, etc? 952 * Definitely go to this thread, as it's 953 * already blocked in the kernel. 954 */ 955 if (q->p_flag & P_SIGSUSPEND) 956 break; 957 } 958 } 959 } 960 961 if (type != SPROPAGATED) 962 KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum); 963 964 prop = sigprop[signum]; 965 966 /* 967 * If proc is traced, always give parent a chance. 968 */ 969 if (pr->ps_flags & PS_TRACED) { 970 action = SIG_DFL; 971 } else { 972 /* 973 * If the signal is being ignored, 974 * then we forget about it immediately. 975 * (Note: we don't set SIGCONT in ps_sigignore, 976 * and if it is set to SIG_IGN, 977 * action will be SIG_DFL here.) 978 */ 979 if (pr->ps_sigacts->ps_sigignore & mask) 980 return; 981 if (p->p_sigmask & mask) { 982 action = SIG_HOLD; 983 } else if (pr->ps_sigacts->ps_sigcatch & mask) { 984 action = SIG_CATCH; 985 } else { 986 action = SIG_DFL; 987 988 if (prop & SA_KILL && pr->ps_nice > NZERO) 989 pr->ps_nice = NZERO; 990 991 /* 992 * If sending a tty stop signal to a member of an 993 * orphaned process group, discard the signal here if 994 * the action is default; don't stop the process below 995 * if sleeping, and don't clear any pending SIGCONT. 996 */ 997 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0) 998 return; 999 } 1000 } 1001 /* 1002 * If delivered to process, mark as pending there. Continue and stop 1003 * signals will be propagated to all threads. So they are always 1004 * marked at thread level. 1005 */ 1006 siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist; 1007 if (prop & SA_CONT) { 1008 siglist = &p->p_siglist; 1009 atomic_clearbits_int(siglist, STOPSIGMASK); 1010 } 1011 if (prop & SA_STOP) { 1012 siglist = &p->p_siglist; 1013 atomic_clearbits_int(siglist, CONTSIGMASK); 1014 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 1015 } 1016 atomic_setbits_int(siglist, mask); 1017 1018 /* 1019 * XXX delay processing of SA_STOP signals unless action == SIG_DFL? 1020 */ 1021 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED) 1022 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) 1023 if (q != p) 1024 ptsignal(q, signum, SPROPAGATED); 1025 1026 /* 1027 * Defer further processing for signals which are held, 1028 * except that stopped processes must be continued by SIGCONT. 1029 */ 1030 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) 1031 return; 1032 1033 SCHED_LOCK(s); 1034 1035 switch (p->p_stat) { 1036 1037 case SSLEEP: 1038 /* 1039 * If process is sleeping uninterruptibly 1040 * we can't interrupt the sleep... the signal will 1041 * be noticed when the process returns through 1042 * trap() or syscall(). 1043 */ 1044 if ((p->p_flag & P_SINTR) == 0) 1045 goto out; 1046 /* 1047 * Process is sleeping and traced... make it runnable 1048 * so it can discover the signal in cursig() and stop 1049 * for the parent. 1050 */ 1051 if (pr->ps_flags & PS_TRACED) 1052 goto run; 1053 /* 1054 * If SIGCONT is default (or ignored) and process is 1055 * asleep, we are finished; the process should not 1056 * be awakened. 1057 */ 1058 if ((prop & SA_CONT) && action == SIG_DFL) { 1059 atomic_clearbits_int(siglist, mask); 1060 goto out; 1061 } 1062 /* 1063 * When a sleeping process receives a stop 1064 * signal, process immediately if possible. 1065 */ 1066 if ((prop & SA_STOP) && action == SIG_DFL) { 1067 /* 1068 * If a child holding parent blocked, 1069 * stopping could cause deadlock. 1070 */ 1071 if (pr->ps_flags & PS_PPWAIT) 1072 goto out; 1073 atomic_clearbits_int(siglist, mask); 1074 pr->ps_xsig = signum; 1075 proc_stop(p, 0); 1076 goto out; 1077 } 1078 /* 1079 * All other (caught or default) signals 1080 * cause the process to run. 1081 */ 1082 goto runfast; 1083 /*NOTREACHED*/ 1084 1085 case SSTOP: 1086 /* 1087 * If traced process is already stopped, 1088 * then no further action is necessary. 1089 */ 1090 if (pr->ps_flags & PS_TRACED) 1091 goto out; 1092 1093 /* 1094 * Kill signal always sets processes running. 1095 */ 1096 if (signum == SIGKILL) { 1097 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1098 goto runfast; 1099 } 1100 1101 if (prop & SA_CONT) { 1102 /* 1103 * If SIGCONT is default (or ignored), we continue the 1104 * process but don't leave the signal in p_siglist, as 1105 * it has no further action. If SIGCONT is held, we 1106 * continue the process and leave the signal in 1107 * p_siglist. If the process catches SIGCONT, let it 1108 * handle the signal itself. If it isn't waiting on 1109 * an event, then it goes back to run state. 1110 * Otherwise, process goes back to sleep state. 1111 */ 1112 atomic_setbits_int(&p->p_flag, P_CONTINUED); 1113 atomic_clearbits_int(&p->p_flag, P_SUSPSIG); 1114 wakeparent = 1; 1115 if (action == SIG_DFL) 1116 atomic_clearbits_int(siglist, mask); 1117 if (action == SIG_CATCH) 1118 goto runfast; 1119 if (p->p_wchan == 0) 1120 goto run; 1121 p->p_stat = SSLEEP; 1122 goto out; 1123 } 1124 1125 if (prop & SA_STOP) { 1126 /* 1127 * Already stopped, don't need to stop again. 1128 * (If we did the shell could get confused.) 1129 */ 1130 atomic_clearbits_int(siglist, mask); 1131 goto out; 1132 } 1133 1134 /* 1135 * If process is sleeping interruptibly, then simulate a 1136 * wakeup so that when it is continued, it will be made 1137 * runnable and can look at the signal. But don't make 1138 * the process runnable, leave it stopped. 1139 */ 1140 if (p->p_flag & P_SINTR) 1141 unsleep(p); 1142 goto out; 1143 1144 case SONPROC: 1145 signotify(p); 1146 /* FALLTHROUGH */ 1147 default: 1148 /* 1149 * SRUN, SIDL, SDEAD do nothing with the signal, 1150 * other than kicking ourselves if we are running. 1151 * It will either never be noticed, or noticed very soon. 1152 */ 1153 goto out; 1154 } 1155 /*NOTREACHED*/ 1156 1157 runfast: 1158 /* 1159 * Raise priority to at least PUSER. 1160 */ 1161 if (p->p_usrpri > PUSER) 1162 p->p_usrpri = PUSER; 1163 run: 1164 setrunnable(p); 1165 out: 1166 SCHED_UNLOCK(s); 1167 if (wakeparent) 1168 wakeup(pr->ps_pptr); 1169 } 1170 1171 /* 1172 * Determine signal that should be delivered to process p, the current 1173 * process, 0 if none. 1174 * 1175 * If the current process has received a signal (should be caught or cause 1176 * termination, should interrupt current syscall), return the signal number. 1177 * Stop signals with default action are processed immediately, then cleared; 1178 * they aren't returned. This is checked after each entry to the system for 1179 * a syscall or trap. The normal call sequence is 1180 * 1181 * while (signum = cursig(curproc)) 1182 * postsig(signum); 1183 * 1184 * Assumes that if the P_SINTR flag is set, we're holding both the 1185 * kernel and scheduler locks. 1186 */ 1187 int 1188 cursig(struct proc *p) 1189 { 1190 struct process *pr = p->p_p; 1191 int sigpending, signum, mask, prop; 1192 int dolock = (p->p_flag & P_SINTR) == 0; 1193 int s; 1194 1195 KERNEL_ASSERT_LOCKED(); 1196 1197 sigpending = (p->p_siglist | pr->ps_siglist); 1198 if (sigpending == 0) 1199 return 0; 1200 1201 if (!ISSET(pr->ps_flags, PS_TRACED) && SIGPENDING(p) == 0) 1202 return 0; 1203 1204 for (;;) { 1205 mask = SIGPENDING(p); 1206 if (pr->ps_flags & PS_PPWAIT) 1207 mask &= ~STOPSIGMASK; 1208 if (mask == 0) /* no signal to send */ 1209 return (0); 1210 signum = ffs((long)mask); 1211 mask = sigmask(signum); 1212 atomic_clearbits_int(&p->p_siglist, mask); 1213 atomic_clearbits_int(&pr->ps_siglist, mask); 1214 1215 /* 1216 * We should see pending but ignored signals 1217 * only if PS_TRACED was on when they were posted. 1218 */ 1219 if (mask & pr->ps_sigacts->ps_sigignore && 1220 (pr->ps_flags & PS_TRACED) == 0) 1221 continue; 1222 1223 /* 1224 * If traced, always stop, and stay stopped until released 1225 * by the debugger. If our parent process is waiting for 1226 * us, don't hang as we could deadlock. 1227 */ 1228 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) && 1229 signum != SIGKILL) { 1230 pr->ps_xsig = signum; 1231 1232 single_thread_set(p, SINGLE_SUSPEND, 0); 1233 1234 if (dolock) 1235 SCHED_LOCK(s); 1236 proc_stop(p, 1); 1237 if (dolock) 1238 SCHED_UNLOCK(s); 1239 1240 single_thread_clear(p, 0); 1241 1242 /* 1243 * If we are no longer being traced, or the parent 1244 * didn't give us a signal, look for more signals. 1245 */ 1246 if ((pr->ps_flags & PS_TRACED) == 0 || 1247 pr->ps_xsig == 0) 1248 continue; 1249 1250 /* 1251 * If the new signal is being masked, look for other 1252 * signals. 1253 */ 1254 signum = pr->ps_xsig; 1255 mask = sigmask(signum); 1256 if ((p->p_sigmask & mask) != 0) 1257 continue; 1258 1259 /* take the signal! */ 1260 atomic_clearbits_int(&p->p_siglist, mask); 1261 atomic_clearbits_int(&pr->ps_siglist, mask); 1262 } 1263 1264 prop = sigprop[signum]; 1265 1266 /* 1267 * Decide whether the signal should be returned. 1268 * Return the signal's number, or fall through 1269 * to clear it from the pending mask. 1270 */ 1271 switch ((long)pr->ps_sigacts->ps_sigact[signum]) { 1272 case (long)SIG_DFL: 1273 /* 1274 * Don't take default actions on system processes. 1275 */ 1276 if (pr->ps_pid <= 1) { 1277 #ifdef DIAGNOSTIC 1278 /* 1279 * Are you sure you want to ignore SIGSEGV 1280 * in init? XXX 1281 */ 1282 printf("Process (pid %d) got signal" 1283 " %d\n", pr->ps_pid, signum); 1284 #endif 1285 break; /* == ignore */ 1286 } 1287 /* 1288 * If there is a pending stop signal to process 1289 * with default action, stop here, 1290 * then clear the signal. However, 1291 * if process is member of an orphaned 1292 * process group, ignore tty stop signals. 1293 */ 1294 if (prop & SA_STOP) { 1295 if (pr->ps_flags & PS_TRACED || 1296 (pr->ps_pgrp->pg_jobc == 0 && 1297 prop & SA_TTYSTOP)) 1298 break; /* == ignore */ 1299 pr->ps_xsig = signum; 1300 if (dolock) 1301 SCHED_LOCK(s); 1302 proc_stop(p, 1); 1303 if (dolock) 1304 SCHED_UNLOCK(s); 1305 break; 1306 } else if (prop & SA_IGNORE) { 1307 /* 1308 * Except for SIGCONT, shouldn't get here. 1309 * Default action is to ignore; drop it. 1310 */ 1311 break; /* == ignore */ 1312 } else 1313 goto keep; 1314 /*NOTREACHED*/ 1315 case (long)SIG_IGN: 1316 /* 1317 * Masking above should prevent us ever trying 1318 * to take action on an ignored signal other 1319 * than SIGCONT, unless process is traced. 1320 */ 1321 if ((prop & SA_CONT) == 0 && 1322 (pr->ps_flags & PS_TRACED) == 0) 1323 printf("%s\n", __func__); 1324 break; /* == ignore */ 1325 default: 1326 /* 1327 * This signal has an action, let 1328 * postsig() process it. 1329 */ 1330 goto keep; 1331 } 1332 } 1333 /* NOTREACHED */ 1334 1335 keep: 1336 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */ 1337 return (signum); 1338 } 1339 1340 /* 1341 * Put the argument process into the stopped state and notify the parent 1342 * via wakeup. Signals are handled elsewhere. The process must not be 1343 * on the run queue. 1344 */ 1345 void 1346 proc_stop(struct proc *p, int sw) 1347 { 1348 struct process *pr = p->p_p; 1349 1350 #ifdef MULTIPROCESSOR 1351 SCHED_ASSERT_LOCKED(); 1352 #endif 1353 1354 p->p_stat = SSTOP; 1355 atomic_clearbits_int(&pr->ps_flags, PS_WAITED); 1356 atomic_setbits_int(&pr->ps_flags, PS_STOPPED); 1357 atomic_setbits_int(&p->p_flag, P_SUSPSIG); 1358 /* 1359 * We need this soft interrupt to be handled fast. 1360 * Extra calls to softclock don't hurt. 1361 */ 1362 softintr_schedule(proc_stop_si); 1363 if (sw) 1364 mi_switch(); 1365 } 1366 1367 /* 1368 * Called from a soft interrupt to send signals to the parents of stopped 1369 * processes. 1370 * We can't do this in proc_stop because it's called with nasty locks held 1371 * and we would need recursive scheduler lock to deal with that. 1372 */ 1373 void 1374 proc_stop_sweep(void *v) 1375 { 1376 struct process *pr; 1377 1378 LIST_FOREACH(pr, &allprocess, ps_list) { 1379 if ((pr->ps_flags & PS_STOPPED) == 0) 1380 continue; 1381 atomic_clearbits_int(&pr->ps_flags, PS_STOPPED); 1382 1383 if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0) 1384 prsignal(pr->ps_pptr, SIGCHLD); 1385 wakeup(pr->ps_pptr); 1386 } 1387 } 1388 1389 /* 1390 * Take the action for the specified signal 1391 * from the current set of pending signals. 1392 */ 1393 void 1394 postsig(struct proc *p, int signum) 1395 { 1396 struct process *pr = p->p_p; 1397 struct sigacts *ps = pr->ps_sigacts; 1398 sig_t action; 1399 u_long trapno; 1400 int mask, returnmask; 1401 siginfo_t si; 1402 union sigval sigval; 1403 int s, code, info, onstack; 1404 1405 KASSERT(signum != 0); 1406 KERNEL_ASSERT_LOCKED(); 1407 1408 mask = sigmask(signum); 1409 atomic_clearbits_int(&p->p_siglist, mask); 1410 action = ps->ps_sigact[signum]; 1411 info = (ps->ps_siginfo & mask) != 0; 1412 onstack = (ps->ps_sigonstack & mask) != 0; 1413 sigval.sival_ptr = 0; 1414 1415 if (p->p_sisig != signum) { 1416 trapno = 0; 1417 code = SI_USER; 1418 sigval.sival_ptr = 0; 1419 } else { 1420 trapno = p->p_sitrapno; 1421 code = p->p_sicode; 1422 sigval = p->p_sigval; 1423 } 1424 initsiginfo(&si, signum, trapno, code, sigval); 1425 1426 #ifdef KTRACE 1427 if (KTRPOINT(p, KTR_PSIG)) { 1428 ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ? 1429 p->p_oldmask : p->p_sigmask, code, &si); 1430 } 1431 #endif 1432 if (action == SIG_DFL) { 1433 /* 1434 * Default action, where the default is to kill 1435 * the process. (Other cases were ignored above.) 1436 */ 1437 sigexit(p, signum); 1438 /* NOTREACHED */ 1439 } else { 1440 /* 1441 * If we get here, the signal must be caught. 1442 */ 1443 #ifdef DIAGNOSTIC 1444 if (action == SIG_IGN || (p->p_sigmask & mask)) 1445 panic("postsig action"); 1446 #endif 1447 /* 1448 * Set the new mask value and also defer further 1449 * occurrences of this signal. 1450 * 1451 * Special case: user has done a sigpause. Here the 1452 * current mask is not of interest, but rather the 1453 * mask from before the sigpause is what we want 1454 * restored after the signal processing is completed. 1455 */ 1456 #ifdef MULTIPROCESSOR 1457 s = splsched(); 1458 #else 1459 s = splhigh(); 1460 #endif 1461 if (p->p_flag & P_SIGSUSPEND) { 1462 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1463 returnmask = p->p_oldmask; 1464 } else { 1465 returnmask = p->p_sigmask; 1466 } 1467 if (p->p_sisig == signum) { 1468 p->p_sisig = 0; 1469 p->p_sitrapno = 0; 1470 p->p_sicode = SI_USER; 1471 p->p_sigval.sival_ptr = NULL; 1472 } 1473 1474 if (sendsig(action, signum, returnmask, &si, info, onstack)) { 1475 sigexit(p, SIGILL); 1476 /* NOTREACHED */ 1477 } 1478 postsig_done(p, signum, ps); 1479 splx(s); 1480 } 1481 } 1482 1483 /* 1484 * Force the current process to exit with the specified signal, dumping core 1485 * if appropriate. We bypass the normal tests for masked and caught signals, 1486 * allowing unrecoverable failures to terminate the process without changing 1487 * signal state. Mark the accounting record with the signal termination. 1488 * If dumping core, save the signal number for the debugger. Calls exit and 1489 * does not return. 1490 */ 1491 void 1492 sigexit(struct proc *p, int signum) 1493 { 1494 /* Mark process as going away */ 1495 atomic_setbits_int(&p->p_flag, P_WEXIT); 1496 1497 p->p_p->ps_acflag |= AXSIG; 1498 if (sigprop[signum] & SA_CORE) { 1499 p->p_sisig = signum; 1500 1501 /* if there are other threads, pause them */ 1502 if (P_HASSIBLING(p)) 1503 single_thread_set(p, SINGLE_SUSPEND, 1); 1504 1505 if (coredump(p) == 0) 1506 signum |= WCOREFLAG; 1507 } 1508 exit1(p, 0, signum, EXIT_NORMAL); 1509 /* NOTREACHED */ 1510 } 1511 1512 /* 1513 * Send uncatchable SIGABRT for coredump. 1514 */ 1515 void 1516 sigabort(struct proc *p) 1517 { 1518 struct sigaction sa; 1519 1520 memset(&sa, 0, sizeof sa); 1521 sa.sa_handler = SIG_DFL; 1522 setsigvec(p, SIGABRT, &sa); 1523 atomic_clearbits_int(&p->p_sigmask, sigmask(SIGABRT)); 1524 psignal(p, SIGABRT); 1525 } 1526 1527 /* 1528 * Return 1 if `sig', a given signal, is ignored or masked for `p', a given 1529 * thread, and 0 otherwise. 1530 */ 1531 int 1532 sigismasked(struct proc *p, int sig) 1533 { 1534 struct process *pr = p->p_p; 1535 1536 if ((pr->ps_sigacts->ps_sigignore & sigmask(sig)) || 1537 (p->p_sigmask & sigmask(sig))) 1538 return 1; 1539 1540 return 0; 1541 } 1542 1543 int nosuidcoredump = 1; 1544 1545 struct coredump_iostate { 1546 struct proc *io_proc; 1547 struct vnode *io_vp; 1548 struct ucred *io_cred; 1549 off_t io_offset; 1550 }; 1551 1552 /* 1553 * Dump core, into a file named "progname.core", unless the process was 1554 * setuid/setgid. 1555 */ 1556 int 1557 coredump(struct proc *p) 1558 { 1559 #ifdef SMALL_KERNEL 1560 return EPERM; 1561 #else 1562 struct process *pr = p->p_p; 1563 struct vnode *vp; 1564 struct ucred *cred = p->p_ucred; 1565 struct vmspace *vm = p->p_vmspace; 1566 struct nameidata nd; 1567 struct vattr vattr; 1568 struct coredump_iostate io; 1569 int error, len, incrash = 0; 1570 char *name; 1571 const char *dir = "/var/crash"; 1572 1573 if (pr->ps_emul->e_coredump == NULL) 1574 return (EINVAL); 1575 1576 atomic_setbits_int(&pr->ps_flags, PS_COREDUMP); 1577 1578 /* Don't dump if will exceed file size limit. */ 1579 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE)) 1580 return (EFBIG); 1581 1582 name = pool_get(&namei_pool, PR_WAITOK); 1583 1584 /* 1585 * If the process has inconsistent uids, nosuidcoredump 1586 * determines coredump placement policy. 1587 */ 1588 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) || 1589 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) { 1590 if (nosuidcoredump == 3) { 1591 /* 1592 * If the program directory does not exist, dumps of 1593 * that core will silently fail. 1594 */ 1595 len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core", 1596 dir, pr->ps_comm, pr->ps_pid); 1597 incrash = KERNELPATH; 1598 } else if (nosuidcoredump == 2) { 1599 len = snprintf(name, MAXPATHLEN, "%s/%s.core", 1600 dir, pr->ps_comm); 1601 incrash = KERNELPATH; 1602 } else { 1603 pool_put(&namei_pool, name); 1604 return (EPERM); 1605 } 1606 } else 1607 len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm); 1608 1609 if (len >= MAXPATHLEN) { 1610 pool_put(&namei_pool, name); 1611 return (EACCES); 1612 } 1613 1614 /* 1615 * Control the UID used to write out. The normal case uses 1616 * the real UID. If the sugid case is going to write into the 1617 * controlled directory, we do so as root. 1618 */ 1619 if (incrash == 0) { 1620 cred = crdup(cred); 1621 cred->cr_uid = cred->cr_ruid; 1622 cred->cr_gid = cred->cr_rgid; 1623 } else { 1624 if (p->p_fd->fd_rdir) { 1625 vrele(p->p_fd->fd_rdir); 1626 p->p_fd->fd_rdir = NULL; 1627 } 1628 p->p_ucred = crdup(p->p_ucred); 1629 crfree(cred); 1630 cred = p->p_ucred; 1631 crhold(cred); 1632 cred->cr_uid = 0; 1633 cred->cr_gid = 0; 1634 } 1635 1636 /* incrash should be 0 or KERNELPATH only */ 1637 NDINIT(&nd, 0, incrash, UIO_SYSSPACE, name, p); 1638 1639 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK, 1640 S_IRUSR | S_IWUSR); 1641 1642 if (error) 1643 goto out; 1644 1645 /* 1646 * Don't dump to non-regular files, files with links, or files 1647 * owned by someone else. 1648 */ 1649 vp = nd.ni_vp; 1650 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) { 1651 VOP_UNLOCK(vp); 1652 vn_close(vp, FWRITE, cred, p); 1653 goto out; 1654 } 1655 if (vp->v_type != VREG || vattr.va_nlink != 1 || 1656 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) || 1657 vattr.va_uid != cred->cr_uid) { 1658 error = EACCES; 1659 VOP_UNLOCK(vp); 1660 vn_close(vp, FWRITE, cred, p); 1661 goto out; 1662 } 1663 VATTR_NULL(&vattr); 1664 vattr.va_size = 0; 1665 VOP_SETATTR(vp, &vattr, cred, p); 1666 pr->ps_acflag |= ACORE; 1667 1668 io.io_proc = p; 1669 io.io_vp = vp; 1670 io.io_cred = cred; 1671 io.io_offset = 0; 1672 VOP_UNLOCK(vp); 1673 vref(vp); 1674 error = vn_close(vp, FWRITE, cred, p); 1675 if (error == 0) 1676 error = (*pr->ps_emul->e_coredump)(p, &io); 1677 vrele(vp); 1678 out: 1679 crfree(cred); 1680 pool_put(&namei_pool, name); 1681 return (error); 1682 #endif 1683 } 1684 1685 #ifndef SMALL_KERNEL 1686 int 1687 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len) 1688 { 1689 struct coredump_iostate *io = cookie; 1690 off_t coffset = 0; 1691 size_t csize; 1692 int chunk, error; 1693 1694 csize = len; 1695 do { 1696 if (sigmask(SIGKILL) & 1697 (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist)) 1698 return (EINTR); 1699 1700 /* Rest of the loop sleeps with lock held, so... */ 1701 yield(); 1702 1703 chunk = MIN(csize, MAXPHYS); 1704 error = vn_rdwr(UIO_WRITE, io->io_vp, 1705 (caddr_t)data + coffset, chunk, 1706 io->io_offset + coffset, segflg, 1707 IO_UNIT, io->io_cred, NULL, io->io_proc); 1708 if (error) { 1709 struct process *pr = io->io_proc->p_p; 1710 1711 if (error == ENOSPC) 1712 log(LOG_ERR, 1713 "coredump of %s(%d) failed, filesystem full\n", 1714 pr->ps_comm, pr->ps_pid); 1715 else 1716 log(LOG_ERR, 1717 "coredump of %s(%d), write failed: errno %d\n", 1718 pr->ps_comm, pr->ps_pid, error); 1719 return (error); 1720 } 1721 1722 coffset += chunk; 1723 csize -= chunk; 1724 } while (csize > 0); 1725 1726 io->io_offset += len; 1727 return (0); 1728 } 1729 1730 void 1731 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end) 1732 { 1733 struct coredump_iostate *io = cookie; 1734 1735 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end); 1736 } 1737 1738 #endif /* !SMALL_KERNEL */ 1739 1740 /* 1741 * Nonexistent system call-- signal process (may want to handle it). 1742 * Flag error in case process won't see signal immediately (blocked or ignored). 1743 */ 1744 int 1745 sys_nosys(struct proc *p, void *v, register_t *retval) 1746 { 1747 1748 ptsignal(p, SIGSYS, STHREAD); 1749 return (ENOSYS); 1750 } 1751 1752 int 1753 sys___thrsigdivert(struct proc *p, void *v, register_t *retval) 1754 { 1755 static int sigwaitsleep; 1756 struct sys___thrsigdivert_args /* { 1757 syscallarg(sigset_t) sigmask; 1758 syscallarg(siginfo_t *) info; 1759 syscallarg(const struct timespec *) timeout; 1760 } */ *uap = v; 1761 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask; 1762 siginfo_t si; 1763 uint64_t nsecs = INFSLP; 1764 int timeinvalid = 0; 1765 int error = 0; 1766 1767 memset(&si, 0, sizeof(si)); 1768 1769 if (SCARG(uap, timeout) != NULL) { 1770 struct timespec ts; 1771 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1772 return (error); 1773 #ifdef KTRACE 1774 if (KTRPOINT(p, KTR_STRUCT)) 1775 ktrreltimespec(p, &ts); 1776 #endif 1777 if (!timespecisvalid(&ts)) 1778 timeinvalid = 1; 1779 else 1780 nsecs = TIMESPEC_TO_NSEC(&ts); 1781 } 1782 1783 dosigsuspend(p, p->p_sigmask &~ mask); 1784 for (;;) { 1785 si.si_signo = cursig(p); 1786 if (si.si_signo != 0) { 1787 sigset_t smask = sigmask(si.si_signo); 1788 if (smask & mask) { 1789 atomic_clearbits_int(&p->p_siglist, smask); 1790 error = 0; 1791 break; 1792 } 1793 } 1794 1795 /* per-POSIX, delay this error until after the above */ 1796 if (timeinvalid) 1797 error = EINVAL; 1798 /* per-POSIX, return immediatly if timeout is zero-valued */ 1799 if (nsecs == 0) 1800 error = EAGAIN; 1801 1802 if (error != 0) 1803 break; 1804 1805 error = tsleep_nsec(&sigwaitsleep, PPAUSE|PCATCH, "sigwait", 1806 nsecs); 1807 } 1808 1809 if (error == 0) { 1810 *retval = si.si_signo; 1811 if (SCARG(uap, info) != NULL) 1812 error = copyout(&si, SCARG(uap, info), sizeof(si)); 1813 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) { 1814 /* 1815 * Restarting is wrong if there's a timeout, as it'll be 1816 * for the same interval again 1817 */ 1818 error = EINTR; 1819 } 1820 1821 return (error); 1822 } 1823 1824 void 1825 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val) 1826 { 1827 memset(si, 0, sizeof(*si)); 1828 1829 si->si_signo = sig; 1830 si->si_code = code; 1831 if (code == SI_USER) { 1832 si->si_value = val; 1833 } else { 1834 switch (sig) { 1835 case SIGSEGV: 1836 case SIGILL: 1837 case SIGBUS: 1838 case SIGFPE: 1839 si->si_addr = val.sival_ptr; 1840 si->si_trapno = trapno; 1841 break; 1842 case SIGXFSZ: 1843 break; 1844 } 1845 } 1846 } 1847 1848 int 1849 filt_sigattach(struct knote *kn) 1850 { 1851 struct process *pr = curproc->p_p; 1852 int s; 1853 1854 if (kn->kn_id >= NSIG) 1855 return EINVAL; 1856 1857 kn->kn_ptr.p_process = pr; 1858 kn->kn_flags |= EV_CLEAR; /* automatically set */ 1859 1860 s = splhigh(); 1861 klist_insert_locked(&pr->ps_klist, kn); 1862 splx(s); 1863 1864 return (0); 1865 } 1866 1867 void 1868 filt_sigdetach(struct knote *kn) 1869 { 1870 struct process *pr = kn->kn_ptr.p_process; 1871 int s; 1872 1873 s = splhigh(); 1874 klist_remove_locked(&pr->ps_klist, kn); 1875 splx(s); 1876 } 1877 1878 /* 1879 * signal knotes are shared with proc knotes, so we apply a mask to 1880 * the hint in order to differentiate them from process hints. This 1881 * could be avoided by using a signal-specific knote list, but probably 1882 * isn't worth the trouble. 1883 */ 1884 int 1885 filt_signal(struct knote *kn, long hint) 1886 { 1887 1888 if (hint & NOTE_SIGNAL) { 1889 hint &= ~NOTE_SIGNAL; 1890 1891 if (kn->kn_id == hint) 1892 kn->kn_data++; 1893 } 1894 return (kn->kn_data != 0); 1895 } 1896 1897 void 1898 userret(struct proc *p) 1899 { 1900 int signum; 1901 1902 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */ 1903 if (p->p_flag & P_PROFPEND) { 1904 atomic_clearbits_int(&p->p_flag, P_PROFPEND); 1905 KERNEL_LOCK(); 1906 psignal(p, SIGPROF); 1907 KERNEL_UNLOCK(); 1908 } 1909 if (p->p_flag & P_ALRMPEND) { 1910 atomic_clearbits_int(&p->p_flag, P_ALRMPEND); 1911 KERNEL_LOCK(); 1912 psignal(p, SIGVTALRM); 1913 KERNEL_UNLOCK(); 1914 } 1915 1916 if (SIGPENDING(p) != 0) { 1917 KERNEL_LOCK(); 1918 while ((signum = cursig(p)) != 0) 1919 postsig(p, signum); 1920 KERNEL_UNLOCK(); 1921 } 1922 1923 /* 1924 * If P_SIGSUSPEND is still set here, then we still need to restore 1925 * the original sigmask before returning to userspace. Also, this 1926 * might unmask some pending signals, so we need to check a second 1927 * time for signals to post. 1928 */ 1929 if (p->p_flag & P_SIGSUSPEND) { 1930 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND); 1931 p->p_sigmask = p->p_oldmask; 1932 1933 KERNEL_LOCK(); 1934 while ((signum = cursig(p)) != 0) 1935 postsig(p, signum); 1936 KERNEL_UNLOCK(); 1937 } 1938 1939 if (p->p_flag & P_SUSPSINGLE) 1940 single_thread_check(p, 0); 1941 1942 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning"); 1943 1944 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri; 1945 } 1946 1947 int 1948 single_thread_check_locked(struct proc *p, int deep, int s) 1949 { 1950 struct process *pr = p->p_p; 1951 1952 SCHED_ASSERT_LOCKED(); 1953 1954 if (pr->ps_single != NULL && pr->ps_single != p) { 1955 do { 1956 /* if we're in deep, we need to unwind to the edge */ 1957 if (deep) { 1958 if (pr->ps_flags & PS_SINGLEUNWIND) 1959 return (ERESTART); 1960 if (pr->ps_flags & PS_SINGLEEXIT) 1961 return (EINTR); 1962 } 1963 1964 if (pr->ps_single == NULL) 1965 continue; 1966 1967 if (atomic_dec_int_nv(&pr->ps_singlecount) == 0) 1968 wakeup(&pr->ps_singlecount); 1969 1970 if (pr->ps_flags & PS_SINGLEEXIT) { 1971 SCHED_UNLOCK(s); 1972 KERNEL_LOCK(); 1973 exit1(p, 0, 0, EXIT_THREAD_NOCHECK); 1974 /* NOTREACHED */ 1975 } 1976 1977 /* not exiting and don't need to unwind, so suspend */ 1978 p->p_stat = SSTOP; 1979 mi_switch(); 1980 } while (pr->ps_single != NULL); 1981 } 1982 1983 return (0); 1984 } 1985 1986 int 1987 single_thread_check(struct proc *p, int deep) 1988 { 1989 int s, error; 1990 1991 SCHED_LOCK(s); 1992 error = single_thread_check_locked(p, deep, s); 1993 SCHED_UNLOCK(s); 1994 1995 return error; 1996 } 1997 1998 /* 1999 * Stop other threads in the process. The mode controls how and 2000 * where the other threads should stop: 2001 * - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit 2002 * (by setting to SINGLE_EXIT) or be released (via single_thread_clear()) 2003 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit 2004 * or released as with SINGLE_SUSPEND 2005 * - SINGLE_EXIT: unwind to kernel boundary and exit 2006 */ 2007 int 2008 single_thread_set(struct proc *p, enum single_thread_mode mode, int wait) 2009 { 2010 struct process *pr = p->p_p; 2011 struct proc *q; 2012 int error, s; 2013 2014 KASSERT(curproc == p); 2015 2016 SCHED_LOCK(s); 2017 error = single_thread_check_locked(p, (mode == SINGLE_UNWIND), s); 2018 if (error) { 2019 SCHED_UNLOCK(s); 2020 return error; 2021 } 2022 2023 switch (mode) { 2024 case SINGLE_SUSPEND: 2025 break; 2026 case SINGLE_UNWIND: 2027 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2028 break; 2029 case SINGLE_EXIT: 2030 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT); 2031 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND); 2032 break; 2033 #ifdef DIAGNOSTIC 2034 default: 2035 panic("single_thread_mode = %d", mode); 2036 #endif 2037 } 2038 pr->ps_singlecount = 0; 2039 membar_producer(); 2040 pr->ps_single = p; 2041 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2042 if (q == p) 2043 continue; 2044 if (q->p_flag & P_WEXIT) { 2045 if (mode == SINGLE_EXIT) { 2046 if (q->p_stat == SSTOP) { 2047 setrunnable(q); 2048 atomic_inc_int(&pr->ps_singlecount); 2049 } 2050 } 2051 continue; 2052 } 2053 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE); 2054 switch (q->p_stat) { 2055 case SIDL: 2056 case SRUN: 2057 atomic_inc_int(&pr->ps_singlecount); 2058 break; 2059 case SSLEEP: 2060 /* if it's not interruptible, then just have to wait */ 2061 if (q->p_flag & P_SINTR) { 2062 /* merely need to suspend? just stop it */ 2063 if (mode == SINGLE_SUSPEND) { 2064 q->p_stat = SSTOP; 2065 break; 2066 } 2067 /* need to unwind or exit, so wake it */ 2068 setrunnable(q); 2069 } 2070 atomic_inc_int(&pr->ps_singlecount); 2071 break; 2072 case SSTOP: 2073 if (mode == SINGLE_EXIT) { 2074 setrunnable(q); 2075 atomic_inc_int(&pr->ps_singlecount); 2076 } 2077 break; 2078 case SDEAD: 2079 break; 2080 case SONPROC: 2081 atomic_inc_int(&pr->ps_singlecount); 2082 signotify(q); 2083 break; 2084 } 2085 } 2086 SCHED_UNLOCK(s); 2087 2088 if (wait) 2089 single_thread_wait(pr, 1); 2090 2091 return 0; 2092 } 2093 2094 /* 2095 * Wait for other threads to stop. If recheck is false then the function 2096 * returns non-zero if the caller needs to restart the check else 0 is 2097 * returned. If recheck is true the return value is always 0. 2098 */ 2099 int 2100 single_thread_wait(struct process *pr, int recheck) 2101 { 2102 struct sleep_state sls; 2103 int wait; 2104 2105 /* wait until they're all suspended */ 2106 wait = pr->ps_singlecount > 0; 2107 while (wait) { 2108 sleep_setup(&sls, &pr->ps_singlecount, PWAIT, "suspend", 0); 2109 wait = pr->ps_singlecount > 0; 2110 sleep_finish(&sls, wait); 2111 if (!recheck) 2112 break; 2113 } 2114 2115 return wait; 2116 } 2117 2118 void 2119 single_thread_clear(struct proc *p, int flag) 2120 { 2121 struct process *pr = p->p_p; 2122 struct proc *q; 2123 int s; 2124 2125 KASSERT(pr->ps_single == p); 2126 KASSERT(curproc == p); 2127 2128 SCHED_LOCK(s); 2129 pr->ps_single = NULL; 2130 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); 2131 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 2132 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0) 2133 continue; 2134 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE); 2135 2136 /* 2137 * if the thread was only stopped for single threading 2138 * then clearing that either makes it runnable or puts 2139 * it back into some sleep queue 2140 */ 2141 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) { 2142 if (q->p_wchan == 0) 2143 setrunnable(q); 2144 else 2145 q->p_stat = SSLEEP; 2146 } 2147 } 2148 SCHED_UNLOCK(s); 2149 } 2150 2151 void 2152 sigio_del(struct sigiolst *rmlist) 2153 { 2154 struct sigio *sigio; 2155 2156 while ((sigio = LIST_FIRST(rmlist)) != NULL) { 2157 LIST_REMOVE(sigio, sio_pgsigio); 2158 crfree(sigio->sio_ucred); 2159 free(sigio, M_SIGIO, sizeof(*sigio)); 2160 } 2161 } 2162 2163 void 2164 sigio_unlink(struct sigio_ref *sir, struct sigiolst *rmlist) 2165 { 2166 struct sigio *sigio; 2167 2168 MUTEX_ASSERT_LOCKED(&sigio_lock); 2169 2170 sigio = sir->sir_sigio; 2171 if (sigio != NULL) { 2172 KASSERT(sigio->sio_myref == sir); 2173 sir->sir_sigio = NULL; 2174 2175 if (sigio->sio_pgid > 0) 2176 sigio->sio_proc = NULL; 2177 else 2178 sigio->sio_pgrp = NULL; 2179 LIST_REMOVE(sigio, sio_pgsigio); 2180 2181 LIST_INSERT_HEAD(rmlist, sigio, sio_pgsigio); 2182 } 2183 } 2184 2185 void 2186 sigio_free(struct sigio_ref *sir) 2187 { 2188 struct sigiolst rmlist; 2189 2190 if (sir->sir_sigio == NULL) 2191 return; 2192 2193 LIST_INIT(&rmlist); 2194 2195 mtx_enter(&sigio_lock); 2196 sigio_unlink(sir, &rmlist); 2197 mtx_leave(&sigio_lock); 2198 2199 sigio_del(&rmlist); 2200 } 2201 2202 void 2203 sigio_freelist(struct sigiolst *sigiolst) 2204 { 2205 struct sigiolst rmlist; 2206 struct sigio *sigio; 2207 2208 if (LIST_EMPTY(sigiolst)) 2209 return; 2210 2211 LIST_INIT(&rmlist); 2212 2213 mtx_enter(&sigio_lock); 2214 while ((sigio = LIST_FIRST(sigiolst)) != NULL) 2215 sigio_unlink(sigio->sio_myref, &rmlist); 2216 mtx_leave(&sigio_lock); 2217 2218 sigio_del(&rmlist); 2219 } 2220 2221 int 2222 sigio_setown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2223 { 2224 struct sigiolst rmlist; 2225 struct proc *p = curproc; 2226 struct pgrp *pgrp = NULL; 2227 struct process *pr = NULL; 2228 struct sigio *sigio; 2229 int error; 2230 pid_t pgid = *(int *)data; 2231 2232 if (pgid == 0) { 2233 sigio_free(sir); 2234 return (0); 2235 } 2236 2237 if (cmd == TIOCSPGRP) { 2238 if (pgid < 0) 2239 return (EINVAL); 2240 pgid = -pgid; 2241 } 2242 2243 sigio = malloc(sizeof(*sigio), M_SIGIO, M_WAITOK); 2244 sigio->sio_pgid = pgid; 2245 sigio->sio_ucred = crhold(p->p_ucred); 2246 sigio->sio_myref = sir; 2247 2248 LIST_INIT(&rmlist); 2249 2250 /* 2251 * The kernel lock, and not sleeping between prfind()/pgfind() and 2252 * linking of the sigio ensure that the process or process group does 2253 * not disappear unexpectedly. 2254 */ 2255 KERNEL_LOCK(); 2256 mtx_enter(&sigio_lock); 2257 2258 if (pgid > 0) { 2259 pr = prfind(pgid); 2260 if (pr == NULL) { 2261 error = ESRCH; 2262 goto fail; 2263 } 2264 2265 /* 2266 * Policy - Don't allow a process to FSETOWN a process 2267 * in another session. 2268 * 2269 * Remove this test to allow maximum flexibility or 2270 * restrict FSETOWN to the current process or process 2271 * group for maximum safety. 2272 */ 2273 if (pr->ps_session != p->p_p->ps_session) { 2274 error = EPERM; 2275 goto fail; 2276 } 2277 2278 if ((pr->ps_flags & PS_EXITING) != 0) { 2279 error = ESRCH; 2280 goto fail; 2281 } 2282 } else /* if (pgid < 0) */ { 2283 pgrp = pgfind(-pgid); 2284 if (pgrp == NULL) { 2285 error = ESRCH; 2286 goto fail; 2287 } 2288 2289 /* 2290 * Policy - Don't allow a process to FSETOWN a process 2291 * in another session. 2292 * 2293 * Remove this test to allow maximum flexibility or 2294 * restrict FSETOWN to the current process or process 2295 * group for maximum safety. 2296 */ 2297 if (pgrp->pg_session != p->p_p->ps_session) { 2298 error = EPERM; 2299 goto fail; 2300 } 2301 } 2302 2303 if (pgid > 0) { 2304 sigio->sio_proc = pr; 2305 LIST_INSERT_HEAD(&pr->ps_sigiolst, sigio, sio_pgsigio); 2306 } else { 2307 sigio->sio_pgrp = pgrp; 2308 LIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); 2309 } 2310 2311 sigio_unlink(sir, &rmlist); 2312 sir->sir_sigio = sigio; 2313 2314 mtx_leave(&sigio_lock); 2315 KERNEL_UNLOCK(); 2316 2317 sigio_del(&rmlist); 2318 2319 return (0); 2320 2321 fail: 2322 mtx_leave(&sigio_lock); 2323 KERNEL_UNLOCK(); 2324 2325 crfree(sigio->sio_ucred); 2326 free(sigio, M_SIGIO, sizeof(*sigio)); 2327 2328 return (error); 2329 } 2330 2331 void 2332 sigio_getown(struct sigio_ref *sir, u_long cmd, caddr_t data) 2333 { 2334 struct sigio *sigio; 2335 pid_t pgid = 0; 2336 2337 mtx_enter(&sigio_lock); 2338 sigio = sir->sir_sigio; 2339 if (sigio != NULL) 2340 pgid = sigio->sio_pgid; 2341 mtx_leave(&sigio_lock); 2342 2343 if (cmd == TIOCGPGRP) 2344 pgid = -pgid; 2345 2346 *(int *)data = pgid; 2347 } 2348 2349 void 2350 sigio_copy(struct sigio_ref *dst, struct sigio_ref *src) 2351 { 2352 struct sigiolst rmlist; 2353 struct sigio *newsigio, *sigio; 2354 2355 sigio_free(dst); 2356 2357 if (src->sir_sigio == NULL) 2358 return; 2359 2360 newsigio = malloc(sizeof(*newsigio), M_SIGIO, M_WAITOK); 2361 LIST_INIT(&rmlist); 2362 2363 mtx_enter(&sigio_lock); 2364 2365 sigio = src->sir_sigio; 2366 if (sigio == NULL) { 2367 mtx_leave(&sigio_lock); 2368 free(newsigio, M_SIGIO, sizeof(*newsigio)); 2369 return; 2370 } 2371 2372 newsigio->sio_pgid = sigio->sio_pgid; 2373 newsigio->sio_ucred = crhold(sigio->sio_ucred); 2374 newsigio->sio_myref = dst; 2375 if (newsigio->sio_pgid > 0) { 2376 newsigio->sio_proc = sigio->sio_proc; 2377 LIST_INSERT_HEAD(&newsigio->sio_proc->ps_sigiolst, newsigio, 2378 sio_pgsigio); 2379 } else { 2380 newsigio->sio_pgrp = sigio->sio_pgrp; 2381 LIST_INSERT_HEAD(&newsigio->sio_pgrp->pg_sigiolst, newsigio, 2382 sio_pgsigio); 2383 } 2384 2385 sigio_unlink(dst, &rmlist); 2386 dst->sir_sigio = newsigio; 2387 2388 mtx_leave(&sigio_lock); 2389 2390 sigio_del(&rmlist); 2391 } 2392