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 p->p_xstat = SIGCONT; 2207 PROC_LOCK(p->p_pptr); 2208 childproc_continued(p); 2209 PROC_UNLOCK(p->p_pptr); 2210 } 2211 if (action == SIG_DFL) { 2212 sigqueue_delete(sigqueue, sig); 2213 } else if (action == SIG_CATCH) { 2214 /* 2215 * The process wants to catch it so it needs 2216 * to run at least one thread, but which one? 2217 * It would seem that the answer would be to 2218 * run an upcall in the next KSE to run, and 2219 * deliver the signal that way. In a NON KSE 2220 * process, we need to make sure that the 2221 * single thread is runnable asap. 2222 * XXXKSE for now however, make them all run. 2223 */ 2224 goto runfast; 2225 } 2226 /* 2227 * The signal is not ignored or caught. 2228 */ 2229 mtx_lock_spin(&sched_lock); 2230 thread_unsuspend(p); 2231 mtx_unlock_spin(&sched_lock); 2232 goto out; 2233 } 2234 2235 if (prop & SA_STOP) { 2236 /* 2237 * Already stopped, don't need to stop again 2238 * (If we did the shell could get confused). 2239 * Just make sure the signal STOP bit set. 2240 */ 2241 p->p_flag |= P_STOPPED_SIG; 2242 sigqueue_delete(sigqueue, sig); 2243 goto out; 2244 } 2245 2246 /* 2247 * All other kinds of signals: 2248 * If a thread is sleeping interruptibly, simulate a 2249 * wakeup so that when it is continued it will be made 2250 * runnable and can look at the signal. However, don't make 2251 * the PROCESS runnable, leave it stopped. 2252 * It may run a bit until it hits a thread_suspend_check(). 2253 */ 2254 mtx_lock_spin(&sched_lock); 2255 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2256 sleepq_abort(td); 2257 mtx_unlock_spin(&sched_lock); 2258 goto out; 2259 /* 2260 * Mutexes are short lived. Threads waiting on them will 2261 * hit thread_suspend_check() soon. 2262 */ 2263 } else if (p->p_state == PRS_NORMAL) { 2264 if (p->p_flag & P_TRACED || action == SIG_CATCH) { 2265 mtx_lock_spin(&sched_lock); 2266 tdsigwakeup(td, sig, action); 2267 mtx_unlock_spin(&sched_lock); 2268 goto out; 2269 } 2270 2271 MPASS(action == SIG_DFL); 2272 2273 if (prop & SA_STOP) { 2274 if (p->p_flag & P_PPWAIT) 2275 goto out; 2276 p->p_flag |= P_STOPPED_SIG; 2277 p->p_xstat = sig; 2278 mtx_lock_spin(&sched_lock); 2279 FOREACH_THREAD_IN_PROC(p, td0) { 2280 if (TD_IS_SLEEPING(td0) && 2281 (td0->td_flags & TDF_SINTR) && 2282 !TD_IS_SUSPENDED(td0)) { 2283 thread_suspend_one(td0); 2284 } else { 2285 td0->td_flags |= TDF_ASTPENDING; 2286 } 2287 } 2288 if (p->p_numthreads == p->p_suspcount) { 2289 /* 2290 * only thread sending signal to another 2291 * process can reach here, if thread is sending 2292 * signal to its process, because thread does 2293 * not suspend itself here, p_numthreads 2294 * should never be equal to p_suspcount. 2295 */ 2296 thread_stopped(p); 2297 mtx_unlock_spin(&sched_lock); 2298 sigqueue_delete_proc(p, p->p_xstat); 2299 } else 2300 mtx_unlock_spin(&sched_lock); 2301 goto out; 2302 } 2303 else 2304 goto runfast; 2305 /* NOTREACHED */ 2306 } else { 2307 /* Not in "NORMAL" state. discard the signal. */ 2308 sigqueue_delete(sigqueue, sig); 2309 goto out; 2310 } 2311 2312 /* 2313 * The process is not stopped so we need to apply the signal to all the 2314 * running threads. 2315 */ 2316 2317 runfast: 2318 mtx_lock_spin(&sched_lock); 2319 tdsigwakeup(td, sig, action); 2320 thread_unsuspend(p); 2321 mtx_unlock_spin(&sched_lock); 2322 out: 2323 /* If we jump here, sched_lock should not be owned. */ 2324 mtx_assert(&sched_lock, MA_NOTOWNED); 2325 return (ret); 2326 } 2327 2328 /* 2329 * The force of a signal has been directed against a single 2330 * thread. We need to see what we can do about knocking it 2331 * out of any sleep it may be in etc. 2332 */ 2333 static void 2334 tdsigwakeup(struct thread *td, int sig, sig_t action) 2335 { 2336 struct proc *p = td->td_proc; 2337 register int prop; 2338 2339 PROC_LOCK_ASSERT(p, MA_OWNED); 2340 mtx_assert(&sched_lock, MA_OWNED); 2341 prop = sigprop(sig); 2342 2343 /* 2344 * Bring the priority of a thread up if we want it to get 2345 * killed in this lifetime. 2346 */ 2347 if (action == SIG_DFL && (prop & SA_KILL)) { 2348 if (p->p_nice > 0) 2349 sched_nice(td->td_proc, 0); 2350 if (td->td_priority > PUSER) 2351 sched_prio(td, PUSER); 2352 } 2353 2354 if (TD_ON_SLEEPQ(td)) { 2355 /* 2356 * If thread is sleeping uninterruptibly 2357 * we can't interrupt the sleep... the signal will 2358 * be noticed when the process returns through 2359 * trap() or syscall(). 2360 */ 2361 if ((td->td_flags & TDF_SINTR) == 0) 2362 return; 2363 /* 2364 * If SIGCONT is default (or ignored) and process is 2365 * asleep, we are finished; the process should not 2366 * be awakened. 2367 */ 2368 if ((prop & SA_CONT) && action == SIG_DFL) { 2369 mtx_unlock_spin(&sched_lock); 2370 sigqueue_delete(&p->p_sigqueue, sig); 2371 /* 2372 * It may be on either list in this state. 2373 * Remove from both for now. 2374 */ 2375 sigqueue_delete(&td->td_sigqueue, sig); 2376 mtx_lock_spin(&sched_lock); 2377 return; 2378 } 2379 2380 /* 2381 * Give low priority threads a better chance to run. 2382 */ 2383 if (td->td_priority > PUSER) 2384 sched_prio(td, PUSER); 2385 2386 sleepq_abort(td); 2387 } else { 2388 /* 2389 * Other states do nothing with the signal immediately, 2390 * other than kicking ourselves if we are running. 2391 * It will either never be noticed, or noticed very soon. 2392 */ 2393 #ifdef SMP 2394 if (TD_IS_RUNNING(td) && td != curthread) 2395 forward_signal(td); 2396 #endif 2397 } 2398 } 2399 2400 int 2401 ptracestop(struct thread *td, int sig) 2402 { 2403 struct proc *p = td->td_proc; 2404 struct thread *td0; 2405 2406 PROC_LOCK_ASSERT(p, MA_OWNED); 2407 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2408 &p->p_mtx.mtx_object, "Stopping for traced signal"); 2409 2410 mtx_lock_spin(&sched_lock); 2411 td->td_flags |= TDF_XSIG; 2412 mtx_unlock_spin(&sched_lock); 2413 td->td_xsig = sig; 2414 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) { 2415 if (p->p_flag & P_SINGLE_EXIT) { 2416 mtx_lock_spin(&sched_lock); 2417 td->td_flags &= ~TDF_XSIG; 2418 mtx_unlock_spin(&sched_lock); 2419 return (sig); 2420 } 2421 /* 2422 * Just make wait() to work, the last stopped thread 2423 * will win. 2424 */ 2425 p->p_xstat = sig; 2426 p->p_xthread = td; 2427 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE); 2428 mtx_lock_spin(&sched_lock); 2429 FOREACH_THREAD_IN_PROC(p, td0) { 2430 if (TD_IS_SLEEPING(td0) && 2431 (td0->td_flags & TDF_SINTR) && 2432 !TD_IS_SUSPENDED(td0)) { 2433 thread_suspend_one(td0); 2434 } else if (td != td0) { 2435 td0->td_flags |= TDF_ASTPENDING; 2436 } 2437 } 2438 stopme: 2439 thread_stopped(p); 2440 thread_suspend_one(td); 2441 PROC_UNLOCK(p); 2442 DROP_GIANT(); 2443 mi_switch(SW_VOL, NULL); 2444 mtx_unlock_spin(&sched_lock); 2445 PICKUP_GIANT(); 2446 PROC_LOCK(p); 2447 if (!(p->p_flag & P_TRACED)) 2448 break; 2449 if (td->td_flags & TDF_DBSUSPEND) { 2450 if (p->p_flag & P_SINGLE_EXIT) 2451 break; 2452 mtx_lock_spin(&sched_lock); 2453 goto stopme; 2454 } 2455 } 2456 return (td->td_xsig); 2457 } 2458 2459 /* 2460 * If the current process has received a signal (should be caught or cause 2461 * termination, should interrupt current syscall), return the signal number. 2462 * Stop signals with default action are processed immediately, then cleared; 2463 * they aren't returned. This is checked after each entry to the system for 2464 * a syscall or trap (though this can usually be done without calling issignal 2465 * by checking the pending signal masks in cursig.) The normal call 2466 * sequence is 2467 * 2468 * while (sig = cursig(curthread)) 2469 * postsig(sig); 2470 */ 2471 static int 2472 issignal(td) 2473 struct thread *td; 2474 { 2475 struct proc *p; 2476 struct sigacts *ps; 2477 sigset_t sigpending; 2478 int sig, prop, newsig; 2479 struct thread *td0; 2480 2481 p = td->td_proc; 2482 ps = p->p_sigacts; 2483 mtx_assert(&ps->ps_mtx, MA_OWNED); 2484 PROC_LOCK_ASSERT(p, MA_OWNED); 2485 for (;;) { 2486 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2487 2488 sigpending = td->td_sigqueue.sq_signals; 2489 SIGSETNAND(sigpending, td->td_sigmask); 2490 2491 if (p->p_flag & P_PPWAIT) 2492 SIG_STOPSIGMASK(sigpending); 2493 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2494 return (0); 2495 sig = sig_ffs(&sigpending); 2496 2497 if (p->p_stops & S_SIG) { 2498 mtx_unlock(&ps->ps_mtx); 2499 stopevent(p, S_SIG, sig); 2500 mtx_lock(&ps->ps_mtx); 2501 } 2502 2503 /* 2504 * We should see pending but ignored signals 2505 * only if P_TRACED was on when they were posted. 2506 */ 2507 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2508 sigqueue_delete(&td->td_sigqueue, sig); 2509 if (td->td_pflags & TDP_SA) 2510 SIGADDSET(td->td_sigmask, sig); 2511 continue; 2512 } 2513 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) { 2514 /* 2515 * If traced, always stop. 2516 */ 2517 mtx_unlock(&ps->ps_mtx); 2518 newsig = ptracestop(td, sig); 2519 mtx_lock(&ps->ps_mtx); 2520 2521 if (td->td_pflags & TDP_SA) 2522 SIGADDSET(td->td_sigmask, sig); 2523 2524 if (sig != newsig) { 2525 ksiginfo_t ksi; 2526 /* 2527 * clear old signal. 2528 * XXX shrug off debugger, it causes siginfo to 2529 * be thrown away. 2530 */ 2531 sigqueue_get(&td->td_sigqueue, sig, &ksi); 2532 2533 /* 2534 * If parent wants us to take the signal, 2535 * then it will leave it in p->p_xstat; 2536 * otherwise we just look for signals again. 2537 */ 2538 if (newsig == 0) 2539 continue; 2540 sig = newsig; 2541 2542 /* 2543 * Put the new signal into td_sigqueue. If the 2544 * signal is being masked, look for other signals. 2545 */ 2546 SIGADDSET(td->td_sigqueue.sq_signals, sig); 2547 if (td->td_pflags & TDP_SA) 2548 SIGDELSET(td->td_sigmask, sig); 2549 if (SIGISMEMBER(td->td_sigmask, sig)) 2550 continue; 2551 signotify(td); 2552 } 2553 2554 /* 2555 * If the traced bit got turned off, go back up 2556 * to the top to rescan signals. This ensures 2557 * that p_sig* and p_sigact are consistent. 2558 */ 2559 if ((p->p_flag & P_TRACED) == 0) 2560 continue; 2561 } 2562 2563 prop = sigprop(sig); 2564 2565 /* 2566 * Decide whether the signal should be returned. 2567 * Return the signal's number, or fall through 2568 * to clear it from the pending mask. 2569 */ 2570 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2571 2572 case (intptr_t)SIG_DFL: 2573 /* 2574 * Don't take default actions on system processes. 2575 */ 2576 if (p->p_pid <= 1) { 2577 #ifdef DIAGNOSTIC 2578 /* 2579 * Are you sure you want to ignore SIGSEGV 2580 * in init? XXX 2581 */ 2582 printf("Process (pid %lu) got signal %d\n", 2583 (u_long)p->p_pid, sig); 2584 #endif 2585 break; /* == ignore */ 2586 } 2587 /* 2588 * If there is a pending stop signal to process 2589 * with default action, stop here, 2590 * then clear the signal. However, 2591 * if process is member of an orphaned 2592 * process group, ignore tty stop signals. 2593 */ 2594 if (prop & SA_STOP) { 2595 if (p->p_flag & P_TRACED || 2596 (p->p_pgrp->pg_jobc == 0 && 2597 prop & SA_TTYSTOP)) 2598 break; /* == ignore */ 2599 mtx_unlock(&ps->ps_mtx); 2600 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2601 &p->p_mtx.mtx_object, "Catching SIGSTOP"); 2602 p->p_flag |= P_STOPPED_SIG; 2603 p->p_xstat = sig; 2604 mtx_lock_spin(&sched_lock); 2605 FOREACH_THREAD_IN_PROC(p, td0) { 2606 if (TD_IS_SLEEPING(td0) && 2607 (td0->td_flags & TDF_SINTR) && 2608 !TD_IS_SUSPENDED(td0)) { 2609 thread_suspend_one(td0); 2610 } else if (td != td0) { 2611 td0->td_flags |= TDF_ASTPENDING; 2612 } 2613 } 2614 thread_stopped(p); 2615 thread_suspend_one(td); 2616 PROC_UNLOCK(p); 2617 DROP_GIANT(); 2618 mi_switch(SW_INVOL, NULL); 2619 mtx_unlock_spin(&sched_lock); 2620 PICKUP_GIANT(); 2621 PROC_LOCK(p); 2622 mtx_lock(&ps->ps_mtx); 2623 break; 2624 } else if (prop & SA_IGNORE) { 2625 /* 2626 * Except for SIGCONT, shouldn't get here. 2627 * Default action is to ignore; drop it. 2628 */ 2629 break; /* == ignore */ 2630 } else 2631 return (sig); 2632 /*NOTREACHED*/ 2633 2634 case (intptr_t)SIG_IGN: 2635 /* 2636 * Masking above should prevent us ever trying 2637 * to take action on an ignored signal other 2638 * than SIGCONT, unless process is traced. 2639 */ 2640 if ((prop & SA_CONT) == 0 && 2641 (p->p_flag & P_TRACED) == 0) 2642 printf("issignal\n"); 2643 break; /* == ignore */ 2644 2645 default: 2646 /* 2647 * This signal has an action, let 2648 * postsig() process it. 2649 */ 2650 return (sig); 2651 } 2652 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 2653 } 2654 /* NOTREACHED */ 2655 } 2656 2657 /* 2658 * MPSAFE 2659 */ 2660 void 2661 thread_stopped(struct proc *p) 2662 { 2663 struct proc *p1 = curthread->td_proc; 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 childproc_stopped(p, (p->p_flag & P_TRACED) ? 2676 CLD_TRAPPED : CLD_STOPPED); 2677 PROC_UNLOCK(p->p_pptr); 2678 mtx_lock_spin(&sched_lock); 2679 } 2680 } 2681 2682 /* 2683 * Take the action for the specified signal 2684 * from the current set of pending signals. 2685 */ 2686 void 2687 postsig(sig) 2688 register int sig; 2689 { 2690 struct thread *td = curthread; 2691 register struct proc *p = td->td_proc; 2692 struct sigacts *ps; 2693 sig_t action; 2694 ksiginfo_t ksi; 2695 sigset_t returnmask; 2696 int code; 2697 2698 KASSERT(sig != 0, ("postsig")); 2699 2700 PROC_LOCK_ASSERT(p, MA_OWNED); 2701 ps = p->p_sigacts; 2702 mtx_assert(&ps->ps_mtx, MA_OWNED); 2703 ksiginfo_init(&ksi); 2704 sigqueue_get(&td->td_sigqueue, sig, &ksi); 2705 ksi.ksi_signo = sig; 2706 if (ksi.ksi_code == SI_TIMER) 2707 itimer_accept(p, ksi.ksi_timerid, &ksi); 2708 action = ps->ps_sigact[_SIG_IDX(sig)]; 2709 #ifdef KTRACE 2710 if (KTRPOINT(td, KTR_PSIG)) 2711 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2712 &td->td_oldsigmask : &td->td_sigmask, 0); 2713 #endif 2714 if (p->p_stops & S_SIG) { 2715 mtx_unlock(&ps->ps_mtx); 2716 stopevent(p, S_SIG, sig); 2717 mtx_lock(&ps->ps_mtx); 2718 } 2719 2720 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) { 2721 /* 2722 * Default action, where the default is to kill 2723 * the process. (Other cases were ignored above.) 2724 */ 2725 mtx_unlock(&ps->ps_mtx); 2726 sigexit(td, sig); 2727 /* NOTREACHED */ 2728 } else { 2729 if (td->td_pflags & TDP_SA) { 2730 if (sig == SIGKILL) { 2731 mtx_unlock(&ps->ps_mtx); 2732 sigexit(td, sig); 2733 } 2734 } 2735 2736 /* 2737 * If we get here, the signal must be caught. 2738 */ 2739 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2740 ("postsig action")); 2741 /* 2742 * Set the new mask value and also defer further 2743 * occurrences of this signal. 2744 * 2745 * Special case: user has done a sigsuspend. Here the 2746 * current mask is not of interest, but rather the 2747 * mask from before the sigsuspend is what we want 2748 * restored after the signal processing is completed. 2749 */ 2750 if (td->td_pflags & TDP_OLDMASK) { 2751 returnmask = td->td_oldsigmask; 2752 td->td_pflags &= ~TDP_OLDMASK; 2753 } else 2754 returnmask = td->td_sigmask; 2755 2756 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]); 2757 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 2758 SIGADDSET(td->td_sigmask, sig); 2759 2760 if (SIGISMEMBER(ps->ps_sigreset, sig)) { 2761 /* 2762 * See kern_sigaction() for origin of this code. 2763 */ 2764 SIGDELSET(ps->ps_sigcatch, sig); 2765 if (sig != SIGCONT && 2766 sigprop(sig) & SA_IGNORE) 2767 SIGADDSET(ps->ps_sigignore, sig); 2768 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 2769 } 2770 p->p_stats->p_ru.ru_nsignals++; 2771 if (p->p_sig != sig) { 2772 code = 0; 2773 } else { 2774 code = p->p_code; 2775 p->p_code = 0; 2776 p->p_sig = 0; 2777 } 2778 if (td->td_pflags & TDP_SA) 2779 thread_signal_add(curthread, &ksi); 2780 else 2781 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 2782 } 2783 } 2784 2785 /* 2786 * Kill the current process for stated reason. 2787 */ 2788 void 2789 killproc(p, why) 2790 struct proc *p; 2791 char *why; 2792 { 2793 2794 PROC_LOCK_ASSERT(p, MA_OWNED); 2795 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", 2796 p, p->p_pid, p->p_comm); 2797 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm, 2798 p->p_ucred ? p->p_ucred->cr_uid : -1, why); 2799 psignal(p, SIGKILL); 2800 } 2801 2802 /* 2803 * Force the current process to exit with the specified signal, dumping core 2804 * if appropriate. We bypass the normal tests for masked and caught signals, 2805 * allowing unrecoverable failures to terminate the process without changing 2806 * signal state. Mark the accounting record with the signal termination. 2807 * If dumping core, save the signal number for the debugger. Calls exit and 2808 * does not return. 2809 * 2810 * MPSAFE 2811 */ 2812 void 2813 sigexit(td, sig) 2814 struct thread *td; 2815 int sig; 2816 { 2817 struct proc *p = td->td_proc; 2818 2819 PROC_LOCK_ASSERT(p, MA_OWNED); 2820 p->p_acflag |= AXSIG; 2821 /* 2822 * We must be single-threading to generate a core dump. This 2823 * ensures that the registers in the core file are up-to-date. 2824 * Also, the ELF dump handler assumes that the thread list doesn't 2825 * change out from under it. 2826 * 2827 * XXX If another thread attempts to single-thread before us 2828 * (e.g. via fork()), we won't get a dump at all. 2829 */ 2830 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) { 2831 p->p_sig = sig; 2832 /* 2833 * Log signals which would cause core dumps 2834 * (Log as LOG_INFO to appease those who don't want 2835 * these messages.) 2836 * XXX : Todo, as well as euid, write out ruid too 2837 * Note that coredump() drops proc lock. 2838 */ 2839 if (coredump(td) == 0) 2840 sig |= WCOREFLAG; 2841 if (kern_logsigexit) 2842 log(LOG_INFO, 2843 "pid %d (%s), uid %d: exited on signal %d%s\n", 2844 p->p_pid, p->p_comm, 2845 td->td_ucred ? td->td_ucred->cr_uid : -1, 2846 sig &~ WCOREFLAG, 2847 sig & WCOREFLAG ? " (core dumped)" : ""); 2848 } else 2849 PROC_UNLOCK(p); 2850 exit1(td, W_EXITCODE(0, sig)); 2851 /* NOTREACHED */ 2852 } 2853 2854 /* 2855 * Send queued SIGCHLD to parent when child process's state 2856 * is changed. 2857 */ 2858 static void 2859 sigparent(struct proc *p, int reason, int status) 2860 { 2861 PROC_LOCK_ASSERT(p, MA_OWNED); 2862 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 2863 2864 if (p->p_ksi != NULL) { 2865 p->p_ksi->ksi_signo = SIGCHLD; 2866 p->p_ksi->ksi_code = reason; 2867 p->p_ksi->ksi_status = status; 2868 p->p_ksi->ksi_pid = p->p_pid; 2869 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 2870 if (KSI_ONQ(p->p_ksi)) 2871 return; 2872 } 2873 tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi); 2874 } 2875 2876 static void 2877 childproc_jobstate(struct proc *p, int reason, int status) 2878 { 2879 struct sigacts *ps; 2880 2881 PROC_LOCK_ASSERT(p, MA_OWNED); 2882 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 2883 2884 /* 2885 * Wake up parent sleeping in kern_wait(), also send 2886 * SIGCHLD to parent, but SIGCHLD does not guarantee 2887 * that parent will awake, because parent may masked 2888 * the signal. 2889 */ 2890 p->p_pptr->p_flag |= P_STATCHILD; 2891 wakeup(p->p_pptr); 2892 2893 ps = p->p_pptr->p_sigacts; 2894 mtx_lock(&ps->ps_mtx); 2895 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 2896 mtx_unlock(&ps->ps_mtx); 2897 sigparent(p, reason, status); 2898 } else 2899 mtx_unlock(&ps->ps_mtx); 2900 } 2901 2902 void 2903 childproc_stopped(struct proc *p, int reason) 2904 { 2905 childproc_jobstate(p, reason, p->p_xstat); 2906 } 2907 2908 void 2909 childproc_continued(struct proc *p) 2910 { 2911 childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 2912 } 2913 2914 void 2915 childproc_exited(struct proc *p) 2916 { 2917 int reason; 2918 int status = p->p_xstat; /* convert to int */ 2919 2920 reason = CLD_EXITED; 2921 if (WCOREDUMP(status)) 2922 reason = CLD_DUMPED; 2923 else if (WIFSIGNALED(status)) 2924 reason = CLD_KILLED; 2925 /* 2926 * XXX avoid calling wakeup(p->p_pptr), the work is 2927 * done in exit1(). 2928 */ 2929 sigparent(p, reason, status); 2930 } 2931 2932 static char corefilename[MAXPATHLEN] = {"%N.core"}; 2933 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename, 2934 sizeof(corefilename), "process corefile name format string"); 2935 2936 /* 2937 * expand_name(name, uid, pid) 2938 * Expand the name described in corefilename, using name, uid, and pid. 2939 * corefilename is a printf-like string, with three format specifiers: 2940 * %N name of process ("name") 2941 * %P process id (pid) 2942 * %U user id (uid) 2943 * For example, "%N.core" is the default; they can be disabled completely 2944 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 2945 * This is controlled by the sysctl variable kern.corefile (see above). 2946 */ 2947 2948 static char * 2949 expand_name(name, uid, pid) 2950 const char *name; 2951 uid_t uid; 2952 pid_t pid; 2953 { 2954 const char *format, *appendstr; 2955 char *temp; 2956 char buf[11]; /* Buffer for pid/uid -- max 4B */ 2957 size_t i, l, n; 2958 2959 format = corefilename; 2960 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); 2961 if (temp == NULL) 2962 return (NULL); 2963 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) { 2964 switch (format[i]) { 2965 case '%': /* Format character */ 2966 i++; 2967 switch (format[i]) { 2968 case '%': 2969 appendstr = "%"; 2970 break; 2971 case 'N': /* process name */ 2972 appendstr = name; 2973 break; 2974 case 'P': /* process id */ 2975 sprintf(buf, "%u", pid); 2976 appendstr = buf; 2977 break; 2978 case 'U': /* user id */ 2979 sprintf(buf, "%u", uid); 2980 appendstr = buf; 2981 break; 2982 default: 2983 appendstr = ""; 2984 log(LOG_ERR, 2985 "Unknown format character %c in `%s'\n", 2986 format[i], format); 2987 } 2988 l = strlen(appendstr); 2989 if ((n + l) >= MAXPATHLEN) 2990 goto toolong; 2991 memcpy(temp + n, appendstr, l); 2992 n += l; 2993 break; 2994 default: 2995 temp[n++] = format[i]; 2996 } 2997 } 2998 if (format[i] != '\0') 2999 goto toolong; 3000 return (temp); 3001 toolong: 3002 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n", 3003 (long)pid, name, (u_long)uid); 3004 free(temp, M_TEMP); 3005 return (NULL); 3006 } 3007 3008 /* 3009 * Dump a process' core. The main routine does some 3010 * policy checking, and creates the name of the coredump; 3011 * then it passes on a vnode and a size limit to the process-specific 3012 * coredump routine if there is one; if there _is not_ one, it returns 3013 * ENOSYS; otherwise it returns the error from the process-specific routine. 3014 */ 3015 3016 static int 3017 coredump(struct thread *td) 3018 { 3019 struct proc *p = td->td_proc; 3020 register struct vnode *vp; 3021 register struct ucred *cred = td->td_ucred; 3022 struct flock lf; 3023 struct nameidata nd; 3024 struct vattr vattr; 3025 int error, error1, flags, locked; 3026 struct mount *mp; 3027 char *name; /* name of corefile */ 3028 off_t limit; 3029 3030 PROC_LOCK_ASSERT(p, MA_OWNED); 3031 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3032 _STOPEVENT(p, S_CORE, 0); 3033 3034 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) { 3035 PROC_UNLOCK(p); 3036 return (EFAULT); 3037 } 3038 3039 /* 3040 * Note that the bulk of limit checking is done after 3041 * the corefile is created. The exception is if the limit 3042 * for corefiles is 0, in which case we don't bother 3043 * creating the corefile at all. This layout means that 3044 * a corefile is truncated instead of not being created, 3045 * if it is larger than the limit. 3046 */ 3047 limit = (off_t)lim_cur(p, RLIMIT_CORE); 3048 PROC_UNLOCK(p); 3049 if (limit == 0) 3050 return (EFBIG); 3051 3052 mtx_lock(&Giant); 3053 restart: 3054 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); 3055 if (name == NULL) { 3056 mtx_unlock(&Giant); 3057 return (EINVAL); 3058 } 3059 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */ 3060 flags = O_CREAT | FWRITE | O_NOFOLLOW; 3061 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1); 3062 free(name, M_TEMP); 3063 if (error) { 3064 mtx_unlock(&Giant); 3065 return (error); 3066 } 3067 NDFREE(&nd, NDF_ONLY_PNBUF); 3068 vp = nd.ni_vp; 3069 3070 /* Don't dump to non-regular files or files with links. */ 3071 if (vp->v_type != VREG || 3072 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) { 3073 VOP_UNLOCK(vp, 0, td); 3074 error = EFAULT; 3075 goto out; 3076 } 3077 3078 VOP_UNLOCK(vp, 0, td); 3079 lf.l_whence = SEEK_SET; 3080 lf.l_start = 0; 3081 lf.l_len = 0; 3082 lf.l_type = F_WRLCK; 3083 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 3084 3085 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 3086 lf.l_type = F_UNLCK; 3087 if (locked) 3088 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3089 if ((error = vn_close(vp, FWRITE, cred, td)) != 0) 3090 return (error); 3091 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 3092 return (error); 3093 goto restart; 3094 } 3095 3096 VATTR_NULL(&vattr); 3097 vattr.va_size = 0; 3098 if (set_core_nodump_flag) 3099 vattr.va_flags = UF_NODUMP; 3100 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 3101 VOP_LEASE(vp, td, cred, LEASE_WRITE); 3102 VOP_SETATTR(vp, &vattr, cred, td); 3103 VOP_UNLOCK(vp, 0, td); 3104 PROC_LOCK(p); 3105 p->p_acflag |= ACORE; 3106 PROC_UNLOCK(p); 3107 3108 error = p->p_sysent->sv_coredump ? 3109 p->p_sysent->sv_coredump(td, vp, limit) : 3110 ENOSYS; 3111 3112 if (locked) { 3113 lf.l_type = F_UNLCK; 3114 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3115 } 3116 vn_finished_write(mp); 3117 out: 3118 error1 = vn_close(vp, FWRITE, cred, td); 3119 mtx_unlock(&Giant); 3120 if (error == 0) 3121 error = error1; 3122 return (error); 3123 } 3124 3125 /* 3126 * Nonexistent system call-- signal process (may want to handle it). 3127 * Flag error in case process won't see signal immediately (blocked or ignored). 3128 */ 3129 #ifndef _SYS_SYSPROTO_H_ 3130 struct nosys_args { 3131 int dummy; 3132 }; 3133 #endif 3134 /* 3135 * MPSAFE 3136 */ 3137 /* ARGSUSED */ 3138 int 3139 nosys(td, args) 3140 struct thread *td; 3141 struct nosys_args *args; 3142 { 3143 struct proc *p = td->td_proc; 3144 3145 PROC_LOCK(p); 3146 psignal(p, SIGSYS); 3147 PROC_UNLOCK(p); 3148 return (ENOSYS); 3149 } 3150 3151 /* 3152 * Send a SIGIO or SIGURG signal to a process or process group using 3153 * stored credentials rather than those of the current process. 3154 */ 3155 void 3156 pgsigio(sigiop, sig, checkctty) 3157 struct sigio **sigiop; 3158 int sig, checkctty; 3159 { 3160 struct sigio *sigio; 3161 3162 SIGIO_LOCK(); 3163 sigio = *sigiop; 3164 if (sigio == NULL) { 3165 SIGIO_UNLOCK(); 3166 return; 3167 } 3168 if (sigio->sio_pgid > 0) { 3169 PROC_LOCK(sigio->sio_proc); 3170 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 3171 psignal(sigio->sio_proc, sig); 3172 PROC_UNLOCK(sigio->sio_proc); 3173 } else if (sigio->sio_pgid < 0) { 3174 struct proc *p; 3175 3176 PGRP_LOCK(sigio->sio_pgrp); 3177 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3178 PROC_LOCK(p); 3179 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3180 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 3181 psignal(p, sig); 3182 PROC_UNLOCK(p); 3183 } 3184 PGRP_UNLOCK(sigio->sio_pgrp); 3185 } 3186 SIGIO_UNLOCK(); 3187 } 3188 3189 static int 3190 filt_sigattach(struct knote *kn) 3191 { 3192 struct proc *p = curproc; 3193 3194 kn->kn_ptr.p_proc = p; 3195 kn->kn_flags |= EV_CLEAR; /* automatically set */ 3196 3197 knlist_add(&p->p_klist, kn, 0); 3198 3199 return (0); 3200 } 3201 3202 static void 3203 filt_sigdetach(struct knote *kn) 3204 { 3205 struct proc *p = kn->kn_ptr.p_proc; 3206 3207 knlist_remove(&p->p_klist, kn, 0); 3208 } 3209 3210 /* 3211 * signal knotes are shared with proc knotes, so we apply a mask to 3212 * the hint in order to differentiate them from process hints. This 3213 * could be avoided by using a signal-specific knote list, but probably 3214 * isn't worth the trouble. 3215 */ 3216 static int 3217 filt_signal(struct knote *kn, long hint) 3218 { 3219 3220 if (hint & NOTE_SIGNAL) { 3221 hint &= ~NOTE_SIGNAL; 3222 3223 if (kn->kn_id == hint) 3224 kn->kn_data++; 3225 } 3226 return (kn->kn_data != 0); 3227 } 3228 3229 struct sigacts * 3230 sigacts_alloc(void) 3231 { 3232 struct sigacts *ps; 3233 3234 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3235 ps->ps_refcnt = 1; 3236 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 3237 return (ps); 3238 } 3239 3240 void 3241 sigacts_free(struct sigacts *ps) 3242 { 3243 3244 mtx_lock(&ps->ps_mtx); 3245 ps->ps_refcnt--; 3246 if (ps->ps_refcnt == 0) { 3247 mtx_destroy(&ps->ps_mtx); 3248 free(ps, M_SUBPROC); 3249 } else 3250 mtx_unlock(&ps->ps_mtx); 3251 } 3252 3253 struct sigacts * 3254 sigacts_hold(struct sigacts *ps) 3255 { 3256 mtx_lock(&ps->ps_mtx); 3257 ps->ps_refcnt++; 3258 mtx_unlock(&ps->ps_mtx); 3259 return (ps); 3260 } 3261 3262 void 3263 sigacts_copy(struct sigacts *dest, struct sigacts *src) 3264 { 3265 3266 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 3267 mtx_lock(&src->ps_mtx); 3268 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 3269 mtx_unlock(&src->ps_mtx); 3270 } 3271 3272 int 3273 sigacts_shared(struct sigacts *ps) 3274 { 3275 int shared; 3276 3277 mtx_lock(&ps->ps_mtx); 3278 shared = ps->ps_refcnt > 1; 3279 mtx_unlock(&ps->ps_mtx); 3280 return (shared); 3281 } 3282