From 29b1d2b23f0d2d410968adaccfacf72a85e13e70 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 17 Nov 2014 11:55:40 -0800 Subject: [PATCH] Move RemovePaths into cgroups pkg for reuse Signed-off-by: Michael Crosby --- cgroups/fs/apply_raw.go | 14 ++++++++------ cgroups/utils.go | 13 ++++++++++++- namespaces/exec.go | 13 ++----------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 3597374b..6f85793d 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -64,21 +64,23 @@ func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) { } paths := make(map[string]string) + defer func() { + if err != nil { + cgroups.RemovePaths(paths) + } + }() for name, sys := range subsystems { if err := sys.Set(d); err != nil { - for _, p := range paths { - os.RemoveAll(p) - } return nil, err } + // FIXME: Apply should, ideally, be reentrant or be broken up into a separate + // create and join phase so that the cgroup hierarchy for a container can be + // created then join consists of writing the process pids to cgroup.procs p, err := d.path(name) if err != nil { if cgroups.IsNotFound(err) { continue } - for _, p := range paths { - os.RemoveAll(p) - } return nil, err } paths[name] = p diff --git a/cgroups/utils.go b/cgroups/utils.go index 77a3c0d7..224a20b9 100644 --- a/cgroups/utils.go +++ b/cgroups/utils.go @@ -189,6 +189,17 @@ func EnterPid(cgroupPaths map[string]string, pid int) error { } } } - return nil } + +// RemovePaths iterates over the provided paths removing them. +// If an error is encountered the removal proceeds and the first error is +// returned to ensure a partial removal is not possible. +func RemovePaths(paths map[string]string) (err error) { + for _, path := range paths { + if rerr := os.RemoveAll(path); err == nil { + err = rerr + } + } + return err +} diff --git a/namespaces/exec.go b/namespaces/exec.go index eac03d5d..b7873edd 100644 --- a/namespaces/exec.go +++ b/namespaces/exec.go @@ -10,6 +10,7 @@ import ( "syscall" "github.com/docker/libcontainer" + "github.com/docker/libcontainer/cgroups" "github.com/docker/libcontainer/cgroups/fs" "github.com/docker/libcontainer/cgroups/systemd" "github.com/docker/libcontainer/network" @@ -63,7 +64,7 @@ func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Wri if err != nil { return terminate(err) } - defer removeCgroupPaths(cgroupPaths) + defer cgroups.RemovePaths(cgroupPaths) var networkState network.NetworkState if err := InitializeNetworking(container, command.Process.Pid, &networkState); err != nil { @@ -172,13 +173,3 @@ func InitializeNetworking(container *libcontainer.Config, nspid int, networkStat } return nil } - -// removeCgroupPaths takes the subsystem paths for each cgroup that was -// setup for the container and removes them from the cgroup filesystem. -// Errors are ignored during the removal because we don't want to end up -// with partially removed cgroups. -func removeCgroupPaths(paths map[string]string) { - for _, path := range paths { - os.RemoveAll(path) - } -}