Skip to main content

agent_procs/daemon/
spawn.rs

1use crate::paths;
2use std::fs;
3use std::path::Path;
4use std::process::Command;
5
6/// Spawns the daemon as a detached background process by re-executing the
7/// current binary with the `--run-daemon SESSION` internal flag.
8/// This avoids the fork-inside-tokio-runtime problem.
9pub fn spawn_daemon(session: &str) -> std::io::Result<()> {
10    // Create socket base dir with restricted permissions
11    let socket_dir = paths::socket_base_dir();
12    fs::create_dir_all(&socket_dir)?;
13    // Set permissions to 0700 (owner only)
14    #[cfg(unix)]
15    {
16        use std::os::unix::fs::PermissionsExt;
17        fs::set_permissions(&socket_dir, fs::Permissions::from_mode(0o700))?;
18    }
19
20    let state = paths::state_dir(session);
21    fs::create_dir_all(state.join("logs"))?;
22
23    let socket_path = paths::socket_path(session);
24    let pid_path = paths::pid_path(session);
25
26    // Remove stale socket if present (ignore ENOENT)
27    let _ = fs::remove_file(&socket_path);
28
29    // Re-exec self with a hidden flag to run as daemon
30    let exe = std::env::current_exe()?;
31
32    // Use double-fork via shell to fully detach
33    let child = Command::new(&exe)
34        .args(["--run-daemon", session])
35        .stdin(std::process::Stdio::null())
36        .stdout(std::process::Stdio::null())
37        .stderr(std::process::Stdio::null())
38        .spawn()?;
39
40    // Detach: we don't want to wait for it (let OS reap it as orphan → init)
41    // Drop the child handle without waiting — it becomes a daemon
42    drop(child);
43
44    wait_for_daemon_ready(&pid_path, &socket_path)
45}
46
47fn wait_for_daemon_ready(pid_path: &Path, socket_path: &Path) -> std::io::Result<()> {
48    // Poll for socket existence, then try to connect to verify it's accepting
49    for _ in 0..100 {
50        if pid_path.exists() && socket_path.exists() {
51            // Try connecting to confirm the server is actually listening
52            if std::os::unix::net::UnixStream::connect(socket_path).is_ok() {
53                return Ok(());
54            }
55        }
56        std::thread::sleep(std::time::Duration::from_millis(50));
57    }
58    Err(std::io::Error::new(std::io::ErrorKind::TimedOut, "daemon did not start within 5s"))
59}
60
61/// Entry point called when running as daemon (via --run-daemon flag in main.rs).
62/// This runs in a fresh process with no tokio runtime yet.
63pub async fn run_daemon(session: &str) {
64    let socket_path = paths::socket_path(session);
65    let pid_path = paths::pid_path(session);
66
67    // Ensure dirs exist
68    let socket_dir = paths::socket_base_dir();
69    let _ = std::fs::create_dir_all(&socket_dir);
70    #[cfg(unix)]
71    {
72        use std::os::unix::fs::PermissionsExt;
73        let _ = std::fs::set_permissions(&socket_dir, std::fs::Permissions::from_mode(0o700));
74    }
75    let state = paths::state_dir(session);
76    let _ = std::fs::create_dir_all(state.join("logs"));
77
78    // Write PID file
79    if let Ok(mut f) = std::fs::File::create(&pid_path) {
80        use std::io::Write;
81        let _ = writeln!(f, "{}", std::process::id());
82    }
83
84    super::server::run(session, &socket_path).await;
85
86    let _ = std::fs::remove_file(&socket_path);
87    let _ = std::fs::remove_file(&pid_path);
88}