2018-09-24 18:37:45 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-02 21:38:38 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-24 18:37:45 +00:00
|
|
|
"context"
|
2016-02-03 02:54:04 +00:00
|
|
|
"fmt"
|
2016-02-03 18:23:00 +00:00
|
|
|
"os"
|
2018-09-24 18:37:45 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2016-02-03 02:54:04 +00:00
|
|
|
"path/filepath"
|
2016-04-19 20:48:02 +00:00
|
|
|
"strings"
|
2016-02-03 18:23:00 +00:00
|
|
|
"syscall"
|
2016-05-09 14:57:26 +00:00
|
|
|
"time"
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
"github.com/armon/circbuf"
|
|
|
|
"github.com/hashicorp/consul-template/signals"
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-05-20 09:05:48 +00:00
|
|
|
"github.com/hashicorp/nomad/client/stats"
|
2016-06-12 03:15:50 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-09-24 18:37:45 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
|
|
|
shelpers "github.com/hashicorp/nomad/helper/stats"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-09-24 18:37:45 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
cgroupFs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
|
|
|
|
lconfigs "github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
|
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultCgroupParent = "nomad"
|
2016-02-03 18:23:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-06-10 02:45:41 +00:00
|
|
|
// The statistics the executor exposes when using cgroups
|
2016-06-10 17:38:29 +00:00
|
|
|
ExecutorCgroupMeasuredMemStats = []string{"RSS", "Cache", "Swap", "Max Usage", "Kernel Usage", "Kernel Max Usage"}
|
|
|
|
ExecutorCgroupMeasuredCpuStats = []string{"System Mode", "User Mode", "Throttled Periods", "Throttled Time", "Percent"}
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
// allCaps is all linux capabilities which is used to configure libcontainer
|
|
|
|
allCaps []string
|
2016-02-02 21:38:38 +00:00
|
|
|
)
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// initialize the allCaps var with all capabilities available on the system
|
|
|
|
func init() {
|
|
|
|
last := capability.CAP_LAST_CAP
|
|
|
|
// workaround for RHEL6 which has no /proc/sys/kernel/cap_last_cap
|
|
|
|
if last == capability.Cap(63) {
|
|
|
|
last = capability.CAP_BLOCK_SUSPEND
|
|
|
|
}
|
|
|
|
for _, cap := range capability.List() {
|
|
|
|
if cap > last {
|
|
|
|
continue
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
allCaps = append(allCaps, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// LibcontainerExecutor implements an Executor with the runc/libcontainer api
|
|
|
|
type LibcontainerExecutor struct {
|
|
|
|
id string
|
|
|
|
command *ExecCommand
|
|
|
|
|
|
|
|
logger hclog.Logger
|
|
|
|
|
|
|
|
totalCpuStats *stats.CpuStats
|
|
|
|
userCpuStats *stats.CpuStats
|
|
|
|
systemCpuStats *stats.CpuStats
|
|
|
|
pidCollector *pidCollector
|
|
|
|
|
|
|
|
container libcontainer.Container
|
|
|
|
userProc *libcontainer.Process
|
|
|
|
userProcExited chan interface{}
|
|
|
|
exitState *ProcessState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutorWithIsolation(logger hclog.Logger) Executor {
|
|
|
|
logger = logger.Named("isolated_executor")
|
|
|
|
if err := shelpers.Init(); err != nil {
|
|
|
|
logger.Error("unable to initialize stats", "error", err)
|
|
|
|
}
|
|
|
|
return &LibcontainerExecutor{
|
|
|
|
id: strings.Replace(uuid.Generate(), "-", "_", 0),
|
|
|
|
logger: logger,
|
|
|
|
totalCpuStats: stats.NewCpuStats(),
|
|
|
|
userCpuStats: stats.NewCpuStats(),
|
|
|
|
systemCpuStats: stats.NewCpuStats(),
|
|
|
|
pidCollector: newPidCollector(logger),
|
2016-02-03 19:41:49 +00:00
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-02-03 19:41:49 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// Launch creates a new container in libcontainer and starts a new process with it
|
|
|
|
func (l *LibcontainerExecutor) Launch(command *ExecCommand) (*ProcessState, error) {
|
|
|
|
l.logger.Info("launching command", "command", command.Cmd, "args", strings.Join(command.Args, " "))
|
|
|
|
// Find the nomad executable to launch the executor process with
|
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2016-02-05 08:11:09 +00:00
|
|
|
|
2018-10-16 02:37:58 +00:00
|
|
|
if command.Resources == nil {
|
|
|
|
command.Resources = &Resources{}
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
l.command = command
|
|
|
|
|
|
|
|
// Move to the root cgroup until process is started
|
|
|
|
subsystems, err := cgroups.GetAllSubsystems()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := JoinRootCgroup(subsystems); err != nil {
|
|
|
|
return nil, err
|
2016-02-03 19:41:49 +00:00
|
|
|
}
|
2018-05-26 01:49:20 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// create a new factory which will store the container state in the allocDir
|
|
|
|
factory, err := libcontainer.New(
|
|
|
|
path.Join(command.TaskDir, "../alloc/container"),
|
|
|
|
libcontainer.Cgroupfs,
|
|
|
|
libcontainer.InitArgs(bin, "libcontainer-shim"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create factory: %v", err)
|
|
|
|
}
|
2018-05-26 01:49:20 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// A container groups processes under the same isolation enforcement
|
|
|
|
container, err := factory.Create(l.id, newLibcontainerConfig(command))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create container(%s): %v", l.id, err)
|
|
|
|
}
|
|
|
|
l.container = container
|
2018-05-26 01:49:20 +00:00
|
|
|
|
2018-10-16 02:37:58 +00:00
|
|
|
// Look up the binary path and make it executable
|
|
|
|
absPath, err := lookupBin(command.TaskDir, command.Cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := makeExecutable(absPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := absPath
|
|
|
|
|
|
|
|
// Determine the path to run as it may have to be relative to the chroot.
|
|
|
|
rel, err := filepath.Rel(command.TaskDir, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to determine relative path base=%q target=%q: %v", command.TaskDir, path, err)
|
|
|
|
}
|
|
|
|
path = rel
|
|
|
|
|
|
|
|
combined := append([]string{path}, command.Args...)
|
2018-09-24 18:37:45 +00:00
|
|
|
stdout, err := command.Stdout()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stderr, err := command.Stderr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-26 01:49:20 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// the task process will be started by the container
|
|
|
|
process := &libcontainer.Process{
|
|
|
|
Args: combined,
|
|
|
|
Env: command.Env,
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
Init: true,
|
|
|
|
}
|
2018-05-26 01:49:20 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if command.User != "" {
|
|
|
|
process.User = command.User
|
|
|
|
}
|
|
|
|
l.userProc = process
|
|
|
|
|
|
|
|
l.totalCpuStats = stats.NewCpuStats()
|
|
|
|
l.userCpuStats = stats.NewCpuStats()
|
|
|
|
l.systemCpuStats = stats.NewCpuStats()
|
|
|
|
|
|
|
|
// Starts the task
|
|
|
|
if err := container.Run(process); err != nil {
|
|
|
|
container.Destroy()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := process.Pid()
|
|
|
|
if err != nil {
|
|
|
|
container.Destroy()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join process cgroups
|
|
|
|
containerState, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
l.logger.Error("error entering user process cgroups", "executor_pid", os.Getpid(), "error", err)
|
|
|
|
}
|
|
|
|
if err := cgroups.EnterPid(containerState.CgroupPaths, os.Getpid()); err != nil {
|
|
|
|
l.logger.Error("error entering user process cgroups", "executor_pid", os.Getpid(), "error", err)
|
2018-05-26 01:49:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// start a goroutine to wait on the process to complete, so Wait calls can
|
|
|
|
// be multiplexed
|
|
|
|
l.userProcExited = make(chan interface{})
|
|
|
|
go l.pidCollector.collectPids(l.userProcExited, l.getAllPids)
|
|
|
|
go l.wait()
|
|
|
|
|
|
|
|
return &ProcessState{
|
|
|
|
Pid: pid,
|
|
|
|
ExitCode: -1,
|
|
|
|
Time: time.Now(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LibcontainerExecutor) getAllPids() (map[int]*nomadPid, error) {
|
|
|
|
pids, err := l.container.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nPids := make(map[int]*nomadPid)
|
|
|
|
for _, pid := range pids {
|
|
|
|
nPids[pid] = &nomadPid{
|
|
|
|
pid: pid,
|
|
|
|
cpuStatsTotal: stats.NewCpuStats(),
|
|
|
|
cpuStatsUser: stats.NewCpuStats(),
|
|
|
|
cpuStatsSys: stats.NewCpuStats(),
|
2016-04-02 08:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
return nPids, nil
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// Wait waits until a process has exited and returns it's exitcode and errors
|
|
|
|
func (l *LibcontainerExecutor) Wait() (*ProcessState, error) {
|
|
|
|
<-l.userProcExited
|
|
|
|
return l.exitState, nil
|
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func (l *LibcontainerExecutor) wait() {
|
|
|
|
defer close(l.userProcExited)
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
ps, err := l.userProc.Wait()
|
|
|
|
if err != nil {
|
|
|
|
// If the process has exited before we called wait an error is returned
|
|
|
|
// the process state is embedded in the error
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
ps = exitErr.ProcessState
|
|
|
|
} else {
|
|
|
|
l.logger.Error("failed to call wait on user process", "error", err)
|
2018-12-01 23:42:02 +00:00
|
|
|
l.exitState = &ProcessState{Pid: 0, ExitCode: -2, Time: time.Now()}
|
2018-09-24 18:37:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.command.Close()
|
|
|
|
|
|
|
|
exitCode := 1
|
|
|
|
var signal int
|
|
|
|
if status, ok := ps.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
if status.Signaled() {
|
|
|
|
const exitSignalBase = 128
|
|
|
|
signal = int(status.Signal())
|
|
|
|
exitCode = exitSignalBase + signal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.exitState = &ProcessState{
|
|
|
|
Pid: ps.Pid(),
|
|
|
|
ExitCode: exitCode,
|
|
|
|
Signal: signal,
|
|
|
|
Time: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops all processes started and cleans up any resources
|
|
|
|
// created (such as mountpoints, devices, etc).
|
|
|
|
func (l *LibcontainerExecutor) Shutdown(signal string, grace time.Duration) error {
|
|
|
|
if l.container == nil {
|
2018-05-25 22:25:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// move executor to root cgroup
|
|
|
|
subsystems, err := cgroups.GetAllSubsystems()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := JoinRootCgroup(subsystems); err != nil {
|
|
|
|
return err
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
status, err := l.container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
defer l.container.Destroy()
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if status == libcontainer.Stopped {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if grace > 0 {
|
|
|
|
if signal == "" {
|
|
|
|
signal = "SIGINT"
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
sig, ok := signals.SignalLookup[signal]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("error unknown signal given for shutdown: %s", signal)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.container.Signal(sig, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-l.userProcExited:
|
|
|
|
return nil
|
|
|
|
case <-time.After(grace):
|
2018-12-01 23:42:02 +00:00
|
|
|
return l.container.Signal(os.Kill, true)
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-01 23:42:02 +00:00
|
|
|
return l.container.Signal(os.Kill, true)
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// UpdateResources updates the resource isolation with new values to be enforced
|
|
|
|
func (l *LibcontainerExecutor) UpdateResources(resources *Resources) error {
|
2016-02-03 18:23:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// Version returns the api version of the executor
|
|
|
|
func (l *LibcontainerExecutor) Version() (*ExecutorVersion, error) {
|
|
|
|
return &ExecutorVersion{Version: ExecutorVersionLatest}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stats returns the resource statistics for processes managed by the executor
|
|
|
|
func (l *LibcontainerExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
|
|
|
|
lstats, err := l.container.Stats()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-05-11 19:56:47 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
pidStats, err := l.pidCollector.pidStats()
|
2016-04-28 23:06:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-29 18:40:37 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
ts := time.Now()
|
|
|
|
stats := lstats.CgroupStats
|
|
|
|
|
2016-04-29 18:40:37 +00:00
|
|
|
// Memory Related Stats
|
2016-04-29 20:03:02 +00:00
|
|
|
swap := stats.MemoryStats.SwapUsage
|
|
|
|
maxUsage := stats.MemoryStats.Usage.MaxUsage
|
2016-04-29 18:06:19 +00:00
|
|
|
rss := stats.MemoryStats.Stats["rss"]
|
|
|
|
cache := stats.MemoryStats.Stats["cache"]
|
2016-04-29 18:40:37 +00:00
|
|
|
ms := &cstructs.MemoryStats{
|
2016-04-29 20:03:02 +00:00
|
|
|
RSS: rss,
|
|
|
|
Cache: cache,
|
|
|
|
Swap: swap.Usage,
|
|
|
|
MaxUsage: maxUsage,
|
|
|
|
KernelUsage: stats.MemoryStats.KernelUsage.Usage,
|
|
|
|
KernelMaxUsage: stats.MemoryStats.KernelUsage.MaxUsage,
|
2016-06-10 02:45:41 +00:00
|
|
|
Measured: ExecutorCgroupMeasuredMemStats,
|
2016-04-29 18:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CPU Related Stats
|
2016-06-10 17:38:29 +00:00
|
|
|
totalProcessCPUUsage := float64(stats.CpuStats.CpuUsage.TotalUsage)
|
|
|
|
userModeTime := float64(stats.CpuStats.CpuUsage.UsageInUsermode)
|
|
|
|
kernelModeTime := float64(stats.CpuStats.CpuUsage.UsageInKernelmode)
|
2016-04-29 18:40:37 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
totalPercent := l.totalCpuStats.Percent(totalProcessCPUUsage)
|
2016-05-21 07:49:17 +00:00
|
|
|
cs := &cstructs.CpuStats{
|
2018-09-24 18:37:45 +00:00
|
|
|
SystemMode: l.systemCpuStats.Percent(kernelModeTime),
|
|
|
|
UserMode: l.userCpuStats.Percent(userModeTime),
|
2016-06-10 23:16:52 +00:00
|
|
|
Percent: totalPercent,
|
2016-04-29 20:03:02 +00:00
|
|
|
ThrottledPeriods: stats.CpuStats.ThrottlingData.ThrottledPeriods,
|
|
|
|
ThrottledTime: stats.CpuStats.ThrottlingData.ThrottledTime,
|
2018-09-24 18:37:45 +00:00
|
|
|
TotalTicks: l.systemCpuStats.TicksConsumed(totalPercent),
|
2016-06-10 02:45:41 +00:00
|
|
|
Measured: ExecutorCgroupMeasuredCpuStats,
|
2016-04-29 18:40:37 +00:00
|
|
|
}
|
2016-05-25 05:54:32 +00:00
|
|
|
taskResUsage := cstructs.TaskResourceUsage{
|
|
|
|
ResourceUsage: &cstructs.ResourceUsage{
|
|
|
|
MemoryStats: ms,
|
|
|
|
CpuStats: cs,
|
|
|
|
},
|
2016-05-28 02:08:17 +00:00
|
|
|
Timestamp: ts.UTC().UnixNano(),
|
2018-09-24 18:37:45 +00:00
|
|
|
Pids: pidStats,
|
2016-05-25 05:54:32 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
|
2016-05-21 09:05:08 +00:00
|
|
|
return &taskResUsage, nil
|
2016-04-28 23:06:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// Signal sends a signal to the process managed by the executor
|
|
|
|
func (l *LibcontainerExecutor) Signal(s os.Signal) error {
|
|
|
|
return l.userProc.Signal(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec starts an additional process inside the container
|
|
|
|
func (l *LibcontainerExecutor) Exec(deadline time.Time, cmd string, args []string) ([]byte, int, error) {
|
|
|
|
combined := append([]string{cmd}, args...)
|
|
|
|
// Capture output
|
2018-11-30 09:56:45 +00:00
|
|
|
buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize))
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
process := &libcontainer.Process{
|
|
|
|
Args: combined,
|
|
|
|
Env: l.command.Env,
|
|
|
|
Stdout: buf,
|
|
|
|
Stderr: buf,
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
err := l.container.Run(process)
|
2017-03-20 21:21:13 +00:00
|
|
|
if err != nil {
|
2018-09-24 18:37:45 +00:00
|
|
|
return nil, 0, err
|
2017-03-20 21:21:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
waitCh := make(chan *waitResult)
|
|
|
|
defer close(waitCh)
|
|
|
|
go l.handleExecWait(waitCh, process)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result := <-waitCh:
|
|
|
|
ps := result.ps
|
|
|
|
if result.err != nil {
|
|
|
|
if exitErr, ok := result.err.(*exec.ExitError); ok {
|
|
|
|
ps = exitErr.ProcessState
|
|
|
|
} else {
|
|
|
|
return nil, 0, result.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var exitCode int
|
|
|
|
if status, ok := ps.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
2017-03-20 21:21:13 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
return buf.Bytes(), exitCode, nil
|
2017-03-20 21:21:13 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
case <-time.After(time.Until(deadline)):
|
|
|
|
process.Signal(os.Kill)
|
|
|
|
return nil, 0, context.DeadlineExceeded
|
2017-03-20 21:21:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type waitResult struct {
|
|
|
|
ps *os.ProcessState
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LibcontainerExecutor) handleExecWait(ch chan *waitResult, process *libcontainer.Process) {
|
|
|
|
ps, err := process.Wait()
|
|
|
|
ch <- &waitResult{ps, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureCapabilities(cfg *lconfigs.Config, command *ExecCommand) {
|
|
|
|
// TODO: allow better control of these
|
|
|
|
cfg.Capabilities = &lconfigs.Capabilities{
|
|
|
|
Bounding: allCaps,
|
|
|
|
Permitted: allCaps,
|
|
|
|
Inheritable: allCaps,
|
|
|
|
Ambient: allCaps,
|
|
|
|
Effective: allCaps,
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func configureIsolation(cfg *lconfigs.Config, command *ExecCommand) {
|
|
|
|
defaultMountFlags := syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
|
|
|
|
|
|
|
|
// set the new root directory for the container
|
|
|
|
cfg.Rootfs = command.TaskDir
|
|
|
|
|
|
|
|
// launch with mount namespace
|
|
|
|
cfg.Namespaces = lconfigs.Namespaces{
|
|
|
|
{Type: lconfigs.NEWNS},
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
// paths to mask using a bind mount to /dev/null to prevent reading
|
|
|
|
cfg.MaskPaths = []string{
|
|
|
|
"/proc/kcore",
|
|
|
|
"/sys/firmware",
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2017-03-20 21:21:13 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// paths that should be remounted as readonly inside the container
|
|
|
|
cfg.ReadonlyPaths = []string{
|
|
|
|
"/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus",
|
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
cfg.Devices = lconfigs.DefaultAutoCreatedDevices
|
|
|
|
cfg.Mounts = []*lconfigs.Mount{
|
|
|
|
{
|
|
|
|
Source: "tmpfs",
|
|
|
|
Destination: "/dev",
|
|
|
|
Device: "tmpfs",
|
|
|
|
Flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME,
|
|
|
|
Data: "mode=755",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "devpts",
|
|
|
|
Destination: "/dev/pts",
|
|
|
|
Device: "devpts",
|
|
|
|
Flags: syscall.MS_NOSUID | syscall.MS_NOEXEC,
|
|
|
|
Data: "newinstance,ptmxmode=0666,mode=0620,gid=5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Device: "tmpfs",
|
|
|
|
Source: "shm",
|
|
|
|
Destination: "/dev/shm",
|
|
|
|
Data: "mode=1777,size=65536k",
|
|
|
|
Flags: defaultMountFlags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "mqueue",
|
|
|
|
Destination: "/dev/mqueue",
|
|
|
|
Device: "mqueue",
|
|
|
|
Flags: defaultMountFlags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "sysfs",
|
|
|
|
Destination: "/sys",
|
|
|
|
Device: "sysfs",
|
|
|
|
Flags: defaultMountFlags | syscall.MS_RDONLY,
|
|
|
|
},
|
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func configureCgroups(cfg *lconfigs.Config, command *ExecCommand) error {
|
|
|
|
|
|
|
|
// If resources are not limited then manually create cgroups needed
|
|
|
|
if !command.ResourceLimits {
|
|
|
|
return configureBasicCgroups(cfg)
|
2016-02-03 18:23:00 +00:00
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
id := uuid.Generate()
|
|
|
|
cfg.Cgroups.Path = filepath.Join(defaultCgroupParent, id)
|
|
|
|
if command.Resources.MemoryMB > 0 {
|
|
|
|
// Total amount of memory allowed to consume
|
|
|
|
cfg.Cgroups.Resources.Memory = int64(command.Resources.MemoryMB * 1024 * 1024)
|
|
|
|
// Disable swap to avoid issues on the machine
|
|
|
|
var memSwappiness uint64 = 0
|
|
|
|
cfg.Cgroups.Resources.MemorySwappiness = &memSwappiness
|
|
|
|
}
|
2016-02-03 18:23:00 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if command.Resources.CPU < 2 {
|
|
|
|
return fmt.Errorf("resources.CPU must be equal to or greater than 2: %v", command.Resources.CPU)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the relative CPU shares for this cgroup.
|
|
|
|
cfg.Cgroups.Resources.CpuShares = uint64(command.Resources.CPU)
|
|
|
|
|
|
|
|
if command.Resources.IOPS != 0 {
|
|
|
|
// Validate it is in an acceptable range.
|
|
|
|
if command.Resources.IOPS < 10 || command.Resources.IOPS > 1000 {
|
|
|
|
return fmt.Errorf("resources.IOPS must be between 10 and 1000: %d", command.Resources.IOPS)
|
2016-05-20 09:05:48 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
cfg.Cgroups.Resources.BlkioWeight = uint16(command.Resources.IOPS)
|
2016-05-11 19:56:47 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureBasicCgroups(cfg *lconfigs.Config) error {
|
|
|
|
id := uuid.Generate()
|
|
|
|
|
|
|
|
// Manually create freezer cgroup
|
|
|
|
cfg.Cgroups.Paths = map[string]string{}
|
|
|
|
root, err := cgroups.FindCgroupMountpointDir()
|
2016-05-26 07:53:41 +00:00
|
|
|
if err != nil {
|
2018-09-24 18:37:45 +00:00
|
|
|
return err
|
2016-05-26 07:53:41 +00:00
|
|
|
}
|
2016-05-11 19:56:47 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if _, err := os.Stat(root); err != nil {
|
|
|
|
return err
|
2016-02-03 19:41:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
freezer := cgroupFs.FreezerGroup{}
|
|
|
|
subsystem := freezer.Name()
|
|
|
|
path, err := cgroups.FindCgroupMountpoint(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find %s cgroup mountpoint: %v", subsystem, err)
|
2016-04-19 20:48:02 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
// Sometimes subsystems can be mounted together as 'cpu,cpuacct'.
|
|
|
|
path = filepath.Join(root, filepath.Base(path), defaultCgroupParent, id)
|
2016-04-19 20:48:02 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if err = os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
2016-04-19 20:48:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
cfg.Cgroups.Paths[subsystem] = path
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-19 20:48:02 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func newLibcontainerConfig(command *ExecCommand) *lconfigs.Config {
|
|
|
|
cfg := &lconfigs.Config{
|
|
|
|
Cgroups: &lconfigs.Cgroup{
|
|
|
|
Resources: &lconfigs.Resources{
|
|
|
|
AllowAllDevices: nil,
|
|
|
|
MemorySwappiness: nil,
|
|
|
|
AllowedDevices: lconfigs.DefaultAllowedDevices,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Version: "1.0.0",
|
2016-04-19 20:48:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
configureCapabilities(cfg, command)
|
|
|
|
configureIsolation(cfg, command)
|
|
|
|
configureCgroups(cfg, command)
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinRootCgroup moves the current process to the cgroups of the init process
|
|
|
|
func JoinRootCgroup(subsystems []string) error {
|
|
|
|
mErrs := new(multierror.Error)
|
|
|
|
paths := map[string]string{}
|
|
|
|
for _, s := range subsystems {
|
|
|
|
mnt, _, err := cgroups.FindCgroupMountpointAndRoot(s)
|
2016-04-19 20:48:02 +00:00
|
|
|
if err != nil {
|
2018-09-24 18:37:45 +00:00
|
|
|
multierror.Append(mErrs, fmt.Errorf("error getting cgroup path for subsystem: %s", s))
|
2016-04-19 20:48:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
paths[s] = mnt
|
2016-04-19 20:48:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
err := cgroups.EnterPid(paths, os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
multierror.Append(mErrs, err)
|
2016-04-19 00:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 20:48:02 +00:00
|
|
|
return mErrs.ErrorOrNil()
|
2016-02-03 19:41:49 +00:00
|
|
|
}
|