Skip to content

Commit

Permalink
Merge branch 'release/0.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Mar 9, 2018
2 parents f30a97d + 46a080f commit 1c7dcd3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
7 changes: 1 addition & 6 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package daemon 0.9.2 for use with Go (golang) services.
Package daemon 0.10.0 for use with Go (golang) services.
Package daemon provides primitives for daemonization of golang services.
This package is not provide implementation of user daemon,
Expand Down Expand Up @@ -184,8 +184,3 @@ type Daemon interface {
func New(name, description string, dependencies ...string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description, dependencies)
}

// ExecPath tries to get executable path
func ExecPath() (string, error) {
return execPath()
}
21 changes: 13 additions & 8 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by
// license that can be found in the LICENSE file.

//+build go1.8

package daemon

import (
Expand All @@ -22,8 +24,8 @@ var (
// ErrUnsupportedSystem appears if try to use service on system which is not supported by this release
ErrUnsupportedSystem = errors.New("Unsupported system")

// ErrRootPriveleges appears if run installation or deleting the service without root privileges
ErrRootPriveleges = errors.New("You must have root user privileges. Possibly using 'sudo' command should help")
// ErrRootPrivileges appears if run installation or deleting the service without root privileges
ErrRootPrivileges = errors.New("You must have root user privileges. Possibly using 'sudo' command should help")

// ErrAlreadyInstalled appears if service already installed on the system
ErrAlreadyInstalled = errors.New("Service has already been installed")
Expand All @@ -38,16 +40,19 @@ var (
ErrAlreadyStopped = errors.New("Service has already been stopped")
)

// ExecPath tries to get executable path
func ExecPath() (string, error) {
return os.Executable()
}

// Lookup path for executable file
func executablePath(name string) (string, error) {
if path, err := exec.LookPath(name); err == nil {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return execPath()
if _, err := os.Stat(path); err == nil {
return path, nil
}
return path, nil
}
return execPath()
return os.Executable()
}

// Check root rights to use system service
Expand All @@ -58,7 +63,7 @@ func checkPrivileges() (bool, error) {
if gid == 0 {
return true, nil
}
return false, ErrRootPriveleges
return false, ErrRootPrivileges
}
}
return false, ErrUnsupportedSystem
Expand Down
70 changes: 70 additions & 0 deletions helper_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

//+build !go1.8

package daemon

import (
"errors"
"os"
"os/exec"
"strconv"
"strings"
)

// Service constants
const (
success = "\t\t\t\t\t[ \033[32mOK\033[0m ]" // Show colored "OK"
failed = "\t\t\t\t\t[\033[31mFAILED\033[0m]" // Show colored "FAILED"
)

var (
// ErrUnsupportedSystem appears if try to use service on system which is not supported by this release
ErrUnsupportedSystem = errors.New("Unsupported system")

// ErrRootPrivileges appears if run installation or deleting the service without root privileges
ErrRootPrivileges = errors.New("You must have root user privileges. Possibly using 'sudo' command should help")

// ErrAlreadyInstalled appears if service already installed on the system
ErrAlreadyInstalled = errors.New("Service has already been installed")

// ErrNotInstalled appears if try to delete service which was not been installed
ErrNotInstalled = errors.New("Service is not installed")

// ErrAlreadyRunning appears if try to start already running service
ErrAlreadyRunning = errors.New("Service is already running")

// ErrAlreadyStopped appears if try to stop already stopped service
ErrAlreadyStopped = errors.New("Service has already been stopped")
)

// ExecPath tries to get executable path
func ExecPath() (string, error) {
return execPath()
}

// Lookup path for executable file
func executablePath(name string) (string, error) {
if path, err := exec.LookPath(name); err == nil {
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return execPath()
}

// Check root rights to use system service
func checkPrivileges() (bool, error) {

if output, err := exec.Command("id", "-g").Output(); err == nil {
if gid, parseErr := strconv.ParseUint(strings.TrimSpace(string(output)), 10, 32); parseErr == nil {
if gid == 0 {
return true, nil
}
return false, ErrRootPrivileges
}
}
return false, ErrUnsupportedSystem
}

0 comments on commit 1c7dcd3

Please sign in to comment.