diff --git a/main.go b/main.go index fba98b65..23665039 100644 --- a/main.go +++ b/main.go @@ -93,6 +93,7 @@ func main() { killCommand, listCommand, pauseCommand, + psCommand, restoreCommand, resumeCommand, specCommand, diff --git a/ps.go b/ps.go new file mode 100644 index 00000000..5a0dae61 --- /dev/null +++ b/ps.go @@ -0,0 +1,76 @@ +// +build linux + +package main + +import ( + "fmt" + "os/exec" + "strconv" + "strings" + + "github.com/codegangsta/cli" +) + +var psCommand = cli.Command{ + Name: "ps", + Usage: "ps displays the processes running inside a container", + ArgsUsage: ` `, + Action: func(context *cli.Context) { + container, err := getContainer(context) + if err != nil { + fatal(err) + } + + psArgs := context.Args().Get(1) + if psArgs == "" { + psArgs = "-ef" + } + + output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output() + if err != nil { + fatal(err) + } + + lines := strings.Split(string(output), "\n") + pidIndex, err := getPidIndex(lines[0]) + if err != nil { + fatal(err) + } + + pids, err := container.Processes() + if err != nil { + fatal(err) + } + fmt.Println(lines[0]) + for _, line := range lines[1:] { + if len(line) == 0 { + continue + } + fields := strings.Fields(line) + p, err := strconv.Atoi(fields[pidIndex]) + if err != nil { + fatal(fmt.Errorf("unexpected pid '%s': %s", fields[pidIndex], err)) + } + + for _, pid := range pids { + if pid == p { + fmt.Println(line) + break + } + } + } + }, +} + +func getPidIndex(title string) (int, error) { + titles := strings.Fields(title) + + pidIndex := -1 + for i, name := range titles { + if name == "PID" { + return i, nil + } + } + + return pidIndex, fmt.Errorf("couldn't find PID field in ps output") +}