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