Skip to content

Commit

Permalink
Move RemovePaths into cgroups pkg for reuse
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <[email protected]>
  • Loading branch information
crosbymichael committed Nov 17, 2014
1 parent bc7efa6 commit 29b1d2b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
14 changes: 8 additions & 6 deletions cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 2 additions & 11 deletions namespaces/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 29b1d2b

Please sign in to comment.