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