1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #4197 from tertsdiepraam/cleanup-uucore-process

`uucore/process`: remove custom `ExitStatus`
This commit is contained in:
Sylvestre Ledru 2022-12-03 10:15:48 +01:00 committed by GitHub
commit 8e5d374ec2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 60 deletions

View file

@ -11,6 +11,7 @@ mod status;
use crate::status::ExitStatus;
use clap::{crate_version, Arg, ArgAction, Command};
use std::io::ErrorKind;
use std::os::unix::process::ExitStatusExt;
use std::process::{self, Child, Stdio};
use std::time::Duration;
use uucore::display::Quotable;

View file

@ -12,10 +12,9 @@
//! Set of functions to manage IDs
use libc::{gid_t, pid_t, uid_t};
use std::fmt;
use std::io;
use std::process::Child;
use std::process::ExitStatus as StdExitStatus;
use std::process::ExitStatus;
use std::thread;
use std::time::{Duration, Instant};
@ -41,60 +40,6 @@ pub fn getuid() -> uid_t {
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
pub trait ChildExt {
/// 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>> {
if timeout == Duration::from_micros(0) {
return self
.wait()
.map(|status| Some(ExitStatus::from_std_status(status)));
return self.wait().map(Some);
}
// .try_wait() doesn't drop stdin, so we do it manually
drop(self.stdin.take());
@ -129,7 +72,7 @@ impl ChildExt for Child {
let start = Instant::now();
loop {
if let Some(status) = self.try_wait()? {
return Ok(Some(ExitStatus::from_std_status(status)));
return Ok(Some(status));
}
if start.elapsed() >= timeout {