diff --git a/cmd/runmqdevserver/main.go b/cmd/runmqdevserver/main.go index 0007b56..1cf6763 100644 --- a/cmd/runmqdevserver/main.go +++ b/cmd/runmqdevserver/main.go @@ -22,7 +22,6 @@ import ( "os/exec" "syscall" - "github.com/ibm-messaging/mq-container/internal/command" "github.com/ibm-messaging/mq-container/internal/mqtemplate" "github.com/ibm-messaging/mq-container/pkg/containerruntimelogger" "github.com/ibm-messaging/mq-container/pkg/logger" @@ -43,7 +42,7 @@ func setPassword(user string, password string) error { if err != nil { log.Errorf("Error closing password stdin: %v", err) } - out, _, err := command.RunCmd(cmd) + out, err := cmd.CombinedOutput() if err != nil { // Include the command output in the error return fmt.Errorf("%v: %v", err.Error(), out) diff --git a/cmd/runmqserver/webserver.go b/cmd/runmqserver/webserver.go index c0a66ad..6318451 100644 --- a/cmd/runmqserver/webserver.go +++ b/cmd/runmqserver/webserver.go @@ -73,7 +73,8 @@ func startWebServer(keystore, keystorepw, p12TrustStoreRef string) error { cmd.SysProcAttr = &syscall.SysProcAttr{} cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)} } - out, rc, err := command.RunCmd(cmd) + out, err := cmd.CombinedOutput() + rc := cmd.ProcessState.ExitCode() if err != nil { log.Printf("Error %v starting web server: %v", rc, string(out)) return err diff --git a/internal/command/command.go b/internal/command/command.go index 7790c55..2dc23e2 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -21,40 +21,24 @@ import ( "fmt" "os/exec" "os/user" - "runtime" "strconv" "syscall" ) -// RunCmd runs an OS command. On Linux it waits for the command to -// complete and returns the exit status (return code). -// Do not use this function to run shell built-ins (like "cd"), because -// the error handling works differently -func RunCmd(cmd *exec.Cmd) (string, int, error) { - // Run the command and wait for completion - out, err := cmd.CombinedOutput() - if err != nil { - // Assert that this is an ExitError - exiterr, ok := err.(*exec.ExitError) - // If the type assertion was correct, and we're on Linux - if ok && runtime.GOOS == "linux" { - status, ok := exiterr.Sys().(syscall.WaitStatus) - if ok { - return string(out), status.ExitStatus(), fmt.Errorf("%v: %v", cmd.Path, err) - } - } - return string(out), -1, err - } - return string(out), 0, nil -} - // Run runs an OS command. On Linux it waits for the command to // complete and returns the exit status (return code). // Do not use this function to run shell built-ins (like "cd"), because // the error handling works differently func Run(name string, arg ...string) (string, int, error) { + // Run the command and wait for completion // #nosec G204 - return RunCmd(exec.Command(name, arg...)) + cmd := exec.Command(name, arg...) + out, err := cmd.CombinedOutput() + rc := cmd.ProcessState.ExitCode() + if err != nil { + return string(out), rc, fmt.Errorf("%v: %v", cmd.Path, err) + } + return string(out), rc, nil } // RunAsMQM runs the specified command as the mqm user @@ -67,7 +51,7 @@ func RunAsMQM(name string, arg ...string) (string, int, error) { return "", 0, err } cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)} - return RunCmd(cmd) + return Run(name, arg...) } // LookupMQM looks up the UID & GID of the mqm user diff --git a/internal/keystore/keystore.go b/internal/keystore/keystore.go index 2c2b758..792f691 100644 --- a/internal/keystore/keystore.go +++ b/internal/keystore/keystore.go @@ -207,7 +207,7 @@ func (ks *KeyStore) RenameCertificate(from, to string) error { // #nosec G204 cmd := exec.Command("/opt/mqm/gskit8/bin/gsk8capicmd_64", "-cert", "-rename", "-db", ks.Filename, "-pw", ks.Password, "-label", from, "-new_label", to) cmd.Env = append(os.Environ(), "LD_LIBRARY_PATH=/opt/mqm/gskit8/lib64/:/opt/mqm/gskit8/lib") - out, _, err := command.RunCmd(cmd) + out, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("error running \"%v -cert -rename\": %v %s", "/opt/mqm/gskit8/bin/gsk8capicmd_64", err, out) }