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