From 4f0e679e355fb1f645ebc8d9296b11ff14b25a6b Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 1 Dec 2019 17:54:08 +0100 Subject: [PATCH 1/4] [process][darwin][openbsd][freebsd] Fix #795 don't truncate process names to 16 characters --- process/process_darwin.go | 19 ++++++++++++++++++- process/process_freebsd.go | 19 ++++++++++++++++++- process/process_openbsd.go | 19 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 198cce2..5f49eb6 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -8,6 +8,7 @@ import ( "encoding/binary" "fmt" "os/exec" + "path/filepath" "strconv" "strings" "time" @@ -89,8 +90,24 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { if err != nil { return "", err } + name := common.IntToString(k.Proc.P_comm[:]) - return common.IntToString(k.Proc.P_comm[:]), nil + if len(name) >= 15 { + cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) + if err != nil { + return "", err + } + if len(cmdlineSlice) > 0 { + extendedName := filepath.Base(cmdlineSlice[0]) + if strings.HasPrefix(extendedName, p.name) { + name = extendedName + } else { + name = cmdlineSlice[0] + } + } + } + + return name, nil } func (p *Process) Tgid() (int32, error) { return 0, common.ErrNotImplementedError diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 3da66b5..0cf1699 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -7,6 +7,7 @@ import ( "context" "encoding/binary" "os/exec" + "path/filepath" "strconv" "strings" @@ -58,8 +59,24 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { if err != nil { return "", err } + name := common.IntToString(k.Comm[:]) - return common.IntToString(k.Comm[:]), nil + if len(name) >= 15 { + cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) + if err != nil { + return "", err + } + if len(cmdlineSlice) > 0 { + extendedName := filepath.Base(cmdlineSlice[0]) + if strings.HasPrefix(extendedName, p.name) { + name = extendedName + } else { + name = cmdlineSlice[0] + } + } + } + + return name, nil } func (p *Process) Tgid() (int32, error) { return 0, common.ErrNotImplementedError diff --git a/process/process_openbsd.go b/process/process_openbsd.go index 8bac098..1f3c645 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -8,6 +8,7 @@ import ( "context" "encoding/binary" "os/exec" + "path/filepath" "strconv" "strings" "unsafe" @@ -61,8 +62,24 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { if err != nil { return "", err } + name := common.IntToString(k.Comm[:]) - return common.IntToString(k.Comm[:]), nil + if len(name) >= 15 { + cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) + if err != nil { + return "", err + } + if len(cmdlineSlice) > 0 { + extendedName := filepath.Base(cmdlineSlice[0]) + if strings.HasPrefix(extendedName, p.name) { + name = extendedName + } else { + name = cmdlineSlice[0] + } + } + } + + return name, nil } func (p *Process) Tgid() (int32, error) { return 0, common.ErrNotImplementedError From e4f087a4af8aebb7a7cd2ce0e81c95fca5eee2df Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 1 Dec 2019 19:22:29 +0100 Subject: [PATCH 2/4] [process] Test process.Name() with long names #795 --- process/process_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/process/process_test.go b/process/process_test.go index ffa8257..16d8818 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -13,6 +13,8 @@ import ( "sync" "testing" "time" + "path/filepath" + "io/ioutil" "github.com/shirou/gopsutil/internal/common" "github.com/stretchr/testify/assert" @@ -289,6 +291,51 @@ func Test_Process_Name(t *testing.T) { t.Errorf("invalid Exe %s", n) } } +func Test_Process_Long_Name(t *testing.T) { + 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.go") + tmpfile, err := os.Create(tmpfilepath) + if err != nil { + t.Fatalf("unable to create temp file %v", err) + } + + tmpfilecontent := []byte("package main\nimport(\n\"time\"\n)\nfunc main(){\ntime.Sleep(time.Second)\n}") + if _, err := tmpfile.Write(tmpfilecontent); err != nil { + tmpfile.Close() + t.Fatalf("unable to write temp file %v", err) + } + if err := tmpfile.Close(); err != nil { + t.Fatalf("unable to close temp file %v", err) + } + + err = exec.Command("go", "build", "-o", tmpfile.Name() + ".exe", tmpfile.Name()).Run() + if err != nil { + t.Fatalf("unable to build temp file %v", err) + } + + cmd := exec.Command(tmpfile.Name() + ".exe") + + assert.Nil(t, cmd.Start()) + time.Sleep(100 * time.Millisecond) + p, err := NewProcess(int32(cmd.Process.Pid)) + skipIfNotImplementedErr(t, err) + assert.Nil(t, err) + + n, err := p.Name() + skipIfNotImplementedErr(t, err) + if err != nil { + t.Fatalf("getting name error %v", err) + } + basename := filepath.Base(tmpfile.Name() + ".exe") + if basename != n { + t.Fatalf("%s != %s", basename, n) + } + cmd.Wait() +} func Test_Process_Exe(t *testing.T) { p := testGetProcess() From 1fcea92f73519a197281706433ab4bb256401d53 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 1 Dec 2019 19:25:34 +0100 Subject: [PATCH 3/4] [process] Skip if not implemented Test_IsRunning (for openbsd) --- process/process_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/process/process_test.go b/process/process_test.go index 16d8818..717eade 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -596,8 +596,10 @@ func Test_IsRunning(t *testing.T) { } cmd.Start() p, err := NewProcess(int32(cmd.Process.Pid)) + skipIfNotImplementedErr(t, err) assert.Nil(t, err) running, err := p.IsRunning() + skipIfNotImplementedErr(t, err) if err != nil { t.Fatalf("IsRunning error: %v", err) } @@ -606,6 +608,7 @@ func Test_IsRunning(t *testing.T) { } cmd.Wait() running, err = p.IsRunning() + skipIfNotImplementedErr(t, err) if err != nil { t.Fatalf("IsRunning error: %v", err) } From e7090ba9fcd1651d41851317b70725fd4142c2bc Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 1 Dec 2019 20:24:11 +0100 Subject: [PATCH 4/4] [process] Fix Test_Children test regression because of some other running test --- process/process_test.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/process/process_test.go b/process/process_test.go index 717eade..9b3b5ca 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -2,10 +2,12 @@ package process import ( "fmt" + "io/ioutil" "net" "os" "os/exec" "os/user" + "path/filepath" "reflect" "runtime" "strconv" @@ -13,8 +15,6 @@ import ( "sync" "testing" "time" - "path/filepath" - "io/ioutil" "github.com/shirou/gopsutil/internal/common" "github.com/stretchr/testify/assert" @@ -303,7 +303,7 @@ func Test_Process_Long_Name(t *testing.T) { t.Fatalf("unable to create temp file %v", err) } - tmpfilecontent := []byte("package main\nimport(\n\"time\"\n)\nfunc main(){\ntime.Sleep(time.Second)\n}") + tmpfilecontent := []byte("package main\nimport(\n\"time\"\n)\nfunc main(){\nfor range time.Tick(time.Second) {}\n}") if _, err := tmpfile.Write(tmpfilecontent); err != nil { tmpfile.Close() t.Fatalf("unable to write temp file %v", err) @@ -312,7 +312,7 @@ func Test_Process_Long_Name(t *testing.T) { t.Fatalf("unable to close temp file %v", err) } - err = exec.Command("go", "build", "-o", tmpfile.Name() + ".exe", tmpfile.Name()).Run() + err = exec.Command("go", "build", "-o", tmpfile.Name()+".exe", tmpfile.Name()).Run() if err != nil { t.Fatalf("unable to build temp file %v", err) } @@ -324,7 +324,7 @@ func Test_Process_Long_Name(t *testing.T) { p, err := NewProcess(int32(cmd.Process.Pid)) skipIfNotImplementedErr(t, err) assert.Nil(t, err) - + n, err := p.Name() skipIfNotImplementedErr(t, err) if err != nil { @@ -334,7 +334,7 @@ func Test_Process_Long_Name(t *testing.T) { if basename != n { t.Fatalf("%s != %s", basename, n) } - cmd.Wait() + cmd.Process.Kill() } func Test_Process_Exe(t *testing.T) { p := testGetProcess() @@ -503,7 +503,14 @@ func Test_Children(t *testing.T) { if len(c) == 0 { t.Fatalf("children is empty") } - if c[0].Pid != int32(cmd.Process.Pid) { + found := false + for _, child := range c { + if child.Pid == int32(cmd.Process.Pid) { + found = true + break + } + } + if !found { t.Errorf("could not find child %d", cmd.Process.Pid) } }