1use std::fs;
2use std::io::Write;
3use std::path::PathBuf;
4use std::process::Command;
5
6use anyhow::{Context, Result};
7
8use crate::ipc;
9
10fn data_dir() -> PathBuf {
11 dirs::data_local_dir()
12 .unwrap_or_else(|| dirs::home_dir().unwrap_or_default().join(".local/share"))
13 .join("lean-ctx")
14}
15
16pub fn daemon_pid_path() -> PathBuf {
17 data_dir().join("daemon.pid")
18}
19
20pub fn daemon_addr() -> ipc::DaemonAddr {
21 ipc::DaemonAddr::default_for_current_os()
22}
23
24pub fn is_daemon_running() -> bool {
25 let pid_path = daemon_pid_path();
26 let Ok(contents) = fs::read_to_string(&pid_path) else {
27 return false;
28 };
29 let Ok(pid) = contents.trim().parse::<u32>() else {
30 return false;
31 };
32 if ipc::process::is_alive(pid) {
33 return true;
34 }
35 let _ = fs::remove_file(&pid_path);
36 ipc::cleanup(&daemon_addr());
37 false
38}
39
40pub fn read_daemon_pid() -> Option<u32> {
41 let contents = fs::read_to_string(daemon_pid_path()).ok()?;
42 contents.trim().parse::<u32>().ok()
43}
44
45pub fn start_daemon(args: &[String]) -> Result<()> {
46 if is_daemon_running() {
47 let pid = read_daemon_pid().unwrap_or(0);
48 anyhow::bail!("Daemon already running (PID {pid}). Use --stop to stop it first.");
49 }
50
51 ipc::cleanup(&daemon_addr());
52
53 if let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() {
54 crate::config_io::cleanup_legacy_backups(&data_dir);
55 }
56
57 let exe = std::env::current_exe().context("cannot determine own executable path")?;
58
59 let mut cmd_args = vec!["serve".to_string()];
60 for arg in args {
61 if arg == "--daemon" || arg == "-d" {
62 continue;
63 }
64 cmd_args.push(arg.clone());
65 }
66 cmd_args.push("--_foreground-daemon".to_string());
67
68 let log_dir = data_dir();
69 let _ = fs::create_dir_all(&log_dir);
70 let stderr_log = log_dir.join("daemon-stderr.log");
71 let stderr_file = fs::OpenOptions::new()
72 .create(true)
73 .write(true)
74 .truncate(true)
75 .open(&stderr_log);
76 let stderr_cfg = match stderr_file {
77 Ok(f) => std::process::Stdio::from(f),
78 Err(_) => std::process::Stdio::inherit(),
79 };
80
81 let child = Command::new(&exe)
82 .args(&cmd_args)
83 .stdin(std::process::Stdio::null())
84 .stdout(std::process::Stdio::null())
85 .stderr(stderr_cfg)
86 .spawn()
87 .with_context(|| format!("failed to spawn daemon: {}", exe.display()))?;
88
89 let pid = child.id();
90 write_pid_file(pid)?;
91
92 std::thread::sleep(std::time::Duration::from_millis(200));
93
94 if !ipc::process::is_alive(pid) {
95 let _ = fs::remove_file(daemon_pid_path());
96 let stderr_content = fs::read_to_string(&stderr_log).unwrap_or_default();
97 let stderr_trimmed = stderr_content.trim();
98 if stderr_trimmed.is_empty() {
99 anyhow::bail!("Daemon process exited immediately. Check logs for errors.");
100 }
101 anyhow::bail!("Daemon process exited immediately:\n{stderr_trimmed}");
102 }
103
104 let addr = daemon_addr();
105 if crate::core::protocol::meta_visible() {
106 eprintln!(
107 "lean-ctx daemon started (PID {pid})\n Endpoint: {}\n PID file: {}",
108 addr.display(),
109 daemon_pid_path().display()
110 );
111 }
112
113 Ok(())
114}
115
116pub fn stop_daemon() -> Result<()> {
117 let pid_path = daemon_pid_path();
118
119 let Some(pid) = read_daemon_pid() else {
120 eprintln!("No daemon PID file found. Nothing to stop.");
121 return Ok(());
122 };
123
124 if !ipc::process::is_alive(pid) {
125 eprintln!("Daemon (PID {pid}) is not running. Cleaning up stale files.");
126 ipc::cleanup(&daemon_addr());
127 let _ = fs::remove_file(&pid_path);
128 return Ok(());
129 }
130
131 let http_shutdown_ok = try_http_shutdown();
132
133 if http_shutdown_ok {
134 for _ in 0..30 {
135 std::thread::sleep(std::time::Duration::from_millis(100));
136 if !ipc::process::is_alive(pid) {
137 break;
138 }
139 }
140 }
141
142 if ipc::process::is_alive(pid) {
143 let _ = ipc::process::terminate_gracefully(pid);
144 for _ in 0..20 {
145 std::thread::sleep(std::time::Duration::from_millis(100));
146 if !ipc::process::is_alive(pid) {
147 break;
148 }
149 }
150 }
151
152 if ipc::process::is_alive(pid) {
153 eprintln!("Daemon (PID {pid}) did not stop gracefully, force killing.");
154 let _ = ipc::process::force_kill(pid);
155 std::thread::sleep(std::time::Duration::from_millis(200));
156 }
157
158 let _ = fs::remove_file(&pid_path);
159 ipc::cleanup(&daemon_addr());
160 eprintln!("lean-ctx daemon stopped (PID {pid}).");
161
162 let orphans = ipc::process::find_pids_by_name("lean-ctx");
163 if !orphans.is_empty() {
164 eprintln!(" Cleaning up {} orphan process(es)…", orphans.len());
165 ipc::process::kill_all_by_name("lean-ctx");
166 }
167
168 Ok(())
169}
170
171fn try_http_shutdown() -> bool {
172 let Ok(rt) = tokio::runtime::Runtime::new() else {
173 return false;
174 };
175
176 rt.block_on(async {
177 crate::daemon_client::daemon_request("POST", "/v1/shutdown", "")
178 .await
179 .is_ok()
180 })
181}
182
183pub fn daemon_status() -> String {
184 let addr = daemon_addr();
185 if let Some(pid) = read_daemon_pid() {
186 if ipc::process::is_alive(pid) {
187 let listening = addr.is_listening();
188 return format!(
189 "Daemon running (PID {pid})\n Endpoint: {} ({})\n PID file: {}",
190 addr.display(),
191 if listening { "ready" } else { "missing" },
192 daemon_pid_path().display()
193 );
194 }
195 return format!("Daemon not running (stale PID file for PID {pid})");
196 }
197 "Daemon not running".to_string()
198}
199
200fn write_pid_file(pid: u32) -> Result<()> {
201 let pid_path = daemon_pid_path();
202 if let Some(parent) = pid_path.parent() {
203 fs::create_dir_all(parent)
204 .with_context(|| format!("cannot create dir: {}", parent.display()))?;
205 }
206 let mut f = fs::File::create(&pid_path)
207 .with_context(|| format!("cannot write PID file: {}", pid_path.display()))?;
208 write!(f, "{pid}")?;
209 Ok(())
210}
211
212pub fn init_foreground_daemon() -> Result<()> {
214 let pid = std::process::id();
215 write_pid_file(pid)?;
216 Ok(())
217}
218
219pub fn cleanup_daemon_files() {
221 let _ = fs::remove_file(daemon_pid_path());
222 ipc::cleanup(&daemon_addr());
223}