github.com/alexandrev/docker@v1.9.0/daemon/execdriver/driver.go (about)

     1  package execdriver
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os/exec"
     7  	"time"
     8  
     9  	"github.com/docker/docker/pkg/idtools"
    10  	// TODO Windows: Factor out ulimit
    11  	"github.com/docker/docker/pkg/ulimit"
    12  	"github.com/opencontainers/runc/libcontainer"
    13  	"github.com/opencontainers/runc/libcontainer/configs"
    14  )
    15  
    16  // Context is a generic key value pair that allows
    17  // arbatrary data to be sent
    18  type Context map[string]string
    19  
    20  // Define error messages
    21  var (
    22  	ErrNotRunning              = errors.New("Container is not running")
    23  	ErrWaitTimeoutReached      = errors.New("Wait timeout reached")
    24  	ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
    25  	ErrDriverNotFound          = errors.New("The requested docker init has not been found")
    26  )
    27  
    28  // DriverCallback defines a callback function which is used in "Run" and "Exec".
    29  // This allows work to be done in the parent process when the child is passing
    30  // through PreStart, Start and PostStop events.
    31  // Callbacks are provided a processConfig pointer and the pid of the child.
    32  // The channel will be used to notify the OOM events.
    33  type DriverCallback func(processConfig *ProcessConfig, pid int, chOOM <-chan struct{}) error
    34  
    35  // Hooks is a struct containing function pointers to callbacks
    36  // used by any execdriver implementation exploiting hooks capabilities
    37  type Hooks struct {
    38  	// PreStart is called before container's CMD/ENTRYPOINT is executed
    39  	PreStart []DriverCallback
    40  	// Start is called after the container's process is full started
    41  	Start DriverCallback
    42  	// PostStop is called after the container process exits
    43  	PostStop []DriverCallback
    44  }
    45  
    46  // Info is driver specific information based on
    47  // processes registered with the driver
    48  type Info interface {
    49  	IsRunning() bool
    50  }
    51  
    52  // Terminal represents a pseudo TTY, it is for when
    53  // using a container interactively.
    54  type Terminal interface {
    55  	io.Closer
    56  	Resize(height, width int) error
    57  }
    58  
    59  // ExitStatus provides exit reasons for a container.
    60  type ExitStatus struct {
    61  	// The exit code with which the container exited.
    62  	ExitCode int
    63  
    64  	// Whether the container encountered an OOM.
    65  	OOMKilled bool
    66  }
    67  
    68  // Driver is an interface for drivers to implement
    69  // including all basic functions a driver should have
    70  type Driver interface {
    71  	// Run executes the process, blocks until the process exits and returns
    72  	// the exit code. It's the last stage on Docker side for running a container.
    73  	Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
    74  
    75  	// Exec executes the process in an existing container, blocks until the
    76  	// process exits and returns the exit code.
    77  	Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
    78  
    79  	// Kill sends signals to process in container.
    80  	Kill(c *Command, sig int) error
    81  
    82  	// Pause pauses a container.
    83  	Pause(c *Command) error
    84  
    85  	// Unpause unpauses a container.
    86  	Unpause(c *Command) error
    87  
    88  	// Name returns the name of the driver.
    89  	Name() string
    90  
    91  	// Info returns the configuration stored in the driver struct,
    92  	// "temporary" hack (until we move state from core to plugins).
    93  	Info(id string) Info
    94  
    95  	// GetPidsForContainer returns a list of pid for the processes running in a container.
    96  	GetPidsForContainer(id string) ([]int, error)
    97  
    98  	// Terminate kills a container by sending signal SIGKILL.
    99  	Terminate(c *Command) error
   100  
   101  	// Clean removes all traces of container exec.
   102  	Clean(id string) error
   103  
   104  	// Stats returns resource stats for a running container
   105  	Stats(id string) (*ResourceStats, error)
   106  
   107  	// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
   108  	SupportsHooks() bool
   109  }
   110  
   111  // Ipc settings of the container
   112  // It is for IPC namespace setting. Usually different containers
   113  // have their own IPC namespace, however this specifies to use
   114  // an existing IPC namespace.
   115  // You can join the host's or a container's IPC namespace.
   116  type Ipc struct {
   117  	ContainerID string `json:"container_id"` // id of the container to join ipc.
   118  	HostIpc     bool   `json:"host_ipc"`
   119  }
   120  
   121  // Pid settings of the container
   122  // It is for PID namespace setting. Usually different containers
   123  // have their own PID namespace, however this specifies to use
   124  // an existing PID namespace.
   125  // Joining the host's PID namespace is currently the only supported
   126  // option.
   127  type Pid struct {
   128  	HostPid bool `json:"host_pid"`
   129  }
   130  
   131  // UTS settings of the container
   132  // It is for UTS namespace setting. Usually different containers
   133  // have their own UTS namespace, however this specifies to use
   134  // an existing UTS namespace.
   135  // Joining the host's UTS namespace is currently the only supported
   136  // option.
   137  type UTS struct {
   138  	HostUTS bool `json:"host_uts"`
   139  }
   140  
   141  // Resources contains all resource configs for a driver.
   142  // Currently these are all for cgroup configs.
   143  // TODO Windows: Factor out ulimit.Rlimit
   144  type Resources struct {
   145  	Memory            int64            `json:"memory"`
   146  	MemorySwap        int64            `json:"memory_swap"`
   147  	MemoryReservation int64            `json:"memory_reservation"`
   148  	KernelMemory      int64            `json:"kernel_memory"`
   149  	CPUShares         int64            `json:"cpu_shares"`
   150  	CpusetCpus        string           `json:"cpuset_cpus"`
   151  	CpusetMems        string           `json:"cpuset_mems"`
   152  	CPUPeriod         int64            `json:"cpu_period"`
   153  	CPUQuota          int64            `json:"cpu_quota"`
   154  	BlkioWeight       uint16           `json:"blkio_weight"`
   155  	Rlimits           []*ulimit.Rlimit `json:"rlimits"`
   156  	OomKillDisable    bool             `json:"oom_kill_disable"`
   157  	MemorySwappiness  int64            `json:"memory_swappiness"`
   158  }
   159  
   160  // ResourceStats contains information about resource usage by a container.
   161  type ResourceStats struct {
   162  	*libcontainer.Stats
   163  	Read        time.Time `json:"read"`
   164  	MemoryLimit int64     `json:"memory_limit"`
   165  	SystemUsage uint64    `json:"system_usage"`
   166  }
   167  
   168  // Mount contains information for a mount operation.
   169  type Mount struct {
   170  	Source      string `json:"source"`
   171  	Destination string `json:"destination"`
   172  	Writable    bool   `json:"writable"`
   173  	Private     bool   `json:"private"`
   174  	Slave       bool   `json:"slave"`
   175  }
   176  
   177  // User contains the uid and gid representing a Unix user
   178  type User struct {
   179  	UID int `json:"root_uid"`
   180  	GID int `json:"root_gid"`
   181  }
   182  
   183  // ProcessConfig describes a process that will be run inside a container.
   184  type ProcessConfig struct {
   185  	exec.Cmd `json:"-"`
   186  
   187  	Privileged  bool     `json:"privileged"`
   188  	User        string   `json:"user"`
   189  	Tty         bool     `json:"tty"`
   190  	Entrypoint  string   `json:"entrypoint"`
   191  	Arguments   []string `json:"arguments"`
   192  	Terminal    Terminal `json:"-"` // standard or tty terminal
   193  	Console     string   `json:"-"` // dev/console path
   194  	ConsoleSize [2]int   `json:"-"` // h,w of initial console size
   195  }
   196  
   197  // Command wraps an os/exec.Cmd to add more metadata
   198  //
   199  // TODO Windows: Factor out unused fields such as LxcConfig, AppArmorProfile,
   200  // and CgroupParent.
   201  type Command struct {
   202  	ID                 string            `json:"id"`
   203  	Rootfs             string            `json:"rootfs"` // root fs of the container
   204  	ReadonlyRootfs     bool              `json:"readonly_rootfs"`
   205  	InitPath           string            `json:"initpath"` // dockerinit
   206  	WorkingDir         string            `json:"working_dir"`
   207  	ConfigPath         string            `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
   208  	Network            *Network          `json:"network"`
   209  	Ipc                *Ipc              `json:"ipc"`
   210  	Pid                *Pid              `json:"pid"`
   211  	UTS                *UTS              `json:"uts"`
   212  	RemappedRoot       *User             `json:"remap_root"`
   213  	UIDMapping         []idtools.IDMap   `json:"uidmapping"`
   214  	GIDMapping         []idtools.IDMap   `json:"gidmapping"`
   215  	Resources          *Resources        `json:"resources"`
   216  	Mounts             []Mount           `json:"mounts"`
   217  	AllowedDevices     []*configs.Device `json:"allowed_devices"`
   218  	AutoCreatedDevices []*configs.Device `json:"autocreated_devices"`
   219  	CapAdd             []string          `json:"cap_add"`
   220  	CapDrop            []string          `json:"cap_drop"`
   221  	GroupAdd           []string          `json:"group_add"`
   222  	ContainerPid       int               `json:"container_pid"`  // the pid for the process inside a container
   223  	ProcessConfig      ProcessConfig     `json:"process_config"` // Describes the init process of the container.
   224  	ProcessLabel       string            `json:"process_label"`
   225  	MountLabel         string            `json:"mount_label"`
   226  	LxcConfig          []string          `json:"lxc_config"`
   227  	AppArmorProfile    string            `json:"apparmor_profile"`
   228  	CgroupParent       string            `json:"cgroup_parent"` // The parent cgroup for this command.
   229  	FirstStart         bool              `json:"first_start"`
   230  	LayerPaths         []string          `json:"layer_paths"` // Windows needs to know the layer paths and folder for a command
   231  	LayerFolder        string            `json:"layer_folder"`
   232  	Hostname           string            `json:"hostname"` // Windows sets the hostname in the execdriver
   233  }