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