Skip to content

Commit 3023ba5

Browse files
leongrossdeadprogram
authored andcommitted
Add process.Release for unix
Signed-off-by: leongross <[email protected]>
1 parent 3a8ef33 commit 3023ba5

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/os/exec.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)
6262
}
6363

6464
func (p *Process) Wait() (*ProcessState, error) {
65+
if p.Pid == -1 {
66+
return nil, syscall.EINVAL
67+
}
6568
return nil, ErrNotImplemented
6669
}
6770

@@ -78,6 +81,13 @@ func Ignore(sig ...Signal) {
7881
return
7982
}
8083

84+
// Release releases any resources associated with the Process p,
85+
// rendering it unusable in the future.
86+
// Release only needs to be called if Wait is not.
87+
func (p *Process) Release() error {
88+
return p.release()
89+
}
90+
8191
// Keep compatibility with golang and always succeed and return new proc with pid on Linux.
8292
func FindProcess(pid int) (*Process, error) {
8393
return findProcess(pid)

src/os/exec_posix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package os
88

99
import (
10+
"runtime"
1011
"syscall"
1112
)
1213

@@ -24,3 +25,11 @@ var (
2425
func findProcess(pid int) (*Process, error) {
2526
return &Process{Pid: pid}, nil
2627
}
28+
29+
func (p *Process) release() error {
30+
// NOOP for unix.
31+
p.Pid = -1
32+
// no need for a finalizer anymore
33+
runtime.SetFinalizer(p, nil)
34+
return nil
35+
}

src/os/exec_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ import (
1212

1313
func TestFindProcess(t *testing.T) {
1414
// NOTE: For now, we only test the Linux case since only exec_posix.go is currently the only implementation.
15-
if runtime.GOOS == "linux" {
16-
// Linux guarantees that there is pid 0
17-
proc, err := FindProcess(0)
18-
if err != nil {
19-
t.Error("FindProcess(0): wanted err == nil, got %v:", err)
20-
}
21-
22-
if proc.Pid != 0 {
23-
t.Error("Expected pid 0, got: ", proc.Pid)
24-
}
15+
// Linux guarantees that there is pid 0
16+
proc, err := FindProcess(0)
17+
if err != nil {
18+
t.Error("FindProcess(0): wanted err == nil, got %v:", err)
19+
}
2520

26-
pid0 := Process{Pid: 0}
27-
if *proc != pid0 {
28-
t.Error("Expected &Process{Pid: 0}, got", *proc)
29-
}
21+
if proc.Pid != 0 {
22+
t.Error("Expected pid 0, got: ", proc.Pid)
3023
}
3124

25+
pid0 := Process{Pid: 0}
26+
if *proc != pid0 {
27+
t.Error("Expected &Process{Pid: 0}, got", *proc)
28+
}
3229
}

0 commit comments

Comments
 (0)