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