2014-10-23 04:45:23 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
2014-10-31 06:08:28 +08:00
|
|
|
import (
|
2015-02-12 08:45:23 +08:00
|
|
|
"encoding/json"
|
2014-12-15 23:05:11 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-02-12 08:45:23 +08:00
|
|
|
"path/filepath"
|
2015-02-14 06:41:37 +08:00
|
|
|
"sync"
|
2014-12-15 23:05:11 +08:00
|
|
|
"syscall"
|
|
|
|
|
2015-02-17 05:09:00 +08:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-01-13 05:54:00 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-10-31 06:08:28 +08:00
|
|
|
)
|
2014-10-23 04:45:23 +08:00
|
|
|
|
|
|
|
type linuxContainer struct {
|
2014-10-23 07:53:28 +08:00
|
|
|
id string
|
|
|
|
root string
|
2014-12-17 17:12:23 +08:00
|
|
|
config *configs.Config
|
2015-01-13 05:54:00 +08:00
|
|
|
cgroupManager cgroups.Manager
|
2015-02-25 09:53:11 +08:00
|
|
|
initPath string
|
2014-12-15 23:05:11 +08:00
|
|
|
initArgs []string
|
2015-02-07 13:12:27 +08:00
|
|
|
initProcess parentProcess
|
2015-02-14 06:41:37 +08:00
|
|
|
m sync.Mutex
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
// ID returns the container's unique ID
|
2014-10-23 04:45:23 +08:00
|
|
|
func (c *linuxContainer) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
// Config returns the container's configuration
|
|
|
|
func (c *linuxContainer) Config() configs.Config {
|
|
|
|
return *c.config
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
func (c *linuxContainer) Status() (Status, error) {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
return c.currentStatus()
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:45:07 +08:00
|
|
|
func (c *linuxContainer) State() (*State, error) {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
return c.currentState()
|
2015-02-12 06:45:07 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (c *linuxContainer) Processes() ([]int, error) {
|
2014-12-06 09:02:49 +08:00
|
|
|
pids, err := c.cgroupManager.GetPids()
|
2014-10-23 04:45:23 +08:00
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func (c *linuxContainer) Stats() (*Stats, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
var (
|
|
|
|
err error
|
2015-02-01 11:56:27 +08:00
|
|
|
stats = &Stats{}
|
2014-10-23 04:45:23 +08:00
|
|
|
)
|
2014-12-06 09:02:49 +08:00
|
|
|
if stats.CgroupStats, err = c.cgroupManager.GetStats(); err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return stats, newSystemError(err)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
for _, iface := range c.config.Networks {
|
2015-02-10 07:16:27 +08:00
|
|
|
switch iface.Type {
|
|
|
|
case "veth":
|
2015-02-11 03:51:45 +08:00
|
|
|
istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
|
2015-02-10 07:16:27 +08:00
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return stats, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-10 07:16:27 +08:00
|
|
|
stats.Interfaces = append(stats.Interfaces, istats)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
2014-10-28 08:51:14 +08:00
|
|
|
|
2015-03-11 16:46:54 +08:00
|
|
|
func (c *linuxContainer) Set(config configs.Config) error {
|
2015-02-27 12:09:42 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-03-11 16:46:54 +08:00
|
|
|
c.config = &config
|
2015-02-27 12:09:42 +08:00
|
|
|
return c.cgroupManager.Set(c.config)
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
func (c *linuxContainer) Start(process *Process) error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
status, err := c.currentStatus()
|
2014-12-15 23:05:11 +08:00
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
return err
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
doInit := status == Destroyed
|
2015-02-07 13:12:27 +08:00
|
|
|
parent, err := c.newParentProcess(process, doInit)
|
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
if err := parent.start(); err != nil {
|
|
|
|
// terminate the process to ensure that it properly is reaped.
|
|
|
|
if err := parent.terminate(); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
log.Warn(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
process.ops = parent
|
2015-02-07 13:12:27 +08:00
|
|
|
if doInit {
|
2015-02-14 06:41:37 +08:00
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
c.updateState(parent)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
return nil
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProcess, error) {
|
|
|
|
parentPipe, childPipe, err := newPipe()
|
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
cmd, err := c.commandTemplate(p, childPipe)
|
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if !doInit {
|
|
|
|
return c.newSetnsProcess(p, cmd, parentPipe, childPipe), nil
|
|
|
|
}
|
2015-02-13 04:58:40 +08:00
|
|
|
return c.newInitProcess(p, cmd, parentPipe, childPipe)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) commandTemplate(p *Process, childPipe *os.File) (*exec.Cmd, error) {
|
2015-02-25 09:53:11 +08:00
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: c.initPath,
|
|
|
|
Args: c.initArgs,
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
cmd.Stdin = p.Stdin
|
|
|
|
cmd.Stdout = p.Stdout
|
|
|
|
cmd.Stderr = p.Stderr
|
2015-02-04 09:44:58 +08:00
|
|
|
cmd.Dir = c.config.Rootfs
|
2014-12-23 06:06:22 +08:00
|
|
|
if cmd.SysProcAttr == nil {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
cmd.ExtraFiles = []*os.File{childPipe}
|
2015-04-03 04:55:55 +08:00
|
|
|
// NOTE: when running a container with no PID namespace and the parent process spawning the container is
|
|
|
|
// PID1 the pdeathsig is being delivered to the container's init process by the kernel for some reason
|
|
|
|
// even with the parent still running.
|
2015-02-12 08:45:23 +08:00
|
|
|
if c.config.ParentDeathSignal > 0 {
|
|
|
|
cmd.SysProcAttr.Pdeathsig = syscall.Signal(c.config.ParentDeathSignal)
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
return cmd, nil
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 04:58:40 +08:00
|
|
|
func (c *linuxContainer) newInitProcess(p *Process, cmd *exec.Cmd, parentPipe, childPipe *os.File) (*initProcess, error) {
|
2015-02-11 03:51:45 +08:00
|
|
|
t := "_LIBCONTAINER_INITTYPE=standard"
|
2015-02-07 13:12:27 +08:00
|
|
|
cloneFlags := c.config.Namespaces.CloneFlags()
|
|
|
|
if cloneFlags&syscall.CLONE_NEWUSER != 0 {
|
2015-02-13 04:58:40 +08:00
|
|
|
if err := c.addUidGidMappings(cmd.SysProcAttr); err != nil {
|
2015-02-14 08:06:17 +08:00
|
|
|
// user mappings are not supported
|
2015-02-13 04:58:40 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
// Default to root user when user namespaces are enabled.
|
|
|
|
if cmd.SysProcAttr.Credential == nil {
|
|
|
|
cmd.SysProcAttr.Credential = &syscall.Credential{}
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
cmd.Env = append(cmd.Env, t)
|
2015-02-07 13:12:27 +08:00
|
|
|
cmd.SysProcAttr.Cloneflags = cloneFlags
|
|
|
|
return &initProcess{
|
|
|
|
cmd: cmd,
|
|
|
|
childPipe: childPipe,
|
|
|
|
parentPipe: parentPipe,
|
|
|
|
manager: c.cgroupManager,
|
|
|
|
config: c.newInitConfig(p),
|
2015-02-13 04:58:40 +08:00
|
|
|
}, nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) newSetnsProcess(p *Process, cmd *exec.Cmd, parentPipe, childPipe *os.File) *setnsProcess {
|
|
|
|
cmd.Env = append(cmd.Env,
|
|
|
|
fmt.Sprintf("_LIBCONTAINER_INITPID=%d", c.initProcess.pid()),
|
|
|
|
"_LIBCONTAINER_INITTYPE=setns",
|
|
|
|
)
|
2015-03-05 08:04:20 +08:00
|
|
|
|
|
|
|
if p.consolePath != "" {
|
|
|
|
cmd.Env = append(cmd.Env, "_LIBCONTAINER_CONSOLE_PATH="+p.consolePath)
|
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
// TODO: set on container for process management
|
|
|
|
return &setnsProcess{
|
|
|
|
cmd: cmd,
|
|
|
|
cgroupPaths: c.cgroupManager.GetPaths(),
|
|
|
|
childPipe: childPipe,
|
|
|
|
parentPipe: parentPipe,
|
|
|
|
config: c.newInitConfig(p),
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) newInitConfig(process *Process) *initConfig {
|
|
|
|
return &initConfig{
|
2015-03-26 03:40:32 +08:00
|
|
|
Config: c.config,
|
|
|
|
Args: process.Args,
|
|
|
|
Env: process.Env,
|
|
|
|
User: process.User,
|
|
|
|
Cwd: process.Cwd,
|
|
|
|
Console: process.consolePath,
|
|
|
|
Capabilities: process.Capabilities,
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func newPipe() (parent *os.File, child *os.File, err error) {
|
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
2015-02-01 13:21:06 +08:00
|
|
|
if err != nil {
|
2015-02-07 13:12:27 +08:00
|
|
|
return nil, nil, err
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Destroy() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
status, err := c.currentStatus()
|
2014-12-15 23:00:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
if status != Destroyed {
|
2015-02-27 16:47:57 +08:00
|
|
|
return newGenericError(fmt.Errorf("container is not destroyed"), ContainerNotStopped)
|
2014-12-15 23:00:04 +08:00
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
if !c.config.Namespaces.Contains(configs.NEWPID) {
|
|
|
|
if err := killCgroupProcesses(c.cgroupManager); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
log.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = c.cgroupManager.Destroy()
|
|
|
|
if rerr := os.RemoveAll(c.root); err == nil {
|
|
|
|
err = rerr
|
|
|
|
}
|
2015-02-12 09:42:58 +08:00
|
|
|
c.initProcess = nil
|
2015-02-12 08:45:23 +08:00
|
|
|
return err
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Pause() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-02-01 11:56:27 +08:00
|
|
|
return c.cgroupManager.Freeze(configs.Frozen)
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Resume() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-02-01 11:56:27 +08:00
|
|
|
return c.cgroupManager.Freeze(configs.Thawed)
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 07:09:54 +08:00
|
|
|
func (c *linuxContainer) NotifyOOM() (<-chan struct{}, error) {
|
2015-02-12 09:12:03 +08:00
|
|
|
return notifyOnOOM(c.cgroupManager.GetPaths())
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
|
|
|
|
func (c *linuxContainer) updateState(process parentProcess) error {
|
|
|
|
c.initProcess = process
|
2015-02-14 06:41:37 +08:00
|
|
|
state, err := c.currentState()
|
2015-02-12 08:45:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(filepath.Join(c.root, stateFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return json.NewEncoder(f).Encode(state)
|
|
|
|
}
|
2015-02-14 06:41:37 +08:00
|
|
|
|
|
|
|
func (c *linuxContainer) currentStatus() (Status, error) {
|
|
|
|
if c.initProcess == nil {
|
|
|
|
return Destroyed, nil
|
|
|
|
}
|
|
|
|
// return Running if the init process is alive
|
|
|
|
if err := syscall.Kill(c.initProcess.pid(), 0); err != nil {
|
|
|
|
if err == syscall.ESRCH {
|
|
|
|
return Destroyed, nil
|
|
|
|
}
|
|
|
|
return 0, newSystemError(err)
|
|
|
|
}
|
|
|
|
if c.config.Cgroups != nil && c.config.Cgroups.Freezer == configs.Frozen {
|
|
|
|
return Paused, nil
|
|
|
|
}
|
|
|
|
return Running, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) currentState() (*State, error) {
|
|
|
|
status, err := c.currentStatus()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if status == Destroyed {
|
|
|
|
return nil, newGenericError(fmt.Errorf("container destroyed"), ContainerNotExists)
|
|
|
|
}
|
|
|
|
startTime, err := c.initProcess.startTime()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newSystemError(err)
|
|
|
|
}
|
|
|
|
state := &State{
|
|
|
|
ID: c.ID(),
|
|
|
|
Config: *c.config,
|
|
|
|
InitProcessPid: c.initProcess.pid(),
|
|
|
|
InitProcessStartTime: startTime,
|
|
|
|
CgroupPaths: c.cgroupManager.GetPaths(),
|
2015-02-25 03:54:58 +08:00
|
|
|
NamespacePaths: make(map[configs.NamespaceType]string),
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
|
|
|
for _, ns := range c.config.Namespaces {
|
2015-02-25 03:54:58 +08:00
|
|
|
state.NamespacePaths[ns.Type] = ns.GetPath(c.initProcess.pid())
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|