浏览代码

Fix race in native driver on activeContainers usage

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
Alexandr Morozov 11 年之前
父节点
当前提交
64bd6a6a53
共有 2 个文件被更改,包括 12 次插入0 次删除
  1. 4 0
      daemon/execdriver/native/create.go
  2. 8 0
      daemon/execdriver/native/driver.go

+ 4 - 0
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)
 		}

+ 8 - 0
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))
 }