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