kern_sig.c revision 1.130 1 /* $NetBSD: kern_sig.c,v 1.130 2003/01/18 10:06:29 thorpej 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.130 2003/01/18 10:06:29 thorpej 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 for (l = LIST_FIRST(&p->p_lwps);
890 l != NULL;
891 l = LIST_NEXT(l, l_sibling)) {
892 if (l->l_stat == LSSLEEP &&
893 l->l_flag & L_SINTR)
894 break;
895 if (l->l_stat == LSSUSPENDED)
896 suspended = l;
897 else if ((l->l_stat != LSZOMB) &&
898 (l->l_stat != LSDEAD))
899 allsusp = 0;
900 }
901 }
902 if (p->p_stat == SACTIVE) {
903 /* All LWPs must be sleeping */
904 KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL));
905
906 if (l != NULL && (p->p_flag & P_TRACED))
907 goto run;
908
909 /*
910 * If SIGCONT is default (or ignored) and process is
911 * asleep, we are finished; the process should not
912 * be awakened.
913 */
914 if ((prop & SA_CONT) && action == SIG_DFL) {
915 sigdelset(&p->p_sigctx.ps_siglist, signum);
916 goto out;
917 }
918
919 /*
920 * When a sleeping process receives a stop
921 * signal, process immediately if possible.
922 */
923 if ((prop & SA_STOP) && action == SIG_DFL) {
924 /*
925 * If a child holding parent blocked,
926 * stopping could cause deadlock.
927 */
928 if (p->p_flag & P_PPWAIT)
929 goto out;
930 sigdelset(&p->p_sigctx.ps_siglist, signum);
931 p->p_xstat = signum;
932 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
933 /*
934 * XXXSMP: recursive call; don't lock
935 * the second time around.
936 */
937 sched_psignal(p->p_pptr, SIGCHLD);
938 }
939 proc_stop(p); /* XXXSMP: recurse? */
940 goto out;
941 }
942
943 if (l == NULL) {
944 /*
945 * Special case: SIGKILL of a process
946 * which is entirely composed of
947 * suspended LWPs should succeed. We
948 * make this happen by unsuspending one of
949 * them.
950 */
951 if (allsusp && (signum == SIGKILL))
952 lwp_continue(suspended);
953 goto out;
954 }
955 /*
956 * All other (caught or default) signals
957 * cause the process to run.
958 */
959 goto runfast;
960 /*NOTREACHED*/
961 } else if (p->p_stat == SSTOP) {
962 /* Process is stopped */
963 /*
964 * If traced process is already stopped,
965 * then no further action is necessary.
966 */
967 if (p->p_flag & P_TRACED)
968 goto out;
969
970 /*
971 * Kill signal always sets processes running,
972 * if possible.
973 */
974 if (signum == SIGKILL) {
975 l = proc_unstop(p);
976 if (l)
977 goto runfast;
978 /* XXX should this be possible? */
979 goto out;
980 }
981
982 if (prop & SA_CONT) {
983 /*
984 * If SIGCONT is default (or ignored),
985 * we continue the process but don't
986 * leave the signal in ps_siglist, as
987 * it has no further action. If
988 * SIGCONT is held, we continue the
989 * process and leave the signal in
990 * ps_siglist. If the process catches
991 * SIGCONT, let it handle the signal
992 * itself. If it isn't waiting on an
993 * event, then it goes back to run
994 * state. Otherwise, process goes
995 * back to sleep state.
996 */
997 if (action == SIG_DFL)
998 sigdelset(&p->p_sigctx.ps_siglist,
999 signum);
1000 l = proc_unstop(p);
1001 /*
1002 * XXX see note in proc_unstop(). SIGKILL
1003 * XXX and SIGCONT have conflicting needs.
1004 */
1005 if (l && (l->l_stat == LSSLEEP))
1006 l = NULL;
1007 if (l && (action == SIG_CATCH))
1008 goto runfast;
1009 if (l)
1010 goto run;
1011 goto out;
1012 }
1013
1014 if (prop & SA_STOP) {
1015 /*
1016 * Already stopped, don't need to stop again.
1017 * (If we did the shell could get confused.)
1018 */
1019 sigdelset(&p->p_sigctx.ps_siglist, signum);
1020 goto out;
1021 }
1022
1023 /*
1024 * If process is sleeping interruptibly, then
1025 * simulate a wakeup so that when it is
1026 * continued, it will be made runnable and can
1027 * look at the signal. But don't make the
1028 * process runnable, leave it stopped.
1029 */
1030 if (l)
1031 unsleep(l);
1032 goto out;
1033 } else {
1034 /* Else what? */
1035 panic("psignal: Invalid process state.");
1036 }
1037 }
1038 /*NOTREACHED*/
1039
1040 runfast:
1041 /*
1042 * Raise priority to at least PUSER.
1043 */
1044 if (l->l_priority > PUSER)
1045 l->l_priority = PUSER;
1046 run:
1047 setrunnable(l); /* XXXSMP: recurse? */
1048 out:
1049 /* XXXSMP: works, but icky */
1050 if (dolock)
1051 SCHED_UNLOCK(s);
1052 }
1053
1054 void
1055 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code)
1056 {
1057 struct proc *p = l->l_proc;
1058 struct lwp *le, *li;
1059 siginfo_t *si;
1060
1061 if (p->p_flag & P_SA) {
1062 si = pool_get(&siginfo_pool, PR_WAITOK);
1063 si->si_signo = sig;
1064 si->si_errno = 0;
1065 si->si_code = code;
1066 le = li = NULL;
1067 if (code)
1068 le = l;
1069 else
1070 li = l;
1071
1072 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1073 sizeof(siginfo_t), si);
1074 return;
1075 }
1076
1077 (*p->p_emul->e_sendsig)(sig, mask, code);
1078 }
1079
1080 static __inline int firstsig(const sigset_t *);
1081
1082 static __inline int
1083 firstsig(const sigset_t *ss)
1084 {
1085 int sig;
1086
1087 sig = ffs(ss->__bits[0]);
1088 if (sig != 0)
1089 return (sig);
1090 #if NSIG > 33
1091 sig = ffs(ss->__bits[1]);
1092 if (sig != 0)
1093 return (sig + 32);
1094 #endif
1095 #if NSIG > 65
1096 sig = ffs(ss->__bits[2]);
1097 if (sig != 0)
1098 return (sig + 64);
1099 #endif
1100 #if NSIG > 97
1101 sig = ffs(ss->__bits[3]);
1102 if (sig != 0)
1103 return (sig + 96);
1104 #endif
1105 return (0);
1106 }
1107
1108 /*
1109 * If the current process has received a signal (should be caught or cause
1110 * termination, should interrupt current syscall), return the signal number.
1111 * Stop signals with default action are processed immediately, then cleared;
1112 * they aren't returned. This is checked after each entry to the system for
1113 * a syscall or trap (though this can usually be done without calling issignal
1114 * by checking the pending signal masks in the CURSIG macro.) The normal call
1115 * sequence is
1116 *
1117 * while (signum = CURSIG(curlwp))
1118 * postsig(signum);
1119 */
1120 int
1121 issignal(struct lwp *l)
1122 {
1123 struct proc *p = l->l_proc;
1124 int s = 0, signum, prop;
1125 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1126 sigset_t ss;
1127
1128 if (p->p_stat == SSTOP) {
1129 /*
1130 * The process is stopped/stopping. Stop ourselves now that
1131 * we're on the kernel/userspace boundary.
1132 */
1133 if (dolock)
1134 SCHED_LOCK(s);
1135 l->l_stat = LSSTOP;
1136 p->p_nrlwps--;
1137 if (p->p_flag & P_TRACED)
1138 goto sigtraceswitch;
1139 else
1140 goto sigswitch;
1141 }
1142 for (;;) {
1143 sigpending1(p, &ss);
1144 if (p->p_flag & P_PPWAIT)
1145 sigminusset(&stopsigmask, &ss);
1146 signum = firstsig(&ss);
1147 if (signum == 0) { /* no signal to send */
1148 p->p_sigctx.ps_sigcheck = 0;
1149 if (locked && dolock)
1150 SCHED_LOCK(s);
1151 return (0);
1152 }
1153 /* take the signal! */
1154 sigdelset(&p->p_sigctx.ps_siglist, signum);
1155
1156 /*
1157 * We should see pending but ignored signals
1158 * only if P_TRACED was on when they were posted.
1159 */
1160 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1161 (p->p_flag & P_TRACED) == 0)
1162 continue;
1163
1164 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1165 /*
1166 * If traced, always stop, and stay
1167 * stopped until released by the debugger.
1168 */
1169 p->p_xstat = signum;
1170 if ((p->p_flag & P_FSTRACE) == 0)
1171 psignal1(p->p_pptr, SIGCHLD, dolock);
1172 if (dolock)
1173 SCHED_LOCK(s);
1174 proc_stop(p);
1175 sigtraceswitch:
1176 mi_switch(l, NULL);
1177 SCHED_ASSERT_UNLOCKED();
1178 if (dolock)
1179 splx(s);
1180 else
1181 dolock = 1;
1182
1183 /*
1184 * If we are no longer being traced, or the parent
1185 * didn't give us a signal, look for more signals.
1186 */
1187 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1188 continue;
1189
1190 /*
1191 * If the new signal is being masked, look for other
1192 * signals.
1193 */
1194 signum = p->p_xstat;
1195 p->p_xstat = 0;
1196 /*
1197 * `p->p_sigctx.ps_siglist |= mask' is done
1198 * in setrunnable().
1199 */
1200 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1201 continue;
1202 /* take the signal! */
1203 sigdelset(&p->p_sigctx.ps_siglist, signum);
1204 }
1205
1206 prop = sigprop[signum];
1207
1208 /*
1209 * Decide whether the signal should be returned.
1210 * Return the signal's number, or fall through
1211 * to clear it from the pending mask.
1212 */
1213 switch ((long)SIGACTION(p, signum).sa_handler) {
1214
1215 case (long)SIG_DFL:
1216 /*
1217 * Don't take default actions on system processes.
1218 */
1219 if (p->p_pid <= 1) {
1220 #ifdef DIAGNOSTIC
1221 /*
1222 * Are you sure you want to ignore SIGSEGV
1223 * in init? XXX
1224 */
1225 printf("Process (pid %d) got signal %d\n",
1226 p->p_pid, signum);
1227 #endif
1228 break; /* == ignore */
1229 }
1230 /*
1231 * If there is a pending stop signal to process
1232 * with default action, stop here,
1233 * then clear the signal. However,
1234 * if process is member of an orphaned
1235 * process group, ignore tty stop signals.
1236 */
1237 if (prop & SA_STOP) {
1238 if (p->p_flag & P_TRACED ||
1239 (p->p_pgrp->pg_jobc == 0 &&
1240 prop & SA_TTYSTOP))
1241 break; /* == ignore */
1242 p->p_xstat = signum;
1243 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1244 psignal1(p->p_pptr, SIGCHLD, dolock);
1245 if (dolock)
1246 SCHED_LOCK(s);
1247 proc_stop(p);
1248 sigswitch:
1249 mi_switch(l, NULL);
1250 SCHED_ASSERT_UNLOCKED();
1251 if (dolock)
1252 splx(s);
1253 else
1254 dolock = 1;
1255 break;
1256 } else if (prop & SA_IGNORE) {
1257 /*
1258 * Except for SIGCONT, shouldn't get here.
1259 * Default action is to ignore; drop it.
1260 */
1261 break; /* == ignore */
1262 } else
1263 goto keep;
1264 /*NOTREACHED*/
1265
1266 case (long)SIG_IGN:
1267 /*
1268 * Masking above should prevent us ever trying
1269 * to take action on an ignored signal other
1270 * than SIGCONT, unless process is traced.
1271 */
1272 #ifdef DEBUG_ISSIGNAL
1273 if ((prop & SA_CONT) == 0 &&
1274 (p->p_flag & P_TRACED) == 0)
1275 printf("issignal\n");
1276 #endif
1277 break; /* == ignore */
1278
1279 default:
1280 /*
1281 * This signal has an action, let
1282 * postsig() process it.
1283 */
1284 goto keep;
1285 }
1286 }
1287 /* NOTREACHED */
1288
1289 keep:
1290 /* leave the signal for later */
1291 sigaddset(&p->p_sigctx.ps_siglist, signum);
1292 CHECKSIGS(p);
1293 if (locked && dolock)
1294 SCHED_LOCK(s);
1295 return (signum);
1296 }
1297
1298 /*
1299 * Put the argument process into the stopped state and notify the parent
1300 * via wakeup. Signals are handled elsewhere. The process must not be
1301 * on the run queue.
1302 */
1303 static void
1304 proc_stop(struct proc *p)
1305 {
1306 struct lwp *l;
1307
1308 SCHED_ASSERT_LOCKED();
1309
1310 /* XXX lock process LWP state */
1311 p->p_stat = SSTOP;
1312 p->p_flag &= ~P_WAITED;
1313
1314 /*
1315 * Put as many LWP's as possible in stopped state.
1316 * Sleeping ones will notice the stopped state as they try to
1317 * return to userspace.
1318 */
1319
1320 for (l = LIST_FIRST(&p->p_lwps); l != NULL;
1321 l = LIST_NEXT(l, l_sibling)) {
1322 if (l->l_stat == LSONPROC) {
1323 /* XXX SMP this assumes that a LWP that is LSONPROC
1324 * is curlwp and hence is about to be mi_switched
1325 * away; the only callers of proc_stop() are:
1326 * - psignal
1327 * - issignal()
1328 * For the former, proc_stop() is only called when
1329 * no processes are running, so we don't worry.
1330 * For the latter, proc_stop() is called right
1331 * before mi_switch().
1332 */
1333 l->l_stat = LSSTOP;
1334 p->p_nrlwps--;
1335 } else if (l->l_stat == LSRUN) {
1336 /* Remove LWP from the run queue */
1337 remrunqueue(l);
1338 l->l_stat = LSSTOP;
1339 p->p_nrlwps--;
1340 } else if ((l->l_stat == LSSLEEP) ||
1341 (l->l_stat == LSSUSPENDED) ||
1342 (l->l_stat == LSZOMB) ||
1343 (l->l_stat == LSDEAD)) {
1344 /*
1345 * Don't do anything; let sleeping LWPs
1346 * discover the stopped state of the process
1347 * on their way out of the kernel; otherwise,
1348 * things like NFS threads that sleep with
1349 * locks will block the rest of the system
1350 * from getting any work done.
1351 *
1352 * Suspended/dead/zombie LWPs aren't going
1353 * anywhere, so we don't need to touch them.
1354 */
1355 }
1356 #ifdef DIAGNOSTIC
1357 else {
1358 panic("proc_stop: process %d lwp %d "
1359 "in unstoppable state %d.\n",
1360 p->p_pid, l->l_lid, l->l_stat);
1361 }
1362 #endif
1363 }
1364 /* XXX unlock process LWP state */
1365
1366 sched_wakeup((caddr_t)p->p_pptr);
1367 }
1368
1369 struct lwp *
1370 proc_unstop(p)
1371 struct proc *p;
1372 {
1373 struct lwp *l, *lr = NULL;
1374
1375 SCHED_ASSERT_LOCKED();
1376
1377 /*
1378 * Our caller will want to invoke setrunnable() on whatever we return,
1379 * provided that it isn't NULL.
1380 */
1381
1382 p->p_stat = SACTIVE;
1383 /*
1384 * For the benefit of SIGKILL, return the idle LWP if there's
1385 * nothing better (and if there is an idle LWP, there
1386 * shouldn't be anything better.
1387 * XXX This is bad for SIGCONT; SIGSTOP/SIGCONT shouldn't
1388 * XXX noticably affect the state of a process, idling or
1389 * XXX not. We work around this in the SIGCONT handling in
1390 * XXX psignal().
1391 */
1392 if (p->p_flag & P_SA)
1393 lr = p->p_sa->sa_idle; /* OK if this is NULL. */
1394 for (l = LIST_FIRST(&p->p_lwps); l != NULL;
1395 l = LIST_NEXT(l, l_sibling))
1396 if (l->l_stat == LSSTOP) {
1397 if (l->l_wchan == 0) {
1398 if (lr == NULL || l == lr)
1399 lr = l;
1400 else
1401 setrunnable(l);
1402 } else
1403 l->l_stat = LSSLEEP;
1404 }
1405
1406 return lr;
1407 }
1408
1409 /*
1410 * Take the action for the specified signal
1411 * from the current set of pending signals.
1412 */
1413 void
1414 postsig(int signum)
1415 {
1416 struct lwp *l;
1417 struct proc *p;
1418 struct sigacts *ps;
1419 sig_t action;
1420 u_long code;
1421 sigset_t *returnmask;
1422
1423 l = curlwp;
1424 p = l->l_proc;
1425 ps = p->p_sigacts;
1426 #ifdef DIAGNOSTIC
1427 if (signum == 0)
1428 panic("postsig");
1429 #endif
1430
1431 KERNEL_PROC_LOCK(l);
1432
1433 sigdelset(&p->p_sigctx.ps_siglist, signum);
1434 action = SIGACTION_PS(ps, signum).sa_handler;
1435 #ifdef KTRACE
1436 if (KTRPOINT(p, KTR_PSIG))
1437 ktrpsig(p,
1438 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1439 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1440 #endif
1441 if (action == SIG_DFL) {
1442 /*
1443 * Default action, where the default is to kill
1444 * the process. (Other cases were ignored above.)
1445 */
1446 sigexit(l, signum);
1447 /* NOTREACHED */
1448 } else {
1449 /*
1450 * If we get here, the signal must be caught.
1451 */
1452 #ifdef DIAGNOSTIC
1453 if (action == SIG_IGN ||
1454 sigismember(&p->p_sigctx.ps_sigmask, signum))
1455 panic("postsig action");
1456 #endif
1457 /*
1458 * Set the new mask value and also defer further
1459 * occurences of this signal.
1460 *
1461 * Special case: user has done a sigpause. Here the
1462 * current mask is not of interest, but rather the
1463 * mask from before the sigpause is what we want
1464 * restored after the signal processing is completed.
1465 */
1466 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1467 returnmask = &p->p_sigctx.ps_oldmask;
1468 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1469 } else
1470 returnmask = &p->p_sigctx.ps_sigmask;
1471 p->p_stats->p_ru.ru_nsignals++;
1472 if (p->p_sigctx.ps_sig != signum) {
1473 code = 0;
1474 } else {
1475 code = p->p_sigctx.ps_code;
1476 p->p_sigctx.ps_code = 0;
1477 p->p_sigctx.ps_sig = 0;
1478 }
1479 psendsig(l, signum, returnmask, code);
1480 (void) splsched(); /* XXXSMP */
1481 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1482 &p->p_sigctx.ps_sigmask);
1483 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1484 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1485 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1486 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1487 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1488 }
1489 (void) spl0(); /* XXXSMP */
1490 }
1491
1492 KERNEL_PROC_UNLOCK(l);
1493 }
1494
1495 /*
1496 * Kill the current process for stated reason.
1497 */
1498 void
1499 killproc(struct proc *p, const char *why)
1500 {
1501
1502 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1503 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1504 psignal(p, SIGKILL);
1505 }
1506
1507 /*
1508 * Force the current process to exit with the specified signal, dumping core
1509 * if appropriate. We bypass the normal tests for masked and caught signals,
1510 * allowing unrecoverable failures to terminate the process without changing
1511 * signal state. Mark the accounting record with the signal termination.
1512 * If dumping core, save the signal number for the debugger. Calls exit and
1513 * does not return.
1514 */
1515
1516 #if defined(DEBUG)
1517 int kern_logsigexit = 1; /* not static to make public for sysctl */
1518 #else
1519 int kern_logsigexit = 0; /* not static to make public for sysctl */
1520 #endif
1521
1522 static const char logcoredump[] =
1523 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1524 static const char lognocoredump[] =
1525 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1526
1527 /* Wrapper function for use in p_userret */
1528 static void
1529 lwp_coredump_hook(struct lwp *l, void *arg)
1530 {
1531 int s;
1532
1533 /*
1534 * Suspend ourselves, so that the kernel stack and therefore
1535 * the userland registers saved in the trapframe are around
1536 * for coredump() to write them out.
1537 */
1538 KERNEL_PROC_LOCK(l);
1539 l->l_flag &= ~L_DETACHED;
1540 SCHED_LOCK(s);
1541 l->l_stat = LSSUSPENDED;
1542 l->l_proc->p_nrlwps--;
1543 /* XXX NJWLWP check if this makes sense here: */
1544 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1545 mi_switch(l, NULL);
1546 SCHED_ASSERT_UNLOCKED();
1547 splx(s);
1548
1549 lwp_exit(l);
1550 }
1551
1552 void
1553 sigexit(struct lwp *l, int signum)
1554 {
1555 struct proc *p;
1556 int error, exitsig;
1557
1558 p = l->l_proc;
1559
1560 /*
1561 * Don't permit coredump() or exit1() multiple times
1562 * in the same process.
1563 */
1564 if (p->p_flag & P_WEXIT)
1565 (*p->p_userret)(l, p->p_userret_arg);
1566 p->p_flag |= P_WEXIT;
1567 /* We don't want to switch away from exiting. */
1568 /* XXX multiprocessor: stop LWPs on other processors. */
1569 if (l->l_flag & L_SA) {
1570 l->l_flag &= ~L_SA;
1571 p->p_flag &= ~P_SA;
1572 }
1573
1574 /* Make other LWPs stick around long enough to be dumped */
1575 p->p_userret = lwp_coredump_hook;
1576 p->p_userret_arg = NULL;
1577
1578 exitsig = signum;
1579 p->p_acflag |= AXSIG;
1580 if (sigprop[signum] & SA_CORE) {
1581 p->p_sigctx.ps_sig = signum;
1582 if ((error = coredump(l)) == 0)
1583 exitsig |= WCOREFLAG;
1584
1585 if (kern_logsigexit) {
1586 /* XXX What if we ever have really large UIDs? */
1587 int uid = p->p_cred && p->p_ucred ?
1588 (int) p->p_ucred->cr_uid : -1;
1589
1590 if (error)
1591 log(LOG_INFO, lognocoredump, p->p_pid,
1592 p->p_comm, uid, signum, error);
1593 else
1594 log(LOG_INFO, logcoredump, p->p_pid,
1595 p->p_comm, uid, signum);
1596 }
1597
1598 }
1599
1600 exit1(l, W_EXITCODE(0, exitsig));
1601 /* NOTREACHED */
1602 }
1603
1604 /*
1605 * Dump core, into a file named "progname.core" or "core" (depending on the
1606 * value of shortcorename), unless the process was setuid/setgid.
1607 */
1608 int
1609 coredump(struct lwp *l)
1610 {
1611 struct vnode *vp;
1612 struct proc *p;
1613 struct vmspace *vm;
1614 struct ucred *cred;
1615 struct nameidata nd;
1616 struct vattr vattr;
1617 int error, error1;
1618 char name[MAXPATHLEN];
1619
1620 p = l->l_proc;
1621 vm = p->p_vmspace;
1622 cred = p->p_cred->pc_ucred;
1623
1624 /*
1625 * Make sure the process has not set-id, to prevent data leaks.
1626 */
1627 if (p->p_flag & P_SUGID)
1628 return (EPERM);
1629
1630 /*
1631 * Refuse to core if the data + stack + user size is larger than
1632 * the core dump limit. XXX THIS IS WRONG, because of mapped
1633 * data.
1634 */
1635 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1636 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1637 return (EFBIG); /* better error code? */
1638
1639 /*
1640 * The core dump will go in the current working directory. Make
1641 * sure that the directory is still there and that the mount flags
1642 * allow us to write core dumps there.
1643 */
1644 vp = p->p_cwdi->cwdi_cdir;
1645 if (vp->v_mount == NULL ||
1646 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1647 return (EPERM);
1648
1649 error = build_corename(p, name);
1650 if (error)
1651 return error;
1652
1653 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1654 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1655 if (error)
1656 return (error);
1657 vp = nd.ni_vp;
1658
1659 /* Don't dump to non-regular files or files with links. */
1660 if (vp->v_type != VREG ||
1661 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1662 error = EINVAL;
1663 goto out;
1664 }
1665 VATTR_NULL(&vattr);
1666 vattr.va_size = 0;
1667 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1668 VOP_SETATTR(vp, &vattr, cred, p);
1669 p->p_acflag |= ACORE;
1670
1671 /* Now dump the actual core file. */
1672 error = (*p->p_execsw->es_coredump)(l, vp, cred);
1673 out:
1674 VOP_UNLOCK(vp, 0);
1675 error1 = vn_close(vp, FWRITE, cred, p);
1676 if (error == 0)
1677 error = error1;
1678 return (error);
1679 }
1680
1681 /*
1682 * Nonexistent system call-- signal process (may want to handle it).
1683 * Flag error in case process won't see signal immediately (blocked or ignored).
1684 */
1685 /* ARGSUSED */
1686 int
1687 sys_nosys(struct lwp *l, void *v, register_t *retval)
1688 {
1689 struct proc *p;
1690
1691 p = l->l_proc;
1692 psignal(p, SIGSYS);
1693 return (ENOSYS);
1694 }
1695
1696 static int
1697 build_corename(struct proc *p, char dst[MAXPATHLEN])
1698 {
1699 const char *s;
1700 char *d, *end;
1701 int i;
1702
1703 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1704 *s != '\0'; s++) {
1705 if (*s == '%') {
1706 switch (*(s + 1)) {
1707 case 'n':
1708 i = snprintf(d, end - d, "%s", p->p_comm);
1709 break;
1710 case 'p':
1711 i = snprintf(d, end - d, "%d", p->p_pid);
1712 break;
1713 case 'u':
1714 i = snprintf(d, end - d, "%s",
1715 p->p_pgrp->pg_session->s_login);
1716 break;
1717 case 't':
1718 i = snprintf(d, end - d, "%ld",
1719 p->p_stats->p_start.tv_sec);
1720 break;
1721 default:
1722 goto copy;
1723 }
1724 d += i;
1725 s++;
1726 } else {
1727 copy: *d = *s;
1728 d++;
1729 }
1730 if (d >= end)
1731 return (ENAMETOOLONG);
1732 }
1733 *d = '\0';
1734 return 0;
1735 }
1736
1737 void
1738 getucontext(struct lwp *l, ucontext_t *ucp)
1739 {
1740 struct proc *p;
1741
1742 p = l->l_proc;
1743
1744 ucp->uc_flags = 0;
1745 ucp->uc_link = l->l_ctxlink;
1746
1747 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
1748 ucp->uc_flags |= _UC_SIGMASK;
1749
1750 /*
1751 * The (unsupplied) definition of the `current execution stack'
1752 * in the System V Interface Definition appears to allow returning
1753 * the main context stack.
1754 */
1755 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
1756 ucp->uc_stack.ss_sp = (void *)USRSTACK;
1757 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
1758 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
1759 } else {
1760 /* Simply copy alternate signal execution stack. */
1761 ucp->uc_stack = p->p_sigctx.ps_sigstk;
1762 }
1763 ucp->uc_flags |= _UC_STACK;
1764
1765 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1766 }
1767
1768 /* ARGSUSED */
1769 int
1770 sys_getcontext(struct lwp *l, void *v, register_t *retval)
1771 {
1772 struct sys_getcontext_args /* {
1773 syscallarg(struct __ucontext *) ucp;
1774 } */ *uap = v;
1775 ucontext_t uc;
1776
1777 getucontext(l, &uc);
1778
1779 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
1780 }
1781
1782 int
1783 setucontext(struct lwp *l, const ucontext_t *ucp)
1784 {
1785 struct proc *p;
1786 int error;
1787
1788 p = l->l_proc;
1789 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
1790 return (error);
1791 l->l_ctxlink = ucp->uc_link;
1792 /*
1793 * We might want to take care of the stack portion here but currently
1794 * don't; see the comment in getucontext().
1795 */
1796 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
1797 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
1798
1799 return 0;
1800 }
1801
1802 /* ARGSUSED */
1803 int
1804 sys_setcontext(struct lwp *l, void *v, register_t *retval)
1805 {
1806 struct sys_setcontext_args /* {
1807 syscallarg(const ucontext_t *) ucp;
1808 } */ *uap = v;
1809 ucontext_t uc;
1810 int error;
1811
1812 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
1813 exit1(l, W_EXITCODE(0, 0));
1814 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
1815 (error = setucontext(l, &uc)) != 0)
1816 return (error);
1817
1818 return (EJUSTRETURN);
1819 }
1820
1821
1822 /*
1823 * Returns true if signal is ignored or masked for passed process.
1824 */
1825 int
1826 sigismasked(struct proc *p, int sig)
1827 {
1828
1829 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1830 sigismember(&p->p_sigctx.ps_sigmask, sig));
1831 }
1832
1833 static int
1834 filt_sigattach(struct knote *kn)
1835 {
1836 struct proc *p = curproc;
1837
1838 kn->kn_ptr.p_proc = p;
1839 kn->kn_flags |= EV_CLEAR; /* automatically set */
1840
1841 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1842
1843 return (0);
1844 }
1845
1846 static void
1847 filt_sigdetach(struct knote *kn)
1848 {
1849 struct proc *p = kn->kn_ptr.p_proc;
1850
1851 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1852 }
1853
1854 /*
1855 * signal knotes are shared with proc knotes, so we apply a mask to
1856 * the hint in order to differentiate them from process hints. This
1857 * could be avoided by using a signal-specific knote list, but probably
1858 * isn't worth the trouble.
1859 */
1860 static int
1861 filt_signal(struct knote *kn, long hint)
1862 {
1863
1864 if (hint & NOTE_SIGNAL) {
1865 hint &= ~NOTE_SIGNAL;
1866
1867 if (kn->kn_id == hint)
1868 kn->kn_data++;
1869 }
1870 return (kn->kn_data != 0);
1871 }
1872
1873 const struct filterops sig_filtops = {
1874 0, filt_sigattach, filt_sigdetach, filt_signal
1875 };
1876