Use osext package for getting the executable name.

This commit is contained in:
Daniel Theophanes
2013-01-11 16:54:30 -08:00
parent a3c4739864
commit da2c993cb8
4 changed files with 13 additions and 71 deletions
+4 -1
View File
@@ -2,6 +2,8 @@
// Currently supports Windows, Linux/Upstart, and OSX/Launchd.
package service
import "bitbucket.org/kardianos/osext"
// Creates a new service. name is the internal name
// and should not contain spaces. Display name is the pretty print
// name. The description is an arbitrary string used to describe the
@@ -59,9 +61,10 @@ type Logger interface {
LogInfo(format string, a ...interface{}) error
}
// Depreciated. Use osext.Executable instead.
// Returns the full path of the running executable
// as reported by the system. Includes the executable
// image name.
func GetExePath() (exePath string, err error) {
return getExePath()
return osext.Executable()
}
+2 -31
View File
@@ -1,25 +1,7 @@
package service
/*
#include <mach-o/dyld.h>
#include <string.h>
int
GetExecPath(char* path, int bufferSize) {
uint32_t size = bufferSize;
if (_NSGetExecutablePath(path, &size) == 0) {
// Despite Apple docs, size does NOT get set in call.
return strlen(path);
} else {
return 0;
}
}
*/
import "C"
import (
"unsafe"
"errors"
"bitbucket.org/kardianos/osext"
"fmt"
"log/syslog"
"os"
@@ -67,7 +49,7 @@ func (s *darwinLaunchdService) Install() error {
}
defer f.Close()
path, err := getExePath()
path, err := osext.Executable()
if err != nil {
return err
}
@@ -133,17 +115,6 @@ func (s *darwinLaunchdService) LogInfo(format string, a ...interface{}) error {
return s.logger.Info(fmt.Sprintf(format, a...))
}
func getExePath() (exePath string, err error) {
buffer := make([]byte, maxPathSize)
size := C.GetExecPath((*C.char)(unsafe.Pointer(&buffer[0])), maxPathSize)
if size == 0 {
return "", errors.New("Unable to get exec path.")
}
buffer = buffer[:size]
ret := string(buffer)
return ret, nil
}
var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
+2 -8
View File
@@ -1,13 +1,13 @@
package service
import (
"bitbucket.org/kardianos/osext"
"fmt"
"log/syslog"
"os"
"os/exec"
"os/signal"
"text/template"
"path/filepath"
)
func newService(name, displayName, description string) (s *linuxUpstartService, err error) {
@@ -43,7 +43,7 @@ func (s *linuxUpstartService) Install() error {
}
defer f.Close()
path, err := getExePath()
path, err := osext.Executable()
if err != nil {
return err
}
@@ -109,12 +109,6 @@ func (s *linuxUpstartService) LogInfo(format string, a ...interface{}) error {
return s.logger.Info(fmt.Sprintf(format, a...))
}
func getExePath() (exePath string, err error) {
exePath, err = os.Readlink(`/proc/self/exe`)
exePath = filepath.Clean(exePath)
return
}
var upstartScript = `# {{.Description}}
description "{{.Display}}"
+5 -31
View File
@@ -1,13 +1,11 @@
package service
import (
"syscall"
"unicode/utf16"
"fmt"
"unsafe"
"code.google.com/p/winsvc/svc"
"code.google.com/p/winsvc/mgr"
"bitbucket.org/kardianos/osext"
"code.google.com/p/winsvc/eventlog"
"code.google.com/p/winsvc/mgr"
"code.google.com/p/winsvc/svc"
"fmt"
)
func newService(name, displayName, description string) (*windowsService, error) {
@@ -57,7 +55,7 @@ loop:
}
func (ws *windowsService) Install() error {
exepath, err := getExePath()
exepath, err := osext.Executable()
if err != nil {
return err
}
@@ -173,27 +171,3 @@ func (ws *windowsService) LogInfo(format string, a ...interface{}) error {
}
return ws.logger.Info(1, fmt.Sprintf(format, a...))
}
func getExePath() (exePath string, err error) {
return getModuleFileName()
}
var (
kernel = syscall.MustLoadDLL("kernel32.dll")
// kernel32.dll
getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW")
)
func getModuleFileName() (string, error) {
var n uint32
b := make([]uint16, syscall.MAX_PATH)
size := uint32(len(b))
r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
n = uint32(r0)
if n == 0 {
return "", e1
}
return string(utf16.Decode(b[0:n])), nil
}