Merge pull request #1369 from Lomanic/issue1368

[process][windows] Retrieve process name as basename of executable
tags/v3.22.10
shirou 3 years ago committed by GitHub
commit 20b15fb639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -294,7 +294,7 @@ func Test_Process_Name(t *testing.T) {
t.Errorf("getting name error %v", err) t.Errorf("getting name error %v", err)
} }
if !strings.Contains(n, "process.test") { if !strings.Contains(n, "process.test") {
t.Errorf("invalid Exe %s", n) t.Errorf("invalid Name %s", n)
} }
} }

@ -10,6 +10,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"reflect" "reflect"
"strings" "strings"
"syscall" "syscall"
@ -319,18 +320,19 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
} }
func (p *Process) NameWithContext(ctx context.Context) (string, error) { func (p *Process) NameWithContext(ctx context.Context) (string, error) {
ppid, _, name, err := getFromSnapProcess(p.Pid) if p.Pid == 0 {
if err != nil { return "System Idle Process", nil
return "", fmt.Errorf("could not get Name: %s", err) }
if p.Pid == 4 {
return "System", nil
} }
// if no errors and not cached already, cache ppid exe, err := p.ExeWithContext(ctx)
p.parent = ppid if err != nil {
if 0 == p.getPpid() { return "", fmt.Errorf("could not get Name: %s", err)
p.setPpid(ppid)
} }
return name, nil return filepath.Base(exe), nil
} }
func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
@ -987,15 +989,9 @@ func is32BitProcess(h windows.Handle) bool {
var procIs32Bits bool var procIs32Bits bool
switch processorArchitecture { switch processorArchitecture {
case PROCESSOR_ARCHITECTURE_INTEL: case PROCESSOR_ARCHITECTURE_INTEL, PROCESSOR_ARCHITECTURE_ARM:
fallthrough
case PROCESSOR_ARCHITECTURE_ARM:
procIs32Bits = true procIs32Bits = true
case PROCESSOR_ARCHITECTURE_ARM64: case PROCESSOR_ARCHITECTURE_ARM64, PROCESSOR_ARCHITECTURE_IA64, PROCESSOR_ARCHITECTURE_AMD64:
fallthrough
case PROCESSOR_ARCHITECTURE_IA64:
fallthrough
case PROCESSOR_ARCHITECTURE_AMD64:
var wow64 uint var wow64 uint
ret, _, _ := common.ProcNtQueryInformationProcess.Call( ret, _, _ := common.ProcNtQueryInformationProcess.Call(

Loading…
Cancel
Save