-
Notifications
You must be signed in to change notification settings - Fork 689
Add WaitStatus::PtraceSyscall for use with PTRACE_O_TRACESYSGOOD #566
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -40,14 +40,56 @@ libc_bitflags!( | |
target_os = "android"))] | ||
const WSTOPPED: WaitPidFlag = WUNTRACED; | ||
|
||
/// Possible return values from `wait()` or `waitpid()`. | ||
/// | ||
/// Each status (other than `StillAlive`) describes a state transition | ||
/// in a child process `Pid`, such as the process exiting or stopping, | ||
/// plus additional data about the transition if any. | ||
/// | ||
/// Note that there are two Linux-specific enum variants, `PtraceEvent` | ||
/// and `PtraceSyscall`. Portable code should avoid exhaustively | ||
/// matching on `WaitStatus`. | ||
#[derive(Eq, PartialEq, Clone, Copy, Debug)] | ||
pub enum WaitStatus { | ||
/// The process exited normally (as with `exit()` or returning from | ||
/// `main`) with the given exit code. This case matches the C macro | ||
/// `WIFEXITED(status)`; the second field is `WEXITSTATUS(status)`. | ||
Exited(Pid, i8), | ||
/// The process was killed by the given signal. The third field | ||
/// indicates whether the signal generated a core dump. This case | ||
/// matches the C macro `WIFSIGNALED(status)`; the last two fields | ||
/// correspond to `WTERMSIG(status)` and `WCOREDUMP(status)`. | ||
Signaled(Pid, Signal, bool), | ||
/// The process is alive, but was stopped by the given signal. This | ||
/// is only reported if `WaitPidFlag::WUNTRACED` was passed. This | ||
/// case matches the C macro `WIFSTOPPED(status)`; the second field | ||
/// is `WSTOPSIG(status)`. | ||
Stopped(Pid, Signal), | ||
/// The traced process was stopped by a `PTRACE_EVENT_*` event. See | ||
/// [`nix::sys::ptrace`] and [`ptrace`(2)] for more information. All | ||
/// currently-defined events use `SIGTRAP` as the signal; the third | ||
/// field is the `PTRACE_EVENT_*` value of the event. | ||
/// | ||
/// [`nix::sys::ptrace`]: ../ptrace/index.html | ||
/// [`ptrace`(2)]: http://man7.org/linux/man-pages/man2/ptrace.2.html | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
PtraceEvent(Pid, Signal, c_int), | ||
/// The traced process was stopped by execution of a system call, | ||
/// and `PTRACE_O_TRACESYSGOOD` is in effect. See [`ptrace`(2)] for | ||
/// more information. | ||
/// | ||
/// [`ptrace`(2)]: http://man7.org/linux/man-pages/man2/ptrace.2.html | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
PtraceSyscall(Pid), | ||
/// The process was previously stopped but has resumed execution | ||
/// after receiving a `SIGCONT` signal. This is only reported if | ||
/// `WaitPidFlag::WCONTINUED` was passed. This case matches the C | ||
/// macro `WIFCONTINUED(status)`. | ||
Continued(Pid), | ||
/// There are currently no state changes to report in any awaited | ||
/// child process. This is only returned if `WaitPidFlag::WNOHANG` | ||
/// was used (otherwise `wait()` or `waitpid()` would block until | ||
/// there was something to report). | ||
StillAlive | ||
} | ||
|
||
|
@@ -56,6 +98,7 @@ pub enum WaitStatus { | |
mod status { | ||
use sys::signal::Signal; | ||
use libc::c_int; | ||
use libc::SIGTRAP; | ||
|
||
pub fn exited(status: i32) -> bool { | ||
(status & 0x7F) == 0 | ||
|
@@ -82,7 +125,17 @@ mod status { | |
} | ||
|
||
pub fn stop_signal(status: i32) -> Signal { | ||
Signal::from_c_int((status & 0xFF00) >> 8).unwrap() | ||
// Keep only 7 bits of the signal: the high bit | ||
// is used to indicate syscall stops, below. | ||
Signal::from_c_int((status & 0x7F00) >> 8).unwrap() | ||
} | ||
|
||
pub fn syscall_stop(status: i32) -> bool { | ||
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. Needs doccomments for the function. |
||
// From ptrace(2), setting PTRACE_O_TRACESYSGOOD has the effect | ||
// of delivering SIGTRAP | 0x80 as the signal number for syscall | ||
// stops. This allows easily distinguishing syscall stops from | ||
// genuine SIGTRAP signals. | ||
((status & 0xFF00) >> 8) == SIGTRAP | 0x80 | ||
} | ||
|
||
pub fn stop_additional(status: i32) -> c_int { | ||
|
@@ -196,7 +249,9 @@ fn decode(pid : Pid, status: i32) -> WaitStatus { | |
if #[cfg(any(target_os = "linux", target_os = "android"))] { | ||
fn decode_stopped(pid: Pid, status: i32) -> WaitStatus { | ||
let status_additional = status::stop_additional(status); | ||
if status_additional == 0 { | ||
if status::syscall_stop(status) { | ||
WaitStatus::PtraceSyscall(pid) | ||
} else if status_additional == 0 { | ||
WaitStatus::Stopped(pid, status::stop_signal(status)) | ||
} else { | ||
WaitStatus::PtraceEvent(pid, status::stop_signal(status), status::stop_additional(status)) | ||
|
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
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.
This function needs doc comments as well.