Skip to content

Commit 8b96d2e

Browse files
committed
[process][posix] Realign process.Name() with python psutil to return 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 executable 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
1 parent c0f3eb1 commit 8b96d2e

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

process/process_darwin.go

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
8282
extendedName := filepath.Base(cmdName)
8383
if strings.HasPrefix(extendedName, p.name) {
8484
name = extendedName
85-
} else {
86-
name = cmdName
8785
}
8886
}
8987
}

process/process_freebsd.go

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
5555
extendedName := filepath.Base(cmdlineSlice[0])
5656
if strings.HasPrefix(extendedName, p.name) {
5757
name = extendedName
58-
} else {
59-
name = cmdlineSlice[0]
6058
}
6159
}
6260
}

process/process_linux.go

-2
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,6 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
845845
extendedName := filepath.Base(cmdlineSlice[0])
846846
if strings.HasPrefix(extendedName, p.name) {
847847
p.name = extendedName
848-
} else {
849-
p.name = cmdlineSlice[0]
850848
}
851849
}
852850
}

process/process_openbsd.go

-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
6060
extendedName := filepath.Base(cmdlineSlice[0])
6161
if strings.HasPrefix(extendedName, p.name) {
6262
name = extendedName
63-
} else {
64-
name = cmdlineSlice[0]
6563
}
6664
}
6765
}

process/process_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package process
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io/ioutil"
@@ -391,6 +392,62 @@ func Test_Process_Long_Name(t *testing.T) {
391392
cmd.Process.Kill()
392393
}
393394

395+
func Test_Process_Name_Against_Python(t *testing.T) {
396+
if runtime.GOOS == "windows" {
397+
t.Skip("only applies to posix")
398+
}
399+
py3Path, err := exec.LookPath("python3")
400+
if err != nil {
401+
t.Skipf("python3 not found: %s", err)
402+
}
403+
if out, err := exec.Command(py3Path, "-c", "import psutil").CombinedOutput(); err != nil {
404+
t.Skipf("psutil not found for %s: %s", py3Path, out)
405+
}
406+
407+
tmpdir, err := ioutil.TempDir("", "")
408+
if err != nil {
409+
t.Fatalf("unable to create temp dir %v", err)
410+
}
411+
defer os.RemoveAll(tmpdir) // clean up
412+
tmpfilepath := filepath.Join(tmpdir, "looooooooooooooooooooong.py")
413+
tmpfile, err := os.Create(tmpfilepath)
414+
if err != nil {
415+
t.Fatalf("unable to create temp file %v", err)
416+
}
417+
tmpfilecontent := []byte("#!" + py3Path + "\nimport psutil, time\nprint(psutil.Process().name(), flush=True)\nwhile True:\n\ttime.sleep(1)")
418+
if _, err := tmpfile.Write(tmpfilecontent); err != nil {
419+
tmpfile.Close()
420+
t.Fatalf("unable to write temp file %v", err)
421+
}
422+
if err := tmpfile.Chmod(0o744); err != nil {
423+
t.Fatalf("unable to chmod u+x temp file %v", err)
424+
}
425+
if err := tmpfile.Close(); err != nil {
426+
t.Fatalf("unable to close temp file %v", err)
427+
}
428+
cmd := exec.Command(tmpfilepath)
429+
outPipe, _ := cmd.StdoutPipe()
430+
scanner := bufio.NewScanner(outPipe)
431+
cmd.Start()
432+
defer cmd.Process.Kill()
433+
scanner.Scan()
434+
pyName := scanner.Text() // first line printed by py3 script, its name
435+
t.Logf("pyName %s", pyName)
436+
p, err := NewProcess(int32(cmd.Process.Pid))
437+
skipIfNotImplementedErr(t, err)
438+
if err != nil {
439+
t.Fatalf("getting process error %v", err)
440+
}
441+
name, err := p.Name()
442+
skipIfNotImplementedErr(t, err)
443+
if err != nil {
444+
t.Fatalf("getting name error %v", err)
445+
}
446+
if pyName != name {
447+
t.Fatalf("psutil and gopsutil process.Name() results differ: expected %s, got %s", pyName, name)
448+
}
449+
}
450+
394451
func Test_Process_Exe(t *testing.T) {
395452
p := testGetProcess()
396453

0 commit comments

Comments
 (0)