2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-19 22:16:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-24 00:18:10 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-01-26 23:09:59 +00:00
|
|
|
"syscall"
|
2017-01-24 00:18:10 +00:00
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-01-24 23:55:32 +00:00
|
|
|
apishim "github.com/docker/containerd/api/shim"
|
2017-01-19 22:16:50 +00:00
|
|
|
"github.com/docker/containerd/utils"
|
|
|
|
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-01-19 23:21:25 +00:00
|
|
|
var emptyResponse = &google_protobuf.Empty{}
|
|
|
|
|
2017-01-25 21:27:48 +00:00
|
|
|
// NewService returns a new shim service that can be used via GRPC
|
2017-01-24 23:55:32 +00:00
|
|
|
func NewService() *Service {
|
|
|
|
return &Service{
|
|
|
|
processes: make(map[int]process),
|
2017-01-26 19:29:19 +00:00
|
|
|
events: make(chan *apishim.Event, 4096),
|
2017-01-24 23:55:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2017-01-25 21:27:48 +00:00
|
|
|
initProcess *initProcess
|
|
|
|
id string
|
|
|
|
mu sync.Mutex
|
|
|
|
processes map[int]process
|
|
|
|
events chan *apishim.Event
|
2017-01-26 23:09:59 +00:00
|
|
|
execID int
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishim.CreateResponse, error) {
|
2017-01-24 00:18:10 +00:00
|
|
|
process, err := newInitProcess(ctx, r)
|
2017-01-19 22:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
2017-01-25 21:27:48 +00:00
|
|
|
s.initProcess = process
|
2017-01-24 00:18:10 +00:00
|
|
|
pid := process.Pid()
|
2017-01-25 21:27:48 +00:00
|
|
|
s.processes[pid] = process
|
|
|
|
s.id = r.ID
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-26 19:29:19 +00:00
|
|
|
s.events <- &apishim.Event{
|
|
|
|
Type: apishim.EventType_CREATED,
|
|
|
|
ID: r.ID,
|
|
|
|
Pid: uint32(pid),
|
|
|
|
}
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.CreateResponse{
|
2017-01-24 00:18:10 +00:00
|
|
|
Pid: uint32(pid),
|
2017-01-19 22:16:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_protobuf.Empty, error) {
|
2017-01-25 21:27:48 +00:00
|
|
|
if err := s.initProcess.Start(ctx); err != nil {
|
2017-01-19 23:21:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 19:29:19 +00:00
|
|
|
s.events <- &apishim.Event{
|
|
|
|
Type: apishim.EventType_STARTED,
|
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(s.initProcess.Pid()),
|
|
|
|
}
|
2017-01-19 23:21:25 +00:00
|
|
|
return emptyResponse, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishim.DeleteResponse, error) {
|
2017-01-26 23:09:59 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
p, ok := s.processes[int(r.Pid)]
|
|
|
|
s.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
|
|
|
}
|
|
|
|
if err := p.Delete(ctx); err != nil {
|
2017-01-24 00:18:10 +00:00
|
|
|
return nil, err
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
2017-01-26 23:09:59 +00:00
|
|
|
delete(s.processes, p.Pid())
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.DeleteResponse{
|
2017-01-26 23:09:59 +00:00
|
|
|
ExitStatus: uint32(p.Status()),
|
2017-01-24 00:18:10 +00:00
|
|
|
}, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.ExecResponse, error) {
|
2017-01-24 22:45:18 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2017-01-26 23:09:59 +00:00
|
|
|
s.execID++
|
|
|
|
process, err := newExecProcess(ctx, r, s.initProcess, s.execID)
|
2017-01-24 22:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pid := process.Pid()
|
|
|
|
s.processes[pid] = process
|
2017-01-26 19:29:19 +00:00
|
|
|
s.events <- &apishim.Event{
|
|
|
|
Type: apishim.EventType_EXEC_ADDED,
|
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(pid),
|
|
|
|
}
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.ExecResponse{
|
2017-01-24 22:45:18 +00:00
|
|
|
Pid: uint32(pid),
|
|
|
|
}, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) {
|
2017-01-26 19:29:19 +00:00
|
|
|
if r.Pid == 0 {
|
|
|
|
return nil, fmt.Errorf("pid not provided in request")
|
|
|
|
}
|
|
|
|
ws := console.WinSize{
|
2017-01-19 22:16:50 +00:00
|
|
|
Width: uint16(r.Width),
|
|
|
|
Height: uint16(r.Height),
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
p, ok := s.processes[int(r.Pid)]
|
|
|
|
s.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
|
|
|
}
|
|
|
|
if err := p.Resize(ws); err != nil {
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 23:21:25 +00:00
|
|
|
return emptyResponse, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:27:48 +00:00
|
|
|
func (s *Service) Events(r *apishim.EventsRequest, stream apishim.Shim_EventsServer) error {
|
|
|
|
for e := range s.events {
|
|
|
|
if err := stream.Send(e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
func (s *Service) State(ctx context.Context, r *apishim.StateRequest) (*apishim.StateResponse, error) {
|
|
|
|
o := &apishim.StateResponse{
|
|
|
|
ID: s.id,
|
|
|
|
Processes: []*apishim.Process{},
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, p := range s.processes {
|
|
|
|
state := apishim.State_RUNNING
|
|
|
|
if err := syscall.Kill(p.Pid(), 0); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state = apishim.State_STOPPED
|
|
|
|
}
|
|
|
|
o.Processes = append(o.Processes, &apishim.Process{
|
|
|
|
Pid: uint32(p.Pid()),
|
|
|
|
State: state,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) ProcessExit(e utils.Exit) error {
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
if p, ok := s.processes[e.Pid]; ok {
|
|
|
|
p.Exited(e.Status)
|
2017-01-25 21:27:48 +00:00
|
|
|
s.events <- &apishim.Event{
|
|
|
|
Type: apishim.EventType_EXIT,
|
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(p.Pid()),
|
|
|
|
ExitStatus: uint32(e.Status),
|
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil
|
|
|
|
}
|