mirror of
https://github.com/jech/galene.git
synced 2024-11-22 16:45:58 +01:00
44 lines
679 B
Go
44 lines
679 B
Go
// +build !windows
|
|
// +build !plan9
|
|
|
|
package limit
|
|
|
|
import (
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func Nofile(desired uint64) (uint64, error) {
|
|
var old unix.Rlimit
|
|
|
|
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &old)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if old.Cur >= desired {
|
|
return old.Cur, nil
|
|
}
|
|
|
|
if old.Max < desired {
|
|
limit := unix.Rlimit{
|
|
Cur: desired,
|
|
Max: desired,
|
|
}
|
|
err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit)
|
|
if err == nil {
|
|
return limit.Cur, nil
|
|
}
|
|
|
|
desired = old.Max
|
|
}
|
|
|
|
limit := unix.Rlimit{
|
|
Cur: desired,
|
|
Max: old.Max,
|
|
}
|
|
err = unix.Setrlimit(unix.RLIMIT_NOFILE, &limit)
|
|
if err != nil {
|
|
return old.Cur, err
|
|
}
|
|
return limit.Cur, nil
|
|
}
|