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