From f3773dc116989cf560296773417fa15306e6e6dc Mon Sep 17 00:00:00 2001 From: Gaurav Jain Date: Tue, 17 Dec 2024 23:19:27 +0530 Subject: [PATCH 01/14] check if process is system --- src/processes/linux_list.rs | 13 +++++++++---- src/processes/macos_list.rs | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/processes/linux_list.rs b/src/processes/linux_list.rs index 170ab80a..53b9927c 100644 --- a/src/processes/linux_list.rs +++ b/src/processes/linux_list.rs @@ -26,15 +26,20 @@ pub fn active_executables() -> Result { let executable = e.key().clone(); // .file_name() returns `None` if the path terminates in `..` // We use the absolute path in such a case. - let display_name = match path.file_name() { - Some(s) => s.to_string_lossy().to_string(), - None => path.to_string_lossy().to_string(), + let display_name = path + .file_name() + .unwrap_or(path.as_os_str()) + .to_string_lossy() + .to_string(); + let is_system = match process.effective_user_id() { + Some(id) => id.to_string() == "0", + None => false, }; e.insert(ProcessInfo { executable, display_name, is_visible: false, - is_system: false, + is_system, }); } } diff --git a/src/processes/macos_list.rs b/src/processes/macos_list.rs index eac64c17..863520ec 100644 --- a/src/processes/macos_list.rs +++ b/src/processes/macos_list.rs @@ -42,12 +42,16 @@ pub fn active_executables() -> Result { let executable = e.key().clone(); // .file_name() returns `None` if the path terminates in `..` // We use the absolute path in such a case. - let display_name = match path.file_name() { - Some(s) => s.to_string_lossy().to_string(), - None => path.to_string_lossy().to_string(), + let display_name = path + .file_name() + .unwrap_or(path.as_os_str()) + .to_string_lossy() + .to_string(); + let is_system = match process.effective_user_id() { + Some(id) => id.to_string() == "0", + None => false, }; let is_visible = visible.contains(&pid); - let is_system = executable.starts_with("/System/"); e.insert(ProcessInfo { executable, display_name, From 21eb6f5f8f5242665572640c73791096930c8ccb Mon Sep 17 00:00:00 2001 From: Gaurav Jain Date: Thu, 19 Dec 2024 16:22:20 +0530 Subject: [PATCH 02/14] reuse code across macos_list.rs and linux_list.rs --- src/processes/linux_list.rs | 60 -------------------- src/processes/mod.rs | 10 ++-- src/processes/{macos_list.rs => nix_list.rs} | 19 ++++++- 3 files changed, 21 insertions(+), 68 deletions(-) delete mode 100644 src/processes/linux_list.rs rename src/processes/{macos_list.rs => nix_list.rs} (90%) diff --git a/src/processes/linux_list.rs b/src/processes/linux_list.rs deleted file mode 100644 index 53b9927c..00000000 --- a/src/processes/linux_list.rs +++ /dev/null @@ -1,60 +0,0 @@ -use crate::processes::{ProcessInfo, ProcessList}; -use anyhow::Result; -use std::collections::hash_map::Entry; -use std::collections::HashMap; -use std::path::PathBuf; -use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; - -pub fn active_executables() -> Result { - let mut executables: HashMap = HashMap::new(); - let mut sys = System::new(); - - sys.refresh_processes_specifics( - ProcessesToUpdate::All, - true, - ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet), - ); - - for process in sys.processes().values() { - // process.exe() will return an empty path if there was an error while trying to read /proc//exe. - if let Some(path) = process.exe() { - let executable = path.to_path_buf(); - - match executables.entry(executable) { - Entry::Occupied(_) => {} - Entry::Vacant(e) => { - let executable = e.key().clone(); - // .file_name() returns `None` if the path terminates in `..` - // We use the absolute path in such a case. - let display_name = path - .file_name() - .unwrap_or(path.as_os_str()) - .to_string_lossy() - .to_string(); - let is_system = match process.effective_user_id() { - Some(id) => id.to_string() == "0", - None => false, - }; - e.insert(ProcessInfo { - executable, - display_name, - is_visible: false, - is_system, - }); - } - } - } - } - Ok(executables.into_values().collect()) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn process_list() { - let lst = active_executables().unwrap(); - assert!(!lst.is_empty()); - } -} diff --git a/src/processes/mod.rs b/src/processes/mod.rs index ec8505b3..e7d8fe6a 100644 --- a/src/processes/mod.rs +++ b/src/processes/mod.rs @@ -2,9 +2,9 @@ pub use image; use std::path::PathBuf; #[cfg(target_os = "macos")] -mod macos_list; +mod nix_list; #[cfg(target_os = "macos")] -pub use self::macos_list::active_executables; +pub use self::nix_list::active_executables; #[cfg(windows)] mod windows_list; @@ -14,9 +14,9 @@ pub use self::windows_list::active_executables; pub use self::windows_list::get_process_name; #[cfg(target_os = "linux")] -mod linux_list; +mod nix_list; #[cfg(target_os = "linux")] -pub use self::linux_list::active_executables; +pub use self::nix_list::active_executables; #[cfg(target_os = "macos")] mod macos_icons; @@ -44,7 +44,7 @@ pub static ICON_CACHE: once_cell::sync::Lazy> = pub mod bench { #[cfg(target_os = "macos")] - pub use super::macos_list::visible_windows; + pub use super::nix_list::visible_windows; #[cfg(windows)] pub use super::windows_list::visible_windows; diff --git a/src/processes/macos_list.rs b/src/processes/nix_list.rs similarity index 90% rename from src/processes/macos_list.rs rename to src/processes/nix_list.rs index 863520ec..46ac285f 100644 --- a/src/processes/macos_list.rs +++ b/src/processes/nix_list.rs @@ -1,18 +1,23 @@ use crate::intercept_conf::PID; use crate::processes::{ProcessInfo, ProcessList}; use anyhow::Result; + +#[cfg(target_os = "macos")] use cocoa::base::nil; +#[cfg(target_os = "macos")] use cocoa::foundation::NSString; -use core_foundation::number::kCFNumberSInt32Type; -use core_foundation::number::CFNumberGetValue; -use core_foundation::number::CFNumberRef; +#[cfg(target_os = "macos")] +use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; +#[cfg(target_os = "macos")] use core_graphics::display::{ kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, CGWindowListCopyWindowInfo, }; + use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; +#[cfg(target_os = "macos")] use std::ffi::c_void; use std::path::PathBuf; use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; @@ -65,6 +70,7 @@ pub fn active_executables() -> Result { Ok(executables.into_values().collect()) } +#[cfg(target_os = "macos")] pub fn visible_windows() -> Result> { let mut pids: HashSet = HashSet::new(); unsafe { @@ -100,6 +106,12 @@ pub fn visible_windows() -> Result> { } } +#[cfg(target_os = "linux")] +pub fn visible_windows() -> Result> { + // Finding visible windows on linux is not worth the effort + Ok(HashSet::new()) +} + #[cfg(test)] mod tests { use super::*; @@ -117,6 +129,7 @@ mod tests { dbg!(lst.len()); } + #[cfg(target_os = "macos")] #[test] fn visible_windows_list() { let open_windows_pids = visible_windows().unwrap(); From bb540e475b63b8cc6076ce40b2b2a11d2240f31b Mon Sep 17 00:00:00 2001 From: Gaurav Jain Date: Thu, 19 Dec 2024 16:33:34 +0530 Subject: [PATCH 03/14] nits --- src/processes/mod.rs | 9 ++------- src/processes/nix_list.rs | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/processes/mod.rs b/src/processes/mod.rs index e7d8fe6a..4ba0794c 100644 --- a/src/processes/mod.rs +++ b/src/processes/mod.rs @@ -1,9 +1,9 @@ pub use image; use std::path::PathBuf; -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "linux", target_os = "macos"))] mod nix_list; -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "linux", target_os = "macos"))] pub use self::nix_list::active_executables; #[cfg(windows)] @@ -13,11 +13,6 @@ pub use self::windows_list::active_executables; #[cfg(windows)] pub use self::windows_list::get_process_name; -#[cfg(target_os = "linux")] -mod nix_list; -#[cfg(target_os = "linux")] -pub use self::nix_list::active_executables; - #[cfg(target_os = "macos")] mod macos_icons; #[cfg(target_os = "macos")] diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 46ac285f..7116155e 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -106,9 +106,9 @@ pub fn visible_windows() -> Result> { } } -#[cfg(target_os = "linux")] +#[cfg(not(target_os = "macos"))] pub fn visible_windows() -> Result> { - // Finding visible windows on linux is not worth the effort + // Finding visible windows on other platforms is not worth the effort Ok(HashSet::new()) } From 3e39fbb0e481f2a8d487f2070a62fbd77f3c8a96 Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 01:39:25 +0530 Subject: [PATCH 04/14] modify is_system heuristic --- src/processes/macos_visible_windows.rs | 47 +++++++++++++++++ src/processes/mod.rs | 3 ++ src/processes/nix_list.rs | 73 ++++++-------------------- 3 files changed, 67 insertions(+), 56 deletions(-) create mode 100644 src/processes/macos_visible_windows.rs diff --git a/src/processes/macos_visible_windows.rs b/src/processes/macos_visible_windows.rs new file mode 100644 index 00000000..5899b5d8 --- /dev/null +++ b/src/processes/macos_visible_windows.rs @@ -0,0 +1,47 @@ +use anyhow::Result; +use cocoa::base::nil; +use cocoa::foundation::NSString; +use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; +use core_graphics::display::{ + kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, + CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, + CGWindowListCopyWindowInfo, +}; +use crate::intercept_conf::PID; +use std::ffi::c_void; +use std::collections::HashSet; + +pub fn macos_visible_windows() -> Result> { + let mut pids: HashSet = HashSet::new(); + unsafe { + let windows_info_list = CGWindowListCopyWindowInfo( + kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements, + kCGNullWindowID, + ); + let count = CFArrayGetCount(windows_info_list); + + for i in 0..count - 1 { + let dic_ref = CFArrayGetValueAtIndex(windows_info_list, i); + let key = NSString::alloc(nil).init_str("kCGWindowOwnerPID"); + let mut pid: *const c_void = std::ptr::null_mut(); + + if CFDictionaryGetValueIfPresent( + dic_ref as CFDictionaryRef, + key as *const c_void, + &mut pid, + ) != 0 + { + let pid_cf_ref = pid as CFNumberRef; + let mut pid: i32 = 0; + if CFNumberGetValue( + pid_cf_ref, + kCFNumberSInt32Type, + &mut pid as *mut i32 as *mut c_void, + ) { + pids.insert(pid as u32); + } + } + } + Ok(pids) + } +} diff --git a/src/processes/mod.rs b/src/processes/mod.rs index 4ba0794c..62cf1a4c 100644 --- a/src/processes/mod.rs +++ b/src/processes/mod.rs @@ -23,6 +23,9 @@ mod windows_icons; #[cfg(windows)] use self::windows_icons::IconCache; +#[cfg(target_os = "macos")] +mod macos_visible_windows; + #[derive(Debug, Clone)] pub struct ProcessInfo { pub executable: PathBuf, diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 7116155e..cb32dbee 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -1,27 +1,14 @@ use crate::intercept_conf::PID; use crate::processes::{ProcessInfo, ProcessList}; use anyhow::Result; - -#[cfg(target_os = "macos")] -use cocoa::base::nil; -#[cfg(target_os = "macos")] -use cocoa::foundation::NSString; -#[cfg(target_os = "macos")] -use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; -#[cfg(target_os = "macos")] -use core_graphics::display::{ - kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, - CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, - CGWindowListCopyWindowInfo, -}; - use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; -#[cfg(target_os = "macos")] -use std::ffi::c_void; use std::path::PathBuf; use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; +#[cfg(target_os = "macos")] +use crate::processes::macos_visible_windows::macos_visible_windows; + pub fn active_executables() -> Result { let mut executables: HashMap = HashMap::new(); let visible = visible_windows()?; @@ -52,10 +39,7 @@ pub fn active_executables() -> Result { .unwrap_or(path.as_os_str()) .to_string_lossy() .to_string(); - let is_system = match process.effective_user_id() { - Some(id) => id.to_string() == "0", - None => false, - }; + let is_system = is_system(&executable); let is_visible = visible.contains(&pid); e.insert(ProcessInfo { executable, @@ -70,46 +54,23 @@ pub fn active_executables() -> Result { Ok(executables.into_values().collect()) } -#[cfg(target_os = "macos")] pub fn visible_windows() -> Result> { - let mut pids: HashSet = HashSet::new(); - unsafe { - let windows_info_list = CGWindowListCopyWindowInfo( - kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements, - kCGNullWindowID, - ); - let count = CFArrayGetCount(windows_info_list); - - for i in 0..count - 1 { - let dic_ref = CFArrayGetValueAtIndex(windows_info_list, i); - let key = NSString::alloc(nil).init_str("kCGWindowOwnerPID"); - let mut pid: *const c_void = std::ptr::null_mut(); + #[cfg(target_os = "macos")] + return macos_visible_windows(); - if CFDictionaryGetValueIfPresent( - dic_ref as CFDictionaryRef, - key as *const c_void, - &mut pid, - ) != 0 - { - let pid_cf_ref = pid as CFNumberRef; - let mut pid: i32 = 0; - if CFNumberGetValue( - pid_cf_ref, - kCFNumberSInt32Type, - &mut pid as *mut i32 as *mut c_void, - ) { - pids.insert(pid as u32); - } - } - } - Ok(pids) - } + #[cfg(target_os = "linux")] + // Finding visible windows is less useful on Linux, where more applications tend to be CLI-based. + // So we skip all the X11/Wayland complexity. + return Ok(HashSet::new()); } -#[cfg(not(target_os = "macos"))] -pub fn visible_windows() -> Result> { - // Finding visible windows on other platforms is not worth the effort - Ok(HashSet::new()) +fn is_system(executable: &PathBuf) -> bool { + #[cfg(target_os = "macos")] + let sys_paths = vec!["/sbin", "/usr/sbin", "/usr/libexec", "/System/"]; + #[cfg(target_os = "linux")] + let sys_paths = vec!["/sbin/", "/usr/sbin/", "/usr/libexec/", "/usr/lib/systemd/"]; + + sys_paths.into_iter().any(|path| executable.starts_with(path)) } #[cfg(test)] From 4bb74de1bdab6fb4c9b82fbf55f0317b261be65e Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 20:10:33 +0000 Subject: [PATCH 05/14] [autofix.ci] apply automated fixes --- src/processes/macos_visible_windows.rs | 4 ++-- src/processes/nix_list.rs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/processes/macos_visible_windows.rs b/src/processes/macos_visible_windows.rs index 5899b5d8..fcb3a961 100644 --- a/src/processes/macos_visible_windows.rs +++ b/src/processes/macos_visible_windows.rs @@ -1,3 +1,4 @@ +use crate::intercept_conf::PID; use anyhow::Result; use cocoa::base::nil; use cocoa::foundation::NSString; @@ -7,9 +8,8 @@ use core_graphics::display::{ CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, CGWindowListCopyWindowInfo, }; -use crate::intercept_conf::PID; -use std::ffi::c_void; use std::collections::HashSet; +use std::ffi::c_void; pub fn macos_visible_windows() -> Result> { let mut pids: HashSet = HashSet::new(); diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index cb32dbee..6b36065c 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -70,7 +70,9 @@ fn is_system(executable: &PathBuf) -> bool { #[cfg(target_os = "linux")] let sys_paths = vec!["/sbin/", "/usr/sbin/", "/usr/libexec/", "/usr/lib/systemd/"]; - sys_paths.into_iter().any(|path| executable.starts_with(path)) + sys_paths + .into_iter() + .any(|path| executable.starts_with(path)) } #[cfg(test)] From edfa0be14f9326f525204ae519e2f102b4de3801 Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 12:10:04 +0530 Subject: [PATCH 06/14] use uid to check if process is system --- src/processes/nix_list.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 6b36065c..8078d7a2 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -4,7 +4,7 @@ use anyhow::Result; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; +use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind, Process, Uid}; #[cfg(target_os = "macos")] use crate::processes::macos_visible_windows::macos_visible_windows; @@ -16,7 +16,9 @@ pub fn active_executables() -> Result { sys.refresh_processes_specifics( ProcessesToUpdate::All, true, - ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet), + ProcessRefreshKind::nothing() + .with_exe(UpdateKind::OnlyIfNotSet) + .with_user(UpdateKind::OnlyIfNotSet), ); for (pid, process) in sys.processes() { // process.exe() will return empty path if there was an error while trying to read /proc//exe. @@ -39,7 +41,7 @@ pub fn active_executables() -> Result { .unwrap_or(path.as_os_str()) .to_string_lossy() .to_string(); - let is_system = is_system(&executable); + let is_system = is_system(&process); let is_visible = visible.contains(&pid); e.insert(ProcessInfo { executable, @@ -64,15 +66,16 @@ pub fn visible_windows() -> Result> { return Ok(HashSet::new()); } -fn is_system(executable: &PathBuf) -> bool { +fn is_system(process: &Process) -> bool { #[cfg(target_os = "macos")] - let sys_paths = vec!["/sbin", "/usr/sbin", "/usr/libexec", "/System/"]; + return process.exe().starts_with("/System/"); #[cfg(target_os = "linux")] - let sys_paths = vec!["/sbin/", "/usr/sbin/", "/usr/libexec/", "/usr/lib/systemd/"]; - - sys_paths - .into_iter() - .any(|path| executable.starts_with(path)) + process + .user_id() + .and_then(|uid| Uid::try_from(1000) + .ok() + .map(|uid_1000| uid < &uid_1000)) + .unwrap_or(false) } #[cfg(test)] From a73bb65ee7c6f54232c3100b125b0de8d2737f9b Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 06:41:13 +0000 Subject: [PATCH 07/14] [autofix.ci] apply automated fixes --- src/processes/nix_list.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 8078d7a2..1f9e5e0e 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -4,7 +4,7 @@ use anyhow::Result; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind, Process, Uid}; +use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, Uid, UpdateKind}; #[cfg(target_os = "macos")] use crate::processes::macos_visible_windows::macos_visible_windows; @@ -41,7 +41,7 @@ pub fn active_executables() -> Result { .unwrap_or(path.as_os_str()) .to_string_lossy() .to_string(); - let is_system = is_system(&process); + let is_system = is_system(process); let is_visible = visible.contains(&pid); e.insert(ProcessInfo { executable, @@ -72,9 +72,7 @@ fn is_system(process: &Process) -> bool { #[cfg(target_os = "linux")] process .user_id() - .and_then(|uid| Uid::try_from(1000) - .ok() - .map(|uid_1000| uid < &uid_1000)) + .and_then(|uid| Uid::try_from(1000).ok().map(|uid_1000| uid < &uid_1000)) .unwrap_or(false) } From 90fc0bb8ee05664839547e4f8065941922e59cd5 Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 12:21:02 +0530 Subject: [PATCH 08/14] fix --- src/processes/nix_list.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 1f9e5e0e..941e908a 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -68,7 +68,10 @@ pub fn visible_windows() -> Result> { fn is_system(process: &Process) -> bool { #[cfg(target_os = "macos")] - return process.exe().starts_with("/System/"); + return process.exe() + .map(|path| path.starts_with("/System/")) + .unwrap_or(false); + #[cfg(target_os = "linux")] process .user_id() From 251f704de7c2e871cae2d90d486ecdaf2a64881d Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 06:52:45 +0000 Subject: [PATCH 09/14] [autofix.ci] apply automated fixes --- src/processes/nix_list.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 941e908a..b053b473 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -68,7 +68,8 @@ pub fn visible_windows() -> Result> { fn is_system(process: &Process) -> bool { #[cfg(target_os = "macos")] - return process.exe() + return process + .exe() .map(|path| path.starts_with("/System/")) .unwrap_or(false); From 8d00627c8b06865bae3fa09a153eebbe218598df Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 06:53:49 +0000 Subject: [PATCH 10/14] [autofix.ci] apply automated fixes (attempt 2/3) --- src/processes/nix_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index b053b473..2449e4bd 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -4,7 +4,7 @@ use anyhow::Result; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, Uid, UpdateKind}; +use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; #[cfg(target_os = "macos")] use crate::processes::macos_visible_windows::macos_visible_windows; From 46e9034271ad70393cce81262741bfde0033974a Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 13:01:16 +0530 Subject: [PATCH 11/14] fix --- src/processes/nix_list.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 2449e4bd..25613dc2 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -74,10 +74,11 @@ fn is_system(process: &Process) -> bool { .unwrap_or(false); #[cfg(target_os = "linux")] - process + // process.user_id() returns 0 even if process is started using `sudo` + return process .user_id() - .and_then(|uid| Uid::try_from(1000).ok().map(|uid_1000| uid < &uid_1000)) - .unwrap_or(false) + .and_then(|uid| sysinfo::Uid::try_from(1000).ok().map(|uid_1000| uid < &uid_1000)) + .unwrap_or(false); } #[cfg(test)] From c5afd0d301281177145be63005f826b99cb81f37 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 07:32:41 +0000 Subject: [PATCH 12/14] [autofix.ci] apply automated fixes --- src/processes/nix_list.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index 25613dc2..f6667c70 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -77,7 +77,11 @@ fn is_system(process: &Process) -> bool { // process.user_id() returns 0 even if process is started using `sudo` return process .user_id() - .and_then(|uid| sysinfo::Uid::try_from(1000).ok().map(|uid_1000| uid < &uid_1000)) + .and_then(|uid| { + sysinfo::Uid::try_from(1000) + .ok() + .map(|uid_1000| uid < &uid_1000) + }) .unwrap_or(false); } From e610fa20f5f56aa74cebf7a27a50b05cfa0c4508 Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 14:15:53 +0530 Subject: [PATCH 13/14] restructure macos_visible_windows --- src/processes/macos_visible_windows.rs | 47 ----------------------- src/processes/mod.rs | 3 -- src/processes/nix_list.rs | 53 +++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 51 deletions(-) delete mode 100644 src/processes/macos_visible_windows.rs diff --git a/src/processes/macos_visible_windows.rs b/src/processes/macos_visible_windows.rs deleted file mode 100644 index fcb3a961..00000000 --- a/src/processes/macos_visible_windows.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::intercept_conf::PID; -use anyhow::Result; -use cocoa::base::nil; -use cocoa::foundation::NSString; -use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; -use core_graphics::display::{ - kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, - CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, - CGWindowListCopyWindowInfo, -}; -use std::collections::HashSet; -use std::ffi::c_void; - -pub fn macos_visible_windows() -> Result> { - let mut pids: HashSet = HashSet::new(); - unsafe { - let windows_info_list = CGWindowListCopyWindowInfo( - kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements, - kCGNullWindowID, - ); - let count = CFArrayGetCount(windows_info_list); - - for i in 0..count - 1 { - let dic_ref = CFArrayGetValueAtIndex(windows_info_list, i); - let key = NSString::alloc(nil).init_str("kCGWindowOwnerPID"); - let mut pid: *const c_void = std::ptr::null_mut(); - - if CFDictionaryGetValueIfPresent( - dic_ref as CFDictionaryRef, - key as *const c_void, - &mut pid, - ) != 0 - { - let pid_cf_ref = pid as CFNumberRef; - let mut pid: i32 = 0; - if CFNumberGetValue( - pid_cf_ref, - kCFNumberSInt32Type, - &mut pid as *mut i32 as *mut c_void, - ) { - pids.insert(pid as u32); - } - } - } - Ok(pids) - } -} diff --git a/src/processes/mod.rs b/src/processes/mod.rs index 62cf1a4c..4ba0794c 100644 --- a/src/processes/mod.rs +++ b/src/processes/mod.rs @@ -23,9 +23,6 @@ mod windows_icons; #[cfg(windows)] use self::windows_icons::IconCache; -#[cfg(target_os = "macos")] -mod macos_visible_windows; - #[derive(Debug, Clone)] pub struct ProcessInfo { pub executable: PathBuf, diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index f6667c70..d2ada6cd 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -7,7 +7,7 @@ use std::path::PathBuf; use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; #[cfg(target_os = "macos")] -use crate::processes::macos_visible_windows::macos_visible_windows; +use macos_visible_windows::macos_visible_windows; pub fn active_executables() -> Result { let mut executables: HashMap = HashMap::new(); @@ -85,6 +85,57 @@ fn is_system(process: &Process) -> bool { .unwrap_or(false); } +#[cfg(target_os = "macos")] +mod macos_visible_windows { + use crate::intercept_conf::PID; + use anyhow::Result; + use cocoa::base::nil; + use cocoa::foundation::NSString; + use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; + use core_graphics::display::{ + kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, + CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, + CGWindowListCopyWindowInfo, + }; + use std::collections::HashSet; + use std::ffi::c_void; + + pub fn macos_visible_windows() -> Result> { + let mut pids: HashSet = HashSet::new(); + unsafe { + let windows_info_list = CGWindowListCopyWindowInfo( + kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements, + kCGNullWindowID, + ); + let count = CFArrayGetCount(windows_info_list); + + for i in 0..count - 1 { + let dic_ref = CFArrayGetValueAtIndex(windows_info_list, i); + let key = NSString::alloc(nil).init_str("kCGWindowOwnerPID"); + let mut pid: *const c_void = std::ptr::null_mut(); + + if CFDictionaryGetValueIfPresent( + dic_ref as CFDictionaryRef, + key as *const c_void, + &mut pid, + ) != 0 + { + let pid_cf_ref = pid as CFNumberRef; + let mut pid: i32 = 0; + if CFNumberGetValue( + pid_cf_ref, + kCFNumberSInt32Type, + &mut pid as *mut i32 as *mut c_void, + ) { + pids.insert(pid as u32); + } + } + } + Ok(pids) + } + } +} + #[cfg(test)] mod tests { use super::*; From e06ee81d6de8d8cb81e731e4b0268abda46932db Mon Sep 17 00:00:00 2001 From: errorxyz Date: Sat, 21 Dec 2024 20:57:38 +0530 Subject: [PATCH 14/14] .deref() uids for comparison --- src/processes/nix_list.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/processes/nix_list.rs b/src/processes/nix_list.rs index d2ada6cd..543fa99b 100644 --- a/src/processes/nix_list.rs +++ b/src/processes/nix_list.rs @@ -6,6 +6,9 @@ use std::collections::{HashMap, HashSet}; use std::path::PathBuf; use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; +#[cfg(target_os = "linux")] +use std::ops::Deref; + #[cfg(target_os = "macos")] use macos_visible_windows::macos_visible_windows; @@ -74,14 +77,9 @@ fn is_system(process: &Process) -> bool { .unwrap_or(false); #[cfg(target_os = "linux")] - // process.user_id() returns 0 even if process is started using `sudo` return process .user_id() - .and_then(|uid| { - sysinfo::Uid::try_from(1000) - .ok() - .map(|uid_1000| uid < &uid_1000) - }) + .map(|uid| *uid.deref() < 1000) .unwrap_or(false); }