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