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