Merge pull request #46 from status-im/rlimits

POSIX stack size limiting
This commit is contained in:
Ștefan Talpalaru 2020-06-25 19:17:21 +02:00 committed by GitHub
commit 5b28c5f694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

29
stew/rlimits.nim Normal file
View File

@ -0,0 +1,29 @@
# This module should be globally imported from a top-level "config.nims":
#
# const stack_size {.intdefine.}: int = 0
# when defined(stack_size):
# when defined(posix):
# switch("import", "stew/rlimits")
{.used.}
const
RLIMIT_STACK = 3 # from "/usr/include/bits/resource.h"
stack_size {.intdefine.}: int = 0
## Set the stack size limit on POSIX systems (the Windows one should be set at
## compile time, using GCC options, elsewhere).
when defined(stack_size) and defined(posix) and not (defined(nimscript) or defined(js)):
import os, posix
var rlimit: RLimit
if getrlimit(RLIMIT_STACK, rlimit) == -1:
stderr.writeLine("getrlimit() error: ", osErrorMsg(osLastError()))
else:
rlimit.rlim_cur = stack_size
if setrlimit(RLIMIT_STACK, rlimit) == -1:
stderr.writeLine("setrlimit() error: ", osErrorMsg(osLastError()))
else:
echo "Stack size successfully limited to ", stack_size, " bytes."