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