From bfd3df535496df540ac4424db5fcd3482a6aa48d Mon Sep 17 00:00:00 2001 From: Paul Reichelt Date: Mon, 11 Nov 2019 11:06:51 +0100 Subject: [PATCH 1/5] 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) {} From d971621e66dccfff531f7c91793bb0b4524ad151 Mon Sep 17 00:00:00 2001 From: Paul Reichelt Date: Mon, 11 Nov 2019 11:32:44 +0100 Subject: [PATCH 2/5] Kill all related processes using windows internals Use windows kernel32.dll to fetch all related processes. Inspired by: https://blog.csdn.net/fyxichen/article/details/51857864 --- pkg/commands/os_windows.go | 114 ++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/pkg/commands/os_windows.go b/pkg/commands/os_windows.go index 8f9b0a97d..374f8b7d4 100644 --- a/pkg/commands/os_windows.go +++ b/pkg/commands/os_windows.go @@ -1,7 +1,10 @@ package commands import ( + "os" "os/exec" + "syscall" + "unsafe" ) func getPlatform() *Platform { @@ -21,8 +24,117 @@ func (c *OSCommand) Kill(cmd *exec.Cmd) error { return nil } - return cmd.Process.Kill() + pids := Getppids(uint32(cmd.Process.Pid)) + for _, pid := range pids { + pro, err := os.FindProcess(int(pid)) + if err != nil { + continue + } + + pro.Kill() + } + + return nil } // 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) {} + +const ( + MAX_PATH = 260 + TH32CS_SNAPPROCESS = 0x00000002 +) + +type ProcessInfo struct { + Name string + Pid uint32 + PPid uint32 +} + +type PROCESSENTRY32 struct { + DwSize uint32 + CntUsage uint32 + Th32ProcessID uint32 + Th32DefaultHeapID uintptr + Th32ModuleID uint32 + CntThreads uint32 + Th32ParentProcessID uint32 + PcPriClassBase int32 + DwFlags uint32 + SzExeFile [MAX_PATH]uint16 +} + +type HANDLE uintptr + +var ( + modkernel32 = syscall.NewLazyDLL("kernel32.dll") + procCreateToolhelp32Snapshot = modkernel32.NewProc("CreateToolhelp32Snapshot") + procProcess32First = modkernel32.NewProc("Process32FirstW") + procProcess32Next = modkernel32.NewProc("Process32NextW") + procCloseHandle = modkernel32.NewProc("CloseHandle") +) + +func Getppids(pid uint32) []uint32 { + infos, err := GetProcs() + if err != nil { + return []uint32{pid} + } + var pids []uint32 = make([]uint32, 0, len(infos)) + var index int = 0 + pids = append(pids, pid) + + var length int = len(pids) + for index < length { + for _, info := range infos { + if info.PPid == pids[index] { + pids = append(pids, info.Pid) + } + } + index += 1 + length = len(pids) + } + return pids +} + +func GetProcs() (procs []ProcessInfo, err error) { + snap := createToolhelp32Snapshot(TH32CS_SNAPPROCESS, uint32(0)) + if snap == 0 { + err = syscall.GetLastError() + return + } + defer closeHandle(snap) + var pe32 PROCESSENTRY32 + pe32.DwSize = uint32(unsafe.Sizeof(pe32)) + if process32First(snap, &pe32) == false { + err = syscall.GetLastError() + return + } + procs = append(procs, ProcessInfo{syscall.UTF16ToString(pe32.SzExeFile[:260]), pe32.Th32ProcessID, pe32.Th32ParentProcessID}) + for process32Next(snap, &pe32) { + procs = append(procs, ProcessInfo{syscall.UTF16ToString(pe32.SzExeFile[:260]), pe32.Th32ProcessID, pe32.Th32ParentProcessID}) + } + return +} + +func createToolhelp32Snapshot(flags, processId uint32) HANDLE { + ret, _, _ := procCreateToolhelp32Snapshot.Call(uintptr(flags), uintptr(processId)) + if ret <= 0 { + return HANDLE(0) + } + return HANDLE(ret) +} + +func process32First(snapshot HANDLE, pe *PROCESSENTRY32) bool { + ret, _, _ := procProcess32First.Call(uintptr(snapshot), uintptr(unsafe.Pointer(pe))) + return ret != 0 +} + +func process32Next(snapshot HANDLE, pe *PROCESSENTRY32) bool { + ret, _, _ := procProcess32Next.Call(uintptr(snapshot), uintptr(unsafe.Pointer(pe))) + return ret != 0 +} + +func closeHandle(object HANDLE) bool { + ret, _, _ := procCloseHandle.Call(uintptr(object)) + return ret != 0 +} From 168f63c2d595b8ff67a4d262bf43a8e0421317f1 Mon Sep 17 00:00:00 2001 From: Dawid Dziurla Date: Mon, 11 Nov 2019 12:01:08 +0100 Subject: [PATCH 3/5] Add missing imports --- pkg/commands/os_default_platform.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/commands/os_default_platform.go b/pkg/commands/os_default_platform.go index 9c2ba8f36..067bb5e6f 100644 --- a/pkg/commands/os_default_platform.go +++ b/pkg/commands/os_default_platform.go @@ -3,7 +3,9 @@ package commands import ( + "os/exec" "runtime" + "syscall" ) func getPlatform() *Platform { From 131d14f37d3ad3dd74e66d1d4fc74809ddae5e42 Mon Sep 17 00:00:00 2001 From: Paul Reichelt Date: Mon, 11 Nov 2019 12:04:39 +0100 Subject: [PATCH 4/5] Re-enable windows build in goreleaser --- .goreleaser.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 35dae16cb..559d882e6 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,7 +9,7 @@ builds: - id: binary goos: # - freebsd - # - windows # may reenable later + - windows - darwin - linux goarch: From 214f96d0ca5af71a1063b7f71a39b92a2c307d8e Mon Sep 17 00:00:00 2001 From: Dawid Dziurla Date: Mon, 11 Nov 2019 12:10:25 +0100 Subject: [PATCH 5/5] Build windows amd64 target on CI --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c21a7ff06..0be01eff8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,7 +33,7 @@ build-shared: &build-shared - run: name: Compile project on every platform command: | - gox -parallel 10 -os "linux freebsd" -osarch "darwin/i386 darwin/amd64" + gox -parallel 10 -os "linux freebsd" -osarch "darwin/i386 darwin/amd64 windows/amd64" - save_cache: key: pkg-cache-{{ checksum "go.sum" }}-v5 paths: