Skip to content

Commit

Permalink
Inline conversion from wait status to process state
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed May 19, 2024
1 parent 0e93fbc commit 7dcbabb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
40 changes: 0 additions & 40 deletions yash-env/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

use crate::semantics::ExitStatus;
use crate::trap::Signal;
use nix::sys::wait::WaitStatus;
use slab::Slab;
use std::collections::HashMap;
use std::iter::FusedIterator;
Expand Down Expand Up @@ -140,45 +139,6 @@ impl ProcessState {
ProcessState::Exited(_) | ProcessState::Signaled { .. } => false,
}
}

/// Converts `ProcessState` to `WaitStatus`.
///
/// This function returns a type defined in the `nix` crate, which is not
/// covered by the semantic versioning policy of this crate.
#[must_use]
pub fn to_wait_status(self, pid: Pid) -> WaitStatus {
match self {
ProcessState::Running => WaitStatus::Continued(pid.into()),
ProcessState::Exited(exit_status) => WaitStatus::Exited(pid.into(), exit_status.0),
ProcessState::Stopped(signal) => WaitStatus::Stopped(pid.into(), signal),
ProcessState::Signaled { signal, core_dump } => {
WaitStatus::Signaled(pid.into(), signal, core_dump)
}
}
}

/// Converts `WaitStatus` to `ProcessState`.
///
/// If the given `WaitStatus` represents a change in the process state, this
/// function returns the new state with the process ID. Otherwise, it
/// returns `None`.
///
/// The `WaitStatus` type is defined in the `nix` crate, which is not
/// covered by the semantic versioning policy of this crate.
#[must_use]
pub fn from_wait_status(status: WaitStatus) -> Option<(Pid, Self)> {
match status {
WaitStatus::Continued(pid) => Some((pid.into(), ProcessState::Running)),
WaitStatus::Exited(pid, exit_status) => {
Some((pid.into(), ProcessState::Exited(ExitStatus(exit_status))))
}
WaitStatus::Stopped(pid, signal) => Some((pid.into(), ProcessState::Stopped(signal))),
WaitStatus::Signaled(pid, signal, core_dump) => {
Some((pid.into(), ProcessState::Signaled { signal, core_dump }))
}
_ => None,
}
}
}

/// Error value indicating that the process is running.
Expand Down
15 changes: 14 additions & 1 deletion yash-env/src/system/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use super::UnknownSignalError;
use crate::io::Fd;
use crate::job::Pid;
use crate::job::ProcessState;
use crate::semantics::ExitStatus;
use crate::SignalHandling;
use nix::errno::Errno as NixErrno;
use nix::libc::DIR;
Expand All @@ -50,6 +51,7 @@ use nix::sys::signal::SaFlags;
use nix::sys::signal::SigAction;
use nix::sys::signal::SigHandler;
use nix::sys::stat::stat;
use nix::sys::wait::WaitStatus;
use nix::unistd::AccessFlags;
use std::convert::Infallible;
use std::convert::TryInto;
Expand Down Expand Up @@ -543,7 +545,18 @@ impl System for RealSystem {
use nix::sys::wait::WaitPidFlag;
let options = WaitPidFlag::WUNTRACED | WaitPidFlag::WCONTINUED | WaitPidFlag::WNOHANG;
let status = nix::sys::wait::waitpid(Some(target.into()), options.into())?;
Ok(ProcessState::from_wait_status(status))
let result = match status {
WaitStatus::Continued(pid) => Some((pid.into(), ProcessState::Running)),
WaitStatus::Exited(pid, exit_status) => {
Some((pid.into(), ProcessState::Exited(ExitStatus(exit_status))))
}
WaitStatus::Stopped(pid, signal) => Some((pid.into(), ProcessState::Stopped(signal))),
WaitStatus::Signaled(pid, signal, core_dump) => {
Some((pid.into(), ProcessState::Signaled { signal, core_dump }))
}
_ => None,
};
Ok(result)
}

fn execve(&mut self, path: &CStr, args: &[CString], envs: &[CString]) -> Result<Infallible> {
Expand Down

0 comments on commit 7dcbabb

Please sign in to comment.