2022-05-10 15:59:00 -04:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2015-11-20 17:35:16 -05:00
|
|
|
|
|
|
|
import (
|
2016-10-17 17:39:52 -04:00
|
|
|
"runtime"
|
2015-11-20 17:35:16 -05:00
|
|
|
"sync"
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
"github.com/containerd/containerd/cio"
|
2016-11-14 15:15:09 -05:00
|
|
|
"github.com/docker/docker/container/stream"
|
2022-05-10 15:59:00 -04:00
|
|
|
"github.com/docker/docker/libcontainerd/types"
|
2015-11-20 17:35:16 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-20 17:35:16 -05:00
|
|
|
)
|
|
|
|
|
2022-05-10 15:59:00 -04:00
|
|
|
// ExecConfig holds the configurations for execs. The Daemon keeps
|
2015-11-20 17:35:16 -05:00
|
|
|
// track of both running and finished execs so that they can be
|
|
|
|
// examined both during and after completion.
|
2022-05-10 15:59:00 -04:00
|
|
|
type ExecConfig struct {
|
2015-11-20 17:35:16 -05:00
|
|
|
sync.Mutex
|
2018-06-07 23:07:48 -04:00
|
|
|
Started chan struct{}
|
2016-11-14 15:15:09 -05:00
|
|
|
StreamConfig *stream.Config
|
|
|
|
ID string
|
|
|
|
Running bool
|
|
|
|
ExitCode *int
|
|
|
|
OpenStdin bool
|
|
|
|
OpenStderr bool
|
|
|
|
OpenStdout bool
|
|
|
|
CanRemove bool
|
2022-05-10 15:59:00 -04:00
|
|
|
Container *Container
|
2016-11-14 15:15:09 -05:00
|
|
|
DetachKeys []byte
|
|
|
|
Entrypoint string
|
|
|
|
Args []string
|
|
|
|
Tty bool
|
|
|
|
Privileged bool
|
|
|
|
User string
|
2017-12-01 03:06:07 -05:00
|
|
|
WorkingDir string
|
2016-11-14 15:15:09 -05:00
|
|
|
Env []string
|
2022-05-10 15:59:00 -04:00
|
|
|
Process types.Process
|
2022-06-15 03:28:20 -04:00
|
|
|
ConsoleSize *[2]uint
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:59:00 -04:00
|
|
|
// NewExecConfig initializes the a new exec configuration
|
|
|
|
func NewExecConfig(c *Container) *ExecConfig {
|
|
|
|
return &ExecConfig{
|
2019-06-07 06:21:18 -04:00
|
|
|
ID: stringid.GenerateRandomID(),
|
2022-05-10 15:59:00 -04:00
|
|
|
Container: c,
|
2016-11-14 15:15:09 -05:00
|
|
|
StreamConfig: stream.NewConfig(),
|
2018-06-07 23:07:48 -04:00
|
|
|
Started: make(chan struct{}),
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
// InitializeStdio is called by libcontainerd to connect the stdio.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (c *ExecConfig) InitializeStdio(iop *cio.DirectIO) (cio.IO, error) {
|
2016-10-17 17:39:52 -04:00
|
|
|
c.StreamConfig.CopyToPipe(iop)
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if c.StreamConfig.Stdin() == nil && !c.Tty && runtime.GOOS == "windows" {
|
2016-10-17 17:39:52 -04:00
|
|
|
if iop.Stdin != nil {
|
|
|
|
if err := iop.Stdin.Close(); err != nil {
|
2016-10-27 18:29:24 -04:00
|
|
|
logrus.Errorf("error closing exec stdin: %+v", err)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
return &rio{IO: iop, sc: c.StreamConfig}, nil
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
// CloseStreams closes the stdio streams for the exec
|
2022-05-10 15:59:00 -04:00
|
|
|
func (c *ExecConfig) CloseStreams() error {
|
2016-11-14 15:15:09 -05:00
|
|
|
return c.StreamConfig.CloseStreams()
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// SetExitCode sets the exec config's exit code
|
2022-05-10 15:59:00 -04:00
|
|
|
func (c *ExecConfig) SetExitCode(code int) {
|
2017-07-19 10:20:13 -04:00
|
|
|
c.ExitCode = &code
|
|
|
|
}
|
|
|
|
|
2022-05-10 15:59:00 -04:00
|
|
|
// ExecStore keeps track of the exec configurations.
|
|
|
|
type ExecStore struct {
|
|
|
|
byID map[string]*ExecConfig
|
2022-04-21 15:33:23 -04:00
|
|
|
mu sync.RWMutex
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:59:00 -04:00
|
|
|
// NewExecStore initializes a new exec store.
|
|
|
|
func NewExecStore() *ExecStore {
|
|
|
|
return &ExecStore{
|
|
|
|
byID: make(map[string]*ExecConfig),
|
2017-09-22 09:52:41 -04:00
|
|
|
}
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commands returns the exec configurations in the store.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (e *ExecStore) Commands() map[string]*ExecConfig {
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RLock()
|
2022-05-10 15:59:00 -04:00
|
|
|
byID := make(map[string]*ExecConfig, len(e.byID))
|
2017-09-22 09:52:41 -04:00
|
|
|
for id, config := range e.byID {
|
|
|
|
byID[id] = config
|
2016-01-19 07:30:48 -05:00
|
|
|
}
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RUnlock()
|
2017-09-22 09:52:41 -04:00
|
|
|
return byID
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a new exec configuration to the store.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (e *ExecStore) Add(id string, Config *ExecConfig) {
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
e.byID[id] = Config
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.Unlock()
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an exec configuration by its id.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (e *ExecStore) Get(id string) *ExecConfig {
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RLock()
|
2017-09-22 09:52:41 -04:00
|
|
|
res := e.byID[id]
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RUnlock()
|
2017-09-22 09:52:41 -04:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:35:16 -05:00
|
|
|
// Delete removes an exec configuration from the store.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (e *ExecStore) Delete(id string) {
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
delete(e.byID, id)
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.Unlock()
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns the list of exec ids in the store.
|
2022-05-10 15:59:00 -04:00
|
|
|
func (e *ExecStore) List() []string {
|
2015-11-20 17:35:16 -05:00
|
|
|
var IDs []string
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RLock()
|
2017-09-22 09:52:41 -04:00
|
|
|
for id := range e.byID {
|
2015-11-20 17:35:16 -05:00
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
2022-04-21 15:33:23 -04:00
|
|
|
e.mu.RUnlock()
|
2015-11-20 17:35:16 -05:00
|
|
|
return IDs
|
|
|
|
}
|