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