Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.FindProcess, process.Release #4218

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/os/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ type Process struct {
}

func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) {
return nil, &PathError{"fork/exec", name, ErrNotImplemented}
return nil, &PathError{Op: "fork/exec", Path: name, Err: ErrNotImplemented}
}

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

Expand All @@ -77,3 +80,15 @@ func Ignore(sig ...Signal) {
// leave all the signals unaltered
return
}

// 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)
}
14 changes: 14 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 @@ -19,3 +20,16 @@ var (
Interrupt Signal = syscall.SIGINT
Kill Signal = syscall.SIGKILL
)

// Keep compatible with golang and always succeed and return new proc with pid on Linux.
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
}
28 changes: 28 additions & 0 deletions src/os/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 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.

package os_test

import (
. "os"
"testing"
)

func TestFindProcess(t *testing.T) {
// NOTE: For now, we only test the Linux case since only exec_posix.go is currently the only implementation.
// 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)
}

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