Skip to content

Commit

Permalink
Merge pull request #1156 from hashicorp/f-x-sys-unix
Browse files Browse the repository at this point in the history
Use x/sys/unix vs syscall package where appropriate
  • Loading branch information
sean- committed May 9, 2016
2 parents 902550c + b84bf39 commit 9d4e37a
Show file tree
Hide file tree
Showing 36 changed files with 5,899 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

// Functions shared between linux/darwin.
package allocdir
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
17 changes: 14 additions & 3 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,20 @@ func TestDockerUser(t *testing.T) {
handle.Kill()
t.Fatalf("Should've failed")
}
msg := "System error: Unable to find user alice"
if !strings.Contains(err.Error(), msg) {
t.Fatalf("Expecting '%v' in '%v'", msg, err)

msgs := []string{
"System error: Unable to find user alice",
"linux spec user: Unable to find user alice",
}
var found bool
for _, msg := range msgs {
if strings.Contains(err.Error(), msg) {
found = true
break
}
}
if !found {
t.Fatalf("Expected failure string not found, found %q instead", err.Error())
}
}

Expand Down
28 changes: 0 additions & 28 deletions client/driver/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -81,33 +80,6 @@ func (d *ExecDriver) Validate(config map[string]interface{}) error {
return nil
}

func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Get the current status so that we can log any debug messages only if the
// state changes
_, currentlyEnabled := node.Attributes[execDriverAttr]

// Only enable if cgroups are available and we are root
if _, ok := node.Attributes["unique.cgroup.mountpoint"]; !ok {
if currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling")
}
delete(node.Attributes, execDriverAttr)
return false, nil
} else if syscall.Geteuid() != 0 {
if currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
}
delete(node.Attributes, execDriverAttr)
return false, nil
}

if !currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: exec driver is enabled")
}
node.Attributes[execDriverAttr] = "1"
return true, nil
}

func (d *ExecDriver) Periodic() (bool, time.Duration) {
return true, 15 * time.Second
}
Expand Down
12 changes: 12 additions & 0 deletions client/driver/exec_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris

package driver

import (
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)

func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
return false, nil
}
36 changes: 36 additions & 0 deletions client/driver/exec_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package driver

import (
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
"golang.org/x/sys/unix"
)

func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Get the current status so that we can log any debug messages only if the
// state changes
_, currentlyEnabled := node.Attributes[execDriverAttr]

// Only enable if cgroups are available and we are root
if _, ok := node.Attributes["unique.cgroup.mountpoint"]; !ok {
if currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling")
}
delete(node.Attributes, execDriverAttr)
return false, nil
} else if unix.Geteuid() != 0 {
if currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
}
delete(node.Attributes, execDriverAttr)
return false, nil
}

if !currentlyEnabled {
d.logger.Printf("[DEBUG] driver.exec: exec driver is enabled")
}
node.Attributes[execDriverAttr] = "1"
return true, nil
}
1 change: 0 additions & 1 deletion client/driver/executor/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/armon/circbuf"
docker "github.com/fsouza/go-dockerclient"

cstructs "github.com/hashicorp/nomad/client/driver/structs"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package executor

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// +build !linux
// +build windows

package executor

import (
"os/exec"
)
import "os/exec"

func (e *ExecScriptCheck) setChroot(cmd *exec.Cmd) {
}
10 changes: 9 additions & 1 deletion client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,16 @@ func (e *UniversalExecutor) wait() {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
if status.Signaled() {
// bash(1) uses the lower 7 bits of a uint8
// to indicate normal program failure (see
// <sysexits.h>). 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 = 128 + signal
exitCode = exitSignalBase + signal
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package executor

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package logging

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package logging

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package logging

Expand Down
16 changes: 0 additions & 16 deletions client/driver/utils_linux.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux,!windows
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package driver

Expand Down
5 changes: 4 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GIT_DIRTY="$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)"

# Determine the arch/os combos we're building for
XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
XC_OS=${XC_OS:-darwin dragonfly freebsd linux netbsd openbsd solaris windows}

# Delete the old dir
echo "==> Removing old directory..."
Expand All @@ -35,8 +35,11 @@ fi
echo "==> Building..."
gox \
-os="${XC_OS}" \
-os="!dragonfly" \
-os="!freebsd" \
-os="!netbsd" \
-os="!openbsd" \
-os="!solaris" \
-arch="${XC_ARCH}" \
-osarch="!linux/arm !darwin/386" \
-ldflags "-X main.GitCommit='${GIT_COMMIT}${GIT_DIRTY}'" \
Expand Down
13 changes: 13 additions & 0 deletions vendor/golang.org/x/sys/windows/asm_windows_386.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/golang.org/x/sys/windows/asm_windows_amd64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d4e37a

Please sign in to comment.