Skip to content

Commit c4d0ff8

Browse files
committed
runc exec: fail with exit code of 255
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 <[email protected]>
1 parent 1f5f237 commit c4d0ff8

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

exec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ following will output a list of processes running in the container:
101101
if err == nil {
102102
os.Exit(status)
103103
}
104-
return fmt.Errorf("exec failed: %w", err)
104+
fatalWithCode(fmt.Errorf("exec failed: %w", err), -1)
105+
return nil // to satisfy the linter
105106
},
106107
SkipArgReorder: true,
107108
}

man/runc-exec.8.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ multiple times.
5959
: Pass _N_ additional file descriptors to the container (**stdio** +
6060
**$LISTEN_FDS** + _N_ in total). Default is **0**.
6161

62+
# EXIT STATUS
63+
64+
Exits with a status of _command_ (unless **-d** is used), or **255** if
65+
an error occurred.
66+
6267
# EXAMPLES
6368
If the container can run **ps**(1) command, the following
6469
will output a list of processes running in the container:

tests/integration/exec.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ function teardown() {
2121
[[ "${output}" == *"Hello from exec"* ]]
2222
}
2323

24+
@test "runc exec [exit codes]" {
25+
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
26+
[ "$status" -eq 0 ]
27+
28+
runc exec test_busybox false
29+
[ "$status" -eq 1 ]
30+
31+
runc exec test_busybox sh -c "exit 42"
32+
[ "$status" -eq 42 ]
33+
34+
runc exec --pid-file /non-existent/directory test_busybox true
35+
[ "$status" -eq 255 ]
36+
37+
runc exec bad_container true
38+
[ "$status" -eq 255 ]
39+
}
40+
2441
@test "runc exec --pid-file" {
2542
# run busybox detached
2643
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox

utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ func logrusToStderr() bool {
5353
// fatal prints the error's details if it is a libcontainer specific error type
5454
// then exits the program with an exit status of 1.
5555
func fatal(err error) {
56-
// make sure the error is written to the logger
56+
fatalWithCode(err, 1)
57+
}
58+
59+
func fatalWithCode(err error, ret int) {
60+
// Make sure the error is written to the logger.
5761
logrus.Error(err)
5862
if !logrusToStderr() {
5963
fmt.Fprintln(os.Stderr, err)
6064
}
6165

62-
os.Exit(1)
66+
os.Exit(ret)
6367
}
6468

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

0 commit comments

Comments
 (0)