GRA-408: check if netclient daemon exists to start client daemon service

This commit is contained in:
Abhishek Kondur 2022-08-02 11:19:46 +04:00
parent c537722b76
commit c772d15080
3 changed files with 28 additions and 2 deletions

View file

@ -13,6 +13,11 @@ import (
// InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system.
func InstallDaemon() error {
if ncutils.CheckIfDaemonExists() {
return nil
}
os := runtime.GOOS
var err error

View file

@ -205,7 +205,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error {
logger.Log(0, "network:", cfg.Network, "failed to publish update for join", err.Error())
}
if cfg.Daemon == "install" || ncutils.IsFreeBSD() {
if cfg.Daemon == "on" || ncutils.IsFreeBSD() {
err = daemon.InstallDaemon()
if err != nil {
return err

View file

@ -17,6 +17,7 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/c-robinson/iplib"
@ -89,7 +90,7 @@ func IsLinux() bool {
return runtime.GOOS == "linux"
}
// IsLinux - checks if is linux
// IsFreeBSD - checks if is freebsd
func IsFreeBSD() bool {
return runtime.GOOS == "freebsd"
}
@ -592,3 +593,23 @@ func ModPort(node *models.Node) error {
}
return err
}
func CheckIfDaemonExists() bool {
daemonExists := false
pid, err := ReadPID()
if err != nil {
logger.Log(1, "failed to get netclient PID: ", err.Error())
return daemonExists
}
process, err := os.FindProcess(pid)
if err != nil {
fmt.Printf("Failed to find process: %s\n", err)
return daemonExists
}
err = process.Signal(syscall.Signal(0))
if err == nil {
daemonExists = true
}
return daemonExists
}