fixed test of raised fd limits
Raising FD limits was erroring when the OS's max was at the maximum signed integer value. Switched the code to using uint64 instead of int64. fixed #5495 License: MIT Signed-off-by: Rob Deutsch <rdeutschob@gmail.com>
rob-deutsch committed
Sep 20, 2018 at 17:41 UTC
182507d0d1e35c4325f83080d9d605ce623f6c29
3 files changed
+26
-19
cmd/ipfs/util/ulimit.go
+7
-9
@@ -16,9 +16,9 @@ var (
16
supportsFDManagement = false
17
18
// getlimit returns the soft and hard limits of file descriptors counts
19
- getLimit func() (int64, int64, error)
19
+ getLimit func() (uint64, uint64, error)
20
// set limit sets the soft and hard limits of file descriptors counts
21
- setLimit func(int64, int64) error
21
+ setLimit func(uint64, uint64) error
22
)
23
24
// maxFds is the maximum number of file descriptors that go-ipfs
@@ -56,9 +56,7 @@ func ManageFdLimit() error {
56
return err
57
}
58
59
- max := int64(maxFds)
60
-
61
- if max <= soft {
59
+ if maxFds <= soft {
60
return nil
61
}
62
@@ -67,25 +65,25 @@ func ManageFdLimit() error {
65
// the hard limit acts as a ceiling for the soft limit
66
// an unprivileged process may only set it's soft limit to a
67
// alue in the range from 0 up to the hard limit
70
- if err = setLimit(max, max); err != nil {
68
+ if err = setLimit(maxFds, maxFds); err != nil {
69
if err != syscall.EPERM {
70
return fmt.Errorf("error setting: ulimit: %s", err)
71
}
72
73
// the process does not have permission so we should only
74
// set the soft value
77
- if max > hard {
75
+ if maxFds > hard {
76
return errors.New(
77
"cannot set rlimit, IPFS_FD_MAX is larger than the hard limit",
78
)
79
}
80
83
- if err = setLimit(max, hard); err != nil {
81
+ if err = setLimit(maxFds, hard); err != nil {
82
return fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
83
}
84
}
85
88
- fmt.Printf("Successfully raised file descriptor limit to %d.\n", max)
86
+ fmt.Printf("Successfully raised file descriptor limit to %d.\n", maxFds)
87
88
return nil
89
}
cmd/ipfs/util/ulimit_freebsd.go
+14
-5
@@ -3,6 +3,9 @@
3
package util
4
5
import (
6
+ "errors"
7
+ "math"
8
+
9
unix "gx/ipfs/QmVGjyM9i2msKvLXwh9VosCTgP4mL91kC7hDmqnwTTx6Hu/sys/unix"
10
)
11
@@ -12,16 +15,22 @@ func init() {
15
setLimit = freebsdSetLimit
16
}
17
15
-func freebsdGetLimit() (int64, int64, error) {
18
+func freebsdGetLimit() (uint64, uint64, error) {
19
rlimit := unix.Rlimit{}
20
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
18
- return rlimit.Cur, rlimit.Max, err
21
+ if (rlimit.Cur < 0) || (rlimit.Max < 0) {
22
+ return 0, 0, errors.New("invalid rlimits")
23
+ }
24
+ return uint64(rlimit.Cur), uint64(rlimit.Max), err
25
}
26
21
-func freebsdSetLimit(soft int64, max int64) error {
27
+func freebsdSetLimit(soft uint64, max uint64) error {
28
+ if (soft > math.MaxInt64) || (max > math.MaxInt64) {
29
+ return errors.New("invalid rlimits")
30
+ }
31
rlimit := unix.Rlimit{
23
- Cur: soft,
24
- Max: max,
32
+ Cur: int64(soft),
33
+ Max: int64(max),
34
}
35
return unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
36
}
cmd/ipfs/util/ulimit_unix.go
+5
-5
@@ -12,16 +12,16 @@ func init() {
12
setLimit = unixSetLimit
13
}
14
15
-func unixGetLimit() (int64, int64, error) {
15
+func unixGetLimit() (uint64, uint64, error) {
16
rlimit := unix.Rlimit{}
17
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
18
- return int64(rlimit.Cur), int64(rlimit.Max), err
18
+ return rlimit.Cur, rlimit.Max, err
19
}
20
21
-func unixSetLimit(soft int64, max int64) error {
21
+func unixSetLimit(soft uint64, max uint64) error {
22
rlimit := unix.Rlimit{
23
- Cur: uint64(soft),
24
- Max: uint64(max),
23
+ Cur: soft,
24
+ Max: max,
25
}
26
return unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
27
}