diff --git a/Cargo.lock b/Cargo.lock index 63d5d25..4aa76ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,6 +167,7 @@ version = "0.5.0" dependencies = [ "anyhow", "image", + "libc", "log", "stderrlog", "structopt", diff --git a/Cargo.toml b/Cargo.toml index b01aa9b..cfd0fee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/singleton.rs b/src/singleton.rs index 4729c6e..13ee12c 100644 --- a/src/singleton.rs +++ b/src/singleton.rs @@ -11,9 +11,8 @@ const PATHS: &[&str] = &["/var/run", "/tmp"]; pub fn check_singleton() -> Result { 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::()?; - let mut parts = contents.split(":"); - let timestamp = parts.next().unwrap().parse::()?; - let pid = parts.next().unwrap(); - let time = UNIX_EPOCH + Duration::from_millis(timestamp); + // let mut parts = contents.split(":"); + // let timestamp = parts.next().unwrap().parse::()?; + // 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