github.com/demonoid81/containerd@v1.3.4/runtime/v1/shim/service_unix.go (about)

     1  // +build !windows,!linux
     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 shim
    20  
    21  import (
    22  	"context"
    23  	"io"
    24  	"sync"
    25  	"syscall"
    26  
    27  	"github.com/containerd/console"
    28  	"github.com/containerd/fifo"
    29  )
    30  
    31  type unixPlatform struct {
    32  }
    33  
    34  func (p *unixPlatform) CopyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string, wg *sync.WaitGroup) (console.Console, error) {
    35  	var cwg sync.WaitGroup
    36  	if stdin != "" {
    37  		in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  		cwg.Add(1)
    42  		go func() {
    43  			cwg.Done()
    44  			p := bufPool.Get().(*[]byte)
    45  			defer bufPool.Put(p)
    46  
    47  			io.CopyBuffer(console, in, *p)
    48  		}()
    49  	}
    50  	outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	wg.Add(1)
    59  	cwg.Add(1)
    60  	go func() {
    61  		cwg.Done()
    62  		p := bufPool.Get().(*[]byte)
    63  		defer bufPool.Put(p)
    64  
    65  		io.CopyBuffer(outw, console, *p)
    66  		console.Close()
    67  		outr.Close()
    68  		outw.Close()
    69  		wg.Done()
    70  	}()
    71  	cwg.Wait()
    72  	return console, nil
    73  }
    74  
    75  func (p *unixPlatform) ShutdownConsole(ctx context.Context, cons console.Console) error {
    76  	return nil
    77  }
    78  
    79  func (p *unixPlatform) Close() error {
    80  	return nil
    81  }
    82  
    83  func (s *Service) initPlatform() error {
    84  	s.platform = &unixPlatform{}
    85  	return nil
    86  }