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