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