FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sig.c
1 /* $NetBSD: kern_sig.c,v 1.289.4.6 2010/01/16 17:32:52 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.289.4.6 2010/01/16 17:32:52 bouyer Exp $");
70
71 #include "opt_ptrace.h"
72 #include "opt_compat_sunos.h"
73 #include "opt_compat_netbsd.h"
74 #include "opt_compat_netbsd32.h"
75 #include "opt_pax.h"
76 #include "opt_sa.h"
77
78 #define SIGPROP /* include signal properties table */
79 #include <sys/param.h>
80 #include <sys/signalvar.h>
81 #include <sys/proc.h>
82 #include <sys/systm.h>
83 #include <sys/wait.h>
84 #include <sys/ktrace.h>
85 #include <sys/syslog.h>
86 #include <sys/filedesc.h>
87 #include <sys/file.h>
88 #include <sys/malloc.h>
89 #include <sys/pool.h>
90 #include <sys/ucontext.h>
91 #include <sys/sa.h>
92 #include <sys/savar.h>
93 #include <sys/exec.h>
94 #include <sys/kauth.h>
95 #include <sys/acct.h>
96 #include <sys/callout.h>
97 #include <sys/atomic.h>
98 #include <sys/cpu.h>
99
100 #ifdef PAX_SEGVGUARD
101 #include <sys/pax.h>
102 #endif /* PAX_SEGVGUARD */
103
104 #include <uvm/uvm.h>
105 #include <uvm/uvm_extern.h>
106
107 static void ksiginfo_exechook(struct proc *, void *);
108 static void proc_stop_callout(void *);
109 static int sigchecktrace(void);
110 static int sigpost(struct lwp *, sig_t, int, int, int);
111 static void sigput(sigpend_t *, struct proc *, ksiginfo_t *);
112 static int sigunwait(struct proc *, const ksiginfo_t *);
113 static void sigswitch(bool, int, int);
114
115 sigset_t contsigmask, stopsigmask, sigcantmask;
116 static pool_cache_t sigacts_cache; /* memory pool for sigacts structures */
117 static void sigacts_poolpage_free(struct pool *, void *);
118 static void *sigacts_poolpage_alloc(struct pool *, int);
119 static callout_t proc_stop_ch;
120 static pool_cache_t siginfo_cache;
121 static pool_cache_t ksiginfo_cache;
122
123 static struct pool_allocator sigactspool_allocator = {
124 .pa_alloc = sigacts_poolpage_alloc,
125 .pa_free = sigacts_poolpage_free,
126 };
127
128 #ifdef DEBUG
129 int kern_logsigexit = 1;
130 #else
131 int kern_logsigexit = 0;
132 #endif
133
134 static const char logcoredump[] =
135 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
136 static const char lognocoredump[] =
137 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
138
139 /*
140 * signal_init:
141 *
142 * Initialize global signal-related data structures.
143 */
144 void
145 signal_init(void)
146 {
147
148 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
149
150 sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
151 "sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
152 &sigactspool_allocator : NULL, IPL_NONE, NULL, NULL, NULL);
153
154 siginfo_cache = pool_cache_init(sizeof(siginfo_t), 0, 0, 0,
155 "siginfo", NULL, IPL_NONE, NULL, NULL, NULL);
156
157 ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
158 "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
159
160 exechook_establish(ksiginfo_exechook, NULL);
161
162 callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
163 callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
164 }
165
166 /*
167 * sigacts_poolpage_alloc:
168 *
169 * Allocate a page for the sigacts memory pool.
170 */
171 static void *
172 sigacts_poolpage_alloc(struct pool *pp, int flags)
173 {
174
175 return (void *)uvm_km_alloc(kernel_map,
176 (PAGE_SIZE)*2, (PAGE_SIZE)*2,
177 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
178 | UVM_KMF_WIRED);
179 }
180
181 /*
182 * sigacts_poolpage_free:
183 *
184 * Free a page on behalf of the sigacts memory pool.
185 */
186 static void
187 sigacts_poolpage_free(struct pool *pp, void *v)
188 {
189
190 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED);
191 }
192
193 /*
194 * sigactsinit:
195 *
196 * Create an initial sigctx structure, using the same signal state as
197 * p. If 'share' is set, share the sigctx_proc part, otherwise just
198 * copy it from parent.
199 */
200 struct sigacts *
201 sigactsinit(struct proc *pp, int share)
202 {
203 struct sigacts *ps = pp->p_sigacts, *ps2;
204
205 if (__predict_false(share)) {
206 atomic_inc_uint(&ps->sa_refcnt);
207 return ps;
208 }
209 ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
210 mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
211 ps2->sa_refcnt = 1;
212
213 mutex_enter(&ps->sa_mutex);
214 memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
215 mutex_exit(&ps->sa_mutex);
216 return ps2;
217 }
218
219 /*
220 * sigactsunshare:
221 *
222 * Make this process not share its sigctx, maintaining all
223 * signal state.
224 */
225 void
226 sigactsunshare(struct proc *p)
227 {
228 struct sigacts *ps, *oldps = p->p_sigacts;
229
230 if (__predict_true(oldps->sa_refcnt == 1))
231 return;
232
233 ps = pool_cache_get(sigacts_cache, PR_WAITOK);
234 mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
235 memset(ps->sa_sigdesc, 0, sizeof(ps->sa_sigdesc));
236 ps->sa_refcnt = 1;
237
238 p->p_sigacts = ps;
239 sigactsfree(oldps);
240 }
241
242 /*
243 * sigactsfree;
244 *
245 * Release a sigctx structure.
246 */
247 void
248 sigactsfree(struct sigacts *ps)
249 {
250
251 if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
252 mutex_destroy(&ps->sa_mutex);
253 pool_cache_put(sigacts_cache, ps);
254 }
255 }
256
257 /*
258 * siginit:
259 *
260 * Initialize signal state for process 0; set to ignore signals that
261 * are ignored by default and disable the signal stack. Locking not
262 * required as the system is still cold.
263 */
264 void
265 siginit(struct proc *p)
266 {
267 struct lwp *l;
268 struct sigacts *ps;
269 int signo, prop;
270
271 ps = p->p_sigacts;
272 sigemptyset(&contsigmask);
273 sigemptyset(&stopsigmask);
274 sigemptyset(&sigcantmask);
275 for (signo = 1; signo < NSIG; signo++) {
276 prop = sigprop[signo];
277 if (prop & SA_CONT)
278 sigaddset(&contsigmask, signo);
279 if (prop & SA_STOP)
280 sigaddset(&stopsigmask, signo);
281 if (prop & SA_CANTMASK)
282 sigaddset(&sigcantmask, signo);
283 if (prop & SA_IGNORE && signo != SIGCONT)
284 sigaddset(&p->p_sigctx.ps_sigignore, signo);
285 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
286 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
287 }
288 sigemptyset(&p->p_sigctx.ps_sigcatch);
289 p->p_sflag &= ~PS_NOCLDSTOP;
290
291 ksiginfo_queue_init(&p->p_sigpend.sp_info);
292 sigemptyset(&p->p_sigpend.sp_set);
293
294 /*
295 * Reset per LWP state.
296 */
297 l = LIST_FIRST(&p->p_lwps);
298 l->l_sigwaited = NULL;
299 l->l_sigstk.ss_flags = SS_DISABLE;
300 l->l_sigstk.ss_size = 0;
301 l->l_sigstk.ss_sp = 0;
302 ksiginfo_queue_init(&l->l_sigpend.sp_info);
303 sigemptyset(&l->l_sigpend.sp_set);
304
305 /* One reference. */
306 ps->sa_refcnt = 1;
307 }
308
309 /*
310 * execsigs:
311 *
312 * Reset signals for an exec of the specified process.
313 */
314 void
315 execsigs(struct proc *p)
316 {
317 struct sigacts *ps;
318 struct lwp *l;
319 int signo, prop;
320 sigset_t tset;
321 ksiginfoq_t kq;
322
323 KASSERT(p->p_nlwps == 1);
324
325 sigactsunshare(p);
326 ps = p->p_sigacts;
327
328 /*
329 * Reset caught signals. Held signals remain held through
330 * l->l_sigmask (unless they were caught, and are now ignored
331 * by default).
332 *
333 * No need to lock yet, the process has only one LWP and
334 * at this point the sigacts are private to the process.
335 */
336 sigemptyset(&tset);
337 for (signo = 1; signo < NSIG; signo++) {
338 if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
339 prop = sigprop[signo];
340 if (prop & SA_IGNORE) {
341 if ((prop & SA_CONT) == 0)
342 sigaddset(&p->p_sigctx.ps_sigignore,
343 signo);
344 sigaddset(&tset, signo);
345 }
346 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
347 }
348 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
349 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
350 }
351 ksiginfo_queue_init(&kq);
352
353 mutex_enter(p->p_lock);
354 sigclearall(p, &tset, &kq);
355 sigemptyset(&p->p_sigctx.ps_sigcatch);
356
357 /*
358 * Reset no zombies if child dies flag as Solaris does.
359 */
360 p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
361 if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
362 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
363
364 /*
365 * Reset per-LWP state.
366 */
367 l = LIST_FIRST(&p->p_lwps);
368 l->l_sigwaited = NULL;
369 l->l_sigstk.ss_flags = SS_DISABLE;
370 l->l_sigstk.ss_size = 0;
371 l->l_sigstk.ss_sp = 0;
372 ksiginfo_queue_init(&l->l_sigpend.sp_info);
373 sigemptyset(&l->l_sigpend.sp_set);
374 mutex_exit(p->p_lock);
375
376 ksiginfo_queue_drain(&kq);
377 }
378
379 /*
380 * ksiginfo_exechook:
381 *
382 * Free all pending ksiginfo entries from a process on exec.
383 * Additionally, drain any unused ksiginfo structures in the
384 * system back to the pool.
385 *
386 * XXX This should not be a hook, every process has signals.
387 */
388 static void
389 ksiginfo_exechook(struct proc *p, void *v)
390 {
391 ksiginfoq_t kq;
392
393 ksiginfo_queue_init(&kq);
394
395 mutex_enter(p->p_lock);
396 sigclearall(p, NULL, &kq);
397 mutex_exit(p->p_lock);
398
399 ksiginfo_queue_drain(&kq);
400 }
401
402 /*
403 * ksiginfo_alloc:
404 *
405 * Allocate a new ksiginfo structure from the pool, and optionally copy
406 * an existing one. If the existing ksiginfo_t is from the pool, and
407 * has not been queued somewhere, then just return it. Additionally,
408 * if the existing ksiginfo_t does not contain any information beyond
409 * the signal number, then just return it.
410 */
411 ksiginfo_t *
412 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
413 {
414 ksiginfo_t *kp;
415
416 if (ok != NULL) {
417 if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
418 KSI_FROMPOOL)
419 return ok;
420 if (KSI_EMPTY_P(ok))
421 return ok;
422 }
423
424 kp = pool_cache_get(ksiginfo_cache, flags);
425 if (kp == NULL) {
426 #ifdef DIAGNOSTIC
427 printf("Out of memory allocating ksiginfo for pid %d\n",
428 p->p_pid);
429 #endif
430 return NULL;
431 }
432
433 if (ok != NULL) {
434 memcpy(kp, ok, sizeof(*kp));
435 kp->ksi_flags &= ~KSI_QUEUED;
436 } else
437 KSI_INIT_EMPTY(kp);
438
439 kp->ksi_flags |= KSI_FROMPOOL;
440
441 return kp;
442 }
443
444 /*
445 * ksiginfo_free:
446 *
447 * If the given ksiginfo_t is from the pool and has not been queued,
448 * then free it.
449 */
450 void
451 ksiginfo_free(ksiginfo_t *kp)
452 {
453
454 if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
455 return;
456 pool_cache_put(ksiginfo_cache, kp);
457 }
458
459 /*
460 * ksiginfo_queue_drain:
461 *
462 * Drain a non-empty ksiginfo_t queue.
463 */
464 void
465 ksiginfo_queue_drain0(ksiginfoq_t *kq)
466 {
467 ksiginfo_t *ksi;
468
469 KASSERT(!CIRCLEQ_EMPTY(kq));
470
471 while (!CIRCLEQ_EMPTY(kq)) {
472 ksi = CIRCLEQ_FIRST(kq);
473 CIRCLEQ_REMOVE(kq, ksi, ksi_list);
474 pool_cache_put(ksiginfo_cache, ksi);
475 }
476 }
477
478 /*
479 * sigget:
480 *
481 * Fetch the first pending signal from a set. Optionally, also fetch
482 * or manufacture a ksiginfo element. Returns the number of the first
483 * pending signal, or zero.
484 */
485 int
486 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
487 {
488 ksiginfo_t *ksi;
489 sigset_t tset;
490
491 /* If there's no pending set, the signal is from the debugger. */
492 if (sp == NULL)
493 goto out;
494
495 /* Construct mask from signo, and 'mask'. */
496 if (signo == 0) {
497 if (mask != NULL) {
498 tset = *mask;
499 __sigandset(&sp->sp_set, &tset);
500 } else
501 tset = sp->sp_set;
502
503 /* If there are no signals pending, that's it. */
504 if ((signo = firstsig(&tset)) == 0)
505 goto out;
506 } else {
507 KASSERT(sigismember(&sp->sp_set, signo));
508 }
509
510 sigdelset(&sp->sp_set, signo);
511
512 /* Find siginfo and copy it out. */
513 CIRCLEQ_FOREACH(ksi, &sp->sp_info, ksi_list) {
514 if (ksi->ksi_signo == signo) {
515 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list);
516 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
517 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
518 ksi->ksi_flags &= ~KSI_QUEUED;
519 if (out != NULL) {
520 memcpy(out, ksi, sizeof(*out));
521 out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
522 }
523 ksiginfo_free(ksi);
524 return signo;
525 }
526 }
527
528 out:
529 /* If there's no siginfo, then manufacture it. */
530 if (out != NULL) {
531 KSI_INIT(out);
532 out->ksi_info._signo = signo;
533 out->ksi_info._code = SI_NOINFO;
534 }
535
536 return signo;
537 }
538
539 /*
540 * sigput:
541 *
542 * Append a new ksiginfo element to the list of pending ksiginfo's.
543 */
544 static void
545 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
546 {
547 ksiginfo_t *kp;
548
549 KASSERT(mutex_owned(p->p_lock));
550 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
551
552 sigaddset(&sp->sp_set, ksi->ksi_signo);
553
554 /*
555 * If there is no siginfo, we are done.
556 */
557 if (KSI_EMPTY_P(ksi))
558 return;
559
560 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
561
562 #ifdef notyet /* XXX: QUEUING */
563 if (ksi->ksi_signo < SIGRTMIN)
564 #endif
565 {
566 CIRCLEQ_FOREACH(kp, &sp->sp_info, ksi_list) {
567 if (kp->ksi_signo == ksi->ksi_signo) {
568 KSI_COPY(ksi, kp);
569 kp->ksi_flags |= KSI_QUEUED;
570 return;
571 }
572 }
573 }
574
575 ksi->ksi_flags |= KSI_QUEUED;
576 CIRCLEQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
577 }
578
579 /*
580 * sigclear:
581 *
582 * Clear all pending signals in the specified set.
583 */
584 void
585 sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
586 {
587 ksiginfo_t *ksi, *next;
588
589 if (mask == NULL)
590 sigemptyset(&sp->sp_set);
591 else
592 sigminusset(mask, &sp->sp_set);
593
594 ksi = CIRCLEQ_FIRST(&sp->sp_info);
595 for (; ksi != (void *)&sp->sp_info; ksi = next) {
596 next = CIRCLEQ_NEXT(ksi, ksi_list);
597 if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
598 CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list);
599 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
600 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
601 CIRCLEQ_INSERT_TAIL(kq, ksi, ksi_list);
602 }
603 }
604 }
605
606 /*
607 * sigclearall:
608 *
609 * Clear all pending signals in the specified set from a process and
610 * its LWPs.
611 */
612 void
613 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
614 {
615 struct lwp *l;
616
617 KASSERT(mutex_owned(p->p_lock));
618
619 sigclear(&p->p_sigpend, mask, kq);
620
621 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
622 sigclear(&l->l_sigpend, mask, kq);
623 }
624 }
625
626 /*
627 * sigispending:
628 *
629 * Return true if there are pending signals for the current LWP. May
630 * be called unlocked provided that LW_PENDSIG is set, and that the
631 * signal has been posted to the appopriate queue before LW_PENDSIG is
632 * set.
633 */
634 int
635 sigispending(struct lwp *l, int signo)
636 {
637 struct proc *p = l->l_proc;
638 sigset_t tset;
639
640 membar_consumer();
641
642 tset = l->l_sigpend.sp_set;
643 sigplusset(&p->p_sigpend.sp_set, &tset);
644 sigminusset(&p->p_sigctx.ps_sigignore, &tset);
645 sigminusset(&l->l_sigmask, &tset);
646
647 if (signo == 0) {
648 if (firstsig(&tset) != 0)
649 return EINTR;
650 } else if (sigismember(&tset, signo))
651 return EINTR;
652
653 return 0;
654 }
655
656 /*
657 * siginfo_alloc:
658 *
659 * Allocate a new siginfo_t structure from the pool.
660 */
661 siginfo_t *
662 siginfo_alloc(int flags)
663 {
664
665 return pool_cache_get(siginfo_cache, flags);
666 }
667
668 /*
669 * siginfo_free:
670 *
671 * Return a siginfo_t structure to the pool.
672 */
673 void
674 siginfo_free(void *arg)
675 {
676
677 pool_cache_put(siginfo_cache, arg);
678 }
679
680 void
681 getucontext(struct lwp *l, ucontext_t *ucp)
682 {
683 struct proc *p = l->l_proc;
684
685 KASSERT(mutex_owned(p->p_lock));
686
687 ucp->uc_flags = 0;
688 ucp->uc_link = l->l_ctxlink;
689
690 #if KERN_SA
691 if (p->p_sa != NULL)
692 ucp->uc_sigmask = p->p_sa->sa_sigmask;
693 else
694 #endif /* KERN_SA */
695 ucp->uc_sigmask = l->l_sigmask;
696 ucp->uc_flags |= _UC_SIGMASK;
697
698 /*
699 * The (unsupplied) definition of the `current execution stack'
700 * in the System V Interface Definition appears to allow returning
701 * the main context stack.
702 */
703 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
704 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
705 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
706 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
707 } else {
708 /* Simply copy alternate signal execution stack. */
709 ucp->uc_stack = l->l_sigstk;
710 }
711 ucp->uc_flags |= _UC_STACK;
712 mutex_exit(p->p_lock);
713 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
714 mutex_enter(p->p_lock);
715 }
716
717 /*
718 * getucontext_sa:
719 * Get a ucontext_t for use in SA upcall generation.
720 * Teweaked version of getucontext(). We 1) do not take p_lock, 2)
721 * fudge things with uc_link (which is usually NULL for libpthread
722 * code), and 3) we report an empty signal mask.
723 */
724 void
725 getucontext_sa(struct lwp *l, ucontext_t *ucp)
726 {
727 ucp->uc_flags = 0;
728 ucp->uc_link = l->l_ctxlink;
729
730 sigemptyset(&ucp->uc_sigmask);
731 ucp->uc_flags |= _UC_SIGMASK;
732
733 /*
734 * The (unsupplied) definition of the `current execution stack'
735 * in the System V Interface Definition appears to allow returning
736 * the main context stack.
737 */
738 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
739 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
740 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
741 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
742 } else {
743 /* Simply copy alternate signal execution stack. */
744 ucp->uc_stack = l->l_sigstk;
745 }
746 ucp->uc_flags |= _UC_STACK;
747 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
748 }
749
750 int
751 setucontext(struct lwp *l, const ucontext_t *ucp)
752 {
753 struct proc *p = l->l_proc;
754 int error;
755
756 KASSERT(mutex_owned(p->p_lock));
757
758 if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
759 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
760 if (error != 0)
761 return error;
762 }
763
764 mutex_exit(p->p_lock);
765 error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
766 mutex_enter(p->p_lock);
767 if (error != 0)
768 return (error);
769
770 l->l_ctxlink = ucp->uc_link;
771
772 /*
773 * If there was stack information, update whether or not we are
774 * still running on an alternate signal stack.
775 */
776 if ((ucp->uc_flags & _UC_STACK) != 0) {
777 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
778 l->l_sigstk.ss_flags |= SS_ONSTACK;
779 else
780 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
781 }
782
783 return 0;
784 }
785
786 /*
787 * Common code for kill process group/broadcast kill. cp is calling
788 * process.
789 */
790 int
791 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
792 {
793 struct proc *p, *cp;
794 kauth_cred_t pc;
795 struct pgrp *pgrp;
796 int nfound;
797 int signo = ksi->ksi_signo;
798
799 cp = l->l_proc;
800 pc = l->l_cred;
801 nfound = 0;
802
803 mutex_enter(proc_lock);
804 if (all) {
805 /*
806 * broadcast
807 */
808 PROCLIST_FOREACH(p, &allproc) {
809 if (p->p_pid <= 1 || p == cp ||
810 p->p_flag & (PK_SYSTEM|PK_MARKER))
811 continue;
812 mutex_enter(p->p_lock);
813 if (kauth_authorize_process(pc,
814 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
815 NULL) == 0) {
816 nfound++;
817 if (signo)
818 kpsignal2(p, ksi);
819 }
820 mutex_exit(p->p_lock);
821 }
822 } else {
823 if (pgid == 0)
824 /*
825 * zero pgid means send to my process group.
826 */
827 pgrp = cp->p_pgrp;
828 else {
829 pgrp = pg_find(pgid, PFIND_LOCKED);
830 if (pgrp == NULL)
831 goto out;
832 }
833 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
834 if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
835 continue;
836 mutex_enter(p->p_lock);
837 if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
838 p, KAUTH_ARG(signo), NULL, NULL) == 0) {
839 nfound++;
840 if (signo && P_ZOMBIE(p) == 0)
841 kpsignal2(p, ksi);
842 }
843 mutex_exit(p->p_lock);
844 }
845 }
846 out:
847 mutex_exit(proc_lock);
848 return (nfound ? 0 : ESRCH);
849 }
850
851 /*
852 * Send a signal to a process group. If checktty is 1, limit to members
853 * which have a controlling terminal.
854 */
855 void
856 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
857 {
858 ksiginfo_t ksi;
859
860 KASSERT(!cpu_intr_p());
861 KASSERT(mutex_owned(proc_lock));
862
863 KSI_INIT_EMPTY(&ksi);
864 ksi.ksi_signo = sig;
865 kpgsignal(pgrp, &ksi, NULL, checkctty);
866 }
867
868 void
869 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
870 {
871 struct proc *p;
872
873 KASSERT(!cpu_intr_p());
874 KASSERT(mutex_owned(proc_lock));
875
876 if (__predict_false(pgrp == 0))
877 return;
878 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
879 if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
880 kpsignal(p, ksi, data);
881 }
882
883 /*
884 * Send a signal caused by a trap to the current LWP. If it will be caught
885 * immediately, deliver it with correct code. Otherwise, post it normally.
886 */
887 void
888 trapsignal(struct lwp *l, ksiginfo_t *ksi)
889 {
890 struct proc *p;
891 struct sigacts *ps;
892 int signo = ksi->ksi_signo;
893 sigset_t *mask;
894
895 KASSERT(KSI_TRAP_P(ksi));
896
897 ksi->ksi_lid = l->l_lid;
898 p = l->l_proc;
899
900 KASSERT(!cpu_intr_p());
901 mutex_enter(proc_lock);
902 mutex_enter(p->p_lock);
903 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
904 ps = p->p_sigacts;
905 if ((p->p_slflag & PSL_TRACED) == 0 &&
906 sigismember(&p->p_sigctx.ps_sigcatch, signo) &&
907 !sigismember(mask, signo)) {
908 mutex_exit(proc_lock);
909 l->l_ru.ru_nsignals++;
910 kpsendsig(l, ksi, mask);
911 mutex_exit(p->p_lock);
912 ktrpsig(signo, SIGACTION_PS(ps, signo).sa_handler,
913 mask, ksi);
914 } else {
915 /* XXX for core dump/debugger */
916 p->p_sigctx.ps_lwp = l->l_lid;
917 p->p_sigctx.ps_signo = ksi->ksi_signo;
918 p->p_sigctx.ps_code = ksi->ksi_trap;
919 kpsignal2(p, ksi);
920 mutex_exit(p->p_lock);
921 mutex_exit(proc_lock);
922 }
923 }
924
925 /*
926 * Fill in signal information and signal the parent for a child status change.
927 */
928 void
929 child_psignal(struct proc *p, int mask)
930 {
931 ksiginfo_t ksi;
932 struct proc *q;
933 int xstat;
934
935 KASSERT(mutex_owned(proc_lock));
936 KASSERT(mutex_owned(p->p_lock));
937
938 xstat = p->p_xstat;
939
940 KSI_INIT(&ksi);
941 ksi.ksi_signo = SIGCHLD;
942 ksi.ksi_code = (xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
943 ksi.ksi_pid = p->p_pid;
944 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
945 ksi.ksi_status = xstat;
946 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
947 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
948
949 q = p->p_pptr;
950
951 mutex_exit(p->p_lock);
952 mutex_enter(q->p_lock);
953
954 if ((q->p_sflag & mask) == 0)
955 kpsignal2(q, &ksi);
956
957 mutex_exit(q->p_lock);
958 mutex_enter(p->p_lock);
959 }
960
961 void
962 psignal(struct proc *p, int signo)
963 {
964 ksiginfo_t ksi;
965
966 KASSERT(!cpu_intr_p());
967 KASSERT(mutex_owned(proc_lock));
968
969 KSI_INIT_EMPTY(&ksi);
970 ksi.ksi_signo = signo;
971 mutex_enter(p->p_lock);
972 kpsignal2(p, &ksi);
973 mutex_exit(p->p_lock);
974 }
975
976 void
977 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
978 {
979 fdfile_t *ff;
980 file_t *fp;
981
982 KASSERT(!cpu_intr_p());
983 KASSERT(mutex_owned(proc_lock));
984
985 if ((p->p_sflag & PS_WEXIT) == 0 && data) {
986 size_t fd;
987 filedesc_t *fdp = p->p_fd;
988
989 /* XXXSMP locking */
990 ksi->ksi_fd = -1;
991 for (fd = 0; fd < fdp->fd_nfiles; fd++) {
992 if ((ff = fdp->fd_ofiles[fd]) == NULL)
993 continue;
994 if ((fp = ff->ff_file) == NULL)
995 continue;
996 if (fp->f_data == data) {
997 ksi->ksi_fd = fd;
998 break;
999 }
1000 }
1001 }
1002 mutex_enter(p->p_lock);
1003 kpsignal2(p, ksi);
1004 mutex_exit(p->p_lock);
1005 }
1006
1007 /*
1008 * sigismasked:
1009 *
1010 * Returns true if signal is ignored or masked for the specified LWP.
1011 */
1012 int
1013 sigismasked(struct lwp *l, int sig)
1014 {
1015 struct proc *p = l->l_proc;
1016
1017 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1018 sigismember(&l->l_sigmask, sig)
1019 #if KERN_SA
1020 || ((p->p_sa != NULL) && sigismember(&p->p_sa->sa_sigmask, sig))
1021 #endif /* KERN_SA */
1022 );
1023 }
1024
1025 /*
1026 * sigpost:
1027 *
1028 * Post a pending signal to an LWP. Returns non-zero if the LWP may
1029 * be able to take the signal.
1030 */
1031 static int
1032 sigpost(struct lwp *l, sig_t action, int prop, int sig, int idlecheck)
1033 {
1034 int rv, masked;
1035 struct proc *p = l->l_proc;
1036
1037 KASSERT(mutex_owned(p->p_lock));
1038
1039 /*
1040 * If the LWP is on the way out, sigclear() will be busy draining all
1041 * pending signals. Don't give it more.
1042 */
1043 if (l->l_refcnt == 0)
1044 return 0;
1045
1046 /*
1047 * Have the LWP check for signals. This ensures that even if no LWP
1048 * is found to take the signal immediately, it should be taken soon.
1049 */
1050 lwp_lock(l);
1051 l->l_flag |= LW_PENDSIG;
1052
1053 /*
1054 * When sending signals to SA processes, we first try to find an
1055 * idle VP to take it.
1056 */
1057 if (idlecheck && (l->l_flag & (LW_SA_IDLE | LW_SA_YIELD)) == 0) {
1058 lwp_unlock(l);
1059 return 0;
1060 }
1061
1062 /*
1063 * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1064 * Note: SIGKILL and SIGSTOP cannot be masked.
1065 */
1066 #if KERN_SA
1067 if (p->p_sa != NULL)
1068 masked = sigismember(&p->p_sa->sa_sigmask, sig);
1069 else
1070 #endif
1071 masked = sigismember(&l->l_sigmask, sig);
1072 if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
1073 lwp_unlock(l);
1074 return 0;
1075 }
1076
1077 /*
1078 * If killing the process, make it run fast.
1079 */
1080 if (__predict_false((prop & SA_KILL) != 0) &&
1081 action == SIG_DFL && l->l_priority < MAXPRI_USER) {
1082 KASSERT(l->l_class == SCHED_OTHER);
1083 lwp_changepri(l, MAXPRI_USER);
1084 }
1085
1086 /*
1087 * If the LWP is running or on a run queue, then we win. If it's
1088 * sleeping interruptably, wake it and make it take the signal. If
1089 * the sleep isn't interruptable, then the chances are it will get
1090 * to see the signal soon anyhow. If suspended, it can't take the
1091 * signal right now. If it's LWP private or for all LWPs, save it
1092 * for later; otherwise punt.
1093 */
1094 rv = 0;
1095
1096 switch (l->l_stat) {
1097 case LSRUN:
1098 case LSONPROC:
1099 lwp_need_userret(l);
1100 rv = 1;
1101 break;
1102
1103 case LSSLEEP:
1104 if ((l->l_flag & LW_SINTR) != 0) {
1105 /* setrunnable() will release the lock. */
1106 setrunnable(l);
1107 return 1;
1108 }
1109 break;
1110
1111 case LSSUSPENDED:
1112 if ((prop & SA_KILL) != 0) {
1113 /* lwp_continue() will release the lock. */
1114 lwp_continue(l);
1115 return 1;
1116 }
1117 break;
1118
1119 case LSSTOP:
1120 if ((prop & SA_STOP) != 0)
1121 break;
1122
1123 /*
1124 * If the LWP is stopped and we are sending a continue
1125 * signal, then start it again.
1126 */
1127 if ((prop & SA_CONT) != 0) {
1128 if (l->l_wchan != NULL) {
1129 l->l_stat = LSSLEEP;
1130 p->p_nrlwps++;
1131 rv = 1;
1132 break;
1133 }
1134 /* setrunnable() will release the lock. */
1135 setrunnable(l);
1136 return 1;
1137 } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1138 /* setrunnable() will release the lock. */
1139 setrunnable(l);
1140 return 1;
1141 }
1142 break;
1143
1144 default:
1145 break;
1146 }
1147
1148 lwp_unlock(l);
1149 return rv;
1150 }
1151
1152 /*
1153 * Notify an LWP that it has a pending signal.
1154 */
1155 void
1156 signotify(struct lwp *l)
1157 {
1158 KASSERT(lwp_locked(l, NULL));
1159
1160 l->l_flag |= LW_PENDSIG;
1161 lwp_need_userret(l);
1162 }
1163
1164 /*
1165 * Find an LWP within process p that is waiting on signal ksi, and hand
1166 * it on.
1167 */
1168 static int
1169 sigunwait(struct proc *p, const ksiginfo_t *ksi)
1170 {
1171 struct lwp *l;
1172 int signo;
1173
1174 KASSERT(mutex_owned(p->p_lock));
1175
1176 signo = ksi->ksi_signo;
1177
1178 if (ksi->ksi_lid != 0) {
1179 /*
1180 * Signal came via _lwp_kill(). Find the LWP and see if
1181 * it's interested.
1182 */
1183 if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
1184 return 0;
1185 if (l->l_sigwaited == NULL ||
1186 !sigismember(&l->l_sigwaitset, signo))
1187 return 0;
1188 } else {
1189 /*
1190 * Look for any LWP that may be interested.
1191 */
1192 LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
1193 KASSERT(l->l_sigwaited != NULL);
1194 if (sigismember(&l->l_sigwaitset, signo))
1195 break;
1196 }
1197 }
1198
1199 if (l != NULL) {
1200 l->l_sigwaited->ksi_info = ksi->ksi_info;
1201 l->l_sigwaited = NULL;
1202 LIST_REMOVE(l, l_sigwaiter);
1203 cv_signal(&l->l_sigcv);
1204 return 1;
1205 }
1206
1207 return 0;
1208 }
1209
1210 /*
1211 * Send the signal to the process. If the signal has an action, the action
1212 * is usually performed by the target process rather than the caller; we add
1213 * the signal to the set of pending signals for the process.
1214 *
1215 * Exceptions:
1216 * o When a stop signal is sent to a sleeping process that takes the
1217 * default action, the process is stopped without awakening it.
1218 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1219 * regardless of the signal action (eg, blocked or ignored).
1220 *
1221 * Other ignored signals are discarded immediately.
1222 */
1223 void
1224 kpsignal2(struct proc *p, ksiginfo_t *ksi)
1225 {
1226 int prop, lid, toall, signo = ksi->ksi_signo;
1227 struct sigacts *sa;
1228 struct lwp *l;
1229 ksiginfo_t *kp;
1230 ksiginfoq_t kq;
1231 sig_t action;
1232 #ifdef KERN_SA
1233 struct sadata_vp *vp;
1234 #endif
1235
1236 KASSERT(!cpu_intr_p());
1237 KASSERT(mutex_owned(proc_lock));
1238 KASSERT(mutex_owned(p->p_lock));
1239 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
1240 KASSERT(signo > 0 && signo < NSIG);
1241
1242 /*
1243 * If the process is being created by fork, is a zombie or is
1244 * exiting, then just drop the signal here and bail out.
1245 */
1246 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1247 return;
1248
1249 /*
1250 * Notify any interested parties of the signal.
1251 */
1252 KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
1253
1254 /*
1255 * Some signals including SIGKILL must act on the entire process.
1256 */
1257 kp = NULL;
1258 prop = sigprop[signo];
1259 toall = ((prop & SA_TOALL) != 0);
1260
1261 if (toall)
1262 lid = 0;
1263 else
1264 lid = ksi->ksi_lid;
1265
1266 /*
1267 * If proc is traced, always give parent a chance.
1268 */
1269 if (p->p_slflag & PSL_TRACED) {
1270 action = SIG_DFL;
1271
1272 if (lid == 0) {
1273 /*
1274 * If the process is being traced and the signal
1275 * is being caught, make sure to save any ksiginfo.
1276 */
1277 if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1278 return;
1279 sigput(&p->p_sigpend, p, kp);
1280 }
1281 } else {
1282 /*
1283 * If the signal was the result of a trap and is not being
1284 * caught, then reset it to default action so that the
1285 * process dumps core immediately.
1286 */
1287 if (KSI_TRAP_P(ksi)) {
1288 sa = p->p_sigacts;
1289 mutex_enter(&sa->sa_mutex);
1290 if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
1291 sigdelset(&p->p_sigctx.ps_sigignore, signo);
1292 SIGACTION(p, signo).sa_handler = SIG_DFL;
1293 }
1294 mutex_exit(&sa->sa_mutex);
1295 }
1296
1297 /*
1298 * If the signal is being ignored, then drop it. Note: we
1299 * don't set SIGCONT in ps_sigignore, and if it is set to
1300 * SIG_IGN, action will be SIG_DFL here.
1301 */
1302 if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1303 return;
1304
1305 else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
1306 action = SIG_CATCH;
1307 else {
1308 action = SIG_DFL;
1309
1310 /*
1311 * If sending a tty stop signal to a member of an
1312 * orphaned process group, discard the signal here if
1313 * the action is default; don't stop the process below
1314 * if sleeping, and don't clear any pending SIGCONT.
1315 */
1316 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1317 return;
1318
1319 if (prop & SA_KILL && p->p_nice > NZERO)
1320 p->p_nice = NZERO;
1321 }
1322 }
1323
1324 /*
1325 * If stopping or continuing a process, discard any pending
1326 * signals that would do the inverse.
1327 */
1328 if ((prop & (SA_CONT | SA_STOP)) != 0) {
1329 ksiginfo_queue_init(&kq);
1330 if ((prop & SA_CONT) != 0)
1331 sigclear(&p->p_sigpend, &stopsigmask, &kq);
1332 if ((prop & SA_STOP) != 0)
1333 sigclear(&p->p_sigpend, &contsigmask, &kq);
1334 ksiginfo_queue_drain(&kq); /* XXXSMP */
1335 }
1336
1337 /*
1338 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1339 * please!), check if any LWPs are waiting on it. If yes, pass on
1340 * the signal info. The signal won't be processed further here.
1341 */
1342 if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
1343 p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
1344 sigunwait(p, ksi))
1345 return;
1346
1347 /*
1348 * XXXSMP Should be allocated by the caller, we're holding locks
1349 * here.
1350 */
1351 if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1352 return;
1353
1354 /*
1355 * LWP private signals are easy - just find the LWP and post
1356 * the signal to it.
1357 */
1358 if (lid != 0) {
1359 l = lwp_find(p, lid);
1360 if (l != NULL) {
1361 sigput(&l->l_sigpend, p, kp);
1362 membar_producer();
1363 (void)sigpost(l, action, prop, kp->ksi_signo, 0);
1364 }
1365 goto out;
1366 }
1367
1368 /*
1369 * Some signals go to all LWPs, even if posted with _lwp_kill()
1370 * or for an SA process.
1371 */
1372 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1373 if ((p->p_slflag & PSL_TRACED) != 0)
1374 goto deliver;
1375
1376 /*
1377 * If SIGCONT is default (or ignored) and process is
1378 * asleep, we are finished; the process should not
1379 * be awakened.
1380 */
1381 if ((prop & SA_CONT) != 0 && action == SIG_DFL)
1382 goto out;
1383 } else {
1384 /*
1385 * Process is stopped or stopping. If traced, then no
1386 * further action is necessary.
1387 */
1388 if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL)
1389 goto out;
1390
1391 /*
1392 * Run the process only if sending SIGCONT or SIGKILL.
1393 */
1394 if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1395 /*
1396 * Re-adjust p_nstopchild if the process wasn't
1397 * collected by its parent.
1398 */
1399 p->p_stat = SACTIVE;
1400 p->p_sflag &= ~PS_STOPPING;
1401 if (!p->p_waited)
1402 p->p_pptr->p_nstopchild--;
1403
1404 /*
1405 * Do not make signal pending if SIGCONT is default.
1406 *
1407 * If the process catches SIGCONT, let it handle the
1408 * signal itself (if waiting on event - process runs,
1409 * otherwise continues sleeping).
1410 */
1411 if ((prop & SA_CONT) != 0 && action == SIG_DFL) {
1412 KASSERT(signo != SIGKILL);
1413 goto deliver;
1414 }
1415 } else if ((prop & SA_STOP) != 0) {
1416 /*
1417 * Already stopped, don't need to stop again.
1418 * (If we did the shell could get confused.)
1419 */
1420 goto out;
1421 }
1422 }
1423 /*
1424 * Make signal pending.
1425 */
1426 sigput(&p->p_sigpend, p, kp);
1427
1428 deliver:
1429 /*
1430 * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1431 * visible on the per process list (for sigispending()). This
1432 * is unlikely to be needed in practice, but...
1433 */
1434 membar_producer();
1435
1436 /*
1437 * Try to find an LWP that can take the signal.
1438 */
1439 #if KERN_SA
1440 if ((p->p_sa != NULL) && !toall) {
1441 /*
1442 * If we're in this delivery path, we are delivering a
1443 * signal that needs to go to one thread in the process.
1444 *
1445 * In the SA case, we try to find an idle LWP that can take
1446 * the signal. If that fails, only then do we consider
1447 * interrupting active LWPs. Since the signal's going to
1448 * just one thread, we need only look at "blessed" lwps,
1449 * so scan the vps for them.
1450 */
1451 l = NULL;
1452 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1453 l = vp->savp_lwp;
1454 if (sigpost(l, action, prop, kp->ksi_signo, 1))
1455 break;
1456 }
1457
1458 if (l == NULL) {
1459 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1460 l = vp->savp_lwp;
1461 if (sigpost(l, action, prop, kp->ksi_signo, 0))
1462 break;
1463 }
1464 }
1465 } else /* Catch the brace below if we're defined */
1466 #endif /* KERN_SA */
1467 {
1468 LIST_FOREACH(l, &p->p_lwps, l_sibling)
1469 if (sigpost(l, action, prop, kp->ksi_signo, 0) && !toall)
1470 break;
1471 }
1472
1473 out:
1474 /*
1475 * If the ksiginfo wasn't used, then bin it. XXXSMP freeing memory
1476 * with locks held. The caller should take care of this.
1477 */
1478 ksiginfo_free(kp);
1479 }
1480
1481 void
1482 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1483 {
1484 struct proc *p = l->l_proc;
1485 #ifdef KERN_SA
1486 struct lwp *le, *li;
1487 siginfo_t *si;
1488 int f;
1489 #endif /* KERN_SA */
1490
1491 KASSERT(mutex_owned(p->p_lock));
1492
1493 #ifdef KERN_SA
1494 if (p->p_sflag & PS_SA) {
1495 /* f indicates if we should clear LP_SA_NOBLOCK */
1496 f = ~l->l_pflag & LP_SA_NOBLOCK;
1497 l->l_pflag |= LP_SA_NOBLOCK;
1498
1499 mutex_exit(p->p_lock);
1500 /* XXXUPSXXX What if not on sa_vp? */
1501 /*
1502 * WRS: I think it won't matter, beyond the
1503 * question of what exactly we do with a signal
1504 * to a blocked user thread. Also, we try hard to always
1505 * send signals to blessed lwps, so we would only send
1506 * to a non-blessed lwp under special circumstances.
1507 */
1508 si = siginfo_alloc(PR_WAITOK);
1509
1510 si->_info = ksi->ksi_info;
1511
1512 /*
1513 * Figure out if we're the innocent victim or the main
1514 * perpitrator.
1515 */
1516 le = li = NULL;
1517 if (KSI_TRAP_P(ksi))
1518 le = l;
1519 else
1520 li = l;
1521 if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1522 sizeof(*si), si, siginfo_free) != 0) {
1523 siginfo_free(si);
1524 #if 0
1525 if (KSI_TRAP_P(ksi))
1526 /* XXX What dowe do here? The signal
1527 * didn't make it
1528 */;
1529 #endif
1530 }
1531 l->l_pflag ^= f;
1532 mutex_enter(p->p_lock);
1533 return;
1534 }
1535 #endif /* KERN_SA */
1536
1537 (*p->p_emul->e_sendsig)(ksi, mask);
1538 }
1539
1540 /*
1541 * Stop any LWPs sleeping interruptably.
1542 */
1543 static void
1544 proc_stop_lwps(struct proc *p)
1545 {
1546 struct lwp *l;
1547
1548 KASSERT(mutex_owned(p->p_lock));
1549 KASSERT((p->p_sflag & PS_STOPPING) != 0);
1550
1551 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1552 lwp_lock(l);
1553 if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
1554 l->l_stat = LSSTOP;
1555 p->p_nrlwps--;
1556 }
1557 lwp_unlock(l);
1558 }
1559 }
1560
1561 /*
1562 * Finish stopping of a process. Mark it stopped and notify the parent.
1563 *
1564 * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1565 */
1566 static void
1567 proc_stop_done(struct proc *p, bool ppsig, int ppmask)
1568 {
1569
1570 KASSERT(mutex_owned(proc_lock));
1571 KASSERT(mutex_owned(p->p_lock));
1572 KASSERT((p->p_sflag & PS_STOPPING) != 0);
1573 KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
1574
1575 p->p_sflag &= ~PS_STOPPING;
1576 p->p_stat = SSTOP;
1577 p->p_waited = 0;
1578 p->p_pptr->p_nstopchild++;
1579 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
1580 if (ppsig) {
1581 /* child_psignal drops p_lock briefly. */
1582 child_psignal(p, ppmask);
1583 }
1584 cv_broadcast(&p->p_pptr->p_waitcv);
1585 }
1586 }
1587
1588 /*
1589 * Stop the current process and switch away when being stopped or traced.
1590 */
1591 static void
1592 sigswitch(bool ppsig, int ppmask, int signo)
1593 {
1594 struct lwp *l = curlwp;
1595 struct proc *p = l->l_proc;
1596 int biglocks;
1597
1598 KASSERT(mutex_owned(p->p_lock));
1599 KASSERT(l->l_stat == LSONPROC);
1600 KASSERT(p->p_nrlwps > 0);
1601
1602 /*
1603 * On entry we know that the process needs to stop. If it's
1604 * the result of a 'sideways' stop signal that has been sourced
1605 * through issignal(), then stop other LWPs in the process too.
1606 */
1607 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1608 KASSERT(signo != 0);
1609 proc_stop(p, 1, signo);
1610 KASSERT(p->p_nrlwps > 0);
1611 }
1612
1613 /*
1614 * If we are the last live LWP, and the stop was a result of
1615 * a new signal, then signal the parent.
1616 */
1617 if ((p->p_sflag & PS_STOPPING) != 0) {
1618 if (!mutex_tryenter(proc_lock)) {
1619 mutex_exit(p->p_lock);
1620 mutex_enter(proc_lock);
1621 mutex_enter(p->p_lock);
1622 }
1623
1624 if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1625 /*
1626 * Note that proc_stop_done() can drop
1627 * p->p_lock briefly.
1628 */
1629 proc_stop_done(p, ppsig, ppmask);
1630 }
1631
1632 mutex_exit(proc_lock);
1633 }
1634
1635 /*
1636 * Unlock and switch away.
1637 */
1638 KERNEL_UNLOCK_ALL(l, &biglocks);
1639 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1640 p->p_nrlwps--;
1641 lwp_lock(l);
1642 KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
1643 l->l_stat = LSSTOP;
1644 lwp_unlock(l);
1645 }
1646
1647 mutex_exit(p->p_lock);
1648 lwp_lock(l);
1649 mi_switch(l);
1650 KERNEL_LOCK(biglocks, l);
1651 mutex_enter(p->p_lock);
1652 }
1653
1654 /*
1655 * Check for a signal from the debugger.
1656 */
1657 static int
1658 sigchecktrace(void)
1659 {
1660 struct lwp *l = curlwp;
1661 struct proc *p = l->l_proc;
1662 sigset_t *mask;
1663 int signo;
1664
1665 KASSERT(mutex_owned(p->p_lock));
1666
1667 /* If there's a pending SIGKILL, process it immediately. */
1668 if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
1669 return 0;
1670
1671 /*
1672 * If we are no longer being traced, or the parent didn't
1673 * give us a signal, look for more signals.
1674 */
1675 if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xstat == 0)
1676 return 0;
1677
1678 /*
1679 * If the new signal is being masked, look for other signals.
1680 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1681 */
1682 signo = p->p_xstat;
1683 p->p_xstat = 0;
1684 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
1685 if (sigismember(mask, signo))
1686 signo = 0;
1687
1688 return signo;
1689 }
1690
1691 /*
1692 * If the current process has received a signal (should be caught or cause
1693 * termination, should interrupt current syscall), return the signal number.
1694 *
1695 * Stop signals with default action are processed immediately, then cleared;
1696 * they aren't returned. This is checked after each entry to the system for
1697 * a syscall or trap.
1698 *
1699 * We will also return -1 if the process is exiting and the current LWP must
1700 * follow suit.
1701 */
1702 int
1703 issignal(struct lwp *l)
1704 {
1705 struct proc *p;
1706 int signo, prop;
1707 sigpend_t *sp;
1708 sigset_t ss;
1709
1710 p = l->l_proc;
1711 sp = NULL;
1712 signo = 0;
1713
1714 KASSERT(p == curproc);
1715 KASSERT(mutex_owned(p->p_lock));
1716
1717 for (;;) {
1718 /* Discard any signals that we have decided not to take. */
1719 if (signo != 0)
1720 (void)sigget(sp, NULL, signo, NULL);
1721
1722 /* Bail out if we do not own the virtual processor */
1723 if (l->l_flag & LW_SA && l->l_savp->savp_lwp != l)
1724 break;
1725
1726 /*
1727 * If the process is stopped/stopping, then stop ourselves
1728 * now that we're on the kernel/userspace boundary. When
1729 * we awaken, check for a signal from the debugger.
1730 */
1731 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1732 sigswitch(true, PS_NOCLDSTOP, 0);
1733 signo = sigchecktrace();
1734 } else
1735 signo = 0;
1736
1737 /* Signals from the debugger are "out of band". */
1738 sp = NULL;
1739
1740 /*
1741 * If the debugger didn't provide a signal, find a pending
1742 * signal from our set. Check per-LWP signals first, and
1743 * then per-process.
1744 */
1745 if (signo == 0) {
1746 sp = &l->l_sigpend;
1747 ss = sp->sp_set;
1748 if ((p->p_lflag & PL_PPWAIT) != 0)
1749 sigminusset(&stopsigmask, &ss);
1750 sigminusset(&l->l_sigmask, &ss);
1751
1752 if ((signo = firstsig(&ss)) == 0) {
1753 sp = &p->p_sigpend;
1754 ss = sp->sp_set;
1755 if ((p->p_lflag & PL_PPWAIT) != 0)
1756 sigminusset(&stopsigmask, &ss);
1757 sigminusset(&l->l_sigmask, &ss);
1758
1759 if ((signo = firstsig(&ss)) == 0) {
1760 /*
1761 * No signal pending - clear the
1762 * indicator and bail out.
1763 */
1764 lwp_lock(l);
1765 l->l_flag &= ~LW_PENDSIG;
1766 lwp_unlock(l);
1767 sp = NULL;
1768 break;
1769 }
1770 }
1771 }
1772
1773 /*
1774 * We should see pending but ignored signals only if
1775 * we are being traced.
1776 */
1777 if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
1778 (p->p_slflag & PSL_TRACED) == 0) {
1779 /* Discard the signal. */
1780 continue;
1781 }
1782
1783 /*
1784 * If traced, always stop, and stay stopped until released
1785 * by the debugger. If the our parent process is waiting
1786 * for us, don't hang as we could deadlock.
1787 */
1788 if ((p->p_slflag & PSL_TRACED) != 0 &&
1789 (p->p_lflag & PL_PPWAIT) == 0 && signo != SIGKILL) {
1790 /* Take the signal. */
1791 (void)sigget(sp, NULL, signo, NULL);
1792 p->p_xstat = signo;
1793
1794 /* Emulation-specific handling of signal trace */
1795 if (p->p_emul->e_tracesig == NULL ||
1796 (*p->p_emul->e_tracesig)(p, signo) == 0)
1797 sigswitch(!(p->p_slflag & PSL_FSTRACE), 0,
1798 signo);
1799
1800 /* Check for a signal from the debugger. */
1801 if ((signo = sigchecktrace()) == 0)
1802 continue;
1803
1804 /* Signals from the debugger are "out of band". */
1805 sp = NULL;
1806 }
1807
1808 prop = sigprop[signo];
1809
1810 /*
1811 * Decide whether the signal should be returned.
1812 */
1813 switch ((long)SIGACTION(p, signo).sa_handler) {
1814 case (long)SIG_DFL:
1815 /*
1816 * Don't take default actions on system processes.
1817 */
1818 if (p->p_pid <= 1) {
1819 #ifdef DIAGNOSTIC
1820 /*
1821 * Are you sure you want to ignore SIGSEGV
1822 * in init? XXX
1823 */
1824 printf_nolog("Process (pid %d) got sig %d\n",
1825 p->p_pid, signo);
1826 #endif
1827 continue;
1828 }
1829
1830 /*
1831 * If there is a pending stop signal to process with
1832 * default action, stop here, then clear the signal.
1833 * However, if process is member of an orphaned
1834 * process group, ignore tty stop signals.
1835 */
1836 if (prop & SA_STOP) {
1837 /*
1838 * XXX Don't hold proc_lock for p_lflag,
1839 * but it's not a big deal.
1840 */
1841 if (p->p_slflag & PSL_TRACED ||
1842 ((p->p_lflag & PL_ORPHANPG) != 0 &&
1843 prop & SA_TTYSTOP)) {
1844 /* Ignore the signal. */
1845 continue;
1846 }
1847 /* Take the signal. */
1848 (void)sigget(sp, NULL, signo, NULL);
1849 p->p_xstat = signo;
1850 signo = 0;
1851 sigswitch(true, PS_NOCLDSTOP, p->p_xstat);
1852 } else if (prop & SA_IGNORE) {
1853 /*
1854 * Except for SIGCONT, shouldn't get here.
1855 * Default action is to ignore; drop it.
1856 */
1857 continue;
1858 }
1859 break;
1860
1861 case (long)SIG_IGN:
1862 #ifdef DEBUG_ISSIGNAL
1863 /*
1864 * Masking above should prevent us ever trying
1865 * to take action on an ignored signal other
1866 * than SIGCONT, unless process is traced.
1867 */
1868 if ((prop & SA_CONT) == 0 &&
1869 (p->p_slflag & PSL_TRACED) == 0)
1870 printf_nolog("issignal\n");
1871 #endif
1872 continue;
1873
1874 default:
1875 /*
1876 * This signal has an action, let postsig() process
1877 * it.
1878 */
1879 break;
1880 }
1881
1882 break;
1883 }
1884
1885 l->l_sigpendset = sp;
1886 return signo;
1887 }
1888
1889 /*
1890 * Take the action for the specified signal
1891 * from the current set of pending signals.
1892 */
1893 void
1894 postsig(int signo)
1895 {
1896 struct lwp *l;
1897 struct proc *p;
1898 struct sigacts *ps;
1899 sig_t action;
1900 sigset_t *returnmask;
1901 ksiginfo_t ksi;
1902
1903 l = curlwp;
1904 p = l->l_proc;
1905 ps = p->p_sigacts;
1906
1907 KASSERT(mutex_owned(p->p_lock));
1908 KASSERT(signo > 0);
1909
1910 /*
1911 * Set the new mask value and also defer further occurrences of this
1912 * signal.
1913 *
1914 * Special case: user has done a sigsuspend. Here the current mask is
1915 * not of interest, but rather the mask from before the sigsuspend is
1916 * what we want restored after the signal processing is completed.
1917 */
1918 if (l->l_sigrestore) {
1919 returnmask = &l->l_sigoldmask;
1920 l->l_sigrestore = 0;
1921 } else
1922 returnmask = &l->l_sigmask;
1923
1924 /*
1925 * Commit to taking the signal before releasing the mutex.
1926 */
1927 action = SIGACTION_PS(ps, signo).sa_handler;
1928 l->l_ru.ru_nsignals++;
1929 sigget(l->l_sigpendset, &ksi, signo, NULL);
1930
1931 if (ktrpoint(KTR_PSIG)) {
1932 mutex_exit(p->p_lock);
1933 ktrpsig(signo, action, returnmask, &ksi);
1934 mutex_enter(p->p_lock);
1935 }
1936
1937 if (action == SIG_DFL) {
1938 /*
1939 * Default action, where the default is to kill
1940 * the process. (Other cases were ignored above.)
1941 */
1942 sigexit(l, signo);
1943 return;
1944 }
1945
1946 /*
1947 * If we get here, the signal must be caught.
1948 */
1949 #ifdef DIAGNOSTIC
1950 if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
1951 panic("postsig action");
1952 #endif
1953
1954 kpsendsig(l, &ksi, returnmask);
1955 }
1956
1957 /*
1958 * sendsig_reset:
1959 *
1960 * Reset the signal action. Called from emulation specific sendsig()
1961 * before unlocking to deliver the signal.
1962 */
1963 void
1964 sendsig_reset(struct lwp *l, int signo)
1965 {
1966 struct proc *p = l->l_proc;
1967 struct sigacts *ps = p->p_sigacts;
1968 sigset_t *mask;
1969
1970 KASSERT(mutex_owned(p->p_lock));
1971
1972 p->p_sigctx.ps_lwp = 0;
1973 p->p_sigctx.ps_code = 0;
1974 p->p_sigctx.ps_signo = 0;
1975
1976 mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
1977
1978 mutex_enter(&ps->sa_mutex);
1979 sigplusset(&SIGACTION_PS(ps, signo).sa_mask, mask);
1980 if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
1981 sigdelset(&p->p_sigctx.ps_sigcatch, signo);
1982 if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
1983 sigaddset(&p->p_sigctx.ps_sigignore, signo);
1984 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1985 }
1986 mutex_exit(&ps->sa_mutex);
1987 }
1988
1989 /*
1990 * Kill the current process for stated reason.
1991 */
1992 void
1993 killproc(struct proc *p, const char *why)
1994 {
1995
1996 KASSERT(mutex_owned(proc_lock));
1997
1998 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1999 uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
2000 psignal(p, SIGKILL);
2001 }
2002
2003 /*
2004 * Force the current process to exit with the specified signal, dumping core
2005 * if appropriate. We bypass the normal tests for masked and caught
2006 * signals, allowing unrecoverable failures to terminate the process without
2007 * changing signal state. Mark the accounting record with the signal
2008 * termination. If dumping core, save the signal number for the debugger.
2009 * Calls exit and does not return.
2010 */
2011 void
2012 sigexit(struct lwp *l, int signo)
2013 {
2014 int exitsig, error, docore;
2015 struct proc *p;
2016 struct lwp *t;
2017
2018 p = l->l_proc;
2019
2020 KASSERT(mutex_owned(p->p_lock));
2021 KERNEL_UNLOCK_ALL(l, NULL);
2022
2023 /*
2024 * Don't permit coredump() multiple times in the same process.
2025 * Call back into sigexit, where we will be suspended until
2026 * the deed is done. Note that this is a recursive call, but
2027 * LW_WCORE will prevent us from coming back this way.
2028 */
2029 if ((p->p_sflag & PS_WCORE) != 0) {
2030 lwp_lock(l);
2031 l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
2032 lwp_unlock(l);
2033 mutex_exit(p->p_lock);
2034 lwp_userret(l);
2035 panic("sigexit 1");
2036 /* NOTREACHED */
2037 }
2038
2039 /* If process is already on the way out, then bail now. */
2040 if ((p->p_sflag & PS_WEXIT) != 0) {
2041 mutex_exit(p->p_lock);
2042 lwp_exit(l);
2043 panic("sigexit 2");
2044 /* NOTREACHED */
2045 }
2046
2047 /*
2048 * Prepare all other LWPs for exit. If dumping core, suspend them
2049 * so that their registers are available long enough to be dumped.
2050 */
2051 if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
2052 p->p_sflag |= PS_WCORE;
2053 for (;;) {
2054 LIST_FOREACH(t, &p->p_lwps, l_sibling) {
2055 lwp_lock(t);
2056 if (t == l) {
2057 t->l_flag &= ~LW_WSUSPEND;
2058 lwp_unlock(t);
2059 continue;
2060 }
2061 t->l_flag |= (LW_WCORE | LW_WEXIT);
2062 lwp_suspend(l, t);
2063 }
2064
2065 if (p->p_nrlwps == 1)
2066 break;
2067
2068 /*
2069 * Kick any LWPs sitting in lwp_wait1(), and wait
2070 * for everyone else to stop before proceeding.
2071 */
2072 p->p_nlwpwait++;
2073 cv_broadcast(&p->p_lwpcv);
2074 cv_wait(&p->p_lwpcv, p->p_lock);
2075 p->p_nlwpwait--;
2076 }
2077 }
2078
2079 exitsig = signo;
2080 p->p_acflag |= AXSIG;
2081 p->p_sigctx.ps_signo = signo;
2082
2083 if (docore) {
2084 mutex_exit(p->p_lock);
2085 if ((error = coredump(l, NULL)) == 0)
2086 exitsig |= WCOREFLAG;
2087
2088 if (kern_logsigexit) {
2089 int uid = l->l_cred ?
2090 (int)kauth_cred_geteuid(l->l_cred) : -1;
2091
2092 if (error)
2093 log(LOG_INFO, lognocoredump, p->p_pid,
2094 p->p_comm, uid, signo, error);
2095 else
2096 log(LOG_INFO, logcoredump, p->p_pid,
2097 p->p_comm, uid, signo);
2098 }
2099
2100 #ifdef PAX_SEGVGUARD
2101 pax_segvguard(l, p->p_textvp, p->p_comm, true);
2102 #endif /* PAX_SEGVGUARD */
2103 /* Acquire the sched state mutex. exit1() will release it. */
2104 mutex_enter(p->p_lock);
2105 }
2106
2107 /* No longer dumping core. */
2108 p->p_sflag &= ~PS_WCORE;
2109
2110 exit1(l, W_EXITCODE(0, exitsig));
2111 /* NOTREACHED */
2112 }
2113
2114 /*
2115 * Put process 'p' into the stopped state and optionally, notify the parent.
2116 */
2117 void
2118 proc_stop(struct proc *p, int notify, int signo)
2119 {
2120 struct lwp *l;
2121
2122 KASSERT(mutex_owned(p->p_lock));
2123
2124 /*
2125 * First off, set the stopping indicator and bring all sleeping
2126 * LWPs to a halt so they are included in p->p_nrlwps. We musn't
2127 * unlock between here and the p->p_nrlwps check below.
2128 */
2129 p->p_sflag |= PS_STOPPING;
2130 if (notify)
2131 p->p_sflag |= PS_NOTIFYSTOP;
2132 else
2133 p->p_sflag &= ~PS_NOTIFYSTOP;
2134 membar_producer();
2135
2136 proc_stop_lwps(p);
2137
2138 /*
2139 * If there are no LWPs available to take the signal, then we
2140 * signal the parent process immediately. Otherwise, the last
2141 * LWP to stop will take care of it.
2142 */
2143
2144 if (p->p_nrlwps == 0) {
2145 proc_stop_done(p, true, PS_NOCLDSTOP);
2146 } else {
2147 /*
2148 * Have the remaining LWPs come to a halt, and trigger
2149 * proc_stop_callout() to ensure that they do.
2150 */
2151 LIST_FOREACH(l, &p->p_lwps, l_sibling)
2152 sigpost(l, SIG_DFL, SA_STOP, signo, 0);
2153 callout_schedule(&proc_stop_ch, 1);
2154 }
2155 }
2156
2157 /*
2158 * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2159 * but wait for them to come to a halt at the kernel-user boundary. This is
2160 * to allow LWPs to release any locks that they may hold before stopping.
2161 *
2162 * Non-interruptable sleeps can be long, and there is the potential for an
2163 * LWP to begin sleeping interruptably soon after the process has been set
2164 * stopping (PS_STOPPING). These LWPs will not notice that the process is
2165 * stopping, and so complete halt of the process and the return of status
2166 * information to the parent could be delayed indefinitely.
2167 *
2168 * To handle this race, proc_stop_callout() runs once per tick while there
2169 * are stopping processes in the system. It sets LWPs that are sleeping
2170 * interruptably into the LSSTOP state.
2171 *
2172 * Note that we are not concerned about keeping all LWPs stopped while the
2173 * process is stopped: stopped LWPs can awaken briefly to handle signals.
2174 * What we do need to ensure is that all LWPs in a stopping process have
2175 * stopped at least once, so that notification can be sent to the parent
2176 * process.
2177 */
2178 static void
2179 proc_stop_callout(void *cookie)
2180 {
2181 bool more, restart;
2182 struct proc *p;
2183
2184 (void)cookie;
2185
2186 do {
2187 restart = false;
2188 more = false;
2189
2190 mutex_enter(proc_lock);
2191 PROCLIST_FOREACH(p, &allproc) {
2192 if ((p->p_flag & PK_MARKER) != 0)
2193 continue;
2194 mutex_enter(p->p_lock);
2195
2196 if ((p->p_sflag & PS_STOPPING) == 0) {
2197 mutex_exit(p->p_lock);
2198 continue;
2199 }
2200
2201 /* Stop any LWPs sleeping interruptably. */
2202 proc_stop_lwps(p);
2203 if (p->p_nrlwps == 0) {
2204 /*
2205 * We brought the process to a halt.
2206 * Mark it as stopped and notify the
2207 * parent.
2208 */
2209 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
2210 /*
2211 * Note that proc_stop_done() will
2212 * drop p->p_lock briefly.
2213 * Arrange to restart and check
2214 * all processes again.
2215 */
2216 restart = true;
2217 }
2218 proc_stop_done(p, true, PS_NOCLDSTOP);
2219 } else
2220 more = true;
2221
2222 mutex_exit(p->p_lock);
2223 if (restart)
2224 break;
2225 }
2226 mutex_exit(proc_lock);
2227 } while (restart);
2228
2229 /*
2230 * If we noted processes that are stopping but still have
2231 * running LWPs, then arrange to check again in 1 tick.
2232 */
2233 if (more)
2234 callout_schedule(&proc_stop_ch, 1);
2235 }
2236
2237 /*
2238 * Given a process in state SSTOP, set the state back to SACTIVE and
2239 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
2240 */
2241 void
2242 proc_unstop(struct proc *p)
2243 {
2244 struct lwp *l;
2245 int sig;
2246
2247 KASSERT(mutex_owned(proc_lock));
2248 KASSERT(mutex_owned(p->p_lock));
2249
2250 p->p_stat = SACTIVE;
2251 p->p_sflag &= ~PS_STOPPING;
2252 sig = p->p_xstat;
2253
2254 if (!p->p_waited)
2255 p->p_pptr->p_nstopchild--;
2256
2257 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2258 lwp_lock(l);
2259 if (l->l_stat != LSSTOP) {
2260 lwp_unlock(l);
2261 continue;
2262 }
2263 if (l->l_wchan == NULL) {
2264 setrunnable(l);
2265 continue;
2266 }
2267 if (sig && (l->l_flag & LW_SINTR) != 0) {
2268 setrunnable(l);
2269 sig = 0;
2270 } else {
2271 l->l_stat = LSSLEEP;
2272 p->p_nrlwps++;
2273 lwp_unlock(l);
2274 }
2275 }
2276 }
2277
2278 static int
2279 filt_sigattach(struct knote *kn)
2280 {
2281 struct proc *p = curproc;
2282
2283 kn->kn_obj = p;
2284 kn->kn_flags |= EV_CLEAR; /* automatically set */
2285
2286 mutex_enter(p->p_lock);
2287 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2288 mutex_exit(p->p_lock);
2289
2290 return (0);
2291 }
2292
2293 static void
2294 filt_sigdetach(struct knote *kn)
2295 {
2296 struct proc *p = kn->kn_obj;
2297
2298 mutex_enter(p->p_lock);
2299 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2300 mutex_exit(p->p_lock);
2301 }
2302
2303 /*
2304 * signal knotes are shared with proc knotes, so we apply a mask to
2305 * the hint in order to differentiate them from process hints. This
2306 * could be avoided by using a signal-specific knote list, but probably
2307 * isn't worth the trouble.
2308 */
2309 static int
2310 filt_signal(struct knote *kn, long hint)
2311 {
2312
2313 if (hint & NOTE_SIGNAL) {
2314 hint &= ~NOTE_SIGNAL;
2315
2316 if (kn->kn_id == hint)
2317 kn->kn_data++;
2318 }
2319 return (kn->kn_data != 0);
2320 }
2321
2322 const struct filterops sig_filtops = {
2323 0, filt_sigattach, filt_sigdetach, filt_signal
2324 };
Cache object: 6cd9797e03226ee2290bafbaa1500374
|