Solaris compile fix.
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blob9964d4d4b9d7584103727d5f16bd96797b7b5e31
1 /*
2 * i386 signal handling routines
3 *
4 * Copyright 1999 Alexandre Julliard
5 */
7 #ifdef __i386__
9 #include "config.h"
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdlib.h>
14 #include <unistd.h>
16 #ifdef HAVE_SYS_PARAM_H
17 # include <sys/param.h>
18 #endif
19 #ifdef HAVE_SYSCALL_H
20 # include <syscall.h>
21 #else
22 # ifdef HAVE_SYS_SYSCALL_H
23 # include <sys/syscall.h>
24 # endif
25 #endif
27 /***********************************************************************
28 * signal context platform-specific definitions
31 #ifdef linux
32 typedef struct
34 unsigned short sc_gs, __gsh;
35 unsigned short sc_fs, __fsh;
36 unsigned short sc_es, __esh;
37 unsigned short sc_ds, __dsh;
38 unsigned long sc_edi;
39 unsigned long sc_esi;
40 unsigned long sc_ebp;
41 unsigned long sc_esp;
42 unsigned long sc_ebx;
43 unsigned long sc_edx;
44 unsigned long sc_ecx;
45 unsigned long sc_eax;
46 unsigned long sc_trapno;
47 unsigned long sc_err;
48 unsigned long sc_eip;
49 unsigned short sc_cs, __csh;
50 unsigned long sc_eflags;
51 unsigned long esp_at_signal;
52 unsigned short sc_ss, __ssh;
53 unsigned long i387;
54 unsigned long oldmask;
55 unsigned long cr2;
56 } SIGCONTEXT;
58 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
59 #define HANDLER_CONTEXT (&__context)
61 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
62 #undef sa_handler
63 struct kernel_sigaction
65 void (*sa_handler)();
66 unsigned long sa_mask;
67 unsigned long sa_flags;
68 void *sa_restorer;
71 /* Similar to the sigaction function in libc, except it leaves alone the
72 restorer field, which is used to specify the signal stack address */
73 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
74 struct kernel_sigaction *old )
76 __asm__ __volatile__( "pushl %%ebx\n\t"
77 "movl %2,%%ebx\n\t"
78 "int $0x80\n\t"
79 "popl %%ebx"
80 : "=a" (sig)
81 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
82 if (sig>=0) return 0;
83 errno = -sig;
84 return -1;
87 #endif /* linux */
89 #ifdef BSDI
91 #define EAX_sig(context) ((context)->tf_eax)
92 #define EBX_sig(context) ((context)->tf_ebx)
93 #define ECX_sig(context) ((context)->tf_ecx)
94 #define EDX_sig(context) ((context)->tf_edx)
95 #define ESI_sig(context) ((context)->tf_esi)
96 #define EDI_sig(context) ((context)->tf_edi)
97 #define EBP_sig(context) ((context)->tf_ebp)
99 #define CS_sig(context) ((context)->tf_cs)
100 #define DS_sig(context) ((context)->tf_ds)
101 #define ES_sig(context) ((context)->tf_es)
102 #define SS_sig(context) ((context)->tf_ss)
104 #include <machine/frame.h>
105 typedef struct trapframe SIGCONTEXT;
107 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
108 #define HANDLER_CONTEXT __context
110 #define EFL_sig(context) ((context)->tf_eflags)
112 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
113 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
115 #endif /* bsdi */
117 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
119 #include <signal.h>
120 typedef struct sigcontext SIGCONTEXT;
122 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
123 #define HANDLER_CONTEXT __context
125 #endif /* FreeBSD */
127 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
129 #include <signal.h>
130 #ifdef _SCO_DS
131 #include <sys/regset.h>
132 #endif
133 /* Solaris kludge */
134 #undef ERR
135 #include <sys/ucontext.h>
136 #undef ERR
137 typedef struct ucontext SIGCONTEXT;
139 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
140 #define HANDLER_CONTEXT __context
142 #endif /* svr4 || SCO_DS */
144 #ifdef __EMX__
146 typedef struct
148 unsigned long ContextFlags;
149 FLOATING_SAVE_AREA sc_float;
150 unsigned long sc_gs;
151 unsigned long sc_fs;
152 unsigned long sc_es;
153 unsigned long sc_ds;
154 unsigned long sc_edi;
155 unsigned long sc_esi;
156 unsigned long sc_eax;
157 unsigned long sc_ebx;
158 unsigned long sc_ecx;
159 unsigned long sc_edx;
160 unsigned long sc_ebp;
161 unsigned long sc_eip;
162 unsigned long sc_cs;
163 unsigned long sc_eflags;
164 unsigned long sc_esp;
165 unsigned long sc_ss;
166 } SIGCONTEXT;
168 #endif /* __EMX__ */
171 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
173 #define EAX_sig(context) ((context)->sc_eax)
174 #define EBX_sig(context) ((context)->sc_ebx)
175 #define ECX_sig(context) ((context)->sc_ecx)
176 #define EDX_sig(context) ((context)->sc_edx)
177 #define ESI_sig(context) ((context)->sc_esi)
178 #define EDI_sig(context) ((context)->sc_edi)
179 #define EBP_sig(context) ((context)->sc_ebp)
181 #define CS_sig(context) ((context)->sc_cs)
182 #define DS_sig(context) ((context)->sc_ds)
183 #define ES_sig(context) ((context)->sc_es)
184 #define SS_sig(context) ((context)->sc_ss)
186 #ifdef linux
187 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
188 * saved by the exception handling. duh.
190 #define FS_sig(context) ((context)->sc_fs)
191 #define GS_sig(context) ((context)->sc_gs)
192 #define CR2_sig(context) ((context)->cr2)
193 #define TRAP_sig(context) ((context)->sc_trapno)
194 #define ERROR_sig(context) ((context)->sc_err)
195 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
196 #endif
198 #ifndef __FreeBSD__
199 #define EFL_sig(context) ((context)->sc_eflags)
200 #else
201 #define EFL_sig(context) ((context)->sc_efl)
202 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
203 #define CR2_sig(context) ((context)->sc_err)
204 #define TRAP_sig(context) ((context)->sc_trapno)
205 #endif
207 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
208 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
210 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
212 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
214 #ifdef _SCO_DS
215 #define gregs regs
216 #endif
218 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
219 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
220 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
221 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
222 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
223 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
224 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
226 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
227 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
228 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
229 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
231 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
232 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
234 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
236 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
237 #ifdef R_ESP
238 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
239 #else
240 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
241 #endif
242 #ifdef TRAPNO
243 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
244 #endif
246 #endif /* svr4 || SCO_DS */
249 /* exception code definitions (already defined by FreeBSD) */
250 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
251 #define T_DIVIDE 0 /* Division by zero exception */
252 #define T_TRCTRAP 1 /* Single-step exception */
253 #define T_NMI 2 /* NMI interrupt */
254 #define T_BPTFLT 3 /* Breakpoint exception */
255 #define T_OFLOW 4 /* Overflow exception */
256 #define T_BOUND 5 /* Bound range exception */
257 #define T_PRIVINFLT 6 /* Invalid opcode exception */
258 #define T_DNA 7 /* Device not available exception */
259 #define T_DOUBLEFLT 8 /* Double fault exception */
260 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
261 #define T_TSSFLT 10 /* Invalid TSS exception */
262 #define T_SEGNPFLT 11 /* Segment not present exception */
263 #define T_STKFLT 12 /* Stack fault */
264 #define T_PROTFLT 13 /* General protection fault */
265 #define T_PAGEFLT 14 /* Page fault */
266 #define T_RESERVED 15 /* Unknown exception */
267 #define T_ARITHTRAP 16 /* Floating point exception */
268 #define T_ALIGNFLT 17 /* Alignment check exception */
269 #define T_MCHK 18 /* Machine check exception */
270 #define T_CACHEFLT 19 /* Cache flush exception */
271 #endif
273 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
275 #include "winnt.h"
276 #include "stackframe.h"
277 #include "global.h"
278 #include "miscemu.h"
279 #include "ntddk.h"
280 #include "syslevel.h"
281 #include "debugtools.h"
283 DEFAULT_DEBUG_CHANNEL(seh)
286 extern void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context );
289 /***********************************************************************
290 * handler_init
292 * Initialization code for a signal handler.
293 * Restores the proper %fs value for the current thread.
295 static inline void handler_init( CONTEXT *context, const SIGCONTEXT *sigcontext )
297 WORD fs;
298 /* get %fs at time of the fault */
299 #ifdef FS_sig
300 fs = FS_sig(sigcontext);
301 #else
302 GET_FS(fs);
303 #endif
304 context->SegFs = fs;
305 /* now restore a proper %fs for the fault handler */
306 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) fs = SYSLEVEL_Win16CurrentTeb;
307 if (!fs) fs = SYSLEVEL_EmergencyTeb;
308 SET_FS(fs);
311 /***********************************************************************
312 * get_trap_code
314 * Get the trap code for a signal.
316 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
318 #ifdef TRAP_sig
319 return TRAP_sig(sigcontext);
320 #else
321 return T_UNKNOWN; /* unknown trap code */
322 #endif
325 /***********************************************************************
326 * save_context
328 * Set the register values from a sigcontext.
330 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
332 context->Eax = EAX_sig(sigcontext);
333 context->Ebx = EBX_sig(sigcontext);
334 context->Ecx = ECX_sig(sigcontext);
335 context->Edx = EDX_sig(sigcontext);
336 context->Esi = ESI_sig(sigcontext);
337 context->Edi = EDI_sig(sigcontext);
338 context->Ebp = EBP_sig(sigcontext);
339 context->EFlags = EFL_sig(sigcontext);
340 context->Eip = EIP_sig(sigcontext);
341 context->Esp = ESP_sig(sigcontext);
342 context->SegCs = LOWORD(CS_sig(sigcontext));
343 context->SegDs = LOWORD(DS_sig(sigcontext));
344 context->SegEs = LOWORD(ES_sig(sigcontext));
345 context->SegSs = LOWORD(SS_sig(sigcontext));
346 /* %fs already handled in handler_init */
347 #ifdef GS_sig
348 context->SegGs = LOWORD(GS_sig(sigcontext));
349 #else
350 GET_GS( context->SegGs );
351 context->SegGs &= 0xffff;
352 #endif
353 if (ISV86(context)) V86BASE(context) = (DWORD)DOSMEM_MemoryBase(0);
357 /***********************************************************************
358 * restore_context
360 * Build a sigcontext from the register values.
362 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
364 EAX_sig(sigcontext) = context->Eax;
365 EBX_sig(sigcontext) = context->Ebx;
366 ECX_sig(sigcontext) = context->Ecx;
367 EDX_sig(sigcontext) = context->Edx;
368 ESI_sig(sigcontext) = context->Esi;
369 EDI_sig(sigcontext) = context->Edi;
370 EBP_sig(sigcontext) = context->Ebp;
371 EFL_sig(sigcontext) = context->EFlags;
372 EIP_sig(sigcontext) = context->Eip;
373 ESP_sig(sigcontext) = context->Esp;
374 CS_sig(sigcontext) = context->SegCs;
375 DS_sig(sigcontext) = context->SegDs;
376 ES_sig(sigcontext) = context->SegEs;
377 SS_sig(sigcontext) = context->SegSs;
378 #ifdef FS_sig
379 FS_sig(sigcontext) = context->SegFs;
380 #else
381 SET_FS( context->SegFs );
382 #endif
383 #ifdef GS_sig
384 GS_sig(sigcontext) = context->SegGs;
385 #else
386 SET_GS( context->SegGs );
387 #endif
391 /***********************************************************************
392 * save_fpu
394 * Set the FPU context from a sigcontext.
396 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
398 #ifdef FPU_sig
399 if (FPU_sig(sigcontext))
401 context->FloatSave = *FPU_sig(sigcontext);
402 return;
404 #endif /* FPU_sig */
405 #ifdef __GNUC__
406 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
407 #endif /* __GNUC__ */
411 /***********************************************************************
412 * restore_fpu
414 * Restore the FPU context to a sigcontext.
416 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
418 #ifdef FPU_sig
419 if (FPU_sig(sigcontext))
421 *FPU_sig(sigcontext) = context->FloatSave;
422 return;
424 #endif /* FPU_sig */
425 #ifdef __GNUC__
426 /* avoid nested exceptions */
427 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
428 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
429 #endif /* __GNUC__ */
433 /**********************************************************************
434 * get_fpu_code
436 * Get the FPU exception code from the FPU status.
438 static inline DWORD get_fpu_code( const CONTEXT *context )
440 DWORD status = context->FloatSave.StatusWord;
442 if (status & 0x01) /* IE */
444 if (status & 0x40) /* SF */
445 return EXCEPTION_FLT_STACK_CHECK;
446 else
447 return EXCEPTION_FLT_INVALID_OPERATION;
449 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
450 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
451 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
452 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
453 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
454 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
458 /**********************************************************************
459 * segv_handler
461 * Handler for SIGSEGV and related errors.
463 static HANDLER_DEF(segv_handler)
465 EXCEPTION_RECORD rec;
466 CONTEXT context;
468 handler_init( &context, HANDLER_CONTEXT );
470 #ifdef CR2_sig
471 /* we want the page-fault case to be fast */
472 if (get_trap_code(HANDLER_CONTEXT) == T_PAGEFLT)
473 if (VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) )) return;
474 #endif
476 save_context( &context, HANDLER_CONTEXT );
477 rec.ExceptionRecord = NULL;
478 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
479 rec.ExceptionAddress = (LPVOID)context.Eip;
480 rec.NumberParameters = 0;
482 switch(get_trap_code(HANDLER_CONTEXT))
484 case T_OFLOW: /* Overflow exception */
485 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
486 break;
487 case T_BOUND: /* Bound range exception */
488 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
489 break;
490 case T_PRIVINFLT: /* Invalid opcode exception */
491 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
492 break;
493 case T_STKFLT: /* Stack fault */
494 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
495 break;
496 case T_SEGNPFLT: /* Segment not present exception */
497 case T_PROTFLT: /* General protection fault */
498 case T_UNKNOWN: /* Unknown fault code */
499 if (INSTR_EmulateInstruction( &context )) goto restore;
500 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
501 break;
502 case T_PAGEFLT: /* Page fault */
503 #ifdef CR2_sig
504 rec.NumberParameters = 2;
505 #ifdef ERROR_sig
506 rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
507 #else
508 rec.ExceptionInformation[0] = 0;
509 #endif /* ERROR_sig */
510 rec.ExceptionInformation[1] = CR2_sig(HANDLER_CONTEXT);
511 #endif /* CR2_sig */
512 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
513 break;
514 case T_ALIGNFLT: /* Alignment check exception */
515 /* FIXME: pass through exception handler first? */
516 if (context.EFlags & 0x00040000)
518 /* Disable AC flag, return */
519 context.EFlags &= ~0x00040000;
520 goto restore;
522 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
523 break;
524 default:
525 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
526 /* fall through */
527 case T_NMI: /* NMI interrupt */
528 case T_DNA: /* Device not available exception */
529 case T_DOUBLEFLT: /* Double fault exception */
530 case T_TSSFLT: /* Invalid TSS exception */
531 case T_RESERVED: /* Unknown exception */
532 case T_MCHK: /* Machine check exception */
533 #ifdef T_CACHEFLT
534 case T_CACHEFLT: /* Cache flush exception */
535 #endif
536 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
537 break;
540 REGS_FUNC(RtlRaiseException)( &rec, &context );
541 restore:
542 restore_context( &context, HANDLER_CONTEXT );
546 /**********************************************************************
547 * trap_handler
549 * Handler for SIGTRAP.
551 static HANDLER_DEF(trap_handler)
553 EXCEPTION_RECORD rec;
554 CONTEXT context;
556 handler_init( &context, HANDLER_CONTEXT );
558 switch(get_trap_code(HANDLER_CONTEXT))
560 case T_TRCTRAP: /* Single-step exception */
561 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
562 break;
563 case T_BPTFLT: /* Breakpoint exception */
564 default:
565 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
566 break;
568 save_context( &context, HANDLER_CONTEXT );
569 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
570 rec.ExceptionRecord = NULL;
571 rec.ExceptionAddress = (LPVOID)context.Eip;
572 rec.NumberParameters = 0;
573 REGS_FUNC(RtlRaiseException)( &rec, &context );
574 restore_context( &context, HANDLER_CONTEXT );
578 /**********************************************************************
579 * fpe_handler
581 * Handler for SIGFPE.
583 static HANDLER_DEF(fpe_handler)
585 EXCEPTION_RECORD rec;
586 CONTEXT context;
588 handler_init( &context, HANDLER_CONTEXT );
590 save_fpu( &context, HANDLER_CONTEXT );
592 switch(get_trap_code(HANDLER_CONTEXT))
594 case T_DIVIDE: /* Division by zero exception */
595 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
596 break;
597 case T_FPOPFLT: /* Coprocessor segment overrun */
598 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
599 break;
600 case T_ARITHTRAP: /* Floating point exception */
601 case T_UNKNOWN: /* Unknown fault code */
602 rec.ExceptionCode = get_fpu_code( &context );
603 break;
604 default:
605 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
606 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
607 break;
609 save_context( &context, HANDLER_CONTEXT );
610 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
611 rec.ExceptionRecord = NULL;
612 rec.ExceptionAddress = (LPVOID)context.Eip;
613 rec.NumberParameters = 0;
614 REGS_FUNC(RtlRaiseException)( &rec, &context );
615 restore_context( &context, HANDLER_CONTEXT );
616 restore_fpu( &context, HANDLER_CONTEXT );
620 /**********************************************************************
621 * int_handler
623 * Handler for SIGINT.
625 static HANDLER_DEF(int_handler)
627 EXCEPTION_RECORD rec;
628 CONTEXT context;
630 handler_init( &context, HANDLER_CONTEXT );
631 save_context( &context, HANDLER_CONTEXT );
632 rec.ExceptionCode = CONTROL_C_EXIT;
633 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
634 rec.ExceptionRecord = NULL;
635 rec.ExceptionAddress = (LPVOID)context.Eip;
636 rec.NumberParameters = 0;
637 REGS_FUNC(RtlRaiseException)( &rec, &context );
638 restore_context( &context, HANDLER_CONTEXT );
642 /***********************************************************************
643 * set_handler
645 * Set a signal handler
647 static int set_handler( int sig, void (*func)() )
649 #ifdef linux
650 struct kernel_sigaction sig_act;
651 sig_act.sa_handler = func;
652 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
653 sig_act.sa_mask = 0;
654 /* point to the top of the stack */
655 sig_act.sa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
656 return wine_sigaction( sig, &sig_act, NULL );
657 #else /* linux */
658 struct sigaction sig_act;
659 sig_act.sa_handler = func;
660 sigemptyset( &sig_act.sa_mask );
662 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
663 sig_act.sa_flags = SA_ONSTACK;
664 # elif defined (__svr4__) || defined(_SCO_DS)
665 sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
666 # elif defined(__EMX__)
667 sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
668 # else
669 sig_act.sa_flags = 0;
670 # endif
671 return sigaction( sig, &sig_act, NULL );
672 #endif /* linux */
676 /**********************************************************************
677 * SIGNAL_Init
679 BOOL SIGNAL_Init(void)
681 #ifdef HAVE_WORKING_SIGALTSTACK
682 struct sigaltstack ss;
683 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
685 ss.ss_size = SIGNAL_STACK_SIZE;
686 ss.ss_flags = 0;
687 if (sigaltstack(&ss, NULL) < 0)
689 perror("sigaltstack");
690 /* fall through on error and try it differently */
693 #endif /* HAVE_SIGALTSTACK */
695 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
696 signal( SIGPIPE, SIG_IGN );
697 /* automatic child reaping to avoid zombies */
698 signal( SIGCHLD, SIG_IGN );
700 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
701 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
702 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
703 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
704 #ifdef SIGBUS
705 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
706 #endif
707 #ifdef SIGTRAP
708 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
709 #endif
710 return TRUE;
712 error:
713 perror("sigaction");
714 return FALSE;
717 #endif /* __i386__ */