]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os/exec: document a method to check if a process is alive
authorMoritz Poldrack <git@moritz.sh>
Fri, 16 Jun 2023 21:12:25 +0000 (21:12 +0000)
committerGopher Robot <gobot@golang.org>
Sat, 17 Jun 2023 19:02:45 +0000 (19:02 +0000)
Fixes #34396

Change-Id: I35c4e3447f84e349adf7edba92ccb19b324bfe14
GitHub-Last-Rev: 4f06764109ddd9bdfbe4841fc1bebebe026eeb29
GitHub-Pull-Request: golang/go#60763
Reviewed-on: https://go-review.googlesource.com/c/go/+/502815
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/os/exec.go
src/os/exec_unix_test.go

index d01ca592baefedbacb9bf8940272e3e57c29925a..ed5a75c4d13f87405dde7c123896d5a4bf03784a 100644 (file)
@@ -86,7 +86,9 @@ func Getppid() int { return syscall.Getppid() }
 // about the underlying operating system process.
 //
 // On Unix systems, FindProcess always succeeds and returns a Process
-// for the given pid, regardless of whether the process exists.
+// for the given pid, regardless of whether the process exists. To test whether
+// the process actually exists, see whether p.Signal(syscall.Signal(0)) reports
+// an error.
 func FindProcess(pid int) (*Process, error) {
        return findProcess(pid)
 }
index 82c072a746dc3c11dc3b6e6d331cf78bce06508e..26045192ff65531db928ade41fbbce7a0b11efb8 100644 (file)
@@ -9,6 +9,7 @@ package os_test
 import (
        "internal/testenv"
        . "os"
+       "syscall"
        "testing"
 )
 
@@ -25,3 +26,20 @@ func TestErrProcessDone(t *testing.T) {
                t.Errorf("got %v want %v", got, ErrProcessDone)
        }
 }
+
+func TestUNIXProcessAlive(t *testing.T) {
+       testenv.MustHaveGoBuild(t)
+       t.Parallel()
+
+       p, err := StartProcess(testenv.GoToolPath(t), []string{"sleep", "1"}, &ProcAttr{})
+       if err != nil {
+               t.Skipf("starting test process: %v", err)
+       }
+       defer p.Kill()
+
+       proc, _ := FindProcess(p.Pid)
+       err = proc.Signal(syscall.Signal(0))
+       if err != nil {
+               t.Errorf("OS reported error for running process: %v", err)
+       }
+}