github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/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  	// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
    95  	SupportsHooks() bool
    96  }
    97  
    98  // CommonResources contains the resource configs for a driver that are
    99  // common across platforms.
   100  type CommonResources struct {
   101  	Memory            int64  `json:"memory"`
   102  	MemoryReservation int64  `json:"memory_reservation"`
   103  	CPUShares         int64  `json:"cpu_shares"`
   104  	BlkioWeight       uint16 `json:"blkio_weight"`
   105  }
   106  
   107  // ResourceStats contains information about resource usage by a container.
   108  type ResourceStats struct {
   109  	*libcontainer.Stats
   110  	Read        time.Time `json:"read"`
   111  	MemoryLimit int64     `json:"memory_limit"`
   112  	SystemUsage uint64    `json:"system_usage"`
   113  }
   114  
   115  // CommonProcessConfig is the common platform agnostic part of the ProcessConfig
   116  // structure that describes a process that will be run inside a container.
   117  type CommonProcessConfig struct {
   118  	exec.Cmd `json:"-"`
   119  
   120  	Tty        bool     `json:"tty"`
   121  	Entrypoint string   `json:"entrypoint"`
   122  	Arguments  []string `json:"arguments"`
   123  	Terminal   Terminal `json:"-"` // standard or tty terminal
   124  }
   125  
   126  // CommonCommand is the common platform agnostic part of the Command structure
   127  // which wraps an os/exec.Cmd to add more metadata
   128  type CommonCommand struct {
   129  	ContainerPid  int           `json:"container_pid"` // the pid for the process inside a container
   130  	ID            string        `json:"id"`
   131  	InitPath      string        `json:"initpath"`    // dockerinit
   132  	MountLabel    string        `json:"mount_label"` // TODO Windows. More involved, but can be factored out
   133  	Mounts        []Mount       `json:"mounts"`
   134  	Network       *Network      `json:"network"`
   135  	ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
   136  	ProcessLabel  string        `json:"process_label"`  // TODO Windows. More involved, but can be factored out
   137  	Resources     *Resources    `json:"resources"`
   138  	Rootfs        string        `json:"rootfs"` // root fs of the container
   139  	WorkingDir    string        `json:"working_dir"`
   140  	TmpDir        string        `json:"tmpdir"` // Directory used to store docker tmpdirs.
   141  }