diff --git a/src/backend/libc/fs/syscalls.rs b/src/backend/libc/fs/syscalls.rs index 7b00504e5..ec70b38c5 100644 --- a/src/backend/libc/fs/syscalls.rs +++ b/src/backend/libc/fs/syscalls.rs @@ -86,7 +86,7 @@ use crate::fs::Advice; target_os = "solaris", )))] use crate::fs::FallocateFlags; -#[cfg(not(any(target_os = "solaris", target_os = "wasi")))] +#[cfg(not(target_os = "wasi"))] use crate::fs::FlockOperation; #[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))] use crate::fs::MemfdFlags; @@ -968,6 +968,37 @@ pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result unsafe { ret(c::flock(borrowed_fd(fd), operation as c::c_int)) } } +#[cfg(target_os = "solaris")] +pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> { + // Solaris lacks flock(), so try to emulate using fcntl() + let flag = operation as c::c_int; + let mut flock = c::flock { + l_type: 0, + l_whence: 0, + l_start: 0, + l_len: 0, + l_sysid: 0, + l_pid: 0, + l_pad: [0, 0, 0, 0], + }; + flock.l_type = if flag & libc::LOCK_UN != 0 { + libc::F_UNLCK + } else if flag & libc::LOCK_EX != 0 { + libc::F_WRLCK + } else if flag & libc::LOCK_SH != 0 { + libc::F_RDLCK + } else { + panic!("unexpected flock() operation") + }; + + let mut cmd = libc::F_SETLKW; + if (flag & libc::LOCK_NB) != 0 { + cmd = libc::F_SETLK; + } + + unsafe { ret(c::fcntl(borrowed_fd(fd), cmd, &flock)) } +} + pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result { // 32-bit and mips64 Linux: `struct stat64` is not y2038 compatible; use // `statx`. diff --git a/src/backend/libc/fs/types.rs b/src/backend/libc/fs/types.rs index 852593ccc..23863e50e 100644 --- a/src/backend/libc/fs/types.rs +++ b/src/backend/libc/fs/types.rs @@ -850,7 +850,7 @@ bitflags! { /// `LOCK_*` constants for use with [`flock`] /// /// [`flock`]: crate::fs::flock -#[cfg(not(any(target_os = "solaris", target_os = "wasi")))] +#[cfg(not(target_os = "wasi"))] #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(i32)] pub enum FlockOperation { diff --git a/src/fs/fd.rs b/src/fs/fd.rs index 1fede6b49..9a0809bdb 100644 --- a/src/fs/fd.rs +++ b/src/fs/fd.rs @@ -8,7 +8,7 @@ use crate::process::{Gid, Uid}; use crate::{backend, io}; use backend::fd::{AsFd, BorrowedFd}; -#[cfg(not(any(target_os = "solaris", target_os = "wasi")))] +#[cfg(not(target_os = "wasi"))] pub use backend::fs::types::FlockOperation; #[cfg(not(any( @@ -343,7 +343,7 @@ pub fn ftruncate(fd: Fd, length: u64) -> io::Result<()> { /// - [Linux] /// /// [Linux]: https://man7.org/linux/man-pages/man2/flock.2.html -#[cfg(not(any(target_os = "solaris", target_os = "wasi")))] +#[cfg(not(target_os = "wasi"))] #[inline] pub fn flock(fd: Fd, operation: FlockOperation) -> io::Result<()> { backend::fs::syscalls::flock(fd.as_fd(), operation) diff --git a/src/fs/mod.rs b/src/fs/mod.rs index 599bca81c..dd3369410 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -162,7 +162,7 @@ pub use fd::fdatasync; pub use fd::{fallocate, FallocateFlags}; #[cfg(not(target_os = "wasi"))] pub use fd::{fchmod, fchown}; -#[cfg(not(any(target_os = "solaris", target_os = "wasi")))] +#[cfg(not(target_os = "wasi"))] pub use fd::{flock, FlockOperation}; pub use fd::{fstat, fsync, ftruncate, futimens, is_file_read_write, seek, tell, Stat, Timestamps}; #[cfg(not(any(