From a1566352733c85bcf1544b2243c258c2ad1dfced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=B3=E5=85=89=E5=B0=91=E5=B9=B4?= <849317537@qq.com> Date: Wed, 31 Jul 2024 06:46:53 +0000 Subject: [PATCH] =?UTF-8?q?--ps=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1ae5816..e35a453 100644 --- a/src/main.rs +++ b/src/main.rs @@ -168,9 +168,9 @@ fn init_container_log(log: bool) -> Result<()> { let log_path = Path::new("logs"); create_dir_and_set777(log_path)?; let log_fd = File::create(log_path.join("log"))?; - let log_fd_raw = log_fd.as_raw_fd(); if log { unsafe { + let log_fd_raw = log_fd.as_raw_fd(); dup2(log_fd_raw, 1)?; dup2(log_fd_raw, 2)?; } @@ -313,7 +313,7 @@ impl Display for ContainerInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let volume: String = self.volume.chars().take(20).collect(); let env: String = self.env.chars().take(20).collect(); - write!(f, "{:<10}{:<8}{:<10}{:<20}{:<20}{:<20}{:<10}", self.id, self.pid, self.image, self.run, volume, env, &self.status) + write!(f, "\x1b[4m{:<10}{:<8}{:<10}{:<20}{:<20}{:<20}{:<10}\x1b[24m", self.id, self.pid, self.image, self.run, volume, env, &self.status) } } @@ -338,11 +338,36 @@ fn show_containers() -> Result<()> { let containers_path = Path::new(WORKSPACE).join("containers"); println!("{:<10}{:<8}{:<10}{:<20}{:<20}{:<20}{:<10}", "id", "pid", "image", "run", "volume", "env", "status"); - for entry in fs::read_dir(containers_path)? { - let path = entry?.path(); - let info_path = path.join(INFO_FILE); - let info_str = fs::read_to_string(info_path)?; - let container_info: ContainerInfo = toml::from_str(&info_str)?; + + let all_container_work_path = fs::read_dir(containers_path)? + .map(|res| res.map(|e| e.path())) + .filter_map(|p| p.ok()) + .collect::>(); + + for container_work_path in all_container_work_path.iter() { + let info_path = container_work_path.join(INFO_FILE); + let mut container_info: ContainerInfo; + if let Ok(info_str) = fs::read_to_string(info_path) { + container_info = toml::from_str(&info_str)?; + } else { + continue; + } + + // 判断是否正在运行, 首先得到该容器所有的fd + let lock_path = container_work_path.join(LOCK_FILE); + let proc_fd_path = Path::new("/proc").join(container_info.pid.to_string()).join("fd"); + let is_running = if let Ok(fd_dir) = fs::read_dir(proc_fd_path) { + fd_dir.filter_map(|p|p.ok()) + .filter_map(|f| fs::read_link(f.path()).ok()) + .any(|p|p == lock_path) + } else { + false + }; + if is_running { + container_info.status = "✅".to_string(); + } else { + container_info.status = "❌".to_string(); + } println!("{}", container_info); } Ok(())