Add a helper functions for checking unix root

This commit is contained in:
Mahmood Ali 2018-11-07 11:53:11 -08:00
parent 21d126cd68
commit 032f86bc78
3 changed files with 13 additions and 2 deletions

View file

@ -22,7 +22,6 @@ import (
"github.com/hashicorp/nomad/plugins/shared/hclspec" "github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/loader" "github.com/hashicorp/nomad/plugins/shared/loader"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/sys/unix"
) )
const ( const (
@ -207,7 +206,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
return fp return fp
} }
if unix.Geteuid() != 0 { if !utils.IsUnixRoot() {
fp.Health = drivers.HealthStateUnhealthy fp.Health = drivers.HealthStateUnhealthy
fp.HealthDescription = "exec driver must run as root" fp.HealthDescription = "exec driver must run as root"
return fp return fp

View file

@ -5,6 +5,8 @@ package utils
import ( import (
"os/exec" "os/exec"
"syscall" "syscall"
"golang.org/x/sys/unix"
) )
// isolateCommand sets the setsid flag in exec.Cmd to true so that the process // isolateCommand sets the setsid flag in exec.Cmd to true so that the process
@ -16,3 +18,8 @@ func isolateCommand(cmd *exec.Cmd) {
} }
cmd.SysProcAttr.Setsid = true cmd.SysProcAttr.Setsid = true
} }
// IsUnixRoot returns true if system is unix and user running is effectively root
func IsUnixRoot() bool {
return unix.Geteuid() == 0
}

View file

@ -6,3 +6,8 @@ import (
// TODO Figure out if this is needed in Windows // TODO Figure out if this is needed in Windows
func isolateCommand(cmd *exec.Cmd) {} func isolateCommand(cmd *exec.Cmd) {}
// IsUnixRoot returns true if system is a unix system and the effective uid of user is root
func IsUnixRoot() bool {
return false
}