Browse code

Merge pull request #3808 from alexlarsson/execdriver-get-pids-for-container

execdriver: Make GetPidsForContainer() a driver call

Michael Crosby authored on 2014/01/30 03:38:10
Showing 5 changed files
... ...
@@ -95,3 +95,7 @@ func (d *driver) Info(id string) execdriver.Info {
95 95
 func (d *driver) Name() string {
96 96
 	return fmt.Sprintf("%s-%s", DriverName, Version)
97 97
 }
98
+
99
+func (d *driver) GetPidsForContainer(id string) ([]int, error) {
100
+	return nil, fmt.Errorf("Not supported")
101
+}
... ...
@@ -60,9 +60,10 @@ type Info interface {
60 60
 type Driver interface {
61 61
 	Run(c *Command, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code
62 62
 	Kill(c *Command, sig int) error
63
-	Restore(c *Command) error // Wait and try to re-attach on an out of process command
64
-	Name() string             // Driver name
65
-	Info(id string) Info      // "temporary" hack (until we move state from core to plugins)
63
+	Restore(c *Command) error                     // Wait and try to re-attach on an out of process command
64
+	Name() string                                 // Driver name
65
+	Info(id string) Info                          // "temporary" hack (until we move state from core to plugins)
66
+	GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
66 67
 }
67 68
 
68 69
 // Network settings of the container
... ...
@@ -3,12 +3,14 @@ package lxc
3 3
 import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/execdriver"
6
+	"github.com/dotcloud/docker/pkg/cgroups"
6 7
 	"github.com/dotcloud/docker/utils"
7 8
 	"io/ioutil"
8 9
 	"log"
9 10
 	"os"
10 11
 	"os/exec"
11 12
 	"path"
13
+	"path/filepath"
12 14
 	"strconv"
13 15
 	"strings"
14 16
 	"syscall"
... ...
@@ -281,6 +283,45 @@ func (d *driver) Info(id string) execdriver.Info {
281 281
 	}
282 282
 }
283 283
 
284
+func (d *driver) GetPidsForContainer(id string) ([]int, error) {
285
+	pids := []int{}
286
+
287
+	// memory is chosen randomly, any cgroup used by docker works
288
+	subsystem := "memory"
289
+
290
+	cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem)
291
+	if err != nil {
292
+		return pids, err
293
+	}
294
+
295
+	cgroupDir, err := cgroups.GetThisCgroupDir(subsystem)
296
+	if err != nil {
297
+		return pids, err
298
+	}
299
+
300
+	filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
301
+	if _, err := os.Stat(filename); os.IsNotExist(err) {
302
+		// With more recent lxc versions use, cgroup will be in lxc/
303
+		filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks")
304
+	}
305
+
306
+	output, err := ioutil.ReadFile(filename)
307
+	if err != nil {
308
+		return pids, err
309
+	}
310
+	for _, p := range strings.Split(string(output), "\n") {
311
+		if len(p) == 0 {
312
+			continue
313
+		}
314
+		pid, err := strconv.Atoi(p)
315
+		if err != nil {
316
+			return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
317
+		}
318
+		pids = append(pids, pid)
319
+	}
320
+	return pids, nil
321
+}
322
+
284 323
 func linkLxcStart(root string) error {
285 324
 	sourcePath, err := exec.LookPath("lxc-start")
286 325
 	if err != nil {
... ...
@@ -5,10 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker/pkg/mount"
7 7
 	"io"
8
-	"io/ioutil"
9 8
 	"os"
10
-	"path/filepath"
11
-	"strconv"
12 9
 	"strings"
13 10
 )
14 11
 
... ...
@@ -33,7 +30,7 @@ func FindCgroupMountpoint(subsystem string) (string, error) {
33 33
 }
34 34
 
35 35
 // Returns the relative path to the cgroup docker is running in.
36
-func getThisCgroupDir(subsystem string) (string, error) {
36
+func GetThisCgroupDir(subsystem string) (string, error) {
37 37
 	f, err := os.Open("/proc/self/cgroup")
38 38
 	if err != nil {
39 39
 		return "", err
... ...
@@ -58,43 +55,3 @@ func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
58 58
 	}
59 59
 	return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
60 60
 }
61
-
62
-// Returns a list of pids for the given container.
63
-func GetPidsForContainer(id string) ([]int, error) {
64
-	pids := []int{}
65
-
66
-	// memory is chosen randomly, any cgroup used by docker works
67
-	subsystem := "memory"
68
-
69
-	cgroupRoot, err := FindCgroupMountpoint(subsystem)
70
-	if err != nil {
71
-		return pids, err
72
-	}
73
-
74
-	cgroupDir, err := getThisCgroupDir(subsystem)
75
-	if err != nil {
76
-		return pids, err
77
-	}
78
-
79
-	filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
80
-	if _, err := os.Stat(filename); os.IsNotExist(err) {
81
-		// With more recent lxc versions use, cgroup will be in lxc/
82
-		filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks")
83
-	}
84
-
85
-	output, err := ioutil.ReadFile(filename)
86
-	if err != nil {
87
-		return pids, err
88
-	}
89
-	for _, p := range strings.Split(string(output), "\n") {
90
-		if len(p) == 0 {
91
-			continue
92
-		}
93
-		pid, err := strconv.Atoi(p)
94
-		if err != nil {
95
-			return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
96
-		}
97
-		pids = append(pids, pid)
98
-	}
99
-	return pids, nil
100
-}
... ...
@@ -7,7 +7,6 @@ import (
7 7
 	"github.com/dotcloud/docker/archive"
8 8
 	"github.com/dotcloud/docker/auth"
9 9
 	"github.com/dotcloud/docker/engine"
10
-	"github.com/dotcloud/docker/pkg/cgroups"
11 10
 	"github.com/dotcloud/docker/pkg/graphdb"
12 11
 	"github.com/dotcloud/docker/pkg/systemd"
13 12
 	"github.com/dotcloud/docker/registry"
... ...
@@ -976,7 +975,7 @@ func (srv *Server) ContainerTop(job *engine.Job) engine.Status {
976 976
 			job.Errorf("Container %s is not running", name)
977 977
 			return engine.StatusErr
978 978
 		}
979
-		pids, err := cgroups.GetPidsForContainer(container.ID)
979
+		pids, err := srv.runtime.execDriver.GetPidsForContainer(container.ID)
980 980
 		if err != nil {
981 981
 			job.Error(err)
982 982
 			return engine.StatusErr