]> Cypherpunks repositories - gostls13.git/commitdiff
os: export errFinished as ErrProcessDone
authorColin Arnott <colin@urandom.co.uk>
Thu, 16 Jul 2020 17:42:47 +0000 (17:42 +0000)
committerTobias Klauser <tobias.klauser@gmail.com>
Sat, 31 Oct 2020 08:41:25 +0000 (08:41 +0000)
(*Process).Signal returns an error sentinel, previously errFinished,
when (*Process).done or syscall.ESRCH. Callers would like the ability to
test for this state, so the value has been exported as ErrProcessDone.

Fixes #39444

Change-Id: I510e7647cc032af290180de5149f35ab7b09a526
Reviewed-on: https://go-review.googlesource.com/c/go/+/242998
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Tobias Klauser <tobias.klauser@gmail.com>

src/os/exec_unix.go
src/os/exec_unix_test.go [new file with mode: 0644]

index 7759a2d2eab2181775716be74b613b1915ff07df..624061297bbe34ccf7ef03b996861be99a9c5b61 100644 (file)
@@ -59,7 +59,8 @@ func (p *Process) wait() (ps *ProcessState, err error) {
        return ps, nil
 }
 
-var errFinished = errors.New("os: process already finished")
+// ErrProcessDone indicates a Process has finished.
+var ErrProcessDone = errors.New("os: process already finished")
 
 func (p *Process) signal(sig Signal) error {
        if p.Pid == -1 {
@@ -71,7 +72,7 @@ func (p *Process) signal(sig Signal) error {
        p.sigMu.RLock()
        defer p.sigMu.RUnlock()
        if p.done() {
-               return errFinished
+               return ErrProcessDone
        }
        s, ok := sig.(syscall.Signal)
        if !ok {
@@ -79,7 +80,7 @@ func (p *Process) signal(sig Signal) error {
        }
        if e := syscall.Kill(p.Pid, s); e != nil {
                if e == syscall.ESRCH {
-                       return errFinished
+                       return ErrProcessDone
                }
                return e
        }
diff --git a/src/os/exec_unix_test.go b/src/os/exec_unix_test.go
new file mode 100644 (file)
index 0000000..d942cdb
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
+
+package os_test
+
+import (
+       "internal/testenv"
+       . "os"
+       "testing"
+)
+
+func TestErrProcessDone(t *testing.T) {
+       testenv.MustHaveGoBuild(t)
+       path, err := testenv.GoTool()
+       if err != nil {
+               t.Errorf("finding go tool: %v", err)
+       }
+       p, err := StartProcess(path, []string{"go"}, &ProcAttr{})
+       if err != nil {
+               t.Errorf("starting test process: %v", err)
+       }
+       p.Wait()
+       if got := p.Signal(Kill); got != ErrProcessDone {
+               t.Errorf("got %v want %v", got, ErrProcessDone)
+       }
+}