mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-09-13 19:17:52 +00:00
Use github.com/containers/psgo instead of execing `ps (1)`. The psgo library enables a much more flexible interface with respect to which data to be printed (e.g., capabilities, seccomp mode, PID, PCPU, etc.) while the output can be parsed reliably. The library does not use ps (1) but parses /proc and /dev instead. To list the processes of a given container, psgo will join the mount namespace of the given container and extract all data from there. Notice that this commit breaks compatibility with docker-top. Signed-off-by: Valentin Rothberg <vrothberg@suse.com> Closes: #1113 Approved by: rhatdan
34 lines
662 B
Go
34 lines
662 B
Go
package ps
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
// readCmdline can be used for mocking in unit tests.
|
|
func readCmdline(path string) (string, error) {
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
err = errNoSuchPID
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
return string(data), nil
|
|
}
|
|
|
|
// parseCmdline parses a /proc/$pid/cmdline file and returns a string slice.
|
|
func parseCmdline(path string) ([]string, error) {
|
|
raw, err := readCmdline(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cmdLine := []string{}
|
|
for _, rawCmd := range bytes.Split([]byte(raw), []byte{0}) {
|
|
cmdLine = append(cmdLine, string(rawCmd))
|
|
}
|
|
return cmdLine, nil
|
|
}
|