2017-01-23 22:03:14 +00:00
|
|
|
package runc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IO interface {
|
|
|
|
io.Closer
|
|
|
|
Stdin() io.WriteCloser
|
|
|
|
Stdout() io.ReadCloser
|
|
|
|
Stderr() io.ReadCloser
|
|
|
|
Set(*exec.Cmd)
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:52:05 +00:00
|
|
|
type StartCloser interface {
|
|
|
|
CloseAfterStart() error
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:03:14 +00:00
|
|
|
// NewPipeIO creates pipe pairs to be used with runc
|
|
|
|
func NewPipeIO(uid, gid int) (i IO, err error) {
|
|
|
|
var pipes []*pipe
|
|
|
|
// cleanup in case of an error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, p := range pipes {
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
stdin, err := newPipe(uid, gid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pipes = append(pipes, stdin)
|
|
|
|
|
|
|
|
stdout, err := newPipe(uid, gid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pipes = append(pipes, stdout)
|
|
|
|
|
|
|
|
stderr, err := newPipe(uid, gid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pipes = append(pipes, stderr)
|
|
|
|
|
|
|
|
return &pipeIO{
|
|
|
|
in: stdin,
|
|
|
|
out: stdout,
|
|
|
|
err: stderr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPipe(uid, gid int) (*pipe, error) {
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := syscall.Fchown(int(r.Fd()), uid, gid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := syscall.Fchown(int(w.Fd()), uid, gid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pipe{
|
|
|
|
r: r,
|
|
|
|
w: w,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipe struct {
|
|
|
|
r *os.File
|
|
|
|
w *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pipe) Close() error {
|
|
|
|
err := p.r.Close()
|
|
|
|
if werr := p.w.Close(); err == nil {
|
|
|
|
err = werr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeIO struct {
|
|
|
|
in *pipe
|
|
|
|
out *pipe
|
|
|
|
err *pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *pipeIO) Stdin() io.WriteCloser {
|
|
|
|
return i.in.w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *pipeIO) Stdout() io.ReadCloser {
|
2017-01-25 19:52:05 +00:00
|
|
|
return i.out.r
|
2017-01-23 22:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *pipeIO) Stderr() io.ReadCloser {
|
2017-01-25 19:52:05 +00:00
|
|
|
return i.err.r
|
2017-01-23 22:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *pipeIO) Close() error {
|
|
|
|
var err error
|
|
|
|
for _, v := range []*pipe{
|
|
|
|
i.in,
|
|
|
|
i.out,
|
|
|
|
i.err,
|
|
|
|
} {
|
|
|
|
if cerr := v.Close(); err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:52:05 +00:00
|
|
|
func (i *pipeIO) CloseAfterStart() error {
|
|
|
|
for _, f := range []*os.File{
|
|
|
|
i.out.w,
|
|
|
|
i.err.w,
|
|
|
|
} {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:03:14 +00:00
|
|
|
// Set sets the io to the exec.Cmd
|
|
|
|
func (i *pipeIO) Set(cmd *exec.Cmd) {
|
|
|
|
cmd.Stdin = i.in.r
|
|
|
|
cmd.Stdout = i.out.w
|
|
|
|
cmd.Stderr = i.err.w
|
|
|
|
}
|
2017-01-25 19:52:05 +00:00
|
|
|
|
|
|
|
func NewSTDIO() (IO, error) {
|
|
|
|
return &stdio{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stdio struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Set(cmd *exec.Cmd) {
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Stdin() io.WriteCloser {
|
|
|
|
return os.Stdin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Stdout() io.ReadCloser {
|
|
|
|
return os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Stderr() io.ReadCloser {
|
|
|
|
return os.Stderr
|
|
|
|
}
|