github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/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  // Terminal represents a pseudo TTY, it is for when
    43  // using a container interactively.
    44  type Terminal interface {
    45  	io.Closer
    46  	Resize(height, width int) error
    47  }
    48  
    49  // Driver is an interface for drivers to implement
    50  // including all basic functions a driver should have
    51  type Driver interface {
    52  	// Run executes the process, blocks until the process exits and returns
    53  	// the exit code. It's the last stage on Docker side for running a container.
    54  	Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
    55  
    56  	// Exec executes the process in an existing container, blocks until the
    57  	// process exits and returns the exit code.
    58  	Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
    59  
    60  	// Kill sends signals to process in container.
    61  	Kill(c *Command, sig int) error
    62  
    63  	// Pause pauses a container.
    64  	Pause(c *Command) error
    65  
    66  	// Unpause unpauses a container.
    67  	Unpause(c *Command) error
    68  
    69  	// Name returns the name of the driver.
    70  	Name() string
    71  
    72  	// GetPidsForContainer returns a list of pid for the processes running in a container.
    73  	GetPidsForContainer(id string) ([]int, error)
    74  
    75  	// Terminate kills a container by sending signal SIGKILL.
    76  	Terminate(c *Command) error
    77  
    78  	// Clean removes all traces of container exec.
    79  	Clean(id string) error
    80  
    81  	// Stats returns resource stats for a running container
    82  	Stats(id string) (*ResourceStats, error)
    83  
    84  	// Update updates resource configs for a container
    85  	Update(c *Command) error
    86  
    87  	// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
    88  	SupportsHooks() bool
    89  }
    90  
    91  // CommonResources contains the resource configs for a driver that are
    92  // common across platforms.
    93  type CommonResources struct {
    94  	Memory            int64  `json:"memory"`
    95  	MemoryReservation int64  `json:"memory_reservation"`
    96  	CPUShares         int64  `json:"cpu_shares"`
    97  	BlkioWeight       uint16 `json:"blkio_weight"`
    98  }
    99  
   100  // ResourceStats contains information about resource usage by a container.
   101  type ResourceStats struct {
   102  	*libcontainer.Stats
   103  	Read        time.Time `json:"read"`
   104  	MemoryLimit int64     `json:"memory_limit"`
   105  	SystemUsage uint64    `json:"system_usage"`
   106  }
   107  
   108  // CommonProcessConfig is the common platform agnostic part of the ProcessConfig
   109  // structure that describes a process that will be run inside a container.
   110  type CommonProcessConfig struct {
   111  	exec.Cmd `json:"-"`
   112  
   113  	Tty        bool     `json:"tty"`
   114  	Entrypoint string   `json:"entrypoint"`
   115  	Arguments  []string `json:"arguments"`
   116  	Terminal   Terminal `json:"-"` // standard or tty terminal
   117  }
   118  
   119  // CommonCommand is the common platform agnostic part of the Command structure
   120  // which wraps an os/exec.Cmd to add more metadata
   121  type CommonCommand struct {
   122  	ContainerPid  int           `json:"container_pid"` // the pid for the process inside a container
   123  	ID            string        `json:"id"`
   124  	MountLabel    string        `json:"mount_label"` // TODO Windows. More involved, but can be factored out
   125  	Mounts        []Mount       `json:"mounts"`
   126  	Network       *Network      `json:"network"`
   127  	ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
   128  	ProcessLabel  string        `json:"process_label"`  // TODO Windows. More involved, but can be factored out
   129  	Resources     *Resources    `json:"resources"`
   130  	Rootfs        string        `json:"rootfs"` // root fs of the container
   131  	WorkingDir    string        `json:"working_dir"`
   132  	TmpDir        string        `json:"tmpdir"` // Directory used to store docker tmpdirs.
   133  }