2020-03-22 21:08:22 +00:00
|
|
|
package ulimit
|
|
|
|
|
|
|
|
// from go-ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
2021-04-06 13:04:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-03-22 21:08:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("ulimit")
|
|
|
|
|
|
|
|
var (
|
|
|
|
supportsFDManagement = false
|
|
|
|
|
|
|
|
// getlimit returns the soft and hard limits of file descriptors counts
|
2021-08-17 12:39:36 +00:00
|
|
|
GetLimit func() (uint64, uint64, error)
|
2020-03-22 21:08:22 +00:00
|
|
|
// set limit sets the soft and hard limits of file descriptors counts
|
|
|
|
setLimit func(uint64, uint64) error
|
|
|
|
)
|
|
|
|
|
|
|
|
// minimum file descriptor limit before we complain
|
|
|
|
const minFds = 2048
|
|
|
|
|
|
|
|
// default max file descriptor limit.
|
|
|
|
const maxFds = 16 << 10
|
|
|
|
|
2020-08-20 19:52:50 +00:00
|
|
|
// userMaxFDs returns the value of LOTUS_FD_MAX
|
2020-03-22 21:08:22 +00:00
|
|
|
func userMaxFDs() uint64 {
|
2020-08-20 19:52:50 +00:00
|
|
|
// check if the LOTUS_FD_MAX is set up and if it does
|
2020-03-22 21:08:22 +00:00
|
|
|
// not have a valid fds number notify the user
|
2020-08-20 19:52:50 +00:00
|
|
|
val := os.Getenv("LOTUS_FD_MAX")
|
|
|
|
if val == "" {
|
|
|
|
val = os.Getenv("IPFS_FD_MAX")
|
|
|
|
}
|
|
|
|
|
|
|
|
if val != "" {
|
2020-03-22 21:08:22 +00:00
|
|
|
fds, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
2020-08-20 19:52:50 +00:00
|
|
|
log.Errorf("bad value for LOTUS_FD_MAX: %s", err)
|
2020-03-22 21:08:22 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return fds
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// ManageFdLimit raise the current max file descriptor count
|
2020-08-20 19:52:50 +00:00
|
|
|
// of the process based on the LOTUS_FD_MAX value
|
2020-03-22 21:08:22 +00:00
|
|
|
func ManageFdLimit() (changed bool, newLimit uint64, err error) {
|
|
|
|
if !supportsFDManagement {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
targetLimit := uint64(maxFds)
|
|
|
|
userLimit := userMaxFDs()
|
|
|
|
if userLimit > 0 {
|
|
|
|
targetLimit = userLimit
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:39:36 +00:00
|
|
|
soft, hard, err := GetLimit()
|
2020-03-22 21:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetLimit <= soft {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the soft limit is the value that the kernel enforces for the
|
|
|
|
// corresponding resource
|
|
|
|
// the hard limit acts as a ceiling for the soft limit
|
|
|
|
// an unprivileged process may only set it's soft limit to a
|
|
|
|
// alue in the range from 0 up to the hard limit
|
|
|
|
err = setLimit(targetLimit, targetLimit)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
newLimit = targetLimit
|
|
|
|
case syscall.EPERM:
|
|
|
|
// lower limit if necessary.
|
|
|
|
if targetLimit > hard {
|
|
|
|
targetLimit = hard
|
|
|
|
}
|
|
|
|
|
|
|
|
// the process does not have permission so we should only
|
|
|
|
// set the soft value
|
|
|
|
err = setLimit(targetLimit, hard)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
newLimit = targetLimit
|
|
|
|
|
|
|
|
// Warn on lowered limit.
|
|
|
|
|
|
|
|
if newLimit < userLimit {
|
|
|
|
err = fmt.Errorf(
|
2020-08-20 19:52:50 +00:00
|
|
|
"failed to raise ulimit to LOTUS_FD_MAX (%d): set to %d",
|
2020-03-22 21:08:22 +00:00
|
|
|
userLimit,
|
|
|
|
newLimit,
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if userLimit == 0 && newLimit < minFds {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"failed to raise ulimit to minimum %d: set to %d",
|
|
|
|
minFds,
|
|
|
|
newLimit,
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("error setting: ulimit: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newLimit > 0, newLimit, err
|
|
|
|
}
|