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; 850 int sig; 851 int hz; 852 int i; 853 854 p = td->td_proc; 855 error = 0; 856 sig = 0; 857 SIG_CANTMASK(waitset); 858 859 PROC_LOCK(p); 860 ps = p->p_sigacts; 861 savedmask = td->td_sigmask; 862 863 again: 864 for (i = 1; i <= _SIG_MAXSIG; ++i) { 865 if (!SIGISMEMBER(waitset, i)) 866 continue; 867 if (SIGISMEMBER(td->td_siglist, i)) { 868 SIGFILLSET(td->td_sigmask); 869 SIG_CANTMASK(td->td_sigmask); 870 SIGDELSET(td->td_sigmask, i); 871 mtx_lock(&ps->ps_mtx); 872 sig = cursig(td); 873 i = 0; 874 mtx_unlock(&ps->ps_mtx); 875 } else if (SIGISMEMBER(p->p_siglist, i)) { 876 if (p->p_flag & P_SA) { 877 p->p_flag |= P_SIGEVENT; 878 wakeup(&p->p_siglist); 879 } 880 SIGDELSET(p->p_siglist, i); 881 SIGADDSET(td->td_siglist, i); 882 SIGFILLSET(td->td_sigmask); 883 SIG_CANTMASK(td->td_sigmask); 884 SIGDELSET(td->td_sigmask, i); 885 mtx_lock(&ps->ps_mtx); 886 sig = cursig(td); 887 i = 0; 888 mtx_unlock(&ps->ps_mtx); 889 } 890 if (sig) { 891 td->td_sigmask = savedmask; 892 signotify(td); 893 goto out; 894 } 895 } 896 if (error) 897 goto out; 898 899 td->td_sigmask = savedmask; 900 signotify(td); 901 sigset = td->td_siglist; 902 SIGSETOR(sigset, p->p_siglist); 903 SIGSETAND(sigset, waitset); 904 if (!SIGISEMPTY(sigset)) 905 goto again; 906 907 /* 908 * POSIX says this must be checked after looking for pending 909 * signals. 910 */ 911 if (timeout) { 912 struct timeval tv; 913 914 if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) { 915 error = EINVAL; 916 goto out; 917 } 918 if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) { 919 error = EAGAIN; 920 goto out; 921 } 922 TIMESPEC_TO_TIMEVAL(&tv, timeout); 923 hz = tvtohz(&tv); 924 } else 925 hz = 0; 926 927 td->td_waitset = &waitset; 928 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 929 td->td_waitset = NULL; 930 if (error == 0) /* surplus wakeup ? */ 931 error = EINTR; 932 goto again; 933 934 out: 935 if (sig) { 936 sig_t action; 937 938 error = 0; 939 mtx_lock(&ps->ps_mtx); 940 action = ps->ps_sigact[_SIG_IDX(sig)]; 941 mtx_unlock(&ps->ps_mtx); 942 #ifdef KTRACE 943 if (KTRPOINT(td, KTR_PSIG)) 944 ktrpsig(sig, action, &td->td_sigmask, 0); 945 #endif 946 _STOPEVENT(p, S_SIG, sig); 947 948 SIGDELSET(td->td_siglist, sig); 949 info->si_signo = sig; 950 info->si_code = 0; 951 } 952 PROC_UNLOCK(p); 953 return (error); 954 } 955 956 /* 957 * MPSAFE 958 */ 959 int 960 sigpending(td, uap) 961 struct thread *td; 962 struct sigpending_args *uap; 963 { 964 struct proc *p = td->td_proc; 965 sigset_t siglist; 966 967 PROC_LOCK(p); 968 siglist = p->p_siglist; 969 SIGSETOR(siglist, td->td_siglist); 970 PROC_UNLOCK(p); 971 return (copyout(&siglist, uap->set, sizeof(sigset_t))); 972 } 973 974 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 975 #ifndef _SYS_SYSPROTO_H_ 976 struct osigpending_args { 977 int dummy; 978 }; 979 #endif 980 /* 981 * MPSAFE 982 */ 983 int 984 osigpending(td, uap) 985 struct thread *td; 986 struct osigpending_args *uap; 987 { 988 struct proc *p = td->td_proc; 989 sigset_t siglist; 990 991 PROC_LOCK(p); 992 siglist = p->p_siglist; 993 SIGSETOR(siglist, td->td_siglist); 994 PROC_UNLOCK(p); 995 SIG2OSIG(siglist, td->td_retval[0]); 996 return (0); 997 } 998 #endif /* COMPAT_43 */ 999 1000 #if defined(COMPAT_43) 1001 /* 1002 * Generalized interface signal handler, 4.3-compatible. 1003 */ 1004 #ifndef _SYS_SYSPROTO_H_ 1005 struct osigvec_args { 1006 int signum; 1007 struct sigvec *nsv; 1008 struct sigvec *osv; 1009 }; 1010 #endif 1011 /* 1012 * MPSAFE 1013 */ 1014 /* ARGSUSED */ 1015 int 1016 osigvec(td, uap) 1017 struct thread *td; 1018 register struct osigvec_args *uap; 1019 { 1020 struct sigvec vec; 1021 struct sigaction nsa, osa; 1022 register struct sigaction *nsap, *osap; 1023 int error; 1024 1025 if (uap->signum <= 0 || uap->signum >= ONSIG) 1026 return (EINVAL); 1027 nsap = (uap->nsv != NULL) ? &nsa : NULL; 1028 osap = (uap->osv != NULL) ? &osa : NULL; 1029 if (nsap) { 1030 error = copyin(uap->nsv, &vec, sizeof(vec)); 1031 if (error) 1032 return (error); 1033 nsap->sa_handler = vec.sv_handler; 1034 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 1035 nsap->sa_flags = vec.sv_flags; 1036 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1037 } 1038 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1039 if (osap && !error) { 1040 vec.sv_handler = osap->sa_handler; 1041 SIG2OSIG(osap->sa_mask, vec.sv_mask); 1042 vec.sv_flags = osap->sa_flags; 1043 vec.sv_flags &= ~SA_NOCLDWAIT; 1044 vec.sv_flags ^= SA_RESTART; 1045 error = copyout(&vec, uap->osv, sizeof(vec)); 1046 } 1047 return (error); 1048 } 1049 1050 #ifndef _SYS_SYSPROTO_H_ 1051 struct osigblock_args { 1052 int mask; 1053 }; 1054 #endif 1055 /* 1056 * MPSAFE 1057 */ 1058 int 1059 osigblock(td, uap) 1060 register struct thread *td; 1061 struct osigblock_args *uap; 1062 { 1063 struct proc *p = td->td_proc; 1064 sigset_t set; 1065 1066 OSIG2SIG(uap->mask, set); 1067 SIG_CANTMASK(set); 1068 PROC_LOCK(p); 1069 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1070 SIGSETOR(td->td_sigmask, set); 1071 PROC_UNLOCK(p); 1072 return (0); 1073 } 1074 1075 #ifndef _SYS_SYSPROTO_H_ 1076 struct osigsetmask_args { 1077 int mask; 1078 }; 1079 #endif 1080 /* 1081 * MPSAFE 1082 */ 1083 int 1084 osigsetmask(td, uap) 1085 struct thread *td; 1086 struct osigsetmask_args *uap; 1087 { 1088 struct proc *p = td->td_proc; 1089 sigset_t set; 1090 1091 OSIG2SIG(uap->mask, set); 1092 SIG_CANTMASK(set); 1093 PROC_LOCK(p); 1094 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1095 SIGSETLO(td->td_sigmask, set); 1096 signotify(td); 1097 PROC_UNLOCK(p); 1098 return (0); 1099 } 1100 #endif /* COMPAT_43 */ 1101 1102 /* 1103 * Suspend process until signal, providing mask to be set 1104 * in the meantime. 1105 ***** XXXKSE this doesn't make sense under KSE. 1106 ***** Do we suspend the thread or all threads in the process? 1107 ***** How do we suspend threads running NOW on another processor? 1108 */ 1109 #ifndef _SYS_SYSPROTO_H_ 1110 struct sigsuspend_args { 1111 const sigset_t *sigmask; 1112 }; 1113 #endif 1114 /* 1115 * MPSAFE 1116 */ 1117 /* ARGSUSED */ 1118 int 1119 sigsuspend(td, uap) 1120 struct thread *td; 1121 struct sigsuspend_args *uap; 1122 { 1123 sigset_t mask; 1124 int error; 1125 1126 error = copyin(uap->sigmask, &mask, sizeof(mask)); 1127 if (error) 1128 return (error); 1129 return (kern_sigsuspend(td, mask)); 1130 } 1131 1132 int 1133 kern_sigsuspend(struct thread *td, sigset_t mask) 1134 { 1135 struct proc *p = td->td_proc; 1136 1137 /* 1138 * When returning from sigsuspend, we want 1139 * the old mask to be restored after the 1140 * signal handler has finished. Thus, we 1141 * save it here and mark the sigacts structure 1142 * to indicate this. 1143 */ 1144 PROC_LOCK(p); 1145 td->td_oldsigmask = td->td_sigmask; 1146 td->td_pflags |= TDP_OLDMASK; 1147 SIG_CANTMASK(mask); 1148 td->td_sigmask = mask; 1149 signotify(td); 1150 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 1151 /* void */; 1152 PROC_UNLOCK(p); 1153 /* always return EINTR rather than ERESTART... */ 1154 return (EINTR); 1155 } 1156 1157 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1158 /* 1159 * Compatibility sigsuspend call for old binaries. Note nonstandard calling 1160 * convention: libc stub passes mask, not pointer, to save a copyin. 1161 */ 1162 #ifndef _SYS_SYSPROTO_H_ 1163 struct osigsuspend_args { 1164 osigset_t mask; 1165 }; 1166 #endif 1167 /* 1168 * MPSAFE 1169 */ 1170 /* ARGSUSED */ 1171 int 1172 osigsuspend(td, uap) 1173 struct thread *td; 1174 struct osigsuspend_args *uap; 1175 { 1176 struct proc *p = td->td_proc; 1177 sigset_t mask; 1178 1179 PROC_LOCK(p); 1180 td->td_oldsigmask = td->td_sigmask; 1181 td->td_pflags |= TDP_OLDMASK; 1182 OSIG2SIG(uap->mask, mask); 1183 SIG_CANTMASK(mask); 1184 SIGSETLO(td->td_sigmask, mask); 1185 signotify(td); 1186 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1187 /* void */; 1188 PROC_UNLOCK(p); 1189 /* always return EINTR rather than ERESTART... */ 1190 return (EINTR); 1191 } 1192 #endif /* COMPAT_43 */ 1193 1194 #if defined(COMPAT_43) 1195 #ifndef _SYS_SYSPROTO_H_ 1196 struct osigstack_args { 1197 struct sigstack *nss; 1198 struct sigstack *oss; 1199 }; 1200 #endif 1201 /* 1202 * MPSAFE 1203 */ 1204 /* ARGSUSED */ 1205 int 1206 osigstack(td, uap) 1207 struct thread *td; 1208 register struct osigstack_args *uap; 1209 { 1210 struct sigstack nss, oss; 1211 int error = 0; 1212 1213 if (uap->nss != NULL) { 1214 error = copyin(uap->nss, &nss, sizeof(nss)); 1215 if (error) 1216 return (error); 1217 } 1218 oss.ss_sp = td->td_sigstk.ss_sp; 1219 oss.ss_onstack = sigonstack(cpu_getstack(td)); 1220 if (uap->nss != NULL) { 1221 td->td_sigstk.ss_sp = nss.ss_sp; 1222 td->td_sigstk.ss_size = 0; 1223 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1224 td->td_pflags |= TDP_ALTSTACK; 1225 } 1226 if (uap->oss != NULL) 1227 error = copyout(&oss, uap->oss, sizeof(oss)); 1228 1229 return (error); 1230 } 1231 #endif /* COMPAT_43 */ 1232 1233 #ifndef _SYS_SYSPROTO_H_ 1234 struct sigaltstack_args { 1235 stack_t *ss; 1236 stack_t *oss; 1237 }; 1238 #endif 1239 /* 1240 * MPSAFE 1241 */ 1242 /* ARGSUSED */ 1243 int 1244 sigaltstack(td, uap) 1245 struct thread *td; 1246 register struct sigaltstack_args *uap; 1247 { 1248 stack_t ss, oss; 1249 int error; 1250 1251 if (uap->ss != NULL) { 1252 error = copyin(uap->ss, &ss, sizeof(ss)); 1253 if (error) 1254 return (error); 1255 } 1256 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 1257 (uap->oss != NULL) ? &oss : NULL); 1258 if (error) 1259 return (error); 1260 if (uap->oss != NULL) 1261 error = copyout(&oss, uap->oss, sizeof(stack_t)); 1262 return (error); 1263 } 1264 1265 int 1266 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 1267 { 1268 struct proc *p = td->td_proc; 1269 int oonstack; 1270 1271 oonstack = sigonstack(cpu_getstack(td)); 1272 1273 if (oss != NULL) { 1274 *oss = td->td_sigstk; 1275 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1276 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1277 } 1278 1279 if (ss != NULL) { 1280 if (oonstack) 1281 return (EPERM); 1282 if ((ss->ss_flags & ~SS_DISABLE) != 0) 1283 return (EINVAL); 1284 if (!(ss->ss_flags & SS_DISABLE)) { 1285 if (ss->ss_size < p->p_sysent->sv_minsigstksz) { 1286 return (ENOMEM); 1287 } 1288 td->td_sigstk = *ss; 1289 td->td_pflags |= TDP_ALTSTACK; 1290 } else { 1291 td->td_pflags &= ~TDP_ALTSTACK; 1292 } 1293 } 1294 return (0); 1295 } 1296 1297 /* 1298 * Common code for kill process group/broadcast kill. 1299 * cp is calling process. 1300 */ 1301 static int 1302 killpg1(td, sig, pgid, all) 1303 register struct thread *td; 1304 int sig, pgid, all; 1305 { 1306 register struct proc *p; 1307 struct pgrp *pgrp; 1308 int nfound = 0; 1309 1310 if (all) { 1311 /* 1312 * broadcast 1313 */ 1314 sx_slock(&allproc_lock); 1315 LIST_FOREACH(p, &allproc, p_list) { 1316 PROC_LOCK(p); 1317 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 1318 p == td->td_proc) { 1319 PROC_UNLOCK(p); 1320 continue; 1321 } 1322 if (p_cansignal(td, p, sig) == 0) { 1323 nfound++; 1324 if (sig) 1325 psignal(p, sig); 1326 } 1327 PROC_UNLOCK(p); 1328 } 1329 sx_sunlock(&allproc_lock); 1330 } else { 1331 sx_slock(&proctree_lock); 1332 if (pgid == 0) { 1333 /* 1334 * zero pgid means send to my process group. 1335 */ 1336 pgrp = td->td_proc->p_pgrp; 1337 PGRP_LOCK(pgrp); 1338 } else { 1339 pgrp = pgfind(pgid); 1340 if (pgrp == NULL) { 1341 sx_sunlock(&proctree_lock); 1342 return (ESRCH); 1343 } 1344 } 1345 sx_sunlock(&proctree_lock); 1346 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1347 PROC_LOCK(p); 1348 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1349 PROC_UNLOCK(p); 1350 continue; 1351 } 1352 if (p_cansignal(td, p, sig) == 0) { 1353 nfound++; 1354 if (sig) 1355 psignal(p, sig); 1356 } 1357 PROC_UNLOCK(p); 1358 } 1359 PGRP_UNLOCK(pgrp); 1360 } 1361 return (nfound ? 0 : ESRCH); 1362 } 1363 1364 #ifndef _SYS_SYSPROTO_H_ 1365 struct kill_args { 1366 int pid; 1367 int signum; 1368 }; 1369 #endif 1370 /* 1371 * MPSAFE 1372 */ 1373 /* ARGSUSED */ 1374 int 1375 kill(td, uap) 1376 register struct thread *td; 1377 register struct kill_args *uap; 1378 { 1379 register struct proc *p; 1380 int error; 1381 1382 if ((u_int)uap->signum > _SIG_MAXSIG) 1383 return (EINVAL); 1384 1385 if (uap->pid > 0) { 1386 /* kill single process */ 1387 if ((p = pfind(uap->pid)) == NULL) { 1388 if ((p = zpfind(uap->pid)) == NULL) 1389 return (ESRCH); 1390 } 1391 error = p_cansignal(td, p, uap->signum); 1392 if (error == 0 && uap->signum) 1393 psignal(p, uap->signum); 1394 PROC_UNLOCK(p); 1395 return (error); 1396 } 1397 switch (uap->pid) { 1398 case -1: /* broadcast signal */ 1399 return (killpg1(td, uap->signum, 0, 1)); 1400 case 0: /* signal own process group */ 1401 return (killpg1(td, uap->signum, 0, 0)); 1402 default: /* negative explicit process group */ 1403 return (killpg1(td, uap->signum, -uap->pid, 0)); 1404 } 1405 /* NOTREACHED */ 1406 } 1407 1408 #if defined(COMPAT_43) 1409 #ifndef _SYS_SYSPROTO_H_ 1410 struct okillpg_args { 1411 int pgid; 1412 int signum; 1413 }; 1414 #endif 1415 /* 1416 * MPSAFE 1417 */ 1418 /* ARGSUSED */ 1419 int 1420 okillpg(td, uap) 1421 struct thread *td; 1422 register struct okillpg_args *uap; 1423 { 1424 1425 if ((u_int)uap->signum > _SIG_MAXSIG) 1426 return (EINVAL); 1427 return (killpg1(td, uap->signum, uap->pgid, 0)); 1428 } 1429 #endif /* COMPAT_43 */ 1430 1431 /* 1432 * Send a signal to a process group. 1433 */ 1434 void 1435 gsignal(pgid, sig) 1436 int pgid, sig; 1437 { 1438 struct pgrp *pgrp; 1439 1440 if (pgid != 0) { 1441 sx_slock(&proctree_lock); 1442 pgrp = pgfind(pgid); 1443 sx_sunlock(&proctree_lock); 1444 if (pgrp != NULL) { 1445 pgsignal(pgrp, sig, 0); 1446 PGRP_UNLOCK(pgrp); 1447 } 1448 } 1449 } 1450 1451 /* 1452 * Send a signal to a process group. If checktty is 1, 1453 * limit to members which have a controlling terminal. 1454 */ 1455 void 1456 pgsignal(pgrp, sig, checkctty) 1457 struct pgrp *pgrp; 1458 int sig, checkctty; 1459 { 1460 register struct proc *p; 1461 1462 if (pgrp) { 1463 PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1464 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1465 PROC_LOCK(p); 1466 if (checkctty == 0 || p->p_flag & P_CONTROLT) 1467 psignal(p, sig); 1468 PROC_UNLOCK(p); 1469 } 1470 } 1471 } 1472 1473 /* 1474 * Send a signal caused by a trap to the current thread. 1475 * If it will be caught immediately, deliver it with correct code. 1476 * Otherwise, post it normally. 1477 * 1478 * MPSAFE 1479 */ 1480 void 1481 trapsignal(struct thread *td, int sig, u_long code) 1482 { 1483 struct sigacts *ps; 1484 struct proc *p; 1485 siginfo_t siginfo; 1486 int error; 1487 1488 p = td->td_proc; 1489 if (td->td_pflags & TDP_SA) { 1490 if (td->td_mailbox == NULL) 1491 thread_user_enter(td); 1492 PROC_LOCK(p); 1493 SIGDELSET(td->td_sigmask, sig); 1494 mtx_lock_spin(&sched_lock); 1495 /* 1496 * Force scheduling an upcall, so UTS has chance to 1497 * process the signal before thread runs again in 1498 * userland. 1499 */ 1500 if (td->td_upcall) 1501 td->td_upcall->ku_flags |= KUF_DOUPCALL; 1502 mtx_unlock_spin(&sched_lock); 1503 } else { 1504 PROC_LOCK(p); 1505 } 1506 ps = p->p_sigacts; 1507 mtx_lock(&ps->ps_mtx); 1508 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 1509 !SIGISMEMBER(td->td_sigmask, sig)) { 1510 p->p_stats->p_ru.ru_nsignals++; 1511 #ifdef KTRACE 1512 if (KTRPOINT(curthread, KTR_PSIG)) 1513 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 1514 &td->td_sigmask, code); 1515 #endif 1516 if (!(td->td_pflags & TDP_SA)) 1517 (*p->p_sysent->sv_sendsig)( 1518 ps->ps_sigact[_SIG_IDX(sig)], sig, 1519 &td->td_sigmask, code); 1520 else if (td->td_mailbox == NULL) { 1521 mtx_unlock(&ps->ps_mtx); 1522 /* UTS caused a sync signal */ 1523 p->p_code = code; /* XXX for core dump/debugger */ 1524 p->p_sig = sig; /* XXX to verify code */ 1525 sigexit(td, sig); 1526 } else { 1527 cpu_thread_siginfo(sig, code, &siginfo); 1528 mtx_unlock(&ps->ps_mtx); 1529 SIGADDSET(td->td_sigmask, sig); 1530 PROC_UNLOCK(p); 1531 error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 1532 sizeof(siginfo)); 1533 PROC_LOCK(p); 1534 /* UTS memory corrupted */ 1535 if (error) 1536 sigexit(td, SIGSEGV); 1537 mtx_lock(&ps->ps_mtx); 1538 } 1539 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1540 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1541 SIGADDSET(td->td_sigmask, sig); 1542 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1543 /* 1544 * See kern_sigaction() for origin of this code. 1545 */ 1546 SIGDELSET(ps->ps_sigcatch, sig); 1547 if (sig != SIGCONT && 1548 sigprop(sig) & SA_IGNORE) 1549 SIGADDSET(ps->ps_sigignore, sig); 1550 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1551 } 1552 mtx_unlock(&ps->ps_mtx); 1553 } else { 1554 mtx_unlock(&ps->ps_mtx); 1555 p->p_code = code; /* XXX for core dump/debugger */ 1556 p->p_sig = sig; /* XXX to verify code */ 1557 tdsignal(td, sig, SIGTARGET_TD); 1558 } 1559 PROC_UNLOCK(p); 1560 } 1561 1562 static struct thread * 1563 sigtd(struct proc *p, int sig, int prop) 1564 { 1565 struct thread *td, *signal_td; 1566 1567 PROC_LOCK_ASSERT(p, MA_OWNED); 1568 1569 /* 1570 * First find a thread in sigwait state and signal belongs to 1571 * its wait set. POSIX's arguments is that speed of delivering signal 1572 * to sigwait thread is faster than delivering signal to user stack. 1573 * If we can not find sigwait thread, then find the first thread in 1574 * the proc that doesn't have this signal masked, an exception is 1575 * if current thread is sending signal to its process, and it does not 1576 * mask the signal, it should get the signal, this is another fast 1577 * way to deliver signal. 1578 */ 1579 signal_td = NULL; 1580 mtx_lock_spin(&sched_lock); 1581 FOREACH_THREAD_IN_PROC(p, td) { 1582 if (td->td_waitset != NULL && 1583 SIGISMEMBER(*(td->td_waitset), sig)) { 1584 mtx_unlock_spin(&sched_lock); 1585 return (td); 1586 } 1587 if (!SIGISMEMBER(td->td_sigmask, sig)) { 1588 if (td == curthread) 1589 signal_td = curthread; 1590 else if (signal_td == NULL) 1591 signal_td = td; 1592 } 1593 } 1594 if (signal_td == NULL) 1595 signal_td = FIRST_THREAD_IN_PROC(p); 1596 mtx_unlock_spin(&sched_lock); 1597 return (signal_td); 1598 } 1599 1600 /* 1601 * Send the signal to the process. If the signal has an action, the action 1602 * is usually performed by the target process rather than the caller; we add 1603 * the signal to the set of pending signals for the process. 1604 * 1605 * Exceptions: 1606 * o When a stop signal is sent to a sleeping process that takes the 1607 * default action, the process is stopped without awakening it. 1608 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1609 * regardless of the signal action (eg, blocked or ignored). 1610 * 1611 * Other ignored signals are discarded immediately. 1612 * 1613 * MPSAFE 1614 */ 1615 void 1616 psignal(struct proc *p, int sig) 1617 { 1618 struct thread *td; 1619 int prop; 1620 1621 if (!_SIG_VALID(sig)) 1622 panic("psignal(): invalid signal"); 1623 1624 PROC_LOCK_ASSERT(p, MA_OWNED); 1625 /* 1626 * IEEE Std 1003.1-2001: return success when killing a zombie. 1627 */ 1628 if (p->p_state == PRS_ZOMBIE) 1629 return; 1630 prop = sigprop(sig); 1631 1632 /* 1633 * Find a thread to deliver the signal to. 1634 */ 1635 td = sigtd(p, sig, prop); 1636 1637 tdsignal(td, sig, SIGTARGET_P); 1638 } 1639 1640 /* 1641 * MPSAFE 1642 */ 1643 void 1644 tdsignal(struct thread *td, int sig, sigtarget_t target) 1645 { 1646 sigset_t saved; 1647 struct proc *p = td->td_proc; 1648 1649 if (p->p_flag & P_SA) 1650 saved = p->p_siglist; 1651 do_tdsignal(td, sig, target); 1652 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 1653 if (!SIGSETEQ(saved, p->p_siglist)) { 1654 /* pending set changed */ 1655 p->p_flag |= P_SIGEVENT; 1656 wakeup(&p->p_siglist); 1657 } 1658 } 1659 } 1660 1661 static void 1662 do_tdsignal(struct thread *td, int sig, sigtarget_t target) 1663 { 1664 struct proc *p; 1665 register sig_t action; 1666 sigset_t *siglist; 1667 struct thread *td0; 1668 register int prop; 1669 struct sigacts *ps; 1670 1671 if (!_SIG_VALID(sig)) 1672 panic("do_tdsignal(): invalid signal"); 1673 1674 p = td->td_proc; 1675 ps = p->p_sigacts; 1676 1677 PROC_LOCK_ASSERT(p, MA_OWNED); 1678 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 1679 1680 prop = sigprop(sig); 1681 1682 /* 1683 * If the signal is blocked and not destined for this thread, then 1684 * assign it to the process so that we can find it later in the first 1685 * thread that unblocks it. Otherwise, assign it to this thread now. 1686 */ 1687 if (target == SIGTARGET_TD) { 1688 siglist = &td->td_siglist; 1689 } else { 1690 if (!SIGISMEMBER(td->td_sigmask, sig)) 1691 siglist = &td->td_siglist; 1692 else if (td->td_waitset != NULL && 1693 SIGISMEMBER(*(td->td_waitset), sig)) 1694 siglist = &td->td_siglist; 1695 else 1696 siglist = &p->p_siglist; 1697 } 1698 1699 /* 1700 * If proc is traced, always give parent a chance; 1701 * if signal event is tracked by procfs, give *that* 1702 * a chance, as well. 1703 */ 1704 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1705 action = SIG_DFL; 1706 } else { 1707 /* 1708 * If the signal is being ignored, 1709 * then we forget about it immediately. 1710 * (Note: we don't set SIGCONT in ps_sigignore, 1711 * and if it is set to SIG_IGN, 1712 * action will be SIG_DFL here.) 1713 */ 1714 mtx_lock(&ps->ps_mtx); 1715 if (SIGISMEMBER(ps->ps_sigignore, sig) || 1716 (p->p_flag & P_WEXIT)) { 1717 mtx_unlock(&ps->ps_mtx); 1718 return; 1719 } 1720 if (((td->td_waitset == NULL) && 1721 SIGISMEMBER(td->td_sigmask, sig)) || 1722 ((td->td_waitset != NULL) && 1723 SIGISMEMBER(td->td_sigmask, sig) && 1724 !SIGISMEMBER(*(td->td_waitset), sig))) 1725 action = SIG_HOLD; 1726 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1727 action = SIG_CATCH; 1728 else 1729 action = SIG_DFL; 1730 mtx_unlock(&ps->ps_mtx); 1731 } 1732 1733 if (prop & SA_CONT) { 1734 SIG_STOPSIGMASK(p->p_siglist); 1735 /* 1736 * XXX Should investigate leaving STOP and CONT sigs only in 1737 * the proc's siglist. 1738 */ 1739 mtx_lock_spin(&sched_lock); 1740 FOREACH_THREAD_IN_PROC(p, td0) 1741 SIG_STOPSIGMASK(td0->td_siglist); 1742 mtx_unlock_spin(&sched_lock); 1743 } 1744 1745 if (prop & SA_STOP) { 1746 /* 1747 * If sending a tty stop signal to a member of an orphaned 1748 * process group, discard the signal here if the action 1749 * is default; don't stop the process below if sleeping, 1750 * and don't clear any pending SIGCONT. 1751 */ 1752 if ((prop & SA_TTYSTOP) && 1753 (p->p_pgrp->pg_jobc == 0) && 1754 (action == SIG_DFL)) 1755 return; 1756 SIG_CONTSIGMASK(p->p_siglist); 1757 mtx_lock_spin(&sched_lock); 1758 FOREACH_THREAD_IN_PROC(p, td0) 1759 SIG_CONTSIGMASK(td0->td_siglist); 1760 mtx_unlock_spin(&sched_lock); 1761 p->p_flag &= ~P_CONTINUED; 1762 } 1763 1764 SIGADDSET(*siglist, sig); 1765 signotify(td); /* uses schedlock */ 1766 if (siglist == &td->td_siglist && (td->td_waitset != NULL) && 1767 action != SIG_HOLD) { 1768 td->td_waitset = NULL; 1769 } 1770 1771 /* 1772 * Defer further processing for signals which are held, 1773 * except that stopped processes must be continued by SIGCONT. 1774 */ 1775 if (action == SIG_HOLD && 1776 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 1777 return; 1778 /* 1779 * SIGKILL: Remove procfs STOPEVENTs. 1780 */ 1781 if (sig == SIGKILL) { 1782 /* from procfs_ioctl.c: PIOCBIC */ 1783 p->p_stops = 0; 1784 /* from procfs_ioctl.c: PIOCCONT */ 1785 p->p_step = 0; 1786 wakeup(&p->p_step); 1787 } 1788 /* 1789 * Some signals have a process-wide effect and a per-thread 1790 * component. Most processing occurs when the process next 1791 * tries to cross the user boundary, however there are some 1792 * times when processing needs to be done immediatly, such as 1793 * waking up threads so that they can cross the user boundary. 1794 * We try do the per-process part here. 1795 */ 1796 if (P_SHOULDSTOP(p)) { 1797 /* 1798 * The process is in stopped mode. All the threads should be 1799 * either winding down or already on the suspended queue. 1800 */ 1801 if (p->p_flag & P_TRACED) { 1802 /* 1803 * The traced process is already stopped, 1804 * so no further action is necessary. 1805 * No signal can restart us. 1806 */ 1807 goto out; 1808 } 1809 1810 if (sig == SIGKILL) { 1811 /* 1812 * SIGKILL sets process running. 1813 * It will die elsewhere. 1814 * All threads must be restarted. 1815 */ 1816 p->p_flag &= ~P_STOPPED_SIG; 1817 goto runfast; 1818 } 1819 1820 if (prop & SA_CONT) { 1821 /* 1822 * If SIGCONT is default (or ignored), we continue the 1823 * process but don't leave the signal in siglist as 1824 * it has no further action. If SIGCONT is held, we 1825 * continue the process and leave the signal in 1826 * siglist. If the process catches SIGCONT, let it 1827 * handle the signal itself. If it isn't waiting on 1828 * an event, it goes back to run state. 1829 * Otherwise, process goes back to sleep state. 1830 */ 1831 p->p_flag &= ~P_STOPPED_SIG; 1832 p->p_flag |= P_CONTINUED; 1833 if (action == SIG_DFL) { 1834 SIGDELSET(*siglist, sig); 1835 } else if (action == SIG_CATCH) { 1836 /* 1837 * The process wants to catch it so it needs 1838 * to run at least one thread, but which one? 1839 * It would seem that the answer would be to 1840 * run an upcall in the next KSE to run, and 1841 * deliver the signal that way. In a NON KSE 1842 * process, we need to make sure that the 1843 * single thread is runnable asap. 1844 * XXXKSE for now however, make them all run. 1845 */ 1846 goto runfast; 1847 } 1848 /* 1849 * The signal is not ignored or caught. 1850 */ 1851 mtx_lock_spin(&sched_lock); 1852 thread_unsuspend(p); 1853 mtx_unlock_spin(&sched_lock); 1854 goto out; 1855 } 1856 1857 if (prop & SA_STOP) { 1858 /* 1859 * Already stopped, don't need to stop again 1860 * (If we did the shell could get confused). 1861 * Just make sure the signal STOP bit set. 1862 */ 1863 p->p_flag |= P_STOPPED_SIG; 1864 SIGDELSET(*siglist, sig); 1865 goto out; 1866 } 1867 1868 /* 1869 * All other kinds of signals: 1870 * If a thread is sleeping interruptibly, simulate a 1871 * wakeup so that when it is continued it will be made 1872 * runnable and can look at the signal. However, don't make 1873 * the PROCESS runnable, leave it stopped. 1874 * It may run a bit until it hits a thread_suspend_check(). 1875 */ 1876 mtx_lock_spin(&sched_lock); 1877 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 1878 sleepq_abort(td); 1879 mtx_unlock_spin(&sched_lock); 1880 goto out; 1881 /* 1882 * Mutexes are short lived. Threads waiting on them will 1883 * hit thread_suspend_check() soon. 1884 */ 1885 } else if (p->p_state == PRS_NORMAL) { 1886 if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 1887 !(prop & SA_STOP)) { 1888 mtx_lock_spin(&sched_lock); 1889 tdsigwakeup(td, sig, action); 1890 mtx_unlock_spin(&sched_lock); 1891 goto out; 1892 } 1893 if (prop & SA_STOP) { 1894 if (p->p_flag & P_PPWAIT) 1895 goto out; 1896 p->p_flag |= P_STOPPED_SIG; 1897 p->p_xstat = sig; 1898 p->p_xthread = td; 1899 mtx_lock_spin(&sched_lock); 1900 FOREACH_THREAD_IN_PROC(p, td0) { 1901 if (TD_IS_SLEEPING(td0) && 1902 (td0->td_flags & TDF_SINTR) && 1903 !TD_IS_SUSPENDED(td0)) { 1904 thread_suspend_one(td0); 1905 } else if (td != td0) { 1906 td0->td_flags |= TDF_ASTPENDING; 1907 } 1908 } 1909 thread_stopped(p); 1910 if (p->p_numthreads == p->p_suspcount) { 1911 SIGDELSET(p->p_siglist, p->p_xstat); 1912 FOREACH_THREAD_IN_PROC(p, td0) 1913 SIGDELSET(td0->td_siglist, p->p_xstat); 1914 } 1915 mtx_unlock_spin(&sched_lock); 1916 goto out; 1917 } 1918 else 1919 goto runfast; 1920 /* NOTREACHED */ 1921 } else { 1922 /* Not in "NORMAL" state. discard the signal. */ 1923 SIGDELSET(*siglist, sig); 1924 goto out; 1925 } 1926 1927 /* 1928 * The process is not stopped so we need to apply the signal to all the 1929 * running threads. 1930 */ 1931 1932 runfast: 1933 mtx_lock_spin(&sched_lock); 1934 tdsigwakeup(td, sig, action); 1935 thread_unsuspend(p); 1936 mtx_unlock_spin(&sched_lock); 1937 out: 1938 /* If we jump here, sched_lock should not be owned. */ 1939 mtx_assert(&sched_lock, MA_NOTOWNED); 1940 } 1941 1942 /* 1943 * The force of a signal has been directed against a single 1944 * thread. We need to see what we can do about knocking it 1945 * out of any sleep it may be in etc. 1946 */ 1947 static void 1948 tdsigwakeup(struct thread *td, int sig, sig_t action) 1949 { 1950 struct proc *p = td->td_proc; 1951 register int prop; 1952 1953 PROC_LOCK_ASSERT(p, MA_OWNED); 1954 mtx_assert(&sched_lock, MA_OWNED); 1955 prop = sigprop(sig); 1956 1957 /* 1958 * Bring the priority of a thread up if we want it to get 1959 * killed in this lifetime. 1960 */ 1961 if (action == SIG_DFL && (prop & SA_KILL)) { 1962 if (p->p_nice > 0) 1963 sched_nice(td->td_proc, 0); 1964 if (td->td_priority > PUSER) 1965 sched_prio(td, PUSER); 1966 } 1967 1968 if (TD_ON_SLEEPQ(td)) { 1969 /* 1970 * If thread is sleeping uninterruptibly 1971 * we can't interrupt the sleep... the signal will 1972 * be noticed when the process returns through 1973 * trap() or syscall(). 1974 */ 1975 if ((td->td_flags & TDF_SINTR) == 0) 1976 return; 1977 /* 1978 * Process is sleeping and traced. Make it runnable 1979 * so it can discover the signal in issignal() and stop 1980 * for its parent. 1981 */ 1982 if (p->p_flag & P_TRACED) { 1983 p->p_flag &= ~P_STOPPED_TRACE; 1984 } else { 1985 /* 1986 * If SIGCONT is default (or ignored) and process is 1987 * asleep, we are finished; the process should not 1988 * be awakened. 1989 */ 1990 if ((prop & SA_CONT) && action == SIG_DFL) { 1991 SIGDELSET(p->p_siglist, sig); 1992 /* 1993 * It may be on either list in this state. 1994 * Remove from both for now. 1995 */ 1996 SIGDELSET(td->td_siglist, sig); 1997 return; 1998 } 1999 2000 /* 2001 * Give low priority threads a better chance to run. 2002 */ 2003 if (td->td_priority > PUSER) 2004 sched_prio(td, PUSER); 2005 } 2006 sleepq_abort(td); 2007 } else { 2008 /* 2009 * Other states do nothing with the signal immediately, 2010 * other than kicking ourselves if we are running. 2011 * It will either never be noticed, or noticed very soon. 2012 */ 2013 #ifdef SMP 2014 if (TD_IS_RUNNING(td) && td != curthread) 2015 forward_signal(td); 2016 #endif 2017 } 2018 } 2019 2020 int 2021 ptracestop(struct thread *td, int sig) 2022 { 2023 struct proc *p = td->td_proc; 2024 struct thread *td0; 2025 2026 PROC_LOCK_ASSERT(p, MA_OWNED); 2027 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2028 &p->p_mtx.mtx_object, "Stopping for traced signal"); 2029 2030 mtx_lock_spin(&sched_lock); 2031 td->td_flags |= TDF_XSIG; 2032 mtx_unlock_spin(&sched_lock); 2033 td->td_xsig = sig; 2034 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2035 if (p->p_flag & P_SINGLE_EXIT) { 2036 mtx_lock_spin(&sched_lock); 2037 td->td_flags &= ~TDF_XSIG; 2038 mtx_unlock_spin(&sched_lock); 2039 return (sig); 2040 } 2041 /* 2042 * Just make wait() to work, the last stopped thread 2043 * will win. 2044 */ 2045 p->p_xstat = sig; 2046 p->p_xthread = td; 2047 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2048 mtx_lock_spin(&sched_lock); 2049 FOREACH_THREAD_IN_PROC(p, td0) { 2050 if (TD_IS_SLEEPING(td0) && 2051 (td0->td_flags & TDF_SINTR) && 2052 !TD_IS_SUSPENDED(td0)) { 2053 thread_suspend_one(td0); 2054 } else if (td != td0) { 2055 td0->td_flags |= TDF_ASTPENDING; 2056 } 2057 } 2058 stopme: 2059 thread_stopped(p); 2060 thread_suspend_one(td); 2061 PROC_UNLOCK(p); 2062 DROP_GIANT(); 2063 mi_switch(SW_VOL, NULL); 2064 mtx_unlock_spin(&sched_lock); 2065 PICKUP_GIANT(); 2066 PROC_LOCK(p); 2067 if (!(p->p_flag & P_TRACED)) 2068 break; 2069 if (td->td_flags & TDF_DBSUSPEND) { 2070 if (p->p_flag & P_SINGLE_EXIT) 2071 break; 2072 mtx_lock_spin(&sched_lock); 2073 goto stopme; 2074 } 2075 } 2076 return (td->td_xsig); 2077 } 2078 2079 /* 2080 * If the current process has received a signal (should be caught or cause 2081 * termination, should interrupt current syscall), return the signal number. 2082 * Stop signals with default action are processed immediately, then cleared; 2083 * they aren't returned. This is checked after each entry to the system for 2084 * a syscall or trap (though this can usually be done without calling issignal 2085 * by checking the pending signal masks in cursig.) The normal call 2086 * sequence is 2087 * 2088 * while (sig = cursig(curthread)) 2089 * postsig(sig); 2090 */ 2091 static int 2092 issignal(td) 2093 struct thread *td; 2094 { 2095 struct proc *p; 2096 struct sigacts *ps; 2097 sigset_t sigpending; 2098 int sig, prop, newsig; 2099 struct thread *td0; 2100 2101 p = td->td_proc; 2102 ps = p->p_sigacts; 2103 mtx_assert(&ps->ps_mtx, MA_OWNED); 2104 PROC_LOCK_ASSERT(p, MA_OWNED); 2105 for (;;) { 2106 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2107 2108 sigpending = td->td_siglist; 2109 SIGSETNAND(sigpending, td->td_sigmask); 2110 2111 if (p->p_flag & P_PPWAIT) 2112 SIG_STOPSIGMASK(sigpending); 2113 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2114 return (0); 2115 sig = sig_ffs(&sigpending); 2116 2117 if (p->p_stops & S_SIG) { 2118 mtx_unlock(&ps->ps_mtx); 2119 stopevent(p, S_SIG, sig); 2120 mtx_lock(&ps->ps_mtx); 2121 } 2122 2123 /* 2124 * We should see pending but ignored signals 2125 * only if P_TRACED was on when they were posted. 2126 */ 2127 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2128 SIGDELSET(td->td_siglist, sig); 2129 if (td->td_pflags & TDP_SA) 2130 SIGADDSET(td->td_sigmask, sig); 2131 continue; 2132 } 2133 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2134 /* 2135 * If traced, always stop. 2136 */ 2137 mtx_unlock(&ps->ps_mtx); 2138 newsig = ptracestop(td, sig); 2139 mtx_lock(&ps->ps_mtx); 2140 2141 /* 2142 * If parent wants us to take the signal, 2143 * then it will leave it in p->p_xstat; 2144 * otherwise we just look for signals again. 2145 */ 2146 SIGDELSET(td->td_siglist, sig); /* clear old signal */ 2147 if (td->td_pflags & TDP_SA) 2148 SIGADDSET(td->td_sigmask, sig); 2149 if (newsig == 0) 2150 continue; 2151 sig = newsig; 2152 /* 2153 * If the traced bit got turned off, go back up 2154 * to the top to rescan signals. This ensures 2155 * that p_sig* and p_sigact are consistent. 2156 */ 2157 if ((p->p_flag & P_TRACED) == 0) 2158 continue; 2159 2160 /* 2161 * Put the new signal into td_siglist. If the 2162 * signal is being masked, look for other signals. 2163 */ 2164 SIGADDSET(td->td_siglist, sig); 2165 if (td->td_pflags & TDP_SA) 2166 SIGDELSET(td->td_sigmask, sig); 2167 if (SIGISMEMBER(td->td_sigmask, sig)) 2168 continue; 2169 signotify(td); 2170 } 2171 2172 prop = sigprop(sig); 2173 2174 /* 2175 * Decide whether the signal should be returned. 2176 * Return the signal's number, or fall through 2177 * to clear it from the pending mask. 2178 */ 2179 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2180 2181 case (intptr_t)SIG_DFL: 2182 /* 2183 * Don't take default actions on system processes. 2184 */ 2185 if (p->p_pid <= 1) { 2186 #ifdef DIAGNOSTIC 2187 /* 2188 * Are you sure you want to ignore SIGSEGV 2189 * in init? XXX 2190 */ 2191 printf("Process (pid %lu) got signal %d\n", 2192 (u_long)p->p_pid, sig); 2193 #endif 2194 break; /* == ignore */ 2195 } 2196 /* 2197 * If there is a pending stop signal to process 2198 * with default action, stop here, 2199 * then clear the signal. However, 2200 * if process is member of an orphaned 2201 * process group, ignore tty stop signals. 2202 */ 2203 if (prop & SA_STOP) { 2204 if (p->p_flag & P_TRACED || 2205 (p->p_pgrp->pg_jobc == 0 && 2206 prop & SA_TTYSTOP)) 2207 break; /* == ignore */ 2208 mtx_unlock(&ps->ps_mtx); 2209 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2210 &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2211 p->p_flag |= P_STOPPED_SIG; 2212 p->p_xstat = sig; 2213 p->p_xthread = td; 2214 mtx_lock_spin(&sched_lock); 2215 FOREACH_THREAD_IN_PROC(p, td0) { 2216 if (TD_IS_SLEEPING(td0) && 2217 (td0->td_flags & TDF_SINTR) && 2218 !TD_IS_SUSPENDED(td0)) { 2219 thread_suspend_one(td0); 2220 } else if (td != td0) { 2221 td0->td_flags |= TDF_ASTPENDING; 2222 } 2223 } 2224 thread_stopped(p); 2225 thread_suspend_one(td); 2226 PROC_UNLOCK(p); 2227 DROP_GIANT(); 2228 mi_switch(SW_INVOL, NULL); 2229 mtx_unlock_spin(&sched_lock); 2230 PICKUP_GIANT(); 2231 PROC_LOCK(p); 2232 mtx_lock(&ps->ps_mtx); 2233 break; 2234 } else if (prop & SA_IGNORE) { 2235 /* 2236 * Except for SIGCONT, shouldn't get here. 2237 * Default action is to ignore; drop it. 2238 */ 2239 break; /* == ignore */ 2240 } else 2241 return (sig); 2242 /*NOTREACHED*/ 2243 2244 case (intptr_t)SIG_IGN: 2245 /* 2246 * Masking above should prevent us ever trying 2247 * to take action on an ignored signal other 2248 * than SIGCONT, unless process is traced. 2249 */ 2250 if ((prop & SA_CONT) == 0 && 2251 (p->p_flag & P_TRACED) == 0) 2252 printf("issignal\n"); 2253 break; /* == ignore */ 2254 2255 default: 2256 /* 2257 * This signal has an action, let 2258 * postsig() process it. 2259 */ 2260 return (sig); 2261 } 2262 SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2263 } 2264 /* NOTREACHED */ 2265 } 2266 2267 /* 2268 * Put the argument process into the stopped state and notify the parent 2269 * via wakeup. Signals are handled elsewhere. The process must not be 2270 * on the run queue. Must be called with the proc p locked. 2271 */ 2272 static void 2273 stop(struct proc *p) 2274 { 2275 2276 PROC_LOCK_ASSERT(p, MA_OWNED); 2277 p->p_flag |= P_STOPPED_SIG; 2278 p->p_flag &= ~P_WAITED; 2279 wakeup(p->p_pptr); 2280 } 2281 2282 /* 2283 * MPSAFE 2284 */ 2285 void 2286 thread_stopped(struct proc *p) 2287 { 2288 struct proc *p1 = curthread->td_proc; 2289 struct sigacts *ps; 2290 int n; 2291 2292 PROC_LOCK_ASSERT(p, MA_OWNED); 2293 mtx_assert(&sched_lock, MA_OWNED); 2294 n = p->p_suspcount; 2295 if (p == p1) 2296 n++; 2297 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2298 mtx_unlock_spin(&sched_lock); 2299 stop(p); 2300 PROC_LOCK(p->p_pptr); 2301 ps = p->p_pptr->p_sigacts; 2302 mtx_lock(&ps->ps_mtx); 2303 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 2304 mtx_unlock(&ps->ps_mtx); 2305 psignal(p->p_pptr, SIGCHLD); 2306 } else 2307 mtx_unlock(&ps->ps_mtx); 2308 PROC_UNLOCK(p->p_pptr); 2309 mtx_lock_spin(&sched_lock); 2310 } 2311 } 2312 2313 /* 2314 * Take the action for the specified signal 2315 * from the current set of pending signals. 2316 */ 2317 void 2318 postsig(sig) 2319 register int sig; 2320 { 2321 struct thread *td = curthread; 2322 register struct proc *p = td->td_proc; 2323 struct sigacts *ps; 2324 sig_t action; 2325 sigset_t returnmask; 2326 int code; 2327 2328 KASSERT(sig != 0, ("postsig")); 2329 2330 PROC_LOCK_ASSERT(p, MA_OWNED); 2331 ps = p->p_sigacts; 2332 mtx_assert(&ps->ps_mtx, MA_OWNED); 2333 SIGDELSET(td->td_siglist, sig); 2334 action = ps->ps_sigact[_SIG_IDX(sig)]; 2335 #ifdef KTRACE 2336 if (KTRPOINT(td, KTR_PSIG)) 2337 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2338 &td->td_oldsigmask : &td->td_sigmask, 0); 2339 #endif 2340 if (p->p_stops & S_SIG) { 2341 mtx_unlock(&ps->ps_mtx); 2342 stopevent(p, S_SIG, sig); 2343 mtx_lock(&ps->ps_mtx); 2344 } 2345 2346 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2347 /* 2348 * Default action, where the default is to kill 2349 * the process. (Other cases were ignored above.) 2350 */ 2351 mtx_unlock(&ps->ps_mtx); 2352 sigexit(td, sig); 2353 /* NOTREACHED */ 2354 } else { 2355 if (td->td_pflags & TDP_SA) { 2356 if (sig == SIGKILL) { 2357 mtx_unlock(&ps->ps_mtx); 2358 sigexit(td, sig); 2359 } 2360 } 2361 2362 /* 2363 * If we get here, the signal must be caught. 2364 */ 2365 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2366 ("postsig action")); 2367 /* 2368 * Set the new mask value and also defer further 2369 * occurrences of this signal. 2370 * 2371 * Special case: user has done a sigsuspend. Here the 2372 * current mask is not of interest, but rather the 2373 * mask from before the sigsuspend is what we want 2374 * restored after the signal processing is completed. 2375 */ 2376 if (td->td_pflags & TDP_OLDMASK) { 2377 returnmask = td->td_oldsigmask; 2378 td->td_pflags &= ~TDP_OLDMASK; 2379 } else 2380 returnmask = td->td_sigmask; 2381 2382 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 2383 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2384 SIGADDSET(td->td_sigmask, sig); 2385 2386 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2387 /* 2388 * See kern_sigaction() for origin of this code. 2389 */ 2390 SIGDELSET(ps->ps_sigcatch, sig); 2391 if (sig != SIGCONT && 2392 sigprop(sig) & SA_IGNORE) 2393 SIGADDSET(ps->ps_sigignore, sig); 2394 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2395 } 2396 p->p_stats->p_ru.ru_nsignals++; 2397 if (p->p_sig != sig) { 2398 code = 0; 2399 } else { 2400 code = p->p_code; 2401 p->p_code = 0; 2402 p->p_sig = 0; 2403 } 2404 if (td->td_pflags & TDP_SA) 2405 thread_signal_add(curthread, sig); 2406 else 2407 (*p->p_sysent->sv_sendsig)(action, sig, 2408 &returnmask, code); 2409 } 2410 } 2411 2412 /* 2413 * Kill the current process for stated reason. 2414 */ 2415 void 2416 killproc(p, why) 2417 struct proc *p; 2418 char *why; 2419 { 2420 2421 PROC_LOCK_ASSERT(p, MA_OWNED); 2422 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 2423 p, p->p_pid, p->p_comm); 2424 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2425 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2426 psignal(p, SIGKILL); 2427 } 2428 2429 /* 2430 * Force the current process to exit with the specified signal, dumping core 2431 * if appropriate. We bypass the normal tests for masked and caught signals, 2432 * allowing unrecoverable failures to terminate the process without changing 2433 * signal state. Mark the accounting record with the signal termination. 2434 * If dumping core, save the signal number for the debugger. Calls exit and 2435 * does not return. 2436 * 2437 * MPSAFE 2438 */ 2439 void 2440 sigexit(td, sig) 2441 struct thread *td; 2442 int sig; 2443 { 2444 struct proc *p = td->td_proc; 2445 2446 PROC_LOCK_ASSERT(p, MA_OWNED); 2447 p->p_acflag |= AXSIG; 2448 if (sigprop(sig) & SA_CORE) { 2449 p->p_sig = sig; 2450 /* 2451 * Log signals which would cause core dumps 2452 * (Log as LOG_INFO to appease those who don't want 2453 * these messages.) 2454 * XXX : Todo, as well as euid, write out ruid too 2455 * Note that coredump() drops proc lock. 2456 */ 2457 if (coredump(td) == 0) 2458 sig |= WCOREFLAG; 2459 if (kern_logsigexit) 2460 log(LOG_INFO, 2461 "pid %d (%s), uid %d: exited on signal %d%s\n", 2462 p->p_pid, p->p_comm, 2463 td->td_ucred ? td->td_ucred->cr_uid : -1, 2464 sig &~ WCOREFLAG, 2465 sig & WCOREFLAG ? " (core dumped)" : ""); 2466 } else 2467 PROC_UNLOCK(p); 2468 exit1(td, W_EXITCODE(0, sig)); 2469 /* NOTREACHED */ 2470 } 2471 2472 static char corefilename[MAXPATHLEN] = {"%N.core"}; 2473 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2474 sizeof(corefilename), "process corefile name format string"); 2475 2476 /* 2477 * expand_name(name, uid, pid) 2478 * Expand the name described in corefilename, using name, uid, and pid. 2479 * corefilename is a printf-like string, with three format specifiers: 2480 * %N name of process ("name") 2481 * %P process id (pid) 2482 * %U user id (uid) 2483 * For example, "%N.core" is the default; they can be disabled completely 2484 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2485 * This is controlled by the sysctl variable kern.corefile (see above). 2486 */ 2487 2488 static char * 2489 expand_name(name, uid, pid) 2490 const char *name; 2491 uid_t uid; 2492 pid_t pid; 2493 { 2494 const char *format, *appendstr; 2495 char *temp; 2496 char buf[11]; /* Buffer for pid/uid -- max 4B */ 2497 size_t i, l, n; 2498 2499 format = corefilename; 2500 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 2501 if (temp == NULL) 2502 return (NULL); 2503 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2504 switch (format[i]) { 2505 case '%': /* Format character */ 2506 i++; 2507 switch (format[i]) { 2508 case '%': 2509 appendstr = "%"; 2510 break; 2511 case 'N': /* process name */ 2512 appendstr = name; 2513 break; 2514 case 'P': /* process id */ 2515 sprintf(buf, "%u", pid); 2516 appendstr = buf; 2517 break; 2518 case 'U': /* user id */ 2519 sprintf(buf, "%u", uid); 2520 appendstr = buf; 2521 break; 2522 default: 2523 appendstr = ""; 2524 log(LOG_ERR, 2525 "Unknown format character %c in `%s'\n", 2526 format[i], format); 2527 } 2528 l = strlen(appendstr); 2529 if ((n + l) >= MAXPATHLEN) 2530 goto toolong; 2531 memcpy(temp + n, appendstr, l); 2532 n += l; 2533 break; 2534 default: 2535 temp[n++] = format[i]; 2536 } 2537 } 2538 if (format[i] != '\0') 2539 goto toolong; 2540 return (temp); 2541 toolong: 2542 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 2543 (long)pid, name, (u_long)uid); 2544 free(temp, M_TEMP); 2545 return (NULL); 2546 } 2547 2548 /* 2549 * Dump a process' core. The main routine does some 2550 * policy checking, and creates the name of the coredump; 2551 * then it passes on a vnode and a size limit to the process-specific 2552 * coredump routine if there is one; if there _is not_ one, it returns 2553 * ENOSYS; otherwise it returns the error from the process-specific routine. 2554 */ 2555 2556 static int 2557 coredump(struct thread *td) 2558 { 2559 struct proc *p = td->td_proc; 2560 register struct vnode *vp; 2561 register struct ucred *cred = td->td_ucred; 2562 struct flock lf; 2563 struct nameidata nd; 2564 struct vattr vattr; 2565 int error, error1, flags, locked; 2566 struct mount *mp; 2567 char *name; /* name of corefile */ 2568 off_t limit; 2569 2570 PROC_LOCK_ASSERT(p, MA_OWNED); 2571 _STOPEVENT(p, S_CORE, 0); 2572 2573 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2574 PROC_UNLOCK(p); 2575 return (EFAULT); 2576 } 2577 2578 /* 2579 * Note that the bulk of limit checking is done after 2580 * the corefile is created. The exception is if the limit 2581 * for corefiles is 0, in which case we don't bother 2582 * creating the corefile at all. This layout means that 2583 * a corefile is truncated instead of not being created, 2584 * if it is larger than the limit. 2585 */ 2586 limit = (off_t)lim_cur(p, RLIMIT_CORE); 2587 PROC_UNLOCK(p); 2588 if (limit == 0) 2589 return (EFBIG); 2590 2591 mtx_lock(&Giant); 2592 restart: 2593 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2594 if (name == NULL) { 2595 mtx_unlock(&Giant); 2596 return (EINVAL); 2597 } 2598 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2599 flags = O_CREAT | FWRITE | O_NOFOLLOW; 2600 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 2601 free(name, M_TEMP); 2602 if (error) { 2603 mtx_unlock(&Giant); 2604 return (error); 2605 } 2606 NDFREE(&nd, NDF_ONLY_PNBUF); 2607 vp = nd.ni_vp; 2608 2609 /* Don't dump to non-regular files or files with links. */ 2610 if (vp->v_type != VREG || 2611 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2612 VOP_UNLOCK(vp, 0, td); 2613 error = EFAULT; 2614 goto out; 2615 } 2616 2617 VOP_UNLOCK(vp, 0, td); 2618 lf.l_whence = SEEK_SET; 2619 lf.l_start = 0; 2620 lf.l_len = 0; 2621 lf.l_type = F_WRLCK; 2622 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 2623 2624 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2625 lf.l_type = F_UNLCK; 2626 if (locked) 2627 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2628 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2629 return (error); 2630 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2631 return (error); 2632 goto restart; 2633 } 2634 2635 VATTR_NULL(&vattr); 2636 vattr.va_size = 0; 2637 if (set_core_nodump_flag) 2638 vattr.va_flags = UF_NODUMP; 2639 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2640 VOP_LEASE(vp, td, cred, LEASE_WRITE); 2641 VOP_SETATTR(vp, &vattr, cred, td); 2642 VOP_UNLOCK(vp, 0, td); 2643 PROC_LOCK(p); 2644 p->p_acflag |= ACORE; 2645 PROC_UNLOCK(p); 2646 2647 error = p->p_sysent->sv_coredump ? 2648 p->p_sysent->sv_coredump(td, vp, limit) : 2649 ENOSYS; 2650 2651 if (locked) { 2652 lf.l_type = F_UNLCK; 2653 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2654 } 2655 vn_finished_write(mp); 2656 out: 2657 error1 = vn_close(vp, FWRITE, cred, td); 2658 mtx_unlock(&Giant); 2659 if (error == 0) 2660 error = error1; 2661 return (error); 2662 } 2663 2664 /* 2665 * Nonexistent system call-- signal process (may want to handle it). 2666 * Flag error in case process won't see signal immediately (blocked or ignored). 2667 */ 2668 #ifndef _SYS_SYSPROTO_H_ 2669 struct nosys_args { 2670 int dummy; 2671 }; 2672 #endif 2673 /* 2674 * MPSAFE 2675 */ 2676 /* ARGSUSED */ 2677 int 2678 nosys(td, args) 2679 struct thread *td; 2680 struct nosys_args *args; 2681 { 2682 struct proc *p = td->td_proc; 2683 2684 PROC_LOCK(p); 2685 psignal(p, SIGSYS); 2686 PROC_UNLOCK(p); 2687 return (ENOSYS); 2688 } 2689 2690 /* 2691 * Send a SIGIO or SIGURG signal to a process or process group using 2692 * stored credentials rather than those of the current process. 2693 */ 2694 void 2695 pgsigio(sigiop, sig, checkctty) 2696 struct sigio **sigiop; 2697 int sig, checkctty; 2698 { 2699 struct sigio *sigio; 2700 2701 SIGIO_LOCK(); 2702 sigio = *sigiop; 2703 if (sigio == NULL) { 2704 SIGIO_UNLOCK(); 2705 return; 2706 } 2707 if (sigio->sio_pgid > 0) { 2708 PROC_LOCK(sigio->sio_proc); 2709 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 2710 psignal(sigio->sio_proc, sig); 2711 PROC_UNLOCK(sigio->sio_proc); 2712 } else if (sigio->sio_pgid < 0) { 2713 struct proc *p; 2714 2715 PGRP_LOCK(sigio->sio_pgrp); 2716 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2717 PROC_LOCK(p); 2718 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2719 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 2720 psignal(p, sig); 2721 PROC_UNLOCK(p); 2722 } 2723 PGRP_UNLOCK(sigio->sio_pgrp); 2724 } 2725 SIGIO_UNLOCK(); 2726 } 2727 2728 static int 2729 filt_sigattach(struct knote *kn) 2730 { 2731 struct proc *p = curproc; 2732 2733 kn->kn_ptr.p_proc = p; 2734 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2735 2736 knlist_add(&p->p_klist, kn, 0); 2737 2738 return (0); 2739 } 2740 2741 static void 2742 filt_sigdetach(struct knote *kn) 2743 { 2744 struct proc *p = kn->kn_ptr.p_proc; 2745 2746 knlist_remove(&p->p_klist, kn, 0); 2747 } 2748 2749 /* 2750 * signal knotes are shared with proc knotes, so we apply a mask to 2751 * the hint in order to differentiate them from process hints. This 2752 * could be avoided by using a signal-specific knote list, but probably 2753 * isn't worth the trouble. 2754 */ 2755 static int 2756 filt_signal(struct knote *kn, long hint) 2757 { 2758 2759 if (hint & NOTE_SIGNAL) { 2760 hint &= ~NOTE_SIGNAL; 2761 2762 if (kn->kn_id == hint) 2763 kn->kn_data++; 2764 } 2765 return (kn->kn_data != 0); 2766 } 2767 2768 struct sigacts * 2769 sigacts_alloc(void) 2770 { 2771 struct sigacts *ps; 2772 2773 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 2774 ps->ps_refcnt = 1; 2775 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 2776 return (ps); 2777 } 2778 2779 void 2780 sigacts_free(struct sigacts *ps) 2781 { 2782 2783 mtx_lock(&ps->ps_mtx); 2784 ps->ps_refcnt--; 2785 if (ps->ps_refcnt == 0) { 2786 mtx_destroy(&ps->ps_mtx); 2787 free(ps, M_SUBPROC); 2788 } else 2789 mtx_unlock(&ps->ps_mtx); 2790 } 2791 2792 struct sigacts * 2793 sigacts_hold(struct sigacts *ps) 2794 { 2795 mtx_lock(&ps->ps_mtx); 2796 ps->ps_refcnt++; 2797 mtx_unlock(&ps->ps_mtx); 2798 return (ps); 2799 } 2800 2801 void 2802 sigacts_copy(struct sigacts *dest, struct sigacts *src) 2803 { 2804 2805 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 2806 mtx_lock(&src->ps_mtx); 2807 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 2808 mtx_unlock(&src->ps_mtx); 2809 } 2810 2811 int 2812 sigacts_shared(struct sigacts *ps) 2813 { 2814 int shared; 2815 2816 mtx_lock(&ps->ps_mtx); 2817 shared = ps->ps_refcnt > 1; 2818 mtx_unlock(&ps->ps_mtx); 2819 return (shared); 2820 } 2821