Skip to content

Commit

Permalink
Make starting & cleaning process group Windows compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
emate committed Nov 22, 2017
1 parent a7952ec commit f02c19e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
13 changes: 12 additions & 1 deletion client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,17 @@ func ClientCleanup(ic *dstructs.IsolationConfig, pid int) error {
return clientCleanup(ic, pid)
}

// Cleanup any still hanging user processes
func (e *UniversalExecutor) cleanupUserLeftovers(proc *os.Process) error {
// If new process group was created upon command execution
// we can kill the whole process group now to cleanup any leftovers.
if e.cmd.SysProcAttr != nil && e.cmd.SysProcAttr.Setpgid {
return syscall.Kill(-proc.Pid, syscall.SIGKILL)
} else {
return proc.Kill()
}
}

// Exit cleans up the alloc directory, destroys resource container and kills the
// user process
func (e *UniversalExecutor) Exit() error {
Expand Down Expand Up @@ -472,7 +483,7 @@ func (e *UniversalExecutor) Exit() error {
if err != nil {
e.logger.Printf("[ERR] executor: can't find process with pid: %v, err: %v",
e.cmd.Process.Pid, err)
} else if err := proc.Kill(); err != nil && err.Error() != finishedErr {
} else if err := e.cleanupUserLeftovers(proc); err != nil && err.Error() != finishedErr {
merr.Errors = append(merr.Errors,
fmt.Errorf("can't kill process with pid: %v, err: %v", e.cmd.Process.Pid, err))
}
Expand Down
9 changes: 0 additions & 9 deletions client/driver/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,6 @@ func DestroyCgroup(groups *cgroupConfig.Cgroup, cgPaths map[string]string, execu
return mErrs.ErrorOrNil()
}

// configure new process group for child process
func (e *UniversalExecutor) setNewProcessGroup() error {
if e.cmd.SysProcAttr == nil {
e.cmd.SysProcAttr = &syscall.SysProcAttr{}
}
e.cmd.SysProcAttr.Setpgid = true
return nil
}

// getCgroupManager returns the correct libcontainer cgroup manager.
func getCgroupManager(groups *cgroupConfig.Cgroup, paths map[string]string) cgroups.Manager {
return &cgroupFs.Manager{Cgroups: groups, Paths: paths}
Expand Down
10 changes: 10 additions & 0 deletions client/driver/executor/executor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package executor
import (
"fmt"
"io"
"syscall"

syslog "github.com/RackSec/srslog"

Expand Down Expand Up @@ -47,3 +48,12 @@ func (e *UniversalExecutor) collectLogs(we io.Writer, wo io.Writer) {
}
}
}

// configure new process group for child process
func (e *UniversalExecutor) setNewProcessGroup() error {
if e.cmd.SysProcAttr == nil {
e.cmd.SysProcAttr = &syscall.SysProcAttr{}
}
e.cmd.SysProcAttr.Setpgid = true
return nil
}
4 changes: 2 additions & 2 deletions client/driver/raw_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func (h *rawExecHandle) run() {
ps, werr := h.executor.Wait()
close(h.doneCh)
if ps.ExitCode == 0 && werr != nil {
if e := killProcessGroup(h.userPid); e != nil {
h.logger.Printf("[ERR] driver.raw_exec: error killing user process group: %v", e)
if e := killProcess(h.userPid); e != nil {
h.logger.Printf("[ERR] driver.raw_exec: error killing user process: %v", e)
}
}

Expand Down
10 changes: 0 additions & 10 deletions client/driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -110,15 +109,6 @@ func killProcess(pid int) error {
return proc.Kill()
}

// killProcessGroup kills a process group with the given pid
func killProcessGroup(pid int) error {
proc, err := os.FindProcess(pid)
if err != nil {
return err
}
return syscall.Kill(-proc.Pid, syscall.SIGKILL)
}

// destroyPlugin kills the plugin with the given pid and also kills the user
// process
func destroyPlugin(pluginPid int, userPid int) error {
Expand Down

0 comments on commit f02c19e

Please sign in to comment.