From 15e3f4925a214589798dc293cc7d0c43a1b6263b Mon Sep 17 00:00:00 2001 From: GameXG Date: Sat, 31 Oct 2020 00:05:42 +0800 Subject: [PATCH] Use the standard library Executable function instead (#28) --- daemon.go | 5 ++--- daemon_darwin.go | 40 -------------------------------------- daemon_freebsd.go | 49 ----------------------------------------------- daemon_linux.go | 22 --------------------- daemon_windows.go | 29 ---------------------------- 5 files changed, 2 insertions(+), 143 deletions(-) delete mode 100644 daemon_darwin.go delete mode 100644 daemon_freebsd.go delete mode 100644 daemon_linux.go delete mode 100755 daemon_windows.go diff --git a/daemon.go b/daemon.go index 1128588..06062ad 100644 --- a/daemon.go +++ b/daemon.go @@ -163,13 +163,12 @@ func MakeDaemon(attrs *DaemonAttr) (io.Reader, io.Reader, error) { } if stage < 2 { - // getExecutablePath() is OS-specific. - procName, err := GetExecutablePath() + procName, err := os.Executable() if err != nil { return fatal(fmt.Errorf("can't determine full path to executable: %s", err)) } - // If getExecutablePath() returns "" but no error, determinating the + // If Executable() returns "" but no error, determinating the // executable path is not implemented on the host OS, so daemonization // is not supported. if len(procName) == 0 { diff --git a/daemon_darwin.go b/daemon_darwin.go deleted file mode 100644 index 06a999b..0000000 --- a/daemon_darwin.go +++ /dev/null @@ -1,40 +0,0 @@ -package godaemon - -// Copyright (c) 2013 VividCortex, Inc. All rights reserved. -// Please see the LICENSE file for applicable license terms. - -//#include -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 - 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 -} diff --git a/daemon_freebsd.go b/daemon_freebsd.go deleted file mode 100644 index 2c93ded..0000000 --- a/daemon_freebsd.go +++ /dev/null @@ -1,49 +0,0 @@ -package godaemon - -// Copyright (c) 2013 VividCortex, Inc. All rights reserved. -// Please see the LICENSE file for applicable license terms. - -//#include -//#include -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 - exePath := make([]byte, PATH_MAX) - exeLen := C.size_t(len(exePath)) - - // Beware: sizeof(int) != sizeof(C.int) - var mib [4]C.int - // From - mib[0] = 1 // CTL_KERN - mib[1] = 14 // KERN_PROC - mib[2] = 12 // KERN_PROC_PATHNAME - mib[3] = -1 - - status, err := C.sysctl((*C.int)(unsafe.Pointer(&mib[0])), 4, unsafe.Pointer(&exePath[0]), &exeLen, nil, 0) - - if err != nil { - return "", fmt.Errorf("sysctl: %v", err) - } - - // Not sure why this might happen with err being nil, but... - if status != 0 { - return "", fmt.Errorf("sysctl 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 -} diff --git a/daemon_linux.go b/daemon_linux.go deleted file mode 100644 index 728f099..0000000 --- a/daemon_linux.go +++ /dev/null @@ -1,22 +0,0 @@ -package godaemon - -// Copyright (c) 2013 VividCortex, Inc. All rights reserved. -// Please see the LICENSE file for applicable license terms. - -import ( - "fmt" - "path/filepath" -) - -// 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) { - exePath, err := Readlink("/proc/self/exe") - - if err != nil { - err = fmt.Errorf("can't read /proc/self/exe: %v", err) - } - - return filepath.Clean(exePath), err -} diff --git a/daemon_windows.go b/daemon_windows.go deleted file mode 100755 index b765c74..0000000 --- a/daemon_windows.go +++ /dev/null @@ -1,29 +0,0 @@ -package godaemon - -// Copyright (c) 2013-2015 VividCortex, Inc. All rights reserved. -// Please see the LICENSE file for applicable license terms. - -import ( - "fmt" - "syscall" - "unicode/utf16" - "unsafe" -) - -var ( - getModuleFileName = syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleFileNameW") -) - -// 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) { - buf := make([]uint16, syscall.MAX_PATH+1) - - res, _, err := getModuleFileName.Call(0, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf))) - if res == 0 || res >= syscall.MAX_PATH || buf[0] == 0 || buf[res-1] == 0 { - return "", fmt.Errorf("GetModuleFileNameW returned %d errno=%d", res, err) - } - - return string(utf16.Decode(buf[:res])), nil -}