]> Cypherpunks repositories - gostls13.git/commitdiff
os: sysctl-based Executable implementation for NetBSD
authorBenny Siegert <bsiegert@gmail.com>
Mon, 18 Nov 2024 12:43:58 +0000 (13:43 +0100)
committerGopher Robot <gobot@golang.org>
Mon, 18 Nov 2024 19:09:39 +0000 (19:09 +0000)
FreeBSD and Dragonfly have used the sysctl method for years, while
NetBSD has read the name of the executable from /proc. Unfortunately,
some folks are hitting errors when building Go software in a sandbox
that lacks a mounted /proc filesystem.

Switch NetBSD to use the same implementation as FreeBSD and Dragonfly.
Unfortunately, the order of the arguments in the MIB is also
OS-dependent.

Change-Id: I6fd774904af417ccd127e3779af45a20dc8696ca
Reviewed-on: https://go-review.googlesource.com/c/go/+/629035
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Benny Siegert <bsiegert@gmail.com>
TryBot-Bypass: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>

src/os/executable_dragonfly.go
src/os/executable_freebsd.go
src/os/executable_netbsd.go [new file with mode: 0644]
src/os/executable_procfs.go
src/os/executable_sysctl.go

index 19c2ae890f9a53cf1b5ece85aec677c05f1921eb..939c6f6ebbac2159a7cad68db69608afd5bdfeb5 100644 (file)
@@ -10,3 +10,5 @@ const (
        _KERN_PROC          = 14
        _KERN_PROC_PATHNAME = 9
 )
+
+var executableMIB = [4]int32{_CTL_KERN, _KERN_PROC, _KERN_PROC_PATHNAME, -1}
index 95f1a93cb9512c738dc5f40bd5878d746a5546f6..da40fcb32ab913afa24e211259a4d70fb8c05ca8 100644 (file)
@@ -10,3 +10,5 @@ const (
        _KERN_PROC          = 14
        _KERN_PROC_PATHNAME = 12
 )
+
+var executableMIB = [4]int32{_CTL_KERN, _KERN_PROC, _KERN_PROC_PATHNAME, -1}
diff --git a/src/os/executable_netbsd.go b/src/os/executable_netbsd.go
new file mode 100644 (file)
index 0000000..fd07539
--- /dev/null
@@ -0,0 +1,14 @@
+// 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 os
+
+// From NetBSD's <sys/sysctl.h>
+const (
+       _CTL_KERN           = 1
+       _KERN_PROC_ARGS     = 48
+       _KERN_PROC_PATHNAME = 5
+)
+
+var executableMIB = [4]int32{_CTL_KERN, _KERN_PROC_ARGS, -1, _KERN_PROC_PATHNAME}
index 6a2cd10be7c402349784b9779a11869d7f558919..a52631c0bb2fa110234c4c090b2bf6f253b9d129 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build linux || netbsd
+//go:build linux
 
 package os
 
@@ -19,8 +19,6 @@ func executable() (string, error) {
                return "", errors.New("Executable not implemented for " + runtime.GOOS)
        case "linux", "android":
                procfn = "/proc/self/exe"
-       case "netbsd":
-               procfn = "/proc/curproc/exe"
        }
        path, err := Readlink(procfn)
 
index 3c2aeacf7da53f82e43ed698596ac2c4018adf78..8b52e92c413cb1656f8679b977691a3af10be839 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build freebsd || dragonfly
+//go:build freebsd || dragonfly || netbsd
 
 package os
 
@@ -12,11 +12,9 @@ import (
 )
 
 func executable() (string, error) {
-       mib := [4]int32{_CTL_KERN, _KERN_PROC, _KERN_PROC_PATHNAME, -1}
-
        n := uintptr(0)
        // get length
-       _, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0)
+       _, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&executableMIB[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0)
        if err != 0 {
                return "", err
        }
@@ -24,7 +22,7 @@ func executable() (string, error) {
                return "", nil
        }
        buf := make([]byte, n)
-       _, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0)
+       _, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&executableMIB[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0)
        if err != 0 {
                return "", err
        }