Skip to content

Commit

Permalink
internal/syscall/unix: implement Eaccess on all unix platforms
Browse files Browse the repository at this point in the history
Eaccess, initially added by CL 414824 for linux only, was later
implemented for freebsd (CL 531155), netbsd (CL 531876), dragonfly
(CL 532675), openbsd (CL 538836), and darwin (CL 579976).

The only unix platforms which lack Eaccess are Solaris/Illumos and AIX.

For AIX, syscall.Faccessat is already available, the only missing piece
was AT_EACCESS constant. Let's take it from [1], which, judging by a few
other known AT_ constants, appears to be accurate.

For Solaris, wire the faccessat using the same logic as in the syscall
package.

Now, when we have faccessat for every unix, we can drop eaccess_other.go
and consolidate Eaccess implementations to use faccessat.

[1]: https://github.com/rust-lang/libc/blob/main/src/unix/aix/mod.rs

Change-Id: I7e1b90dedc5d8174235d3a79d5c662f3dcb909c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/611295
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent 8f2486d commit a0d7bfa
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/internal/syscall/unix/at_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package unix
//go:cgo_import_dynamic libc_unlinkat unlinkat "libc.a/shr_64.o"

const (
AT_EACCESS = 0x1
AT_FDCWD = -0x02
AT_REMOVEDIR = 0x1
AT_SYMLINK_NOFOLLOW = 0x1
UTIME_OMIT = -0x3
Expand Down
3 changes: 3 additions & 0 deletions src/internal/syscall/unix/at_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ func syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err
// Implemented as rawsysvicall6 in runtime/syscall_solaris.go.
func rawSyscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)

//go:cgo_import_dynamic libc_faccessat faccessat "libc.so"
//go:cgo_import_dynamic libc_fstatat fstatat "libc.so"
//go:cgo_import_dynamic libc_openat openat "libc.so"
//go:cgo_import_dynamic libc_unlinkat unlinkat "libc.so"
//go:cgo_import_dynamic libc_uname uname "libc.so"

const (
AT_EACCESS = 0x4
AT_FDCWD = 0xffd19553
AT_REMOVEDIR = 0x1
AT_SYMLINK_NOFOLLOW = 0x1000

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package unix
//go:build unix

import "syscall"
package unix

func Eaccess(path string, mode uint32) error {
return syscall.Faccessat(AT_FDCWD, path, mode, AT_EACCESS)
return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
}
13 changes: 0 additions & 13 deletions src/internal/syscall/unix/eaccess_other.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
}
return err
}

func Eaccess(path string, mode uint32) error {
return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
}
return nil
}

func Eaccess(path string, mode uint32) error {
return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
}
return err
}

func Eaccess(path string, mode uint32) error {
return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
}
28 changes: 28 additions & 0 deletions src/internal/syscall/unix/faccessat_solaris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package unix

import (
"syscall"
"unsafe"
)

//go:linkname procFaccessat libc_faccessat

var procFaccessat uintptr

func faccessat(dirfd int, path string, mode uint32, flags int) error {
p, err := syscall.BytePtrFromString(path)
if err != nil {
return err
}

_, _, errno := syscall6(uintptr(unsafe.Pointer(&procFaccessat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(mode), uintptr(flags), 0, 0)
if errno != 0 {
return errno
}

return nil
}
11 changes: 11 additions & 0 deletions src/internal/syscall/unix/faccessat_syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || linux

package unix

import "syscall"

var faccessat = syscall.Faccessat

0 comments on commit a0d7bfa

Please sign in to comment.