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 {
17 next_id: u32,
18}
19
20impl Default for IdCounter {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl IdCounter {
27 pub fn new() -> Self {
28 Self { next_id: 1 }
29 }
30 pub fn next_id(&mut self) -> String {
31 let id = format!("p{}", self.next_id);
32 self.next_id += 1;
33 id
34 }
35}