1use std::fs;
2use std::path::Path;
3
4pub fn is_daemon_alive(pid_path: &Path) -> bool {
5 let content = match fs::read_to_string(pid_path) {
6 Ok(c) => c,
7 Err(_) => return false,
8 };
9 let pid: i32 = match content.trim().parse() {
10 Ok(p) => p,
11 Err(_) => return false,
12 };
13 nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid), None).is_ok()
14}
15
16pub struct IdCounter { next_id: u32 }
17
18impl Default for IdCounter {
19 fn default() -> Self { Self::new() }
20}
21
22impl IdCounter {
23 pub fn new() -> Self { Self { next_id: 1 } }
24 pub fn next_id(&mut self) -> String {
25 let id = format!("p{}", self.next_id);
26 self.next_id += 1;
27 id
28 }
29}