From 8f998771c9f98030a64d767dcc6cba5c4d1697f3 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sat, 15 Jan 2022 14:08:24 +0800 Subject: [PATCH] refactor process id --- Cargo.lock | 2 +- Cargo.toml | 1 + libs/hbb_common/Cargo.toml | 1 - libs/hbb_common/src/lib.rs | 15 ++------------- src/ipc.rs | 8 +++++--- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9dc71966d..6690e5b8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1726,7 +1726,6 @@ dependencies = [ "serde_json 1.0.74", "socket2 0.3.19", "sodiumoxide", - "sysinfo", "tokio", "tokio-socks", "tokio-util", @@ -3398,6 +3397,7 @@ dependencies = [ "serde_json 1.0.74", "sha2", "sys-locale", + "sysinfo", "systray", "uuid", "whoami", diff --git a/Cargo.toml b/Cargo.toml index 33516b6ba..4dccf9ebf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ uuid = { version = "0.8", features = ["v4"] } clap = "3.0" rpassword = "5.0" base64 = "0.13" +sysinfo = "0.22" [target.'cfg(not(any(target_os = "android")))'.dependencies] cpal = { git = "https://github.com/open-trade/cpal" } diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index b8a8258cf..d1868f5b5 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -30,7 +30,6 @@ filetime = "0.2" sodiumoxide = "0.2" regex = "1.4" tokio-socks = { git = "https://github.com/fufesou/tokio-socks" } -sysinfo = "0.22" [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] mac_address = "1.1" diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs index 79f72b136..e66eed7d0 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -27,13 +27,13 @@ pub use anyhow::{self, bail}; pub use futures_util; pub mod config; pub mod fs; -pub use mac_address; +pub use rand; pub use regex; pub use sodiumoxide; -pub use sysinfo; pub use tokio_socks; pub use tokio_socks::IntoTargetAddr; pub use tokio_socks::TargetAddr; +pub use mac_address; #[cfg(feature = "quic")] pub type Stream = quic::Connection; @@ -186,17 +186,6 @@ pub fn get_version_number(v: &str) -> i64 { n } -pub fn get_process(pid: i32) -> Option { - use sysinfo::{System, SystemExt}; - let mut sys = System::new(); - sys.refresh_processes(); - sys.process(pid).cloned() -} - -pub fn get_current_process() -> Option { - get_process(std::process::id() as _) -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/ipc.rs b/src/ipc.rs index 6c3f87478..bc25f803b 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -298,15 +298,17 @@ fn get_pid_file(postfix: &str) -> String { #[cfg(not(windows))] async fn check_pid(postfix: &str) { - use hbb_common::sysinfo::ProcessExt; let pid_file = get_pid_file(postfix); if let Ok(mut file) = File::open(&pid_file) { let mut content = String::new(); file.read_to_string(&mut content).ok(); let pid = content.parse::().unwrap_or(0); if pid > 0 { - if let Some(p) = hbb_common::get_process(pid) { - if let Some(current) = hbb_common::get_current_process() { + use sysinfo::{ProcessExt, System, SystemExt}; + let mut sys = System::new(); + sys.refresh_processes(); + if let Some(p) = sys.process(pid) { + if let Some(current) = sys.process(std::process::id() as _) { if current.name() == p.name() { // double check with connect if connect(1000, postfix).await.is_ok() {