Skip to content

Commit

Permalink
runc exec: fail with exit code of 255
Browse files Browse the repository at this point in the history
Currently there's no way to distinguish between the two cases:
 - runc exec failed;
 - the command executed returned 1.

This was possible before commit 8477638, as runc exec exited with
the code of 255 if exec itself has failed. The code of 255 is the same
convention as used by e.g. ssh.

Re-introduce the feature, document it, and add some tests so it won't be
broken again.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Jul 28, 2021
1 parent 1f5f237 commit 60e02b4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ following will output a list of processes running in the container:
if err == nil {
os.Exit(status)
}
return fmt.Errorf("exec failed: %w", err)
fatalWithCode(fmt.Errorf("exec failed: %w", err), 255)
return nil // to satisfy the linter
},
SkipArgReorder: true,
}
Expand Down
5 changes: 5 additions & 0 deletions man/runc-exec.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ multiple times.
: Pass _N_ additional file descriptors to the container (**stdio** +
**$LISTEN_FDS** + _N_ in total). Default is **0**.

# EXIT STATUS

Exits with a status of _command_ (unless **-d** is used), or **255** if
an error occurred.

# EXAMPLES
If the container can run **ps**(1) command, the following
will output a list of processes running in the container:
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ function teardown() {
[[ "${output}" == *"Hello from exec"* ]]
}

@test "runc exec [exit codes]" {
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
[ "$status" -eq 0 ]

runc exec test_busybox false
[ "$status" -eq 1 ]

runc exec test_busybox sh -c "exit 42"
[ "$status" -eq 42 ]

runc exec --pid-file /non-existent/directory test_busybox true
[ "$status" -eq 255 ]

runc exec test_busybox no-such-binary
[ "$status" -eq 255 ]

runc exec no_such_container true
[ "$status" -eq 255 ]
}

@test "runc exec --pid-file" {
# run busybox detached
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
Expand Down
8 changes: 6 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ func logrusToStderr() bool {
// fatal prints the error's details if it is a libcontainer specific error type
// then exits the program with an exit status of 1.
func fatal(err error) {
// make sure the error is written to the logger
fatalWithCode(err, 1)
}

func fatalWithCode(err error, ret int) {
// Make sure the error is written to the logger.
logrus.Error(err)
if !logrusToStderr() {
fmt.Fprintln(os.Stderr, err)
}

os.Exit(1)
os.Exit(ret)
}

// setupSpec performs initial setup based on the cli.Context for the container
Expand Down

0 comments on commit 60e02b4

Please sign in to comment.