2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-12 18:29:37 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-24 00:18:10 +00:00
|
|
|
"context"
|
2017-01-12 18:29:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-01-24 00:18:10 +00:00
|
|
|
"sync"
|
2017-01-12 18:29:37 +00:00
|
|
|
"syscall"
|
|
|
|
|
2017-01-24 00:18:10 +00:00
|
|
|
runc "github.com/crosbymichael/go-runc"
|
2017-01-12 18:29:37 +00:00
|
|
|
"github.com/tonistiigi/fifo"
|
|
|
|
)
|
|
|
|
|
2017-01-24 00:18:10 +00:00
|
|
|
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)
|
2017-01-12 18:29:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
go io.Copy(console, in)
|
|
|
|
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0)
|
2017-01-12 18:29:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
io.Copy(outw, console)
|
|
|
|
console.Close()
|
|
|
|
outr.Close()
|
|
|
|
outw.Close()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
|
2017-01-12 18:29:37 +00:00
|
|
|
for name, dest := range map[string]func(wc io.WriteCloser, rc io.Closer){
|
2017-01-24 00:18:10 +00:00
|
|
|
stdout: func(wc io.WriteCloser, rc io.Closer) {
|
|
|
|
wg.Add(1)
|
2017-01-12 18:29:37 +00:00
|
|
|
go func() {
|
2017-01-24 00:18:10 +00:00
|
|
|
io.Copy(wc, rio.Stdout())
|
|
|
|
wg.Done()
|
2017-01-12 18:29:37 +00:00
|
|
|
wc.Close()
|
|
|
|
rc.Close()
|
|
|
|
}()
|
|
|
|
},
|
2017-01-24 00:18:10 +00:00
|
|
|
stderr: func(wc io.WriteCloser, rc io.Closer) {
|
|
|
|
wg.Add(1)
|
2017-01-12 18:29:37 +00:00
|
|
|
go func() {
|
2017-01-24 00:18:10 +00:00
|
|
|
io.Copy(wc, rio.Stderr())
|
|
|
|
wg.Done()
|
2017-01-12 18:29:37 +00:00
|
|
|
wc.Close()
|
|
|
|
rc.Close()
|
|
|
|
}()
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
fw, err := fifo.OpenFifo(ctx, name, syscall.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err)
|
|
|
|
}
|
|
|
|
fr, err := fifo.OpenFifo(ctx, name, syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err)
|
|
|
|
}
|
|
|
|
dest(fw, fr)
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
|
2017-01-12 18:29:37 +00:00
|
|
|
if err != nil {
|
2017-01-24 00:18:10 +00:00
|
|
|
return fmt.Errorf("containerd-shim: opening %s failed: %s", stdin, err)
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
|
|
|
go func() {
|
2017-01-24 00:18:10 +00:00
|
|
|
io.Copy(rio.Stdin(), f)
|
|
|
|
rio.Stdin().Close()
|
2017-01-12 18:29:37 +00:00
|
|
|
f.Close()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|