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
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
Comment on lines +5090 to +5091
Copy link
Member

Choose a reason for hiding this comment

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

Under what conditions are we expecting to hit this branch?

Copy link
Contributor Author

@rohlem rohlem Aug 25, 2025

Choose a reason for hiding this comment

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

Building for a non-android target range including no support (expected before Linux kernel 5.8) and support (expected Linux kernel 5.8 and later),
being run on a Linux kernel without support (returning ENOSYS from the syscall).

Copy link
Contributor Author

@rohlem rohlem Aug 25, 2025

Choose a reason for hiding this comment

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

I looked at fchmodat2 in the same file for reference, which also does no separate kernel version check at runtime, and instead checks if the syscall fails with .NOSYS.
Two differences I only noticed now: fchmodat2 uses E.init(...) instead of errno(...), and it caches the support in a static (container-scoped) var (atomically accessed) to avoid repeated unsupported calls.

else
return unexpectedErrno(err),
else => |err| return unexpectedErrno(err),
}
}
Expand Down
Loading