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