How to set ulimit -n from a golang program?

前端 未结 1 1713
醉话见心
醉话见心 2021-01-31 12:12

My purspose was to set ulimit -n from within a golang program so that I do not have to set it globally but restrict it within the program.

Found systemcalls setrlimit and

相关标签:
1条回答
  • 2021-01-31 13:04

    It works as expected.

    setrlimit(2).

    The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process (under Linux: one with the CAP_SYS_RESOURCE capability) may make arbitrary changes to either 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 ran rlimit.go for linux/amd64, you failed for linux/386. There were a Go bugs in Getrlimit and Setrlimit for Linux 32-bit distributions. These bugs have been fixed.

    Using the Go default branch tip (to include the 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}
    $ 
    
    0 讨论(0)
提交回复
热议问题