-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathpod_top_freebsd.go
78 lines (67 loc) · 1.92 KB
/
pod_top_freebsd.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//go:build !remote
package libpod
import (
"fmt"
"strings"
"github.com/containers/podman/v5/libpod/define"
)
// GetPodPidInformation returns process-related data of all processes in
// the pod. The output data can be controlled via the `descriptors`
// argument which expects format descriptors and supports all AIXformat
// descriptors of ps (1) plus some additional ones to for instance inspect the
// set of effective capabilities. Each element in the returned string slice
// is a tab-separated string.
//
// For more details, please refer to github.com/containers/psgo.
func (p *Pod) GetPodPidInformation(descriptors []string) ([]string, error) {
// Default to 'ps -ef' compatible descriptors
if len(strings.Join(descriptors, "")) == 0 {
descriptors = []string{"user", "pid", "ppid", "pcpu", "etime", "tty", "time", "args"}
}
jailNames := make([]string, 0)
ctrsInPod, err := p.AllContainers()
if err != nil {
return nil, err
}
for _, c := range ctrsInPod {
c.lock.Lock()
err := c.syncContainer()
c.lock.Unlock()
if err != nil {
return nil, err
}
if c.state.State == define.ContainerStateRunning {
jailName, err := c.jailName()
if err != nil {
return nil, fmt.Errorf("getting jail name: %w", err)
}
jailNames = append(jailNames, jailName)
}
}
// Also support comma-separated input.
psDescriptors := []string{}
for _, d := range descriptors {
for _, s := range strings.Split(d, ",") {
if s != "" {
psDescriptors = append(psDescriptors, s)
}
}
}
// For consistency with pod_top_linux.go, only allow descriptor names
for _, d := range psDescriptors {
if _, ok := isDescriptor[d]; !ok {
return nil, fmt.Errorf("unknown descriptor: %s", d)
}
}
args := []string{
"-J",
strings.Join(jailNames, ","),
"-ao",
strings.Join(psDescriptors, ","),
}
output, err := execPS(args)
if err != nil {
return nil, fmt.Errorf("executing ps(1): %w", err)
}
return output, nil
}