From f7439c01637f0401f0e881dcc7f8020b404478dc Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Thu, 23 Jan 2025 13:35:57 +0100 Subject: [PATCH 1/3] Give the extension the ability to get the parent process info --- Cargo.lock | 201 +++++++++++++++++++++++++------ extension/bridge/Cargo.toml | 7 ++ extension/bridge/src/commands.rs | 67 +++++++++++ 3 files changed, 241 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7bfb3df34..409102c9ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -378,9 +378,9 @@ dependencies = [ [[package]] name = "core-foundation-sys" -version = "0.8.4" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "crc32fast" @@ -402,14 +402,30 @@ dependencies = [ ] [[package]] -name = "crossbeam-utils" -version = "0.8.15" +name = "crossbeam-deque" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ - "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "ctor" version = "0.2.8" @@ -870,7 +886,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows", + "windows 0.48.0", ] [[package]] @@ -1111,6 +1127,8 @@ dependencies = [ "byteorder", "mio 1.0.3", "serde_json", + "sysinfo", + "windows 0.51.1", ] [[package]] @@ -1141,6 +1159,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "num-bigint" version = "0.4.4" @@ -1347,6 +1374,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "regex" version = "1.10.4" @@ -1757,6 +1804,20 @@ dependencies = [ "syn", ] +[[package]] +name = "sysinfo" +version = "0.33.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fc858248ea01b66f19d8e8a6d55f41deaf91e9d495246fd01368d99935c6c01" +dependencies = [ + "core-foundation-sys", + "libc", + "memchr", + "ntapi", + "rayon", + "windows 0.57.0", +] + [[package]] name = "system-configuration" version = "0.6.1" @@ -2406,7 +2467,70 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" dependencies = [ - "windows-targets 0.48.0", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" +dependencies = [ + "windows-core 0.51.1", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core 0.57.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2415,11 +2539,20 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" dependencies = [ - "windows-result", + "windows-result 0.2.0", "windows-strings", "windows-targets 0.52.6", ] +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.2.0" @@ -2435,7 +2568,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" dependencies = [ - "windows-result", + "windows-result 0.2.0", "windows-targets 0.52.6", ] @@ -2460,7 +2593,7 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets 0.48.0", + "windows-targets 0.48.5", ] [[package]] @@ -2483,17 +2616,17 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm 0.48.0", - "windows_aarch64_msvc 0.48.0", - "windows_i686_gnu 0.48.0", - "windows_i686_msvc 0.48.0", - "windows_x86_64_gnu 0.48.0", - "windows_x86_64_gnullvm 0.48.0", - "windows_x86_64_msvc 0.48.0", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -2520,9 +2653,9 @@ checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" @@ -2538,9 +2671,9 @@ checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] name = "windows_aarch64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" @@ -2556,9 +2689,9 @@ checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] name = "windows_i686_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" @@ -2580,9 +2713,9 @@ checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] name = "windows_i686_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" @@ -2598,9 +2731,9 @@ checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" [[package]] name = "windows_x86_64_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" @@ -2616,9 +2749,9 @@ checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" @@ -2634,9 +2767,9 @@ checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "windows_x86_64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" diff --git a/extension/bridge/Cargo.toml b/extension/bridge/Cargo.toml index 8b447a0039..ecb4e12798 100644 --- a/extension/bridge/Cargo.toml +++ b/extension/bridge/Cargo.toml @@ -7,3 +7,10 @@ edition = "2018" mio = { version = "1.0.3", features = ["os-poll", "net"] } serde_json = "1.0.135" byteorder = "1.5.0" +sysinfo = "0.33.1" + +[target.'cfg(windows)'.dependencies] +windows = { version = "0.51.1", features = [ + "Win32_Foundation", + "Win32_System_Diagnostics_ToolHelp", +] } diff --git a/extension/bridge/src/commands.rs b/extension/bridge/src/commands.rs index 92fafff7af..7f64cd765e 100644 --- a/extension/bridge/src/commands.rs +++ b/extension/bridge/src/commands.rs @@ -6,6 +6,8 @@ use std::error::Error; use serde_json::{Value, json}; + use sysinfo; + /** * Handles commands that are sent from @@ -33,6 +35,28 @@ crate::io::write_output(std::io::stdout(),&out) .expect("Unable to Write to STDOUT?"); Ok(true) + } + "proc_info" => { + let parent_pid = get_parent_pid(); + let s = sysinfo::System::new_all(); + + if let Some(parent_process) = s.process(sysinfo::Pid::from_u32(parent_pid)) { + let parent_info = json!({ + "b":"proc_info", + "pid": parent_process.pid().as_u32(), + "exe": parent_process.exe() + }); + crate::io::write_output(std::io::stdout(), &parent_info) + .expect("Unable to Write to STDOUT?"); + } else { + crate::io::write_output( + std::io::stdout(), + &json!({"error": "Parent process not found"}), + ) + .expect("Unable to Write to STDOUT?"); + } + + Ok(true) } _ =>{ // We did not handle this. @@ -75,3 +99,46 @@ mod launcher { json!("{error:'start_unsupported!'}") } } + + + +#[cfg(target_os = "windows")] +fn get_parent_pid() -> u32 { + use std::mem; + use windows::Win32::Foundation::{CloseHandle, HANDLE}; + use windows::Win32::System::Diagnostics::ToolHelp::{ + CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, + TH32CS_SNAPPROCESS, + }; + + let mut ppid: u32 = 0; + let pid = std::process::id(); + + unsafe { + let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) + .expect("Failed to create snapshot"); + let snapshot_handle = HANDLE(snapshot.0); + + let mut entry = PROCESSENTRY32 { + dwSize: mem::size_of::() as u32, + ..Default::default() + }; + + if Process32First(snapshot_handle, &mut entry).is_ok() { + while Process32Next(snapshot_handle, &mut entry).is_ok() { + if entry.th32ProcessID == pid { + ppid = entry.th32ParentProcessID; + break; + } + } + } + + let _ = CloseHandle(snapshot_handle); + } + ppid +} + +#[cfg(any(target_os = "macos", target_os = "linux"))] +fn get_parent_pid() -> u32 { + unsafe { libc::getppid() as u32 } +} From 71eb3b418264bf9710e40ca79e2d9d5b3705ce9c Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Thu, 23 Jan 2025 13:40:33 +0100 Subject: [PATCH 2/3] use internals --- extension/bridge/src/commands.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/bridge/src/commands.rs b/extension/bridge/src/commands.rs index 7f64cd765e..cebfed91d1 100644 --- a/extension/bridge/src/commands.rs +++ b/extension/bridge/src/commands.rs @@ -140,5 +140,5 @@ fn get_parent_pid() -> u32 { #[cfg(any(target_os = "macos", target_os = "linux"))] fn get_parent_pid() -> u32 { - unsafe { libc::getppid() as u32 } + return os::unix::process::parent_id; } From 3cb793af2dfa3bc4143428bf55eab1a722938baf Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Wed, 29 Jan 2025 16:58:58 +0100 Subject: [PATCH 3/3] fix build issues on unix platforms --- extension/bridge/src/commands.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extension/bridge/src/commands.rs b/extension/bridge/src/commands.rs index cebfed91d1..29a89f6217 100644 --- a/extension/bridge/src/commands.rs +++ b/extension/bridge/src/commands.rs @@ -95,6 +95,7 @@ mod launcher { #[cfg(not(target_os = "windows"))] mod launcher { + use serde_json::json; pub fn start_vpn() -> serde_json::Value{ json!("{error:'start_unsupported!'}") } @@ -140,5 +141,5 @@ fn get_parent_pid() -> u32 { #[cfg(any(target_os = "macos", target_os = "linux"))] fn get_parent_pid() -> u32 { - return os::unix::process::parent_id; + return std::os::unix::process::parent_id(); }