Switch to using pid checking instead
This commit is contained in:
parent
f66d70836e
commit
59493c5ce4
3 changed files with 15 additions and 9 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -167,6 +167,7 @@ version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"image",
|
"image",
|
||||||
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"stderrlog",
|
"stderrlog",
|
||||||
"structopt",
|
"structopt",
|
||||||
|
|
|
@ -15,3 +15,4 @@ stderrlog = "0.5.1"
|
||||||
xcb-util = { version = "0.3.0", features = ["image", "cursor"] }
|
xcb-util = { version = "0.3.0", features = ["image", "cursor"] }
|
||||||
xcb = "0.9.0"
|
xcb = "0.9.0"
|
||||||
structopt = "0.3.21"
|
structopt = "0.3.21"
|
||||||
|
libc = "0.2.86"
|
||||||
|
|
|
@ -11,9 +11,8 @@ const PATHS: &[&str] = &["/var/run", "/tmp"];
|
||||||
pub fn check_singleton() -> Result<Lockfile> {
|
pub fn check_singleton() -> Result<Lockfile> {
|
||||||
let (mut file, path) = get_lock_file()?;
|
let (mut file, path) = get_lock_file()?;
|
||||||
|
|
||||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();
|
|
||||||
let pid = process::id();
|
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)?;
|
file.write_all(&contents)?;
|
||||||
|
|
||||||
Ok(Lockfile(path))
|
Ok(Lockfile(path))
|
||||||
|
@ -40,15 +39,20 @@ fn get_lock_file() -> Result<(File, PathBuf)> {
|
||||||
Ok(mut f) => {
|
Ok(mut f) => {
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
f.read_to_string(&mut contents)?;
|
f.read_to_string(&mut contents)?;
|
||||||
|
let pid = contents.parse::<i32>()?;
|
||||||
|
|
||||||
let mut parts = contents.split(":");
|
// let mut parts = contents.split(":");
|
||||||
let timestamp = parts.next().unwrap().parse::<u64>()?;
|
// let timestamp = parts.next().unwrap().parse::<u64>()?;
|
||||||
let pid = parts.next().unwrap();
|
// let pid = parts.next().unwrap();
|
||||||
let time = UNIX_EPOCH + Duration::from_millis(timestamp);
|
// let time = UNIX_EPOCH + Duration::from_millis(timestamp);
|
||||||
|
|
||||||
// if it hasn't been 5 minutes since the last open, then bail
|
// if the process that has the lockfile open is still running, bail
|
||||||
if SystemTime::now() - Duration::from_secs(60 * 5) < time {
|
let status = unsafe {libc::kill(pid, 0)};
|
||||||
bail!("existing lockfile found for pid {} at time {:?}", pid, time);
|
|
||||||
|
// 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
|
// ignore errors
|
||||||
|
|
Loading…
Reference in a new issue