unix: add bindings for kinfo_proc on Darwin

On Linux, we can extract a list of all the processes on the system by
calling readdir() against /proc. On BSD-like systems, this information
needs to be extracted from sysctl in the form of kinfo_proc structures.

This change adds bindings for this structure and adds a method for
reading an array of these structures from sysctl.

Change-Id: Iaaed27cdbbf13d7c2cc6a6787667ac04d65bf41c
GitHub-Last-Rev: 34926f847495e7584bb8b68bbf24c5a0764ca861
GitHub-Pull-Request: golang/sys#111
Reviewed-on: https://go-review.googlesource.com/c/sys/+/328169
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ed Schouten
2021-06-16 09:43:52 +00:00
committed by Tobias Klauser
parent e2b7044e8c
commit 59db8d763f
5 changed files with 275 additions and 0 deletions
+15
View File
@@ -50,6 +50,21 @@ func main() {
b = sttimespec.ReplaceAll(b, []byte("Timespec"))
}
if goos == "darwin" {
// KinfoProc contains various pointers to objects stored
// in kernel space. Replace these by uintptr to prevent
// accidental dereferencing.
kinfoProcPointerRegex := regexp.MustCompile(`\*_Ctype_struct_(pgrp|proc|session|sigacts|ucred|user|vnode)`)
b = kinfoProcPointerRegex.ReplaceAll(b, []byte("uintptr"))
// ExternProc contains a p_un member that in kernel
// space stores a pair of pointers and in user space
// stores the process creation time. We only care about
// the process creation time.
externProcStarttimeRegex := regexp.MustCompile(`P_un\s*\[\d+\]byte`)
b = externProcStarttimeRegex.ReplaceAll(b, []byte("P_starttime Timeval"))
}
// Intentionally export __val fields in Fsid and Sigset_t
valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`)
b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}"))