-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
proc.go
51 lines (44 loc) · 1.13 KB
/
proc.go
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
41
42
43
44
45
46
47
48
49
50
51
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package kernel
import (
"os"
"strconv"
)
// AllPidsProcs will return all pids under procRoot
func AllPidsProcs(procRoot string) ([]int, error) {
f, err := os.Open(procRoot)
if err != nil {
return nil, err
}
defer f.Close()
dirs, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
pids := make([]int, 0, len(dirs))
for _, name := range dirs {
if pid, err := strconv.Atoi(name); err == nil {
pids = append(pids, pid)
}
}
return pids, nil
}
// WithAllProcs will execute `fn` for every pid under procRoot. `fn` is
// passed the `pid`. If `fn` returns an error the iteration aborts,
// returning the last error returned from `fn`.
func WithAllProcs(procRoot string, fn func(int) error) error {
pids, err := AllPidsProcs(procRoot)
if err != nil {
return err
}
for _, pid := range pids {
if err = fn(pid); err != nil {
return err
}
}
return nil
}