2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-19 22:16:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-13 18:23:28 +00:00
|
|
|
"os"
|
2017-01-24 00:18:10 +00:00
|
|
|
"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-02-09 07:31:26 +00:00
|
|
|
shimapi "github.com/docker/containerd/api/services/shim"
|
2017-02-13 18:23:28 +00:00
|
|
|
"github.com/docker/containerd/api/types/container"
|
2017-01-19 22:16:50 +00:00
|
|
|
"github.com/docker/containerd/utils"
|
|
|
|
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
2017-02-06 22:57:43 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-01-19 22:16:50 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
var empty = &google_protobuf.Empty{}
|
2017-01-19 23:21:25 +00:00
|
|
|
|
2017-02-01 21:25:28 +00:00
|
|
|
// New returns a new shim service that can be used via GRPC
|
|
|
|
func New() *Service {
|
2017-01-24 23:55:32 +00:00
|
|
|
return &Service{
|
|
|
|
processes: make(map[int]process),
|
2017-02-13 18:23:28 +00:00
|
|
|
events: make(chan *container.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
|
2017-02-06 22:57:43 +00:00
|
|
|
bundle string
|
2017-01-25 21:27:48 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
processes map[int]process
|
2017-02-13 18:23:28 +00:00
|
|
|
events chan *container.Event
|
2017-01-26 23:09:59 +00:00
|
|
|
execID int
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Create(ctx context.Context, r *shimapi.CreateRequest) (*shimapi.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-02-06 22:57:43 +00:00
|
|
|
s.id = r.ID
|
|
|
|
s.bundle = r.Bundle
|
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
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-02-13 18:23:28 +00:00
|
|
|
s.events <- &container.Event{
|
|
|
|
Type: container.Event_CREATE,
|
2017-01-26 19:29:19 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Pid: uint32(pid),
|
|
|
|
}
|
2017-02-09 07:31:26 +00:00
|
|
|
return &shimapi.CreateResponse{
|
2017-01-24 00:18:10 +00:00
|
|
|
Pid: uint32(pid),
|
2017-01-19 22:16:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Start(ctx context.Context, r *shimapi.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-02-13 18:23:28 +00:00
|
|
|
s.events <- &container.Event{
|
|
|
|
Type: container.Event_START,
|
2017-01-26 19:29:19 +00:00
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(s.initProcess.Pid()),
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
return empty, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Delete(ctx context.Context, r *shimapi.DeleteRequest) (*shimapi.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 {
|
2017-02-13 18:23:28 +00:00
|
|
|
p = s.initProcess
|
2017-01-26 23:09:59 +00:00
|
|
|
}
|
|
|
|
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-02-09 07:31:26 +00:00
|
|
|
return &shimapi.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-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Exec(ctx context.Context, r *shimapi.ExecRequest) (*shimapi.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-02-13 18:23:28 +00:00
|
|
|
s.events <- &container.Event{
|
|
|
|
Type: container.Event_EXEC_ADDED,
|
2017-01-26 19:29:19 +00:00
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(pid),
|
|
|
|
}
|
2017-02-09 07:31:26 +00:00
|
|
|
return &shimapi.ExecResponse{
|
2017-01-24 22:45:18 +00:00
|
|
|
Pid: uint32(pid),
|
|
|
|
}, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Pty(ctx context.Context, r *shimapi.PtyRequest) (*google_protobuf.Empty, error) {
|
2017-01-26 19:29:19 +00:00
|
|
|
if r.Pid == 0 {
|
2017-02-06 22:57:43 +00:00
|
|
|
return nil, errors.Errorf("pid not provided in request")
|
2017-01-26 19:29:19 +00:00
|
|
|
}
|
|
|
|
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 {
|
2017-02-06 22:57:43 +00:00
|
|
|
return nil, errors.Errorf("process does not exist %d", r.Pid)
|
2017-01-24 00:18:10 +00:00
|
|
|
}
|
|
|
|
if err := p.Resize(ws); err != nil {
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
return empty, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Events(r *shimapi.EventsRequest, stream shimapi.Shim_EventsServer) error {
|
2017-01-25 21:27:48 +00:00
|
|
|
for e := range s.events {
|
|
|
|
if err := stream.Send(e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.StateResponse, error) {
|
2017-03-02 09:42:55 +00:00
|
|
|
st, err := s.initProcess.ContainerStatus(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
status := container.Status_UNKNOWN
|
|
|
|
switch st {
|
|
|
|
case "created":
|
|
|
|
status = container.Status_CREATED
|
|
|
|
case "running":
|
|
|
|
status = container.Status_RUNNING
|
|
|
|
case "stopped":
|
|
|
|
status = container.Status_STOPPED
|
|
|
|
case "paused":
|
|
|
|
status = container.Status_PAUSED
|
|
|
|
}
|
2017-02-09 07:31:26 +00:00
|
|
|
o := &shimapi.StateResponse{
|
2017-01-26 23:09:59 +00:00
|
|
|
ID: s.id,
|
2017-02-06 22:57:43 +00:00
|
|
|
Bundle: s.bundle,
|
2017-02-13 18:23:28 +00:00
|
|
|
Pid: uint32(s.initProcess.Pid()),
|
2017-03-02 09:42:55 +00:00
|
|
|
Status: status,
|
2017-02-13 18:23:28 +00:00
|
|
|
Processes: []*container.Process{},
|
2017-01-26 23:09:59 +00:00
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, p := range s.processes {
|
2017-02-13 18:23:28 +00:00
|
|
|
status := container.Status_RUNNING
|
2017-01-26 23:09:59 +00:00
|
|
|
if err := syscall.Kill(p.Pid(), 0); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
status = container.Status_STOPPED
|
2017-01-26 23:09:59 +00:00
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
o.Processes = append(o.Processes, &container.Process{
|
|
|
|
Pid: uint32(p.Pid()),
|
|
|
|
Status: status,
|
2017-01-26 23:09:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Pause(ctx context.Context, r *shimapi.PauseRequest) (*google_protobuf.Empty, error) {
|
2017-02-01 21:25:28 +00:00
|
|
|
if err := s.initProcess.Pause(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
return empty, nil
|
2017-02-01 21:25:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func (s *Service) Resume(ctx context.Context, r *shimapi.ResumeRequest) (*google_protobuf.Empty, error) {
|
2017-02-01 21:25:28 +00:00
|
|
|
if err := s.initProcess.Resume(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
return empty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Exit(ctx context.Context, r *shimapi.ExitRequest) (*google_protobuf.Empty, error) {
|
|
|
|
// signal ourself to exit
|
|
|
|
if err := syscall.Kill(os.Getpid(), syscall.SIGTERM); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return empty, nil
|
2017-02-01 21:25:28 +00:00
|
|
|
}
|
|
|
|
|
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-02-13 18:23:28 +00:00
|
|
|
s.events <- &container.Event{
|
|
|
|
Type: container.Event_EXIT,
|
2017-01-25 21:27:48 +00:00
|
|
|
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
|
|
|
|
}
|