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