From b5544c16cd2f12f26491d3b30191d6f32852d606 Mon Sep 17 00:00:00 2001 From: jackwener Date: Thu, 14 May 2026 16:58:07 +0800 Subject: [PATCH] fix: keep daemon command compatible with pid metadata --- src/cli/daemon_cmd.rs | 61 ++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/src/cli/daemon_cmd.rs b/src/cli/daemon_cmd.rs index 31b0792..ded6827 100644 --- a/src/cli/daemon_cmd.rs +++ b/src/cli/daemon_cmd.rs @@ -1,7 +1,7 @@ -use anyhow::Result; -use crate::config; -use crate::cli::DaemonCommands; use crate::cli::transport; +use crate::cli::DaemonCommands; +use crate::config; +use anyhow::Result; pub fn cmd_daemon(cmd: DaemonCommands) -> Result<()> { match cmd { @@ -15,7 +15,13 @@ fn cmd_status() -> Result<()> { if transport::is_alive() { let pid_path = config::pid_path(); let pid = std::fs::read_to_string(&pid_path) - .map(|s| s.trim().to_string()) + .map(|s| { + serde_json::from_str::(&s) + .ok() + .and_then(|v| v.get("pid").and_then(|p| p.as_u64())) + .map(|pid| pid.to_string()) + .unwrap_or_else(|| s.trim().to_string()) + }) .unwrap_or_else(|_| "?".into()); println!("wx-daemon 运行中 (PID {})", pid); } else { @@ -25,42 +31,13 @@ fn cmd_status() -> Result<()> { } fn cmd_stop() -> Result<()> { - let pid_path = config::pid_path(); - if !pid_path.exists() { + if !transport::is_alive() { println!("daemon 未运行"); return Ok(()); } - let pid_str = std::fs::read_to_string(&pid_path)?; - let pid: u32 = pid_str.trim().parse() - .map_err(|_| anyhow::anyhow!("PID 文件格式错误"))?; - - #[cfg(unix)] - { - let ret = unsafe { libc::kill(pid as libc::pid_t, libc::SIGTERM) }; - if ret != 0 { - let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0); - if errno == libc::ESRCH { - println!("wx-daemon (PID {}) 已不在运行,清理残留文件", pid); - } else { - anyhow::bail!("发送 SIGTERM 失败 (errno {})", errno); - } - } else { - println!("已停止 wx-daemon (PID {})", pid); - } - } - - #[cfg(windows)] - { - std::process::Command::new("taskkill") - .args(["/PID", &pid.to_string(), "/F"]) - .output()?; - println!("已停止 wx-daemon (PID {})", pid); - } - - let _ = std::fs::remove_file(config::sock_path()); - let _ = std::fs::remove_file(&pid_path); - + transport::stop_daemon()?; + println!("已停止 wx-daemon"); Ok(()) } @@ -89,19 +66,25 @@ fn cmd_logs(follow: bool, lines: usize) -> Result<()> { file.read_to_string(&mut content)?; let all_lines: Vec<&str> = content.lines().collect(); let show = &all_lines[all_lines.len().saturating_sub(lines)..]; - for line in show { println!("{}", line); } + for line in show { + println!("{}", line); + } loop { std::thread::sleep(std::time::Duration::from_millis(500)); let mut buf = String::new(); file.read_to_string(&mut buf)?; - if !buf.is_empty() { print!("{}", buf); } + if !buf.is_empty() { + print!("{}", buf); + } } } } else { let content = std::fs::read_to_string(&log_path)?; let all_lines: Vec<&str> = content.lines().collect(); let show = &all_lines[all_lines.len().saturating_sub(lines)..]; - for line in show { println!("{}", line); } + for line in show { + println!("{}", line); + } } Ok(())