Release 980413
[wine] / loader / signal.c
1 /*
2  * Wine signal handling
3  *
4  */
5
6 #include "config.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <time.h>
14 #include <setjmp.h>
15
16 #include <sys/time.h>
17 #include <sys/timeb.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20
21 #ifdef HAVE_SYS_PARAM_H
22 # include <sys/param.h>
23 #endif
24 #ifdef HAVE_SYSCALL_H
25 # include <syscall.h>
26 #else
27 # ifdef HAVE_SYS_SYSCALL_H
28 #  include <sys/syscall.h>
29 # endif
30 #endif
31
32 #include "miscemu.h"
33 #include "selectors.h"
34 #include "sig_context.h"
35 #include "winsock.h"
36
37
38 /* Linux sigaction function */
39
40 #if defined(linux) && defined(__i386__)
41 /* This is the sigaction structure from the Linux 2.1.20 kernel.  */
42 #undef sa_handler
43 struct kernel_sigaction
44 {
45     void (*sa_handler)();
46     unsigned long sa_mask;
47     unsigned long sa_flags;
48     void (*sa_restorer)();
49 };
50
51 /* Similar to the sigaction function in libc, except it leaves alone the
52    restorer field, which is used to specify the signal stack address */
53 static __inline__ int wine_sigaction( int sig, struct kernel_sigaction *new,
54                                       struct kernel_sigaction *old )
55 {
56 #ifdef __PIC__
57     __asm__ __volatile__( "pushl %%ebx\n\t"
58                           "movl %2,%%ebx\n\t"
59                           "int $0x80\n\t"
60                           "popl %%ebx"
61                           : "=a" (sig)
62                           : "0" (SYS_sigaction),
63                             "g" (sig),
64                             "c" (new),
65                             "d" (old) );
66 #else
67     __asm__ __volatile__( "int $0x80"
68                           : "=a" (sig)
69                           : "0" (SYS_sigaction),
70                             "b" (sig),
71                             "c" (new),
72                             "d" (old) );
73 #endif  /* __PIC__ */
74     if (sig>=0)
75         return 0;
76     errno = -sig;
77     return -1;
78 }
79 #endif /* linux && __i386__ */
80
81 /* Signal stack */
82
83 static char SIGNAL_Stack[16384];
84 static sigset_t async_signal_set;
85
86 /**********************************************************************
87  *              SIGNAL_child
88  * 
89  * wait4 terminated child processes
90  */
91 static HANDLER_DEF(SIGNAL_child)
92 {
93     HANDLER_INIT();
94 #ifdef HAVE_WAIT4
95     wait4( 0, NULL, WNOHANG, NULL);
96 #elif defined (HAVE_WAITPID)
97     /* I am sort-of guessing that this is the same as the wait4 call.  */
98     waitpid (0, NULL, WNOHANG);
99 #else
100     wait(NULL);
101 #endif
102 }
103
104
105 /**********************************************************************
106  *              SIGNAL_SetHandler
107  */
108 void SIGNAL_SetHandler( int sig, void (*func)(), int flags )
109 {
110     int ret;
111
112 #if defined(linux) && defined(__i386__)
113
114     struct kernel_sigaction sig_act;
115     sig_act.sa_handler = func;
116     sig_act.sa_flags   = SA_RESTART | (flags) ? SA_NOMASK : 0;
117     sig_act.sa_mask    = 0;
118     /* Point to the top of the stack, minus 4 just in case, and make
119        it aligned  */
120     sig_act.sa_restorer = 
121         (void (*)())((int)(SIGNAL_Stack + sizeof(SIGNAL_Stack) - 4) & ~3);
122     ret = wine_sigaction( sig, &sig_act, NULL );
123
124 #else  /* linux && __i386__ */
125
126     struct sigaction sig_act;
127     sig_act.sa_handler = func;
128     sigemptyset( &sig_act.sa_mask );
129
130 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
131     sig_act.sa_flags = SA_ONSTACK;
132 # elif defined (__svr4__) || defined(_SCO_DS)
133     sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
134 # elif defined(__EMX__)
135     sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
136 # else
137     sig_act.sa_flags = 0;
138 # endif
139     ret = sigaction( sig, &sig_act, NULL );
140
141 #endif  /* linux && __i386__ */
142
143     if (ret < 0)
144     {
145         perror( "sigaction" );
146         exit(1);
147     }
148 }
149
150 extern void stop_wait(int a);
151 extern void WINSOCK_sigio(int a);
152
153
154 /**********************************************************************
155  *              SIGNAL_Init
156  */
157 BOOL32 SIGNAL_Init(void)
158 {
159     extern void SYNC_SetupSignals(void);
160
161 #ifdef HAVE_SIGALTSTACK
162     struct sigaltstack ss;
163     ss.ss_sp    = SIGNAL_Stack;
164     ss.ss_size  = sizeof(SIGNAL_Stack);
165     ss.ss_flags = 0;
166     if (sigaltstack(&ss, NULL) < 0)
167     {
168         perror("sigstack");
169         return FALSE;
170     }
171 #endif  /* HAVE_SIGALTSTACK */
172     
173     sigemptyset(&async_signal_set);
174
175     SIGNAL_SetHandler( SIGCHLD, (void (*)())SIGNAL_child, 1);
176 #ifdef CONFIG_IPC
177     sigaddset(&async_signal_set, SIGUSR2);
178     SIGNAL_SetHandler( SIGUSR2, (void (*)())stop_wait, 1);      /* For IPC */
179 #endif
180 #ifdef SIGIO
181     sigaddset(&async_signal_set, SIGIO);
182     SIGNAL_SetHandler( SIGIO,   (void (*)())WINSOCK_sigio, 0); 
183 #endif
184
185     sigaddset(&async_signal_set, SIGALRM);
186
187     /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead  */
188     signal (SIGPIPE, SIG_IGN);
189
190     SYNC_SetupSignals();
191     return TRUE;
192 }
193
194
195 /**********************************************************************
196  *              SIGNAL_MaskAsyncEvents
197  */
198 void SIGNAL_MaskAsyncEvents( BOOL32 flag )
199 {
200   sigprocmask( (flag) ? SIG_BLOCK : SIG_UNBLOCK , &async_signal_set, NULL);
201 }
202