Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions cmd/sops/subcommand/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package exec

import (
"bytes"
"context"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"

Expand Down Expand Up @@ -76,7 +79,14 @@ func ExecWithFile(opts ExecOpts) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := cmd.Start(); err != nil {
return err
}
redirectSignals(ctx, cmd)

return cmd.Wait()
}

func ExecWithEnv(opts ExecOpts) error {
Expand Down Expand Up @@ -107,5 +117,32 @@ func ExecWithEnv(opts ExecOpts) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := cmd.Start(); err != nil {
return err
}
redirectSignals(ctx, cmd)

return cmd.Wait()
}

func redirectSignals(ctx context.Context, cmd *exec.Cmd) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc)

go func(c chan os.Signal) {
for {
select {
case sig := <-c:
if err := cmd.Process.Signal(sig); err != nil {
log.Warn("Unable to send a signal to the child process")
continue
}
case <-ctx.Done():
signal.Stop(sigc)
return
}
}
}(sigc)
}
4 changes: 3 additions & 1 deletion cmd/sops/subcommand/exec/exec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

func BuildCommand(command string) *exec.Cmd {
return exec.Command("/bin/sh", "-c", command)
cmd := exec.Command("/bin/sh", "-c", command)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}
return cmd
}

func WritePipe(pipe string, contents []byte) {
Expand Down