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