From 95da234e122c5f925d4f955e47843f027369c273 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 1 Nov 2021 15:20:19 +0100 Subject: [PATCH] unix: add SysctlKinfoProc on darwin This allows to get KinfoProc for a given process using SysctlKinfoProcSlice("kern.proc.pid", pid) rather than having to query all processes using SysctlKinfoProcSlice() and the extracting the relevant KinfoProc. Change-Id: I965ea5c77d6f3441592b4540c54ab56f6ac9e27d Reviewed-on: https://go-review.googlesource.com/c/sys/+/359676 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- unix/syscall_darwin.go | 17 +++++++++++++++++ unix/syscall_darwin_test.go | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/unix/syscall_darwin.go b/unix/syscall_darwin.go index a8c13317..b799c5d7 100644 --- a/unix/syscall_darwin.go +++ b/unix/syscall_darwin.go @@ -430,6 +430,23 @@ func GetsockoptXucred(fd, level, opt int) (*Xucred, error) { return x, err } +func SysctlKinfoProc(name string, args ...int) (*KinfoProc, error) { + mib, err := sysctlmib(name, args...) + if err != nil { + return nil, err + } + + var kinfo KinfoProc + n := uintptr(SizeofKinfoProc) + if err := sysctl(mib, (*byte)(unsafe.Pointer(&kinfo)), &n, nil, 0); err != nil { + return nil, err + } + if n != SizeofKinfoProc { + return nil, EIO + } + return &kinfo, nil +} + func SysctlKinfoProcSlice(name string) ([]KinfoProc, error) { mib, err := sysctlmib(name) if err != nil { diff --git a/unix/syscall_darwin_test.go b/unix/syscall_darwin_test.go index 731488f4..9a68bfba 100644 --- a/unix/syscall_darwin_test.go +++ b/unix/syscall_darwin_test.go @@ -256,3 +256,14 @@ func TestGetsockoptXucred(t *testing.T) { } } } + +func TestSysctlKinfoProc(t *testing.T) { + pid := unix.Getpid() + kp, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + t.Fatalf("SysctlKinfoProc: %v", err) + } + if got, want := int(kp.Proc.P_pid), pid; got != want { + t.Errorf("got pid %d, want %d", got, want) + } +}