Use osext package for getting the executable name.
This commit is contained in:
+4
-1
@@ -2,6 +2,8 @@
|
|||||||
// Currently supports Windows, Linux/Upstart, and OSX/Launchd.
|
// Currently supports Windows, Linux/Upstart, and OSX/Launchd.
|
||||||
package service
|
package service
|
||||||
|
|
||||||
|
import "bitbucket.org/kardianos/osext"
|
||||||
|
|
||||||
// Creates a new service. name is the internal name
|
// Creates a new service. name is the internal name
|
||||||
// and should not contain spaces. Display name is the pretty print
|
// and should not contain spaces. Display name is the pretty print
|
||||||
// name. The description is an arbitrary string used to describe the
|
// name. The description is an arbitrary string used to describe the
|
||||||
@@ -59,9 +61,10 @@ type Logger interface {
|
|||||||
LogInfo(format string, a ...interface{}) error
|
LogInfo(format string, a ...interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Depreciated. Use osext.Executable instead.
|
||||||
// Returns the full path of the running executable
|
// Returns the full path of the running executable
|
||||||
// as reported by the system. Includes the executable
|
// as reported by the system. Includes the executable
|
||||||
// image name.
|
// image name.
|
||||||
func GetExePath() (exePath string, err error) {
|
func GetExePath() (exePath string, err error) {
|
||||||
return getExePath()
|
return osext.Executable()
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-31
@@ -1,25 +1,7 @@
|
|||||||
package service
|
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 (
|
import (
|
||||||
"unsafe"
|
"bitbucket.org/kardianos/osext"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/syslog"
|
"log/syslog"
|
||||||
"os"
|
"os"
|
||||||
@@ -67,7 +49,7 @@ func (s *darwinLaunchdService) Install() error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
path, err := getExePath()
|
path, err := osext.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -133,17 +115,6 @@ func (s *darwinLaunchdService) LogInfo(format string, a ...interface{}) error {
|
|||||||
return s.logger.Info(fmt.Sprintf(format, a...))
|
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'?>
|
var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
|
||||||
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
|
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
|
||||||
|
|||||||
+2
-8
@@ -1,13 +1,13 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bitbucket.org/kardianos/osext"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/syslog"
|
"log/syslog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"text/template"
|
"text/template"
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newService(name, displayName, description string) (s *linuxUpstartService, err error) {
|
func newService(name, displayName, description string) (s *linuxUpstartService, err error) {
|
||||||
@@ -43,7 +43,7 @@ func (s *linuxUpstartService) Install() error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
path, err := getExePath()
|
path, err := osext.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -109,12 +109,6 @@ func (s *linuxUpstartService) LogInfo(format string, a ...interface{}) error {
|
|||||||
return s.logger.Info(fmt.Sprintf(format, a...))
|
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}}
|
var upstartScript = `# {{.Description}}
|
||||||
|
|
||||||
description "{{.Display}}"
|
description "{{.Display}}"
|
||||||
|
|||||||
+5
-31
@@ -1,13 +1,11 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall"
|
"bitbucket.org/kardianos/osext"
|
||||||
"unicode/utf16"
|
|
||||||
"fmt"
|
|
||||||
"unsafe"
|
|
||||||
"code.google.com/p/winsvc/svc"
|
|
||||||
"code.google.com/p/winsvc/mgr"
|
|
||||||
"code.google.com/p/winsvc/eventlog"
|
"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) {
|
func newService(name, displayName, description string) (*windowsService, error) {
|
||||||
@@ -57,7 +55,7 @@ loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *windowsService) Install() error {
|
func (ws *windowsService) Install() error {
|
||||||
exepath, err := getExePath()
|
exepath, err := osext.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -173,27 +171,3 @@ func (ws *windowsService) LogInfo(format string, a ...interface{}) error {
|
|||||||
}
|
}
|
||||||
return ws.logger.Info(1, fmt.Sprintf(format, a...))
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user