2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-24 00:18:10 +00:00
|
|
|
|
2017-01-24 22:45:18 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2017-03-21 21:12:23 +00:00
|
|
|
"encoding/json"
|
2017-01-24 22:45:18 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-01-24 22:45:18 +00:00
|
|
|
runc "github.com/crosbymichael/go-runc"
|
2017-02-09 07:31:26 +00:00
|
|
|
shimapi "github.com/docker/containerd/api/services/shim"
|
2017-01-24 22:45:18 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type execProcess struct {
|
|
|
|
sync.WaitGroup
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
id int
|
2017-01-26 19:29:19 +00:00
|
|
|
console console.Console
|
2017-01-24 22:45:18 +00:00
|
|
|
io runc.IO
|
|
|
|
status int
|
|
|
|
pid int
|
|
|
|
|
|
|
|
parent *initProcess
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:28:21 +00:00
|
|
|
func newExecProcess(context context.Context, path string, r *shimapi.ExecRequest, parent *initProcess, id int) (process, error) {
|
2017-01-24 22:45:18 +00:00
|
|
|
e := &execProcess{
|
2017-01-26 23:09:59 +00:00
|
|
|
id: id,
|
2017-01-24 22:45:18 +00:00
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
var (
|
2017-03-10 23:28:21 +00:00
|
|
|
err error
|
2017-01-24 22:45:18 +00:00
|
|
|
socket *runc.ConsoleSocket
|
|
|
|
io runc.IO
|
2017-03-10 23:28:21 +00:00
|
|
|
pidfile = filepath.Join(path, fmt.Sprintf("%d.pid", id))
|
2017-01-24 22:45:18 +00:00
|
|
|
)
|
|
|
|
if r.Terminal {
|
2017-03-10 23:28:21 +00:00
|
|
|
if socket, err = runc.NewConsoleSocket(filepath.Join(path, "pty.sock")); err != nil {
|
2017-01-24 22:45:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 19:29:19 +00:00
|
|
|
defer os.Remove(socket.Path())
|
2017-01-24 22:45:18 +00:00
|
|
|
} else {
|
|
|
|
// TODO: get uid/gid
|
|
|
|
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.io = io
|
|
|
|
}
|
|
|
|
opts := &runc.ExecOpts{
|
|
|
|
PidFile: pidfile,
|
|
|
|
ConsoleSocket: socket,
|
|
|
|
IO: io,
|
|
|
|
Detach: true,
|
|
|
|
}
|
2017-03-21 21:12:23 +00:00
|
|
|
|
|
|
|
// process exec request
|
|
|
|
var spec specs.Process
|
|
|
|
if err := json.Unmarshal(r.Spec.Value, &spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
spec.Terminal = r.Terminal
|
|
|
|
|
|
|
|
if err := parent.runc.Exec(context, parent.id, spec, opts); err != nil {
|
2017-01-24 22:45:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-27 00:07:43 +00:00
|
|
|
if socket != nil {
|
|
|
|
console, err := socket.ReceiveMaster()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.console = console
|
|
|
|
if err := copyConsole(context, console, r.Stdin, r.Stdout, r.Stderr, &e.WaitGroup); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := copyPipes(context, io, r.Stdin, r.Stdout, r.Stderr, &e.WaitGroup); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
pid, err := runc.ReadPidFile(opts.PidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.pid = pid
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func rlimits(rr []*shimapi.Rlimit) (o []specs.LinuxRlimit) {
|
2017-01-24 22:45:18 +00:00
|
|
|
for _, r := range rr {
|
|
|
|
o = append(o, specs.LinuxRlimit{
|
|
|
|
Type: r.Type,
|
|
|
|
Hard: r.Hard,
|
|
|
|
Soft: r.Soft,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Pid() int {
|
|
|
|
return e.pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Status() int {
|
|
|
|
return e.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Exited(status int) {
|
|
|
|
e.status = status
|
|
|
|
e.Wait()
|
2017-01-26 19:29:19 +00:00
|
|
|
if e.io != nil {
|
|
|
|
e.io.Close()
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
func (e *execProcess) Delete(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
func (e *execProcess) Resize(ws console.WinSize) error {
|
2017-01-24 22:45:18 +00:00
|
|
|
if e.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.console.Resize(ws)
|
2017-01-24 00:18:10 +00:00
|
|
|
}
|