util: better IsRunning(pid)

Michael Muré created

Change summary

util/process/process.go | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

Detailed changes

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
 }