How to install ulimit -n from golang program?

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 #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux george@george-Not-Specified ~/work/odesk/progium/trial $ 

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 #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux george@george-Not-Specified ~/work/odesk/progium/trial $ go version go version devel +7c42cfa28e24 Tue Jul 30 14:22:14 2013 +1000 linux/386 
+7
go system-calls operating-system network-programming ulimit
source share
1 answer

It works as expected.

setrlimit (2) .

The soft limit is the value that the kernel provides for the corresponding resource. A hard limit acts like a ceiling for a soft limit: an unprivileged process can only set its soft limit to a value in the range from 0 to the hard limit and the (irreversibly) hard limit. A privileged process (under Linux: one with CAP_SYS_RESOURCE) can arbitrarily change either the limit value.

rlimit.go :

 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) } 

Output:

 $ uname -a Linux peterSO 3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux $ go build rlimit.go $ ./rlimit {1024 4096} Error Setting Rlimit operation not permitted Rlimit Final {1024 4096} $ sudo ./rlimit [sudo] password for peterSO: {1024 4096} Rlimit Final {999999 999999} 

UPDATE:

I successfully executed rlimit.go for linux/amd64 , you failed to execute linux/386 . There were Go errors in Getrlimit and Setrlimit for 32-bit Linux distributions. These bugs are fixed.

Using the Go default tip branch (to enable bug fixes), run the following and update your question with the results.

 $ uname -a Linux peterSO 3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:46:08 UTC 2013 i686 i686 i686 GNU/Linux $ go version go version devel +ba52f6399462 Thu Jul 25 09:56:06 2013 -0400 linux/386 $ ulimit -Sn 1024 $ ulimit -Hn 4096 $ go build rlimit.go $ ./rlimit {1024 4096} Error Setting Rlimit operation not permitted Rlimit Final {1024 4096} $ sudo ./rlimit [sudo] password for peterSO: {1024 4096} Rlimit Final {999999 999999} $ 
+14
source share

All Articles