Fix race in native driver on activeContainers usage

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
This commit is contained in:
Alexandr Morozov 2014-05-29 16:40:42 +04:00
Родитель 4e5f6951f2
Коммит 64bd6a6a53
2 изменённых файлов: 12 добавлений и 0 удалений

Просмотреть файл

@ -47,9 +47,11 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
return nil, err return nil, err
} }
cmds := make(map[string]*exec.Cmd) cmds := make(map[string]*exec.Cmd)
d.Lock()
for k, v := range d.activeContainers { for k, v := range d.activeContainers {
cmds[k] = v.cmd cmds[k] = v.cmd
} }
d.Unlock()
if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil { if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil {
return nil, err return nil, err
} }
@ -86,7 +88,9 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.
} }
if c.Network.ContainerID != "" { if c.Network.ContainerID != "" {
d.Lock()
active := d.activeContainers[c.Network.ContainerID] active := d.activeContainers[c.Network.ContainerID]
d.Unlock()
if active == nil || active.cmd.Process == nil { if active == nil || active.cmd.Process == nil {
return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID) return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
} }

Просмотреть файл

@ -8,6 +8,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/dotcloud/docker/daemon/execdriver" "github.com/dotcloud/docker/daemon/execdriver"
@ -62,6 +63,7 @@ type driver struct {
root string root string
initPath string initPath string
activeContainers map[string]*activeContainer activeContainers map[string]*activeContainer
sync.Mutex
} }
func NewDriver(root, initPath string) (*driver, error) { 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 { if err != nil {
return -1, err return -1, err
} }
d.Lock()
d.activeContainers[c.ID] = &activeContainer{ d.activeContainers[c.ID] = &activeContainer{
container: container, container: container,
cmd: &c.Cmd, cmd: &c.Cmd,
} }
d.Unlock()
var ( var (
dataPath = filepath.Join(d.root, c.ID) dataPath = filepath.Join(d.root, c.ID)
@ -186,7 +190,9 @@ func (d *driver) Name() string {
} }
func (d *driver) GetPidsForContainer(id string) ([]int, error) { func (d *driver) GetPidsForContainer(id string) ([]int, error) {
d.Lock()
active := d.activeContainers[id] active := d.activeContainers[id]
d.Unlock()
if active == nil { if active == nil {
return nil, fmt.Errorf("active container for %s does not exist", id) 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 { func (d *driver) removeContainerRoot(id string) error {
d.Lock()
delete(d.activeContainers, id) delete(d.activeContainers, id)
d.Unlock()
return os.RemoveAll(filepath.Join(d.root, id)) return os.RemoveAll(filepath.Join(d.root, id))
} }