]> Cypherpunks repositories - gostls13.git/commitdiff
os: do not use procfs for os.Executable in dragonfly
authorAntonio Huete Jimenez <tuxillo@quantumachine.net>
Wed, 28 Oct 2020 11:44:26 +0000 (11:44 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Oct 2020 17:08:06 +0000 (17:08 +0000)
  procfs(5) is not always mounted in DragonFly BSD, for example during
  the binary package build with synth. os.Executable() consumers
  will then fail, we've spotted this when trying to build tinygo:

    [...]

    copying source files
    ./build/tinygo build-builtins -target=armv6m-none-eabi [...]
    panic: could not get executable path: readlink /proc/curproc/file:
    no such file or directory

    [...]

  Use KERN_PROC_PATHNAME as FreeBSD does.

Change-Id: Ic65bea02cd0309fb24dec8ba8d2b151d1acde67b
GitHub-Last-Rev: 083120a43b3158bb45d7e1a66fa32e3335a2d407
GitHub-Pull-Request: golang/go#36826
Reviewed-on: https://go-review.googlesource.com/c/go/+/216622
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>

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

diff --git a/src/os/executable_dragonfly.go b/src/os/executable_dragonfly.go
new file mode 100644 (file)
index 0000000..b0deb7b
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2020 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 DragonFly's <sys/sysctl.h>
+const (
+        _CTL_KERN           = 1
+        _KERN_PROC          = 14
+        _KERN_PROC_PATHNAME = 9
+)
index ccaf8e6dd4408c6113747de17a24ed25b4632edd..57930b1b16178df3c68af321bf3636034a46a5a3 100644 (file)
@@ -1,33 +1,12 @@
-// Copyright 2016 The Go Authors. All rights reserved.
+// Copyright 2020 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
 
-import (
-       "syscall"
-       "unsafe"
+// From FreeBSD's <sys/sysctl.h>
+const (
+        _CTL_KERN           = 1
+        _KERN_PROC          = 14
+        _KERN_PROC_PATHNAME = 12
 )
-
-func executable() (string, error) {
-       mib := [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* 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)
-       if err != 0 {
-               return "", err
-       }
-       if n == 0 { // shouldn't happen
-               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)
-       if err != 0 {
-               return "", err
-       }
-       if n == 0 { // shouldn't happen
-               return "", nil
-       }
-       return string(buf[:n-1]), nil
-}
index 2628223b8db8434221be8b33835ca867a5a4050b..5ee41a4b2e28f6dc4579e09ad7a4558257bbdca8 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.
 
-// +build linux netbsd dragonfly js,wasm
+// +build linux netbsd js,wasm
 
 package os
 
@@ -23,8 +23,6 @@ var executablePath, executablePathErr = func() (string, error) {
                procfn = "/proc/self/exe"
        case "netbsd":
                procfn = "/proc/curproc/exe"
-       case "dragonfly":
-               procfn = "/proc/curproc/file"
        }
        return Readlink(procfn)
 }()
diff --git a/src/os/executable_sysctl.go b/src/os/executable_sysctl.go
new file mode 100644 (file)
index 0000000..f9a4b18
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2016 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.
+
+// +build freebsd dragonfly
+
+package os
+
+import (
+       "syscall"
+       "unsafe"
+)
+
+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)
+       if err != 0 {
+               return "", err
+       }
+       if n == 0 { // shouldn't happen
+               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)
+       if err != 0 {
+               return "", err
+       }
+       if n == 0 { // shouldn't happen
+               return "", nil
+       }
+       return string(buf[:n-1]), nil
+}