Files
podman/libpod/pod_top_freebsd.go
Doug Rabson bb57c1631c libpod: add 'pod top' support on FreeBSD
This shares code with 'container top' which runs ps on the host,
filtering for the containers that are part of the pod.
(*Container).jailName is modified to take into account the possiblity
that the container is in a pod - this also fixes stats reporting for
pods on FreeBSD.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2023-07-28 10:52:20 +01:00

80 lines
1.9 KiB
Go

//go:build freebsd
// +build freebsd
package libpod
import (
"fmt"
"strings"
"github.com/containers/podman/v4/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
}