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 p->p_code = code; /* XXX for core dump/debugger */ 1521 p->p_sig = sig; /* XXX to verify code */ 1522 sigexit(td, sig); 1523 } 1524 } else { 1525 PROC_LOCK(p); 1526 } 1527 ps = p->p_sigacts; 1528 mtx_lock(&ps->ps_mtx); 1529 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 1530 !SIGISMEMBER(td->td_sigmask, sig)) { 1531 p->p_stats->p_ru.ru_nsignals++; 1532 #ifdef KTRACE 1533 if (KTRPOINT(curthread, KTR_PSIG)) 1534 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 1535 &td->td_sigmask, code); 1536 #endif 1537 if (!(td->td_flags & TDF_SA)) 1538 (*p->p_sysent->sv_sendsig)( 1539 ps->ps_sigact[_SIG_IDX(sig)], sig, 1540 &td->td_sigmask, code); 1541 else { 1542 cpu_thread_siginfo(sig, code, &siginfo); 1543 mtx_unlock(&ps->ps_mtx); 1544 PROC_UNLOCK(p); 1545 error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, 1546 sizeof(siginfo)); 1547 PROC_LOCK(p); 1548 /* UTS memory corrupted */ 1549 if (error) 1550 sigexit(td, SIGILL); 1551 SIGADDSET(td->td_sigmask, sig); 1552 mtx_lock(&ps->ps_mtx); 1553 } 1554 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1555 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1556 SIGADDSET(td->td_sigmask, sig); 1557 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1558 /* 1559 * See kern_sigaction() for origin of this code. 1560 */ 1561 SIGDELSET(ps->ps_sigcatch, sig); 1562 if (sig != SIGCONT && 1563 sigprop(sig) & SA_IGNORE) 1564 SIGADDSET(ps->ps_sigignore, sig); 1565 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1566 } 1567 mtx_unlock(&ps->ps_mtx); 1568 } else { 1569 mtx_unlock(&ps->ps_mtx); 1570 p->p_code = code; /* XXX for core dump/debugger */ 1571 p->p_sig = sig; /* XXX to verify code */ 1572 tdsignal(td, sig, SIGTARGET_TD); 1573 } 1574 PROC_UNLOCK(p); 1575 } 1576 1577 static struct thread * 1578 sigtd(struct proc *p, int sig, int prop) 1579 { 1580 struct thread *td, *signal_td; 1581 1582 PROC_LOCK_ASSERT(p, MA_OWNED); 1583 1584 /* 1585 * First find a thread in sigwait state and signal belongs to 1586 * its wait set. POSIX's arguments is that speed of delivering signal 1587 * to sigwait thread is faster than delivering signal to user stack. 1588 * If we can not find sigwait thread, then find the first thread in 1589 * the proc that doesn't have this signal masked, an exception is 1590 * if current thread is sending signal to its process, and it does not 1591 * mask the signal, it should get the signal, this is another fast 1592 * way to deliver signal. 1593 */ 1594 signal_td = NULL; 1595 FOREACH_THREAD_IN_PROC(p, td) { 1596 if (td->td_waitset != NULL && 1597 SIGISMEMBER(*(td->td_waitset), sig)) 1598 return (td); 1599 if (!SIGISMEMBER(td->td_sigmask, sig)) { 1600 if (td == curthread) 1601 signal_td = curthread; 1602 else if (signal_td == NULL) 1603 signal_td = td; 1604 } 1605 } 1606 if (signal_td == NULL) 1607 signal_td = FIRST_THREAD_IN_PROC(p); 1608 return (signal_td); 1609 } 1610 1611 /* 1612 * Send the signal to the process. If the signal has an action, the action 1613 * is usually performed by the target process rather than the caller; we add 1614 * the signal to the set of pending signals for the process. 1615 * 1616 * Exceptions: 1617 * o When a stop signal is sent to a sleeping process that takes the 1618 * default action, the process is stopped without awakening it. 1619 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1620 * regardless of the signal action (eg, blocked or ignored). 1621 * 1622 * Other ignored signals are discarded immediately. 1623 * 1624 * MPSAFE 1625 */ 1626 void 1627 psignal(struct proc *p, int sig) 1628 { 1629 struct thread *td; 1630 int prop; 1631 1632 if (!_SIG_VALID(sig)) 1633 panic("psignal(): invalid signal"); 1634 1635 PROC_LOCK_ASSERT(p, MA_OWNED); 1636 prop = sigprop(sig); 1637 1638 /* 1639 * Find a thread to deliver the signal to. 1640 */ 1641 td = sigtd(p, sig, prop); 1642 1643 tdsignal(td, sig, SIGTARGET_P); 1644 } 1645 1646 /* 1647 * MPSAFE 1648 */ 1649 void 1650 tdsignal(struct thread *td, int sig, sigtarget_t target) 1651 { 1652 sigset_t saved; 1653 struct proc *p = td->td_proc; 1654 1655 if (p->p_flag & P_SA) 1656 saved = p->p_siglist; 1657 do_tdsignal(td, sig, target); 1658 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 1659 if (SIGSETEQ(saved, p->p_siglist)) 1660 return; 1661 else { 1662 /* pending set changed */ 1663 p->p_flag |= P_SIGEVENT; 1664 wakeup(&p->p_siglist); 1665 } 1666 } 1667 } 1668 1669 static void 1670 do_tdsignal(struct thread *td, int sig, sigtarget_t target) 1671 { 1672 struct proc *p; 1673 register sig_t action; 1674 sigset_t *siglist; 1675 struct thread *td0; 1676 register int prop; 1677 struct sigacts *ps; 1678 1679 if (!_SIG_VALID(sig)) 1680 panic("do_tdsignal(): invalid signal"); 1681 1682 p = td->td_proc; 1683 ps = p->p_sigacts; 1684 1685 PROC_LOCK_ASSERT(p, MA_OWNED); 1686 KNOTE(&p->p_klist, NOTE_SIGNAL | sig); 1687 1688 prop = sigprop(sig); 1689 1690 /* 1691 * If the signal is blocked and not destined for this thread, then 1692 * assign it to the process so that we can find it later in the first 1693 * thread that unblocks it. Otherwise, assign it to this thread now. 1694 */ 1695 if (target == SIGTARGET_TD) { 1696 siglist = &td->td_siglist; 1697 } else { 1698 if (!SIGISMEMBER(td->td_sigmask, sig)) 1699 siglist = &td->td_siglist; 1700 else if (td->td_waitset != NULL && 1701 SIGISMEMBER(*(td->td_waitset), sig)) 1702 siglist = &td->td_siglist; 1703 else 1704 siglist = &p->p_siglist; 1705 } 1706 1707 /* 1708 * If proc is traced, always give parent a chance; 1709 * if signal event is tracked by procfs, give *that* 1710 * a chance, as well. 1711 */ 1712 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) { 1713 action = SIG_DFL; 1714 } else { 1715 /* 1716 * If the signal is being ignored, 1717 * then we forget about it immediately. 1718 * (Note: we don't set SIGCONT in ps_sigignore, 1719 * and if it is set to SIG_IGN, 1720 * action will be SIG_DFL here.) 1721 */ 1722 mtx_lock(&ps->ps_mtx); 1723 if (SIGISMEMBER(ps->ps_sigignore, sig) || 1724 (p->p_flag & P_WEXIT)) { 1725 mtx_unlock(&ps->ps_mtx); 1726 return; 1727 } 1728 if (((td->td_waitset == NULL) && 1729 SIGISMEMBER(td->td_sigmask, sig)) || 1730 ((td->td_waitset != NULL) && 1731 SIGISMEMBER(td->td_sigmask, sig) && 1732 !SIGISMEMBER(*(td->td_waitset), sig))) 1733 action = SIG_HOLD; 1734 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 1735 action = SIG_CATCH; 1736 else 1737 action = SIG_DFL; 1738 mtx_unlock(&ps->ps_mtx); 1739 } 1740 1741 if (prop & SA_CONT) { 1742 SIG_STOPSIGMASK(p->p_siglist); 1743 /* 1744 * XXX Should investigate leaving STOP and CONT sigs only in 1745 * the proc's siglist. 1746 */ 1747 FOREACH_THREAD_IN_PROC(p, td0) 1748 SIG_STOPSIGMASK(td0->td_siglist); 1749 } 1750 1751 if (prop & SA_STOP) { 1752 /* 1753 * If sending a tty stop signal to a member of an orphaned 1754 * process group, discard the signal here if the action 1755 * is default; don't stop the process below if sleeping, 1756 * and don't clear any pending SIGCONT. 1757 */ 1758 if ((prop & SA_TTYSTOP) && 1759 (p->p_pgrp->pg_jobc == 0) && 1760 (action == SIG_DFL)) 1761 return; 1762 SIG_CONTSIGMASK(p->p_siglist); 1763 FOREACH_THREAD_IN_PROC(p, td0) 1764 SIG_CONTSIGMASK(td0->td_siglist); 1765 p->p_flag &= ~P_CONTINUED; 1766 } 1767 1768 SIGADDSET(*siglist, sig); 1769 signotify(td); /* uses schedlock */ 1770 if (siglist == &td->td_siglist && (td->td_waitset != NULL) && 1771 action != SIG_HOLD) { 1772 td->td_waitset = NULL; 1773 } 1774 1775 /* 1776 * Defer further processing for signals which are held, 1777 * except that stopped processes must be continued by SIGCONT. 1778 */ 1779 if (action == SIG_HOLD && 1780 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 1781 return; 1782 /* 1783 * Some signals have a process-wide effect and a per-thread 1784 * component. Most processing occurs when the process next 1785 * tries to cross the user boundary, however there are some 1786 * times when processing needs to be done immediatly, such as 1787 * waking up threads so that they can cross the user boundary. 1788 * We try do the per-process part here. 1789 */ 1790 if (P_SHOULDSTOP(p)) { 1791 /* 1792 * The process is in stopped mode. All the threads should be 1793 * either winding down or already on the suspended queue. 1794 */ 1795 if (p->p_flag & P_TRACED) { 1796 /* 1797 * The traced process is already stopped, 1798 * so no further action is necessary. 1799 * No signal can restart us. 1800 */ 1801 goto out; 1802 } 1803 1804 if (sig == SIGKILL) { 1805 /* 1806 * SIGKILL sets process running. 1807 * It will die elsewhere. 1808 * All threads must be restarted. 1809 */ 1810 p->p_flag &= ~P_STOPPED; 1811 goto runfast; 1812 } 1813 1814 if (prop & SA_CONT) { 1815 /* 1816 * If SIGCONT is default (or ignored), we continue the 1817 * process but don't leave the signal in siglist as 1818 * it has no further action. If SIGCONT is held, we 1819 * continue the process and leave the signal in 1820 * siglist. If the process catches SIGCONT, let it 1821 * handle the signal itself. If it isn't waiting on 1822 * an event, it goes back to run state. 1823 * Otherwise, process goes back to sleep state. 1824 */ 1825 p->p_flag &= ~P_STOPPED_SIG; 1826 p->p_flag |= P_CONTINUED; 1827 if (action == SIG_DFL) { 1828 SIGDELSET(*siglist, sig); 1829 } else if (action == SIG_CATCH) { 1830 /* 1831 * The process wants to catch it so it needs 1832 * to run at least one thread, but which one? 1833 * It would seem that the answer would be to 1834 * run an upcall in the next KSE to run, and 1835 * deliver the signal that way. In a NON KSE 1836 * process, we need to make sure that the 1837 * single thread is runnable asap. 1838 * XXXKSE for now however, make them all run. 1839 */ 1840 goto runfast; 1841 } 1842 /* 1843 * The signal is not ignored or caught. 1844 */ 1845 mtx_lock_spin(&sched_lock); 1846 thread_unsuspend(p); 1847 mtx_unlock_spin(&sched_lock); 1848 goto out; 1849 } 1850 1851 if (prop & SA_STOP) { 1852 /* 1853 * Already stopped, don't need to stop again 1854 * (If we did the shell could get confused). 1855 * Just make sure the signal STOP bit set. 1856 */ 1857 p->p_flag |= P_STOPPED_SIG; 1858 SIGDELSET(*siglist, sig); 1859 goto out; 1860 } 1861 1862 /* 1863 * All other kinds of signals: 1864 * If a thread is sleeping interruptibly, simulate a 1865 * wakeup so that when it is continued it will be made 1866 * runnable and can look at the signal. However, don't make 1867 * the PROCESS runnable, leave it stopped. 1868 * It may run a bit until it hits a thread_suspend_check(). 1869 */ 1870 mtx_lock_spin(&sched_lock); 1871 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) { 1872 if (td->td_flags & TDF_CVWAITQ) 1873 cv_abort(td); 1874 else 1875 abortsleep(td); 1876 } 1877 mtx_unlock_spin(&sched_lock); 1878 goto out; 1879 /* 1880 * XXXKSE What about threads that are waiting on mutexes? 1881 * Shouldn't they abort too? 1882 * No, hopefully mutexes are short lived.. They'll 1883 * eventually hit thread_suspend_check(). 1884 */ 1885 } else if (p->p_state == PRS_NORMAL) { 1886 if ((p->p_flag & P_TRACED) || (action != SIG_DFL) || 1887 !(prop & SA_STOP)) { 1888 mtx_lock_spin(&sched_lock); 1889 tdsigwakeup(td, sig, action); 1890 mtx_unlock_spin(&sched_lock); 1891 goto out; 1892 } 1893 if (prop & SA_STOP) { 1894 if (p->p_flag & P_PPWAIT) 1895 goto out; 1896 p->p_flag |= P_STOPPED_SIG; 1897 p->p_xstat = sig; 1898 mtx_lock_spin(&sched_lock); 1899 FOREACH_THREAD_IN_PROC(p, td0) { 1900 if (TD_IS_SLEEPING(td0) && 1901 (td0->td_flags & TDF_SINTR) && 1902 !TD_IS_SUSPENDED(td0)) { 1903 thread_suspend_one(td0); 1904 } else if (td != td0) { 1905 td0->td_flags |= TDF_ASTPENDING; 1906 } 1907 } 1908 thread_stopped(p); 1909 if (p->p_numthreads == p->p_suspcount) { 1910 SIGDELSET(p->p_siglist, p->p_xstat); 1911 FOREACH_THREAD_IN_PROC(p, td0) 1912 SIGDELSET(td0->td_siglist, p->p_xstat); 1913 } 1914 mtx_unlock_spin(&sched_lock); 1915 goto out; 1916 } 1917 else 1918 goto runfast; 1919 /* NOTREACHED */ 1920 } else { 1921 /* Not in "NORMAL" state. discard the signal. */ 1922 SIGDELSET(*siglist, sig); 1923 goto out; 1924 } 1925 1926 /* 1927 * The process is not stopped so we need to apply the signal to all the 1928 * running threads. 1929 */ 1930 1931 runfast: 1932 mtx_lock_spin(&sched_lock); 1933 tdsigwakeup(td, sig, action); 1934 thread_unsuspend(p); 1935 mtx_unlock_spin(&sched_lock); 1936 out: 1937 /* If we jump here, sched_lock should not be owned. */ 1938 mtx_assert(&sched_lock, MA_NOTOWNED); 1939 } 1940 1941 /* 1942 * The force of a signal has been directed against a single 1943 * thread. We need to see what we can do about knocking it 1944 * out of any sleep it may be in etc. 1945 */ 1946 static void 1947 tdsigwakeup(struct thread *td, int sig, sig_t action) 1948 { 1949 struct proc *p = td->td_proc; 1950 register int prop; 1951 1952 PROC_LOCK_ASSERT(p, MA_OWNED); 1953 mtx_assert(&sched_lock, MA_OWNED); 1954 prop = sigprop(sig); 1955 /* 1956 * Bring the priority of a thread up if we want it to get 1957 * killed in this lifetime. 1958 */ 1959 if ((action == SIG_DFL) && (prop & SA_KILL)) { 1960 if (td->td_priority > PUSER) { 1961 td->td_priority = PUSER; 1962 } 1963 } 1964 if (TD_IS_SLEEPING(td)) { 1965 /* 1966 * If thread is sleeping uninterruptibly 1967 * we can't interrupt the sleep... the signal will 1968 * be noticed when the process returns through 1969 * trap() or syscall(). 1970 */ 1971 if ((td->td_flags & TDF_SINTR) == 0) { 1972 return; 1973 } 1974 /* 1975 * Process is sleeping and traced. Make it runnable 1976 * so it can discover the signal in issignal() and stop 1977 * for its parent. 1978 */ 1979 if (p->p_flag & P_TRACED) { 1980 p->p_flag &= ~P_STOPPED_TRACE; 1981 } else { 1982 1983 /* 1984 * If SIGCONT is default (or ignored) and process is 1985 * asleep, we are finished; the process should not 1986 * be awakened. 1987 */ 1988 if ((prop & SA_CONT) && action == SIG_DFL) { 1989 SIGDELSET(p->p_siglist, sig); 1990 /* 1991 * It may be on either list in this state. 1992 * Remove from both for now. 1993 */ 1994 SIGDELSET(td->td_siglist, sig); 1995 return; 1996 } 1997 1998 /* 1999 * Raise priority to at least PUSER. 2000 */ 2001 if (td->td_priority > PUSER) { 2002 td->td_priority = PUSER; 2003 } 2004 } 2005 if (td->td_flags & TDF_CVWAITQ) 2006 cv_abort(td); 2007 else 2008 abortsleep(td); 2009 } 2010 #ifdef SMP 2011 else { 2012 /* 2013 * Other states do nothing with the signal immediatly, 2014 * other than kicking ourselves if we are running. 2015 * It will either never be noticed, or noticed very soon. 2016 */ 2017 if (TD_IS_RUNNING(td) && td != curthread) { 2018 forward_signal(td); 2019 } 2020 } 2021 #endif 2022 } 2023 2024 /* 2025 * If the current process has received a signal (should be caught or cause 2026 * termination, should interrupt current syscall), return the signal number. 2027 * Stop signals with default action are processed immediately, then cleared; 2028 * they aren't returned. This is checked after each entry to the system for 2029 * a syscall or trap (though this can usually be done without calling issignal 2030 * by checking the pending signal masks in cursig.) The normal call 2031 * sequence is 2032 * 2033 * while (sig = cursig(curthread)) 2034 * postsig(sig); 2035 */ 2036 static int 2037 issignal(td) 2038 struct thread *td; 2039 { 2040 struct proc *p; 2041 struct sigacts *ps; 2042 sigset_t sigpending; 2043 int sig, prop; 2044 struct thread *td0; 2045 2046 p = td->td_proc; 2047 ps = p->p_sigacts; 2048 mtx_assert(&ps->ps_mtx, MA_OWNED); 2049 PROC_LOCK_ASSERT(p, MA_OWNED); 2050 for (;;) { 2051 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2052 2053 sigpending = td->td_siglist; 2054 SIGSETNAND(sigpending, td->td_sigmask); 2055 2056 if (p->p_flag & P_PPWAIT) 2057 SIG_STOPSIGMASK(sigpending); 2058 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2059 return (0); 2060 sig = sig_ffs(&sigpending); 2061 2062 _STOPEVENT(p, S_SIG, sig); 2063 2064 /* 2065 * We should see pending but ignored signals 2066 * only if P_TRACED was on when they were posted. 2067 */ 2068 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2069 SIGDELSET(td->td_siglist, sig); 2070 continue; 2071 } 2072 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2073 /* 2074 * If traced, always stop. 2075 */ 2076 mtx_unlock(&ps->ps_mtx); 2077 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2078 &p->p_mtx.mtx_object, "Stopping for traced signal"); 2079 p->p_xstat = sig; 2080 PROC_LOCK(p->p_pptr); 2081 psignal(p->p_pptr, SIGCHLD); 2082 PROC_UNLOCK(p->p_pptr); 2083 mtx_lock_spin(&sched_lock); 2084 stop(p); /* uses schedlock too eventually */ 2085 thread_suspend_one(td); 2086 PROC_UNLOCK(p); 2087 DROP_GIANT(); 2088 p->p_stats->p_ru.ru_nivcsw++; 2089 mi_switch(); 2090 mtx_unlock_spin(&sched_lock); 2091 PICKUP_GIANT(); 2092 PROC_LOCK(p); 2093 mtx_lock(&ps->ps_mtx); 2094 2095 /* 2096 * If parent wants us to take the signal, 2097 * then it will leave it in p->p_xstat; 2098 * otherwise we just look for signals again. 2099 */ 2100 SIGDELSET(td->td_siglist, sig); /* clear old signal */ 2101 sig = p->p_xstat; 2102 if (sig == 0) 2103 continue; 2104 2105 /* 2106 * If the traced bit got turned off, go back up 2107 * to the top to rescan signals. This ensures 2108 * that p_sig* and p_sigact are consistent. 2109 */ 2110 if ((p->p_flag & P_TRACED) == 0) 2111 continue; 2112 2113 /* 2114 * Put the new signal into td_siglist. If the 2115 * signal is being masked, look for other signals. 2116 */ 2117 SIGADDSET(td->td_siglist, sig); 2118 if (SIGISMEMBER(td->td_sigmask, sig)) 2119 continue; 2120 signotify(td); 2121 } 2122 2123 prop = sigprop(sig); 2124 2125 /* 2126 * Decide whether the signal should be returned. 2127 * Return the signal's number, or fall through 2128 * to clear it from the pending mask. 2129 */ 2130 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2131 2132 case (intptr_t)SIG_DFL: 2133 /* 2134 * Don't take default actions on system processes. 2135 */ 2136 if (p->p_pid <= 1) { 2137 #ifdef DIAGNOSTIC 2138 /* 2139 * Are you sure you want to ignore SIGSEGV 2140 * in init? XXX 2141 */ 2142 printf("Process (pid %lu) got signal %d\n", 2143 (u_long)p->p_pid, sig); 2144 #endif 2145 break; /* == ignore */ 2146 } 2147 /* 2148 * If there is a pending stop signal to process 2149 * with default action, stop here, 2150 * then clear the signal. However, 2151 * if process is member of an orphaned 2152 * process group, ignore tty stop signals. 2153 */ 2154 if (prop & SA_STOP) { 2155 if (p->p_flag & P_TRACED || 2156 (p->p_pgrp->pg_jobc == 0 && 2157 prop & SA_TTYSTOP)) 2158 break; /* == ignore */ 2159 mtx_unlock(&ps->ps_mtx); 2160 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2161 &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2162 p->p_flag |= P_STOPPED_SIG; 2163 p->p_xstat = sig; 2164 mtx_lock_spin(&sched_lock); 2165 FOREACH_THREAD_IN_PROC(p, td0) { 2166 if (TD_IS_SLEEPING(td0) && 2167 (td0->td_flags & TDF_SINTR) && 2168 !TD_IS_SUSPENDED(td0)) { 2169 thread_suspend_one(td0); 2170 } else if (td != td0) { 2171 td0->td_flags |= TDF_ASTPENDING; 2172 } 2173 } 2174 thread_stopped(p); 2175 thread_suspend_one(td); 2176 PROC_UNLOCK(p); 2177 DROP_GIANT(); 2178 p->p_stats->p_ru.ru_nivcsw++; 2179 mi_switch(); 2180 mtx_unlock_spin(&sched_lock); 2181 PICKUP_GIANT(); 2182 PROC_LOCK(p); 2183 mtx_lock(&ps->ps_mtx); 2184 break; 2185 } else if (prop & SA_IGNORE) { 2186 /* 2187 * Except for SIGCONT, shouldn't get here. 2188 * Default action is to ignore; drop it. 2189 */ 2190 break; /* == ignore */ 2191 } else 2192 return (sig); 2193 /*NOTREACHED*/ 2194 2195 case (intptr_t)SIG_IGN: 2196 /* 2197 * Masking above should prevent us ever trying 2198 * to take action on an ignored signal other 2199 * than SIGCONT, unless process is traced. 2200 */ 2201 if ((prop & SA_CONT) == 0 && 2202 (p->p_flag & P_TRACED) == 0) 2203 printf("issignal\n"); 2204 break; /* == ignore */ 2205 2206 default: 2207 /* 2208 * This signal has an action, let 2209 * postsig() process it. 2210 */ 2211 return (sig); 2212 } 2213 SIGDELSET(td->td_siglist, sig); /* take the signal! */ 2214 } 2215 /* NOTREACHED */ 2216 } 2217 2218 /* 2219 * Put the argument process into the stopped state and notify the parent 2220 * via wakeup. Signals are handled elsewhere. The process must not be 2221 * on the run queue. Must be called with the proc p locked and the scheduler 2222 * lock held. 2223 */ 2224 static void 2225 stop(struct proc *p) 2226 { 2227 2228 PROC_LOCK_ASSERT(p, MA_OWNED); 2229 p->p_flag |= P_STOPPED_SIG; 2230 p->p_flag &= ~P_WAITED; 2231 wakeup(p->p_pptr); 2232 } 2233 2234 /* 2235 * MPSAFE 2236 */ 2237 void 2238 thread_stopped(struct proc *p) 2239 { 2240 struct proc *p1 = curthread->td_proc; 2241 struct sigacts *ps; 2242 int n; 2243 2244 PROC_LOCK_ASSERT(p, MA_OWNED); 2245 mtx_assert(&sched_lock, MA_OWNED); 2246 n = p->p_suspcount; 2247 if (p == p1) 2248 n++; 2249 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2250 mtx_unlock_spin(&sched_lock); 2251 stop(p); 2252 PROC_LOCK(p->p_pptr); 2253 ps = p->p_pptr->p_sigacts; 2254 mtx_lock(&ps->ps_mtx); 2255 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 2256 mtx_unlock(&ps->ps_mtx); 2257 psignal(p->p_pptr, SIGCHLD); 2258 } else 2259 mtx_unlock(&ps->ps_mtx); 2260 PROC_UNLOCK(p->p_pptr); 2261 mtx_lock_spin(&sched_lock); 2262 } 2263 } 2264 2265 /* 2266 * Take the action for the specified signal 2267 * from the current set of pending signals. 2268 */ 2269 void 2270 postsig(sig) 2271 register int sig; 2272 { 2273 struct thread *td = curthread; 2274 register struct proc *p = td->td_proc; 2275 struct sigacts *ps; 2276 sig_t action; 2277 sigset_t returnmask; 2278 int code; 2279 2280 KASSERT(sig != 0, ("postsig")); 2281 2282 PROC_LOCK_ASSERT(p, MA_OWNED); 2283 ps = p->p_sigacts; 2284 mtx_assert(&ps->ps_mtx, MA_OWNED); 2285 SIGDELSET(td->td_siglist, sig); 2286 action = ps->ps_sigact[_SIG_IDX(sig)]; 2287 #ifdef KTRACE 2288 if (KTRPOINT(td, KTR_PSIG)) 2289 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2290 &td->td_oldsigmask : &td->td_sigmask, 0); 2291 #endif 2292 _STOPEVENT(p, S_SIG, sig); 2293 2294 if (!(td->td_flags & TDF_SA && td->td_mailbox) && 2295 action == SIG_DFL) { 2296 /* 2297 * Default action, where the default is to kill 2298 * the process. (Other cases were ignored above.) 2299 */ 2300 mtx_unlock(&ps->ps_mtx); 2301 sigexit(td, sig); 2302 /* NOTREACHED */ 2303 } else { 2304 if (td->td_flags & TDF_SA && td->td_mailbox) { 2305 if (sig == SIGKILL) { 2306 mtx_unlock(&ps->ps_mtx); 2307 sigexit(td, sig); 2308 } 2309 } 2310 2311 /* 2312 * If we get here, the signal must be caught. 2313 */ 2314 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2315 ("postsig action")); 2316 /* 2317 * Set the new mask value and also defer further 2318 * occurrences of this signal. 2319 * 2320 * Special case: user has done a sigsuspend. Here the 2321 * current mask is not of interest, but rather the 2322 * mask from before the sigsuspend is what we want 2323 * restored after the signal processing is completed. 2324 */ 2325 if (td->td_pflags & TDP_OLDMASK) { 2326 returnmask = td->td_oldsigmask; 2327 td->td_pflags &= ~TDP_OLDMASK; 2328 } else 2329 returnmask = td->td_sigmask; 2330 2331 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 2332 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2333 SIGADDSET(td->td_sigmask, sig); 2334 2335 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2336 /* 2337 * See kern_sigaction() for origin of this code. 2338 */ 2339 SIGDELSET(ps->ps_sigcatch, sig); 2340 if (sig != SIGCONT && 2341 sigprop(sig) & SA_IGNORE) 2342 SIGADDSET(ps->ps_sigignore, sig); 2343 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2344 } 2345 p->p_stats->p_ru.ru_nsignals++; 2346 if (p->p_sig != sig) { 2347 code = 0; 2348 } else { 2349 code = p->p_code; 2350 p->p_code = 0; 2351 p->p_sig = 0; 2352 } 2353 if (td->td_flags & TDF_SA && td->td_mailbox) 2354 thread_signal_add(curthread, sig); 2355 else 2356 (*p->p_sysent->sv_sendsig)(action, sig, 2357 &returnmask, code); 2358 } 2359 } 2360 2361 /* 2362 * Kill the current process for stated reason. 2363 */ 2364 void 2365 killproc(p, why) 2366 struct proc *p; 2367 char *why; 2368 { 2369 2370 PROC_LOCK_ASSERT(p, MA_OWNED); 2371 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 2372 p, p->p_pid, p->p_comm); 2373 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2374 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2375 psignal(p, SIGKILL); 2376 } 2377 2378 /* 2379 * Force the current process to exit with the specified signal, dumping core 2380 * if appropriate. We bypass the normal tests for masked and caught signals, 2381 * allowing unrecoverable failures to terminate the process without changing 2382 * signal state. Mark the accounting record with the signal termination. 2383 * If dumping core, save the signal number for the debugger. Calls exit and 2384 * does not return. 2385 * 2386 * MPSAFE 2387 */ 2388 void 2389 sigexit(td, sig) 2390 struct thread *td; 2391 int sig; 2392 { 2393 struct proc *p = td->td_proc; 2394 2395 PROC_LOCK_ASSERT(p, MA_OWNED); 2396 p->p_acflag |= AXSIG; 2397 if (sigprop(sig) & SA_CORE) { 2398 p->p_sig = sig; 2399 /* 2400 * Log signals which would cause core dumps 2401 * (Log as LOG_INFO to appease those who don't want 2402 * these messages.) 2403 * XXX : Todo, as well as euid, write out ruid too 2404 */ 2405 PROC_UNLOCK(p); 2406 if (!mtx_owned(&Giant)) 2407 mtx_lock(&Giant); 2408 if (coredump(td) == 0) 2409 sig |= WCOREFLAG; 2410 if (kern_logsigexit) 2411 log(LOG_INFO, 2412 "pid %d (%s), uid %d: exited on signal %d%s\n", 2413 p->p_pid, p->p_comm, 2414 td->td_ucred ? td->td_ucred->cr_uid : -1, 2415 sig &~ WCOREFLAG, 2416 sig & WCOREFLAG ? " (core dumped)" : ""); 2417 } else { 2418 PROC_UNLOCK(p); 2419 if (!mtx_owned(&Giant)) 2420 mtx_lock(&Giant); 2421 } 2422 exit1(td, W_EXITCODE(0, sig)); 2423 /* NOTREACHED */ 2424 } 2425 2426 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 2427 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2428 sizeof(corefilename), "process corefile name format string"); 2429 2430 /* 2431 * expand_name(name, uid, pid) 2432 * Expand the name described in corefilename, using name, uid, and pid. 2433 * corefilename is a printf-like string, with three format specifiers: 2434 * %N name of process ("name") 2435 * %P process id (pid) 2436 * %U user id (uid) 2437 * For example, "%N.core" is the default; they can be disabled completely 2438 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2439 * This is controlled by the sysctl variable kern.corefile (see above). 2440 */ 2441 2442 static char * 2443 expand_name(name, uid, pid) 2444 const char *name; 2445 uid_t uid; 2446 pid_t pid; 2447 { 2448 const char *format, *appendstr; 2449 char *temp; 2450 char buf[11]; /* Buffer for pid/uid -- max 4B */ 2451 size_t i, l, n; 2452 2453 format = corefilename; 2454 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 2455 if (temp == NULL) 2456 return (NULL); 2457 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2458 switch (format[i]) { 2459 case '%': /* Format character */ 2460 i++; 2461 switch (format[i]) { 2462 case '%': 2463 appendstr = "%"; 2464 break; 2465 case 'N': /* process name */ 2466 appendstr = name; 2467 break; 2468 case 'P': /* process id */ 2469 sprintf(buf, "%u", pid); 2470 appendstr = buf; 2471 break; 2472 case 'U': /* user id */ 2473 sprintf(buf, "%u", uid); 2474 appendstr = buf; 2475 break; 2476 default: 2477 appendstr = ""; 2478 log(LOG_ERR, 2479 "Unknown format character %c in `%s'\n", 2480 format[i], format); 2481 } 2482 l = strlen(appendstr); 2483 if ((n + l) >= MAXPATHLEN) 2484 goto toolong; 2485 memcpy(temp + n, appendstr, l); 2486 n += l; 2487 break; 2488 default: 2489 temp[n++] = format[i]; 2490 } 2491 } 2492 if (format[i] != '\0') 2493 goto toolong; 2494 return (temp); 2495 toolong: 2496 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 2497 (long)pid, name, (u_long)uid); 2498 free(temp, M_TEMP); 2499 return (NULL); 2500 } 2501 2502 /* 2503 * Dump a process' core. The main routine does some 2504 * policy checking, and creates the name of the coredump; 2505 * then it passes on a vnode and a size limit to the process-specific 2506 * coredump routine if there is one; if there _is not_ one, it returns 2507 * ENOSYS; otherwise it returns the error from the process-specific routine. 2508 */ 2509 2510 static int 2511 coredump(struct thread *td) 2512 { 2513 struct proc *p = td->td_proc; 2514 register struct vnode *vp; 2515 register struct ucred *cred = td->td_ucred; 2516 struct flock lf; 2517 struct nameidata nd; 2518 struct vattr vattr; 2519 int error, error1, flags; 2520 struct mount *mp; 2521 char *name; /* name of corefile */ 2522 off_t limit; 2523 2524 PROC_LOCK(p); 2525 _STOPEVENT(p, S_CORE, 0); 2526 2527 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 2528 PROC_UNLOCK(p); 2529 return (EFAULT); 2530 } 2531 2532 /* 2533 * Note that the bulk of limit checking is done after 2534 * the corefile is created. The exception is if the limit 2535 * for corefiles is 0, in which case we don't bother 2536 * creating the corefile at all. This layout means that 2537 * a corefile is truncated instead of not being created, 2538 * if it is larger than the limit. 2539 */ 2540 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 2541 if (limit == 0) { 2542 PROC_UNLOCK(p); 2543 return 0; 2544 } 2545 PROC_UNLOCK(p); 2546 2547 restart: 2548 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 2549 if (name == NULL) 2550 return (EINVAL); 2551 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2552 flags = O_CREAT | FWRITE | O_NOFOLLOW; 2553 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 2554 free(name, M_TEMP); 2555 if (error) 2556 return (error); 2557 NDFREE(&nd, NDF_ONLY_PNBUF); 2558 vp = nd.ni_vp; 2559 2560 /* Don't dump to non-regular files or files with links. */ 2561 if (vp->v_type != VREG || 2562 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2563 VOP_UNLOCK(vp, 0, td); 2564 error = EFAULT; 2565 goto out2; 2566 } 2567 2568 VOP_UNLOCK(vp, 0, td); 2569 lf.l_whence = SEEK_SET; 2570 lf.l_start = 0; 2571 lf.l_len = 0; 2572 lf.l_type = F_WRLCK; 2573 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 2574 if (error) 2575 goto out2; 2576 2577 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2578 lf.l_type = F_UNLCK; 2579 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2580 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2581 return (error); 2582 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2583 return (error); 2584 goto restart; 2585 } 2586 2587 VATTR_NULL(&vattr); 2588 vattr.va_size = 0; 2589 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2590 VOP_LEASE(vp, td, cred, LEASE_WRITE); 2591 VOP_SETATTR(vp, &vattr, cred, td); 2592 VOP_UNLOCK(vp, 0, td); 2593 PROC_LOCK(p); 2594 p->p_acflag |= ACORE; 2595 PROC_UNLOCK(p); 2596 2597 error = p->p_sysent->sv_coredump ? 2598 p->p_sysent->sv_coredump(td, vp, limit) : 2599 ENOSYS; 2600 2601 lf.l_type = F_UNLCK; 2602 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2603 vn_finished_write(mp); 2604 out2: 2605 error1 = vn_close(vp, FWRITE, cred, td); 2606 if (error == 0) 2607 error = error1; 2608 return (error); 2609 } 2610 2611 /* 2612 * Nonexistent system call-- signal process (may want to handle it). 2613 * Flag error in case process won't see signal immediately (blocked or ignored). 2614 */ 2615 #ifndef _SYS_SYSPROTO_H_ 2616 struct nosys_args { 2617 int dummy; 2618 }; 2619 #endif 2620 /* 2621 * MPSAFE 2622 */ 2623 /* ARGSUSED */ 2624 int 2625 nosys(td, args) 2626 struct thread *td; 2627 struct nosys_args *args; 2628 { 2629 struct proc *p = td->td_proc; 2630 2631 PROC_LOCK(p); 2632 psignal(p, SIGSYS); 2633 PROC_UNLOCK(p); 2634 return (ENOSYS); 2635 } 2636 2637 /* 2638 * Send a SIGIO or SIGURG signal to a process or process group using 2639 * stored credentials rather than those of the current process. 2640 */ 2641 void 2642 pgsigio(sigiop, sig, checkctty) 2643 struct sigio **sigiop; 2644 int sig, checkctty; 2645 { 2646 struct sigio *sigio; 2647 2648 SIGIO_LOCK(); 2649 sigio = *sigiop; 2650 if (sigio == NULL) { 2651 SIGIO_UNLOCK(); 2652 return; 2653 } 2654 if (sigio->sio_pgid > 0) { 2655 PROC_LOCK(sigio->sio_proc); 2656 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 2657 psignal(sigio->sio_proc, sig); 2658 PROC_UNLOCK(sigio->sio_proc); 2659 } else if (sigio->sio_pgid < 0) { 2660 struct proc *p; 2661 2662 PGRP_LOCK(sigio->sio_pgrp); 2663 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2664 PROC_LOCK(p); 2665 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2666 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 2667 psignal(p, sig); 2668 PROC_UNLOCK(p); 2669 } 2670 PGRP_UNLOCK(sigio->sio_pgrp); 2671 } 2672 SIGIO_UNLOCK(); 2673 } 2674 2675 static int 2676 filt_sigattach(struct knote *kn) 2677 { 2678 struct proc *p = curproc; 2679 2680 kn->kn_ptr.p_proc = p; 2681 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2682 2683 PROC_LOCK(p); 2684 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2685 PROC_UNLOCK(p); 2686 2687 return (0); 2688 } 2689 2690 static void 2691 filt_sigdetach(struct knote *kn) 2692 { 2693 struct proc *p = kn->kn_ptr.p_proc; 2694 2695 PROC_LOCK(p); 2696 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2697 PROC_UNLOCK(p); 2698 } 2699 2700 /* 2701 * signal knotes are shared with proc knotes, so we apply a mask to 2702 * the hint in order to differentiate them from process hints. This 2703 * could be avoided by using a signal-specific knote list, but probably 2704 * isn't worth the trouble. 2705 */ 2706 static int 2707 filt_signal(struct knote *kn, long hint) 2708 { 2709 2710 if (hint & NOTE_SIGNAL) { 2711 hint &= ~NOTE_SIGNAL; 2712 2713 if (kn->kn_id == hint) 2714 kn->kn_data++; 2715 } 2716 return (kn->kn_data != 0); 2717 } 2718 2719 struct sigacts * 2720 sigacts_alloc(void) 2721 { 2722 struct sigacts *ps; 2723 2724 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 2725 ps->ps_refcnt = 1; 2726 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 2727 return (ps); 2728 } 2729 2730 void 2731 sigacts_free(struct sigacts *ps) 2732 { 2733 2734 mtx_lock(&ps->ps_mtx); 2735 ps->ps_refcnt--; 2736 if (ps->ps_refcnt == 0) { 2737 mtx_destroy(&ps->ps_mtx); 2738 free(ps, M_SUBPROC); 2739 } else 2740 mtx_unlock(&ps->ps_mtx); 2741 } 2742 2743 struct sigacts * 2744 sigacts_hold(struct sigacts *ps) 2745 { 2746 mtx_lock(&ps->ps_mtx); 2747 ps->ps_refcnt++; 2748 mtx_unlock(&ps->ps_mtx); 2749 return (ps); 2750 } 2751 2752 void 2753 sigacts_copy(struct sigacts *dest, struct sigacts *src) 2754 { 2755 2756 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 2757 mtx_lock(&src->ps_mtx); 2758 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 2759 mtx_unlock(&src->ps_mtx); 2760 } 2761 2762 int 2763 sigacts_shared(struct sigacts *ps) 2764 { 2765 int shared; 2766 2767 mtx_lock(&ps->ps_mtx); 2768 shared = ps->ps_refcnt > 1; 2769 mtx_unlock(&ps->ps_mtx); 2770 return (shared); 2771 } 2772