Skip to content
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

Check if process is system on linux #205

Merged
merged 14 commits into from
Dec 23, 2024
55 changes: 0 additions & 55 deletions src/processes/linux_list.rs

This file was deleted.

15 changes: 5 additions & 10 deletions src/processes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub use image;
use std::path::PathBuf;

#[cfg(target_os = "macos")]
mod macos_list;
#[cfg(target_os = "macos")]
pub use self::macos_list::active_executables;
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod nix_list;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use self::nix_list::active_executables;

#[cfg(windows)]
mod windows_list;
Expand All @@ -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 linux_list;
#[cfg(target_os = "linux")]
pub use self::linux_list::active_executables;

#[cfg(target_os = "macos")]
mod macos_icons;
#[cfg(target_os = "macos")]
Expand All @@ -44,7 +39,7 @@ pub static ICON_CACHE: once_cell::sync::Lazy<std::sync::Mutex<IconCache>> =

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;

Expand Down
31 changes: 24 additions & 7 deletions src/processes/macos_list.rs → src/processes/nix_list.rs
Original file line number Diff line number Diff line change
@@ -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")]
mhils marked this conversation as resolved.
Show resolved Hide resolved
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};
Expand Down Expand Up @@ -42,12 +47,16 @@ pub fn active_executables() -> Result<ProcessList> {
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/");
mhils marked this conversation as resolved.
Show resolved Hide resolved
e.insert(ProcessInfo {
executable,
display_name,
Expand All @@ -61,6 +70,7 @@ pub fn active_executables() -> Result<ProcessList> {
Ok(executables.into_values().collect())
}

#[cfg(target_os = "macos")]
pub fn visible_windows() -> Result<HashSet<PID>> {
let mut pids: HashSet<PID> = HashSet::new();
unsafe {
Expand Down Expand Up @@ -96,6 +106,12 @@ pub fn visible_windows() -> Result<HashSet<PID>> {
}
}

#[cfg(not(target_os = "macos"))]
pub fn visible_windows() -> Result<HashSet<PID>> {
// Finding visible windows on other platforms is not worth the effort
errorxyz marked this conversation as resolved.
Show resolved Hide resolved
Ok(HashSet::new())
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -113,6 +129,7 @@ mod tests {
dbg!(lst.len());
}

#[cfg(target_os = "macos")]
#[test]
fn visible_windows_list() {
let open_windows_pids = visible_windows().unwrap();
Expand Down
Loading