Skip to content

Commit

Permalink
Use exit code constants
Browse files Browse the repository at this point in the history
We have leaked the exit number codess all over the code, this patch
removes the numbers to constants.

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Sep 12, 2019
1 parent af8fedc commit 535111b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cmd/podman/containers_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func pruneContainersCmd(c *cliconfig.PruneContainersValues) error {
if err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
if len(c.InputArgs) > 1 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
} else {
exitCode = 1
}
}
return err
}
if len(failures) > 0 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
}
return printCmdResults(ok, failures)
}
8 changes: 5 additions & 3 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/define"
_ "github.com/containers/libpod/pkg/hooks/0.1.0"
"github.com/containers/libpod/pkg/rootless"
"github.com/containers/libpod/version"
Expand All @@ -20,7 +21,7 @@ import (
// This is populated by the Makefile from the VERSION file
// in the repository
var (
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
Ctx context.Context
span opentracing.Span
closer io.Closer
Expand Down Expand Up @@ -152,11 +153,12 @@ func main() {
if err := rootCmd.Execute(); err != nil {
outputError(err)
} else {
// The exitCode modified from 125, indicates an application
// The exitCode modified from define.ExecErrorCodeGeneric,
// indicates an application
// running inside of a container failed, as opposed to the
// podman command failed. Must exit with that exit code
// otherwise command exited correctly.
if exitCode == 125 {
if exitCode == define.ExecErrorCodeGeneric {
exitCode = 0
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func pauseCmd(c *cliconfig.PauseValues) error {
if err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
if len(c.InputArgs) > 1 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
} else {
exitCode = 1
}
}
return err
}
if len(failures) > 0 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
}
return printCmdResults(ok, failures)
}
4 changes: 2 additions & 2 deletions cmd/podman/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func restartCmd(c *cliconfig.RestartValues) error {
if err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
if len(c.InputArgs) > 1 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
} else {
exitCode = 1
}
}
return err
}
if len(failures) > 0 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
}
return printCmdResults(ok, failures)
}
4 changes: 2 additions & 2 deletions cmd/podman/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ func unpauseCmd(c *cliconfig.UnpauseValues) error {
if err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
if len(c.InputArgs) > 1 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
} else {
exitCode = 1
}
}
return err
}
if len(failures) > 0 {
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
}
return printCmdResults(ok, failures)
}
4 changes: 2 additions & 2 deletions libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ func (c *Container) Kill(signal uint) error {
}

// Exec starts a new process inside the container
// Returns an exit code and an error. If Exec was not able to exec in the container before a failure, an exit code of 126 is returned.
// If another generic error happens, an exit code of 125 is returned.
// Returns an exit code and an error. If Exec was not able to exec in the container before a failure, an exit code of define.ExecErrorCodeCannotInvoke is returned.
// If another generic error happens, an exit code of define.ExecErrorCodeGeneric is returned.
// Sometimes, the $RUNTIME exec call errors, and if that is the case, the exit code is the exit code of the call.
// Otherwise, the exit code will be the exit code of the executed call inside of the container.
// TODO investigate allowing exec without attaching
Expand Down
18 changes: 18 additions & 0 deletions libpod/define/exec_codes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package define

import (
"strings"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -28,3 +30,19 @@ func TranslateExecErrorToExitCode(originalEC int, err error) int {
}
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())
if strings.Contains(e, "file not found") ||
strings.Contains(e, "no such file or directory") {
return ExecErrorCodeNotFound
}

return ExecErrorCodeCannotInvoke
}
17 changes: 6 additions & 11 deletions pkg/adapter/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,7 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
// if the container was created as part of a pod, also start its dependencies, if any.
if err := ctr.Start(ctx, c.IsSet("pod")); err != nil {
// This means the command did not exist
exitCode = 127
e := strings.ToLower(err.Error())
if strings.Contains(e, "permission denied") || strings.Contains(e, "operation not permitted") || strings.Contains(e, "file not found") || strings.Contains(e, "no such file or directory") {
exitCode = 126
}
return exitCode, err
return define.ExitCode(err), err
}

fmt.Printf("%s\n", ctr.ID())
Expand Down Expand Up @@ -415,7 +410,7 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
logrus.Debugf("unable to remove container %s after failing to start and attach to it", ctr.ID())
}
}
return exitCode, err
return define.ExitCode(err), err
}

if ecode, err := ctr.Wait(); err != nil {
Expand All @@ -424,7 +419,7 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
event, err := r.Runtime.GetLastContainerEvent(ctr.ID(), events.Exited)
if err != nil {
logrus.Errorf("Cannot get exit code: %v", err)
exitCode = 127
exitCode = define.ExecErrorCodeNotFound
} else {
exitCode = event.ContainerExitCode
}
Expand Down Expand Up @@ -576,7 +571,7 @@ func (r *LocalRuntime) Restore(ctx context.Context, c *cliconfig.RestoreValues)
// Start will start a container
func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigProxy bool) (int, error) {
var (
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
lastError error
)

Expand Down Expand Up @@ -636,7 +631,7 @@ func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigP
event, err := r.Runtime.GetLastContainerEvent(ctr.ID(), events.Exited)
if err != nil {
logrus.Errorf("Cannot get exit code: %v", err)
exitCode = 127
exitCode = define.ExecErrorCodeNotFound
} else {
exitCode = event.ContainerExitCode
}
Expand Down Expand Up @@ -914,7 +909,7 @@ func (r *LocalRuntime) ExecContainer(ctx context.Context, cli *cliconfig.ExecVal
cmd []string
)
// default invalid command exit code
ec := 125
ec := define.ExecErrorCodeGeneric

if cli.Latest {
if ctr, err = r.GetLatestContainer(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/adapter/containers_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ func (r *LocalRuntime) Restore(ctx context.Context, c *cliconfig.RestoreValues)
func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigProxy bool) (int, error) {
var (
finalErr error
exitCode = 125
exitCode = define.ExecErrorCodeGeneric
)
// TODO Figure out how to deal with exit codes
inputStream := os.Stdin
Expand Down
13 changes: 7 additions & 6 deletions test/e2e/run_exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration
import (
"os"

"github.com/containers/libpod/libpod/define"
. "github.com/containers/libpod/test/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -34,22 +35,22 @@ var _ = Describe("Podman run exit", func() {

})

It("podman run exit 125", func() {
It("podman run exit define.ExecErrorCodeGeneric", func() {
result := podmanTest.Podman([]string{"run", "--foobar", ALPINE, "ls", "$tmp"})
result.WaitWithDefaultTimeout()
Expect(result.ExitCode()).To(Equal(125))
Expect(result.ExitCode()).To(Equal(define.ExecErrorCodeGeneric))
})

It("podman run exit 126", func() {
It("podman run exit ExecErrorCodeCannotInvoke", func() {
result := podmanTest.Podman([]string{"run", ALPINE, "/etc"})
result.WaitWithDefaultTimeout()
Expect(result.ExitCode()).To(Equal(126))
Expect(result.ExitCode()).To(Equal(define.ExecErrorCodeCannotInvoke))
})

It("podman run exit 127", func() {
It("podman run exit ExecErrorCodeNotFound", func() {
result := podmanTest.Podman([]string{"run", ALPINE, "foobar"})
result.WaitWithDefaultTimeout()
Expect(result.ExitCode()).To(Equal(127))
Expect(result.ExitCode()).To(Equal(define.ExecErrorCodeNotFound))
})

It("podman run exit 0", func() {
Expand Down

0 comments on commit 535111b

Please sign in to comment.