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..af93151 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,11 @@ [package] name = "pidfile" -version = "0.1.0" +version = "0.1.1" authors = ["Carl Lerche "] [dependencies] -log = "0.3.0" -libc = "0.1.6" - -[dependencies.nix] -git = "https://github.com/carllerche/nix-rust" +log = "0.4.0" +libc = "0.2.0" +nix = "0.7.0" +tempdir = "*" 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/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/file_posix.rs b/src/file_posix.rs index 4415c9c..3f39247 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()); @@ -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) } diff --git a/src/lib.rs b/src/lib.rs index d8ada87..b12653c 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; @@ -14,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; @@ -195,3 +199,42 @@ pub type LockResult = Result; fn pid() -> pid_t { unsafe { libc::getpid() } } + +#[cfg(test)] +mod tests { + + #[test] + fn main() { + use std::thread; + use at; + use tempdir::TempDir; + + let p = TempDir::new("").expect("create temp dir"); + + assert!(p.path().exists()); + + let p = p.path().join("pidfile"); + + // 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); + + + } +}