Skip to content

Commit

Permalink
Create new process group on process startup.
Browse files Browse the repository at this point in the history
Clean up by sending SIGKILL to the whole process group.
  • Loading branch information
emate committed Nov 20, 2017
1 parent 815972b commit a7952ec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func (e *UniversalExecutor) LaunchCmd(command *ExecCommand) (*ProcessState, erro
// set the task dir as the working directory for the command
e.cmd.Dir = e.ctx.TaskDir

// start command in separate process group
if err := e.setNewProcessGroup(); err != nil {
return nil, err
}

// configuring the chroot, resource container, and start the plugin
// process in the chroot.
if err := e.configureIsolation(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions client/driver/executor/executor_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (e *UniversalExecutor) configureIsolation() error {
return nil
}

func (e *UniversalExecutor) setNewProcessGroup() error {
return nil
}

func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
pidStats, err := e.pidStats()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions client/driver/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ 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
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 := killProcess(h.userPid); e != nil {
h.logger.Printf("[ERR] driver.raw_exec: error killing user process: %v", e)
if e := killProcessGroup(h.userPid); e != nil {
h.logger.Printf("[ERR] driver.raw_exec: error killing user process group: %v", e)
}
}

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

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -109,6 +110,15 @@ 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 a7952ec

Please sign in to comment.