Skip to content

Commit

Permalink
cio: should not open fifo for stderr if terminal
Browse files Browse the repository at this point in the history
fix: #4342

Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Jul 2, 2020
1 parent 468d4e1 commit 68b736d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cio/io_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func openFifos(ctx context.Context, fifos *FIFOSet) (pipes, error) {
}
}()
}
if fifos.Stderr != "" {
if !fifos.Terminal && fifos.Stderr != "" {
if f.Stderr, err = fifo.OpenFifo(ctx, fifos.Stderr, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); err != nil {
return f, errors.Wrapf(err, "failed to open stderr fifo")
}
Expand Down
46 changes: 46 additions & 0 deletions cio/io_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package cio

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -55,3 +58,46 @@ func TestOpenFifos(t *testing.T) {
assert.Assert(t, err != nil, scenario)
}
}

// TestOpenFifosWithTerminal tests openFifos should not open stderr if terminal
// is set.
func TestOpenFifosWithTerminal(t *testing.T) {
var ctx, cancel = context.WithCancel(context.Background())
defer cancel()

ioFifoDir, err := ioutil.TempDir("", fmt.Sprintf("cio-%s", t.Name()))
if err != nil {
t.Fatalf("unexpected error during creating temp dir: %v", err)
}
defer os.RemoveAll(ioFifoDir)

cfg := Config{
Stdout: filepath.Join(ioFifoDir, "test-stdout"),
Stderr: filepath.Join(ioFifoDir, "test-stderr"),
}

// Without terminal, pipes.Stderr should not be nil
{
p, err := openFifos(ctx, NewFIFOSet(cfg, nil))
if err != nil {
t.Fatalf("unexpected error during openFifos: %v", err)
}

if p.Stderr == nil {
t.Fatalf("unexpected empty stderr pipe")
}
}

// With terminal, pipes.Stderr should be nil
{
cfg.Terminal = true
p, err := openFifos(ctx, NewFIFOSet(cfg, nil))
if err != nil {
t.Fatalf("unexpected error during openFifos: %v", err)
}

if p.Stderr != nil {
t.Fatalf("unexpected stderr pipe")
}
}
}
12 changes: 8 additions & 4 deletions container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,15 @@ func (f *directIO) Cancel() {
// Close closes all open fds
func (f *directIO) Close() error {
err := f.Stdin.Close()
if err2 := f.Stdout.Close(); err == nil {
err = err2
if f.Stdout != nil {
if err2 := f.Stdout.Close(); err == nil {
err = err2
}
}
if err2 := f.Stderr.Close(); err == nil {
err = err2
if f.Stderr != nil {
if err2 := f.Stderr.Close(); err == nil {
err = err2
}
}
return err
}
Expand Down

0 comments on commit 68b736d

Please sign in to comment.