github.com/containerd/nerdctl@v1.7.7/pkg/cioutil/container_io_windows.go (about)

     1  //go:build windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package cioutil
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"os/exec"
    25  
    26  	"github.com/Microsoft/go-winio"
    27  	"github.com/containerd/containerd/cio"
    28  	"github.com/containerd/log"
    29  )
    30  
    31  // copyIO is from https://github.com/containerd/containerd/blob/148d21b1ae0718b75718a09ecb307bb874270f59/cio/io_windows.go#L44
    32  func copyIO(_ *exec.Cmd, fifos *cio.FIFOSet, ioset *cio.Streams) (_ *ncio, retErr error) {
    33  	ncios := &ncio{cmd: nil, config: fifos.Config}
    34  
    35  	defer func() {
    36  		if retErr != nil {
    37  			_ = ncios.Close()
    38  		}
    39  	}()
    40  
    41  	if fifos.Stdin != "" {
    42  		l, err := winio.ListenPipe(fifos.Stdin, nil)
    43  		if err != nil {
    44  			return nil, fmt.Errorf("failed to create stdin pipe %s: %w", fifos.Stdin, err)
    45  		}
    46  		ncios.closers = append(ncios.closers, l)
    47  
    48  		go func() {
    49  			c, err := l.Accept()
    50  			if err != nil {
    51  				log.L.WithError(err).Errorf("failed to accept stdin connection on %s", fifos.Stdin)
    52  				return
    53  			}
    54  
    55  			p := bufPool.Get().(*[]byte)
    56  			defer bufPool.Put(p)
    57  
    58  			io.CopyBuffer(c, ioset.Stdin, *p)
    59  			c.Close()
    60  			l.Close()
    61  		}()
    62  	}
    63  
    64  	if fifos.Stdout != "" {
    65  		l, err := winio.ListenPipe(fifos.Stdout, nil)
    66  		if err != nil {
    67  			return nil, fmt.Errorf("failed to create stdout pipe %s: %w", fifos.Stdout, err)
    68  		}
    69  		ncios.closers = append(ncios.closers, l)
    70  
    71  		go func() {
    72  			c, err := l.Accept()
    73  			if err != nil {
    74  				log.L.WithError(err).Errorf("failed to accept stdout connection on %s", fifos.Stdout)
    75  				return
    76  			}
    77  
    78  			p := bufPool.Get().(*[]byte)
    79  			defer bufPool.Put(p)
    80  
    81  			io.CopyBuffer(ioset.Stdout, c, *p)
    82  			c.Close()
    83  			l.Close()
    84  		}()
    85  	}
    86  
    87  	if fifos.Stderr != "" {
    88  		l, err := winio.ListenPipe(fifos.Stderr, nil)
    89  		if err != nil {
    90  			return nil, fmt.Errorf("failed to create stderr pipe %s: %w", fifos.Stderr, err)
    91  		}
    92  		ncios.closers = append(ncios.closers, l)
    93  
    94  		go func() {
    95  			c, err := l.Accept()
    96  			if err != nil {
    97  				log.L.WithError(err).Errorf("failed to accept stderr connection on %s", fifos.Stderr)
    98  				return
    99  			}
   100  
   101  			p := bufPool.Get().(*[]byte)
   102  			defer bufPool.Put(p)
   103  
   104  			io.CopyBuffer(ioset.Stderr, c, *p)
   105  			c.Close()
   106  			l.Close()
   107  		}()
   108  	}
   109  
   110  	return ncios, nil
   111  }