summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndy Grover <agrover@redhat.com>2015-08-15 19:08:44 -0700
committerAndy Grover <agrover@redhat.com>2015-08-15 19:08:44 -0700
commit7f73ce34083efa53bb2f0f78064efefb1c70243a (patch)
tree9f7873dd2257309a292fbcbd60e08330d5403da9 /src
parent23dc9e01e191618ea5f90d2b8aed67f57e67652d (diff)
downloadnix-7f73ce34083efa53bb2f0f78064efefb1c70243a.zip
Add flock(2) support
Diffstat (limited to 'src')
-rw-r--r--src/fcntl.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs
index 7893addb..1bc11c74 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -11,6 +11,8 @@ pub use self::ffi::flock;
mod ffi {
pub use libc::{open, fcntl};
pub use self::os::*;
+ pub use libc::funcs::bsd44::flock as libc_flock;
+ pub use libc::consts::os::bsd44::{LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN};
#[cfg(any(target_os = "linux", target_os = "android"))]
mod os {
@@ -128,6 +130,36 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
Ok(res)
}
+pub enum FlockArg {
+ LockShared,
+ LockExclusive,
+ Unlock,
+ LockSharedNonblock,
+ LockExclusiveNonblock,
+ UnlockNonblock,
+}
+
+pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
+ use self::FlockArg::*;
+
+ let res = unsafe {
+ match arg {
+ LockShared => ffi::libc_flock(fd, ffi::LOCK_SH),
+ LockExclusive => ffi::libc_flock(fd, ffi::LOCK_EX),
+ Unlock => ffi::libc_flock(fd, ffi::LOCK_UN),
+ LockSharedNonblock => ffi::libc_flock(fd, ffi::LOCK_SH | ffi::LOCK_NB),
+ LockExclusiveNonblock => ffi::libc_flock(fd, ffi::LOCK_EX | ffi::LOCK_NB),
+ UnlockNonblock => ffi::libc_flock(fd, ffi::LOCK_UN | ffi::LOCK_NB),
+ }
+ };
+
+ if res < 0 {
+ return Err(Error::Sys(Errno::last()));
+ }
+
+ Ok(())
+}
+
#[cfg(any(target_os = "linux", target_os = "android"))]
mod consts {
use libc::c_int;