exec.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package exec
  2. import (
  3. "sync"
  4. "github.com/docker/docker/pkg/stringid"
  5. "github.com/docker/docker/runconfig"
  6. )
  7. // Config holds the configurations for execs. The Daemon keeps
  8. // track of both running and finished execs so that they can be
  9. // examined both during and after completion.
  10. type Config struct {
  11. sync.Mutex
  12. *runconfig.StreamConfig
  13. ID string
  14. Running bool
  15. ExitCode *int
  16. OpenStdin bool
  17. OpenStderr bool
  18. OpenStdout bool
  19. CanRemove bool
  20. ContainerID string
  21. DetachKeys []byte
  22. Entrypoint string
  23. Args []string
  24. Tty bool
  25. Privileged bool
  26. User string
  27. Env []string
  28. }
  29. // NewConfig initializes the a new exec configuration
  30. func NewConfig() *Config {
  31. return &Config{
  32. ID: stringid.GenerateNonCryptoID(),
  33. StreamConfig: runconfig.NewStreamConfig(),
  34. }
  35. }
  36. // Store keeps track of the exec configurations.
  37. type Store struct {
  38. commands map[string]*Config
  39. sync.RWMutex
  40. }
  41. // NewStore initializes a new exec store.
  42. func NewStore() *Store {
  43. return &Store{commands: make(map[string]*Config, 0)}
  44. }
  45. // Commands returns the exec configurations in the store.
  46. func (e *Store) Commands() map[string]*Config {
  47. e.RLock()
  48. commands := make(map[string]*Config, len(e.commands))
  49. for id, config := range e.commands {
  50. commands[id] = config
  51. }
  52. e.RUnlock()
  53. return commands
  54. }
  55. // Add adds a new exec configuration to the store.
  56. func (e *Store) Add(id string, Config *Config) {
  57. e.Lock()
  58. e.commands[id] = Config
  59. e.Unlock()
  60. }
  61. // Get returns an exec configuration by its id.
  62. func (e *Store) Get(id string) *Config {
  63. e.RLock()
  64. res := e.commands[id]
  65. e.RUnlock()
  66. return res
  67. }
  68. // Delete removes an exec configuration from the store.
  69. func (e *Store) Delete(id string) {
  70. e.Lock()
  71. delete(e.commands, id)
  72. e.Unlock()
  73. }
  74. // List returns the list of exec ids in the store.
  75. func (e *Store) List() []string {
  76. var IDs []string
  77. e.RLock()
  78. for id := range e.commands {
  79. IDs = append(IDs, id)
  80. }
  81. e.RUnlock()
  82. return IDs
  83. }