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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_compat.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/signalvar.h> 46 #include <sys/vnode.h> 47 #include <sys/acct.h> 48 #include <sys/condvar.h> 49 #include <sys/event.h> 50 #include <sys/fcntl.h> 51 #include <sys/kernel.h> 52 #include <sys/kse.h> 53 #include <sys/ktr.h> 54 #include <sys/ktrace.h> 55 #include <sys/lock.h> 56 #include <sys/malloc.h> 57 #include <sys/mutex.h> 58 #include <sys/namei.h> 59 #include <sys/proc.h> 60 #include <sys/pioctl.h> 61 #include <sys/resourcevar.h> 62 #include <sys/sched.h> 63 #include <sys/sleepqueue.h> 64 #include <sys/smp.h> 65 #include <sys/stat.h> 66 #include <sys/sx.h> 67 #include <sys/syscallsubr.h> 68 #include <sys/sysctl.h> 69 #include <sys/sysent.h> 70 #include <sys/syslog.h> 71 #include <sys/sysproto.h> 72 #include <sys/timers.h> 73 #include <sys/unistd.h> 74 #include <sys/wait.h> 75 #include <vm/vm.h> 76 #include <vm/vm_extern.h> 77 #include <vm/uma.h> 78 79 #include <posix4/posix4.h> 80 #include <machine/cpu.h> 81 82 #if defined (__alpha__) && !defined(COMPAT_43) 83 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)" 84 #endif 85 86 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */ 87 88 static int coredump(struct thread *); 89 static char *expand_name(const char *, uid_t, pid_t); 90 static int killpg1(struct thread *td, int sig, int pgid, int all); 91 static int issignal(struct thread *p); 92 static int sigprop(int sig); 93 static void tdsigwakeup(struct thread *td, int sig, sig_t action); 94 static int filt_sigattach(struct knote *kn); 95 static void filt_sigdetach(struct knote *kn); 96 static int filt_signal(struct knote *kn, long hint); 97 static struct thread *sigtd(struct proc *p, int sig, int prop); 98 static int kern_sigtimedwait(struct thread *, sigset_t, 99 ksiginfo_t *, struct timespec *); 100 static int do_tdsignal(struct proc *, struct thread *, int, ksiginfo_t *); 101 static void sigqueue_start(void); 102 103 static uma_zone_t ksiginfo_zone = NULL; 104 struct filterops sig_filtops = 105 { 0, filt_sigattach, filt_sigdetach, filt_signal }; 106 107 static int kern_logsigexit = 1; 108 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 109 &kern_logsigexit, 0, 110 "Log processes quitting on abnormal signals to syslog(3)"); 111 112 static int kern_forcesigexit = 1; 113 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW, 114 &kern_forcesigexit, 0, "Force trap signal to be handled"); 115 116 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal"); 117 118 static int max_pending_per_proc = 128; 119 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW, 120 &max_pending_per_proc, 0, "Max pending signals per proc"); 121 122 static int preallocate_siginfo = 1024; 123 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo); 124 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD, 125 &preallocate_siginfo, 0, "Preallocated signal memory size"); 126 127 static int signal_overflow = 0; 128 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD, 129 &signal_overflow, 0, "Number of signals overflew"); 130 131 static int signal_alloc_fail = 0; 132 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD, 133 &signal_alloc_fail, 0, "signals failed to be allocated"); 134 135 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL); 136 137 /* 138 * Policy -- Can ucred cr1 send SIGIO to process cr2? 139 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG 140 * in the right situations. 141 */ 142 #define CANSIGIO(cr1, cr2) \ 143 ((cr1)->cr_uid == 0 || \ 144 (cr1)->cr_ruid == (cr2)->cr_ruid || \ 145 (cr1)->cr_uid == (cr2)->cr_ruid || \ 146 (cr1)->cr_ruid == (cr2)->cr_uid || \ 147 (cr1)->cr_uid == (cr2)->cr_uid) 148 149 int sugid_coredump; 150 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 151 &sugid_coredump, 0, "Enable coredumping set user/group ID processes"); 152 153 static int do_coredump = 1; 154 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW, 155 &do_coredump, 0, "Enable/Disable coredumps"); 156 157 static int set_core_nodump_flag = 0; 158 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag, 159 0, "Enable setting the NODUMP flag on coredump files"); 160 161 /* 162 * Signal properties and actions. 163 * The array below categorizes the signals and their default actions 164 * according to the following properties: 165 */ 166 #define SA_KILL 0x01 /* terminates process by default */ 167 #define SA_CORE 0x02 /* ditto and coredumps */ 168 #define SA_STOP 0x04 /* suspend process */ 169 #define SA_TTYSTOP 0x08 /* ditto, from tty */ 170 #define SA_IGNORE 0x10 /* ignore by default */ 171 #define SA_CONT 0x20 /* continue if suspended */ 172 #define SA_CANTMASK 0x40 /* non-maskable, catchable */ 173 #define SA_PROC 0x80 /* deliverable to any thread */ 174 175 static int sigproptbl[NSIG] = { 176 SA_KILL|SA_PROC, /* SIGHUP */ 177 SA_KILL|SA_PROC, /* SIGINT */ 178 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */ 179 SA_KILL|SA_CORE, /* SIGILL */ 180 SA_KILL|SA_CORE, /* SIGTRAP */ 181 SA_KILL|SA_CORE, /* SIGABRT */ 182 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */ 183 SA_KILL|SA_CORE, /* SIGFPE */ 184 SA_KILL|SA_PROC, /* SIGKILL */ 185 SA_KILL|SA_CORE, /* SIGBUS */ 186 SA_KILL|SA_CORE, /* SIGSEGV */ 187 SA_KILL|SA_CORE, /* SIGSYS */ 188 SA_KILL|SA_PROC, /* SIGPIPE */ 189 SA_KILL|SA_PROC, /* SIGALRM */ 190 SA_KILL|SA_PROC, /* SIGTERM */ 191 SA_IGNORE|SA_PROC, /* SIGURG */ 192 SA_STOP|SA_PROC, /* SIGSTOP */ 193 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */ 194 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */ 195 SA_IGNORE|SA_PROC, /* SIGCHLD */ 196 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */ 197 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */ 198 SA_IGNORE|SA_PROC, /* SIGIO */ 199 SA_KILL, /* SIGXCPU */ 200 SA_KILL, /* SIGXFSZ */ 201 SA_KILL|SA_PROC, /* SIGVTALRM */ 202 SA_KILL|SA_PROC, /* SIGPROF */ 203 SA_IGNORE|SA_PROC, /* SIGWINCH */ 204 SA_IGNORE|SA_PROC, /* SIGINFO */ 205 SA_KILL|SA_PROC, /* SIGUSR1 */ 206 SA_KILL|SA_PROC, /* SIGUSR2 */ 207 }; 208 209 static void 210 sigqueue_start(void) 211 { 212 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t), 213 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 214 uma_prealloc(ksiginfo_zone, preallocate_siginfo); 215 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); 216 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); 217 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc); 218 } 219 220 ksiginfo_t * 221 ksiginfo_alloc(int wait) 222 { 223 int flags; 224 225 flags = M_ZERO; 226 if (! wait) 227 flags |= M_NOWAIT; 228 if (ksiginfo_zone != NULL) 229 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags)); 230 return (NULL); 231 } 232 233 void 234 ksiginfo_free(ksiginfo_t *ksi) 235 { 236 uma_zfree(ksiginfo_zone, ksi); 237 } 238 239 static __inline int 240 ksiginfo_tryfree(ksiginfo_t *ksi) 241 { 242 if (!(ksi->ksi_flags & KSI_EXT)) { 243 uma_zfree(ksiginfo_zone, ksi); 244 return (1); 245 } 246 return (0); 247 } 248 249 void 250 sigqueue_init(sigqueue_t *list, struct proc *p) 251 { 252 SIGEMPTYSET(list->sq_signals); 253 TAILQ_INIT(&list->sq_list); 254 list->sq_proc = p; 255 list->sq_flags = SQ_INIT; 256 } 257 258 /* 259 * Get a signal's ksiginfo. 260 * Return: 261 * 0 - signal not found 262 * others - signal number 263 */ 264 int 265 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si) 266 { 267 struct proc *p = sq->sq_proc; 268 struct ksiginfo *ksi, *next; 269 int count = 0; 270 271 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 272 273 if (!SIGISMEMBER(sq->sq_signals, signo)) 274 return (0); 275 276 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) { 277 next = TAILQ_NEXT(ksi, ksi_link); 278 if (ksi->ksi_signo == signo) { 279 if (count == 0) { 280 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 281 ksi->ksi_sigq = NULL; 282 ksiginfo_copy(ksi, si); 283 if (ksiginfo_tryfree(ksi) && p != NULL) 284 p->p_pendingcnt--; 285 } 286 count++; 287 } 288 } 289 290 if (count <= 1) 291 SIGDELSET(sq->sq_signals, signo); 292 si->ksi_signo = signo; 293 return (signo); 294 } 295 296 void 297 sigqueue_take(ksiginfo_t *ksi) 298 { 299 struct ksiginfo *kp; 300 struct proc *p; 301 sigqueue_t *sq; 302 303 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL) 304 return; 305 306 p = sq->sq_proc; 307 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 308 ksi->ksi_sigq = NULL; 309 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL) 310 p->p_pendingcnt--; 311 312 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL; 313 kp = TAILQ_NEXT(kp, ksi_link)) { 314 if (kp->ksi_signo == ksi->ksi_signo) 315 break; 316 } 317 if (kp == NULL) 318 SIGDELSET(sq->sq_signals, ksi->ksi_signo); 319 } 320 321 int 322 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si) 323 { 324 struct proc *p = sq->sq_proc; 325 struct ksiginfo *ksi; 326 int ret = 0; 327 328 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 329 330 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) 331 goto out_set_bit; 332 333 /* directly insert the ksi, don't copy it */ 334 if (si->ksi_flags & KSI_INS) { 335 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link); 336 si->ksi_sigq = sq; 337 goto out_set_bit; 338 } 339 340 if (__predict_false(ksiginfo_zone == NULL)) 341 goto out_set_bit; 342 343 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) { 344 signal_overflow++; 345 ret = EAGAIN; 346 } else if ((ksi = ksiginfo_alloc(0)) == NULL) { 347 signal_alloc_fail++; 348 ret = EAGAIN; 349 } else { 350 if (p != NULL) 351 p->p_pendingcnt++; 352 ksiginfo_copy(si, ksi); 353 ksi->ksi_signo = signo; 354 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link); 355 ksi->ksi_sigq = sq; 356 } 357 358 if ((si->ksi_flags & KSI_TRAP) != 0) { 359 ret = 0; 360 goto out_set_bit; 361 } 362 363 if (ret != 0) 364 return (ret); 365 366 out_set_bit: 367 SIGADDSET(sq->sq_signals, signo); 368 return (ret); 369 } 370 371 void 372 sigqueue_flush(sigqueue_t *sq) 373 { 374 struct proc *p = sq->sq_proc; 375 ksiginfo_t *ksi; 376 377 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 378 379 if (p != NULL) 380 PROC_LOCK_ASSERT(p, MA_OWNED); 381 382 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) { 383 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 384 ksi->ksi_sigq = NULL; 385 if (ksiginfo_tryfree(ksi) && p != NULL) 386 p->p_pendingcnt--; 387 } 388 389 SIGEMPTYSET(sq->sq_signals); 390 } 391 392 void 393 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set) 394 { 395 ksiginfo_t *ksi; 396 397 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited")); 398 399 TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link) 400 SIGADDSET(*set, ksi->ksi_signo); 401 } 402 403 void 404 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp) 405 { 406 sigset_t tmp, set; 407 struct proc *p1, *p2; 408 ksiginfo_t *ksi, *next; 409 410 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited")); 411 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited")); 412 /* 413 * make a copy, this allows setp to point to src or dst 414 * sq_signals without trouble. 415 */ 416 set = *setp; 417 p1 = src->sq_proc; 418 p2 = dst->sq_proc; 419 /* Move siginfo to target list */ 420 for (ksi = TAILQ_FIRST(&src->sq_list); ksi != NULL; ksi = next) { 421 next = TAILQ_NEXT(ksi, ksi_link); 422 if (SIGISMEMBER(set, ksi->ksi_signo)) { 423 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link); 424 if (p1 != NULL) 425 p1->p_pendingcnt--; 426 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link); 427 ksi->ksi_sigq = dst; 428 if (p2 != NULL) 429 p2->p_pendingcnt++; 430 } 431 } 432 433 /* Move pending bits to target list */ 434 tmp = src->sq_signals; 435 SIGSETAND(tmp, set); 436 SIGSETOR(dst->sq_signals, tmp); 437 SIGSETNAND(src->sq_signals, tmp); 438 439 /* Finally, rescan src queue and set pending bits for it */ 440 sigqueue_collect_set(src, &src->sq_signals); 441 } 442 443 void 444 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo) 445 { 446 sigset_t set; 447 448 SIGEMPTYSET(set); 449 SIGADDSET(set, signo); 450 sigqueue_move_set(src, dst, &set); 451 } 452 453 void 454 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set) 455 { 456 struct proc *p = sq->sq_proc; 457 ksiginfo_t *ksi, *next; 458 459 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited")); 460 461 /* Remove siginfo queue */ 462 for (ksi = TAILQ_FIRST(&sq->sq_list); ksi != NULL; ksi = next) { 463 next = TAILQ_NEXT(ksi, ksi_link); 464 if (SIGISMEMBER(*set, ksi->ksi_signo)) { 465 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link); 466 ksi->ksi_sigq = NULL; 467 if (ksiginfo_tryfree(ksi) && p != NULL) 468 p->p_pendingcnt--; 469 } 470 } 471 SIGSETNAND(sq->sq_signals, *set); 472 /* Finally, rescan queue and set pending bits for it */ 473 sigqueue_collect_set(sq, &sq->sq_signals); 474 } 475 476 void 477 sigqueue_delete(sigqueue_t *sq, int signo) 478 { 479 sigset_t set; 480 481 SIGEMPTYSET(set); 482 SIGADDSET(set, signo); 483 sigqueue_delete_set(sq, &set); 484 } 485 486 /* Remove a set of signals for a process */ 487 void 488 sigqueue_delete_set_proc(struct proc *p, sigset_t *set) 489 { 490 sigqueue_t worklist; 491 struct thread *td0; 492 493 PROC_LOCK_ASSERT(p, MA_OWNED); 494 495 sigqueue_init(&worklist, NULL); 496 sigqueue_move_set(&p->p_sigqueue, &worklist, set); 497 498 mtx_lock_spin(&sched_lock); 499 FOREACH_THREAD_IN_PROC(p, td0) 500 sigqueue_move_set(&td0->td_sigqueue, &worklist, set); 501 mtx_unlock_spin(&sched_lock); 502 503 sigqueue_flush(&worklist); 504 } 505 506 void 507 sigqueue_delete_proc(struct proc *p, int signo) 508 { 509 sigset_t set; 510 511 SIGEMPTYSET(set); 512 SIGADDSET(set, signo); 513 sigqueue_delete_set_proc(p, &set); 514 } 515 516 void 517 sigqueue_delete_stopmask_proc(struct proc *p) 518 { 519 sigset_t set; 520 521 SIGEMPTYSET(set); 522 SIGADDSET(set, SIGSTOP); 523 SIGADDSET(set, SIGTSTP); 524 SIGADDSET(set, SIGTTIN); 525 SIGADDSET(set, SIGTTOU); 526 sigqueue_delete_set_proc(p, &set); 527 } 528 529 /* 530 * Determine signal that should be delivered to process p, the current 531 * process, 0 if none. If there is a pending stop signal with default 532 * action, the process stops in issignal(). 533 * 534 * MP SAFE. 535 */ 536 int 537 cursig(struct thread *td) 538 { 539 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 540 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED); 541 mtx_assert(&sched_lock, MA_NOTOWNED); 542 return (SIGPENDING(td) ? issignal(td) : 0); 543 } 544 545 /* 546 * Arrange for ast() to handle unmasked pending signals on return to user 547 * mode. This must be called whenever a signal is added to td_sigqueue or 548 * unmasked in td_sigmask. 549 */ 550 void 551 signotify(struct thread *td) 552 { 553 struct proc *p; 554 sigset_t set, saved; 555 556 p = td->td_proc; 557 558 PROC_LOCK_ASSERT(p, MA_OWNED); 559 560 /* 561 * If our mask changed we may have to move signal that were 562 * previously masked by all threads to our sigqueue. 563 */ 564 set = p->p_sigqueue.sq_signals; 565 if (p->p_flag & P_SA) 566 saved = p->p_sigqueue.sq_signals; 567 SIGSETNAND(set, td->td_sigmask); 568 if (! SIGISEMPTY(set)) 569 sigqueue_move_set(&p->p_sigqueue, &td->td_sigqueue, &set); 570 if (SIGPENDING(td)) { 571 mtx_lock_spin(&sched_lock); 572 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING; 573 mtx_unlock_spin(&sched_lock); 574 } 575 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 576 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) { 577 /* pending set changed */ 578 p->p_flag |= P_SIGEVENT; 579 wakeup(&p->p_siglist); 580 } 581 } 582 } 583 584 int 585 sigonstack(size_t sp) 586 { 587 struct thread *td = curthread; 588 589 return ((td->td_pflags & TDP_ALTSTACK) ? 590 #if defined(COMPAT_43) 591 ((td->td_sigstk.ss_size == 0) ? 592 (td->td_sigstk.ss_flags & SS_ONSTACK) : 593 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)) 594 #else 595 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size) 596 #endif 597 : 0); 598 } 599 600 static __inline int 601 sigprop(int sig) 602 { 603 604 if (sig > 0 && sig < NSIG) 605 return (sigproptbl[_SIG_IDX(sig)]); 606 return (0); 607 } 608 609 int 610 sig_ffs(sigset_t *set) 611 { 612 int i; 613 614 for (i = 0; i < _SIG_WORDS; i++) 615 if (set->__bits[i]) 616 return (ffs(set->__bits[i]) + (i * 32)); 617 return (0); 618 } 619 620 /* 621 * kern_sigaction 622 * sigaction 623 * freebsd4_sigaction 624 * osigaction 625 * 626 * MPSAFE 627 */ 628 int 629 kern_sigaction(td, sig, act, oact, flags) 630 struct thread *td; 631 register int sig; 632 struct sigaction *act, *oact; 633 int flags; 634 { 635 struct sigacts *ps; 636 struct proc *p = td->td_proc; 637 638 if (!_SIG_VALID(sig)) 639 return (EINVAL); 640 641 PROC_LOCK(p); 642 ps = p->p_sigacts; 643 mtx_lock(&ps->ps_mtx); 644 if (oact) { 645 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)]; 646 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)]; 647 oact->sa_flags = 0; 648 if (SIGISMEMBER(ps->ps_sigonstack, sig)) 649 oact->sa_flags |= SA_ONSTACK; 650 if (!SIGISMEMBER(ps->ps_sigintr, sig)) 651 oact->sa_flags |= SA_RESTART; 652 if (SIGISMEMBER(ps->ps_sigreset, sig)) 653 oact->sa_flags |= SA_RESETHAND; 654 if (SIGISMEMBER(ps->ps_signodefer, sig)) 655 oact->sa_flags |= SA_NODEFER; 656 if (SIGISMEMBER(ps->ps_siginfo, sig)) 657 oact->sa_flags |= SA_SIGINFO; 658 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP) 659 oact->sa_flags |= SA_NOCLDSTOP; 660 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT) 661 oact->sa_flags |= SA_NOCLDWAIT; 662 } 663 if (act) { 664 if ((sig == SIGKILL || sig == SIGSTOP) && 665 act->sa_handler != SIG_DFL) { 666 mtx_unlock(&ps->ps_mtx); 667 PROC_UNLOCK(p); 668 return (EINVAL); 669 } 670 671 /* 672 * Change setting atomically. 673 */ 674 675 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask; 676 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]); 677 if (act->sa_flags & SA_SIGINFO) { 678 ps->ps_sigact[_SIG_IDX(sig)] = 679 (__sighandler_t *)act->sa_sigaction; 680 SIGADDSET(ps->ps_siginfo, sig); 681 } else { 682 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler; 683 SIGDELSET(ps->ps_siginfo, sig); 684 } 685 if (!(act->sa_flags & SA_RESTART)) 686 SIGADDSET(ps->ps_sigintr, sig); 687 else 688 SIGDELSET(ps->ps_sigintr, sig); 689 if (act->sa_flags & SA_ONSTACK) 690 SIGADDSET(ps->ps_sigonstack, sig); 691 else 692 SIGDELSET(ps->ps_sigonstack, sig); 693 if (act->sa_flags & SA_RESETHAND) 694 SIGADDSET(ps->ps_sigreset, sig); 695 else 696 SIGDELSET(ps->ps_sigreset, sig); 697 if (act->sa_flags & SA_NODEFER) 698 SIGADDSET(ps->ps_signodefer, sig); 699 else 700 SIGDELSET(ps->ps_signodefer, sig); 701 if (sig == SIGCHLD) { 702 if (act->sa_flags & SA_NOCLDSTOP) 703 ps->ps_flag |= PS_NOCLDSTOP; 704 else 705 ps->ps_flag &= ~PS_NOCLDSTOP; 706 if (act->sa_flags & SA_NOCLDWAIT) { 707 /* 708 * Paranoia: since SA_NOCLDWAIT is implemented 709 * by reparenting the dying child to PID 1 (and 710 * trust it to reap the zombie), PID 1 itself 711 * is forbidden to set SA_NOCLDWAIT. 712 */ 713 if (p->p_pid == 1) 714 ps->ps_flag &= ~PS_NOCLDWAIT; 715 else 716 ps->ps_flag |= PS_NOCLDWAIT; 717 } else 718 ps->ps_flag &= ~PS_NOCLDWAIT; 719 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 720 ps->ps_flag |= PS_CLDSIGIGN; 721 else 722 ps->ps_flag &= ~PS_CLDSIGIGN; 723 } 724 /* 725 * Set bit in ps_sigignore for signals that are set to SIG_IGN, 726 * and for signals set to SIG_DFL where the default is to 727 * ignore. However, don't put SIGCONT in ps_sigignore, as we 728 * have to restart the process. 729 */ 730 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 731 (sigprop(sig) & SA_IGNORE && 732 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) { 733 if ((p->p_flag & P_SA) && 734 SIGISMEMBER(p->p_sigqueue.sq_signals, sig)) { 735 p->p_flag |= P_SIGEVENT; 736 wakeup(&p->p_siglist); 737 } 738 /* never to be seen again */ 739 sigqueue_delete_proc(p, sig); 740 if (sig != SIGCONT) 741 /* easier in psignal */ 742 SIGADDSET(ps->ps_sigignore, sig); 743 SIGDELSET(ps->ps_sigcatch, sig); 744 } else { 745 SIGDELSET(ps->ps_sigignore, sig); 746 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL) 747 SIGDELSET(ps->ps_sigcatch, sig); 748 else 749 SIGADDSET(ps->ps_sigcatch, sig); 750 } 751 #ifdef COMPAT_FREEBSD4 752 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 753 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 754 (flags & KSA_FREEBSD4) == 0) 755 SIGDELSET(ps->ps_freebsd4, sig); 756 else 757 SIGADDSET(ps->ps_freebsd4, sig); 758 #endif 759 #ifdef COMPAT_43 760 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN || 761 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || 762 (flags & KSA_OSIGSET) == 0) 763 SIGDELSET(ps->ps_osigset, sig); 764 else 765 SIGADDSET(ps->ps_osigset, sig); 766 #endif 767 } 768 mtx_unlock(&ps->ps_mtx); 769 PROC_UNLOCK(p); 770 return (0); 771 } 772 773 #ifndef _SYS_SYSPROTO_H_ 774 struct sigaction_args { 775 int sig; 776 struct sigaction *act; 777 struct sigaction *oact; 778 }; 779 #endif 780 /* 781 * MPSAFE 782 */ 783 int 784 sigaction(td, uap) 785 struct thread *td; 786 register struct sigaction_args *uap; 787 { 788 struct sigaction act, oact; 789 register struct sigaction *actp, *oactp; 790 int error; 791 792 actp = (uap->act != NULL) ? &act : NULL; 793 oactp = (uap->oact != NULL) ? &oact : NULL; 794 if (actp) { 795 error = copyin(uap->act, actp, sizeof(act)); 796 if (error) 797 return (error); 798 } 799 error = kern_sigaction(td, uap->sig, actp, oactp, 0); 800 if (oactp && !error) 801 error = copyout(oactp, uap->oact, sizeof(oact)); 802 return (error); 803 } 804 805 #ifdef COMPAT_FREEBSD4 806 #ifndef _SYS_SYSPROTO_H_ 807 struct freebsd4_sigaction_args { 808 int sig; 809 struct sigaction *act; 810 struct sigaction *oact; 811 }; 812 #endif 813 /* 814 * MPSAFE 815 */ 816 int 817 freebsd4_sigaction(td, uap) 818 struct thread *td; 819 register struct freebsd4_sigaction_args *uap; 820 { 821 struct sigaction act, oact; 822 register struct sigaction *actp, *oactp; 823 int error; 824 825 826 actp = (uap->act != NULL) ? &act : NULL; 827 oactp = (uap->oact != NULL) ? &oact : NULL; 828 if (actp) { 829 error = copyin(uap->act, actp, sizeof(act)); 830 if (error) 831 return (error); 832 } 833 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4); 834 if (oactp && !error) 835 error = copyout(oactp, uap->oact, sizeof(oact)); 836 return (error); 837 } 838 #endif /* COMAPT_FREEBSD4 */ 839 840 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 841 #ifndef _SYS_SYSPROTO_H_ 842 struct osigaction_args { 843 int signum; 844 struct osigaction *nsa; 845 struct osigaction *osa; 846 }; 847 #endif 848 /* 849 * MPSAFE 850 */ 851 int 852 osigaction(td, uap) 853 struct thread *td; 854 register struct osigaction_args *uap; 855 { 856 struct osigaction sa; 857 struct sigaction nsa, osa; 858 register struct sigaction *nsap, *osap; 859 int error; 860 861 if (uap->signum <= 0 || uap->signum >= ONSIG) 862 return (EINVAL); 863 864 nsap = (uap->nsa != NULL) ? &nsa : NULL; 865 osap = (uap->osa != NULL) ? &osa : NULL; 866 867 if (nsap) { 868 error = copyin(uap->nsa, &sa, sizeof(sa)); 869 if (error) 870 return (error); 871 nsap->sa_handler = sa.sa_handler; 872 nsap->sa_flags = sa.sa_flags; 873 OSIG2SIG(sa.sa_mask, nsap->sa_mask); 874 } 875 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 876 if (osap && !error) { 877 sa.sa_handler = osap->sa_handler; 878 sa.sa_flags = osap->sa_flags; 879 SIG2OSIG(osap->sa_mask, sa.sa_mask); 880 error = copyout(&sa, uap->osa, sizeof(sa)); 881 } 882 return (error); 883 } 884 885 #if !defined(__i386__) && !defined(__alpha__) 886 /* Avoid replicating the same stub everywhere */ 887 int 888 osigreturn(td, uap) 889 struct thread *td; 890 struct osigreturn_args *uap; 891 { 892 893 return (nosys(td, (struct nosys_args *)uap)); 894 } 895 #endif 896 #endif /* COMPAT_43 */ 897 898 /* 899 * Initialize signal state for process 0; 900 * set to ignore signals that are ignored by default. 901 */ 902 void 903 siginit(p) 904 struct proc *p; 905 { 906 register int i; 907 struct sigacts *ps; 908 909 PROC_LOCK(p); 910 ps = p->p_sigacts; 911 mtx_lock(&ps->ps_mtx); 912 for (i = 1; i <= NSIG; i++) 913 if (sigprop(i) & SA_IGNORE && i != SIGCONT) 914 SIGADDSET(ps->ps_sigignore, i); 915 mtx_unlock(&ps->ps_mtx); 916 PROC_UNLOCK(p); 917 } 918 919 /* 920 * Reset signals for an exec of the specified process. 921 */ 922 void 923 execsigs(struct proc *p) 924 { 925 struct sigacts *ps; 926 int sig; 927 struct thread *td; 928 929 /* 930 * Reset caught signals. Held signals remain held 931 * through td_sigmask (unless they were caught, 932 * and are now ignored by default). 933 */ 934 PROC_LOCK_ASSERT(p, MA_OWNED); 935 td = FIRST_THREAD_IN_PROC(p); 936 ps = p->p_sigacts; 937 mtx_lock(&ps->ps_mtx); 938 while (SIGNOTEMPTY(ps->ps_sigcatch)) { 939 sig = sig_ffs(&ps->ps_sigcatch); 940 SIGDELSET(ps->ps_sigcatch, sig); 941 if (sigprop(sig) & SA_IGNORE) { 942 if (sig != SIGCONT) 943 SIGADDSET(ps->ps_sigignore, sig); 944 sigqueue_delete_proc(p, sig); 945 } 946 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 947 } 948 /* 949 * Reset stack state to the user stack. 950 * Clear set of signals caught on the signal stack. 951 */ 952 td->td_sigstk.ss_flags = SS_DISABLE; 953 td->td_sigstk.ss_size = 0; 954 td->td_sigstk.ss_sp = 0; 955 td->td_pflags &= ~TDP_ALTSTACK; 956 /* 957 * Reset no zombies if child dies flag as Solaris does. 958 */ 959 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 960 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 961 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 962 mtx_unlock(&ps->ps_mtx); 963 } 964 965 /* 966 * kern_sigprocmask() 967 * 968 * Manipulate signal mask. 969 */ 970 int 971 kern_sigprocmask(td, how, set, oset, old) 972 struct thread *td; 973 int how; 974 sigset_t *set, *oset; 975 int old; 976 { 977 int error; 978 979 PROC_LOCK(td->td_proc); 980 if (oset != NULL) 981 *oset = td->td_sigmask; 982 983 error = 0; 984 if (set != NULL) { 985 switch (how) { 986 case SIG_BLOCK: 987 SIG_CANTMASK(*set); 988 SIGSETOR(td->td_sigmask, *set); 989 break; 990 case SIG_UNBLOCK: 991 SIGSETNAND(td->td_sigmask, *set); 992 signotify(td); 993 break; 994 case SIG_SETMASK: 995 SIG_CANTMASK(*set); 996 if (old) 997 SIGSETLO(td->td_sigmask, *set); 998 else 999 td->td_sigmask = *set; 1000 signotify(td); 1001 break; 1002 default: 1003 error = EINVAL; 1004 break; 1005 } 1006 } 1007 PROC_UNLOCK(td->td_proc); 1008 return (error); 1009 } 1010 1011 /* 1012 * sigprocmask() - MP SAFE 1013 */ 1014 1015 #ifndef _SYS_SYSPROTO_H_ 1016 struct sigprocmask_args { 1017 int how; 1018 const sigset_t *set; 1019 sigset_t *oset; 1020 }; 1021 #endif 1022 int 1023 sigprocmask(td, uap) 1024 register struct thread *td; 1025 struct sigprocmask_args *uap; 1026 { 1027 sigset_t set, oset; 1028 sigset_t *setp, *osetp; 1029 int error; 1030 1031 setp = (uap->set != NULL) ? &set : NULL; 1032 osetp = (uap->oset != NULL) ? &oset : NULL; 1033 if (setp) { 1034 error = copyin(uap->set, setp, sizeof(set)); 1035 if (error) 1036 return (error); 1037 } 1038 error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 1039 if (osetp && !error) { 1040 error = copyout(osetp, uap->oset, sizeof(oset)); 1041 } 1042 return (error); 1043 } 1044 1045 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1046 /* 1047 * osigprocmask() - MP SAFE 1048 */ 1049 #ifndef _SYS_SYSPROTO_H_ 1050 struct osigprocmask_args { 1051 int how; 1052 osigset_t mask; 1053 }; 1054 #endif 1055 int 1056 osigprocmask(td, uap) 1057 register struct thread *td; 1058 struct osigprocmask_args *uap; 1059 { 1060 sigset_t set, oset; 1061 int error; 1062 1063 OSIG2SIG(uap->mask, set); 1064 error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1065 SIG2OSIG(oset, td->td_retval[0]); 1066 return (error); 1067 } 1068 #endif /* COMPAT_43 */ 1069 1070 /* 1071 * MPSAFE 1072 */ 1073 int 1074 sigwait(struct thread *td, struct sigwait_args *uap) 1075 { 1076 ksiginfo_t ksi; 1077 sigset_t set; 1078 int error; 1079 1080 error = copyin(uap->set, &set, sizeof(set)); 1081 if (error) { 1082 td->td_retval[0] = error; 1083 return (0); 1084 } 1085 1086 error = kern_sigtimedwait(td, set, &ksi, NULL); 1087 if (error) { 1088 if (error == ERESTART) 1089 return (error); 1090 td->td_retval[0] = error; 1091 return (0); 1092 } 1093 1094 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 1095 td->td_retval[0] = error; 1096 return (0); 1097 } 1098 /* 1099 * MPSAFE 1100 */ 1101 int 1102 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1103 { 1104 struct timespec ts; 1105 struct timespec *timeout; 1106 sigset_t set; 1107 ksiginfo_t ksi; 1108 int error; 1109 1110 if (uap->timeout) { 1111 error = copyin(uap->timeout, &ts, sizeof(ts)); 1112 if (error) 1113 return (error); 1114 1115 timeout = &ts; 1116 } else 1117 timeout = NULL; 1118 1119 error = copyin(uap->set, &set, sizeof(set)); 1120 if (error) 1121 return (error); 1122 1123 error = kern_sigtimedwait(td, set, &ksi, timeout); 1124 if (error) 1125 return (error); 1126 1127 if (uap->info) 1128 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1129 1130 if (error == 0) 1131 td->td_retval[0] = ksi.ksi_signo; 1132 return (error); 1133 } 1134 1135 /* 1136 * MPSAFE 1137 */ 1138 int 1139 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1140 { 1141 ksiginfo_t ksi; 1142 sigset_t set; 1143 int error; 1144 1145 error = copyin(uap->set, &set, sizeof(set)); 1146 if (error) 1147 return (error); 1148 1149 error = kern_sigtimedwait(td, set, &ksi, NULL); 1150 if (error) 1151 return (error); 1152 1153 if (uap->info) 1154 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1155 1156 if (error == 0) 1157 td->td_retval[0] = ksi.ksi_signo; 1158 return (error); 1159 } 1160 1161 static int 1162 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1163 struct timespec *timeout) 1164 { 1165 struct sigacts *ps; 1166 sigset_t savedmask; 1167 struct proc *p; 1168 int error, sig, hz, i, timevalid = 0; 1169 struct timespec rts, ets, ts; 1170 struct timeval tv; 1171 1172 p = td->td_proc; 1173 error = 0; 1174 sig = 0; 1175 SIG_CANTMASK(waitset); 1176 1177 PROC_LOCK(p); 1178 ps = p->p_sigacts; 1179 savedmask = td->td_sigmask; 1180 if (timeout) { 1181 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 1182 timevalid = 1; 1183 getnanouptime(&rts); 1184 ets = rts; 1185 timespecadd(&ets, timeout); 1186 } 1187 } 1188 1189 again: 1190 for (i = 1; i <= _SIG_MAXSIG; ++i) { 1191 if (!SIGISMEMBER(waitset, i)) 1192 continue; 1193 if (SIGISMEMBER(td->td_sigqueue.sq_signals, i)) { 1194 SIGFILLSET(td->td_sigmask); 1195 SIG_CANTMASK(td->td_sigmask); 1196 SIGDELSET(td->td_sigmask, i); 1197 mtx_lock(&ps->ps_mtx); 1198 sig = cursig(td); 1199 i = 0; 1200 mtx_unlock(&ps->ps_mtx); 1201 } else if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) { 1202 if (p->p_flag & P_SA) { 1203 p->p_flag |= P_SIGEVENT; 1204 wakeup(&p->p_siglist); 1205 } 1206 sigqueue_move(&p->p_sigqueue, &td->td_sigqueue, i); 1207 SIGFILLSET(td->td_sigmask); 1208 SIG_CANTMASK(td->td_sigmask); 1209 SIGDELSET(td->td_sigmask, i); 1210 mtx_lock(&ps->ps_mtx); 1211 sig = cursig(td); 1212 i = 0; 1213 mtx_unlock(&ps->ps_mtx); 1214 } 1215 if (sig) 1216 goto out; 1217 } 1218 if (error) 1219 goto out; 1220 1221 /* 1222 * POSIX says this must be checked after looking for pending 1223 * signals. 1224 */ 1225 if (timeout) { 1226 if (!timevalid) { 1227 error = EINVAL; 1228 goto out; 1229 } 1230 getnanouptime(&rts); 1231 if (timespeccmp(&rts, &ets, >=)) { 1232 error = EAGAIN; 1233 goto out; 1234 } 1235 ts = ets; 1236 timespecsub(&ts, &rts); 1237 TIMESPEC_TO_TIMEVAL(&tv, &ts); 1238 hz = tvtohz(&tv); 1239 } else 1240 hz = 0; 1241 1242 td->td_sigmask = savedmask; 1243 SIGSETNAND(td->td_sigmask, waitset); 1244 signotify(td); 1245 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz); 1246 if (timeout) { 1247 if (error == ERESTART) { 1248 /* timeout can not be restarted. */ 1249 error = EINTR; 1250 } else if (error == EAGAIN) { 1251 /* will calculate timeout by ourself. */ 1252 error = 0; 1253 } 1254 } 1255 goto again; 1256 1257 out: 1258 if (sig) { 1259 sig_t action; 1260 1261 ksiginfo_init(ksi); 1262 sigqueue_get(&td->td_sigqueue, sig, ksi); 1263 ksi->ksi_signo = sig; 1264 if (ksi->ksi_code == SI_TIMER) 1265 itimer_accept(p, ksi->ksi_timerid, ksi); 1266 error = 0; 1267 mtx_lock(&ps->ps_mtx); 1268 action = ps->ps_sigact[_SIG_IDX(sig)]; 1269 mtx_unlock(&ps->ps_mtx); 1270 #ifdef KTRACE 1271 if (KTRPOINT(td, KTR_PSIG)) 1272 ktrpsig(sig, action, &td->td_sigmask, 0); 1273 #endif 1274 _STOPEVENT(p, S_SIG, sig); 1275 1276 } 1277 td->td_sigmask = savedmask; 1278 signotify(td); 1279 PROC_UNLOCK(p); 1280 return (error); 1281 } 1282 1283 #ifndef _SYS_SYSPROTO_H_ 1284 struct sigpending_args { 1285 sigset_t *set; 1286 }; 1287 #endif 1288 /* 1289 * MPSAFE 1290 */ 1291 int 1292 sigpending(td, uap) 1293 struct thread *td; 1294 struct sigpending_args *uap; 1295 { 1296 struct proc *p = td->td_proc; 1297 sigset_t pending; 1298 1299 PROC_LOCK(p); 1300 pending = p->p_sigqueue.sq_signals; 1301 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1302 PROC_UNLOCK(p); 1303 return (copyout(&pending, uap->set, sizeof(sigset_t))); 1304 } 1305 1306 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1307 #ifndef _SYS_SYSPROTO_H_ 1308 struct osigpending_args { 1309 int dummy; 1310 }; 1311 #endif 1312 /* 1313 * MPSAFE 1314 */ 1315 int 1316 osigpending(td, uap) 1317 struct thread *td; 1318 struct osigpending_args *uap; 1319 { 1320 struct proc *p = td->td_proc; 1321 sigset_t pending; 1322 1323 PROC_LOCK(p); 1324 pending = p->p_sigqueue.sq_signals; 1325 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1326 PROC_UNLOCK(p); 1327 SIG2OSIG(pending, td->td_retval[0]); 1328 return (0); 1329 } 1330 #endif /* COMPAT_43 */ 1331 1332 #if defined(COMPAT_43) 1333 /* 1334 * Generalized interface signal handler, 4.3-compatible. 1335 */ 1336 #ifndef _SYS_SYSPROTO_H_ 1337 struct osigvec_args { 1338 int signum; 1339 struct sigvec *nsv; 1340 struct sigvec *osv; 1341 }; 1342 #endif 1343 /* 1344 * MPSAFE 1345 */ 1346 /* ARGSUSED */ 1347 int 1348 osigvec(td, uap) 1349 struct thread *td; 1350 register struct osigvec_args *uap; 1351 { 1352 struct sigvec vec; 1353 struct sigaction nsa, osa; 1354 register struct sigaction *nsap, *osap; 1355 int error; 1356 1357 if (uap->signum <= 0 || uap->signum >= ONSIG) 1358 return (EINVAL); 1359 nsap = (uap->nsv != NULL) ? &nsa : NULL; 1360 osap = (uap->osv != NULL) ? &osa : NULL; 1361 if (nsap) { 1362 error = copyin(uap->nsv, &vec, sizeof(vec)); 1363 if (error) 1364 return (error); 1365 nsap->sa_handler = vec.sv_handler; 1366 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 1367 nsap->sa_flags = vec.sv_flags; 1368 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1369 } 1370 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1371 if (osap && !error) { 1372 vec.sv_handler = osap->sa_handler; 1373 SIG2OSIG(osap->sa_mask, vec.sv_mask); 1374 vec.sv_flags = osap->sa_flags; 1375 vec.sv_flags &= ~SA_NOCLDWAIT; 1376 vec.sv_flags ^= SA_RESTART; 1377 error = copyout(&vec, uap->osv, sizeof(vec)); 1378 } 1379 return (error); 1380 } 1381 1382 #ifndef _SYS_SYSPROTO_H_ 1383 struct osigblock_args { 1384 int mask; 1385 }; 1386 #endif 1387 /* 1388 * MPSAFE 1389 */ 1390 int 1391 osigblock(td, uap) 1392 register struct thread *td; 1393 struct osigblock_args *uap; 1394 { 1395 struct proc *p = td->td_proc; 1396 sigset_t set; 1397 1398 OSIG2SIG(uap->mask, set); 1399 SIG_CANTMASK(set); 1400 PROC_LOCK(p); 1401 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1402 SIGSETOR(td->td_sigmask, set); 1403 PROC_UNLOCK(p); 1404 return (0); 1405 } 1406 1407 #ifndef _SYS_SYSPROTO_H_ 1408 struct osigsetmask_args { 1409 int mask; 1410 }; 1411 #endif 1412 /* 1413 * MPSAFE 1414 */ 1415 int 1416 osigsetmask(td, uap) 1417 struct thread *td; 1418 struct osigsetmask_args *uap; 1419 { 1420 struct proc *p = td->td_proc; 1421 sigset_t set; 1422 1423 OSIG2SIG(uap->mask, set); 1424 SIG_CANTMASK(set); 1425 PROC_LOCK(p); 1426 SIG2OSIG(td->td_sigmask, td->td_retval[0]); 1427 SIGSETLO(td->td_sigmask, set); 1428 signotify(td); 1429 PROC_UNLOCK(p); 1430 return (0); 1431 } 1432 #endif /* COMPAT_43 */ 1433 1434 /* 1435 * Suspend calling thread until signal, providing mask to be set 1436 * in the meantime. 1437 */ 1438 #ifndef _SYS_SYSPROTO_H_ 1439 struct sigsuspend_args { 1440 const sigset_t *sigmask; 1441 }; 1442 #endif 1443 /* 1444 * MPSAFE 1445 */ 1446 /* ARGSUSED */ 1447 int 1448 sigsuspend(td, uap) 1449 struct thread *td; 1450 struct sigsuspend_args *uap; 1451 { 1452 sigset_t mask; 1453 int error; 1454 1455 error = copyin(uap->sigmask, &mask, sizeof(mask)); 1456 if (error) 1457 return (error); 1458 return (kern_sigsuspend(td, mask)); 1459 } 1460 1461 int 1462 kern_sigsuspend(struct thread *td, sigset_t mask) 1463 { 1464 struct proc *p = td->td_proc; 1465 1466 /* 1467 * When returning from sigsuspend, we want 1468 * the old mask to be restored after the 1469 * signal handler has finished. Thus, we 1470 * save it here and mark the sigacts structure 1471 * to indicate this. 1472 */ 1473 PROC_LOCK(p); 1474 td->td_oldsigmask = td->td_sigmask; 1475 td->td_pflags |= TDP_OLDMASK; 1476 SIG_CANTMASK(mask); 1477 td->td_sigmask = mask; 1478 signotify(td); 1479 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0) 1480 /* void */; 1481 PROC_UNLOCK(p); 1482 /* always return EINTR rather than ERESTART... */ 1483 return (EINTR); 1484 } 1485 1486 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1487 /* 1488 * Compatibility sigsuspend call for old binaries. Note nonstandard calling 1489 * convention: libc stub passes mask, not pointer, to save a copyin. 1490 */ 1491 #ifndef _SYS_SYSPROTO_H_ 1492 struct osigsuspend_args { 1493 osigset_t mask; 1494 }; 1495 #endif 1496 /* 1497 * MPSAFE 1498 */ 1499 /* ARGSUSED */ 1500 int 1501 osigsuspend(td, uap) 1502 struct thread *td; 1503 struct osigsuspend_args *uap; 1504 { 1505 struct proc *p = td->td_proc; 1506 sigset_t mask; 1507 1508 PROC_LOCK(p); 1509 td->td_oldsigmask = td->td_sigmask; 1510 td->td_pflags |= TDP_OLDMASK; 1511 OSIG2SIG(uap->mask, mask); 1512 SIG_CANTMASK(mask); 1513 SIGSETLO(td->td_sigmask, mask); 1514 signotify(td); 1515 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0) 1516 /* void */; 1517 PROC_UNLOCK(p); 1518 /* always return EINTR rather than ERESTART... */ 1519 return (EINTR); 1520 } 1521 #endif /* COMPAT_43 */ 1522 1523 #if defined(COMPAT_43) 1524 #ifndef _SYS_SYSPROTO_H_ 1525 struct osigstack_args { 1526 struct sigstack *nss; 1527 struct sigstack *oss; 1528 }; 1529 #endif 1530 /* 1531 * MPSAFE 1532 */ 1533 /* ARGSUSED */ 1534 int 1535 osigstack(td, uap) 1536 struct thread *td; 1537 register struct osigstack_args *uap; 1538 { 1539 struct sigstack nss, oss; 1540 int error = 0; 1541 1542 if (uap->nss != NULL) { 1543 error = copyin(uap->nss, &nss, sizeof(nss)); 1544 if (error) 1545 return (error); 1546 } 1547 oss.ss_sp = td->td_sigstk.ss_sp; 1548 oss.ss_onstack = sigonstack(cpu_getstack(td)); 1549 if (uap->nss != NULL) { 1550 td->td_sigstk.ss_sp = nss.ss_sp; 1551 td->td_sigstk.ss_size = 0; 1552 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1553 td->td_pflags |= TDP_ALTSTACK; 1554 } 1555 if (uap->oss != NULL) 1556 error = copyout(&oss, uap->oss, sizeof(oss)); 1557 1558 return (error); 1559 } 1560 #endif /* COMPAT_43 */ 1561 1562 #ifndef _SYS_SYSPROTO_H_ 1563 struct sigaltstack_args { 1564 stack_t *ss; 1565 stack_t *oss; 1566 }; 1567 #endif 1568 /* 1569 * MPSAFE 1570 */ 1571 /* ARGSUSED */ 1572 int 1573 sigaltstack(td, uap) 1574 struct thread *td; 1575 register struct sigaltstack_args *uap; 1576 { 1577 stack_t ss, oss; 1578 int error; 1579 1580 if (uap->ss != NULL) { 1581 error = copyin(uap->ss, &ss, sizeof(ss)); 1582 if (error) 1583 return (error); 1584 } 1585 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 1586 (uap->oss != NULL) ? &oss : NULL); 1587 if (error) 1588 return (error); 1589 if (uap->oss != NULL) 1590 error = copyout(&oss, uap->oss, sizeof(stack_t)); 1591 return (error); 1592 } 1593 1594 int 1595 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 1596 { 1597 struct proc *p = td->td_proc; 1598 int oonstack; 1599 1600 oonstack = sigonstack(cpu_getstack(td)); 1601 1602 if (oss != NULL) { 1603 *oss = td->td_sigstk; 1604 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1605 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1606 } 1607 1608 if (ss != NULL) { 1609 if (oonstack) 1610 return (EPERM); 1611 if ((ss->ss_flags & ~SS_DISABLE) != 0) 1612 return (EINVAL); 1613 if (!(ss->ss_flags & SS_DISABLE)) { 1614 if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1615 return (ENOMEM); 1616 1617 td->td_sigstk = *ss; 1618 td->td_pflags |= TDP_ALTSTACK; 1619 } else { 1620 td->td_pflags &= ~TDP_ALTSTACK; 1621 } 1622 } 1623 return (0); 1624 } 1625 1626 /* 1627 * Common code for kill process group/broadcast kill. 1628 * cp is calling process. 1629 */ 1630 static int 1631 killpg1(td, sig, pgid, all) 1632 register struct thread *td; 1633 int sig, pgid, all; 1634 { 1635 register struct proc *p; 1636 struct pgrp *pgrp; 1637 int nfound = 0; 1638 1639 if (all) { 1640 /* 1641 * broadcast 1642 */ 1643 sx_slock(&allproc_lock); 1644 LIST_FOREACH(p, &allproc, p_list) { 1645 PROC_LOCK(p); 1646 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 1647 p == td->td_proc) { 1648 PROC_UNLOCK(p); 1649 continue; 1650 } 1651 if (p_cansignal(td, p, sig) == 0) { 1652 nfound++; 1653 if (sig) 1654 psignal(p, sig); 1655 } 1656 PROC_UNLOCK(p); 1657 } 1658 sx_sunlock(&allproc_lock); 1659 } else { 1660 sx_slock(&proctree_lock); 1661 if (pgid == 0) { 1662 /* 1663 * zero pgid means send to my process group. 1664 */ 1665 pgrp = td->td_proc->p_pgrp; 1666 PGRP_LOCK(pgrp); 1667 } else { 1668 pgrp = pgfind(pgid); 1669 if (pgrp == NULL) { 1670 sx_sunlock(&proctree_lock); 1671 return (ESRCH); 1672 } 1673 } 1674 sx_sunlock(&proctree_lock); 1675 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1676 PROC_LOCK(p); 1677 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) { 1678 PROC_UNLOCK(p); 1679 continue; 1680 } 1681 if (p_cansignal(td, p, sig) == 0) { 1682 nfound++; 1683 if (sig) 1684 psignal(p, sig); 1685 } 1686 PROC_UNLOCK(p); 1687 } 1688 PGRP_UNLOCK(pgrp); 1689 } 1690 return (nfound ? 0 : ESRCH); 1691 } 1692 1693 #ifndef _SYS_SYSPROTO_H_ 1694 struct kill_args { 1695 int pid; 1696 int signum; 1697 }; 1698 #endif 1699 /* 1700 * MPSAFE 1701 */ 1702 /* ARGSUSED */ 1703 int 1704 kill(td, uap) 1705 register struct thread *td; 1706 register struct kill_args *uap; 1707 { 1708 register struct proc *p; 1709 int error; 1710 1711 if ((u_int)uap->signum > _SIG_MAXSIG) 1712 return (EINVAL); 1713 1714 if (uap->pid > 0) { 1715 /* kill single process */ 1716 if ((p = pfind(uap->pid)) == NULL) { 1717 if ((p = zpfind(uap->pid)) == NULL) 1718 return (ESRCH); 1719 } 1720 error = p_cansignal(td, p, uap->signum); 1721 if (error == 0 && uap->signum) 1722 psignal(p, uap->signum); 1723 PROC_UNLOCK(p); 1724 return (error); 1725 } 1726 switch (uap->pid) { 1727 case -1: /* broadcast signal */ 1728 return (killpg1(td, uap->signum, 0, 1)); 1729 case 0: /* signal own process group */ 1730 return (killpg1(td, uap->signum, 0, 0)); 1731 default: /* negative explicit process group */ 1732 return (killpg1(td, uap->signum, -uap->pid, 0)); 1733 } 1734 /* NOTREACHED */ 1735 } 1736 1737 #if defined(COMPAT_43) 1738 #ifndef _SYS_SYSPROTO_H_ 1739 struct okillpg_args { 1740 int pgid; 1741 int signum; 1742 }; 1743 #endif 1744 /* 1745 * MPSAFE 1746 */ 1747 /* ARGSUSED */ 1748 int 1749 okillpg(td, uap) 1750 struct thread *td; 1751 register struct okillpg_args *uap; 1752 { 1753 1754 if ((u_int)uap->signum > _SIG_MAXSIG) 1755 return (EINVAL); 1756 1757 return (killpg1(td, uap->signum, uap->pgid, 0)); 1758 } 1759 #endif /* COMPAT_43 */ 1760 1761 #ifndef _SYS_SYSPROTO_H_ 1762 struct sigqueue_args { 1763 pid_t pid; 1764 int signum; 1765 /* union sigval */ void *value; 1766 }; 1767 #endif 1768 1769 int 1770 sigqueue(struct thread *td, struct sigqueue_args *uap) 1771 { 1772 ksiginfo_t ksi; 1773 struct proc *p; 1774 int error; 1775 1776 if ((u_int)uap->signum > _SIG_MAXSIG) 1777 return (EINVAL); 1778 1779 /* 1780 * Specification says sigqueue can only send signal to 1781 * single process. 1782 */ 1783 if (uap->pid <= 0) 1784 return (EINVAL); 1785 1786 if ((p = pfind(uap->pid)) == NULL) { 1787 if ((p = zpfind(uap->pid)) == NULL) 1788 return (ESRCH); 1789 } 1790 error = p_cansignal(td, p, uap->signum); 1791 if (error == 0 && uap->signum != 0) { 1792 ksiginfo_init(&ksi); 1793 ksi.ksi_signo = uap->signum; 1794 ksi.ksi_code = SI_QUEUE; 1795 ksi.ksi_pid = td->td_proc->p_pid; 1796 ksi.ksi_uid = td->td_ucred->cr_ruid; 1797 ksi.ksi_value.sival_ptr = uap->value; 1798 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi); 1799 } 1800 PROC_UNLOCK(p); 1801 return (error); 1802 } 1803 1804 /* 1805 * Send a signal to a process group. 1806 */ 1807 void 1808 gsignal(pgid, sig) 1809 int pgid, sig; 1810 { 1811 struct pgrp *pgrp; 1812 1813 if (pgid != 0) { 1814 sx_slock(&proctree_lock); 1815 pgrp = pgfind(pgid); 1816 sx_sunlock(&proctree_lock); 1817 if (pgrp != NULL) { 1818 pgsignal(pgrp, sig, 0); 1819 PGRP_UNLOCK(pgrp); 1820 } 1821 } 1822 } 1823 1824 /* 1825 * Send a signal to a process group. If checktty is 1, 1826 * limit to members which have a controlling terminal. 1827 */ 1828 void 1829 pgsignal(pgrp, sig, checkctty) 1830 struct pgrp *pgrp; 1831 int sig, checkctty; 1832 { 1833 register struct proc *p; 1834 1835 if (pgrp) { 1836 PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1837 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1838 PROC_LOCK(p); 1839 if (checkctty == 0 || p->p_flag & P_CONTROLT) 1840 psignal(p, sig); 1841 PROC_UNLOCK(p); 1842 } 1843 } 1844 } 1845 1846 /* 1847 * Send a signal caused by a trap to the current thread. 1848 * If it will be caught immediately, deliver it with correct code. 1849 * Otherwise, post it normally. 1850 * 1851 * MPSAFE 1852 */ 1853 void 1854 trapsignal(struct thread *td, ksiginfo_t *ksi) 1855 { 1856 struct sigacts *ps; 1857 struct proc *p; 1858 int error; 1859 int sig; 1860 int code; 1861 1862 p = td->td_proc; 1863 sig = ksi->ksi_signo; 1864 code = ksi->ksi_code; 1865 KASSERT(_SIG_VALID(sig), ("invalid signal")); 1866 1867 if (td->td_pflags & TDP_SA) { 1868 if (td->td_mailbox == NULL) 1869 thread_user_enter(td); 1870 PROC_LOCK(p); 1871 SIGDELSET(td->td_sigmask, sig); 1872 mtx_lock_spin(&sched_lock); 1873 /* 1874 * Force scheduling an upcall, so UTS has chance to 1875 * process the signal before thread runs again in 1876 * userland. 1877 */ 1878 if (td->td_upcall) 1879 td->td_upcall->ku_flags |= KUF_DOUPCALL; 1880 mtx_unlock_spin(&sched_lock); 1881 } else { 1882 PROC_LOCK(p); 1883 } 1884 ps = p->p_sigacts; 1885 mtx_lock(&ps->ps_mtx); 1886 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 1887 !SIGISMEMBER(td->td_sigmask, sig)) { 1888 p->p_stats->p_ru.ru_nsignals++; 1889 #ifdef KTRACE 1890 if (KTRPOINT(curthread, KTR_PSIG)) 1891 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 1892 &td->td_sigmask, code); 1893 #endif 1894 if (!(td->td_pflags & TDP_SA)) 1895 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 1896 ksi, &td->td_sigmask); 1897 else if (td->td_mailbox == NULL) { 1898 mtx_unlock(&ps->ps_mtx); 1899 /* UTS caused a sync signal */ 1900 p->p_code = code; /* XXX for core dump/debugger */ 1901 p->p_sig = sig; /* XXX to verify code */ 1902 sigexit(td, sig); 1903 } else { 1904 mtx_unlock(&ps->ps_mtx); 1905 SIGADDSET(td->td_sigmask, sig); 1906 PROC_UNLOCK(p); 1907 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig, 1908 sizeof(siginfo_t)); 1909 PROC_LOCK(p); 1910 /* UTS memory corrupted */ 1911 if (error) 1912 sigexit(td, SIGSEGV); 1913 mtx_lock(&ps->ps_mtx); 1914 } 1915 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 1916 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1917 SIGADDSET(td->td_sigmask, sig); 1918 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 1919 /* 1920 * See kern_sigaction() for origin of this code. 1921 */ 1922 SIGDELSET(ps->ps_sigcatch, sig); 1923 if (sig != SIGCONT && 1924 sigprop(sig) & SA_IGNORE) 1925 SIGADDSET(ps->ps_sigignore, sig); 1926 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1927 } 1928 mtx_unlock(&ps->ps_mtx); 1929 } else { 1930 /* 1931 * Avoid a possible infinite loop if the thread 1932 * masking the signal or process is ignoring the 1933 * signal. 1934 */ 1935 if (kern_forcesigexit && 1936 (SIGISMEMBER(td->td_sigmask, sig) || 1937 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 1938 SIGDELSET(td->td_sigmask, sig); 1939 SIGDELSET(ps->ps_sigcatch, sig); 1940 SIGDELSET(ps->ps_sigignore, sig); 1941 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1942 } 1943 mtx_unlock(&ps->ps_mtx); 1944 p->p_code = code; /* XXX for core dump/debugger */ 1945 p->p_sig = sig; /* XXX to verify code */ 1946 tdsignal(p, td, sig, ksi); 1947 } 1948 PROC_UNLOCK(p); 1949 } 1950 1951 static struct thread * 1952 sigtd(struct proc *p, int sig, int prop) 1953 { 1954 struct thread *td, *signal_td; 1955 1956 PROC_LOCK_ASSERT(p, MA_OWNED); 1957 1958 /* 1959 * Check if current thread can handle the signal without 1960 * switching conetxt to another thread. 1961 */ 1962 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 1963 return (curthread); 1964 signal_td = NULL; 1965 mtx_lock_spin(&sched_lock); 1966 FOREACH_THREAD_IN_PROC(p, td) { 1967 if (!SIGISMEMBER(td->td_sigmask, sig)) { 1968 signal_td = td; 1969 break; 1970 } 1971 } 1972 if (signal_td == NULL) 1973 signal_td = FIRST_THREAD_IN_PROC(p); 1974 mtx_unlock_spin(&sched_lock); 1975 return (signal_td); 1976 } 1977 1978 /* 1979 * Send the signal to the process. If the signal has an action, the action 1980 * is usually performed by the target process rather than the caller; we add 1981 * the signal to the set of pending signals for the process. 1982 * 1983 * Exceptions: 1984 * o When a stop signal is sent to a sleeping process that takes the 1985 * default action, the process is stopped without awakening it. 1986 * o SIGCONT restarts stopped processes (or puts them back to sleep) 1987 * regardless of the signal action (eg, blocked or ignored). 1988 * 1989 * Other ignored signals are discarded immediately. 1990 * 1991 * MPSAFE 1992 */ 1993 void 1994 psignal(struct proc *p, int sig) 1995 { 1996 (void) tdsignal(p, NULL, sig, NULL); 1997 } 1998 1999 int 2000 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi) 2001 { 2002 struct thread *td = NULL; 2003 2004 PROC_LOCK_ASSERT(p, MA_OWNED); 2005 2006 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue")); 2007 2008 /* 2009 * ksi_code and other fields should be set before 2010 * calling this function. 2011 */ 2012 ksi->ksi_signo = sigev->sigev_signo; 2013 ksi->ksi_value = sigev->sigev_value; 2014 if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2015 td = thread_find(p, sigev->sigev_notify_thread_id); 2016 if (td == NULL) 2017 return (ESRCH); 2018 } 2019 return (tdsignal(p, td, ksi->ksi_signo, ksi)); 2020 } 2021 2022 /* 2023 * MPSAFE 2024 */ 2025 int 2026 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 2027 { 2028 sigset_t saved; 2029 int ret; 2030 2031 if (p->p_flag & P_SA) 2032 saved = p->p_sigqueue.sq_signals; 2033 ret = do_tdsignal(p, td, sig, ksi); 2034 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) { 2035 if (!SIGSETEQ(saved, p->p_sigqueue.sq_signals)) { 2036 /* pending set changed */ 2037 p->p_flag |= P_SIGEVENT; 2038 wakeup(&p->p_siglist); 2039 } 2040 } 2041 return (ret); 2042 } 2043 2044 static int 2045 do_tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 2046 { 2047 sig_t action; 2048 sigqueue_t *sigqueue; 2049 struct thread *td0; 2050 int prop; 2051 struct sigacts *ps; 2052 int ret = 0; 2053 2054 PROC_LOCK_ASSERT(p, MA_OWNED); 2055 2056 if (!_SIG_VALID(sig)) 2057 panic("do_tdsignal(): invalid signal"); 2058 2059 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("do_tdsignal: ksi on queue")); 2060 2061 /* 2062 * IEEE Std 1003.1-2001: return success when killing a zombie. 2063 */ 2064 if (p->p_state == PRS_ZOMBIE) { 2065 if (ksi && (ksi->ksi_flags & KSI_INS)) 2066 ksiginfo_tryfree(ksi); 2067 return (ret); 2068 } 2069 2070 ps = p->p_sigacts; 2071 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig); 2072 prop = sigprop(sig); 2073 2074 /* 2075 * If the signal is blocked and not destined for this thread, then 2076 * assign it to the process so that we can find it later in the first 2077 * thread that unblocks it. Otherwise, assign it to this thread now. 2078 */ 2079 if (td == NULL) { 2080 td = sigtd(p, sig, prop); 2081 if (SIGISMEMBER(td->td_sigmask, sig)) 2082 sigqueue = &p->p_sigqueue; 2083 else 2084 sigqueue = &td->td_sigqueue; 2085 } else { 2086 KASSERT(td->td_proc == p, ("invalid thread")); 2087 sigqueue = &td->td_sigqueue; 2088 } 2089 2090 /* 2091 * If the signal is being ignored, 2092 * or process is exiting or thread is exiting, 2093 * then we forget about it immediately. 2094 * (Note: we don't set SIGCONT in ps_sigignore, 2095 * and if it is set to SIG_IGN, 2096 * action will be SIG_DFL here.) 2097 */ 2098 mtx_lock(&ps->ps_mtx); 2099 if (SIGISMEMBER(ps->ps_sigignore, sig) || 2100 (p->p_flag & P_WEXIT)) { 2101 mtx_unlock(&ps->ps_mtx); 2102 if (ksi && (ksi->ksi_flags & KSI_INS)) 2103 ksiginfo_tryfree(ksi); 2104 return (ret); 2105 } 2106 if (SIGISMEMBER(td->td_sigmask, sig)) 2107 action = SIG_HOLD; 2108 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2109 action = SIG_CATCH; 2110 else 2111 action = SIG_DFL; 2112 mtx_unlock(&ps->ps_mtx); 2113 2114 if (prop & SA_CONT) 2115 sigqueue_delete_stopmask_proc(p); 2116 else if (prop & SA_STOP) { 2117 /* 2118 * If sending a tty stop signal to a member of an orphaned 2119 * process group, discard the signal here if the action 2120 * is default; don't stop the process below if sleeping, 2121 * and don't clear any pending SIGCONT. 2122 */ 2123 if ((prop & SA_TTYSTOP) && 2124 (p->p_pgrp->pg_jobc == 0) && 2125 (action == SIG_DFL)) { 2126 if (ksi && (ksi->ksi_flags & KSI_INS)) 2127 ksiginfo_tryfree(ksi); 2128 return (ret); 2129 } 2130 sigqueue_delete_proc(p, SIGCONT); 2131 if (p->p_flag & P_CONTINUED) { 2132 p->p_flag &= ~P_CONTINUED; 2133 PROC_LOCK(p->p_pptr); 2134 sigqueue_take(p->p_ksi); 2135 PROC_UNLOCK(p->p_pptr); 2136 } 2137 } 2138 2139 ret = sigqueue_add(sigqueue, sig, ksi); 2140 if (ret != 0) 2141 return (ret); 2142 signotify(td); 2143 /* 2144 * Defer further processing for signals which are held, 2145 * except that stopped processes must be continued by SIGCONT. 2146 */ 2147 if (action == SIG_HOLD && 2148 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG))) 2149 return (ret); 2150 /* 2151 * SIGKILL: Remove procfs STOPEVENTs. 2152 */ 2153 if (sig == SIGKILL) { 2154 /* from procfs_ioctl.c: PIOCBIC */ 2155 p->p_stops = 0; 2156 /* from procfs_ioctl.c: PIOCCONT */ 2157 p->p_step = 0; 2158 wakeup(&p->p_step); 2159 } 2160 /* 2161 * Some signals have a process-wide effect and a per-thread 2162 * component. Most processing occurs when the process next 2163 * tries to cross the user boundary, however there are some 2164 * times when processing needs to be done immediatly, such as 2165 * waking up threads so that they can cross the user boundary. 2166 * We try do the per-process part here. 2167 */ 2168 if (P_SHOULDSTOP(p)) { 2169 /* 2170 * The process is in stopped mode. All the threads should be 2171 * either winding down or already on the suspended queue. 2172 */ 2173 if (p->p_flag & P_TRACED) { 2174 /* 2175 * The traced process is already stopped, 2176 * so no further action is necessary. 2177 * No signal can restart us. 2178 */ 2179 goto out; 2180 } 2181 2182 if (sig == SIGKILL) { 2183 /* 2184 * SIGKILL sets process running. 2185 * It will die elsewhere. 2186 * All threads must be restarted. 2187 */ 2188 p->p_flag &= ~P_STOPPED_SIG; 2189 goto runfast; 2190 } 2191 2192 if (prop & SA_CONT) { 2193 /* 2194 * If SIGCONT is default (or ignored), we continue the 2195 * process but don't leave the signal in sigqueue as 2196 * it has no further action. If SIGCONT is held, we 2197 * continue the process and leave the signal in 2198 * sigqueue. If the process catches SIGCONT, let it 2199 * handle the signal itself. If it isn't waiting on 2200 * an event, it goes back to run state. 2201 * Otherwise, process goes back to sleep state. 2202 */ 2203 p->p_flag &= ~P_STOPPED_SIG; 2204 if (p->p_numthreads == p->p_suspcount) { 2205 p->p_flag |= P_CONTINUED; 2206 childproc_continued(p); 2207 } 2208 if (action == SIG_DFL) { 2209 sigqueue_delete(sigqueue, sig); 2210 } else if (action == SIG_CATCH) { 2211 /* 2212 * The process wants to catch it so it needs 2213 * to run at least one thread, but which one? 2214 * It would seem that the answer would be to 2215 * run an upcall in the next KSE to run, and 2216 * deliver the signal that way. In a NON KSE 2217 * process, we need to make sure that the 2218 * single thread is runnable asap. 2219 * XXXKSE for now however, make them all run. 2220 */ 2221 goto runfast; 2222 } 2223 /* 2224 * The signal is not ignored or caught. 2225 */ 2226 mtx_lock_spin(&sched_lock); 2227 thread_unsuspend(p); 2228 mtx_unlock_spin(&sched_lock); 2229 goto out; 2230 } 2231 2232 if (prop & SA_STOP) { 2233 /* 2234 * Already stopped, don't need to stop again 2235 * (If we did the shell could get confused). 2236 * Just make sure the signal STOP bit set. 2237 */ 2238 p->p_flag |= P_STOPPED_SIG; 2239 sigqueue_delete(sigqueue, sig); 2240 goto out; 2241 } 2242 2243 /* 2244 * All other kinds of signals: 2245 * If a thread is sleeping interruptibly, simulate a 2246 * wakeup so that when it is continued it will be made 2247 * runnable and can look at the signal. However, don't make 2248 * the PROCESS runnable, leave it stopped. 2249 * It may run a bit until it hits a thread_suspend_check(). 2250 */ 2251 mtx_lock_spin(&sched_lock); 2252 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2253 sleepq_abort(td); 2254 mtx_unlock_spin(&sched_lock); 2255 goto out; 2256 /* 2257 * Mutexes are short lived. Threads waiting on them will 2258 * hit thread_suspend_check() soon. 2259 */ 2260 } else if (p->p_state == PRS_NORMAL) { 2261 if (p->p_flag & P_TRACED || action == SIG_CATCH) { 2262 mtx_lock_spin(&sched_lock); 2263 tdsigwakeup(td, sig, action); 2264 mtx_unlock_spin(&sched_lock); 2265 goto out; 2266 } 2267 2268 MPASS(action == SIG_DFL); 2269 2270 if (prop & SA_STOP) { 2271 if (p->p_flag & P_PPWAIT) 2272 goto out; 2273 p->p_flag |= P_STOPPED_SIG; 2274 p->p_xstat = sig; 2275 p->p_xthread = td; 2276 mtx_lock_spin(&sched_lock); 2277 FOREACH_THREAD_IN_PROC(p, td0) { 2278 if (TD_IS_SLEEPING(td0) && 2279 (td0->td_flags & TDF_SINTR) && 2280 !TD_IS_SUSPENDED(td0)) { 2281 thread_suspend_one(td0); 2282 } else { 2283 td0->td_flags |= TDF_ASTPENDING; 2284 } 2285 } 2286 if (p->p_numthreads == p->p_suspcount) { 2287 /* 2288 * only thread sending signal to another 2289 * process can reach here, if thread is sending 2290 * signal to its process, because thread does 2291 * not suspend itself here, p_numthreads 2292 * should never be equal to p_suspcount. 2293 */ 2294 thread_stopped(p); 2295 mtx_unlock_spin(&sched_lock); 2296 sigqueue_delete_proc(p, p->p_xstat); 2297 } else 2298 mtx_unlock_spin(&sched_lock); 2299 goto out; 2300 } 2301 else 2302 goto runfast; 2303 /* NOTREACHED */ 2304 } else { 2305 /* Not in "NORMAL" state. discard the signal. */ 2306 sigqueue_delete(sigqueue, sig); 2307 goto out; 2308 } 2309 2310 /* 2311 * The process is not stopped so we need to apply the signal to all the 2312 * running threads. 2313 */ 2314 2315 runfast: 2316 mtx_lock_spin(&sched_lock); 2317 tdsigwakeup(td, sig, action); 2318 thread_unsuspend(p); 2319 mtx_unlock_spin(&sched_lock); 2320 out: 2321 /* If we jump here, sched_lock should not be owned. */ 2322 mtx_assert(&sched_lock, MA_NOTOWNED); 2323 return (ret); 2324 } 2325 2326 /* 2327 * The force of a signal has been directed against a single 2328 * thread. We need to see what we can do about knocking it 2329 * out of any sleep it may be in etc. 2330 */ 2331 static void 2332 tdsigwakeup(struct thread *td, int sig, sig_t action) 2333 { 2334 struct proc *p = td->td_proc; 2335 register int prop; 2336 2337 PROC_LOCK_ASSERT(p, MA_OWNED); 2338 mtx_assert(&sched_lock, MA_OWNED); 2339 prop = sigprop(sig); 2340 2341 /* 2342 * Bring the priority of a thread up if we want it to get 2343 * killed in this lifetime. 2344 */ 2345 if (action == SIG_DFL && (prop & SA_KILL)) { 2346 if (p->p_nice > 0) 2347 sched_nice(td->td_proc, 0); 2348 if (td->td_priority > PUSER) 2349 sched_prio(td, PUSER); 2350 } 2351 2352 if (TD_ON_SLEEPQ(td)) { 2353 /* 2354 * If thread is sleeping uninterruptibly 2355 * we can't interrupt the sleep... the signal will 2356 * be noticed when the process returns through 2357 * trap() or syscall(). 2358 */ 2359 if ((td->td_flags & TDF_SINTR) == 0) 2360 return; 2361 /* 2362 * If SIGCONT is default (or ignored) and process is 2363 * asleep, we are finished; the process should not 2364 * be awakened. 2365 */ 2366 if ((prop & SA_CONT) && action == SIG_DFL) { 2367 mtx_unlock_spin(&sched_lock); 2368 sigqueue_delete(&p->p_sigqueue, sig); 2369 /* 2370 * It may be on either list in this state. 2371 * Remove from both for now. 2372 */ 2373 sigqueue_delete(&td->td_sigqueue, sig); 2374 mtx_lock_spin(&sched_lock); 2375 return; 2376 } 2377 2378 /* 2379 * Give low priority threads a better chance to run. 2380 */ 2381 if (td->td_priority > PUSER) 2382 sched_prio(td, PUSER); 2383 2384 sleepq_abort(td); 2385 } else { 2386 /* 2387 * Other states do nothing with the signal immediately, 2388 * other than kicking ourselves if we are running. 2389 * It will either never be noticed, or noticed very soon. 2390 */ 2391 #ifdef SMP 2392 if (TD_IS_RUNNING(td) && td != curthread) 2393 forward_signal(td); 2394 #endif 2395 } 2396 } 2397 2398 int 2399 ptracestop(struct thread *td, int sig) 2400 { 2401 struct proc *p = td->td_proc; 2402 struct thread *td0; 2403 2404 PROC_LOCK_ASSERT(p, MA_OWNED); 2405 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2406 &p->p_mtx.mtx_object, "Stopping for traced signal"); 2407 2408 mtx_lock_spin(&sched_lock); 2409 td->td_flags |= TDF_XSIG; 2410 mtx_unlock_spin(&sched_lock); 2411 td->td_xsig = sig; 2412 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2413 if (p->p_flag & P_SINGLE_EXIT) { 2414 mtx_lock_spin(&sched_lock); 2415 td->td_flags &= ~TDF_XSIG; 2416 mtx_unlock_spin(&sched_lock); 2417 return (sig); 2418 } 2419 /* 2420 * Just make wait() to work, the last stopped thread 2421 * will win. 2422 */ 2423 p->p_xstat = sig; 2424 p->p_xthread = td; 2425 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2426 mtx_lock_spin(&sched_lock); 2427 FOREACH_THREAD_IN_PROC(p, td0) { 2428 if (TD_IS_SLEEPING(td0) && 2429 (td0->td_flags & TDF_SINTR) && 2430 !TD_IS_SUSPENDED(td0)) { 2431 thread_suspend_one(td0); 2432 } else if (td != td0) { 2433 td0->td_flags |= TDF_ASTPENDING; 2434 } 2435 } 2436 stopme: 2437 thread_stopped(p); 2438 thread_suspend_one(td); 2439 PROC_UNLOCK(p); 2440 DROP_GIANT(); 2441 mi_switch(SW_VOL, NULL); 2442 mtx_unlock_spin(&sched_lock); 2443 PICKUP_GIANT(); 2444 PROC_LOCK(p); 2445 if (!(p->p_flag & P_TRACED)) 2446 break; 2447 if (td->td_flags & TDF_DBSUSPEND) { 2448 if (p->p_flag & P_SINGLE_EXIT) 2449 break; 2450 mtx_lock_spin(&sched_lock); 2451 goto stopme; 2452 } 2453 } 2454 return (td->td_xsig); 2455 } 2456 2457 /* 2458 * If the current process has received a signal (should be caught or cause 2459 * termination, should interrupt current syscall), return the signal number. 2460 * Stop signals with default action are processed immediately, then cleared; 2461 * they aren't returned. This is checked after each entry to the system for 2462 * a syscall or trap (though this can usually be done without calling issignal 2463 * by checking the pending signal masks in cursig.) The normal call 2464 * sequence is 2465 * 2466 * while (sig = cursig(curthread)) 2467 * postsig(sig); 2468 */ 2469 static int 2470 issignal(td) 2471 struct thread *td; 2472 { 2473 struct proc *p; 2474 struct sigacts *ps; 2475 sigset_t sigpending; 2476 int sig, prop, newsig; 2477 struct thread *td0; 2478 2479 p = td->td_proc; 2480 ps = p->p_sigacts; 2481 mtx_assert(&ps->ps_mtx, MA_OWNED); 2482 PROC_LOCK_ASSERT(p, MA_OWNED); 2483 for (;;) { 2484 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2485 2486 sigpending = td->td_sigqueue.sq_signals; 2487 SIGSETNAND(sigpending, td->td_sigmask); 2488 2489 if (p->p_flag & P_PPWAIT) 2490 SIG_STOPSIGMASK(sigpending); 2491 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2492 return (0); 2493 sig = sig_ffs(&sigpending); 2494 2495 if (p->p_stops & S_SIG) { 2496 mtx_unlock(&ps->ps_mtx); 2497 stopevent(p, S_SIG, sig); 2498 mtx_lock(&ps->ps_mtx); 2499 } 2500 2501 /* 2502 * We should see pending but ignored signals 2503 * only if P_TRACED was on when they were posted. 2504 */ 2505 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2506 sigqueue_delete(&td->td_sigqueue, sig); 2507 if (td->td_pflags & TDP_SA) 2508 SIGADDSET(td->td_sigmask, sig); 2509 continue; 2510 } 2511 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2512 /* 2513 * If traced, always stop. 2514 */ 2515 mtx_unlock(&ps->ps_mtx); 2516 newsig = ptracestop(td, sig); 2517 mtx_lock(&ps->ps_mtx); 2518 2519 if (td->td_pflags & TDP_SA) 2520 SIGADDSET(td->td_sigmask, sig); 2521 2522 if (sig != newsig) { 2523 ksiginfo_t ksi; 2524 /* 2525 * clear old signal. 2526 * XXX shrug off debugger, it causes siginfo to 2527 * be thrown away. 2528 */ 2529 sigqueue_get(&td->td_sigqueue, sig, &ksi); 2530 2531 /* 2532 * If parent wants us to take the signal, 2533 * then it will leave it in p->p_xstat; 2534 * otherwise we just look for signals again. 2535 */ 2536 if (newsig == 0) 2537 continue; 2538 sig = newsig; 2539 2540 /* 2541 * Put the new signal into td_sigqueue. If the 2542 * signal is being masked, look for other signals. 2543 */ 2544 SIGADDSET(td->td_sigqueue.sq_signals, sig); 2545 if (td->td_pflags & TDP_SA) 2546 SIGDELSET(td->td_sigmask, sig); 2547 if (SIGISMEMBER(td->td_sigmask, sig)) 2548 continue; 2549 signotify(td); 2550 } 2551 2552 /* 2553 * If the traced bit got turned off, go back up 2554 * to the top to rescan signals. This ensures 2555 * that p_sig* and p_sigact are consistent. 2556 */ 2557 if ((p->p_flag & P_TRACED) == 0) 2558 continue; 2559 } 2560 2561 prop = sigprop(sig); 2562 2563 /* 2564 * Decide whether the signal should be returned. 2565 * Return the signal's number, or fall through 2566 * to clear it from the pending mask. 2567 */ 2568 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2569 2570 case (intptr_t)SIG_DFL: 2571 /* 2572 * Don't take default actions on system processes. 2573 */ 2574 if (p->p_pid <= 1) { 2575 #ifdef DIAGNOSTIC 2576 /* 2577 * Are you sure you want to ignore SIGSEGV 2578 * in init? XXX 2579 */ 2580 printf("Process (pid %lu) got signal %d\n", 2581 (u_long)p->p_pid, sig); 2582 #endif 2583 break; /* == ignore */ 2584 } 2585 /* 2586 * If there is a pending stop signal to process 2587 * with default action, stop here, 2588 * then clear the signal. However, 2589 * if process is member of an orphaned 2590 * process group, ignore tty stop signals. 2591 */ 2592 if (prop & SA_STOP) { 2593 if (p->p_flag & P_TRACED || 2594 (p->p_pgrp->pg_jobc == 0 && 2595 prop & SA_TTYSTOP)) 2596 break; /* == ignore */ 2597 mtx_unlock(&ps->ps_mtx); 2598 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2599 &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2600 p->p_flag |= P_STOPPED_SIG; 2601 p->p_xstat = sig; 2602 p->p_xthread = td; 2603 mtx_lock_spin(&sched_lock); 2604 FOREACH_THREAD_IN_PROC(p, td0) { 2605 if (TD_IS_SLEEPING(td0) && 2606 (td0->td_flags & TDF_SINTR) && 2607 !TD_IS_SUSPENDED(td0)) { 2608 thread_suspend_one(td0); 2609 } else if (td != td0) { 2610 td0->td_flags |= TDF_ASTPENDING; 2611 } 2612 } 2613 thread_stopped(p); 2614 thread_suspend_one(td); 2615 PROC_UNLOCK(p); 2616 DROP_GIANT(); 2617 mi_switch(SW_INVOL, NULL); 2618 mtx_unlock_spin(&sched_lock); 2619 PICKUP_GIANT(); 2620 PROC_LOCK(p); 2621 mtx_lock(&ps->ps_mtx); 2622 break; 2623 } else if (prop & SA_IGNORE) { 2624 /* 2625 * Except for SIGCONT, shouldn't get here. 2626 * Default action is to ignore; drop it. 2627 */ 2628 break; /* == ignore */ 2629 } else 2630 return (sig); 2631 /*NOTREACHED*/ 2632 2633 case (intptr_t)SIG_IGN: 2634 /* 2635 * Masking above should prevent us ever trying 2636 * to take action on an ignored signal other 2637 * than SIGCONT, unless process is traced. 2638 */ 2639 if ((prop & SA_CONT) == 0 && 2640 (p->p_flag & P_TRACED) == 0) 2641 printf("issignal\n"); 2642 break; /* == ignore */ 2643 2644 default: 2645 /* 2646 * This signal has an action, let 2647 * postsig() process it. 2648 */ 2649 return (sig); 2650 } 2651 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 2652 } 2653 /* NOTREACHED */ 2654 } 2655 2656 /* 2657 * MPSAFE 2658 */ 2659 void 2660 thread_stopped(struct proc *p) 2661 { 2662 struct proc *p1 = curthread->td_proc; 2663 struct sigacts *ps; 2664 int n; 2665 2666 PROC_LOCK_ASSERT(p, MA_OWNED); 2667 mtx_assert(&sched_lock, MA_OWNED); 2668 n = p->p_suspcount; 2669 if (p == p1) 2670 n++; 2671 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2672 mtx_unlock_spin(&sched_lock); 2673 p->p_flag &= ~P_WAITED; 2674 PROC_LOCK(p->p_pptr); 2675 /* 2676 * Wake up parent sleeping in kern_wait(), also send 2677 * SIGCHLD to parent, but SIGCHLD does not guarantee 2678 * that parent will awake, because parent may masked 2679 * the signal. 2680 */ 2681 p->p_pptr->p_flag |= P_STATCHILD; 2682 wakeup(p->p_pptr); 2683 ps = p->p_pptr->p_sigacts; 2684 mtx_lock(&ps->ps_mtx); 2685 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 2686 mtx_unlock(&ps->ps_mtx); 2687 childproc_stopped(p, 2688 (p->p_flag & P_TRACED) ? 2689 CLD_TRAPPED : CLD_STOPPED); 2690 } else 2691 mtx_unlock(&ps->ps_mtx); 2692 PROC_UNLOCK(p->p_pptr); 2693 mtx_lock_spin(&sched_lock); 2694 } 2695 } 2696 2697 /* 2698 * Take the action for the specified signal 2699 * from the current set of pending signals. 2700 */ 2701 void 2702 postsig(sig) 2703 register int sig; 2704 { 2705 struct thread *td = curthread; 2706 register struct proc *p = td->td_proc; 2707 struct sigacts *ps; 2708 sig_t action; 2709 ksiginfo_t ksi; 2710 sigset_t returnmask; 2711 int code; 2712 2713 KASSERT(sig != 0, ("postsig")); 2714 2715 PROC_LOCK_ASSERT(p, MA_OWNED); 2716 ps = p->p_sigacts; 2717 mtx_assert(&ps->ps_mtx, MA_OWNED); 2718 ksiginfo_init(&ksi); 2719 sigqueue_get(&td->td_sigqueue, sig, &ksi); 2720 ksi.ksi_signo = sig; 2721 if (ksi.ksi_code == SI_TIMER) 2722 itimer_accept(p, ksi.ksi_timerid, &ksi); 2723 action = ps->ps_sigact[_SIG_IDX(sig)]; 2724 #ifdef KTRACE 2725 if (KTRPOINT(td, KTR_PSIG)) 2726 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2727 &td->td_oldsigmask : &td->td_sigmask, 0); 2728 #endif 2729 if (p->p_stops & S_SIG) { 2730 mtx_unlock(&ps->ps_mtx); 2731 stopevent(p, S_SIG, sig); 2732 mtx_lock(&ps->ps_mtx); 2733 } 2734 2735 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2736 /* 2737 * Default action, where the default is to kill 2738 * the process. (Other cases were ignored above.) 2739 */ 2740 mtx_unlock(&ps->ps_mtx); 2741 sigexit(td, sig); 2742 /* NOTREACHED */ 2743 } else { 2744 if (td->td_pflags & TDP_SA) { 2745 if (sig == SIGKILL) { 2746 mtx_unlock(&ps->ps_mtx); 2747 sigexit(td, sig); 2748 } 2749 } 2750 2751 /* 2752 * If we get here, the signal must be caught. 2753 */ 2754 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2755 ("postsig action")); 2756 /* 2757 * Set the new mask value and also defer further 2758 * occurrences of this signal. 2759 * 2760 * Special case: user has done a sigsuspend. Here the 2761 * current mask is not of interest, but rather the 2762 * mask from before the sigsuspend is what we want 2763 * restored after the signal processing is completed. 2764 */ 2765 if (td->td_pflags & TDP_OLDMASK) { 2766 returnmask = td->td_oldsigmask; 2767 td->td_pflags &= ~TDP_OLDMASK; 2768 } else 2769 returnmask = td->td_sigmask; 2770 2771 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 2772 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2773 SIGADDSET(td->td_sigmask, sig); 2774 2775 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2776 /* 2777 * See kern_sigaction() for origin of this code. 2778 */ 2779 SIGDELSET(ps->ps_sigcatch, sig); 2780 if (sig != SIGCONT && 2781 sigprop(sig) & SA_IGNORE) 2782 SIGADDSET(ps->ps_sigignore, sig); 2783 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2784 } 2785 p->p_stats->p_ru.ru_nsignals++; 2786 if (p->p_sig != sig) { 2787 code = 0; 2788 } else { 2789 code = p->p_code; 2790 p->p_code = 0; 2791 p->p_sig = 0; 2792 } 2793 if (td->td_pflags & TDP_SA) 2794 thread_signal_add(curthread, &ksi); 2795 else 2796 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 2797 } 2798 } 2799 2800 /* 2801 * Kill the current process for stated reason. 2802 */ 2803 void 2804 killproc(p, why) 2805 struct proc *p; 2806 char *why; 2807 { 2808 2809 PROC_LOCK_ASSERT(p, MA_OWNED); 2810 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 2811 p, p->p_pid, p->p_comm); 2812 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2813 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2814 psignal(p, SIGKILL); 2815 } 2816 2817 /* 2818 * Force the current process to exit with the specified signal, dumping core 2819 * if appropriate. We bypass the normal tests for masked and caught signals, 2820 * allowing unrecoverable failures to terminate the process without changing 2821 * signal state. Mark the accounting record with the signal termination. 2822 * If dumping core, save the signal number for the debugger. Calls exit and 2823 * does not return. 2824 * 2825 * MPSAFE 2826 */ 2827 void 2828 sigexit(td, sig) 2829 struct thread *td; 2830 int sig; 2831 { 2832 struct proc *p = td->td_proc; 2833 2834 PROC_LOCK_ASSERT(p, MA_OWNED); 2835 p->p_acflag |= AXSIG; 2836 /* 2837 * We must be single-threading to generate a core dump. This 2838 * ensures that the registers in the core file are up-to-date. 2839 * Also, the ELF dump handler assumes that the thread list doesn't 2840 * change out from under it. 2841 * 2842 * XXX If another thread attempts to single-thread before us 2843 * (e.g. via fork()), we won't get a dump at all. 2844 */ 2845 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) { 2846 p->p_sig = sig; 2847 /* 2848 * Log signals which would cause core dumps 2849 * (Log as LOG_INFO to appease those who don't want 2850 * these messages.) 2851 * XXX : Todo, as well as euid, write out ruid too 2852 * Note that coredump() drops proc lock. 2853 */ 2854 if (coredump(td) == 0) 2855 sig |= WCOREFLAG; 2856 if (kern_logsigexit) 2857 log(LOG_INFO, 2858 "pid %d (%s), uid %d: exited on signal %d%s\n", 2859 p->p_pid, p->p_comm, 2860 td->td_ucred ? td->td_ucred->cr_uid : -1, 2861 sig &~ WCOREFLAG, 2862 sig & WCOREFLAG ? " (core dumped)" : ""); 2863 } else 2864 PROC_UNLOCK(p); 2865 exit1(td, W_EXITCODE(0, sig)); 2866 /* NOTREACHED */ 2867 } 2868 2869 /* 2870 * Send queued SIGCHLD to parent when child process is stopped 2871 * or exited. 2872 */ 2873 void 2874 childproc_stopped(struct proc *p, int reason) 2875 { 2876 PROC_LOCK_ASSERT(p, MA_OWNED); 2877 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 2878 2879 if (p->p_ksi != NULL) { 2880 p->p_ksi->ksi_signo = SIGCHLD; 2881 p->p_ksi->ksi_code = reason; 2882 p->p_ksi->ksi_status = p->p_xstat; 2883 p->p_ksi->ksi_pid = p->p_pid; 2884 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 2885 if (KSI_ONQ(p->p_ksi)) 2886 return; 2887 } 2888 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi); 2889 } 2890 2891 void 2892 childproc_continued(struct proc *p) 2893 { 2894 PROC_LOCK_ASSERT(p, MA_OWNED); 2895 PROC_LOCK_ASSERT(p->p_pptr, MA_NOTOWNED); 2896 2897 PROC_LOCK(p->p_pptr); 2898 if (p->p_ksi != NULL) { 2899 p->p_ksi->ksi_signo = SIGCHLD; 2900 p->p_ksi->ksi_code = CLD_CONTINUED; 2901 p->p_ksi->ksi_status = SIGCONT; 2902 p->p_ksi->ksi_pid = p->p_pid; 2903 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 2904 if (KSI_ONQ(p->p_ksi)) { 2905 PROC_UNLOCK(p->p_pptr); 2906 return; 2907 } 2908 } 2909 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi); 2910 PROC_UNLOCK(p->p_pptr); 2911 } 2912 2913 void 2914 childproc_exited(struct proc *p) 2915 { 2916 int reason; 2917 int status = p->p_xstat; /* convert to int */ 2918 2919 reason = CLD_EXITED; 2920 if (WCOREDUMP(status)) 2921 reason = CLD_DUMPED; 2922 else if (WIFSIGNALED(status)) 2923 reason = CLD_KILLED; 2924 childproc_stopped(p, reason); 2925 } 2926 2927 static char corefilename[MAXPATHLEN] = {"%N.core"}; 2928 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2929 sizeof(corefilename), "process corefile name format string"); 2930 2931 /* 2932 * expand_name(name, uid, pid) 2933 * Expand the name described in corefilename, using name, uid, and pid. 2934 * corefilename is a printf-like string, with three format specifiers: 2935 * %N name of process ("name") 2936 * %P process id (pid) 2937 * %U user id (uid) 2938 * For example, "%N.core" is the default; they can be disabled completely 2939 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2940 * This is controlled by the sysctl variable kern.corefile (see above). 2941 */ 2942 2943 static char * 2944 expand_name(name, uid, pid) 2945 const char *name; 2946 uid_t uid; 2947 pid_t pid; 2948 { 2949 const char *format, *appendstr; 2950 char *temp; 2951 char buf[11]; /* Buffer for pid/uid -- max 4B */ 2952 size_t i, l, n; 2953 2954 format = corefilename; 2955 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 2956 if (temp == NULL) 2957 return (NULL); 2958 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2959 switch (format[i]) { 2960 case '%': /* Format character */ 2961 i++; 2962 switch (format[i]) { 2963 case '%': 2964 appendstr = "%"; 2965 break; 2966 case 'N': /* process name */ 2967 appendstr = name; 2968 break; 2969 case 'P': /* process id */ 2970 sprintf(buf, "%u", pid); 2971 appendstr = buf; 2972 break; 2973 case 'U': /* user id */ 2974 sprintf(buf, "%u", uid); 2975 appendstr = buf; 2976 break; 2977 default: 2978 appendstr = ""; 2979 log(LOG_ERR, 2980 "Unknown format character %c in `%s'\n", 2981 format[i], format); 2982 } 2983 l = strlen(appendstr); 2984 if ((n + l) >= MAXPATHLEN) 2985 goto toolong; 2986 memcpy(temp + n, appendstr, l); 2987 n += l; 2988 break; 2989 default: 2990 temp[n++] = format[i]; 2991 } 2992 } 2993 if (format[i] != '\0') 2994 goto toolong; 2995 return (temp); 2996 toolong: 2997 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 2998 (long)pid, name, (u_long)uid); 2999 free(temp, M_TEMP); 3000 return (NULL); 3001 } 3002 3003 /* 3004 * Dump a process' core. The main routine does some 3005 * policy checking, and creates the name of the coredump; 3006 * then it passes on a vnode and a size limit to the process-specific 3007 * coredump routine if there is one; if there _is not_ one, it returns 3008 * ENOSYS; otherwise it returns the error from the process-specific routine. 3009 */ 3010 3011 static int 3012 coredump(struct thread *td) 3013 { 3014 struct proc *p = td->td_proc; 3015 register struct vnode *vp; 3016 register struct ucred *cred = td->td_ucred; 3017 struct flock lf; 3018 struct nameidata nd; 3019 struct vattr vattr; 3020 int error, error1, flags, locked; 3021 struct mount *mp; 3022 char *name; /* name of corefile */ 3023 off_t limit; 3024 3025 PROC_LOCK_ASSERT(p, MA_OWNED); 3026 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3027 _STOPEVENT(p, S_CORE, 0); 3028 3029 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 3030 PROC_UNLOCK(p); 3031 return (EFAULT); 3032 } 3033 3034 /* 3035 * Note that the bulk of limit checking is done after 3036 * the corefile is created. The exception is if the limit 3037 * for corefiles is 0, in which case we don't bother 3038 * creating the corefile at all. This layout means that 3039 * a corefile is truncated instead of not being created, 3040 * if it is larger than the limit. 3041 */ 3042 limit = (off_t)lim_cur(p, RLIMIT_CORE); 3043 PROC_UNLOCK(p); 3044 if (limit == 0) 3045 return (EFBIG); 3046 3047 mtx_lock(&Giant); 3048 restart: 3049 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 3050 if (name == NULL) { 3051 mtx_unlock(&Giant); 3052 return (EINVAL); 3053 } 3054 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 3055 flags = O_CREAT | FWRITE | O_NOFOLLOW; 3056 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 3057 free(name, M_TEMP); 3058 if (error) { 3059 mtx_unlock(&Giant); 3060 return (error); 3061 } 3062 NDFREE(&nd, NDF_ONLY_PNBUF); 3063 vp = nd.ni_vp; 3064 3065 /* Don't dump to non-regular files or files with links. */ 3066 if (vp->v_type != VREG || 3067 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 3068 VOP_UNLOCK(vp, 0, td); 3069 error = EFAULT; 3070 goto out; 3071 } 3072 3073 VOP_UNLOCK(vp, 0, td); 3074 lf.l_whence = SEEK_SET; 3075 lf.l_start = 0; 3076 lf.l_len = 0; 3077 lf.l_type = F_WRLCK; 3078 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 3079 3080 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 3081 lf.l_type = F_UNLCK; 3082 if (locked) 3083 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3084 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 3085 return (error); 3086 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 3087 return (error); 3088 goto restart; 3089 } 3090 3091 VATTR_NULL(&vattr); 3092 vattr.va_size = 0; 3093 if (set_core_nodump_flag) 3094 vattr.va_flags = UF_NODUMP; 3095 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 3096 VOP_LEASE(vp, td, cred, LEASE_WRITE); 3097 VOP_SETATTR(vp, &vattr, cred, td); 3098 VOP_UNLOCK(vp, 0, td); 3099 PROC_LOCK(p); 3100 p->p_acflag |= ACORE; 3101 PROC_UNLOCK(p); 3102 3103 error = p->p_sysent->sv_coredump ? 3104 p->p_sysent->sv_coredump(td, vp, limit) : 3105 ENOSYS; 3106 3107 if (locked) { 3108 lf.l_type = F_UNLCK; 3109 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3110 } 3111 vn_finished_write(mp); 3112 out: 3113 error1 = vn_close(vp, FWRITE, cred, td); 3114 mtx_unlock(&Giant); 3115 if (error == 0) 3116 error = error1; 3117 return (error); 3118 } 3119 3120 /* 3121 * Nonexistent system call-- signal process (may want to handle it). 3122 * Flag error in case process won't see signal immediately (blocked or ignored). 3123 */ 3124 #ifndef _SYS_SYSPROTO_H_ 3125 struct nosys_args { 3126 int dummy; 3127 }; 3128 #endif 3129 /* 3130 * MPSAFE 3131 */ 3132 /* ARGSUSED */ 3133 int 3134 nosys(td, args) 3135 struct thread *td; 3136 struct nosys_args *args; 3137 { 3138 struct proc *p = td->td_proc; 3139 3140 PROC_LOCK(p); 3141 psignal(p, SIGSYS); 3142 PROC_UNLOCK(p); 3143 return (ENOSYS); 3144 } 3145 3146 /* 3147 * Send a SIGIO or SIGURG signal to a process or process group using 3148 * stored credentials rather than those of the current process. 3149 */ 3150 void 3151 pgsigio(sigiop, sig, checkctty) 3152 struct sigio **sigiop; 3153 int sig, checkctty; 3154 { 3155 struct sigio *sigio; 3156 3157 SIGIO_LOCK(); 3158 sigio = *sigiop; 3159 if (sigio == NULL) { 3160 SIGIO_UNLOCK(); 3161 return; 3162 } 3163 if (sigio->sio_pgid > 0) { 3164 PROC_LOCK(sigio->sio_proc); 3165 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 3166 psignal(sigio->sio_proc, sig); 3167 PROC_UNLOCK(sigio->sio_proc); 3168 } else if (sigio->sio_pgid < 0) { 3169 struct proc *p; 3170 3171 PGRP_LOCK(sigio->sio_pgrp); 3172 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3173 PROC_LOCK(p); 3174 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3175 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 3176 psignal(p, sig); 3177 PROC_UNLOCK(p); 3178 } 3179 PGRP_UNLOCK(sigio->sio_pgrp); 3180 } 3181 SIGIO_UNLOCK(); 3182 } 3183 3184 static int 3185 filt_sigattach(struct knote *kn) 3186 { 3187 struct proc *p = curproc; 3188 3189 kn->kn_ptr.p_proc = p; 3190 kn->kn_flags |= EV_CLEAR; /* automatically set */ 3191 3192 knlist_add(&p->p_klist, kn, 0); 3193 3194 return (0); 3195 } 3196 3197 static void 3198 filt_sigdetach(struct knote *kn) 3199 { 3200 struct proc *p = kn->kn_ptr.p_proc; 3201 3202 knlist_remove(&p->p_klist, kn, 0); 3203 } 3204 3205 /* 3206 * signal knotes are shared with proc knotes, so we apply a mask to 3207 * the hint in order to differentiate them from process hints. This 3208 * could be avoided by using a signal-specific knote list, but probably 3209 * isn't worth the trouble. 3210 */ 3211 static int 3212 filt_signal(struct knote *kn, long hint) 3213 { 3214 3215 if (hint & NOTE_SIGNAL) { 3216 hint &= ~NOTE_SIGNAL; 3217 3218 if (kn->kn_id == hint) 3219 kn->kn_data++; 3220 } 3221 return (kn->kn_data != 0); 3222 } 3223 3224 struct sigacts * 3225 sigacts_alloc(void) 3226 { 3227 struct sigacts *ps; 3228 3229 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3230 ps->ps_refcnt = 1; 3231 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 3232 return (ps); 3233 } 3234 3235 void 3236 sigacts_free(struct sigacts *ps) 3237 { 3238 3239 mtx_lock(&ps->ps_mtx); 3240 ps->ps_refcnt--; 3241 if (ps->ps_refcnt == 0) { 3242 mtx_destroy(&ps->ps_mtx); 3243 free(ps, M_SUBPROC); 3244 } else 3245 mtx_unlock(&ps->ps_mtx); 3246 } 3247 3248 struct sigacts * 3249 sigacts_hold(struct sigacts *ps) 3250 { 3251 mtx_lock(&ps->ps_mtx); 3252 ps->ps_refcnt++; 3253 mtx_unlock(&ps->ps_mtx); 3254 return (ps); 3255 } 3256 3257 void 3258 sigacts_copy(struct sigacts *dest, struct sigacts *src) 3259 { 3260 3261 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 3262 mtx_lock(&src->ps_mtx); 3263 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 3264 mtx_unlock(&src->ps_mtx); 3265 } 3266 3267 int 3268 sigacts_shared(struct sigacts *ps) 3269 { 3270 int shared; 3271 3272 mtx_lock(&ps->ps_mtx); 3273 shared = ps->ps_refcnt > 1; 3274 mtx_unlock(&ps->ps_mtx); 3275 return (shared); 3276 } 3277