Skip to content

Commit

Permalink
Change OS specific command behavior defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kruse <[email protected]>
  • Loading branch information
c-kruse committed Sep 7, 2023
1 parent cb4e099 commit 98c5bea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
9 changes: 2 additions & 7 deletions pkg/receiver/jobreceiver/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func NewExecution(ctx context.Context, request ExecutionRequest) *Execution {
if len(request.Env) > 0 {
cmd.Env = request.Env
}

setOptions(cmd)
return &Execution{
cmd: cmd,
ctx: ctx,
Expand Down Expand Up @@ -120,10 +122,3 @@ type ExecutionResponse struct {
// Error is passed when the outcome of the execution is uncertain
Error error
}

// cError const error type for sentinels
type cError string

func (e cError) Error() string {
return string(e)
}
15 changes: 15 additions & 0 deletions pkg/receiver/jobreceiver/command/exec_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package command

import (
"os/exec"
"syscall"
)

// setOptions sets the process group of the command processprocgroup
func setOptions(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pdeathsig: syscall.SIGTERM}
cmd.Cancel = func() error {
// Kill process group instead
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}
18 changes: 18 additions & 0 deletions pkg/receiver/jobreceiver/command/exec_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !linux && !windows
// +build !linux,!windows

package command

import (
"os/exec"
"syscall"
)

// setOptions sets the process group of the command process
func setOptions(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Cancel = func() error {
// Kill process group instead
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}
26 changes: 26 additions & 0 deletions pkg/receiver/jobreceiver/command/exec_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package command

import (
"fmt"
"os/exec"
"syscall"
)

// setOptions sets the process group of the command process
func setOptions(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
cmd.Cancel = func() error {
// Try with taskkill first
taskkill := exec.Command(
"taskkill",
"/T", "/F", "/PID", fmt.Sprint(cmd.Process.Pid),
)
if err := taskkill.Run(); err == nil {
return nil
}
// Fall back to the default behavior
return cmd.Process.Kill()
}
}

0 comments on commit 98c5bea

Please sign in to comment.