golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/buildlet/buildlet_rlimit.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build netbsd || openbsd
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"log"
    12  	"os"
    13  	"runtime"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  func init() {
    19  	switch runtime.GOOS {
    20  	case "netbsd":
    21  		setOSRlimit = setNetBSDRlimit
    22  	case "openbsd":
    23  		setOSRlimit = setOpenBSDRlimit
    24  	}
    25  }
    26  
    27  // setNetBSDRlimit sets limits for NetBSD.
    28  // See https://github.com/golang/go/issues/22871#issuecomment-346888363
    29  func setNetBSDRlimit() error {
    30  	limit := unix.Rlimit{
    31  		Cur: unix.RLIM_INFINITY,
    32  		Max: unix.RLIM_INFINITY,
    33  	}
    34  	if err := unix.Setrlimit(unix.RLIMIT_DATA, &limit); err != nil && os.Getuid() == 0 {
    35  		return err
    36  	}
    37  	return nil
    38  }
    39  
    40  // setOpenBSDRlimit sets limits for OpenBSD.
    41  // See https://go-review.googlesource.com/c/go/+/81876
    42  func setOpenBSDRlimit() error {
    43  	var lim unix.Rlimit
    44  	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &lim); err != nil {
    45  		return fmt.Errorf("getting initial rlimit: %v", err)
    46  	}
    47  	log.Printf("initial NOFILE rlimit: %+v", lim)
    48  
    49  	lim.Cur = 32 << 10
    50  	lim.Max = 32 << 10
    51  	if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &lim); err != nil && os.Getuid() == 0 {
    52  		return fmt.Errorf("Setrlimit: %v", err)
    53  	}
    54  
    55  	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &lim); err != nil {
    56  		return fmt.Errorf("getting updated rlimit: %v", err)
    57  	}
    58  	log.Printf("updated NOFILE rlimit: %+v", lim)
    59  
    60  	return nil
    61  }