Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the right way to get process name in win32 format #909

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions component/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package process
import (
"fmt"
"net/netip"
"path/filepath"
"strings"
"sync"
"syscall"
"unsafe"
Expand Down Expand Up @@ -103,10 +101,6 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string
return 0, "", err
}
pp, err := getExecPathFromPID(pid)
if err != nil {
return 0, "", err
}
pp, err = convertDOSPath(pp)
return 0, pp, err
}

Expand Down Expand Up @@ -224,7 +218,7 @@ func getExecPathFromPID(pid uint32) (string, error) {
r1, _, err := syscall.SyscallN(
queryProcName,
uintptr(h),
uintptr(1),
uintptr(0),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&size)),
)
Expand All @@ -233,29 +227,3 @@ func getExecPathFromPID(pid uint32) (string, error) {
}
return syscall.UTF16ToString(buf[:size]), nil
}

// modify from https://github.com/shirou/gopsutil/blob/9deadc99147d80f732af3a59e624af73d0143891/internal/common/common_windows.go#L220-L241
// Convert paths using native DOS format like:
//
// "\Device\HarddiskVolume1\Windows\systemew\file.txt"
//
// into:
//
// "C:\Windows\systemew\file.txt"
func convertDOSPath(p string) (string, error) {
rawDrive := strings.Join(strings.Split(p, `\`)[:3], `\`)

for d := 'A'; d <= 'Z'; d++ {
szDeviceName := string(d) + ":"
deviceName, err := syscall.UTF16PtrFromString(szDeviceName)
if err != nil {
return "", err
}
szTarget := make([]uint16, 512)
n, err := windows.QueryDosDevice(deviceName, &szTarget[0], uint32(len(szTarget)))
if err == nil && windows.UTF16ToString(szTarget[:n]) == rawDrive {
return filepath.Join(szDeviceName, p[len(rawDrive):]), nil
}
}
return p, nil
}