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