From f5a95fa244860bfa07f00f1c499446d24a60a697 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Wed, 6 Jul 2016 11:46:22 +0800 Subject: [PATCH 1/2] cli: Workaround for ps's argument Currently, ps command can not support argument: (But following usage is in manual) | # ./runc ps 123 -ef | Incorrect Usage. | | NAME: | runc ps - ps displays the processes running inside a container | | USAGE: | runc ps [command options] [ps options] | | OPTIONS: | --format value, -f value select one of: table or json | | flag provided but not defined: -ef | # Instead of using odd command like: | # ./runc ps -- 123 -ef We can make it seems little better: | # ./runc ps 123 -- -ef | UID PID PPID C STIME TTY TIME CMD | root 29046 29038 0 11:18 pts/2 00:00:00 sh | # This patch also fixed manual which can not working in current code. Closes #788 Signed-off-by: Zhao Lei --- man/runc-ps.8.md | 2 +- ps.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/man/runc-ps.8.md b/man/runc-ps.8.md index 694990a90..d60634714 100644 --- a/man/runc-ps.8.md +++ b/man/runc-ps.8.md @@ -2,7 +2,7 @@ runc ps - ps displays the processes running inside a container # SYNOPSIS - runc ps [command options] [ps options] + runc ps [command options] [-- ps options] # OPTIONS --format value, -f value select one of: table(default) or json diff --git a/ps.go b/ps.go index 3b462a0a5..e40a973f2 100644 --- a/ps.go +++ b/ps.go @@ -16,7 +16,7 @@ import ( var psCommand = cli.Command{ Name: "ps", Usage: "ps displays the processes running inside a container", - ArgsUsage: ` [ps options]`, + ArgsUsage: ` [-- ps options]`, Flags: []cli.Flag{ cli.StringFlag{ Name: "format, f", @@ -43,6 +43,9 @@ var psCommand = cli.Command{ } psArgs := context.Args().Get(1) + if psArgs == "--" { + psArgs = context.Args().Get(2) + } if psArgs == "" { psArgs = "-ef" } From cdb552d341368dca4be007d1967ccb930f8e27b6 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Thu, 7 Jul 2016 18:36:26 +0800 Subject: [PATCH 2/2] ps: Support muitiple ps arguments Before patch: | # ./runc ps test -- -e -f | PID TTY TIME CMD | 29046 pts/2 00:00:00 sh <-- The -f option was skipped | # After patch: | # ./runc ps test -- -e -f | UID PID PPID C STIME TTY TIME CMD | root 29046 29038 0 Jul06 pts/2 00:00:00 sh | # Reported-by: Qiang Huang Signed-off-by: Zhao Lei --- ps.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ps.go b/ps.go index e40a973f2..af2618360 100644 --- a/ps.go +++ b/ps.go @@ -42,15 +42,21 @@ var psCommand = cli.Command{ return nil } - psArgs := context.Args().Get(1) - if psArgs == "--" { - psArgs = context.Args().Get(2) - } - if psArgs == "" { - psArgs = "-ef" + // [1:] is to remove command name, ex: + // context.Args(): [containet_id ps_arg1 ps_arg2 ...] + // psArgs: [ps_arg1 ps_arg2 ...] + // + psArgs := context.Args()[1:] + + if len(psArgs) > 0 && psArgs[0] == "--" { + psArgs = psArgs[1:] } - output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output() + if len(psArgs) == 0 { + psArgs = []string{"-ef"} + } + + output, err := exec.Command("ps", psArgs...).Output() if err != nil { return err }