@@ -15,7 +15,7 @@ use nix::sys::signal;
15
15
use std:: io:: Write ;
16
16
use std:: ptr;
17
17
use std:: sync:: atomic:: Ordering :: SeqCst ;
18
- use std:: sync:: atomic:: { AtomicPtr , AtomicU64 } ;
18
+ use std:: sync:: atomic:: { AtomicBool , AtomicPtr , AtomicU64 } ;
19
19
use std:: time:: Instant ;
20
20
21
21
// Note that this file makes use the following async-signal safe functions in a signal handler.
@@ -100,10 +100,44 @@ pub(crate) extern "C" fn handle_posix_sigaction(
100
100
unsafe { chain_signal_handler ( signum, sig_info, ucontext) } ;
101
101
}
102
102
103
+ static CRASHTRACKER_ENABLED : AtomicBool = AtomicBool :: new ( false ) ;
104
+
105
+ /// Disables the crashtracker.
106
+ /// Note that this does not restore the old signal handlers, but rather turns crash-tracking into a
107
+ /// no-op, and then chains the old handlers. This means that handlers registered after the
108
+ /// crashtracker will continue to work as expected.
109
+ ///
110
+ /// # Preconditions
111
+ /// None
112
+ /// # Safety
113
+ /// None
114
+ /// # Atomicity
115
+ /// This function is atomic and idempotent. Calling it multiple times is allowed.
116
+ pub fn disable_crashtracker ( ) {
117
+ CRASHTRACKER_ENABLED . store ( false , SeqCst ) ;
118
+ }
119
+
120
+ /// Enables the crashtracker, if had been previously disabled.
121
+ /// If crashtracking has not been initialized, this function will have no effect.
122
+ ///
123
+ /// # Preconditions
124
+ /// None
125
+ /// # Safety
126
+ /// None
127
+ /// # Atomicity
128
+ /// This function is atomic and idempotent. Calling it multiple times is allowed.
129
+ pub fn enable_crashtracker ( ) {
130
+ CRASHTRACKER_ENABLED . store ( true , SeqCst ) ;
131
+ }
132
+
103
133
fn handle_posix_signal_impl (
104
134
sig_info : * const siginfo_t ,
105
135
ucontext : * const ucontext_t ,
106
136
) -> anyhow:: Result < ( ) > {
137
+ if !CRASHTRACKER_ENABLED . load ( SeqCst ) {
138
+ return Ok ( ( ) ) ;
139
+ }
140
+
107
141
// If this is a SIGSEGV signal, it could be called due to a stack overflow. In that case, since
108
142
// this signal allocates to the stack and cannot guarantee it is running without SA_NODEFER, it
109
143
// is possible that we will re-emit the signal. Contemporary unices handle this just fine (no
0 commit comments