From 09f7d5e595450a51d9962ca41ad09b6bcd3f23d7 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Sat, 7 May 2016 10:43:02 -0700 Subject: [PATCH] Prefer `golang.org/x/sys/unix` where appropriate Favor the `unix` package on *NIX platforms vs the now frozen `syscall` package. --- client/allocdir/alloc_dir_unix.go | 5 +++-- client/driver/exec.go | 5 +++-- client/driver/executor/executor.go | 26 ---------------------- client/driver/executor/executor_unix.go | 29 +++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/client/allocdir/alloc_dir_unix.go b/client/allocdir/alloc_dir_unix.go index 0e8bdd39a..b03fd8e9d 100644 --- a/client/allocdir/alloc_dir_unix.go +++ b/client/allocdir/alloc_dir_unix.go @@ -8,7 +8,8 @@ import ( "os" "os/user" "strconv" - "syscall" + + "golang.org/x/sys/unix" ) func (d *AllocDir) linkOrCopy(src, dst string, perm os.FileMode) error { @@ -22,7 +23,7 @@ func (d *AllocDir) linkOrCopy(src, dst string, perm os.FileMode) error { func (d *AllocDir) dropDirPermissions(path string) error { // Can't do anything if not root. - if syscall.Geteuid() != 0 { + if unix.Geteuid() != 0 { return nil } diff --git a/client/driver/exec.go b/client/driver/exec.go index 4bdfb7fcd..2fbd55306 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -7,9 +7,10 @@ import ( "os/exec" "path/filepath" "strings" - "syscall" "time" + "golang.org/x/sys/unix" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/go-plugin" "github.com/hashicorp/nomad/client/allocdir" @@ -93,7 +94,7 @@ func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, } delete(node.Attributes, execDriverAttr) return false, nil - } else if syscall.Geteuid() != 0 { + } else if unix.Geteuid() != 0 { if currentlyEnabled { d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling") } diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index ce36c5e4e..620df4d98 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -11,7 +11,6 @@ import ( "runtime" "strings" "sync" - "syscall" "time" "github.com/hashicorp/go-multierror" @@ -324,31 +323,6 @@ 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 { - 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()} -} - 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 7c8ddf724..5eeddc997 100644 --- a/client/driver/executor/executor_unix.go +++ b/client/driver/executor/executor_unix.go @@ -6,6 +6,10 @@ import ( "fmt" "io" "log/syslog" + "os/exec" + "time" + + "golang.org/x/sys/unix" "github.com/hashicorp/nomad/client/driver/logging" ) @@ -48,3 +52,28 @@ 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()} +}