kern_sig.c revision 1.69 1 /* $NetBSD: kern_sig.c,v 1.69 1998/02/05 07:59:54 mrg 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 #if defined(UVM)
74 #include <uvm/uvm_extern.h>
75 #endif
76
77 void stop __P((struct proc *p));
78 void killproc __P((struct proc *, char *));
79
80 /*
81 * Can process p, with pcred pc, send the signal signum to process q?
82 */
83 #define CANSIGNAL(p, pc, q, signum) \
84 ((pc)->pc_ucred->cr_uid == 0 || \
85 (pc)->p_ruid == (q)->p_cred->p_ruid || \
86 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
87 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
88 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
89 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
90
91 /* ARGSUSED */
92 int
93 sys_sigaction(p, v, retval)
94 struct proc *p;
95 void *v;
96 register_t *retval;
97 {
98 register struct sys_sigaction_args /* {
99 syscallarg(int) signum;
100 syscallarg(const struct sigaction *) nsa;
101 syscallarg(struct sigaction *) osa;
102 } */ *uap = v;
103 struct sigaction vec;
104 register struct sigaction *sa;
105 register struct sigacts *ps = p->p_sigacts;
106 register int signum;
107 int bit, error;
108
109 signum = SCARG(uap, signum);
110 if (signum <= 0 || signum >= NSIG ||
111 ((signum == SIGKILL || signum == SIGSTOP) && SCARG(uap, nsa)))
112 return (EINVAL);
113 sa = &vec;
114 if (SCARG(uap, osa)) {
115 sa->sa_handler = ps->ps_sigact[signum];
116 sa->sa_mask = ps->ps_catchmask[signum];
117 bit = sigmask(signum);
118 sa->sa_flags = 0;
119 if ((ps->ps_sigonstack & bit) != 0)
120 sa->sa_flags |= SA_ONSTACK;
121 if ((ps->ps_sigintr & bit) == 0)
122 sa->sa_flags |= SA_RESTART;
123 if ((ps->ps_sigreset & bit) != 0)
124 sa->sa_flags |= SA_RESETHAND;
125 if (signum == SIGCHLD) {
126 if ((p->p_flag & P_NOCLDSTOP) != 0)
127 sa->sa_flags |= SA_NOCLDSTOP;
128 }
129 if ((sa->sa_mask & bit) == 0)
130 sa->sa_flags |= SA_NODEFER;
131 sa->sa_mask &= ~bit;
132 error = copyout(sa, SCARG(uap, osa), sizeof (vec));
133 if (error)
134 return (error);
135 }
136 if (SCARG(uap, nsa)) {
137 error = copyin(SCARG(uap, nsa), sa, sizeof (vec));
138 if (error)
139 return (error);
140 setsigvec(p, signum, sa);
141 }
142 return (0);
143 }
144
145 void
146 setsigvec(p, signum, sa)
147 register struct proc *p;
148 int signum;
149 register struct sigaction *sa;
150 {
151 register struct sigacts *ps = p->p_sigacts;
152 register int bit;
153
154 bit = sigmask(signum);
155 /*
156 * Change setting atomically.
157 */
158 (void) splhigh();
159 ps->ps_sigact[signum] = sa->sa_handler;
160 if ((sa->sa_flags & SA_NODEFER) == 0)
161 sa->sa_mask |= sigmask(signum);
162 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
163 if (signum == SIGCHLD) {
164 if (sa->sa_flags & SA_NOCLDSTOP)
165 p->p_flag |= P_NOCLDSTOP;
166 else
167 p->p_flag &= ~P_NOCLDSTOP;
168 }
169 if ((sa->sa_flags & SA_RESETHAND) != 0)
170 ps->ps_sigreset |= bit;
171 else
172 ps->ps_sigreset &= ~bit;
173 if ((sa->sa_flags & SA_RESTART) == 0)
174 ps->ps_sigintr |= bit;
175 else
176 ps->ps_sigintr &= ~bit;
177 if ((sa->sa_flags & SA_ONSTACK) != 0)
178 ps->ps_sigonstack |= bit;
179 else
180 ps->ps_sigonstack &= ~bit;
181 #ifdef COMPAT_SUNOS
182 {
183 extern struct emul emul_sunos;
184 if (p->p_emul == &emul_sunos && sa->sa_flags & SA_USERTRAMP)
185 ps->ps_usertramp |= bit;
186 else
187 ps->ps_usertramp &= ~bit;
188 }
189 #endif
190 /*
191 * Set bit in p_sigignore for signals that are set to SIG_IGN,
192 * and for signals set to SIG_DFL where the default is to ignore.
193 * However, don't put SIGCONT in p_sigignore,
194 * as we have to restart the process.
195 */
196 if (sa->sa_handler == SIG_IGN ||
197 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
198 p->p_siglist &= ~bit; /* never to be seen again */
199 if (signum != SIGCONT)
200 p->p_sigignore |= bit; /* easier in psignal */
201 p->p_sigcatch &= ~bit;
202 } else {
203 p->p_sigignore &= ~bit;
204 if (sa->sa_handler == SIG_DFL)
205 p->p_sigcatch &= ~bit;
206 else
207 p->p_sigcatch |= bit;
208 }
209 (void) spl0();
210 }
211
212 /*
213 * Initialize signal state for process 0;
214 * set to ignore signals that are ignored by default.
215 */
216 void
217 siginit(p)
218 struct proc *p;
219 {
220 register int i;
221
222 for (i = 0; i < NSIG; i++)
223 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
224 p->p_sigignore |= sigmask(i);
225 }
226
227 /*
228 * Reset signals for an exec of the specified process.
229 */
230 void
231 execsigs(p)
232 register struct proc *p;
233 {
234 register struct sigacts *ps = p->p_sigacts;
235 register int nc, mask;
236
237 /*
238 * Reset caught signals. Held signals remain held
239 * through p_sigmask (unless they were caught,
240 * and are now ignored by default).
241 */
242 while (p->p_sigcatch) {
243 nc = ffs((long)p->p_sigcatch);
244 mask = sigmask(nc);
245 p->p_sigcatch &= ~mask;
246 if (sigprop[nc] & SA_IGNORE) {
247 if (nc != SIGCONT)
248 p->p_sigignore |= mask;
249 p->p_siglist &= ~mask;
250 }
251 ps->ps_sigact[nc] = SIG_DFL;
252 }
253 /*
254 * Reset stack state to the user stack.
255 * Clear set of signals caught on the signal stack.
256 */
257 ps->ps_sigstk.ss_flags = SS_DISABLE;
258 ps->ps_sigstk.ss_size = 0;
259 ps->ps_sigstk.ss_sp = 0;
260 ps->ps_flags = 0;
261 }
262
263 /*
264 * Manipulate signal mask.
265 * Note that we receive new mask, not pointer,
266 * and return old mask as return value;
267 * the library stub does the rest.
268 */
269 int
270 sys_sigprocmask(p, v, retval)
271 register struct proc *p;
272 void *v;
273 register_t *retval;
274 {
275 struct sys_sigprocmask_args /* {
276 syscallarg(int) how;
277 syscallarg(sigset_t) mask;
278 } */ *uap = v;
279 int error = 0;
280
281 *retval = p->p_sigmask;
282 (void) splhigh();
283
284 switch (SCARG(uap, how)) {
285 case SIG_BLOCK:
286 p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask;
287 break;
288
289 case SIG_UNBLOCK:
290 p->p_sigmask &= ~SCARG(uap, mask);
291 break;
292
293 case SIG_SETMASK:
294 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
295 break;
296
297 default:
298 error = EINVAL;
299 break;
300 }
301 (void) spl0();
302 return (error);
303 }
304
305 /* ARGSUSED */
306 int
307 sys_sigpending(p, v, retval)
308 struct proc *p;
309 void *v;
310 register_t *retval;
311 {
312
313 *retval = p->p_siglist;
314 return (0);
315 }
316
317 /*
318 * Suspend process until signal, providing mask to be set
319 * in the meantime. Note nonstandard calling convention:
320 * libc stub passes mask, not pointer, to save a copyin.
321 */
322 /* ARGSUSED */
323 int
324 sys_sigsuspend(p, v, retval)
325 register struct proc *p;
326 void *v;
327 register_t *retval;
328 {
329 struct sys_sigsuspend_args /* {
330 syscallarg(int) mask;
331 } */ *uap = v;
332 register struct sigacts *ps = p->p_sigacts;
333
334 /*
335 * When returning from sigpause, we want
336 * the old mask to be restored after the
337 * signal handler has finished. Thus, we
338 * save it here and mark the sigacts structure
339 * to indicate this.
340 */
341 ps->ps_oldmask = p->p_sigmask;
342 ps->ps_flags |= SAS_OLDMASK;
343 p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
344 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
345 /* void */;
346 /* always return EINTR rather than ERESTART... */
347 return (EINTR);
348 }
349
350 /* ARGSUSED */
351 int
352 sys___sigaltstack14(p, v, retval)
353 struct proc *p;
354 void *v;
355 register_t *retval;
356 {
357 register struct sys___sigaltstack14_args /* {
358 syscallarg(const struct sigaltstack *) nss;
359 syscallarg(struct sigaltstack *) oss;
360 } */ *uap = v;
361 struct sigacts *psp;
362 struct sigaltstack ss;
363 int error;
364
365 psp = p->p_sigacts;
366 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
367 psp->ps_sigstk.ss_flags |= SS_DISABLE;
368 if (SCARG(uap, oss) && (error = copyout(&psp->ps_sigstk,
369 SCARG(uap, oss), sizeof (struct sigaltstack))))
370 return (error);
371 if (SCARG(uap, nss) == 0)
372 return (0);
373 error = copyin(SCARG(uap, nss), &ss, sizeof (ss));
374 if (error)
375 return (error);
376 if (ss.ss_flags & SS_DISABLE) {
377 if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
378 return (EINVAL);
379 psp->ps_flags &= ~SAS_ALTSTACK;
380 psp->ps_sigstk.ss_flags = ss.ss_flags;
381 return (0);
382 }
383 if (ss.ss_size < MINSIGSTKSZ)
384 return (ENOMEM);
385 psp->ps_flags |= SAS_ALTSTACK;
386 psp->ps_sigstk= ss;
387 return (0);
388 }
389
390 /* ARGSUSED */
391 int
392 sys_kill(cp, v, retval)
393 register struct proc *cp;
394 void *v;
395 register_t *retval;
396 {
397 register struct sys_kill_args /* {
398 syscallarg(int) pid;
399 syscallarg(int) signum;
400 } */ *uap = v;
401 register struct proc *p;
402 register struct pcred *pc = cp->p_cred;
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 if ((p->p_flag & P_FSTRACE) == 0)
794 psignal(p->p_pptr, SIGCHLD);
795 do {
796 stop(p);
797 mi_switch();
798 } while (!trace_req(p) && p->p_flag & P_TRACED);
799
800 /*
801 * If we are no longer being traced, or the parent
802 * didn't give us a signal, look for more signals.
803 */
804 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
805 continue;
806
807 /*
808 * If the new signal is being masked, look for other
809 * signals.
810 */
811 signum = p->p_xstat;
812 mask = sigmask(signum);
813 if ((p->p_sigmask & mask) != 0)
814 continue;
815 p->p_siglist &= ~mask; /* take the signal! */
816 }
817
818 prop = sigprop[signum];
819
820 /*
821 * Decide whether the signal should be returned.
822 * Return the signal's number, or fall through
823 * to clear it from the pending mask.
824 */
825 switch ((long)p->p_sigacts->ps_sigact[signum]) {
826
827 case (long)SIG_DFL:
828 /*
829 * Don't take default actions on system processes.
830 */
831 if (p->p_pid <= 1) {
832 #ifdef DIAGNOSTIC
833 /*
834 * Are you sure you want to ignore SIGSEGV
835 * in init? XXX
836 */
837 printf("Process (pid %d) got signal %d\n",
838 p->p_pid, signum);
839 #endif
840 break; /* == ignore */
841 }
842 /*
843 * If there is a pending stop signal to process
844 * with default action, stop here,
845 * then clear the signal. However,
846 * if process is member of an orphaned
847 * process group, ignore tty stop signals.
848 */
849 if (prop & SA_STOP) {
850 if (p->p_flag & P_TRACED ||
851 (p->p_pgrp->pg_jobc == 0 &&
852 prop & SA_TTYSTOP))
853 break; /* == ignore */
854 p->p_xstat = signum;
855 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
856 psignal(p->p_pptr, SIGCHLD);
857 stop(p);
858 mi_switch();
859 break;
860 } else if (prop & SA_IGNORE) {
861 /*
862 * Except for SIGCONT, shouldn't get here.
863 * Default action is to ignore; drop it.
864 */
865 break; /* == ignore */
866 } else
867 goto keep;
868 /*NOTREACHED*/
869
870 case (long)SIG_IGN:
871 /*
872 * Masking above should prevent us ever trying
873 * to take action on an ignored signal other
874 * than SIGCONT, unless process is traced.
875 */
876 if ((prop & SA_CONT) == 0 &&
877 (p->p_flag & P_TRACED) == 0)
878 printf("issignal\n");
879 break; /* == ignore */
880
881 default:
882 /*
883 * This signal has an action, let
884 * postsig() process it.
885 */
886 goto keep;
887 }
888 }
889 /* NOTREACHED */
890
891 keep:
892 p->p_siglist |= mask; /* leave the signal for later */
893 return (signum);
894 }
895
896 /*
897 * Put the argument process into the stopped state and notify the parent
898 * via wakeup. Signals are handled elsewhere. The process must not be
899 * on the run queue.
900 */
901 void
902 stop(p)
903 register struct proc *p;
904 {
905
906 p->p_stat = SSTOP;
907 p->p_flag &= ~P_WAITED;
908 wakeup((caddr_t)p->p_pptr);
909 }
910
911 /*
912 * Take the action for the specified signal
913 * from the current set of pending signals.
914 */
915 void
916 postsig(signum)
917 register int signum;
918 {
919 register struct proc *p = curproc;
920 register struct sigacts *ps = p->p_sigacts;
921 register sig_t action;
922 u_long code;
923 int mask, returnmask;
924
925 #ifdef DIAGNOSTIC
926 if (signum == 0)
927 panic("postsig");
928 #endif
929 mask = sigmask(signum);
930 p->p_siglist &= ~mask;
931 action = ps->ps_sigact[signum];
932 #ifdef KTRACE
933 if (KTRPOINT(p, KTR_PSIG))
934 ktrpsig(p->p_tracep,
935 signum, action, ps->ps_flags & SAS_OLDMASK ?
936 ps->ps_oldmask : p->p_sigmask, 0);
937 #endif
938 if (action == SIG_DFL) {
939 /*
940 * Default action, where the default is to kill
941 * the process. (Other cases were ignored above.)
942 */
943 sigexit(p, signum);
944 /* NOTREACHED */
945 } else {
946 /*
947 * If we get here, the signal must be caught.
948 */
949 #ifdef DIAGNOSTIC
950 if (action == SIG_IGN || (p->p_sigmask & mask))
951 panic("postsig action");
952 #endif
953 /*
954 * Set the new mask value and also defer further
955 * occurences of this signal.
956 *
957 * Special case: user has done a sigpause. Here the
958 * current mask is not of interest, but rather the
959 * mask from before the sigpause is what we want
960 * restored after the signal processing is completed.
961 */
962 (void) splhigh();
963 if (ps->ps_flags & SAS_OLDMASK) {
964 returnmask = ps->ps_oldmask;
965 ps->ps_flags &= ~SAS_OLDMASK;
966 } else
967 returnmask = p->p_sigmask;
968 p->p_sigmask |= ps->ps_catchmask[signum];
969 if ((ps->ps_sigreset & mask) != 0) {
970 p->p_sigcatch &= ~mask;
971 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
972 p->p_sigignore |= mask;
973 ps->ps_sigact[signum] = SIG_DFL;
974 }
975 (void) spl0();
976 p->p_stats->p_ru.ru_nsignals++;
977 if (ps->ps_sig != signum) {
978 code = 0;
979 } else {
980 code = ps->ps_code;
981 ps->ps_code = 0;
982 }
983 (*p->p_emul->e_sendsig)(action, signum, returnmask, code);
984 }
985 }
986
987 /*
988 * Kill the current process for stated reason.
989 */
990 void
991 killproc(p, why)
992 struct proc *p;
993 char *why;
994 {
995
996 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
997 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
998 psignal(p, SIGKILL);
999 }
1000
1001 /*
1002 * Force the current process to exit with the specified signal, dumping core
1003 * if appropriate. We bypass the normal tests for masked and caught signals,
1004 * allowing unrecoverable failures to terminate the process without changing
1005 * signal state. Mark the accounting record with the signal termination.
1006 * If dumping core, save the signal number for the debugger. Calls exit and
1007 * does not return.
1008 */
1009 void
1010 sigexit(p, signum)
1011 register struct proc *p;
1012 int signum;
1013 {
1014
1015 p->p_acflag |= AXSIG;
1016 if (sigprop[signum] & SA_CORE) {
1017 p->p_sigacts->ps_sig = signum;
1018 if (coredump(p) == 0)
1019 signum |= WCOREFLAG;
1020 }
1021 exit1(p, W_EXITCODE(0, signum));
1022 /* NOTREACHED */
1023 }
1024
1025 /*
1026 * Dump core, into a file named "progname.core", unless the process was
1027 * setuid/setgid.
1028 */
1029 int
1030 coredump(p)
1031 register struct proc *p;
1032 {
1033 register struct vnode *vp;
1034 register struct vmspace *vm = p->p_vmspace;
1035 register struct ucred *cred = p->p_cred->pc_ucred;
1036 struct nameidata nd;
1037 struct vattr vattr;
1038 int error, error1;
1039 char name[MAXCOMLEN+6]; /* progname.core */
1040 struct core core;
1041
1042 /*
1043 * Make sure the process has not set-id, to prevent data leaks.
1044 */
1045 if (p->p_flag & P_SUGID)
1046 return (EPERM);
1047
1048 /*
1049 * Refuse to core if the data + stack + user size is larger than
1050 * the core dump limit. XXX THIS IS WRONG, because of mapped
1051 * data.
1052 */
1053 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1054 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1055 return (EFBIG); /* better error code? */
1056
1057 /*
1058 * The core dump will go in the current working directory. Make
1059 * sure that mount flags allow us to write core dumps there.
1060 */
1061 if (p->p_fd->fd_cdir->v_mount->mnt_flag & MNT_NOCOREDUMP)
1062 return (EPERM);
1063
1064 sprintf(name, "%s.core", p->p_comm);
1065 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1066 error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
1067 if (error)
1068 return (error);
1069 vp = nd.ni_vp;
1070
1071 /* Don't dump to non-regular files or files with links. */
1072 if (vp->v_type != VREG ||
1073 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1074 error = EINVAL;
1075 goto out;
1076 }
1077 VATTR_NULL(&vattr);
1078 vattr.va_size = 0;
1079 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1080 VOP_SETATTR(vp, &vattr, cred, p);
1081 p->p_acflag |= ACORE;
1082 #if 0
1083 /*
1084 * XXX
1085 * It would be nice if we at least dumped the signal state (and made it
1086 * available at run time to the debugger, as well), but this code
1087 * hasn't actually had any effect for a long time, since we don't dump
1088 * the user area. For now, it's dead.
1089 */
1090 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1091 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1092 #endif
1093
1094 core.c_midmag = 0;
1095 strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1096 core.c_nseg = 0;
1097 core.c_signo = p->p_sigacts->ps_sig;
1098 core.c_ucode = p->p_sigacts->ps_code;
1099 core.c_cpusize = 0;
1100 core.c_tsize = (u_long)ctob(vm->vm_tsize);
1101 core.c_dsize = (u_long)ctob(vm->vm_dsize);
1102 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1103 error = cpu_coredump(p, vp, cred, &core);
1104 if (error)
1105 goto out;
1106 if (core.c_midmag == 0) {
1107 /* XXX
1108 * cpu_coredump() didn't bother to set the magic; assume
1109 * this is a request to do a traditional dump. cpu_coredump()
1110 * is still responsible for setting sensible values in
1111 * the core header.
1112 */
1113 if (core.c_cpusize == 0)
1114 core.c_cpusize = USPACE; /* Just in case */
1115 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1116 (int)core.c_dsize,
1117 (off_t)core.c_cpusize, UIO_USERSPACE,
1118 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1119 if (error)
1120 goto out;
1121 error = vn_rdwr(UIO_WRITE, vp,
1122 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1123 core.c_ssize,
1124 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1125 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1126 } else {
1127 /*
1128 * vm_coredump() spits out all appropriate segments.
1129 * All that's left to do is to write the core header.
1130 */
1131 #if defined(UVM)
1132 error = uvm_coredump(p, vp, cred, &core);
1133 #else
1134 error = vm_coredump(p, vp, cred, &core);
1135 #endif
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