refactor process id

This commit is contained in:
rustdesk 2022-01-15 14:08:24 +08:00
parent f47dcd1a8b
commit 8f998771c9
5 changed files with 9 additions and 18 deletions

2
Cargo.lock generated
View File

@ -1726,7 +1726,6 @@ dependencies = [
"serde_json 1.0.74", "serde_json 1.0.74",
"socket2 0.3.19", "socket2 0.3.19",
"sodiumoxide", "sodiumoxide",
"sysinfo",
"tokio", "tokio",
"tokio-socks", "tokio-socks",
"tokio-util", "tokio-util",
@ -3398,6 +3397,7 @@ dependencies = [
"serde_json 1.0.74", "serde_json 1.0.74",
"sha2", "sha2",
"sys-locale", "sys-locale",
"sysinfo",
"systray", "systray",
"uuid", "uuid",
"whoami", "whoami",

View File

@ -43,6 +43,7 @@ uuid = { version = "0.8", features = ["v4"] }
clap = "3.0" clap = "3.0"
rpassword = "5.0" rpassword = "5.0"
base64 = "0.13" base64 = "0.13"
sysinfo = "0.22"
[target.'cfg(not(any(target_os = "android")))'.dependencies] [target.'cfg(not(any(target_os = "android")))'.dependencies]
cpal = { git = "https://github.com/open-trade/cpal" } cpal = { git = "https://github.com/open-trade/cpal" }

View File

@ -30,7 +30,6 @@ filetime = "0.2"
sodiumoxide = "0.2" sodiumoxide = "0.2"
regex = "1.4" regex = "1.4"
tokio-socks = { git = "https://github.com/fufesou/tokio-socks" } tokio-socks = { git = "https://github.com/fufesou/tokio-socks" }
sysinfo = "0.22"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
mac_address = "1.1" mac_address = "1.1"

View File

@ -27,13 +27,13 @@ pub use anyhow::{self, bail};
pub use futures_util; pub use futures_util;
pub mod config; pub mod config;
pub mod fs; pub mod fs;
pub use mac_address; pub use rand;
pub use regex; pub use regex;
pub use sodiumoxide; pub use sodiumoxide;
pub use sysinfo;
pub use tokio_socks; pub use tokio_socks;
pub use tokio_socks::IntoTargetAddr; pub use tokio_socks::IntoTargetAddr;
pub use tokio_socks::TargetAddr; pub use tokio_socks::TargetAddr;
pub use mac_address;
#[cfg(feature = "quic")] #[cfg(feature = "quic")]
pub type Stream = quic::Connection; pub type Stream = quic::Connection;
@ -186,17 +186,6 @@ pub fn get_version_number(v: &str) -> i64 {
n n
} }
pub fn get_process(pid: i32) -> Option<sysinfo::Process> {
use sysinfo::{System, SystemExt};
let mut sys = System::new();
sys.refresh_processes();
sys.process(pid).cloned()
}
pub fn get_current_process() -> Option<sysinfo::Process> {
get_process(std::process::id() as _)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -298,15 +298,17 @@ fn get_pid_file(postfix: &str) -> String {
#[cfg(not(windows))] #[cfg(not(windows))]
async fn check_pid(postfix: &str) { async fn check_pid(postfix: &str) {
use hbb_common::sysinfo::ProcessExt;
let pid_file = get_pid_file(postfix); let pid_file = get_pid_file(postfix);
if let Ok(mut file) = File::open(&pid_file) { if let Ok(mut file) = File::open(&pid_file) {
let mut content = String::new(); let mut content = String::new();
file.read_to_string(&mut content).ok(); file.read_to_string(&mut content).ok();
let pid = content.parse::<i32>().unwrap_or(0); let pid = content.parse::<i32>().unwrap_or(0);
if pid > 0 { if pid > 0 {
if let Some(p) = hbb_common::get_process(pid) { use sysinfo::{ProcessExt, System, SystemExt};
if let Some(current) = hbb_common::get_current_process() { 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() { if current.name() == p.name() {
// double check with connect // double check with connect
if connect(1000, postfix).await.is_ok() { if connect(1000, postfix).await.is_ok() {