Skip to content

Commit

Permalink
[process][posix] Realign process.Name() with python psutil to return …
Browse files Browse the repository at this point in the history
…same value on python3 scripts processes

e2c79a1 started to blindly set the process name to the full path (instead of the basename) of the cmdline exectuable
if the process name from the process comm was truncated on linux. Python psutil never did that, and this is just wrong
for python (or any excutable interpreted script) where the process name is not the interpreter binary but the script
itself.

A new test to check process name value against psutil value is added here, which would hopefully catch any potential
future changes in psutil.

Reverts #542

Fixes #1485
  • Loading branch information
Lomanic committed Jul 6, 2023
1 parent c0f3eb1 commit 2b6de69
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
2 changes: 0 additions & 2 deletions process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
extendedName := filepath.Base(cmdName)
if strings.HasPrefix(extendedName, p.name) {
name = extendedName
} else {
name = cmdName
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions process/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
extendedName := filepath.Base(cmdlineSlice[0])
if strings.HasPrefix(extendedName, p.name) {
name = extendedName
} else {
name = cmdlineSlice[0]
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,6 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
extendedName := filepath.Base(cmdlineSlice[0])
if strings.HasPrefix(extendedName, p.name) {
p.name = extendedName
} else {
p.name = cmdlineSlice[0]
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions process/process_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
extendedName := filepath.Base(cmdlineSlice[0])
if strings.HasPrefix(extendedName, p.name) {
name = extendedName
} else {
name = cmdlineSlice[0]
}
}
}
Expand Down
57 changes: 57 additions & 0 deletions process/process_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package process

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -391,6 +392,62 @@ func Test_Process_Long_Name(t *testing.T) {
cmd.Process.Kill()
}

func Test_Process_Name_Against_Python(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("only applies to posix")
}
py3Path, err := exec.LookPath("python3")
if err != nil {
t.Skipf("python3 not found: %s", err)
}
if out, err := exec.Command(py3Path, "-c", "import psutil").CombinedOutput(); err != nil {
t.Skipf("psutil not found for %s: %s", py3Path, out)
}

tmpdir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to create temp dir %v", err)
}
defer os.RemoveAll(tmpdir) // clean up
tmpfilepath := filepath.Join(tmpdir, "looooooooooooooooooooong.py")
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
t.Fatalf("unable to create temp file %v", err)
}
tmpfilecontent := []byte("#!" + py3Path + "\nimport psutil, time\nprint(psutil.Process().name(), flush=True)\nwhile True:\n\ttime.sleep(1)")
if _, err := tmpfile.Write(tmpfilecontent); err != nil {
tmpfile.Close()
t.Fatalf("unable to write temp file %v", err)
}
if err := tmpfile.Chmod(0o744); err != nil {
t.Fatalf("unable to chmod u+x temp file %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("unable to close temp file %v", err)
}
cmd := exec.Command(tmpfilepath)
outPipe, _ := cmd.StdoutPipe()
scanner := bufio.NewScanner(outPipe)
cmd.Start()
defer cmd.Process.Kill()
scanner.Scan()
pyName := scanner.Text() // first line printed by py3 script, its name
t.Logf("pyName %s", pyName)
p, err := NewProcess(int32(cmd.Process.Pid))
skipIfNotImplementedErr(t, err)
if err != nil {
t.Fatalf("getting process error %v", err)
}
name, err := p.Name()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Fatalf("getting name error %v", err)
}
if pyName != name {
t.Fatalf("psutil and gopsutil process.Name() results differ: expected %s, got %s", pyName, name)
}
}

func Test_Process_Exe(t *testing.T) {
p := testGetProcess()

Expand Down

0 comments on commit 2b6de69

Please sign in to comment.