Skip to content

Commit e3c58bb

Browse files
tuxillobradfitz
authored andcommitted
os: do not use procfs for os.Executable in dragonfly
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: 083120a GitHub-Pull-Request: #36826 Reviewed-on: https://go-review.googlesource.com/c/go/+/216622 Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Trust: Brad Fitzpatrick <[email protected]>
1 parent d4c1ad8 commit e3c58bb

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

src/os/executable_dragonfly.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package os
6+
7+
// From DragonFly's <sys/sysctl.h>
8+
const (
9+
_CTL_KERN = 1
10+
_KERN_PROC = 14
11+
_KERN_PROC_PATHNAME = 9
12+
)

src/os/executable_freebsd.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
1-
// Copyright 2016 The Go Authors. All rights reserved.
1+
// Copyright 2020 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

55
package os
66

7-
import (
8-
"syscall"
9-
"unsafe"
7+
// From FreeBSD's <sys/sysctl.h>
8+
const (
9+
_CTL_KERN = 1
10+
_KERN_PROC = 14
11+
_KERN_PROC_PATHNAME = 12
1012
)
11-
12-
func executable() (string, error) {
13-
mib := [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1}
14-
15-
n := uintptr(0)
16-
// get length
17-
_, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0)
18-
if err != 0 {
19-
return "", err
20-
}
21-
if n == 0 { // shouldn't happen
22-
return "", nil
23-
}
24-
buf := make([]byte, n)
25-
_, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0)
26-
if err != 0 {
27-
return "", err
28-
}
29-
if n == 0 { // shouldn't happen
30-
return "", nil
31-
}
32-
return string(buf[:n-1]), nil
33-
}

src/os/executable_procfs.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build linux netbsd dragonfly js,wasm
5+
// +build linux netbsd js,wasm
66

77
package os
88

@@ -23,8 +23,6 @@ var executablePath, executablePathErr = func() (string, error) {
2323
procfn = "/proc/self/exe"
2424
case "netbsd":
2525
procfn = "/proc/curproc/exe"
26-
case "dragonfly":
27-
procfn = "/proc/curproc/file"
2826
}
2927
return Readlink(procfn)
3028
}()

src/os/executable_sysctl.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build freebsd dragonfly
6+
7+
package os
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
func executable() (string, error) {
15+
mib := [4]int32{_CTL_KERN, _KERN_PROC, _KERN_PROC_PATHNAME, -1}
16+
17+
n := uintptr(0)
18+
// get length
19+
_, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0)
20+
if err != 0 {
21+
return "", err
22+
}
23+
if n == 0 { // shouldn't happen
24+
return "", nil
25+
}
26+
buf := make([]byte, n)
27+
_, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0)
28+
if err != 0 {
29+
return "", err
30+
}
31+
if n == 0 { // shouldn't happen
32+
return "", nil
33+
}
34+
return string(buf[:n-1]), nil
35+
}

0 commit comments

Comments
 (0)