github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/libcontainerd/io_unix.go (about)

     1  // +build !windows
     2  
     3  package libcontainerd
     4  
     5  import (
     6  	"context"
     7  	"io"
     8  	"syscall"
     9  
    10  	"github.com/containerd/containerd/cio"
    11  	"github.com/containerd/fifo"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  func newIOPipe(fifos *cio.FIFOSet) (*IOPipe, error) {
    16  	var (
    17  		err         error
    18  		ctx, cancel = context.WithCancel(context.Background())
    19  		f           io.ReadWriteCloser
    20  		iop         = &IOPipe{
    21  			Terminal: fifos.Terminal,
    22  			cancel:   cancel,
    23  			config: cio.Config{
    24  				Terminal: fifos.Terminal,
    25  				Stdin:    fifos.In,
    26  				Stdout:   fifos.Out,
    27  				Stderr:   fifos.Err,
    28  			},
    29  		}
    30  	)
    31  	defer func() {
    32  		if err != nil {
    33  			cancel()
    34  			iop.Close()
    35  		}
    36  	}()
    37  
    38  	if fifos.In != "" {
    39  		if f, err = fifo.OpenFifo(ctx, fifos.In, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); err != nil {
    40  			return nil, errors.WithStack(err)
    41  		}
    42  		iop.Stdin = f
    43  	}
    44  
    45  	if fifos.Out != "" {
    46  		if f, err = fifo.OpenFifo(ctx, fifos.Out, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); err != nil {
    47  			return nil, errors.WithStack(err)
    48  		}
    49  		iop.Stdout = f
    50  	}
    51  
    52  	if fifos.Err != "" {
    53  		if f, err = fifo.OpenFifo(ctx, fifos.Err, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); err != nil {
    54  			return nil, errors.WithStack(err)
    55  		}
    56  		iop.Stderr = f
    57  	}
    58  
    59  	return iop, nil
    60  }