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