syncthing/vendor/github.com/kardianos/osext/osext_sysctl.go

127 lines
3.2 KiB
Go
Raw Normal View History

2014-05-02 08:05:48 +00:00
// Copyright 2012 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 !go1.8,darwin !go1.8,freebsd openbsd
2014-05-02 08:05:48 +00:00
package osext
import (
"os"
2015-11-23 12:09:42 +00:00
"os/exec"
2014-05-02 08:05:48 +00:00
"path/filepath"
"runtime"
"syscall"
"unsafe"
)
2014-07-06 21:13:10 +00:00
var initCwd, initCwdErr = os.Getwd()
2014-05-02 08:05:48 +00:00
func executable() (string, error) {
var mib [4]int32
switch runtime.GOOS {
case "freebsd":
mib = [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1}
case "darwin":
mib = [4]int32{1 /* CTL_KERN */, 38 /* KERN_PROCARGS */, int32(os.Getpid()), -1}
2015-11-23 12:09:42 +00:00
case "openbsd":
mib = [4]int32{1 /* CTL_KERN */, 55 /* KERN_PROC_ARGS */, int32(os.Getpid()), 1 /* KERN_PROC_ARGV */}
2014-05-02 08:05:48 +00:00
}
n := uintptr(0)
2014-07-06 21:13:10 +00:00
// Get length.
_, _, errNum := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0)
if errNum != 0 {
return "", errNum
2014-05-02 08:05:48 +00:00
}
2014-07-06 21:13:10 +00:00
if n == 0 { // This shouldn't happen.
2014-05-02 08:05:48 +00:00
return "", nil
}
buf := make([]byte, n)
2014-07-06 21:13:10 +00:00
_, _, errNum = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0)
if errNum != 0 {
return "", errNum
2014-05-02 08:05:48 +00:00
}
2014-07-06 21:13:10 +00:00
if n == 0 { // This shouldn't happen.
2014-05-02 08:05:48 +00:00
return "", nil
}
2015-11-23 12:09:42 +00:00
var execPath string
switch runtime.GOOS {
case "openbsd":
// buf now contains **argv, with pointers to each of the C-style
// NULL terminated arguments.
var args []string
argv := uintptr(unsafe.Pointer(&buf[0]))
2015-12-22 08:43:25 +00:00
Loop:
for {
argp := *(**[1 << 20]byte)(unsafe.Pointer(argv))
if argp == nil {
break
}
for i := 0; uintptr(i) < n; i++ {
// we don't want the full arguments list
if string(argp[i]) == " " {
break Loop
2015-11-23 12:09:42 +00:00
}
2015-12-22 08:43:25 +00:00
if argp[i] != 0 {
continue
2015-11-23 12:09:42 +00:00
}
2015-12-22 08:43:25 +00:00
args = append(args, string(argp[:i]))
n -= uintptr(i)
break
}
if n < unsafe.Sizeof(argv) {
break
2015-11-23 12:09:42 +00:00
}
2015-12-22 08:43:25 +00:00
argv += unsafe.Sizeof(argv)
n -= unsafe.Sizeof(argv)
}
2015-11-23 12:09:42 +00:00
execPath = args[0]
// There is no canonical way to get an executable path on
// OpenBSD, so check PATH in case we are called directly
if execPath[0] != '/' && execPath[0] != '.' {
execIsInPath, err := exec.LookPath(execPath)
if err == nil {
execPath = execIsInPath
}
2014-05-02 08:05:48 +00:00
}
2015-11-23 12:09:42 +00:00
default:
for i, v := range buf {
if v == 0 {
buf = buf[:i]
break
}
}
execPath = string(buf)
2014-05-02 08:05:48 +00:00
}
2015-11-23 12:09:42 +00:00
2014-07-06 21:13:10 +00:00
var err error
// execPath will not be empty due to above checks.
// Try to get the absolute path if the execPath is not rooted.
if execPath[0] != '/' {
execPath, err = getAbs(execPath)
if err != nil {
return execPath, err
2014-05-02 08:05:48 +00:00
}
}
2014-07-06 21:13:10 +00:00
// For darwin KERN_PROCARGS may return the path to a symlink rather than the
// actual executable.
2014-05-02 08:05:48 +00:00
if runtime.GOOS == "darwin" {
2014-07-06 21:13:10 +00:00
if execPath, err = filepath.EvalSymlinks(execPath); err != nil {
return execPath, err
2014-05-02 08:05:48 +00:00
}
}
2014-07-06 21:13:10 +00:00
return execPath, nil
2014-05-02 08:05:48 +00:00
}
2014-07-06 21:13:10 +00:00
func getAbs(execPath string) (string, error) {
if initCwdErr != nil {
return execPath, initCwdErr
2014-05-02 08:05:48 +00:00
}
2014-07-06 21:13:10 +00:00
// The execPath may begin with a "../" or a "./" so clean it first.
// Join the two paths, trailing and starting slashes undetermined, so use
// the generic Join function.
return filepath.Join(initCwd, filepath.Clean(execPath)), nil
2014-05-02 08:05:48 +00:00
}