From a2c8c8e6e8aff18f21ba72c90dad2c3afd4242fd Mon Sep 17 00:00:00 2001 From: Antoni Przygienda Date: Wed, 15 Feb 2017 22:20:53 -0800 Subject: [PATCH 1/5] * added tests, properly would need to start a process and invoke same again to make sure it blocks ... tons work and I'm not even sure whether the file handle lock isn't passed through ... --- .gitignore | 1 + Cargo.toml | 1 + README.md | 2 +- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a9d37c5..64f40ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target Cargo.lock +.idea diff --git a/Cargo.toml b/Cargo.toml index 6e3c4a0..61b0e96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Carl Lerche "] [dependencies] log = "0.3.0" libc = "0.1.6" +tempdir = "*" [dependencies.nix] git = "https://github.com/carllerche/nix-rust" diff --git a/README.md b/README.md index 87176ee..93a1644 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Pidfile for Rust -Daemon mutual exclusion +Daemon mutual exclusion via a PID file diff --git a/src/lib.rs b/src/lib.rs index d8ada87..4f959ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,11 @@ +//! Crate allows to lock a pidfile for a process to prevent another from +//! starting as long the lock is held. + #![crate_name = "pidfile"] extern crate libc; extern crate nix; +extern crate tempdir; #[macro_use] extern crate log; @@ -195,3 +199,30 @@ pub type LockResult = Result; fn pid() -> pid_t { unsafe { libc::getpid() } } + +#[cfg(test)] +mod tests { + + #[test] + fn main() { + use at; + use tempdir::TempDir; + use std::fs::File; + + let p = TempDir::new("").expect("create temp dir"); + + assert!(p.path().exists()); + + let p = p.path().join("pidfile"); + + match File::create(&p) { + Ok(_) => { }, + Err(e) => panic!(format!("cannot create tmp file in {:?} with {:?}",p,&e)), + } + + match at(&p).lock() { + Ok(_) => { }, + Err(e) => panic!(format!("cannot lock in {:?} on start with {:?}",p,&e)), + } + } +} From 910b81f3f7206ee9295b4b9317c6a4e524f8bf11 Mon Sep 17 00:00:00 2001 From: Antoni Przygienda Date: Wed, 15 Feb 2017 22:34:01 -0800 Subject: [PATCH 2/5] * added tests, to test negative (i.e. failure) properly would need to start a process and invoke same again to make sure it blocks ... tons work and I'm not even sure whether the file handle lock isn't passed through ... --- src/lib.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f959ba..ae8a1f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,6 +205,7 @@ mod tests { #[test] fn main() { + use std::thread; use at; use tempdir::TempDir; use std::fs::File; @@ -215,14 +216,26 @@ mod tests { let p = p.path().join("pidfile"); - match File::create(&p) { - Ok(_) => { }, - Err(e) => panic!(format!("cannot create tmp file in {:?} with {:?}",p,&e)), - } + // run couple threads + + let allcan = (0 .. 3) + .map(|_| { + let pc = p.clone(); + let atit = move || { + at(&pc).lock() + }; + + thread::spawn(atit) + }) + .collect::>() + .into_iter() + .map(|t| t.join()) + .collect::>() + .into_iter() + .all(|v| v.is_ok()); + + assert!(allcan); + - match at(&p).lock() { - Ok(_) => { }, - Err(e) => panic!(format!("cannot lock in {:?} on start with {:?}",p,&e)), - } } } From aa1d959c93865751212605b9834da5245d0ddde8 Mon Sep 17 00:00:00 2001 From: Antoni Przygienda Date: Wed, 17 May 2017 19:04:25 -0700 Subject: [PATCH 3/5] * updated for FreeBSD --- src/file_posix.rs | 2 +- src/lib.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/file_posix.rs b/src/file_posix.rs index 4415c9c..638da81 100644 --- a/src/file_posix.rs +++ b/src/file_posix.rs @@ -56,7 +56,7 @@ macro_rules! nix_check { let mut ret; loop { - let res = unsafe { $expr }; + let res = $expr; debug!("ffi; expr={}; success={}", stringify!($expr), res.is_ok()); diff --git a/src/lib.rs b/src/lib.rs index ae8a1f4..fdafe1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,10 @@ mod ffi; #[path = "ffi_linux.rs"] mod ffi; +#[cfg(target_os = "freebsd")] +#[path = "ffi_linux.rs"] +mod ffi; + #[cfg(unix)] #[path = "file_posix.rs"] mod file; @@ -208,7 +212,6 @@ mod tests { use std::thread; use at; use tempdir::TempDir; - use std::fs::File; let p = TempDir::new("").expect("create temp dir"); From ffaadb360bd0214594802a10d70aa6900d8e656a Mon Sep 17 00:00:00 2001 From: Antoni Przygienda Date: Wed, 17 May 2017 23:21:35 -0700 Subject: [PATCH 4/5] * updated for FreeBSD, taken patch from github --- Cargo.toml | 6 ++---- src/ffi_darwin.rs | 2 +- src/ffi_linux.rs | 2 +- src/lib.rs | 6 +----- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61b0e96..797335f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,6 @@ authors = ["Carl Lerche "] [dependencies] log = "0.3.0" -libc = "0.1.6" +libc = "0.2.0" +nix = "0.7.0" tempdir = "*" - -[dependencies.nix] -git = "https://github.com/carllerche/nix-rust" diff --git a/src/ffi_darwin.rs b/src/ffi_darwin.rs index 9c3bcc5..aa70305 100644 --- a/src/ffi_darwin.rs +++ b/src/ffi_darwin.rs @@ -1,7 +1,7 @@ #![allow(non_camel_case_types)] use libc::{off_t, pid_t, c_int, c_short}; -pub use libc::consts::os::extra::O_SYNC; +pub use libc::O_SYNC; #[repr(C)] pub struct flock { diff --git a/src/ffi_linux.rs b/src/ffi_linux.rs index 65e9e35..e1f1ffe 100644 --- a/src/ffi_linux.rs +++ b/src/ffi_linux.rs @@ -1,7 +1,7 @@ #![allow(non_camel_case_types)] use libc::{off_t, pid_t, c_int, c_short}; -pub use libc::consts::os::extra::O_SYNC; +pub use libc::O_SYNC; #[repr(C)] pub struct flock { diff --git a/src/lib.rs b/src/lib.rs index fdafe1f..b12653c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ use std::io::Read; use std::path::{Path, PathBuf}; use std::str::FromStr; -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] #[path = "ffi_darwin.rs"] mod ffi; @@ -26,10 +26,6 @@ mod ffi; #[path = "ffi_linux.rs"] mod ffi; -#[cfg(target_os = "freebsd")] -#[path = "ffi_linux.rs"] -mod ffi; - #[cfg(unix)] #[path = "file_posix.rs"] mod file; From a2c61ad6aa9110c395e68a873f0a3db7fa8ac22d Mon Sep 17 00:00:00 2001 From: "prz@zeta2.ch" Date: Wed, 23 Feb 2022 20:08:25 +0100 Subject: [PATCH 5/5] updated to newest from_raw_os_error --- Cargo.toml | 4 ++-- src/file_posix.rs | 13 +------------ 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 797335f..af93151 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "pidfile" -version = "0.1.0" +version = "0.1.1" authors = ["Carl Lerche "] [dependencies] -log = "0.3.0" +log = "0.4.0" libc = "0.2.0" nix = "0.7.0" tempdir = "*" diff --git a/src/file_posix.rs b/src/file_posix.rs index 638da81..3f39247 100644 --- a/src/file_posix.rs +++ b/src/file_posix.rs @@ -174,16 +174,5 @@ impl Drop for File { } fn from_raw_os_error(err: i32) -> io::Error { - use std::mem; - // TODO: Remove insane hacks once `std::io::Error::from_os_error` lands - // rust-lang/rust#24028 - #[allow(dead_code)] - enum Repr { - Os(i32), - Custom(*const ()), - } - - unsafe { - mem::transmute(Repr::Os(err)) - } + io::Error::from_raw_os_error(err) }