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