2018-02-05 16:05:59 -05:00
|
|
|
package exec // import "github.com/docker/docker/daemon/exec"
|
2015-11-20 17:35:16 -05:00
|
|
|
|
|
|
|
import (
|
2019-06-20 16:21:42 -04:00
|
|
|
"context"
|
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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds the configurations for execs. The Daemon keeps
|
|
|
|
// track of both running and finished execs so that they can be
|
|
|
|
// examined both during and after completion.
|
|
|
|
type Config struct {
|
|
|
|
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
|
|
|
|
ContainerID string
|
|
|
|
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
|
|
|
|
Pid int
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig initializes the a new exec configuration
|
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
2019-06-07 06:21:18 -04:00
|
|
|
ID: stringid.GenerateRandomID(),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
type rio struct {
|
|
|
|
cio.IO
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
sc *stream.Config
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
func (i *rio) Close() error {
|
2017-09-22 09:52:41 -04:00
|
|
|
i.IO.Close()
|
|
|
|
|
|
|
|
return i.sc.CloseStreams()
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
func (i *rio) Wait() {
|
2019-06-20 16:21:42 -04:00
|
|
|
i.sc.Wait(context.Background())
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
i.IO.Wait()
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
// InitializeStdio is called by libcontainerd to connect the stdio.
|
2017-12-07 14:26:27 -05:00
|
|
|
func (c *Config) 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
|
|
|
|
func (c *Config) CloseStreams() error {
|
|
|
|
return c.StreamConfig.CloseStreams()
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// SetExitCode sets the exec config's exit code
|
|
|
|
func (c *Config) SetExitCode(code int) {
|
|
|
|
c.ExitCode = &code
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:35:16 -05:00
|
|
|
// Store keeps track of the exec configurations.
|
|
|
|
type Store struct {
|
2017-11-27 10:53:16 -05:00
|
|
|
byID map[string]*Config
|
2015-11-20 17:35:16 -05:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStore initializes a new exec store.
|
|
|
|
func NewStore() *Store {
|
2017-09-22 09:52:41 -04:00
|
|
|
return &Store{
|
2017-11-27 10:53:16 -05:00
|
|
|
byID: make(map[string]*Config),
|
2017-09-22 09:52:41 -04:00
|
|
|
}
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commands returns the exec configurations in the store.
|
|
|
|
func (e *Store) Commands() map[string]*Config {
|
2016-01-19 07:30:48 -05:00
|
|
|
e.RLock()
|
2017-09-22 09:52:41 -04:00
|
|
|
byID := make(map[string]*Config, len(e.byID))
|
|
|
|
for id, config := range e.byID {
|
|
|
|
byID[id] = config
|
2016-01-19 07:30:48 -05:00
|
|
|
}
|
|
|
|
e.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.
|
|
|
|
func (e *Store) Add(id string, Config *Config) {
|
|
|
|
e.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
e.byID[id] = Config
|
2015-11-20 17:35:16 -05:00
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an exec configuration by its id.
|
|
|
|
func (e *Store) Get(id string) *Config {
|
|
|
|
e.RLock()
|
2017-09-22 09:52:41 -04:00
|
|
|
res := e.byID[id]
|
|
|
|
e.RUnlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:35:16 -05:00
|
|
|
// Delete removes an exec configuration from the store.
|
2017-09-22 09:52:41 -04:00
|
|
|
func (e *Store) Delete(id string, pid int) {
|
2015-11-20 17:35:16 -05:00
|
|
|
e.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
delete(e.byID, id)
|
2015-11-20 17:35:16 -05:00
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns the list of exec ids in the store.
|
|
|
|
func (e *Store) List() []string {
|
|
|
|
var IDs []string
|
|
|
|
e.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)
|
|
|
|
}
|
|
|
|
e.RUnlock()
|
|
|
|
return IDs
|
|
|
|
}
|