Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runc exec: fail with exit code of 255 #3073

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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