@@ -130,6 +130,92 @@ impl Default for RegisteredUserSignal {
130130#[ cfg( unix) ]
131131static USER_SIGNALS : Mutex < Option < Vec < RegisteredUserSignal > > > = Mutex :: new ( None ) ;
132132
133+ // Signal handlers cannot take USER_SIGNALS: delivery may interrupt a thread
134+ // holding that mutex. Publish only the copyable dump configuration separately.
135+ // Keep the original descriptor number (not a dup), including its dup2 behavior.
136+ // Previous dispositions and chaining remain in the existing bookkeeping path;
137+ // this does not make concurrent chain/unregister signal-safe.
138+ #[ cfg( all( unix, target_has_atomic = "64" ) ) ]
139+ mod user_signal_config {
140+ use super :: { USER_SIGNAL_CAPACITY , UserSignal } ;
141+ use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
142+
143+ const ENABLED : u64 = 1 << 32 ;
144+ const ALL_THREADS : u64 = 1 << 33 ;
145+ const CHAIN : u64 = 1 << 34 ;
146+ static SLOTS : [ AtomicU64 ; USER_SIGNAL_CAPACITY ] =
147+ [ const { AtomicU64 :: new ( 0 ) } ; USER_SIGNAL_CAPACITY ] ;
148+
149+ fn encode ( signal : UserSignal ) -> u64 {
150+ u64:: from ( signal. fd as u32 )
151+ | ENABLED
152+ | if signal. all_threads { ALL_THREADS } else { 0 }
153+ | if signal. chain { CHAIN } else { 0 }
154+ }
155+
156+ fn decode ( value : u64 ) -> Option < UserSignal > {
157+ ( value & ENABLED != 0 ) . then_some ( UserSignal {
158+ fd : value as u32 as i32 ,
159+ all_threads : value & ALL_THREADS != 0 ,
160+ chain : value & CHAIN != 0 ,
161+ } )
162+ }
163+
164+ pub ( super ) fn store ( signum : usize , signal : Option < UserSignal > ) {
165+ SLOTS [ signum] . store ( signal. map_or ( 0 , encode) , Ordering :: SeqCst ) ;
166+ }
167+
168+ pub ( super ) fn load ( signum : usize ) -> Option < UserSignal > {
169+ decode ( SLOTS . get ( signum) ?. load ( Ordering :: SeqCst ) )
170+ }
171+
172+ #[ cfg( test) ]
173+ mod tests {
174+ use super :: * ;
175+
176+ #[ test]
177+ fn configuration_preserves_descriptor_bits_and_flags ( ) {
178+ for fd in [ 0 , 1 , i32:: MAX , i32:: MIN , -1 ] {
179+ for all_threads in [ false , true ] {
180+ for chain in [ false , true ] {
181+ let actual = decode ( encode ( UserSignal {
182+ fd,
183+ all_threads,
184+ chain,
185+ } ) )
186+ . unwrap ( ) ;
187+ assert_eq ! ( actual. fd, fd) ;
188+ assert_eq ! ( actual. all_threads, all_threads) ;
189+ assert_eq ! ( actual. chain, chain) ;
190+ }
191+ }
192+ }
193+ assert ! ( decode( 0 ) . is_none( ) ) ;
194+ }
195+
196+ #[ test]
197+ fn reading_configuration_does_not_lock_registration_bookkeeping ( ) {
198+ // Slot zero is never a valid registration. Model a signal interrupting
199+ // the writer, without installing a process-global test disposition.
200+ let _writer = super :: super :: USER_SIGNALS . lock ( ) ;
201+ store (
202+ 0 ,
203+ Some ( UserSignal {
204+ fd : 17 ,
205+ all_threads : true ,
206+ chain : false ,
207+ } ) ,
208+ ) ;
209+ let actual = super :: super :: get_user_signal ( 0 ) . unwrap ( ) ;
210+ store ( 0 , None ) ;
211+ assert_eq ! ( actual. fd, 17 ) ;
212+ assert ! ( actual. all_threads) ;
213+ assert ! ( !actual. chain) ;
214+ assert ! ( super :: super :: get_user_signal( USER_SIGNAL_CAPACITY ) . is_none( ) ) ;
215+ }
216+ }
217+ }
218+
133219pub fn write_fd ( fd : i32 , buf : & [ u8 ] ) {
134220 let _ = unsafe { libc:: write ( fd, buf. as_ptr ( ) as * const libc:: c_void , buf. len ( ) as _ ) } ;
135221}
@@ -262,7 +348,14 @@ pub fn exit_immediately(code: libc::c_int) -> ! {
262348 unsafe { libc:: _exit ( code) }
263349}
264350
265- #[ cfg( unix) ]
351+ #[ cfg( all( unix, target_has_atomic = "64" ) ) ]
352+ pub fn get_user_signal ( signum : usize ) -> Option < UserSignal > {
353+ user_signal_config:: load ( signum)
354+ }
355+
356+ // Preserve the existing implementation on targets without lock-free 64-bit
357+ // atomics. Its signal-context mutex limitation is not fixed by this change.
358+ #[ cfg( all( unix, not( target_has_atomic = "64" ) ) ) ]
266359pub fn get_user_signal ( signum : usize ) -> Option < UserSignal > {
267360 let guard = USER_SIGNALS . lock ( ) ;
268361 guard
@@ -319,6 +412,15 @@ pub fn register_user_signal(
319412 entry. fd = fd;
320413 entry. all_threads = all_threads;
321414 entry. chain = chain;
415+ #[ cfg( target_has_atomic = "64" ) ]
416+ user_signal_config:: store (
417+ signum,
418+ Some ( UserSignal {
419+ fd,
420+ all_threads,
421+ chain,
422+ } ) ,
423+ ) ;
322424 Ok ( ( ) )
323425}
324426
@@ -340,6 +442,8 @@ pub fn unregister_user_signal(signum: libc::c_int) -> bool {
340442 }
341443
342444 let previous = entry. previous ;
445+ #[ cfg( target_has_atomic = "64" ) ]
446+ user_signal_config:: store ( signum, None ) ;
343447 * entry = RegisteredUserSignal :: default ( ) ;
344448 restore_sigaction ( signum as libc:: c_int , & previous) ;
345449 true
0 commit comments