@@ -13,6 +13,10 @@ impl Handler {
1313 pub unsafe fn new ( ) -> Handler {
1414 make_handler ( )
1515 }
16+
17+ fn null ( ) -> Handler {
18+ Handler { _data : crate :: ptr:: null_mut ( ) }
19+ }
1620}
1721
1822impl Drop for Handler {
@@ -108,13 +112,20 @@ mod imp {
108112 }
109113
110114 static mut MAIN_ALTSTACK : * mut libc:: c_void = ptr:: null_mut ( ) ;
115+ static mut NEED_ALTSTACK : bool = false ;
111116
112117 pub unsafe fn init ( ) {
113118 let mut action: sigaction = mem:: zeroed ( ) ;
114- action. sa_flags = SA_SIGINFO | SA_ONSTACK ;
115- action. sa_sigaction = signal_handler as sighandler_t ;
116- sigaction ( SIGSEGV , & action, ptr:: null_mut ( ) ) ;
117- sigaction ( SIGBUS , & action, ptr:: null_mut ( ) ) ;
119+ for & signal in & [ SIGSEGV , SIGBUS ] {
120+ sigaction ( signal, ptr:: null_mut ( ) , & mut action) ;
121+ // Configure our signal handler if one is not already set.
122+ if action. sa_sigaction == SIG_DFL {
123+ action. sa_flags = SA_SIGINFO | SA_ONSTACK ;
124+ action. sa_sigaction = signal_handler as sighandler_t ;
125+ sigaction ( signal, & action, ptr:: null_mut ( ) ) ;
126+ NEED_ALTSTACK = true ;
127+ }
128+ }
118129
119130 let handler = make_handler ( ) ;
120131 MAIN_ALTSTACK = handler. _data ;
@@ -152,6 +163,9 @@ mod imp {
152163 }
153164
154165 pub unsafe fn make_handler ( ) -> Handler {
166+ if !NEED_ALTSTACK {
167+ return Handler :: null ( ) ;
168+ }
155169 let mut stack = mem:: zeroed ( ) ;
156170 sigaltstack ( ptr:: null ( ) , & mut stack) ;
157171 // Configure alternate signal stack, if one is not already set.
@@ -160,7 +174,7 @@ mod imp {
160174 sigaltstack ( & stack, ptr:: null_mut ( ) ) ;
161175 Handler { _data : stack. ss_sp as * mut libc:: c_void }
162176 } else {
163- Handler { _data : ptr :: null_mut ( ) }
177+ Handler :: null ( )
164178 }
165179 }
166180
@@ -191,14 +205,12 @@ mod imp {
191205 target_os = "openbsd"
192206) ) ) ]
193207mod imp {
194- use crate :: ptr;
195-
196208 pub unsafe fn init ( ) { }
197209
198210 pub unsafe fn cleanup ( ) { }
199211
200212 pub unsafe fn make_handler ( ) -> super :: Handler {
201- super :: Handler { _data : ptr :: null_mut ( ) }
213+ super :: Handler :: null ( )
202214 }
203215
204216 pub unsafe fn drop_handler ( _handler : & mut super :: Handler ) { }
0 commit comments