Skip to content

Commit 1c7dcd3

Browse files
committed
Merge branch 'release/0.10.0'
2 parents f30a97d + 46a080f commit 1c7dcd3

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

daemon.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
/*
6-
Package daemon 0.9.2 for use with Go (golang) services.
6+
Package daemon 0.10.0 for use with Go (golang) services.
77
88
Package daemon provides primitives for daemonization of golang services.
99
This package is not provide implementation of user daemon,
@@ -184,8 +184,3 @@ type Daemon interface {
184184
func New(name, description string, dependencies ...string) (Daemon, error) {
185185
return newDaemon(strings.Join(strings.Fields(name), "_"), description, dependencies)
186186
}
187-
188-
// ExecPath tries to get executable path
189-
func ExecPath() (string, error) {
190-
return execPath()
191-
}

helper.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by
33
// license that can be found in the LICENSE file.
44

5+
//+build go1.8
6+
57
package daemon
68

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

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

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

43+
// ExecPath tries to get executable path
44+
func ExecPath() (string, error) {
45+
return os.Executable()
46+
}
47+
4148
// Lookup path for executable file
4249
func executablePath(name string) (string, error) {
4350
if path, err := exec.LookPath(name); err == nil {
44-
_, err := os.Stat(path)
45-
if os.IsNotExist(err) {
46-
return execPath()
51+
if _, err := os.Stat(path); err == nil {
52+
return path, nil
4753
}
48-
return path, nil
4954
}
50-
return execPath()
55+
return os.Executable()
5156
}
5257

5358
// Check root rights to use system service
@@ -58,7 +63,7 @@ func checkPrivileges() (bool, error) {
5863
if gid == 0 {
5964
return true, nil
6065
}
61-
return false, ErrRootPriveleges
66+
return false, ErrRootPrivileges
6267
}
6368
}
6469
return false, ErrUnsupportedSystem

helper_legacy.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by
3+
// license that can be found in the LICENSE file.
4+
5+
//+build !go1.8
6+
7+
package daemon
8+
9+
import (
10+
"errors"
11+
"os"
12+
"os/exec"
13+
"strconv"
14+
"strings"
15+
)
16+
17+
// Service constants
18+
const (
19+
success = "\t\t\t\t\t[ \033[32mOK\033[0m ]" // Show colored "OK"
20+
failed = "\t\t\t\t\t[\033[31mFAILED\033[0m]" // Show colored "FAILED"
21+
)
22+
23+
var (
24+
// ErrUnsupportedSystem appears if try to use service on system which is not supported by this release
25+
ErrUnsupportedSystem = errors.New("Unsupported system")
26+
27+
// ErrRootPrivileges appears if run installation or deleting the service without root privileges
28+
ErrRootPrivileges = errors.New("You must have root user privileges. Possibly using 'sudo' command should help")
29+
30+
// ErrAlreadyInstalled appears if service already installed on the system
31+
ErrAlreadyInstalled = errors.New("Service has already been installed")
32+
33+
// ErrNotInstalled appears if try to delete service which was not been installed
34+
ErrNotInstalled = errors.New("Service is not installed")
35+
36+
// ErrAlreadyRunning appears if try to start already running service
37+
ErrAlreadyRunning = errors.New("Service is already running")
38+
39+
// ErrAlreadyStopped appears if try to stop already stopped service
40+
ErrAlreadyStopped = errors.New("Service has already been stopped")
41+
)
42+
43+
// ExecPath tries to get executable path
44+
func ExecPath() (string, error) {
45+
return execPath()
46+
}
47+
48+
// Lookup path for executable file
49+
func executablePath(name string) (string, error) {
50+
if path, err := exec.LookPath(name); err == nil {
51+
if _, err := os.Stat(path); err == nil {
52+
return path, nil
53+
}
54+
}
55+
return execPath()
56+
}
57+
58+
// Check root rights to use system service
59+
func checkPrivileges() (bool, error) {
60+
61+
if output, err := exec.Command("id", "-g").Output(); err == nil {
62+
if gid, parseErr := strconv.ParseUint(strings.TrimSpace(string(output)), 10, 32); parseErr == nil {
63+
if gid == 0 {
64+
return true, nil
65+
}
66+
return false, ErrRootPrivileges
67+
}
68+
}
69+
return false, ErrUnsupportedSystem
70+
}

0 commit comments

Comments
 (0)