Skip to content

Commit

Permalink
Merge pull request #976 from GuillaumeGomez/mac-proc-status
Browse files Browse the repository at this point in the history
Correctly handle process status on macOS
  • Loading branch information
GuillaumeGomez committed May 5, 2023
2 parents 02af6ab + 08d56ba commit 41367e7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
9 changes: 8 additions & 1 deletion src/apple/macos/process.rs
Expand Up @@ -39,7 +39,7 @@ pub struct Process {
/// enough rights to get this information.
///
/// This is very likely this one that you want instead of `process_status`.
pub status: Option<ThreadStatus>,
pub(crate) status: Option<ThreadStatus>,
pub(crate) old_read_bytes: u64,
pub(crate) old_written_bytes: u64,
pub(crate) read_bytes: u64,
Expand Down Expand Up @@ -153,6 +153,13 @@ impl ProcessExt for Process {
}

fn status(&self) -> ProcessStatus {
// If the status is `Run`, then it's very likely wrong so we instead
// return a `ProcessStatus` converted from the `ThreadStatus`.
if self.process_status == ProcessStatus::Run {
if let Some(thread_status) = self.status {
return ProcessStatus::from(thread_status);
}
}
self.process_status
}

Expand Down
65 changes: 33 additions & 32 deletions src/apple/process.rs
Expand Up @@ -5,20 +5,41 @@ use std::fmt;
pub use crate::sys::inner::process::*;
use crate::ProcessStatus;

// FIXME: To be removed once <https://github.com/rust-lang/libc/pull/3233> is merged and released.
const SIDL: u32 = 1;
const SRUN: u32 = 2;
const SSLEEP: u32 = 3;
const SSTOP: u32 = 4;
const SZOMB: u32 = 5;

#[doc(hidden)]
impl From<u32> for ProcessStatus {
fn from(status: u32) -> ProcessStatus {
match status {
1 => ProcessStatus::Idle,
2 => ProcessStatus::Run,
3 => ProcessStatus::Sleep,
4 => ProcessStatus::Stop,
5 => ProcessStatus::Zombie,
SIDL => ProcessStatus::Idle,
SRUN => ProcessStatus::Run,
SSLEEP => ProcessStatus::Sleep,
SSTOP => ProcessStatus::Stop,
SZOMB => ProcessStatus::Zombie,
x => ProcessStatus::Unknown(x),
}
}
}

#[doc(hidden)]
impl From<ThreadStatus> for ProcessStatus {
fn from(status: ThreadStatus) -> ProcessStatus {
match status {
ThreadStatus::Running => ProcessStatus::Run,
ThreadStatus::Stopped => ProcessStatus::Stop,
ThreadStatus::Waiting => ProcessStatus::Sleep,
ThreadStatus::Uninterruptible => ProcessStatus::Dead,
ThreadStatus::Halted => ProcessStatus::Parked,
ThreadStatus::Unknown(x) => ProcessStatus::Unknown(x as _),
}
}
}

impl fmt::Display for ProcessStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Expand All @@ -33,8 +54,8 @@ impl fmt::Display for ProcessStatus {
}

/// Enum describing the different status of a thread.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ThreadStatus {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ThreadStatus {
/// Thread is running normally.
Running,
/// Thread is stopped.
Expand All @@ -52,32 +73,12 @@ pub enum ThreadStatus {
impl From<i32> for ThreadStatus {
fn from(status: i32) -> ThreadStatus {
match status {
1 => ThreadStatus::Running,
2 => ThreadStatus::Stopped,
3 => ThreadStatus::Waiting,
4 => ThreadStatus::Uninterruptible,
5 => ThreadStatus::Halted,
libc::TH_STATE_RUNNING => ThreadStatus::Running,
libc::TH_STATE_STOPPED => ThreadStatus::Stopped,
libc::TH_STATE_WAITING => ThreadStatus::Waiting,
libc::TH_STATE_UNINTERRUPTIBLE => ThreadStatus::Uninterruptible,
libc::TH_STATE_HALTED => ThreadStatus::Halted,
x => ThreadStatus::Unknown(x),
}
}
}

impl ThreadStatus {
/// Used to display `ThreadStatus`.
pub fn to_string(&self) -> &str {
match *self {
ThreadStatus::Running => "Running",
ThreadStatus::Stopped => "Stopped",
ThreadStatus::Waiting => "Waiting",
ThreadStatus::Uninterruptible => "Uninterruptible",
ThreadStatus::Halted => "Halted",
ThreadStatus::Unknown(_) => "Unknown",
}
}
}

impl fmt::Display for ThreadStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
4 changes: 4 additions & 0 deletions src/common.rs
Expand Up @@ -910,6 +910,10 @@ pub enum ProcessStatus {
///
/// Parked (Linux 3.9 to 3.13 only).
///
/// ## macOS
///
/// Halted at a clean point.
///
/// ## Other OS
///
/// Not available.
Expand Down

0 comments on commit 41367e7

Please sign in to comment.