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