Skip to content

Commit ba1568d

Browse files
committed
Merge pull request opencontainers#436 from calavera/linux_process
Move linux only Process.InitializeIO behind the linux build flag.
2 parents 334b935 + 77c36f4 commit ba1568d

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

libcontainer/process.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66
"math"
77
"os"
8-
"syscall"
98
)
109

1110
type processOperations interface {
@@ -86,47 +85,6 @@ type IO struct {
8685
Stderr io.ReadCloser
8786
}
8887

89-
// InitializeIO creates pipes for use with the process's STDIO
90-
// and returns the opposite side for each
91-
func (p *Process) InitializeIO(rootuid int) (i *IO, err error) {
92-
var fds []uintptr
93-
i = &IO{}
94-
// cleanup in case of an error
95-
defer func() {
96-
if err != nil {
97-
for _, fd := range fds {
98-
syscall.Close(int(fd))
99-
}
100-
}
101-
}()
102-
// STDIN
103-
r, w, err := os.Pipe()
104-
if err != nil {
105-
return nil, err
106-
}
107-
fds = append(fds, r.Fd(), w.Fd())
108-
p.Stdin, i.Stdin = r, w
109-
// STDOUT
110-
if r, w, err = os.Pipe(); err != nil {
111-
return nil, err
112-
}
113-
fds = append(fds, r.Fd(), w.Fd())
114-
p.Stdout, i.Stdout = w, r
115-
// STDERR
116-
if r, w, err = os.Pipe(); err != nil {
117-
return nil, err
118-
}
119-
fds = append(fds, r.Fd(), w.Fd())
120-
p.Stderr, i.Stderr = w, r
121-
// change ownership of the pipes incase we are in a user namespace
122-
for _, fd := range fds {
123-
if err := syscall.Fchown(int(fd), rootuid, rootuid); err != nil {
124-
return nil, err
125-
}
126-
}
127-
return i, nil
128-
}
129-
13088
// NewConsole creates new console for process and returns it
13189
func (p *Process) NewConsole(rootuid int) (Console, error) {
13290
console, err := NewConsole(rootuid, rootuid)

libcontainer/process_linux.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,44 @@ func getPipeFds(pid int) ([]string, error) {
320320
}
321321
return fds, nil
322322
}
323+
324+
// InitializeIO creates pipes for use with the process's STDIO
325+
// and returns the opposite side for each
326+
func (p *Process) InitializeIO(rootuid int) (i *IO, err error) {
327+
var fds []uintptr
328+
i = &IO{}
329+
// cleanup in case of an error
330+
defer func() {
331+
if err != nil {
332+
for _, fd := range fds {
333+
syscall.Close(int(fd))
334+
}
335+
}
336+
}()
337+
// STDIN
338+
r, w, err := os.Pipe()
339+
if err != nil {
340+
return nil, err
341+
}
342+
fds = append(fds, r.Fd(), w.Fd())
343+
p.Stdin, i.Stdin = r, w
344+
// STDOUT
345+
if r, w, err = os.Pipe(); err != nil {
346+
return nil, err
347+
}
348+
fds = append(fds, r.Fd(), w.Fd())
349+
p.Stdout, i.Stdout = w, r
350+
// STDERR
351+
if r, w, err = os.Pipe(); err != nil {
352+
return nil, err
353+
}
354+
fds = append(fds, r.Fd(), w.Fd())
355+
p.Stderr, i.Stderr = w, r
356+
// change ownership of the pipes incase we are in a user namespace
357+
for _, fd := range fds {
358+
if err := syscall.Fchown(int(fd), rootuid, rootuid); err != nil {
359+
return nil, err
360+
}
361+
}
362+
return i, nil
363+
}

0 commit comments

Comments
 (0)