1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_compat.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/signalvar.h> 46 #include <sys/vnode.h> 47 #include <sys/acct.h> 48 #include <sys/condvar.h> 49 #include <sys/event.h> 50 #include <sys/fcntl.h> 51 #include <sys/kernel.h> 52 #include <sys/kse.h> 53 #include <sys/ktr.h> 54 #include <sys/ktrace.h> 55 #include <sys/lock.h> 56 #include <sys/malloc.h> 57 #include <sys/mutex.h> 58 #include <sys/namei.h> 59 #include <sys/proc.h> 60 #include <sys/pioctl.h> 61 #include <sys/resourcevar.h> 62 #include <sys/sched.h> 63 #include <sys/sleepqueue.h> 64 #include <sys/smp.h> 65 #include <sys/stat.h> 66 #include <sys/sx.h> 67 #include <sys/syscallsubr.h> 68 #include <sys/sysctl.h> 69 #include <sys/sysent.h> 70 #include <sys/syslog.h> 71 #include <sys/sysproto.h> 72 #include <sys/unistd.h> 73 #include <sys/wait.h> 74 75 #include <machine/cpu.h> 76 77 #if defined (__alpha__) && !defined(COMPAT_43) 78 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)" 79 #endif 80 81 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 82 83 static int coredump(struct thread *); 84 static char *expand_name(const char *, uid_t, pid_t); 85 static int killpg1(struct thread *td, int sig, int pgid, int all); 86 static int issignal(struct thread *p); 87 static int sigprop(int sig); 88 static void stop(struct proc *); 89 static void tdsigwakeup(struct thread *td, int sig, sig_t action); 90 static int filt_sigattach(struct knote *kn); 91 static void filt_sigdetach(struct knote *kn); 92 static int filt_signal(struct knote *kn, long hint); 93 static struct thread *sigtd(struct proc *p, int sig, int prop); 94 static int kern_sigtimedwait(struct thread *td, sigset_t set, 95 siginfo_t *info, struct timespec *timeout); 96 static void do_tdsignal(struct thread *td, int sig, sigtarget_t target); 97 98 struct filterops sig_filtops = 99 { 0, filt_sigattach, filt_sigdetach, filt_signal }; 100 101 static int kern_logsigexit = 1; 102 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 103 &kern_logsigexit, 0, 104 "Log processes quitting on abnormal signals to syslog(3)"); 105 106 /* 107 * Policy -- Can ucred cr1 send SIGIO to process cr2? 108 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 109 * in the right situations. 110 */ 111 #define CANSIGIO(cr1, cr2) \ 112 ((cr1)->cr_uid == 0 || \ 113 (cr1)->cr_ruid == (cr2)->cr_ruid || \ 114 (cr1)->cr_uid == (cr2)->cr_ruid || \ 115 (cr1)->cr_ruid == (cr2)->cr_uid || \ 116 (cr1)->cr_uid == (cr2)->cr_uid) 117 118 int sugid_coredump; 119 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 120 &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 121 122 static int do_coredump = 1; 123 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 124 &do_coredump, 0, "Enable/Disable coredumps"); 125 126 static int set_core_nodump_flag = 0; 127 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 128 0, "Enable setting the NODUMP flag on coredump files"); 129 130 /* 131 * Signal properties and actions. 132 * The array below categorizes the signals and their default actions 133 * according to the following properties: 134 */ 135 #define SA_KILL 0x01 /* terminates process by default */ 136 #define SA_CORE 0x02 /* ditto and coredumps */ 137 #define SA_STOP 0x04 /* suspend process */ 138 #define SA_TTYSTOP 0x08 /* ditto, from tty */ 139 #define SA_IGNORE 0x10 /* ignore by default */ 140 #define SA_CONT 0x20 /* continue if suspended */ 141 #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 142 #define SA_PROC 0x80 /* deliverable to any thread */ 143 144 static int sigproptbl[NSIG] = { 145 SA_KILL|SA_PROC, /* SIGHUP */ 146 SA_KILL|SA_PROC, /* SIGINT */ 147 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */ 148 SA_KILL|SA_CORE, /* SIGILL */ 149 SA_KILL|SA_CORE, /* SIGTRAP */ 150 SA_KILL|SA_CORE, /* SIGABRT */ 151 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */ 152 SA_KILL|SA_CORE, /* SIGFPE */ 153 SA_KILL|SA_PROC, /* SIGKILL */ 154 SA_KILL|SA_CORE, /* SIGBUS */ 155 SA_KILL|SA_CORE, /* SIGSEGV */ 156 SA_KILL|SA_CORE, /* SIGSYS */ 157 SA_KILL|SA_PROC, /* SIGPIPE */ 158 SA_KILL|SA_PROC, /* SIGALRM */ 159 SA_KILL|SA_PROC, /* SIGTERM */ 160 SA_IGNORE|SA_PROC, /* SIGURG */ 161 SA_STOP|SA_PROC, /* SIGSTOP */ 162 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */ 163 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */ 164 SA_IGNORE|SA_PROC, /* SIGCHLD */ 165 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */ 166 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */ 167 SA_IGNORE|SA_PROC, /* SIGIO */ 168 SA_KILL, /* SIGXCPU */ 169 SA_KILL, /* SIGXFSZ */ 170 SA_KILL|SA_PROC, /* SIGVTALRM */ 171 SA_KILL|SA_PROC, /* SIGPROF */ 172 SA_IGNORE|SA_PROC, /* SIGWINCH */ 173 SA_IGNORE|SA_PROC, /* SIGINFO */ 174 SA_KILL|SA_PROC, /* SIGUSR1 */ 175 SA_KILL|SA_PROC, /* SIGUSR2 */ 176 }; 177 178 /* 179 * Determine signal that should be delivered to process p, the current 180 * process, 0 if none. If there is a pending stop signal with default 181 * action, the process stops in issignal(). 182 * XXXKSE the check for a pending stop is not done under KSE 183 * 184 * MP SAFE. 185 */ 186 int 187 cursig(struct thread *td) 188 { 189 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 190 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 191 mtx_assert(&sched_lock, MA_NOTOWNED); 192 return (SIGPENDING(td) ? issignal(td) : 0); 193 } 194 195 /* 196 * Arrange for ast() to handle unmasked pending signals on return to user 197 * mode. This must be called whenever a signal is added to td_siglist or 198 * unmasked in td_sigmask. 199 */ 200 void 201 signotify(struct thread *td) 202 { 203 struct proc *p; 204 sigset_t set, saved; 205 206 p = td->td_proc; 207 208 PROC_LOCK_ASSERT(p, MA_OWNED); 209 210 /* 211 * If our mask changed we may have to move signal that were 212 * previously masked by all threads to our siglist. 213 */ 214 set = p->p_siglist; 215 if (p->p_flag & P_SA) 216 saved = p->p_siglist; 217 SIGSETNAND(set, td->td_sigmask); 218 SIGSETNAND(p->p_siglist, set); 219 SIGSETOR(td->td_siglist, set); 220 221 if (SIGPENDING(td)) { 222 mtx_lock_spin(&sched_lock); 223 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 224 mtx_unlock_spin(&sched_lock); 225 } 226 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 227 if (!SIGSETEQ(saved, p->p_siglist)) { 228 /* pending set changed */ 229 p->p_flag |= P_SIGEVENT; 230 wakeup(&p->p_siglist); 231 } 232 } 233 } 234 235 int 236 sigonstack(size_t sp) 237 { 238 struct thread *td = curthread; 239 240 return ((td->td_pflags & TDP_ALTSTACK) ? 241 #if defined(COMPAT_43) 242 ((td->td_sigstk.ss_size == 0) ? 243 (td->td_sigstk.ss_flags & SS_ONSTACK) : 244 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 245 #else 246 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 247 #endif 248 : 0); 249 } 250 251 static __inline int 252 sigprop(int sig) 253 { 254 255 if (sig > 0 && sig < NSIG) 256 return (sigproptbl[_SIG_IDX(sig)]); 257 return (0); 258 } 259 260 int 261 sig_ffs(sigset_t *set) 262 { 263 int i; 264 265 for (i = 0; i < _SIG_WORDS; i++) 266 if (set->__bits[i]) 267 return (ffs(set->__bits[i]) + (i * 32)); 268 return (0); 269 } 270 271 /* 272 * kern_sigaction 273 * sigaction 274 * freebsd4_sigaction 275 * osigaction 276 * 277 * MPSAFE 278 */ 279 int 280 kern_sigaction(td, sig, act, oact, flags) 281 struct thread *td; 282 register int sig; 283 struct sigaction *act, *oact; 284 int flags; 285 { 286 struct sigacts *ps; 287 struct thread *td0; 288 struct proc *p = td->td_proc; 289 290 if (!_SIG_VALID(sig)) 291 return (EINVAL); 292 293 PROC_LOCK(p); 294 ps = p->p_sigacts; 295 mtx_lock(&ps->ps_mtx); 296 if (oact) { 297 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 298 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 299 oact->sa_flags = 0; 300 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 301 oact->sa_flags |= SA_ONSTACK; 302 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 303 oact->sa_flags |= SA_RESTART; 304 if (SIGISMEMBER(ps->ps_sigreset, sig)) 305 oact->sa_flags |= SA_RESETHAND; 306 if (SIGISMEMBER(ps->ps_signodefer, sig)) 307 oact->sa_flags |= SA_NODEFER; 308 if (SIGISMEMBER(ps->ps_siginfo, sig)) 309 oact->sa_flags |= SA_SIGINFO; 310 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 311 oact->sa_flags |= SA_NOCLDSTOP; 312 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 313 oact->sa_flags |= SA_NOCLDWAIT; 314 } 315 if (act) { 316 if ((sig == SIGKILL || sig == SIGSTOP) && 317 act->sa_handler != SIG_DFL) { 318 mtx_unlock(&ps->ps_mtx); 319 PROC_UNLOCK(p); 320 return (EINVAL); 321 } 322 323 /* 324 * Change setting atomically. 325 */ 326 327 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 328 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 329 if (act->sa_flags & SA_SIGINFO) { 330 ps->ps_sigact[_SIG_IDX(sig)] = 331 (__sighandler_t *)act->sa_sigaction; 332 SIGADDSET(ps->ps_siginfo, sig); 333 } else { 334 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 335 SIGDELSET(ps->ps_siginfo, sig); 336 } 337 if (!(act->sa_flags & SA_RESTART)) 338 SIGADDSET(ps->ps_sigintr, sig); 339 else 340 SIGDELSET(ps->ps_sigintr, sig); 341 if (act->sa_flags & SA_ONSTACK) 342 SIGADDSET(ps->ps_sigonstack, sig); 343 else 344 SIGDELSET(ps->ps_sigonstack, sig); 345 if (act->sa_flags & SA_RESETHAND) 346 SIGADDSET(ps->ps_sigreset, sig); 347 else 348 SIGDELSET(ps->ps_sigreset, sig); 349 if (act->sa_flags & SA_NODEFER) 350 SIGADDSET(ps->ps_signodefer, sig); 351 else 352 SIGDELSET(ps->ps_signodefer, sig); 353 if (sig == SIGCHLD) { 354 if (act->sa_flags & SA_NOCLDSTOP) 355 ps->ps_flag |= PS_NOCLDSTOP; 356 else 357 ps->ps_flag &= ~PS_NOCLDSTOP; 358 if (act->sa_flags & SA_NOCLDWAIT) { 359 /* 360 * Paranoia: since SA_NOCLDWAIT is implemented 361 * by reparenting the dying child to PID 1 (and 362 * trust it to reap the zombie), PID 1 itself 363 * is forbidden to set SA_NOCLDWAIT. 364 */ 365 if (p->p_pid == 1) 366 ps->ps_flag &= ~PS_NOCLDWAIT; 367 else 368 ps->ps_flag |= PS_NOCLDWAIT; 369 } else 370 ps->ps_flag &= ~PS_NOCLDWAIT; 371 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 372 ps->ps_flag |= PS_CLDSIGIGN; 373 else 374 ps->ps_flag &= ~PS_CLDSIGIGN; 375 } 376 /* 377 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 378 * and for signals set to SIG_DFL where the default is to 379 * ignore. However, don't put SIGCONT in ps_sigignore, as we 380 * have to restart the process. 381 */ 382 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 383 (sigprop(sig) & SA_IGNORE && 384 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 385 if ((p->p_flag & P_SA) && 386 SIGISMEMBER(p->p_siglist, sig)) { 387 p->p_flag |= P_SIGEVENT; 388 wakeup(&p->p_siglist); 389 } 390 /* never to be seen again */ 391 SIGDELSET(p->p_siglist, sig); 392 mtx_lock_spin(&sched_lock); 393 FOREACH_THREAD_IN_PROC(p, td0) 394 SIGDELSET(td0->td_siglist, sig); 395 mtx_unlock_spin(&sched_lock); 396 if (sig != SIGCONT) 397 /* easier in psignal */ 398 SIGADDSET(ps->ps_sigignore, sig); 399 SIGDELSET(ps->ps_sigcatch, sig); 400 } else { 401 SIGDELSET(ps->ps_sigignore, sig); 402 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 403 SIGDELSET(ps->ps_sigcatch, sig); 404 else 405 SIGADDSET(ps->ps_sigcatch, sig); 406 } 407 #ifdef COMPAT_FREEBSD4 408 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 409 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 410 (flags & KSA_FREEBSD4) == 0) 411 SIGDELSET(ps->ps_freebsd4, sig); 412 else 413 SIGADDSET(ps->ps_freebsd4, sig); 414 #endif 415 #ifdef COMPAT_43 416 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 417 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 418 (flags & KSA_OSIGSET) == 0) 419 SIGDELSET(ps->ps_osigset, sig); 420 else 421 SIGADDSET(ps->ps_osigset, sig); 422 #endif 423 } 424 mtx_unlock(&ps->ps_mtx); 425 PROC_UNLOCK(p); 426 return (0); 427 } 428 429 #ifndef _SYS_SYSPROTO_H_ 430 struct sigaction_args { 431 int sig; 432 struct sigaction *act; 433 struct sigaction *oact; 434 }; 435 #endif 436 /* 437 * MPSAFE 438 */ 439 int 440 sigaction(td, uap) 441 struct thread *td; 442 register struct sigaction_args *uap; 443 { 444 struct sigaction act, oact; 445 register struct sigaction *actp, *oactp; 446 int error; 447 448 actp = (uap->act != NULL) ? &act : NULL; 449 oactp = (uap->oact != NULL) ? &oact : NULL; 450 if (actp) { 451 error = copyin(uap->act, actp, sizeof(act)); 452 if (error) 453 return (error); 454 } 455 error = kern_sigaction(td, uap->sig, actp, oactp, 0); 456 if (oactp && !error) 457 error = copyout(oactp, uap->oact, sizeof(oact)); 458 return (error); 459 } 460 461 #ifdef COMPAT_FREEBSD4 462 #ifndef _SYS_SYSPROTO_H_ 463 struct freebsd4_sigaction_args { 464 int sig; 465 struct sigaction *act; 466 struct sigaction *oact; 467 }; 468 #endif 469 /* 470 * MPSAFE 471 */ 472 int 473 freebsd4_sigaction(td, uap) 474 struct thread *td; 475 register struct freebsd4_sigaction_args *uap; 476 { 477 struct sigaction act, oact; 478 register struct sigaction *actp, *oactp; 479 int error; 480 481 482 actp = (uap->act != NULL) ? &act : NULL; 483 oactp = (uap->oact != NULL) ? &oact : NULL; 484 if (actp) { 485 error = copyin(uap->act, actp, sizeof(act)); 486 if (error) 487 return (error); 488 } 489 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 490 if (oactp && !error) 491 error = copyout(oactp, uap->oact, sizeof(oact)); 492 return (error); 493 } 494 #endif /* COMAPT_FREEBSD4 */ 495 496 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 497 #ifndef _SYS_SYSPROTO_H_ 498 struct osigaction_args { 499 int signum; 500 struct osigaction *nsa; 501 struct osigaction *osa; 502 }; 503 #endif 504 /* 505 * MPSAFE 506 */ 507 int 508 osigaction(td, uap) 509 struct thread *td; 510 register struct osigaction_args *uap; 511 { 512 struct osigaction sa; 513 struct sigaction nsa, osa; 514 register struct sigaction *nsap, *osap; 515 int error; 516 517 if (uap->signum <= 0 || uap->signum >= ONSIG) 518 return (EINVAL); 519 520 nsap = (uap->nsa != NULL) ? &nsa : NULL; 521 osap = (uap->osa != NULL) ? &osa : NULL; 522 523 if (nsap) { 524 error = copyin(uap->nsa, &sa, sizeof(sa)); 525 if (error) 526 return (error); 527 nsap->sa_handler = sa.sa_handler; 528 nsap->sa_flags = sa.sa_flags; 529 OSIG2SIG(sa.sa_mask, nsap->sa_mask); 530 } 531 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 532 if (osap && !error) { 533 sa.sa_handler = osap->sa_handler; 534 sa.sa_flags = osap->sa_flags; 535 SIG2OSIG(osap->sa_mask, sa.sa_mask); 536 error = copyout(&sa, uap->osa, sizeof(sa)); 537 } 538 return (error); 539 } 540 541 #if !defined(__i386__) && !defined(__alpha__) 542 /* Avoid replicating the same stub everywhere */ 543 int 544 osigreturn(td, uap) 545 struct thread *td; 546 struct osigreturn_args *uap; 547 { 548 549 return (nosys(td, (struct nosys_args *)uap)); 550 } 551 #endif 552 #endif /* COMPAT_43 */ 553 554 /* 555 * Initialize signal state for process 0; 556 * set to ignore signals that are ignored by default. 557 */ 558 void 559 siginit(p) 560 struct proc *p; 561 { 562 register int i; 563 struct sigacts *ps; 564 565 PROC_LOCK(p); 566 ps = p->p_sigacts; 567 mtx_lock(&ps->ps_mtx); 568 for (i = 1; i <= NSIG; i++) 569 if (sigprop(i) & SA_IGNORE && i != SIGCONT) 570 SIGADDSET(ps->ps_sigignore, i); 571 mtx_unlock(&ps->ps_mtx); 572 PROC_UNLOCK(p); 573 } 574 575 /* 576 * Reset signals for an exec of the specified process. 577 */ 578 void 579 execsigs(struct proc *p) 580 { 581 struct sigacts *ps; 582 int sig; 583 struct thread *td; 584 585 /* 586 * Reset caught signals. Held signals remain held 587 * through td_sigmask (unless they were caught, 588 * and are now ignored by default). 589 */ 590 PROC_LOCK_ASSERT(p, MA_OWNED); 591 td = FIRST_THREAD_IN_PROC(p); 592 ps = p->p_sigacts; 593 mtx_lock(&ps->ps_mtx); 594 while (SIGNOTEMPTY(ps->ps_sigcatch)) { 595 sig = sig_ffs(&ps->ps_sigcatch); 596 SIGDELSET(ps->ps_sigcatch, sig); 597 if (sigprop(sig) & SA_IGNORE) { 598 if (sig != SIGCONT) 599 SIGADDSET(ps->ps_sigignore, sig); 600 SIGDELSET(p->p_siglist, sig); 601 /* 602 * There is only one thread at this point. 603 */ 604 SIGDELSET(td->td_siglist, sig); 605 } 606 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 607 } 608 /* 609 * Reset stack state to the user stack. 610 * Clear set of signals caught on the signal stack. 611 */ 612 td->td_sigstk.ss_flags = SS_DISABLE; 613 td->td_sigstk.ss_size = 0; 614 td->td_sigstk.ss_sp = 0; 615 td->td_pflags &= ~TDP_ALTSTACK; 616 /* 617 * Reset no zombies if child dies flag as Solaris does. 618 */ 619 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 620 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 621 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 622 mtx_unlock(&ps->ps_mtx); 623 } 624 625 /* 626 * kern_sigprocmask() 627 * 628 * Manipulate signal mask. 629 */ 630 int 631 kern_sigprocmask(td, how, set, oset, old) 632 struct thread *td; 633 int how; 634 sigset_t *set, *oset; 635 int old; 636 { 637 int error; 638 639 PROC_LOCK(td->td_proc); 640 if (oset != NULL) 641 *oset = td->td_sigmask; 642 643 error = 0; 644 if (set != NULL) { 645 switch (how) { 646 case SIG_BLOCK: 647 SIG_CANTMASK(*set); 648 SIGSETOR(td->td_sigmask, *set); 649 break; 650 case SIG_UNBLOCK: 651 SIGSETNAND(td->td_sigmask, *set); 652 signotify(td); 653 break; 654 case SIG_SETMASK: 655 SIG_CANTMASK(*set); 656 if (old) 657 SIGSETLO(td->td_sigmask, *set); 658 else 659 td->td_sigmask = *set; 660 signotify(td); 661 break; 662 default: 663 error = EINVAL; 664 break; 665 } 666 } 667 PROC_UNLOCK(td->td_proc); 668 return (error); 669 } 670 671 /* 672 * sigprocmask() - MP SAFE 673 */ 674 675 #ifndef _SYS_SYSPROTO_H_ 676 struct sigprocmask_args { 677 int how; 678 const sigset_t *set; 679 sigset_t *oset; 680 }; 681 #endif 682 int 683 sigprocmask(td, uap) 684 register struct thread *td; 685 struct sigprocmask_args *uap; 686 { 687 sigset_t set, oset; 688 sigset_t *setp, *osetp; 689 int error; 690 691 setp = (uap->set != NULL) ? &set : NULL; 692 osetp = (uap->oset != NULL) ? &oset : NULL; 693 if (setp) { 694 error = copyin(uap->set, setp, sizeof(set)); 695 if (error) 696 return (error); 697 } 698 error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 699 if (osetp && !error) { 700 error = copyout(osetp, uap->oset, sizeof(oset)); 701 } 702 return (error); 703 } 704 705 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 706 /* 707 * osigprocmask() - MP SAFE 708 */ 709 #ifndef _SYS_SYSPROTO_H_ 710 struct osigprocmask_args { 711 int how; 712 osigset_t mask; 713 }; 714 #endif 715 int 716 osigprocmask(td, uap) 717 register struct thread *td; 718 struct osigprocmask_args *uap; 719 { 720 sigset_t set, oset; 721 int error; 722 723 OSIG2SIG(uap->mask, set); 724 error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 725 SIG2OSIG(oset, td->td_retval[0]); 726 return (error); 727 } 728 #endif /* COMPAT_43 */ 729 730 #ifndef _SYS_SYSPROTO_H_ 731 struct sigpending_args { 732 sigset_t *set; 733 }; 734 #endif 735 /* 736 * MPSAFE 737 */ 738 int 739 sigwait(struct thread *td, struct sigwait_args *uap) 740 { 741 siginfo_t info; 742 sigset_t set; 743 int error; 744 745 error = copyin(uap->set, &set, sizeof(set)); 746 if (error) { 747 td->td_retval[0] = error; 748 return (0); 749 } 750 751 error = kern_sigtimedwait(td, set, &info, NULL); 752 if (error) { 753 if (error == ERESTART) 754 return (error); 755 td->td_retval[0] = error; 756 return (0); 757 } 758 759 error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo)); 760 /* Repost if we got an error. */ 761 if (error && info.si_signo) { 762 PROC_LOCK(td->td_proc); 763 tdsignal(td, info.si_signo, SIGTARGET_TD); 764 PROC_UNLOCK(td->td_proc); 765 } 766 td->td_retval[0] = error; 767 return (0); 768 } 769 /* 770 * MPSAFE 771 */ 772 int 773 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 774 { 775 struct timespec ts; 776 struct timespec *timeout; 777 sigset_t set; 778 siginfo_t info; 779 int error; 780 781 if (uap->timeout) { 782 error = copyin(uap->timeout, &ts, sizeof(ts)); 783 if (error) 784 return (error); 785 786 timeout = &ts; 787 } else 788 timeout = NULL; 789 790 error = copyin(uap->set, &set, sizeof(set)); 791 if (error) 792 return (error); 793 794 error = kern_sigtimedwait(td, set, &info, timeout); 795 if (error) 796 return (error); 797 798 if (uap->info) 799 error = copyout(&info, uap->info, sizeof(info)); 800 /* Repost if we got an error. */ 801 if (error && info.si_signo) { 802 PROC_LOCK(td->td_proc); 803 tdsignal(td, info.si_signo, SIGTARGET_TD); 804 PROC_UNLOCK(td->td_proc); 805 } else { 806 td->td_retval[0] = info.si_signo; 807 } 808 return (error); 809 } 810 811 /* 812 * MPSAFE 813 */ 814 int 815 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 816 { 817 siginfo_t info; 818 sigset_t set; 819 int error; 820 821 error = copyin(uap->set, &set, sizeof(set)); 822 if (error) 823 return (error); 824 825 error = kern_sigtimedwait(td, set, &info, NULL); 826 if (error) 827 return (error); 828 829 if (uap->info) 830 error = copyout(&info, uap->info, sizeof(info)); 831 /* Repost if we got an error. */ 832 if (error && info.si_signo) { 833 PROC_LOCK(td->td_proc); 834 tdsignal(td, info.si_signo, SIGTARGET_TD); 835 PROC_UNLOCK(td->td_proc); 836 } else { 837 td->td_retval[0] = info.si_signo; 838 } 839 return (error); 840 } 841 842 static int 843 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info, 844 struct timespec *timeout) 845 { 846 struct sigacts *ps; 847 sigset_t savedmask, sigset; 848 struct proc *p; 849 int error, sig, hz, i, timevalid = 0; 850 struct timespec rts, ets, ts; 851 struct timeval tv; 852 853 p = td->td_proc; 854 error = 0; 855 sig = 0; 856 SIG_CANTMASK(waitset); 857 858 PROC_LOCK(p); 859 ps = p->p_sigacts; 860 savedmask = td->td_sigmask; 861 if (timeout) { 862 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 863 timevalid = 1; 864 getnanouptime(&rts); 865 ets = rts; 866 timespecadd(&ets, timeout); 867 } 868 } 869 870 again: 871 for (i = 1; i <= _SIG_MAXSIG; ++i) { 872 if (!SIGISMEMBER(waitset, i)) 873 continue; 874 if (SIGISMEMBER(td->td_siglist, i)) { 875 SIGFILLSET(td->td_sigmask); 876 SIG_CANTMASK(td->td_sigmask); 877 SIGDELSET(td->td_sigmask, i); 878 mtx_lock(&ps->ps_mtx); 879 sig = cursig(td); 880 i = 0; 881 mtx_unlock(&ps->ps_mtx); 882 } else if (SIGISMEMBER(p->p_siglist, i)) { 883 if (p->p_flag & P_SA) { 884 p->p_flag |= P_SIGEVENT; 885 wakeup(&p->p_siglist); 886 } 887 SIGDELSET(p->p_siglist, i); 888 SIGADDSET(td->td_siglist, i); 889 SIGFILLSET(td->td_sigmask); 890 SIG_CANTMASK(td->td_sigmask); 891 SIGDELSET(td->td_sigmask, i); 892 mtx_lock(&ps->ps_mtx); 893 sig = cursig(td); 894 i = 0; 895 mtx_unlock(&ps->ps_mtx); 896 } 897 if (sig) { 898 td->td_sigmask = savedmask; 899 signotify(td); 900 goto out; 901 } 902 } 903 if (error) 904 goto out; 905 906 td->td_sigmask = savedmask; 907 signotify(td); 908 sigset = td->td_siglist; 909 SIGSETOR(sigset, p->p_siglist); 910 SIGSETAND(sigset, waitset); 911 if (!SIGISEMPTY(sigset)) 912 goto again; 913 914 /* 915 * POSIX says this must be checked after looking for pending 916 * signals. 917 */ 918 if (timeout) { 919 if (!timevalid) { 920 error = EINVAL; 921 goto out; 922 } 923 getnanouptime(&rts); 924 if (timespeccmp(&rts, &ets, >=)) { 925 error = EAGAIN; 926 goto out; 927 } 928 ts = ets; 929 timespecsub(&ts, &rts); 930 TIMESPEC_TO_TIMEVAL(&tv, &ts); 931 hz = tvtohz(&tv); 932 } else 933 hz = 0; 934 935 td->td_waitset = &waitset; 936 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 937 td->td_waitset = NULL; 938 if (timeout) { 939 if (error == ERESTART) { 940 /* timeout can not be restarted. */ 941 error = EINTR; 942 } else if (error == EAGAIN) { 943 /* will calculate timeout by ourself. */ 944 error = 0; 945 } 946 } 947 goto again; 948 949 out: 950 if (sig) { 951 sig_t action; 952 953 error = 0; 954 mtx_lock(&ps->ps_mtx); 955 action = ps->ps_sigact[_SIG_IDX(sig)]; 956 mtx_unlock(&ps->ps_mtx); 957 #ifdef KTRACE 958 if (KTRPOINT(td, KTR_PSIG)) 959 ktrpsig(sig, action, &td->td_sigmask, 0); 960 #endif 961 _STOPEVENT(p, S_SIG, sig); 962 963 SIGDELSET(td->td_siglist, sig); 964 bzero(info, sizeof(*info)); 965 info->si_signo = sig; 966 info->si_code = 0; 967 } 968 PROC_UNLOCK(p); 969 return (error); 970 } 971 972 /* 973 * MPSAFE 974 */ 975 int 976 sigpending(td, uap) 977 struct thread *td; 978 struct sigpending_args *uap; 979 { 980 struct proc *p = td->td_proc; 981 sigset_t siglist; 982 983 PROC_LOCK(p); 984 siglist = p->p_siglist; 985 SIGSETOR(siglist, td->td_siglist); 986 PROC_UNLOCK(p); 987 return (copyout(&siglist, uap->set, sizeof(sigset_t))); 988 } 989 990 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 991 #ifndef _SYS_SYSPROTO_H_ 992 struct osigpending_args { 993 int dummy; 994 }; 995 #endif 996 /* 997 * MPSAFE 998 */ 999 int 1000 osigpending(td, uap) 1001 struct thread *td; 1002 struct osigpending_args *uap; 1003 { 1004 struct proc *p = td->td_proc; 1005 sigset_t siglist; 1006 1007 PROC_LOCK(p); 1008 siglist = p->p_siglist; 1009 SIGSETOR(siglist, td->td_siglist); 1010 PROC_UNLOCK(p); 1011 SIG2OSIG(siglist, td->td_retval[0]); 1012 return (0); 1013 } 1014 #endif /* COMPAT_43 */ 1015 1016 #if defined(COMPAT_43) 1017 /* 1018 * Generalized interface signal handler, 4.3-compatible. 1019 */ 1020 #ifndef _SYS_SYSPROTO_H_ 1021 struct osigvec_args { 1022 int signum; 1023 struct sigvec *nsv; 1024 struct sigvec *osv; 1025 }; 1026 #endif 1027 /* 1028 * MPSAFE 1029 */ 1030 /* ARGSUSED */ 1031 int 1032 osigvec(td, uap) 1033 struct thread *td; 1034 register struct osigvec_args *uap; 1035 { 1036 struct sigvec vec; 1037 struct sigaction nsa, osa; 1038 register struct sigaction *nsap, *osap; 1039 int error; 1040 1041 if (uap->signum <= 0 || uap->signum >= ONSIG) 1042 return (EINVAL); 1043 nsap = (uap->nsv != NULL) ? &nsa : NULL; 1044 osap = (uap->osv != NULL) ? &osa : NULL; 1045 if (nsap) { 1046 error = copyin(uap->nsv, &vec, sizeof(vec)); 1047 if (error) 1048 return (error); 1049 nsap->sa_handler = vec.sv_handler; 1050 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 1051 nsap->sa_flags = vec.sv_flags; 1052 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1053 } 1054 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1055 if (osap && !error) { 1056 vec.sv_handler = osap->sa_handler; 1057 SIG2OSIG(osap->sa_mask, vec.sv_mask); 1058 vec.sv_flags = osap->sa_flags; 1059 vec.sv_flags &= ~SA_NOCLDWAIT; 1060 vec.sv_flags ^= SA_RESTART; 1061 error = copyout(&vec, uap->osv, sizeof(vec)); 1062 } 1063 return (error); 1064 } 1065 1066 #ifndef _SYS_SYSPROTO_H_ 1067 struct osigblock_args { 1068 int mask; 1069 }; 1070 #endif 1071 /* 1072 * MPSAFE 1073 */ 1074 int 1075 osigblock(td, uap) 1076 register struct thread *td; 1077 struct osigblock_args *uap; 1078 { 1079 struct proc *p = td->td_proc; 1080 sigset_t set; 1081 1082 OSIG2SIG(uap->mask, set); 1083 SIG_CANTMASK(set); 1084 PROC_LOCK(p); 1085 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1086 SIGSETOR(td->td_sigmask, set); 1087 PROC_UNLOCK(p); 1088 return (0); 1089 } 1090 1091 #ifndef _SYS_SYSPROTO_H_ 1092 struct osigsetmask_args { 1093 int mask; 1094 }; 1095 #endif 1096 /* 1097 * MPSAFE 1098 */ 1099 int 1100 osigsetmask(td, uap) 1101 struct thread *td; 1102 struct osigsetmask_args *uap; 1103 { 1104 struct proc *p = td->td_proc; 1105 sigset_t set; 1106 1107 OSIG2SIG(uap->mask, set); 1108 SIG_CANTMASK(set); 1109 PROC_LOCK(p); 1110 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1111 SIGSETLO(td->td_sigmask, set); 1112 signotify(td); 1113 PROC_UNLOCK(p); 1114 return (0); 1115 } 1116 #endif /* COMPAT_43 */ 1117 1118 /* 1119 * Suspend process until signal, providing mask to be set 1120 * in the meantime. 1121 ***** XXXKSE this doesn't make sense under KSE. 1122 ***** Do we suspend the thread or all threads in the process? 1123 ***** How do we suspend threads running NOW on another processor? 1124 */ 1125 #ifndef _SYS_SYSPROTO_H_ 1126 struct sigsuspend_args { 1127 const sigset_t *sigmask; 1128 }; 1129 #endif 1130 /* 1131 * MPSAFE 1132 */ 1133 /* ARGSUSED */ 1134 int 1135 sigsuspend(td, uap) 1136 struct thread *td; 1137 struct sigsuspend_args *uap; 1138 { 1139 sigset_t mask; 1140 int error; 1141 1142 error = copyin(uap->sigmask, &mask, sizeof(mask)); 1143 if (error) 1144 return (error); 1145 return (kern_sigsuspend(td, mask)); 1146 } 1147 1148 int 1149 kern_sigsuspend(struct thread *td, sigset_t mask) 1150 { 1151 struct proc *p = td->td_proc; 1152 1153 /* 1154 * When returning from sigsuspend, we want 1155 * the old mask to be restored after the 1156 * signal handler has finished. Thus, we 1157 * save it here and mark the sigacts structure 1158 * to indicate this. 1159 */ 1160 PROC_LOCK(p); 1161 td->td_oldsigmask = td->td_sigmask; 1162 td->td_pflags |= TDP_OLDMASK; 1163 SIG_CANTMASK(mask); 1164 td->td_sigmask = mask; 1165 signotify(td); 1166 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 1167 /* void */; 1168 PROC_UNLOCK(p); 1169 /* always return EINTR rather than ERESTART... */ 1170 return (EINTR); 1171 } 1172 1173 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1174 /* 1175 * Compatibility sigsuspend call for old binaries. Note nonstandard calling 1176 * convention: libc stub passes mask, not pointer, to save a copyin. 1177 */ 1178 #ifndef _SYS_SYSPROTO_H_ 1179 struct osigsuspend_args { 1180 osigset_t mask; 1181 }; 1182 #endif 1183 /* 1184 * MPSAFE 1185 */ 1186 /* ARGSUSED */ 1187 int 1188 osigsuspend(td, uap) 1189 struct thread *td; 1190 struct osigsuspend_args *uap; 1191 { 1192 struct proc *p = td->td_proc; 1193 sigset_t mask; 1194 1195 PROC_LOCK(p); 1196 td->td_oldsigmask = td->td_sigmask; 1197 td->td_pflags |= TDP_OLDMASK; 1198 OSIG2SIG(uap->mask, mask); 1199 SIG_CANTMASK(mask); 1200 SIGSETLO(td->td_sigmask, mask); 1201 signotify(td); 1202 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1203 /* void */; 1204 PROC_UNLOCK(p); 1205 /* always return EINTR rather than ERESTART... */ 1206 return (EINTR); 1207 } 1208 #endif /* COMPAT_43 */ 1209 1210 #if defined(COMPAT_43) 1211 #ifndef _SYS_SYSPROTO_H_ 1212 struct osigstack_args { 1213 struct sigstack *nss; 1214 struct sigstack *oss; 1215 }; 1216 #endif 1217 /* 1218 * MPSAFE 1219 */ 1220 /* ARGSUSED */ 1221 int 1222 osigstack(td, uap) 1223 struct thread *td; 1224 register struct osigstack_args *uap; 1225 { 1226 struct sigstack nss, oss; 1227 int error = 0; 1228 1229 if (uap->nss != NULL) { 1230 error = copyin(uap->nss, &nss, sizeof(nss)); 1231 if (error) 1232 return (error); 1233 } 1234 oss.ss_sp = td->td_sigstk.ss_sp; 1235 oss.ss_onstack = sigonstack(cpu_getstack(td)); 1236 if (uap->nss != NULL) { 1237 td->td_sigstk.ss_sp = nss.ss_sp; 1238 td->td_sigstk.ss_size = 0; 1239 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1240 td->td_pflags |= TDP_ALTSTACK; 1241 } 1242 if (uap->oss != NULL) 1243 error = copyout(&oss, uap->oss, sizeof(oss)); 1244 1245 return (error); 1246 } 1247 #endif /* COMPAT_43 */ 1248 1249 #ifndef _SYS_SYSPROTO_H_ 1250 struct sigaltstack_args { 1251 stack_t *ss; 1252 stack_t *oss; 1253 }; 1254 #endif 1255 /* 1256 * MPSAFE 1257 */ 1258 /* ARGSUSED */ 1259 int 1260 sigaltstack(td, uap) 1261 struct thread *td; 1262 register struct sigaltstack_args *uap; 1263 { 1264 stack_t ss, oss; 1265 int error; 1266 1267 if (uap->ss != NULL) { 1268 error = copyin(uap->ss, &ss, sizeof(ss)); 1269 if (error) 1270 return (error); 1271 } 1272 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 1273 (uap->oss != NULL) ? &oss : NULL); 1274 if (error) 1275 return (error); 1276 if (uap->oss != NULL) 1277 error = copyout(&oss, uap->oss, sizeof(stack_t)); 1278 return (error); 1279 } 1280 1281 int 1282 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 1283 { 1284 struct proc *p = td->td_proc; 1285 int oonstack; 1286 1287 oonstack = sigonstack(cpu_getstack(td)); 1288 1289 if (oss != NULL) { 1290 *oss = td->td_sigstk; 1291 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1292 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1293 } 1294 1295 if (ss != NULL) { 1296 if (oonstack) 1297 return (EPERM); 1298 if ((ss->ss_flags & ~SS_DISABLE) != 0) 1299 return (EINVAL); 1300 if (!(ss->ss_flags & SS_DISABLE)) { 1301 if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1302 return (ENOMEM); 1303 } 1304 td->td_sigstk = *ss; 1305 td->td_pflags |= TDP_ALTSTACK; 1306 } else { 1307 td->td_pflags &= ~TDP_ALTSTACK; 1308 } 1309 } 1310 return (0); 1311 } 1312 1313 /* 1314 * Common code for kill process group/broadcast kill. 1315 * cp is calling process. 1316 */ 1317 static int 1318 killpg1(td, sig, pgid, all) 1319 register struct thread *td; 1320 int sig, pgid, all; 1321 { 1322 register struct proc *p; 1323 struct pgrp *pgrp; 1324 int nfound = 0; 1325 1326 if (all) { 1327 /* 1328 * broadcast 1329 */ 1330 sx_slock(&allproc_lock); 1331 LIST_FOREACH(p, &allproc, p_list) { 1332 PROC_LOCK(p); 1333 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 1334 p == td->td_proc) { 1335 PROC_UNLOCK(p); 1336 continue; 1337 } 1338 if (p_cansignal(td, p, sig) == 0) { 1339 nfound++; 1340 if (sig) 1341 psignal(p, sig); 1342 } 1343 PROC_UNLOCK(p); 1344 } 1345 sx_sunlock(&allproc_lock); 1346 } else { 1347 sx_slock(&proctree_lock); 1348 if (pgid == 0) { 1349 /* 1350 * zero pgid means send to my process group. 1351 */ 1352 pgrp = td->td_proc->p_pgrp; 1353 PGRP_LOCK(pgrp); 1354 } else { 1355 pgrp = pgfind(pgid); 1356 if (pgrp == NULL) { 1357 sx_sunlock(&proctree_lock); 1358 return (ESRCH); 1359 } 1360 } 1361 sx_sunlock(&proctree_lock); 1362 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1363 PROC_LOCK(p); 1364 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1365 PROC_UNLOCK(p); 1366 continue; 1367 } 1368 if (p_cansignal(td, p, sig) == 0) { 1369 nfound++; 1370 if (sig) 1371 psignal(p, sig); 1372 } 1373 PROC_UNLOCK(p); 1374 } 1375 PGRP_UNLOCK(pgrp); 1376 } 1377 return (nfound ? 0 : ESRCH); 1378 } 1379 1380 #ifndef _SYS_SYSPROTO_H_ 1381 struct kill_args { 1382 int pid; 1383 int signum; 1384 }; 1385 #endif 1386 /* 1387 * MPSAFE 1388 */ 1389 /* ARGSUSED */ 1390 int 1391 kill(td, uap) 1392 register struct thread *td; 1393 register struct kill_args *uap; 1394 { 1395 register struct proc *p; 1396 int error; 1397 1398 if ((u_int)uap->signum > _SIG_MAXSIG) 1399 return (EINVAL); 1400 1401 if (uap->pid > 0) { 1402 /* kill single process */ 1403 if ((p = pfind(uap->pid)) == NULL) { 1404 if ((p = zpfind(uap->pid)) == NULL) 1405 return (ESRCH); 1406 } 1407 error = p_cansignal(td, p, uap->signum); 1408 if (error == 0 && uap->signum) 1409 psignal(p, uap->signum); 1410 PROC_UNLOCK(p); 1411 return (error); 1412 } 1413 switch (uap->pid) { 1414 case -1: /* broadcast signal */ 1415 return (killpg1(td, uap->signum, 0, 1)); 1416 case 0: /* signal own process group */ 1417 return (killpg1(td, uap->signum, 0, 0)); 1418 default: /* negative explicit process group */ 1419 return (killpg1(td, uap->signum, -uap->pid, 0)); 1420 } 1421 /* NOTREACHED */ 1422 } 1423 1424 #if defined(COMPAT_43) 1425 #ifndef _SYS_SYSPROTO_H_ 1426 struct okillpg_args { 1427 int pgid; 1428 int signum; 1429 }; 1430 #endif 1431 /* 1432 * MPSAFE 1433 */ 1434 /* ARGSUSED */ 1435 int 1436 okillpg(td, uap) 1437 struct thread *td; 1438 register struct okillpg_args *uap; 1439 { 1440 1441 if ((u_int)uap->signum > _SIG_MAXSIG) 1442 return (EINVAL); 1443 return (killpg1(td, uap->signum, uap->pgid, 0)); 1444 } 1445 #endif /* COMPAT_43 */ 1446 1447 /* 1448 * Send a signal to a process group. 1449 */ 1450 void 1451 gsignal(pgid, sig) 1452 int pgid, sig; 1453 { 1454 struct pgrp *pgrp; 1455 1456 if (pgid != 0) { 1457 sx_slock(&proctree_lock); 1458 pgrp = pgfind(pgid); 1459 sx_sunlock(&proctree_lock); 1460 if (pgrp != NULL) { 1461 pgsignal(pgrp, sig, 0); 1462 PGRP_UNLOCK(pgrp); 1463 } 1464 } 1465 } 1466 1467 /* 1468 * Send a signal to a process group. If checktty is 1, 1469 * limit to members which have a controlling terminal. 1470 */ 1471 void 1472 pgsignal(pgrp, sig, checkctty) 1473 struct pgrp *pgrp; 1474 int sig, checkctty; 1475 { 1476 register struct proc *p; 1477 1478 if (pgrp) { 1479 PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1480 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1481 PROC_LOCK(p); 1482 if (checkctty == 0 || p->p_flag & P_CONTROLT) 1483 psignal(p, sig); 1484 PROC_UNLOCK(p); 1485 } 1486 } 1487 } 1488 1489 /* 1490 * Send a signal caused by a trap to the current thread. 1491 * If it will be caught immediately, deliver it with correct code. 1492 * Otherwise, post it normally. 1493 * 1494 * MPSAFE 1495 */ 1496 void 1497 trapsignal(struct thread *td, int sig, u_long code) 1498 { 1499 struct sigacts *ps; 1500 struct proc *p; 1501 siginfo_t siginfo; 1502 int error; 1503 1504 p = td->td_proc; 1505 if (td->td_pflags & TDP_SA) { 1506 if (td->td_mailbox == NULL) 1507 thread_user_enter(td); 1508 PROC_LOCK(p); 1509 SIGDELSET(td->td_sigmask, sig); 1510 mtx_lock_spin(&sched_lock); 1511 /* 1512 * Force scheduling an upcall, so UTS has chance to 1513 * process the signal before thread runs again in 1514 * userland. 1515 */ 1516 if (td->td_upcall) 1517 td->td_upcall->ku_flags |= KUF_DOUPCALL; 1518 mtx_unlock_spin(&sched_lock); 1519 } else { 1520 PROC_LOCK(p); 1521 } 1522 ps = p->p_sigacts; 1523 mtx_lock(&ps->ps_mtx); 1524 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 1525 !SIGISMEMBER(td->td_sigmask, sig)) { 1526 p->p_stats->p_ru.ru_nsignals++; 1527 #ifdef KTRACE 1528 if (KTRPOINT(curthread, KTR_PSIG)) 1529 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 1530 &td->td_sigmask, code); 1531 #endif 1532 if (!(td->td_pflags & TDP_SA)) 1533 (*p->p_sysent->sv_sendsig)( 1534 ps->ps_sigact[_SIG_IDX(sig)], sig, 1535 &td->td_sigmask, code); 1536 else if (td->td_mailbox == NULL) { 1537 mtx_unlock(&ps->ps_mtx); 1538 /* UTS caused a sync signal */ 1539 p->p_code = code; /* XXX for core dump/debugger */ 1540 p->p_sig = sig; /* XXX to verify code */ 1541 sigexit(td, sig); 1542 } else { 1543 cpu_thread_siginfo(sig, code, &siginfo); 1544 mtx_unlock(&ps->ps_mtx); 1545 SIGADDSET(td->td_sigmask, sig); 1546 PROC_UNLOCK(p); 1547 error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 1548 sizeof(siginfo)); 1549 PROC_LOCK(p); 1550 /* UTS memory corrupted */ 1551 if (error) 1552 sigexit(td, SIGSEGV); 1553 mtx_lock(&ps->ps_mtx); 1554 } 1555 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1556 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1557 SIGADDSET(td->td_sigmask, sig); 1558 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1559 /* 1560 * See kern_sigaction() for origin of this code. 1561 */ 1562 SIGDELSET(ps->ps_sigcatch, sig); 1563 if (sig != SIGCONT && 1564 sigprop(sig) & SA_IGNORE) 1565 SIGADDSET(ps->ps_sigignore, sig); 1566 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1567 } 1568 mtx_unlock(&ps->ps_mtx); 1569 } else { 1570 mtx_unlock(&ps->ps_mtx); 1571 p->p_code = code; /* XXX for core dump/debugger */ 1572 p->p_sig = sig; /* XXX to verify code */ 1573 tdsignal(td, sig, SIGTARGET_TD); 1574 } 1575 PROC_UNLOCK(p); 1576 } 1577 1578 static struct thread * 1579 sigtd(struct proc *p, int sig, int prop) 1580 { 1581 struct thread *td, *signal_td; 1582 1583 PROC_LOCK_ASSERT(p, MA_OWNED); 1584 1585 /* 1586 * First find a thread in sigwait state and signal belongs to 1587 * its wait set. POSIX's arguments is that speed of delivering signal 1588 * to sigwait thread is faster than delivering signal to user stack. 1589 * If we can not find sigwait thread, then find the first thread in 1590 * the proc that doesn't have this signal masked, an exception is 1591 * if current thread is sending signal to its process, and it does not 1592 * mask the signal, it should get the signal, this is another fast 1593 * way to deliver signal. 1594 */ 1595 signal_td = NULL; 1596 mtx_lock_spin(&sched_lock); 1597 FOREACH_THREAD_IN_PROC(p, td) { 1598 if (td->td_waitset != NULL && 1599 SIGISMEMBER(*(td->td_waitset), sig)) { 1600 mtx_unlock_spin(&sched_lock); 1601 return (td); 1602 } 1603 if (!SIGISMEMBER(td->td_sigmask, sig)) { 1604 if (td == curthread) 1605 signal_td = curthread; 1606 else if (signal_td == NULL) 1607 signal_td = td; 1608 } 1609 } 1610 if (signal_td == NULL) 1611 signal_td = FIRST_THREAD_IN_PROC(p); 1612 mtx_unlock_spin(&sched_lock); 1613 return (signal_td); 1614 } 1615 1616 /* 1617 * Send the signal to the process. If the signal has an action, the action 1618 * is usually performed by the target process rather than the caller; we add 1619 * the signal to the set of pending signals for the process. 1620 * 1621 * Exceptions: 1622 * o When a stop signal is sent to a sleeping process that takes the 1623 * default action, the process is stopped without awakening it. 1624 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1625 * regardless of the signal action (eg, blocked or ignored). 1626 * 1627 * Other ignored signals are discarded immediately. 1628 * 1629 * MPSAFE 1630 */ 1631 void 1632 psignal(struct proc *p, int sig) 1633 { 1634 struct thread *td; 1635 int prop; 1636 1637 if (!_SIG_VALID(sig)) 1638 panic("psignal(): invalid signal"); 1639 1640 PROC_LOCK_ASSERT(p, MA_OWNED); 1641 /* 1642 * IEEE Std 1003.1-2001: return success when killing a zombie. 1643 */ 1644 if (p->p_state == PRS_ZOMBIE) 1645 return; 1646 prop = sigprop(sig); 1647 1648 /* 1649 * Find a thread to deliver the signal to. 1650 */ 1651 td = sigtd(p, sig, prop); 1652 1653 tdsignal(td, sig, SIGTARGET_P); 1654 } 1655 1656 /* 1657 * MPSAFE 1658 */ 1659 void 1660 tdsignal(struct thread *td, int sig, sigtarget_t target) 1661 { 1662 sigset_t saved; 1663 struct proc *p = td->td_proc; 1664 1665 if (p->p_flag & P_SA) 1666 saved = p->p_siglist; 1667 do_tdsignal(td, sig, target); 1668 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 1669 if (!SIGSETEQ(saved, p->p_siglist)) { 1670 /* pending set changed */ 1671 p->p_flag |= P_SIGEVENT; 1672 wakeup(&p->p_siglist); 1673 } 1674 } 1675 } 1676 1677 static void 1678 do_tdsignal(struct thread *td, int sig, sigtarget_t target) 1679 { 1680 struct proc *p; 1681 register sig_t action; 1682 sigset_t *siglist; 1683 struct thread *td0; 1684 register int prop; 1685 struct sigacts *ps; 1686 1687 if (!_SIG_VALID(sig)) 1688 panic("do_tdsignal(): invalid signal"); 1689 1690 p = td->td_proc; 1691 ps = p->p_sigacts; 1692 1693 PROC_LOCK_ASSERT(p, MA_OWNED); 1694 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 1695 1696 prop = sigprop(sig); 1697 1698 /* 1699 * If the signal is blocked and not destined for this thread, then 1700 * assign it to the process so that we can find it later in the first 1701 * thread that unblocks it. Otherwise, assign it to this thread now. 1702 */ 1703 if (target == SIGTARGET_TD) { 1704 siglist = &td->td_siglist; 1705 } else { 1706 if (!SIGISMEMBER(td->td_sigmask, sig)) 1707 siglist = &td->td_siglist; 1708 else if (td->td_waitset != NULL && 1709 SIGISMEMBER(*(td->td_waitset), sig)) 1710 siglist = &td->td_siglist; 1711 else 1712 siglist = &p->p_siglist; 1713 } 1714 1715 /* 1716 * If proc is traced, always give parent a chance; 1717 * if signal event is tracked by procfs, give *that* 1718 * a chance, as well. 1719 */ 1720 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1721 action = SIG_DFL; 1722 } else { 1723 /* 1724 * If the signal is being ignored, 1725 * then we forget about it immediately. 1726 * (Note: we don't set SIGCONT in ps_sigignore, 1727 * and if it is set to SIG_IGN, 1728 * action will be SIG_DFL here.) 1729 */ 1730 mtx_lock(&ps->ps_mtx); 1731 if (SIGISMEMBER(ps->ps_sigignore, sig) || 1732 (p->p_flag & P_WEXIT)) { 1733 mtx_unlock(&ps->ps_mtx); 1734 return; 1735 } 1736 if (((td->td_waitset == NULL) && 1737 SIGISMEMBER(td->td_sigmask, sig)) || 1738 ((td->td_waitset != NULL) && 1739 SIGISMEMBER(td->td_sigmask, sig) && 1740 !SIGISMEMBER(*(td->td_waitset), sig))) 1741 action = SIG_HOLD; 1742 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1743 action = SIG_CATCH; 1744 else 1745 action = SIG_DFL; 1746 mtx_unlock(&ps->ps_mtx); 1747 } 1748 1749 if (prop & SA_CONT) { 1750 SIG_STOPSIGMASK(p->p_siglist); 1751 /* 1752 * XXX Should investigate leaving STOP and CONT sigs only in 1753 * the proc's siglist. 1754 */ 1755 mtx_lock_spin(&sched_lock); 1756 FOREACH_THREAD_IN_PROC(p, td0) 1757 SIG_STOPSIGMASK(td0->td_siglist); 1758 mtx_unlock_spin(&sched_lock); 1759 } 1760 1761 if (prop & SA_STOP) { 1762 /* 1763 * If sending a tty stop signal to a member of an orphaned 1764 * process group, discard the signal here if the action 1765 * is default; don't stop the process below if sleeping, 1766 * and don't clear any pending SIGCONT. 1767 */ 1768 if ((prop & SA_TTYSTOP) && 1769 (p->p_pgrp->pg_jobc == 0) && 1770 (action == SIG_DFL)) 1771 return; 1772 SIG_CONTSIGMASK(p->p_siglist); 1773 mtx_lock_spin(&sched_lock); 1774 FOREACH_THREAD_IN_PROC(p, td0) 1775 SIG_CONTSIGMASK(td0->td_siglist); 1776 mtx_unlock_spin(&sched_lock); 1777 p->p_flag &= ~P_CONTINUED; 1778 } 1779 1780 SIGADDSET(*siglist, sig); 1781 signotify(td); /* uses schedlock */ 1782 if (siglist == &td->td_siglist && (td->td_waitset != NULL) && 1783 action != SIG_HOLD) { 1784 td->td_waitset = NULL; 1785 } 1786 1787 /* 1788 * Defer further processing for signals which are held, 1789 * except that stopped processes must be continued by SIGCONT. 1790 */ 1791 if (action == SIG_HOLD && 1792 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 1793 return; 1794 /* 1795 * SIGKILL: Remove procfs STOPEVENTs. 1796 */ 1797 if (sig == SIGKILL) { 1798 /* from procfs_ioctl.c: PIOCBIC */ 1799 p->p_stops = 0; 1800 /* from procfs_ioctl.c: PIOCCONT */ 1801 p->p_step = 0; 1802 wakeup(&p->p_step); 1803 } 1804 /* 1805 * Some signals have a process-wide effect and a per-thread 1806 * component. Most processing occurs when the process next 1807 * tries to cross the user boundary, however there are some 1808 * times when processing needs to be done immediatly, such as 1809 * waking up threads so that they can cross the user boundary. 1810 * We try do the per-process part here. 1811 */ 1812 if (P_SHOULDSTOP(p)) { 1813 /* 1814 * The process is in stopped mode. All the threads should be 1815 * either winding down or already on the suspended queue. 1816 */ 1817 if (p->p_flag & P_TRACED) { 1818 /* 1819 * The traced process is already stopped, 1820 * so no further action is necessary. 1821 * No signal can restart us. 1822 */ 1823 goto out; 1824 } 1825 1826 if (sig == SIGKILL) { 1827 /* 1828 * SIGKILL sets process running. 1829 * It will die elsewhere. 1830 * All threads must be restarted. 1831 */ 1832 p->p_flag &= ~P_STOPPED_SIG; 1833 goto runfast; 1834 } 1835 1836 if (prop & SA_CONT) { 1837 /* 1838 * If SIGCONT is default (or ignored), we continue the 1839 * process but don't leave the signal in siglist as 1840 * it has no further action. If SIGCONT is held, we 1841 * continue the process and leave the signal in 1842 * siglist. If the process catches SIGCONT, let it 1843 * handle the signal itself. If it isn't waiting on 1844 * an event, it goes back to run state. 1845 * Otherwise, process goes back to sleep state. 1846 */ 1847 p->p_flag &= ~P_STOPPED_SIG; 1848 p->p_flag |= P_CONTINUED; 1849 if (action == SIG_DFL) { 1850 SIGDELSET(*siglist, sig); 1851 } else if (action == SIG_CATCH) { 1852 /* 1853 * The process wants to catch it so it needs 1854 * to run at least one thread, but which one? 1855 * It would seem that the answer would be to 1856 * run an upcall in the next KSE to run, and 1857 * deliver the signal that way. In a NON KSE 1858 * process, we need to make sure that the 1859 * single thread is runnable asap. 1860 * XXXKSE for now however, make them all run. 1861 */ 1862 goto runfast; 1863 } 1864 /* 1865 * The signal is not ignored or caught. 1866 */ 1867 mtx_lock_spin(&sched_lock); 1868 thread_unsuspend(p); 1869 mtx_unlock_spin(&sched_lock); 1870 goto out; 1871 } 1872 1873 if (prop & SA_STOP) { 1874 /* 1875 * Already stopped, don't need to stop again 1876 * (If we did the shell could get confused). 1877 * Just make sure the signal STOP bit set. 1878 */ 1879 p->p_flag |= P_STOPPED_SIG; 1880 SIGDELSET(*siglist, sig); 1881 goto out; 1882 } 1883 1884 /* 1885 * All other kinds of signals: 1886 * If a thread is sleeping interruptibly, simulate a 1887 * wakeup so that when it is continued it will be made 1888 * runnable and can look at the signal. However, don't make 1889 * the PROCESS runnable, leave it stopped. 1890 * It may run a bit until it hits a thread_suspend_check(). 1891 */ 1892 mtx_lock_spin(&sched_lock); 1893 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 1894 sleepq_abort(td); 1895 mtx_unlock_spin(&sched_lock); 1896 goto out; 1897 /* 1898 * Mutexes are short lived. Threads waiting on them will 1899 * hit thread_suspend_check() soon. 1900 */ 1901 } else if (p->p_state == PRS_NORMAL) { 1902 if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 1903 !(prop & SA_STOP)) { 1904 mtx_lock_spin(&sched_lock); 1905 tdsigwakeup(td, sig, action); 1906 mtx_unlock_spin(&sched_lock); 1907 goto out; 1908 } 1909 if (prop & SA_STOP) { 1910 if (p->p_flag & P_PPWAIT) 1911 goto out; 1912 p->p_flag |= P_STOPPED_SIG; 1913 p->p_xstat = sig; 1914 p->p_xthread = td; 1915 mtx_lock_spin(&sched_lock); 1916 FOREACH_THREAD_IN_PROC(p, td0) { 1917 if (TD_IS_SLEEPING(td0) && 1918 (td0->td_flags & TDF_SINTR) && 1919 !TD_IS_SUSPENDED(td0)) { 1920 thread_suspend_one(td0); 1921 } else if (td != td0) { 1922 td0->td_flags |= TDF_ASTPENDING; 1923 } 1924 } 1925 thread_stopped(p); 1926 if (p->p_numthreads == p->p_suspcount) { 1927 SIGDELSET(p->p_siglist, p->p_xstat); 1928 FOREACH_THREAD_IN_PROC(p, td0) 1929 SIGDELSET(td0->td_siglist, p->p_xstat); 1930 } 1931 mtx_unlock_spin(&sched_lock); 1932 goto out; 1933 } 1934 else 1935 goto runfast; 1936 /* NOTREACHED */ 1937 } else { 1938 /* Not in "NORMAL" state. discard the signal. */ 1939 SIGDELSET(*siglist, sig); 1940 goto out; 1941 } 1942 1943 /* 1944 * The process is not stopped so we need to apply the signal to all the 1945 * running threads. 1946 */ 1947 1948 runfast: 1949 mtx_lock_spin(&sched_lock); 1950 tdsigwakeup(td, sig, action); 1951 thread_unsuspend(p); 1952 mtx_unlock_spin(&sched_lock); 1953 out: 1954 /* If we jump here, sched_lock should not be owned. */ 1955 mtx_assert(&sched_lock, MA_NOTOWNED); 1956 } 1957 1958 /* 1959 * The force of a signal has been directed against a single 1960 * thread. We need to see what we can do about knocking it 1961 * out of any sleep it may be in etc. 1962 */ 1963 static void 1964 tdsigwakeup(struct thread *td, int sig, sig_t action) 1965 { 1966 struct proc *p = td->td_proc; 1967 register int prop; 1968 1969 PROC_LOCK_ASSERT(p, MA_OWNED); 1970 mtx_assert(&sched_lock, MA_OWNED); 1971 prop = sigprop(sig); 1972 1973 /* 1974 * Bring the priority of a thread up if we want it to get 1975 * killed in this lifetime. 1976 */ 1977 if (action == SIG_DFL && (prop & SA_KILL)) { 1978 if (p->p_nice > 0) 1979 sched_nice(td->td_proc, 0); 1980 if (td->td_priority > PUSER) 1981 sched_prio(td, PUSER); 1982 } 1983 1984 if (TD_ON_SLEEPQ(td)) { 1985 /* 1986 * If thread is sleeping uninterruptibly 1987 * we can't interrupt the sleep... the signal will 1988 * be noticed when the process returns through 1989 * trap() or syscall(). 1990 */ 1991 if ((td->td_flags & TDF_SINTR) == 0) 1992 return; 1993 /* 1994 * Process is sleeping and traced. Make it runnable 1995 * so it can discover the signal in issignal() and stop 1996 * for its parent. 1997 */ 1998 if (p->p_flag & P_TRACED) { 1999 p->p_flag &= ~P_STOPPED_TRACE; 2000 } else { 2001 /* 2002 * If SIGCONT is default (or ignored) and process is 2003 * asleep, we are finished; the process should not 2004 * be awakened. 2005 */ 2006 if ((prop & SA_CONT) && action == SIG_DFL) { 2007 SIGDELSET(p->p_siglist, sig); 2008 /* 2009 * It may be on either list in this state. 2010 * Remove from both for now. 2011 */ 2012 SIGDELSET(td->td_siglist, sig); 2013 return; 2014 } 2015 2016 /* 2017 * Give low priority threads a better chance to run. 2018 */ 2019 if (td->td_priority > PUSER) 2020 sched_prio(td, PUSER); 2021 } 2022 sleepq_abort(td); 2023 } else { 2024 /* 2025 * Other states do nothing with the signal immediately, 2026 * other than kicking ourselves if we are running. 2027 * It will either never be noticed, or noticed very soon. 2028 */ 2029 #ifdef SMP 2030 if (TD_IS_RUNNING(td) && td != curthread) 2031 forward_signal(td); 2032 #endif 2033 } 2034 } 2035 2036 int 2037 ptracestop(struct thread *td, int sig) 2038 { 2039 struct proc *p = td->td_proc; 2040 struct thread *td0; 2041 2042 PROC_LOCK_ASSERT(p, MA_OWNED); 2043 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2044 &p->p_mtx.mtx_object, "Stopping for traced signal"); 2045 2046 mtx_lock_spin(&sched_lock); 2047 td->td_flags |= TDF_XSIG; 2048 mtx_unlock_spin(&sched_lock); 2049 td->td_xsig = sig; 2050 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2051 if (p->p_flag & P_SINGLE_EXIT) { 2052 mtx_lock_spin(&sched_lock); 2053 td->td_flags &= ~TDF_XSIG; 2054 mtx_unlock_spin(&sched_lock); 2055 return (sig); 2056 } 2057 /* 2058 * Just make wait() to work, the last stopped thread 2059 * will win. 2060 */ 2061 p->p_xstat = sig; 2062 p->p_xthread = td; 2063 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2064 mtx_lock_spin(&sched_lock); 2065 FOREACH_THREAD_IN_PROC(p, td0) { 2066 if (TD_IS_SLEEPING(td0) && 2067 (td0->td_flags & TDF_SINTR) && 2068 !TD_IS_SUSPENDED(td0)) { 2069 thread_suspend_one(td0); 2070 } else if (td != td0) { 2071 td0->td_flags |= TDF_ASTPENDING; 2072 } 2073 } 2074 stopme: 2075 thread_stopped(p); 2076 thread_suspend_one(td); 2077 PROC_UNLOCK(p); 2078 DROP_GIANT(); 2079 mi_switch(SW_VOL, NULL); 2080 mtx_unlock_spin(&sched_lock); 2081 PICKUP_GIANT(); 2082 PROC_LOCK(p); 2083 if (!(p->p_flag & P_TRACED)) 2084 break; 2085 if (td->td_flags & TDF_DBSUSPEND) { 2086 if (p->p_flag & P_SINGLE_EXIT) 2087 break; 2088 mtx_lock_spin(&sched_lock); 2089 goto stopme; 2090 } 2091 } 2092 return (td->td_xsig); 2093 } 2094 2095 /* 2096 * If the current process has received a signal (should be caught or cause 2097 * termination, should interrupt current syscall), return the signal number. 2098 * Stop signals with default action are processed immediately, then cleared; 2099 * they aren't returned. This is checked after each entry to the system for 2100 * a syscall or trap (though this can usually be done without calling issignal 2101 * by checking the pending signal masks in cursig.) The normal call 2102 * sequence is 2103 * 2104 * while (sig = cursig(curthread)) 2105 * postsig(sig); 2106 */ 2107 static int 2108 issignal(td) 2109 struct thread *td; 2110 { 2111 struct proc *p; 2112 struct sigacts *ps; 2113 sigset_t sigpending; 2114 int sig, prop, newsig; 2115 struct thread *td0; 2116 2117 p = td->td_proc; 2118 ps = p->p_sigacts; 2119 mtx_assert(&ps->ps_mtx, MA_OWNED); 2120 PROC_LOCK_ASSERT(p, MA_OWNED); 2121 for (;;) { 2122 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2123 2124 sigpending = td->td_siglist; 2125 SIGSETNAND(sigpending, td->td_sigmask); 2126 2127 if (p->p_flag & P_PPWAIT) 2128 SIG_STOPSIGMASK(sigpending); 2129 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2130 return (0); 2131 sig = sig_ffs(&sigpending); 2132 2133 if (p->p_stops & S_SIG) { 2134 mtx_unlock(&ps->ps_mtx); 2135 stopevent(p, S_SIG, sig); 2136 mtx_lock(&ps->ps_mtx); 2137 } 2138 2139 /* 2140 * We should see pending but ignored signals 2141 * only if P_TRACED was on when they were posted. 2142 */ 2143 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2144 SIGDELSET(td->td_siglist, sig); 2145 if (td->td_pflags & TDP_SA) 2146 SIGADDSET(td->td_sigmask, sig); 2147 continue; 2148 } 2149 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2150 /* 2151 * If traced, always stop. 2152 */ 2153 mtx_unlock(&ps->ps_mtx); 2154 newsig = ptracestop(td, sig); 2155 mtx_lock(&ps->ps_mtx); 2156 2157 /* 2158 * If parent wants us to take the signal, 2159 * then it will leave it in p->p_xstat; 2160 * otherwise we just look for signals again. 2161 */ 2162 SIGDELSET(td->td_siglist, sig); /* clear old signal */ 2163 if (td->td_pflags & TDP_SA) 2164 SIGADDSET(td->td_sigmask, sig); 2165 if (newsig == 0) 2166 continue; 2167 sig = newsig; 2168 /* 2169 * If the traced bit got turned off, go back up 2170 * to the top to rescan signals. This ensures 2171 * that p_sig* and p_sigact are consistent. 2172 */ 2173 if ((p->p_flag & P_TRACED) == 0) 2174 continue; 2175 2176 /* 2177 * Put the new signal into td_siglist. If the 2178 * signal is being masked, look for other signals. 2179 */ 2180 SIGADDSET(td->td_siglist, sig); 2181 if (td->td_pflags & TDP_SA) 2182 SIGDELSET(td->td_sigmask, sig); 2183 if (SIGISMEMBER(td->td_sigmask, sig)) 2184 continue; 2185 signotify(td); 2186 } 2187 2188 prop = sigprop(sig); 2189 2190 /* 2191 * Decide whether the signal should be returned. 2192 * Return the signal's number, or fall through 2193 * to clear it from the pending mask. 2194 */ 2195 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2196 2197 case (intptr_t)SIG_DFL: 2198 /* 2199 * Don't take default actions on system processes. 2200 */ 2201 if (p->p_pid <= 1) { 2202 #ifdef DIAGNOSTIC 2203 /* 2204 * Are you sure you want to ignore SIGSEGV 2205 * in init? XXX 2206 */ 2207 printf("Process (pid %lu) got signal %d\n", 2208 (u_long)p->p_pid, sig); 2209 #endif 2210 break; /* == ignore */ 2211 } 2212 /* 2213 * If there is a pending stop signal to process 2214 * with default action, stop here, 2215 * then clear the signal. However, 2216 * if process is member of an orphaned 2217 * process group, ignore tty stop signals. 2218 */ 2219 if (prop & SA_STOP) { 2220 if (p->p_flag & P_TRACED || 2221 (p->p_pgrp->pg_jobc == 0 && 2222 prop & SA_TTYSTOP)) 2223 break; /* == ignore */ 2224 mtx_unlock(&ps->ps_mtx); 2225 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2226 &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2227 p->p_flag |= P_STOPPED_SIG; 2228 p->p_xstat = sig; 2229 p->p_xthread = td; 2230 mtx_lock_spin(&sched_lock); 2231 FOREACH_THREAD_IN_PROC(p, td0) { 2232 if (TD_IS_SLEEPING(td0) && 2233 (td0->td_flags & TDF_SINTR) && 2234 !TD_IS_SUSPENDED(td0)) { 2235 thread_suspend_one(td0); 2236 } else if (td != td0) { 2237 td0->td_flags |= TDF_ASTPENDING; 2238 } 2239 } 2240 thread_stopped(p); 2241 thread_suspend_one(td); 2242 PROC_UNLOCK(p); 2243 DROP_GIANT(); 2244 mi_switch(SW_INVOL, NULL); 2245 mtx_unlock_spin(&sched_lock); 2246 PICKUP_GIANT(); 2247 PROC_LOCK(p); 2248 mtx_lock(&ps->ps_mtx); 2249 break; 2250 } else if (prop & SA_IGNORE) { 2251 /* 2252 * Except for SIGCONT, shouldn't get here. 2253 * Default action is to ignore; drop it. 2254 */ 2255 break; /* == ignore */ 2256 } else 2257 return (sig); 2258 /*NOTREACHED*/ 2259 2260 case (intptr_t)SIG_IGN: 2261 /* 2262 * Masking above should prevent us ever trying 2263 * to take action on an ignored signal other 2264 * than SIGCONT, unless process is traced. 2265 */ 2266 if ((prop & SA_CONT) == 0 && 2267 (p->p_flag & P_TRACED) == 0) 2268 printf("issignal\n"); 2269 break; /* == ignore */ 2270 2271 default: 2272 /* 2273 * This signal has an action, let 2274 * postsig() process it. 2275 */ 2276 return (sig); 2277 } 2278 SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2279 } 2280 /* NOTREACHED */ 2281 } 2282 2283 /* 2284 * Put the argument process into the stopped state and notify the parent 2285 * via wakeup. Signals are handled elsewhere. The process must not be 2286 * on the run queue. Must be called with the proc p locked. 2287 */ 2288 static void 2289 stop(struct proc *p) 2290 { 2291 2292 PROC_LOCK_ASSERT(p, MA_OWNED); 2293 p->p_flag |= P_STOPPED_SIG; 2294 p->p_flag &= ~P_WAITED; 2295 wakeup(p->p_pptr); 2296 } 2297 2298 /* 2299 * MPSAFE 2300 */ 2301 void 2302 thread_stopped(struct proc *p) 2303 { 2304 struct proc *p1 = curthread->td_proc; 2305 struct sigacts *ps; 2306 int n; 2307 2308 PROC_LOCK_ASSERT(p, MA_OWNED); 2309 mtx_assert(&sched_lock, MA_OWNED); 2310 n = p->p_suspcount; 2311 if (p == p1) 2312 n++; 2313 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2314 mtx_unlock_spin(&sched_lock); 2315 stop(p); 2316 PROC_LOCK(p->p_pptr); 2317 ps = p->p_pptr->p_sigacts; 2318 mtx_lock(&ps->ps_mtx); 2319 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 2320 mtx_unlock(&ps->ps_mtx); 2321 psignal(p->p_pptr, SIGCHLD); 2322 } else 2323 mtx_unlock(&ps->ps_mtx); 2324 PROC_UNLOCK(p->p_pptr); 2325 mtx_lock_spin(&sched_lock); 2326 } 2327 } 2328 2329 /* 2330 * Take the action for the specified signal 2331 * from the current set of pending signals. 2332 */ 2333 void 2334 postsig(sig) 2335 register int sig; 2336 { 2337 struct thread *td = curthread; 2338 register struct proc *p = td->td_proc; 2339 struct sigacts *ps; 2340 sig_t action; 2341 sigset_t returnmask; 2342 int code; 2343 2344 KASSERT(sig != 0, ("postsig")); 2345 2346 PROC_LOCK_ASSERT(p, MA_OWNED); 2347 ps = p->p_sigacts; 2348 mtx_assert(&ps->ps_mtx, MA_OWNED); 2349 SIGDELSET(td->td_siglist, sig); 2350 action = ps->ps_sigact[_SIG_IDX(sig)]; 2351 #ifdef KTRACE 2352 if (KTRPOINT(td, KTR_PSIG)) 2353 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2354 &td->td_oldsigmask : &td->td_sigmask, 0); 2355 #endif 2356 if (p->p_stops & S_SIG) { 2357 mtx_unlock(&ps->ps_mtx); 2358 stopevent(p, S_SIG, sig); 2359 mtx_lock(&ps->ps_mtx); 2360 } 2361 2362 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2363 /* 2364 * Default action, where the default is to kill 2365 * the process. (Other cases were ignored above.) 2366 */ 2367 mtx_unlock(&ps->ps_mtx); 2368 sigexit(td, sig); 2369 /* NOTREACHED */ 2370 } else { 2371 if (td->td_pflags & TDP_SA) { 2372 if (sig == SIGKILL) { 2373 mtx_unlock(&ps->ps_mtx); 2374 sigexit(td, sig); 2375 } 2376 } 2377 2378 /* 2379 * If we get here, the signal must be caught. 2380 */ 2381 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2382 ("postsig action")); 2383 /* 2384 * Set the new mask value and also defer further 2385 * occurrences of this signal. 2386 * 2387 * Special case: user has done a sigsuspend. Here the 2388 * current mask is not of interest, but rather the 2389 * mask from before the sigsuspend is what we want 2390 * restored after the signal processing is completed. 2391 */ 2392 if (td->td_pflags & TDP_OLDMASK) { 2393 returnmask = td->td_oldsigmask; 2394 td->td_pflags &= ~TDP_OLDMASK; 2395 } else 2396 returnmask = td->td_sigmask; 2397 2398 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 2399 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2400 SIGADDSET(td->td_sigmask, sig); 2401 2402 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2403 /* 2404 * See kern_sigaction() for origin of this code. 2405 */ 2406 SIGDELSET(ps->ps_sigcatch, sig); 2407 if (sig != SIGCONT && 2408 sigprop(sig) & SA_IGNORE) 2409 SIGADDSET(ps->ps_sigignore, sig); 2410 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2411 } 2412 p->p_stats->p_ru.ru_nsignals++; 2413 if (p->p_sig != sig) { 2414 code = 0; 2415 } else { 2416 code = p->p_code; 2417 p->p_code = 0; 2418 p->p_sig = 0; 2419 } 2420 if (td->td_pflags & TDP_SA) 2421 thread_signal_add(curthread, sig); 2422 else 2423 (*p->p_sysent->sv_sendsig)(action, sig, 2424 &returnmask, code); 2425 } 2426 } 2427 2428 /* 2429 * Kill the current process for stated reason. 2430 */ 2431 void 2432 killproc(p, why) 2433 struct proc *p; 2434 char *why; 2435 { 2436 2437 PROC_LOCK_ASSERT(p, MA_OWNED); 2438 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 2439 p, p->p_pid, p->p_comm); 2440 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2441 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2442 psignal(p, SIGKILL); 2443 } 2444 2445 /* 2446 * Force the current process to exit with the specified signal, dumping core 2447 * if appropriate. We bypass the normal tests for masked and caught signals, 2448 * allowing unrecoverable failures to terminate the process without changing 2449 * signal state. Mark the accounting record with the signal termination. 2450 * If dumping core, save the signal number for the debugger. Calls exit and 2451 * does not return. 2452 * 2453 * MPSAFE 2454 */ 2455 void 2456 sigexit(td, sig) 2457 struct thread *td; 2458 int sig; 2459 { 2460 struct proc *p = td->td_proc; 2461 2462 PROC_LOCK_ASSERT(p, MA_OWNED); 2463 p->p_acflag |= AXSIG; 2464 if (sigprop(sig) & SA_CORE) { 2465 p->p_sig = sig; 2466 /* 2467 * Log signals which would cause core dumps 2468 * (Log as LOG_INFO to appease those who don't want 2469 * these messages.) 2470 * XXX : Todo, as well as euid, write out ruid too 2471 * Note that coredump() drops proc lock. 2472 */ 2473 if (coredump(td) == 0) 2474 sig |= WCOREFLAG; 2475 if (kern_logsigexit) 2476 log(LOG_INFO, 2477 "pid %d (%s), uid %d: exited on signal %d%s\n", 2478 p->p_pid, p->p_comm, 2479 td->td_ucred ? td->td_ucred->cr_uid : -1, 2480 sig &~ WCOREFLAG, 2481 sig & WCOREFLAG ? " (core dumped)" : ""); 2482 } else 2483 PROC_UNLOCK(p); 2484 exit1(td, W_EXITCODE(0, sig)); 2485 /* NOTREACHED */ 2486 } 2487 2488 static char corefilename[MAXPATHLEN] = {"%N.core"}; 2489 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2490 sizeof(corefilename), "process corefile name format string"); 2491 2492 /* 2493 * expand_name(name, uid, pid) 2494 * Expand the name described in corefilename, using name, uid, and pid. 2495 * corefilename is a printf-like string, with three format specifiers: 2496 * %N name of process ("name") 2497 * %P process id (pid) 2498 * %U user id (uid) 2499 * For example, "%N.core" is the default; they can be disabled completely 2500 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2501 * This is controlled by the sysctl variable kern.corefile (see above). 2502 */ 2503 2504 static char * 2505 expand_name(name, uid, pid) 2506 const char *name; 2507 uid_t uid; 2508 pid_t pid; 2509 { 2510 const char *format, *appendstr; 2511 char *temp; 2512 char buf[11]; /* Buffer for pid/uid -- max 4B */ 2513 size_t i, l, n; 2514 2515 format = corefilename; 2516 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 2517 if (temp == NULL) 2518 return (NULL); 2519 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2520 switch (format[i]) { 2521 case '%': /* Format character */ 2522 i++; 2523 switch (format[i]) { 2524 case '%': 2525 appendstr = "%"; 2526 break; 2527 case 'N': /* process name */ 2528 appendstr = name; 2529 break; 2530 case 'P': /* process id */ 2531 sprintf(buf, "%u", pid); 2532 appendstr = buf; 2533 break; 2534 case 'U': /* user id */ 2535 sprintf(buf, "%u", uid); 2536 appendstr = buf; 2537 break; 2538 default: 2539 appendstr = ""; 2540 log(LOG_ERR, 2541 "Unknown format character %c in `%s'\n", 2542 format[i], format); 2543 } 2544 l = strlen(appendstr); 2545 if ((n + l) >= MAXPATHLEN) 2546 goto toolong; 2547 memcpy(temp + n, appendstr, l); 2548 n += l; 2549 break; 2550 default: 2551 temp[n++] = format[i]; 2552 } 2553 } 2554 if (format[i] != '\0') 2555 goto toolong; 2556 return (temp); 2557 toolong: 2558 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 2559 (long)pid, name, (u_long)uid); 2560 free(temp, M_TEMP); 2561 return (NULL); 2562 } 2563 2564 /* 2565 * Dump a process' core. The main routine does some 2566 * policy checking, and creates the name of the coredump; 2567 * then it passes on a vnode and a size limit to the process-specific 2568 * coredump routine if there is one; if there _is not_ one, it returns 2569 * ENOSYS; otherwise it returns the error from the process-specific routine. 2570 */ 2571 2572 static int 2573 coredump(struct thread *td) 2574 { 2575 struct proc *p = td->td_proc; 2576 register struct vnode *vp; 2577 register struct ucred *cred = td->td_ucred; 2578 struct flock lf; 2579 struct nameidata nd; 2580 struct vattr vattr; 2581 int error, error1, flags, locked; 2582 struct mount *mp; 2583 char *name; /* name of corefile */ 2584 off_t limit; 2585 2586 PROC_LOCK_ASSERT(p, MA_OWNED); 2587 _STOPEVENT(p, S_CORE, 0); 2588 2589 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2590 PROC_UNLOCK(p); 2591 return (EFAULT); 2592 } 2593 2594 /* 2595 * Note that the bulk of limit checking is done after 2596 * the corefile is created. The exception is if the limit 2597 * for corefiles is 0, in which case we don't bother 2598 * creating the corefile at all. This layout means that 2599 * a corefile is truncated instead of not being created, 2600 * if it is larger than the limit. 2601 */ 2602 limit = (off_t)lim_cur(p, RLIMIT_CORE); 2603 PROC_UNLOCK(p); 2604 if (limit == 0) 2605 return (EFBIG); 2606 2607 mtx_lock(&Giant); 2608 restart: 2609 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2610 if (name == NULL) { 2611 mtx_unlock(&Giant); 2612 return (EINVAL); 2613 } 2614 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2615 flags = O_CREAT | FWRITE | O_NOFOLLOW; 2616 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 2617 free(name, M_TEMP); 2618 if (error) { 2619 mtx_unlock(&Giant); 2620 return (error); 2621 } 2622 NDFREE(&nd, NDF_ONLY_PNBUF); 2623 vp = nd.ni_vp; 2624 2625 /* Don't dump to non-regular files or files with links. */ 2626 if (vp->v_type != VREG || 2627 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2628 VOP_UNLOCK(vp, 0, td); 2629 error = EFAULT; 2630 goto out; 2631 } 2632 2633 VOP_UNLOCK(vp, 0, td); 2634 lf.l_whence = SEEK_SET; 2635 lf.l_start = 0; 2636 lf.l_len = 0; 2637 lf.l_type = F_WRLCK; 2638 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 2639 2640 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2641 lf.l_type = F_UNLCK; 2642 if (locked) 2643 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2644 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2645 return (error); 2646 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2647 return (error); 2648 goto restart; 2649 } 2650 2651 VATTR_NULL(&vattr); 2652 vattr.va_size = 0; 2653 if (set_core_nodump_flag) 2654 vattr.va_flags = UF_NODUMP; 2655 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2656 VOP_LEASE(vp, td, cred, LEASE_WRITE); 2657 VOP_SETATTR(vp, &vattr, cred, td); 2658 VOP_UNLOCK(vp, 0, td); 2659 PROC_LOCK(p); 2660 p->p_acflag |= ACORE; 2661 PROC_UNLOCK(p); 2662 2663 error = p->p_sysent->sv_coredump ? 2664 p->p_sysent->sv_coredump(td, vp, limit) : 2665 ENOSYS; 2666 2667 if (locked) { 2668 lf.l_type = F_UNLCK; 2669 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2670 } 2671 vn_finished_write(mp); 2672 out: 2673 error1 = vn_close(vp, FWRITE, cred, td); 2674 mtx_unlock(&Giant); 2675 if (error == 0) 2676 error = error1; 2677 return (error); 2678 } 2679 2680 /* 2681 * Nonexistent system call-- signal process (may want to handle it). 2682 * Flag error in case process won't see signal immediately (blocked or ignored). 2683 */ 2684 #ifndef _SYS_SYSPROTO_H_ 2685 struct nosys_args { 2686 int dummy; 2687 }; 2688 #endif 2689 /* 2690 * MPSAFE 2691 */ 2692 /* ARGSUSED */ 2693 int 2694 nosys(td, args) 2695 struct thread *td; 2696 struct nosys_args *args; 2697 { 2698 struct proc *p = td->td_proc; 2699 2700 PROC_LOCK(p); 2701 psignal(p, SIGSYS); 2702 PROC_UNLOCK(p); 2703 return (ENOSYS); 2704 } 2705 2706 /* 2707 * Send a SIGIO or SIGURG signal to a process or process group using 2708 * stored credentials rather than those of the current process. 2709 */ 2710 void 2711 pgsigio(sigiop, sig, checkctty) 2712 struct sigio **sigiop; 2713 int sig, checkctty; 2714 { 2715 struct sigio *sigio; 2716 2717 SIGIO_LOCK(); 2718 sigio = *sigiop; 2719 if (sigio == NULL) { 2720 SIGIO_UNLOCK(); 2721 return; 2722 } 2723 if (sigio->sio_pgid > 0) { 2724 PROC_LOCK(sigio->sio_proc); 2725 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 2726 psignal(sigio->sio_proc, sig); 2727 PROC_UNLOCK(sigio->sio_proc); 2728 } else if (sigio->sio_pgid < 0) { 2729 struct proc *p; 2730 2731 PGRP_LOCK(sigio->sio_pgrp); 2732 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2733 PROC_LOCK(p); 2734 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2735 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 2736 psignal(p, sig); 2737 PROC_UNLOCK(p); 2738 } 2739 PGRP_UNLOCK(sigio->sio_pgrp); 2740 } 2741 SIGIO_UNLOCK(); 2742 } 2743 2744 static int 2745 filt_sigattach(struct knote *kn) 2746 { 2747 struct proc *p = curproc; 2748 2749 kn->kn_ptr.p_proc = p; 2750 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2751 2752 knlist_add(&p->p_klist, kn, 0); 2753 2754 return (0); 2755 } 2756 2757 static void 2758 filt_sigdetach(struct knote *kn) 2759 { 2760 struct proc *p = kn->kn_ptr.p_proc; 2761 2762 knlist_remove(&p->p_klist, kn, 0); 2763 } 2764 2765 /* 2766 * signal knotes are shared with proc knotes, so we apply a mask to 2767 * the hint in order to differentiate them from process hints. This 2768 * could be avoided by using a signal-specific knote list, but probably 2769 * isn't worth the trouble. 2770 */ 2771 static int 2772 filt_signal(struct knote *kn, long hint) 2773 { 2774 2775 if (hint & NOTE_SIGNAL) { 2776 hint &= ~NOTE_SIGNAL; 2777 2778 if (kn->kn_id == hint) 2779 kn->kn_data++; 2780 } 2781 return (kn->kn_data != 0); 2782 } 2783 2784 struct sigacts * 2785 sigacts_alloc(void) 2786 { 2787 struct sigacts *ps; 2788 2789 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 2790 ps->ps_refcnt = 1; 2791 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 2792 return (ps); 2793 } 2794 2795 void 2796 sigacts_free(struct sigacts *ps) 2797 { 2798 2799 mtx_lock(&ps->ps_mtx); 2800 ps->ps_refcnt--; 2801 if (ps->ps_refcnt == 0) { 2802 mtx_destroy(&ps->ps_mtx); 2803 free(ps, M_SUBPROC); 2804 } else 2805 mtx_unlock(&ps->ps_mtx); 2806 } 2807 2808 struct sigacts * 2809 sigacts_hold(struct sigacts *ps) 2810 { 2811 mtx_lock(&ps->ps_mtx); 2812 ps->ps_refcnt++; 2813 mtx_unlock(&ps->ps_mtx); 2814 return (ps); 2815 } 2816 2817 void 2818 sigacts_copy(struct sigacts *dest, struct sigacts *src) 2819 { 2820 2821 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 2822 mtx_lock(&src->ps_mtx); 2823 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 2824 mtx_unlock(&src->ps_mtx); 2825 } 2826 2827 int 2828 sigacts_shared(struct sigacts *ps) 2829 { 2830 int shared; 2831 2832 mtx_lock(&ps->ps_mtx); 2833 shared = ps->ps_refcnt > 1; 2834 mtx_unlock(&ps->ps_mtx); 2835 return (shared); 2836 } 2837