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