-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgow_ps_darwin.go
More file actions
40 lines (33 loc) · 860 Bytes
/
gow_ps_darwin.go
File metadata and controls
40 lines (33 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//go:build darwin
package main
import (
"os"
"github.com/mitranim/gg"
"golang.org/x/sys/unix"
)
func SubPids(topPid int, verb bool) ([]int, error) {
pid := os.Getpid()
pids, err := SubPidsViaSyscall(pid)
if err == nil {
return pids, nil
}
if verb {
log.Println(`unable to get pids via syscall, falling back on "ps":`, err)
}
return SubPidsViaPs(pid)
}
func SubPidsViaSyscall(topPid int) ([]int, error) {
// Get all processes.
infos, err := unix.SysctlKinfoProcSlice(`kern.proc.all`)
if err != nil {
return nil, gg.Wrap(err, `failed to get process list`)
}
// Index child pids by ppid.
ppidToPids := make(map[int][]int, len(infos))
for _, info := range infos {
pid := int(info.Proc.P_pid)
ppid := int(info.Eproc.Ppid)
ppidToPids[ppid] = append(ppidToPids[ppid], pid)
}
return procIndexToDescs(ppidToPids, topPid, 0), nil
}