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 ps = p->p_sigacts; 980 mtx_lock(&ps->ps_mtx); 981 while (SIGNOTEMPTY(ps->ps_sigcatch)) { 982 sig = sig_ffs(&ps->ps_sigcatch); 983 sigdflt(ps, sig); 984 if ((sigprop(sig) & SIGPROP_IGNORE) != 0) 985 sigqueue_delete_proc(p, sig); 986 } 987 988 /* 989 * As CloudABI processes cannot modify signal handlers, fully 990 * reset all signals to their default behavior. Do ignore 991 * SIGPIPE, as it would otherwise be impossible to recover from 992 * writes to broken pipes and sockets. 993 */ 994 if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) { 995 osigignore = ps->ps_sigignore; 996 while (SIGNOTEMPTY(osigignore)) { 997 sig = sig_ffs(&osigignore); 998 SIGDELSET(osigignore, sig); 999 if (sig != SIGPIPE) 1000 sigdflt(ps, sig); 1001 } 1002 SIGADDSET(ps->ps_sigignore, SIGPIPE); 1003 } 1004 1005 /* 1006 * Reset stack state to the user stack. 1007 * Clear set of signals caught on the signal stack. 1008 */ 1009 td = curthread; 1010 MPASS(td->td_proc == p); 1011 td->td_sigstk.ss_flags = SS_DISABLE; 1012 td->td_sigstk.ss_size = 0; 1013 td->td_sigstk.ss_sp = 0; 1014 td->td_pflags &= ~TDP_ALTSTACK; 1015 /* 1016 * Reset no zombies if child dies flag as Solaris does. 1017 */ 1018 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN); 1019 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN) 1020 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL; 1021 mtx_unlock(&ps->ps_mtx); 1022 } 1023 1024 /* 1025 * kern_sigprocmask() 1026 * 1027 * Manipulate signal mask. 1028 */ 1029 int 1030 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, 1031 int flags) 1032 { 1033 sigset_t new_block, oset1; 1034 struct proc *p; 1035 int error; 1036 1037 p = td->td_proc; 1038 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0) 1039 PROC_LOCK_ASSERT(p, MA_OWNED); 1040 else 1041 PROC_LOCK(p); 1042 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 1043 ? MA_OWNED : MA_NOTOWNED); 1044 if (oset != NULL) 1045 *oset = td->td_sigmask; 1046 1047 error = 0; 1048 if (set != NULL) { 1049 switch (how) { 1050 case SIG_BLOCK: 1051 SIG_CANTMASK(*set); 1052 oset1 = td->td_sigmask; 1053 SIGSETOR(td->td_sigmask, *set); 1054 new_block = td->td_sigmask; 1055 SIGSETNAND(new_block, oset1); 1056 break; 1057 case SIG_UNBLOCK: 1058 SIGSETNAND(td->td_sigmask, *set); 1059 signotify(td); 1060 goto out; 1061 case SIG_SETMASK: 1062 SIG_CANTMASK(*set); 1063 oset1 = td->td_sigmask; 1064 if (flags & SIGPROCMASK_OLD) 1065 SIGSETLO(td->td_sigmask, *set); 1066 else 1067 td->td_sigmask = *set; 1068 new_block = td->td_sigmask; 1069 SIGSETNAND(new_block, oset1); 1070 signotify(td); 1071 break; 1072 default: 1073 error = EINVAL; 1074 goto out; 1075 } 1076 1077 /* 1078 * The new_block set contains signals that were not previously 1079 * blocked, but are blocked now. 1080 * 1081 * In case we block any signal that was not previously blocked 1082 * for td, and process has the signal pending, try to schedule 1083 * signal delivery to some thread that does not block the 1084 * signal, possibly waking it up. 1085 */ 1086 if (p->p_numthreads != 1) 1087 reschedule_signals(p, new_block, flags); 1088 } 1089 1090 out: 1091 if (!(flags & SIGPROCMASK_PROC_LOCKED)) 1092 PROC_UNLOCK(p); 1093 return (error); 1094 } 1095 1096 #ifndef _SYS_SYSPROTO_H_ 1097 struct sigprocmask_args { 1098 int how; 1099 const sigset_t *set; 1100 sigset_t *oset; 1101 }; 1102 #endif 1103 int 1104 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap) 1105 { 1106 sigset_t set, oset; 1107 sigset_t *setp, *osetp; 1108 int error; 1109 1110 setp = (uap->set != NULL) ? &set : NULL; 1111 osetp = (uap->oset != NULL) ? &oset : NULL; 1112 if (setp) { 1113 error = copyin(uap->set, setp, sizeof(set)); 1114 if (error) 1115 return (error); 1116 } 1117 error = kern_sigprocmask(td, uap->how, setp, osetp, 0); 1118 if (osetp && !error) { 1119 error = copyout(osetp, uap->oset, sizeof(oset)); 1120 } 1121 return (error); 1122 } 1123 1124 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1125 #ifndef _SYS_SYSPROTO_H_ 1126 struct osigprocmask_args { 1127 int how; 1128 osigset_t mask; 1129 }; 1130 #endif 1131 int 1132 osigprocmask(struct thread *td, struct osigprocmask_args *uap) 1133 { 1134 sigset_t set, oset; 1135 int error; 1136 1137 OSIG2SIG(uap->mask, set); 1138 error = kern_sigprocmask(td, uap->how, &set, &oset, 1); 1139 SIG2OSIG(oset, td->td_retval[0]); 1140 return (error); 1141 } 1142 #endif /* COMPAT_43 */ 1143 1144 int 1145 sys_sigwait(struct thread *td, struct sigwait_args *uap) 1146 { 1147 ksiginfo_t ksi; 1148 sigset_t set; 1149 int error; 1150 1151 error = copyin(uap->set, &set, sizeof(set)); 1152 if (error) { 1153 td->td_retval[0] = error; 1154 return (0); 1155 } 1156 1157 error = kern_sigtimedwait(td, set, &ksi, NULL); 1158 if (error) { 1159 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT) 1160 error = ERESTART; 1161 if (error == ERESTART) 1162 return (error); 1163 td->td_retval[0] = error; 1164 return (0); 1165 } 1166 1167 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo)); 1168 td->td_retval[0] = error; 1169 return (0); 1170 } 1171 1172 int 1173 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap) 1174 { 1175 struct timespec ts; 1176 struct timespec *timeout; 1177 sigset_t set; 1178 ksiginfo_t ksi; 1179 int error; 1180 1181 if (uap->timeout) { 1182 error = copyin(uap->timeout, &ts, sizeof(ts)); 1183 if (error) 1184 return (error); 1185 1186 timeout = &ts; 1187 } else 1188 timeout = NULL; 1189 1190 error = copyin(uap->set, &set, sizeof(set)); 1191 if (error) 1192 return (error); 1193 1194 error = kern_sigtimedwait(td, set, &ksi, timeout); 1195 if (error) 1196 return (error); 1197 1198 if (uap->info) 1199 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1200 1201 if (error == 0) 1202 td->td_retval[0] = ksi.ksi_signo; 1203 return (error); 1204 } 1205 1206 int 1207 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap) 1208 { 1209 ksiginfo_t ksi; 1210 sigset_t set; 1211 int error; 1212 1213 error = copyin(uap->set, &set, sizeof(set)); 1214 if (error) 1215 return (error); 1216 1217 error = kern_sigtimedwait(td, set, &ksi, NULL); 1218 if (error) 1219 return (error); 1220 1221 if (uap->info) 1222 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t)); 1223 1224 if (error == 0) 1225 td->td_retval[0] = ksi.ksi_signo; 1226 return (error); 1227 } 1228 1229 int 1230 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, 1231 struct timespec *timeout) 1232 { 1233 struct sigacts *ps; 1234 sigset_t saved_mask, new_block; 1235 struct proc *p; 1236 int error, sig, timo, timevalid = 0; 1237 struct timespec rts, ets, ts; 1238 struct timeval tv; 1239 1240 p = td->td_proc; 1241 error = 0; 1242 ets.tv_sec = 0; 1243 ets.tv_nsec = 0; 1244 1245 if (timeout != NULL) { 1246 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { 1247 timevalid = 1; 1248 getnanouptime(&rts); 1249 ets = rts; 1250 timespecadd(&ets, timeout); 1251 } 1252 } 1253 ksiginfo_init(ksi); 1254 /* Some signals can not be waited for. */ 1255 SIG_CANTMASK(waitset); 1256 ps = p->p_sigacts; 1257 PROC_LOCK(p); 1258 saved_mask = td->td_sigmask; 1259 SIGSETNAND(td->td_sigmask, waitset); 1260 for (;;) { 1261 mtx_lock(&ps->ps_mtx); 1262 sig = cursig(td); 1263 mtx_unlock(&ps->ps_mtx); 1264 KASSERT(sig >= 0, ("sig %d", sig)); 1265 if (sig != 0 && SIGISMEMBER(waitset, sig)) { 1266 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 || 1267 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) { 1268 error = 0; 1269 break; 1270 } 1271 } 1272 1273 if (error != 0) 1274 break; 1275 1276 /* 1277 * POSIX says this must be checked after looking for pending 1278 * signals. 1279 */ 1280 if (timeout != NULL) { 1281 if (!timevalid) { 1282 error = EINVAL; 1283 break; 1284 } 1285 getnanouptime(&rts); 1286 if (timespeccmp(&rts, &ets, >=)) { 1287 error = EAGAIN; 1288 break; 1289 } 1290 ts = ets; 1291 timespecsub(&ts, &rts); 1292 TIMESPEC_TO_TIMEVAL(&tv, &ts); 1293 timo = tvtohz(&tv); 1294 } else { 1295 timo = 0; 1296 } 1297 1298 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo); 1299 1300 if (timeout != NULL) { 1301 if (error == ERESTART) { 1302 /* Timeout can not be restarted. */ 1303 error = EINTR; 1304 } else if (error == EAGAIN) { 1305 /* We will calculate timeout by ourself. */ 1306 error = 0; 1307 } 1308 } 1309 } 1310 1311 new_block = saved_mask; 1312 SIGSETNAND(new_block, td->td_sigmask); 1313 td->td_sigmask = saved_mask; 1314 /* 1315 * Fewer signals can be delivered to us, reschedule signal 1316 * notification. 1317 */ 1318 if (p->p_numthreads != 1) 1319 reschedule_signals(p, new_block, 0); 1320 1321 if (error == 0) { 1322 SDT_PROBE2(proc, , , signal__clear, sig, ksi); 1323 1324 if (ksi->ksi_code == SI_TIMER) 1325 itimer_accept(p, ksi->ksi_timerid, ksi); 1326 1327 #ifdef KTRACE 1328 if (KTRPOINT(td, KTR_PSIG)) { 1329 sig_t action; 1330 1331 mtx_lock(&ps->ps_mtx); 1332 action = ps->ps_sigact[_SIG_IDX(sig)]; 1333 mtx_unlock(&ps->ps_mtx); 1334 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code); 1335 } 1336 #endif 1337 if (sig == SIGKILL) 1338 sigexit(td, sig); 1339 } 1340 PROC_UNLOCK(p); 1341 return (error); 1342 } 1343 1344 #ifndef _SYS_SYSPROTO_H_ 1345 struct sigpending_args { 1346 sigset_t *set; 1347 }; 1348 #endif 1349 int 1350 sys_sigpending(struct thread *td, struct sigpending_args *uap) 1351 { 1352 struct proc *p = td->td_proc; 1353 sigset_t pending; 1354 1355 PROC_LOCK(p); 1356 pending = p->p_sigqueue.sq_signals; 1357 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1358 PROC_UNLOCK(p); 1359 return (copyout(&pending, uap->set, sizeof(sigset_t))); 1360 } 1361 1362 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1363 #ifndef _SYS_SYSPROTO_H_ 1364 struct osigpending_args { 1365 int dummy; 1366 }; 1367 #endif 1368 int 1369 osigpending(struct thread *td, struct osigpending_args *uap) 1370 { 1371 struct proc *p = td->td_proc; 1372 sigset_t pending; 1373 1374 PROC_LOCK(p); 1375 pending = p->p_sigqueue.sq_signals; 1376 SIGSETOR(pending, td->td_sigqueue.sq_signals); 1377 PROC_UNLOCK(p); 1378 SIG2OSIG(pending, td->td_retval[0]); 1379 return (0); 1380 } 1381 #endif /* COMPAT_43 */ 1382 1383 #if defined(COMPAT_43) 1384 /* 1385 * Generalized interface signal handler, 4.3-compatible. 1386 */ 1387 #ifndef _SYS_SYSPROTO_H_ 1388 struct osigvec_args { 1389 int signum; 1390 struct sigvec *nsv; 1391 struct sigvec *osv; 1392 }; 1393 #endif 1394 /* ARGSUSED */ 1395 int 1396 osigvec(struct thread *td, struct osigvec_args *uap) 1397 { 1398 struct sigvec vec; 1399 struct sigaction nsa, osa; 1400 struct sigaction *nsap, *osap; 1401 int error; 1402 1403 if (uap->signum <= 0 || uap->signum >= ONSIG) 1404 return (EINVAL); 1405 nsap = (uap->nsv != NULL) ? &nsa : NULL; 1406 osap = (uap->osv != NULL) ? &osa : NULL; 1407 if (nsap) { 1408 error = copyin(uap->nsv, &vec, sizeof(vec)); 1409 if (error) 1410 return (error); 1411 nsap->sa_handler = vec.sv_handler; 1412 OSIG2SIG(vec.sv_mask, nsap->sa_mask); 1413 nsap->sa_flags = vec.sv_flags; 1414 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 1415 } 1416 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET); 1417 if (osap && !error) { 1418 vec.sv_handler = osap->sa_handler; 1419 SIG2OSIG(osap->sa_mask, vec.sv_mask); 1420 vec.sv_flags = osap->sa_flags; 1421 vec.sv_flags &= ~SA_NOCLDWAIT; 1422 vec.sv_flags ^= SA_RESTART; 1423 error = copyout(&vec, uap->osv, sizeof(vec)); 1424 } 1425 return (error); 1426 } 1427 1428 #ifndef _SYS_SYSPROTO_H_ 1429 struct osigblock_args { 1430 int mask; 1431 }; 1432 #endif 1433 int 1434 osigblock(struct thread *td, struct osigblock_args *uap) 1435 { 1436 sigset_t set, oset; 1437 1438 OSIG2SIG(uap->mask, set); 1439 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 1440 SIG2OSIG(oset, td->td_retval[0]); 1441 return (0); 1442 } 1443 1444 #ifndef _SYS_SYSPROTO_H_ 1445 struct osigsetmask_args { 1446 int mask; 1447 }; 1448 #endif 1449 int 1450 osigsetmask(struct thread *td, struct osigsetmask_args *uap) 1451 { 1452 sigset_t set, oset; 1453 1454 OSIG2SIG(uap->mask, set); 1455 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 1456 SIG2OSIG(oset, td->td_retval[0]); 1457 return (0); 1458 } 1459 #endif /* COMPAT_43 */ 1460 1461 /* 1462 * Suspend calling thread until signal, providing mask to be set in the 1463 * meantime. 1464 */ 1465 #ifndef _SYS_SYSPROTO_H_ 1466 struct sigsuspend_args { 1467 const sigset_t *sigmask; 1468 }; 1469 #endif 1470 /* ARGSUSED */ 1471 int 1472 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap) 1473 { 1474 sigset_t mask; 1475 int error; 1476 1477 error = copyin(uap->sigmask, &mask, sizeof(mask)); 1478 if (error) 1479 return (error); 1480 return (kern_sigsuspend(td, mask)); 1481 } 1482 1483 int 1484 kern_sigsuspend(struct thread *td, sigset_t mask) 1485 { 1486 struct proc *p = td->td_proc; 1487 int has_sig, sig; 1488 1489 /* 1490 * When returning from sigsuspend, we want 1491 * the old mask to be restored after the 1492 * signal handler has finished. Thus, we 1493 * save it here and mark the sigacts structure 1494 * to indicate this. 1495 */ 1496 PROC_LOCK(p); 1497 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask, 1498 SIGPROCMASK_PROC_LOCKED); 1499 td->td_pflags |= TDP_OLDMASK; 1500 1501 /* 1502 * Process signals now. Otherwise, we can get spurious wakeup 1503 * due to signal entered process queue, but delivered to other 1504 * thread. But sigsuspend should return only on signal 1505 * delivery. 1506 */ 1507 (p->p_sysent->sv_set_syscall_retval)(td, EINTR); 1508 for (has_sig = 0; !has_sig;) { 1509 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 1510 0) == 0) 1511 /* void */; 1512 thread_suspend_check(0); 1513 mtx_lock(&p->p_sigacts->ps_mtx); 1514 while ((sig = cursig(td)) != 0) { 1515 KASSERT(sig >= 0, ("sig %d", sig)); 1516 has_sig += postsig(sig); 1517 } 1518 mtx_unlock(&p->p_sigacts->ps_mtx); 1519 } 1520 PROC_UNLOCK(p); 1521 td->td_errno = EINTR; 1522 td->td_pflags |= TDP_NERRNO; 1523 return (EJUSTRETURN); 1524 } 1525 1526 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */ 1527 /* 1528 * Compatibility sigsuspend call for old binaries. Note nonstandard calling 1529 * convention: libc stub passes mask, not pointer, to save a copyin. 1530 */ 1531 #ifndef _SYS_SYSPROTO_H_ 1532 struct osigsuspend_args { 1533 osigset_t mask; 1534 }; 1535 #endif 1536 /* ARGSUSED */ 1537 int 1538 osigsuspend(struct thread *td, struct osigsuspend_args *uap) 1539 { 1540 sigset_t mask; 1541 1542 OSIG2SIG(uap->mask, mask); 1543 return (kern_sigsuspend(td, mask)); 1544 } 1545 #endif /* COMPAT_43 */ 1546 1547 #if defined(COMPAT_43) 1548 #ifndef _SYS_SYSPROTO_H_ 1549 struct osigstack_args { 1550 struct sigstack *nss; 1551 struct sigstack *oss; 1552 }; 1553 #endif 1554 /* ARGSUSED */ 1555 int 1556 osigstack(struct thread *td, struct osigstack_args *uap) 1557 { 1558 struct sigstack nss, oss; 1559 int error = 0; 1560 1561 if (uap->nss != NULL) { 1562 error = copyin(uap->nss, &nss, sizeof(nss)); 1563 if (error) 1564 return (error); 1565 } 1566 oss.ss_sp = td->td_sigstk.ss_sp; 1567 oss.ss_onstack = sigonstack(cpu_getstack(td)); 1568 if (uap->nss != NULL) { 1569 td->td_sigstk.ss_sp = nss.ss_sp; 1570 td->td_sigstk.ss_size = 0; 1571 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK; 1572 td->td_pflags |= TDP_ALTSTACK; 1573 } 1574 if (uap->oss != NULL) 1575 error = copyout(&oss, uap->oss, sizeof(oss)); 1576 1577 return (error); 1578 } 1579 #endif /* COMPAT_43 */ 1580 1581 #ifndef _SYS_SYSPROTO_H_ 1582 struct sigaltstack_args { 1583 stack_t *ss; 1584 stack_t *oss; 1585 }; 1586 #endif 1587 /* ARGSUSED */ 1588 int 1589 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap) 1590 { 1591 stack_t ss, oss; 1592 int error; 1593 1594 if (uap->ss != NULL) { 1595 error = copyin(uap->ss, &ss, sizeof(ss)); 1596 if (error) 1597 return (error); 1598 } 1599 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL, 1600 (uap->oss != NULL) ? &oss : NULL); 1601 if (error) 1602 return (error); 1603 if (uap->oss != NULL) 1604 error = copyout(&oss, uap->oss, sizeof(stack_t)); 1605 return (error); 1606 } 1607 1608 int 1609 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss) 1610 { 1611 struct proc *p = td->td_proc; 1612 int oonstack; 1613 1614 oonstack = sigonstack(cpu_getstack(td)); 1615 1616 if (oss != NULL) { 1617 *oss = td->td_sigstk; 1618 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK) 1619 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 1620 } 1621 1622 if (ss != NULL) { 1623 if (oonstack) 1624 return (EPERM); 1625 if ((ss->ss_flags & ~SS_DISABLE) != 0) 1626 return (EINVAL); 1627 if (!(ss->ss_flags & SS_DISABLE)) { 1628 if (ss->ss_size < p->p_sysent->sv_minsigstksz) 1629 return (ENOMEM); 1630 1631 td->td_sigstk = *ss; 1632 td->td_pflags |= TDP_ALTSTACK; 1633 } else { 1634 td->td_pflags &= ~TDP_ALTSTACK; 1635 } 1636 } 1637 return (0); 1638 } 1639 1640 /* 1641 * Common code for kill process group/broadcast kill. 1642 * cp is calling process. 1643 */ 1644 static int 1645 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi) 1646 { 1647 struct proc *p; 1648 struct pgrp *pgrp; 1649 int err; 1650 int ret; 1651 1652 ret = ESRCH; 1653 if (all) { 1654 /* 1655 * broadcast 1656 */ 1657 sx_slock(&allproc_lock); 1658 FOREACH_PROC_IN_SYSTEM(p) { 1659 PROC_LOCK(p); 1660 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 1661 p == td->td_proc || p->p_state == PRS_NEW) { 1662 PROC_UNLOCK(p); 1663 continue; 1664 } 1665 err = p_cansignal(td, p, sig); 1666 if (err == 0) { 1667 if (sig) 1668 pksignal(p, sig, ksi); 1669 ret = err; 1670 } 1671 else if (ret == ESRCH) 1672 ret = err; 1673 PROC_UNLOCK(p); 1674 } 1675 sx_sunlock(&allproc_lock); 1676 } else { 1677 sx_slock(&proctree_lock); 1678 if (pgid == 0) { 1679 /* 1680 * zero pgid means send to my process group. 1681 */ 1682 pgrp = td->td_proc->p_pgrp; 1683 PGRP_LOCK(pgrp); 1684 } else { 1685 pgrp = pgfind(pgid); 1686 if (pgrp == NULL) { 1687 sx_sunlock(&proctree_lock); 1688 return (ESRCH); 1689 } 1690 } 1691 sx_sunlock(&proctree_lock); 1692 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1693 PROC_LOCK(p); 1694 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 1695 p->p_state == PRS_NEW) { 1696 PROC_UNLOCK(p); 1697 continue; 1698 } 1699 err = p_cansignal(td, p, sig); 1700 if (err == 0) { 1701 if (sig) 1702 pksignal(p, sig, ksi); 1703 ret = err; 1704 } 1705 else if (ret == ESRCH) 1706 ret = err; 1707 PROC_UNLOCK(p); 1708 } 1709 PGRP_UNLOCK(pgrp); 1710 } 1711 return (ret); 1712 } 1713 1714 #ifndef _SYS_SYSPROTO_H_ 1715 struct kill_args { 1716 int pid; 1717 int signum; 1718 }; 1719 #endif 1720 /* ARGSUSED */ 1721 int 1722 sys_kill(struct thread *td, struct kill_args *uap) 1723 { 1724 ksiginfo_t ksi; 1725 struct proc *p; 1726 int error; 1727 1728 /* 1729 * A process in capability mode can send signals only to himself. 1730 * The main rationale behind this is that abort(3) is implemented as 1731 * kill(getpid(), SIGABRT). 1732 */ 1733 if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid) 1734 return (ECAPMODE); 1735 1736 AUDIT_ARG_SIGNUM(uap->signum); 1737 AUDIT_ARG_PID(uap->pid); 1738 if ((u_int)uap->signum > _SIG_MAXSIG) 1739 return (EINVAL); 1740 1741 ksiginfo_init(&ksi); 1742 ksi.ksi_signo = uap->signum; 1743 ksi.ksi_code = SI_USER; 1744 ksi.ksi_pid = td->td_proc->p_pid; 1745 ksi.ksi_uid = td->td_ucred->cr_ruid; 1746 1747 if (uap->pid > 0) { 1748 /* kill single process */ 1749 if ((p = pfind(uap->pid)) == NULL) { 1750 if ((p = zpfind(uap->pid)) == NULL) 1751 return (ESRCH); 1752 } 1753 AUDIT_ARG_PROCESS(p); 1754 error = p_cansignal(td, p, uap->signum); 1755 if (error == 0 && uap->signum) 1756 pksignal(p, uap->signum, &ksi); 1757 PROC_UNLOCK(p); 1758 return (error); 1759 } 1760 switch (uap->pid) { 1761 case -1: /* broadcast signal */ 1762 return (killpg1(td, uap->signum, 0, 1, &ksi)); 1763 case 0: /* signal own process group */ 1764 return (killpg1(td, uap->signum, 0, 0, &ksi)); 1765 default: /* negative explicit process group */ 1766 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi)); 1767 } 1768 /* NOTREACHED */ 1769 } 1770 1771 int 1772 sys_pdkill(struct thread *td, struct pdkill_args *uap) 1773 { 1774 struct proc *p; 1775 cap_rights_t rights; 1776 int error; 1777 1778 AUDIT_ARG_SIGNUM(uap->signum); 1779 AUDIT_ARG_FD(uap->fd); 1780 if ((u_int)uap->signum > _SIG_MAXSIG) 1781 return (EINVAL); 1782 1783 error = procdesc_find(td, uap->fd, 1784 cap_rights_init(&rights, CAP_PDKILL), &p); 1785 if (error) 1786 return (error); 1787 AUDIT_ARG_PROCESS(p); 1788 error = p_cansignal(td, p, uap->signum); 1789 if (error == 0 && uap->signum) 1790 kern_psignal(p, uap->signum); 1791 PROC_UNLOCK(p); 1792 return (error); 1793 } 1794 1795 #if defined(COMPAT_43) 1796 #ifndef _SYS_SYSPROTO_H_ 1797 struct okillpg_args { 1798 int pgid; 1799 int signum; 1800 }; 1801 #endif 1802 /* ARGSUSED */ 1803 int 1804 okillpg(struct thread *td, struct okillpg_args *uap) 1805 { 1806 ksiginfo_t ksi; 1807 1808 AUDIT_ARG_SIGNUM(uap->signum); 1809 AUDIT_ARG_PID(uap->pgid); 1810 if ((u_int)uap->signum > _SIG_MAXSIG) 1811 return (EINVAL); 1812 1813 ksiginfo_init(&ksi); 1814 ksi.ksi_signo = uap->signum; 1815 ksi.ksi_code = SI_USER; 1816 ksi.ksi_pid = td->td_proc->p_pid; 1817 ksi.ksi_uid = td->td_ucred->cr_ruid; 1818 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi)); 1819 } 1820 #endif /* COMPAT_43 */ 1821 1822 #ifndef _SYS_SYSPROTO_H_ 1823 struct sigqueue_args { 1824 pid_t pid; 1825 int signum; 1826 /* union sigval */ void *value; 1827 }; 1828 #endif 1829 int 1830 sys_sigqueue(struct thread *td, struct sigqueue_args *uap) 1831 { 1832 ksiginfo_t ksi; 1833 struct proc *p; 1834 int error; 1835 1836 if ((u_int)uap->signum > _SIG_MAXSIG) 1837 return (EINVAL); 1838 1839 /* 1840 * Specification says sigqueue can only send signal to 1841 * single process. 1842 */ 1843 if (uap->pid <= 0) 1844 return (EINVAL); 1845 1846 if ((p = pfind(uap->pid)) == NULL) { 1847 if ((p = zpfind(uap->pid)) == NULL) 1848 return (ESRCH); 1849 } 1850 error = p_cansignal(td, p, uap->signum); 1851 if (error == 0 && uap->signum != 0) { 1852 ksiginfo_init(&ksi); 1853 ksi.ksi_flags = KSI_SIGQ; 1854 ksi.ksi_signo = uap->signum; 1855 ksi.ksi_code = SI_QUEUE; 1856 ksi.ksi_pid = td->td_proc->p_pid; 1857 ksi.ksi_uid = td->td_ucred->cr_ruid; 1858 ksi.ksi_value.sival_ptr = uap->value; 1859 error = pksignal(p, ksi.ksi_signo, &ksi); 1860 } 1861 PROC_UNLOCK(p); 1862 return (error); 1863 } 1864 1865 /* 1866 * Send a signal to a process group. 1867 */ 1868 void 1869 gsignal(int pgid, int sig, ksiginfo_t *ksi) 1870 { 1871 struct pgrp *pgrp; 1872 1873 if (pgid != 0) { 1874 sx_slock(&proctree_lock); 1875 pgrp = pgfind(pgid); 1876 sx_sunlock(&proctree_lock); 1877 if (pgrp != NULL) { 1878 pgsignal(pgrp, sig, 0, ksi); 1879 PGRP_UNLOCK(pgrp); 1880 } 1881 } 1882 } 1883 1884 /* 1885 * Send a signal to a process group. If checktty is 1, 1886 * limit to members which have a controlling terminal. 1887 */ 1888 void 1889 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi) 1890 { 1891 struct proc *p; 1892 1893 if (pgrp) { 1894 PGRP_LOCK_ASSERT(pgrp, MA_OWNED); 1895 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1896 PROC_LOCK(p); 1897 if (p->p_state == PRS_NORMAL && 1898 (checkctty == 0 || p->p_flag & P_CONTROLT)) 1899 pksignal(p, sig, ksi); 1900 PROC_UNLOCK(p); 1901 } 1902 } 1903 } 1904 1905 1906 /* 1907 * Recalculate the signal mask and reset the signal disposition after 1908 * usermode frame for delivery is formed. Should be called after 1909 * mach-specific routine, because sysent->sv_sendsig() needs correct 1910 * ps_siginfo and signal mask. 1911 */ 1912 static void 1913 postsig_done(int sig, struct thread *td, struct sigacts *ps) 1914 { 1915 sigset_t mask; 1916 1917 mtx_assert(&ps->ps_mtx, MA_OWNED); 1918 td->td_ru.ru_nsignals++; 1919 mask = ps->ps_catchmask[_SIG_IDX(sig)]; 1920 if (!SIGISMEMBER(ps->ps_signodefer, sig)) 1921 SIGADDSET(mask, sig); 1922 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL, 1923 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED); 1924 if (SIGISMEMBER(ps->ps_sigreset, sig)) 1925 sigdflt(ps, sig); 1926 } 1927 1928 1929 /* 1930 * Send a signal caused by a trap to the current thread. If it will be 1931 * caught immediately, deliver it with correct code. Otherwise, post it 1932 * normally. 1933 */ 1934 void 1935 trapsignal(struct thread *td, ksiginfo_t *ksi) 1936 { 1937 struct sigacts *ps; 1938 struct proc *p; 1939 int sig; 1940 int code; 1941 1942 p = td->td_proc; 1943 sig = ksi->ksi_signo; 1944 code = ksi->ksi_code; 1945 KASSERT(_SIG_VALID(sig), ("invalid signal")); 1946 1947 PROC_LOCK(p); 1948 ps = p->p_sigacts; 1949 mtx_lock(&ps->ps_mtx); 1950 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) && 1951 !SIGISMEMBER(td->td_sigmask, sig)) { 1952 #ifdef KTRACE 1953 if (KTRPOINT(curthread, KTR_PSIG)) 1954 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)], 1955 &td->td_sigmask, code); 1956 #endif 1957 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 1958 ksi, &td->td_sigmask); 1959 postsig_done(sig, td, ps); 1960 mtx_unlock(&ps->ps_mtx); 1961 } else { 1962 /* 1963 * Avoid a possible infinite loop if the thread 1964 * masking the signal or process is ignoring the 1965 * signal. 1966 */ 1967 if (kern_forcesigexit && 1968 (SIGISMEMBER(td->td_sigmask, sig) || 1969 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) { 1970 SIGDELSET(td->td_sigmask, sig); 1971 SIGDELSET(ps->ps_sigcatch, sig); 1972 SIGDELSET(ps->ps_sigignore, sig); 1973 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL; 1974 } 1975 mtx_unlock(&ps->ps_mtx); 1976 p->p_code = code; /* XXX for core dump/debugger */ 1977 p->p_sig = sig; /* XXX to verify code */ 1978 tdsendsignal(p, td, sig, ksi); 1979 } 1980 PROC_UNLOCK(p); 1981 } 1982 1983 static struct thread * 1984 sigtd(struct proc *p, int sig, int prop) 1985 { 1986 struct thread *td, *signal_td; 1987 1988 PROC_LOCK_ASSERT(p, MA_OWNED); 1989 1990 /* 1991 * Check if current thread can handle the signal without 1992 * switching context to another thread. 1993 */ 1994 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig)) 1995 return (curthread); 1996 signal_td = NULL; 1997 FOREACH_THREAD_IN_PROC(p, td) { 1998 if (!SIGISMEMBER(td->td_sigmask, sig)) { 1999 signal_td = td; 2000 break; 2001 } 2002 } 2003 if (signal_td == NULL) 2004 signal_td = FIRST_THREAD_IN_PROC(p); 2005 return (signal_td); 2006 } 2007 2008 /* 2009 * Send the signal to the process. If the signal has an action, the action 2010 * is usually performed by the target process rather than the caller; we add 2011 * the signal to the set of pending signals for the process. 2012 * 2013 * Exceptions: 2014 * o When a stop signal is sent to a sleeping process that takes the 2015 * default action, the process is stopped without awakening it. 2016 * o SIGCONT restarts stopped processes (or puts them back to sleep) 2017 * regardless of the signal action (eg, blocked or ignored). 2018 * 2019 * Other ignored signals are discarded immediately. 2020 * 2021 * NB: This function may be entered from the debugger via the "kill" DDB 2022 * command. There is little that can be done to mitigate the possibly messy 2023 * side effects of this unwise possibility. 2024 */ 2025 void 2026 kern_psignal(struct proc *p, int sig) 2027 { 2028 ksiginfo_t ksi; 2029 2030 ksiginfo_init(&ksi); 2031 ksi.ksi_signo = sig; 2032 ksi.ksi_code = SI_KERNEL; 2033 (void) tdsendsignal(p, NULL, sig, &ksi); 2034 } 2035 2036 int 2037 pksignal(struct proc *p, int sig, ksiginfo_t *ksi) 2038 { 2039 2040 return (tdsendsignal(p, NULL, sig, ksi)); 2041 } 2042 2043 /* Utility function for finding a thread to send signal event to. */ 2044 int 2045 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd) 2046 { 2047 struct thread *td; 2048 2049 if (sigev->sigev_notify == SIGEV_THREAD_ID) { 2050 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid); 2051 if (td == NULL) 2052 return (ESRCH); 2053 *ttd = td; 2054 } else { 2055 *ttd = NULL; 2056 PROC_LOCK(p); 2057 } 2058 return (0); 2059 } 2060 2061 void 2062 tdsignal(struct thread *td, int sig) 2063 { 2064 ksiginfo_t ksi; 2065 2066 ksiginfo_init(&ksi); 2067 ksi.ksi_signo = sig; 2068 ksi.ksi_code = SI_KERNEL; 2069 (void) tdsendsignal(td->td_proc, td, sig, &ksi); 2070 } 2071 2072 void 2073 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi) 2074 { 2075 2076 (void) tdsendsignal(td->td_proc, td, sig, ksi); 2077 } 2078 2079 int 2080 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi) 2081 { 2082 sig_t action; 2083 sigqueue_t *sigqueue; 2084 int prop; 2085 struct sigacts *ps; 2086 int intrval; 2087 int ret = 0; 2088 int wakeup_swapper; 2089 2090 MPASS(td == NULL || p == td->td_proc); 2091 PROC_LOCK_ASSERT(p, MA_OWNED); 2092 2093 if (!_SIG_VALID(sig)) 2094 panic("%s(): invalid signal %d", __func__, sig); 2095 2096 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__)); 2097 2098 /* 2099 * IEEE Std 1003.1-2001: return success when killing a zombie. 2100 */ 2101 if (p->p_state == PRS_ZOMBIE) { 2102 if (ksi && (ksi->ksi_flags & KSI_INS)) 2103 ksiginfo_tryfree(ksi); 2104 return (ret); 2105 } 2106 2107 ps = p->p_sigacts; 2108 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig); 2109 prop = sigprop(sig); 2110 2111 if (td == NULL) { 2112 td = sigtd(p, sig, prop); 2113 sigqueue = &p->p_sigqueue; 2114 } else 2115 sigqueue = &td->td_sigqueue; 2116 2117 SDT_PROBE3(proc, , , signal__send, td, p, sig); 2118 2119 /* 2120 * If the signal is being ignored, 2121 * then we forget about it immediately. 2122 * (Note: we don't set SIGCONT in ps_sigignore, 2123 * and if it is set to SIG_IGN, 2124 * action will be SIG_DFL here.) 2125 */ 2126 mtx_lock(&ps->ps_mtx); 2127 if (SIGISMEMBER(ps->ps_sigignore, sig)) { 2128 SDT_PROBE3(proc, , , signal__discard, td, p, sig); 2129 2130 mtx_unlock(&ps->ps_mtx); 2131 if (ksi && (ksi->ksi_flags & KSI_INS)) 2132 ksiginfo_tryfree(ksi); 2133 return (ret); 2134 } 2135 if (SIGISMEMBER(td->td_sigmask, sig)) 2136 action = SIG_HOLD; 2137 else if (SIGISMEMBER(ps->ps_sigcatch, sig)) 2138 action = SIG_CATCH; 2139 else 2140 action = SIG_DFL; 2141 if (SIGISMEMBER(ps->ps_sigintr, sig)) 2142 intrval = EINTR; 2143 else 2144 intrval = ERESTART; 2145 mtx_unlock(&ps->ps_mtx); 2146 2147 if (prop & SIGPROP_CONT) 2148 sigqueue_delete_stopmask_proc(p); 2149 else if (prop & SIGPROP_STOP) { 2150 /* 2151 * If sending a tty stop signal to a member of an orphaned 2152 * process group, discard the signal here if the action 2153 * is default; don't stop the process below if sleeping, 2154 * and don't clear any pending SIGCONT. 2155 */ 2156 if ((prop & SIGPROP_TTYSTOP) && 2157 (p->p_pgrp->pg_jobc == 0) && 2158 (action == SIG_DFL)) { 2159 if (ksi && (ksi->ksi_flags & KSI_INS)) 2160 ksiginfo_tryfree(ksi); 2161 return (ret); 2162 } 2163 sigqueue_delete_proc(p, SIGCONT); 2164 if (p->p_flag & P_CONTINUED) { 2165 p->p_flag &= ~P_CONTINUED; 2166 PROC_LOCK(p->p_pptr); 2167 sigqueue_take(p->p_ksi); 2168 PROC_UNLOCK(p->p_pptr); 2169 } 2170 } 2171 2172 ret = sigqueue_add(sigqueue, sig, ksi); 2173 if (ret != 0) 2174 return (ret); 2175 signotify(td); 2176 /* 2177 * Defer further processing for signals which are held, 2178 * except that stopped processes must be continued by SIGCONT. 2179 */ 2180 if (action == SIG_HOLD && 2181 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG))) 2182 return (ret); 2183 2184 /* SIGKILL: Remove procfs STOPEVENTs. */ 2185 if (sig == SIGKILL) { 2186 /* from procfs_ioctl.c: PIOCBIC */ 2187 p->p_stops = 0; 2188 /* from procfs_ioctl.c: PIOCCONT */ 2189 p->p_step = 0; 2190 wakeup(&p->p_step); 2191 } 2192 /* 2193 * Some signals have a process-wide effect and a per-thread 2194 * component. Most processing occurs when the process next 2195 * tries to cross the user boundary, however there are some 2196 * times when processing needs to be done immediately, such as 2197 * waking up threads so that they can cross the user boundary. 2198 * We try to do the per-process part here. 2199 */ 2200 if (P_SHOULDSTOP(p)) { 2201 KASSERT(!(p->p_flag & P_WEXIT), 2202 ("signal to stopped but exiting process")); 2203 if (sig == SIGKILL) { 2204 /* 2205 * If traced process is already stopped, 2206 * then no further action is necessary. 2207 */ 2208 if (p->p_flag & P_TRACED) 2209 goto out; 2210 /* 2211 * SIGKILL sets process running. 2212 * It will die elsewhere. 2213 * All threads must be restarted. 2214 */ 2215 p->p_flag &= ~P_STOPPED_SIG; 2216 goto runfast; 2217 } 2218 2219 if (prop & SIGPROP_CONT) { 2220 /* 2221 * If traced process is already stopped, 2222 * then no further action is necessary. 2223 */ 2224 if (p->p_flag & P_TRACED) 2225 goto out; 2226 /* 2227 * If SIGCONT is default (or ignored), we continue the 2228 * process but don't leave the signal in sigqueue as 2229 * it has no further action. If SIGCONT is held, we 2230 * continue the process and leave the signal in 2231 * sigqueue. If the process catches SIGCONT, let it 2232 * handle the signal itself. If it isn't waiting on 2233 * an event, it goes back to run state. 2234 * Otherwise, process goes back to sleep state. 2235 */ 2236 p->p_flag &= ~P_STOPPED_SIG; 2237 PROC_SLOCK(p); 2238 if (p->p_numthreads == p->p_suspcount) { 2239 PROC_SUNLOCK(p); 2240 p->p_flag |= P_CONTINUED; 2241 p->p_xsig = SIGCONT; 2242 PROC_LOCK(p->p_pptr); 2243 childproc_continued(p); 2244 PROC_UNLOCK(p->p_pptr); 2245 PROC_SLOCK(p); 2246 } 2247 if (action == SIG_DFL) { 2248 thread_unsuspend(p); 2249 PROC_SUNLOCK(p); 2250 sigqueue_delete(sigqueue, sig); 2251 goto out; 2252 } 2253 if (action == SIG_CATCH) { 2254 /* 2255 * The process wants to catch it so it needs 2256 * to run at least one thread, but which one? 2257 */ 2258 PROC_SUNLOCK(p); 2259 goto runfast; 2260 } 2261 /* 2262 * The signal is not ignored or caught. 2263 */ 2264 thread_unsuspend(p); 2265 PROC_SUNLOCK(p); 2266 goto out; 2267 } 2268 2269 if (prop & SIGPROP_STOP) { 2270 /* 2271 * If traced process is already stopped, 2272 * then no further action is necessary. 2273 */ 2274 if (p->p_flag & P_TRACED) 2275 goto out; 2276 /* 2277 * Already stopped, don't need to stop again 2278 * (If we did the shell could get confused). 2279 * Just make sure the signal STOP bit set. 2280 */ 2281 p->p_flag |= P_STOPPED_SIG; 2282 sigqueue_delete(sigqueue, sig); 2283 goto out; 2284 } 2285 2286 /* 2287 * All other kinds of signals: 2288 * If a thread is sleeping interruptibly, simulate a 2289 * wakeup so that when it is continued it will be made 2290 * runnable and can look at the signal. However, don't make 2291 * the PROCESS runnable, leave it stopped. 2292 * It may run a bit until it hits a thread_suspend_check(). 2293 */ 2294 wakeup_swapper = 0; 2295 PROC_SLOCK(p); 2296 thread_lock(td); 2297 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) 2298 wakeup_swapper = sleepq_abort(td, intrval); 2299 thread_unlock(td); 2300 PROC_SUNLOCK(p); 2301 if (wakeup_swapper) 2302 kick_proc0(); 2303 goto out; 2304 /* 2305 * Mutexes are short lived. Threads waiting on them will 2306 * hit thread_suspend_check() soon. 2307 */ 2308 } else if (p->p_state == PRS_NORMAL) { 2309 if (p->p_flag & P_TRACED || action == SIG_CATCH) { 2310 tdsigwakeup(td, sig, action, intrval); 2311 goto out; 2312 } 2313 2314 MPASS(action == SIG_DFL); 2315 2316 if (prop & SIGPROP_STOP) { 2317 if (p->p_flag & (P_PPWAIT|P_WEXIT)) 2318 goto out; 2319 p->p_flag |= P_STOPPED_SIG; 2320 p->p_xsig = sig; 2321 PROC_SLOCK(p); 2322 wakeup_swapper = sig_suspend_threads(td, p, 1); 2323 if (p->p_numthreads == p->p_suspcount) { 2324 /* 2325 * only thread sending signal to another 2326 * process can reach here, if thread is sending 2327 * signal to its process, because thread does 2328 * not suspend itself here, p_numthreads 2329 * should never be equal to p_suspcount. 2330 */ 2331 thread_stopped(p); 2332 PROC_SUNLOCK(p); 2333 sigqueue_delete_proc(p, p->p_xsig); 2334 } else 2335 PROC_SUNLOCK(p); 2336 if (wakeup_swapper) 2337 kick_proc0(); 2338 goto out; 2339 } 2340 } else { 2341 /* Not in "NORMAL" state. discard the signal. */ 2342 sigqueue_delete(sigqueue, sig); 2343 goto out; 2344 } 2345 2346 /* 2347 * The process is not stopped so we need to apply the signal to all the 2348 * running threads. 2349 */ 2350 runfast: 2351 tdsigwakeup(td, sig, action, intrval); 2352 PROC_SLOCK(p); 2353 thread_unsuspend(p); 2354 PROC_SUNLOCK(p); 2355 out: 2356 /* If we jump here, proc slock should not be owned. */ 2357 PROC_SLOCK_ASSERT(p, MA_NOTOWNED); 2358 return (ret); 2359 } 2360 2361 /* 2362 * The force of a signal has been directed against a single 2363 * thread. We need to see what we can do about knocking it 2364 * out of any sleep it may be in etc. 2365 */ 2366 static void 2367 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval) 2368 { 2369 struct proc *p = td->td_proc; 2370 register int prop; 2371 int wakeup_swapper; 2372 2373 wakeup_swapper = 0; 2374 PROC_LOCK_ASSERT(p, MA_OWNED); 2375 prop = sigprop(sig); 2376 2377 PROC_SLOCK(p); 2378 thread_lock(td); 2379 /* 2380 * Bring the priority of a thread up if we want it to get 2381 * killed in this lifetime. Be careful to avoid bumping the 2382 * priority of the idle thread, since we still allow to signal 2383 * kernel processes. 2384 */ 2385 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 && 2386 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2387 sched_prio(td, PUSER); 2388 if (TD_ON_SLEEPQ(td)) { 2389 /* 2390 * If thread is sleeping uninterruptibly 2391 * we can't interrupt the sleep... the signal will 2392 * be noticed when the process returns through 2393 * trap() or syscall(). 2394 */ 2395 if ((td->td_flags & TDF_SINTR) == 0) 2396 goto out; 2397 /* 2398 * If SIGCONT is default (or ignored) and process is 2399 * asleep, we are finished; the process should not 2400 * be awakened. 2401 */ 2402 if ((prop & SIGPROP_CONT) && action == SIG_DFL) { 2403 thread_unlock(td); 2404 PROC_SUNLOCK(p); 2405 sigqueue_delete(&p->p_sigqueue, sig); 2406 /* 2407 * It may be on either list in this state. 2408 * Remove from both for now. 2409 */ 2410 sigqueue_delete(&td->td_sigqueue, sig); 2411 return; 2412 } 2413 2414 /* 2415 * Don't awaken a sleeping thread for SIGSTOP if the 2416 * STOP signal is deferred. 2417 */ 2418 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY | 2419 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2420 goto out; 2421 2422 /* 2423 * Give low priority threads a better chance to run. 2424 */ 2425 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td)) 2426 sched_prio(td, PUSER); 2427 2428 wakeup_swapper = sleepq_abort(td, intrval); 2429 } else { 2430 /* 2431 * Other states do nothing with the signal immediately, 2432 * other than kicking ourselves if we are running. 2433 * It will either never be noticed, or noticed very soon. 2434 */ 2435 #ifdef SMP 2436 if (TD_IS_RUNNING(td) && td != curthread) 2437 forward_signal(td); 2438 #endif 2439 } 2440 out: 2441 PROC_SUNLOCK(p); 2442 thread_unlock(td); 2443 if (wakeup_swapper) 2444 kick_proc0(); 2445 } 2446 2447 static int 2448 sig_suspend_threads(struct thread *td, struct proc *p, int sending) 2449 { 2450 struct thread *td2; 2451 int wakeup_swapper; 2452 2453 PROC_LOCK_ASSERT(p, MA_OWNED); 2454 PROC_SLOCK_ASSERT(p, MA_OWNED); 2455 2456 wakeup_swapper = 0; 2457 FOREACH_THREAD_IN_PROC(p, td2) { 2458 thread_lock(td2); 2459 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2460 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) && 2461 (td2->td_flags & TDF_SINTR)) { 2462 if (td2->td_flags & TDF_SBDRY) { 2463 /* 2464 * Once a thread is asleep with 2465 * TDF_SBDRY and without TDF_SERESTART 2466 * or TDF_SEINTR set, it should never 2467 * become suspended due to this check. 2468 */ 2469 KASSERT(!TD_IS_SUSPENDED(td2), 2470 ("thread with deferred stops suspended")); 2471 if (TD_SBDRY_INTR(td2) && sending) { 2472 wakeup_swapper |= sleepq_abort(td2, 2473 TD_SBDRY_ERRNO(td2)); 2474 } 2475 } else if (!TD_IS_SUSPENDED(td2)) { 2476 thread_suspend_one(td2); 2477 } 2478 } else if (!TD_IS_SUSPENDED(td2)) { 2479 if (sending || td != td2) 2480 td2->td_flags |= TDF_ASTPENDING; 2481 #ifdef SMP 2482 if (TD_IS_RUNNING(td2) && td2 != td) 2483 forward_signal(td2); 2484 #endif 2485 } 2486 thread_unlock(td2); 2487 } 2488 return (wakeup_swapper); 2489 } 2490 2491 /* 2492 * Stop the process for an event deemed interesting to the debugger. If si is 2493 * non-NULL, this is a signal exchange; the new signal requested by the 2494 * debugger will be returned for handling. If si is NULL, this is some other 2495 * type of interesting event. The debugger may request a signal be delivered in 2496 * that case as well, however it will be deferred until it can be handled. 2497 */ 2498 int 2499 ptracestop(struct thread *td, int sig, ksiginfo_t *si) 2500 { 2501 struct proc *p = td->td_proc; 2502 struct thread *td2; 2503 ksiginfo_t ksi; 2504 int prop; 2505 2506 PROC_LOCK_ASSERT(p, MA_OWNED); 2507 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process")); 2508 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2509 &p->p_mtx.lock_object, "Stopping for traced signal"); 2510 2511 td->td_xsig = sig; 2512 2513 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) { 2514 td->td_dbgflags |= TDB_XSIG; 2515 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d", 2516 td->td_tid, p->p_pid, td->td_dbgflags, sig); 2517 PROC_SLOCK(p); 2518 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) { 2519 if (P_KILLED(p)) { 2520 /* 2521 * Ensure that, if we've been PT_KILLed, the 2522 * exit status reflects that. Another thread 2523 * may also be in ptracestop(), having just 2524 * received the SIGKILL, but this thread was 2525 * unsuspended first. 2526 */ 2527 td->td_dbgflags &= ~TDB_XSIG; 2528 td->td_xsig = SIGKILL; 2529 p->p_ptevents = 0; 2530 break; 2531 } 2532 if (p->p_flag & P_SINGLE_EXIT && 2533 !(td->td_dbgflags & TDB_EXIT)) { 2534 /* 2535 * Ignore ptrace stops except for thread exit 2536 * events when the process exits. 2537 */ 2538 td->td_dbgflags &= ~TDB_XSIG; 2539 PROC_SUNLOCK(p); 2540 return (0); 2541 } 2542 2543 /* 2544 * Make wait(2) work. Ensure that right after the 2545 * attach, the thread which was decided to become the 2546 * leader of attach gets reported to the waiter. 2547 * Otherwise, just avoid overwriting another thread's 2548 * assignment to p_xthread. If another thread has 2549 * already set p_xthread, the current thread will get 2550 * a chance to report itself upon the next iteration. 2551 */ 2552 if ((td->td_dbgflags & TDB_FSTP) != 0 || 2553 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 && 2554 p->p_xthread == NULL)) { 2555 p->p_xsig = sig; 2556 p->p_xthread = td; 2557 td->td_dbgflags &= ~TDB_FSTP; 2558 p->p_flag2 &= ~P2_PTRACE_FSTP; 2559 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE; 2560 sig_suspend_threads(td, p, 0); 2561 } 2562 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) { 2563 td->td_dbgflags &= ~TDB_STOPATFORK; 2564 cv_broadcast(&p->p_dbgwait); 2565 } 2566 stopme: 2567 thread_suspend_switch(td, p); 2568 if (p->p_xthread == td) 2569 p->p_xthread = NULL; 2570 if (!(p->p_flag & P_TRACED)) 2571 break; 2572 if (td->td_dbgflags & TDB_SUSPEND) { 2573 if (p->p_flag & P_SINGLE_EXIT) 2574 break; 2575 goto stopme; 2576 } 2577 } 2578 PROC_SUNLOCK(p); 2579 } 2580 2581 if (si != NULL && sig == td->td_xsig) { 2582 /* Parent wants us to take the original signal unchanged. */ 2583 si->ksi_flags |= KSI_HEAD; 2584 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0) 2585 si->ksi_signo = 0; 2586 } else if (td->td_xsig != 0) { 2587 /* 2588 * If parent wants us to take a new signal, then it will leave 2589 * it in td->td_xsig; otherwise we just look for signals again. 2590 */ 2591 ksiginfo_init(&ksi); 2592 ksi.ksi_signo = td->td_xsig; 2593 ksi.ksi_flags |= KSI_PTRACE; 2594 prop = sigprop(td->td_xsig); 2595 td2 = sigtd(p, td->td_xsig, prop); 2596 tdsendsignal(p, td2, td->td_xsig, &ksi); 2597 if (td != td2) 2598 return (0); 2599 } 2600 2601 return (td->td_xsig); 2602 } 2603 2604 static void 2605 reschedule_signals(struct proc *p, sigset_t block, int flags) 2606 { 2607 struct sigacts *ps; 2608 struct thread *td; 2609 int sig; 2610 2611 PROC_LOCK_ASSERT(p, MA_OWNED); 2612 ps = p->p_sigacts; 2613 mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ? 2614 MA_OWNED : MA_NOTOWNED); 2615 if (SIGISEMPTY(p->p_siglist)) 2616 return; 2617 SIGSETAND(block, p->p_siglist); 2618 while ((sig = sig_ffs(&block)) != 0) { 2619 SIGDELSET(block, sig); 2620 td = sigtd(p, sig, 0); 2621 signotify(td); 2622 if (!(flags & SIGPROCMASK_PS_LOCKED)) 2623 mtx_lock(&ps->ps_mtx); 2624 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig)) 2625 tdsigwakeup(td, sig, SIG_CATCH, 2626 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : 2627 ERESTART)); 2628 if (!(flags & SIGPROCMASK_PS_LOCKED)) 2629 mtx_unlock(&ps->ps_mtx); 2630 } 2631 } 2632 2633 void 2634 tdsigcleanup(struct thread *td) 2635 { 2636 struct proc *p; 2637 sigset_t unblocked; 2638 2639 p = td->td_proc; 2640 PROC_LOCK_ASSERT(p, MA_OWNED); 2641 2642 sigqueue_flush(&td->td_sigqueue); 2643 if (p->p_numthreads == 1) 2644 return; 2645 2646 /* 2647 * Since we cannot handle signals, notify signal post code 2648 * about this by filling the sigmask. 2649 * 2650 * Also, if needed, wake up thread(s) that do not block the 2651 * same signals as the exiting thread, since the thread might 2652 * have been selected for delivery and woken up. 2653 */ 2654 SIGFILLSET(unblocked); 2655 SIGSETNAND(unblocked, td->td_sigmask); 2656 SIGFILLSET(td->td_sigmask); 2657 reschedule_signals(p, unblocked, 0); 2658 2659 } 2660 2661 static int 2662 sigdeferstop_curr_flags(int cflags) 2663 { 2664 2665 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 || 2666 (cflags & TDF_SBDRY) != 0); 2667 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)); 2668 } 2669 2670 /* 2671 * Defer the delivery of SIGSTOP for the current thread, according to 2672 * the requested mode. Returns previous flags, which must be restored 2673 * by sigallowstop(). 2674 * 2675 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and 2676 * cleared by the current thread, which allow the lock-less read-only 2677 * accesses below. 2678 */ 2679 int 2680 sigdeferstop_impl(int mode) 2681 { 2682 struct thread *td; 2683 int cflags, nflags; 2684 2685 td = curthread; 2686 cflags = sigdeferstop_curr_flags(td->td_flags); 2687 switch (mode) { 2688 case SIGDEFERSTOP_NOP: 2689 nflags = cflags; 2690 break; 2691 case SIGDEFERSTOP_OFF: 2692 nflags = 0; 2693 break; 2694 case SIGDEFERSTOP_SILENT: 2695 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART); 2696 break; 2697 case SIGDEFERSTOP_EINTR: 2698 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART; 2699 break; 2700 case SIGDEFERSTOP_ERESTART: 2701 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR; 2702 break; 2703 default: 2704 panic("sigdeferstop: invalid mode %x", mode); 2705 break; 2706 } 2707 if (cflags == nflags) 2708 return (SIGDEFERSTOP_VAL_NCHG); 2709 thread_lock(td); 2710 td->td_flags = (td->td_flags & ~cflags) | nflags; 2711 thread_unlock(td); 2712 return (cflags); 2713 } 2714 2715 /* 2716 * Restores the STOP handling mode, typically permitting the delivery 2717 * of SIGSTOP for the current thread. This does not immediately 2718 * suspend if a stop was posted. Instead, the thread will suspend 2719 * either via ast() or a subsequent interruptible sleep. 2720 */ 2721 void 2722 sigallowstop_impl(int prev) 2723 { 2724 struct thread *td; 2725 int cflags; 2726 2727 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop")); 2728 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0, 2729 ("sigallowstop: incorrect previous mode %x", prev)); 2730 td = curthread; 2731 cflags = sigdeferstop_curr_flags(td->td_flags); 2732 if (cflags != prev) { 2733 thread_lock(td); 2734 td->td_flags = (td->td_flags & ~cflags) | prev; 2735 thread_unlock(td); 2736 } 2737 } 2738 2739 /* 2740 * If the current process has received a signal (should be caught or cause 2741 * termination, should interrupt current syscall), return the signal number. 2742 * Stop signals with default action are processed immediately, then cleared; 2743 * they aren't returned. This is checked after each entry to the system for 2744 * a syscall or trap (though this can usually be done without calling issignal 2745 * by checking the pending signal masks in cursig.) The normal call 2746 * sequence is 2747 * 2748 * while (sig = cursig(curthread)) 2749 * postsig(sig); 2750 */ 2751 static int 2752 issignal(struct thread *td) 2753 { 2754 struct proc *p; 2755 struct sigacts *ps; 2756 struct sigqueue *queue; 2757 sigset_t sigpending; 2758 int sig, prop; 2759 2760 p = td->td_proc; 2761 ps = p->p_sigacts; 2762 mtx_assert(&ps->ps_mtx, MA_OWNED); 2763 PROC_LOCK_ASSERT(p, MA_OWNED); 2764 for (;;) { 2765 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG); 2766 2767 sigpending = td->td_sigqueue.sq_signals; 2768 SIGSETOR(sigpending, p->p_sigqueue.sq_signals); 2769 SIGSETNAND(sigpending, td->td_sigmask); 2770 2771 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags & 2772 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY) 2773 SIG_STOPSIGMASK(sigpending); 2774 if (SIGISEMPTY(sigpending)) /* no signal to send */ 2775 return (0); 2776 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED && 2777 (p->p_flag2 & P2_PTRACE_FSTP) != 0 && 2778 SIGISMEMBER(sigpending, SIGSTOP)) { 2779 /* 2780 * If debugger just attached, always consume 2781 * SIGSTOP from ptrace(PT_ATTACH) first, to 2782 * execute the debugger attach ritual in 2783 * order. 2784 */ 2785 sig = SIGSTOP; 2786 td->td_dbgflags |= TDB_FSTP; 2787 } else { 2788 sig = sig_ffs(&sigpending); 2789 } 2790 2791 if (p->p_stops & S_SIG) { 2792 mtx_unlock(&ps->ps_mtx); 2793 stopevent(p, S_SIG, sig); 2794 mtx_lock(&ps->ps_mtx); 2795 } 2796 2797 /* 2798 * We should see pending but ignored signals 2799 * only if P_TRACED was on when they were posted. 2800 */ 2801 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) { 2802 sigqueue_delete(&td->td_sigqueue, sig); 2803 sigqueue_delete(&p->p_sigqueue, sig); 2804 continue; 2805 } 2806 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) { 2807 /* 2808 * If traced, always stop. 2809 * Remove old signal from queue before the stop. 2810 * XXX shrug off debugger, it causes siginfo to 2811 * be thrown away. 2812 */ 2813 queue = &td->td_sigqueue; 2814 td->td_dbgksi.ksi_signo = 0; 2815 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) { 2816 queue = &p->p_sigqueue; 2817 sigqueue_get(queue, sig, &td->td_dbgksi); 2818 } 2819 2820 mtx_unlock(&ps->ps_mtx); 2821 sig = ptracestop(td, sig, &td->td_dbgksi); 2822 mtx_lock(&ps->ps_mtx); 2823 2824 /* 2825 * Keep looking if the debugger discarded the signal 2826 * or replaced it with a masked signal. 2827 * 2828 * If the traced bit got turned off, go back up 2829 * to the top to rescan signals. This ensures 2830 * that p_sig* and p_sigact are consistent. 2831 */ 2832 if (sig == 0 || (p->p_flag & P_TRACED) == 0) 2833 continue; 2834 } 2835 2836 prop = sigprop(sig); 2837 2838 /* 2839 * Decide whether the signal should be returned. 2840 * Return the signal's number, or fall through 2841 * to clear it from the pending mask. 2842 */ 2843 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) { 2844 2845 case (intptr_t)SIG_DFL: 2846 /* 2847 * Don't take default actions on system processes. 2848 */ 2849 if (p->p_pid <= 1) { 2850 #ifdef DIAGNOSTIC 2851 /* 2852 * Are you sure you want to ignore SIGSEGV 2853 * in init? XXX 2854 */ 2855 printf("Process (pid %lu) got signal %d\n", 2856 (u_long)p->p_pid, sig); 2857 #endif 2858 break; /* == ignore */ 2859 } 2860 /* 2861 * If there is a pending stop signal to process with 2862 * default action, stop here, then clear the signal. 2863 * Traced or exiting processes should ignore stops. 2864 * Additionally, a member of an orphaned process group 2865 * should ignore tty stops. 2866 */ 2867 if (prop & SIGPROP_STOP) { 2868 if (p->p_flag & 2869 (P_TRACED | P_WEXIT | P_SINGLE_EXIT) || 2870 (p->p_pgrp->pg_jobc == 0 && 2871 prop & SIGPROP_TTYSTOP)) 2872 break; /* == ignore */ 2873 if (TD_SBDRY_INTR(td)) { 2874 KASSERT((td->td_flags & TDF_SBDRY) != 0, 2875 ("lost TDF_SBDRY")); 2876 return (-1); 2877 } 2878 mtx_unlock(&ps->ps_mtx); 2879 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 2880 &p->p_mtx.lock_object, "Catching SIGSTOP"); 2881 sigqueue_delete(&td->td_sigqueue, sig); 2882 sigqueue_delete(&p->p_sigqueue, sig); 2883 p->p_flag |= P_STOPPED_SIG; 2884 p->p_xsig = sig; 2885 PROC_SLOCK(p); 2886 sig_suspend_threads(td, p, 0); 2887 thread_suspend_switch(td, p); 2888 PROC_SUNLOCK(p); 2889 mtx_lock(&ps->ps_mtx); 2890 goto next; 2891 } else if (prop & SIGPROP_IGNORE) { 2892 /* 2893 * Except for SIGCONT, shouldn't get here. 2894 * Default action is to ignore; drop it. 2895 */ 2896 break; /* == ignore */ 2897 } else 2898 return (sig); 2899 /*NOTREACHED*/ 2900 2901 case (intptr_t)SIG_IGN: 2902 /* 2903 * Masking above should prevent us ever trying 2904 * to take action on an ignored signal other 2905 * than SIGCONT, unless process is traced. 2906 */ 2907 if ((prop & SIGPROP_CONT) == 0 && 2908 (p->p_flag & P_TRACED) == 0) 2909 printf("issignal\n"); 2910 break; /* == ignore */ 2911 2912 default: 2913 /* 2914 * This signal has an action, let 2915 * postsig() process it. 2916 */ 2917 return (sig); 2918 } 2919 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */ 2920 sigqueue_delete(&p->p_sigqueue, sig); 2921 next:; 2922 } 2923 /* NOTREACHED */ 2924 } 2925 2926 void 2927 thread_stopped(struct proc *p) 2928 { 2929 int n; 2930 2931 PROC_LOCK_ASSERT(p, MA_OWNED); 2932 PROC_SLOCK_ASSERT(p, MA_OWNED); 2933 n = p->p_suspcount; 2934 if (p == curproc) 2935 n++; 2936 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) { 2937 PROC_SUNLOCK(p); 2938 p->p_flag &= ~P_WAITED; 2939 PROC_LOCK(p->p_pptr); 2940 childproc_stopped(p, (p->p_flag & P_TRACED) ? 2941 CLD_TRAPPED : CLD_STOPPED); 2942 PROC_UNLOCK(p->p_pptr); 2943 PROC_SLOCK(p); 2944 } 2945 } 2946 2947 /* 2948 * Take the action for the specified signal 2949 * from the current set of pending signals. 2950 */ 2951 int 2952 postsig(sig) 2953 register int sig; 2954 { 2955 struct thread *td = curthread; 2956 struct proc *p = td->td_proc; 2957 struct sigacts *ps; 2958 sig_t action; 2959 ksiginfo_t ksi; 2960 sigset_t returnmask; 2961 2962 KASSERT(sig != 0, ("postsig")); 2963 2964 PROC_LOCK_ASSERT(p, MA_OWNED); 2965 ps = p->p_sigacts; 2966 mtx_assert(&ps->ps_mtx, MA_OWNED); 2967 ksiginfo_init(&ksi); 2968 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 && 2969 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0) 2970 return (0); 2971 ksi.ksi_signo = sig; 2972 if (ksi.ksi_code == SI_TIMER) 2973 itimer_accept(p, ksi.ksi_timerid, &ksi); 2974 action = ps->ps_sigact[_SIG_IDX(sig)]; 2975 #ifdef KTRACE 2976 if (KTRPOINT(td, KTR_PSIG)) 2977 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ? 2978 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code); 2979 #endif 2980 if (p->p_stops & S_SIG) { 2981 mtx_unlock(&ps->ps_mtx); 2982 stopevent(p, S_SIG, sig); 2983 mtx_lock(&ps->ps_mtx); 2984 } 2985 2986 if (action == SIG_DFL) { 2987 /* 2988 * Default action, where the default is to kill 2989 * the process. (Other cases were ignored above.) 2990 */ 2991 mtx_unlock(&ps->ps_mtx); 2992 sigexit(td, sig); 2993 /* NOTREACHED */ 2994 } else { 2995 /* 2996 * If we get here, the signal must be caught. 2997 */ 2998 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig), 2999 ("postsig action")); 3000 /* 3001 * Set the new mask value and also defer further 3002 * occurrences of this signal. 3003 * 3004 * Special case: user has done a sigsuspend. Here the 3005 * current mask is not of interest, but rather the 3006 * mask from before the sigsuspend is what we want 3007 * restored after the signal processing is completed. 3008 */ 3009 if (td->td_pflags & TDP_OLDMASK) { 3010 returnmask = td->td_oldsigmask; 3011 td->td_pflags &= ~TDP_OLDMASK; 3012 } else 3013 returnmask = td->td_sigmask; 3014 3015 if (p->p_sig == sig) { 3016 p->p_code = 0; 3017 p->p_sig = 0; 3018 } 3019 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask); 3020 postsig_done(sig, td, ps); 3021 } 3022 return (1); 3023 } 3024 3025 /* 3026 * Kill the current process for stated reason. 3027 */ 3028 void 3029 killproc(struct proc *p, char *why) 3030 { 3031 3032 PROC_LOCK_ASSERT(p, MA_OWNED); 3033 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid, 3034 p->p_comm); 3035 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, 3036 p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why); 3037 p->p_flag |= P_WKILLED; 3038 kern_psignal(p, SIGKILL); 3039 } 3040 3041 /* 3042 * Force the current process to exit with the specified signal, dumping core 3043 * if appropriate. We bypass the normal tests for masked and caught signals, 3044 * allowing unrecoverable failures to terminate the process without changing 3045 * signal state. Mark the accounting record with the signal termination. 3046 * If dumping core, save the signal number for the debugger. Calls exit and 3047 * does not return. 3048 */ 3049 void 3050 sigexit(struct thread *td, int sig) 3051 { 3052 struct proc *p = td->td_proc; 3053 3054 PROC_LOCK_ASSERT(p, MA_OWNED); 3055 p->p_acflag |= AXSIG; 3056 /* 3057 * We must be single-threading to generate a core dump. This 3058 * ensures that the registers in the core file are up-to-date. 3059 * Also, the ELF dump handler assumes that the thread list doesn't 3060 * change out from under it. 3061 * 3062 * XXX If another thread attempts to single-thread before us 3063 * (e.g. via fork()), we won't get a dump at all. 3064 */ 3065 if ((sigprop(sig) & SIGPROP_CORE) && 3066 thread_single(p, SINGLE_NO_EXIT) == 0) { 3067 p->p_sig = sig; 3068 /* 3069 * Log signals which would cause core dumps 3070 * (Log as LOG_INFO to appease those who don't want 3071 * these messages.) 3072 * XXX : Todo, as well as euid, write out ruid too 3073 * Note that coredump() drops proc lock. 3074 */ 3075 if (coredump(td) == 0) 3076 sig |= WCOREFLAG; 3077 if (kern_logsigexit) 3078 log(LOG_INFO, 3079 "pid %d (%s), uid %d: exited on signal %d%s\n", 3080 p->p_pid, p->p_comm, 3081 td->td_ucred ? td->td_ucred->cr_uid : -1, 3082 sig &~ WCOREFLAG, 3083 sig & WCOREFLAG ? " (core dumped)" : ""); 3084 } else 3085 PROC_UNLOCK(p); 3086 exit1(td, 0, sig); 3087 /* NOTREACHED */ 3088 } 3089 3090 /* 3091 * Send queued SIGCHLD to parent when child process's state 3092 * is changed. 3093 */ 3094 static void 3095 sigparent(struct proc *p, int reason, int status) 3096 { 3097 PROC_LOCK_ASSERT(p, MA_OWNED); 3098 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3099 3100 if (p->p_ksi != NULL) { 3101 p->p_ksi->ksi_signo = SIGCHLD; 3102 p->p_ksi->ksi_code = reason; 3103 p->p_ksi->ksi_status = status; 3104 p->p_ksi->ksi_pid = p->p_pid; 3105 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid; 3106 if (KSI_ONQ(p->p_ksi)) 3107 return; 3108 } 3109 pksignal(p->p_pptr, SIGCHLD, p->p_ksi); 3110 } 3111 3112 static void 3113 childproc_jobstate(struct proc *p, int reason, int sig) 3114 { 3115 struct sigacts *ps; 3116 3117 PROC_LOCK_ASSERT(p, MA_OWNED); 3118 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED); 3119 3120 /* 3121 * Wake up parent sleeping in kern_wait(), also send 3122 * SIGCHLD to parent, but SIGCHLD does not guarantee 3123 * that parent will awake, because parent may masked 3124 * the signal. 3125 */ 3126 p->p_pptr->p_flag |= P_STATCHILD; 3127 wakeup(p->p_pptr); 3128 3129 ps = p->p_pptr->p_sigacts; 3130 mtx_lock(&ps->ps_mtx); 3131 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) { 3132 mtx_unlock(&ps->ps_mtx); 3133 sigparent(p, reason, sig); 3134 } else 3135 mtx_unlock(&ps->ps_mtx); 3136 } 3137 3138 void 3139 childproc_stopped(struct proc *p, int reason) 3140 { 3141 3142 childproc_jobstate(p, reason, p->p_xsig); 3143 } 3144 3145 void 3146 childproc_continued(struct proc *p) 3147 { 3148 childproc_jobstate(p, CLD_CONTINUED, SIGCONT); 3149 } 3150 3151 void 3152 childproc_exited(struct proc *p) 3153 { 3154 int reason, status; 3155 3156 if (WCOREDUMP(p->p_xsig)) { 3157 reason = CLD_DUMPED; 3158 status = WTERMSIG(p->p_xsig); 3159 } else if (WIFSIGNALED(p->p_xsig)) { 3160 reason = CLD_KILLED; 3161 status = WTERMSIG(p->p_xsig); 3162 } else { 3163 reason = CLD_EXITED; 3164 status = p->p_xexit; 3165 } 3166 /* 3167 * XXX avoid calling wakeup(p->p_pptr), the work is 3168 * done in exit1(). 3169 */ 3170 sigparent(p, reason, status); 3171 } 3172 3173 /* 3174 * We only have 1 character for the core count in the format 3175 * string, so the range will be 0-9 3176 */ 3177 #define MAX_NUM_CORE_FILES 10 3178 #ifndef NUM_CORE_FILES 3179 #define NUM_CORE_FILES 5 3180 #endif 3181 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES); 3182 static int num_cores = NUM_CORE_FILES; 3183 3184 static int 3185 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS) 3186 { 3187 int error; 3188 int new_val; 3189 3190 new_val = num_cores; 3191 error = sysctl_handle_int(oidp, &new_val, 0, req); 3192 if (error != 0 || req->newptr == NULL) 3193 return (error); 3194 if (new_val > MAX_NUM_CORE_FILES) 3195 new_val = MAX_NUM_CORE_FILES; 3196 if (new_val < 0) 3197 new_val = 0; 3198 num_cores = new_val; 3199 return (0); 3200 } 3201 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW, 3202 0, sizeof(int), sysctl_debug_num_cores_check, "I", ""); 3203 3204 #define GZ_SUFFIX ".gz" 3205 3206 #ifdef GZIO 3207 static int compress_user_cores = 1; 3208 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RWTUN, 3209 &compress_user_cores, 0, "Compression of user corefiles"); 3210 3211 int compress_user_cores_gzlevel = 6; 3212 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN, 3213 &compress_user_cores_gzlevel, 0, "Corefile gzip compression level"); 3214 #else 3215 static int compress_user_cores = 0; 3216 #endif 3217 3218 /* 3219 * Protect the access to corefilename[] by allproc_lock. 3220 */ 3221 #define corefilename_lock allproc_lock 3222 3223 static char corefilename[MAXPATHLEN] = {"%N.core"}; 3224 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename)); 3225 3226 static int 3227 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) 3228 { 3229 int error; 3230 3231 sx_xlock(&corefilename_lock); 3232 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), 3233 req); 3234 sx_xunlock(&corefilename_lock); 3235 3236 return (error); 3237 } 3238 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW | 3239 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", 3240 "Process corefile name format string"); 3241 3242 /* 3243 * corefile_open(comm, uid, pid, td, compress, vpp, namep) 3244 * Expand the name described in corefilename, using name, uid, and pid 3245 * and open/create core file. 3246 * corefilename is a printf-like string, with three format specifiers: 3247 * %N name of process ("name") 3248 * %P process id (pid) 3249 * %U user id (uid) 3250 * For example, "%N.core" is the default; they can be disabled completely 3251 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". 3252 * This is controlled by the sysctl variable kern.corefile (see above). 3253 */ 3254 static int 3255 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, 3256 int compress, struct vnode **vpp, char **namep) 3257 { 3258 struct nameidata nd; 3259 struct sbuf sb; 3260 const char *format; 3261 char *hostname, *name; 3262 int indexpos, i, error, cmode, flags, oflags; 3263 3264 hostname = NULL; 3265 format = corefilename; 3266 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); 3267 indexpos = -1; 3268 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); 3269 sx_slock(&corefilename_lock); 3270 for (i = 0; format[i] != '\0'; i++) { 3271 switch (format[i]) { 3272 case '%': /* Format character */ 3273 i++; 3274 switch (format[i]) { 3275 case '%': 3276 sbuf_putc(&sb, '%'); 3277 break; 3278 case 'H': /* hostname */ 3279 if (hostname == NULL) { 3280 hostname = malloc(MAXHOSTNAMELEN, 3281 M_TEMP, M_WAITOK); 3282 } 3283 getcredhostname(td->td_ucred, hostname, 3284 MAXHOSTNAMELEN); 3285 sbuf_printf(&sb, "%s", hostname); 3286 break; 3287 case 'I': /* autoincrementing index */ 3288 sbuf_printf(&sb, "0"); 3289 indexpos = sbuf_len(&sb) - 1; 3290 break; 3291 case 'N': /* process name */ 3292 sbuf_printf(&sb, "%s", comm); 3293 break; 3294 case 'P': /* process id */ 3295 sbuf_printf(&sb, "%u", pid); 3296 break; 3297 case 'U': /* user id */ 3298 sbuf_printf(&sb, "%u", uid); 3299 break; 3300 default: 3301 log(LOG_ERR, 3302 "Unknown format character %c in " 3303 "corename `%s'\n", format[i], format); 3304 break; 3305 } 3306 break; 3307 default: 3308 sbuf_putc(&sb, format[i]); 3309 break; 3310 } 3311 } 3312 sx_sunlock(&corefilename_lock); 3313 free(hostname, M_TEMP); 3314 if (compress) 3315 sbuf_printf(&sb, GZ_SUFFIX); 3316 if (sbuf_error(&sb) != 0) { 3317 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too " 3318 "long\n", (long)pid, comm, (u_long)uid); 3319 sbuf_delete(&sb); 3320 free(name, M_TEMP); 3321 return (ENOMEM); 3322 } 3323 sbuf_finish(&sb); 3324 sbuf_delete(&sb); 3325 3326 cmode = S_IRUSR | S_IWUSR; 3327 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | 3328 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); 3329 3330 /* 3331 * If the core format has a %I in it, then we need to check 3332 * for existing corefiles before returning a name. 3333 * To do this we iterate over 0..num_cores to find a 3334 * non-existing core file name to use. 3335 */ 3336 if (indexpos != -1) { 3337 for (i = 0; i < num_cores; i++) { 3338 flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW; 3339 name[indexpos] = '0' + i; 3340 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3341 error = vn_open_cred(&nd, &flags, cmode, oflags, 3342 td->td_ucred, NULL); 3343 if (error) { 3344 if (error == EEXIST) 3345 continue; 3346 log(LOG_ERR, 3347 "pid %d (%s), uid (%u): Path `%s' failed " 3348 "on initial open test, error = %d\n", 3349 pid, comm, uid, name, error); 3350 } 3351 goto out; 3352 } 3353 } 3354 3355 flags = O_CREAT | FWRITE | O_NOFOLLOW; 3356 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); 3357 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL); 3358 out: 3359 if (error) { 3360 #ifdef AUDIT 3361 audit_proc_coredump(td, name, error); 3362 #endif 3363 free(name, M_TEMP); 3364 return (error); 3365 } 3366 NDFREE(&nd, NDF_ONLY_PNBUF); 3367 *vpp = nd.ni_vp; 3368 *namep = name; 3369 return (0); 3370 } 3371 3372 static int 3373 coredump_sanitise_path(const char *path) 3374 { 3375 size_t i; 3376 3377 /* 3378 * Only send a subset of ASCII to devd(8) because it 3379 * might pass these strings to sh -c. 3380 */ 3381 for (i = 0; path[i]; i++) 3382 if (!(isalpha(path[i]) || isdigit(path[i])) && 3383 path[i] != '/' && path[i] != '.' && 3384 path[i] != '-') 3385 return (0); 3386 3387 return (1); 3388 } 3389 3390 /* 3391 * Dump a process' core. The main routine does some 3392 * policy checking, and creates the name of the coredump; 3393 * then it passes on a vnode and a size limit to the process-specific 3394 * coredump routine if there is one; if there _is not_ one, it returns 3395 * ENOSYS; otherwise it returns the error from the process-specific routine. 3396 */ 3397 3398 static int 3399 coredump(struct thread *td) 3400 { 3401 struct proc *p = td->td_proc; 3402 struct ucred *cred = td->td_ucred; 3403 struct vnode *vp; 3404 struct flock lf; 3405 struct vattr vattr; 3406 int error, error1, locked; 3407 char *name; /* name of corefile */ 3408 void *rl_cookie; 3409 off_t limit; 3410 char *data = NULL; 3411 char *fullpath, *freepath = NULL; 3412 size_t len; 3413 static const char comm_name[] = "comm="; 3414 static const char core_name[] = "core="; 3415 3416 PROC_LOCK_ASSERT(p, MA_OWNED); 3417 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); 3418 _STOPEVENT(p, S_CORE, 0); 3419 3420 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || 3421 (p->p_flag2 & P2_NOTRACE) != 0) { 3422 PROC_UNLOCK(p); 3423 return (EFAULT); 3424 } 3425 3426 /* 3427 * Note that the bulk of limit checking is done after 3428 * the corefile is created. The exception is if the limit 3429 * for corefiles is 0, in which case we don't bother 3430 * creating the corefile at all. This layout means that 3431 * a corefile is truncated instead of not being created, 3432 * if it is larger than the limit. 3433 */ 3434 limit = (off_t)lim_cur(td, RLIMIT_CORE); 3435 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { 3436 PROC_UNLOCK(p); 3437 return (EFBIG); 3438 } 3439 PROC_UNLOCK(p); 3440 3441 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, 3442 compress_user_cores, &vp, &name); 3443 if (error != 0) 3444 return (error); 3445 3446 /* 3447 * Don't dump to non-regular files or files with links. 3448 * Do not dump into system files. 3449 */ 3450 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || 3451 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0) { 3452 VOP_UNLOCK(vp, 0); 3453 error = EFAULT; 3454 goto out; 3455 } 3456 3457 VOP_UNLOCK(vp, 0); 3458 3459 /* Postpone other writers, including core dumps of other processes. */ 3460 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3461 3462 lf.l_whence = SEEK_SET; 3463 lf.l_start = 0; 3464 lf.l_len = 0; 3465 lf.l_type = F_WRLCK; 3466 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0); 3467 3468 VATTR_NULL(&vattr); 3469 vattr.va_size = 0; 3470 if (set_core_nodump_flag) 3471 vattr.va_flags = UF_NODUMP; 3472 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3473 VOP_SETATTR(vp, &vattr, cred); 3474 VOP_UNLOCK(vp, 0); 3475 PROC_LOCK(p); 3476 p->p_acflag |= ACORE; 3477 PROC_UNLOCK(p); 3478 3479 if (p->p_sysent->sv_coredump != NULL) { 3480 error = p->p_sysent->sv_coredump(td, vp, limit, 3481 compress_user_cores ? IMGACT_CORE_COMPRESS : 0); 3482 } else { 3483 error = ENOSYS; 3484 } 3485 3486 if (locked) { 3487 lf.l_type = F_UNLCK; 3488 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK); 3489 } 3490 vn_rangelock_unlock(vp, rl_cookie); 3491 3492 /* 3493 * Notify the userland helper that a process triggered a core dump. 3494 * This allows the helper to run an automated debugging session. 3495 */ 3496 if (error != 0 || coredump_devctl == 0) 3497 goto out; 3498 len = MAXPATHLEN * 2 + sizeof(comm_name) - 1 + 3499 sizeof(' ') + sizeof(core_name) - 1; 3500 data = malloc(len, M_TEMP, M_WAITOK); 3501 if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0) 3502 goto out; 3503 if (!coredump_sanitise_path(fullpath)) 3504 goto out; 3505 snprintf(data, len, "%s%s ", comm_name, fullpath); 3506 free(freepath, M_TEMP); 3507 freepath = NULL; 3508 if (vn_fullpath_global(td, vp, &fullpath, &freepath) != 0) 3509 goto out; 3510 if (!coredump_sanitise_path(fullpath)) 3511 goto out; 3512 strlcat(data, core_name, len); 3513 strlcat(data, fullpath, len); 3514 devctl_notify("kernel", "signal", "coredump", data); 3515 out: 3516 error1 = vn_close(vp, FWRITE, cred, td); 3517 if (error == 0) 3518 error = error1; 3519 #ifdef AUDIT 3520 audit_proc_coredump(td, name, error); 3521 #endif 3522 free(freepath, M_TEMP); 3523 free(data, M_TEMP); 3524 free(name, M_TEMP); 3525 return (error); 3526 } 3527 3528 /* 3529 * Nonexistent system call-- signal process (may want to handle it). Flag 3530 * error in case process won't see signal immediately (blocked or ignored). 3531 */ 3532 #ifndef _SYS_SYSPROTO_H_ 3533 struct nosys_args { 3534 int dummy; 3535 }; 3536 #endif 3537 /* ARGSUSED */ 3538 int 3539 nosys(struct thread *td, struct nosys_args *args) 3540 { 3541 struct proc *p = td->td_proc; 3542 3543 PROC_LOCK(p); 3544 tdsignal(td, SIGSYS); 3545 PROC_UNLOCK(p); 3546 return (ENOSYS); 3547 } 3548 3549 /* 3550 * Send a SIGIO or SIGURG signal to a process or process group using stored 3551 * credentials rather than those of the current process. 3552 */ 3553 void 3554 pgsigio(struct sigio **sigiop, int sig, int checkctty) 3555 { 3556 ksiginfo_t ksi; 3557 struct sigio *sigio; 3558 3559 ksiginfo_init(&ksi); 3560 ksi.ksi_signo = sig; 3561 ksi.ksi_code = SI_KERNEL; 3562 3563 SIGIO_LOCK(); 3564 sigio = *sigiop; 3565 if (sigio == NULL) { 3566 SIGIO_UNLOCK(); 3567 return; 3568 } 3569 if (sigio->sio_pgid > 0) { 3570 PROC_LOCK(sigio->sio_proc); 3571 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred)) 3572 kern_psignal(sigio->sio_proc, sig); 3573 PROC_UNLOCK(sigio->sio_proc); 3574 } else if (sigio->sio_pgid < 0) { 3575 struct proc *p; 3576 3577 PGRP_LOCK(sigio->sio_pgrp); 3578 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) { 3579 PROC_LOCK(p); 3580 if (p->p_state == PRS_NORMAL && 3581 CANSIGIO(sigio->sio_ucred, p->p_ucred) && 3582 (checkctty == 0 || (p->p_flag & P_CONTROLT))) 3583 kern_psignal(p, sig); 3584 PROC_UNLOCK(p); 3585 } 3586 PGRP_UNLOCK(sigio->sio_pgrp); 3587 } 3588 SIGIO_UNLOCK(); 3589 } 3590 3591 static int 3592 filt_sigattach(struct knote *kn) 3593 { 3594 struct proc *p = curproc; 3595 3596 kn->kn_ptr.p_proc = p; 3597 kn->kn_flags |= EV_CLEAR; /* automatically set */ 3598 3599 knlist_add(p->p_klist, kn, 0); 3600 3601 return (0); 3602 } 3603 3604 static void 3605 filt_sigdetach(struct knote *kn) 3606 { 3607 struct proc *p = kn->kn_ptr.p_proc; 3608 3609 knlist_remove(p->p_klist, kn, 0); 3610 } 3611 3612 /* 3613 * signal knotes are shared with proc knotes, so we apply a mask to 3614 * the hint in order to differentiate them from process hints. This 3615 * could be avoided by using a signal-specific knote list, but probably 3616 * isn't worth the trouble. 3617 */ 3618 static int 3619 filt_signal(struct knote *kn, long hint) 3620 { 3621 3622 if (hint & NOTE_SIGNAL) { 3623 hint &= ~NOTE_SIGNAL; 3624 3625 if (kn->kn_id == hint) 3626 kn->kn_data++; 3627 } 3628 return (kn->kn_data != 0); 3629 } 3630 3631 struct sigacts * 3632 sigacts_alloc(void) 3633 { 3634 struct sigacts *ps; 3635 3636 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO); 3637 refcount_init(&ps->ps_refcnt, 1); 3638 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF); 3639 return (ps); 3640 } 3641 3642 void 3643 sigacts_free(struct sigacts *ps) 3644 { 3645 3646 if (refcount_release(&ps->ps_refcnt) == 0) 3647 return; 3648 mtx_destroy(&ps->ps_mtx); 3649 free(ps, M_SUBPROC); 3650 } 3651 3652 struct sigacts * 3653 sigacts_hold(struct sigacts *ps) 3654 { 3655 3656 refcount_acquire(&ps->ps_refcnt); 3657 return (ps); 3658 } 3659 3660 void 3661 sigacts_copy(struct sigacts *dest, struct sigacts *src) 3662 { 3663 3664 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest")); 3665 mtx_lock(&src->ps_mtx); 3666 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt)); 3667 mtx_unlock(&src->ps_mtx); 3668 } 3669 3670 int 3671 sigacts_shared(struct sigacts *ps) 3672 { 3673 3674 return (ps->ps_refcnt > 1); 3675 } 3676