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