Skip to main content

ai_agent/utils/
process.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/utils/process.ts
2//! Process management utilities.
3
4use crate::constants::env::system;
5use std::collections::HashMap;
6use std::process::{Command, Stdio};
7
8/// Get the current process ID
9pub fn get_process_id() -> u32 {
10    std::process::id()
11}
12
13/// Get process info as a map
14pub fn get_process_info() -> HashMap<String, String> {
15    let mut info = HashMap::new();
16    info.insert("pid".to_string(), get_process_id().to_string());
17
18    // Add environment info
19    if let Ok(cwd) = std::env::current_dir() {
20        info.insert("cwd".to_string(), cwd.to_string_lossy().to_string());
21    }
22
23    if let Ok(exe) = std::env::current_exe() {
24        info.insert("exe".to_string(), exe.to_string_lossy().to_string());
25    }
26
27    info
28}
29
30/// Check if running in a specific environment
31pub fn is_running_in_container() -> bool {
32    // Check for common container indicators
33    std::env::var(system::DOCKER_CONTAINER).is_ok()
34        || std::env::var(system::KUBERNETES_SERVICE_HOST).is_ok()
35        || std::path::Path::new("/.dockerenv").exists()
36}
37
38/// Get parent process ID
39pub fn get_parent_process_id() -> Option<u32> {
40    // On Unix, this would use std::os::unix::process::parent_id()
41    // For cross-platform, we'd need a crate
42    #[cfg(unix)]
43    {
44        use std::os::unix::process::CommandExt;
45        // This is a simplification; real implementation would need more work
46        None
47    }
48    #[cfg(not(unix))]
49    {
50        None
51    }
52}
53
54/// Run a command and get its output
55pub fn run_command(cmd: &str, args: &[&str]) -> Result<String, String> {
56    let output = Command::new(cmd)
57        .args(args)
58        .stdout(Stdio::piped())
59        .stderr(Stdio::piped())
60        .output()
61        .map_err(|e| e.to_string())?;
62
63    if output.status.success() {
64        Ok(String::from_utf8_lossy(&output.stdout).to_string())
65    } else {
66        Err(String::from_utf8_lossy(&output.stderr).to_string())
67    }
68}