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