github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/daemon/exec/exec.go (about)

     1  package exec
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  
     7  	"github.com/Sirupsen/logrus"
     8  	"github.com/docker/docker/libcontainerd"
     9  	"github.com/docker/docker/pkg/stringid"
    10  	"github.com/docker/docker/runconfig"
    11  )
    12  
    13  // Config holds the configurations for execs. The Daemon keeps
    14  // track of both running and finished execs so that they can be
    15  // examined both during and after completion.
    16  type Config struct {
    17  	sync.Mutex
    18  	*runconfig.StreamConfig
    19  	ID          string
    20  	Running     bool
    21  	ExitCode    *int
    22  	OpenStdin   bool
    23  	OpenStderr  bool
    24  	OpenStdout  bool
    25  	CanRemove   bool
    26  	ContainerID string
    27  	DetachKeys  []byte
    28  	Entrypoint  string
    29  	Args        []string
    30  	Tty         bool
    31  	Privileged  bool
    32  	User        string
    33  	Env         []string
    34  	Pid         int
    35  }
    36  
    37  // NewConfig initializes the a new exec configuration
    38  func NewConfig() *Config {
    39  	return &Config{
    40  		ID:           stringid.GenerateNonCryptoID(),
    41  		StreamConfig: runconfig.NewStreamConfig(),
    42  	}
    43  }
    44  
    45  // InitializeStdio is called by libcontainerd to connect the stdio.
    46  func (c *Config) InitializeStdio(iop libcontainerd.IOPipe) error {
    47  	c.StreamConfig.CopyToPipe(iop)
    48  
    49  	if c.Stdin() == nil && !c.Tty && runtime.GOOS == "windows" {
    50  		if iop.Stdin != nil {
    51  			if err := iop.Stdin.Close(); err != nil {
    52  				logrus.Error("error closing exec stdin: %+v", err)
    53  			}
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  // Store keeps track of the exec configurations.
    61  type Store struct {
    62  	commands map[string]*Config
    63  	sync.RWMutex
    64  }
    65  
    66  // NewStore initializes a new exec store.
    67  func NewStore() *Store {
    68  	return &Store{commands: make(map[string]*Config, 0)}
    69  }
    70  
    71  // Commands returns the exec configurations in the store.
    72  func (e *Store) Commands() map[string]*Config {
    73  	e.RLock()
    74  	commands := make(map[string]*Config, len(e.commands))
    75  	for id, config := range e.commands {
    76  		commands[id] = config
    77  	}
    78  	e.RUnlock()
    79  	return commands
    80  }
    81  
    82  // Add adds a new exec configuration to the store.
    83  func (e *Store) Add(id string, Config *Config) {
    84  	e.Lock()
    85  	e.commands[id] = Config
    86  	e.Unlock()
    87  }
    88  
    89  // Get returns an exec configuration by its id.
    90  func (e *Store) Get(id string) *Config {
    91  	e.RLock()
    92  	res := e.commands[id]
    93  	e.RUnlock()
    94  	return res
    95  }
    96  
    97  // Delete removes an exec configuration from the store.
    98  func (e *Store) Delete(id string) {
    99  	e.Lock()
   100  	delete(e.commands, id)
   101  	e.Unlock()
   102  }
   103  
   104  // List returns the list of exec ids in the store.
   105  func (e *Store) List() []string {
   106  	var IDs []string
   107  	e.RLock()
   108  	for id := range e.commands {
   109  		IDs = append(IDs, id)
   110  	}
   111  	e.RUnlock()
   112  	return IDs
   113  }