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