github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/libpod/define/exec_codes.go (about) 1 package define 2 3 import ( 4 "strings" 5 6 "github.com/pkg/errors" 7 "github.com/sirupsen/logrus" 8 ) 9 10 const ( 11 // ExecErrorCodeGeneric is the default error code to return from an exec session if libpod failed 12 // prior to calling the runtime 13 ExecErrorCodeGeneric = 125 14 // ExecErrorCodeCannotInvoke is the error code to return when the runtime fails to invoke a command 15 // an example of this can be found by trying to execute a directory: 16 // `podman exec -l /etc` 17 ExecErrorCodeCannotInvoke = 126 18 // ExecErrorCodeNotFound is the error code to return when a command cannot be found 19 ExecErrorCodeNotFound = 127 20 ) 21 22 // TranslateExecErrorToExitCode takes an error and checks whether it 23 // has a predefined exit code associated. If so, it returns that, otherwise it returns 24 // the exit code originally stated in libpod.Exec() 25 func TranslateExecErrorToExitCode(originalEC int, err error) int { 26 if errors.Cause(err) == ErrOCIRuntimePermissionDenied { 27 return ExecErrorCodeCannotInvoke 28 } 29 if errors.Cause(err) == ErrOCIRuntimeNotFound { 30 return ExecErrorCodeNotFound 31 } 32 return originalEC 33 } 34 35 // ExitCode reads the error message when failing to executing container process 36 // and then returns 0 if no error, ExecErrorCodeNotFound if command does not exist, or ExecErrorCodeCannotInvoke for 37 // all other errors 38 func ExitCode(err error) int { 39 if err == nil { 40 return 0 41 } 42 e := strings.ToLower(err.Error()) 43 logrus.Debugf("ExitCode msg: %q", e) 44 if strings.Contains(e, "not found") || 45 strings.Contains(e, "no such file") { 46 return ExecErrorCodeNotFound 47 } 48 49 return ExecErrorCodeCannotInvoke 50 }