aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2023-03-04 15:24:33 +0100
committerGitHub <noreply@github.com>2023-03-04 15:24:33 +0100
commit9dfd62985cb9fc23637fdfd83440d14bd4225d0d (patch)
tree7fbba9b85705c929ae96ed390fb9c28663037525
parentc038e5d44c05ec6e186b305bb89f57463da64532 (diff)
parent4b62a9450abddfe220fd997583533dd391b524bc (diff)
downloadgit-bug-9dfd62985cb9fc23637fdfd83440d14bd4225d0d.tar.gz
Merge pull request #1031 from MichaelMure/better-isrunning
util: better IsRunning(pid)
-rw-r--r--util/process/process.go20
1 files changed, 15 insertions, 5 deletions
diff --git a/util/process/process.go b/util/process/process.go
index f3bcb7f2..e3f47282 100644
--- a/util/process/process.go
+++ b/util/process/process.go
@@ -9,15 +9,25 @@ import (
func IsRunning(pid int) bool {
// never return no error in a unix system
process, err := os.FindProcess(pid)
-
if err != nil {
return false
}
// Signal 0 doesn't do anything but allow testing the process
err = process.Signal(syscall.Signal(0))
-
- // Todo: distinguish "you don't have access" and "process doesn't exist"
-
- return err == nil
+ if err == nil {
+ return true
+ }
+ if err.Error() == "os: process already finished" {
+ return false
+ }
+ if errno, ok := err.(syscall.Errno); ok {
+ switch errno {
+ case syscall.ESRCH:
+ return false
+ case syscall.EPERM:
+ return true
+ }
+ }
+ return false
}