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