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