Skip to content

std.posix.faccessat: fallback to older faccessat syscall if possible #24900

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1238,11 +1238,15 @@ pub fn access(path: [*:0]const u8, mode: u32) usize {
if (@hasField(SYS, "access")) {
return syscall2(.access, @intFromPtr(path), mode);
} else {
return syscall4(.faccessat, @as(usize, @bitCast(@as(isize, AT.FDCWD))), @intFromPtr(path), mode, 0);
return faccessat(AT.FDCWD, path, mode);
}
}

pub fn faccessat(dirfd: i32, path: [*:0]const u8, mode: u32, flags: u32) usize {
pub fn faccessat(dirfd: i32, path: [*:0]const u8, mode: u32) usize {
return syscall3(.faccessat, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), mode);
}

pub fn faccessat2(dirfd: i32, path: [*:0]const u8, mode: u32, flags: u32) usize {
return syscall4(.faccessat2, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), mode, flags);
}

Expand Down
21 changes: 20 additions & 1 deletion lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5054,7 +5054,22 @@ pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) Acces
} else if (native_os == .wasi and !builtin.link_libc) {
return faccessat(dirfd, mem.sliceTo(path, 0), mode, flags);
}
switch (errno(system.faccessat(dirfd, path, mode, flags))) {

const flags_unsupported = switch (system) {
linux => (builtin.abi.isAndroid() //standard android programs run in a seccomp sandbox that seems to trigger signal 31 (SIGSYS) for faccessat2
or builtin.target.os.isAtLeast(.linux, .{ .major = 5, .minor = 8, .patch = 0 }) != true), //faccessat2 was introduced in Linux 5.8
else => false,
};
const need_flags = (flags != 0);
if (need_flags and flags_unsupported) return error.UnsupportedFlags;
Copy link
Contributor Author

@rohlem rohlem Aug 18, 2025

Choose a reason for hiding this comment

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

So I forgot to check whether error.UnsupportedFlags is in the error set, and it's not.
I took it from the FanotifyInitError error set, because the description seemed to pretty much match the situation here.
Should we add error.UnsupportedFlags to the AccessError error set, or is one of the other errors close enough to be used here?
(On android it's basically a low-level permissions issue, so maybe error.PermissionDenied, but I kind of don't think users care about that technicality.)

const faccessat_result = switch (system) {
linux => if (need_flags) //the older, simpler syscall should stay supported, so we only need to use faccessat2 if we need flags
linux.faccessat2(dirfd, path, mode, flags)
else
linux.faccessat(dirfd, path, mode),
else => system.faccessat(dirfd, path, mode, flags),
};
switch (errno(faccessat_result)) {
.SUCCESS => return,
.ACCES => return error.AccessDenied,
.PERM => return error.PermissionDenied,
Expand All @@ -5072,6 +5087,10 @@ pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) Acces
return error.InvalidUtf8
else
return unexpectedErrno(err),
.NOSYS => |err| if (system == linux)
return error.UnsupportedFlags
else
return unexpectedErrno(err),
else => |err| return unexpectedErrno(err),
}
}
Expand Down
Loading