Move RLimit and getrlimit to posix.nim and add setrlimit to easily limit FD allocation (#7564)
This commit is contained in:
parent
85e21158db
commit
63160855aa
4 changed files with 18 additions and 18 deletions
|
|
@ -27,4 +27,4 @@ proc clone*(fn: pointer; child_stack: pointer; flags: cint;
|
||||||
arg: pointer; ptid: ptr Pid; tls: pointer;
|
arg: pointer; ptid: ptr Pid; tls: pointer;
|
||||||
ctid: ptr Pid): cint {.importc, header: "<sched.h>".}
|
ctid: ptr Pid): cint {.importc, header: "<sched.h>".}
|
||||||
|
|
||||||
proc pipe2*(a: array[0..1, cint], flags: cint): cint {.importc, header: "<unistd.h>".}
|
proc pipe2*(a: array[0..1, cint], flags: cint): cint {.importc, header: "<unistd.h>".}
|
||||||
|
|
|
||||||
|
|
@ -973,3 +973,19 @@ template onSignal*(signals: varargs[cint], body: untyped) =
|
||||||
proc (sig: cint) {.noconv.} =
|
proc (sig: cint) {.noconv.} =
|
||||||
body
|
body
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type
|
||||||
|
RLimit* {.importc: "struct rlimit",
|
||||||
|
header: "<sys/resource.h>", pure, final.} = object
|
||||||
|
rlim_cur*: int
|
||||||
|
rlim_max*: int
|
||||||
|
## The getrlimit() and setrlimit() system calls get and set resource limits respectively.
|
||||||
|
## Each resource has an associated soft and hard limit, as defined by the RLimit structure
|
||||||
|
|
||||||
|
proc setrlimit*(resource: cint, rlp: var RLimit): cint
|
||||||
|
{.importc: "setrlimit",header: "<sys/resource.h>".}
|
||||||
|
## The setrlimit() system calls sets resource limits.
|
||||||
|
|
||||||
|
proc getrlimit*(resource: cint, rlp: var RLimit): cint
|
||||||
|
{.importc: "getrlimit",header: "<sys/resource.h>".}
|
||||||
|
## The getrlimit() system call gets resource limits.
|
||||||
|
|
|
||||||
|
|
@ -48,14 +48,6 @@ when not defined(android):
|
||||||
proc signalfd(fd: cint, mask: var Sigset, flags: cint): cint
|
proc signalfd(fd: cint, mask: var Sigset, flags: cint): cint
|
||||||
{.cdecl, importc: "signalfd", header: "<sys/signalfd.h>".}
|
{.cdecl, importc: "signalfd", header: "<sys/signalfd.h>".}
|
||||||
|
|
||||||
type
|
|
||||||
RLimit {.importc: "struct rlimit",
|
|
||||||
header: "<sys/resource.h>", pure, final.} = object
|
|
||||||
rlim_cur: int
|
|
||||||
rlim_max: int
|
|
||||||
proc getrlimit(resource: cint, rlp: var RLimit): cint
|
|
||||||
{.importc: "getrlimit",header: "<sys/resource.h>".}
|
|
||||||
|
|
||||||
when hasThreadSupport:
|
when hasThreadSupport:
|
||||||
type
|
type
|
||||||
SelectorImpl[T] = object
|
SelectorImpl[T] = object
|
||||||
|
|
|
||||||
|
|
@ -40,14 +40,6 @@ type
|
||||||
wfd: cint
|
wfd: cint
|
||||||
SelectEvent* = ptr SelectEventImpl
|
SelectEvent* = ptr SelectEventImpl
|
||||||
|
|
||||||
type
|
|
||||||
rlimit {.importc: "struct rlimit",
|
|
||||||
header: "<sys/resource.h>", pure, final.} = object
|
|
||||||
rlim_cur: int
|
|
||||||
rlim_max: int
|
|
||||||
proc getrlimit(resource: cint, rlp: var rlimit): cint
|
|
||||||
{.importc: "getrlimit",header: "<sys/resource.h>".}
|
|
||||||
|
|
||||||
when hasThreadSupport:
|
when hasThreadSupport:
|
||||||
template withPollLock[T](s: Selector[T], body: untyped) =
|
template withPollLock[T](s: Selector[T], body: untyped) =
|
||||||
acquire(s.lock)
|
acquire(s.lock)
|
||||||
|
|
@ -61,7 +53,7 @@ else:
|
||||||
body
|
body
|
||||||
|
|
||||||
proc newSelector*[T](): Selector[T] =
|
proc newSelector*[T](): Selector[T] =
|
||||||
var a = rlimit()
|
var a = RLimit()
|
||||||
if getrlimit(posix.RLIMIT_NOFILE, a) != 0:
|
if getrlimit(posix.RLIMIT_NOFILE, a) != 0:
|
||||||
raiseIOSelectorsError(osLastError())
|
raiseIOSelectorsError(osLastError())
|
||||||
var maxFD = int(a.rlim_max)
|
var maxFD = int(a.rlim_max)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue