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