kern_sig.c revision 1.149 1 /* $NetBSD: kern_sig.c,v 1.149 2003/09/10 16:41:26 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.149 2003/09/10 16:41:26 kleink Exp $");
41
42 #include "opt_ktrace.h"
43 #include "opt_compat_sunos.h"
44 #include "opt_compat_netbsd32.h"
45
46 #define SIGPROP /* include signal properties table */
47 #include <sys/param.h>
48 #include <sys/signalvar.h>
49 #include <sys/resourcevar.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54 #include <sys/timeb.h>
55 #include <sys/times.h>
56 #include <sys/buf.h>
57 #include <sys/acct.h>
58 #include <sys/file.h>
59 #include <sys/kernel.h>
60 #include <sys/wait.h>
61 #include <sys/ktrace.h>
62 #include <sys/syslog.h>
63 #include <sys/stat.h>
64 #include <sys/core.h>
65 #include <sys/filedesc.h>
66 #include <sys/malloc.h>
67 #include <sys/pool.h>
68 #include <sys/ucontext.h>
69 #include <sys/sa.h>
70 #include <sys/savar.h>
71 #include <sys/exec.h>
72
73 #include <sys/mount.h>
74 #include <sys/syscallargs.h>
75
76 #include <machine/cpu.h>
77
78 #include <sys/user.h> /* for coredump */
79
80 #include <uvm/uvm_extern.h>
81
82 static void proc_stop(struct proc *p);
83 static int build_corename(struct proc *, char [MAXPATHLEN]);
84 sigset_t contsigmask, stopsigmask, sigcantmask;
85
86 struct pool sigacts_pool; /* memory pool for sigacts structures */
87 struct pool siginfo_pool; /* memory pool for siginfo structures */
88
89 /*
90 * Can process p, with pcred pc, send the signal signum to process q?
91 */
92 #define CANSIGNAL(p, pc, q, signum) \
93 ((pc)->pc_ucred->cr_uid == 0 || \
94 (pc)->p_ruid == (q)->p_cred->p_ruid || \
95 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
96 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
97 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
98 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
99
100 /*
101 * Initialize signal-related data structures.
102 */
103 void
104 signal_init(void)
105 {
106
107 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
108 &pool_allocator_nointr);
109 pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
110 &pool_allocator_nointr);
111 }
112
113 /*
114 * Create an initial sigctx structure, using the same signal state
115 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
116 * copy it from parent.
117 */
118 void
119 sigactsinit(struct proc *np, struct proc *pp, int share)
120 {
121 struct sigacts *ps;
122
123 if (share) {
124 np->p_sigacts = pp->p_sigacts;
125 pp->p_sigacts->sa_refcnt++;
126 } else {
127 ps = pool_get(&sigacts_pool, PR_WAITOK);
128 if (pp)
129 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
130 else
131 memset(ps, '\0', sizeof(struct sigacts));
132 ps->sa_refcnt = 1;
133 np->p_sigacts = ps;
134 }
135 }
136
137 /*
138 * Make this process not share its sigctx, maintaining all
139 * signal state.
140 */
141 void
142 sigactsunshare(struct proc *p)
143 {
144 struct sigacts *oldps;
145
146 if (p->p_sigacts->sa_refcnt == 1)
147 return;
148
149 oldps = p->p_sigacts;
150 sigactsinit(p, NULL, 0);
151
152 if (--oldps->sa_refcnt == 0)
153 pool_put(&sigacts_pool, oldps);
154 }
155
156 /*
157 * Release a sigctx structure.
158 */
159 void
160 sigactsfree(struct proc *p)
161 {
162 struct sigacts *ps;
163
164 ps = p->p_sigacts;
165 if (--ps->sa_refcnt > 0)
166 return;
167
168 pool_put(&sigacts_pool, ps);
169 }
170
171 int
172 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
173 struct sigaction *osa, void *tramp, int vers)
174 {
175 struct sigacts *ps;
176 int prop;
177
178 ps = p->p_sigacts;
179 if (signum <= 0 || signum >= NSIG)
180 return (EINVAL);
181
182 /*
183 * Trampoline ABI version 0 is reserved for the legacy
184 * kernel-provided on-stack trampoline. Conversely, if
185 * we are using a non-0 ABI version, we must have a
186 * trampoline.
187 */
188 if ((vers != 0 && tramp == NULL) ||
189 (vers == 0 && tramp != NULL))
190 return (EINVAL);
191
192 if (osa)
193 *osa = SIGACTION_PS(ps, signum);
194
195 if (nsa) {
196 if (nsa->sa_flags & ~SA_ALLBITS)
197 return (EINVAL);
198
199 #ifndef __HAVE_SIGINFO
200 if (nsa->sa_flags & SA_SIGINFO)
201 return (EINVAL);
202 #endif
203
204 prop = sigprop[signum];
205 if (prop & SA_CANTMASK)
206 return (EINVAL);
207
208 (void) splsched(); /* XXXSMP */
209 SIGACTION_PS(ps, signum) = *nsa;
210 ps->sa_sigdesc[signum].sd_tramp = tramp;
211 ps->sa_sigdesc[signum].sd_vers = vers;
212 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
213 if ((prop & SA_NORESET) != 0)
214 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
215 if (signum == SIGCHLD) {
216 if (nsa->sa_flags & SA_NOCLDSTOP)
217 p->p_flag |= P_NOCLDSTOP;
218 else
219 p->p_flag &= ~P_NOCLDSTOP;
220 if (nsa->sa_flags & SA_NOCLDWAIT) {
221 /*
222 * Paranoia: since SA_NOCLDWAIT is implemented
223 * by reparenting the dying child to PID 1 (and
224 * trust it to reap the zombie), PID 1 itself
225 * is forbidden to set SA_NOCLDWAIT.
226 */
227 if (p->p_pid == 1)
228 p->p_flag &= ~P_NOCLDWAIT;
229 else
230 p->p_flag |= P_NOCLDWAIT;
231 } else
232 p->p_flag &= ~P_NOCLDWAIT;
233 }
234 if ((nsa->sa_flags & SA_NODEFER) == 0)
235 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
236 else
237 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
238 /*
239 * Set bit in p_sigctx.ps_sigignore for signals that are set to
240 * SIG_IGN, and for signals set to SIG_DFL where the default is
241 * to ignore. However, don't put SIGCONT in
242 * p_sigctx.ps_sigignore, as we have to restart the process.
243 */
244 if (nsa->sa_handler == SIG_IGN ||
245 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
246 /* never to be seen again */
247 sigdelset(&p->p_sigctx.ps_siglist, signum);
248 if (signum != SIGCONT) {
249 /* easier in psignal */
250 sigaddset(&p->p_sigctx.ps_sigignore, signum);
251 }
252 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
253 } else {
254 sigdelset(&p->p_sigctx.ps_sigignore, signum);
255 if (nsa->sa_handler == SIG_DFL)
256 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
257 else
258 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
259 }
260 (void) spl0();
261 }
262
263 return (0);
264 }
265
266 /* ARGSUSED */
267 int
268 sys___sigaction14(struct lwp *l, void *v, register_t *retval)
269 {
270 struct sys___sigaction14_args /* {
271 syscallarg(int) signum;
272 syscallarg(const struct sigaction *) nsa;
273 syscallarg(struct sigaction *) osa;
274 } */ *uap = v;
275 struct proc *p;
276 struct sigaction nsa, osa;
277 int error;
278
279 if (SCARG(uap, nsa)) {
280 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
281 if (error)
282 return (error);
283 }
284 p = l->l_proc;
285 error = sigaction1(p, SCARG(uap, signum),
286 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
287 NULL, 0);
288 if (error)
289 return (error);
290 if (SCARG(uap, osa)) {
291 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
292 if (error)
293 return (error);
294 }
295 return (0);
296 }
297
298 /* ARGSUSED */
299 int
300 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
301 {
302 struct sys___sigaction_sigtramp_args /* {
303 syscallarg(int) signum;
304 syscallarg(const struct sigaction *) nsa;
305 syscallarg(struct sigaction *) osa;
306 syscallarg(void *) tramp;
307 syscallarg(int) vers;
308 } */ *uap = v;
309 struct proc *p = l->l_proc;
310 struct sigaction nsa, osa;
311 int error;
312
313 if (SCARG(uap, nsa)) {
314 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
315 if (error)
316 return (error);
317 }
318 error = sigaction1(p, SCARG(uap, signum),
319 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
320 SCARG(uap, tramp), SCARG(uap, vers));
321 if (error)
322 return (error);
323 if (SCARG(uap, osa)) {
324 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
325 if (error)
326 return (error);
327 }
328 return (0);
329 }
330
331 /*
332 * Initialize signal state for process 0;
333 * set to ignore signals that are ignored by default and disable the signal
334 * stack.
335 */
336 void
337 siginit(struct proc *p)
338 {
339 struct sigacts *ps;
340 int signum, prop;
341
342 ps = p->p_sigacts;
343 sigemptyset(&contsigmask);
344 sigemptyset(&stopsigmask);
345 sigemptyset(&sigcantmask);
346 for (signum = 1; signum < NSIG; signum++) {
347 prop = sigprop[signum];
348 if (prop & SA_CONT)
349 sigaddset(&contsigmask, signum);
350 if (prop & SA_STOP)
351 sigaddset(&stopsigmask, signum);
352 if (prop & SA_CANTMASK)
353 sigaddset(&sigcantmask, signum);
354 if (prop & SA_IGNORE && signum != SIGCONT)
355 sigaddset(&p->p_sigctx.ps_sigignore, signum);
356 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
357 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
358 }
359 sigemptyset(&p->p_sigctx.ps_sigcatch);
360 p->p_sigctx.ps_sigwaited = 0;
361 p->p_flag &= ~P_NOCLDSTOP;
362
363 /*
364 * Reset stack state to the user stack.
365 */
366 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
367 p->p_sigctx.ps_sigstk.ss_size = 0;
368 p->p_sigctx.ps_sigstk.ss_sp = 0;
369
370 /* One reference. */
371 ps->sa_refcnt = 1;
372 }
373
374 /*
375 * Reset signals for an exec of the specified process.
376 */
377 void
378 execsigs(struct proc *p)
379 {
380 struct sigacts *ps;
381 int signum, prop;
382
383 sigactsunshare(p);
384
385 ps = p->p_sigacts;
386
387 /*
388 * Reset caught signals. Held signals remain held
389 * through p_sigctx.ps_sigmask (unless they were caught,
390 * and are now ignored by default).
391 */
392 for (signum = 1; signum < NSIG; signum++) {
393 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
394 prop = sigprop[signum];
395 if (prop & SA_IGNORE) {
396 if ((prop & SA_CONT) == 0)
397 sigaddset(&p->p_sigctx.ps_sigignore,
398 signum);
399 sigdelset(&p->p_sigctx.ps_siglist, signum);
400 }
401 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
402 }
403 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
404 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
405 }
406 sigemptyset(&p->p_sigctx.ps_sigcatch);
407 p->p_sigctx.ps_sigwaited = 0;
408 p->p_flag &= ~P_NOCLDSTOP;
409
410 /*
411 * Reset stack state to the user stack.
412 */
413 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
414 p->p_sigctx.ps_sigstk.ss_size = 0;
415 p->p_sigctx.ps_sigstk.ss_sp = 0;
416 }
417
418 int
419 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
420 {
421
422 if (oss)
423 *oss = p->p_sigctx.ps_sigmask;
424
425 if (nss) {
426 (void)splsched(); /* XXXSMP */
427 switch (how) {
428 case SIG_BLOCK:
429 sigplusset(nss, &p->p_sigctx.ps_sigmask);
430 break;
431 case SIG_UNBLOCK:
432 sigminusset(nss, &p->p_sigctx.ps_sigmask);
433 CHECKSIGS(p);
434 break;
435 case SIG_SETMASK:
436 p->p_sigctx.ps_sigmask = *nss;
437 CHECKSIGS(p);
438 break;
439 default:
440 (void)spl0(); /* XXXSMP */
441 return (EINVAL);
442 }
443 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
444 (void)spl0(); /* XXXSMP */
445 }
446
447 return (0);
448 }
449
450 /*
451 * Manipulate signal mask.
452 * Note that we receive new mask, not pointer,
453 * and return old mask as return value;
454 * the library stub does the rest.
455 */
456 int
457 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
458 {
459 struct sys___sigprocmask14_args /* {
460 syscallarg(int) how;
461 syscallarg(const sigset_t *) set;
462 syscallarg(sigset_t *) oset;
463 } */ *uap = v;
464 struct proc *p;
465 sigset_t nss, oss;
466 int error;
467
468 if (SCARG(uap, set)) {
469 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
470 if (error)
471 return (error);
472 }
473 p = l->l_proc;
474 error = sigprocmask1(p, SCARG(uap, how),
475 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
476 if (error)
477 return (error);
478 if (SCARG(uap, oset)) {
479 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
480 if (error)
481 return (error);
482 }
483 return (0);
484 }
485
486 void
487 sigpending1(struct proc *p, sigset_t *ss)
488 {
489
490 *ss = p->p_sigctx.ps_siglist;
491 sigminusset(&p->p_sigctx.ps_sigmask, ss);
492 }
493
494 /* ARGSUSED */
495 int
496 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
497 {
498 struct sys___sigpending14_args /* {
499 syscallarg(sigset_t *) set;
500 } */ *uap = v;
501 struct proc *p;
502 sigset_t ss;
503
504 p = l->l_proc;
505 sigpending1(p, &ss);
506 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
507 }
508
509 int
510 sigsuspend1(struct proc *p, const sigset_t *ss)
511 {
512 struct sigacts *ps;
513
514 ps = p->p_sigacts;
515 if (ss) {
516 /*
517 * When returning from sigpause, we want
518 * the old mask to be restored after the
519 * signal handler has finished. Thus, we
520 * save it here and mark the sigctx structure
521 * to indicate this.
522 */
523 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
524 p->p_sigctx.ps_flags |= SAS_OLDMASK;
525 (void) splsched(); /* XXXSMP */
526 p->p_sigctx.ps_sigmask = *ss;
527 CHECKSIGS(p);
528 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
529 (void) spl0(); /* XXXSMP */
530 }
531
532 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
533 /* void */;
534
535 /* always return EINTR rather than ERESTART... */
536 return (EINTR);
537 }
538
539 /*
540 * Suspend process until signal, providing mask to be set
541 * in the meantime. Note nonstandard calling convention:
542 * libc stub passes mask, not pointer, to save a copyin.
543 */
544 /* ARGSUSED */
545 int
546 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
547 {
548 struct sys___sigsuspend14_args /* {
549 syscallarg(const sigset_t *) set;
550 } */ *uap = v;
551 struct proc *p;
552 sigset_t ss;
553 int error;
554
555 if (SCARG(uap, set)) {
556 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
557 if (error)
558 return (error);
559 }
560
561 p = l->l_proc;
562 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
563 }
564
565 int
566 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
567 struct sigaltstack *oss)
568 {
569
570 if (oss)
571 *oss = p->p_sigctx.ps_sigstk;
572
573 if (nss) {
574 if (nss->ss_flags & ~SS_ALLBITS)
575 return (EINVAL);
576
577 if (nss->ss_flags & SS_DISABLE) {
578 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
579 return (EINVAL);
580 } else {
581 if (nss->ss_size < MINSIGSTKSZ)
582 return (ENOMEM);
583 }
584 p->p_sigctx.ps_sigstk = *nss;
585 }
586
587 return (0);
588 }
589
590 /* ARGSUSED */
591 int
592 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
593 {
594 struct sys___sigaltstack14_args /* {
595 syscallarg(const struct sigaltstack *) nss;
596 syscallarg(struct sigaltstack *) oss;
597 } */ *uap = v;
598 struct proc *p;
599 struct sigaltstack nss, oss;
600 int error;
601
602 if (SCARG(uap, nss)) {
603 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
604 if (error)
605 return (error);
606 }
607 p = l->l_proc;
608 error = sigaltstack1(p,
609 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
610 if (error)
611 return (error);
612 if (SCARG(uap, oss)) {
613 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
614 if (error)
615 return (error);
616 }
617 return (0);
618 }
619
620 /* ARGSUSED */
621 int
622 sys_kill(struct lwp *l, void *v, register_t *retval)
623 {
624 struct sys_kill_args /* {
625 syscallarg(int) pid;
626 syscallarg(int) signum;
627 } */ *uap = v;
628 struct proc *cp, *p;
629 struct pcred *pc;
630 ksiginfo_t ksi;
631
632 cp = l->l_proc;
633 pc = cp->p_cred;
634 if ((u_int)SCARG(uap, signum) >= NSIG)
635 return (EINVAL);
636 memset(&ksi, 0, sizeof(ksi));
637 ksi.ksi_signo = SCARG(uap, signum);
638 ksi.ksi_code = SI_USER;
639 ksi.ksi_pid = cp->p_pid;
640 ksi.ksi_uid = cp->p_ucred->cr_uid;
641 if (SCARG(uap, pid) > 0) {
642 /* kill single process */
643 if ((p = pfind(SCARG(uap, pid))) == NULL)
644 return (ESRCH);
645 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
646 return (EPERM);
647 if (SCARG(uap, signum))
648 kpsignal(p, &ksi, NULL);
649 return (0);
650 }
651 switch (SCARG(uap, pid)) {
652 case -1: /* broadcast signal */
653 return (killpg1(cp, &ksi, 0, 1));
654 case 0: /* signal own process group */
655 return (killpg1(cp, &ksi, 0, 0));
656 default: /* negative explicit process group */
657 return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
658 }
659 /* NOTREACHED */
660 }
661
662 /*
663 * Common code for kill process group/broadcast kill.
664 * cp is calling process.
665 */
666 int
667 killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
668 {
669 struct proc *p;
670 struct pcred *pc;
671 struct pgrp *pgrp;
672 int nfound;
673 int signum = ksi->ksi_signo;
674
675 pc = cp->p_cred;
676 nfound = 0;
677 if (all) {
678 /*
679 * broadcast
680 */
681 proclist_lock_read();
682 LIST_FOREACH(p, &allproc, p_list) {
683 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
684 p == cp || !CANSIGNAL(cp, pc, p, signum))
685 continue;
686 nfound++;
687 if (signum)
688 kpsignal(p, ksi, NULL);
689 }
690 proclist_unlock_read();
691 } else {
692 if (pgid == 0)
693 /*
694 * zero pgid means send to my process group.
695 */
696 pgrp = cp->p_pgrp;
697 else {
698 pgrp = pgfind(pgid);
699 if (pgrp == NULL)
700 return (ESRCH);
701 }
702 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
703 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
704 !CANSIGNAL(cp, pc, p, signum))
705 continue;
706 nfound++;
707 if (signum && P_ZOMBIE(p) == 0)
708 kpsignal(p, ksi, NULL);
709 }
710 }
711 return (nfound ? 0 : ESRCH);
712 }
713
714 /*
715 * Send a signal to a process group.
716 */
717 void
718 gsignal(int pgid, int signum)
719 {
720 ksiginfo_t ksi;
721 memset(&ksi, 0, sizeof(ksi));
722 ksi.ksi_signo = signum;
723 kgsignal(pgid, &ksi, NULL);
724 }
725
726 void
727 kgsignal(int pgid, ksiginfo_t *ksi, void *data)
728 {
729 struct pgrp *pgrp;
730
731 if (pgid && (pgrp = pgfind(pgid)))
732 kpgsignal(pgrp, ksi, data, 0);
733 }
734
735 /*
736 * Send a signal to a process group. If checktty is 1,
737 * limit to members which have a controlling terminal.
738 */
739 void
740 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
741 {
742 ksiginfo_t ksi;
743 memset(&ksi, 0, sizeof(ksi));
744 ksi.ksi_signo = sig;
745 kpgsignal(pgrp, &ksi, NULL, checkctty);
746 }
747
748 void
749 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
750 {
751 struct proc *p;
752
753 if (pgrp)
754 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
755 if (checkctty == 0 || p->p_flag & P_CONTROLT)
756 kpsignal(p, ksi, data);
757 }
758
759 /*
760 * Send a signal caused by a trap to the current process.
761 * If it will be caught immediately, deliver it with correct code.
762 * Otherwise, post it normally.
763 */
764 #ifndef __HAVE_SIGINFO
765 void _trapsignal(struct lwp *, ksiginfo_t *);
766 void
767 trapsignal(struct lwp *l, int signum, u_long code)
768 {
769 #define trapsignal _trapsignal
770 ksiginfo_t ksi;
771 memset(&ksi, 0, sizeof(ksi));
772 ksi.ksi_signo = signum;
773 ksi.ksi_trap = (int)code;
774 trapsignal(l, &ksi);
775 }
776 #endif
777
778 void
779 trapsignal(struct lwp *l, ksiginfo_t *ksi)
780 {
781 struct proc *p;
782 struct sigacts *ps;
783 int signum = ksi->ksi_signo;
784
785 p = l->l_proc;
786 ps = p->p_sigacts;
787 if ((p->p_flag & P_TRACED) == 0 &&
788 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
789 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
790 p->p_stats->p_ru.ru_nsignals++;
791 #ifdef KTRACE
792 #ifdef notyet
793 if (KTRPOINT(p, KTR_PSIGINFO))
794 ktrpsiginfo(p, ksi, SIGACTION_PS(ps, signum).sa_handler,
795 &p->p_sigctx.ps_sigmask);
796 #else
797 if (KTRPOINT(p, KTR_PSIG))
798 ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
799 &p->p_sigctx.ps_sigmask, 0);
800 #endif
801 #endif
802 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
803 (void) splsched(); /* XXXSMP */
804 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
805 &p->p_sigctx.ps_sigmask);
806 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
807 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
808 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
809 sigaddset(&p->p_sigctx.ps_sigignore, signum);
810 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
811 }
812 (void) spl0(); /* XXXSMP */
813 } else {
814 /* XXX for core dump/debugger */
815 p->p_sigctx.ps_siginfo = *ksi;
816 p->p_sigctx.ps_lwp = l->l_lid;
817 kpsignal(p, ksi, NULL);
818 }
819 }
820
821 /*
822 * Send the signal to the process. If the signal has an action, the action
823 * is usually performed by the target process rather than the caller; we add
824 * the signal to the set of pending signals for the process.
825 *
826 * Exceptions:
827 * o When a stop signal is sent to a sleeping process that takes the
828 * default action, the process is stopped without awakening it.
829 * o SIGCONT restarts stopped processes (or puts them back to sleep)
830 * regardless of the signal action (eg, blocked or ignored).
831 *
832 * Other ignored signals are discarded immediately.
833 *
834 * XXXSMP: Invoked as psignal() or sched_psignal().
835 */
836 void
837 psignal1(struct proc *p, int signum, int dolock)
838 {
839 ksiginfo_t ksi;
840 memset(&ksi, 0, sizeof(ksi));
841 ksi.ksi_signo = signum;
842 kpsignal1(p, &ksi, NULL, dolock);
843 }
844
845 void
846 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data,
847 int dolock) /* XXXSMP: works, but icky */
848 {
849 struct lwp *l, *suspended;
850 int s = 0, prop, allsusp;
851 sig_t action;
852 int signum = ksi->ksi_signo;
853
854 #ifdef DIAGNOSTIC
855 if (signum <= 0 || signum >= NSIG)
856 panic("psignal signal number %d", signum);
857
858
859 /* XXXSMP: works, but icky */
860 if (dolock)
861 SCHED_ASSERT_UNLOCKED();
862 else
863 SCHED_ASSERT_LOCKED();
864 #endif
865
866 if (data) {
867 size_t fd;
868 struct filedesc *fdp = p->p_fd;
869 ksi->ksi_fd = -1;
870 for (fd = 0; fd < fdp->fd_nfiles; fd++) {
871 struct file *fp = fdp->fd_ofiles[fd];
872 /* XXX: lock? */
873 if (fp && fp->f_data == data) {
874 ksi->ksi_fd = fd;
875 break;
876 }
877 }
878 }
879
880 /*
881 * Notify any interested parties in the signal.
882 */
883 KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
884
885 prop = sigprop[signum];
886
887 /*
888 * If proc is traced, always give parent a chance.
889 */
890 if (p->p_flag & P_TRACED)
891 action = SIG_DFL;
892 else {
893 /*
894 * If the signal is being ignored,
895 * then we forget about it immediately.
896 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
897 * and if it is set to SIG_IGN,
898 * action will be SIG_DFL here.)
899 */
900 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
901 return;
902 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
903 action = SIG_HOLD;
904 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
905 action = SIG_CATCH;
906 else {
907 action = SIG_DFL;
908
909 if (prop & SA_KILL && p->p_nice > NZERO)
910 p->p_nice = NZERO;
911
912 /*
913 * If sending a tty stop signal to a member of an
914 * orphaned process group, discard the signal here if
915 * the action is default; don't stop the process below
916 * if sleeping, and don't clear any pending SIGCONT.
917 */
918 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
919 return;
920 }
921 }
922
923 if (prop & SA_CONT)
924 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
925
926 if (prop & SA_STOP)
927 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
928
929 sigaddset(&p->p_sigctx.ps_siglist, signum);
930
931 /* CHECKSIGS() is "inlined" here. */
932 p->p_sigctx.ps_sigcheck = 1;
933
934 /*
935 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
936 * please!), check if anything waits on it. If yes, clear the
937 * pending signal from siglist set, save it to ps_sigwaited,
938 * clear sigwait list, and wakeup any sigwaiters.
939 * The signal won't be processed further here.
940 */
941 if ((prop & SA_CANTMASK) == 0
942 && p->p_sigctx.ps_sigwaited < 0
943 && sigismember(&p->p_sigctx.ps_sigwait, signum)
944 && p->p_stat != SSTOP) {
945 sigdelset(&p->p_sigctx.ps_siglist, signum);
946 p->p_sigctx.ps_sigwaited = signum;
947 sigemptyset(&p->p_sigctx.ps_sigwait);
948
949 if (dolock)
950 wakeup_one(&p->p_sigctx.ps_sigwait);
951 else
952 sched_wakeup(&p->p_sigctx.ps_sigwait);
953 return;
954 }
955
956 /*
957 * Defer further processing for signals which are held,
958 * except that stopped processes must be continued by SIGCONT.
959 */
960 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
961 return;
962 /* XXXSMP: works, but icky */
963 if (dolock)
964 SCHED_LOCK(s);
965
966 /* XXXUPSXXX LWPs might go to sleep without passing signal handling */
967 if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)
968 && !((p->p_flag & P_SA) && (p->p_sa->sa_idle != NULL))) {
969 /*
970 * At least one LWP is running or on a run queue.
971 * The signal will be noticed when one of them returns
972 * to userspace.
973 */
974 signotify(p);
975 /*
976 * The signal will be noticed very soon.
977 */
978 goto out;
979 } else {
980 /* Process is sleeping or stopped */
981 if (p->p_flag & P_SA) {
982 struct lwp *l2 = p->p_sa->sa_vp;
983 l = NULL;
984 allsusp = 1;
985
986 if ((l2->l_stat == LSSLEEP) && (l2->l_flag & L_SINTR))
987 l = l2;
988 else if (l2->l_stat == LSSUSPENDED)
989 suspended = l2;
990 else if ((l2->l_stat != LSZOMB) &&
991 (l2->l_stat != LSDEAD))
992 allsusp = 0;
993 } else {
994 /*
995 * Find out if any of the sleeps are interruptable,
996 * and if all the live LWPs remaining are suspended.
997 */
998 allsusp = 1;
999 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1000 if (l->l_stat == LSSLEEP &&
1001 l->l_flag & L_SINTR)
1002 break;
1003 if (l->l_stat == LSSUSPENDED)
1004 suspended = l;
1005 else if ((l->l_stat != LSZOMB) &&
1006 (l->l_stat != LSDEAD))
1007 allsusp = 0;
1008 }
1009 }
1010 if (p->p_stat == SACTIVE) {
1011
1012
1013 if (l != NULL && (p->p_flag & P_TRACED))
1014 goto run;
1015
1016 /*
1017 * If SIGCONT is default (or ignored) and process is
1018 * asleep, we are finished; the process should not
1019 * be awakened.
1020 */
1021 if ((prop & SA_CONT) && action == SIG_DFL) {
1022 sigdelset(&p->p_sigctx.ps_siglist, signum);
1023 goto out;
1024 }
1025
1026 /*
1027 * When a sleeping process receives a stop
1028 * signal, process immediately if possible.
1029 */
1030 if ((prop & SA_STOP) && action == SIG_DFL) {
1031 /*
1032 * If a child holding parent blocked,
1033 * stopping could cause deadlock.
1034 */
1035 if (p->p_flag & P_PPWAIT)
1036 goto out;
1037 sigdelset(&p->p_sigctx.ps_siglist, signum);
1038 p->p_xstat = signum;
1039 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
1040 /*
1041 * XXXSMP: recursive call; don't lock
1042 * the second time around.
1043 */
1044 sched_psignal(p->p_pptr, SIGCHLD);
1045 }
1046 proc_stop(p); /* XXXSMP: recurse? */
1047 goto out;
1048 }
1049
1050 if (l == NULL) {
1051 /*
1052 * Special case: SIGKILL of a process
1053 * which is entirely composed of
1054 * suspended LWPs should succeed. We
1055 * make this happen by unsuspending one of
1056 * them.
1057 */
1058 if (allsusp && (signum == SIGKILL))
1059 lwp_continue(suspended);
1060 goto out;
1061 }
1062 /*
1063 * All other (caught or default) signals
1064 * cause the process to run.
1065 */
1066 goto runfast;
1067 /*NOTREACHED*/
1068 } else if (p->p_stat == SSTOP) {
1069 /* Process is stopped */
1070 /*
1071 * If traced process is already stopped,
1072 * then no further action is necessary.
1073 */
1074 if (p->p_flag & P_TRACED)
1075 goto out;
1076
1077 /*
1078 * Kill signal always sets processes running,
1079 * if possible.
1080 */
1081 if (signum == SIGKILL) {
1082 l = proc_unstop(p);
1083 if (l)
1084 goto runfast;
1085 goto out;
1086 }
1087
1088 if (prop & SA_CONT) {
1089 /*
1090 * If SIGCONT is default (or ignored),
1091 * we continue the process but don't
1092 * leave the signal in ps_siglist, as
1093 * it has no further action. If
1094 * SIGCONT is held, we continue the
1095 * process and leave the signal in
1096 * ps_siglist. If the process catches
1097 * SIGCONT, let it handle the signal
1098 * itself. If it isn't waiting on an
1099 * event, then it goes back to run
1100 * state. Otherwise, process goes
1101 * back to sleep state.
1102 */
1103 if (action == SIG_DFL)
1104 sigdelset(&p->p_sigctx.ps_siglist,
1105 signum);
1106 l = proc_unstop(p);
1107 if (l && (action == SIG_CATCH))
1108 goto runfast;
1109 goto out;
1110 }
1111
1112 if (prop & SA_STOP) {
1113 /*
1114 * Already stopped, don't need to stop again.
1115 * (If we did the shell could get confused.)
1116 */
1117 sigdelset(&p->p_sigctx.ps_siglist, signum);
1118 goto out;
1119 }
1120
1121 /*
1122 * If a lwp is sleeping interruptibly, then
1123 * wake it up; it will run until the kernel
1124 * boundary, where it will stop in issignal(),
1125 * since p->p_stat is still SSTOP. When the
1126 * process is continued, it will be made
1127 * runnable and can look at the signal.
1128 */
1129 if (l)
1130 goto run;
1131 goto out;
1132 } else {
1133 /* Else what? */
1134 panic("psignal: Invalid process state %d.",
1135 p->p_stat);
1136 }
1137 }
1138 /*NOTREACHED*/
1139
1140 runfast:
1141 /*
1142 * Raise priority to at least PUSER.
1143 */
1144 if (l->l_priority > PUSER)
1145 l->l_priority = PUSER;
1146 run:
1147
1148 setrunnable(l); /* XXXSMP: recurse? */
1149 out:
1150 /* XXXSMP: works, but icky */
1151 if (dolock)
1152 SCHED_UNLOCK(s);
1153 }
1154
1155 void
1156 kpsendsig(struct lwp *l, ksiginfo_t *ksi, sigset_t *mask)
1157 {
1158 struct proc *p = l->l_proc;
1159 struct lwp *le, *li;
1160 siginfo_t *si;
1161
1162 if (p->p_flag & P_SA) {
1163
1164 /* XXXUPSXXX What if not on sa_vp ? */
1165
1166 int s = l->l_flag & L_SA;
1167 l->l_flag &= ~L_SA;
1168 si = pool_get(&siginfo_pool, PR_WAITOK);
1169 si->_info = *ksi;
1170 le = li = NULL;
1171 if (ksi->ksi_trap)
1172 le = l;
1173 else
1174 li = l;
1175
1176 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1177 sizeof(siginfo_t), si);
1178 l->l_flag |= s;
1179 return;
1180 }
1181
1182 #ifdef __HAVE_SIGINFO
1183 (*p->p_emul->e_sendsig)(ksi, mask);
1184 #else
1185 (*p->p_emul->e_sendsig)(ksi->ksi_signo, mask, ksi->ksi_trap);
1186 #endif
1187 }
1188
1189 static __inline int firstsig(const sigset_t *);
1190
1191 static __inline int
1192 firstsig(const sigset_t *ss)
1193 {
1194 int sig;
1195
1196 sig = ffs(ss->__bits[0]);
1197 if (sig != 0)
1198 return (sig);
1199 #if NSIG > 33
1200 sig = ffs(ss->__bits[1]);
1201 if (sig != 0)
1202 return (sig + 32);
1203 #endif
1204 #if NSIG > 65
1205 sig = ffs(ss->__bits[2]);
1206 if (sig != 0)
1207 return (sig + 64);
1208 #endif
1209 #if NSIG > 97
1210 sig = ffs(ss->__bits[3]);
1211 if (sig != 0)
1212 return (sig + 96);
1213 #endif
1214 return (0);
1215 }
1216
1217 /*
1218 * If the current process has received a signal (should be caught or cause
1219 * termination, should interrupt current syscall), return the signal number.
1220 * Stop signals with default action are processed immediately, then cleared;
1221 * they aren't returned. This is checked after each entry to the system for
1222 * a syscall or trap (though this can usually be done without calling issignal
1223 * by checking the pending signal masks in the CURSIG macro.) The normal call
1224 * sequence is
1225 *
1226 * while (signum = CURSIG(curlwp))
1227 * postsig(signum);
1228 */
1229 int
1230 issignal(struct lwp *l)
1231 {
1232 struct proc *p = l->l_proc;
1233 int s = 0, signum, prop;
1234 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1235 sigset_t ss;
1236
1237 if (l->l_flag & L_SA) {
1238 struct sadata *sa = p->p_sa;
1239
1240 /* Bail out if we do not own the virtual processor */
1241 if (sa->sa_vp != l)
1242 return 0;
1243 }
1244
1245 if (p->p_stat == SSTOP) {
1246 /*
1247 * The process is stopped/stopping. Stop ourselves now that
1248 * we're on the kernel/userspace boundary.
1249 */
1250 if (dolock)
1251 SCHED_LOCK(s);
1252 l->l_stat = LSSTOP;
1253 p->p_nrlwps--;
1254 if (p->p_flag & P_TRACED)
1255 goto sigtraceswitch;
1256 else
1257 goto sigswitch;
1258 }
1259 for (;;) {
1260 sigpending1(p, &ss);
1261 if (p->p_flag & P_PPWAIT)
1262 sigminusset(&stopsigmask, &ss);
1263 signum = firstsig(&ss);
1264 if (signum == 0) { /* no signal to send */
1265 p->p_sigctx.ps_sigcheck = 0;
1266 if (locked && dolock)
1267 SCHED_LOCK(s);
1268 return (0);
1269 }
1270 /* take the signal! */
1271 sigdelset(&p->p_sigctx.ps_siglist, signum);
1272
1273 /*
1274 * We should see pending but ignored signals
1275 * only if P_TRACED was on when they were posted.
1276 */
1277 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1278 (p->p_flag & P_TRACED) == 0)
1279 continue;
1280
1281 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1282 /*
1283 * If traced, always stop, and stay
1284 * stopped until released by the debugger.
1285 */
1286 p->p_xstat = signum;
1287 if ((p->p_flag & P_FSTRACE) == 0)
1288 psignal1(p->p_pptr, SIGCHLD, dolock);
1289 if (dolock)
1290 SCHED_LOCK(s);
1291 proc_stop(p);
1292 sigtraceswitch:
1293 mi_switch(l, NULL);
1294 SCHED_ASSERT_UNLOCKED();
1295 if (dolock)
1296 splx(s);
1297 else
1298 dolock = 1;
1299
1300 /*
1301 * If we are no longer being traced, or the parent
1302 * didn't give us a signal, look for more signals.
1303 */
1304 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1305 continue;
1306
1307 /*
1308 * If the new signal is being masked, look for other
1309 * signals.
1310 */
1311 signum = p->p_xstat;
1312 p->p_xstat = 0;
1313 /*
1314 * `p->p_sigctx.ps_siglist |= mask' is done
1315 * in setrunnable().
1316 */
1317 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1318 continue;
1319 /* take the signal! */
1320 sigdelset(&p->p_sigctx.ps_siglist, signum);
1321 }
1322
1323 prop = sigprop[signum];
1324
1325 /*
1326 * Decide whether the signal should be returned.
1327 * Return the signal's number, or fall through
1328 * to clear it from the pending mask.
1329 */
1330 switch ((long)SIGACTION(p, signum).sa_handler) {
1331
1332 case (long)SIG_DFL:
1333 /*
1334 * Don't take default actions on system processes.
1335 */
1336 if (p->p_pid <= 1) {
1337 #ifdef DIAGNOSTIC
1338 /*
1339 * Are you sure you want to ignore SIGSEGV
1340 * in init? XXX
1341 */
1342 printf("Process (pid %d) got signal %d\n",
1343 p->p_pid, signum);
1344 #endif
1345 break; /* == ignore */
1346 }
1347 /*
1348 * If there is a pending stop signal to process
1349 * with default action, stop here,
1350 * then clear the signal. However,
1351 * if process is member of an orphaned
1352 * process group, ignore tty stop signals.
1353 */
1354 if (prop & SA_STOP) {
1355 if (p->p_flag & P_TRACED ||
1356 (p->p_pgrp->pg_jobc == 0 &&
1357 prop & SA_TTYSTOP))
1358 break; /* == ignore */
1359 p->p_xstat = signum;
1360 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1361 psignal1(p->p_pptr, SIGCHLD, dolock);
1362 if (dolock)
1363 SCHED_LOCK(s);
1364 proc_stop(p);
1365 sigswitch:
1366 mi_switch(l, NULL);
1367 SCHED_ASSERT_UNLOCKED();
1368 if (dolock)
1369 splx(s);
1370 else
1371 dolock = 1;
1372 break;
1373 } else if (prop & SA_IGNORE) {
1374 /*
1375 * Except for SIGCONT, shouldn't get here.
1376 * Default action is to ignore; drop it.
1377 */
1378 break; /* == ignore */
1379 } else
1380 goto keep;
1381 /*NOTREACHED*/
1382
1383 case (long)SIG_IGN:
1384 /*
1385 * Masking above should prevent us ever trying
1386 * to take action on an ignored signal other
1387 * than SIGCONT, unless process is traced.
1388 */
1389 #ifdef DEBUG_ISSIGNAL
1390 if ((prop & SA_CONT) == 0 &&
1391 (p->p_flag & P_TRACED) == 0)
1392 printf("issignal\n");
1393 #endif
1394 break; /* == ignore */
1395
1396 default:
1397 /*
1398 * This signal has an action, let
1399 * postsig() process it.
1400 */
1401 goto keep;
1402 }
1403 }
1404 /* NOTREACHED */
1405
1406 keep:
1407 /* leave the signal for later */
1408 sigaddset(&p->p_sigctx.ps_siglist, signum);
1409 CHECKSIGS(p);
1410 if (locked && dolock)
1411 SCHED_LOCK(s);
1412 return (signum);
1413 }
1414
1415 /*
1416 * Put the argument process into the stopped state and notify the parent
1417 * via wakeup. Signals are handled elsewhere. The process must not be
1418 * on the run queue.
1419 */
1420 static void
1421 proc_stop(struct proc *p)
1422 {
1423 struct lwp *l;
1424
1425 SCHED_ASSERT_LOCKED();
1426
1427 /* XXX lock process LWP state */
1428 p->p_stat = SSTOP;
1429 p->p_flag &= ~P_WAITED;
1430
1431 /*
1432 * Put as many LWP's as possible in stopped state.
1433 * Sleeping ones will notice the stopped state as they try to
1434 * return to userspace.
1435 */
1436
1437 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1438 if ((l->l_stat == LSONPROC) && (l == curlwp)) {
1439 /* XXX SMP this assumes that a LWP that is LSONPROC
1440 * is curlwp and hence is about to be mi_switched
1441 * away; the only callers of proc_stop() are:
1442 * - psignal
1443 * - issignal()
1444 * For the former, proc_stop() is only called when
1445 * no processes are running, so we don't worry.
1446 * For the latter, proc_stop() is called right
1447 * before mi_switch().
1448 */
1449 l->l_stat = LSSTOP;
1450 p->p_nrlwps--;
1451 }
1452 else if ( (l->l_stat == LSSLEEP) && (l->l_flag & L_SINTR)) {
1453 setrunnable(l);
1454 }
1455
1456 /* !!!UPS!!! FIX ME */
1457 #if 0
1458 else if (l->l_stat == LSRUN) {
1459 /* Remove LWP from the run queue */
1460 remrunqueue(l);
1461 l->l_stat = LSSTOP;
1462 p->p_nrlwps--;
1463 } else if ((l->l_stat == LSSLEEP) ||
1464 (l->l_stat == LSSUSPENDED) ||
1465 (l->l_stat == LSZOMB) ||
1466 (l->l_stat == LSDEAD)) {
1467 /*
1468 * Don't do anything; let sleeping LWPs
1469 * discover the stopped state of the process
1470 * on their way out of the kernel; otherwise,
1471 * things like NFS threads that sleep with
1472 * locks will block the rest of the system
1473 * from getting any work done.
1474 *
1475 * Suspended/dead/zombie LWPs aren't going
1476 * anywhere, so we don't need to touch them.
1477 */
1478 }
1479 #ifdef DIAGNOSTIC
1480 else {
1481 panic("proc_stop: process %d lwp %d "
1482 "in unstoppable state %d.\n",
1483 p->p_pid, l->l_lid, l->l_stat);
1484 }
1485 #endif
1486 #endif
1487 }
1488 /* XXX unlock process LWP state */
1489
1490 sched_wakeup((caddr_t)p->p_pptr);
1491 }
1492
1493 /*
1494 * Given a process in state SSTOP, set the state back to SACTIVE and
1495 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1496 *
1497 * If no LWPs ended up runnable (and therefore able to take a signal),
1498 * return a LWP that is sleeping interruptably. The caller can wake
1499 * that LWP up to take a signal.
1500 */
1501 struct lwp *
1502 proc_unstop(struct proc *p)
1503 {
1504 struct lwp *l, *lr = NULL;
1505 int cantake = 0;
1506
1507 SCHED_ASSERT_LOCKED();
1508
1509 /*
1510 * Our caller wants to be informed if there are only sleeping
1511 * and interruptable LWPs left after we have run so that it
1512 * can invoke setrunnable() if required - return one of the
1513 * interruptable LWPs if this is the case.
1514 */
1515
1516 p->p_stat = SACTIVE;
1517 if (p->p_flag & P_SA) {
1518 /*
1519 * Preferentially select the idle LWP as the interruptable
1520 * LWP to return if it exists.
1521 */
1522 lr = p->p_sa->sa_idle;
1523 if (lr != NULL)
1524 cantake = 1;
1525 }
1526 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1527 if (l->l_stat == LSRUN) {
1528 lr = NULL;
1529 cantake = 1;
1530 }
1531 if (l->l_stat != LSSTOP)
1532 continue;
1533
1534 if (l->l_wchan != NULL) {
1535 l->l_stat = LSSLEEP;
1536 if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1537 lr = l;
1538 cantake = 1;
1539 }
1540 } else {
1541 setrunnable(l);
1542 lr = NULL;
1543 cantake = 1;
1544 }
1545 }
1546
1547 return lr;
1548 }
1549
1550 /*
1551 * Take the action for the specified signal
1552 * from the current set of pending signals.
1553 */
1554 void
1555 postsig(int signum)
1556 {
1557 struct lwp *l;
1558 struct proc *p;
1559 struct sigacts *ps;
1560 sig_t action;
1561 sigset_t *returnmask;
1562
1563 l = curlwp;
1564 p = l->l_proc;
1565 ps = p->p_sigacts;
1566 #ifdef DIAGNOSTIC
1567 if (signum == 0)
1568 panic("postsig");
1569 #endif
1570
1571 KERNEL_PROC_LOCK(l);
1572
1573 sigdelset(&p->p_sigctx.ps_siglist, signum);
1574 action = SIGACTION_PS(ps, signum).sa_handler;
1575 #ifdef KTRACE
1576 if (KTRPOINT(p, KTR_PSIG))
1577 ktrpsig(p,
1578 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1579 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1580 #endif
1581 if (action == SIG_DFL) {
1582 /*
1583 * Default action, where the default is to kill
1584 * the process. (Other cases were ignored above.)
1585 */
1586 sigexit(l, signum);
1587 /* NOTREACHED */
1588 } else {
1589 ksiginfo_t ksi;
1590 /*
1591 * If we get here, the signal must be caught.
1592 */
1593 #ifdef DIAGNOSTIC
1594 if (action == SIG_IGN ||
1595 sigismember(&p->p_sigctx.ps_sigmask, signum))
1596 panic("postsig action");
1597 #endif
1598 /*
1599 * Set the new mask value and also defer further
1600 * occurrences of this signal.
1601 *
1602 * Special case: user has done a sigpause. Here the
1603 * current mask is not of interest, but rather the
1604 * mask from before the sigpause is what we want
1605 * restored after the signal processing is completed.
1606 */
1607 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1608 returnmask = &p->p_sigctx.ps_oldmask;
1609 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1610 } else
1611 returnmask = &p->p_sigctx.ps_sigmask;
1612 p->p_stats->p_ru.ru_nsignals++;
1613 if (p->p_sigctx.ps_siginfo.ksi_signo != signum) {
1614 memset(&ksi, 0, sizeof(ksi));
1615 ksi.ksi_signo = signum;
1616 } else {
1617 ksi = p->p_sigctx.ps_siginfo;
1618 memset(&p->p_sigctx.ps_siginfo, 0,
1619 sizeof(p->p_sigctx.ps_siginfo));
1620 p->p_sigctx.ps_lwp = 0;
1621 }
1622 kpsendsig(l, &ksi, returnmask);
1623 (void) splsched(); /* XXXSMP */
1624 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1625 &p->p_sigctx.ps_sigmask);
1626 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1627 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1628 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1629 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1630 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1631 }
1632 (void) spl0(); /* XXXSMP */
1633 }
1634
1635 KERNEL_PROC_UNLOCK(l);
1636 }
1637
1638 /*
1639 * Kill the current process for stated reason.
1640 */
1641 void
1642 killproc(struct proc *p, const char *why)
1643 {
1644 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1645 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1646 psignal(p, SIGKILL);
1647 }
1648
1649 /*
1650 * Force the current process to exit with the specified signal, dumping core
1651 * if appropriate. We bypass the normal tests for masked and caught signals,
1652 * allowing unrecoverable failures to terminate the process without changing
1653 * signal state. Mark the accounting record with the signal termination.
1654 * If dumping core, save the signal number for the debugger. Calls exit and
1655 * does not return.
1656 */
1657
1658 #if defined(DEBUG)
1659 int kern_logsigexit = 1; /* not static to make public for sysctl */
1660 #else
1661 int kern_logsigexit = 0; /* not static to make public for sysctl */
1662 #endif
1663
1664 static const char logcoredump[] =
1665 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1666 static const char lognocoredump[] =
1667 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1668
1669 /* Wrapper function for use in p_userret */
1670 static void
1671 lwp_coredump_hook(struct lwp *l, void *arg)
1672 {
1673 int s;
1674
1675 /*
1676 * Suspend ourselves, so that the kernel stack and therefore
1677 * the userland registers saved in the trapframe are around
1678 * for coredump() to write them out.
1679 */
1680 KERNEL_PROC_LOCK(l);
1681 l->l_flag &= ~L_DETACHED;
1682 SCHED_LOCK(s);
1683 l->l_stat = LSSUSPENDED;
1684 l->l_proc->p_nrlwps--;
1685 /* XXX NJWLWP check if this makes sense here: */
1686 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1687 mi_switch(l, NULL);
1688 SCHED_ASSERT_UNLOCKED();
1689 splx(s);
1690
1691 lwp_exit(l);
1692 }
1693
1694 void
1695 sigexit(struct lwp *l, int signum)
1696 {
1697 struct proc *p;
1698 #if 0
1699 struct lwp *l2;
1700 #endif
1701 int error, exitsig;
1702
1703 p = l->l_proc;
1704
1705 /*
1706 * Don't permit coredump() or exit1() multiple times
1707 * in the same process.
1708 */
1709 if (p->p_flag & P_WEXIT) {
1710 KERNEL_PROC_UNLOCK(l);
1711 (*p->p_userret)(l, p->p_userret_arg);
1712 }
1713 p->p_flag |= P_WEXIT;
1714 /* We don't want to switch away from exiting. */
1715 /* XXX multiprocessor: stop LWPs on other processors. */
1716 #if 0
1717 if (p->p_flag & P_SA) {
1718 LIST_FOREACH(l2, &p->p_lwps, l_sibling)
1719 l2->l_flag &= ~L_SA;
1720 p->p_flag &= ~P_SA;
1721 }
1722 #endif
1723
1724 /* Make other LWPs stick around long enough to be dumped */
1725 p->p_userret = lwp_coredump_hook;
1726 p->p_userret_arg = NULL;
1727
1728 exitsig = signum;
1729 p->p_acflag |= AXSIG;
1730 if (sigprop[signum] & SA_CORE) {
1731 p->p_sigctx.ps_siginfo.ksi_signo = signum;
1732 if ((error = coredump(l)) == 0)
1733 exitsig |= WCOREFLAG;
1734
1735 if (kern_logsigexit) {
1736 /* XXX What if we ever have really large UIDs? */
1737 int uid = p->p_cred && p->p_ucred ?
1738 (int) p->p_ucred->cr_uid : -1;
1739
1740 if (error)
1741 log(LOG_INFO, lognocoredump, p->p_pid,
1742 p->p_comm, uid, signum, error);
1743 else
1744 log(LOG_INFO, logcoredump, p->p_pid,
1745 p->p_comm, uid, signum);
1746 }
1747
1748 }
1749
1750 exit1(l, W_EXITCODE(0, exitsig));
1751 /* NOTREACHED */
1752 }
1753
1754 /*
1755 * Dump core, into a file named "progname.core" or "core" (depending on the
1756 * value of shortcorename), unless the process was setuid/setgid.
1757 */
1758 int
1759 coredump(struct lwp *l)
1760 {
1761 struct vnode *vp;
1762 struct proc *p;
1763 struct vmspace *vm;
1764 struct ucred *cred;
1765 struct nameidata nd;
1766 struct vattr vattr;
1767 int error, error1;
1768 char name[MAXPATHLEN];
1769
1770 p = l->l_proc;
1771 vm = p->p_vmspace;
1772 cred = p->p_cred->pc_ucred;
1773
1774 /*
1775 * Make sure the process has not set-id, to prevent data leaks.
1776 */
1777 if (p->p_flag & P_SUGID)
1778 return (EPERM);
1779
1780 /*
1781 * Refuse to core if the data + stack + user size is larger than
1782 * the core dump limit. XXX THIS IS WRONG, because of mapped
1783 * data.
1784 */
1785 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1786 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1787 return (EFBIG); /* better error code? */
1788
1789 /*
1790 * The core dump will go in the current working directory. Make
1791 * sure that the directory is still there and that the mount flags
1792 * allow us to write core dumps there.
1793 */
1794 vp = p->p_cwdi->cwdi_cdir;
1795 if (vp->v_mount == NULL ||
1796 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1797 return (EPERM);
1798
1799 error = build_corename(p, name);
1800 if (error)
1801 return error;
1802
1803 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1804 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1805 if (error)
1806 return (error);
1807 vp = nd.ni_vp;
1808
1809 /* Don't dump to non-regular files or files with links. */
1810 if (vp->v_type != VREG ||
1811 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1812 error = EINVAL;
1813 goto out;
1814 }
1815 VATTR_NULL(&vattr);
1816 vattr.va_size = 0;
1817 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1818 VOP_SETATTR(vp, &vattr, cred, p);
1819 p->p_acflag |= ACORE;
1820
1821 /* Now dump the actual core file. */
1822 error = (*p->p_execsw->es_coredump)(l, vp, cred);
1823 out:
1824 VOP_UNLOCK(vp, 0);
1825 error1 = vn_close(vp, FWRITE, cred, p);
1826 if (error == 0)
1827 error = error1;
1828 return (error);
1829 }
1830
1831 /*
1832 * Nonexistent system call-- signal process (may want to handle it).
1833 * Flag error in case process won't see signal immediately (blocked or ignored).
1834 */
1835 /* ARGSUSED */
1836 int
1837 sys_nosys(struct lwp *l, void *v, register_t *retval)
1838 {
1839 struct proc *p;
1840
1841 p = l->l_proc;
1842 psignal(p, SIGSYS);
1843 return (ENOSYS);
1844 }
1845
1846 static int
1847 build_corename(struct proc *p, char dst[MAXPATHLEN])
1848 {
1849 const char *s;
1850 char *d, *end;
1851 int i;
1852
1853 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1854 *s != '\0'; s++) {
1855 if (*s == '%') {
1856 switch (*(s + 1)) {
1857 case 'n':
1858 i = snprintf(d, end - d, "%s", p->p_comm);
1859 break;
1860 case 'p':
1861 i = snprintf(d, end - d, "%d", p->p_pid);
1862 break;
1863 case 'u':
1864 i = snprintf(d, end - d, "%.*s",
1865 (int)sizeof p->p_pgrp->pg_session->s_login,
1866 p->p_pgrp->pg_session->s_login);
1867 break;
1868 case 't':
1869 i = snprintf(d, end - d, "%ld",
1870 p->p_stats->p_start.tv_sec);
1871 break;
1872 default:
1873 goto copy;
1874 }
1875 d += i;
1876 s++;
1877 } else {
1878 copy: *d = *s;
1879 d++;
1880 }
1881 if (d >= end)
1882 return (ENAMETOOLONG);
1883 }
1884 *d = '\0';
1885 return 0;
1886 }
1887
1888 void
1889 getucontext(struct lwp *l, ucontext_t *ucp)
1890 {
1891 struct proc *p;
1892
1893 p = l->l_proc;
1894
1895 ucp->uc_flags = 0;
1896 ucp->uc_link = l->l_ctxlink;
1897
1898 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
1899 ucp->uc_flags |= _UC_SIGMASK;
1900
1901 /*
1902 * The (unsupplied) definition of the `current execution stack'
1903 * in the System V Interface Definition appears to allow returning
1904 * the main context stack.
1905 */
1906 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
1907 ucp->uc_stack.ss_sp = (void *)USRSTACK;
1908 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
1909 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
1910 } else {
1911 /* Simply copy alternate signal execution stack. */
1912 ucp->uc_stack = p->p_sigctx.ps_sigstk;
1913 }
1914 ucp->uc_flags |= _UC_STACK;
1915
1916 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1917 }
1918
1919 /* ARGSUSED */
1920 int
1921 sys_getcontext(struct lwp *l, void *v, register_t *retval)
1922 {
1923 struct sys_getcontext_args /* {
1924 syscallarg(struct __ucontext *) ucp;
1925 } */ *uap = v;
1926 ucontext_t uc;
1927
1928 getucontext(l, &uc);
1929
1930 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
1931 }
1932
1933 int
1934 setucontext(struct lwp *l, const ucontext_t *ucp)
1935 {
1936 struct proc *p;
1937 int error;
1938
1939 p = l->l_proc;
1940 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
1941 return (error);
1942 l->l_ctxlink = ucp->uc_link;
1943 /*
1944 * We might want to take care of the stack portion here but currently
1945 * don't; see the comment in getucontext().
1946 */
1947 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
1948 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
1949
1950 return 0;
1951 }
1952
1953 /* ARGSUSED */
1954 int
1955 sys_setcontext(struct lwp *l, void *v, register_t *retval)
1956 {
1957 struct sys_setcontext_args /* {
1958 syscallarg(const ucontext_t *) ucp;
1959 } */ *uap = v;
1960 ucontext_t uc;
1961 int error;
1962
1963 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
1964 exit1(l, W_EXITCODE(0, 0));
1965 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
1966 (error = setucontext(l, &uc)) != 0)
1967 return (error);
1968
1969 return (EJUSTRETURN);
1970 }
1971
1972 /*
1973 * sigtimedwait(2) system call, used also for implementation
1974 * of sigwaitinfo() and sigwait().
1975 *
1976 * This only handles single LWP in signal wait. libpthread provides
1977 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
1978 *
1979 * XXX no support for queued signals, si_code is always SI_USER.
1980 */
1981 int
1982 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
1983 {
1984 struct sys___sigtimedwait_args /* {
1985 syscallarg(const sigset_t *) set;
1986 syscallarg(siginfo_t *) info;
1987 syscallarg(struct timespec *) timeout;
1988 } */ *uap = v;
1989 sigset_t waitset, twaitset;
1990 struct proc *p = l->l_proc;
1991 int error, signum, s;
1992 int timo = 0;
1993 struct timeval tvstart;
1994 struct timespec ts;
1995
1996 if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset))))
1997 return (error);
1998
1999 /*
2000 * Silently ignore SA_CANTMASK signals. psignal1() would
2001 * ignore SA_CANTMASK signals in waitset, we do this
2002 * only for the below siglist check.
2003 */
2004 sigminusset(&sigcantmask, &waitset);
2005
2006 /*
2007 * First scan siglist and check if there is signal from
2008 * our waitset already pending.
2009 */
2010 twaitset = waitset;
2011 __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
2012 if ((signum = firstsig(&twaitset))) {
2013 /* found pending signal */
2014 sigdelset(&p->p_sigctx.ps_siglist, signum);
2015 goto sig;
2016 }
2017
2018 /*
2019 * Calculate timeout, if it was specified.
2020 */
2021 if (SCARG(uap, timeout)) {
2022 uint64_t ms;
2023
2024 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
2025 return (error);
2026
2027 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
2028 timo = mstohz(ms);
2029 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
2030 timo = 1;
2031 if (timo <= 0)
2032 return (EAGAIN);
2033
2034 /*
2035 * Remember current mono_time, it would be used in
2036 * ECANCELED/ERESTART case.
2037 */
2038 s = splclock();
2039 tvstart = mono_time;
2040 splx(s);
2041 }
2042
2043 /*
2044 * Setup ps_sigwait list.
2045 */
2046 p->p_sigctx.ps_sigwaited = -1;
2047 p->p_sigctx.ps_sigwait = waitset;
2048
2049 /*
2050 * Wait for signal to arrive. We can either be woken up or
2051 * time out.
2052 */
2053 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
2054
2055 /*
2056 * Check if a signal from our wait set has arrived, or if it
2057 * was mere wakeup.
2058 */
2059 if (!error) {
2060 if ((signum = p->p_sigctx.ps_sigwaited) <= 0) {
2061 /* wakeup via _lwp_wakeup() */
2062 error = ECANCELED;
2063 }
2064 }
2065
2066 /*
2067 * On error, clear sigwait indication. psignal1() sets it
2068 * in !error case.
2069 */
2070 if (error) {
2071 p->p_sigctx.ps_sigwaited = 0;
2072
2073 /*
2074 * If the sleep was interrupted (either by signal or wakeup),
2075 * update the timeout and copyout new value back.
2076 * It would be used when the syscall would be restarted
2077 * or called again.
2078 */
2079 if (timo && (error == ERESTART || error == ECANCELED)) {
2080 struct timeval tvnow, tvtimo;
2081 int err;
2082
2083 s = splclock();
2084 tvnow = mono_time;
2085 splx(s);
2086
2087 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
2088
2089 /* compute how much time has passed since start */
2090 timersub(&tvnow, &tvstart, &tvnow);
2091 /* substract passed time from timeout */
2092 timersub(&tvtimo, &tvnow, &tvtimo);
2093
2094 if (tvtimo.tv_sec < 0)
2095 return (EAGAIN);
2096
2097 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
2098
2099 /* copy updated timeout to userland */
2100 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts))))
2101 return (err);
2102 }
2103
2104 return (error);
2105 }
2106
2107 /*
2108 * If a signal from the wait set arrived, copy it to userland.
2109 * XXX no queued signals for now
2110 */
2111 if (signum > 0) {
2112 siginfo_t si;
2113
2114 sig:
2115 memset(&si, 0, sizeof(si));
2116 si.si_signo = signum;
2117 si.si_code = SI_USER;
2118
2119 error = copyout(&si, SCARG(uap, info), sizeof(si));
2120 if (error)
2121 return (error);
2122 }
2123
2124 return (0);
2125 }
2126
2127 /*
2128 * Returns true if signal is ignored or masked for passed process.
2129 */
2130 int
2131 sigismasked(struct proc *p, int sig)
2132 {
2133
2134 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2135 sigismember(&p->p_sigctx.ps_sigmask, sig));
2136 }
2137
2138 static int
2139 filt_sigattach(struct knote *kn)
2140 {
2141 struct proc *p = curproc;
2142
2143 kn->kn_ptr.p_proc = p;
2144 kn->kn_flags |= EV_CLEAR; /* automatically set */
2145
2146 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2147
2148 return (0);
2149 }
2150
2151 static void
2152 filt_sigdetach(struct knote *kn)
2153 {
2154 struct proc *p = kn->kn_ptr.p_proc;
2155
2156 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2157 }
2158
2159 /*
2160 * signal knotes are shared with proc knotes, so we apply a mask to
2161 * the hint in order to differentiate them from process hints. This
2162 * could be avoided by using a signal-specific knote list, but probably
2163 * isn't worth the trouble.
2164 */
2165 static int
2166 filt_signal(struct knote *kn, long hint)
2167 {
2168
2169 if (hint & NOTE_SIGNAL) {
2170 hint &= ~NOTE_SIGNAL;
2171
2172 if (kn->kn_id == hint)
2173 kn->kn_data++;
2174 }
2175 return (kn->kn_data != 0);
2176 }
2177
2178 const struct filterops sig_filtops = {
2179 0, filt_sigattach, filt_sigdetach, filt_signal
2180 };
2181