Skip to content

Commit

Permalink
Add process.Release for unix
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <[email protected]>
  • Loading branch information
leongross committed May 16, 2024
1 parent b2ff648 commit 9fa36f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/os/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)
}

func (p *Process) Wait() (*ProcessState, error) {
if p.Pid == -1 {
return nil, syscall.EINVAL
}
return nil, ErrNotImplemented
}

Expand All @@ -73,6 +76,13 @@ func (p *Process) Signal(sig Signal) error {
return ErrNotImplemented
}

// Release releases any resources associated with the Process p,
// rendering it unusable in the future.
// Release only needs to be called if Wait is not.
func (p *Process) Release() error {
return p.release()
}

// Keep compatibility with golang and always succeed and return new proc with pid on Linux.
func FindProcess(pid int) (*Process, error) {
return findProcess(pid)
Expand Down
9 changes: 9 additions & 0 deletions src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package os

import (
"runtime"
"syscall"
)

Expand All @@ -24,3 +25,11 @@ var (
func findProcess(pid int) (*Process, error) {
return &Process{Pid: pid}, nil
}

func (p *Process) release() error {
// NOOP for unix.
p.Pid = -1
// no need for a finalizer anymore
runtime.SetFinalizer(p, nil)
return nil
}
25 changes: 11 additions & 14 deletions src/os/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ import (

func TestFindProcess(t *testing.T) {
// NOTE: For now, we only test the Linux case since only exec_posix.go is currently the only implementation.
if runtime.GOOS == "linux" {
// Linux guarantees that there is pid 0
proc, err := FindProcess(0)
if err != nil {
t.Error("FindProcess(0): wanted err == nil, got %v:", err)
}

if proc.Pid != 0 {
t.Error("Expected pid 0, got: ", proc.Pid)
}
// Linux guarantees that there is pid 0
proc, err := FindProcess(0)
if err != nil {
t.Error("FindProcess(0): wanted err == nil, got %v:", err)
}

pid0 := Process{Pid: 0}
if *proc != pid0 {
t.Error("Expected &Process{Pid: 0}, got", *proc)
}
if proc.Pid != 0 {
t.Error("Expected pid 0, got: ", proc.Pid)
}

pid0 := Process{Pid: 0}
if *proc != pid0 {
t.Error("Expected &Process{Pid: 0}, got", *proc)
}
}

0 comments on commit 9fa36f7

Please sign in to comment.