Skip to content

Commit c9b8b4f

Browse files
author
Mrunal Patel
authored
Merge pull request opencontainers#3073 from kolyshkin/runc-exec-255
runc exec: fail with exit code of 255
2 parents 907c8de + 60e02b4 commit c9b8b4f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-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), 255)
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ 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 test_busybox no-such-binary
38+
[ "$status" -eq 255 ]
39+
40+
runc exec no_such_container true
41+
[ "$status" -eq 255 ]
42+
}
43+
2444
@test "runc exec --pid-file" {
2545
# run busybox detached
2646
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)