From a2f17f7b995c88736f99d1e26b4dd67f6a399542 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 1 Nov 2021 15:23:15 +0100 Subject: [PATCH] unix: allow passing optional args to SysctlKinfoProcSlice This allows using SysctlKinfoProcSlice to e.g. query processes by user id using the kern.proc.uid sysctl and is still backwards compatible to original implementation, i.e. still allows the kern.proc.all sysctl without any additional arguments. Change-Id: Ia2d76ce5b91a077221891e1f2dfd79a38d2be52b Reviewed-on: https://go-review.googlesource.com/c/sys/+/359677 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- unix/syscall_darwin.go | 4 ++-- unix/syscall_darwin_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/unix/syscall_darwin.go b/unix/syscall_darwin.go index b799c5d7..8826f414 100644 --- a/unix/syscall_darwin.go +++ b/unix/syscall_darwin.go @@ -447,8 +447,8 @@ func SysctlKinfoProc(name string, args ...int) (*KinfoProc, error) { return &kinfo, nil } -func SysctlKinfoProcSlice(name string) ([]KinfoProc, error) { - mib, err := sysctlmib(name) +func SysctlKinfoProcSlice(name string, args ...int) ([]KinfoProc, error) { + mib, err := sysctlmib(name, args...) if err != nil { return nil, err } diff --git a/unix/syscall_darwin_test.go b/unix/syscall_darwin_test.go index 9a68bfba..eac98fe7 100644 --- a/unix/syscall_darwin_test.go +++ b/unix/syscall_darwin_test.go @@ -267,3 +267,28 @@ func TestSysctlKinfoProc(t *testing.T) { t.Errorf("got pid %d, want %d", got, want) } } + +func TestSysctlKinfoProcSlice(t *testing.T) { + kps, err := unix.SysctlKinfoProcSlice("kern.proc.all") + if err != nil { + t.Fatalf("SysctlKinfoProc: %v", err) + } + if len(kps) == 0 { + t.Errorf("SysctlKinfoProcSlice: expected at least one process") + } + + uid := unix.Getuid() + kps, err = unix.SysctlKinfoProcSlice("kern.proc.uid", uid) + if err != nil { + t.Fatalf("SysctlKinfoProc: %v", err) + } + if len(kps) == 0 { + t.Errorf("SysctlKinfoProcSlice: expected at least one process") + } + + for _, kp := range kps { + if got, want := int(kp.Eproc.Ucred.Uid), uid; got != want { + t.Errorf("process %d: got uid %d, want %d", kp.Proc.P_pid, got, want) + } + } +}