Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1868,16 +1868,26 @@ pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result<OwnedFd
unsafe { ret_owned_fd(memfd_create(c_str(name), bitflags_bits!(flags))) }
}

#[cfg(linux_raw_dep)]
#[cfg(linux_kernel)]
pub(crate) fn openat2(
dirfd: BorrowedFd<'_>,
path: &CStr,
oflags: OFlags,
mode: Mode,
resolve: ResolveFlags,
) -> io::Result<OwnedFd> {
#[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,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libc (now?) has an open_how declaration. Should we use that instead of declaring this type manually?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been there since v0.2.108 in fact.

@tylersaunders tylersaunders Oct 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't use libc::open_how here for two reasons (I'd love to be wrong here, but this is my read after looking into this):

  1. Not exposed on target_os = "android": In libc, open_how (along with the RESOLVE_* constants) is defined in src/unix/linux_like/linux/mod.rs (#[cfg(target_os = "linux")]) rather than the shared linux_like or android modules, so it is absent on *-linux-android targets (compare x86_64-unknown-linux-gnu docs https://docs.rs/libc/latest/libc/struct.open_how.html vs. aarch64-linux-android docs https://docs.rs/libc/latest/aarch64-linux-android/libc/?search=open_how).

This is the same reason ResolveFlags and SYS_OPENAT2 in rustix's libc backend are defined inline under #[cfg(linux_kernel)] rather than imported from libc.

  1. #[non_exhaustive]: Even on target_os = "linux", libc::open_how is marked #[non_exhaustive], which disallows constructing it via the open_how { flags, mode, resolve } struct literal (E0639) and would require unsafe {core::mem::zeroed() } + field assignment.

Since the kernel ABI for openat2(2) explicitly versions struct open_how by passing size_of::<open_how>() (24 bytes for v0) as the size syscall argument, defining the 3-field #[repr(C)] struct open_how locally when not(linux_raw_dep) works uniformly across both target_os = "linux" and target_os = "android" while keeping the existing struct literal initialization intact.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Android does declare open_how in linux/openat2.h, so an issue should be opened in the libc crate to expose it on Android targets.

Also, open_how should probably be non-exhaustive, per openat2(2):

Any future extensions to openat2() will be implemented as new fields appended to the above structure, with a zero value in a new field resulting in the kernel behaving as though that extension field was not present. Therefore, the caller must zero-fill this structure on initialization.

Context: rust-lang/libc#2477

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue opened: rust-lang/libc#5673.


syscall! {
fn openat2(
base_dirfd: c::c_int,
Expand Down
4 changes: 2 additions & 2 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion tests/fs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading