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