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