From 6adfea0a72bce328d20530b58d3ee75513b0c984 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+dreamacro@users.noreply.github.com> Date: Wed, 2 Nov 2022 09:50:57 +0800 Subject: [PATCH] Fix macOS Ventura process name match --- common/process/searcher_darwin.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/common/process/searcher_darwin.go b/common/process/searcher_darwin.go index 3ddf30de..de8d3b3b 100644 --- a/common/process/searcher_darwin.go +++ b/common/process/searcher_darwin.go @@ -5,6 +5,8 @@ import ( "encoding/binary" "net/netip" "os" + "strconv" + "strings" "syscall" "unsafe" @@ -29,6 +31,22 @@ func (d *darwinSearcher) FindProcessInfo(ctx context.Context, network string, so return &Info{ProcessPath: processName, UserId: -1}, nil } +var structSize = func() int { + value, _ := syscall.Sysctl("kern.osrelease") + major, _, _ := strings.Cut(value, ".") + n, _ := strconv.ParseInt(major, 10, 64) + switch true { + case n >= 22: + return 408 + default: + // from darwin-xnu/bsd/netinet/in_pcblist.c:get_pcblist_n + // size/offset are round up (aligned) to 8 bytes in darwin + // rup8(sizeof(xinpcb_n)) + rup8(sizeof(xsocket_n)) + + // 2 * rup8(sizeof(xsockbuf_n)) + rup8(sizeof(xsockstat_n)) + return 384 + } +}() + func findProcessName(network string, ip netip.Addr, port int) (string, error) { var spath string switch network { @@ -53,7 +71,7 @@ func findProcessName(network string, ip netip.Addr, port int) (string, error) { // size/offset are round up (aligned) to 8 bytes in darwin // rup8(sizeof(xinpcb_n)) + rup8(sizeof(xsocket_n)) + // 2 * rup8(sizeof(xsockbuf_n)) + rup8(sizeof(xsockstat_n)) - itemSize := 384 + itemSize := structSize if network == N.NetworkTCP { // rup8(sizeof(xtcpcb_n)) itemSize += 208