diff options
author | Ruben Pollan <meskio@sindominio.net> | 2018-06-21 11:13:53 +0200 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2018-06-21 13:27:05 +0200 |
commit | a858e94f602ca0824951d4baff441cd13c594e9e (patch) | |
tree | 70209dc1874a57eec97726e6926bc2ed69ed88cd | |
parent | 199ef6f9be94a08580cd6bdbf5d54518e11217c1 (diff) |
[bug] add windows support to the pid file
- Resolves: #43
-rw-r--r-- | pid.go | 34 | ||||
-rw-r--r-- | pid_test.go | 21 |
2 files changed, 46 insertions, 9 deletions
@@ -3,8 +3,10 @@ package main import ( "fmt" "io/ioutil" + "log" "os" "path/filepath" + "runtime" "strconv" "syscall" @@ -20,15 +22,8 @@ func acquirePID() error { return err } - if current != 0 && current != pid { - proc, err := os.FindProcess(current) - if err != nil { - return err - } - err = proc.Signal(syscall.Signal(0)) - if err == nil { - return fmt.Errorf("Another systray is running with pid: %d", current) - } + if current != pid && pidRunning(current) { + return fmt.Errorf("Another systray is running with pid: %d", current) } return setPID(pid) @@ -85,3 +80,24 @@ func setPID(pid int) error { _, err = file.WriteString(fmt.Sprintf("%d", pid)) return err } + +func pidRunning(pid int) bool { + if pid == 0 { + return false + } + + proc, err := os.FindProcess(pid) + if runtime.GOOS == "windows" { + return err == nil + } + + 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 + } + return false +} diff --git a/pid_test.go b/pid_test.go new file mode 100644 index 0000000..2c92641 --- /dev/null +++ b/pid_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "syscall" + "testing" +) + +const ( + invalidPid = 345678 +) + +func TestPidRunning(t *testing.T) { + pid := syscall.Getpid() + if !pidRunning(pid) { + t.Errorf("pid %v is not running", pid) + } + + if pidRunning(invalidPid) { + t.Errorf("pid %v is running", invalidPid) + } +} |