From 4beacf93d71305577db319b0b0e716d80848dd0a Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 21 Feb 2023 15:07:44 +0800 Subject: [PATCH] kill check-hwcodec-config process Signed-off-by: 21pages --- Cargo.lock | 2 +- Cargo.toml | 1 - libs/hbb_common/Cargo.toml | 1 + libs/hbb_common/src/lib.rs | 1 + libs/scrap/src/common/hwcodec.rs | 38 ++++++++++++++++++++++---------- src/ipc.rs | 2 +- src/platform/macos.rs | 2 +- 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48981e169..115845b50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2623,6 +2623,7 @@ dependencies = [ "serde_json 1.0.89", "socket2 0.3.19", "sodiumoxide", + "sysinfo", "tokio", "tokio-socks", "tokio-util", @@ -4887,7 +4888,6 @@ dependencies = [ "shutdown_hooks", "simple_rc", "sys-locale", - "sysinfo", "system_shutdown", "tao", "tray-icon", diff --git a/Cargo.toml b/Cargo.toml index 0ebe49fdf..f685e3f2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,6 @@ uuid = { version = "1.0", features = ["v4"] } clap = "3.0" rpassword = "7.0" base64 = "0.13" -sysinfo = "0.24" num_cpus = "1.13" bytes = { version = "1.2", features = ["serde"] } default-net = "0.12.0" diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index 0457bb19a..a125078d2 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -33,6 +33,7 @@ tokio-socks = { git = "https://github.com/open-trade/tokio-socks" } chrono = "0.4" backtrace = "0.3" libc = "0.2" +sysinfo = "0.24" [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 99cb6f408..bfb773908 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -42,6 +42,7 @@ pub use chrono; pub use libc; pub use directories_next; pub mod keyboard; +pub use sysinfo; #[cfg(feature = "quic")] pub type Stream = quic::Connection; diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index 9cd6077a6..27b157b79 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -317,16 +317,30 @@ pub fn check_config() { } pub fn check_config_process(force_reset: bool) { - if force_reset { - HwCodecConfig::remove(); - } - if let Ok(exe) = std::env::current_exe() { - std::thread::spawn(move || { - std::process::Command::new(exe) - .arg("--check-hwcodec-config") - .status() - .ok(); - HwCodecConfig::refresh(); - }); - }; + use hbb_common::sysinfo::{ProcessExt, System, SystemExt}; + + std::thread::spawn(move || { + if force_reset { + HwCodecConfig::remove(); + } + if let Ok(exe) = std::env::current_exe() { + if let Some(file_name) = exe.file_name().to_owned() { + let s = System::new_all(); + let arg = "--check-hwcodec-config"; + for process in s.processes_by_name(&file_name.to_string_lossy().to_string()) { + if process.cmd().iter().any(|cmd| cmd.contains(arg)) { + log::warn!("already have process {}", arg); + return; + } + } + if let Ok(mut child) = std::process::Command::new(exe).arg(arg).spawn() { + let second = 3; + std::thread::sleep(std::time::Duration::from_secs(second)); + // kill: Different platforms have different results + child.kill().ok(); + HwCodecConfig::refresh(); + } + } + }; + }); } diff --git a/src/ipc.rs b/src/ipc.rs index 699b0bcd7..b1b130340 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -549,7 +549,7 @@ async fn check_pid(postfix: &str) { file.read_to_string(&mut content).ok(); let pid = content.parse::().unwrap_or(0); if pid > 0 { - use sysinfo::{ProcessExt, System, SystemExt}; + use hbb_common::sysinfo::{ProcessExt, System, SystemExt}; let mut sys = System::new(); sys.refresh_processes(); if let Some(p) = sys.process(pid.into()) { diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 0c8c51455..910c26982 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -558,7 +558,7 @@ pub fn hide_dock() { } fn check_main_window() -> bool { - use sysinfo::{ProcessExt, System, SystemExt}; + use hbb_common::sysinfo::{ProcessExt, System, SystemExt}; let mut sys = System::new(); sys.refresh_processes(); let app = format!("/Applications/{}.app", crate::get_app_name());