Switch to using pid checking instead

This commit is contained in:
Michael Zhang 2021-02-27 01:02:22 -06:00
parent f66d70836e
commit 59493c5ce4
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 15 additions and 9 deletions

1
Cargo.lock generated
View file

@ -167,6 +167,7 @@ version = "0.5.0"
dependencies = [
"anyhow",
"image",
"libc",
"log",
"stderrlog",
"structopt",

View file

@ -15,3 +15,4 @@ stderrlog = "0.5.1"
xcb-util = { version = "0.3.0", features = ["image", "cursor"] }
xcb = "0.9.0"
structopt = "0.3.21"
libc = "0.2.86"

View file

@ -11,9 +11,8 @@ const PATHS: &[&str] = &["/var/run", "/tmp"];
pub fn check_singleton() -> Result<Lockfile> {
let (mut file, path) = get_lock_file()?;
let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();
let pid = process::id();
let contents = format!("{}:{}", current_time, pid).as_bytes().to_vec();
let contents = format!("{}", pid).as_bytes().to_vec();
file.write_all(&contents)?;
Ok(Lockfile(path))
@ -40,15 +39,20 @@ fn get_lock_file() -> Result<(File, PathBuf)> {
Ok(mut f) => {
let mut contents = String::new();
f.read_to_string(&mut contents)?;
let pid = contents.parse::<i32>()?;
let mut parts = contents.split(":");
let timestamp = parts.next().unwrap().parse::<u64>()?;
let pid = parts.next().unwrap();
let time = UNIX_EPOCH + Duration::from_millis(timestamp);
// let mut parts = contents.split(":");
// let timestamp = parts.next().unwrap().parse::<u64>()?;
// let pid = parts.next().unwrap();
// let time = UNIX_EPOCH + Duration::from_millis(timestamp);
// if it hasn't been 5 minutes since the last open, then bail
if SystemTime::now() - Duration::from_secs(60 * 5) < time {
bail!("existing lockfile found for pid {} at time {:?}", pid, time);
// if the process that has the lockfile open is still running, bail
let status = unsafe {libc::kill(pid, 0)};
// 0 means the signal was successfully sent, which means it's still running
// if it's 0 then we bail
if status == 0 {
bail!("existing lockfile for pid {}", pid);
}
}
// ignore errors