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