diff options
author | Lantao Liu <lantaol@google.com> | 2018-07-03 11:57:04 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-07-03 11:58:12 -0700 |
commit | 138cb8da5043c1c8f59f4c27b727383e5ad8254e (patch) | |
tree | b11430d0f51cd67b87d0d200f4e575c69a3308a8 /runsc/cmd/wait.go | |
parent | 0ef606616732e1daf9d424658c31e5fed8f1ee4a (diff) |
runsc: `runsc wait` print wait status.
PiperOrigin-RevId: 203160639
Change-Id: I8fb2787ba0efb7eacd9d4c934238a26eb5ae79d5
Diffstat (limited to 'runsc/cmd/wait.go')
-rw-r--r-- | runsc/cmd/wait.go | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/runsc/cmd/wait.go b/runsc/cmd/wait.go index 8437457c4..b41edc725 100644 --- a/runsc/cmd/wait.go +++ b/runsc/cmd/wait.go @@ -15,6 +15,8 @@ package cmd import ( + "encoding/json" + "os" "syscall" "context" @@ -75,7 +77,7 @@ func (wt *Wait) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) Fatalf("error loading container: %v", err) } - waitStatus := args[1].(*syscall.WaitStatus) + var waitStatus syscall.WaitStatus switch { // Wait on the whole container. case wt.rootPID == unsetPID && wt.pid == unsetPID: @@ -83,21 +85,43 @@ func (wt *Wait) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) if err != nil { Fatalf("error waiting on container %q: %v", c.ID, err) } - *waitStatus = ws + waitStatus = ws // Wait on a PID in the root PID namespace. case wt.rootPID != unsetPID: ws, err := c.WaitRootPID(int32(wt.rootPID)) if err != nil { Fatalf("error waiting on PID in root PID namespace %d in container %q: %v", wt.rootPID, c.ID, err) } - *waitStatus = ws + waitStatus = ws // Wait on a PID in the container's PID namespace. case wt.pid != unsetPID: ws, err := c.WaitPID(int32(wt.pid)) if err != nil { Fatalf("error waiting on PID %d in container %q: %v", wt.pid, c.ID, err) } - *waitStatus = ws + waitStatus = ws + } + result := waitResult{ + ID: id, + ExitStatus: exitStatus(waitStatus), + } + // Write json-encoded wait result directly to stdout. + if err := json.NewEncoder(os.Stdout).Encode(result); err != nil { + Fatalf("error marshaling wait result: %v", err) } return subcommands.ExitSuccess } + +type waitResult struct { + ID string `json:"id"` + ExitStatus int `json:"exitStatus"` +} + +// exitStatus returns the correct exit status for a process based on if it +// was signaled or exited cleanly. +func exitStatus(status syscall.WaitStatus) int { + if status.Signaled() { + return 128 + int(status.Signal()) + } + return status.ExitStatus() +} |