From fbd2c099f1c942becb984c52296e30e128a450b3 Mon Sep 17 00:00:00 2001 From: Carlos Hernandez Date: Wed, 22 May 2024 07:01:16 -0600 Subject: [PATCH 1/4] Syscall.signal(0) always returns false on windows --- client/ui/client_ui.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/client/ui/client_ui.go b/client/ui/client_ui.go index 0f16369a5ca..bb80e01b4cf 100644 --- a/client/ui/client_ui.go +++ b/client/ui/client_ui.go @@ -3,6 +3,7 @@ package main import ( + "bufio" "context" _ "embed" "flag" @@ -473,7 +474,6 @@ func (s *serviceClient) updateStatus() error { Stop: backoff.Stop, Clock: backoff.SystemClock, }) - if err != nil { return err } @@ -719,13 +719,40 @@ func checkPIDFile() error { pidFile := path.Join(os.TempDir(), "wiretrustee-ui.pid") if piddata, err := os.ReadFile(pidFile); err == nil { if pid, err := strconv.Atoi(string(piddata)); err == nil { - if process, err := os.FindProcess(pid); err == nil { - if err := process.Signal(syscall.Signal(0)); err == nil { - return fmt.Errorf("process already exists: %d", pid) - } + if pidExists(pid) { + return fmt.Errorf("process already exists: %d", pid) } } } return os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0o664) //nolint:gosec } + +func pidExists(pid int) bool { + if runtime.GOOS != "windows" { + if process, err := os.FindProcess(pid); err == nil { + if err := process.Signal(syscall.Signal(0)); err == nil { + return true + } + } + } + // Prepare the command + cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid)) + + // Get the output + output, err := cmd.Output() + if err != nil { + return false + } + + // Parse the output + scanner := bufio.NewScanner(strings.NewReader(string(output))) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, fmt.Sprintf("%d", pid)) { + return true + } + } + + return false +} From 41b2f6e334966bd6445a2ad3c64a840e3d6b821b Mon Sep 17 00:00:00 2001 From: Carlos Hernandez Date: Wed, 22 May 2024 07:06:47 -0600 Subject: [PATCH 2/4] Update ui pid file name --- client/ui/client_ui.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/ui/client_ui.go b/client/ui/client_ui.go index bb80e01b4cf..5e9a6c8d047 100644 --- a/client/ui/client_ui.go +++ b/client/ui/client_ui.go @@ -716,7 +716,7 @@ func openURL(url string) error { // checkPIDFile exists and return error, or write new. func checkPIDFile() error { - pidFile := path.Join(os.TempDir(), "wiretrustee-ui.pid") + pidFile := path.Join(os.TempDir(), "netbrid-ui.pid") if piddata, err := os.ReadFile(pidFile); err == nil { if pid, err := strconv.Atoi(string(piddata)); err == nil { if pidExists(pid) { From d99f867f59d230ed4c8b0748f0d555be5ae96cb6 Mon Sep 17 00:00:00 2001 From: Carlos Hernandez Date: Thu, 23 May 2024 06:16:59 -0600 Subject: [PATCH 3/4] Update client/ui/client_ui.go Co-authored-by: Viktor Liu --- client/ui/client_ui.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/ui/client_ui.go b/client/ui/client_ui.go index 5e9a6c8d047..5ec3cef467c 100644 --- a/client/ui/client_ui.go +++ b/client/ui/client_ui.go @@ -742,6 +742,7 @@ func pidExists(pid int) bool { // Get the output output, err := cmd.Output() if err != nil { + log.Tracef("Failed to list tasks: %v, output: %s", err, output) return false } From c4b4566d5cbc76183ed0df971bae5beac1954a6f Mon Sep 17 00:00:00 2001 From: Carlos Hernandez Date: Thu, 23 May 2024 06:17:33 -0600 Subject: [PATCH 4/4] Update client/ui/client_ui.go Co-authored-by: Viktor Liu --- client/ui/client_ui.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/ui/client_ui.go b/client/ui/client_ui.go index 5ec3cef467c..630a9648f98 100644 --- a/client/ui/client_ui.go +++ b/client/ui/client_ui.go @@ -716,7 +716,7 @@ func openURL(url string) error { // checkPIDFile exists and return error, or write new. func checkPIDFile() error { - pidFile := path.Join(os.TempDir(), "netbrid-ui.pid") + pidFile := path.Join(os.TempDir(), "netbird-ui.pid") if piddata, err := os.ReadFile(pidFile); err == nil { if pid, err := strconv.Atoi(string(piddata)); err == nil { if pidExists(pid) {