Browse Source

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

Abhishek Kondur 3 years ago
parent
commit
c772d15080
3 changed files with 28 additions and 2 deletions
  1. 5 0
      netclient/daemon/common.go
  2. 1 1
      netclient/functions/join.go
  3. 22 1
      netclient/ncutils/netclientutils.go

+ 5 - 0
netclient/daemon/common.go

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

+ 1 - 1
netclient/functions/join.go

@@ -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())
 		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()
 		err = daemon.InstallDaemon()
 		if err != nil {
 		if err != nil {
 			return err
 			return err

+ 22 - 1
netclient/ncutils/netclientutils.go

@@ -17,6 +17,7 @@ import (
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"syscall"
 	"time"
 	"time"
 
 
 	"github.com/c-robinson/iplib"
 	"github.com/c-robinson/iplib"
@@ -89,7 +90,7 @@ func IsLinux() bool {
 	return runtime.GOOS == "linux"
 	return runtime.GOOS == "linux"
 }
 }
 
 
-// IsLinux - checks if is linux
+// IsFreeBSD - checks if is freebsd
 func IsFreeBSD() bool {
 func IsFreeBSD() bool {
 	return runtime.GOOS == "freebsd"
 	return runtime.GOOS == "freebsd"
 }
 }
@@ -592,3 +593,23 @@ func ModPort(node *models.Node) error {
 	}
 	}
 	return err
 	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
+}