2014-10-23 04:45:23 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
2014-10-31 06:08:28 +08:00
|
|
|
import (
|
2014-12-15 23:05:11 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
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-12-19 17:40:03 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-10-31 06:08:28 +08:00
|
|
|
"github.com/docker/libcontainer/network"
|
2014-12-06 09:02:49 +08:00
|
|
|
"github.com/golang/glog"
|
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
|
2014-12-17 17:30:52 +08:00
|
|
|
state *configs.State
|
2015-01-13 05:54:00 +08:00
|
|
|
cgroupManager cgroups.Manager
|
2014-12-15 23:05:11 +08:00
|
|
|
initArgs []string
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
func (c *linuxContainer) Config() *configs.Config {
|
2014-10-23 04:45:23 +08:00
|
|
|
return c.config
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:30:52 +08:00
|
|
|
func (c *linuxContainer) RunState() (configs.RunState, error) {
|
2014-12-23 21:09:35 +08:00
|
|
|
if c.state.InitPid <= 0 {
|
|
|
|
return configs.Destroyed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// return Running if the init process is alive
|
|
|
|
err := syscall.Kill(c.state.InitPid, 0)
|
|
|
|
if err != nil {
|
2014-12-23 06:06:22 +08:00
|
|
|
if err == syscall.ESRCH {
|
2014-12-23 21:09:35 +08:00
|
|
|
return configs.Destroyed, nil
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME get a cgroup state to check other states
|
|
|
|
|
|
|
|
return configs.Running, nil
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (c *linuxContainer) Processes() ([]int, error) {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Info("fetch container processes")
|
2014-12-06 09:02:49 +08:00
|
|
|
pids, err := c.cgroupManager.GetPids()
|
2014-10-23 04:45:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (c *linuxContainer) Stats() (*ContainerStats, error) {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Info("fetch container stats")
|
2014-10-23 04:45:23 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
stats = &ContainerStats{}
|
|
|
|
)
|
|
|
|
|
2014-12-06 09:02:49 +08:00
|
|
|
if stats.CgroupStats, err = c.cgroupManager.GetStats(); err != nil {
|
2014-10-23 04:45:23 +08:00
|
|
|
return stats, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
if stats.NetworkStats, err = network.GetStats(&c.state.NetworkState); err != nil {
|
|
|
|
return stats, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
2014-10-28 08:51:14 +08:00
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
func (c *linuxContainer) StartProcess(config *ProcessConfig) (int, error) {
|
2014-12-15 23:05:11 +08:00
|
|
|
state, err := c.RunState()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
cmd := exec.Command(c.initArgs[0], c.initArgs[1:]...)
|
|
|
|
cmd.Stdin = config.Stdin
|
|
|
|
cmd.Stdout = config.Stdout
|
|
|
|
cmd.Stderr = config.Stderr
|
|
|
|
|
|
|
|
cmd.Env = config.Env
|
|
|
|
cmd.Dir = c.config.RootFs
|
|
|
|
|
|
|
|
if cmd.SysProcAttr == nil {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL
|
|
|
|
|
2014-12-17 17:30:52 +08:00
|
|
|
if state != configs.Destroyed {
|
2014-12-15 23:05:11 +08:00
|
|
|
glog.Info("start new container process")
|
2014-12-26 20:42:02 +08:00
|
|
|
return namespaces.ExecIn(config.Args, config.Env, config.Console, cmd, c.config, c.state)
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
if err := c.startInitProcess(cmd, config); err != nil {
|
2014-12-15 23:05:11 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.state.InitPid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) updateStateFile() error {
|
|
|
|
fnew := filepath.Join(c.root, fmt.Sprintf("%s.new", stateFilename))
|
|
|
|
f, err := os.Create(fnew)
|
|
|
|
if err != nil {
|
|
|
|
return newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
|
2014-12-17 23:05:39 +08:00
|
|
|
err = json.NewEncoder(f).Encode(c.state)
|
2014-12-15 23:05:11 +08:00
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2014-12-17 23:05:39 +08:00
|
|
|
os.Remove(fnew)
|
2014-12-15 23:05:11 +08:00
|
|
|
return newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
fname := filepath.Join(c.root, stateFilename)
|
|
|
|
if err := os.Rename(fnew, fname); err != nil {
|
|
|
|
return newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
func (c *linuxContainer) startInitProcess(cmd *exec.Cmd, config *ProcessConfig) error {
|
2014-12-26 20:42:02 +08:00
|
|
|
err := namespaces.Exec(config.Args, config.Env, config.Console, cmd, c.config, c.cgroupManager, c.state)
|
2014-12-19 17:40:03 +08:00
|
|
|
if err != nil {
|
2014-12-15 23:05:11 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:40:03 +08:00
|
|
|
err = c.updateStateFile()
|
2014-12-15 23:05:11 +08:00
|
|
|
if err != nil {
|
2014-12-19 17:40:03 +08:00
|
|
|
// FIXME c.Kill()
|
2014-12-15 23:05:11 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Destroy() error {
|
2014-12-15 23:00:04 +08:00
|
|
|
state, err := c.RunState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:30:52 +08:00
|
|
|
if state != configs.Destroyed {
|
2014-12-15 23:00:04 +08:00
|
|
|
return newGenericError(nil, ContainerNotStopped)
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:05:11 +08:00
|
|
|
os.RemoveAll(c.root)
|
|
|
|
return nil
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Pause() error {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Info("pause container")
|
2014-10-28 08:51:14 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Resume() error {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Info("resume container")
|
2014-10-28 08:51:14 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Signal(pid, signal int) error {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Infof("sending signal %d to pid %d", signal, pid)
|
2014-10-28 08:51:14 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Wait() (int, error) {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Info("wait container")
|
2014-10-28 08:51:14 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) WaitProcess(pid int) (int, error) {
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Infof("wait process %d", pid)
|
2014-10-28 08:51:14 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|