From 04c2e61f1854c6583cf45849be051c1f31d24110 Mon Sep 17 00:00:00 2001 From: Tyler Saunders Date: Mon, 19 Oct 2026 09:59:58 +0000 Subject: [PATCH] fix(fs): enable openat2 without linux-raw-sys `rustix::fs::openat2` and its libc backend implementation were gated on `#[cfg(linux_raw_dep)]` in commit fda7cbe6 ("rustix: on Linux, support a build without linux-raw-sys (#1478)") because `backend::fs::syscalls::openat2` imported `linux_raw_sys::general::open_how`. Meanwhile, the supporting `ResolveFlags` bitflags and `SYS_OPENAT2` constant in the libc backend remained gated on `#[cfg(linux_kernel)]`. Because `libc::open_how` is not defined for `target_os = "android"` and is marked `#[non_exhaustive]` on `target_os = "linux"`, define `#[repr(C)] struct open_how` locally in `backend::fs::syscalls::openat2` when `#[cfg(not(linux_raw_dep))]` and gate `openat2` on `#[cfg(linux_kernel)]`. This makes `rustix::fs::openat2` usable on Android and Linux when building with `--cfg=rustix_no_linux_raw`. --- src/backend/libc/fs/syscalls.rs | 12 +++++++++++- src/fs/mod.rs | 4 ++-- tests/fs/main.rs | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/backend/libc/fs/syscalls.rs b/src/backend/libc/fs/syscalls.rs index 07000f3dc..4c04ffad8 100644 --- a/src/backend/libc/fs/syscalls.rs +++ b/src/backend/libc/fs/syscalls.rs @@ -1868,7 +1868,7 @@ pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result, path: &CStr, @@ -1876,8 +1876,18 @@ pub(crate) fn openat2( mode: Mode, resolve: ResolveFlags, ) -> io::Result { + #[cfg(linux_raw_dep)] use linux_raw_sys::general::open_how; + #[cfg(not(linux_raw_dep))] + #[repr(C)] + #[derive(Debug, Copy, Clone)] + struct open_how { + flags: u64, + mode: u64, + resolve: u64, + } + syscall! { fn openat2( base_dirfd: c::c_int, diff --git a/src/fs/mod.rs b/src/fs/mod.rs index baf9d7e14..ccac0486e 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -46,7 +46,7 @@ mod ioctl; mod makedev; #[cfg(any(linux_kernel, target_os = "freebsd"))] mod memfd_create; -#[cfg(linux_raw_dep)] +#[cfg(linux_kernel)] mod openat2; #[cfg(linux_kernel)] mod raw_dir; @@ -112,7 +112,7 @@ pub use ioctl::*; pub use makedev::*; #[cfg(any(linux_kernel, target_os = "freebsd"))] pub use memfd_create::memfd_create; -#[cfg(linux_raw_dep)] +#[cfg(linux_kernel)] pub use openat2::openat2; #[cfg(linux_kernel)] pub use raw_dir::{RawDir, RawDirEntry}; diff --git a/tests/fs/main.rs b/tests/fs/main.rs index 84ac2aef3..869190df6 100644 --- a/tests/fs/main.rs +++ b/tests/fs/main.rs @@ -34,7 +34,7 @@ mod mknodat; mod negative_timestamp; #[cfg(linux_kernel)] mod openat; -#[cfg(linux_raw_dep)] +#[cfg(linux_kernel)] mod openat2; #[cfg(not(target_os = "redox"))] mod readdir;