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