diff --git a/libcontainer/process.go b/libcontainer/process.go index 3a81484cb..9661df80a 100644 --- a/libcontainer/process.go +++ b/libcontainer/process.go @@ -5,7 +5,6 @@ import ( "io" "math" "os" - "syscall" ) type processOperations interface { @@ -86,47 +85,6 @@ type IO struct { Stderr io.ReadCloser } -// InitializeIO creates pipes for use with the process's STDIO -// and returns the opposite side for each -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.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.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.Stderr, i.Stderr = w, r - // change ownership of the pipes incase 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 -} - // NewConsole creates new console for process and returns it func (p *Process) NewConsole(rootuid int) (Console, error) { console, err := NewConsole(rootuid, rootuid) diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index f27b6cf40..114c71b3b 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -320,3 +320,44 @@ func getPipeFds(pid int) ([]string, error) { } return fds, nil } + +// InitializeIO creates pipes for use with the process's STDIO +// and returns the opposite side for each +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.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.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.Stderr, i.Stderr = w, r + // change ownership of the pipes incase 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 +}