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