Merge pull request #5976 from crosbymichael/getpids

Move get pid into cgroup implementation
This commit is contained in:
Victor Vieux
2014-05-21 19:09:50 -07:00
7 changed files with 118 additions and 49 deletions

View File

@@ -3,6 +3,7 @@ package native
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/dotcloud/docker/daemon/execdriver"
@@ -45,7 +46,11 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
if err := d.setupLabels(container, c); err != nil {
return nil, err
}
if err := configuration.ParseConfiguration(container, d.activeContainers, c.Config["native"]); err != nil {
cmds := make(map[string]*exec.Cmd)
for k, v := range d.activeContainers {
cmds[k] = v.cmd
}
if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil {
return nil, err
}
return container, nil
@@ -81,10 +86,12 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.
}
if c.Network.ContainerID != "" {
cmd := d.activeContainers[c.Network.ContainerID]
if cmd == nil || cmd.Process == nil {
active := d.activeContainers[c.Network.ContainerID]
if active == nil || active.cmd.Process == nil {
return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
}
cmd := active.cmd
nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
container.Networks = append(container.Networks, &libcontainer.Network{
Type: "netns",

View File

@@ -7,14 +7,14 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/dotcloud/docker/daemon/execdriver"
"github.com/dotcloud/docker/pkg/apparmor"
"github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
"github.com/dotcloud/docker/pkg/libcontainer/cgroups/fs"
"github.com/dotcloud/docker/pkg/libcontainer/cgroups/systemd"
"github.com/dotcloud/docker/pkg/libcontainer/nsinit"
"github.com/dotcloud/docker/pkg/system"
)
@@ -53,24 +53,31 @@ func init() {
})
}
type activeContainer struct {
container *libcontainer.Container
cmd *exec.Cmd
}
type driver struct {
root string
initPath string
activeContainers map[string]*exec.Cmd
activeContainers map[string]*activeContainer
}
func NewDriver(root, initPath string) (*driver, error) {
if err := os.MkdirAll(root, 0700); err != nil {
return nil, err
}
// native driver root is at docker_root/execdriver/native. Put apparmor at docker_root
if err := apparmor.InstallDefaultProfile(filepath.Join(root, "../..", BackupApparmorProfilePath)); err != nil {
return nil, err
}
return &driver{
root: root,
initPath: initPath,
activeContainers: make(map[string]*exec.Cmd),
activeContainers: make(map[string]*activeContainer),
}, nil
}
@@ -80,7 +87,10 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
if err != nil {
return -1, err
}
d.activeContainers[c.ID] = &c.Cmd
d.activeContainers[c.ID] = &activeContainer{
container: container,
cmd: &c.Cmd,
}
var (
dataPath = filepath.Join(d.root, c.ID)
@@ -175,41 +185,18 @@ func (d *driver) Name() string {
return fmt.Sprintf("%s-%s", DriverName, Version)
}
// TODO: this can be improved with our driver
// there has to be a better way to do this
func (d *driver) GetPidsForContainer(id string) ([]int, error) {
pids := []int{}
active := d.activeContainers[id]
subsystem := "devices"
cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem)
if err != nil {
return pids, err
}
cgroupDir, err := cgroups.GetThisCgroupDir(subsystem)
if err != nil {
return pids, err
if active == nil {
return nil, fmt.Errorf("active container for %s does not exist", id)
}
c := active.container.Cgroups
filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
if _, err := os.Stat(filename); os.IsNotExist(err) {
filename = filepath.Join(cgroupRoot, cgroupDir, "docker", id, "tasks")
if systemd.UseSystemd() {
return systemd.GetPids(c)
}
output, err := ioutil.ReadFile(filename)
if err != nil {
return pids, err
}
for _, p := range strings.Split(string(output), "\n") {
if len(p) == 0 {
continue
}
pid, err := strconv.Atoi(p)
if err != nil {
return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
}
pids = append(pids, pid)
}
return pids, nil
return fs.GetPids(c)
}
func (d *driver) writeContainerFile(container *libcontainer.Container, id string) error {
@@ -225,6 +212,8 @@ func (d *driver) createContainerRoot(id string) error {
}
func (d *driver) removeContainerRoot(id string) error {
delete(d.activeContainers, id)
return os.RemoveAll(filepath.Join(d.root, id))
}