@@ -13,6 +13,10 @@ impl Handler {
13
13
pub unsafe fn new ( ) -> Handler {
14
14
make_handler ( )
15
15
}
16
+
17
+ fn null ( ) -> Handler {
18
+ Handler { _data : crate :: ptr:: null_mut ( ) }
19
+ }
16
20
}
17
21
18
22
impl Drop for Handler {
@@ -108,13 +112,20 @@ mod imp {
108
112
}
109
113
110
114
static mut MAIN_ALTSTACK : * mut libc:: c_void = ptr:: null_mut ( ) ;
115
+ static mut NEED_ALTSTACK : bool = false ;
111
116
112
117
pub unsafe fn init ( ) {
113
118
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
+ }
118
129
119
130
let handler = make_handler ( ) ;
120
131
MAIN_ALTSTACK = handler. _data ;
@@ -152,6 +163,9 @@ mod imp {
152
163
}
153
164
154
165
pub unsafe fn make_handler ( ) -> Handler {
166
+ if !NEED_ALTSTACK {
167
+ return Handler :: null ( ) ;
168
+ }
155
169
let mut stack = mem:: zeroed ( ) ;
156
170
sigaltstack ( ptr:: null ( ) , & mut stack) ;
157
171
// Configure alternate signal stack, if one is not already set.
@@ -160,7 +174,7 @@ mod imp {
160
174
sigaltstack ( & stack, ptr:: null_mut ( ) ) ;
161
175
Handler { _data : stack. ss_sp as * mut libc:: c_void }
162
176
} else {
163
- Handler { _data : ptr :: null_mut ( ) }
177
+ Handler :: null ( )
164
178
}
165
179
}
166
180
@@ -191,14 +205,12 @@ mod imp {
191
205
target_os = "openbsd"
192
206
) ) ) ]
193
207
mod imp {
194
- use crate :: ptr;
195
-
196
208
pub unsafe fn init ( ) { }
197
209
198
210
pub unsafe fn cleanup ( ) { }
199
211
200
212
pub unsafe fn make_handler ( ) -> super :: Handler {
201
- super :: Handler { _data : ptr :: null_mut ( ) }
213
+ super :: Handler :: null ( )
202
214
}
203
215
204
216
pub unsafe fn drop_handler ( _handler : & mut super :: Handler ) { }
0 commit comments