-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgetallpids.go
More file actions
44 lines (40 loc) · 988 Bytes
/
Copy pathgetallpids.go
File metadata and controls
44 lines (40 loc) · 988 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
41
42
43
44
package cgroups
import (
"errors"
"io/fs"
"os"
"path/filepath"
"golang.org/x/sys/unix"
)
// GetAllPids returns all pids from the cgroup identified by path, and all its
// sub-cgroups.
func GetAllPids(path string) ([]int, error) {
var pids []int
err := filepath.WalkDir(path, func(p string, d fs.DirEntry, iErr error) error {
if iErr != nil {
// A descendant cgroup can be removed while we walk, ignore
// any such error unless it's on the root (path) cgroup here
if p != path && ignoreCgroupRemoved(iErr) {
return nil
}
return iErr
}
if !d.IsDir() {
return nil
}
cPids, err := readProcsFile(p)
if err != nil {
if p != path && ignoreCgroupRemoved(err) {
return nil
}
return err
}
pids = append(pids, cPids...)
return nil
})
return pids, err
}
// ignoreCgroupRemoved reports whether err indicates the cgroup was removed.
func ignoreCgroupRemoved(err error) bool {
return os.IsNotExist(err) || errors.Is(err, unix.ENODEV)
}