Skip to content

Commit

Permalink
F
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jul 24, 2024
1 parent 25c095a commit a5dca03
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
15 changes: 8 additions & 7 deletions internal/command/command_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"golang.org/x/sys/unix"
)

// func setSysProcAttrCtty(cmd *exec.Cmd) {
// if cmd.SysProcAttr == nil {
// cmd.SysProcAttr = &syscall.SysProcAttr{}
// }
// cmd.SysProcAttr.Setctty = true
// cmd.SysProcAttr.Setsid = true
// }
func setSysProcAttrCtty(cmd *exec.Cmd, tty int) {
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Ctty = tty
cmd.SysProcAttr.Setctty = true
cmd.SysProcAttr.Setsid = true
}

func setSysProcAttrPgid(cmd *exec.Cmd) {
if cmd.SysProcAttr == nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/command_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func TestCommand_SimulateCtrlC(t *testing.T) {
require.NoError(t, err)
}

require.EqualError(t, cmd.Wait(), "signal: killed")
require.EqualError(t, cmd.Wait(), "exit status 130")
}

func TestCommand_StopWithSignal(t *testing.T) {
Expand Down
12 changes: 5 additions & 7 deletions internal/command/command_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ func (c *virtualCommand) Start(ctx context.Context) (err error) {
c.cmd.Stdout = c.tty
c.cmd.Stderr = c.tty

// TODO(adamb): creating a new session and setting a controling terminal
// might be required; find the use case and then implement it properly.
// The current implementation misses setting Ctty.
// setSysProcAttrCtty(c.cmd)
// Creating a new process group is required to properly replicate a behaviour
// similar to CTRL-C in the terminal, which sends a SIGINT to the whole group.
setSysProcAttrPgid(c.cmd)
// Create a new session and set the controlling terminal to tty.
// The new process group is created automatically so that sending
// a signal to the command will affect the whole group.
setSysProcAttrCtty(c.cmd, 3) // 3 is the index of the i-th element in ExtraFiles
c.cmd.ExtraFiles = []*os.File{c.tty}

c.logger.Info("starting a virtual command", zap.Any("config", redactConfig(c.ProgramConfig())))
if err := c.cmd.Start(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

// func setSysProcAttrCtty(cmd *exec.Cmd) {}
func setSysProcAttrCtty(cmd *exec.Cmd, tty int) {}

func setSysProcAttrPgid(cmd *exec.Cmd) {}

Expand Down
12 changes: 4 additions & 8 deletions internal/runnerv2service/service_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,22 +710,18 @@ func TestRunnerServiceServerExecute_WithInput(t *testing.T) {

// cancel sleep
time.Sleep(time.Millisecond * 500)
err = stream.Send(&runnerv2alpha1.ExecuteRequest{InputData: []byte{0x04}})
err = stream.Send(&runnerv2alpha1.ExecuteRequest{InputData: []byte{0x03}})
assert.NoError(t, err)

// terminate outer shell and its children
time.Sleep(time.Millisecond * 500)
err = stream.Send(&runnerv2alpha1.ExecuteRequest{
// TODO(adamb): figure out why SIGINT does not work
Stop: runnerv2alpha1.ExecuteStop_EXECUTE_STOP_KILL,
})
err = stream.Send(&runnerv2alpha1.ExecuteRequest{InputData: []byte{0x04}})
assert.NoError(t, err)

result := <-execResult

// TODO(adamb): This should be a specific gRPC error rather than Unknown.
assert.Contains(t, result.Err.Error(), "signal: killed")
assert.Equal(t, 137, result.ExitCode)
assert.Contains(t, result.Err.Error(), "exit status 130")
assert.Equal(t, 130, result.ExitCode)
})

t.Run("CloseSendDirection", func(t *testing.T) {
Expand Down

0 comments on commit a5dca03

Please sign in to comment.