mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
uucore/process: remove custom ExitStatus
This commit is contained in:
parent
3ca6139e0f
commit
dc828fed2d
2 changed files with 4 additions and 60 deletions
|
@ -11,6 +11,7 @@ mod status;
|
||||||
use crate::status::ExitStatus;
|
use crate::status::ExitStatus;
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
|
use std::os::unix::process::ExitStatusExt;
|
||||||
use std::process::{self, Child, Stdio};
|
use std::process::{self, Child, Stdio};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
|
|
@ -12,10 +12,9 @@
|
||||||
//! Set of functions to manage IDs
|
//! Set of functions to manage IDs
|
||||||
|
|
||||||
use libc::{gid_t, pid_t, uid_t};
|
use libc::{gid_t, pid_t, uid_t};
|
||||||
use std::fmt;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::process::Child;
|
use std::process::Child;
|
||||||
use std::process::ExitStatus as StdExitStatus;
|
use std::process::ExitStatus;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
@ -41,60 +40,6 @@ pub fn getuid() -> uid_t {
|
||||||
unsafe { libc::getuid() }
|
unsafe { libc::getuid() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is basically sys::unix::process::ExitStatus
|
|
||||||
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
||||||
pub enum ExitStatus {
|
|
||||||
Code(i32),
|
|
||||||
Signal(i32),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
||||||
impl ExitStatus {
|
|
||||||
fn from_std_status(status: StdExitStatus) -> Self {
|
|
||||||
#[cfg(unix)]
|
|
||||||
{
|
|
||||||
use std::os::unix::process::ExitStatusExt;
|
|
||||||
|
|
||||||
if let Some(signal) = status.signal() {
|
|
||||||
return Self::Signal(signal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: this should never fail as we check if the program exited through a signal above
|
|
||||||
Self::Code(status.code().unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn success(&self) -> bool {
|
|
||||||
match *self {
|
|
||||||
Self::Code(code) => code == 0,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn code(&self) -> Option<i32> {
|
|
||||||
match *self {
|
|
||||||
Self::Code(code) => Some(code),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn signal(&self) -> Option<i32> {
|
|
||||||
match *self {
|
|
||||||
Self::Signal(code) => Some(code),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ExitStatus {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match *self {
|
|
||||||
Self::Code(code) => write!(f, "exit code: {}", code),
|
|
||||||
Self::Signal(code) => write!(f, "exit code: {}", code),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Missing methods for Child objects
|
/// Missing methods for Child objects
|
||||||
pub trait ChildExt {
|
pub trait ChildExt {
|
||||||
/// Send a signal to a Child process.
|
/// Send a signal to a Child process.
|
||||||
|
@ -119,9 +64,7 @@ impl ChildExt for Child {
|
||||||
|
|
||||||
fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result<Option<ExitStatus>> {
|
fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result<Option<ExitStatus>> {
|
||||||
if timeout == Duration::from_micros(0) {
|
if timeout == Duration::from_micros(0) {
|
||||||
return self
|
return self.wait().map(Some);
|
||||||
.wait()
|
|
||||||
.map(|status| Some(ExitStatus::from_std_status(status)));
|
|
||||||
}
|
}
|
||||||
// .try_wait() doesn't drop stdin, so we do it manually
|
// .try_wait() doesn't drop stdin, so we do it manually
|
||||||
drop(self.stdin.take());
|
drop(self.stdin.take());
|
||||||
|
@ -129,7 +72,7 @@ impl ChildExt for Child {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
loop {
|
loop {
|
||||||
if let Some(status) = self.try_wait()? {
|
if let Some(status) = self.try_wait()? {
|
||||||
return Ok(Some(ExitStatus::from_std_status(status)));
|
return Ok(Some(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
if start.elapsed() >= timeout {
|
if start.elapsed() >= timeout {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue