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