From bfd3df535496df540ac4424db5fcd3482a6aa48d Mon Sep 17 00:00:00 2001 From: Paul Reichelt Date: Mon, 11 Nov 2019 11:06:51 +0100 Subject: [PATCH] Move Kill and PrepareForChildren to platform specific implementation --- pkg/commands/os.go | 22 ---------------------- pkg/commands/os_default_platform.go | 21 +++++++++++++++++++++ pkg/commands/os_windows.go | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 22490b493..5dee9b36e 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -8,7 +8,6 @@ import ( "path/filepath" "strings" "sync" - "syscall" "time" "github.com/go-errors/errors" @@ -330,24 +329,3 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error { } return nil } - -// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself. -func (c *OSCommand) Kill(cmd *exec.Cmd) error { - if cmd.Process == nil { - // somebody got to it before we were able to, poor bastard - return nil - } - if cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid { - // minus sign means we're talking about a PGID as opposed to a PID - return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) - } - - return cmd.Process.Kill() -} - -// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process. -func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) { - cmd.SysProcAttr = &syscall.SysProcAttr{ - Setpgid: true, - } -} diff --git a/pkg/commands/os_default_platform.go b/pkg/commands/os_default_platform.go index 73e453b6b..9c2ba8f36 100644 --- a/pkg/commands/os_default_platform.go +++ b/pkg/commands/os_default_platform.go @@ -17,3 +17,24 @@ func getPlatform() *Platform { fallbackEscapedQuote: "\"", } } + +// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself. +func (c *OSCommand) Kill(cmd *exec.Cmd) error { + if cmd.Process == nil { + // somebody got to it before we were able to, poor bastard + return nil + } + if cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid { + // minus sign means we're talking about a PGID as opposed to a PID + return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + } + + return cmd.Process.Kill() +} + +// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process. +func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) { + cmd.SysProcAttr = &syscall.SysProcAttr{ + Setpgid: true, + } +} diff --git a/pkg/commands/os_windows.go b/pkg/commands/os_windows.go index 8fa9ce1c2..8f9b0a97d 100644 --- a/pkg/commands/os_windows.go +++ b/pkg/commands/os_windows.go @@ -1,5 +1,9 @@ package commands +import ( + "os/exec" +) + func getPlatform() *Platform { return &Platform{ os: "windows", @@ -9,3 +13,16 @@ func getPlatform() *Platform { fallbackEscapedQuote: "\\'", } } + +// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself. +func (c *OSCommand) Kill(cmd *exec.Cmd) error { + if cmd.Process == nil { + // somebody got to it before we were able to, poor bastard + return nil + } + + return cmd.Process.Kill() +} + +// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process. +func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) {}