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