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