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