From 2fb1e65d17fba1cbf2d53939abb0e26845894602 Mon Sep 17 00:00:00 2001
From: Ruben Pollan <meskio@sindominio.net>
Date: Tue, 23 Oct 2018 12:27:02 -0500
Subject: [PATCH] [bug] check the process name to see if the pid file is valid

When there is a pid file from a previous crash, in some situations the
pid has being reused by the operative system for other processes.
Let's check that the exectuable name maches the name of the systray
binary.

- Resolves: #50
---
 pid.go | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/pid.go b/pid.go
index 3b8e1326..2c283ac5 100644
--- a/pid.go
+++ b/pid.go
@@ -6,11 +6,12 @@ import (
 	"log"
 	"os"
 	"path/filepath"
-	"runtime"
 	"strconv"
+	"strings"
 	"syscall"
 
 	"0xacab.org/leap/bitmask-systray/bitmask"
+	"github.com/mitchellh/go-ps"
 )
 
 var pidFile = filepath.Join(bitmask.ConfigPath, "systray.pid")
@@ -85,19 +86,14 @@ func pidRunning(pid int) bool {
 	if pid == 0 {
 		return false
 	}
-
-	proc, err := os.FindProcess(pid)
-	if runtime.GOOS == "windows" {
-		return err == nil
-	}
-
+	proc, err := ps.FindProcess(pid)
 	if err != nil {
 		log.Printf("An error ocurred finding process: %v", err)
 		return false
 	}
-	err = proc.Signal(syscall.Signal(0))
-	if err == nil {
-		return true
+	if proc == nil {
+		return false
 	}
-	return false
+	log.Printf("There is a running process with the pid %d and executable: %s", pid, proc.Executable())
+	return strings.Contains(os.Args[0], proc.Executable())
 }
-- 
GitLab