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