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