From 9be70f67ca802d05372b239626048791b3978c0a Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Sun, 24 Jul 2022 01:32:22 +0800 Subject: [PATCH] fix process code --- component/process/process_darwin.go | 10 ++++++--- component/process/process_linux.go | 33 ++++------------------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/component/process/process_darwin.go b/component/process/process_darwin.go index 2728aad0..169c7beb 100644 --- a/component/process/process_darwin.go +++ b/component/process/process_darwin.go @@ -15,7 +15,11 @@ const ( proccallnumpidinfo = 0x2 ) -func findProcessName(network string, ip netip.Addr, port int) (string, error) { +func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) { + return 0, 0, ErrPlatformNotSupport +} + +func findProcessName(network string, ip netip.Addr, port int) (int32, string, error) { var spath string switch network { case TCP: @@ -23,14 +27,14 @@ func findProcessName(network string, ip netip.Addr, port int) (string, error) { case UDP: spath = "net.inet.udp.pcblist_n" default: - return "", ErrInvalidNetwork + return -1, "", ErrInvalidNetwork } isIPv4 := ip.Is4() value, err := syscall.Sysctl(spath) if err != nil { - return "", err + return -1, "", err } buf := []byte(value) diff --git a/component/process/process_linux.go b/component/process/process_linux.go index c2809da1..d8a00eb5 100644 --- a/component/process/process_linux.go +++ b/component/process/process_linux.go @@ -8,8 +8,6 @@ import ( "net/netip" "os" "path" - "path/filepath" - "runtime" "strings" "syscall" "unicode" @@ -39,6 +37,7 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string, if err != nil { return -1, "", err } + pp, err := resolveProcessNameByProcSearch(inode, uid) return uid, pp, err } @@ -110,7 +109,7 @@ func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, return 0, 0, fmt.Errorf("netlink message: NLMSG_ERROR") } - inode, uid := unpackSocketDiagResponse(&message) + inode, uid := unpackSocketDiagResponse(&messages[0]) if inode < 0 || uid < 0 { return 0, 0, fmt.Errorf("invalid inode(%d) or uid(%d)", inode, uid) } @@ -198,19 +197,8 @@ func resolveProcessNameByProcSearch(inode, uid int32) (string, error) { continue } - if runtime.GOOS == "android" { - if bytes.Equal(buffer[:n], socket) { - cmdline, err := os.ReadFile(path.Join(processPath, "cmdline")) - if err != nil { - return "", err - } - - return splitCmdline(cmdline), nil - } - } else { - if bytes.Equal(buffer[:n], socket) { - return os.Readlink(path.Join(processPath, "exe")) - } + if bytes.Equal(buffer[:n], socket) { + return os.Readlink(path.Join(processPath, "exe")) } } } @@ -218,19 +206,6 @@ func resolveProcessNameByProcSearch(inode, uid int32) (string, error) { return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode) } -func splitCmdline(cmdline []byte) string { - cmdline = bytes.Trim(cmdline, " ") - - idx := bytes.IndexFunc(cmdline, func(r rune) bool { - return unicode.IsControl(r) || unicode.IsSpace(r) - }) - - if idx == -1 { - return filepath.Base(string(cmdline)) - } - return filepath.Base(string(cmdline[:idx])) -} - func isPid(s string) bool { return strings.IndexFunc(s, func(r rune) bool { return !unicode.IsDigit(r)