From 64bd6a6a5342c87db7096f60365d270d0d69e9d2 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Thu, 29 May 2014 16:40:42 +0400 Subject: [PATCH] Fix race in native driver on activeContainers usage Docker-DCO-1.1-Signed-off-by: Alexandr Morozov (github: LK4D4) --- daemon/execdriver/native/create.go | 4 ++++ daemon/execdriver/native/driver.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/daemon/execdriver/native/create.go b/daemon/execdriver/native/create.go index 3a7001db1a..f4c2a5bd6b 100644 --- a/daemon/execdriver/native/create.go +++ b/daemon/execdriver/native/create.go @@ -47,9 +47,11 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container return nil, err } cmds := make(map[string]*exec.Cmd) + d.Lock() for k, v := range d.activeContainers { cmds[k] = v.cmd } + d.Unlock() if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil { return nil, err } @@ -86,7 +88,9 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver. } if c.Network.ContainerID != "" { + d.Lock() active := d.activeContainers[c.Network.ContainerID] + d.Unlock() if active == nil || active.cmd.Process == nil { return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID) } diff --git a/daemon/execdriver/native/driver.go b/daemon/execdriver/native/driver.go index 425403fa4e..c84dbf7dc9 100644 --- a/daemon/execdriver/native/driver.go +++ b/daemon/execdriver/native/driver.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "syscall" "github.com/dotcloud/docker/daemon/execdriver" @@ -62,6 +63,7 @@ type driver struct { root string initPath string activeContainers map[string]*activeContainer + sync.Mutex } func NewDriver(root, initPath string) (*driver, error) { @@ -87,10 +89,12 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba if err != nil { return -1, err } + d.Lock() d.activeContainers[c.ID] = &activeContainer{ container: container, cmd: &c.Cmd, } + d.Unlock() var ( dataPath = filepath.Join(d.root, c.ID) @@ -186,7 +190,9 @@ func (d *driver) Name() string { } func (d *driver) GetPidsForContainer(id string) ([]int, error) { + d.Lock() active := d.activeContainers[id] + d.Unlock() if active == nil { return nil, fmt.Errorf("active container for %s does not exist", id) @@ -212,7 +218,9 @@ func (d *driver) createContainerRoot(id string) error { } func (d *driver) removeContainerRoot(id string) error { + d.Lock() delete(d.activeContainers, id) + d.Unlock() return os.RemoveAll(filepath.Join(d.root, id)) }