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 <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/unix/syscall_darwin_test.go b/unix/syscall_darwin_test.go
index 9a68bfb..eac98fe 100644
--- a/unix/syscall_darwin_test.go
+++ b/unix/syscall_darwin_test.go
@@ -267,3 +267,28 @@
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)
+ }
+ }
+}