Repository navigation
Preserve Unix signal dispositions when probing handlers #8961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,12 +104,26 @@ mod ffi { | |
| /// The caller must ensure `signalnum` is a valid platform signal number. | ||
| #[cfg(any(unix, windows))] | ||
| pub unsafe fn probe_handler(signalnum: i32) -> Option<sighandler_t> { | ||
| let handler = unsafe { libc::signal(signalnum, libc::SIG_IGN) }; | ||
| if handler == libc::SIG_ERR as sighandler_t { | ||
| None | ||
| } else { | ||
| unsafe { libc::signal(signalnum, handler) }; | ||
| Some(handler) | ||
| #[cfg(unix)] | ||
| { | ||
| // Query without changing disposition: even temporarily ignoring SIGCHLD | ||
| // can discard a child's exit status, and signal() also replaces masks. | ||
| let mut action = core::mem::MaybeUninit::<libc::sigaction>::uninit(); | ||
| if unsafe { libc::sigaction(signalnum, core::ptr::null(), action.as_mut_ptr()) } == 0 { | ||
| Some(unsafe { action.assume_init() }.sa_sigaction) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| #[cfg(windows)] | ||
| { | ||
| let handler = unsafe { libc::signal(signalnum, libc::SIG_IGN) }; | ||
| if handler == libc::SIG_ERR as sighandler_t { | ||
| None | ||
| } else { | ||
| unsafe { libc::signal(signalnum, handler) }; | ||
| Some(handler) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -379,3 +393,49 @@ pub fn sigset_contains(mask: libc::sigset_t, signum: i32) -> bool { | |
| pub fn valid_signals(_max_signum: usize) -> io::Result<Vec<i32>> { | ||
| Ok(VALID_SIGNALS.to_vec()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[cfg(unix)] | ||
| mod tests { | ||
| #[test] | ||
| fn probing_preserves_signal_flags_and_mask() { | ||
| unsafe extern "C" fn handler(_: libc::c_int) {} | ||
|
|
||
| struct Restore(libc::sigaction); | ||
| impl Drop for Restore { | ||
| fn drop(&mut self) { | ||
| unsafe { libc::sigaction(libc::SIGUSR2, &self.0, core::ptr::null_mut()) }; | ||
| } | ||
| } | ||
|
|
||
| // No other host_env test uses SIGUSR2; retain the host disposition even | ||
| // when an assertion fails. Do not deliver any signal to the process. | ||
| unsafe { | ||
| let mut action: libc::sigaction = core::mem::zeroed(); | ||
| action.sa_sigaction = handler as *const () as libc::sighandler_t; | ||
| action.sa_flags = libc::SA_NODEFER; | ||
| assert_eq!(libc::sigemptyset(&mut action.sa_mask), 0); | ||
| assert_eq!(libc::sigaddset(&mut action.sa_mask, libc::SIGUSR1), 0); | ||
| let mut original = core::mem::MaybeUninit::uninit(); | ||
| assert_eq!( | ||
| libc::sigaction(libc::SIGUSR2, &action, original.as_mut_ptr()), | ||
| 0 | ||
| ); | ||
| let _restore = Restore(original.assume_init()); | ||
|
|
||
| assert_eq!( | ||
| super::probe_handler(libc::SIGUSR2), | ||
| Some(action.sa_sigaction) | ||
| ); | ||
| let mut observed = core::mem::MaybeUninit::<libc::sigaction>::uninit(); | ||
| assert_eq!( | ||
| libc::sigaction(libc::SIGUSR2, core::ptr::null(), observed.as_mut_ptr()), | ||
| 0 | ||
| ); | ||
| let observed = observed.assume_init(); | ||
| assert_eq!(observed.sa_sigaction, action.sa_sigaction); | ||
| assert_ne!(observed.sa_flags & libc::SA_NODEFER, 0); | ||
| assert_eq!(libc::sigismember(&observed.sa_mask, libc::SIGUSR1), 1); | ||
| } | ||
|
Comment on lines
+413
to
+439
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is test. simple test is better than perfect test |
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the best way to order these attributes?