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