Skip to content

Commit

Permalink
add FindProcess for posix
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <leon.gross@9elements.com>
  • Loading branch information
leongross committed Apr 5, 2024
1 parent 2733e37 commit b27bafa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/os/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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) {
Expand All @@ -72,3 +72,8 @@ func (p *Process) Kill() error {
func (p *Process) Signal(sig Signal) error {
return ErrNotImplemented
}

// Keep compatibility with golang and always succeed and return new proc with pid on Linux.
func FindProcess(pid int) (*Process, error) {
return findProcess(pid)
}
5 changes: 5 additions & 0 deletions src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ 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
}
31 changes: 31 additions & 0 deletions src/os/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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"
"runtime"
"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.
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)
}

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

0 comments on commit b27bafa

Please sign in to comment.