Update go-runc to 7b66c5da30493c5eb9c655cab67ba88071891ac5

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-01-23 14:03:14 -08:00
parent 911bc57483
commit 6e9e0a895a
2 changed files with 124 additions and 33 deletions

121
vendor/github.com/crosbymichael/go-runc/io.go generated vendored Normal file
View file

@ -0,0 +1,121 @@
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)
}
// 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 {
return i.in.r
}
func (i *pipeIO) Stderr() io.ReadCloser {
return i.in.r
}
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
}
// 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
}

View file

@ -73,36 +73,6 @@ type CreateOpts struct {
NoNewKeyring bool NoNewKeyring bool
} }
type IO struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
func (i *IO) Close() error {
var err error
for _, v := range []interface{}{
i.Stdin,
i.Stderr,
i.Stdout,
} {
if v != nil {
if c, ok := v.(io.Closer); ok {
if cerr := c.Close(); err == nil {
err = cerr
}
}
}
}
return err
}
func (o IO) setSTDIO(cmd *exec.Cmd) {
cmd.Stdin = o.Stdin
cmd.Stdout = o.Stdout
cmd.Stderr = o.Stderr
}
func (o *CreateOpts) args() (out []string) { func (o *CreateOpts) args() (out []string) {
if o.PidFile != "" { if o.PidFile != "" {
out = append(out, "--pid-file", o.PidFile) out = append(out, "--pid-file", o.PidFile)
@ -130,7 +100,7 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
} }
cmd := r.command(context, append(args, id)...) cmd := r.command(context, append(args, id)...)
if opts != nil { if opts != nil {
opts.setSTDIO(cmd) opts.Set(cmd)
} }
return runOrError(cmd) return runOrError(cmd)
} }
@ -190,7 +160,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
} }
cmd := r.command(context, append(args, id)...) cmd := r.command(context, append(args, id)...)
if opts != nil { if opts != nil {
opts.setSTDIO(cmd) opts.Set(cmd)
} }
return runOrError(cmd) return runOrError(cmd)
} }
@ -204,7 +174,7 @@ func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts)
} }
cmd := r.command(context, append(args, id)...) cmd := r.command(context, append(args, id)...)
if opts != nil { if opts != nil {
opts.setSTDIO(cmd) opts.Set(cmd)
} }
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return -1, err return -1, err