Files
Lucas ManningandHan-Wen Nienhuys 74347f9cf6 Add fallback for openat2 on linux.
This change is necessary for kernels that
don't support openat2.

Change-Id: I8f2c0f6cca4599a3139eba98877a2354cdb1d140
2025-09-14 09:08:46 +00:00

21 lines
557 B
Go

package openat
import (
"golang.org/x/sys/unix"
)
func openatNoSymlinks(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
how := unix.OpenHow{
// os/exec expects all fds to have O_CLOEXEC or it will leak them to subprocesses.
Flags: uint64(flags) | unix.O_CLOEXEC,
Mode: uint64(mode),
Resolve: unix.RESOLVE_NO_SYMLINKS,
}
fd, err = unix.Openat2(dirfd, path, &how)
if err != nil && err == unix.ENOSYS {
flags |= unix.O_NOFOLLOW | unix.O_CLOEXEC
fd, err = unix.Openat(dirfd, path, flags, mode)
}
return fd, err
}