Move RemovePaths into cgroups pkg for reuse

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-11-17 11:55:40 -08:00
parent bc7efa6b81
commit 29b1d2b23f
3 changed files with 22 additions and 18 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}
}