Merge pull request #796 from Lomanic/issue795

[process][darwin][openbsd][freebsd] Fix #795 don't truncate process names to 16 characters
This commit is contained in:
Lomanic 2019-12-15 23:31:26 +01:00 committed by GitHub
commit f586a57353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -2,10 +2,12 @@ package process
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"runtime"
"strconv"
@ -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(){\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)
}
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.Process.Kill()
}
func Test_Process_Exe(t *testing.T) {
p := testGetProcess()
@ -456,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)
}
}
@ -549,8 +603,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)
}
@ -559,6 +615,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)
}