2015-11-20 17:35:16 -05:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
2016-10-17 17:39:52 -04:00
|
|
|
"runtime"
|
2015-11-20 17:35:16 -05:00
|
|
|
"sync"
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
"github.com/containerd/containerd"
|
2016-11-14 15:15:09 -05:00
|
|
|
"github.com/docker/docker/container/stream"
|
2016-10-17 17:39:52 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
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
|
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
|
|
|
|
Env []string
|
|
|
|
Pid int
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig initializes the a new exec configuration
|
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
ID: stringid.GenerateNonCryptoID(),
|
2016-11-14 15:15:09 -05:00
|
|
|
StreamConfig: stream.NewConfig(),
|
2015-11-20 17:35:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
type cio struct {
|
|
|
|
containerd.IO
|
|
|
|
|
|
|
|
sc *stream.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *cio) Close() error {
|
|
|
|
i.IO.Close()
|
|
|
|
|
|
|
|
return i.sc.CloseStreams()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *cio) Wait() {
|
|
|
|
i.sc.Wait()
|
|
|
|
|
|
|
|
i.IO.Wait()
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
// InitializeStdio is called by libcontainerd to connect the stdio.
|
2017-09-22 09:52:41 -04:00
|
|
|
func (c *Config) InitializeStdio(iop *libcontainerd.IOPipe) (containerd.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-09-22 09:52:41 -04:00
|
|
|
return &cio{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-09-22 09:52:41 -04:00
|
|
|
byID map[string]*Config
|
|
|
|
byPid map[int]*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{
|
|
|
|
byID: make(map[string]*Config),
|
|
|
|
byPid: make(map[int]*Config),
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// SetPidUnlocked adds an association between a Pid and a config, it does not
|
|
|
|
// synchronized with other operations.
|
|
|
|
func (e *Store) SetPidUnlocked(id string, pid int) {
|
|
|
|
if config, ok := e.byID[id]; ok {
|
|
|
|
e.byPid[pid] = config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:35:16 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByPid returns an exec configuration by its pid.
|
|
|
|
func (e *Store) ByPid(pid int) *Config {
|
|
|
|
e.RLock()
|
|
|
|
res := e.byPid[pid]
|
2015-11-20 17:35:16 -05:00
|
|
|
e.RUnlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.byPid, pid)
|
|
|
|
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
|
|
|
|
}
|