...

Source file src/github.com/Microsoft/hcsshim/internal/uvm/pipes.go

Documentation: github.com/Microsoft/hcsshim/internal/uvm

     1  //go:build windows
     2  
     3  package uvm
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/Microsoft/hcsshim/internal/hcs/resourcepaths"
    11  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    12  	"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
    13  	specs "github.com/opencontainers/runtime-spec/specs-go"
    14  )
    15  
    16  const pipePrefix = `\\.\pipe\`
    17  
    18  // PipeMount contains the host path for pipe mount
    19  type PipeMount struct {
    20  	// UVM the resource belongs to
    21  	vm       *UtilityVM
    22  	HostPath string
    23  }
    24  
    25  // Release frees the resources of the corresponding pipe Mount
    26  func (pipe *PipeMount) Release(ctx context.Context) error {
    27  	if err := pipe.vm.RemovePipe(ctx, pipe.HostPath); err != nil {
    28  		return fmt.Errorf("failed to remove pipe mount: %s", err)
    29  	}
    30  	return nil
    31  }
    32  
    33  // AddPipe shares a named pipe into the UVM.
    34  func (uvm *UtilityVM) AddPipe(ctx context.Context, hostPath string) (*PipeMount, error) {
    35  	modification := &hcsschema.ModifySettingRequest{
    36  		RequestType:  guestrequest.RequestTypeAdd,
    37  		ResourcePath: fmt.Sprintf(resourcepaths.MappedPipeResourceFormat, hostPath),
    38  	}
    39  	if err := uvm.modify(ctx, modification); err != nil {
    40  		return nil, err
    41  	}
    42  	return &PipeMount{uvm, hostPath}, nil
    43  }
    44  
    45  // RemovePipe removes a shared named pipe from the UVM.
    46  func (uvm *UtilityVM) RemovePipe(ctx context.Context, hostPath string) error {
    47  	modification := &hcsschema.ModifySettingRequest{
    48  		RequestType:  guestrequest.RequestTypeRemove,
    49  		ResourcePath: fmt.Sprintf(resourcepaths.MappedPipeResourceFormat, hostPath),
    50  	}
    51  	if err := uvm.modify(ctx, modification); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  // IsPipe returns true if the given path references a named pipe.
    58  func IsPipe(hostPath string) bool {
    59  	return strings.HasPrefix(hostPath, pipePrefix)
    60  }
    61  
    62  // GetContainerPipeMapping returns the source and destination to use for a given
    63  // pipe mount in a container.
    64  func GetContainerPipeMapping(uvm *UtilityVM, mount specs.Mount) (src string, dst string) {
    65  	if uvm == nil {
    66  		src = mount.Source
    67  	} else {
    68  		src = vsmbSharePrefix + `IPC$\` + strings.TrimPrefix(mount.Source, pipePrefix)
    69  	}
    70  	dst = strings.TrimPrefix(mount.Destination, pipePrefix)
    71  	return src, dst
    72  }
    73  

View as plain text