@@ -50,7 +50,7 @@ func (l *taskList) addWithNamespace(namespace string, t *Task) error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
id := t.containerID
|
||||
id := t.id
|
||||
if _, ok := l.tasks[namespace]; !ok {
|
||||
l.tasks[namespace] = make(map[string]*Task)
|
||||
}
|
||||
@@ -70,6 +70,6 @@ func (l *taskList) delete(ctx context.Context, t *Task) {
|
||||
}
|
||||
tasks, ok := l.tasks[namespace]
|
||||
if ok {
|
||||
delete(tasks, t.containerID)
|
||||
delete(tasks, t.id)
|
||||
}
|
||||
}
|
||||
|
||||
86
linux/process.go
Normal file
86
linux/process.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// +build linux
|
||||
|
||||
package linux
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/containerd/containerd/api/types/task"
|
||||
shim "github.com/containerd/containerd/linux/shim/v1"
|
||||
"github.com/containerd/containerd/runtime"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Process struct {
|
||||
id string
|
||||
t *Task
|
||||
}
|
||||
|
||||
func (p *Process) ID() string {
|
||||
return p.id
|
||||
}
|
||||
|
||||
func (p *Process) Kill(ctx context.Context, signal uint32, _ bool) error {
|
||||
_, err := p.t.shim.Kill(ctx, &shim.KillRequest{
|
||||
Signal: signal,
|
||||
ID: p.id,
|
||||
})
|
||||
if err != nil {
|
||||
err = errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) State(ctx context.Context) (runtime.State, error) {
|
||||
// use the container status for the status of the process
|
||||
response, err := p.t.shim.State(ctx, &shim.StateRequest{
|
||||
ID: p.id,
|
||||
})
|
||||
if err != nil {
|
||||
return runtime.State{}, errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
var status runtime.Status
|
||||
switch response.Status {
|
||||
case task.StatusCreated:
|
||||
status = runtime.CreatedStatus
|
||||
case task.StatusRunning:
|
||||
status = runtime.RunningStatus
|
||||
case task.StatusStopped:
|
||||
status = runtime.StoppedStatus
|
||||
case task.StatusPaused:
|
||||
status = runtime.PausedStatus
|
||||
// TODO: containerd.DeletedStatus
|
||||
}
|
||||
return runtime.State{
|
||||
Pid: response.Pid,
|
||||
Status: status,
|
||||
Stdin: response.Stdin,
|
||||
Stdout: response.Stdout,
|
||||
Stderr: response.Stderr,
|
||||
Terminal: response.Terminal,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Process) ResizePty(ctx context.Context, size runtime.ConsoleSize) error {
|
||||
_, err := p.t.shim.ResizePty(ctx, &shim.ResizePtyRequest{
|
||||
ID: p.id,
|
||||
Width: size.Width,
|
||||
Height: size.Height,
|
||||
})
|
||||
if err != nil {
|
||||
err = errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) CloseIO(ctx context.Context) error {
|
||||
_, err := p.t.shim.CloseIO(ctx, &shim.CloseIORequest{
|
||||
ID: p.id,
|
||||
Stdin: true,
|
||||
})
|
||||
if err != nil {
|
||||
err = errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -201,9 +201,9 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
|
||||
})
|
||||
}
|
||||
if err := r.emit(ctx, "/runtime/create", &eventsapi.RuntimeCreate{
|
||||
ID: id,
|
||||
Bundle: bundle.path,
|
||||
RootFS: runtimeMounts,
|
||||
ContainerID: id,
|
||||
Bundle: bundle.path,
|
||||
RootFS: runtimeMounts,
|
||||
IO: &eventsapi.RuntimeIO{
|
||||
Stdin: opts.IO.Stdin,
|
||||
Stdout: opts.IO.Stdout,
|
||||
@@ -239,14 +239,14 @@ func (r *Runtime) Delete(ctx context.Context, c runtime.Task) (*runtime.Exit, er
|
||||
r.tasks.delete(ctx, lc)
|
||||
|
||||
var (
|
||||
bundle = loadBundle(filepath.Join(r.root, namespace, lc.containerID), namespace)
|
||||
bundle = loadBundle(filepath.Join(r.root, namespace, lc.id), namespace)
|
||||
i = c.Info()
|
||||
)
|
||||
if err := r.emit(ctx, "/runtime/delete", &eventsapi.RuntimeDelete{
|
||||
ID: i.ID,
|
||||
Runtime: i.Runtime,
|
||||
ExitStatus: rsp.ExitStatus,
|
||||
ExitedAt: rsp.ExitedAt,
|
||||
ContainerID: i.ID,
|
||||
Runtime: i.Runtime,
|
||||
ExitStatus: rsp.ExitStatus,
|
||||
ExitedAt: rsp.ExitedAt,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -324,9 +324,9 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
|
||||
continue
|
||||
}
|
||||
o = append(o, &Task{
|
||||
containerID: id,
|
||||
shim: s,
|
||||
namespace: ns,
|
||||
id: id,
|
||||
shim: s,
|
||||
namespace: ns,
|
||||
})
|
||||
}
|
||||
return o, nil
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
type execProcess struct {
|
||||
sync.WaitGroup
|
||||
|
||||
id int
|
||||
id string
|
||||
console console.Console
|
||||
io runc.IO
|
||||
status int
|
||||
@@ -34,29 +34,27 @@ type execProcess struct {
|
||||
pid int
|
||||
closers []io.Closer
|
||||
stdin io.Closer
|
||||
stdio stdio
|
||||
|
||||
parent *initProcess
|
||||
|
||||
stdinPath string
|
||||
stdoutPath string
|
||||
stderrPath string
|
||||
terminal bool
|
||||
}
|
||||
|
||||
func newExecProcess(context context.Context, path string, r *shimapi.ExecProcessRequest, parent *initProcess, id int) (process, error) {
|
||||
func newExecProcess(context context.Context, path string, r *shimapi.ExecProcessRequest, parent *initProcess, id string) (process, error) {
|
||||
e := &execProcess{
|
||||
id: id,
|
||||
parent: parent,
|
||||
stdinPath: r.Stdin,
|
||||
stdoutPath: r.Stdout,
|
||||
stderrPath: r.Stderr,
|
||||
terminal: r.Terminal,
|
||||
id: id,
|
||||
parent: parent,
|
||||
stdio: stdio{
|
||||
stdin: r.Stdin,
|
||||
stdout: r.Stdout,
|
||||
stderr: r.Stderr,
|
||||
terminal: r.Terminal,
|
||||
},
|
||||
}
|
||||
var (
|
||||
err error
|
||||
socket *runc.Socket
|
||||
io runc.IO
|
||||
pidfile = filepath.Join(path, fmt.Sprintf("%d.pid", id))
|
||||
pidfile = filepath.Join(path, fmt.Sprintf("%s.pid", id))
|
||||
)
|
||||
if r.Terminal {
|
||||
if socket, err = runc.NewConsoleSocket(filepath.Join(path, "pty.sock")); err != nil {
|
||||
@@ -120,6 +118,10 @@ func newExecProcess(context context.Context, path string, r *shimapi.ExecProcess
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (e *execProcess) ID() string {
|
||||
return e.id
|
||||
}
|
||||
|
||||
func (e *execProcess) Pid() int {
|
||||
return e.pid
|
||||
}
|
||||
@@ -155,7 +157,7 @@ func (e *execProcess) Resize(ws console.WinSize) error {
|
||||
return e.console.Resize(ws)
|
||||
}
|
||||
|
||||
func (e *execProcess) Signal(sig int) error {
|
||||
func (e *execProcess) Kill(ctx context.Context, sig uint32, _ bool) error {
|
||||
if err := unix.Kill(e.pid, syscall.Signal(sig)); err != nil {
|
||||
return checkKillError(err)
|
||||
}
|
||||
@@ -165,3 +167,7 @@ func (e *execProcess) Signal(sig int) error {
|
||||
func (e *execProcess) Stdin() io.Closer {
|
||||
return e.stdin
|
||||
}
|
||||
|
||||
func (e *execProcess) Stdio() stdio {
|
||||
return e.stdio
|
||||
}
|
||||
|
||||
@@ -48,11 +48,7 @@ type initProcess struct {
|
||||
pid int
|
||||
closers []io.Closer
|
||||
stdin io.Closer
|
||||
|
||||
stdinPath string
|
||||
stdoutPath string
|
||||
stderrPath string
|
||||
terminal bool
|
||||
stdio stdio
|
||||
}
|
||||
|
||||
func newInitProcess(context context.Context, path, namespace string, r *shimapi.CreateTaskRequest) (*initProcess, error) {
|
||||
@@ -82,13 +78,15 @@ func newInitProcess(context context.Context, path, namespace string, r *shimapi.
|
||||
Root: filepath.Join(RuncRoot, namespace),
|
||||
}
|
||||
p := &initProcess{
|
||||
id: r.ID,
|
||||
bundle: r.Bundle,
|
||||
runtime: runtime,
|
||||
stdinPath: r.Stdin,
|
||||
stdoutPath: r.Stdout,
|
||||
stderrPath: r.Stderr,
|
||||
terminal: r.Terminal,
|
||||
id: r.ID,
|
||||
bundle: r.Bundle,
|
||||
runtime: runtime,
|
||||
stdio: stdio{
|
||||
stdin: r.Stdin,
|
||||
stdout: r.Stdout,
|
||||
stderr: r.Stderr,
|
||||
terminal: r.Terminal,
|
||||
},
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -171,6 +169,10 @@ func newInitProcess(context context.Context, path, namespace string, r *shimapi.
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *initProcess) ID() string {
|
||||
return p.id
|
||||
}
|
||||
|
||||
func (p *initProcess) Pid() int {
|
||||
return p.pid
|
||||
}
|
||||
@@ -257,10 +259,6 @@ func (p *initProcess) killAll(context context.Context) error {
|
||||
return p.runtimeError(err, "OCI runtime killall failed")
|
||||
}
|
||||
|
||||
func (p *initProcess) Signal(sig int) error {
|
||||
return checkKillError(unix.Kill(p.pid, syscall.Signal(sig)))
|
||||
}
|
||||
|
||||
func (p *initProcess) Stdin() io.Closer {
|
||||
return p.stdin
|
||||
}
|
||||
@@ -306,6 +304,10 @@ func (p *initProcess) Update(context context.Context, r *shimapi.UpdateTaskReque
|
||||
return p.runtime.Update(context, p.id, &resources)
|
||||
}
|
||||
|
||||
func (p *initProcess) Stdio() stdio {
|
||||
return p.stdio
|
||||
}
|
||||
|
||||
// TODO(mlaventure): move to runc package?
|
||||
func getLastRuntimeError(r *runc.Runc) (string, error) {
|
||||
if r.Log == "" {
|
||||
|
||||
@@ -60,7 +60,7 @@ func (c *local) Stream(ctx context.Context, in *shimapi.StreamEventsRequest, opt
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *local) State(ctx context.Context, in *google_protobuf.Empty, opts ...grpc.CallOption) (*shimapi.StateResponse, error) {
|
||||
func (c *local) State(ctx context.Context, in *shimapi.StateRequest, opts ...grpc.CallOption) (*shimapi.StateResponse, error) {
|
||||
return c.s.State(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,16 @@ import (
|
||||
"github.com/containerd/console"
|
||||
)
|
||||
|
||||
type stdio struct {
|
||||
stdin string
|
||||
stdout string
|
||||
stderr string
|
||||
terminal bool
|
||||
}
|
||||
|
||||
type process interface {
|
||||
// ID returns the id for the process
|
||||
ID() string
|
||||
// Pid returns the pid for the process
|
||||
Pid() int
|
||||
// Resize resizes the process console
|
||||
@@ -23,8 +32,10 @@ type process interface {
|
||||
ExitedAt() time.Time
|
||||
// Delete deletes the process and its resourcess
|
||||
Delete(context.Context) error
|
||||
// Signal directly signals the process
|
||||
Signal(int) error
|
||||
// Stdin returns the process STDIN
|
||||
Stdin() io.Closer
|
||||
// Kill kills the process
|
||||
Kill(context.Context, uint32, bool) error
|
||||
// Stdio returns io information for the container
|
||||
Stdio() stdio
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
|
||||
"github.com/containerd/console"
|
||||
events "github.com/containerd/containerd/api/services/events/v1"
|
||||
@@ -16,7 +18,6 @@ import (
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -34,7 +35,7 @@ func NewService(path, namespace string) (*Service, error) {
|
||||
}
|
||||
return &Service{
|
||||
path: path,
|
||||
processes: make(map[int]process),
|
||||
processes: make(map[string]process),
|
||||
events: make(chan *events.RuntimeEvent, 4096),
|
||||
namespace: namespace,
|
||||
}, nil
|
||||
@@ -46,34 +47,38 @@ type Service struct {
|
||||
id string
|
||||
bundle string
|
||||
mu sync.Mutex
|
||||
processes map[int]process
|
||||
processes map[string]process
|
||||
events chan *events.RuntimeEvent
|
||||
eventsMu sync.Mutex
|
||||
deferredEvent *events.RuntimeEvent
|
||||
execID int
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (*shimapi.CreateTaskResponse, error) {
|
||||
if r.ID == "" {
|
||||
return nil, grpc.Errorf(codes.InvalidArgument, "task id cannot be empty")
|
||||
}
|
||||
process, err := newInitProcess(ctx, s.path, s.namespace, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
// save the main task id and bundle to the shim for additional requests
|
||||
s.id = r.ID
|
||||
s.bundle = r.Bundle
|
||||
s.initProcess = process
|
||||
pid := process.Pid()
|
||||
s.processes[pid] = process
|
||||
s.processes[r.ID] = process
|
||||
s.mu.Unlock()
|
||||
cmd := &reaper.Cmd{
|
||||
ExitCh: make(chan int, 1),
|
||||
}
|
||||
reaper.Default.Register(pid, cmd)
|
||||
s.events <- &events.RuntimeEvent{
|
||||
Type: events.RuntimeEvent_CREATE,
|
||||
ID: r.ID,
|
||||
Pid: uint32(pid),
|
||||
Type: events.RuntimeEvent_CREATE,
|
||||
ID: r.ID,
|
||||
ContainerID: s.id,
|
||||
Pid: uint32(pid),
|
||||
}
|
||||
go s.waitExit(process, pid, cmd)
|
||||
return &shimapi.CreateTaskResponse{
|
||||
@@ -89,9 +94,10 @@ func (s *Service) Start(ctx context.Context, r *google_protobuf.Empty) (*google_
|
||||
return nil, err
|
||||
}
|
||||
s.events <- &events.RuntimeEvent{
|
||||
Type: events.RuntimeEvent_START,
|
||||
ID: s.id,
|
||||
Pid: uint32(s.initProcess.Pid()),
|
||||
Type: events.RuntimeEvent_START,
|
||||
ID: s.id,
|
||||
ContainerID: s.id,
|
||||
Pid: uint32(s.initProcess.Pid()),
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
@@ -104,7 +110,7 @@ func (s *Service) Delete(ctx context.Context, r *google_protobuf.Empty) (*shimap
|
||||
// TODO (@crosbymichael): how to handle errors here
|
||||
p.Delete(ctx)
|
||||
s.mu.Lock()
|
||||
delete(s.processes, p.Pid())
|
||||
delete(s.processes, p.ID())
|
||||
s.mu.Unlock()
|
||||
return &shimapi.DeleteResponse{
|
||||
ExitStatus: uint32(p.Status()),
|
||||
@@ -117,19 +123,19 @@ func (s *Service) DeleteProcess(ctx context.Context, r *shimapi.DeleteProcessReq
|
||||
if s.initProcess == nil {
|
||||
return nil, errors.New(ErrContainerNotCreated)
|
||||
}
|
||||
if int(r.Pid) == s.initProcess.pid {
|
||||
return nil, fmt.Errorf("cannot delete init process with DeleteProcess")
|
||||
if r.ID == s.initProcess.id {
|
||||
return nil, grpc.Errorf(codes.InvalidArgument, "cannot delete init process with DeleteProcess")
|
||||
}
|
||||
s.mu.Lock()
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
p, ok := s.processes[r.ID]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("process %d not found", r.Pid)
|
||||
return nil, fmt.Errorf("process %s not found", r.ID)
|
||||
}
|
||||
// TODO (@crosbymichael): how to handle errors here
|
||||
p.Delete(ctx)
|
||||
s.mu.Lock()
|
||||
delete(s.processes, p.Pid())
|
||||
delete(s.processes, p.ID())
|
||||
s.mu.Unlock()
|
||||
return &shimapi.DeleteResponse{
|
||||
ExitStatus: uint32(p.Status()),
|
||||
@@ -144,9 +150,8 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecProcessRequest) (*shi
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.execID++
|
||||
|
||||
process, err := newExecProcess(ctx, s.path, r, s.initProcess, s.execID)
|
||||
process, err := newExecProcess(ctx, s.path, r, s.initProcess, r.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -155,12 +160,13 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecProcessRequest) (*shi
|
||||
ExitCh: make(chan int, 1),
|
||||
}
|
||||
reaper.Default.Register(pid, cmd)
|
||||
s.processes[pid] = process
|
||||
s.processes[r.ID] = process
|
||||
|
||||
s.events <- &events.RuntimeEvent{
|
||||
Type: events.RuntimeEvent_EXEC_ADDED,
|
||||
ID: s.id,
|
||||
Pid: uint32(pid),
|
||||
Type: events.RuntimeEvent_EXEC_ADDED,
|
||||
ID: r.ID,
|
||||
ContainerID: s.id,
|
||||
Pid: uint32(pid),
|
||||
}
|
||||
go s.waitExit(process, pid, cmd)
|
||||
return &shimapi.ExecProcessResponse{
|
||||
@@ -169,18 +175,18 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecProcessRequest) (*shi
|
||||
}
|
||||
|
||||
func (s *Service) ResizePty(ctx context.Context, r *shimapi.ResizePtyRequest) (*google_protobuf.Empty, error) {
|
||||
if r.Pid == 0 {
|
||||
return nil, errors.Errorf("pid not provided in request")
|
||||
if r.ID == "" {
|
||||
return nil, grpc.Errorf(codes.InvalidArgument, "id not provided")
|
||||
}
|
||||
ws := console.WinSize{
|
||||
Width: uint16(r.Width),
|
||||
Height: uint16(r.Height),
|
||||
}
|
||||
s.mu.Lock()
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
p, ok := s.processes[r.ID]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, errors.Errorf("process does not exist %d", r.Pid)
|
||||
return nil, errors.Errorf("process does not exist %s", r.ID)
|
||||
}
|
||||
if err := p.Resize(ws); err != nil {
|
||||
return nil, err
|
||||
@@ -212,10 +218,14 @@ func (s *Service) Stream(r *shimapi.StreamEventsRequest, stream shimapi.Shim_Str
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) State(ctx context.Context, r *google_protobuf.Empty) (*shimapi.StateResponse, error) {
|
||||
func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.StateResponse, error) {
|
||||
if s.initProcess == nil {
|
||||
return nil, errors.New(ErrContainerNotCreated)
|
||||
}
|
||||
p, ok := s.processes[r.ID]
|
||||
if !ok {
|
||||
return nil, grpc.Errorf(codes.NotFound, "process id %s not found", r.ID)
|
||||
}
|
||||
st, err := s.initProcess.ContainerStatus(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -231,38 +241,17 @@ func (s *Service) State(ctx context.Context, r *google_protobuf.Empty) (*shimapi
|
||||
case "paused":
|
||||
status = task.StatusPaused
|
||||
}
|
||||
o := &shimapi.StateResponse{
|
||||
ID: s.id,
|
||||
Bundle: s.bundle,
|
||||
Pid: uint32(s.initProcess.Pid()),
|
||||
Status: status,
|
||||
Processes: []*task.Process{},
|
||||
Stdin: s.initProcess.stdinPath,
|
||||
Stdout: s.initProcess.stdoutPath,
|
||||
Stderr: s.initProcess.stderrPath,
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, p := range s.processes {
|
||||
status := task.StatusRunning
|
||||
if err := unix.Kill(p.Pid(), 0); err != nil {
|
||||
if err != syscall.ESRCH {
|
||||
return nil, err
|
||||
}
|
||||
status = task.StatusStopped
|
||||
}
|
||||
pp := &task.Process{
|
||||
Pid: uint32(p.Pid()),
|
||||
Status: status,
|
||||
}
|
||||
if ep, ok := p.(*execProcess); ok {
|
||||
pp.Stdin = ep.stdinPath
|
||||
pp.Stdout = ep.stdoutPath
|
||||
pp.Stderr = ep.stderrPath
|
||||
}
|
||||
o.Processes = append(o.Processes, pp)
|
||||
}
|
||||
return o, nil
|
||||
sio := p.Stdio()
|
||||
return &shimapi.StateResponse{
|
||||
ID: p.ID(),
|
||||
Bundle: s.bundle,
|
||||
Pid: uint32(p.Pid()),
|
||||
Status: status,
|
||||
Stdin: sio.stdin,
|
||||
Stdout: sio.stdout,
|
||||
Stderr: sio.stderr,
|
||||
Terminal: sio.terminal,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Pause(ctx context.Context, r *google_protobuf.Empty) (*google_protobuf.Empty, error) {
|
||||
@@ -289,35 +278,19 @@ func (s *Service) Kill(ctx context.Context, r *shimapi.KillRequest) (*google_pro
|
||||
if s.initProcess == nil {
|
||||
return nil, errors.New(ErrContainerNotCreated)
|
||||
}
|
||||
if r.Pid == 0 {
|
||||
if r.ID == "" {
|
||||
if err := s.initProcess.Kill(ctx, r.Signal, r.All); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
if int(r.Pid) == s.initProcess.pid {
|
||||
if err := s.initProcess.Kill(ctx, r.Signal, r.All); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
p, ok := s.processes[r.ID]
|
||||
if !ok {
|
||||
return nil, grpc.Errorf(codes.NotFound, "process id %s not found", r.ID)
|
||||
}
|
||||
pids, err := s.getContainerPids(ctx, s.initProcess.id)
|
||||
if err != nil {
|
||||
if err := p.Kill(ctx, r.Signal, r.All); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valid := false
|
||||
for _, p := range pids {
|
||||
if r.Pid == p {
|
||||
valid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
return nil, errors.Errorf("process %d does not exist in container", r.Pid)
|
||||
}
|
||||
if err := unix.Kill(int(r.Pid), syscall.Signal(r.Signal)); err != nil {
|
||||
return nil, checkKillError(err)
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
|
||||
@@ -332,9 +305,9 @@ func (s *Service) ListPids(ctx context.Context, r *shimapi.ListPidsRequest) (*sh
|
||||
}
|
||||
|
||||
func (s *Service) CloseIO(ctx context.Context, r *shimapi.CloseIORequest) (*google_protobuf.Empty, error) {
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
p, ok := s.processes[r.ID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
||||
return nil, grpc.Errorf(codes.NotFound, "process does not exist %s", r.ID)
|
||||
}
|
||||
if err := p.Stdin().Close(); err != nil {
|
||||
return nil, err
|
||||
@@ -374,11 +347,12 @@ func (s *Service) waitExit(p process, pid int, cmd *reaper.Cmd) {
|
||||
|
||||
reaper.Default.Delete(pid)
|
||||
s.events <- &events.RuntimeEvent{
|
||||
Type: events.RuntimeEvent_EXIT,
|
||||
ID: s.id,
|
||||
Pid: uint32(pid),
|
||||
ExitStatus: uint32(status),
|
||||
ExitedAt: p.ExitedAt(),
|
||||
Type: events.RuntimeEvent_EXIT,
|
||||
ID: p.ID(),
|
||||
ContainerID: s.id,
|
||||
Pid: uint32(pid),
|
||||
ExitStatus: uint32(status),
|
||||
ExitedAt: p.ExitedAt(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,11 +361,9 @@ func (s *Service) getContainerPids(ctx context.Context, id string) ([]uint32, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pids := make([]uint32, 0, len(p))
|
||||
for _, pid := range p {
|
||||
pids = append(pids, uint32(pid))
|
||||
}
|
||||
|
||||
return pids, nil
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -18,7 +18,7 @@ option go_package = "github.com/containerd/containerd/linux/shim/v1;shim";
|
||||
// for the container processes.
|
||||
service Shim {
|
||||
// State returns shim and task state information.
|
||||
rpc State(google.protobuf.Empty) returns (StateResponse);
|
||||
rpc State(StateRequest) returns (StateResponse);
|
||||
|
||||
rpc Create(CreateTaskRequest) returns (CreateTaskResponse);
|
||||
|
||||
@@ -77,15 +77,16 @@ message DeleteResponse {
|
||||
}
|
||||
|
||||
message DeleteProcessRequest {
|
||||
uint32 pid = 1;
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message ExecProcessRequest {
|
||||
bool terminal = 1;
|
||||
string stdin = 2;
|
||||
string stdout = 3;
|
||||
string stderr = 4;
|
||||
google.protobuf.Any spec = 5;
|
||||
string id = 1;
|
||||
bool terminal = 2;
|
||||
string stdin = 3;
|
||||
string stdout = 4;
|
||||
string stderr = 5;
|
||||
google.protobuf.Any spec = 6;
|
||||
}
|
||||
|
||||
message ExecProcessResponse {
|
||||
@@ -93,31 +94,34 @@ message ExecProcessResponse {
|
||||
}
|
||||
|
||||
message ResizePtyRequest {
|
||||
uint32 pid = 1;
|
||||
string id = 1;
|
||||
uint32 width = 2;
|
||||
uint32 height = 3;
|
||||
}
|
||||
|
||||
message StateRequest {
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message StateResponse {
|
||||
string id = 1;
|
||||
string bundle = 2;
|
||||
uint32 pid = 3;
|
||||
containerd.v1.types.Status status = 4;
|
||||
repeated containerd.v1.types.Process processes = 5;
|
||||
string stdin = 6;
|
||||
string stdout = 7;
|
||||
string stderr = 8;
|
||||
bool terminal = 9;
|
||||
string stdin = 5;
|
||||
string stdout = 6;
|
||||
string stderr = 7;
|
||||
bool terminal = 8;
|
||||
}
|
||||
|
||||
message KillRequest {
|
||||
uint32 signal = 1;
|
||||
bool all = 2;
|
||||
uint32 pid = 3;
|
||||
string id = 1;
|
||||
uint32 signal = 2;
|
||||
bool all = 3;
|
||||
}
|
||||
|
||||
message CloseIORequest {
|
||||
uint32 pid = 1;
|
||||
string id = 1;
|
||||
bool stdin = 2;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,25 +16,28 @@ import (
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
containerID string
|
||||
shim *client.Client
|
||||
namespace string
|
||||
id string
|
||||
shim *client.Client
|
||||
namespace string
|
||||
}
|
||||
|
||||
func newTask(id, namespace string, shim *client.Client) *Task {
|
||||
return &Task{
|
||||
containerID: id,
|
||||
shim: shim,
|
||||
namespace: namespace,
|
||||
id: id,
|
||||
shim: shim,
|
||||
namespace: namespace,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) ID() string {
|
||||
return t.id
|
||||
}
|
||||
|
||||
func (t *Task) Info() runtime.TaskInfo {
|
||||
return runtime.TaskInfo{
|
||||
ID: t.containerID,
|
||||
ContainerID: t.containerID,
|
||||
Runtime: pluginID,
|
||||
Namespace: t.namespace,
|
||||
ID: t.id,
|
||||
Runtime: pluginID,
|
||||
Namespace: t.namespace,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +50,9 @@ func (t *Task) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (t *Task) State(ctx context.Context) (runtime.State, error) {
|
||||
response, err := t.shim.State(ctx, empty)
|
||||
response, err := t.shim.State(ctx, &shim.StateRequest{
|
||||
ID: t.id,
|
||||
})
|
||||
if err != nil {
|
||||
return runtime.State{}, errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
@@ -89,10 +94,10 @@ func (t *Task) Resume(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Task) Kill(ctx context.Context, signal uint32, pid uint32, all bool) error {
|
||||
func (t *Task) Kill(ctx context.Context, signal uint32, all bool) error {
|
||||
_, err := t.shim.Kill(ctx, &shim.KillRequest{
|
||||
ID: t.id,
|
||||
Signal: signal,
|
||||
Pid: pid,
|
||||
All: all,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -101,28 +106,28 @@ func (t *Task) Kill(ctx context.Context, signal uint32, pid uint32, all bool) er
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Task) Exec(ctx context.Context, opts runtime.ExecOpts) (runtime.Process, error) {
|
||||
func (t *Task) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.Process, error) {
|
||||
request := &shim.ExecProcessRequest{
|
||||
ID: id,
|
||||
Stdin: opts.IO.Stdin,
|
||||
Stdout: opts.IO.Stdout,
|
||||
Stderr: opts.IO.Stderr,
|
||||
Terminal: opts.IO.Terminal,
|
||||
Spec: opts.Spec,
|
||||
}
|
||||
resp, err := t.shim.Exec(ctx, request)
|
||||
if err != nil {
|
||||
if _, err := t.shim.Exec(ctx, request); err != nil {
|
||||
return nil, errors.New(grpc.ErrorDesc(err))
|
||||
|
||||
}
|
||||
return &Process{
|
||||
pid: int(resp.Pid),
|
||||
t: t,
|
||||
id: id,
|
||||
t: t,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *Task) Pids(ctx context.Context) ([]uint32, error) {
|
||||
resp, err := t.shim.ListPids(ctx, &shim.ListPidsRequest{
|
||||
ID: t.containerID,
|
||||
// TODO: (@crosbymichael) this id can probably be removed
|
||||
ID: t.id,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(grpc.ErrorDesc(err))
|
||||
@@ -130,9 +135,9 @@ func (t *Task) Pids(ctx context.Context) ([]uint32, error) {
|
||||
return resp.Pids, nil
|
||||
}
|
||||
|
||||
func (t *Task) ResizePty(ctx context.Context, pid uint32, size runtime.ConsoleSize) error {
|
||||
func (t *Task) ResizePty(ctx context.Context, size runtime.ConsoleSize) error {
|
||||
_, err := t.shim.ResizePty(ctx, &shim.ResizePtyRequest{
|
||||
Pid: pid,
|
||||
ID: t.id,
|
||||
Width: size.Width,
|
||||
Height: size.Height,
|
||||
})
|
||||
@@ -142,9 +147,9 @@ func (t *Task) ResizePty(ctx context.Context, pid uint32, size runtime.ConsoleSi
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Task) CloseIO(ctx context.Context, pid uint32) error {
|
||||
func (t *Task) CloseIO(ctx context.Context) error {
|
||||
_, err := t.shim.CloseIO(ctx, &shim.CloseIORequest{
|
||||
Pid: pid,
|
||||
ID: t.id,
|
||||
Stdin: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -164,9 +169,9 @@ func (t *Task) Checkpoint(ctx context.Context, path string, options *types.Any)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) DeleteProcess(ctx context.Context, pid uint32) (*runtime.Exit, error) {
|
||||
func (t *Task) DeleteProcess(ctx context.Context, id string) (*runtime.Exit, error) {
|
||||
r, err := t.shim.DeleteProcess(ctx, &shim.DeleteProcessRequest{
|
||||
Pid: pid,
|
||||
ID: id,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(grpc.ErrorDesc(err))
|
||||
@@ -174,7 +179,7 @@ func (t *Task) DeleteProcess(ctx context.Context, pid uint32) (*runtime.Exit, er
|
||||
return &runtime.Exit{
|
||||
Status: r.ExitStatus,
|
||||
Timestamp: r.ExitedAt,
|
||||
Pid: pid,
|
||||
Pid: r.Pid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -185,28 +190,10 @@ func (t *Task) Update(ctx context.Context, resources *types.Any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
type Process struct {
|
||||
pid int
|
||||
t *Task
|
||||
}
|
||||
|
||||
func (p *Process) Kill(ctx context.Context, signal uint32, _ bool) error {
|
||||
_, err := p.t.shim.Kill(ctx, &shim.KillRequest{
|
||||
Signal: signal,
|
||||
Pid: uint32(p.pid),
|
||||
})
|
||||
if err != nil {
|
||||
err = errors.New(grpc.ErrorDesc(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) State(ctx context.Context) (runtime.State, error) {
|
||||
// use the container status for the status of the process
|
||||
state, err := p.t.State(ctx)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
state.Pid = uint32(p.pid)
|
||||
return state, nil
|
||||
func (t *Task) Process(ctx context.Context, id string) (runtime.Process, error) {
|
||||
// TODO: verify process exists for container
|
||||
return &Process{
|
||||
id: id,
|
||||
t: t,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user