kern_sig.c revision 1.47 1 /* $NetBSD: kern_sig.c,v 1.47 1995/08/14 06:07:55 mycroft 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.7 (Berkeley) 4/18/94
41 */
42
43 #define SIGPROP /* include signal properties table */
44 #include <sys/param.h>
45 #include <sys/signalvar.h>
46 #include <sys/resourcevar.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/timeb.h>
52 #include <sys/times.h>
53 #include <sys/buf.h>
54 #include <sys/acct.h>
55 #include <sys/file.h>
56 #include <sys/kernel.h>
57 #include <sys/wait.h>
58 #include <sys/ktrace.h>
59 #include <sys/syslog.h>
60 #include <sys/stat.h>
61 #include <sys/core.h>
62
63 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
65
66 #include <machine/cpu.h>
67
68 #include <vm/vm.h>
69 #include <sys/user.h> /* for coredump */
70
71 void stop __P((struct proc *p));
72
73 /*
74 * Can process p, with pcred pc, send the signal signum to process q?
75 */
76 #define CANSIGNAL(p, pc, q, signum) \
77 ((pc)->pc_ucred->cr_uid == 0 || \
78 (pc)->p_ruid == (q)->p_cred->p_ruid || \
79 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
80 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
81 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
82 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
83
84 /* ARGSUSED */
85 sigaction(p, uap, retval)
86 struct proc *p;
87 register struct sigaction_args /* {
88 syscallarg(int) signum;
89 syscallarg(struct sigaction *) nsa;
90 syscallarg(struct sigaction *) osa;
91 } */ *uap;
92 register_t *retval;
93 {
94 struct sigaction vec;
95 register struct sigaction *sa;
96 register struct sigacts *ps = p->p_sigacts;
97 register int signum;
98 int bit, error;
99
100 signum = SCARG(uap, signum);
101 if (signum <= 0 || signum >= NSIG ||
102 signum == SIGKILL || signum == SIGSTOP)
103 return (EINVAL);
104 sa = &vec;
105 if (SCARG(uap, osa)) {
106 sa->sa_handler = ps->ps_sigact[signum];
107 sa->sa_mask = ps->ps_catchmask[signum];
108 bit = sigmask(signum);
109 sa->sa_flags = 0;
110 if ((ps->ps_sigonstack & bit) != 0)
111 sa->sa_flags |= SA_ONSTACK;
112 if ((ps->ps_sigintr & bit) == 0)
113 sa->sa_flags |= SA_RESTART;
114 if ((ps->ps_sigreset & bit) != 0)
115 sa->sa_flags |= SA_RESETHAND;
116 if (signum == SIGCHLD) {
117 if ((p->p_flag & P_NOCLDSTOP) != 0)
118 sa->sa_flags |= SA_NOCLDSTOP;
119 }
120 if ((sa->sa_mask & bit) == 0)
121 sa->sa_flags |= SA_NODEFER;
122 sa->sa_mask &= ~bit;
123 if (error = copyout((caddr_t)sa, (caddr_t)SCARG(uap, osa),
124 sizeof (vec)))
125 return (error);
126 }
127 if (SCARG(uap, nsa)) {
128 if (error = copyin((caddr_t)SCARG(uap, nsa), (caddr_t)sa,
129 sizeof (vec)))
130 return (error);
131 setsigvec(p, signum, sa);
132 }
133 return (0);
134 }
135
136 setsigvec(p, signum, sa)
137 register struct proc *p;
138 int signum;
139 register struct sigaction *sa;
140 {
141 register struct sigacts *ps = p->p_sigacts;
142 register int bit;
143
144 bit = sigmask(signum);
145 /*
146 * Change setting atomically.
147 */
148 (void) splhigh();
149 ps->ps_sigact[signum] = sa->sa_handler;
150 if ((sa->sa_flags & SA_NODEFER) == 0)
151 sa->sa_mask |= sigmask(signum);
152 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
153 if (signum == SIGCHLD) {
154 if (sa->sa_flags & SA_NOCLDSTOP)
155 p->p_flag |= P_NOCLDSTOP;
156 else
157 p->p_flag &= ~P_NOCLDSTOP;
158 }
159 if ((sa->sa_flags & SA_RESETHAND) != 0)
160 ps->ps_sigreset |= bit;
161 else
162 ps->ps_sigreset &= ~bit;
163 if ((sa->sa_flags & SA_RESTART) == 0)
164 ps->ps_sigintr |= bit;
165 else
166 ps->ps_sigintr &= ~bit;
167 if ((sa->sa_flags & SA_ONSTACK) != 0)
168 ps->ps_sigonstack |= bit;
169 else
170 ps->ps_sigonstack &= ~bit;
171 #ifdef COMPAT_SUNOS
172 {
173 extern struct emul emul_sunos;
174 if (p->p_emul == &emul_sunos && sa->sa_flags & SA_USERTRAMP)
175 ps->ps_usertramp |= bit;
176 else
177 ps->ps_usertramp &= ~bit;
178 }
179 #endif
180 /*
181 * Set bit in p_sigignore for signals that are set to SIG_IGN,
182 * and for signals set to SIG_DFL where the default is to ignore.
183 * However, don't put SIGCONT in p_sigignore,
184 * as we have to restart the process.
185 */
186 if (sa->sa_handler == SIG_IGN ||
187 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
188 p->p_siglist &= ~bit; /* never to be seen again */
189 if (signum != SIGCONT)
190 p->p_sigignore |= bit; /* easier in psignal */
191 p->p_sigcatch &= ~bit;
192 } else {
193 p->p_sigignore &= ~bit;
194 if (sa->sa_handler == SIG_DFL)
195 p->p_sigcatch &= ~bit;
196 else
197 p->p_sigcatch |= bit;
198 }
199 (void) spl0();
200 }
201
202 /*
203 * Initialize signal state for process 0;
204 * set to ignore signals that are ignored by default.
205 */
206 void
207 siginit(p)
208 struct proc *p;
209 {
210 register int i;
211
212 for (i = 0; i < NSIG; i++)
213 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
214 p->p_sigignore |= sigmask(i);
215 }
216
217 /*
218 * Reset signals for an exec of the specified process.
219 */
220 void
221 execsigs(p)
222 register struct proc *p;
223 {
224 register struct sigacts *ps = p->p_sigacts;
225 register int nc, mask;
226
227 /*
228 * Reset caught signals. Held signals remain held
229 * through p_sigmask (unless they were caught,
230 * and are now ignored by default).
231 */
232 while (p->p_sigcatch) {
233 nc = ffs((long)p->p_sigcatch);
234 mask = sigmask(nc);
235 p->p_sigcatch &= ~mask;
236 if (sigprop[nc] & SA_IGNORE) {
237 if (nc != SIGCONT)
238 p->p_sigignore |= mask;
239 p->p_siglist &= ~mask;
240 }
241 ps->ps_sigact[nc] = SIG_DFL;
242 }
243 /*
244 * Reset stack state to the user stack.
245 * Clear set of signals caught on the signal stack.
246 */
247 ps->ps_sigstk.ss_flags = SS_DISABLE;
248 ps->ps_sigstk.ss_size = 0;
249 ps->ps_sigstk.ss_base = 0;
250 ps->ps_flags = 0;
251 }
252
253 /*
254 * Manipulate signal mask.
255 * Note that we receive new mask, not pointer,
256 * and return old mask as return value;
257 * the library stub does the rest.
258 */
259 sigprocmask(p, uap, retval)
260 register struct proc *p;
261 struct sigprocmask_args /* {
262 syscallarg(int) how;
263 syscallarg(sigset_t) mask;
264 } */ *uap;
265 register_t *retval;
266 {
267 int error = 0;
268
269 *retval = p->p_sigmask;
270 (void) splhigh();
271
272 switch (SCARG(uap, how)) {
273 case SIG_BLOCK:
274 p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask;
275 break;
276
277 case SIG_UNBLOCK:
278 p->p_sigmask &= ~SCARG(uap, mask);
279 break;
280
281 case SIG_SETMASK:
282 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
283 break;
284
285 default:
286 error = EINVAL;
287 break;
288 }
289 (void) spl0();
290 return (error);
291 }
292
293 /* ARGSUSED */
294 sigpending(p, uap, retval)
295 struct proc *p;
296 void *uap;
297 register_t *retval;
298 {
299
300 *retval = p->p_siglist;
301 return (0);
302 }
303
304 /*
305 * Suspend process until signal, providing mask to be set
306 * in the meantime. Note nonstandard calling convention:
307 * libc stub passes mask, not pointer, to save a copyin.
308 */
309 /* ARGSUSED */
310 int
311 sigsuspend(p, uap, retval)
312 register struct proc *p;
313 struct sigsuspend_args /* {
314 syscallarg(int) mask;
315 } */ *uap;
316 register_t *retval;
317 {
318 register struct sigacts *ps = p->p_sigacts;
319
320 /*
321 * When returning from sigpause, we want
322 * the old mask to be restored after the
323 * signal handler has finished. Thus, we
324 * save it here and mark the sigacts structure
325 * to indicate this.
326 */
327 ps->ps_oldmask = p->p_sigmask;
328 ps->ps_flags |= SAS_OLDMASK;
329 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
330 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
331 /* void */;
332 /* always return EINTR rather than ERESTART... */
333 return (EINTR);
334 }
335
336 /* ARGSUSED */
337 sigaltstack(p, uap, retval)
338 struct proc *p;
339 register struct sigaltstack_args /* {
340 syscallarg(struct sigaltstack *) nss;
341 syscallarg(struct sigaltstack *) oss;
342 } */ *uap;
343 register_t *retval;
344 {
345 struct sigacts *psp;
346 struct sigaltstack ss;
347 int error;
348
349 psp = p->p_sigacts;
350 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
351 psp->ps_sigstk.ss_flags |= SS_DISABLE;
352 if (SCARG(uap, oss) && (error = copyout((caddr_t)&psp->ps_sigstk,
353 (caddr_t)SCARG(uap, oss), sizeof (struct sigaltstack))))
354 return (error);
355 if (SCARG(uap, nss) == 0)
356 return (0);
357 if (error = copyin((caddr_t)SCARG(uap, nss), (caddr_t)&ss,
358 sizeof (ss)))
359 return (error);
360 if (ss.ss_flags & SS_DISABLE) {
361 if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
362 return (EINVAL);
363 psp->ps_flags &= ~SAS_ALTSTACK;
364 psp->ps_sigstk.ss_flags = ss.ss_flags;
365 return (0);
366 }
367 if (ss.ss_size < MINSIGSTKSZ)
368 return (ENOMEM);
369 psp->ps_flags |= SAS_ALTSTACK;
370 psp->ps_sigstk= ss;
371 return (0);
372 }
373
374 /* ARGSUSED */
375 int
376 kill(cp, uap, retval)
377 register struct proc *cp;
378 register struct kill_args /* {
379 syscallarg(int) pid;
380 syscallarg(int) signum;
381 } */ *uap;
382 register_t *retval;
383 {
384 register struct proc *p;
385 register struct pcred *pc = cp->p_cred;
386
387 #ifdef COMPAT_09
388 SCARG(uap, pid) = (short) SCARG(uap, pid);
389 #endif
390
391 if ((u_int)SCARG(uap, signum) >= NSIG)
392 return (EINVAL);
393 if (SCARG(uap, pid) > 0) {
394 /* kill single process */
395 if ((p = pfind(SCARG(uap, pid))) == NULL)
396 return (ESRCH);
397 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
398 return (EPERM);
399 if (SCARG(uap, signum))
400 psignal(p, SCARG(uap, signum));
401 return (0);
402 }
403 switch (SCARG(uap, pid)) {
404 case -1: /* broadcast signal */
405 return (killpg1(cp, SCARG(uap, signum), 0, 1));
406 case 0: /* signal own process group */
407 return (killpg1(cp, SCARG(uap, signum), 0, 0));
408 default: /* negative explicit process group */
409 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
410 }
411 /* NOTREACHED */
412 }
413
414 /*
415 * Common code for kill process group/broadcast kill.
416 * cp is calling process.
417 */
418 killpg1(cp, signum, pgid, all)
419 register struct proc *cp;
420 int signum, pgid, all;
421 {
422 register struct proc *p;
423 register struct pcred *pc = cp->p_cred;
424 struct pgrp *pgrp;
425 int nfound = 0;
426
427 if (all)
428 /*
429 * broadcast
430 */
431 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
432 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
433 p == cp || !CANSIGNAL(cp, pc, p, signum))
434 continue;
435 nfound++;
436 if (signum)
437 psignal(p, signum);
438 }
439 else {
440 if (pgid == 0)
441 /*
442 * zero pgid means send to my process group.
443 */
444 pgrp = cp->p_pgrp;
445 else {
446 pgrp = pgfind(pgid);
447 if (pgrp == NULL)
448 return (ESRCH);
449 }
450 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
451 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
452 p->p_stat == SZOMB ||
453 !CANSIGNAL(cp, pc, p, signum))
454 continue;
455 nfound++;
456 if (signum)
457 psignal(p, signum);
458 }
459 }
460 return (nfound ? 0 : ESRCH);
461 }
462
463 /*
464 * Send a signal to a process group.
465 */
466 void
467 gsignal(pgid, signum)
468 int pgid, signum;
469 {
470 struct pgrp *pgrp;
471
472 if (pgid && (pgrp = pgfind(pgid)))
473 pgsignal(pgrp, signum, 0);
474 }
475
476 /*
477 * Send a signal to a process group. If checktty is 1,
478 * limit to members which have a controlling terminal.
479 */
480 void
481 pgsignal(pgrp, signum, checkctty)
482 struct pgrp *pgrp;
483 int signum, checkctty;
484 {
485 register struct proc *p;
486
487 if (pgrp)
488 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
489 if (checkctty == 0 || p->p_flag & P_CONTROLT)
490 psignal(p, signum);
491 }
492
493 /*
494 * Send a signal caused by a trap to the current process.
495 * If it will be caught immediately, deliver it with correct code.
496 * Otherwise, post it normally.
497 */
498 void
499 trapsignal(p, signum, code)
500 struct proc *p;
501 register int signum;
502 u_long code;
503 {
504 register struct sigacts *ps = p->p_sigacts;
505 int mask;
506
507 mask = sigmask(signum);
508 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
509 (p->p_sigmask & mask) == 0) {
510 p->p_stats->p_ru.ru_nsignals++;
511 #ifdef KTRACE
512 if (KTRPOINT(p, KTR_PSIG))
513 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
514 p->p_sigmask, code);
515 #endif
516 (*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
517 p->p_sigmask, code);
518 p->p_sigmask |= ps->ps_catchmask[signum];
519 if ((ps->ps_sigreset & mask) != 0) {
520 p->p_sigcatch &= ~mask;
521 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
522 p->p_sigignore |= mask;
523 ps->ps_sigact[signum] = SIG_DFL;
524 }
525 } else {
526 ps->ps_code = code; /* XXX for core dump/debugger */
527 psignal(p, signum);
528 }
529 }
530
531 /*
532 * Send the signal to the process. If the signal has an action, the action
533 * is usually performed by the target process rather than the caller; we add
534 * the signal to the set of pending signals for the process.
535 *
536 * Exceptions:
537 * o When a stop signal is sent to a sleeping process that takes the
538 * default action, the process is stopped without awakening it.
539 * o SIGCONT restarts stopped processes (or puts them back to sleep)
540 * regardless of the signal action (eg, blocked or ignored).
541 *
542 * Other ignored signals are discarded immediately.
543 */
544 void
545 psignal(p, signum)
546 register struct proc *p;
547 register int signum;
548 {
549 register int s, prop;
550 register sig_t action;
551 int mask;
552
553 if ((u_int)signum >= NSIG || signum == 0)
554 panic("psignal signal number");
555 mask = sigmask(signum);
556 prop = sigprop[signum];
557
558 /*
559 * If proc is traced, always give parent a chance.
560 */
561 if (p->p_flag & P_TRACED)
562 action = SIG_DFL;
563 else {
564 /*
565 * If the signal is being ignored,
566 * then we forget about it immediately.
567 * (Note: we don't set SIGCONT in p_sigignore,
568 * and if it is set to SIG_IGN,
569 * action will be SIG_DFL here.)
570 */
571 if (p->p_sigignore & mask)
572 return;
573 if (p->p_sigmask & mask)
574 action = SIG_HOLD;
575 else if (p->p_sigcatch & mask)
576 action = SIG_CATCH;
577 else {
578 action = SIG_DFL;
579
580 if (prop & SA_KILL && p->p_nice > NZERO)
581 p->p_nice = NZERO;
582
583 /*
584 * If sending a tty stop signal to a member of an
585 * orphaned process group, discard the signal here if
586 * the action is default; don't stop the process below
587 * if sleeping, and don't clear any pending SIGCONT.
588 */
589 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
590 return;
591 }
592 }
593
594 if (prop & SA_CONT)
595 p->p_siglist &= ~stopsigmask;
596
597 if (prop & SA_STOP)
598 p->p_siglist &= ~contsigmask;
599
600 p->p_siglist |= mask;
601
602 /*
603 * Defer further processing for signals which are held,
604 * except that stopped processes must be continued by SIGCONT.
605 */
606 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
607 return;
608 s = splhigh();
609 switch (p->p_stat) {
610
611 case SSLEEP:
612 /*
613 * If process is sleeping uninterruptibly
614 * we can't interrupt the sleep... the signal will
615 * be noticed when the process returns through
616 * trap() or syscall().
617 */
618 if ((p->p_flag & P_SINTR) == 0)
619 goto out;
620 /*
621 * Process is sleeping and traced... make it runnable
622 * so it can discover the signal in issignal() and stop
623 * for the parent.
624 */
625 if (p->p_flag & P_TRACED)
626 goto run;
627 /*
628 * If SIGCONT is default (or ignored) and process is
629 * asleep, we are finished; the process should not
630 * be awakened.
631 */
632 if ((prop & SA_CONT) && action == SIG_DFL) {
633 p->p_siglist &= ~mask;
634 goto out;
635 }
636 /*
637 * When a sleeping process receives a stop
638 * signal, process immediately if possible.
639 */
640 if ((prop & SA_STOP) && action == SIG_DFL) {
641 /*
642 * If a child holding parent blocked,
643 * stopping could cause deadlock.
644 */
645 if (p->p_flag & P_PPWAIT)
646 goto out;
647 p->p_siglist &= ~mask;
648 p->p_xstat = signum;
649 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
650 psignal(p->p_pptr, SIGCHLD);
651 stop(p);
652 goto out;
653 }
654 /*
655 * All other (caught or default) signals
656 * cause the process to run.
657 */
658 goto runfast;
659 /*NOTREACHED*/
660
661 case SSTOP:
662 /*
663 * If traced process is already stopped,
664 * then no further action is necessary.
665 */
666 if (p->p_flag & P_TRACED)
667 goto out;
668
669 /*
670 * Kill signal always sets processes running.
671 */
672 if (signum == SIGKILL)
673 goto runfast;
674
675 if (prop & SA_CONT) {
676 /*
677 * If SIGCONT is default (or ignored), we continue the
678 * process but don't leave the signal in p_siglist, as
679 * it has no further action. If SIGCONT is held, we
680 * continue the process and leave the signal in
681 * p_siglist. If the process catches SIGCONT, let it
682 * handle the signal itself. If it isn't waiting on
683 * an event, then it goes back to run state.
684 * Otherwise, process goes back to sleep state.
685 */
686 if (action == SIG_DFL)
687 p->p_siglist &= ~mask;
688 if (action == SIG_CATCH)
689 goto runfast;
690 if (p->p_wchan == 0)
691 goto run;
692 p->p_stat = SSLEEP;
693 goto out;
694 }
695
696 if (prop & SA_STOP) {
697 /*
698 * Already stopped, don't need to stop again.
699 * (If we did the shell could get confused.)
700 */
701 p->p_siglist &= ~mask; /* take it away */
702 goto out;
703 }
704
705 /*
706 * If process is sleeping interruptibly, then simulate a
707 * wakeup so that when it is continued, it will be made
708 * runnable and can look at the signal. But don't make
709 * the process runnable, leave it stopped.
710 */
711 if (p->p_wchan && p->p_flag & P_SINTR)
712 unsleep(p);
713 goto out;
714
715 default:
716 /*
717 * SRUN, SIDL, SZOMB do nothing with the signal,
718 * other than kicking ourselves if we are running.
719 * It will either never be noticed, or noticed very soon.
720 */
721 if (p == curproc)
722 signotify(p);
723 goto out;
724 }
725 /*NOTREACHED*/
726
727 runfast:
728 /*
729 * Raise priority to at least PUSER.
730 */
731 if (p->p_priority > PUSER)
732 p->p_priority = PUSER;
733 run:
734 setrunnable(p);
735 out:
736 splx(s);
737 }
738
739 /*
740 * If the current process has received a signal (should be caught or cause
741 * termination, should interrupt current syscall), return the signal number.
742 * Stop signals with default action are processed immediately, then cleared;
743 * they aren't returned. This is checked after each entry to the system for
744 * a syscall or trap (though this can usually be done without calling issignal
745 * by checking the pending signal masks in the CURSIG macro.) The normal call
746 * sequence is
747 *
748 * while (signum = CURSIG(curproc))
749 * postsig(signum);
750 */
751 int
752 issignal(p)
753 register struct proc *p;
754 {
755 register int signum, mask, prop;
756
757 for (;;) {
758 mask = p->p_siglist & ~p->p_sigmask;
759 if (p->p_flag & P_PPWAIT)
760 mask &= ~stopsigmask;
761 if (mask == 0) /* no signal to send */
762 return (0);
763 signum = ffs((long)mask);
764 mask = sigmask(signum);
765 p->p_siglist &= ~mask; /* take the signal! */
766
767 /*
768 * We should see pending but ignored signals
769 * only if P_TRACED was on when they were posted.
770 */
771 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
772 continue;
773
774 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
775 /*
776 * If traced, always stop, and stay
777 * stopped until released by the debugger.
778 */
779 p->p_xstat = signum;
780
781 if (p->p_flag & P_FSTRACE) {
782 #ifdef PROCFS
783 /* procfs debugging */
784 p->p_stat = SSTOP;
785 wakeup((caddr_t)p);
786 mi_switch();
787 #else
788 panic("procfs debugging");
789 #endif
790 } else {
791 /* ptrace debugging */
792 psignal(p->p_pptr, SIGCHLD);
793 do {
794 stop(p);
795 mi_switch();
796 } while (!trace_req(p) && p->p_flag & P_TRACED);
797 }
798
799 /*
800 * If we are no longer being traced, or the parent
801 * didn't give us a signal, look for more signals.
802 */
803 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
804 continue;
805
806 /*
807 * If the new signal is being masked, look for other
808 * signals.
809 */
810 signum = p->p_xstat;
811 mask = sigmask(signum);
812 if ((p->p_sigmask & mask) != 0)
813 continue;
814 p->p_siglist &= ~mask; /* take the signal! */
815 }
816
817 prop = sigprop[signum];
818
819 /*
820 * Decide whether the signal should be returned.
821 * Return the signal's number, or fall through
822 * to clear it from the pending mask.
823 */
824 switch ((long)p->p_sigacts->ps_sigact[signum]) {
825
826 case (long)SIG_DFL:
827 /*
828 * Don't take default actions on system processes.
829 */
830 if (p->p_pid <= 1) {
831 #ifdef DIAGNOSTIC
832 /*
833 * Are you sure you want to ignore SIGSEGV
834 * in init? XXX
835 */
836 printf("Process (pid %d) got signal %d\n",
837 p->p_pid, signum);
838 #endif
839 break; /* == ignore */
840 }
841 /*
842 * If there is a pending stop signal to process
843 * with default action, stop here,
844 * then clear the signal. However,
845 * if process is member of an orphaned
846 * process group, ignore tty stop signals.
847 */
848 if (prop & SA_STOP) {
849 if (p->p_flag & P_TRACED ||
850 (p->p_pgrp->pg_jobc == 0 &&
851 prop & SA_TTYSTOP))
852 break; /* == ignore */
853 p->p_xstat = signum;
854 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
855 psignal(p->p_pptr, SIGCHLD);
856 stop(p);
857 mi_switch();
858 break;
859 } else if (prop & SA_IGNORE) {
860 /*
861 * Except for SIGCONT, shouldn't get here.
862 * Default action is to ignore; drop it.
863 */
864 break; /* == ignore */
865 } else
866 goto keep;
867 /*NOTREACHED*/
868
869 case (long)SIG_IGN:
870 /*
871 * Masking above should prevent us ever trying
872 * to take action on an ignored signal other
873 * than SIGCONT, unless process is traced.
874 */
875 if ((prop & SA_CONT) == 0 &&
876 (p->p_flag & P_TRACED) == 0)
877 printf("issignal\n");
878 break; /* == ignore */
879
880 default:
881 /*
882 * This signal has an action, let
883 * postsig() process it.
884 */
885 goto keep;
886 }
887 }
888 /* NOTREACHED */
889
890 keep:
891 p->p_siglist |= mask; /* leave the signal for later */
892 return (signum);
893 }
894
895 /*
896 * Put the argument process into the stopped state and notify the parent
897 * via wakeup. Signals are handled elsewhere. The process must not be
898 * on the run queue.
899 */
900 void
901 stop(p)
902 register struct proc *p;
903 {
904
905 p->p_stat = SSTOP;
906 p->p_flag &= ~P_WAITED;
907 wakeup((caddr_t)p->p_pptr);
908 }
909
910 /*
911 * Take the action for the specified signal
912 * from the current set of pending signals.
913 */
914 void
915 postsig(signum)
916 register int signum;
917 {
918 register struct proc *p = curproc;
919 register struct sigacts *ps = p->p_sigacts;
920 register sig_t action;
921 u_long code;
922 int mask, returnmask;
923
924 #ifdef DIAGNOSTIC
925 if (signum == 0)
926 panic("postsig");
927 #endif
928 mask = sigmask(signum);
929 p->p_siglist &= ~mask;
930 action = ps->ps_sigact[signum];
931 #ifdef KTRACE
932 if (KTRPOINT(p, KTR_PSIG))
933 ktrpsig(p->p_tracep,
934 signum, action, ps->ps_flags & SAS_OLDMASK ?
935 ps->ps_oldmask : p->p_sigmask, 0);
936 #endif
937 if (action == SIG_DFL) {
938 /*
939 * Default action, where the default is to kill
940 * the process. (Other cases were ignored above.)
941 */
942 sigexit(p, signum);
943 /* NOTREACHED */
944 } else {
945 /*
946 * If we get here, the signal must be caught.
947 */
948 #ifdef DIAGNOSTIC
949 if (action == SIG_IGN || (p->p_sigmask & mask))
950 panic("postsig action");
951 #endif
952 /*
953 * Set the new mask value and also defer further
954 * occurences of this signal.
955 *
956 * Special case: user has done a sigpause. Here the
957 * current mask is not of interest, but rather the
958 * mask from before the sigpause is what we want
959 * restored after the signal processing is completed.
960 */
961 (void) splhigh();
962 if (ps->ps_flags & SAS_OLDMASK) {
963 returnmask = ps->ps_oldmask;
964 ps->ps_flags &= ~SAS_OLDMASK;
965 } else
966 returnmask = p->p_sigmask;
967 p->p_sigmask |= ps->ps_catchmask[signum];
968 if ((ps->ps_sigreset & mask) != 0) {
969 p->p_sigcatch &= ~mask;
970 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
971 p->p_sigignore |= mask;
972 ps->ps_sigact[signum] = SIG_DFL;
973 }
974 (void) spl0();
975 p->p_stats->p_ru.ru_nsignals++;
976 if (ps->ps_sig != signum) {
977 code = 0;
978 } else {
979 code = ps->ps_code;
980 ps->ps_code = 0;
981 }
982 (*p->p_emul->e_sendsig)(action, signum, returnmask, code);
983 }
984 }
985
986 /*
987 * Kill the current process for stated reason.
988 */
989 killproc(p, why)
990 struct proc *p;
991 char *why;
992 {
993
994 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
995 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
996 psignal(p, SIGKILL);
997 }
998
999 /*
1000 * Force the current process to exit with the specified signal, dumping core
1001 * if appropriate. We bypass the normal tests for masked and caught signals,
1002 * allowing unrecoverable failures to terminate the process without changing
1003 * signal state. Mark the accounting record with the signal termination.
1004 * If dumping core, save the signal number for the debugger. Calls exit and
1005 * does not return.
1006 */
1007 int
1008 sigexit(p, signum)
1009 register struct proc *p;
1010 int signum;
1011 {
1012
1013 p->p_acflag |= AXSIG;
1014 if (sigprop[signum] & SA_CORE) {
1015 p->p_sigacts->ps_sig = signum;
1016 if (coredump(p) == 0)
1017 signum |= WCOREFLAG;
1018 }
1019 exit1(p, W_EXITCODE(0, signum));
1020 /* NOTREACHED */
1021 }
1022
1023 /*
1024 * Dump core, into a file named "progname.core", unless the process was
1025 * setuid/setgid.
1026 */
1027 int
1028 coredump(p)
1029 register struct proc *p;
1030 {
1031 register struct vnode *vp;
1032 register struct pcred *pcred = p->p_cred;
1033 register struct ucred *cred = pcred->pc_ucred;
1034 register struct vmspace *vm = p->p_vmspace;
1035 struct nameidata nd;
1036 struct vattr vattr;
1037 int error, error1;
1038 char name[MAXCOMLEN+6]; /* progname.core */
1039 struct core core;
1040
1041 if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
1042 return (EFAULT);
1043 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1044 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1045 return (EFAULT);
1046 sprintf(name, "%s.core", p->p_comm);
1047 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1048 if (error = vn_open(&nd,
1049 O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
1050 return (error);
1051 vp = nd.ni_vp;
1052
1053 /* Don't dump to non-regular files or files with links. */
1054 if (vp->v_type != VREG ||
1055 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1056 error = EFAULT;
1057 goto out;
1058 }
1059 VATTR_NULL(&vattr);
1060 vattr.va_size = 0;
1061 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1062 VOP_SETATTR(vp, &vattr, cred, p);
1063 p->p_acflag |= ACORE;
1064 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1065 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1066
1067 core.c_midmag = 0;
1068 strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1069 core.c_nseg = 0;
1070 core.c_signo = p->p_sigacts->ps_sig;
1071 core.c_ucode = p->p_sigacts->ps_code;
1072 core.c_cpusize = 0;
1073 core.c_tsize = (u_long)ctob(vm->vm_tsize);
1074 core.c_dsize = (u_long)ctob(vm->vm_dsize);
1075 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1076 error = cpu_coredump(p, vp, cred, &core);
1077 if (error)
1078 goto out;
1079 if (core.c_midmag == 0) {
1080 /* XXX
1081 * cpu_coredump() didn't bother to set the magic; assume
1082 * this is a request to do a traditional dump. cpu_coredump()
1083 * is still responsible for setting sensible values in
1084 * the core header.
1085 */
1086 if (core.c_cpusize == 0)
1087 core.c_cpusize = USPACE; /* Just in case */
1088 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1089 (int)core.c_dsize,
1090 (off_t)core.c_cpusize, UIO_USERSPACE,
1091 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1092 if (error)
1093 goto out;
1094 error = vn_rdwr(UIO_WRITE, vp,
1095 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1096 core.c_ssize,
1097 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1098 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1099 } else {
1100 /*
1101 * vm_coredump() spits out all appropriate segments.
1102 * All that's left to do is to write the core header.
1103 */
1104 error = vm_coredump(p, vp, cred, &core);
1105 if (error)
1106 goto out;
1107 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1108 (int)core.c_hdrsize, (off_t)0,
1109 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1110 }
1111 out:
1112 VOP_UNLOCK(vp);
1113 error1 = vn_close(vp, FWRITE, cred, p);
1114 if (error == 0)
1115 error = error1;
1116 return (error);
1117 }
1118
1119 /*
1120 * Nonexistent system call-- signal process (may want to handle it).
1121 * Flag error in case process won't see signal immediately (blocked or ignored).
1122 */
1123 /* ARGSUSED */
1124 int
1125 nosys(p, args, retval)
1126 struct proc *p;
1127 void *args;
1128 register_t *retval;
1129 {
1130
1131 psignal(p, SIGSYS);
1132 return (ENOSYS);
1133 }
1134