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