|
| 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