go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/simul/platform/fd_unix.go (about)

     1  //go:build !windows && !darwin
     2  // +build !windows,!darwin
     3  
     4  package platform
     5  
     6  import (
     7  	"syscall"
     8  
     9  	"go.dedis.ch/onet/v3/log"
    10  	"golang.org/x/xerrors"
    11  )
    12  
    13  // By default in simulation we update the per-process file descriptor limit
    14  // to the maximal limit.
    15  func init() {
    16  	var rLimit syscall.Rlimit
    17  	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    18  	if err != nil {
    19  		log.Fatal("Error Getting Rlimit ", err)
    20  	}
    21  
    22  	if rLimit.Cur < rLimit.Max {
    23  		rLimit.Cur = rLimit.Max
    24  		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    25  		if err != nil {
    26  			log.Warn("Error Setting Rlimit:", err)
    27  		}
    28  	}
    29  
    30  	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    31  	if err != nil {
    32  		log.Error("Couldn't raise Rlimit: " + err.Error())
    33  	}
    34  }
    35  
    36  // CheckOutOfFileDescriptors tries to duplicate the stdout file descriptor
    37  // and throws an error if it cannot do it. This is a horrible hack mainly for
    38  // MacOSX where the file descriptor limit is quite low and we need to tell
    39  // people running simulations what they can do about it.
    40  func CheckOutOfFileDescriptors() error {
    41  	// Check if we're out of file descriptors
    42  	newFS, err := syscall.Dup(syscall.Stdout)
    43  	if err != nil {
    44  		return xerrors.New(`Out of file descriptors. You might want to do something like this for Mac OSX:
    45      sudo sysctl -w kern.maxfiles=122880
    46      sudo sysctl -w kern.maxfilesperproc=102400
    47      sudo sysctl -w kern.ipc.somaxconn=20480`)
    48  	}
    49  	if err = syscall.Close(newFS); err != nil {
    50  		return xerrors.New("Couldn't close new file descriptor: " + err.Error())
    51  	}
    52  	return nil
    53  }