Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
API: add sandbox winsizeprocess api
Browse files Browse the repository at this point in the history
It sends tty resize request to the agent to resize a process's tty
window.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed May 4, 2018
1 parent 55dc0b2 commit bf4ef43
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ type agent interface {
// the container will be sent the signal.
signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error

// winsizeProcess will tell the agent to set a process' tty size
winsizeProcess(c *Container, processID string, height, width uint32) error

// processListContainer will list the processes running inside the container
processListContainer(sandbox *Sandbox, c Container, options ProcessListOptions) (ProcessList, error)

Expand Down
8 changes: 8 additions & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ func (c *Container) signalProcess(processID string, signal syscall.Signal, all b
return c.sandbox.agent.signalProcess(c, processID, signal, all)
}

func (c *Container) winsizeProcess(processID string, height, width uint32) error {
if c.state.State != StateReady && c.state.State != StateRunning {
return fmt.Errorf("Container not ready or running, impossible to signal the container")
}

return c.sandbox.agent.winsizeProcess(c, processID, height, width)
}

func (c *Container) processList(options ProcessListOptions) (ProcessList, error) {
if err := c.checkSandboxRunning("ps"); err != nil {
return nil, err
Expand Down
26 changes: 26 additions & 0 deletions virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,29 @@ func TestKillContainerErrorState(t *testing.T) {
err = c.kill(syscall.SIGKILL, true)
assert.Error(err)
}

func TestWinsizeProcessErrorState(t *testing.T) {
assert := assert.New(t)
c := &Container{
sandbox: &Sandbox{
state: State{
State: StateRunning,
},
},
}
processID := "foobar"

// Container state undefined
err := c.winsizeProcess(processID, 100, 200)
assert.Error(err)

// Container paused
c.state.State = StatePaused
err = c.winsizeProcess(processID, 100, 200)
assert.Error(err)

// Container stopped
c.state.State = StateStopped
err = c.winsizeProcess(processID, 100, 200)
assert.Error(err)
}
5 changes: 5 additions & 0 deletions virtcontainers/hyperstart_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,8 @@ func (h *hyper) waitProcess(c *Container, processID string) (int32, error) {
// cc-agent does not support wait process
return 0, nil
}

func (h *hyper) winsizeProcess(c *Container, processID string, height, width uint32) error {
// cc-agent does not support winsize process
return nil
}
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type VCSandbox interface {
EnterContainer(containerID string, cmd Cmd) (VCContainer, *Process, error)
WaitProcess(containerID, processID string) (int32, error)
SignalProcess(containerID, processID string, signal syscall.Signal, all bool) error
WinsizeProcess(containerID, processID string, height, width uint32) error
}

// VCContainer is the Container interface
Expand Down
15 changes: 15 additions & 0 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,18 @@ func (k *kataAgent) signalProcess(c *Container, processID string, signal syscall
return err
}

func (k *kataAgent) winsizeProcess(c *Container, processID string, height, width uint32) error {
req := &grpc.TtyWinResizeRequest{
ContainerId: c.id,
ExecId: processID,
Row: height,
Column: width,
}

_, err := k.sendReq(req)
return err
}

func (k *kataAgent) processListContainer(sandbox *Sandbox, c Container, options ProcessListOptions) (ProcessList, error) {
req := &grpc.ListProcessesRequest{
ContainerId: c.id,
Expand Down Expand Up @@ -999,6 +1011,9 @@ func (k *kataAgent) installReqFunc(c *kataclient.AgentClient) {
k.reqHandlers["grpc.WaitProcessRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) {
return k.client.WaitProcess(ctx, req.(*grpc.WaitProcessRequest), opts...)
}
k.reqHandlers["grpc.TtyWinResizeRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) {
return k.client.TtyWinResize(ctx, req.(*grpc.TtyWinResizeRequest), opts...)
}
}

func (k *kataAgent) sendReq(request interface{}) (interface{}, error) {
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/noop_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ func (n *noopAgent) check() error {
func (n *noopAgent) waitProcess(c *Container, processID string) (int32, error) {
return 0, nil
}

// winsizeProcess is the Noop agent process tty resizer. It does nothing.
func (n *noopAgent) winsizeProcess(c *Container, processID string, height, width uint32) error {
return nil
}
5 changes: 5 additions & 0 deletions virtcontainers/pkg/vcmock/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ func (p *Sandbox) WaitProcess(containerID, processID string) (int32, error) {
func (p *Sandbox) SignalProcess(containerID, processID string, signal syscall.Signal, all bool) error {
return nil
}

// WinsizeProcess implements the VCSandbox function of the same name.
func (p *Sandbox) WinsizeProcess(containerID, processID string, height, width uint32) error {
return nil
}
14 changes: 14 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,20 @@ func (s *Sandbox) SignalProcess(containerID, processID string, signal syscall.Si
return c.signalProcess(processID, signal, all)
}

// WinsizeProcess resizes the tty window of a process
func (s *Sandbox) WinsizeProcess(containerID, processID string, height, width uint32) error {
if s.state.State != StateRunning {
return fmt.Errorf("Sandbox not running")
}

c, err := s.findContainer(containerID)
if err != nil {
return err
}

return c.winsizeProcess(processID, height, width)
}

func createAssets(sandboxConfig *SandboxConfig) error {
kernel, err := newAsset(sandboxConfig, kernelAsset)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,3 +1469,33 @@ func TestSignalProcess(t *testing.T) {
err = s.SignalProcess(contID, execID, syscall.SIGKILL, false)
assert.Nil(t, err, "Wait process failed: %v", err)
}

func TestWinsizeProcess(t *testing.T) {
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NoopNetworkModel, NetworkConfig{}, nil, nil)
assert.Nil(t, err, "VirtContainers should not allow empty sandboxes")
defer cleanUp()

contID := "foo"
execID := "bar"
err = s.WinsizeProcess(contID, execID, 100, 200)
assert.NotNil(t, err, "Winsize process in stopped sandbox should fail")

err = s.start()
assert.Nil(t, err, "Failed to start sandbox: %v", err)

err = s.WinsizeProcess(contID, execID, 100, 200)
assert.NotNil(t, err, "Winsize process in non-existing container should fail")

contConfig := newTestContainerConfigNoop(contID)
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)

err = s.WinsizeProcess(contID, execID, 100, 200)
assert.Nil(t, err, "Winsize process in ready container failed: %v", err)

_, err = s.StartContainer(contID)
assert.Nil(t, err, "Start container failed: %v", err)

err = s.WinsizeProcess(contID, execID, 100, 200)
assert.Nil(t, err, "Winsize process failed: %v", err)
}

0 comments on commit bf4ef43

Please sign in to comment.