executor_unix.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //go:build !windows
  2. // +build !windows
  3. package executor
  4. import (
  5. "context"
  6. "os/exec"
  7. "syscall"
  8. )
  9. func (e *Executor) Kill(execReq *InternalLogEntry) error {
  10. if execReq == nil {
  11. return nil
  12. }
  13. helper := ""
  14. killID := ""
  15. if execReq.Attributes != nil {
  16. helper = execReq.Attributes["helper"]
  17. killID = execReq.Attributes["kill_id"]
  18. }
  19. if helper != "" && killID != "" {
  20. killCmd := exec.CommandContext(context.Background(), "olivetin-"+helper, "kill", killID)
  21. _ = killCmd.Run()
  22. }
  23. if execReq.Process != nil {
  24. return syscall.Kill(-execReq.Process.Pid, syscall.SIGKILL)
  25. }
  26. return nil
  27. }
  28. func wrapCommandInShell(ctx context.Context, finalParsedCommand string) *exec.Cmd {
  29. cmd := exec.CommandContext(ctx, "sh", "-c", finalParsedCommand)
  30. // This is to ensure that the process group is killed when the parent process is killed.
  31. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  32. return cmd
  33. }
  34. func wrapCommandDirect(ctx context.Context, execArgs []string) *exec.Cmd {
  35. if len(execArgs) == 0 {
  36. return nil
  37. }
  38. cmd := exec.CommandContext(ctx, execArgs[0], execArgs[1:]...)
  39. // This is to ensure that the process group is killed when the parent process is killed.
  40. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  41. return cmd
  42. }
  43. func wrapCommandExecTool(ctx context.Context, name string) *exec.Cmd {
  44. cmd := exec.CommandContext(ctx, "olivetin-"+name, "exec")
  45. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  46. return cmd
  47. }