github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/pipe_supported.go (about)

     1  //go:build linux
     2  
     3  // Copyright (c) 2015-2023 MinIO, Inc.
     4  //
     5  // This file is part of MinIO Object Storage stack
     6  //
     7  // This program is free software: you can redistribute it and/or modify
     8  // it under the terms of the GNU Affero General Public License as published by
     9  // the Free Software Foundation, either version 3 of the License, or
    10  // (at your option) any later version.
    11  //
    12  // This program is distributed in the hope that it will be useful
    13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  // GNU Affero General Public License for more details.
    16  //
    17  // You should have received a copy of the GNU Affero General Public License
    18  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  
    20  package cmd
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"strconv"
    26  	"strings"
    27  
    28  	"golang.org/x/sys/unix"
    29  )
    30  
    31  const pipeMaxSizeProcFile = "/proc/sys/fs/pipe-max-size"
    32  
    33  func setPipeSize(fd uintptr, size int) error {
    34  	_, err := unix.FcntlInt(fd, unix.F_SETPIPE_SZ, size)
    35  	return err
    36  }
    37  
    38  func getConfiguredMaxPipeSize() (int, error) {
    39  	b, err := os.ReadFile(pipeMaxSizeProcFile)
    40  	if err != nil {
    41  		return 0, err
    42  	}
    43  	maxSize, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
    44  	if err != nil {
    45  		return 0, fmt.Errorf("error parsing %s content: %v", pipeMaxSizeProcFile, err)
    46  	}
    47  	return int(maxSize), nil
    48  }
    49  
    50  // increasePipeBufferSize attempts to increase the pipe size to the the input value
    51  // or system-max, if the provided size is 0 or less.
    52  func increasePipeBufferSize(f *os.File, desiredPipeSize int) error {
    53  	fd := f.Fd()
    54  
    55  	if desiredPipeSize <= 0 {
    56  		maxSize, err := getConfiguredMaxPipeSize()
    57  		if err == nil {
    58  			setPipeSize(fd, maxSize)
    59  			return nil
    60  		}
    61  	}
    62  
    63  	return setPipeSize(fd, desiredPipeSize)
    64  }