File: exec_codes.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (55 lines) | stat: -rw-r--r-- 1,687 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package define

import (
	"errors"
	"strings"

	"github.com/sirupsen/logrus"
)

const (
	// ExecErrorCodeGeneric is the default error code to return from an exec session if libpod failed
	// prior to calling the runtime
	ExecErrorCodeGeneric = 125
	// ExecErrorCodeCannotInvoke is the error code to return when the runtime fails to invoke a command.
	// An example of this can be found by trying to execute a directory:
	// `podman exec -l /etc`
	ExecErrorCodeCannotInvoke = 126
	// ExecErrorCodeNotFound is the error code to return when a command cannot be found
	ExecErrorCodeNotFound = 127
)

// TranslateExecErrorToExitCode takes an error and checks whether it
// has a predefined exit code associated. If so, it returns that, otherwise it returns
// the exit code originally stated in libpod.Exec()
func TranslateExecErrorToExitCode(originalEC int, err error) int {
	if errors.Is(err, ErrOCIRuntimePermissionDenied) {
		return ExecErrorCodeCannotInvoke
	}
	if errors.Is(err, ErrOCIRuntimeNotFound) {
		return ExecErrorCodeNotFound
	}
	if errors.Is(err, ErrInvalidArg) {
		return ExecErrorCodeGeneric
	}

	return originalEC
}

// ExitCode reads the error message when failing to executing container process
// and then returns 0 if no error, ExecErrorCodeNotFound if command does not exist, or ExecErrorCodeCannotInvoke for
// all other errors
func ExitCode(err error) int {
	if err == nil {
		return 0
	}
	e := strings.ToLower(err.Error())
	logrus.Debugf("ExitCode msg: %q", e)
	if strings.Contains(e, "not found") ||
		strings.Contains(e, "executable path is empty") ||
		strings.Contains(e, "no such file") {
		return ExecErrorCodeNotFound
	}

	return ExecErrorCodeCannotInvoke
}