Browse Source

revert to old way for windows

Matthew R. Kasun 3 years ago
parent
commit
fcedf7b60f
2 changed files with 15 additions and 7 deletions
  1. 3 0
      netclient/daemon/common.go
  2. 12 7
      netclient/ncutils/pid.go

+ 3 - 0
netclient/daemon/common.go

@@ -33,6 +33,9 @@ func InstallDaemon() error {
 
 // Restart - restarts a system daemon
 func Restart() error {
+	if ncutils.IsWindows() {
+		RestartWindowsDaemon()
+	}
 	pid, err := ncutils.ReadPID()
 	if err != nil {
 		return fmt.Errorf("failed to find pid %w", err)

+ 12 - 7
netclient/ncutils/pid.go

@@ -8,16 +8,22 @@ import (
 
 // PIDFILE - path/name of pid file
 const PIDFILE = "/var/run/netclient.pid"
-const WIN_PIDFILE = "C:\\Windows\\Temp\\netclient.pid"
+
+// WindowsPIDError - error returned from pid function on windows
+type WindowsPIDError struct{}
+
+// Error generates error for windows os
+func (*WindowsPIDError) Error() string {
+	return "pid tracking not supported on windows"
+}
 
 // SavePID - saves the pid of running program to disk
 func SavePID() error {
-	pidfile := PIDFILE
 	if IsWindows() {
-		pidfile = WIN_PIDFILE
+		return &WindowsPIDError{}
 	}
 	pid := os.Getpid()
-	if err := os.WriteFile(pidfile, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
+	if err := os.WriteFile(PIDFILE, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
 		return fmt.Errorf("could not write to pid file %w", err)
 	}
 	return nil
@@ -25,11 +31,10 @@ func SavePID() error {
 
 // ReadPID - reads a previously saved pid from disk
 func ReadPID() (int, error) {
-	pidfile := PIDFILE
 	if IsWindows() {
-		pidfile = WIN_PIDFILE
+		return 0, &WindowsPIDError{}
 	}
-	bytes, err := os.ReadFile(pidfile)
+	bytes, err := os.ReadFile(PIDFILE)
 	if err != nil {
 		return 0, fmt.Errorf("could not read pid file %w", err)
 	}