github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/daemon/execdriver/driver.go (about)

     1  package execdriver
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os/exec"
     7  	"time"
     8  
     9  	"github.com/opencontainers/runc/libcontainer"
    10  )
    11  
    12  // Context is a generic key value pair that allows
    13  // arbitrary data to be sent
    14  type Context map[string]string
    15  
    16  // Define error messages
    17  var (
    18  	ErrNotRunning              = errors.New("Container is not running")
    19  	ErrWaitTimeoutReached      = errors.New("Wait timeout reached")
    20  	ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
    21  	ErrDriverNotFound          = errors.New("The requested docker init has not been found")
    22  )
    23  
    24  // DriverCallback defines a callback function which is used in "Run" and "Exec".
    25  // This allows work to be done in the parent process when the child is passing
    26  // through PreStart, Start and PostStop events.
    27  // Callbacks are provided a processConfig pointer and the pid of the child.
    28  // The channel will be used to notify the OOM events.
    29  type DriverCallback func(processConfig *ProcessConfig, pid int, chOOM <-chan struct{}) error
    30  
    31  // Hooks is a struct containing function pointers to callbacks
    32  // used by any execdriver implementation exploiting hooks capabilities
    33  type Hooks struct {
    34  	// PreStart is called before container's CMD/ENTRYPOINT is executed
    35  	PreStart []DriverCallback
    36  	// Start is called after the container's process is full started
    37  	Start DriverCallback
    38  	// PostStop is called after the container process exits
    39  	PostStop []DriverCallback
    40  }
    41  
    42  // Info is driver specific information based on
    43  // processes registered with the driver
    44  type Info interface {
    45  	IsRunning() bool
    46  }
    47  
    48  // Terminal represents a pseudo TTY, it is for when
    49  // using a container interactively.
    50  type Terminal interface {
    51  	io.Closer
    52  	Resize(height, width int) error
    53  }
    54  
    55  // Driver is an interface for drivers to implement
    56  // including all basic functions a driver should have
    57  type Driver interface {
    58  	// Run executes the process, blocks until the process exits and returns
    59  	// the exit code. It's the last stage on Docker side for running a container.
    60  	Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
    61  
    62  	// Exec executes the process in an existing container, blocks until the
    63  	// process exits and returns the exit code.
    64  	Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
    65  
    66  	// Kill sends signals to process in container.
    67  	Kill(c *Command, sig int) error
    68  
    69  	// Pause pauses a container.
    70  	Pause(c *Command) error
    71  
    72  	// Unpause unpauses a container.
    73  	Unpause(c *Command) error
    74  
    75  	// Name returns the name of the driver.
    76  	Name() string
    77  
    78  	// Info returns the configuration stored in the driver struct,
    79  	// "temporary" hack (until we move state from core to plugins).
    80  	Info(id string) Info
    81  
    82  	// GetPidsForContainer returns a list of pid for the processes running in a container.
    83  	GetPidsForContainer(id string) ([]int, error)
    84  
    85  	// Terminate kills a container by sending signal SIGKILL.
    86  	Terminate(c *Command) error
    87  
    88  	// Clean removes all traces of container exec.
    89  	Clean(id string) error
    90  
    91  	// Stats returns resource stats for a running container
    92  	Stats(id string) (*ResourceStats, error)
    93  
    94  	// Update updates resource configs for a container
    95  	Update(c *Command) error
    96  
    97  	// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
    98  	SupportsHooks() bool
    99  }
   100  
   101  // CommonResources contains the resource configs for a driver that are
   102  // common across platforms.
   103  type CommonResources struct {
   104  	Memory            int64  `json:"memory"`
   105  	MemoryReservation int64  `json:"memory_reservation"`
   106  	CPUShares         int64  `json:"cpu_shares"`
   107  	BlkioWeight       uint16 `json:"blkio_weight"`
   108  }
   109  
   110  // ResourceStats contains information about resource usage by a container.
   111  type ResourceStats struct {
   112  	*libcontainer.Stats
   113  	Read        time.Time `json:"read"`
   114  	MemoryLimit int64     `json:"memory_limit"`
   115  	SystemUsage uint64    `json:"system_usage"`
   116  }
   117  
   118  // CommonProcessConfig is the common platform agnostic part of the ProcessConfig
   119  // structure that describes a process that will be run inside a container.
   120  type CommonProcessConfig struct {
   121  	exec.Cmd `json:"-"`
   122  
   123  	Tty        bool     `json:"tty"`
   124  	Entrypoint string   `json:"entrypoint"`
   125  	Arguments  []string `json:"arguments"`
   126  	Terminal   Terminal `json:"-"` // standard or tty terminal
   127  }
   128  
   129  // CommonCommand is the common platform agnostic part of the Command structure
   130  // which wraps an os/exec.Cmd to add more metadata
   131  type CommonCommand struct {
   132  	ContainerPid  int           `json:"container_pid"` // the pid for the process inside a container
   133  	ID            string        `json:"id"`
   134  	InitPath      string        `json:"initpath"`    // dockerinit
   135  	MountLabel    string        `json:"mount_label"` // TODO Windows. More involved, but can be factored out
   136  	Mounts        []Mount       `json:"mounts"`
   137  	Network       *Network      `json:"network"`
   138  	ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
   139  	ProcessLabel  string        `json:"process_label"`  // TODO Windows. More involved, but can be factored out
   140  	Resources     *Resources    `json:"resources"`
   141  	Rootfs        string        `json:"rootfs"` // root fs of the container
   142  	WorkingDir    string        `json:"working_dir"`
   143  	TmpDir        string        `json:"tmpdir"` // Directory used to store docker tmpdirs.
   144  }