Skip to content

Commit 3a8ef33

Browse files
leongrossdeadprogram
authored andcommitted
add FindProcess for posix
Signed-off-by: leongross <[email protected]>
1 parent 20b58a0 commit 3a8ef33

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/os/exec.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Process struct {
5858
}
5959

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

6464
func (p *Process) Wait() (*ProcessState, error) {
@@ -77,3 +77,8 @@ func Ignore(sig ...Signal) {
7777
// leave all the signals unaltered
7878
return
7979
}
80+
81+
// Keep compatibility with golang and always succeed and return new proc with pid on Linux.
82+
func FindProcess(pid int) (*Process, error) {
83+
return findProcess(pid)
84+
}

src/os/exec_posix.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ var (
1919
Interrupt Signal = syscall.SIGINT
2020
Kill Signal = syscall.SIGKILL
2121
)
22+
23+
// Keep compatible with golang and always succeed and return new proc with pid on Linux.
24+
func findProcess(pid int) (*Process, error) {
25+
return &Process{Pid: pid}, nil
26+
}

src/os/exec_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package os_test
6+
7+
import (
8+
. "os"
9+
"runtime"
10+
"testing"
11+
)
12+
13+
func TestFindProcess(t *testing.T) {
14+
// 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+
}
25+
26+
pid0 := Process{Pid: 0}
27+
if *proc != pid0 {
28+
t.Error("Expected &Process{Pid: 0}, got", *proc)
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)