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