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