Deleted Added
sdiff udiff text old ( 46155 ) new ( 46381 )
full compact
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 * $Id: kern_sig.c,v 1.54 1999/01/26 02:38:10 julian Exp $
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
45#define SIGPROP /* include signal properties table */
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/sysproto.h>
49#include <sys/signalvar.h>
50#include <sys/resourcevar.h>
51#include <sys/namei.h>
52#include <sys/vnode.h>
53#include <sys/proc.h>
54#include <sys/pioctl.h>
55#include <sys/systm.h>
56#include <sys/acct.h>
57#include <sys/fcntl.h>
58#include <sys/wait.h>
59#include <sys/ktrace.h>
60#include <sys/syslog.h>
61#include <sys/stat.h>
62#include <sys/sysent.h>
63#include <sys/sysctl.h>
64#include <sys/malloc.h>
65
66#include <machine/cpu.h>
67#ifdef SMP
68#include <machine/smp.h>
69#endif
70
71static int killpg1 __P((struct proc *cp, int signum, int pgid, int all));
72static void setsigvec __P((struct proc *p, int signum, struct sigaction *sa));
73static void stop __P((struct proc *));
74
75static int kern_logsigexit = 1;
76SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, &kern_logsigexit, 0, "");
77
78/*
79 * Can process p, with pcred pc, send the signal signum to process q?
80 */
81#define CANSIGNAL(p, pc, q, signum) \
82 (PRISON_CHECK(p, q) && ((pc)->pc_ucred->cr_uid == 0 || \
83 (pc)->p_ruid == (q)->p_cred->p_ruid || \
84 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
85 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
86 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
87 ((signum) == SIGCONT && (q)->p_session == (p)->p_session)))
88
89/*
90 * Policy -- Can real uid ruid with ucred uc send a signal to process q?
91 */
92#define CANSIGIO(ruid, uc, q) \
93 ((uc)->cr_uid == 0 || \
94 (ruid) == (q)->p_cred->p_ruid || \
95 (uc)->cr_uid == (q)->p_cred->p_ruid || \
96 (ruid) == (q)->p_ucred->cr_uid || \
97 (uc)->cr_uid == (q)->p_ucred->cr_uid)
98
99int sugid_coredump;
100SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, &sugid_coredump, 0, "");
101
102#ifndef _SYS_SYSPROTO_H_
103struct sigaction_args {
104 int signum;
105 struct sigaction *nsa;
106 struct sigaction *osa;
107};
108#endif
109/* ARGSUSED */
110int
111sigaction(p, uap)
112 struct proc *p;
113 register struct sigaction_args *uap;
114{
115 struct sigaction vec;
116 register struct sigaction *sa;
117 register struct sigacts *ps = p->p_sigacts;
118 register int signum;
119 int bit, error;
120
121 signum = uap->signum;
122 if (signum <= 0 || signum >= NSIG)
123 return (EINVAL);
124 sa = &vec;
125 if (uap->osa) {
126 sa->sa_handler = ps->ps_sigact[signum];
127 sa->sa_mask = ps->ps_catchmask[signum];
128 bit = sigmask(signum);
129 sa->sa_flags = 0;
130 if ((ps->ps_sigonstack & bit) != 0)
131 sa->sa_flags |= SA_ONSTACK;
132 if ((ps->ps_sigintr & bit) == 0)
133 sa->sa_flags |= SA_RESTART;
134 if ((ps->ps_sigreset & bit) != 0)
135 sa->sa_flags |= SA_RESETHAND;
136 if ((ps->ps_signodefer & bit) != 0)
137 sa->sa_flags |= SA_NODEFER;
138 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
139 sa->sa_flags |= SA_NOCLDSTOP;
140 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDWAIT)
141 sa->sa_flags |= SA_NOCLDWAIT;
142 if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
143 sizeof (vec))))
144 return (error);
145 }
146 if (uap->nsa) {
147 if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
148 sizeof (vec))))
149 return (error);
150 if ((signum == SIGKILL || signum == SIGSTOP) &&
151 sa->sa_handler != SIG_DFL)
152 return (EINVAL);
153 setsigvec(p, signum, sa);
154 }
155 return (0);
156}
157
158static void
159setsigvec(p, signum, sa)
160 register struct proc *p;
161 int signum;
162 register struct sigaction *sa;
163{
164 register struct sigacts *ps = p->p_sigacts;
165 register int bit;
166
167 bit = sigmask(signum);
168 /*
169 * Change setting atomically.
170 */
171 (void) splhigh();
172 ps->ps_sigact[signum] = sa->sa_handler;
173 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
174 if ((sa->sa_flags & SA_RESTART) == 0)
175 ps->ps_sigintr |= bit;
176 else
177 ps->ps_sigintr &= ~bit;
178 if (sa->sa_flags & SA_ONSTACK)
179 ps->ps_sigonstack |= bit;
180 else
181 ps->ps_sigonstack &= ~bit;
182 if (sa->sa_flags & SA_RESETHAND)
183 ps->ps_sigreset |= bit;
184 else
185 ps->ps_sigreset &= ~bit;
186 if (sa->sa_flags & SA_NODEFER)
187 ps->ps_signodefer |= bit;
188 else
189 ps->ps_signodefer &= ~bit;
190#ifdef COMPAT_SUNOS
191 if (sa->sa_flags & SA_USERTRAMP)
192 ps->ps_usertramp |= bit;
193 else
194 ps->ps_usertramp &= ~bit;
195#endif
196 if (signum == SIGCHLD) {
197 if (sa->sa_flags & SA_NOCLDSTOP)
198 p->p_procsig->ps_flag |= P_NOCLDSTOP;
199 else
200 p->p_procsig->ps_flag &= ~P_NOCLDSTOP;
201 if (sa->sa_flags & SA_NOCLDWAIT) {
202 /*
203 * Paranoia: since SA_NOCLDWAIT is implemented by
204 * reparenting the dying child to PID 1 (and
205 * trust it to reap the zombie), PID 1 itself is
206 * forbidden to set SA_NOCLDWAIT.
207 */
208 if (p->p_pid == 1)
209 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
210 else
211 p->p_procsig->ps_flag |= P_NOCLDWAIT;
212 } else
213 p->p_procsig->ps_flag &= ~P_NOCLDWAIT;
214 }
215 /*
216 * Set bit in p_sigignore for signals that are set to SIG_IGN,
217 * and for signals set to SIG_DFL where the default is to ignore.
218 * However, don't put SIGCONT in p_sigignore,
219 * as we have to restart the process.
220 */
221 if (sa->sa_handler == SIG_IGN ||
222 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
223 p->p_siglist &= ~bit; /* never to be seen again */
224 if (signum != SIGCONT)
225 p->p_sigignore |= bit; /* easier in psignal */
226 p->p_sigcatch &= ~bit;
227 } else {
228 p->p_sigignore &= ~bit;
229 if (sa->sa_handler == SIG_DFL)
230 p->p_sigcatch &= ~bit;
231 else
232 p->p_sigcatch |= bit;
233 }
234 (void) spl0();
235}
236
237/*
238 * Initialize signal state for process 0;
239 * set to ignore signals that are ignored by default.
240 */
241void
242siginit(p)
243 struct proc *p;
244{
245 register int i;
246
247 for (i = 0; i < NSIG; i++)
248 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
249 p->p_sigignore |= sigmask(i);
250}
251
252/*
253 * Reset signals for an exec of the specified process.
254 */
255void
256execsigs(p)
257 register struct proc *p;
258{
259 register struct sigacts *ps = p->p_sigacts;
260 register int nc, mask;
261
262 /*
263 * Reset caught signals. Held signals remain held
264 * through p_sigmask (unless they were caught,
265 * and are now ignored by default).
266 */
267 while (p->p_sigcatch) {
268 nc = ffs((long)p->p_sigcatch);
269 mask = sigmask(nc);
270 p->p_sigcatch &= ~mask;
271 if (sigprop[nc] & SA_IGNORE) {
272 if (nc != SIGCONT)
273 p->p_sigignore |= mask;
274 p->p_siglist &= ~mask;
275 }
276 ps->ps_sigact[nc] = SIG_DFL;
277 }
278 /*
279 * Reset stack state to the user stack.
280 * Clear set of signals caught on the signal stack.
281 */
282 ps->ps_sigstk.ss_flags = SS_DISABLE;
283 ps->ps_sigstk.ss_size = 0;
284 ps->ps_sigstk.ss_sp = 0;
285 ps->ps_flags = 0;
286}
287
288/*
289 * Manipulate signal mask.
290 * Note that we receive new mask, not pointer,
291 * and return old mask as return value;
292 * the library stub does the rest.
293 */
294#ifndef _SYS_SYSPROTO_H_
295struct sigprocmask_args {
296 int how;
297 sigset_t mask;
298};
299#endif
300int
301sigprocmask(p, uap)
302 register struct proc *p;
303 struct sigprocmask_args *uap;
304{
305 int error = 0;
306
307 p->p_retval[0] = p->p_sigmask;
308 (void) splhigh();
309
310 switch (uap->how) {
311 case SIG_BLOCK:
312 p->p_sigmask |= uap->mask &~ sigcantmask;
313 break;
314
315 case SIG_UNBLOCK:
316 p->p_sigmask &= ~uap->mask;
317 break;
318
319 case SIG_SETMASK:
320 p->p_sigmask = uap->mask &~ sigcantmask;
321 break;
322
323 default:
324 error = EINVAL;
325 break;
326 }
327 (void) spl0();
328 return (error);
329}
330
331#ifndef _SYS_SYSPROTO_H_
332struct sigpending_args {
333 int dummy;
334};
335#endif
336/* ARGSUSED */
337int
338sigpending(p, uap)
339 struct proc *p;
340 struct sigpending_args *uap;
341{
342
343 p->p_retval[0] = p->p_siglist;
344 return (0);
345}
346
347#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
348/*
349 * Generalized interface signal handler, 4.3-compatible.
350 */
351#ifndef _SYS_SYSPROTO_H_
352struct osigvec_args {
353 int signum;
354 struct sigvec *nsv;
355 struct sigvec *osv;
356};
357#endif
358/* ARGSUSED */
359int
360osigvec(p, uap)
361 struct proc *p;
362 register struct osigvec_args *uap;
363{
364 struct sigvec vec;
365 register struct sigacts *ps = p->p_sigacts;
366 register struct sigvec *sv;
367 register int signum;
368 int bit, error;
369
370 signum = uap->signum;
371 if (signum <= 0 || signum >= NSIG)
372 return (EINVAL);
373 sv = &vec;
374 if (uap->osv) {
375 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
376 sv->sv_mask = ps->ps_catchmask[signum];
377 bit = sigmask(signum);
378 sv->sv_flags = 0;
379 if ((ps->ps_sigonstack & bit) != 0)
380 sv->sv_flags |= SV_ONSTACK;
381 if ((ps->ps_sigintr & bit) != 0)
382 sv->sv_flags |= SV_INTERRUPT;
383 if ((ps->ps_sigreset & bit) != 0)
384 sv->sv_flags |= SV_RESETHAND;
385 if ((ps->ps_signodefer & bit) != 0)
386 sv->sv_flags |= SV_NODEFER;
387#ifndef COMPAT_SUNOS
388 if (signum == SIGCHLD && p->p_procsig->ps_flag & P_NOCLDSTOP)
389 sv->sv_flags |= SV_NOCLDSTOP;
390#endif
391 if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
392 sizeof (vec))))
393 return (error);
394 }
395 if (uap->nsv) {
396 if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
397 sizeof (vec))))
398 return (error);
399 if ((signum == SIGKILL || signum == SIGSTOP) &&
400 sv->sv_handler != SIG_DFL)
401 return (EINVAL);
402#ifdef COMPAT_SUNOS
403 sv->sv_flags |= SA_USERTRAMP;
404#endif
405 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
406 setsigvec(p, signum, (struct sigaction *)sv);
407 }
408 return (0);
409}
410
411#ifndef _SYS_SYSPROTO_H_
412struct osigblock_args {
413 int mask;
414};
415#endif
416int
417osigblock(p, uap)
418 register struct proc *p;
419 struct osigblock_args *uap;
420{
421
422 (void) splhigh();
423 p->p_retval[0] = p->p_sigmask;
424 p->p_sigmask |= uap->mask &~ sigcantmask;
425 (void) spl0();
426 return (0);
427}
428
429#ifndef _SYS_SYSPROTO_H_
430struct osigsetmask_args {
431 int mask;
432};
433#endif
434int
435osigsetmask(p, uap)
436 struct proc *p;
437 struct osigsetmask_args *uap;
438{
439
440 (void) splhigh();
441 p->p_retval[0] = p->p_sigmask;
442 p->p_sigmask = uap->mask &~ sigcantmask;
443 (void) spl0();
444 return (0);
445}
446#endif /* COMPAT_43 || COMPAT_SUNOS */
447
448/*
449 * Suspend process until signal, providing mask to be set
450 * in the meantime. Note nonstandard calling convention:
451 * libc stub passes mask, not pointer, to save a copyin.
452 */
453#ifndef _SYS_SYSPROTO_H_
454struct sigsuspend_args {
455 sigset_t mask;
456};
457#endif
458/* ARGSUSED */
459int
460sigsuspend(p, uap)
461 register struct proc *p;
462 struct sigsuspend_args *uap;
463{
464 register struct sigacts *ps = p->p_sigacts;
465
466 /*
467 * When returning from sigpause, we want
468 * the old mask to be restored after the
469 * signal handler has finished. Thus, we
470 * save it here and mark the sigacts structure
471 * to indicate this.
472 */
473 p->p_oldsigmask = p->p_sigmask;
474 p->p_sigmask = uap->mask &~ sigcantmask;
475 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
476 /* void */;
477 /* always return EINTR rather than ERESTART... */
478 return (EINTR);
479}
480
481#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
482#ifndef _SYS_SYSPROTO_H_
483struct osigstack_args {
484 struct sigstack *nss;
485 struct sigstack *oss;
486};
487#endif
488/* ARGSUSED */
489int
490osigstack(p, uap)
491 struct proc *p;
492 register struct osigstack_args *uap;
493{
494 struct sigstack ss;
495 struct sigacts *psp;
496 int error = 0;
497
498 psp = p->p_sigacts;
499 ss.ss_sp = psp->ps_sigstk.ss_sp;
500 ss.ss_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
501 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
502 sizeof (struct sigstack))))
503 return (error);
504 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
505 sizeof (ss))) == 0) {
506 psp->ps_sigstk.ss_sp = ss.ss_sp;
507 psp->ps_sigstk.ss_size = 0;
508 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
509 psp->ps_flags |= SAS_ALTSTACK;
510 }
511 return (error);
512}
513#endif /* COMPAT_43 || COMPAT_SUNOS */
514
515#ifndef _SYS_SYSPROTO_H_
516struct sigaltstack_args {
517 struct sigaltstack *nss;
518 struct sigaltstack *oss;
519};
520#endif
521/* ARGSUSED */
522int
523sigaltstack(p, uap)
524 struct proc *p;
525 register struct sigaltstack_args *uap;
526{
527 struct sigacts *psp;
528 struct sigaltstack ss;
529 int error;
530
531 psp = p->p_sigacts;
532 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
533 psp->ps_sigstk.ss_flags |= SS_DISABLE;
534 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
535 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
536 return (error);
537 if (uap->nss == 0)
538 return (0);
539 if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
540 return (error);
541 if (ss.ss_flags & SS_DISABLE) {
542 if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
543 return (EINVAL);
544 psp->ps_flags &= ~SAS_ALTSTACK;
545 psp->ps_sigstk.ss_flags = ss.ss_flags;
546 return (0);
547 }
548 if (ss.ss_size < MINSIGSTKSZ)
549 return (ENOMEM);
550 psp->ps_flags |= SAS_ALTSTACK;
551 psp->ps_sigstk= ss;
552 return (0);
553}
554
555/*
556 * Common code for kill process group/broadcast kill.
557 * cp is calling process.
558 */
559int
560killpg1(cp, signum, pgid, all)
561 register struct proc *cp;
562 int signum, pgid, all;
563{
564 register struct proc *p;
565 register struct pcred *pc = cp->p_cred;
566 struct pgrp *pgrp;
567 int nfound = 0;
568
569 if (all)
570 /*
571 * broadcast
572 */
573 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
574 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
575 p == cp || !CANSIGNAL(cp, pc, p, signum))
576 continue;
577 nfound++;
578 if (signum)
579 psignal(p, signum);
580 }
581 else {
582 if (pgid == 0)
583 /*
584 * zero pgid means send to my process group.
585 */
586 pgrp = cp->p_pgrp;
587 else {
588 pgrp = pgfind(pgid);
589 if (pgrp == NULL)
590 return (ESRCH);
591 }
592 for (p = pgrp->pg_members.lh_first; p != 0;
593 p = p->p_pglist.le_next) {
594 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
595 p->p_stat == SZOMB ||
596 !CANSIGNAL(cp, pc, p, signum))
597 continue;
598 nfound++;
599 if (signum)
600 psignal(p, signum);
601 }
602 }
603 return (nfound ? 0 : ESRCH);
604}
605
606#ifndef _SYS_SYSPROTO_H_
607struct kill_args {
608 int pid;
609 int signum;
610};
611#endif
612/* ARGSUSED */
613int
614kill(cp, uap)
615 register struct proc *cp;
616 register struct kill_args *uap;
617{
618 register struct proc *p;
619 register struct pcred *pc = cp->p_cred;
620
621 if ((u_int)uap->signum >= NSIG)
622 return (EINVAL);
623 if (uap->pid > 0) {
624 /* kill single process */
625 if ((p = pfind(uap->pid)) == NULL)
626 return (ESRCH);
627 if (!CANSIGNAL(cp, pc, p, uap->signum))
628 return (EPERM);
629 if (uap->signum)
630 psignal(p, uap->signum);
631 return (0);
632 }
633 switch (uap->pid) {
634 case -1: /* broadcast signal */
635 return (killpg1(cp, uap->signum, 0, 1));
636 case 0: /* signal own process group */
637 return (killpg1(cp, uap->signum, 0, 0));
638 default: /* negative explicit process group */
639 return (killpg1(cp, uap->signum, -uap->pid, 0));
640 }
641 /* NOTREACHED */
642}
643
644#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
645#ifndef _SYS_SYSPROTO_H_
646struct okillpg_args {
647 int pgid;
648 int signum;
649};
650#endif
651/* ARGSUSED */
652int
653okillpg(p, uap)
654 struct proc *p;
655 register struct okillpg_args *uap;
656{
657
658 if ((u_int)uap->signum >= NSIG)
659 return (EINVAL);
660 return (killpg1(p, uap->signum, uap->pgid, 0));
661}
662#endif /* COMPAT_43 || COMPAT_SUNOS */
663
664/*
665 * Send a signal to a process group.
666 */
667void
668gsignal(pgid, signum)
669 int pgid, signum;
670{
671 struct pgrp *pgrp;
672
673 if (pgid && (pgrp = pgfind(pgid)))
674 pgsignal(pgrp, signum, 0);
675}
676
677/*
678 * Send a signal to a process group. If checktty is 1,
679 * limit to members which have a controlling terminal.
680 */
681void
682pgsignal(pgrp, signum, checkctty)
683 struct pgrp *pgrp;
684 int signum, checkctty;
685{
686 register struct proc *p;
687
688 if (pgrp)
689 for (p = pgrp->pg_members.lh_first; p != 0;
690 p = p->p_pglist.le_next)
691 if (checkctty == 0 || p->p_flag & P_CONTROLT)
692 psignal(p, signum);
693}
694
695/*
696 * Send a signal caused by a trap to the current process.
697 * If it will be caught immediately, deliver it with correct code.
698 * Otherwise, post it normally.
699 */
700void
701trapsignal(p, signum, code)
702 struct proc *p;
703 register int signum;
704 u_long code;
705{
706 register struct sigacts *ps = p->p_sigacts;
707 int mask;
708
709 mask = sigmask(signum);
710 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
711 (p->p_sigmask & mask) == 0) {
712 p->p_stats->p_ru.ru_nsignals++;
713#ifdef KTRACE
714 if (KTRPOINT(p, KTR_PSIG))
715 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
716 p->p_sigmask, code);
717#endif
718 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[signum], signum,
719 p->p_sigmask, code);
720 p->p_sigmask |= ps->ps_catchmask[signum] |
721 (mask & ~ps->ps_signodefer);
722 if ((ps->ps_sigreset & mask) != 0) {
723 /*
724 * See setsigvec() for origin of this code.
725 */
726 p->p_sigcatch &= ~mask;
727 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
728 p->p_sigignore |= mask;
729 ps->ps_sigact[signum] = SIG_DFL;
730 }
731 } else {
732 p->p_code = code; /* XXX for core dump/debugger */
733 p->p_sig = signum; /* XXX to verify code */
734 psignal(p, signum);
735 }
736}
737
738/*
739 * Send the signal to the process. If the signal has an action, the action
740 * is usually performed by the target process rather than the caller; we add
741 * the signal to the set of pending signals for the process.
742 *
743 * Exceptions:
744 * o When a stop signal is sent to a sleeping process that takes the
745 * default action, the process is stopped without awakening it.
746 * o SIGCONT restarts stopped processes (or puts them back to sleep)
747 * regardless of the signal action (eg, blocked or ignored).
748 *
749 * Other ignored signals are discarded immediately.
750 */
751void
752psignal(p, signum)
753 register struct proc *p;
754 register int signum;
755{
756 register int s, prop;
757 register sig_t action;
758 int mask;
759
760 if ((u_int)signum >= NSIG || signum == 0) {
761 printf("psignal: signum %d\n", signum);
762 panic("psignal signal number");
763 }
764 mask = sigmask(signum);
765 prop = sigprop[signum];
766
767 /*
768 * If proc is traced, always give parent a chance;
769 * if signal event is tracked by procfs, give *that*
770 * a chance, as well.
771 */
772 if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
773 action = SIG_DFL;
774 else {
775 /*
776 * If the signal is being ignored,
777 * then we forget about it immediately.
778 * (Note: we don't set SIGCONT in p_sigignore,
779 * and if it is set to SIG_IGN,
780 * action will be SIG_DFL here.)
781 */
782 if ((p->p_sigignore & mask) || (p->p_flag & P_WEXIT))
783 return;
784 if (p->p_sigmask & mask)
785 action = SIG_HOLD;
786 else if (p->p_sigcatch & mask)
787 action = SIG_CATCH;
788 else
789 action = SIG_DFL;
790 }
791
792 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
793 (p->p_flag & P_TRACED) == 0)
794 p->p_nice = NZERO;
795
796 if (prop & SA_CONT)
797 p->p_siglist &= ~stopsigmask;
798
799 if (prop & SA_STOP) {
800 /*
801 * If sending a tty stop signal to a member of an orphaned
802 * process group, discard the signal here if the action
803 * is default; don't stop the process below if sleeping,
804 * and don't clear any pending SIGCONT.
805 */
806 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
807 action == SIG_DFL)
808 return;
809 p->p_siglist &= ~contsigmask;
810 }
811 p->p_siglist |= mask;
812
813 /*
814 * Defer further processing for signals which are held,
815 * except that stopped processes must be continued by SIGCONT.
816 */
817 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
818 return;
819 s = splhigh();
820 switch (p->p_stat) {
821
822 case SSLEEP:
823 /*
824 * If process is sleeping uninterruptibly
825 * we can't interrupt the sleep... the signal will
826 * be noticed when the process returns through
827 * trap() or syscall().
828 */
829 if ((p->p_flag & P_SINTR) == 0)
830 goto out;
831 /*
832 * Process is sleeping and traced... make it runnable
833 * so it can discover the signal in issignal() and stop
834 * for the parent.
835 */
836 if (p->p_flag & P_TRACED)
837 goto run;
838 /*
839 * If SIGCONT is default (or ignored) and process is
840 * asleep, we are finished; the process should not
841 * be awakened.
842 */
843 if ((prop & SA_CONT) && action == SIG_DFL) {
844 p->p_siglist &= ~mask;
845 goto out;
846 }
847 /*
848 * When a sleeping process receives a stop
849 * signal, process immediately if possible.
850 * All other (caught or default) signals
851 * cause the process to run.
852 */
853 if (prop & SA_STOP) {
854 if (action != SIG_DFL)
855 goto runfast;
856 /*
857 * If a child holding parent blocked,
858 * stopping could cause deadlock.
859 */
860 if (p->p_flag & P_PPWAIT)
861 goto out;
862 p->p_siglist &= ~mask;
863 p->p_xstat = signum;
864 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
865 psignal(p->p_pptr, SIGCHLD);
866 stop(p);
867 goto out;
868 } else
869 goto runfast;
870 /*NOTREACHED*/
871
872 case SSTOP:
873 /*
874 * If traced process is already stopped,
875 * then no further action is necessary.
876 */
877 if (p->p_flag & P_TRACED)
878 goto out;
879
880 /*
881 * Kill signal always sets processes running.
882 */
883 if (signum == SIGKILL)
884 goto runfast;
885
886 if (prop & SA_CONT) {
887 /*
888 * If SIGCONT is default (or ignored), we continue the
889 * process but don't leave the signal in p_siglist, as
890 * it has no further action. If SIGCONT is held, we
891 * continue the process and leave the signal in
892 * p_siglist. If the process catches SIGCONT, let it
893 * handle the signal itself. If it isn't waiting on
894 * an event, then it goes back to run state.
895 * Otherwise, process goes back to sleep state.
896 */
897 if (action == SIG_DFL)
898 p->p_siglist &= ~mask;
899 if (action == SIG_CATCH)
900 goto runfast;
901 if (p->p_wchan == 0)
902 goto run;
903 p->p_stat = SSLEEP;
904 goto out;
905 }
906
907 if (prop & SA_STOP) {
908 /*
909 * Already stopped, don't need to stop again.
910 * (If we did the shell could get confused.)
911 */
912 p->p_siglist &= ~mask; /* take it away */
913 goto out;
914 }
915
916 /*
917 * If process is sleeping interruptibly, then simulate a
918 * wakeup so that when it is continued, it will be made
919 * runnable and can look at the signal. But don't make
920 * the process runnable, leave it stopped.
921 */
922 if (p->p_wchan && p->p_flag & P_SINTR)
923 unsleep(p);
924 goto out;
925
926 default:
927 /*
928 * SRUN, SIDL, SZOMB do nothing with the signal,
929 * other than kicking ourselves if we are running.
930 * It will either never be noticed, or noticed very soon.
931 */
932 if (p == curproc)
933 signotify(p);
934#ifdef SMP
935 else if (p->p_stat == SRUN)
936 forward_signal(p);
937#endif
938 goto out;
939 }
940 /*NOTREACHED*/
941
942runfast:
943 /*
944 * Raise priority to at least PUSER.
945 */
946 if (p->p_priority > PUSER)
947 p->p_priority = PUSER;
948run:
949 setrunnable(p);
950out:
951 splx(s);
952}
953
954/*
955 * If the current process has received a signal (should be caught or cause
956 * termination, should interrupt current syscall), return the signal number.
957 * Stop signals with default action are processed immediately, then cleared;
958 * they aren't returned. This is checked after each entry to the system for
959 * a syscall or trap (though this can usually be done without calling issignal
960 * by checking the pending signal masks in the CURSIG macro.) The normal call
961 * sequence is
962 *
963 * while (signum = CURSIG(curproc))
964 * postsig(signum);
965 */
966int
967issignal(p)
968 register struct proc *p;
969{
970 register int signum, mask, prop;
971
972 for (;;) {
973 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
974
975 mask = p->p_siglist & ~p->p_sigmask;
976 if (p->p_flag & P_PPWAIT)
977 mask &= ~stopsigmask;
978 if (mask == 0) /* no signal to send */
979 return (0);
980 signum = ffs((long)mask);
981 mask = sigmask(signum);
982 prop = sigprop[signum];
983
984 STOPEVENT(p, S_SIG, signum);
985
986 /*
987 * We should see pending but ignored signals
988 * only if P_TRACED was on when they were posted.
989 */
990 if ((mask & p->p_sigignore) && (traced == 0)) {
991 p->p_siglist &= ~mask;
992 continue;
993 }
994 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
995 /*
996 * If traced, always stop, and stay
997 * stopped until released by the parent.
998 */
999 p->p_xstat = signum;
1000 psignal(p->p_pptr, SIGCHLD);
1001 do {
1002 stop(p);
1003 mi_switch();
1004 } while (!trace_req(p)
1005 && p->p_flag & P_TRACED);
1006
1007 /*
1008 * If the traced bit got turned off, go back up
1009 * to the top to rescan signals. This ensures
1010 * that p_sig* and ps_sigact are consistent.
1011 */
1012 if ((p->p_flag & P_TRACED) == 0)
1013 continue;
1014
1015 /*
1016 * If parent wants us to take the signal,
1017 * then it will leave it in p->p_xstat;
1018 * otherwise we just look for signals again.
1019 */
1020 p->p_siglist &= ~mask; /* clear the old signal */
1021 signum = p->p_xstat;
1022 if (signum == 0)
1023 continue;
1024
1025 /*
1026 * Put the new signal into p_siglist. If the
1027 * signal is being masked, look for other signals.
1028 */
1029 mask = sigmask(signum);
1030 p->p_siglist |= mask;
1031 if (p->p_sigmask & mask)
1032 continue;
1033 }
1034
1035 /*
1036 * Decide whether the signal should be returned.
1037 * Return the signal's number, or fall through
1038 * to clear it from the pending mask.
1039 */
1040 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[signum]) {
1041
1042 case (int)SIG_DFL:
1043 /*
1044 * Don't take default actions on system processes.
1045 */
1046 if (p->p_pid <= 1) {
1047#ifdef DIAGNOSTIC
1048 /*
1049 * Are you sure you want to ignore SIGSEGV
1050 * in init? XXX
1051 */
1052 printf("Process (pid %lu) got signal %d\n",
1053 (u_long)p->p_pid, signum);
1054#endif
1055 break; /* == ignore */
1056 }
1057 /*
1058 * If there is a pending stop signal to process
1059 * with default action, stop here,
1060 * then clear the signal. However,
1061 * if process is member of an orphaned
1062 * process group, ignore tty stop signals.
1063 */
1064 if (prop & SA_STOP) {
1065 if (p->p_flag & P_TRACED ||
1066 (p->p_pgrp->pg_jobc == 0 &&
1067 prop & SA_TTYSTOP))
1068 break; /* == ignore */
1069 p->p_xstat = signum;
1070 stop(p);
1071 if ((p->p_pptr->p_procsig->ps_flag & P_NOCLDSTOP) == 0)
1072 psignal(p->p_pptr, SIGCHLD);
1073 mi_switch();
1074 break;
1075 } else if (prop & SA_IGNORE) {
1076 /*
1077 * Except for SIGCONT, shouldn't get here.
1078 * Default action is to ignore; drop it.
1079 */
1080 break; /* == ignore */
1081 } else
1082 return (signum);
1083 /*NOTREACHED*/
1084
1085 case (int)SIG_IGN:
1086 /*
1087 * Masking above should prevent us ever trying
1088 * to take action on an ignored signal other
1089 * than SIGCONT, unless process is traced.
1090 */
1091 if ((prop & SA_CONT) == 0 &&
1092 (p->p_flag & P_TRACED) == 0)
1093 printf("issignal\n");
1094 break; /* == ignore */
1095
1096 default:
1097 /*
1098 * This signal has an action, let
1099 * postsig() process it.
1100 */
1101 return (signum);
1102 }
1103 p->p_siglist &= ~mask; /* take the signal! */
1104 }
1105 /* NOTREACHED */
1106}
1107
1108/*
1109 * Put the argument process into the stopped state and notify the parent
1110 * via wakeup. Signals are handled elsewhere. The process must not be
1111 * on the run queue.
1112 */
1113void
1114stop(p)
1115 register struct proc *p;
1116{
1117
1118 p->p_stat = SSTOP;
1119 p->p_flag &= ~P_WAITED;
1120 wakeup((caddr_t)p->p_pptr);
1121}
1122
1123/*
1124 * Take the action for the specified signal
1125 * from the current set of pending signals.
1126 */
1127void
1128postsig(signum)
1129 register int signum;
1130{
1131 register struct proc *p = curproc;
1132 register struct sigacts *ps = p->p_sigacts;
1133 register sig_t action;
1134 int code, mask, returnmask;
1135
1136 KASSERT(signum != 0, ("postsig"));
1137
1138 mask = sigmask(signum);
1139 p->p_siglist &= ~mask;
1140 action = ps->ps_sigact[signum];
1141#ifdef KTRACE
1142 if (KTRPOINT(p, KTR_PSIG))
1143 ktrpsig(p->p_tracep,
1144 signum, action, p->p_oldsigmask ?
1145 p->p_oldsigmask : p->p_sigmask, 0);
1146#endif
1147 STOPEVENT(p, S_SIG, signum);
1148
1149 if (action == SIG_DFL) {
1150 /*
1151 * Default action, where the default is to kill
1152 * the process. (Other cases were ignored above.)
1153 */
1154 sigexit(p, signum);
1155 /* NOTREACHED */
1156 } else {
1157 /*
1158 * If we get here, the signal must be caught.
1159 */
1160 KASSERT(action != SIG_IGN && (p->p_sigmask & mask) == 0,
1161 ("postsig action"));
1162 /*
1163 * Set the new mask value and also defer further
1164 * occurences of this signal.
1165 *
1166 * Special case: user has done a sigpause. Here the
1167 * current mask is not of interest, but rather the
1168 * mask from before the sigpause is what we want
1169 * restored after the signal processing is completed.
1170 */
1171 (void) splhigh();
1172 if (p->p_oldsigmask) {
1173 returnmask = p->p_oldsigmask;
1174 p->p_oldsigmask = 0;
1175 } else
1176 returnmask = p->p_sigmask;
1177 p->p_sigmask |= ps->ps_catchmask[signum] |
1178 (mask & ~ps->ps_signodefer);
1179 if ((ps->ps_sigreset & mask) != 0) {
1180 /*
1181 * See setsigvec() for origin of this code.
1182 */
1183 p->p_sigcatch &= ~mask;
1184 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1185 p->p_sigignore |= mask;
1186 ps->ps_sigact[signum] = SIG_DFL;
1187 }
1188 (void) spl0();
1189 p->p_stats->p_ru.ru_nsignals++;
1190 if (p->p_sig != signum) {
1191 code = 0;
1192 } else {
1193 code = p->p_code;
1194 p->p_code = 0;
1195 p->p_sig = 0;
1196 }
1197 (*p->p_sysent->sv_sendsig)(action, signum, returnmask, code);
1198 }
1199}
1200
1201/*
1202 * Kill the current process for stated reason.
1203 */
1204void
1205killproc(p, why)
1206 struct proc *p;
1207 char *why;
1208{
1209 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1210 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1211 psignal(p, SIGKILL);
1212}
1213
1214/*
1215 * Force the current process to exit with the specified signal, dumping core
1216 * if appropriate. We bypass the normal tests for masked and caught signals,
1217 * allowing unrecoverable failures to terminate the process without changing
1218 * signal state. Mark the accounting record with the signal termination.
1219 * If dumping core, save the signal number for the debugger. Calls exit and
1220 * does not return.
1221 */
1222void
1223sigexit(p, signum)
1224 register struct proc *p;
1225 int signum;
1226{
1227
1228 p->p_acflag |= AXSIG;
1229 if (sigprop[signum] & SA_CORE) {
1230 p->p_sig = signum;
1231 /*
1232 * Log signals which would cause core dumps
1233 * (Log as LOG_INFO to appease those who don't want
1234 * these messages.)
1235 * XXX : Todo, as well as euid, write out ruid too
1236 */
1237 if (p->p_sysent->sv_coredump != NULL &&
1238 (*p->p_sysent->sv_coredump)(p) == 0)
1239 signum |= WCOREFLAG;
1240 if (kern_logsigexit)
1241 log(LOG_INFO,
1242 "pid %d (%s), uid %d: exited on signal %d%s\n",
1243 p->p_pid, p->p_comm,
1244 p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1245 signum &~ WCOREFLAG,
1246 signum & WCOREFLAG ? " (core dumped)" : "");
1247 }
1248 exit1(p, W_EXITCODE(0, signum));
1249 /* NOTREACHED */
1250}
1251
1252static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1253SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1254 sizeof(corefilename), "process corefile name format string");
1255
1256/*
1257 * expand_name(name, uid, pid)
1258 * Expand the name described in corefilename, using name, uid, and pid.
1259 * corefilename is a printf-like string, with three format specifiers:
1260 * %N name of process ("name")
1261 * %P process id (pid)
1262 * %U user id (uid)
1263 * For example, "%N.core" is the default; they can be disabled completely
1264 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1265 * This is controlled by the sysctl variable kern.corefile (see above).
1266 */
1267
1268char *
1269expand_name(name, uid, pid)
1270const char *name; int uid; int pid; {
1271 char *temp;
1272 char buf[11]; /* Buffer for pid/uid -- max 4B */
1273 int i, n;
1274 char *format = corefilename;
1275
1276 temp = malloc(MAXPATHLEN + 3, M_TEMP, M_NOWAIT);
1277 if (temp == NULL)
1278 return NULL;
1279 bzero(temp, MAXPATHLEN+3);
1280 for (i = 0, n = 0; i < MAXPATHLEN && format[i]; i++) {
1281 int l;
1282 switch (format[i]) {
1283 case '%': /* Format character */
1284 i++;
1285 switch (format[i]) {
1286 case '%':
1287 temp[n++] = '%';
1288 break;
1289 case 'N': /* process name */
1290 l = strlen(name);
1291 if ((n + l) > MAXPATHLEN) {
1292 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1293 pid, name, uid, temp, name);
1294 free(temp, M_TEMP);
1295 return NULL;
1296 }
1297 memcpy(temp+n, name, l);
1298 n += l;
1299 break;
1300 case 'P': /* process id */
1301 sprintf(buf, "%u", pid);
1302 l = strlen(buf);
1303 if ((n + l) > MAXPATHLEN) {
1304 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1305 pid, name, uid, temp, name);
1306 free(temp, M_TEMP);
1307 return NULL;
1308 }
1309 memcpy(temp+n, buf, l);
1310 n += l;
1311 break;
1312 case 'U': /* user id */
1313 sprintf(buf, "%u", uid);
1314 l = strlen(buf);
1315 if ((n + l) > MAXPATHLEN) {
1316 log(LOG_ERR, "pid %d (%s), uid (%d): Path `%s%s' is too long\n",
1317 pid, name, uid, temp, name);
1318 free(temp, M_TEMP);
1319 return NULL;
1320 }
1321 memcpy(temp+n, buf, l);
1322 n += l;
1323 break;
1324 default:
1325 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1326 }
1327 break;
1328 default:
1329 temp[n++] = format[i];
1330 }
1331 }
1332 return temp;
1333}
1334
1335/*
1336 * Nonexistent system call-- signal process (may want to handle it).
1337 * Flag error in case process won't see signal immediately (blocked or ignored).
1338 */
1339#ifndef _SYS_SYSPROTO_H_
1340struct nosys_args {
1341 int dummy;
1342};
1343#endif
1344/* ARGSUSED */
1345int
1346nosys(p, args)
1347 struct proc *p;
1348 struct nosys_args *args;
1349{
1350
1351 psignal(p, SIGSYS);
1352 return (EINVAL);
1353}
1354
1355/*
1356 * Send a signal to a SIGIO or SIGURG to a process or process group using
1357 * stored credentials rather than those of the current process.
1358 */
1359void
1360pgsigio(sigio, signum, checkctty)
1361 struct sigio *sigio;
1362 int signum, checkctty;
1363{
1364 if (sigio == NULL)
1365 return;
1366
1367 if (sigio->sio_pgid > 0) {
1368 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1369 sigio->sio_proc))
1370 psignal(sigio->sio_proc, signum);
1371 } else if (sigio->sio_pgid < 0) {
1372 struct proc *p;
1373
1374 for (p = sigio->sio_pgrp->pg_members.lh_first; p != NULL;
1375 p = p->p_pglist.le_next)
1376 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1377 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1378 psignal(p, signum);
1379 }
1380}