-
-
Notifications
You must be signed in to change notification settings - Fork 56
Check if process is system on linux #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f3773dc
check if process is system
errorxyz 21eb6f5
reuse code across macos_list.rs and linux_list.rs
errorxyz bb540e4
nits
errorxyz 3e39fbb
modify is_system heuristic
errorxyz 4bb74de
[autofix.ci] apply automated fixes
autofix-ci[bot] edfa0be
use uid to check if process is system
errorxyz a73bb65
[autofix.ci] apply automated fixes
autofix-ci[bot] 90fc0bb
fix
errorxyz 251f704
[autofix.ci] apply automated fixes
autofix-ci[bot] 8d00627
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] 46e9034
fix
errorxyz c5afd0d
[autofix.ci] apply automated fixes
autofix-ci[bot] e610fa2
restructure macos_visible_windows
errorxyz e06ee81
.deref() uids for comparison
errorxyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| use crate::intercept_conf::PID; | ||
| use crate::processes::{ProcessInfo, ProcessList}; | ||
| use anyhow::Result; | ||
| use std::collections::hash_map::Entry; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::path::PathBuf; | ||
| use sysinfo::{Process, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| use macos_visible_windows::macos_visible_windows; | ||
|
|
||
| pub fn active_executables() -> Result<ProcessList> { | ||
| let mut executables: HashMap<PathBuf, ProcessInfo> = HashMap::new(); | ||
| let visible = visible_windows()?; | ||
| let mut sys = System::new(); | ||
| sys.refresh_processes_specifics( | ||
| ProcessesToUpdate::All, | ||
| true, | ||
| 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/<pid>/exe. | ||
| if let Some(path) = process.exe() { | ||
| let pid = pid.as_u32(); | ||
| let executable = path.to_path_buf(); | ||
| match executables.entry(executable) { | ||
| Entry::Occupied(mut e) => { | ||
| let process_info = e.get(); | ||
| if !process_info.is_visible && visible.contains(&pid) { | ||
| e.get_mut().is_visible = true; | ||
| } | ||
| } | ||
| 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 = is_system(process); | ||
| let is_visible = visible.contains(&pid); | ||
| e.insert(ProcessInfo { | ||
| executable, | ||
| display_name, | ||
| is_visible, | ||
| is_system, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Ok(executables.into_values().collect()) | ||
| } | ||
|
|
||
| pub fn visible_windows() -> Result<HashSet<PID>> { | ||
| #[cfg(target_os = "macos")] | ||
| return macos_visible_windows(); | ||
|
|
||
| #[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()); | ||
| } | ||
|
|
||
| fn is_system(process: &Process) -> bool { | ||
| #[cfg(target_os = "macos")] | ||
| return process | ||
| .exe() | ||
| .map(|path| path.starts_with("/System/")) | ||
| .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) | ||
| }) | ||
| .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<HashSet<PID>> { | ||
| let mut pids: HashSet<PID> = 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::*; | ||
|
|
||
| #[test] | ||
| fn process_list() { | ||
| let lst = active_executables().unwrap(); | ||
| assert!(!lst.is_empty()); | ||
|
|
||
| for proc in &lst { | ||
| if !proc.is_visible { | ||
| dbg!(&proc.display_name); | ||
| } | ||
| } | ||
| dbg!(lst.len()); | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| #[test] | ||
| fn visible_windows_list() { | ||
| let open_windows_pids = visible_windows().unwrap(); | ||
| assert!(!open_windows_pids.is_empty()); | ||
| dbg!(open_windows_pids.len()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.