1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::fs::{self, File};
use std::io::Write;
use std::io::{Error, ErrorKind, Result};
use std::path::Path;
use std::str::FromStr;
pub fn write_pidfile<P>(path: P) -> Result<()>
where
P: AsRef<Path>,
{
return write!(&mut File::create(path)?, "{}", super::getpid());
}
pub fn read_pidfile<P>(path: P) -> Result<super::PID>
where
P: AsRef<Path>,
{
let contents = fs::read_to_string(&path)?;
match FromStr::from_str(&contents) {
Ok(pid) => Ok(pid),
Err(_) => Err(Error::new(
ErrorKind::Other,
"Could not parse pidfile as PID",
)),
}
}