MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / try_acquire_lock

Function try_acquire_lock

block/src/fcntl.rs:197–219  ·  view source on GitHub ↗

Tries to acquire a lock using [`fcntl`] with respect to the given parameters. Please note that `fcntl()` OFD locks are **advisory locks**, which do not prevent to `open()` a file if a lock is already placed. # Parameters - `file`: The file to acquire a lock for [`LockType`]. The file's state will be logically mutated, but not technically. - `lock_type`: The [`LockType`] - `granularity`: The [`Lo

(
    file: &Fd,
    lock_type: LockType,
    granularity: LockGranularity,
)

Source from the content-addressed store, hash-verified

195/// - `lock_type`: The [`LockType`]
196/// - `granularity`: The [`LockGranularity`].
197pub fn try_acquire_lock<Fd: AsRawFd>(
198 file: &Fd,
199 lock_type: LockType,
200 granularity: LockGranularity,
201) -> Result<(), LockError> {
202 let flock = get_flock(lock_type, granularity);
203
204 let res = fcntl(file.as_raw_fd(), FcntlArg::F_OFD_SETLK(&flock));
205 match res {
206 0 => Ok(()),
207 -1 => {
208 let io_error = io::Error::last_os_error();
209 let errno = io_error.raw_os_error().unwrap();
210 match errno {
211 // See man page for error code:
212 // <https://man7.org/linux/man-pages/man2/fcntl.2.html>
213 libc::EAGAIN | libc::EACCES => Err(LockError::AlreadyLocked),
214 _ => Err(LockError::Io(io_error)),
215 }
216 }
217 val => panic!("Unexpected return value from fcntl(): {val}"),
218 }
219}
220
221/// Clears a lock.
222///

Callers 2

clear_lockFunction · 0.85
try_lock_imageMethod · 0.85

Calls 3

get_flockFunction · 0.85
fcntlFunction · 0.85
as_raw_fdMethod · 0.45

Tested by

no test coverage detected