Refactor shim terminal and io handling
This also finishes the service implementation of the shim behind GRPC Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
6e9e0a895a
commit
d5d2e586cd
12 changed files with 547 additions and 630 deletions
38
cmd/containerd-shim/checkpoint.go
Normal file
38
cmd/containerd-shim/checkpoint.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type checkpoint struct {
|
||||
// Timestamp is the time that checkpoint happened
|
||||
Created time.Time `json:"created"`
|
||||
// Name is the name of the checkpoint
|
||||
Name string `json:"name"`
|
||||
// TCP checkpoints open tcp connections
|
||||
TCP bool `json:"tcp"`
|
||||
// UnixSockets persists unix sockets in the checkpoint
|
||||
UnixSockets bool `json:"unixSockets"`
|
||||
// Shell persists tty sessions in the checkpoint
|
||||
Shell bool `json:"shell"`
|
||||
// Exit exits the container after the checkpoint is finished
|
||||
Exit bool `json:"exit"`
|
||||
// EmptyNS tells CRIU not to restore a particular namespace
|
||||
EmptyNS []string `json:"emptyNS,omitempty"`
|
||||
}
|
||||
|
||||
func loadCheckpoint(checkpointPath string) (*checkpoint, error) {
|
||||
f, err := os.Open(filepath.Join(checkpointPath, "config.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
var cpt checkpoint
|
||||
if err := json.NewDecoder(f).Decode(&cpt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cpt, nil
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
// +build !solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// NewConsole returns an initialized console that can be used within a container by copying bytes
|
||||
// from the master side to the slave that is attached as the tty for the container's init process.
|
||||
func newConsole(uid, gid int) (*os.File, string, error) {
|
||||
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if err = saneTerminal(master); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
console, err := ptsname(master)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if err := unlockpt(master); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if err := os.Chmod(console, 0600); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if err := os.Chown(console, uid, gid); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return master, console, nil
|
||||
}
|
||||
|
||||
// saneTerminal sets the necessary tty_ioctl(4)s to ensure that a pty pair
|
||||
// created by us acts normally. In particular, a not-very-well-known default of
|
||||
// Linux unix98 ptys is that they have +onlcr by default. While this isn't a
|
||||
// problem for terminal emulators, because we relay data from the terminal we
|
||||
// also relay that funky line discipline.
|
||||
func saneTerminal(terminal *os.File) error {
|
||||
// Go doesn't have a wrapper for any of the termios ioctls.
|
||||
var termios syscall.Termios
|
||||
|
||||
if err := ioctl(terminal.Fd(), syscall.TCGETS, uintptr(unsafe.Pointer(&termios))); err != nil {
|
||||
return fmt.Errorf("ioctl(tty, tcgets): %s", err.Error())
|
||||
}
|
||||
|
||||
// Set -onlcr so we don't have to deal with \r.
|
||||
termios.Oflag &^= syscall.ONLCR
|
||||
|
||||
if err := ioctl(terminal.Fd(), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != nil {
|
||||
return fmt.Errorf("ioctl(tty, tcsets): %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ioctl(fd uintptr, flag, data uintptr) error {
|
||||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
|
||||
// unlockpt should be called before opening the slave side of a pty.
|
||||
func unlockpt(f *os.File) error {
|
||||
var u int32
|
||||
return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
||||
}
|
||||
|
||||
// ptsname retrieves the name of the first available pts for the given master.
|
||||
func ptsname(f *os.File) (string, error) {
|
||||
var n int32
|
||||
if err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("/dev/pts/%d", n), nil
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
// +build solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
// NewConsole returns an initialized console that can be used within a container by copying bytes
|
||||
// from the master side to the slave that is attached as the tty for the container's init process.
|
||||
func newConsole(uid, gid int) (*os.File, string, error) {
|
||||
return nil, "", errors.New("newConsole not implemented on Solaris")
|
||||
}
|
5
cmd/containerd-shim/exec.go
Normal file
5
cmd/containerd-shim/exec.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package main
|
||||
|
||||
func newExecProcess(id, bundle, runtimeName string) (process, error) {
|
||||
return nil, nil
|
||||
}
|
122
cmd/containerd-shim/init.go
Normal file
122
cmd/containerd-shim/init.go
Normal file
|
@ -0,0 +1,122 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
)
|
||||
|
||||
type initProcess struct {
|
||||
sync.WaitGroup
|
||||
|
||||
id string
|
||||
bundle string
|
||||
console *runc.Console
|
||||
io runc.IO
|
||||
runc *runc.Runc
|
||||
status int
|
||||
pid int
|
||||
}
|
||||
|
||||
func newInitProcess(context context.Context, r *shim.CreateRequest) (process, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtime := &runc.Runc{
|
||||
Command: r.Runtime,
|
||||
Log: filepath.Join(cwd, "log.json"),
|
||||
LogFormat: runc.JSON,
|
||||
PdeathSignal: syscall.SIGKILL,
|
||||
}
|
||||
p := &initProcess{
|
||||
id: r.ID,
|
||||
bundle: r.Bundle,
|
||||
runc: runtime,
|
||||
}
|
||||
var (
|
||||
socket *runc.ConsoleSocket
|
||||
io runc.IO
|
||||
)
|
||||
if r.Terminal {
|
||||
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// TODO: get uid/gid
|
||||
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
opts := &runc.CreateOpts{
|
||||
PidFile: filepath.Join(cwd, "pid"),
|
||||
ConsoleSocket: socket,
|
||||
IO: io,
|
||||
NoPivot: r.NoPivot,
|
||||
}
|
||||
if err := p.runc.Create(context, r.ID, r.Bundle, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if socket != nil {
|
||||
console, err := socket.ReceiveMaster()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.console = console
|
||||
if err := copyConsole(context, console, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := copyPipes(context, io, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
pid, err := runc.ReadPidFile(opts.PidFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.pid = pid
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *initProcess) Pid() int {
|
||||
return p.pid
|
||||
}
|
||||
|
||||
func (p *initProcess) Status() int {
|
||||
return p.status
|
||||
}
|
||||
|
||||
func (p *initProcess) Start(context context.Context) error {
|
||||
return p.runc.Start(context, p.id)
|
||||
}
|
||||
|
||||
func (p *initProcess) Exited(status int) {
|
||||
p.status = status
|
||||
}
|
||||
|
||||
func (p *initProcess) Delete(context context.Context) error {
|
||||
p.killAll(context)
|
||||
p.Wait()
|
||||
err := p.runc.Delete(context, p.id)
|
||||
p.io.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *initProcess) Resize(ws runc.WinSize) error {
|
||||
if p.console == nil {
|
||||
return nil
|
||||
}
|
||||
return p.console.Resize(ws)
|
||||
}
|
||||
|
||||
func (p *initProcess) killAll(context context.Context) error {
|
||||
return p.runc.Kill(context, p.id, int(syscall.SIGKILL), &runc.KillOpts{
|
||||
All: true,
|
||||
})
|
||||
}
|
|
@ -52,7 +52,9 @@ func main() {
|
|||
}
|
||||
var (
|
||||
server = grpc.NewServer()
|
||||
sv = &service{}
|
||||
sv = &service{
|
||||
processes: make(map[int]process),
|
||||
}
|
||||
)
|
||||
shim.RegisterShimServiceServer(server, sv)
|
||||
l, err := utils.CreateUnixSocket("shim.sock")
|
||||
|
|
|
@ -1,301 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
)
|
||||
|
||||
var errRuntime = errors.New("shim: runtime execution error")
|
||||
|
||||
type checkpoint struct {
|
||||
// Timestamp is the time that checkpoint happened
|
||||
Created time.Time `json:"created"`
|
||||
// Name is the name of the checkpoint
|
||||
Name string `json:"name"`
|
||||
// TCP checkpoints open tcp connections
|
||||
TCP bool `json:"tcp"`
|
||||
// UnixSockets persists unix sockets in the checkpoint
|
||||
UnixSockets bool `json:"unixSockets"`
|
||||
// Shell persists tty sessions in the checkpoint
|
||||
Shell bool `json:"shell"`
|
||||
// Exit exits the container after the checkpoint is finished
|
||||
Exit bool `json:"exit"`
|
||||
// EmptyNS tells CRIU not to restore a particular namespace
|
||||
EmptyNS []string `json:"emptyNS,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
type processState struct {
|
||||
Terminal bool `json:"terminal"`
|
||||
Exec bool `json:"exec"`
|
||||
Stdin string `json:"containerdStdin"`
|
||||
Stdout string `json:"containerdStdout"`
|
||||
Stderr string `json:"containerdStderr"`
|
||||
RuntimeArgs []string `json:"runtimeArgs"`
|
||||
|
||||
NoPivotRoot bool `json:"noPivotRoot"`
|
||||
CheckpointPath string `json:"checkpoint"`
|
||||
RootUID int `json:"rootUID"`
|
||||
RootGID int `json:"rootGID"`
|
||||
}
|
||||
*/
|
||||
|
||||
type process struct {
|
||||
sync.WaitGroup
|
||||
id string
|
||||
bundle string
|
||||
stdio *stdio
|
||||
exec bool
|
||||
containerPid int
|
||||
checkpoint *checkpoint
|
||||
checkpointPath string
|
||||
shimIO *IO
|
||||
stdinCloser io.Closer
|
||||
console *os.File
|
||||
consolePath string
|
||||
runtime string
|
||||
exitStatus int
|
||||
Stdin string `json:"containerdStdin"`
|
||||
Stdout string `json:"containerdStdout"`
|
||||
Stderr string `json:"containerdStderr"`
|
||||
Terminal bool `json:"terminal"`
|
||||
RootUID int `json:"rootUID"`
|
||||
RootGID int `json:"rootGID"`
|
||||
}
|
||||
|
||||
func newProcess(id, bundle, runtimeName string) (*process, error) {
|
||||
p := &process{
|
||||
id: id,
|
||||
bundle: bundle,
|
||||
runtime: runtimeName,
|
||||
}
|
||||
if err := p.openIO(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func loadCheckpoint(checkpointPath string) (*checkpoint, error) {
|
||||
f, err := os.Open(filepath.Join(checkpointPath, "config.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
var cpt checkpoint
|
||||
if err := json.NewDecoder(f).Decode(&cpt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cpt, nil
|
||||
}
|
||||
|
||||
func (p *process) create(isExec bool) error {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logPath := filepath.Join(cwd, "log.json")
|
||||
args := []string{
|
||||
"--log", logPath,
|
||||
"--log-format", "json",
|
||||
}
|
||||
if isExec {
|
||||
args = append(args, "exec",
|
||||
"-d",
|
||||
"--process", filepath.Join(cwd, "process.json"),
|
||||
"--console-socket", p.consolePath,
|
||||
)
|
||||
} else if p.checkpoint != nil {
|
||||
args = append(args, "restore",
|
||||
"-d",
|
||||
"--image-path", p.checkpointPath,
|
||||
"--work-path", filepath.Join(p.checkpointPath, "criu.work", "restore-"+time.Now().Format(time.RFC3339)),
|
||||
)
|
||||
add := func(flags ...string) {
|
||||
args = append(args, flags...)
|
||||
}
|
||||
if p.checkpoint.Shell {
|
||||
add("--shell-job")
|
||||
}
|
||||
if p.checkpoint.TCP {
|
||||
add("--tcp-established")
|
||||
}
|
||||
if p.checkpoint.UnixSockets {
|
||||
add("--ext-unix-sk")
|
||||
}
|
||||
/*
|
||||
if p.state.NoPivotRoot {
|
||||
add("--no-pivot")
|
||||
}
|
||||
*/
|
||||
for _, ns := range p.checkpoint.EmptyNS {
|
||||
add("--empty-ns", ns)
|
||||
}
|
||||
|
||||
} else {
|
||||
args = append(args, "create",
|
||||
"--bundle", p.bundle,
|
||||
"--console-socket", p.consolePath,
|
||||
)
|
||||
/*
|
||||
if p.state.NoPivotRoot {
|
||||
args = append(args, "--no-pivot")
|
||||
}
|
||||
*/
|
||||
}
|
||||
args = append(args,
|
||||
"--pid-file", filepath.Join(cwd, "pid"),
|
||||
p.id,
|
||||
)
|
||||
cmd := exec.Command(p.runtime, args...)
|
||||
cmd.Dir = p.bundle
|
||||
if p.stdio != nil {
|
||||
cmd.Stdin = p.stdio.stdin
|
||||
cmd.Stdout = p.stdio.stdout
|
||||
cmd.Stderr = p.stdio.stderr
|
||||
}
|
||||
// Call out to setPDeathSig to set SysProcAttr as elements are platform specific
|
||||
cmd.SysProcAttr = setPDeathSig()
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
if exErr, ok := err.(*exec.Error); ok {
|
||||
if exErr.Err == exec.ErrNotFound || exErr.Err == os.ErrNotExist {
|
||||
return fmt.Errorf("%s not installed on system", p.runtime)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
if runtime.GOOS != "solaris" {
|
||||
// Since current logic dictates that we need a pid at the end of p.create
|
||||
// we need to call runtime start as well on Solaris hence we need the
|
||||
// pipes to stay open.
|
||||
if p.stdio != nil {
|
||||
p.stdio.stdout.Close()
|
||||
p.stdio.stderr.Close()
|
||||
}
|
||||
}
|
||||
if err := cmd.Wait(); err != nil {
|
||||
if _, ok := err.(*exec.ExitError); ok {
|
||||
return errRuntime
|
||||
}
|
||||
return err
|
||||
}
|
||||
data, err := ioutil.ReadFile("pid")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pid, err := strconv.Atoi(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.containerPid = pid
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process) pid() int {
|
||||
return p.containerPid
|
||||
}
|
||||
|
||||
func (p *process) delete() error {
|
||||
cmd := exec.Command(p.runtime, "delete", p.id)
|
||||
cmd.SysProcAttr = setPDeathSig()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", out, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IO holds all 3 standard io Reader/Writer (stdin,stdout,stderr)
|
||||
type IO struct {
|
||||
Stdin io.WriteCloser
|
||||
Stdout io.ReadCloser
|
||||
Stderr io.ReadCloser
|
||||
}
|
||||
|
||||
func (p *process) initializeIO(rootuid int) (i *IO, err error) {
|
||||
var fds []uintptr
|
||||
i = &IO{}
|
||||
// cleanup in case of an error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
for _, fd := range fds {
|
||||
syscall.Close(int(fd))
|
||||
}
|
||||
}
|
||||
}()
|
||||
// STDIN
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fds = append(fds, r.Fd(), w.Fd())
|
||||
p.stdio.stdin, i.Stdin = r, w
|
||||
// STDOUT
|
||||
if r, w, err = os.Pipe(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fds = append(fds, r.Fd(), w.Fd())
|
||||
p.stdio.stdout, i.Stdout = w, r
|
||||
// STDERR
|
||||
if r, w, err = os.Pipe(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fds = append(fds, r.Fd(), w.Fd())
|
||||
p.stdio.stderr, i.Stderr = w, r
|
||||
// change ownership of the pipes in case we are in a user namespace
|
||||
for _, fd := range fds {
|
||||
if err := syscall.Fchown(int(fd), rootuid, rootuid); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (p *process) Close() error {
|
||||
if p.stdio == nil {
|
||||
return nil
|
||||
}
|
||||
return p.stdio.Close()
|
||||
}
|
||||
|
||||
func (p *process) start() error {
|
||||
cmd := exec.Command(p.runtime, "start", p.id)
|
||||
cmd.SysProcAttr = setPDeathSig()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", out, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process) setExited(status int) {
|
||||
p.exitStatus = status
|
||||
}
|
||||
|
||||
type stdio struct {
|
||||
stdin *os.File
|
||||
stdout *os.File
|
||||
stderr *os.File
|
||||
}
|
||||
|
||||
func (s *stdio) Close() error {
|
||||
err := s.stdin.Close()
|
||||
if oerr := s.stdout.Close(); err == nil {
|
||||
err = oerr
|
||||
}
|
||||
if oerr := s.stderr.Close(); err == nil {
|
||||
err = oerr
|
||||
}
|
||||
return err
|
||||
type process interface {
|
||||
// Pid returns the pid for the process
|
||||
Pid() int
|
||||
// Start starts the user's defined process inside
|
||||
Start(context.Context) error
|
||||
// Delete deletes the process and closes all open pipes
|
||||
Delete(context.Context) error
|
||||
// Resize resizes the process console
|
||||
Resize(ws runc.WinSize) error
|
||||
// Exited sets the exit status for the process
|
||||
Exited(status int)
|
||||
// Status returns the exit status
|
||||
Status() int
|
||||
}
|
||||
|
|
|
@ -1,95 +1,57 @@
|
|||
// +build !solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/tonistiigi/fifo"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// setPDeathSig sets the parent death signal to SIGKILL so that if the
|
||||
// shim dies the container process also dies.
|
||||
func setPDeathSig() *syscall.SysProcAttr {
|
||||
return &syscall.SysProcAttr{
|
||||
Pdeathsig: syscall.SIGKILL,
|
||||
func copyConsole(ctx context.Context, console *runc.Console, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
|
||||
in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go io.Copy(console, in)
|
||||
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(outw, console)
|
||||
console.Close()
|
||||
outr.Close()
|
||||
outw.Close()
|
||||
wg.Done()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// openIO opens the pre-created fifo's for use with the container
|
||||
// in RDWR so that they remain open if the other side stops listening
|
||||
func (p *process) openIO() error {
|
||||
return nil
|
||||
p.stdio = &stdio{}
|
||||
var (
|
||||
uid = p.RootUID
|
||||
gid = p.RootGID
|
||||
)
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
|
||||
stdinCloser, err := fifo.OpenFifo(ctx, p.Stdin, syscall.O_WRONLY|syscall.O_NONBLOCK, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.stdinCloser = stdinCloser
|
||||
|
||||
if p.Terminal {
|
||||
master, console, err := newConsole(uid, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.console = master
|
||||
p.consolePath = console
|
||||
stdin, err := fifo.OpenFifo(ctx, p.Stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go io.Copy(master, stdin)
|
||||
stdoutw, err := fifo.OpenFifo(ctx, p.Stdout, syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stdoutr, err := fifo.OpenFifo(ctx, p.Stdout, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Add(1)
|
||||
go func() {
|
||||
io.Copy(stdoutw, master)
|
||||
master.Close()
|
||||
stdoutr.Close()
|
||||
stdoutw.Close()
|
||||
p.Done()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
i, err := p.initializeIO(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.shimIO = i
|
||||
// non-tty
|
||||
func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
|
||||
for name, dest := range map[string]func(wc io.WriteCloser, rc io.Closer){
|
||||
p.Stdout: func(wc io.WriteCloser, rc io.Closer) {
|
||||
p.Add(1)
|
||||
stdout: func(wc io.WriteCloser, rc io.Closer) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(wc, i.Stdout)
|
||||
p.Done()
|
||||
io.Copy(wc, rio.Stdout())
|
||||
wg.Done()
|
||||
wc.Close()
|
||||
rc.Close()
|
||||
}()
|
||||
},
|
||||
p.Stderr: func(wc io.WriteCloser, rc io.Closer) {
|
||||
p.Add(1)
|
||||
stderr: func(wc io.WriteCloser, rc io.Closer) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(wc, i.Stderr)
|
||||
p.Done()
|
||||
io.Copy(wc, rio.Stderr())
|
||||
wg.Done()
|
||||
wc.Close()
|
||||
rc.Close()
|
||||
}()
|
||||
|
@ -106,25 +68,14 @@ func (p *process) openIO() error {
|
|||
dest(fw, fr)
|
||||
}
|
||||
|
||||
f, err := fifo.OpenFifo(ctx, p.Stdin, syscall.O_RDONLY, 0)
|
||||
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("containerd-shim: opening %s failed: %s", p.Stdin, err)
|
||||
return fmt.Errorf("containerd-shim: opening %s failed: %s", stdin, err)
|
||||
}
|
||||
go func() {
|
||||
io.Copy(i.Stdin, f)
|
||||
i.Stdin.Close()
|
||||
io.Copy(rio.Stdin(), f)
|
||||
rio.Stdin().Close()
|
||||
f.Close()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process) killAll() error {
|
||||
cmd := exec.Command(p.runtime, "kill", "--all", p.id, "SIGKILL")
|
||||
cmd.SysProcAttr = setPDeathSig()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", out, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
// +build solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// setPDeathSig is a no-op on Solaris as Pdeathsig is not defined.
|
||||
func setPDeathSig() *syscall.SysProcAttr {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Update to using fifo's package in openIO. Need to
|
||||
// 1. Merge and vendor changes in the package to use sys/unix.
|
||||
// 2. Figure out why context.Background is timing out.
|
||||
// openIO opens the pre-created fifo's for use with the container
|
||||
// in RDWR so that they remain open if the other side stops listening
|
||||
func (p *process) openIO() error {
|
||||
p.stdio = &stdio{}
|
||||
var (
|
||||
uid = p.state.RootUID
|
||||
)
|
||||
i, err := p.initializeIO(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.shimIO = i
|
||||
// Both tty and non-tty mode are handled by the runtime using
|
||||
// the following pipes
|
||||
for name, dest := range map[string]func(f *os.File){
|
||||
p.state.Stdout: func(f *os.File) {
|
||||
p.Add(1)
|
||||
go func() {
|
||||
io.Copy(f, i.Stdout)
|
||||
p.Done()
|
||||
}()
|
||||
},
|
||||
p.state.Stderr: func(f *os.File) {
|
||||
p.Add(1)
|
||||
go func() {
|
||||
io.Copy(f, i.Stderr)
|
||||
p.Done()
|
||||
}()
|
||||
},
|
||||
} {
|
||||
f, err := os.OpenFile(name, syscall.O_RDWR, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dest(f)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(p.state.Stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
io.Copy(i.Stdin, f)
|
||||
i.Stdin.Close()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process) killAll() error {
|
||||
return nil
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
"github.com/docker/containerd/utils"
|
||||
"github.com/docker/docker/pkg/term"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
@ -11,43 +14,51 @@ import (
|
|||
var emptyResponse = &google_protobuf.Empty{}
|
||||
|
||||
type service struct {
|
||||
init *process
|
||||
initPid int
|
||||
mu sync.Mutex
|
||||
processes map[int]process
|
||||
}
|
||||
|
||||
func (s *service) Create(ctx context.Context, r *shim.CreateRequest) (*shim.CreateResponse, error) {
|
||||
process, err := newProcess(r.ID, r.Bundle, r.Runtime)
|
||||
process, err := newInitProcess(ctx, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.init = process
|
||||
if err := process.create(false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
pid := process.Pid()
|
||||
s.initPid, s.processes[pid] = pid, process
|
||||
s.mu.Unlock()
|
||||
return &shim.CreateResponse{
|
||||
Pid: uint32(process.pid()),
|
||||
Pid: uint32(pid),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_protobuf.Empty, error) {
|
||||
if err := s.init.start(); err != nil {
|
||||
s.mu.Lock()
|
||||
p := s.processes[s.initPid]
|
||||
s.mu.Unlock()
|
||||
if err := p.Start(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.DeleteResponse, error) {
|
||||
// TODO: error when container has not stopped
|
||||
err := s.init.killAll()
|
||||
s.init.Wait()
|
||||
if derr := s.init.delete(); err == nil {
|
||||
err = derr
|
||||
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 cerr := s.init.Close(); err == nil {
|
||||
err = cerr
|
||||
if err := p.Delete(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
delete(s.processes, int(r.Pid))
|
||||
s.mu.Unlock()
|
||||
return &shim.DeleteResponse{
|
||||
ExitStatus: uint32(s.init.exitStatus),
|
||||
}, err
|
||||
ExitStatus: uint32(p.Status()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResponse, error) {
|
||||
|
@ -55,22 +66,27 @@ func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResp
|
|||
}
|
||||
|
||||
func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf.Empty, error) {
|
||||
if s.init.console == nil {
|
||||
return emptyResponse, nil
|
||||
}
|
||||
ws := term.Winsize{
|
||||
ws := runc.WinSize{
|
||||
Width: uint16(r.Width),
|
||||
Height: uint16(r.Height),
|
||||
}
|
||||
if err := term.SetWinsize(s.init.console.Fd(), &ws); err != nil {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) processExited(e utils.Exit) error {
|
||||
if s.init.pid() == e.Pid {
|
||||
s.init.setExited(e.Status)
|
||||
s.mu.Lock()
|
||||
if p, ok := s.processes[e.Pid]; ok {
|
||||
p.Exited(e.Status)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue