kern_sig.c revision 1.62 1 /* $NetBSD: kern_sig.c,v 1.62 1997/04/03 21:08:27 kleink 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 !CANSIGNAL(cp, pc, p, signum))
467 continue;
468 nfound++;
469 if (signum && p->p_stat != SZOMB)
470 psignal(p, signum);
471 }
472 }
473 return (nfound ? 0 : ESRCH);
474 }
475
476 /*
477 * Send a signal to a process group.
478 */
479 void
480 gsignal(pgid, signum)
481 int pgid, signum;
482 {
483 struct pgrp *pgrp;
484
485 if (pgid && (pgrp = pgfind(pgid)))
486 pgsignal(pgrp, signum, 0);
487 }
488
489 /*
490 * Send a signal to a process group. If checktty is 1,
491 * limit to members which have a controlling terminal.
492 */
493 void
494 pgsignal(pgrp, signum, checkctty)
495 struct pgrp *pgrp;
496 int signum, checkctty;
497 {
498 register struct proc *p;
499
500 if (pgrp)
501 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
502 if (checkctty == 0 || p->p_flag & P_CONTROLT)
503 psignal(p, signum);
504 }
505
506 /*
507 * Send a signal caused by a trap to the current process.
508 * If it will be caught immediately, deliver it with correct code.
509 * Otherwise, post it normally.
510 */
511 void
512 trapsignal(p, signum, code)
513 struct proc *p;
514 register int signum;
515 u_long code;
516 {
517 register struct sigacts *ps = p->p_sigacts;
518 int mask;
519
520 mask = sigmask(signum);
521 if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
522 (p->p_sigmask & mask) == 0) {
523 p->p_stats->p_ru.ru_nsignals++;
524 #ifdef KTRACE
525 if (KTRPOINT(p, KTR_PSIG))
526 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
527 p->p_sigmask, code);
528 #endif
529 (*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
530 p->p_sigmask, code);
531 p->p_sigmask |= ps->ps_catchmask[signum];
532 if ((ps->ps_sigreset & mask) != 0) {
533 p->p_sigcatch &= ~mask;
534 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
535 p->p_sigignore |= mask;
536 ps->ps_sigact[signum] = SIG_DFL;
537 }
538 } else {
539 ps->ps_code = code; /* XXX for core dump/debugger */
540 psignal(p, signum);
541 }
542 }
543
544 /*
545 * Send the signal to the process. If the signal has an action, the action
546 * is usually performed by the target process rather than the caller; we add
547 * the signal to the set of pending signals for the process.
548 *
549 * Exceptions:
550 * o When a stop signal is sent to a sleeping process that takes the
551 * default action, the process is stopped without awakening it.
552 * o SIGCONT restarts stopped processes (or puts them back to sleep)
553 * regardless of the signal action (eg, blocked or ignored).
554 *
555 * Other ignored signals are discarded immediately.
556 */
557 void
558 psignal(p, signum)
559 register struct proc *p;
560 register int signum;
561 {
562 register int s, prop;
563 register sig_t action;
564 int mask;
565
566 if ((u_int)signum >= NSIG || signum == 0)
567 panic("psignal signal number");
568 mask = sigmask(signum);
569 prop = sigprop[signum];
570
571 /*
572 * If proc is traced, always give parent a chance.
573 */
574 if (p->p_flag & P_TRACED)
575 action = SIG_DFL;
576 else {
577 /*
578 * If the signal is being ignored,
579 * then we forget about it immediately.
580 * (Note: we don't set SIGCONT in p_sigignore,
581 * and if it is set to SIG_IGN,
582 * action will be SIG_DFL here.)
583 */
584 if (p->p_sigignore & mask)
585 return;
586 if (p->p_sigmask & mask)
587 action = SIG_HOLD;
588 else if (p->p_sigcatch & mask)
589 action = SIG_CATCH;
590 else {
591 action = SIG_DFL;
592
593 if (prop & SA_KILL && p->p_nice > NZERO)
594 p->p_nice = NZERO;
595
596 /*
597 * If sending a tty stop signal to a member of an
598 * orphaned process group, discard the signal here if
599 * the action is default; don't stop the process below
600 * if sleeping, and don't clear any pending SIGCONT.
601 */
602 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
603 return;
604 }
605 }
606
607 if (prop & SA_CONT)
608 p->p_siglist &= ~stopsigmask;
609
610 if (prop & SA_STOP)
611 p->p_siglist &= ~contsigmask;
612
613 p->p_siglist |= mask;
614
615 /*
616 * Defer further processing for signals which are held,
617 * except that stopped processes must be continued by SIGCONT.
618 */
619 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
620 return;
621 s = splhigh();
622 switch (p->p_stat) {
623
624 case SSLEEP:
625 /*
626 * If process is sleeping uninterruptibly
627 * we can't interrupt the sleep... the signal will
628 * be noticed when the process returns through
629 * trap() or syscall().
630 */
631 if ((p->p_flag & P_SINTR) == 0)
632 goto out;
633 /*
634 * Process is sleeping and traced... make it runnable
635 * so it can discover the signal in issignal() and stop
636 * for the parent.
637 */
638 if (p->p_flag & P_TRACED)
639 goto run;
640 /*
641 * If SIGCONT is default (or ignored) and process is
642 * asleep, we are finished; the process should not
643 * be awakened.
644 */
645 if ((prop & SA_CONT) && action == SIG_DFL) {
646 p->p_siglist &= ~mask;
647 goto out;
648 }
649 /*
650 * When a sleeping process receives a stop
651 * signal, process immediately if possible.
652 */
653 if ((prop & SA_STOP) && action == SIG_DFL) {
654 /*
655 * If a child holding parent blocked,
656 * stopping could cause deadlock.
657 */
658 if (p->p_flag & P_PPWAIT)
659 goto out;
660 p->p_siglist &= ~mask;
661 p->p_xstat = signum;
662 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
663 psignal(p->p_pptr, SIGCHLD);
664 stop(p);
665 goto out;
666 }
667 /*
668 * All other (caught or default) signals
669 * cause the process to run.
670 */
671 goto runfast;
672 /*NOTREACHED*/
673
674 case SSTOP:
675 /*
676 * If traced process is already stopped,
677 * then no further action is necessary.
678 */
679 if (p->p_flag & P_TRACED)
680 goto out;
681
682 /*
683 * Kill signal always sets processes running.
684 */
685 if (signum == SIGKILL)
686 goto runfast;
687
688 if (prop & SA_CONT) {
689 /*
690 * If SIGCONT is default (or ignored), we continue the
691 * process but don't leave the signal in p_siglist, as
692 * it has no further action. If SIGCONT is held, we
693 * continue the process and leave the signal in
694 * p_siglist. If the process catches SIGCONT, let it
695 * handle the signal itself. If it isn't waiting on
696 * an event, then it goes back to run state.
697 * Otherwise, process goes back to sleep state.
698 */
699 if (action == SIG_DFL)
700 p->p_siglist &= ~mask;
701 if (action == SIG_CATCH)
702 goto runfast;
703 if (p->p_wchan == 0)
704 goto run;
705 p->p_stat = SSLEEP;
706 goto out;
707 }
708
709 if (prop & SA_STOP) {
710 /*
711 * Already stopped, don't need to stop again.
712 * (If we did the shell could get confused.)
713 */
714 p->p_siglist &= ~mask; /* take it away */
715 goto out;
716 }
717
718 /*
719 * If process is sleeping interruptibly, then simulate a
720 * wakeup so that when it is continued, it will be made
721 * runnable and can look at the signal. But don't make
722 * the process runnable, leave it stopped.
723 */
724 if (p->p_wchan && p->p_flag & P_SINTR)
725 unsleep(p);
726 goto out;
727
728 default:
729 /*
730 * SRUN, SIDL, SZOMB do nothing with the signal,
731 * other than kicking ourselves if we are running.
732 * It will either never be noticed, or noticed very soon.
733 */
734 if (p == curproc)
735 signotify(p);
736 goto out;
737 }
738 /*NOTREACHED*/
739
740 runfast:
741 /*
742 * Raise priority to at least PUSER.
743 */
744 if (p->p_priority > PUSER)
745 p->p_priority = PUSER;
746 run:
747 setrunnable(p);
748 out:
749 splx(s);
750 }
751
752 /*
753 * If the current process has received a signal (should be caught or cause
754 * termination, should interrupt current syscall), return the signal number.
755 * Stop signals with default action are processed immediately, then cleared;
756 * they aren't returned. This is checked after each entry to the system for
757 * a syscall or trap (though this can usually be done without calling issignal
758 * by checking the pending signal masks in the CURSIG macro.) The normal call
759 * sequence is
760 *
761 * while (signum = CURSIG(curproc))
762 * postsig(signum);
763 */
764 int
765 issignal(p)
766 register struct proc *p;
767 {
768 register int signum, mask, prop;
769
770 for (;;) {
771 mask = p->p_siglist & ~p->p_sigmask;
772 if (p->p_flag & P_PPWAIT)
773 mask &= ~stopsigmask;
774 if (mask == 0) /* no signal to send */
775 return (0);
776 signum = ffs((long)mask);
777 mask = sigmask(signum);
778 p->p_siglist &= ~mask; /* take the signal! */
779
780 /*
781 * We should see pending but ignored signals
782 * only if P_TRACED was on when they were posted.
783 */
784 if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
785 continue;
786
787 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
788 /*
789 * If traced, always stop, and stay
790 * stopped until released by the debugger.
791 */
792 p->p_xstat = signum;
793
794 if (p->p_flag & P_FSTRACE) {
795 #ifdef PROCFS
796 /* procfs debugging */
797 p->p_stat = SSTOP;
798 wakeup((caddr_t)p);
799 mi_switch();
800 #else
801 panic("procfs debugging");
802 #endif
803 } else {
804 /* ptrace debugging */
805 psignal(p->p_pptr, SIGCHLD);
806 do {
807 stop(p);
808 mi_switch();
809 } while (!trace_req(p) && p->p_flag & P_TRACED);
810 }
811
812 /*
813 * If we are no longer being traced, or the parent
814 * didn't give us a signal, look for more signals.
815 */
816 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
817 continue;
818
819 /*
820 * If the new signal is being masked, look for other
821 * signals.
822 */
823 signum = p->p_xstat;
824 mask = sigmask(signum);
825 if ((p->p_sigmask & mask) != 0)
826 continue;
827 p->p_siglist &= ~mask; /* take the signal! */
828 }
829
830 prop = sigprop[signum];
831
832 /*
833 * Decide whether the signal should be returned.
834 * Return the signal's number, or fall through
835 * to clear it from the pending mask.
836 */
837 switch ((long)p->p_sigacts->ps_sigact[signum]) {
838
839 case (long)SIG_DFL:
840 /*
841 * Don't take default actions on system processes.
842 */
843 if (p->p_pid <= 1) {
844 #ifdef DIAGNOSTIC
845 /*
846 * Are you sure you want to ignore SIGSEGV
847 * in init? XXX
848 */
849 printf("Process (pid %d) got signal %d\n",
850 p->p_pid, signum);
851 #endif
852 break; /* == ignore */
853 }
854 /*
855 * If there is a pending stop signal to process
856 * with default action, stop here,
857 * then clear the signal. However,
858 * if process is member of an orphaned
859 * process group, ignore tty stop signals.
860 */
861 if (prop & SA_STOP) {
862 if (p->p_flag & P_TRACED ||
863 (p->p_pgrp->pg_jobc == 0 &&
864 prop & SA_TTYSTOP))
865 break; /* == ignore */
866 p->p_xstat = signum;
867 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
868 psignal(p->p_pptr, SIGCHLD);
869 stop(p);
870 mi_switch();
871 break;
872 } else if (prop & SA_IGNORE) {
873 /*
874 * Except for SIGCONT, shouldn't get here.
875 * Default action is to ignore; drop it.
876 */
877 break; /* == ignore */
878 } else
879 goto keep;
880 /*NOTREACHED*/
881
882 case (long)SIG_IGN:
883 /*
884 * Masking above should prevent us ever trying
885 * to take action on an ignored signal other
886 * than SIGCONT, unless process is traced.
887 */
888 if ((prop & SA_CONT) == 0 &&
889 (p->p_flag & P_TRACED) == 0)
890 printf("issignal\n");
891 break; /* == ignore */
892
893 default:
894 /*
895 * This signal has an action, let
896 * postsig() process it.
897 */
898 goto keep;
899 }
900 }
901 /* NOTREACHED */
902
903 keep:
904 p->p_siglist |= mask; /* leave the signal for later */
905 return (signum);
906 }
907
908 /*
909 * Put the argument process into the stopped state and notify the parent
910 * via wakeup. Signals are handled elsewhere. The process must not be
911 * on the run queue.
912 */
913 void
914 stop(p)
915 register struct proc *p;
916 {
917
918 p->p_stat = SSTOP;
919 p->p_flag &= ~P_WAITED;
920 wakeup((caddr_t)p->p_pptr);
921 }
922
923 /*
924 * Take the action for the specified signal
925 * from the current set of pending signals.
926 */
927 void
928 postsig(signum)
929 register int signum;
930 {
931 register struct proc *p = curproc;
932 register struct sigacts *ps = p->p_sigacts;
933 register sig_t action;
934 u_long code;
935 int mask, returnmask;
936
937 #ifdef DIAGNOSTIC
938 if (signum == 0)
939 panic("postsig");
940 #endif
941 mask = sigmask(signum);
942 p->p_siglist &= ~mask;
943 action = ps->ps_sigact[signum];
944 #ifdef KTRACE
945 if (KTRPOINT(p, KTR_PSIG))
946 ktrpsig(p->p_tracep,
947 signum, action, ps->ps_flags & SAS_OLDMASK ?
948 ps->ps_oldmask : p->p_sigmask, 0);
949 #endif
950 if (action == SIG_DFL) {
951 /*
952 * Default action, where the default is to kill
953 * the process. (Other cases were ignored above.)
954 */
955 sigexit(p, signum);
956 /* NOTREACHED */
957 } else {
958 /*
959 * If we get here, the signal must be caught.
960 */
961 #ifdef DIAGNOSTIC
962 if (action == SIG_IGN || (p->p_sigmask & mask))
963 panic("postsig action");
964 #endif
965 /*
966 * Set the new mask value and also defer further
967 * occurences of this signal.
968 *
969 * Special case: user has done a sigpause. Here the
970 * current mask is not of interest, but rather the
971 * mask from before the sigpause is what we want
972 * restored after the signal processing is completed.
973 */
974 (void) splhigh();
975 if (ps->ps_flags & SAS_OLDMASK) {
976 returnmask = ps->ps_oldmask;
977 ps->ps_flags &= ~SAS_OLDMASK;
978 } else
979 returnmask = p->p_sigmask;
980 p->p_sigmask |= ps->ps_catchmask[signum];
981 if ((ps->ps_sigreset & mask) != 0) {
982 p->p_sigcatch &= ~mask;
983 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
984 p->p_sigignore |= mask;
985 ps->ps_sigact[signum] = SIG_DFL;
986 }
987 (void) spl0();
988 p->p_stats->p_ru.ru_nsignals++;
989 if (ps->ps_sig != signum) {
990 code = 0;
991 } else {
992 code = ps->ps_code;
993 ps->ps_code = 0;
994 }
995 (*p->p_emul->e_sendsig)(action, signum, returnmask, code);
996 }
997 }
998
999 /*
1000 * Kill the current process for stated reason.
1001 */
1002 void
1003 killproc(p, why)
1004 struct proc *p;
1005 char *why;
1006 {
1007
1008 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1009 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1010 psignal(p, SIGKILL);
1011 }
1012
1013 /*
1014 * Force the current process to exit with the specified signal, dumping core
1015 * if appropriate. We bypass the normal tests for masked and caught signals,
1016 * allowing unrecoverable failures to terminate the process without changing
1017 * signal state. Mark the accounting record with the signal termination.
1018 * If dumping core, save the signal number for the debugger. Calls exit and
1019 * does not return.
1020 */
1021 void
1022 sigexit(p, signum)
1023 register struct proc *p;
1024 int signum;
1025 {
1026
1027 p->p_acflag |= AXSIG;
1028 if (sigprop[signum] & SA_CORE) {
1029 p->p_sigacts->ps_sig = signum;
1030 if (coredump(p) == 0)
1031 signum |= WCOREFLAG;
1032 }
1033 exit1(p, W_EXITCODE(0, signum));
1034 /* NOTREACHED */
1035 }
1036
1037 /*
1038 * Dump core, into a file named "progname.core", unless the process was
1039 * setuid/setgid.
1040 */
1041 int
1042 coredump(p)
1043 register struct proc *p;
1044 {
1045 register struct vnode *vp;
1046 register struct vmspace *vm = p->p_vmspace;
1047 register struct ucred *cred = p->p_cred->pc_ucred;
1048 struct nameidata nd;
1049 struct vattr vattr;
1050 int error, error1;
1051 char name[MAXCOMLEN+6]; /* progname.core */
1052 struct core core;
1053
1054 /*
1055 * Make sure the process has not set-id, to prevent data leaks.
1056 */
1057 if (p->p_flag & P_SUGID)
1058 return (EPERM);
1059
1060 /*
1061 * Refuse to core if the data + stack + user size is larger than
1062 * the core dump limit. XXX THIS IS WRONG, because of mapped
1063 * data.
1064 */
1065 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1066 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1067 return (EFBIG); /* better error code? */
1068
1069 /*
1070 * The core dump will go in the current working directory. Make
1071 * sure that mount flags allow us to write core dumps there.
1072 */
1073 if (p->p_fd->fd_cdir->v_mount->mnt_flag & MNT_NOCOREDUMP)
1074 return (EPERM);
1075
1076 sprintf(name, "%s.core", p->p_comm);
1077 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1078 error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
1079 if (error)
1080 return (error);
1081 vp = nd.ni_vp;
1082
1083 /* Don't dump to non-regular files or files with links. */
1084 if (vp->v_type != VREG ||
1085 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1086 error = EINVAL;
1087 goto out;
1088 }
1089 VATTR_NULL(&vattr);
1090 vattr.va_size = 0;
1091 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1092 VOP_SETATTR(vp, &vattr, cred, p);
1093 p->p_acflag |= ACORE;
1094 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1095 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1096
1097 core.c_midmag = 0;
1098 strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1099 core.c_nseg = 0;
1100 core.c_signo = p->p_sigacts->ps_sig;
1101 core.c_ucode = p->p_sigacts->ps_code;
1102 core.c_cpusize = 0;
1103 core.c_tsize = (u_long)ctob(vm->vm_tsize);
1104 core.c_dsize = (u_long)ctob(vm->vm_dsize);
1105 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1106 error = cpu_coredump(p, vp, cred, &core);
1107 if (error)
1108 goto out;
1109 if (core.c_midmag == 0) {
1110 /* XXX
1111 * cpu_coredump() didn't bother to set the magic; assume
1112 * this is a request to do a traditional dump. cpu_coredump()
1113 * is still responsible for setting sensible values in
1114 * the core header.
1115 */
1116 if (core.c_cpusize == 0)
1117 core.c_cpusize = USPACE; /* Just in case */
1118 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1119 (int)core.c_dsize,
1120 (off_t)core.c_cpusize, UIO_USERSPACE,
1121 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1122 if (error)
1123 goto out;
1124 error = vn_rdwr(UIO_WRITE, vp,
1125 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1126 core.c_ssize,
1127 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1128 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1129 } else {
1130 /*
1131 * vm_coredump() spits out all appropriate segments.
1132 * All that's left to do is to write the core header.
1133 */
1134 error = vm_coredump(p, vp, cred, &core);
1135 if (error)
1136 goto out;
1137 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1138 (int)core.c_hdrsize, (off_t)0,
1139 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1140 }
1141 out:
1142 VOP_UNLOCK(vp);
1143 error1 = vn_close(vp, FWRITE, cred, p);
1144 if (error == 0)
1145 error = error1;
1146 return (error);
1147 }
1148
1149 /*
1150 * Nonexistent system call-- signal process (may want to handle it).
1151 * Flag error in case process won't see signal immediately (blocked or ignored).
1152 */
1153 /* ARGSUSED */
1154 int
1155 sys_nosys(p, v, retval)
1156 struct proc *p;
1157 void *v;
1158 register_t *retval;
1159 {
1160
1161 psignal(p, SIGSYS);
1162 return (ENOSYS);
1163 }
1164