Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / doscmd / signal.c
1 /*
2  * Copyright (c) 1992, 1993, 1996
3  *      Berkeley Software Design, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Berkeley Software
16  *      Design, Inc.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      BSDI signal.c,v 2.2 1996/04/08 19:33:06 bostic Exp
31  *
32  * $FreeBSD: src/usr.bin/doscmd/signal.c,v 1.6.2.1 2002/04/25 11:04:51 tg Exp $
33  * $DragonFly: src/usr.bin/doscmd/signal.c,v 1.2 2003/06/17 04:29:26 dillon Exp $
34  */
35
36 #include "doscmd.h"
37 #include "trap.h"
38
39 static void     (*handler[NSIG])(struct sigframe *);
40 static char     signal_stack[16 * 1024];
41 #define PSS(w) { char s; printf(w " @ %08x\n", (signal_stack + sizeof signal_stack) - &s); }
42
43 struct sigframe *saved_sigframe;
44 regcontext_t *saved_regcontext;
45 int             saved_valid = 0; 
46
47 static void
48 sanity_check(struct sigframe *sf __unused)
49 {
50 #if 0
51     static sigset_t oset;
52     int i;
53
54     for (i = 1; i < 32; ++i) {
55         if (sigismember(&sf->sf_sc.sc_mask, i) != sigismember(&oset, i))
56             fprintf(debugf, "Signal %s %s being blocked\n",
57                     sys_signame[i],
58                     sigismember(&sf->sf_sc.sc_mask, i) ? "now" : "no longer");
59     }
60     oset = sf->sf_sc.sc_mask;
61 #endif
62
63     if (dead)
64         fatal("attempting to return to vm86 while dead");
65 }
66
67 #if defined(__FreeBSD__) || defined(USE_VM86)
68 static void
69 generichandler(struct sigframe sf)
70 {
71     if (sf.sf_uc.uc_mcontext.mc_eflags & PSL_VM) {
72         saved_sigframe = &sf;
73         saved_regcontext = (regcontext_t *)&(sf.sf_uc.uc_mcontext);
74         saved_valid = 1;
75         if (handler[sf.sf_signum])
76             (*handler[sf.sf_signum])(&sf);
77         saved_valid = 0;
78         sanity_check(&sf);
79     } else {
80         if (handler[sf.sf_signum])
81             (*handler[sf.sf_signum])(&sf);
82     }
83 }
84 #else
85 #error BSD/OS sigframe/trapframe kernel interface not currently supported.
86 #endif
87
88 void setsignal(int s, void (*h)(struct sigframe *))
89 {
90     static int first = 1;
91     struct sigaction sa;
92
93     if (first) {
94         struct sigaltstack sstack;
95
96         sstack.ss_sp = signal_stack;
97         sstack.ss_size = sizeof signal_stack;
98         sstack.ss_flags = 0;
99         sigaltstack (&sstack, NULL);
100         first = 0;
101     }
102
103     if (s >= 0 && s < NSIG) {
104         handler[s] = h;
105
106         sa.sa_handler = (__sighandler_t *)generichandler;
107         sigemptyset(&sa.sa_mask);
108         sigaddset(&sa.sa_mask, SIGIO);
109         sigaddset(&sa.sa_mask, SIGALRM);
110         sa.sa_flags = SA_ONSTACK;
111         sigaction(s, &sa, NULL);
112     }
113 }