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