Skip to content

Commit

Permalink
Remove duplication between Process::wait methods on unix targets
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 20, 2024
1 parent ee6ea3d commit 7753154
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 48 deletions.
14 changes: 1 addition & 13 deletions src/unix/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::ffi::{OsStr, OsString};
use std::mem::{self, MaybeUninit};
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;

Expand Down Expand Up @@ -205,18 +204,7 @@ impl ProcessInner {
}

pub(crate) fn wait(&self) -> Option<ExitStatus> {
let mut status = 0;
// attempt waiting
unsafe {
if retry_eintr!(libc::waitpid(self.pid.0, &mut status, 0)) < 0 {
// attempt failed (non-child process) so loop until process ends
let duration = std::time::Duration::from_millis(10);
while kill(self.pid.0, 0) == 0 {
std::thread::sleep(duration);
}
}
Some(ExitStatus::from_raw(status))
}
crate::unix::utils::wait_process(self.pid)
}

pub(crate) fn session_id(&self) -> Option<Pid> {
Expand Down
16 changes: 1 addition & 15 deletions src/unix/freebsd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use crate::{DiskUsage, Gid, Pid, Process, ProcessRefreshKind, ProcessStatus, Sig

use std::ffi::{OsStr, OsString};
use std::fmt;
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;

use libc::kill;

use super::utils::{get_sys_value_str, WrapMap};

#[doc(hidden)]
Expand Down Expand Up @@ -157,18 +154,7 @@ impl ProcessInner {
}

pub(crate) fn wait(&self) -> Option<ExitStatus> {
let mut status = 0;
// attempt waiting
unsafe {
if retry_eintr!(libc::waitpid(self.pid.0, &mut status, 0)) < 0 {
// attempt failed (non-child process) so loop until process ends
let duration = std::time::Duration::from_millis(10);
while kill(self.pid.0, 0) == 0 {
std::thread::sleep(duration);
}
}
Some(ExitStatus::from_raw(status))
}
crate::unix::utils::wait_process(self.pid)
}

pub(crate) fn session_id(&self) -> Option<Pid> {
Expand Down
18 changes: 3 additions & 15 deletions src/unix/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use std::fmt;
use std::fs::{self, DirEntry, File};
use std::io::Read;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::str::{self, FromStr};
use std::sync::atomic::{AtomicUsize, Ordering};

use libc::{c_ulong, gid_t, kill, uid_t};
use libc::{c_ulong, gid_t, uid_t};

use crate::sys::system::SystemInfo;
use crate::sys::utils::{
Expand Down Expand Up @@ -169,7 +168,7 @@ impl ProcessInner {

pub(crate) fn kill_with(&self, signal: Signal) -> Option<bool> {
let c_signal = crate::sys::system::convert_signal(signal)?;
unsafe { Some(kill(self.pid.0, c_signal) == 0) }
unsafe { Some(libc::kill(self.pid.0, c_signal) == 0) }
}

pub(crate) fn name(&self) -> &OsStr {
Expand Down Expand Up @@ -254,18 +253,7 @@ impl ProcessInner {
}

pub(crate) fn wait(&self) -> Option<ExitStatus> {
let mut status = 0;
// attempt waiting
unsafe {
if retry_eintr!(libc::waitpid(self.pid.0, &mut status, 0)) < 0 {
// attempt failed (non-child process) so loop until process ends
let duration = std::time::Duration::from_millis(10);
while kill(self.pid.0, 0) == 0 {
std::thread::sleep(duration);
}
}
Some(ExitStatus::from_raw(status))
}
crate::unix::utils::wait_process(self.pid)
}

pub(crate) fn session_id(&self) -> Option<Pid> {
Expand Down
21 changes: 21 additions & 0 deletions src/unix/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,24 @@ pub(crate) fn cstr_to_rust_with_size(
String::from_utf8(s).ok()
}
}

#[cfg(all(
feature = "system",
not(any(target_os = "ios", feature = "apple-sandbox"))
))]
pub(crate) fn wait_process(pid: crate::Pid) -> Option<std::process::ExitStatus> {
use std::os::unix::process::ExitStatusExt;

let mut status = 0;
// attempt waiting
unsafe {
if retry_eintr!(libc::waitpid(pid.0, &mut status, 0)) < 0 {
// attempt failed (non-child process) so loop until process ends
let duration = std::time::Duration::from_millis(10);
while libc::kill(pid.0, 0) == 0 {
std::thread::sleep(duration);
}
}
Some(std::process::ExitStatus::from_raw(status))
}
}
12 changes: 7 additions & 5 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,13 @@ impl ProcessInner {
std::thread::sleep(std::time::Duration::from_millis(10));
}
let mut exit_status = 0;
match GetExitCodeProcess(handle, &mut exit_status) {
Ok(_) => Some(ExitStatus::from_raw(exit_status)),
Err(_error) => {
sysinfo_debug!("failed to retrieve process exit status: {_error:?}");
None
unsafe {
match GetExitCodeProcess(handle, &mut exit_status) {
Ok(_) => Some(ExitStatus::from_raw(exit_status)),
Err(_error) => {
sysinfo_debug!("failed to retrieve process exit status: {_error:?}");
None
}
}
}
} else {
Expand Down

0 comments on commit 7753154

Please sign in to comment.