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/kernel.h> 47 #include <sys/sysproto.h> 48 #include <sys/signalvar.h> 49 #include <sys/resourcevar.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/proc.h> 53 #include <sys/pioctl.h> 54 #include <sys/systm.h> 55 #include <sys/acct.h> 56 #include <sys/fcntl.h> 57 #include <sys/wait.h> 58 #include <sys/ktrace.h> 59 #include <sys/syslog.h> 60 #include <sys/stat.h> 61 #include <sys/sysent.h> 62 #include <sys/sysctl.h> 63 #include <sys/malloc.h> 64 65 #include <vm/vm_zone.h> 66 67 #include <machine/cpu.h> 68 #ifdef SMP 69 #include <machine/smp.h> 70 #endif 71 72 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 73 74 static int coredump __P((struct proc *)); 75 static int do_sigaction __P((struct proc *p, int sig, struct sigaction *act, 76 struct sigaction *oact, int old)); 77 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set, 78 sigset_t *oset, int old)); 79 static char *expand_name __P((const char *, uid_t, pid_t)); 80 static int killpg1 __P((struct proc *cp, int sig, int pgid, int all)); 81 static int sig_ffs __P((sigset_t *set)); 82 static int sigprop __P((int sig)); 83 static void stop __P((struct proc *)); 84 85 static int kern_logsigexit = 1; 86 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 87 &kern_logsigexit, 0, 88 "Log processes quitting on abnormal signals to syslog(3)"); 89 90 /* 91 * Can process p, with pcred pc, send the signal sig to process q? 92 */ 93 #define CANSIGNAL(p, q, sig) \ 94 (!p_trespass(p, q) || \ 95 ((sig) == SIGCONT && (q)->p_session == (p)->p_session)) 96 97 /* 98 * Policy -- Can real uid ruid with ucred uc send a signal to process q? 99 */ 100 #define CANSIGIO(ruid, uc, q) \ 101 ((uc)->cr_uid == 0 || \ 102 (ruid) == (q)->p_cred->p_ruid || \ 103 (uc)->cr_uid == (q)->p_cred->p_ruid || \ 104 (ruid) == (q)->p_ucred->cr_uid || \ 105 (uc)->cr_uid == (q)->p_ucred->cr_uid) 106 107 int sugid_coredump; 108 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 109 &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 110 111 /* 112 * Signal properties and actions. 113 * The array below categorizes the signals and their default actions 114 * according to the following properties: 115 */ 116 #define SA_KILL 0x01 /* terminates process by default */ 117 #define SA_CORE 0x02 /* ditto and coredumps */ 118 #define SA_STOP 0x04 /* suspend process */ 119 #define SA_TTYSTOP 0x08 /* ditto, from tty */ 120 #define SA_IGNORE 0x10 /* ignore by default */ 121 #define SA_CONT 0x20 /* continue if suspended */ 122 #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 123 124 static int sigproptbl[NSIG] = { 125 SA_KILL, /* SIGHUP */ 126 SA_KILL, /* SIGINT */ 127 SA_KILL|SA_CORE, /* SIGQUIT */ 128 SA_KILL|SA_CORE, /* SIGILL */ 129 SA_KILL|SA_CORE, /* SIGTRAP */ 130 SA_KILL|SA_CORE, /* SIGABRT */ 131 SA_KILL|SA_CORE, /* SIGEMT */ 132 SA_KILL|SA_CORE, /* SIGFPE */ 133 SA_KILL, /* SIGKILL */ 134 SA_KILL|SA_CORE, /* SIGBUS */ 135 SA_KILL|SA_CORE, /* SIGSEGV */ 136 SA_KILL|SA_CORE, /* SIGSYS */ 137 SA_KILL, /* SIGPIPE */ 138 SA_KILL, /* SIGALRM */ 139 SA_KILL, /* SIGTERM */ 140 SA_IGNORE, /* SIGURG */ 141 SA_STOP, /* SIGSTOP */ 142 SA_STOP|SA_TTYSTOP, /* SIGTSTP */ 143 SA_IGNORE|SA_CONT, /* SIGCONT */ 144 SA_IGNORE, /* SIGCHLD */ 145 SA_STOP|SA_TTYSTOP, /* SIGTTIN */ 146 SA_STOP|SA_TTYSTOP, /* SIGTTOU */ 147 SA_IGNORE, /* SIGIO */ 148 SA_KILL, /* SIGXCPU */ 149 SA_KILL, /* SIGXFSZ */ 150 SA_KILL, /* SIGVTALRM */ 151 SA_KILL, /* SIGPROF */ 152 SA_IGNORE, /* SIGWINCH */ 153 SA_IGNORE, /* SIGINFO */ 154 SA_KILL, /* SIGUSR1 */ 155 SA_KILL, /* SIGUSR2 */ 156 }; 157 158 static __inline int 159 sigprop(int sig) 160 { 161 162 if (sig > 0 && sig < NSIG) 163 return (sigproptbl[_SIG_IDX(sig)]); 164 return (0); 165 } 166 167 static __inline int 168 sig_ffs(sigset_t *set) 169 { 170 int i; 171 172 for (i = 0; i < _SIG_WORDS; i++) 173 if (set->__bits[i]) 174 return (ffs(set->__bits[i]) + (i * 32)); 175 return (0); 176 } 177 178 /* 179 * do_sigaction 180 * sigaction 181 * osigaction 182 */ 183 static int 184 do_sigaction(p, sig, act, oact, old) 185 struct proc *p; 186 register int sig; 187 struct sigaction *act, *oact; 188 int old; 189 { 190 register struct sigacts *ps = p->p_sigacts; 191 192 if (sig <= 0 || sig > _SIG_MAXSIG) 193 return (EINVAL); 194 195 if (oact) { 196 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 197 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 198 oact->sa_flags = 0; 199 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 200 oact->sa_flags |= SA_ONSTACK; 201 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 202 oact->sa_flags |= SA_RESTART; 203 if (SIGISMEMBER(ps->ps_sigreset, sig)) 204 oact->sa_flags |= SA_RESETHAND; 205 if (SIGISMEMBER(ps->ps_signodefer, sig)) 206 oact->sa_flags |= SA_NODEFER; 207 if (SIGISMEMBER(ps->ps_siginfo, sig)) 208 oact->sa_flags |= SA_SIGINFO; 209 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP) 210 oact->sa_flags |= SA_NOCLDSTOP; 211 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT) 212 oact->sa_flags |= SA_NOCLDWAIT; 213 } 214 if (act) { 215 if ((sig == SIGKILL || sig == SIGSTOP) && 216 act->sa_handler != SIG_DFL) 217 return (EINVAL); 218 219 /* 220 * Change setting atomically. 221 */ 222 (void) splhigh(); 223 224 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 225 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 226 if (act->sa_flags & SA_SIGINFO) { 227 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 228 SIGADDSET(ps->ps_siginfo, sig); 229 } else { 230 ps->ps_sigact[_SIG_IDX(sig)] = 231 (__sighandler_t *)act->sa_sigaction; 232 SIGDELSET(ps->ps_siginfo, sig); 233 } 234 if (!(act->sa_flags & SA_RESTART)) 235 SIGADDSET(ps->ps_sigintr, sig); 236 else 237 SIGDELSET(ps->ps_sigintr, sig); 238 if (act->sa_flags & SA_ONSTACK) 239 SIGADDSET(ps->ps_sigonstack, sig); 240 else 241 SIGDELSET(ps->ps_sigonstack, sig); 242 if (act->sa_flags & SA_RESETHAND) 243 SIGADDSET(ps->ps_sigreset, sig); 244 else 245 SIGDELSET(ps->ps_sigreset, sig); 246 if (act->sa_flags & SA_NODEFER) 247 SIGADDSET(ps->ps_signodefer, sig); 248 else 249 SIGDELSET(ps->ps_signodefer, sig); 250 #ifdef COMPAT_SUNOS 251 if (act->sa_flags & SA_USERTRAMP) 252 SIGADDSET(ps->ps_usertramp, sig); 253 else 254 SIGDELSET(ps->ps_usertramp, seg); 255 #endif 256 if (sig == SIGCHLD) { 257 if (act->sa_flags & SA_NOCLDSTOP) 258 p->p_procsig->ps_flag |= PS_NOCLDSTOP; 259 else 260 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP; 261 if (act->sa_flags & SA_NOCLDWAIT) { 262 /* 263 * Paranoia: since SA_NOCLDWAIT is implemented 264 * by reparenting the dying child to PID 1 (and 265 * trust it to reap the zombie), PID 1 itself 266 * is forbidden to set SA_NOCLDWAIT. 267 */ 268 if (p->p_pid == 1) 269 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 270 else 271 p->p_procsig->ps_flag |= PS_NOCLDWAIT; 272 } else 273 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 274 } 275 /* 276 * Set bit in p_sigignore for signals that are set to SIG_IGN, 277 * and for signals set to SIG_DFL where the default is to 278 * ignore. However, don't put SIGCONT in p_sigignore, as we 279 * have to restart the process. 280 */ 281 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 282 (sigprop(sig) & SA_IGNORE && 283 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 284 /* never to be seen again */ 285 SIGDELSET(p->p_siglist, sig); 286 if (sig != SIGCONT) 287 /* easier in psignal */ 288 SIGADDSET(p->p_sigignore, sig); 289 SIGDELSET(p->p_sigcatch, sig); 290 } else { 291 SIGDELSET(p->p_sigignore, sig); 292 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 293 SIGDELSET(p->p_sigcatch, sig); 294 else 295 SIGADDSET(p->p_sigcatch, sig); 296 } 297 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 298 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old) 299 SIGDELSET(ps->ps_osigset, sig); 300 else 301 SIGADDSET(ps->ps_osigset, sig); 302 303 (void) spl0(); 304 } 305 return (0); 306 } 307 308 #ifndef _SYS_SYSPROTO_H_ 309 struct sigaction_args { 310 int sig; 311 struct sigaction *act; 312 struct sigaction *oact; 313 }; 314 #endif 315 /* ARGSUSED */ 316 int 317 sigaction(p, uap) 318 struct proc *p; 319 register struct sigaction_args *uap; 320 { 321 struct sigaction act, oact; 322 register struct sigaction *actp, *oactp; 323 int error; 324 325 actp = (uap->act != NULL) ? &act : NULL; 326 oactp = (uap->oact != NULL) ? &oact : NULL; 327 if (actp) { 328 error = copyin(uap->act, actp, sizeof(act)); 329 if (error) 330 return (error); 331 } 332 error = do_sigaction(p, uap->sig, actp, oactp, 0); 333 if (oactp && !error) { 334 error = copyout(oactp, uap->oact, sizeof(oact)); 335 } 336 return (error); 337 } 338 339 #ifndef _SYS_SYSPROTO_H_ 340 struct osigaction_args { 341 int signum; 342 struct osigaction *nsa; 343 struct osigaction *osa; 344 }; 345 #endif 346 /* ARGSUSED */ 347 int 348 osigaction(p, uap) 349 struct proc *p; 350 register struct osigaction_args *uap; 351 { 352 struct osigaction sa; 353 struct sigaction nsa, osa; 354 register struct sigaction *nsap, *osap; 355 int error; 356 357 if (uap->signum <= 0 || uap->signum >= ONSIG) 358 return (EINVAL); 359 nsap = (uap->nsa != NULL) ? &nsa : NULL; 360 osap = (uap->osa != NULL) ? &osa : NULL; 361 if (nsap) { 362 error = copyin(uap->nsa, &sa, sizeof(sa)); 363 if (error) 364 return (error); 365 nsap->sa_handler = sa.sa_handler; 366 nsap->sa_flags = sa.sa_flags; 367 OSIG2SIG(sa.sa_mask, nsap->sa_mask); 368 } 369 error = do_sigaction(p, uap->signum, nsap, osap, 1); 370 if (osap && !error) { 371 sa.sa_handler = osap->sa_handler; 372 sa.sa_flags = osap->sa_flags; 373 SIG2OSIG(osap->sa_mask, sa.sa_mask); 374 error = copyout(&sa, uap->osa, sizeof(sa)); 375 } 376 return (error); 377 } 378 379 /* 380 * Initialize signal state for process 0; 381 * set to ignore signals that are ignored by default. 382 */ 383 void 384 siginit(p) 385 struct proc *p; 386 { 387 register int i; 388 389 for (i = 1; i <= NSIG; i++) 390 if (sigprop(i) & SA_IGNORE && i != SIGCONT) 391 SIGADDSET(p->p_sigignore, i); 392 } 393 394 /* 395 * Reset signals for an exec of the specified process. 396 */ 397 void 398 execsigs(p) 399 register struct proc *p; 400 { 401 register struct sigacts *ps = p->p_sigacts; 402 register int sig; 403 404 /* 405 * Reset caught signals. Held signals remain held 406 * through p_sigmask (unless they were caught, 407 * and are now ignored by default). 408 */ 409 while (SIGNOTEMPTY(p->p_sigcatch)) { 410 sig = sig_ffs(&p->p_sigcatch); 411 SIGDELSET(p->p_sigcatch, sig); 412 if (sigprop(sig) & SA_IGNORE) { 413 if (sig != SIGCONT) 414 SIGADDSET(p->p_sigignore, sig); 415 SIGDELSET(p->p_siglist, sig); 416 } 417 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 418 } 419 /* 420 * Reset stack state to the user stack. 421 * Clear set of signals caught on the signal stack. 422 */ 423 p->p_sigstk.ss_flags = SS_DISABLE; 424 p->p_sigstk.ss_size = 0; 425 p->p_sigstk.ss_sp = 0; 426 /* 427 * Reset no zombies if child dies flag as Solaris does. 428 */ 429 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT; 430 } 431 432 /* 433 * Manipulate signal mask. 434 * Note that we receive new mask, not pointer, 435 * and return old mask as return value; 436 * the library stub does the rest. 437 */ 438 static int 439 do_sigprocmask(p, how, set, oset, old) 440 struct proc *p; 441 int how; 442 sigset_t *set, *oset; 443 int old; 444 { 445 int error; 446 447 if (oset != NULL) 448 *oset = p->p_sigmask; 449 450 error = 0; 451 if (set != NULL) { 452 (void) splhigh(); 453 switch (how) { 454 case SIG_BLOCK: 455 SIG_CANTMASK(*set); 456 SIGSETOR(p->p_sigmask, *set); 457 break; 458 case SIG_UNBLOCK: 459 SIGSETNAND(p->p_sigmask, *set); 460 break; 461 case SIG_SETMASK: 462 SIG_CANTMASK(*set); 463 if (old) 464 SIGSETLO(p->p_sigmask, *set); 465 else 466 p->p_sigmask = *set; 467 break; 468 default: 469 error = EINVAL; 470 break; 471 } 472 (void) spl0(); 473 } 474 return (error); 475 } 476 477 #ifndef _SYS_SYSPROTO_H_ 478 struct sigprocmask_args { 479 int how; 480 const sigset_t *set; 481 sigset_t *oset; 482 }; 483 #endif 484 int 485 sigprocmask(p, uap) 486 register struct proc *p; 487 struct sigprocmask_args *uap; 488 { 489 sigset_t set, oset; 490 sigset_t *setp, *osetp; 491 int error; 492 493 setp = (uap->set != NULL) ? &set : NULL; 494 osetp = (uap->oset != NULL) ? &oset : NULL; 495 if (setp) { 496 error = copyin(uap->set, setp, sizeof(set)); 497 if (error) 498 return (error); 499 } 500 error = do_sigprocmask(p, uap->how, setp, osetp, 0); 501 if (osetp && !error) { 502 error = copyout(osetp, uap->oset, sizeof(oset)); 503 } 504 return (error); 505 } 506 507 #ifndef _SYS_SYSPROTO_H_ 508 struct osigprocmask_args { 509 int how; 510 osigset_t mask; 511 }; 512 #endif 513 int 514 osigprocmask(p, uap) 515 register struct proc *p; 516 struct osigprocmask_args *uap; 517 { 518 sigset_t set, oset; 519 int error; 520 521 OSIG2SIG(uap->mask, set); 522 error = do_sigprocmask(p, uap->how, &set, &oset, 1); 523 SIG2OSIG(oset, p->p_retval[0]); 524 return (error); 525 } 526 527 #ifndef _SYS_SYSPROTO_H_ 528 struct sigpending_args { 529 sigset_t *set; 530 }; 531 #endif 532 /* ARGSUSED */ 533 int 534 sigpending(p, uap) 535 struct proc *p; 536 struct sigpending_args *uap; 537 { 538 539 return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t))); 540 } 541 542 #ifndef _SYS_SYSPROTO_H_ 543 struct osigpending_args { 544 int dummy; 545 }; 546 #endif 547 /* ARGSUSED */ 548 int 549 osigpending(p, uap) 550 struct proc *p; 551 struct osigpending_args *uap; 552 { 553 554 SIG2OSIG(p->p_siglist, p->p_retval[0]); 555 return (0); 556 } 557 558 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 559 /* 560 * Generalized interface signal handler, 4.3-compatible. 561 */ 562 #ifndef _SYS_SYSPROTO_H_ 563 struct osigvec_args { 564 int signum; 565 struct sigvec *nsv; 566 struct sigvec *osv; 567 }; 568 #endif 569 /* ARGSUSED */ 570 int 571 osigvec(p, uap) 572 struct proc *p; 573 register struct osigvec_args *uap; 574 { 575 struct sigvec vec; 576 struct sigaction nsa, osa; 577 register struct sigaction *nsap, *osap; 578 int error; 579 580 if (uap->signum <= 0 || uap->signum >= ONSIG) 581 return (EINVAL); 582 nsap = (uap->nsv != NULL) ? &nsa : NULL; 583 osap = (uap->osv != NULL) ? &osa : NULL; 584 if (nsap) { 585 error = copyin(uap->nsv, &vec, sizeof(vec)); 586 if (error) 587 return (error); 588 nsap->sa_handler = vec.sv_handler; 589 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 590 nsap->sa_flags = vec.sv_flags; 591 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 592 #ifdef COMPAT_SUNOS 593 nsap->sa_flags |= SA_USERTRAMP; 594 #endif 595 } 596 error = do_sigaction(p, uap->signum, nsap, osap, 1); 597 if (osap && !error) { 598 vec.sv_handler = osap->sa_handler; 599 SIG2OSIG(osap->sa_mask, vec.sv_mask); 600 vec.sv_flags = osap->sa_flags; 601 vec.sv_flags &= ~SA_NOCLDWAIT; 602 vec.sv_flags ^= SA_RESTART; 603 #ifdef COMPAT_SUNOS 604 vec.sv_flags &= ~SA_NOCLDSTOP; 605 #endif 606 error = copyout(&vec, uap->osv, sizeof(vec)); 607 } 608 return (error); 609 } 610 611 #ifndef _SYS_SYSPROTO_H_ 612 struct osigblock_args { 613 int mask; 614 }; 615 #endif 616 int 617 osigblock(p, uap) 618 register struct proc *p; 619 struct osigblock_args *uap; 620 { 621 sigset_t set; 622 623 OSIG2SIG(uap->mask, set); 624 SIG_CANTMASK(set); 625 (void) splhigh(); 626 SIG2OSIG(p->p_sigmask, p->p_retval[0]); 627 SIGSETOR(p->p_sigmask, set); 628 (void) spl0(); 629 return (0); 630 } 631 632 #ifndef _SYS_SYSPROTO_H_ 633 struct osigsetmask_args { 634 int mask; 635 }; 636 #endif 637 int 638 osigsetmask(p, uap) 639 struct proc *p; 640 struct osigsetmask_args *uap; 641 { 642 sigset_t set; 643 644 OSIG2SIG(uap->mask, set); 645 SIG_CANTMASK(set); 646 (void) splhigh(); 647 SIG2OSIG(p->p_sigmask, p->p_retval[0]); 648 SIGSETLO(p->p_sigmask, set); 649 (void) spl0(); 650 return (0); 651 } 652 #endif /* COMPAT_43 || COMPAT_SUNOS */ 653 654 /* 655 * Suspend process until signal, providing mask to be set 656 * in the meantime. Note nonstandard calling convention: 657 * libc stub passes mask, not pointer, to save a copyin. 658 */ 659 #ifndef _SYS_SYSPROTO_H_ 660 struct sigsuspend_args { 661 const sigset_t *sigmask; 662 }; 663 #endif 664 /* ARGSUSED */ 665 int 666 sigsuspend(p, uap) 667 register struct proc *p; 668 struct sigsuspend_args *uap; 669 { 670 sigset_t mask; 671 register struct sigacts *ps = p->p_sigacts; 672 int error; 673 674 error = copyin(uap->sigmask, &mask, sizeof(mask)); 675 if (error) 676 return (error); 677 678 /* 679 * When returning from sigsuspend, we want 680 * the old mask to be restored after the 681 * signal handler has finished. Thus, we 682 * save it here and mark the sigacts structure 683 * to indicate this. 684 */ 685 p->p_oldsigmask = p->p_sigmask; 686 p->p_flag |= P_OLDMASK; 687 688 SIG_CANTMASK(mask); 689 p->p_sigmask = mask; 690 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0) 691 /* void */; 692 /* always return EINTR rather than ERESTART... */ 693 return (EINTR); 694 } 695 696 #ifndef _SYS_SYSPROTO_H_ 697 struct osigsuspend_args { 698 osigset_t mask; 699 }; 700 #endif 701 /* ARGSUSED */ 702 int 703 osigsuspend(p, uap) 704 register struct proc *p; 705 struct osigsuspend_args *uap; 706 { 707 sigset_t mask; 708 register struct sigacts *ps = p->p_sigacts; 709 710 p->p_oldsigmask = p->p_sigmask; 711 p->p_flag |= P_OLDMASK; 712 OSIG2SIG(uap->mask, mask); 713 SIG_CANTMASK(mask); 714 SIGSETLO(p->p_sigmask, mask); 715 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0) 716 /* void */; 717 /* always return EINTR rather than ERESTART... */ 718 return (EINTR); 719 } 720 721 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 722 #ifndef _SYS_SYSPROTO_H_ 723 struct osigstack_args { 724 struct sigstack *nss; 725 struct sigstack *oss; 726 }; 727 #endif 728 /* ARGSUSED */ 729 int 730 osigstack(p, uap) 731 struct proc *p; 732 register struct osigstack_args *uap; 733 { 734 struct sigstack ss; 735 int error = 0; 736 737 ss.ss_sp = p->p_sigstk.ss_sp; 738 ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK; 739 if (uap->oss && (error = copyout(&ss, uap->oss, 740 sizeof(struct sigstack)))) 741 return (error); 742 if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) { 743 p->p_sigstk.ss_sp = ss.ss_sp; 744 p->p_sigstk.ss_size = 0; 745 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK; 746 p->p_flag |= P_ALTSTACK; 747 } 748 return (error); 749 } 750 #endif /* COMPAT_43 || COMPAT_SUNOS */ 751 752 #ifndef _SYS_SYSPROTO_H_ 753 struct sigaltstack_args { 754 stack_t *ss; 755 stack_t *oss; 756 }; 757 #endif 758 /* ARGSUSED */ 759 int 760 sigaltstack(p, uap) 761 struct proc *p; 762 register struct sigaltstack_args *uap; 763 { 764 stack_t ss; 765 int error; 766 767 if ((p->p_flag & P_ALTSTACK) == 0) 768 p->p_sigstk.ss_flags |= SS_DISABLE; 769 if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss, 770 sizeof(stack_t)))) 771 return (error); 772 if (uap->ss == 0) 773 return (0); 774 if ((error = copyin(uap->ss, &ss, sizeof(ss)))) 775 return (error); 776 if (ss.ss_flags & SS_DISABLE) { 777 if (p->p_sigstk.ss_flags & SS_ONSTACK) 778 return (EINVAL); 779 p->p_flag &= ~P_ALTSTACK; 780 p->p_sigstk.ss_flags = ss.ss_flags; 781 return (0); 782 } 783 if (ss.ss_size < MINSIGSTKSZ) 784 return (ENOMEM); 785 p->p_flag |= P_ALTSTACK; 786 p->p_sigstk = ss; 787 return (0); 788 } 789 790 /* 791 * Common code for kill process group/broadcast kill. 792 * cp is calling process. 793 */ 794 int 795 killpg1(cp, sig, pgid, all) 796 register struct proc *cp; 797 int sig, pgid, all; 798 { 799 register struct proc *p; 800 struct pgrp *pgrp; 801 int nfound = 0; 802 803 if (all) 804 /* 805 * broadcast 806 */ 807 LIST_FOREACH(p, &allproc, p_list) { 808 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 809 p == cp || !CANSIGNAL(cp, p, sig)) 810 continue; 811 nfound++; 812 if (sig) 813 psignal(p, sig); 814 } 815 else { 816 if (pgid == 0) 817 /* 818 * zero pgid means send to my process group. 819 */ 820 pgrp = cp->p_pgrp; 821 else { 822 pgrp = pgfind(pgid); 823 if (pgrp == NULL) 824 return (ESRCH); 825 } 826 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 827 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 828 p->p_stat == SZOMB || 829 !CANSIGNAL(cp, p, sig)) 830 continue; 831 nfound++; 832 if (sig) 833 psignal(p, sig); 834 } 835 } 836 return (nfound ? 0 : ESRCH); 837 } 838 839 #ifndef _SYS_SYSPROTO_H_ 840 struct kill_args { 841 int pid; 842 int signum; 843 }; 844 #endif 845 /* ARGSUSED */ 846 int 847 kill(cp, uap) 848 register struct proc *cp; 849 register struct kill_args *uap; 850 { 851 register struct proc *p; 852 853 if ((u_int)uap->signum > _SIG_MAXSIG) 854 return (EINVAL); 855 if (uap->pid > 0) { 856 /* kill single process */ 857 if ((p = pfind(uap->pid)) == NULL) 858 return (ESRCH); 859 if (!CANSIGNAL(cp, p, uap->signum)) 860 return (EPERM); 861 if (uap->signum) 862 psignal(p, uap->signum); 863 return (0); 864 } 865 switch (uap->pid) { 866 case -1: /* broadcast signal */ 867 return (killpg1(cp, uap->signum, 0, 1)); 868 case 0: /* signal own process group */ 869 return (killpg1(cp, uap->signum, 0, 0)); 870 default: /* negative explicit process group */ 871 return (killpg1(cp, uap->signum, -uap->pid, 0)); 872 } 873 /* NOTREACHED */ 874 } 875 876 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 877 #ifndef _SYS_SYSPROTO_H_ 878 struct okillpg_args { 879 int pgid; 880 int signum; 881 }; 882 #endif 883 /* ARGSUSED */ 884 int 885 okillpg(p, uap) 886 struct proc *p; 887 register struct okillpg_args *uap; 888 { 889 890 if ((u_int)uap->signum > _SIG_MAXSIG) 891 return (EINVAL); 892 return (killpg1(p, uap->signum, uap->pgid, 0)); 893 } 894 #endif /* COMPAT_43 || COMPAT_SUNOS */ 895 896 /* 897 * Send a signal to a process group. 898 */ 899 void 900 gsignal(pgid, sig) 901 int pgid, sig; 902 { 903 struct pgrp *pgrp; 904 905 if (pgid && (pgrp = pgfind(pgid))) 906 pgsignal(pgrp, sig, 0); 907 } 908 909 /* 910 * Send a signal to a process group. If checktty is 1, 911 * limit to members which have a controlling terminal. 912 */ 913 void 914 pgsignal(pgrp, sig, checkctty) 915 struct pgrp *pgrp; 916 int sig, checkctty; 917 { 918 register struct proc *p; 919 920 if (pgrp) 921 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) 922 if (checkctty == 0 || p->p_flag & P_CONTROLT) 923 psignal(p, sig); 924 } 925 926 /* 927 * Send a signal caused by a trap to the current process. 928 * If it will be caught immediately, deliver it with correct code. 929 * Otherwise, post it normally. 930 */ 931 void 932 trapsignal(p, sig, code) 933 struct proc *p; 934 register int sig; 935 u_long code; 936 { 937 register struct sigacts *ps = p->p_sigacts; 938 939 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) && 940 SIGISMEMBER(p->p_sigmask, sig)) { 941 p->p_stats->p_ru.ru_nsignals++; 942 #ifdef KTRACE 943 if (KTRPOINT(p, KTR_PSIG)) 944 ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)], 945 &p->p_sigmask, code); 946 #endif 947 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig, 948 &p->p_sigmask, code); 949 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 950 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 951 SIGADDSET(p->p_sigmask, sig); 952 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 953 /* 954 * See do_sigaction() for origin of this code. 955 */ 956 SIGDELSET(p->p_sigcatch, sig); 957 if (sig != SIGCONT && 958 sigprop(sig) & SA_IGNORE) 959 SIGADDSET(p->p_sigignore, sig); 960 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 961 } 962 } else { 963 p->p_code = code; /* XXX for core dump/debugger */ 964 p->p_sig = sig; /* XXX to verify code */ 965 psignal(p, sig); 966 } 967 } 968 969 /* 970 * Send the signal to the process. If the signal has an action, the action 971 * is usually performed by the target process rather than the caller; we add 972 * the signal to the set of pending signals for the process. 973 * 974 * Exceptions: 975 * o When a stop signal is sent to a sleeping process that takes the 976 * default action, the process is stopped without awakening it. 977 * o SIGCONT restarts stopped processes (or puts them back to sleep) 978 * regardless of the signal action (eg, blocked or ignored). 979 * 980 * Other ignored signals are discarded immediately. 981 */ 982 void 983 psignal(p, sig) 984 register struct proc *p; 985 register int sig; 986 { 987 register int s, prop; 988 register sig_t action; 989 990 if (sig > _SIG_MAXSIG || sig <= 0) { 991 printf("psignal: signal %d\n", sig); 992 panic("psignal signal number"); 993 } 994 995 prop = sigprop(sig); 996 997 /* 998 * If proc is traced, always give parent a chance; 999 * if signal event is tracked by procfs, give *that* 1000 * a chance, as well. 1001 */ 1002 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) 1003 action = SIG_DFL; 1004 else { 1005 /* 1006 * If the signal is being ignored, 1007 * then we forget about it immediately. 1008 * (Note: we don't set SIGCONT in p_sigignore, 1009 * and if it is set to SIG_IGN, 1010 * action will be SIG_DFL here.) 1011 */ 1012 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT)) 1013 return; 1014 if (SIGISMEMBER(p->p_sigmask, sig)) 1015 action = SIG_HOLD; 1016 else if (SIGISMEMBER(p->p_sigcatch, sig)) 1017 action = SIG_CATCH; 1018 else 1019 action = SIG_DFL; 1020 } 1021 1022 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) && 1023 (p->p_flag & P_TRACED) == 0) 1024 p->p_nice = NZERO; 1025 1026 if (prop & SA_CONT) 1027 SIG_STOPSIGMASK(p->p_siglist); 1028 1029 if (prop & SA_STOP) { 1030 /* 1031 * If sending a tty stop signal to a member of an orphaned 1032 * process group, discard the signal here if the action 1033 * is default; don't stop the process below if sleeping, 1034 * and don't clear any pending SIGCONT. 1035 */ 1036 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 && 1037 action == SIG_DFL) 1038 return; 1039 SIG_CONTSIGMASK(p->p_siglist); 1040 } 1041 SIGADDSET(p->p_siglist, sig); 1042 1043 /* 1044 * Defer further processing for signals which are held, 1045 * except that stopped processes must be continued by SIGCONT. 1046 */ 1047 if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) 1048 return; 1049 s = splhigh(); 1050 switch (p->p_stat) { 1051 1052 case SSLEEP: 1053 /* 1054 * If process is sleeping uninterruptibly 1055 * we can't interrupt the sleep... the signal will 1056 * be noticed when the process returns through 1057 * trap() or syscall(). 1058 */ 1059 if ((p->p_flag & P_SINTR) == 0) 1060 goto out; 1061 /* 1062 * Process is sleeping and traced... make it runnable 1063 * so it can discover the signal in issignal() and stop 1064 * for the parent. 1065 */ 1066 if (p->p_flag & P_TRACED) 1067 goto run; 1068 /* 1069 * If SIGCONT is default (or ignored) and process is 1070 * asleep, we are finished; the process should not 1071 * be awakened. 1072 */ 1073 if ((prop & SA_CONT) && action == SIG_DFL) { 1074 SIGDELSET(p->p_siglist, sig); 1075 goto out; 1076 } 1077 /* 1078 * When a sleeping process receives a stop 1079 * signal, process immediately if possible. 1080 * All other (caught or default) signals 1081 * cause the process to run. 1082 */ 1083 if (prop & SA_STOP) { 1084 if (action != SIG_DFL) 1085 goto runfast; 1086 /* 1087 * If a child holding parent blocked, 1088 * stopping could cause deadlock. 1089 */ 1090 if (p->p_flag & P_PPWAIT) 1091 goto out; 1092 SIGDELSET(p->p_siglist, sig); 1093 p->p_xstat = sig; 1094 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1095 psignal(p->p_pptr, SIGCHLD); 1096 stop(p); 1097 goto out; 1098 } else 1099 goto runfast; 1100 /*NOTREACHED*/ 1101 1102 case SSTOP: 1103 /* 1104 * If traced process is already stopped, 1105 * then no further action is necessary. 1106 */ 1107 if (p->p_flag & P_TRACED) 1108 goto out; 1109 1110 /* 1111 * Kill signal always sets processes running. 1112 */ 1113 if (sig == SIGKILL) 1114 goto runfast; 1115 1116 if (prop & SA_CONT) { 1117 /* 1118 * If SIGCONT is default (or ignored), we continue the 1119 * process but don't leave the signal in p_siglist, as 1120 * it has no further action. If SIGCONT is held, we 1121 * continue the process and leave the signal in 1122 * p_siglist. If the process catches SIGCONT, let it 1123 * handle the signal itself. If it isn't waiting on 1124 * an event, then it goes back to run state. 1125 * Otherwise, process goes back to sleep state. 1126 */ 1127 if (action == SIG_DFL) 1128 SIGDELSET(p->p_siglist, sig); 1129 if (action == SIG_CATCH) 1130 goto runfast; 1131 if (p->p_wchan == 0) 1132 goto run; 1133 p->p_stat = SSLEEP; 1134 goto out; 1135 } 1136 1137 if (prop & SA_STOP) { 1138 /* 1139 * Already stopped, don't need to stop again. 1140 * (If we did the shell could get confused.) 1141 */ 1142 SIGDELSET(p->p_siglist, sig); 1143 goto out; 1144 } 1145 1146 /* 1147 * If process is sleeping interruptibly, then simulate a 1148 * wakeup so that when it is continued, it will be made 1149 * runnable and can look at the signal. But don't make 1150 * the process runnable, leave it stopped. 1151 */ 1152 if (p->p_wchan && p->p_flag & P_SINTR) 1153 unsleep(p); 1154 goto out; 1155 1156 default: 1157 /* 1158 * SRUN, SIDL, SZOMB do nothing with the signal, 1159 * other than kicking ourselves if we are running. 1160 * It will either never be noticed, or noticed very soon. 1161 */ 1162 if (p == curproc) 1163 signotify(p); 1164 #ifdef SMP 1165 else if (p->p_stat == SRUN) 1166 forward_signal(p); 1167 #endif 1168 goto out; 1169 } 1170 /*NOTREACHED*/ 1171 1172 runfast: 1173 /* 1174 * Raise priority to at least PUSER. 1175 */ 1176 if (p->p_priority > PUSER) 1177 p->p_priority = PUSER; 1178 run: 1179 setrunnable(p); 1180 out: 1181 splx(s); 1182 } 1183 1184 /* 1185 * If the current process has received a signal (should be caught or cause 1186 * termination, should interrupt current syscall), return the signal number. 1187 * Stop signals with default action are processed immediately, then cleared; 1188 * they aren't returned. This is checked after each entry to the system for 1189 * a syscall or trap (though this can usually be done without calling issignal 1190 * by checking the pending signal masks in the CURSIG macro.) The normal call 1191 * sequence is 1192 * 1193 * while (sig = CURSIG(curproc)) 1194 * postsig(sig); 1195 */ 1196 int 1197 issignal(p) 1198 register struct proc *p; 1199 { 1200 sigset_t mask; 1201 register int sig, prop; 1202 1203 for (;;) { 1204 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1205 1206 mask = p->p_siglist; 1207 SIGSETNAND(mask, p->p_sigmask); 1208 if (p->p_flag & P_PPWAIT) 1209 SIG_STOPSIGMASK(mask); 1210 if (!SIGNOTEMPTY(mask)) /* no signal to send */ 1211 return (0); 1212 sig = sig_ffs(&mask); 1213 prop = sigprop(sig); 1214 1215 STOPEVENT(p, S_SIG, sig); 1216 1217 /* 1218 * We should see pending but ignored signals 1219 * only if P_TRACED was on when they were posted. 1220 */ 1221 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1222 SIGDELSET(p->p_siglist, sig); 1223 continue; 1224 } 1225 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1226 /* 1227 * If traced, always stop, and stay 1228 * stopped until released by the parent. 1229 */ 1230 p->p_xstat = sig; 1231 psignal(p->p_pptr, SIGCHLD); 1232 do { 1233 stop(p); 1234 mi_switch(); 1235 } while (!trace_req(p) 1236 && p->p_flag & P_TRACED); 1237 1238 /* 1239 * If the traced bit got turned off, go back up 1240 * to the top to rescan signals. This ensures 1241 * that p_sig* and ps_sigact are consistent. 1242 */ 1243 if ((p->p_flag & P_TRACED) == 0) 1244 continue; 1245 1246 /* 1247 * If parent wants us to take the signal, 1248 * then it will leave it in p->p_xstat; 1249 * otherwise we just look for signals again. 1250 */ 1251 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1252 sig = p->p_xstat; 1253 if (sig == 0) 1254 continue; 1255 1256 /* 1257 * Put the new signal into p_siglist. If the 1258 * signal is being masked, look for other signals. 1259 */ 1260 SIGADDSET(p->p_siglist, sig); 1261 if (SIGISMEMBER(p->p_sigmask, sig)) 1262 continue; 1263 } 1264 1265 /* 1266 * Decide whether the signal should be returned. 1267 * Return the signal's number, or fall through 1268 * to clear it from the pending mask. 1269 */ 1270 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1271 1272 case (int)SIG_DFL: 1273 /* 1274 * Don't take default actions on system processes. 1275 */ 1276 if (p->p_pid <= 1) { 1277 #ifdef DIAGNOSTIC 1278 /* 1279 * Are you sure you want to ignore SIGSEGV 1280 * in init? XXX 1281 */ 1282 printf("Process (pid %lu) got signal %d\n", 1283 (u_long)p->p_pid, sig); 1284 #endif 1285 break; /* == ignore */ 1286 } 1287 /* 1288 * If there is a pending stop signal to process 1289 * with default action, stop here, 1290 * then clear the signal. However, 1291 * if process is member of an orphaned 1292 * process group, ignore tty stop signals. 1293 */ 1294 if (prop & SA_STOP) { 1295 if (p->p_flag & P_TRACED || 1296 (p->p_pgrp->pg_jobc == 0 && 1297 prop & SA_TTYSTOP)) 1298 break; /* == ignore */ 1299 p->p_xstat = sig; 1300 stop(p); 1301 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0) 1302 psignal(p->p_pptr, SIGCHLD); 1303 mi_switch(); 1304 break; 1305 } else if (prop & SA_IGNORE) { 1306 /* 1307 * Except for SIGCONT, shouldn't get here. 1308 * Default action is to ignore; drop it. 1309 */ 1310 break; /* == ignore */ 1311 } else 1312 return (sig); 1313 /*NOTREACHED*/ 1314 1315 case (int)SIG_IGN: 1316 /* 1317 * Masking above should prevent us ever trying 1318 * to take action on an ignored signal other 1319 * than SIGCONT, unless process is traced. 1320 */ 1321 if ((prop & SA_CONT) == 0 && 1322 (p->p_flag & P_TRACED) == 0) 1323 printf("issignal\n"); 1324 break; /* == ignore */ 1325 1326 default: 1327 /* 1328 * This signal has an action, let 1329 * postsig() process it. 1330 */ 1331 return (sig); 1332 } 1333 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1334 } 1335 /* NOTREACHED */ 1336 } 1337 1338 /* 1339 * Put the argument process into the stopped state and notify the parent 1340 * via wakeup. Signals are handled elsewhere. The process must not be 1341 * on the run queue. 1342 */ 1343 void 1344 stop(p) 1345 register struct proc *p; 1346 { 1347 1348 p->p_stat = SSTOP; 1349 p->p_flag &= ~P_WAITED; 1350 wakeup((caddr_t)p->p_pptr); 1351 } 1352 1353 /* 1354 * Take the action for the specified signal 1355 * from the current set of pending signals. 1356 */ 1357 void 1358 postsig(sig) 1359 register int sig; 1360 { 1361 register struct proc *p = curproc; 1362 struct sigacts *ps = p->p_sigacts; 1363 sig_t action; 1364 sigset_t returnmask; 1365 int code; 1366 1367 KASSERT(sig != 0, ("postsig")); 1368 1369 SIGDELSET(p->p_siglist, sig); 1370 action = ps->ps_sigact[_SIG_IDX(sig)]; 1371 #ifdef KTRACE 1372 if (KTRPOINT(p, KTR_PSIG)) 1373 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ? 1374 &p->p_oldsigmask : &p->p_sigmask, 0); 1375 #endif 1376 STOPEVENT(p, S_SIG, sig); 1377 1378 if (action == SIG_DFL) { 1379 /* 1380 * Default action, where the default is to kill 1381 * the process. (Other cases were ignored above.) 1382 */ 1383 sigexit(p, sig); 1384 /* NOTREACHED */ 1385 } else { 1386 /* 1387 * If we get here, the signal must be caught. 1388 */ 1389 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1390 ("postsig action")); 1391 /* 1392 * Set the new mask value and also defer further 1393 * occurrences of this signal. 1394 * 1395 * Special case: user has done a sigsuspend. Here the 1396 * current mask is not of interest, but rather the 1397 * mask from before the sigsuspend is what we want 1398 * restored after the signal processing is completed. 1399 */ 1400 (void) splhigh(); 1401 if (p->p_flag & P_OLDMASK) { 1402 returnmask = p->p_oldsigmask; 1403 p->p_flag &= ~P_OLDMASK; 1404 } else 1405 returnmask = p->p_sigmask; 1406 1407 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1408 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1409 SIGADDSET(p->p_sigmask, sig); 1410 1411 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1412 /* 1413 * See do_sigaction() for origin of this code. 1414 */ 1415 SIGDELSET(p->p_sigcatch, sig); 1416 if (sig != SIGCONT && 1417 sigprop(sig) & SA_IGNORE) 1418 SIGADDSET(p->p_sigignore, sig); 1419 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1420 } 1421 (void) spl0(); 1422 p->p_stats->p_ru.ru_nsignals++; 1423 if (p->p_sig != sig) { 1424 code = 0; 1425 } else { 1426 code = p->p_code; 1427 p->p_code = 0; 1428 p->p_sig = 0; 1429 } 1430 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1431 } 1432 } 1433 1434 /* 1435 * Kill the current process for stated reason. 1436 */ 1437 void 1438 killproc(p, why) 1439 struct proc *p; 1440 char *why; 1441 { 1442 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1443 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1444 psignal(p, SIGKILL); 1445 } 1446 1447 /* 1448 * Force the current process to exit with the specified signal, dumping core 1449 * if appropriate. We bypass the normal tests for masked and caught signals, 1450 * allowing unrecoverable failures to terminate the process without changing 1451 * signal state. Mark the accounting record with the signal termination. 1452 * If dumping core, save the signal number for the debugger. Calls exit and 1453 * does not return. 1454 */ 1455 void 1456 sigexit(p, sig) 1457 register struct proc *p; 1458 int sig; 1459 { 1460 1461 p->p_acflag |= AXSIG; 1462 if (sigprop(sig) & SA_CORE) { 1463 p->p_sig = sig; 1464 /* 1465 * Log signals which would cause core dumps 1466 * (Log as LOG_INFO to appease those who don't want 1467 * these messages.) 1468 * XXX : Todo, as well as euid, write out ruid too 1469 */ 1470 if (coredump(p) == 0) 1471 sig |= WCOREFLAG; 1472 if (kern_logsigexit) 1473 log(LOG_INFO, 1474 "pid %d (%s), uid %d: exited on signal %d%s\n", 1475 p->p_pid, p->p_comm, 1476 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, 1477 sig &~ WCOREFLAG, 1478 sig & WCOREFLAG ? " (core dumped)" : ""); 1479 } 1480 exit1(p, W_EXITCODE(0, sig)); 1481 /* NOTREACHED */ 1482 } 1483 1484 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1485 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1486 sizeof(corefilename), "process corefile name format string"); 1487 1488 /* 1489 * expand_name(name, uid, pid) 1490 * Expand the name described in corefilename, using name, uid, and pid. 1491 * corefilename is a printf-like string, with three format specifiers: 1492 * %N name of process ("name") 1493 * %P process id (pid) 1494 * %U user id (uid) 1495 * For example, "%N.core" is the default; they can be disabled completely 1496 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1497 * This is controlled by the sysctl variable kern.corefile (see above). 1498 */ 1499 1500 static char * 1501 expand_name(name, uid, pid) 1502 const char *name; uid_t uid; pid_t pid; { 1503 char *temp; 1504 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1505 int i, n; 1506 char *format = corefilename; 1507 size_t namelen; 1508 1509 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); 1510 if (temp == NULL) 1511 return NULL; 1512 namelen = strlen(name); 1513 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1514 int l; 1515 switch (format[i]) { 1516 case '%': /* Format character */ 1517 i++; 1518 switch (format[i]) { 1519 case '%': 1520 temp[n++] = '%'; 1521 break; 1522 case 'N': /* process name */ 1523 if ((n + namelen) > MAXPATHLEN) { 1524 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1525 pid, name, uid, temp, name); 1526 free(temp, M_TEMP); 1527 return NULL; 1528 } 1529 memcpy(temp+n, name, namelen); 1530 n += namelen; 1531 break; 1532 case 'P': /* process id */ 1533 l = sprintf(buf, "%u", pid); 1534 if ((n + l) > MAXPATHLEN) { 1535 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1536 pid, name, uid, temp, name); 1537 free(temp, M_TEMP); 1538 return NULL; 1539 } 1540 memcpy(temp+n, buf, l); 1541 n += l; 1542 break; 1543 case 'U': /* user id */ 1544 l = sprintf(buf, "%u", uid); 1545 if ((n + l) > MAXPATHLEN) { 1546 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", 1547 pid, name, uid, temp, name); 1548 free(temp, M_TEMP); 1549 return NULL; 1550 } 1551 memcpy(temp+n, buf, l); 1552 n += l; 1553 break; 1554 default: 1555 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); 1556 } 1557 break; 1558 default: 1559 temp[n++] = format[i]; 1560 } 1561 } 1562 temp[n] = '\0'; 1563 return temp; 1564 } 1565 1566 /* 1567 * Dump a process' core. The main routine does some 1568 * policy checking, and creates the name of the coredump; 1569 * then it passes on a vnode and a size limit to the process-specific 1570 * coredump routine if there is one; if there _is not_ one, it returns 1571 * ENOSYS; otherwise it returns the error from the process-specific routine. 1572 */ 1573 1574 static int 1575 coredump(p) 1576 register struct proc *p; 1577 { 1578 register struct vnode *vp; 1579 register struct ucred *cred = p->p_ucred; 1580 struct nameidata nd; 1581 struct vattr vattr; 1582 int error, error1; 1583 char *name; /* name of corefile */ 1584 off_t limit; 1585 1586 STOPEVENT(p, S_CORE, 0); 1587 1588 if ((sugid_coredump == 0) && p->p_flag & P_SUGID) 1589 return (EFAULT); 1590 1591 /* 1592 * Note that the bulk of limit checking is done after 1593 * the corefile is created. The exception is if the limit 1594 * for corefiles is 0, in which case we don't bother 1595 * creating the corefile at all. This layout means that 1596 * a corefile is truncated instead of not being created, 1597 * if it is larger than the limit. 1598 */ 1599 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1600 if (limit == 0) 1601 return 0; 1602 1603 name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); 1604 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p); 1605 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR); 1606 free(name, M_TEMP); 1607 if (error) 1608 return (error); 1609 NDFREE(&nd, NDF_ONLY_PNBUF); 1610 vp = nd.ni_vp; 1611 1612 /* Don't dump to non-regular files or files with links. */ 1613 if (vp->v_type != VREG || 1614 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { 1615 error = EFAULT; 1616 goto out; 1617 } 1618 VATTR_NULL(&vattr); 1619 vattr.va_size = 0; 1620 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1621 VOP_SETATTR(vp, &vattr, cred, p); 1622 p->p_acflag |= ACORE; 1623 1624 error = p->p_sysent->sv_coredump ? 1625 p->p_sysent->sv_coredump(p, vp, limit) : 1626 ENOSYS; 1627 1628 out: 1629 VOP_UNLOCK(vp, 0, p); 1630 error1 = vn_close(vp, FWRITE, cred, p); 1631 if (error == 0) 1632 error = error1; 1633 return (error); 1634 } 1635 1636 /* 1637 * Nonexistent system call-- signal process (may want to handle it). 1638 * Flag error in case process won't see signal immediately (blocked or ignored). 1639 */ 1640 #ifndef _SYS_SYSPROTO_H_ 1641 struct nosys_args { 1642 int dummy; 1643 }; 1644 #endif 1645 /* ARGSUSED */ 1646 int 1647 nosys(p, args) 1648 struct proc *p; 1649 struct nosys_args *args; 1650 { 1651 1652 psignal(p, SIGSYS); 1653 return (EINVAL); 1654 } 1655 1656 /* 1657 * Send a signal to a SIGIO or SIGURG to a process or process group using 1658 * stored credentials rather than those of the current process. 1659 */ 1660 void 1661 pgsigio(sigio, sig, checkctty) 1662 struct sigio *sigio; 1663 int sig, checkctty; 1664 { 1665 if (sigio == NULL) 1666 return; 1667 1668 if (sigio->sio_pgid > 0) { 1669 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, 1670 sigio->sio_proc)) 1671 psignal(sigio->sio_proc, sig); 1672 } else if (sigio->sio_pgid < 0) { 1673 struct proc *p; 1674 1675 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) 1676 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) && 1677 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 1678 psignal(p, sig); 1679 } 1680 } 1681