tools: add nonblocking mode to lock_file

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
This commit is contained in:
Stefan Reiter 2020-07-29 14:33:12 +02:00 committed by Dietmar Maurer
parent 505c5f0f76
commit 8cd29fb24a
1 changed files with 13 additions and 0 deletions

View File

@ -91,6 +91,9 @@ pub fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
/// Create a file lock using fntl. This function allows you to specify
/// a timeout if you want to avoid infinite blocking.
///
/// With timeout set to 0, non-blocking mode is used and the function
/// will fail immediately if the lock can't be acquired.
pub fn lock_file<F: AsRawFd>(
file: &mut F,
exclusive: bool,
@ -110,6 +113,16 @@ pub fn lock_file<F: AsRawFd>(
Some(t) => t,
};
if timeout.as_nanos() == 0 {
let lockarg = if exclusive {
nix::fcntl::FlockArg::LockExclusiveNonblock
} else {
nix::fcntl::FlockArg::LockSharedNonblock
};
nix::fcntl::flock(file.as_raw_fd(), lockarg)?;
return Ok(());
}
// unblock the timeout signal temporarily
let _sigblock_guard = timer::unblock_timeout_signal();