From 63160855aade7fd64755e4bdc78bf4694513df50 Mon Sep 17 00:00:00 2001 From: aguspiza Date: Thu, 12 Apr 2018 17:42:33 +0200 Subject: [PATCH] Move RLimit and getrlimit to posix.nim and add setrlimit to easily limit FD allocation (#7564) --- lib/posix/linux.nim | 2 +- lib/posix/posix.nim | 16 ++++++++++++++++ lib/pure/ioselects/ioselectors_epoll.nim | 8 -------- lib/pure/ioselects/ioselectors_poll.nim | 10 +--------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/posix/linux.nim b/lib/posix/linux.nim index 8786ab5356..6f9f75e340 100644 --- a/lib/posix/linux.nim +++ b/lib/posix/linux.nim @@ -27,4 +27,4 @@ proc clone*(fn: pointer; child_stack: pointer; flags: cint; arg: pointer; ptid: ptr Pid; tls: pointer; ctid: ptr Pid): cint {.importc, header: "".} -proc pipe2*(a: array[0..1, cint], flags: cint): cint {.importc, header: "".} \ No newline at end of file +proc pipe2*(a: array[0..1, cint], flags: cint): cint {.importc, header: "".} diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index fba35868ca..a23d760509 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -973,3 +973,19 @@ template onSignal*(signals: varargs[cint], body: untyped) = proc (sig: cint) {.noconv.} = body ) + +type + RLimit* {.importc: "struct rlimit", + header: "", 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: "".} + ## The setrlimit() system calls sets resource limits. + +proc getrlimit*(resource: cint, rlp: var RLimit): cint + {.importc: "getrlimit",header: "".} + ## The getrlimit() system call gets resource limits. diff --git a/lib/pure/ioselects/ioselectors_epoll.nim b/lib/pure/ioselects/ioselectors_epoll.nim index 65efeabab3..36145abc72 100644 --- a/lib/pure/ioselects/ioselectors_epoll.nim +++ b/lib/pure/ioselects/ioselectors_epoll.nim @@ -48,14 +48,6 @@ when not defined(android): proc signalfd(fd: cint, mask: var Sigset, flags: cint): cint {.cdecl, importc: "signalfd", header: "".} -type - RLimit {.importc: "struct rlimit", - header: "", pure, final.} = object - rlim_cur: int - rlim_max: int -proc getrlimit(resource: cint, rlp: var RLimit): cint - {.importc: "getrlimit",header: "".} - when hasThreadSupport: type SelectorImpl[T] = object diff --git a/lib/pure/ioselects/ioselectors_poll.nim b/lib/pure/ioselects/ioselectors_poll.nim index 081c57fa68..c36750c8d2 100644 --- a/lib/pure/ioselects/ioselectors_poll.nim +++ b/lib/pure/ioselects/ioselectors_poll.nim @@ -40,14 +40,6 @@ type wfd: cint SelectEvent* = ptr SelectEventImpl -type - rlimit {.importc: "struct rlimit", - header: "", pure, final.} = object - rlim_cur: int - rlim_max: int -proc getrlimit(resource: cint, rlp: var rlimit): cint - {.importc: "getrlimit",header: "".} - when hasThreadSupport: template withPollLock[T](s: Selector[T], body: untyped) = acquire(s.lock) @@ -61,7 +53,7 @@ else: body proc newSelector*[T](): Selector[T] = - var a = rlimit() + var a = RLimit() if getrlimit(posix.RLIMIT_NOFILE, a) != 0: raiseIOSelectorsError(osLastError()) var maxFD = int(a.rlim_max)