From 5fa3d2fec80e03d6e3dcdd935dc3b40827268b23 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 30 Oct 2024 16:28:00 +0300 Subject: [PATCH] bugfix: get pid --- src/utils/prcs.rs | 22 +++++++++++++++------- tests/start_stop.rs | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/prcs.rs b/src/utils/prcs.rs index 750049d..523d00b 100644 --- a/src/utils/prcs.rs +++ b/src/utils/prcs.rs @@ -1,21 +1,27 @@ use crate::options::structs::CustomError; use log::{error, warn}; +use std::io; use std::process::{Command, Output}; use std::sync::Arc; use tokio::time::Duration; pub async fn get_pid(name: &str) -> Result { let name = Arc::new(name.to_string()); - tokio::task::spawn_blocking(move || { + let res= tokio::task::spawn_blocking(move || { Command::new("pidof") .arg(&*name) .output() - // .unwrap_or_else(|_| { - // error!("Failed to execute command 'pidof'"); - // std::process::exit(101); - // }) }) - .await? + .await?; + if let Ok(output) = res { + if output.stderr.is_empty() && output.stdout.is_empty() { + return Err(io::ErrorKind::NotFound.into()); + } else { + Ok(output) + } + } else { + return Err(io::ErrorKind::NotFound.into()); + } } // ! can be with bug !!! // * APPROVED @@ -154,8 +160,10 @@ mod process_unittests { async fn pidof_active_process() { assert!(get_pid("systemd").await.is_ok()); } + + // broken mechanism need to check #[tokio::test] async fn pidof_disabled_process() { - assert!(get_pid("invalid-process-name").await.is_ok()); + assert!(get_pid("invalid-process-name").await.is_err()); } } \ No newline at end of file diff --git a/tests/start_stop.rs b/tests/start_stop.rs index fc45295..a95b4fd 100644 --- a/tests/start_stop.rs +++ b/tests/start_stop.rs @@ -3,4 +3,4 @@ use runner_rs::*; #[test] fn test() { assert_eq!(2, 2); -} +} \ No newline at end of file