Working tty and io support in shim

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-01-26 11:29:19 -08:00
parent 8e5e9ae70e
commit b59bd59d8a
9 changed files with 171 additions and 79 deletions

View file

@ -7,6 +7,7 @@ import (
"path/filepath"
"sync"
"github.com/crosbymichael/console"
runc "github.com/crosbymichael/go-runc"
apishim "github.com/docker/containerd/api/shim"
specs "github.com/opencontainers/runtime-spec/specs-go"
@ -16,7 +17,7 @@ type execProcess struct {
sync.WaitGroup
id string
console *runc.Console
console console.Console
io runc.IO
status int
pid int
@ -42,6 +43,7 @@ func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *ini
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
return nil, err
}
defer os.Remove(socket.Path())
} else {
// TODO: get uid/gid
if io, err = runc.NewPipeIO(0, 0); err != nil {
@ -108,10 +110,12 @@ func (e *execProcess) Status() int {
func (e *execProcess) Exited(status int) {
e.status = status
e.Wait()
e.io.Close()
if e.io != nil {
e.io.Close()
}
}
func (e *execProcess) Resize(ws runc.WinSize) error {
func (e *execProcess) Resize(ws console.WinSize) error {
if e.console == nil {
return nil
}

View file

@ -7,6 +7,7 @@ import (
"sync"
"syscall"
"github.com/crosbymichael/console"
runc "github.com/crosbymichael/go-runc"
apishim "github.com/docker/containerd/api/shim"
)
@ -16,7 +17,7 @@ type initProcess struct {
id string
bundle string
console *runc.Console
console console.Console
io runc.IO
runc *runc.Runc
status int
@ -47,6 +48,7 @@ func newInitProcess(context context.Context, r *apishim.CreateRequest) (*initPro
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
return nil, err
}
defer os.Remove(socket.Path())
} else {
// TODO: get uid/gid
if io, err = runc.NewPipeIO(0, 0); err != nil {
@ -105,11 +107,13 @@ func (p *initProcess) Delete(context context.Context) error {
p.killAll(context)
p.Wait()
err := p.runc.Delete(context, p.id)
p.io.Close()
if p.io != nil {
p.io.Close()
}
return err
}
func (p *initProcess) Resize(ws runc.WinSize) error {
func (p *initProcess) Resize(ws console.WinSize) error {
if p.console == nil {
return nil
}

View file

@ -7,11 +7,12 @@ import (
"sync"
"syscall"
"github.com/crosbymichael/console"
runc "github.com/crosbymichael/go-runc"
"github.com/tonistiigi/fifo"
)
func copyConsole(ctx context.Context, console *runc.Console, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
func copyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
if err != nil {
return err

View file

@ -1,12 +1,12 @@
package shim
import runc "github.com/crosbymichael/go-runc"
import "github.com/crosbymichael/console"
type process interface {
// Pid returns the pid for the process
Pid() int
// Resize resizes the process console
Resize(ws runc.WinSize) error
Resize(ws console.WinSize) error
// Exited sets the exit status for the process
Exited(status int)
// Status returns the exit status

View file

@ -4,7 +4,7 @@ import (
"fmt"
"sync"
runc "github.com/crosbymichael/go-runc"
"github.com/crosbymichael/console"
apishim "github.com/docker/containerd/api/shim"
"github.com/docker/containerd/utils"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
@ -17,7 +17,7 @@ var emptyResponse = &google_protobuf.Empty{}
func NewService() *Service {
return &Service{
processes: make(map[int]process),
events: make(chan *apishim.Event, 2048),
events: make(chan *apishim.Event, 4096),
}
}
@ -40,6 +40,11 @@ func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishi
s.processes[pid] = process
s.id = r.ID
s.mu.Unlock()
s.events <- &apishim.Event{
Type: apishim.EventType_CREATED,
ID: r.ID,
Pid: uint32(pid),
}
return &apishim.CreateResponse{
Pid: uint32(pid),
}, nil
@ -49,6 +54,11 @@ func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_p
if err := s.initProcess.Start(ctx); err != nil {
return nil, err
}
s.events <- &apishim.Event{
Type: apishim.EventType_STARTED,
ID: s.id,
Pid: uint32(s.initProcess.Pid()),
}
return emptyResponse, nil
}
@ -73,13 +83,21 @@ func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.Ex
}
pid := process.Pid()
s.processes[pid] = process
s.events <- &apishim.Event{
Type: apishim.EventType_EXEC_ADDED,
ID: s.id,
Pid: uint32(pid),
}
return &apishim.ExecResponse{
Pid: uint32(pid),
}, nil
}
func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) {
ws := runc.WinSize{
if r.Pid == 0 {
return nil, fmt.Errorf("pid not provided in request")
}
ws := console.WinSize{
Width: uint16(r.Width),
Height: uint16(r.Height),
}