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

add processes_info::active_executables for linux #200

Merged
merged 10 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ sysinfo = "0.29.10"

[target.'cfg(target_os = "linux")'.dependencies]
tun = { version = "0.7.5", features = ["async"] }
sysinfo = "0.33.0"
mhils marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
env_logger = "0.11"
Expand Down
10 changes: 5 additions & 5 deletions mitmproxy-rs/src/process_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use pyo3::prelude::*;

#[cfg(any(windows, target_os = "macos"))]
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
use mitmproxy::processes;

#[pyclass(module = "mitmproxy_rs.process_info", frozen)]
Expand Down Expand Up @@ -47,18 +47,18 @@ impl Process {
/// Return a list of all running executables.
/// Note that this groups multiple processes by executable name.
///
/// *Availability: Windows, macOS*
/// *Availability: Windows, macOS, Linux*
#[pyfunction]
pub fn active_executables() -> PyResult<Vec<Process>> {
#[cfg(any(windows, target_os = "macos"))]
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
{
processes::active_executables()
.map(|p| p.into_iter().map(Process).collect())
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{}", e)))
}
#[cfg(not(any(windows, target_os = "macos")))]
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
Err(pyo3::exceptions::PyNotImplementedError::new_err(
"active_executables is only available on Windows",
"active_executables not supported on the current OS",
))
}

Expand Down
55 changes: 55 additions & 0 deletions src/processes/linux_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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<ProcessList> {
let mut executables: HashMap<PathBuf, ProcessInfo> = 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/<pid>/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 = match path.file_name() {
Some(s) => s.to_string_lossy().to_string(),
None => path.to_string_lossy().to_string(),
};
e.insert(ProcessInfo {
executable,
display_name,
is_visible: false,
is_system: false,
});
}
}
}
}
Ok(executables.into_values().collect())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn process_list() {
let lst = active_executables().unwrap();
assert!(!lst.is_empty());
}
}
5 changes: 5 additions & 0 deletions src/processes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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 Down
Loading