forked from VividCortex/godaemon
/
daemon_darwin.go
40 lines (31 loc) · 1.12 KB
/
daemon_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package godaemon
// Copyright (c) 2013 VividCortex, Inc. All rights reserved.
// Please see the LICENSE file for applicable license terms.
//#include <mach-o/dyld.h>
import "C"
import (
"bytes"
"fmt"
"path/filepath"
"unsafe"
)
// GetExecutablePath returns the absolute path to the currently running
// executable. It is used internally by the godaemon package, and exported
// publicly because it's useful outside of the package too.
func GetExecutablePath() (string, error) {
PATH_MAX := 1024 // From <sys/syslimits.h>
exePath := make([]byte, PATH_MAX)
exeLen := C.uint32_t(len(exePath))
status, err := C._NSGetExecutablePath((*C.char)(unsafe.Pointer(&exePath[0])), &exeLen)
if err != nil {
return "", fmt.Errorf("_NSGetExecutablePath: %v", err)
}
// Not sure why this might happen with err being nil, but...
if status != 0 {
return "", fmt.Errorf("_NSGetExecutablePath returned %d", status)
}
// Convert from null-padded []byte to a clean string. (Can't simply cast!)
exePathStringLen := bytes.Index(exePath, []byte{0})
exePathString := string(exePath[:exePathStringLen])
return filepath.Clean(exePathString), nil
}