exec.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  28. // NewConfig initializes the a new exec configuration
  29. func NewConfig() *Config {
  30. return &Config{
  31. ID: stringid.GenerateNonCryptoID(),
  32. StreamConfig: runconfig.NewStreamConfig(),
  33. }
  34. }
  35. // Store keeps track of the exec configurations.
  36. type Store struct {
  37. commands map[string]*Config
  38. sync.RWMutex
  39. }
  40. // NewStore initializes a new exec store.
  41. func NewStore() *Store {
  42. return &Store{commands: make(map[string]*Config, 0)}
  43. }
  44. // Commands returns the exec configurations in the store.
  45. func (e *Store) Commands() map[string]*Config {
  46. e.RLock()
  47. commands := make(map[string]*Config, len(e.commands))
  48. for id, config := range e.commands {
  49. commands[id] = config
  50. }
  51. e.RUnlock()
  52. return commands
  53. }
  54. // Add adds a new exec configuration to the store.
  55. func (e *Store) Add(id string, Config *Config) {
  56. e.Lock()
  57. e.commands[id] = Config
  58. e.Unlock()
  59. }
  60. // Get returns an exec configuration by its id.
  61. func (e *Store) Get(id string) *Config {
  62. e.RLock()
  63. res := e.commands[id]
  64. e.RUnlock()
  65. return res
  66. }
  67. // Delete removes an exec configuration from the store.
  68. func (e *Store) Delete(id string) {
  69. e.Lock()
  70. delete(e.commands, id)
  71. e.Unlock()
  72. }
  73. // List returns the list of exec ids in the store.
  74. func (e *Store) List() []string {
  75. var IDs []string
  76. e.RLock()
  77. for id := range e.commands {
  78. IDs = append(IDs, id)
  79. }
  80. e.RUnlock()
  81. return IDs
  82. }