diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index 620df4d98..3fa43c561 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -11,6 +11,7 @@ import ( "runtime" "strings" "sync" + "syscall" "time" "github.com/hashicorp/go-multierror" @@ -323,6 +324,40 @@ func (e *UniversalExecutor) UpdateTask(task *structs.Task) error { return nil } +func (e *UniversalExecutor) wait() { + defer close(e.processExited) + err := e.cmd.Wait() + ic := &cstructs.IsolationConfig{Cgroup: e.groups, CgroupPaths: e.cgPaths} + if err == nil { + e.exitState = &ProcessState{Pid: 0, ExitCode: 0, IsolationConfig: ic, Time: time.Now()} + return + } + exitCode := 1 + var signal int + if exitErr, ok := err.(*exec.ExitError); ok { + if status, ok := exitErr.Sys().(syscall.WaitStatus); ok { + fmt.Printf("Found unix wait status\n") + exitCode = status.ExitStatus() + if status.Signaled() { + // bash(1) uses the lower 7 bits of a uint8 + // to indicate normal program failure (see + // ). If a process terminates due + // to a signal, encode the signal number to + // indicate which signal caused the process + // to terminate. Mirror this exit code + // encoding scheme. + const exitSignalBase = 128 + signal = int(status.Signal()) + exitCode = exitSignalBase + signal + } + } + } else { + e.logger.Printf("[DEBUG] executor: unexpected Wait() error type: %v", err) + } + + e.exitState = &ProcessState{Pid: 0, ExitCode: exitCode, Signal: signal, IsolationConfig: ic, Time: time.Now()} +} + var ( // finishedErr is the error message received when trying to kill and already // exited process. diff --git a/client/driver/executor/executor_unix.go b/client/driver/executor/executor_unix.go index 15627c9ca..7c8ddf724 100644 --- a/client/driver/executor/executor_unix.go +++ b/client/driver/executor/executor_unix.go @@ -6,13 +6,8 @@ import ( "fmt" "io" "log/syslog" - "os/exec" - "time" - - "golang.org/x/sys/unix" "github.com/hashicorp/nomad/client/driver/logging" - cstructs "github.com/hashicorp/nomad/client/driver/structs" ) func (e *UniversalExecutor) LaunchSyslogServer(ctx *ExecutorContext) (*SyslogServerState, error) { @@ -53,28 +48,3 @@ func (e *UniversalExecutor) collectLogs(we io.Writer, wo io.Writer) { } } } - -func (e *UniversalExecutor) wait() { - defer close(e.processExited) - err := e.cmd.Wait() - ic := &cstructs.IsolationConfig{Cgroup: e.groups, CgroupPaths: e.cgPaths} - if err == nil { - e.exitState = &ProcessState{Pid: 0, ExitCode: 0, IsolationConfig: ic, Time: time.Now()} - return - } - exitCode := 1 - var signal int - if exitErr, ok := err.(*exec.ExitError); ok { - if status, ok := exitErr.Sys().(unix.WaitStatus); ok { - exitCode = status.ExitStatus() - if status.Signaled() { - signal = int(status.Signal()) - exitCode = 128 + signal - } - } - } else { - e.logger.Printf("[DEBUG] executor: unexpected Wait() error type: %v", err) - } - - e.exitState = &ProcessState{Pid: 0, ExitCode: exitCode, Signal: signal, IsolationConfig: ic, Time: time.Now()} -} diff --git a/client/driver/executor/executor_windows.go b/client/driver/executor/executor_windows.go index 0108a8c3d..e93f936e7 100644 --- a/client/driver/executor/executor_windows.go +++ b/client/driver/executor/executor_windows.go @@ -1,38 +1,5 @@ package executor -import ( - "os/exec" - "time" - - cstructs "github.com/hashicorp/nomad/client/driver/structs" - "golang.org/x/sys/windows" -) - func (e *UniversalExecutor) LaunchSyslogServer(ctx *ExecutorContext) (*SyslogServerState, error) { return nil, nil } - -func (e *UniversalExecutor) wait() { - defer close(e.processExited) - err := e.cmd.Wait() - ic := &cstructs.IsolationConfig{Cgroup: e.groups, CgroupPaths: e.cgPaths} - if err == nil { - e.exitState = &ProcessState{Pid: 0, ExitCode: 0, IsolationConfig: ic, Time: time.Now()} - return - } - exitCode := 1 - var signal int - if exitErr, ok := err.(*exec.ExitError); ok { - if status, ok := exitErr.Sys().(windows.WaitStatus); ok { - exitCode = status.ExitStatus() - if status.Signaled() { - signal = int(status.Signal()) - exitCode = 128 + signal - } - } - } else { - e.logger.Printf("[DEBUG] executor: unexpected Wait() error type: %v", err) - } - - e.exitState = &ProcessState{Pid: 0, ExitCode: exitCode, Signal: signal, IsolationConfig: ic, Time: time.Now()} -}