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
use std::fs::File;
use std::io::{Error, ErrorKind, Result};
use std::io::{Read, Write};
use std::path::Path;
use std::str::FromStr;
pub fn write_pidfile(path: &Path) -> Result<()> {
return write!(&mut File::create(path).unwrap(), "{}", super::getpid());
}
pub fn read_pidfile(path: &Path) -> Result<super::PID> {
let mut file = try!(File::open(path));
let mut contents = String::new();
try!(file.read_to_string(&mut contents));
match FromStr::from_str(&contents) {
Ok(pid) => Ok(pid),
Err(_) => Err(Error::new(
ErrorKind::Other,
"Could not parse pidfile as PID",
)),
}
}