execdriver: Make GetPidsForContainer() a driver call
The current implementation is lxc specific. Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
parent
291f245f79
commit
455aa20483
1 changed files with 1 additions and 44 deletions
|
@ -5,10 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/pkg/mount"
|
"github.com/dotcloud/docker/pkg/mount"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,7 +30,7 @@ func FindCgroupMountpoint(subsystem string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the relative path to the cgroup docker is running in.
|
// Returns the relative path to the cgroup docker is running in.
|
||||||
func getThisCgroupDir(subsystem string) (string, error) {
|
func GetThisCgroupDir(subsystem string) (string, error) {
|
||||||
f, err := os.Open("/proc/self/cgroup")
|
f, err := os.Open("/proc/self/cgroup")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -58,43 +55,3 @@ func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a list of pids for the given container.
|
|
||||||
func GetPidsForContainer(id string) ([]int, error) {
|
|
||||||
pids := []int{}
|
|
||||||
|
|
||||||
// memory is chosen randomly, any cgroup used by docker works
|
|
||||||
subsystem := "memory"
|
|
||||||
|
|
||||||
cgroupRoot, err := FindCgroupMountpoint(subsystem)
|
|
||||||
if err != nil {
|
|
||||||
return pids, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cgroupDir, err := getThisCgroupDir(subsystem)
|
|
||||||
if err != nil {
|
|
||||||
return pids, err
|
|
||||||
}
|
|
||||||
|
|
||||||
filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
|
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
||||||
// With more recent lxc versions use, cgroup will be in lxc/
|
|
||||||
filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks")
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue