Ensure that the WM_ENTERIDLE message is not sent if the wake-up event
[wine] / loader / signal.c
1 /*
2  * Wine signal handling
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <errno.h>
14 #include <time.h>
15 #include <setjmp.h>
16
17 #include <sys/time.h>
18 #include <sys/timeb.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21
22 #ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
24 #endif
25 #ifdef HAVE_SYSCALL_H
26 # include <syscall.h>
27 #else
28 # ifdef HAVE_SYS_SYSCALL_H
29 #  include <sys/syscall.h>
30 # endif
31 #endif
32
33 #include "winsock.h"
34 #include "global.h"
35 #include "options.h"
36 #include "miscemu.h"
37 #include "dosexe.h"
38 #include "thread.h"
39 #include "wine/exception.h"
40 #include "debugtools.h"
41
42
43 /* Linux sigaction function */
44
45 #if defined(linux) && defined(__i386__)
46 /* This is the sigaction structure from the Linux 2.1.20 kernel.  */
47
48 #undef sa_handler
49 struct kernel_sigaction
50 {
51     void (*sa_handler)();
52     unsigned long sa_mask;
53     unsigned long sa_flags;
54     void (*sa_restorer)();
55 };
56
57 /* Similar to the sigaction function in libc, except it leaves alone the
58    restorer field, which is used to specify the signal stack address */
59 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
60                                       struct kernel_sigaction *old )
61 {
62 #ifdef __PIC__
63     __asm__ __volatile__( "pushl %%ebx\n\t"
64                           "movl %2,%%ebx\n\t"
65                           "int $0x80\n\t"
66                           "popl %%ebx"
67                           : "=a" (sig)
68                           : "0" (SYS_sigaction),
69                             "r" (sig),
70                             "c" (new),
71                             "d" (old) );
72 #else
73     __asm__ __volatile__( "int $0x80"
74                           : "=a" (sig)
75                           : "0" (SYS_sigaction),
76                             "b" (sig),
77                             "c" (new),
78                             "d" (old) );
79 #endif  /* __PIC__ */
80     if (sig>=0)
81         return 0;
82     errno = -sig;
83     return -1;
84 }
85 #endif /* linux && __i386__ */
86
87 /* Signal stack */
88
89 static char SIGNAL_Stack[16384];
90
91
92 /**********************************************************************
93  *              SIGNAL_SetHandler
94  */
95 void SIGNAL_SetHandler( int sig, void (*func)() )
96 {
97     int ret;
98
99 #if defined(linux) && defined(__i386__)
100
101     struct kernel_sigaction sig_act;
102     sig_act.sa_handler = func;
103     sig_act.sa_flags   = SA_RESTART | SA_NOMASK;
104     sig_act.sa_mask    = 0;
105     /* Point to the top of the stack, minus 4 just in case, and make
106        it aligned  */
107     sig_act.sa_restorer = 
108         (void (*)())((int)(SIGNAL_Stack + sizeof(SIGNAL_Stack) - 4) & ~3);
109     ret = wine_sigaction( sig, &sig_act, NULL );
110
111 #else  /* linux && __i386__ */
112
113     struct sigaction sig_act;
114     sig_act.sa_handler = func;
115     sigemptyset( &sig_act.sa_mask );
116
117 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
118     sig_act.sa_flags = SA_ONSTACK;
119 # elif defined (__svr4__) || defined(_SCO_DS)
120     sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
121 # elif defined(__EMX__)
122     sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
123 # else
124     sig_act.sa_flags = 0;
125 # endif
126     ret = sigaction( sig, &sig_act, NULL );
127
128 #endif  /* linux && __i386__ */
129
130     if (ret < 0)
131     {
132         perror( "sigaction" );
133         exit(1);
134     }
135 }
136
137
138 /**********************************************************************
139  *              SIGNAL_Init
140  */
141 BOOL SIGNAL_Init(void)
142 {
143 #ifdef HAVE_WORKING_SIGALTSTACK
144     struct sigaltstack ss;
145     ss.ss_sp    = SIGNAL_Stack;
146     ss.ss_size  = sizeof(SIGNAL_Stack);
147     ss.ss_flags = 0;
148     if (sigaltstack(&ss, NULL) < 0)
149     {
150         perror("sigaltstack");
151         /* fall through on error and try it differently */
152     }
153 #endif  /* HAVE_SIGALTSTACK */
154     
155     /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead  */
156     signal (SIGPIPE, SIG_IGN);
157     /* automatic child reaping to avoid zombies */
158     signal (SIGCHLD, SIG_IGN);
159     EXC_InitHandlers();
160     return TRUE;
161 }