added my responsibility for the `cpm' port
[unix-history] / sys / kern / kern_sig.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
600f7f07 33 * from: @(#)kern_sig.c 7.35 (Berkeley) 6/28/91
26560360 34 * $Id: kern_sig.c,v 1.11 1994/05/05 18:44:13 csgr Exp $
15637ed4
RG
35 */
36
37#define SIGPROP /* include signal properties table */
38#include "param.h"
39#include "signalvar.h"
40#include "resourcevar.h"
41#include "namei.h"
42#include "vnode.h"
ce723be2
CR
43#include "mount.h"
44#include "filedesc.h"
15637ed4 45#include "proc.h"
1523a604 46#include "ucred.h"
15637ed4
RG
47#include "systm.h"
48#include "timeb.h"
49#include "times.h"
50#include "buf.h"
51#include "acct.h"
52#include "file.h"
53#include "kernel.h"
54#include "wait.h"
55#include "ktrace.h"
1523a604 56#include "syslog.h"
15637ed4
RG
57
58#include "machine/cpu.h"
59
60#include "vm/vm.h"
61#include "kinfo_proc.h"
62#include "user.h" /* for coredump */
63
4c45483e
GW
64static void setsigvec(struct proc *, int, struct sigaction *);
65static void stop(struct proc *);
66static void sigexit(struct proc *, int);
fde1aeb2 67static int killpg1(struct proc *, int, int, int);
4c45483e 68
15637ed4
RG
69/*
70 * Can process p, with pcred pc, send the signal signo to process q?
71 */
72#define CANSIGNAL(p, pc, q, signo) \
73 ((pc)->pc_ucred->cr_uid == 0 || \
74 (pc)->p_ruid == (q)->p_cred->p_ruid || \
75 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
76 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
77 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
78 ((signo) == SIGCONT && (q)->p_session == (p)->p_session))
79
3c7eb27c
DG
80struct sigaction_args {
81 int signo;
82 struct sigaction *nsa;
83 struct sigaction *osa;
84};
85
15637ed4 86/* ARGSUSED */
4c45483e 87int
15637ed4
RG
88sigaction(p, uap, retval)
89 struct proc *p;
3c7eb27c 90 register struct sigaction_args *uap;
15637ed4
RG
91 int *retval;
92{
93 struct sigaction vec;
94 register struct sigaction *sa;
95 register struct sigacts *ps = p->p_sigacts;
96 register int sig;
97 int bit, error;
98
99 sig = uap->signo;
100 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
101 return (EINVAL);
102 sa = &vec;
103 if (uap->osa) {
104 sa->sa_handler = ps->ps_sigact[sig];
105 sa->sa_mask = ps->ps_catchmask[sig];
106 bit = sigmask(sig);
107 sa->sa_flags = 0;
108 if ((ps->ps_sigonstack & bit) != 0)
109 sa->sa_flags |= SA_ONSTACK;
110 if ((ps->ps_sigintr & bit) == 0)
111 sa->sa_flags |= SA_RESTART;
112 if (p->p_flag & SNOCLDSTOP)
113 sa->sa_flags |= SA_NOCLDSTOP;
114 if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
115 sizeof (vec)))
116 return (error);
117 }
118 if (uap->nsa) {
119 if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
120 sizeof (vec)))
121 return (error);
122 setsigvec(p, sig, sa);
123 }
124 return (0);
125}
126
4c45483e 127void
15637ed4
RG
128setsigvec(p, sig, sa)
129 register struct proc *p;
130 int sig;
131 register struct sigaction *sa;
132{
133 register struct sigacts *ps = p->p_sigacts;
134 register int bit;
135
136 bit = sigmask(sig);
137 /*
138 * Change setting atomically.
139 */
140 (void) splhigh();
141 ps->ps_sigact[sig] = sa->sa_handler;
142 ps->ps_catchmask[sig] = sa->sa_mask &~ sigcantmask;
143 if ((sa->sa_flags & SA_RESTART) == 0)
144 ps->ps_sigintr |= bit;
145 else
146 ps->ps_sigintr &= ~bit;
147 if (sa->sa_flags & SA_ONSTACK)
148 ps->ps_sigonstack |= bit;
149 else
150 ps->ps_sigonstack &= ~bit;
151 if (sig == SIGCHLD) {
152 if (sa->sa_flags & SA_NOCLDSTOP)
153 p->p_flag |= SNOCLDSTOP;
154 else
155 p->p_flag &= ~SNOCLDSTOP;
156 }
157 /*
158 * Set bit in p_sigignore for signals that are set to SIG_IGN,
159 * and for signals set to SIG_DFL where the default is to ignore.
160 * However, don't put SIGCONT in p_sigignore,
161 * as we have to restart the process.
162 */
163 if (sa->sa_handler == SIG_IGN ||
164 (sigprop[sig] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
165 p->p_sig &= ~bit; /* never to be seen again */
166 if (sig != SIGCONT)
167 p->p_sigignore |= bit; /* easier in psignal */
168 p->p_sigcatch &= ~bit;
169 } else {
170 p->p_sigignore &= ~bit;
171 if (sa->sa_handler == SIG_DFL)
172 p->p_sigcatch &= ~bit;
173 else
174 p->p_sigcatch |= bit;
175 }
176 (void) spl0();
177}
178
179/*
180 * Initialize signal state for process 0;
181 * set to ignore signals that are ignored by default.
182 */
183void
184siginit(p)
185 struct proc *p;
186{
187 register int i;
188
189 for (i = 0; i < NSIG; i++)
190 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
191 p->p_sigignore |= sigmask(i);
192}
193
194/*
195 * Reset signals for an exec of the specified process.
196 */
197void
198execsigs(p)
199 register struct proc *p;
200{
201 register struct sigacts *ps = p->p_sigacts;
202 register int nc, mask;
203
204 /*
205 * Reset caught signals. Held signals remain held
206 * through p_sigmask (unless they were caught,
207 * and are now ignored by default).
208 */
209 while (p->p_sigcatch) {
210 nc = ffs((long)p->p_sigcatch);
211 mask = sigmask(nc);
212 p->p_sigcatch &= ~mask;
213 if (sigprop[nc] & SA_IGNORE) {
214 if (nc != SIGCONT)
215 p->p_sigignore |= mask;
216 p->p_sig &= ~mask;
217 }
218 ps->ps_sigact[nc] = SIG_DFL;
219 }
220 /*
221 * Reset stack state to the user stack.
222 * Clear set of signals caught on the signal stack.
223 */
224 ps->ps_onstack = 0;
225 ps->ps_sigsp = 0;
226 ps->ps_sigonstack = 0;
227}
228
229/*
230 * Manipulate signal mask.
231 * Note that we receive new mask, not pointer,
232 * and return old mask as return value;
233 * the library stub does the rest.
234 */
3c7eb27c
DG
235
236struct sigprocmask_args {
237 int how;
238 sigset_t mask;
239};
240
4c45483e 241int
15637ed4
RG
242sigprocmask(p, uap, retval)
243 register struct proc *p;
3c7eb27c 244 struct sigprocmask_args *uap;
15637ed4
RG
245 int *retval;
246{
247 int error = 0;
248
249 *retval = p->p_sigmask;
250 (void) splhigh();
251
252 switch (uap->how) {
253 case SIG_BLOCK:
254 p->p_sigmask |= uap->mask &~ sigcantmask;
255 break;
256
257 case SIG_UNBLOCK:
258 p->p_sigmask &= ~uap->mask;
259 break;
260
261 case SIG_SETMASK:
262 p->p_sigmask = uap->mask &~ sigcantmask;
263 break;
264
265 default:
266 error = EINVAL;
267 break;
268 }
269 (void) spl0();
270 return (error);
271}
272
273/* ARGSUSED */
4c45483e 274int
15637ed4
RG
275sigpending(p, uap, retval)
276 struct proc *p;
277 void *uap;
278 int *retval;
279{
280
281 *retval = p->p_sig;
282 return (0);
283}
284
285#ifdef COMPAT_43
286/*
287 * Generalized interface signal handler, 4.3-compatible.
288 */
3c7eb27c
DG
289
290struct osigvec_args {
291 int signo;
292 struct sigvec *nsv;
293 struct sigvec *osv;
294};
295
15637ed4 296/* ARGSUSED */
4c45483e 297int
15637ed4
RG
298osigvec(p, uap, retval)
299 struct proc *p;
3c7eb27c 300 register struct osigvec_args *uap;
15637ed4
RG
301 int *retval;
302{
303 struct sigvec vec;
304 register struct sigacts *ps = p->p_sigacts;
305 register struct sigvec *sv;
306 register int sig;
307 int bit, error;
308
309 sig = uap->signo;
310 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
311 return (EINVAL);
312 sv = &vec;
313 if (uap->osv) {
314 *(sig_t *)&sv->sv_handler = ps->ps_sigact[sig];
315 sv->sv_mask = ps->ps_catchmask[sig];
316 bit = sigmask(sig);
317 sv->sv_flags = 0;
318 if ((ps->ps_sigonstack & bit) != 0)
319 sv->sv_flags |= SV_ONSTACK;
320 if ((ps->ps_sigintr & bit) != 0)
321 sv->sv_flags |= SV_INTERRUPT;
322 if (p->p_flag & SNOCLDSTOP)
323 sv->sv_flags |= SA_NOCLDSTOP;
324 if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
325 sizeof (vec)))
326 return (error);
327 }
328 if (uap->nsv) {
329 if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
330 sizeof (vec)))
331 return (error);
332 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
333 setsigvec(p, sig, (struct sigaction *)sv);
334 }
335 return (0);
336}
337
3c7eb27c
DG
338struct osigblock_args {
339 int mask;
340};
341
4c45483e 342int
15637ed4
RG
343osigblock(p, uap, retval)
344 register struct proc *p;
3c7eb27c 345 struct osigblock_args *uap;
15637ed4
RG
346 int *retval;
347{
348
349 (void) splhigh();
350 *retval = p->p_sigmask;
351 p->p_sigmask |= uap->mask &~ sigcantmask;
352 (void) spl0();
353 return (0);
354}
355
3c7eb27c
DG
356struct osigsetmask_args {
357 int mask;
358};
359
4c45483e 360int
15637ed4
RG
361osigsetmask(p, uap, retval)
362 struct proc *p;
3c7eb27c 363 struct osigsetmask_args *uap;
15637ed4
RG
364 int *retval;
365{
366
367 (void) splhigh();
368 *retval = p->p_sigmask;
369 p->p_sigmask = uap->mask &~ sigcantmask;
370 (void) spl0();
371 return (0);
372}
373#endif
374
375/*
376 * Suspend process until signal, providing mask to be set
377 * in the meantime. Note nonstandard calling convention:
378 * libc stub passes mask, not pointer, to save a copyin.
379 */
3c7eb27c
DG
380
381struct sigsuspend_args {
382 sigset_t mask;
383};
384
15637ed4 385/* ARGSUSED */
4c45483e 386int
15637ed4
RG
387sigsuspend(p, uap, retval)
388 register struct proc *p;
3c7eb27c 389 struct sigsuspend_args *uap;
15637ed4
RG
390 int *retval;
391{
392 register struct sigacts *ps = p->p_sigacts;
393
394 /*
395 * When returning from sigpause, we want
396 * the old mask to be restored after the
397 * signal handler has finished. Thus, we
398 * save it here and mark the proc structure
399 * to indicate this (should be in sigacts).
400 */
401 ps->ps_oldmask = p->p_sigmask;
402 ps->ps_flags |= SA_OLDMASK;
403 p->p_sigmask = uap->mask &~ sigcantmask;
404 (void) tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0);
405 /* always return EINTR rather than ERESTART... */
406 return (EINTR);
407}
408
3c7eb27c
DG
409struct sigstack_args {
410 struct sigstack *nss;
411 struct sigstack *oss;
412};
413
15637ed4 414/* ARGSUSED */
4c45483e 415int
15637ed4
RG
416sigstack(p, uap, retval)
417 struct proc *p;
3c7eb27c 418 register struct sigstack_args *uap;
15637ed4
RG
419 int *retval;
420{
421 struct sigstack ss;
422 int error = 0;
423
424 if (uap->oss && (error = copyout((caddr_t)&p->p_sigacts->ps_sigstack,
425 (caddr_t)uap->oss, sizeof (struct sigstack))))
426 return (error);
427 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
428 sizeof (ss))) == 0)
429 p->p_sigacts->ps_sigstack = ss;
430 return (error);
431}
432
3c7eb27c
DG
433struct kill_args {
434 int pid;
435 int signo;
436};
437
15637ed4 438/* ARGSUSED */
4c45483e 439int
15637ed4
RG
440kill(cp, uap, retval)
441 register struct proc *cp;
3c7eb27c 442 register struct kill_args *uap;
15637ed4
RG
443 int *retval;
444{
445 register struct proc *p;
446 register struct pcred *pc = cp->p_cred;
447
448 if ((unsigned) uap->signo >= NSIG)
449 return (EINVAL);
450 if (uap->pid > 0) {
451 /* kill single process */
452 p = pfind(uap->pid);
453 if (p == 0)
454 return (ESRCH);
455 if (!CANSIGNAL(cp, pc, p, uap->signo))
456 return (EPERM);
457 if (uap->signo)
458 psignal(p, uap->signo);
459 return (0);
460 }
461 switch (uap->pid) {
462 case -1: /* broadcast signal */
463 return (killpg1(cp, uap->signo, 0, 1));
464 case 0: /* signal own process group */
465 return (killpg1(cp, uap->signo, 0, 0));
466 default: /* negative explicit process group */
467 return (killpg1(cp, uap->signo, -uap->pid, 0));
468 }
469 /* NOTREACHED */
470}
471
472#ifdef COMPAT_43
3c7eb27c
DG
473
474struct okillpg_args {
475 int pgid;
476 int signo;
477};
478
15637ed4 479/* ARGSUSED */
4c45483e 480int
15637ed4
RG
481okillpg(p, uap, retval)
482 struct proc *p;
3c7eb27c 483 register struct okillpg_args *uap;
15637ed4
RG
484 int *retval;
485{
486
487 if ((unsigned) uap->signo >= NSIG)
488 return (EINVAL);
489 return (killpg1(p, uap->signo, uap->pgid, 0));
490}
491#endif
492
493/*
494 * Common code for kill process group/broadcast kill.
495 * cp is calling process.
496 */
fde1aeb2 497static int
15637ed4
RG
498killpg1(cp, signo, pgid, all)
499 register struct proc *cp;
500 int signo, pgid, all;
501{
502 register struct proc *p;
503 register struct pcred *pc = cp->p_cred;
504 struct pgrp *pgrp;
505 int nfound = 0;
506
507 if (all)
508 /*
509 * broadcast
510 */
511 for (p = allproc; p != NULL; p = p->p_nxt) {
512 if (p->p_pid <= 1 || p->p_flag&SSYS ||
513 p == cp || !CANSIGNAL(cp, pc, p, signo))
514 continue;
515 nfound++;
516 if (signo)
517 psignal(p, signo);
518 }
519 else {
520 if (pgid == 0)
521 /*
522 * zero pgid means send to my process group.
523 */
524 pgrp = cp->p_pgrp;
525 else {
526 pgrp = pgfind(pgid);
527 if (pgrp == NULL)
528 return (ESRCH);
529 }
530 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
531 if (p->p_pid <= 1 || p->p_flag&SSYS ||
532 p->p_stat == SZOMB || !CANSIGNAL(cp, pc, p, signo))
533 continue;
534 nfound++;
535 if (signo)
536 psignal(p, signo);
537 }
538 }
539 return (nfound ? 0 : ESRCH);
540}
541
542/*
543 * Send the specified signal to
544 * all processes with 'pgid' as
545 * process group.
546 */
547void
548gsignal(pgid, sig)
549 int pgid, sig;
550{
551 struct pgrp *pgrp;
552
553 if (pgid && (pgrp = pgfind(pgid)))
554 pgsignal(pgrp, sig, 0);
555}
556
557/*
558 * Send sig to every member of a process group.
559 * If checktty is 1, limit to members which have a controlling
560 * terminal.
561 */
562void
563pgsignal(pgrp, sig, checkctty)
564 struct pgrp *pgrp;
565 int sig, checkctty;
566{
567 register struct proc *p;
568
569 if (pgrp)
570 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
571 if (checkctty == 0 || p->p_flag&SCTTY)
572 psignal(p, sig);
573}
574
575/*
576 * Send a signal caused by a trap to the current process.
577 * If it will be caught immediately, deliver it with correct code.
578 * Otherwise, post it normally.
579 */
580void
581trapsignal(p, sig, code)
582 struct proc *p;
583 register int sig;
584 unsigned code;
585{
586 register struct sigacts *ps = p->p_sigacts;
587 int mask;
588
589 mask = sigmask(sig);
590 if (p == curproc && (p->p_flag & STRC) == 0 &&
591 (p->p_sigcatch & mask) != 0 && (p->p_sigmask & mask) == 0) {
592 p->p_stats->p_ru.ru_nsignals++;
593#ifdef KTRACE
594 if (KTRPOINT(p, KTR_PSIG))
595 ktrpsig(p->p_tracep, sig, ps->ps_sigact[sig],
596 p->p_sigmask, code);
597#endif
598 sendsig(ps->ps_sigact[sig], sig, p->p_sigmask, code);
599 p->p_sigmask |= ps->ps_catchmask[sig] | mask;
600 } else {
601 ps->ps_code = code; /* XXX for core dump/debugger */
602 psignal(p, sig);
603 }
604}
605
606/*
607 * Send the specified signal to the specified process.
608 * If the signal has an action, the action is usually performed
609 * by the target process rather than the caller; we simply add
610 * the signal to the set of pending signals for the process.
611 * Exceptions:
612 * o When a stop signal is sent to a sleeping process that takes the default
613 * action, the process is stopped without awakening it.
614 * o SIGCONT restarts stopped processes (or puts them back to sleep)
615 * regardless of the signal action (eg, blocked or ignored).
616 * Other ignored signals are discarded immediately.
617 */
618void
619psignal(p, sig)
620 register struct proc *p;
621 register int sig;
622{
623 register int s, prop;
624 register sig_t action;
625 int mask;
626
58167ae3
DG
627 /* Ignore signals to system (internal) daemons */
628 if (p->p_flag & SSYS)
629 return;
630
15637ed4
RG
631 if ((unsigned)sig >= NSIG || sig == 0)
632 panic("psignal sig");
633 mask = sigmask(sig);
634 prop = sigprop[sig];
635
636 /*
637 * If proc is traced, always give parent a chance.
638 */
639 if (p->p_flag & STRC)
640 action = SIG_DFL;
641 else {
642 /*
643 * If the signal is being ignored,
644 * then we forget about it immediately.
645 * (Note: we don't set SIGCONT in p_sigignore,
646 * and if it is set to SIG_IGN,
647 * action will be SIG_DFL here.)
648 */
649 if (p->p_sigignore & mask)
650 return;
651 if (p->p_sigmask & mask)
652 action = SIG_HOLD;
653 else if (p->p_sigcatch & mask)
654 action = SIG_CATCH;
655 else
656 action = SIG_DFL;
657 }
658
659 if (p->p_nice > NZERO && (sig == SIGKILL ||
660 sig == SIGTERM && (p->p_flag&STRC || action != SIG_DFL)))
661 p->p_nice = NZERO;
662
663 if (prop & SA_CONT)
664 p->p_sig &= ~stopsigmask;
665
666 if (prop & SA_STOP) {
667 /*
668 * If sending a tty stop signal to a member of an orphaned
669 * process group, discard the signal here if the action
670 * is default; don't stop the process below if sleeping,
671 * and don't clear any pending SIGCONT.
672 */
673 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
674 action == SIG_DFL)
675 return;
676 p->p_sig &= ~contsigmask;
677 }
678 p->p_sig |= mask;
679
680 /*
681 * Defer further processing for signals which are held,
682 * except that stopped processes must be continued by SIGCONT.
683 */
684 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
685 return;
686 s = splhigh();
687 switch (p->p_stat) {
688
689 case SSLEEP:
690 /*
691 * If process is sleeping uninterruptibly
692 * we can't interrupt the sleep... the signal will
693 * be noticed when the process returns through
694 * trap() or syscall().
695 */
696 if ((p->p_flag & SSINTR) == 0)
697 goto out;
698 /*
699 * Process is sleeping and traced... make it runnable
700 * so it can discover the signal in issig() and stop
701 * for the parent.
702 */
703 if (p->p_flag&STRC)
704 goto run;
705 /*
706 * When a sleeping process receives a stop
707 * signal, process immediately if possible.
708 * All other (caught or default) signals
709 * cause the process to run.
710 */
711 if (prop & SA_STOP) {
712 if (action != SIG_DFL)
713 goto runfast;
714 /*
715 * If a child holding parent blocked,
716 * stopping could cause deadlock.
717 */
718 if (p->p_flag&SPPWAIT)
719 goto out;
720 p->p_sig &= ~mask;
721 p->p_xstat = sig;
722 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
723 psignal(p->p_pptr, SIGCHLD);
724 stop(p);
725 goto out;
726 } else
727 goto runfast;
728 /*NOTREACHED*/
729
730 case SSTOP:
731 /*
732 * If traced process is already stopped,
733 * then no further action is necessary.
734 */
735 if (p->p_flag&STRC)
736 goto out;
737
738 /*
739 * Kill signal always sets processes running.
740 */
741 if (sig == SIGKILL)
742 goto runfast;
743
744 if (prop & SA_CONT) {
745 /*
746 * If SIGCONT is default (or ignored), we continue
747 * the process but don't leave the signal in p_sig,
748 * as it has no further action. If SIGCONT is held,
749 * continue the process and leave the signal in p_sig.
750 * If the process catches SIGCONT, let it handle
751 * the signal itself. If it isn't waiting on
752 * an event, then it goes back to run state.
753 * Otherwise, process goes back to sleep state.
754 */
755 if (action == SIG_DFL)
756 p->p_sig &= ~mask;
757 if (action == SIG_CATCH)
758 goto runfast;
759 if (p->p_wchan == 0)
760 goto run;
761 p->p_stat = SSLEEP;
762 goto out;
763 }
764
765 if (prop & SA_STOP) {
766 /*
767 * Already stopped, don't need to stop again.
768 * (If we did the shell could get confused.)
769 */
770 p->p_sig &= ~mask; /* take it away */
771 goto out;
772 }
773
774 /*
775 * If process is sleeping interruptibly, then
776 * simulate a wakeup so that when it is continued,
777 * it will be made runnable and can look at the signal.
778 * But don't setrun the process, leave it stopped.
779 */
780 if (p->p_wchan && p->p_flag & SSINTR)
781 unsleep(p);
782 goto out;
783
784 default:
785 /*
786 * SRUN, SIDL, SZOMB do nothing with the signal,
787 * other than kicking ourselves if we are running.
788 * It will either never be noticed, or noticed very soon.
789 */
790 if (p == curproc)
791 signotify(p);
792 goto out;
793 }
794 /*NOTREACHED*/
795
796runfast:
797 /*
798 * Raise priority to at least PUSER.
799 */
800 if (p->p_pri > PUSER)
801 p->p_pri = PUSER;
802run:
803 setrun(p);
804out:
805 splx(s);
806}
807
808/*
809 * If the current process has a signal to process (should be caught
810 * or cause termination, should interrupt current syscall),
811 * return the signal number. Stop signals with default action
812 * are processed immediately, then cleared; they aren't returned.
813 * This is checked after each entry to the system for a syscall
814 * or trap (though this can usually be done without actually calling
815 * issig by checking the pending signal masks in the CURSIG macro.)
816 * The normal call sequence is
817 *
818 * while (sig = CURSIG(curproc))
819 * psig(sig);
820 */
4c45483e 821int
15637ed4
RG
822issig(p)
823 register struct proc *p;
824{
825 register int sig, mask, prop;
826
827 for (;;) {
828 mask = p->p_sig &~ p->p_sigmask;
829 if (p->p_flag&SPPWAIT)
830 mask &= ~stopsigmask;
831 if (mask == 0) /* no signal to send */
832 return (0);
833 sig = ffs((long)mask);
834 mask = sigmask(sig);
835 prop = sigprop[sig];
836 /*
837 * We should see pending but ignored signals
838 * only if STRC was on when they were posted.
839 */
840 if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
841 p->p_sig &= ~mask;
842 continue;
843 }
844 if (p->p_flag&STRC && (p->p_flag&SPPWAIT) == 0) {
845 /*
846 * If traced, always stop, and stay
847 * stopped until released by the parent.
848 */
849 p->p_xstat = sig;
850 psignal(p->p_pptr, SIGCHLD);
851 do {
852 stop(p);
853 (void) splclock();
854 swtch();
855 (void) splnone();
856 } while (!procxmt(p) && p->p_flag&STRC);
857
858 /*
859 * If the traced bit got turned off,
860 * go back up to the top to rescan signals.
861 * This ensures that p_sig* and ps_sigact
862 * are consistent.
863 */
864 if ((p->p_flag&STRC) == 0)
865 continue;
866
867 /*
868 * If parent wants us to take the signal,
869 * then it will leave it in p->p_xstat;
870 * otherwise we just look for signals again.
871 */
872 p->p_sig &= ~mask; /* clear the old signal */
873 sig = p->p_xstat;
874 if (sig == 0)
875 continue;
876
877 /*
878 * Put the new signal into p_sig.
879 * If signal is being masked,
880 * look for other signals.
881 */
882 mask = sigmask(sig);
883 p->p_sig |= mask;
884 if (p->p_sigmask & mask)
885 continue;
886 }
887
888 /*
889 * Decide whether the signal should be returned.
890 * Return the signal's number, or fall through
891 * to clear it from the pending mask.
892 */
893 switch ((int)p->p_sigacts->ps_sigact[sig]) {
894
895 case SIG_DFL:
896 /*
897 * Don't take default actions on system processes.
898 */
899 if (p->p_pid <= 1)
900 break; /* == ignore */
901 /*
902 * If there is a pending stop signal to process
903 * with default action, stop here,
904 * then clear the signal. However,
905 * if process is member of an orphaned
906 * process group, ignore tty stop signals.
907 */
908 if (prop & SA_STOP) {
909 if (p->p_flag&STRC ||
910 (p->p_pgrp->pg_jobc == 0 &&
911 prop & SA_TTYSTOP))
912 break; /* == ignore */
913 p->p_xstat = sig;
914 stop(p);
915 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
916 psignal(p->p_pptr, SIGCHLD);
917 (void) splclock();
918 swtch();
919 (void) splnone();
920 break;
921 } else if (prop & SA_IGNORE) {
922 /*
923 * Except for SIGCONT, shouldn't get here.
924 * Default action is to ignore; drop it.
925 */
926 break; /* == ignore */
927 } else
928 return (sig);
929 /*NOTREACHED*/
930
931 case SIG_IGN:
932 /*
933 * Masking above should prevent us ever trying
934 * to take action on an ignored signal other
935 * than SIGCONT, unless process is traced.
936 */
937 if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
938 printf("issig\n");
939 break; /* == ignore */
940
941 default:
942 /*
943 * This signal has an action, let
944 * psig process it.
945 */
946 return (sig);
947 }
948 p->p_sig &= ~mask; /* take the signal! */
949 }
950 /* NOTREACHED */
951}
952
953/*
954 * Put the argument process into the stopped
955 * state and notify the parent via wakeup.
956 * Signals are handled elsewhere.
957 * The process must not be on the run queue.
958 */
4c45483e 959static void
15637ed4
RG
960stop(p)
961 register struct proc *p;
962{
963
964 p->p_stat = SSTOP;
965 p->p_flag &= ~SWTED;
966 wakeup((caddr_t)p->p_pptr);
967}
968
969/*
970 * Take the action for the specified signal
971 * from the current set of pending signals.
972 */
973void
974psig(sig)
975 register int sig;
976{
977 register struct proc *p = curproc;
978 register struct sigacts *ps = p->p_sigacts;
979 register sig_t action;
980 int mask, returnmask;
981
982#ifdef DIAGNOSTIC
983 if (sig == 0)
984 panic("psig");
985#endif
986 mask = sigmask(sig);
987 p->p_sig &= ~mask;
988 action = ps->ps_sigact[sig];
989#ifdef KTRACE
990 if (KTRPOINT(p, KTR_PSIG))
991 ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SA_OLDMASK ?
992 ps->ps_oldmask : p->p_sigmask, 0);
993#endif
994 if (action == SIG_DFL) {
995 /*
996 * Default action, where the default is to kill
997 * the process. (Other cases were ignored above.)
998 */
999 sigexit(p, sig);
1000 /* NOTREACHED */
1001 } else {
1002 /*
1003 * If we get here, the signal must be caught.
1004 */
1005#ifdef DIAGNOSTIC
1006 if (action == SIG_IGN || (p->p_sigmask & mask))
1007 panic("psig action");
1008#endif
1009 /*
1010 * Set the new mask value and also defer further
1011 * occurences of this signal.
1012 *
1013 * Special case: user has done a sigpause. Here the
1014 * current mask is not of interest, but rather the
1015 * mask from before the sigpause is what we want
1016 * restored after the signal processing is completed.
1017 */
1018 (void) splhigh();
1019 if (ps->ps_flags & SA_OLDMASK) {
1020 returnmask = ps->ps_oldmask;
1021 ps->ps_flags &= ~SA_OLDMASK;
1022 } else
1023 returnmask = p->p_sigmask;
1024 p->p_sigmask |= ps->ps_catchmask[sig] | mask;
1025 (void) spl0();
1026 p->p_stats->p_ru.ru_nsignals++;
1027 sendsig(action, sig, returnmask, 0);
1028 }
1029}
1030
1031/*
1032 * Force the current process to exit with the specified
1033 * signal, dumping core if appropriate. We bypass the normal
1034 * tests for masked and caught signals, allowing unrecoverable
1035 * failures to terminate the process without changing signal state.
1036 * Mark the accounting record with the signal termination.
1037 * If dumping core, save the signal number for the debugger.
1038 * Calls exit and does not return.
1039 */
4c45483e 1040static void
15637ed4
RG
1041sigexit(p, sig)
1042 register struct proc *p;
1043 int sig;
1044{
1045
1046 p->p_acflag |= AXSIG;
1047 if (sigprop[sig] & SA_CORE) {
1048 p->p_sigacts->ps_sig = sig;
1523a604
GR
1049 /*
1050 * Log signals which would cause core dumps
c01ca970
GR
1051 * (Log as LOG_INFO to appease those who don't want
1052 * these messages.)
1053 * XXX : Todo, as well as euid, write out ruid too
1523a604 1054 */
26560360 1055 log(LOG_INFO, "pid %d: %s: uid %d: exited on signal %d\n",
1523a604 1056 p->p_pid, p->p_comm, p->p_ucred->cr_uid, sig);
15637ed4
RG
1057 if (coredump(p) == 0)
1058 sig |= WCOREFLAG;
1059 }
e5b1af89 1060 kexit(p, W_EXITCODE(0, sig));
15637ed4
RG
1061 /* NOTREACHED */
1062}
1063
1064/*
1065 * Create a core dump.
ce723be2
CR
1066 * The file name is "progname.core".
1067 * Core dumps are not created if:
1068 * the process is setuid,
1069 * we are on a filesystem mounted with MNT_NOCORE,
1070 * a file already exists and is not a core file,
1071 * or was not produced from the same program,
1072 * the link count to the corefile is > 1.
15637ed4 1073 */
4c45483e 1074int
15637ed4
RG
1075coredump(p)
1076 register struct proc *p;
1077{
1078 register struct vnode *vp;
1079 register struct pcred *pcred = p->p_cred;
1080 register struct ucred *cred = pcred->pc_ucred;
1081 register struct vmspace *vm = p->p_vmspace;
1082 struct vattr vattr;
ce723be2 1083 int error, error1, exists;
15637ed4 1084 struct nameidata nd;
ce723be2 1085 char name[MAXCOMLEN+6]; /* progname.core */
15637ed4
RG
1086
1087 if (pcred->p_svuid != pcred->p_ruid ||
1088 pcred->p_svgid != pcred->p_rgid)
1089 return (EFAULT);
1090 if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1091 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1092 return (EFAULT);
ce723be2
CR
1093 if (p->p_fd->fd_cdir->v_mount->mnt_flag & MNT_NOCORE)
1094 return (EFAULT);
1095
1096 sprintf(name, "%s.core", p->p_comm);
15637ed4
RG
1097 nd.ni_dirp = name;
1098 nd.ni_segflg = UIO_SYSSPACE;
cff1b394 1099 if ((error = vn_open(&nd, p, FWRITE, 0600)) == 0)
ce723be2
CR
1100 exists = 1;
1101 else
1102 exists = 0;
1103 if (error == ENOENT)
cff1b394 1104 error = vn_open(&nd, p, O_CREAT | FWRITE, 0600);
ce723be2 1105 if (error)
15637ed4
RG
1106 return (error);
1107 vp = nd.ni_vp;
1108 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
1109 vattr.va_nlink != 1) {
1110 error = EFAULT;
1111 goto out;
1112 }
ce723be2
CR
1113 if (exists) { /* if file already exists, look if it's a coredump */
1114 struct user userbuf; /* XXX */
1115 error = vn_rdwr(UIO_READ, vp, (caddr_t)&userbuf, sizeof(userbuf),
1116 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred,
1117 (int *)NULL, p);
1118 if (error || (vattr.va_size != ctob(UPAGES +
1119 userbuf.u_kproc.kp_eproc.e_vm.vm_dsize +
1120 userbuf.u_kproc.kp_eproc.e_vm.vm_ssize)) ||
1121 strcmp(p->p_comm, userbuf.u_kproc.kp_proc.p_comm)) {
1122 error = EFAULT;
1123 goto out;
1124 }
1125 }
15637ed4
RG
1126 VATTR_NULL(&vattr);
1127 vattr.va_size = 0;
1128 VOP_SETATTR(vp, &vattr, cred, p);
1129 p->p_acflag |= ACORE;
1130 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1131 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1132#ifdef HPUXCOMPAT
1133 /*
1134 * BLETCH! If we loaded from an HPUX format binary file
1135 * we have to dump an HPUX style user struct so that the
1136 * HPUX debuggers can grok it.
1137 */
1138 if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXBIN)
1139 error = hpuxdumpu(vp, cred);
1140 else
1141#endif
1142 error = vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
1143 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL,
1144 p);
1145 if (error == 0)
1146 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1147 (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1148 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1149 if (error == 0)
1150 error = vn_rdwr(UIO_WRITE, vp,
1151 (caddr_t) trunc_page(vm->vm_maxsaddr + MAXSSIZ
1152 - ctob(vm->vm_ssize)),
1153 round_page(ctob(vm->vm_ssize)),
1154 (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1155 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1156out:
1157 VOP_UNLOCK(vp);
1158 error1 = vn_close(vp, FWRITE, cred, p);
1159 if (error == 0)
1160 error = error1;
1161 return (error);
1162}
1163
1164/*
1165 * Nonexistent system call-- signal process (may want to handle it).
1166 * Flag error in case process won't see signal immediately (blocked or ignored).
1167 */
1168/* ARGSUSED */
4c45483e 1169int
15637ed4
RG
1170nosys(p, args, retval)
1171 struct proc *p;
1172 void *args;
1173 int *retval;
1174{
1175
1176 psignal(p, SIGSYS);
1177 return (EINVAL);
1178}