Skip to content
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
30 changes: 15 additions & 15 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,21 +719,21 @@ The <b>process.info</b> method returns all information on a given process ID. De
"-i"
],
"exe": "/home/user/realm/implants/target/debug/golem",
"environ": [
"USER=user",
"HOME=/home/user",
"PATH=/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/home/user/.dotnet/tools",
"SHELL=/bin/zsh",
"TERM=xterm-256color",
"SSH_TTY=/dev/pts/0",
"SHLVL=1",
"PWD=/home/user",
"OLDPWD=/home/user",
"XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop",
"P9K_TTY=old",
"_P9K_TTY=/dev/pts/0",
"ZSH=/home/user/.oh-my-zsh",
],
"environ": {
"USER": "user",
"HOME": "/home/user",
"PATH": "/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/home/user/.dotnet/tools",
"SHELL": "/bin/zsh",
"TERM": "xterm-256color",
"SSH_TTY": "/dev/pts/0",
"SHLVL": "1",
"PWD": "/home/user",
"OLDPWD": "/home/user",
"XDG_DATA_DIRS": "/usr/local/share:/usr/share:/var/lib/snapd/desktop",
"P9K_TTY": "old",
"_P9K_TTY": "/dev/pts/0",
"ZSH": "/home/user/.oh-my-zsh",
},
"cwd": "/home/user/realm/implants",
"root": "/",
"memory_usage": 32317440,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use eldritch_core::Value;
use spin::RwLock;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

pub fn info(pid: Option<i64>) -> Result<BTreeMap<String, Value>, String> {
Expand All @@ -26,10 +28,21 @@ pub fn info(pid: Option<i64>) -> Result<BTreeMap<String, Value>, String> {
"exe".to_string(),
Value::String(process.exe().display().to_string()),
);

let mut env_map = BTreeMap::new();
for env_str in process.environ() {
if let Some((key, val)) = env_str.split_once('=') {
env_map.insert(
Value::String(key.to_string()),
Value::String(val.to_string()),
);
}
}
map.insert(
"environ".to_string(),
Value::String(process.environ().join(",")),
Value::Dictionary(Arc::new(RwLock::new(env_map))),
);

map.insert(
"cwd".to_string(),
Value::String(process.cwd().display().to_string()),
Expand Down Expand Up @@ -99,6 +112,20 @@ mod tests {
assert!(info.contains_key("cmd"));
assert!(info.contains_key("exe"));
assert!(info.contains_key("environ"));

if let Some(Value::Dictionary(env_dict)) = info.get("environ") {
let env_map = env_dict.read();
// We can't guarantee any specific variable exists across all platforms, but we can check it's a dict.
// On unix/windows PATH or HOME/USERPROFILE usually exists.
// But just checking it is a Dictionary is enough per the request "assert_eq(type(proc['env']), dict)"
// Using >= 0 is always true for usize, effectively we just want to ensure we could access the map.
// So we'll just check that it's a valid map structure which we already did by matching Value::Dictionary.
// Let's print the length just to use the variable and avoid warnings if we don't assert anything.
let _ = env_map.len();
} else {
panic!("environ is not a dictionary");
}

assert!(info.contains_key("cwd"));
assert!(info.contains_key("root"));
assert!(info.contains_key("memory_usage"));
Expand Down
4 changes: 2 additions & 2 deletions tavern/tomes/process_info/main.eldritch
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def process_info(pid):
print("\t- {}".format(nested_value))
elif key == "environ":
print("env_variables=")
for nested_value in value.split(","):
print("\t- {}".format(nested_value))
for k, v in value.items():
print("\t- {}={}".format(k, v))
else:
print("{}={}".format(key, value))

Expand Down
Loading