-
Notifications
You must be signed in to change notification settings - Fork 196
Basic fcntl-style locking. #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1022,6 +1022,58 @@ pub(crate) fn fcntl_add_seals(fd: BorrowedFd<'_>, seals: SealFlags) -> io::Resul | |
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn fcntl_lock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> { | ||
#[cfg(target_pointer_width = "64")] | ||
use linux_raw_sys::general::{flock, F_SETLK, F_SETLKW}; | ||
#[cfg(target_pointer_width = "32")] | ||
use linux_raw_sys::general::{flock64 as flock, F_SETLK64 as F_SETLK, F_SETLKW64 as F_SETLKW}; | ||
use linux_raw_sys::general::{F_RDLCK, F_UNLCK, F_WRLCK}; | ||
|
||
let (cmd, l_type) = match operation { | ||
FlockOperation::LockShared => (F_SETLKW, F_RDLCK), | ||
FlockOperation::LockExclusive => (F_SETLKW, F_WRLCK), | ||
FlockOperation::Unlock => (F_SETLKW, F_UNLCK), | ||
FlockOperation::NonBlockingLockShared => (F_SETLK, F_RDLCK), | ||
FlockOperation::NonBlockingLockExclusive => (F_SETLK, F_WRLCK), | ||
FlockOperation::NonBlockingUnlock => (F_SETLK, F_UNLCK), | ||
}; | ||
|
||
unsafe { | ||
let lock = flock { | ||
l_type: l_type as _, | ||
|
||
// When `l_len` is zero, this locks all the bytes from | ||
// `l_whence`/`l_start` to the end of the file, even as the | ||
// file grows dynamically. | ||
l_whence: SEEK_SET as _, | ||
l_start: 0, | ||
l_len: 0, | ||
|
||
..core::mem::zeroed() | ||
}; | ||
|
||
#[cfg(target_pointer_width = "32")] | ||
{ | ||
ret(syscall_readonly!( | ||
__NR_fcntl64, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately |
||
fd, | ||
c_uint(cmd), | ||
by_ref(&lock) | ||
)) | ||
} | ||
#[cfg(target_pointer_width = "64")] | ||
{ | ||
ret(syscall_readonly!( | ||
__NR_fcntl, | ||
fd, | ||
c_uint(cmd), | ||
by_ref(&lock) | ||
)) | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn rename(oldname: &CStr, newname: &CStr) -> io::Result<()> { | ||
#[cfg(target_arch = "riscv64")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[test] | ||
fn test_fcntl_lock() { | ||
use rustix::fs::{fcntl_lock, FlockOperation}; | ||
|
||
let f = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&f, FlockOperation::LockExclusive).unwrap(); | ||
fcntl_lock(&f, FlockOperation::Unlock).unwrap(); | ||
let g = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&g, FlockOperation::LockExclusive).unwrap(); | ||
fcntl_lock(&g, FlockOperation::Unlock).unwrap(); | ||
drop(f); | ||
drop(g); | ||
|
||
let f = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&f, FlockOperation::LockShared).unwrap(); | ||
let g = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&g, FlockOperation::LockShared).unwrap(); | ||
fcntl_lock(&f, FlockOperation::Unlock).unwrap(); | ||
fcntl_lock(&g, FlockOperation::Unlock).unwrap(); | ||
drop(f); | ||
drop(g); | ||
|
||
let f = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&f, FlockOperation::LockShared).unwrap(); | ||
fcntl_lock(&f, FlockOperation::LockExclusive).unwrap(); | ||
fcntl_lock(&f, FlockOperation::Unlock).unwrap(); | ||
let g = tempfile::tempfile().unwrap(); | ||
fcntl_lock(&g, FlockOperation::LockShared).unwrap(); | ||
fcntl_lock(&g, FlockOperation::LockExclusive).unwrap(); | ||
fcntl_lock(&g, FlockOperation::Unlock).unwrap(); | ||
drop(f); | ||
drop(g); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like we could rely on
core::mem::zeroed()
also handling the zero-init for the other members?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
l_len
to zero is a special case in the documented behavior offcntl
, so I like keeping it explicit. I've now added more comments about this though.