| 1 | // FreeBSD ulimit handling via sysctl. |
| 2 | //go:build freebsd |
| 3 | |
| 4 | package util |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "math" |
| 9 | |
| 10 | unix "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 13 | func init() { |
| 14 | supportsFDManagement = true |
| 15 | getLimit = freebsdGetLimit |
| 16 | setLimit = freebsdSetLimit |
| 17 | } |
| 18 | |
| 19 | func freebsdGetLimit() (uint64, uint64, error) { |
| 20 | rlimit := unix.Rlimit{} |
| 21 | err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit) |
| 22 | if (rlimit.Cur < 0) || (rlimit.Max < 0) { |
| 23 | return 0, 0, errors.New("invalid rlimits") |
| 24 | } |
| 25 | return uint64(rlimit.Cur), uint64(rlimit.Max), err |
| 26 | } |
| 27 | |
| 28 | func freebsdSetLimit(soft uint64, max uint64) error { |
| 29 | if (soft > math.MaxInt64) || (max > math.MaxInt64) { |
| 30 | return errors.New("invalid rlimits") |
| 31 | } |
| 32 | rlimit := unix.Rlimit{ |
| 33 | Cur: int64(soft), |
| 34 | Max: int64(max), |
| 35 | } |
| 36 | return unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit) |
| 37 | } |