Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Cargo.lock
.idea
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]

name = "pidfile"
version = "0.1.0"
version = "0.1.1"
authors = ["Carl Lerche <[email protected]>"]

[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 = "*"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Pidfile for Rust

Daemon mutual exclusion
Daemon mutual exclusion via a PID file
2 changes: 1 addition & 1 deletion src/ffi_darwin.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi_linux.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
15 changes: 2 additions & 13 deletions src/file_posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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)
}
45 changes: 44 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -195,3 +199,42 @@ pub type LockResult<T> = Result<T, LockError>;
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::<Vec<_>>()
.into_iter()
.map(|t| t.join())
.collect::<Vec<_>>()
.into_iter()
.all(|v| v.is_ok());

assert!(allcan);


}
}