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