POSIX stack size limiting

This commit is contained in:
Ștefan Talpalaru 2020-06-25 19:01:01 +02:00
parent 152eb1b58c
commit d5e3c44917
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
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."