Refactor killAllPids

This refactors the function to avoid two calls to FindProcess

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-01-12 10:31:31 -08:00
parent 2d9ef3af72
commit 4903df2ed5
1 changed files with 19 additions and 15 deletions

View File

@ -116,32 +116,36 @@ func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Wri
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
}
func killAllPids(container *libcontainer.Config) {
// killAllPids itterates over all of the container's processes
// sending a SIGKILL to each process.
func killAllPids(container *libcontainer.Config) error {
var (
pids []int
err error
procs []*os.Process
freeze = fs.Freeze
getPids = fs.GetPids
)
freeze := fs.Freeze
getPids := fs.GetPids
if systemd.UseSystemd() {
freeze = systemd.Freeze
getPids = systemd.GetPids
}
freeze(container.Cgroups, cgroups.Frozen)
if pids, err = getPids(container.Cgroups); err == nil {
for _, pid := range pids {
if p, err := os.FindProcess(pid); err == nil {
p.Kill()
}
pids, err := getPids(container.Cgroups)
if err != nil {
return err
}
for _, pid := range pids {
// TODO: log err without aborting if we are unable to find
// a single PID
if p, err := os.FindProcess(pid); err == nil {
procs = append(procs, p)
p.Kill()
}
}
freeze(container.Cgroups, cgroups.Thawed)
for _, pid := range pids {
if p, err := os.FindProcess(pid); err == nil {
p.Wait()
}
for _, p := range procs {
p.Wait()
}
return err
}
// DefaultCreateCommand will return an exec.Cmd with the Cloneflags set to the proper namespaces