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 stop(p); 1438 goto out; 1439 } else 1440 goto runfast; 1441 /* NOTREACHED */ 1442 } else { 1443 /* Not in "NORMAL" state. discard the signal. */ 1444 SIGDELSET(p->p_siglist, sig); 1445 goto out; 1446 } 1447 1448 /* 1449 * The process is not stopped so we need to apply the signal to all the 1450 * running threads. 1451 */ 1452 1453 runfast: 1454 mtx_lock_spin(&sched_lock); 1455 FOREACH_THREAD_IN_PROC(p, td) 1456 tdsignal(td, sig, action); 1457 thread_unsuspend(p); 1458 mtx_unlock_spin(&sched_lock); 1459 out: 1460 /* If we jump here, sched_lock should not be owned. */ 1461 mtx_assert(&sched_lock, MA_NOTOWNED); 1462 } 1463 1464 /* 1465 * The force of a signal has been directed against a single 1466 * thread. We need to see what we can do about knocking it 1467 * out of any sleep it may be in etc. 1468 */ 1469 static void 1470 tdsignal(struct thread *td, int sig, sig_t action) 1471 { 1472 struct proc *p = td->td_proc; 1473 register int prop; 1474 1475 mtx_assert(&sched_lock, MA_OWNED); 1476 prop = sigprop(sig); 1477 /* 1478 * Bring the priority of a thread up if we want it to get 1479 * killed in this lifetime. 1480 */ 1481 if ((action == SIG_DFL) && (prop & SA_KILL)) { 1482 if (td->td_priority > PUSER) { 1483 td->td_priority = PUSER; 1484 } 1485 } 1486 1487 /* 1488 * Defer further processing for signals which are held, 1489 * except that stopped processes must be continued by SIGCONT. 1490 */ 1491 if (action == SIG_HOLD) { 1492 return; 1493 } 1494 if (td->td_state == TDS_SLP) { 1495 /* 1496 * If thread is sleeping uninterruptibly 1497 * we can't interrupt the sleep... the signal will 1498 * be noticed when the process returns through 1499 * trap() or syscall(). 1500 */ 1501 if ((td->td_flags & TDF_SINTR) == 0) { 1502 return; 1503 } 1504 /* 1505 * Process is sleeping and traced. Make it runnable 1506 * so it can discover the signal in issignal() and stop 1507 * for its parent. 1508 */ 1509 if (p->p_flag & P_TRACED) { 1510 p->p_flag &= ~P_STOPPED_TRACE; 1511 } else { 1512 1513 /* 1514 * If SIGCONT is default (or ignored) and process is 1515 * asleep, we are finished; the process should not 1516 * be awakened. 1517 */ 1518 if ((prop & SA_CONT) && action == SIG_DFL) { 1519 SIGDELSET(p->p_siglist, sig); 1520 return; 1521 } 1522 1523 /* 1524 * Raise priority to at least PUSER. 1525 */ 1526 if (td->td_priority > PUSER) { 1527 td->td_priority = PUSER; 1528 } 1529 } 1530 setrunnable(td); 1531 } 1532 #ifdef SMP 1533 else { 1534 /* 1535 * Other states do nothing with the signal immediatly, 1536 * other than kicking ourselves if we are running. 1537 * It will either never be noticed, or noticed very soon. 1538 */ 1539 if (td->td_state == TDS_RUNNING && td != curthread) { 1540 forward_signal(td); 1541 } 1542 } 1543 #endif 1544 } 1545 1546 /* 1547 * If the current process has received a signal (should be caught or cause 1548 * termination, should interrupt current syscall), return the signal number. 1549 * Stop signals with default action are processed immediately, then cleared; 1550 * they aren't returned. This is checked after each entry to the system for 1551 * a syscall or trap (though this can usually be done without calling issignal 1552 * by checking the pending signal masks in cursig.) The normal call 1553 * sequence is 1554 * 1555 * while (sig = cursig(curthread)) 1556 * postsig(sig); 1557 */ 1558 int 1559 issignal(td) 1560 struct thread *td; 1561 { 1562 struct proc *p; 1563 sigset_t mask; 1564 register int sig, prop; 1565 1566 p = td->td_proc; 1567 PROC_LOCK_ASSERT(p, MA_OWNED); 1568 for (;;) { 1569 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 1570 1571 mask = p->p_siglist; 1572 SIGSETNAND(mask, p->p_sigmask); 1573 if (p->p_flag & P_PPWAIT) 1574 SIG_STOPSIGMASK(mask); 1575 if (SIGISEMPTY(mask)) /* no signal to send */ 1576 return (0); 1577 sig = sig_ffs(&mask); 1578 prop = sigprop(sig); 1579 1580 _STOPEVENT(p, S_SIG, sig); 1581 1582 /* 1583 * We should see pending but ignored signals 1584 * only if P_TRACED was on when they were posted. 1585 */ 1586 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) { 1587 SIGDELSET(p->p_siglist, sig); 1588 continue; 1589 } 1590 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 1591 /* 1592 * If traced, always stop. 1593 */ 1594 p->p_xstat = sig; 1595 PROC_LOCK(p->p_pptr); 1596 psignal(p->p_pptr, SIGCHLD); 1597 PROC_UNLOCK(p->p_pptr); 1598 mtx_lock_spin(&sched_lock); 1599 stop(p); /* uses schedlock too eventually */ 1600 td->td_state = TDS_UNQUEUED; 1601 PROC_UNLOCK(p); 1602 DROP_GIANT(); 1603 p->p_stats->p_ru.ru_nivcsw++; 1604 mi_switch(); 1605 mtx_unlock_spin(&sched_lock); 1606 PICKUP_GIANT(); 1607 PROC_LOCK(p); 1608 1609 /* 1610 * If the traced bit got turned off, go back up 1611 * to the top to rescan signals. This ensures 1612 * that p_sig* and ps_sigact are consistent. 1613 */ 1614 if ((p->p_flag & P_TRACED) == 0) 1615 continue; 1616 1617 /* 1618 * If parent wants us to take the signal, 1619 * then it will leave it in p->p_xstat; 1620 * otherwise we just look for signals again. 1621 */ 1622 SIGDELSET(p->p_siglist, sig); /* clear old signal */ 1623 sig = p->p_xstat; 1624 if (sig == 0) 1625 continue; 1626 1627 /* 1628 * Put the new signal into p_siglist. If the 1629 * signal is being masked, look for other signals. 1630 */ 1631 SIGADDSET(p->p_siglist, sig); 1632 if (SIGISMEMBER(p->p_sigmask, sig)) 1633 continue; 1634 } 1635 1636 /* 1637 * Decide whether the signal should be returned. 1638 * Return the signal's number, or fall through 1639 * to clear it from the pending mask. 1640 */ 1641 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 1642 1643 case (int)SIG_DFL: 1644 /* 1645 * Don't take default actions on system processes. 1646 */ 1647 if (p->p_pid <= 1) { 1648 #ifdef DIAGNOSTIC 1649 /* 1650 * Are you sure you want to ignore SIGSEGV 1651 * in init? XXX 1652 */ 1653 printf("Process (pid %lu) got signal %d\n", 1654 (u_long)p->p_pid, sig); 1655 #endif 1656 break; /* == ignore */ 1657 } 1658 /* 1659 * If there is a pending stop signal to process 1660 * with default action, stop here, 1661 * then clear the signal. However, 1662 * if process is member of an orphaned 1663 * process group, ignore tty stop signals. 1664 */ 1665 if (prop & SA_STOP) { 1666 if (p->p_flag & P_TRACED || 1667 (p->p_pgrp->pg_jobc == 0 && 1668 prop & SA_TTYSTOP)) 1669 break; /* == ignore */ 1670 p->p_xstat = sig; 1671 PROC_LOCK(p->p_pptr); 1672 if ((p->p_pptr->p_procsig->ps_flag & 1673 PS_NOCLDSTOP) == 0) { 1674 psignal(p->p_pptr, SIGCHLD); 1675 } 1676 PROC_UNLOCK(p->p_pptr); 1677 stop(p); 1678 break; 1679 } else 1680 if (prop & SA_IGNORE) { 1681 /* 1682 * Except for SIGCONT, shouldn't get here. 1683 * Default action is to ignore; drop it. 1684 */ 1685 break; /* == ignore */ 1686 } else 1687 return (sig); 1688 /*NOTREACHED*/ 1689 1690 case (int)SIG_IGN: 1691 /* 1692 * Masking above should prevent us ever trying 1693 * to take action on an ignored signal other 1694 * than SIGCONT, unless process is traced. 1695 */ 1696 if ((prop & SA_CONT) == 0 && 1697 (p->p_flag & P_TRACED) == 0) 1698 printf("issignal\n"); 1699 break; /* == ignore */ 1700 1701 default: 1702 /* 1703 * This signal has an action, let 1704 * postsig() process it. 1705 */ 1706 return (sig); 1707 } 1708 SIGDELSET(p->p_siglist, sig); /* take the signal! */ 1709 } 1710 /* NOTREACHED */ 1711 } 1712 1713 /* 1714 * Put the argument process into the stopped state and notify the parent 1715 * via wakeup. Signals are handled elsewhere. The process must not be 1716 * on the run queue. Must be called with the proc p locked and the scheduler 1717 * lock held. 1718 */ 1719 static void 1720 stop(p) 1721 register struct proc *p; 1722 { 1723 1724 PROC_LOCK_ASSERT(p, MA_OWNED); 1725 p->p_flag |= P_STOPPED_SGNL; 1726 p->p_flag &= ~P_WAITED; 1727 wakeup(p->p_pptr); 1728 } 1729 1730 /* 1731 * Take the action for the specified signal 1732 * from the current set of pending signals. 1733 */ 1734 void 1735 postsig(sig) 1736 register int sig; 1737 { 1738 struct thread *td = curthread; 1739 register struct proc *p = td->td_proc; 1740 struct sigacts *ps; 1741 sig_t action; 1742 sigset_t returnmask; 1743 int code; 1744 1745 KASSERT(sig != 0, ("postsig")); 1746 1747 PROC_LOCK_ASSERT(p, MA_OWNED); 1748 ps = p->p_sigacts; 1749 SIGDELSET(p->p_siglist, sig); 1750 action = ps->ps_sigact[_SIG_IDX(sig)]; 1751 #ifdef KTRACE 1752 if (KTRPOINT(td, KTR_PSIG)) 1753 ktrpsig(sig, action, p->p_flag & P_OLDMASK ? 1754 &p->p_oldsigmask : &p->p_sigmask, 0); 1755 #endif 1756 _STOPEVENT(p, S_SIG, sig); 1757 1758 if (action == SIG_DFL) { 1759 /* 1760 * Default action, where the default is to kill 1761 * the process. (Other cases were ignored above.) 1762 */ 1763 sigexit(td, sig); 1764 /* NOTREACHED */ 1765 } else { 1766 /* 1767 * If we get here, the signal must be caught. 1768 */ 1769 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig), 1770 ("postsig action")); 1771 /* 1772 * Set the new mask value and also defer further 1773 * occurrences of this signal. 1774 * 1775 * Special case: user has done a sigsuspend. Here the 1776 * current mask is not of interest, but rather the 1777 * mask from before the sigsuspend is what we want 1778 * restored after the signal processing is completed. 1779 */ 1780 if (p->p_flag & P_OLDMASK) { 1781 returnmask = p->p_oldsigmask; 1782 p->p_flag &= ~P_OLDMASK; 1783 } else 1784 returnmask = p->p_sigmask; 1785 1786 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1787 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1788 SIGADDSET(p->p_sigmask, sig); 1789 1790 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1791 /* 1792 * See do_sigaction() for origin of this code. 1793 */ 1794 SIGDELSET(p->p_sigcatch, sig); 1795 if (sig != SIGCONT && 1796 sigprop(sig) & SA_IGNORE) 1797 SIGADDSET(p->p_sigignore, sig); 1798 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1799 } 1800 p->p_stats->p_ru.ru_nsignals++; 1801 if (p->p_sig != sig) { 1802 code = 0; 1803 } else { 1804 code = p->p_code; 1805 p->p_code = 0; 1806 p->p_sig = 0; 1807 } 1808 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code); 1809 } 1810 } 1811 1812 /* 1813 * Kill the current process for stated reason. 1814 */ 1815 void 1816 killproc(p, why) 1817 struct proc *p; 1818 char *why; 1819 { 1820 1821 PROC_LOCK_ASSERT(p, MA_OWNED); 1822 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 1823 p, p->p_pid, p->p_comm); 1824 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 1825 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 1826 psignal(p, SIGKILL); 1827 } 1828 1829 /* 1830 * Force the current process to exit with the specified signal, dumping core 1831 * if appropriate. We bypass the normal tests for masked and caught signals, 1832 * allowing unrecoverable failures to terminate the process without changing 1833 * signal state. Mark the accounting record with the signal termination. 1834 * If dumping core, save the signal number for the debugger. Calls exit and 1835 * does not return. 1836 */ 1837 void 1838 sigexit(td, sig) 1839 struct thread *td; 1840 int sig; 1841 { 1842 struct proc *p = td->td_proc; 1843 1844 PROC_LOCK_ASSERT(p, MA_OWNED); 1845 p->p_acflag |= AXSIG; 1846 if (sigprop(sig) & SA_CORE) { 1847 p->p_sig = sig; 1848 /* 1849 * Log signals which would cause core dumps 1850 * (Log as LOG_INFO to appease those who don't want 1851 * these messages.) 1852 * XXX : Todo, as well as euid, write out ruid too 1853 */ 1854 PROC_UNLOCK(p); 1855 if (!mtx_owned(&Giant)) 1856 mtx_lock(&Giant); 1857 if (coredump(td) == 0) 1858 sig |= WCOREFLAG; 1859 if (kern_logsigexit) 1860 log(LOG_INFO, 1861 "pid %d (%s), uid %d: exited on signal %d%s\n", 1862 p->p_pid, p->p_comm, 1863 td->td_ucred ? td->td_ucred->cr_uid : -1, 1864 sig &~ WCOREFLAG, 1865 sig & WCOREFLAG ? " (core dumped)" : ""); 1866 } else { 1867 PROC_UNLOCK(p); 1868 if (!mtx_owned(&Giant)) 1869 mtx_lock(&Giant); 1870 } 1871 exit1(td, W_EXITCODE(0, sig)); 1872 /* NOTREACHED */ 1873 } 1874 1875 static char corefilename[MAXPATHLEN+1] = {"%N.core"}; 1876 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 1877 sizeof(corefilename), "process corefile name format string"); 1878 1879 /* 1880 * expand_name(name, uid, pid) 1881 * Expand the name described in corefilename, using name, uid, and pid. 1882 * corefilename is a printf-like string, with three format specifiers: 1883 * %N name of process ("name") 1884 * %P process id (pid) 1885 * %U user id (uid) 1886 * For example, "%N.core" is the default; they can be disabled completely 1887 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 1888 * This is controlled by the sysctl variable kern.corefile (see above). 1889 */ 1890 1891 static char * 1892 expand_name(name, uid, pid) 1893 const char *name; 1894 uid_t uid; 1895 pid_t pid; 1896 { 1897 const char *format, *appendstr; 1898 char *temp; 1899 char buf[11]; /* Buffer for pid/uid -- max 4B */ 1900 size_t i, l, n; 1901 1902 format = corefilename; 1903 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 1904 if (temp == NULL) 1905 return (NULL); 1906 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 1907 switch (format[i]) { 1908 case '%': /* Format character */ 1909 i++; 1910 switch (format[i]) { 1911 case '%': 1912 appendstr = "%"; 1913 break; 1914 case 'N': /* process name */ 1915 appendstr = name; 1916 break; 1917 case 'P': /* process id */ 1918 sprintf(buf, "%u", pid); 1919 appendstr = buf; 1920 break; 1921 case 'U': /* user id */ 1922 sprintf(buf, "%u", uid); 1923 appendstr = buf; 1924 break; 1925 default: 1926 appendstr = ""; 1927 log(LOG_ERR, 1928 "Unknown format character %c in `%s'\n", 1929 format[i], format); 1930 } 1931 l = strlen(appendstr); 1932 if ((n + l) >= MAXPATHLEN) 1933 goto toolong; 1934 memcpy(temp + n, appendstr, l); 1935 n += l; 1936 break; 1937 default: 1938 temp[n++] = format[i]; 1939 } 1940 } 1941 if (format[i] != '\0') 1942 goto toolong; 1943 return (temp); 1944 toolong: 1945 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 1946 (long)pid, name, (u_long)uid); 1947 free(temp, M_TEMP); 1948 return (NULL); 1949 } 1950 1951 /* 1952 * Dump a process' core. The main routine does some 1953 * policy checking, and creates the name of the coredump; 1954 * then it passes on a vnode and a size limit to the process-specific 1955 * coredump routine if there is one; if there _is not_ one, it returns 1956 * ENOSYS; otherwise it returns the error from the process-specific routine. 1957 */ 1958 1959 static int 1960 coredump(struct thread *td) 1961 { 1962 struct proc *p = td->td_proc; 1963 register struct vnode *vp; 1964 register struct ucred *cred = td->td_ucred; 1965 struct flock lf; 1966 struct nameidata nd; 1967 struct vattr vattr; 1968 int error, error1, flags; 1969 struct mount *mp; 1970 char *name; /* name of corefile */ 1971 off_t limit; 1972 1973 PROC_LOCK(p); 1974 _STOPEVENT(p, S_CORE, 0); 1975 1976 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 1977 PROC_UNLOCK(p); 1978 return (EFAULT); 1979 } 1980 1981 /* 1982 * Note that the bulk of limit checking is done after 1983 * the corefile is created. The exception is if the limit 1984 * for corefiles is 0, in which case we don't bother 1985 * creating the corefile at all. This layout means that 1986 * a corefile is truncated instead of not being created, 1987 * if it is larger than the limit. 1988 */ 1989 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; 1990 if (limit == 0) { 1991 PROC_UNLOCK(p); 1992 return 0; 1993 } 1994 PROC_UNLOCK(p); 1995 1996 restart: 1997 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 1998 if (name == NULL) 1999 return (EINVAL); 2000 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 2001 flags = O_CREAT | FWRITE | O_NOFOLLOW; 2002 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR); 2003 free(name, M_TEMP); 2004 if (error) 2005 return (error); 2006 NDFREE(&nd, NDF_ONLY_PNBUF); 2007 vp = nd.ni_vp; 2008 2009 /* Don't dump to non-regular files or files with links. */ 2010 if (vp->v_type != VREG || 2011 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 2012 VOP_UNLOCK(vp, 0, td); 2013 error = EFAULT; 2014 goto out2; 2015 } 2016 2017 VOP_UNLOCK(vp, 0, td); 2018 lf.l_whence = SEEK_SET; 2019 lf.l_start = 0; 2020 lf.l_len = 0; 2021 lf.l_type = F_WRLCK; 2022 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK); 2023 if (error) 2024 goto out2; 2025 2026 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2027 lf.l_type = F_UNLCK; 2028 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2029 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 2030 return (error); 2031 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 2032 return (error); 2033 goto restart; 2034 } 2035 2036 VATTR_NULL(&vattr); 2037 vattr.va_size = 0; 2038 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2039 VOP_LEASE(vp, td, cred, LEASE_WRITE); 2040 VOP_SETATTR(vp, &vattr, cred, td); 2041 VOP_UNLOCK(vp, 0, td); 2042 PROC_LOCK(p); 2043 p->p_acflag |= ACORE; 2044 PROC_UNLOCK(p); 2045 2046 error = p->p_sysent->sv_coredump ? 2047 p->p_sysent->sv_coredump(td, vp, limit) : 2048 ENOSYS; 2049 2050 lf.l_type = F_UNLCK; 2051 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 2052 vn_finished_write(mp); 2053 out2: 2054 error1 = vn_close(vp, FWRITE, cred, td); 2055 if (error == 0) 2056 error = error1; 2057 return (error); 2058 } 2059 2060 /* 2061 * Nonexistent system call-- signal process (may want to handle it). 2062 * Flag error in case process won't see signal immediately (blocked or ignored). 2063 */ 2064 #ifndef _SYS_SYSPROTO_H_ 2065 struct nosys_args { 2066 int dummy; 2067 }; 2068 #endif 2069 /* 2070 * MPSAFE 2071 */ 2072 /* ARGSUSED */ 2073 int 2074 nosys(td, args) 2075 struct thread *td; 2076 struct nosys_args *args; 2077 { 2078 struct proc *p = td->td_proc; 2079 2080 mtx_lock(&Giant); 2081 PROC_LOCK(p); 2082 psignal(p, SIGSYS); 2083 PROC_UNLOCK(p); 2084 mtx_unlock(&Giant); 2085 return (ENOSYS); 2086 } 2087 2088 /* 2089 * Send a SIGIO or SIGURG signal to a process or process group using 2090 * stored credentials rather than those of the current process. 2091 */ 2092 void 2093 pgsigio(sigiop, sig, checkctty) 2094 struct sigio **sigiop; 2095 int sig, checkctty; 2096 { 2097 struct sigio *sigio; 2098 2099 SIGIO_LOCK(); 2100 sigio = *sigiop; 2101 if (sigio == NULL) { 2102 SIGIO_UNLOCK(); 2103 return; 2104 } 2105 if (sigio->sio_pgid > 0) { 2106 PROC_LOCK(sigio->sio_proc); 2107 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 2108 psignal(sigio->sio_proc, sig); 2109 PROC_UNLOCK(sigio->sio_proc); 2110 } else if (sigio->sio_pgid < 0) { 2111 struct proc *p; 2112 2113 PGRP_LOCK(sigio->sio_pgrp); 2114 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 2115 PROC_LOCK(p); 2116 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 2117 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 2118 psignal(p, sig); 2119 PROC_UNLOCK(p); 2120 } 2121 PGRP_UNLOCK(sigio->sio_pgrp); 2122 } 2123 SIGIO_UNLOCK(); 2124 } 2125 2126 static int 2127 filt_sigattach(struct knote *kn) 2128 { 2129 struct proc *p = curproc; 2130 2131 kn->kn_ptr.p_proc = p; 2132 kn->kn_flags |= EV_CLEAR; /* automatically set */ 2133 2134 PROC_LOCK(p); 2135 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext); 2136 PROC_UNLOCK(p); 2137 2138 return (0); 2139 } 2140 2141 static void 2142 filt_sigdetach(struct knote *kn) 2143 { 2144 struct proc *p = kn->kn_ptr.p_proc; 2145 2146 PROC_LOCK(p); 2147 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext); 2148 PROC_UNLOCK(p); 2149 } 2150 2151 /* 2152 * signal knotes are shared with proc knotes, so we apply a mask to 2153 * the hint in order to differentiate them from process hints. This 2154 * could be avoided by using a signal-specific knote list, but probably 2155 * isn't worth the trouble. 2156 */ 2157 static int 2158 filt_signal(struct knote *kn, long hint) 2159 { 2160 2161 if (hint & NOTE_SIGNAL) { 2162 hint &= ~NOTE_SIGNAL; 2163 2164 if (kn->kn_id == hint) 2165 kn->kn_data++; 2166 } 2167 return (kn->kn_data != 0); 2168 } 2169