github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/cmd/fd_mapping.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/ttpreport/gvisor-ligolo/runsc/boot"
    23  )
    24  
    25  // fdMappings can be used with flags that appear multiple times.
    26  type fdMappings []boot.FDMapping
    27  
    28  // String implements flag.Value.
    29  func (i *fdMappings) String() string {
    30  	return fmt.Sprintf("%v", *i)
    31  }
    32  
    33  // Get implements flag.Value.
    34  func (i *fdMappings) Get() any {
    35  	return i
    36  }
    37  
    38  // GetArray returns an array of mappings.
    39  func (i *fdMappings) GetArray() []boot.FDMapping {
    40  	return *i
    41  }
    42  
    43  // Set implements flag.Value and appends a mapping from the command line to the
    44  // mappings array.
    45  func (i *fdMappings) Set(s string) error {
    46  	split := strings.Split(s, ":")
    47  	if len(split) != 2 {
    48  		// Split returns a slice of length 1 if its first argument does not
    49  		// contain the separator. An additional length check is not necessary.
    50  		// In case no separator is used and the argument is a valid integer, we
    51  		// assume that host FD and guest FD should be identical.
    52  		fd, err := strconv.Atoi(split[0])
    53  		if err != nil {
    54  			return fmt.Errorf("invalid flag value: must be an integer or a mapping of format M:N")
    55  		}
    56  		*i = append(*i, boot.FDMapping{
    57  			Host:  fd,
    58  			Guest: fd,
    59  		})
    60  		return nil
    61  	}
    62  
    63  	fdHost, err := strconv.Atoi(split[0])
    64  	if err != nil {
    65  		return fmt.Errorf("invalid flag host value: %v", err)
    66  	}
    67  	if fdHost < 0 {
    68  		return fmt.Errorf("flag host value must be >= 0: %d", fdHost)
    69  	}
    70  
    71  	fdGuest, err := strconv.Atoi(split[1])
    72  	if err != nil {
    73  		return fmt.Errorf("invalid flag guest value: %v", err)
    74  	}
    75  	if fdGuest < 0 {
    76  		return fmt.Errorf("flag guest value must be >= 0: %d", fdGuest)
    77  	}
    78  
    79  	*i = append(*i, boot.FDMapping{
    80  		Host:  fdHost,
    81  		Guest: fdGuest,
    82  	})
    83  	return nil
    84  }