My task was to install ulimit -n from the golang program, so that I did not need to install it globally, but limit it within the program.
Found setrlimit system tables and get rlimit for the same. ( http://linux.die.net/man/2/setrlimit )
But when I tried the sample program for the same, I received an error message with an invalid argument when setting the value.
package main import ( "fmt" "syscall" ) func main() { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Getting Rlimit ", err) } fmt.Println(rLimit) rLimit.Max = 999999 rLimit.Cur = 999999 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Setting Rlimit ", err) } err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Getting Rlimit ", err) } fmt.Println("Rlimit Final", rLimit) }
The result obtained:
george@george-Not-Specified ~/work/odesk/progium/trial $ ./getRlimit {4294963002032703 0} Error Setting Rlimit invalid argument Rlimit Final {4294963002032703 999999} george@george-Not-Specified ~/work/odesk/progium/trial $ sudo ./getRlimit [sudo] password for george: {4294963002032703 0} Error Setting Rlimit invalid argument Rlimit Final {4294963002032703 999999} george@george-Not-Specified ~/work/odesk/progium/trial $ uname -a Linux george-Not-Specified 3.5.0-17-generic
So, I was able to get rlimit Failed to set the limit and return an error. Although this failed, the MAX value changed when I accepted the value again, but the CUR value remains unchanged. Can this error occur due to some kind of problem with my kernel or is it a bad program? Where can I find additional information and how to deal with such a problem?
Update:
It works after the fix.
george@george-Not-Specified ~/work/odesk/progium/trial $ go build getRlimit.go george@george-Not-Specified ~/work/odesk/progium/trial $ ./getRlimit {1024 4096} Error Setting Rlimit operation not permitted Rlimit Final {1024 4096} george@george-Not-Specified ~/work/odesk/progium/trial $ sudo ./getRlimit [sudo] password for george: {1024 4096} Rlimit Final {999999 999999} george@george-Not-Specified ~/work/odesk/progium/trial $ uname -a Linux george-Not-Specified 3.5.0-17-generic
go system-calls operating-system network-programming ulimit
George Thomas
source share