mirror of
https://github.com/opencontainers/runc.git
synced 2025-09-27 03:46:19 +08:00
Return proper exit code for exec errors
Exec erros from the exec() syscall in the container's init should be treated as if the container ran but couldn't execute the process for the user instead of returning a libcontainer error as if it was an issue in the library. Before specifying different commands like `/etc`, `asldfkjasdlfj`, or `/alsdjfkasdlfj` would always return 1 on the command line with a libcontainer specific error message. Now they return the correct message and exit status defined for unix processes. Example: ```bash root@deathstar:/containers/redis# runc start test exec: "/asdlfkjasldkfj": file does not exist root@deathstar:/containers/redis# echo $? 127 root@deathstar:/containers/redis# runc start test exec: "asdlfkjasldkfj": executable file not found in $PATH root@deathstar:/containers/redis# echo $? 127 root@deathstar:/containers/redis# runc start test exec: "/etc": permission denied root@deathstar:/containers/redis# echo $? 126 ``` Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
18
utils.go
18
utils.go
@@ -6,7 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
@@ -161,6 +163,22 @@ func getContainer(context *cli.Context) (libcontainer.Container, error) {
|
||||
// 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) {
|
||||
// return proper unix error codes
|
||||
if exerr, ok := err.(*exec.Error); ok {
|
||||
switch exerr.Err {
|
||||
case os.ErrPermission:
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(126)
|
||||
case exec.ErrNotFound:
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(127)
|
||||
default:
|
||||
if os.IsNotExist(exerr.Err) {
|
||||
fmt.Fprintf(os.Stderr, "exec: %s: %v\n", strconv.Quote(exerr.Name), os.ErrNotExist)
|
||||
os.Exit(127)
|
||||
}
|
||||
}
|
||||
}
|
||||
if lerr, ok := err.(libcontainer.Error); ok {
|
||||
lerr.Detail(os.Stderr)
|
||||
os.Exit(1)
|
||||
|
Reference in New Issue
Block a user