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